use of com.github.anba.es6draft.runtime.ExecutionContext in project es6draft by anba.
the class CodeGenerator method compile.
MethodName compile(BlockStatement node, List<Declaration> declarations, BlockDeclarationInstantiationGenerator generator) {
MethodCode method = newMethod2(node);
BlockDeclInitVisitor body = new BlockDeclInitVisitor(method);
body.lineInfo(node);
body.begin();
Variable<ExecutionContext> cx = body.getExecutionContext();
Variable<LexicalEnvironment<DeclarativeEnvironmentRecord>> env = body.getLexicalEnvironment();
generator.generateMethod(declarations, cx, env, body);
body._return();
body.end();
return methodDesc(node, method.methodName);
}
use of com.github.anba.es6draft.runtime.ExecutionContext in project es6draft by anba.
the class Repl method newRealm.
private Realm newRealm() {
ObjectAllocator<? extends ShellGlobalObject> allocator;
if (options.shellMode == ShellMode.Mozilla) {
allocator = MozShellGlobalObject::new;
} else if (options.shellMode == ShellMode.V8) {
allocator = V8ShellGlobalObject::new;
} else {
allocator = SimpleShellGlobalObject::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()).setGlobalAllocator(allocator).setModuleLoader(moduleLoader).setConsole(console).setWorkerErrorReporter(this::errorReporter).setOptions(compatibilityOptions(options)).setParserOptions(parserOptions(options)).setCompilerOptions(compilerOptions(options)).build();
/* @formatter:on */
World world = new World(context);
Realm realm = world.newRealm();
ExecutionContext cx = realm.defaultContext();
// Add completion to console
console.addCompleter(new ShellCompleter(realm));
// Execute global specific initialization
enqueueScriptTask(realm, () -> {
InitializeHostDefinedRealm(realm);
// Add global "arguments" property
ScriptObject arguments = CreateArrayFromList(cx, options.arguments);
CreateMethodProperty(cx, realm.getGlobalObject(), "arguments", arguments);
});
if (options.console) {
enqueueScriptTask(realm, () -> {
ScriptObject console = ConsoleObject.createConsole(realm);
CreateMethodProperty(cx, realm.getGlobalObject(), "console", console);
});
}
if (options.moduleLoaderMode == ModuleLoaderMode.Node) {
// TODO: Add default initialize(Realm) method to ModuleLoader interface?
enqueueScriptTask(realm, () -> {
NodeModuleLoader nodeLoader = (NodeModuleLoader) world.getModuleLoader();
nodeLoader.initialize(realm);
});
}
// Run eval expressions and files
for (EvalScript evalScript : options.evalScripts) {
if (options.module) {
enqueueScriptTask(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 (file.equals(source.getFileString())) {
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 {
enqueueScriptTask(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;
}
use of com.github.anba.es6draft.runtime.ExecutionContext in project es6draft by anba.
the class ShellCompleter method complete.
@Override
public Optional<Completion> complete(String line, int cursor) {
ExecutionContext cx = realm.defaultContext();
ScriptObject object = realm.getGlobalThis();
String leftContext = line.substring(0, cursor);
if (leftContext.isEmpty()) {
ArrayList<String> candidates = createCandidates(getPropertyNames(cx, object), "", "");
return Optional.of(new Completion(line, 0, cursor, candidates));
}
Matcher m = hierarchyPattern.matcher(leftContext);
if (!m.find()) {
return Optional.empty();
}
ArrayList<String> segments = segments(m.group(1));
StringBuilder prefix = new StringBuilder();
List<String> properties = segments.subList(0, segments.size() - 1);
if (!properties.isEmpty() && "this".equals(properties.get(0))) {
// skip leading `this` segment in property traversal
properties = properties.subList(1, properties.size());
prefix.append("this.");
}
for (String property : properties) {
if (!HasProperty(cx, object, property)) {
return Optional.empty();
}
Object value = Get(cx, object, property);
if (Type.isObject(value)) {
object = Type.objectValue(value);
} else if (!Type.isUndefinedOrNull(value)) {
object = ToObject(cx, value);
} else {
return Optional.empty();
}
prefix.append(property).append('.');
}
String partial = segments.get(segments.size() - 1);
ArrayList<String> candidates = createCandidates(getPropertyNames(cx, object), partial, prefix.toString());
return Optional.of(new Completion(line, m.start(1), cursor, candidates));
}
use of com.github.anba.es6draft.runtime.ExecutionContext in project es6draft by anba.
the class ConsoleObject method createConsole.
/**
* Creates a new {@code console} object.
*
* @param realm
* the realm instance
* @return the console object
* @throws IOException
* if there was any I/O error
* @throws URISyntaxException
* the URL is not a valid URI
* @throws MalformedNameException
* if any imported module request cannot be normalized
* @throws ResolutionException
* if any export binding cannot be resolved
*/
public static ScriptObject createConsole(Realm realm) throws IOException, URISyntaxException, MalformedNameException, ResolutionException {
ExecutionContext cx = realm.defaultContext();
ModuleRecord module = NativeCode.loadModule(realm, "console.jsm");
ScriptObject console = NativeCode.getModuleExport(module, "default", ScriptObject.class);
Callable inspectFn = Properties.createFunction(cx, new InspectFunction(), InspectFunction.class);
CreateMethodProperty(cx, console, "_inspect", inspectFn);
return console;
}
use of com.github.anba.es6draft.runtime.ExecutionContext in project es6draft by anba.
the class InterpretedScriptBody method evalScriptEvaluation.
/**
* 18.2.1.1 Runtime Semantics: PerformEval( x, evalRealm, strictCaller, direct)
*
* @param cx
* the execution context
* @param script
* the script object
* @return the script evaluation result
*/
private Object evalScriptEvaluation(ExecutionContext cx, Script script) {
// TODO: Skip allocating lex-env if not needed
/* steps 1-5 (not applicable) */
/* steps 6-7 */
boolean strictEval = parsedScript.isStrict();
/* step 8 (omitted) */
/* steps 9-10 */
LexicalEnvironment<DeclarativeEnvironmentRecord> lexEnv;
LexicalEnvironment<?> varEnv;
if (parsedScript.isDirectEval()) {
/* step 9 */
lexEnv = newDeclarativeEnvironment(cx.getLexicalEnvironment());
varEnv = cx.getVariableEnvironment();
} else {
Realm evalRealm = cx.getRealm();
/* step 10 */
lexEnv = newDeclarativeEnvironment(evalRealm.getGlobalEnv());
varEnv = evalRealm.getGlobalEnv();
}
/* step 11 */
if (strictEval) {
varEnv = lexEnv;
}
/* steps 12-17 */
ExecutionContext evalCxt = newEvalExecutionContext(cx, script, varEnv, lexEnv);
/* step 18 */
EvalDeclarationInstantiation(evalCxt, parsedScript, varEnv, lexEnv);
/* steps 19-23 */
return parsedScript.accept(new Interpreter(parsedScript), evalCxt);
}
Aggregations