Search in sources :

Example 1 with ModuleLoader

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

use of com.github.anba.es6draft.runtime.modules.ModuleLoader in project es6draft by anba.

the class Test262GlobalObject method evalModule.

/**
     * Parses, compiles and executes the javascript module file.
     * 
     * @param moduleName
     *            the module name
     * @param sourceCode
     *            the source code
     * @param sourceLine
     *            the source line offset
     * @throws ParserException
     *             if the source contains any syntax errors
     * @throws CompilationException
     *             if the parsed source could not be compiled
     * @throws MalformedNameException
     *             if the module name cannot be normalized
     * @throws ResolutionException
     *             if the module exports cannot be resolved
     * @throws IOException
     *             if there was any I/O error
     */
void evalModule(String moduleName, String sourceCode, int sourceLine) throws ParserException, CompilationException, MalformedNameException, ResolutionException, IOException {
    ModuleLoader moduleLoader = getRealm().getModuleLoader();
    SourceIdentifier moduleId = moduleLoader.normalizeName(moduleName, null);
    ModuleSource source = new StringModuleSource(moduleId, sourceCode, sourceLine);
    ModuleRecord module = moduleLoader.define(moduleId, source, getRealm());
    module.instantiate();
    module.evaluate();
}
Also used : ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource)

Example 3 with ModuleLoader

use of com.github.anba.es6draft.runtime.modules.ModuleLoader in project es6draft by anba.

the class ScriptLoading method evalModule.

/**
 * Parses, compiles and executes the javascript module file.
 *
 * @param realm
 *            the realm instance
 * @param moduleName
 *            the unnormalized module name
 * @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
 * @throws ParserException
 *             if the module source contains any syntax errors
 * @throws CompilationException
 *             if the parsed module source cannot be compiled
 */
public static void evalModule(Realm realm, String moduleName) throws IOException, MalformedNameException, ResolutionException, ParserException, CompilationException {
    ModuleLoader moduleLoader = realm.getModuleLoader();
    SourceIdentifier moduleId = moduleLoader.normalizeName(moduleName, null);
    ModuleRecord module = moduleLoader.resolve(moduleId, realm);
    module.instantiate();
    module.evaluate();
}
Also used : ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) URLModuleLoader(com.github.anba.es6draft.runtime.modules.loader.URLModuleLoader) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord) FileSourceIdentifier(com.github.anba.es6draft.runtime.modules.loader.FileSourceIdentifier) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) URLSourceIdentifier(com.github.anba.es6draft.runtime.modules.loader.URLSourceIdentifier)

Example 4 with ModuleLoader

use of com.github.anba.es6draft.runtime.modules.ModuleLoader in project es6draft by anba.

the class TestingFunctions method parseModule.

/**
 * shell-function: {@code parseModule(sourceCode)}
 *
 * @param cx
 *            the execution context
 * @param sourceCode
 *            the module source code
 * @return the module descriptor object
 */
@Function(name = "parseModule", arity = 1)
public ScriptObject parseModule(ExecutionContext cx, String sourceCode) throws MalformedNameException, IOException {
    ModuleLoader moduleLoader = cx.getRealm().getModuleLoader();
    SourceIdentifier sourceId = moduleLoader.normalizeName("parseModule" + moduleCounter.incrementAndGet(), null);
    StringModuleSource moduleSource = new StringModuleSource(sourceId, "<module>", sourceCode);
    ModuleRecord module = moduleLoader.define(sourceId, moduleSource, cx.getRealm());
    OrdinaryObject moduleObject = ObjectCreate(cx, (ScriptObject) null);
    if (module instanceof SourceTextModuleRecord) {
        Properties.createProperties(cx, moduleObject, new ModuleObjectProperties((SourceTextModuleRecord) module), ModuleObjectProperties.class);
        modules.put(moduleObject, (SourceTextModuleRecord) module);
    }
    SetIntegrityLevel(cx, moduleObject, IntegrityLevel.Frozen);
    return moduleObject;
}
Also used : SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord) ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) BiFunction(java.util.function.BiFunction) Function(com.github.anba.es6draft.runtime.internal.Properties.Function) BuiltinFunction(com.github.anba.es6draft.runtime.types.builtins.BuiltinFunction)

Example 5 with ModuleLoader

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

Aggregations

ModuleLoader (com.github.anba.es6draft.runtime.modules.ModuleLoader)12 SourceIdentifier (com.github.anba.es6draft.runtime.modules.SourceIdentifier)11 Realm (com.github.anba.es6draft.runtime.Realm)7 ModuleRecord (com.github.anba.es6draft.runtime.modules.ModuleRecord)7 Function (com.github.anba.es6draft.runtime.internal.Properties.Function)6 SourceTextModuleRecord (com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord)6 Source (com.github.anba.es6draft.runtime.internal.Source)5 ModuleSource (com.github.anba.es6draft.runtime.modules.ModuleSource)5 StringModuleSource (com.github.anba.es6draft.runtime.modules.loader.StringModuleSource)5 Script (com.github.anba.es6draft.Script)4 IOException (java.io.IOException)3 Executable (com.github.anba.es6draft.Executable)2 NodeModuleLoader (com.github.anba.es6draft.repl.loader.NodeModuleLoader)2 World (com.github.anba.es6draft.runtime.World)2 DebugInfo (com.github.anba.es6draft.runtime.internal.DebugInfo)2 AliasFunction (com.github.anba.es6draft.runtime.internal.Properties.AliasFunction)2 ScriptLoader (com.github.anba.es6draft.runtime.internal.ScriptLoader)2 ResolutionException (com.github.anba.es6draft.runtime.modules.ResolutionException)2 FileSourceIdentifier (com.github.anba.es6draft.runtime.modules.loader.FileSourceIdentifier)2 RealmObject (com.github.anba.es6draft.runtime.objects.reflect.RealmObject)2