Search in sources :

Example 61 with Realm

use of com.github.anba.es6draft.runtime.Realm in project es6draft by anba.

the class ShellGlobalObject method eval.

/**
     * Parses, compiles and executes the javascript module file.
     * 
     * @param moduleName
     *            the unnormalized module name
     * @throws IOException
     *             if there was any I/O error
     * @throws MalformedNameException
     *             if any imported module request cannot be normalized
     * @throws ResolutionException
     *             if any export binding cannot be resolved
     * @throws ParserException
     *             if the module source contains any syntax errors
     * @throws CompilationException
     *             if the parsed module source cannot be compiled
     */
public void eval(String moduleName) throws IOException, MalformedNameException, ResolutionException, ParserException, CompilationException {
    Realm realm = getRealm();
    ModuleLoader moduleLoader = realm.getModuleLoader();
    SourceIdentifier moduleId = moduleLoader.normalizeName(moduleName, null);
    ModuleRecord module = moduleLoader.resolve(moduleId, realm);
    module.instantiate();
    module.evaluate();
}
Also used : ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) Realm(com.github.anba.es6draft.runtime.Realm)

Example 62 with Realm

use of com.github.anba.es6draft.runtime.Realm in project es6draft by anba.

the class MozShellFunctions method evaluate.

/**
     * shell-function: {@code evaluate(code, [options])}
     *
     * @param cx
     *            the execution context
     * @param caller
     *            the caller context
     * @param code
     *            the source code to evaluate
     * @param options
     *            additional options object
     * @return the eval result value
     */
@Function(name = "evaluate", arity = 2)
public Object evaluate(ExecutionContext cx, ExecutionContext caller, Object code, Object options) {
    if (!(Type.isString(code) && (Type.isUndefined(options) || Type.isObject(options)))) {
        throw Errors.newError(cx, "invalid arguments");
    }
    String sourceCode = Type.stringValue(code).toString();
    String sourceName = "@evaluate";
    int sourceLine = 1;
    boolean noScriptRval = false;
    boolean catchTermination = false;
    Realm realm = cx.getRealm();
    if (Type.isObject(options)) {
        ScriptObject opts = Type.objectValue(options);
        Object fileName = Get(cx, opts, "fileName");
        if (!Type.isUndefined(fileName)) {
            sourceName = Type.isNull(fileName) ? "" : ToFlatString(cx, fileName);
        }
        Object lineNumber = Get(cx, opts, "lineNumber");
        if (!Type.isUndefined(lineNumber)) {
            sourceLine = ToInt32(cx, lineNumber);
        }
        Object g = Get(cx, opts, "global");
        if (!Type.isUndefined(g)) {
            ScriptObject obj = ToObject(cx, g);
            if (!(obj instanceof GlobalObject)) {
                throw Errors.newError(cx, "invalid global argument");
            }
            realm = ((GlobalObject) obj).getRealm();
        }
        noScriptRval = ToBoolean(Get(cx, opts, "noScriptRval"));
        catchTermination = ToBoolean(Get(cx, opts, "catchTermination"));
    }
    Source source = new Source(cx.getRealm().sourceInfo(caller), sourceName, sourceLine);
    try {
        Script script = realm.getScriptLoader().script(source, sourceCode);
        Object result = script.evaluate(realm);
        return (!noScriptRval ? result : UNDEFINED);
    } catch (ParserException | CompilationException e) {
        // Create a script exception from the requested code realm, not from the caller's realm.
        throw e.toScriptException(realm.defaultContext());
    } catch (ScriptException | StackOverflowError e) {
        throw e;
    } catch (Error | Exception e) {
        if (catchTermination) {
            return "terminated";
        }
        throw Errors.newError(cx, Objects.toString(e.getMessage(), ""));
    }
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) GlobalObject(com.github.anba.es6draft.runtime.objects.GlobalObject) SharedFunctions.relativePathToScript(com.github.anba.es6draft.repl.global.SharedFunctions.relativePathToScript) Script(com.github.anba.es6draft.Script) SharedFunctions.loadScript(com.github.anba.es6draft.repl.global.SharedFunctions.loadScript) ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) ToSource(com.github.anba.es6draft.repl.SourceBuilder.ToSource) Source(com.github.anba.es6draft.runtime.internal.Source) URISyntaxException(java.net.URISyntaxException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) ParserException(com.github.anba.es6draft.parser.ParserException) IOException(java.io.IOException) MalformedNameException(com.github.anba.es6draft.runtime.modules.MalformedNameException) ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ProxyObject(com.github.anba.es6draft.runtime.types.builtins.ProxyObject) GlobalObject(com.github.anba.es6draft.runtime.objects.GlobalObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) WeakMapObject(com.github.anba.es6draft.runtime.objects.collection.WeakMapObject) BoundFunctionObject(com.github.anba.es6draft.runtime.types.builtins.BoundFunctionObject) Realm(com.github.anba.es6draft.runtime.Realm) Function(com.github.anba.es6draft.runtime.internal.Properties.Function) BuiltinFunction(com.github.anba.es6draft.runtime.types.builtins.BuiltinFunction)

Example 63 with Realm

use of com.github.anba.es6draft.runtime.Realm in project es6draft by anba.

the class ShellFunctions method loadModule.

/**
     * shell-function: {@code loadModule(moduleName, [realmObject])}
     * 
     * @param cx
     *            the execution context
     * @param moduleName
     *            the module name
     * @param realmObject
     *            the optional realm object
     * @return the module namespace object
     * @throws MalformedNameException
     *             if any imported module request cannot be normalized
     * @throws ResolutionException
     *             if any export binding cannot be resolved
     */
@Function(name = "loadModule", arity = 1)
public ScriptObject loadModule(ExecutionContext cx, String moduleName, Object realmObject) throws MalformedNameException, ResolutionException {
    Realm realm;
    if (!Type.isUndefined(realmObject)) {
        if (!(realmObject instanceof RealmObject)) {
            throw Errors.newTypeError(cx, Messages.Key.IncompatibleObject);
        }
        realm = ((RealmObject) realmObject).getRealm();
    } else {
        realm = cx.getRealm();
    }
    try {
        ModuleLoader moduleLoader = realm.getModuleLoader();
        SourceIdentifier moduleId = moduleLoader.normalizeName(moduleName, null);
        ModuleRecord module = moduleLoader.resolve(moduleId, realm);
        module.instantiate();
        module.evaluate();
        return GetModuleNamespace(cx, module);
    } catch (IOException e) {
        throw Errors.newInternalError(cx, e, Messages.Key.ModulesIOException, e.getMessage());
    }
}
Also used : ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) RealmObject(com.github.anba.es6draft.runtime.objects.reflect.RealmObject) IOException(java.io.IOException) Realm(com.github.anba.es6draft.runtime.Realm) Function(com.github.anba.es6draft.runtime.internal.Properties.Function)

Example 64 with Realm

use of com.github.anba.es6draft.runtime.Realm 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)

Example 65 with Realm

use of com.github.anba.es6draft.runtime.Realm in project es6draft by anba.

the class SourceTextModuleRecord method evaluate.

/**
     * 15.2.1.16.5 ModuleEvaluation() Concrete Method
     */
@Override
public Object evaluate() throws IOException, MalformedNameException, ResolutionException {
    /* step 1 */
    SourceTextModuleRecord module = this;
    // assert module.instantiated;
    assert module.environment != null : "module is not instantiated";
    /* step 3 */
    Realm realm = module.realm;
    assert realm != null : "module is not linked";
    /* step 4 */
    if (module.evaluated) {
        return UNDEFINED;
    }
    /* step 5 */
    module.evaluated = true;
    // ModuleDeclarationInstantiation did not complete successfully - stop evaluation.
    if (!this.instantiated) {
        return UNDEFINED;
    }
    /* step 6 */
    for (String required : module.requestedModules) {
        /* steps 6.a-b */
        ModuleRecord requiredModule = HostResolveImportedModule(module, required);
        /* steps 6.c-d */
        requiredModule.evaluate();
    }
    /* steps 7-12 */
    ExecutionContext moduleContext = newModuleExecutionContext(realm, module);
    /* steps 13-14 */
    ExecutionContext oldScriptContext = realm.getScriptContext();
    try {
        realm.setScriptContext(moduleContext);
        /* step 15 */
        Object result = module.scriptCode.evaluate(moduleContext);
        /* step 18 */
        return result;
    } finally {
        /* steps 16-17 */
        realm.setScriptContext(oldScriptContext);
    }
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ExecutionContext.newModuleDeclarationExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext.newModuleDeclarationExecutionContext) ExecutionContext.newModuleExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext.newModuleExecutionContext) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) Realm(com.github.anba.es6draft.runtime.Realm)

Aggregations

Realm (com.github.anba.es6draft.runtime.Realm)96 Test (org.junit.Test)39 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)17 Script (com.github.anba.es6draft.Script)16 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)16 Source (com.github.anba.es6draft.runtime.internal.Source)15 ParserException (com.github.anba.es6draft.parser.ParserException)9 Function (com.github.anba.es6draft.runtime.internal.Properties.Function)9 ModuleRecord (com.github.anba.es6draft.runtime.modules.ModuleRecord)8 ModuleLoader (com.github.anba.es6draft.runtime.modules.ModuleLoader)7 IOException (java.io.IOException)7 CompilationException (com.github.anba.es6draft.compiler.CompilationException)6 ScriptException (com.github.anba.es6draft.runtime.internal.ScriptException)6 ToSource (com.github.anba.es6draft.repl.SourceBuilder.ToSource)5 World (com.github.anba.es6draft.runtime.World)5 RuntimeContext (com.github.anba.es6draft.runtime.internal.RuntimeContext)5 ModuleSource (com.github.anba.es6draft.runtime.modules.ModuleSource)5 SourceIdentifier (com.github.anba.es6draft.runtime.modules.SourceIdentifier)5 GlobalObject (com.github.anba.es6draft.runtime.objects.GlobalObject)5 ExecutionContext.newEvalExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext.newEvalExecutionContext)4