use of com.github.anba.es6draft.runtime.objects.GlobalObject 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();
/* 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 */
Realm realm = CreateRealmAndSetRealmGlobalObject(calleeContext, realmObject, newGlobal, newGlobal);
/* step 17 (Moved before extracting extension hooks to avoid uninitialized object state) */
realmObject.setRealm(realm);
// Run any initialization scripts, if required. But do _not_ install extensions!
try {
GlobalObject globalTemplate = realm.getGlobalObjectTemplate();
assert globalTemplate != null;
globalTemplate.initializeScripted();
} catch (ParserException | CompilationException e) {
throw e.toScriptException(calleeContext);
} catch (IOException | URISyntaxException e) {
throw newError(calleeContext, e.getMessage());
}
/* 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.objects.GlobalObject 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(cx.getRealm().sourceInfo(caller), "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.objects.GlobalObject in project es6draft by anba.
the class ShellFunctions method evalScript.
/**
* shell-function: {@code evalScript(sourceString, [options])}
*
* @param cx
* the 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, 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(name, line);
Script script = realm.getScriptLoader().script(source, sourceString);
return script.evaluate(realm);
}
use of com.github.anba.es6draft.runtime.objects.GlobalObject 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(), ""));
}
}
Aggregations