Search in sources :

Example 56 with Realm

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());
    }
}
Also used : ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) RealmObject(com.github.anba.es6draft.runtime.objects.reflect.RealmObject) IOException(java.io.IOException) Realm(com.github.anba.es6draft.runtime.Realm) Function(com.github.anba.es6draft.runtime.internal.Properties.Function) AliasFunction(com.github.anba.es6draft.runtime.internal.Properties.AliasFunction)

Example 57 with Realm

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());
        }
    }
}
Also used : StopExecutionException(com.github.anba.es6draft.repl.functions.StopExecutionException) World(com.github.anba.es6draft.runtime.World) InitializeHostDefinedRealm(com.github.anba.es6draft.runtime.Realm.InitializeHostDefinedRealm) Realm(com.github.anba.es6draft.runtime.Realm)

Example 58 with Realm

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;
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) PropertyDescriptor(com.github.anba.es6draft.runtime.types.PropertyDescriptor) Realm(com.github.anba.es6draft.runtime.Realm)

Example 59 with Realm

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);
}
Also used : Script(com.github.anba.es6draft.ast.Script) TryCatchLabel(com.github.anba.es6draft.compiler.assembler.TryCatchLabel) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) Realm(com.github.anba.es6draft.runtime.Realm)

Example 60 with Realm

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);
}
Also used : Script(com.github.anba.es6draft.Script) Realm(com.github.anba.es6draft.runtime.Realm) Source(com.github.anba.es6draft.runtime.internal.Source)

Aggregations

Realm (com.github.anba.es6draft.runtime.Realm)96 Test (org.junit.Test)39 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)17 Script (com.github.anba.es6draft.Script)16 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)16 Source (com.github.anba.es6draft.runtime.internal.Source)15 ParserException (com.github.anba.es6draft.parser.ParserException)9 Function (com.github.anba.es6draft.runtime.internal.Properties.Function)9 ModuleRecord (com.github.anba.es6draft.runtime.modules.ModuleRecord)8 ModuleLoader (com.github.anba.es6draft.runtime.modules.ModuleLoader)7 IOException (java.io.IOException)7 CompilationException (com.github.anba.es6draft.compiler.CompilationException)6 ScriptException (com.github.anba.es6draft.runtime.internal.ScriptException)6 ToSource (com.github.anba.es6draft.repl.SourceBuilder.ToSource)5 World (com.github.anba.es6draft.runtime.World)5 RuntimeContext (com.github.anba.es6draft.runtime.internal.RuntimeContext)5 ModuleSource (com.github.anba.es6draft.runtime.modules.ModuleSource)5 SourceIdentifier (com.github.anba.es6draft.runtime.modules.SourceIdentifier)5 GlobalObject (com.github.anba.es6draft.runtime.objects.GlobalObject)5 ExecutionContext.newEvalExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext.newEvalExecutionContext)4