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;
}
}
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();
}
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();
}
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;
}
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);
}
});
});
}
Aggregations