com.oracle.truffle.api.TruffleContext Maven / Gradle / Ivy
Show all versions of truffle-api Show documentation
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.truffle.api;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.TruffleLanguage.AccessAPI;
import com.oracle.truffle.api.TruffleLanguage.ContextReference;
import com.oracle.truffle.api.TruffleLanguage.Env;
/**
* A handle on a context of a set of Truffle languages. This context handle is designed to be used
* by Truffle guest language implementations. The Truffle context can be used to create inner
* contexts for isolated execution of guest language code.
*
* A {@link TruffleContext context} consists of a {@link TruffleLanguage#createContext(Env) language
* context} instance for each {@link Env#getLanguages() installed language}. The current language
* context is {@link TruffleLanguage#createContext(Env) created} eagerly and can be accessed using a
* {@link ContextReference context reference} or statically with
* {@link TruffleLanguage#getCurrentContext(Class)} after the context was
* {@link TruffleContext#enter() entered}.
*
* The configuration for each language context is inherited from its parent/creator context. In
* addition to that {@link Builder#config(String, Object) config} parameters can be passed to new
* language context instance of the current language. The configuration of other installed languages
* cannot be modified. To run guest language code in a context, the context needs to be first
* {@link #enter() entered} and then {@link #leave(Object) left}. The context should be
* {@link #close() closed} when it is no longer needed. If the context is not closed explicitly,
* then it is automatically closed together with the parent context.
*
* Example usage: {@link TruffleContextSnippets#executeInContext}
*
* @since 0.27
*/
public final class TruffleContext implements AutoCloseable {
static final TruffleContext EMPTY = new TruffleContext();
private static ThreadLocal> assertStack;
final Object impl;
final boolean closeable;
TruffleContext(Object impl) {
this.impl = impl;
this.closeable = false;
}
private TruffleContext(TruffleLanguage.Env env, Map config) {
this.impl = AccessAPI.engineAccess().createInternalContext(env.getVMObject(), config, this);
this.closeable = false;
// Initialized after this TruffleContext instance is fully set up
AccessAPI.engineAccess().initializeInternalContext(env.getVMObject(), impl);
}
/**
* Creates closeable context representation for use by a language.
*/
private TruffleContext(Object impl, boolean closeable) {
this.impl = impl;
this.closeable = closeable;
}
private TruffleContext() {
this.impl = null;
this.closeable = false;
}
static {
assert initializeAssertStack();
}
private static boolean initializeAssertStack() {
assertStack = new ThreadLocal>() {
@Override
protected List