use of com.github.anba.es6draft.runtime.ExecutionContext 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;
}
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 RegExpConstructor method call.
/**
* 21.2.3.1 RegExp(pattern, flags)
*/
@Override
public ScriptObject call(ExecutionContext callerContext, Object thisValue, Object... args) {
ExecutionContext calleeContext = calleeContext();
Object pattern = argument(args, 0);
Object flags = argument(args, 1);
/* step 1 */
boolean patternIsRegExp = IsRegExp(calleeContext, pattern);
/* step 3 */
if (patternIsRegExp && Type.isUndefined(flags)) {
ScriptObject patternObject = Type.objectValue(pattern);
Object patternConstructor = Get(calleeContext, patternObject, "constructor");
if (this == patternConstructor) {
// SameValue
return patternObject;
}
}
/* steps 4-8 */
return RegExpCreate(calleeContext, this, pattern, flags, patternIsRegExp);
}
use of com.github.anba.es6draft.runtime.ExecutionContext in project es6draft by anba.
the class Uint32x4Constructor method call.
@Override
public Object call(ExecutionContext callerContext, Object thisValue, Object... args) {
ExecutionContext calleeContext = calleeContext();
Object[] fields = new Object[VECTOR_LENGTH];
for (int i = 0; i < VECTOR_LENGTH; ++i) {
fields[i] = i < args.length ? args[i] : UNDEFINED;
}
return SIMDCreateInt(calleeContext, SIMD_TYPE, fields, (cx, v) -> (int) ToUint32(cx, v));
}
use of com.github.anba.es6draft.runtime.ExecutionContext in project es6draft by anba.
the class StringConstructor method call.
/**
* 21.1.1.1 String ( value )
*/
@Override
public CharSequence call(ExecutionContext callerContext, Object thisValue, Object... args) {
ExecutionContext calleeContext = calleeContext();
/* step 1 */
if (args.length == 0) {
return "";
}
Object value = args[0];
/* step 2.a */
if (Type.isSymbol(value)) {
return SymbolDescriptiveString(calleeContext, Type.symbolValue(value));
}
/* steps 2.b, 3 */
return ToString(calleeContext, value);
}
Aggregations