Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2017, 2022, 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.polyglot;
import static org.pkl.thirdparty.truffle.api.CompilerDirectives.shouldNotReachHere;
import static org.pkl.thirdparty.truffle.polyglot.EngineAccessor.LANGUAGE;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import org.pkl.thirdparty.graalvm.collections.UnmodifiableEconomicSet;
import org.pkl.thirdparty.graalvm.polyglot.PolyglotAccess;
import org.pkl.thirdparty.graalvm.polyglot.Value;
import org.pkl.thirdparty.graalvm.polyglot.proxy.Proxy;
import org.pkl.thirdparty.truffle.api.CallTarget;
import org.pkl.thirdparty.truffle.api.CompilerDirectives;
import org.pkl.thirdparty.truffle.api.CompilerDirectives.CompilationFinal;
import org.pkl.thirdparty.truffle.api.CompilerDirectives.TruffleBoundary;
import org.pkl.thirdparty.truffle.api.TruffleLanguage;
import org.pkl.thirdparty.truffle.api.TruffleLanguage.Env;
import org.pkl.thirdparty.truffle.api.TruffleLogger;
import org.pkl.thirdparty.truffle.api.dsl.Cached;
import org.pkl.thirdparty.truffle.api.dsl.GenerateCached;
import org.pkl.thirdparty.truffle.api.dsl.GenerateInline;
import org.pkl.thirdparty.truffle.api.dsl.GenerateUncached;
import org.pkl.thirdparty.truffle.api.dsl.NeverDefault;
import org.pkl.thirdparty.truffle.api.dsl.Specialization;
import org.pkl.thirdparty.truffle.api.dsl.Cached.Shared;
import org.pkl.thirdparty.truffle.api.exception.AbstractTruffleException;
import org.pkl.thirdparty.truffle.api.frame.Frame;
import org.pkl.thirdparty.truffle.api.interop.InteropLibrary;
import org.pkl.thirdparty.truffle.api.interop.NodeLibrary;
import org.pkl.thirdparty.truffle.api.interop.UnsupportedMessageException;
import org.pkl.thirdparty.truffle.api.nodes.ExplodeLoop;
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.profiles.InlinedBranchProfile;
import org.pkl.thirdparty.truffle.api.source.Source;
final class PolyglotLanguageContext implements PolyglotImpl.VMObject {
private static final TruffleLogger LOG = TruffleLogger.getLogger(PolyglotEngineImpl.OPTION_GROUP_ENGINE, PolyglotLanguageContext.class);
/*
* Lazily created when a language context is created.
*/
final class Lazy {
final Set activePolyglotThreads;
final Object polyglotGuestBindings;
final Thread.UncaughtExceptionHandler uncaughtExceptionHandler;
@CompilationFinal PolyglotLanguageInstance languageInstance;
@CompilationFinal Map accessibleInternalLanguages;
@CompilationFinal Map accessiblePublicLanguages;
final Object internalFileSystemContext;
final Object publicFileSystemContext;
final ReentrantLock operationLock;
private boolean multipleThreadsInitialized;
Lazy(PolyglotLanguageInstance languageInstance, PolyglotContextConfig config) {
this.languageInstance = languageInstance;
this.activePolyglotThreads = new HashSet<>();
this.polyglotGuestBindings = new PolyglotBindings(PolyglotLanguageContext.this);
this.uncaughtExceptionHandler = new PolyglotUncaughtExceptionHandler();
this.computeAccessPermissions(config);
// file systems are patched after preinitialization internally using a delegate field
this.publicFileSystemContext = EngineAccessor.LANGUAGE.createFileSystemContext(PolyglotLanguageContext.this, config.fileSystemConfig.fileSystem);
this.internalFileSystemContext = EngineAccessor.LANGUAGE.createFileSystemContext(PolyglotLanguageContext.this, config.fileSystemConfig.internalFileSystem);
this.operationLock = new ReentrantLock();
}
void computeAccessPermissions(PolyglotContextConfig config) {
this.accessibleInternalLanguages = computeAccessibleLanguages(config, true);
this.accessiblePublicLanguages = computeAccessibleLanguages(config, false);
}
private Map computeAccessibleLanguages(PolyglotContextConfig config, boolean internal) {
PolyglotLanguage thisLanguage = languageInstance.language;
if (thisLanguage.isHost()) {
return languageInstance.getEngine().idToInternalLanguageInfo;
}
boolean embedderAllAccess = config.allowedPublicLanguages.isEmpty();
PolyglotEngineImpl engine = languageInstance.getEngine();
Set configuredAccess = null;
UnmodifiableEconomicSet configured = engine.getAPIAccess().getEvalAccess(config.polyglotAccess, thisLanguage.getId());
if (configured != null) {
configuredAccess = new HashSet<>();
configuredAccess.addAll(Arrays.asList(configured.toArray(new String[configured.size()])));
}
Set resolveLanguages;
if (embedderAllAccess) {
if (configuredAccess == null) {
if (internal) {
return engine.idToInternalLanguageInfo;
} else {
resolveLanguages = new HashSet<>();
resolveLanguages.addAll(engine.idToInternalLanguageInfo.keySet());
}
} else {
resolveLanguages = new HashSet<>(configuredAccess);
resolveLanguages.add(thisLanguage.getId());
}
} else {
if (configuredAccess == null) {
// all access configuration
configuredAccess = config.allowedPublicLanguages;
}
resolveLanguages = new HashSet<>(configuredAccess);
resolveLanguages.add(thisLanguage.getId());
}
Map resolvedLanguages = new LinkedHashMap<>();
for (String id : resolveLanguages) {
PolyglotLanguage resolvedLanguage = engine.idToLanguage.get(id);
if (resolvedLanguage != null) { // resolved languages might not be on the
// class-path.
if (!internal && resolvedLanguage.cache.isInternal()) {
// filter internal
continue;
}
resolvedLanguages.put(id, resolvedLanguage.info);
}
}
if (internal) {
addDependentLanguages(engine, resolvedLanguages, thisLanguage);
}
// all internal languages are accessible by default
if (internal) {
for (Entry entry : languageInstance.getEngine().idToLanguage.entrySet()) {
if (entry.getValue().cache.isInternal()) {
resolvedLanguages.put(entry.getKey(), entry.getValue().info);
}
}
assert assertPermissionsConsistent(resolvedLanguages, languageInstance.language, config);
}
return resolvedLanguages;
}
private boolean assertPermissionsConsistent(Map resolvedLanguages, PolyglotLanguage thisLanguage, PolyglotContextConfig config) {
for (Entry entry : languageInstance.getEngine().idToLanguage.entrySet()) {
boolean permitted = config.isAccessPermitted(thisLanguage, entry.getValue());
assert permitted == resolvedLanguages.containsKey(entry.getKey()) : "inconsistent access permissions";
}
return true;
}
private void addDependentLanguages(PolyglotEngineImpl engine, Map resolvedLanguages, PolyglotLanguage currentLanguage) {
for (String dependentLanguage : currentLanguage.cache.getDependentLanguages()) {
PolyglotLanguage dependent = engine.idToLanguage.get(dependentLanguage);
if (dependent == null) { // dependent languages might not exist.
continue;
}
if (resolvedLanguages.containsKey(dependentLanguage)) {
continue; // cycle or duplicate detection
}
resolvedLanguages.put(dependentLanguage, dependent.info);
addDependentLanguages(engine, resolvedLanguages, dependent);
}
}
}
final PolyglotContextImpl context;
final PolyglotLanguage language;
final boolean eventsEnabled;
private Thread creatingThread;
private volatile boolean created;
private volatile boolean initialized;
volatile boolean finalized;
volatile TruffleLanguage.ExitMode exited;
@CompilationFinal private volatile Value hostBindings;
@CompilationFinal private volatile Lazy lazy;
@CompilationFinal volatile Env env; // effectively final
@CompilationFinal private volatile List