Search in sources :

Example 51 with Realm

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

the class ReflectParser method parse.

/**
 * Parses the given script code and returns the matching Reflect AST nodes.
 *
 * @param cx
 *            the execution context
 * @param sourceCode
 *            the source string
 * @param location
 *            if set to {@code true} node locations will be recorded
 * @param sourceInfo
 *            the source info string
 * @param line
 *            the start line offset
 * @param builder
 *            map to customize AST node processing
 * @param target
 *            the parse target
 * @return the parsed node
 */
private static Object parse(ExecutionContext cx, String sourceCode, boolean location, String sourceInfo, int line, EnumMap<Type, Callable> builder, Target target) {
    Realm realm = cx.getRealm();
    ReflectParser reflect = new ReflectParser(cx, location, sourceInfo, builder);
    Source source = new Source("<parse>", line);
    TopLevelNode<?> parsedNode;
    try {
        if (target == Target.Script) {
            parsedNode = realm.getScriptLoader().parseScript(source, sourceCode);
        } else {
            assert target == Target.Module;
            parsedNode = realm.getScriptLoader().parseModule(source, sourceCode);
        }
    } catch (ParserException e) {
        throw e.toScriptException(cx);
    }
    return parsedNode.accept(reflect, null);
}
Also used : ParserException(com.github.anba.es6draft.parser.ParserException) Realm(com.github.anba.es6draft.runtime.Realm) Source(com.github.anba.es6draft.runtime.internal.Source)

Example 52 with Realm

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

the class RealmConstructor method construct.

/**
 * 26.?.1.1 Reflect.Realm ( [ target , handler ] )
 */
@Override
public RealmObject construct(ExecutionContext callerContext, Constructor newTarget, Object... args) {
    ExecutionContext calleeContext = calleeContext();
    // Disable Realm constructor if only FrozenRealm option is enabled.
    if (!getRealm().getRuntimeContext().isEnabled(CompatibilityOption.Realm)) {
        throw newTypeError(calleeContext(), Messages.Key.InvalidCall, "Realm");
    }
    /* steps 2-3 */
    RealmObject realmObject = OrdinaryCreateFromConstructor(calleeContext, newTarget, Intrinsics.RealmPrototype, RealmObject::new);
    /* steps 4-5 */
    ScriptObject newGlobal;
    if (args.length != 0) {
        /* step 4 */
        Object target = argument(args, 0);
        Object handler = argument(args, 1);
        newGlobal = ProxyCreate(calleeContext, target, handler);
    } else {
        /* step 5 */
        newGlobal = null;
    }
    /* steps 6-7, 17 (Moved before extracting extension hooks to avoid uninitialized object state) */
    Realm realm = CreateRealmAndSetRealmGlobalObject(calleeContext, realmObject, newGlobal, newGlobal);
    /* steps 8-9 */
    Callable translate = GetMethod(calleeContext, realmObject, "directEval");
    /* steps 10-11 */
    Callable fallback = GetMethod(calleeContext, realmObject, "nonEval");
    /* steps 12-13 */
    Callable indirectEval = GetMethod(calleeContext, realmObject, "indirectEval");
    /* steps 14-16 */
    realm.setExtensionHooks(translate, fallback, indirectEval);
    /* steps 18-19 */
    Callable initGlobal = GetMethod(calleeContext, realmObject, "initGlobal");
    /* steps 20-21 */
    if (initGlobal != null) {
        /* step 20 */
        initGlobal.call(calleeContext, realmObject);
    } else {
        /* step 21 */
        SetDefaultGlobalBindings(calleeContext, realm);
    }
    /* step 22 */
    return realmObject;
}
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) CreateRealmAndSetRealmGlobalObject(com.github.anba.es6draft.runtime.Realm.CreateRealmAndSetRealmGlobalObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) Realm(com.github.anba.es6draft.runtime.Realm) Callable(com.github.anba.es6draft.runtime.types.Callable)

Example 53 with Realm

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

the class MozShellFunctions method evalcx.

/**
 * shell-function: {@code evalcx(s, [o])}
 *
 * @param cx
 *            the execution context
 * @param caller
 *            the caller context
 * @param sourceCode
 *            the source to evaluate
 * @param o
 *            the global object
 * @return the eval result value
 */
@Function(name = "evalcx", arity = 1)
public Object evalcx(ExecutionContext cx, ExecutionContext caller, String sourceCode, Object o) {
    ScriptObject global;
    if (Type.isUndefinedOrNull(o)) {
        global = newGlobal(cx);
    } else {
        global = ToObject(cx, o);
    }
    if (sourceCode.isEmpty() || "lazy".equals(sourceCode)) {
        return global;
    }
    if (!(global instanceof GlobalObject)) {
        throw Errors.newError(cx, "invalid global argument");
    }
    Source source = new Source(caller.sourceInfo(), "evalcx", 1);
    Realm realm = ((GlobalObject) global).getRealm();
    try {
        Script script = realm.getScriptLoader().script(source, sourceCode);
        return script.evaluate(realm);
    } catch (ParserException | CompilationException e) {
        // Create a script exception from the requested code realm, not from the caller's realm.
        throw e.toScriptException(realm.defaultContext());
    }
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) GlobalObject(com.github.anba.es6draft.runtime.objects.GlobalObject) SharedFunctions.loadScript(com.github.anba.es6draft.repl.functions.SharedFunctions.loadScript) SharedFunctions.relativePathToScript(com.github.anba.es6draft.repl.functions.SharedFunctions.relativePathToScript) Script(com.github.anba.es6draft.Script) ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) Realm(com.github.anba.es6draft.runtime.Realm) ToSource(com.github.anba.es6draft.repl.SourceBuilder.ToSource) Source(com.github.anba.es6draft.runtime.internal.Source) Function(com.github.anba.es6draft.runtime.internal.Properties.Function) BuiltinFunction(com.github.anba.es6draft.runtime.types.builtins.BuiltinFunction)

Example 54 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(caller.sourceInfo(), 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.loadScript(com.github.anba.es6draft.repl.functions.SharedFunctions.loadScript) SharedFunctions.relativePathToScript(com.github.anba.es6draft.repl.functions.SharedFunctions.relativePathToScript) Script(com.github.anba.es6draft.Script) 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) 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 55 with Realm

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

the class ShellFunctions method evalScript.

/**
 * shell-function: {@code evalScript(sourceString, [options])}
 *
 * @param cx
 *            the execution context
 * @param caller
 *            the caller execution context
 * @param sourceString
 *            the source string
 * @param options
 *            the options object (optional)
 * @return the evaluation result
 */
@Function(name = "evalScript", arity = 1)
public Object evalScript(ExecutionContext cx, ExecutionContext caller, String sourceString, Object options) {
    String name = "";
    int line = 1;
    Realm realm = cx.getRealm();
    if (Type.isObject(options)) {
        ScriptObject opts = Type.objectValue(options);
        Object fileName = Get(cx, opts, "fileName");
        if (!Type.isUndefined(fileName)) {
            name = ToFlatString(cx, fileName);
        }
        Object lineNumber = Get(cx, opts, "lineNumber");
        if (!Type.isUndefined(lineNumber)) {
            line = ToInt32(cx, lineNumber);
        }
        Object g = Get(cx, opts, "global");
        if (!Type.isUndefined(g)) {
            if (!(g instanceof GlobalObject)) {
                throw Errors.newError(cx, "invalid global argument");
            }
            realm = ((GlobalObject) g).getRealm();
        }
        Object r = Get(cx, opts, "realm");
        if (!Type.isUndefined(r)) {
            if (!(r instanceof RealmObject)) {
                throw Errors.newError(cx, "invalid realm argument");
            }
            realm = ((RealmObject) r).getRealm();
        }
    }
    Source source = new Source(caller.sourceInfo(), name, line);
    Script script = realm.getScriptLoader().script(source, sourceString);
    return script.evaluate(realm);
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) GlobalObject(com.github.anba.es6draft.runtime.objects.GlobalObject) SharedFunctions.loadScript(com.github.anba.es6draft.repl.functions.SharedFunctions.loadScript) CompiledScript(com.github.anba.es6draft.compiler.CompiledScript) SharedFunctions.relativePathToScript(com.github.anba.es6draft.repl.functions.SharedFunctions.relativePathToScript) Script(com.github.anba.es6draft.Script) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) WeakMapObject(com.github.anba.es6draft.runtime.objects.collection.WeakMapObject) ErrorObject(com.github.anba.es6draft.runtime.objects.ErrorObject) GlobalObject(com.github.anba.es6draft.runtime.objects.GlobalObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) ArrayBufferObject(com.github.anba.es6draft.runtime.objects.binary.ArrayBufferObject) RealmObject(com.github.anba.es6draft.runtime.objects.reflect.RealmObject) RealmObject(com.github.anba.es6draft.runtime.objects.reflect.RealmObject) Realm(com.github.anba.es6draft.runtime.Realm) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) ToSource(com.github.anba.es6draft.repl.SourceBuilder.ToSource) Source(com.github.anba.es6draft.runtime.internal.Source) Function(com.github.anba.es6draft.runtime.internal.Properties.Function) AliasFunction(com.github.anba.es6draft.runtime.internal.Properties.AliasFunction)

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