use of com.github.anba.es6draft.runtime.internal.RuntimeContext 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.internal.RuntimeContext in project es6draft by anba.
the class TestGlobals method compileModules.
private PreloadModules compileModules() throws IOException, MalformedNameException {
List<?> moduleNames = configuration.getList("modules", emptyList());
if (moduleNames.isEmpty()) {
return new PreloadModules(Collections.<ModuleRecord>emptyList(), Collections.<ModuleRecord>emptyList());
}
RuntimeContext context = createContext();
ScriptLoader scriptLoader = new ScriptLoader(context);
TestModuleLoader<?> moduleLoader = this.moduleLoader.apply(context, scriptLoader);
ArrayList<ModuleRecord> modules = new ArrayList<>();
for (String moduleName : nonEmpty(moduleNames)) {
SourceIdentifier moduleId = moduleLoader.normalizeName(moduleName, null);
modules.add(moduleLoader.load(moduleId));
}
return new PreloadModules(modules, moduleLoader.getModules());
}
use of com.github.anba.es6draft.runtime.internal.RuntimeContext in project es6draft by anba.
the class ScriptEngineImpl method eval.
Object eval(Script script, ScriptContext context) throws javax.script.ScriptException {
Realm realm = getEvalRealm(context);
RuntimeContext runtimeContext = realm.getRuntimeContext();
Console console = runtimeContext.getConsole();
runtimeContext.setConsole(new ScriptingConsole(context));
try {
// Prepare a new execution context before calling the generated code.
ExecutionContext evalCxt = newScriptingExecutionContext(realm, script, new LexicalEnvironment<>(realm.getGlobalEnv(), new ScriptContextEnvironmentRecord(realm.defaultContext(), context)));
Object result = script.evaluate(evalCxt);
realm.getWorld().runEventLoop();
return TypeConverter.toJava(result);
} catch (ScriptException e) {
throw new javax.script.ScriptException(e);
} finally {
runtimeContext.setConsole(console);
}
}
use of com.github.anba.es6draft.runtime.internal.RuntimeContext in project es6draft by anba.
the class ScriptEngineImpl method invoke.
private Object invoke(ScriptObject thisValue, String name, Object... args) throws javax.script.ScriptException, NoSuchMethodException {
Realm realm = getEvalRealm(context);
RuntimeContext runtimeContext = realm.getRuntimeContext();
Console console = runtimeContext.getConsole();
runtimeContext.setConsole(new ScriptingConsole(context));
try {
Object[] arguments = TypeConverter.fromJava(args);
if (thisValue == null) {
thisValue = realm.getGlobalThis();
}
ExecutionContext cx = realm.defaultContext();
Object func = thisValue.get(cx, (Object) name, thisValue);
if (!IsCallable(func)) {
throw new NoSuchMethodException(name);
}
Object result = ((Callable) func).call(cx, thisValue, arguments);
realm.getWorld().runEventLoop();
return TypeConverter.toJava(result);
} catch (ScriptException e) {
throw new javax.script.ScriptException(e);
} finally {
runtimeContext.setConsole(console);
}
}
use of com.github.anba.es6draft.runtime.internal.RuntimeContext in project es6draft by anba.
the class DefaultLocaleTimezone method newRealm.
private static Realm newRealm(TimeZone timeZone) throws Exception {
RuntimeContext context = new RuntimeContext.Builder().setTimeZone(timeZone).build();
World world = new World(context);
return Realm.InitializeHostDefinedRealm(world);
}
Aggregations