use of com.github.anba.es6draft.runtime.RealmData in project es6draft by anba.
the class Repl method newRealm.
private Realm newRealm() {
Supplier<RuntimeContext.Data> runtimeData;
Function<Realm, ? extends RealmData> realmData;
if (options.shellMode == ShellMode.Mozilla) {
runtimeData = RuntimeContext.Data::new;
realmData = MozShellRealmData::new;
} else if (options.shellMode == ShellMode.V8) {
runtimeData = RuntimeContext.Data::new;
realmData = V8ShellRealmData::new;
} else {
runtimeData = ShellContextData::new;
realmData = ShellRealmData::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()).setRuntimeData(runtimeData).setRealmData(realmData).setModuleLoader(moduleLoader).setConsole(console).setErrorReporter(this::errorReporter).setWorkerErrorReporter(this::errorReporter).setOptions(compatibilityOptions(options)).setParserOptions(parserOptions(options)).setCompilerOptions(compilerOptions(options)).build();
/* @formatter:on */
World world = new World(context);
Realm realm = new Realm(world);
ExecutionContext cx = realm.defaultContext();
// Add completion to console
console.addCompleter(new ShellCompleter(realm));
// Execute global specific initialization
enqueueScriptJob(realm, () -> {
InitializeHostDefinedRealm(realm);
// Add global "arguments" property
ScriptObject arguments = CreateArrayFromList(cx, options.arguments);
CreateMethodProperty(cx, realm.getGlobalObject(), "arguments", arguments);
});
if (options.console) {
enqueueScriptJob(realm, () -> {
ScriptObject consoleObj = ConsoleObject.createConsole(realm, !options.noColor);
CreateMethodProperty(cx, realm.getGlobalObject(), "console", consoleObj);
});
}
// Run eval expressions and files
for (EvalScript evalScript : options.evalScripts) {
if (evalScript.getType() == EvalScript.Type.Module) {
enqueueScriptJob(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 (source.getFile() != null && file.equals(source.getFile().toString())) {
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 {
enqueueScriptJob(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;
}
Aggregations