use of com.github.anba.es6draft.runtime.objects.reflect.RealmObject 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.reflect.RealmObject 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);
}
use of com.github.anba.es6draft.runtime.objects.reflect.RealmObject 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());
}
}
use of com.github.anba.es6draft.runtime.objects.reflect.RealmObject 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());
}
}
Aggregations