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