Search in sources :

Example 46 with Realm

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

the class WebKitTest method runTest.

@Test
public void runTest() throws Throwable {
    Realm realm = this.realm.get();
    // Evaluate actual test-script
    // - load and execute pre and post before resp. after test-script
    ScriptLoading.include(realm, test.getBaseDir().resolve("resources/standalone-pre.js"));
    ScriptLoading.eval(realm, test.getScript(), test.toFile());
    ScriptLoading.include(realm, test.getBaseDir().resolve("resources/standalone-post.js"));
    // Wait for pending jobs to finish
    realm.getWorld().runEventLoop();
}
Also used : Realm(com.github.anba.es6draft.runtime.Realm) TestRealm(com.github.anba.es6draft.util.TestRealm) Test(org.junit.Test)

Example 47 with Realm

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

the class ModuleOperations method HostImportModuleDynamically.

/**
 * Runtime Semantics: HostImportModuleDynamically ( referencingScriptOrModule, specifier, promiseCapability )
 *
 * @param cx
 *            the execution context
 * @param specifier
 *            the module specifier
 * @param promiseCapability
 *            the promise capability
 */
private static void HostImportModuleDynamically(ExecutionContext cx, String specifier, PromiseCapability<PromiseObject> promiseCapability) {
    Source source = cx.getCurrentExecutable().getSource();
    assert source != null : "HostImportModuleDynamically is only called from compiled code";
    // FIXME: spec bug - cannot assert referencingScriptOrModule is non-null, e.g. consider:
    // Promise.resolve(`import("./t.js").catch(e => print("caught", e))`).then(eval);
    // Or:
    // Promise.resolve(`import("./t.js").catch(e => print("caught",
    // e))`).then(Function).then(Function.prototype.call.bind(Function.prototype.call));
    SourceIdentifier referredId = source.getSourceId();
    if (referredId == null) {
        ScriptException exception = newReferenceError(cx, Messages.Key.ModulesInvalidName, specifier);
        FinishDynamicImport(cx, specifier, promiseCapability, exception);
        return;
    }
    Realm realm = cx.getRealm();
    ModuleLoader moduleLoader = realm.getModuleLoader();
    SourceIdentifier moduleId;
    try {
        moduleId = moduleLoader.normalizeName(specifier, referredId);
    } catch (MalformedNameException e) {
        FinishDynamicImport(cx, specifier, promiseCapability, e.toScriptException(cx));
        return;
    }
    moduleLoader.resolveAsync(moduleId, realm).whenComplete((module, err) -> {
        realm.enqueueAsyncJob(() -> {
            Throwable error = err;
            if (module != null) {
                try {
                    module.instantiate();
                    module.evaluate();
                } catch (ScriptException | IOException | MalformedNameException | ResolutionException e) {
                    assert error == null;
                    error = e;
                }
            }
            if (error != null) {
                ScriptException exception = toScriptException(cx, error, moduleId, referredId);
                FinishDynamicImport(cx, specifier, promiseCapability, exception);
            } else {
                FinishDynamicImport(cx, specifier, promiseCapability, module, referredId);
            }
        });
    });
}
Also used : ResolutionException(com.github.anba.es6draft.runtime.modules.ResolutionException) ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) InternalThrowable(com.github.anba.es6draft.runtime.internal.InternalThrowable) MalformedNameException(com.github.anba.es6draft.runtime.modules.MalformedNameException) IOException(java.io.IOException) Realm(com.github.anba.es6draft.runtime.Realm) Source(com.github.anba.es6draft.runtime.internal.Source)

Example 48 with Realm

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

the class Eval method script.

private static Script script(ExecutionContext cx, ExecutionContext caller, String sourceCode, int flags) {
    try {
        Realm realm = cx.getRealm();
        Source source = evalSource(caller);
        EnumSet<Parser.Option> options = EvalFlags.toOptions(flags);
        EnumSet<Parser.Option> contextOptions = realm.getRuntimeContext().getParserOptions();
        if (contextOptions.contains(Parser.Option.NativeCall)) {
            options.add(Parser.Option.NativeCall);
        }
        return realm.getScriptLoader().evalScript(source, sourceCode, options);
    } catch (ParserException | CompilationException e) {
        throw e.toScriptException(cx);
    }
}
Also used : ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) Realm(com.github.anba.es6draft.runtime.Realm) Source(com.github.anba.es6draft.runtime.internal.Source)

Example 49 with Realm

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

the class SourceTextModuleRecord method ModuleDeclarationEnvironmentSetup.

/**
 * 15.2.1.16.4.2 ModuleDeclarationEnvironmentSetup( module )
 */
private static void ModuleDeclarationEnvironmentSetup(SourceTextModuleRecord module) throws IOException, ResolutionException, MalformedNameException {
    /* step 3 */
    Realm realm = module.realm;
    /* step 4 */
    assert realm != null : "module is not linked";
    /* step 5 */
    LexicalEnvironment<ModuleEnvironmentRecord> env = newModuleEnvironment(realm.getGlobalEnv());
    /* step 6 */
    module.environment = env;
    /* steps 1-2, 7-14 (generated code) */
    Module code = module.scriptCode;
    ExecutionContext context = newModuleDeclarationExecutionContext(realm, code);
    code.getModuleBody().moduleDeclarationInstantiation(context, module, env);
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ExecutionContext.newModuleDeclarationExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext.newModuleDeclarationExecutionContext) ExecutionContext.newModuleExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext.newModuleExecutionContext) HostResolveImportedModule(com.github.anba.es6draft.runtime.modules.ModuleSemantics.HostResolveImportedModule) Module(com.github.anba.es6draft.Module) Realm(com.github.anba.es6draft.runtime.Realm) ModuleEnvironmentRecord(com.github.anba.es6draft.runtime.ModuleEnvironmentRecord)

Example 50 with Realm

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

the class SourceTextModuleRecord method ModuleExecution.

/**
 * 15.2.1.16.5.2 ModuleExecution( module )
 */
private static Object ModuleExecution(SourceTextModuleRecord module) {
    /* step 3 */
    Realm realm = module.realm;
    assert realm != null : "module is not linked";
    /* steps 1-2, 4-8 */
    ExecutionContext moduleContext = newModuleExecutionContext(realm, module);
    /* step 9 */
    ExecutionContext oldScriptContext = realm.getWorld().getScriptContext();
    try {
        /* step 10 */
        realm.getWorld().setScriptContext(moduleContext);
        /* step 11 */
        Object result = module.scriptCode.evaluate(moduleContext);
        /* step 14 */
        return result;
    } finally {
        /* steps 12-13 */
        realm.getWorld().setScriptContext(oldScriptContext);
    }
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ExecutionContext.newModuleDeclarationExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext.newModuleDeclarationExecutionContext) ExecutionContext.newModuleExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext.newModuleExecutionContext) 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