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);
}
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;
}
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());
}
}
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(), ""));
}
}
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);
}
Aggregations