Search in sources :

Example 1 with Realm

use of com.github.anba.es6draft.runtime.Realm 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 Realm

use of com.github.anba.es6draft.runtime.Realm in project es6draft by anba.

the class MozShellFunctions method evalcx.

/**
     * shell-function: {@code evalcx(s, [o])}
     *
     * @param cx
     *            the execution context
     * @param caller
     *            the caller context
     * @param sourceCode
     *            the source to evaluate
     * @param o
     *            the global object
     * @return the eval result value
     */
@Function(name = "evalcx", arity = 1)
public Object evalcx(ExecutionContext cx, ExecutionContext caller, String sourceCode, Object o) {
    ScriptObject global;
    if (Type.isUndefinedOrNull(o)) {
        global = newGlobal(cx);
    } else {
        global = ToObject(cx, o);
    }
    if (sourceCode.isEmpty() || "lazy".equals(sourceCode)) {
        return global;
    }
    if (!(global instanceof GlobalObject)) {
        throw Errors.newError(cx, "invalid global argument");
    }
    Source source = new Source(cx.getRealm().sourceInfo(caller), "evalcx", 1);
    Realm realm = ((GlobalObject) global).getRealm();
    try {
        Script script = realm.getScriptLoader().script(source, sourceCode);
        return script.evaluate(realm);
    } catch (ParserException | CompilationException e) {
        // Create a script exception from the requested code realm, not from the caller's realm.
        throw e.toScriptException(realm.defaultContext());
    }
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) GlobalObject(com.github.anba.es6draft.runtime.objects.GlobalObject) SharedFunctions.relativePathToScript(com.github.anba.es6draft.repl.global.SharedFunctions.relativePathToScript) Script(com.github.anba.es6draft.Script) SharedFunctions.loadScript(com.github.anba.es6draft.repl.global.SharedFunctions.loadScript) ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) Realm(com.github.anba.es6draft.runtime.Realm) ToSource(com.github.anba.es6draft.repl.SourceBuilder.ToSource) Source(com.github.anba.es6draft.runtime.internal.Source) Function(com.github.anba.es6draft.runtime.internal.Properties.Function) BuiltinFunction(com.github.anba.es6draft.runtime.types.builtins.BuiltinFunction)

Example 3 with Realm

use of com.github.anba.es6draft.runtime.Realm in project es6draft by anba.

the class SharedFunctions method loadScript.

/**
     * Reads a file and evalutes its content.
     * 
     * @param cx
     *            the execution context
     * @param fileName
     *            the file name
     * @param path
     *            the file path
     * @throws ParserException
     *             if the source contains any syntax errors
     * @throws CompilationException
     *             if the parsed source cannot be compiled
     */
static void loadScript(ExecutionContext cx, Path fileName, Path path) throws ParserException, CompilationException {
    if (!Files.exists(path)) {
        throw new ScriptException(String.format("can't open '%s'", fileName.toString()));
    }
    try {
        Realm realm = cx.getRealm();
        Source source = new Source(path, fileName.toString(), 1);
        Script script = realm.getScriptLoader().script(source, path);
        script.evaluate(realm);
    } catch (IOException e) {
        throw Errors.newError(cx, Objects.toString(e.getMessage(), ""));
    }
}
Also used : ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) Script(com.github.anba.es6draft.Script) IOException(java.io.IOException) Realm(com.github.anba.es6draft.runtime.Realm) Source(com.github.anba.es6draft.runtime.internal.Source)

Example 4 with Realm

use of com.github.anba.es6draft.runtime.Realm in project es6draft by anba.

the class InterpretedScriptBody method scriptEvaluation.

/**
     * 15.1.7 Runtime Semantics: ScriptEvaluation
     * 
     * @param cx
     *            the execution context
     * @param script
     *            the script object
     * @return the script evaluation result
     */
private Object scriptEvaluation(ExecutionContext cx, Script script) {
    Realm realm = cx.getRealm();
    /* step 1 (not applicable) */
    /* step 2 */
    LexicalEnvironment<GlobalEnvironmentRecord> globalEnv = realm.getGlobalEnv();
    /* steps 3-7 */
    ExecutionContext scriptCxt = newScriptExecutionContext(realm, script);
    /* steps 8-9 */
    ExecutionContext oldScriptContext = realm.getScriptContext();
    try {
        realm.setScriptContext(scriptCxt);
        /* step 10 */
        GlobalDeclarationInstantiation(scriptCxt, parsedScript, globalEnv);
        /* steps 11-12 */
        Object result = parsedScript.accept(new Interpreter(parsedScript), scriptCxt);
        /* step 16 */
        return result;
    } finally {
        /* steps 13-15  */
        realm.setScriptContext(oldScriptContext);
    }
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ExecutionContext.newScriptExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext.newScriptExecutionContext) ExecutionContext.newEvalExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext.newEvalExecutionContext) GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) Realm(com.github.anba.es6draft.runtime.Realm)

Example 5 with Realm

use of com.github.anba.es6draft.runtime.Realm in project es6draft by anba.

the class NativeCode method getModuleExport.

/**
     * Resolves and returns the exported binding from a module record.
     * 
     * @param <T>
     *            the object type
     * @param module
     *            the module record
     * @param exportName
     *            the export name
     * @param clazz
     *            the expected class
     * @return the exported value
     * @throws IOException
     *             if there was any I/O error
     * @throws MalformedNameException
     *             if any imported module request cannot be normalized
     * @throws ResolutionException
     *             if any export binding cannot be resolved
     */
public static <T> T getModuleExport(ModuleRecord module, String exportName, Class<T> clazz) throws IOException, MalformedNameException, ResolutionException {
    ModuleExport export = module.resolveExport(exportName, new HashMap<>(), new HashSet<>());
    if (export == null) {
        throw new ResolutionException(Messages.Key.ModulesUnresolvedExport, exportName);
    }
    if (export.isAmbiguous()) {
        throw new ResolutionException(Messages.Key.ModulesAmbiguousExport, exportName);
    }
    ModuleRecord targetModule = export.getModule();
    if (!targetModule.isInstantiated() || !targetModule.isEvaluated()) {
        throw new IllegalStateException();
    }
    if (export.isNameSpaceExport()) {
        Realm realm = module.getRealm();
        if (realm == null) {
            throw new IllegalArgumentException();
        }
        ScriptObject namespace = GetModuleNamespace(realm.defaultContext(), targetModule);
        return clazz.cast(namespace);
    }
    LexicalEnvironment<?> targetEnv = targetModule.getEnvironment();
    if (targetEnv == null) {
        throw new ResolutionException(Messages.Key.UninitializedModuleBinding, export.getBindingName(), targetModule.getSourceCodeId().toString());
    }
    Object bindingValue = targetEnv.getEnvRec().getBindingValue(export.getBindingName(), true);
    return clazz.cast(bindingValue);
}
Also used : ResolutionException(com.github.anba.es6draft.runtime.modules.ResolutionException) ModuleExport(com.github.anba.es6draft.runtime.modules.ModuleExport) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) Realm(com.github.anba.es6draft.runtime.Realm)

Aggregations

Realm (com.github.anba.es6draft.runtime.Realm)96 Test (org.junit.Test)39 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)17 Script (com.github.anba.es6draft.Script)16 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)16 Source (com.github.anba.es6draft.runtime.internal.Source)15 ParserException (com.github.anba.es6draft.parser.ParserException)9 Function (com.github.anba.es6draft.runtime.internal.Properties.Function)9 ModuleRecord (com.github.anba.es6draft.runtime.modules.ModuleRecord)8 ModuleLoader (com.github.anba.es6draft.runtime.modules.ModuleLoader)7 IOException (java.io.IOException)7 CompilationException (com.github.anba.es6draft.compiler.CompilationException)6 ScriptException (com.github.anba.es6draft.runtime.internal.ScriptException)6 ToSource (com.github.anba.es6draft.repl.SourceBuilder.ToSource)5 World (com.github.anba.es6draft.runtime.World)5 RuntimeContext (com.github.anba.es6draft.runtime.internal.RuntimeContext)5 ModuleSource (com.github.anba.es6draft.runtime.modules.ModuleSource)5 SourceIdentifier (com.github.anba.es6draft.runtime.modules.SourceIdentifier)5 GlobalObject (com.github.anba.es6draft.runtime.objects.GlobalObject)5 ExecutionContext.newEvalExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext.newEvalExecutionContext)4