com.oracle.truffle.api.LanguageAccessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of truffle-api Show documentation
Show all versions of truffle-api Show documentation
Truffle is a multi-language framework for executing dynamic languages
that achieves high performance when combined with Graal.
/*
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.oracle.truffle.api;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import org.graalvm.options.OptionDescriptor;
import org.graalvm.options.OptionDescriptors;
import org.graalvm.options.OptionValues;
import org.graalvm.polyglot.io.FileSystem;
import com.oracle.truffle.api.TruffleLanguage.ContextLocalFactory;
import com.oracle.truffle.api.TruffleLanguage.ContextThreadLocalFactory;
import com.oracle.truffle.api.TruffleLanguage.Env;
import com.oracle.truffle.api.TruffleLogger.LoggerCache;
import com.oracle.truffle.api.frame.Frame;
import com.oracle.truffle.api.frame.MaterializedFrame;
import com.oracle.truffle.api.impl.Accessor;
import com.oracle.truffle.api.nodes.ExecutableNode;
import com.oracle.truffle.api.nodes.LanguageInfo;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.RootNode;
import com.oracle.truffle.api.source.Source;
import com.oracle.truffle.api.source.SourceSection;
final class LanguageAccessor extends Accessor {
static final LanguageAccessor ACCESSOR = new LanguageAccessor();
static final NodeSupport NODES = ACCESSOR.nodeSupport();
static final SourceSupport SOURCE = ACCESSOR.sourceSupport();
static final InstrumentSupport INSTRUMENT = ACCESSOR.instrumentSupport();
static final JDKSupport JDK = ACCESSOR.jdkSupport();
static final EngineSupport ENGINE = ACCESSOR.engineSupport();
static final InteropSupport INTEROP = ACCESSOR.interopSupport();
static final RuntimeSupport RUNTIME = ACCESSOR.runtimeSupport();
private LanguageAccessor() {
}
static EngineSupport engineAccess() {
return ACCESSOR.engineSupport();
}
static InstrumentSupport instrumentAccess() {
return ACCESSOR.instrumentSupport();
}
static NodeSupport nodesAccess() {
return ACCESSOR.nodeSupport();
}
static InteropSupport interopAccess() {
return ACCESSOR.interopSupport();
}
static ExceptionSupport exceptionAccess() {
return ACCESSOR.exceptionSupport();
}
static IOSupport ioAccess() {
return ACCESSOR.ioSupport();
}
static JDKSupport jdkServicesAccessor() {
return ACCESSOR.jdkSupport();
}
static final class LanguageImpl extends LanguageSupport {
@Override
public boolean isTruffleStackTrace(Throwable t) {
return t instanceof TruffleStackTrace.LazyStackTrace;
}
@Override
public StackTraceElement[] getInternalStackTraceElements(Throwable t) {
TruffleStackTrace trace = ((TruffleStackTrace.LazyStackTrace) t).getInternalStackTrace();
if (trace == null) {
return new StackTraceElement[0];
} else {
return trace.getInternalStackTrace();
}
}
@Override
public void materializeHostFrames(Throwable original) {
TruffleStackTrace.materializeHostFrames(original);
}
@Override
public InstrumentInfo createInstrument(Object polyglotInstrument, String id, String name, String version) {
return new InstrumentInfo(polyglotInstrument, id, name, version);
}
@Override
public Object getPolyglotInstrument(InstrumentInfo info) {
return info.getPolyglotInstrument();
}
@SuppressWarnings("unchecked")
@Override
public void initializeLanguage(TruffleLanguage> impl, LanguageInfo language, Object polyglotLanguage, Object polyglotLanguageInstance) {
impl.languageInfo = language;
impl.polyglotLanguageInstance = polyglotLanguageInstance;
if (impl.contextLocals == null) {
impl.contextLocals = Collections.emptyList();
} else {
ENGINE.initializeLanguageContextLocal(impl.contextLocals, polyglotLanguageInstance);
impl.contextLocals = Collections.unmodifiableList(impl.contextLocals);
}
if (impl.contextThreadLocals == null) {
impl.contextThreadLocals = Collections.emptyList();
} else {
ENGINE.initializeLanguageContextThreadLocal(impl.contextThreadLocals, polyglotLanguageInstance);
impl.contextThreadLocals = Collections.unmodifiableList(impl.contextThreadLocals);
}
}
@SuppressWarnings("deprecation")
@Override
public boolean initializeMultiContext(TruffleLanguage> language) {
language.initializeMultipleContexts();
return language.initializeMultiContext();
}
@Override
public Object getContext(TruffleLanguage.Env env) {
Object c = env.getLanguageContext();
if (c != TruffleLanguage.Env.UNSET_CONTEXT) {
return c;
} else {
return null;
}
}
@Override
public Object getPolyglotLanguageContext(Env env) {
return env.getPolyglotLanguageContext();
}
@Override
public Object getFileSystemContext(TruffleFile file) {
return file.getFileSystemContext();
}
@Override
public Object getLanguageView(Env env, Object value) {
Object c = env.getLanguageContext();
if (c == TruffleLanguage.Env.UNSET_CONTEXT) {
CompilerDirectives.transferToInterpreter();
return null;
} else {
Object result = env.getSpi().getLanguageView(c, value);
if (result == null) {
return LanguageAccessor.engineAccess().getDefaultLanguageView(env.spi, c, value);
} else {
return result;
}
}
}
@Override
@SuppressWarnings("deprecation")
public Object getLegacyScopedView(Env env, Node location, Frame frame, Object value) {
Object c = env.getLanguageContext();
if (c == TruffleLanguage.Env.UNSET_CONTEXT) {
CompilerDirectives.transferToInterpreter();
return value;
} else {
return env.getSpi().getScopedView(c, location, frame, value);
}
}
@Override
public Object getScope(Env env) {
Object c = env.getLanguageContext();
if (c == TruffleLanguage.Env.UNSET_CONTEXT) {
CompilerDirectives.transferToInterpreter();
return null;
} else {
Object result = env.getSpi().getScope(c);
assert ACCESSOR.interopSupport().isScopeObject(result) : String.format("%s is not a scope", result);
return result;
}
}
@Override
public Object getPolyglotContext(TruffleContext context) {
return context.polyglotContext;
}
@SuppressWarnings("unchecked")
@Override
public Object invokeContextLocalFactory(Object factory, Object contextImpl) {
Object result = ((ContextLocalFactory