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, 2020, 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.polyglot;
import static com.oracle.truffle.api.CompilerDirectives.shouldNotReachHere;
import static com.oracle.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.ConcurrentHashMap;
import java.util.function.Function;
import java.util.logging.Level;
import org.graalvm.collections.EconomicSet;
import org.graalvm.collections.Equivalence;
import org.graalvm.collections.UnmodifiableEconomicSet;
import org.graalvm.polyglot.PolyglotAccess;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.impl.AbstractPolyglotImpl.APIAccess;
import org.graalvm.polyglot.proxy.Proxy;
import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.TruffleLanguage.Env;
import com.oracle.truffle.api.TruffleLogger;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.GenerateUncached;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.Frame;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.NodeLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.nodes.ExplodeLoop;
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.polyglot.PolyglotLanguageContextFactory.ToGuestValueNodeGen;
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 PolyglotSourceCache sourceCache;
final Set activePolyglotThreads;
final Object polyglotGuestBindings;
final Map, PolyglotValue> valueCache;
final Thread.UncaughtExceptionHandler uncaughtExceptionHandler;
final PolyglotLanguageInstance languageInstance;
@CompilationFinal Map accessibleInternalLanguages;
@CompilationFinal Map accessiblePublicLanguages;
final Object internalFileSystemContext;
final Object publicFileSystemContext;
Lazy(PolyglotLanguageInstance languageInstance, PolyglotContextConfig config) {
/*
* Important anything that is initialized here must be properly patched in #patch.
*/
this.languageInstance = languageInstance;
this.sourceCache = languageInstance.getSourceCache();
this.activePolyglotThreads = new HashSet<>();
this.polyglotGuestBindings = new PolyglotBindings(PolyglotLanguageContext.this);
this.uncaughtExceptionHandler = new PolyglotUncaughtExceptionHandler();
this.valueCache = new ConcurrentHashMap<>();
this.computeAccessPermissions(config);
// file systems are patched after preinitialization internally using a delegate field
this.publicFileSystemContext = EngineAccessor.LANGUAGE.createFileSystemContext(PolyglotLanguageContext.this, config.fileSystem);
this.internalFileSystemContext = EngineAccessor.LANGUAGE.createFileSystemContext(PolyglotLanguageContext.this, config.internalFileSystem);
}
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();
UnmodifiableEconomicSet configuredAccess = engine.getAPIAccess().getEvalAccess(config.polyglotAccess, thisLanguage.getId());
EconomicSet resolveLanguages;
if (embedderAllAccess) {
if (configuredAccess == null) {
if (internal) {
return engine.idToInternalLanguageInfo;
} else {
resolveLanguages = EconomicSet.create(Equivalence.DEFAULT, configuredAccess);
resolveLanguages.addAll(engine.idToInternalLanguageInfo.keySet());
}
} else {
resolveLanguages = EconomicSet.create(Equivalence.DEFAULT, configuredAccess);
resolveLanguages.add(thisLanguage.getId());
}
} else {
if (configuredAccess == null) {
// all access configuration
configuredAccess = config.allowedPublicLanguages;
}
resolveLanguages = EconomicSet.create(Equivalence.DEFAULT, 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 volatile Thread creatingThread;
private volatile boolean initialized;
volatile boolean finalized;
@CompilationFinal private volatile Value hostBindings;
@CompilationFinal private volatile Lazy lazy;
@CompilationFinal volatile Env env; // effectively final
@CompilationFinal private volatile List