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, 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.graalvm.polyglot;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Executable;
import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.logging.Handler;
import java.util.logging.Level;
import org.graalvm.collections.UnmodifiableEconomicMap;
import org.graalvm.collections.UnmodifiableEconomicSet;
import org.graalvm.home.HomeFinder;
import org.graalvm.nativeimage.ImageInfo;
import org.graalvm.options.OptionDescriptor;
import org.graalvm.options.OptionDescriptors;
import org.graalvm.polyglot.HostAccess.MutableTargetMapping;
import org.graalvm.polyglot.HostAccess.TargetMappingPrecedence;
import org.graalvm.polyglot.PolyglotException.StackFrame;
import org.graalvm.polyglot.impl.AbstractPolyglotImpl;
import org.graalvm.polyglot.impl.AbstractPolyglotImpl.AbstractContextDispatch;
import org.graalvm.polyglot.impl.AbstractPolyglotImpl.AbstractEngineDispatch;
import org.graalvm.polyglot.impl.AbstractPolyglotImpl.AbstractExceptionDispatch;
import org.graalvm.polyglot.impl.AbstractPolyglotImpl.AbstractInstrumentDispatch;
import org.graalvm.polyglot.impl.AbstractPolyglotImpl.AbstractLanguageDispatch;
import org.graalvm.polyglot.impl.AbstractPolyglotImpl.AbstractSourceDispatch;
import org.graalvm.polyglot.impl.AbstractPolyglotImpl.AbstractSourceSectionDispatch;
import org.graalvm.polyglot.impl.AbstractPolyglotImpl.AbstractStackFrameImpl;
import org.graalvm.polyglot.impl.AbstractPolyglotImpl.AbstractValueDispatch;
import org.graalvm.polyglot.impl.AbstractPolyglotImpl.LogHandler;
import org.graalvm.polyglot.io.FileSystem;
import org.graalvm.polyglot.io.MessageTransport;
import org.graalvm.polyglot.io.ProcessHandler;
/**
* An execution engine for Graal {@linkplain Language guest languages} that allows to inspect the
* the installed {@link #getLanguages() guest languages}, {@link #getInstruments() instruments} and
* their available options.
*
* By default every context creates its own {@link Engine engine} instance implicitly when
* {@link Context.Builder#build() instantiated}. Multiple contexts can use an
* {@link Context.Builder#engine(Engine) explicit engine} when using a context builder. If contexts
* share the same engine instance then they share instruments and their configuration.
*
* It can be useful to {@link Engine#create() create} an engine instance without a context to only
* access meta-data for installed languages, instruments and their available options.
*
* @since 19.0
*/
public final class Engine implements AutoCloseable {
private static volatile Throwable initializationException;
private static volatile boolean shutdownHookInitialized;
private static final Map ENGINES = Collections.synchronizedMap(new WeakHashMap<>());
final AbstractEngineDispatch dispatch;
final Object receiver;
final Engine currentAPI;
@SuppressWarnings("unchecked")
Engine(AbstractEngineDispatch dispatch, T receiver) {
this.dispatch = dispatch;
this.receiver = receiver;
this.currentAPI = new Engine(this);
if (dispatch != null) {
dispatch.setAPI(receiver, this);
}
}
@SuppressWarnings("unchecked")
private Engine(Engine engine) {
this.dispatch = engine.dispatch;
this.receiver = engine.receiver;
this.currentAPI = null;
}
private static final class ImplHolder {
private static AbstractPolyglotImpl IMPL = initEngineImpl();
static {
try {
// Force initialization of AbstractPolyglotImpl#management when Engine is
// initialized.
Class.forName("org.graalvm.polyglot.management.Management", true, Engine.class.getClassLoader());
} catch (ReflectiveOperationException e) {
throw new InternalError(e);
}
}
/**
* Performs context pre-initialization.
*
* NOTE: this method is called reflectively by downstream projects
* (com.oracle.svm.truffle.TruffleBaseFeature).
*/
@SuppressWarnings("unused")
private static void preInitializeEngine() {
IMPL.preInitializeEngine();
}
/**
* Clears the pre-initialized engine.
*
* NOTE: this method is called reflectively by downstream projects
* (com.oracle.svm.truffle.TruffleBaseFeature).
*/
@SuppressWarnings("unused")
private static void resetPreInitializedEngine() {
IMPL.resetPreInitializedEngine();
}
/**
* Support for Context pre-initialization debugging in HotSpot.
*/
private static void debugContextPreInitialization() {
if (!ImageInfo.inImageCode() && System.getProperty("polyglot.image-build-time.PreinitializeContexts") != null) {
IMPL.preInitializeEngine();
}
}
static {
debugContextPreInitialization();
}
}
/**
* Gets a map of all installed languages with the language id as key and the language object as
* value. The returned map is unmodifiable and might be used from multiple threads.
*
* @since 19.0
*/
public Map getLanguages() {
return dispatch.getLanguages(receiver);
}
/**
* Gets all installed instruments of this engine. An instrument alters and/or monitors the
* execution of guest language source code. Common examples for instruments are debuggers,
* profilers, or monitoring tools. Instruments are enabled via {@link Instrument#getOptions()
* options} passed to the {@link Builder#option(String, String) engine} when the engine or
* context is constructed.
*
* @since 19.0
*/
public Map getInstruments() {
return dispatch.getInstruments(receiver);
}
/**
* Returns all options available for the engine. The engine offers options with the following
* {@link OptionDescriptor#getKey() groups}:
*
*
engine: options to configure the behavior of this engine.
*
* The language and instrument specific options need to be retrieved using
* {@link Instrument#getOptions()} or {@link Language#getOptions()}.
*
* @see Language#getOptions() To get a list of options for a language.
* @see Instrument#getOptions() To get a list of options for an instrument.
* @see Builder#option(String, String) To set an option for an engine, language, or instrument.
* @see Context.Builder#option(String, String) To set an option for a context.
*
* @since 19.0
*/
public OptionDescriptors getOptions() {
return dispatch.getOptions(receiver);
}
/**
* Gets the version string of the engine in an unspecified format.
*
* @since 19.0
*/
@SuppressWarnings("static-method")
public String getVersion() {
return dispatch.getVersion(receiver);
}
/**
* Closes this engine and frees up allocated native resources. If there are still open context
* instances that were created using this engine and they are currently not being executed then
* they will be closed automatically. If an attempt to close an engine was successful then
* consecutive calls to close have no effect. If a context is cancelled then the currently
* executing thread will throw a {@link PolyglotException}. The exception indicates that it was
* {@link PolyglotException#isCancelled() cancelled}.
*
* @param cancelIfExecuting if true then currently executing contexts will be
* cancelled, else an {@link IllegalStateException} is thrown.
* @since 19.0
*/
public void close(boolean cancelIfExecuting) {
if (currentAPI == null) {
throw new IllegalStateException("Engine instances that were indirectly received using Context.getCurrent() cannot be closed.");
}
dispatch.close(receiver, this, cancelIfExecuting);
}
/**
* Closes this engine and frees up allocated native resources. If there are still open context
* instances that were created using this engine and they are currently not being executed then
* they will be closed automatically. If an attempt to close the engine was successful then
* consecutive calls to close have no effect.
*
* @throws IllegalStateException if there currently executing open context instances.
* @see #close(boolean)
* @see Engine#close()
* @since 19.0
*/
@Override
public void close() {
close(false);
}
/**
* Gets a human-readable name of the polyglot implementation (for example, "Default Truffle
* Engine" or "Graal Truffle Engine"). The returned value may change without notice. The value
* is never null.
*
* @since 19.0
*/
public String getImplementationName() {
return dispatch.getImplementationName(receiver);
}
/**
* Creates a new engine instance with default configuration. This method is a shortcut for
* {@link #newBuilder(String...) newBuilder().build()}.
*
* @see Context#create(String...) to create a new execution context.
* @since 19.0
*/
public static Engine create() {
return newBuilder().build();
}
/**
* Creates a new engine instance with default configuration with a set of permitted languages.
* This method is a shortcut for {@link #newBuilder(String...)
* newBuilder(permittedLanuages).build()}.
*
* @see Context#create(String...) to create a new execution context.
* @since 21.3
*/
public static Engine create(String... permittedLanguages) {
return newBuilder(permittedLanguages).build();
}
/**
* Creates a new engine builder that allows to configure an engine instance. This method is
* equivalent to calling {@link #newBuilder(String...)} with an empty set of permitted
* languages.
*
* @since 21.3
*/
public static Builder newBuilder() {
return EMPTY.new Builder(new String[0]);
}
/**
* Creates a new engine builder that allows to configure an engine instance.
*
* @param permittedLanguages names of languages permitted in the engine. If no languages are
* provided, then all installed languages will be permitted. All contexts created
* with this engine will inherit the set of permitted languages.
* @return a builder that can create a context
* @since 21.3
*/
public static Builder newBuilder(String... permittedLanguages) {
Objects.requireNonNull(permittedLanguages);
return EMPTY.new Builder(permittedLanguages);
}
/**
* Finds the GraalVM home folder.
*
* This is equivalent to {@link HomeFinder#getHomeFolder()} which should be preferred.
*
* @return the path to a folder containing the GraalVM or {@code null} if it cannot be found
* @since 19.0
*/
public static Path findHome() {
return HomeFinder.getInstance().getHomeFolder();
}
/**
* Returns the sources previously cached by this engine. Only sources may be returned that allow
* {@link Source.Builder#cached(boolean) caching} (default on). The source cache of the engine
* is using weak references to refer to the source objects. Calling this method will result in a
* strong reference to all cached sources of this engine until the returned set is no longer
* referenced. This method is useful to find out which sources very already evaluated by this
* engine. This method only returns sources that were evaluated using
* {@link Context#eval(Source)}. Sources evaluated by the guest application will not be
* returned. The return set is never null and not modifiable.
*
* @since 20.3
*/
public Set