Search in sources :

Example 41 with ExecutionContext

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);
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) LexicalEnvironment(com.github.anba.es6draft.runtime.LexicalEnvironment) MethodCode(com.github.anba.es6draft.compiler.assembler.Code.MethodCode)

Example 42 with ExecutionContext

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;
}
Also used : Path(java.nio.file.Path) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) 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) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) World(com.github.anba.es6draft.runtime.World) MozShellGlobalObject(com.github.anba.es6draft.repl.global.MozShellGlobalObject) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) NodeStandardModuleLoader(com.github.anba.es6draft.repl.loader.NodeStandardModuleLoader) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) 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)

Example 43 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 44 with ExecutionContext

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

Example 45 with ExecutionContext

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);
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ExecutionContext.newScriptExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext.newScriptExecutionContext) ExecutionContext.newEvalExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext.newEvalExecutionContext) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) Realm(com.github.anba.es6draft.runtime.Realm)

Aggregations

ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)70 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)46 AbstractOperations (com.github.anba.es6draft.runtime.AbstractOperations)10 Realm (com.github.anba.es6draft.runtime.Realm)10 Callable (com.github.anba.es6draft.runtime.types.Callable)10 LexicalEnvironment (com.github.anba.es6draft.runtime.LexicalEnvironment)9 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)8 ScriptException (com.github.anba.es6draft.runtime.internal.ScriptException)7 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)6 Name (com.github.anba.es6draft.ast.scope.Name)5 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)5 HashSet (java.util.HashSet)5 Declaration (com.github.anba.es6draft.ast.Declaration)4 HoistableDeclaration (com.github.anba.es6draft.ast.HoistableDeclaration)4 StatementListItem (com.github.anba.es6draft.ast.StatementListItem)4 MethodCode (com.github.anba.es6draft.compiler.assembler.Code.MethodCode)4 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)4 TryCatchLabel (com.github.anba.es6draft.compiler.assembler.TryCatchLabel)4 IsCallable (com.github.anba.es6draft.runtime.AbstractOperations.IsCallable)4 ToObject (com.github.anba.es6draft.runtime.AbstractOperations.ToObject)4