Search in sources :

Example 1 with World

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

the class Repl method loop.

/**
     * REPL: Loop
     */
private void loop() throws InterruptedException {
    Realm realm = newRealm();
    World world = realm.getWorld();
    TaskSource taskSource = createTaskSource(realm);
    for (; ; ) {
        try {
            world.runEventLoop(taskSource);
            return;
        } catch (StopExecutionException e) {
            if (e.getReason() == Reason.Quit) {
                System.exit(0);
            }
        } catch (ParserExceptionWithSource e) {
            handleException(e);
        } catch (ScriptException e) {
            handleException(realm, e);
        } catch (UnhandledRejectionException e) {
            handleException(realm, e);
        } catch (StackOverflowError e) {
            handleException(realm, e);
        } catch (OutOfMemoryError e) {
            handleException(realm, e);
        } catch (InternalException e) {
            handleException(e);
        } catch (BootstrapMethodError e) {
            handleException(e.getCause());
        } catch (UncheckedIOException e) {
            handleException(e.getCause());
        }
    }
}
Also used : StopExecutionException(com.github.anba.es6draft.repl.global.StopExecutionException) World(com.github.anba.es6draft.runtime.World) InitializeHostDefinedRealm(com.github.anba.es6draft.runtime.Realm.InitializeHostDefinedRealm) Realm(com.github.anba.es6draft.runtime.Realm)

Example 2 with World

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

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

the class Repl method newRealm.

private Realm newRealm() {
    ObjectAllocator<? extends ShellGlobalObject> allocator;
    if (options.shellMode == ShellMode.Mozilla) {
        allocator = MozShellGlobalObject::new;
    } else if (options.shellMode == ShellMode.V8) {
        allocator = V8ShellGlobalObject::new;
    } else {
        allocator = SimpleShellGlobalObject::new;
    }
    BiFunction<RuntimeContext, ScriptLoader, ModuleLoader> moduleLoader;
    switch(options.moduleLoaderMode) {
        case Default:
            moduleLoader = FileModuleLoader::new;
            break;
        case Node:
            moduleLoader = NodeModuleLoader::new;
            break;
        case NodeStandard:
            moduleLoader = NodeStandardModuleLoader::new;
            break;
        default:
            throw new AssertionError();
    }
    /* @formatter:off */
    RuntimeContext context = new RuntimeContext.Builder().setBaseDirectory(Paths.get("").toAbsolutePath()).setGlobalAllocator(allocator).setModuleLoader(moduleLoader).setConsole(console).setWorkerErrorReporter(this::errorReporter).setOptions(compatibilityOptions(options)).setParserOptions(parserOptions(options)).setCompilerOptions(compilerOptions(options)).build();
    /* @formatter:on */
    World world = new World(context);
    Realm realm = world.newRealm();
    ExecutionContext cx = realm.defaultContext();
    // Add completion to console
    console.addCompleter(new ShellCompleter(realm));
    // Execute global specific initialization
    enqueueScriptTask(realm, () -> {
        InitializeHostDefinedRealm(realm);
        // Add global "arguments" property
        ScriptObject arguments = CreateArrayFromList(cx, options.arguments);
        CreateMethodProperty(cx, realm.getGlobalObject(), "arguments", arguments);
    });
    if (options.console) {
        enqueueScriptTask(realm, () -> {
            ScriptObject console = ConsoleObject.createConsole(realm);
            CreateMethodProperty(cx, realm.getGlobalObject(), "console", console);
        });
    }
    if (options.moduleLoaderMode == ModuleLoaderMode.Node) {
        // TODO: Add default initialize(Realm) method to ModuleLoader interface?
        enqueueScriptTask(realm, () -> {
            NodeModuleLoader nodeLoader = (NodeModuleLoader) world.getModuleLoader();
            nodeLoader.initialize(realm);
        });
    }
    // Run eval expressions and files
    for (EvalScript evalScript : options.evalScripts) {
        if (options.module) {
            enqueueScriptTask(realm, () -> {
                ModuleSource moduleSource = evalScript.getModuleSource();
                SourceIdentifier moduleName = evalScript.getModuleName();
                try {
                    ModuleEvaluationJob(realm, moduleName, moduleSource);
                } catch (ParserException e) {
                    Source source = moduleSource.toSource();
                    String file = e.getFile();
                    if (file.equals(source.getFileString())) {
                        throw new ParserExceptionWithSource(e, source, moduleSource.sourceCode());
                    }
                    Path filePath = Paths.get(file).toAbsolutePath();
                    Source errorSource = new Source(filePath, file, 1);
                    String code = new String(Files.readAllBytes(filePath), StandardCharsets.UTF_8);
                    throw new ParserExceptionWithSource(e, errorSource, code);
                }
            });
        } else {
            enqueueScriptTask(realm, () -> {
                Source source = evalScript.getSource();
                String sourceCode = evalScript.getSourceCode();
                try {
                    eval(realm, parse(realm, source, sourceCode));
                } catch (ParserException e) {
                    throw new ParserExceptionWithSource(e, source, sourceCode);
                }
            });
        }
    }
    return realm;
}
Also used : Path(java.nio.file.Path) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ParserException(com.github.anba.es6draft.parser.ParserException) ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) NodeStandardModuleLoader(com.github.anba.es6draft.repl.loader.NodeStandardModuleLoader) FileModuleLoader(com.github.anba.es6draft.runtime.modules.loader.FileModuleLoader) NodeModuleLoader(com.github.anba.es6draft.repl.loader.NodeModuleLoader) NodeModuleLoader(com.github.anba.es6draft.repl.loader.NodeModuleLoader) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) World(com.github.anba.es6draft.runtime.World) MozShellGlobalObject(com.github.anba.es6draft.repl.global.MozShellGlobalObject) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) NodeStandardModuleLoader(com.github.anba.es6draft.repl.loader.NodeStandardModuleLoader) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) FileModuleLoader(com.github.anba.es6draft.runtime.modules.loader.FileModuleLoader) InitializeHostDefinedRealm(com.github.anba.es6draft.runtime.Realm.InitializeHostDefinedRealm) Realm(com.github.anba.es6draft.runtime.Realm) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource)

Example 4 with World

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

the class PropertiesTest method setUp.

@Before
public void setUp() throws Throwable {
    RuntimeContext context = new RuntimeContext.Builder().build();
    World world = new World(context);
    realm = world.newInitializedRealm();
    cx = realm.defaultContext();
}
Also used : RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) World(com.github.anba.es6draft.runtime.World) Before(org.junit.Before)

Example 5 with World

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

the class TestGlobals method newGlobal.

public final GLOBAL newGlobal(Console console, TEST test) throws MalformedNameException, ResolutionException, IOException, URISyntaxException {
    RuntimeContext context = createContext(console, test);
    World world = new World(context);
    Realm realm = world.newInitializedRealm();
    // Evaluate additional initialization scripts and modules
    TestModuleLoader<?> moduleLoader = (TestModuleLoader<?>) world.getModuleLoader();
    for (ModuleRecord module : modules.allModules) {
        moduleLoader.defineFromTemplate(module, realm);
    }
    for (ModuleRecord module : modules.mainModules) {
        ModuleRecord testModule = moduleLoader.get(module.getSourceCodeId(), realm);
        testModule.instantiate();
        testModule.evaluate();
    }
    for (Script script : scripts) {
        script.evaluate(realm);
    }
    @SuppressWarnings("unchecked") GLOBAL global = (GLOBAL) realm.getGlobalObject();
    return global;
}
Also used : Script(com.github.anba.es6draft.Script) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) World(com.github.anba.es6draft.runtime.World) Realm(com.github.anba.es6draft.runtime.Realm)

Aggregations

World (com.github.anba.es6draft.runtime.World)5 Realm (com.github.anba.es6draft.runtime.Realm)4 RuntimeContext (com.github.anba.es6draft.runtime.internal.RuntimeContext)3 Script (com.github.anba.es6draft.Script)2 NodeModuleLoader (com.github.anba.es6draft.repl.loader.NodeModuleLoader)2 InitializeHostDefinedRealm (com.github.anba.es6draft.runtime.Realm.InitializeHostDefinedRealm)2 ModuleLoader (com.github.anba.es6draft.runtime.modules.ModuleLoader)2 ParserException (com.github.anba.es6draft.parser.ParserException)1 MozShellGlobalObject (com.github.anba.es6draft.repl.global.MozShellGlobalObject)1 StopExecutionException (com.github.anba.es6draft.repl.global.StopExecutionException)1 NodeStandardModuleLoader (com.github.anba.es6draft.repl.loader.NodeStandardModuleLoader)1 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)1 Function (com.github.anba.es6draft.runtime.internal.Properties.Function)1 Source (com.github.anba.es6draft.runtime.internal.Source)1 ModuleRecord (com.github.anba.es6draft.runtime.modules.ModuleRecord)1 ModuleSource (com.github.anba.es6draft.runtime.modules.ModuleSource)1 SourceIdentifier (com.github.anba.es6draft.runtime.modules.SourceIdentifier)1 FileModuleLoader (com.github.anba.es6draft.runtime.modules.loader.FileModuleLoader)1 SharedArrayBufferObject (com.github.anba.es6draft.runtime.objects.atomics.SharedArrayBufferObject)1 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)1