Search in sources :

Example 1 with RuntimeContext

use of com.github.anba.es6draft.runtime.internal.RuntimeContext in project es6draft by anba.

the class AtomicsTestFunctions method evalInWorker.

/**
     * shell-function: {@code evalInWorker(sourceString)}
     * 
     * @param cx
     *            the execution context
     * @param caller
     *            the caller execution context
     * @param sourceString
     *            the script source code
     * @return {@code true} if a new script worker was started, otherwise returns {@code false}
     */
@Function(name = "evalInWorker", arity = 1)
public boolean evalInWorker(ExecutionContext cx, ExecutionContext caller, String sourceString) {
    Source baseSource = Objects.requireNonNull(cx.getRealm().sourceInfo(caller));
    try {
        // TODO: Initialize extensions (console.jsm, window timers?).
        CompletableFuture.supplyAsync(() -> {
            // Set 'executor' to null so it doesn't get shared with the current runtime context.
            /* @formatter:off */
            RuntimeContext context = new RuntimeContext.Builder(cx.getRuntimeContext()).setExecutor(null).build();
            /* @formatter:on */
            World world = new World(context);
            Realm realm;
            try {
                realm = world.newInitializedRealm();
            } catch (IOException | URISyntaxException e) {
                throw new CompletionException(e);
            }
            // Bind test functions to this instance.
            realm.createGlobalProperties(this, AtomicsTestFunctions.class);
            // TODO: Add proper abstraction.
            ModuleLoader moduleLoader = world.getModuleLoader();
            if (moduleLoader instanceof NodeModuleLoader) {
                try {
                    ((NodeModuleLoader) moduleLoader).initialize(realm);
                } catch (IOException | URISyntaxException | MalformedNameException | ResolutionException e) {
                    throw new CompletionException(e);
                }
            }
            // Evaluate the script source code and then run pending jobs.
            Source source = new Source(baseSource, "evalInWorker-script", 1);
            Script script = realm.getScriptLoader().script(source, sourceString);
            Object result = script.evaluate(realm);
            world.runEventLoop();
            return result;
        }, cx.getRuntimeContext().getWorkerExecutor()).whenComplete((r, e) -> {
            if (e instanceof CompletionException) {
                Throwable cause = ((CompletionException) e).getCause();
                cx.getRuntimeContext().getWorkerErrorReporter().accept(cx, (cause != null ? cause : e));
            } else if (e != null) {
                cx.getRuntimeContext().getWorkerErrorReporter().accept(cx, e);
            }
        });
        return true;
    } catch (RejectedExecutionException e) {
        return false;
    }
}
Also used : Script(com.github.anba.es6draft.Script) ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) NodeModuleLoader(com.github.anba.es6draft.repl.loader.NodeModuleLoader) NodeModuleLoader(com.github.anba.es6draft.repl.loader.NodeModuleLoader) World(com.github.anba.es6draft.runtime.World) Source(com.github.anba.es6draft.runtime.internal.Source) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) CompletionException(java.util.concurrent.CompletionException) SharedArrayBufferObject(com.github.anba.es6draft.runtime.objects.atomics.SharedArrayBufferObject) RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) Realm(com.github.anba.es6draft.runtime.Realm) Function(com.github.anba.es6draft.runtime.internal.Properties.Function)

Example 2 with RuntimeContext

use of com.github.anba.es6draft.runtime.internal.RuntimeContext in project es6draft by anba.

the class TestGlobals method compileModules.

private PreloadModules compileModules() throws IOException, MalformedNameException {
    List<?> moduleNames = configuration.getList("modules", emptyList());
    if (moduleNames.isEmpty()) {
        return new PreloadModules(Collections.<ModuleRecord>emptyList(), Collections.<ModuleRecord>emptyList());
    }
    RuntimeContext context = createContext();
    ScriptLoader scriptLoader = new ScriptLoader(context);
    TestModuleLoader<?> moduleLoader = this.moduleLoader.apply(context, scriptLoader);
    ArrayList<ModuleRecord> modules = new ArrayList<>();
    for (String moduleName : nonEmpty(moduleNames)) {
        SourceIdentifier moduleId = moduleLoader.normalizeName(moduleName, null);
        modules.add(moduleLoader.load(moduleId));
    }
    return new PreloadModules(modules, moduleLoader.getModules());
}
Also used : ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) ArrayList(java.util.ArrayList) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader)

Example 3 with RuntimeContext

use of com.github.anba.es6draft.runtime.internal.RuntimeContext in project es6draft by anba.

the class ScriptEngineImpl method eval.

Object eval(Script script, ScriptContext context) throws javax.script.ScriptException {
    Realm realm = getEvalRealm(context);
    RuntimeContext runtimeContext = realm.getRuntimeContext();
    Console console = runtimeContext.getConsole();
    runtimeContext.setConsole(new ScriptingConsole(context));
    try {
        // Prepare a new execution context before calling the generated code.
        ExecutionContext evalCxt = newScriptingExecutionContext(realm, script, new LexicalEnvironment<>(realm.getGlobalEnv(), new ScriptContextEnvironmentRecord(realm.defaultContext(), context)));
        Object result = script.evaluate(evalCxt);
        realm.getWorld().runEventLoop();
        return TypeConverter.toJava(result);
    } catch (ScriptException e) {
        throw new javax.script.ScriptException(e);
    } finally {
        runtimeContext.setConsole(console);
    }
}
Also used : ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ExecutionContext.newScriptingExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext.newScriptingExecutionContext) Console(com.github.anba.es6draft.runtime.internal.Console) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) Realm(com.github.anba.es6draft.runtime.Realm)

Example 4 with RuntimeContext

use of com.github.anba.es6draft.runtime.internal.RuntimeContext in project es6draft by anba.

the class ScriptEngineImpl method invoke.

private Object invoke(ScriptObject thisValue, String name, Object... args) throws javax.script.ScriptException, NoSuchMethodException {
    Realm realm = getEvalRealm(context);
    RuntimeContext runtimeContext = realm.getRuntimeContext();
    Console console = runtimeContext.getConsole();
    runtimeContext.setConsole(new ScriptingConsole(context));
    try {
        Object[] arguments = TypeConverter.fromJava(args);
        if (thisValue == null) {
            thisValue = realm.getGlobalThis();
        }
        ExecutionContext cx = realm.defaultContext();
        Object func = thisValue.get(cx, (Object) name, thisValue);
        if (!IsCallable(func)) {
            throw new NoSuchMethodException(name);
        }
        Object result = ((Callable) func).call(cx, thisValue, arguments);
        realm.getWorld().runEventLoop();
        return TypeConverter.toJava(result);
    } catch (ScriptException e) {
        throw new javax.script.ScriptException(e);
    } finally {
        runtimeContext.setConsole(console);
    }
}
Also used : ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ExecutionContext.newScriptingExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext.newScriptingExecutionContext) Console(com.github.anba.es6draft.runtime.internal.Console) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) Realm(com.github.anba.es6draft.runtime.Realm) IsCallable(com.github.anba.es6draft.runtime.AbstractOperations.IsCallable) Callable(com.github.anba.es6draft.runtime.types.Callable)

Example 5 with RuntimeContext

use of com.github.anba.es6draft.runtime.internal.RuntimeContext in project es6draft by anba.

the class DefaultLocaleTimezone method newRealm.

private static Realm newRealm(TimeZone timeZone) throws Exception {
    RuntimeContext context = new RuntimeContext.Builder().setTimeZone(timeZone).build();
    World world = new World(context);
    return Realm.InitializeHostDefinedRealm(world);
}
Also used : RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) World(com.github.anba.es6draft.runtime.World)

Aggregations

RuntimeContext (com.github.anba.es6draft.runtime.internal.RuntimeContext)14 World (com.github.anba.es6draft.runtime.World)7 Script (com.github.anba.es6draft.Script)5 Realm (com.github.anba.es6draft.runtime.Realm)5 ScriptLoader (com.github.anba.es6draft.runtime.internal.ScriptLoader)5 Source (com.github.anba.es6draft.runtime.internal.Source)4 ModuleRecord (com.github.anba.es6draft.runtime.modules.ModuleRecord)4 ArrayList (java.util.ArrayList)4 Path (java.nio.file.Path)3 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)2 ExecutionContext.newScriptingExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext.newScriptingExecutionContext)2 Console (com.github.anba.es6draft.runtime.internal.Console)2 ScriptException (com.github.anba.es6draft.runtime.internal.ScriptException)2 SourceIdentifier (com.github.anba.es6draft.runtime.modules.SourceIdentifier)2 URLSourceIdentifier (com.github.anba.es6draft.runtime.modules.loader.URLSourceIdentifier)2 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)2 Map (java.util.Map)2 NodeModuleLoader (com.github.anba.es6draft.repl.loader.NodeModuleLoader)1 IsCallable (com.github.anba.es6draft.runtime.AbstractOperations.IsCallable)1 Function (com.github.anba.es6draft.runtime.internal.Properties.Function)1