Search in sources :

Example 81 with ExecutionContext

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;
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) World(com.github.anba.es6draft.runtime.World) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) FileModuleSource(com.github.anba.es6draft.runtime.modules.loader.FileModuleSource) NodeStandardModuleLoader(com.github.anba.es6draft.repl.loader.NodeStandardModuleLoader) FileModuleLoader(com.github.anba.es6draft.runtime.modules.loader.FileModuleLoader) InitializeHostDefinedRealm(com.github.anba.es6draft.runtime.Realm.InitializeHostDefinedRealm) Realm(com.github.anba.es6draft.runtime.Realm) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) FileModuleSource(com.github.anba.es6draft.runtime.modules.loader.FileModuleSource) Path(java.nio.file.Path) ParserException(com.github.anba.es6draft.parser.ParserException) ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) NodeStandardModuleLoader(com.github.anba.es6draft.repl.loader.NodeStandardModuleLoader) FileModuleLoader(com.github.anba.es6draft.runtime.modules.loader.FileModuleLoader) NodeModuleLoader(com.github.anba.es6draft.repl.loader.NodeModuleLoader) NodeModuleLoader(com.github.anba.es6draft.repl.loader.NodeModuleLoader) FileSourceIdentifier(com.github.anba.es6draft.runtime.modules.loader.FileSourceIdentifier) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) RealmData(com.github.anba.es6draft.runtime.RealmData) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext)

Example 82 with ExecutionContext

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));
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) Completion(com.github.anba.es6draft.repl.console.ShellConsole.Completion) Matcher(java.util.regex.Matcher) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ToObject(com.github.anba.es6draft.runtime.AbstractOperations.ToObject)

Example 83 with ExecutionContext

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);
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject)

Example 84 with ExecutionContext

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));
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject)

Example 85 with ExecutionContext

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);
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) StringObject(com.github.anba.es6draft.runtime.types.builtins.StringObject) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject)

Aggregations

ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)95 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)61 Realm (com.github.anba.es6draft.runtime.Realm)17 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)16 LexicalEnvironment (com.github.anba.es6draft.runtime.LexicalEnvironment)15 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)14 AbstractOperations (com.github.anba.es6draft.runtime.AbstractOperations)11 Declaration (com.github.anba.es6draft.ast.Declaration)10 HoistableDeclaration (com.github.anba.es6draft.ast.HoistableDeclaration)10 Name (com.github.anba.es6draft.ast.scope.Name)10 Callable (com.github.anba.es6draft.runtime.types.Callable)10 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)10 HashSet (java.util.HashSet)10 StatementListItem (com.github.anba.es6draft.ast.StatementListItem)9 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)9 TryCatchLabel (com.github.anba.es6draft.compiler.assembler.TryCatchLabel)8 DeclarativeEnvironmentRecord (com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)8 ScriptException (com.github.anba.es6draft.runtime.internal.ScriptException)8 ArrayDeque (java.util.ArrayDeque)8 FunctionDeclaration (com.github.anba.es6draft.ast.FunctionDeclaration)7