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 Repl method loop.
/**
* REPL: Loop
*/
private void loop() throws InterruptedException {
Realm realm = newRealm();
World world = realm.getWorld();
JobSource jobSource = createJobSource(realm);
for (; ; ) {
try {
world.runEventLoop(jobSource);
return;
} catch (StopExecutionException e) {
if (e.getReason() == Reason.Quit) {
System.exit(0);
}
} catch (ParserExceptionWithSource e) {
handleException(e);
} catch (ScriptException e) {
handleException(realm.defaultContext(), e);
} catch (UnhandledRejectionException e) {
handleException(realm.defaultContext(), e);
} catch (StackOverflowError e) {
handleException(realm.defaultContext(), e);
} catch (OutOfMemoryError e) {
handleException(realm.defaultContext(), e);
} catch (InternalException e) {
handleException(e);
} catch (BootstrapMethodError e) {
handleException(e.getCause());
} catch (UncheckedIOException e) {
handleException(e.getCause());
}
}
}
use of com.github.anba.es6draft.runtime.Realm in project es6draft by anba.
the class ZoneConstructor method construct.
/**
* Zone ( options )
*/
@Override
public ZoneObject construct(ExecutionContext callerContext, Constructor newTarget, Object... args) {
ExecutionContext calleeContext = calleeContext();
Object options = argument(args, 0);
/* step 1 (not applicable) */
/* step 2 */
Realm functionRealm = getRealm();
/* step 3 */
CharSequence name = "(unnamed zone)";
/* step 4 */
ZoneObject parent = null;
/* step 5 */
if (Type.isUndefined(options)) {
options = ObjectCreate(calleeContext, Intrinsics.ObjectPrototype);
}
/* step 6 */
RequireObjectCoercible(calleeContext, options);
/* step 7 */
Object nameOption = GetV(calleeContext, options, "name");
/* step 8 */
if (!Type.isUndefined(nameOption)) {
name = ToString(calleeContext, nameOption);
}
/* step 9 */
Object parentOption = GetV(calleeContext, options, "parent");
/* steps 10-11 */
if (!Type.isUndefinedOrNull(parentOption)) {
if (!(parentOption instanceof ZoneObject)) {
throw newTypeError(calleeContext, Messages.Key.IncompatibleObject);
}
parent = (ZoneObject) parentOption;
}
/* steps 12-14 */
ZoneObject zone = new ZoneObject(functionRealm, parent, GetPrototypeFromConstructor(calleeContext, newTarget, Intrinsics.ZonePrototype));
/* step 15 */
DefinePropertyOrThrow(calleeContext, zone, "name", new PropertyDescriptor(name, false, false, true));
/* step 17 */
return zone;
}
use of com.github.anba.es6draft.runtime.Realm in project es6draft by anba.
the class ScriptCodeGenerator method generateGlobalScriptEvaluation.
/**
* 15.1.7 Runtime Semantics: ScriptEvaluation
*
* @param node
* the script node
* @param mv
* the instruction visitor
*/
private void generateGlobalScriptEvaluation(Script node, InstructionVisitor mv) {
Variable<ExecutionContext> callerContext = mv.getParameter(EXECUTION_CONTEXT, ExecutionContext.class);
Variable<com.github.anba.es6draft.Script> script = mv.getParameter(SCRIPT, com.github.anba.es6draft.Script.class);
Variable<Realm> realm = mv.newVariable("realm", Realm.class);
Variable<ExecutionContext> scriptCxt = mv.newVariable("scriptCxt", ExecutionContext.class);
Variable<ExecutionContext> oldScriptContext = mv.newVariable("oldScriptContext", ExecutionContext.class);
Variable<Object> result = mv.newVariable("result", Object.class);
Variable<Throwable> throwable = mv.newVariable("throwable", Throwable.class);
getRealm(callerContext, realm, mv);
/* steps 1-2 (not applicable) */
/* steps 3-7 */
newScriptExecutionContext(realm, script, scriptCxt, mv);
/* step 8 */
getScriptContext(realm, oldScriptContext, mv);
/* step 9 */
setScriptContext(realm, scriptCxt, mv);
TryCatchLabel startFinally = new TryCatchLabel(), endFinally = new TryCatchLabel();
TryCatchLabel handlerFinally = new TryCatchLabel();
mv.mark(startFinally);
{
/* step 10 */
mv.load(scriptCxt);
mv.invoke(codegen.methodDesc(node, ScriptName.Init));
/* steps 11-12 */
mv.load(scriptCxt);
mv.invoke(codegen.methodDesc(node, ScriptName.Code));
mv.store(result);
/* steps 13-15 */
setScriptContext(realm, oldScriptContext, mv);
/* step 16 */
mv.load(result);
mv._return();
}
mv.mark(endFinally);
// Exception: Restore script context and then rethrow exception
mv.finallyHandler(handlerFinally);
mv.store(throwable);
/* steps 13-15 */
setScriptContext(realm, oldScriptContext, mv);
mv.load(throwable);
mv.athrow();
mv.tryFinally(startFinally, endFinally, handlerFinally);
}
use of com.github.anba.es6draft.runtime.Realm in project es6draft by anba.
the class ShellGlobalObject method eval.
/**
* Parses, compiles and executes the javascript file.
*
* @param fileName
* the file name for the script file
* @param file
* the absolute path to the file
* @return the evaluation result
* @throws IOException
* if there was any I/O error
* @throws ParserException
* if the source contains any syntax errors
* @throws CompilationException
* if the parsed source could not be compiled
*/
public Object eval(Path fileName, Path file) throws IOException, ParserException, CompilationException {
Realm realm = getRealm();
Source source = new Source(file, fileName.toString(), 1);
Script script = realm.getScriptLoader().script(source, file);
return script.evaluate(realm);
}
Aggregations