org.pkl.thirdparty.truffle.api.LanguageAccessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pkl-config-java-all Show documentation
Show all versions of pkl-config-java-all Show documentation
Shaded fat Jar for pkl-config-java, a Java config library based on the Pkl config language.
/*
* Copyright (c) 2014, 2023, 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 org.pkl.thirdparty.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.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import org.pkl.thirdparty.graalvm.options.OptionDescriptor;
import org.pkl.thirdparty.graalvm.options.OptionDescriptors;
import org.pkl.thirdparty.graalvm.options.OptionValues;
import org.pkl.thirdparty.graalvm.polyglot.SandboxPolicy;
import org.pkl.thirdparty.graalvm.polyglot.io.FileSystem;
import org.pkl.thirdparty.truffle.api.TruffleLanguage.ContextLocalFactory;
import org.pkl.thirdparty.truffle.api.TruffleLanguage.ContextThreadLocalFactory;
import org.pkl.thirdparty.truffle.api.TruffleLanguage.Env;
import org.pkl.thirdparty.truffle.api.TruffleLogger.LoggerCache;
import org.pkl.thirdparty.truffle.api.frame.Frame;
import org.pkl.thirdparty.truffle.api.frame.MaterializedFrame;
import org.pkl.thirdparty.truffle.api.impl.Accessor;
import org.pkl.thirdparty.truffle.api.nodes.ExecutableNode;
import org.pkl.thirdparty.truffle.api.nodes.LanguageInfo;
import org.pkl.thirdparty.truffle.api.nodes.Node;
import org.pkl.thirdparty.truffle.api.nodes.RootNode;
import org.pkl.thirdparty.truffle.api.source.Source;
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 EngineSupport ENGINE = ACCESSOR.engineSupport();
static final InteropSupport INTEROP = ACCESSOR.interopSupport();
static final RuntimeSupport RUNTIME = ACCESSOR.runtimeSupport();
static final ExceptionSupport EXCEPTIONS = ACCESSOR.exceptionSupport();
static final HostSupport HOST = ACCESSOR.hostSupport();
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 IOSupport ioAccess() {
return ACCESSOR.ioSupport();
}
static HostSupport hostAccess() {
return ACCESSOR.hostSupport();
}
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 Throwable getOrCreateLazyStackTrace(Throwable t) {
return TruffleStackTrace.getOrCreateLazyStackTrace(t);
}
@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 (polyglotLanguageInstance != null) {
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 void initializeMultiContext(TruffleLanguage> language) {
language.initializeMultipleContexts();
}
@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.transferToInterpreterAndInvalidate();
return null;
} else {
Object result = env.getSpi().getLanguageView(c, value);
if (result == null) {
return LanguageAccessor.engineAccess().getDefaultLanguageView(env.spi, value);
} else {
return result;
}
}
}
@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 result == null || 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
© 2015 - 2024 Weber Informatics LLC | Privacy Policy