Search in sources :

Example 71 with Realm

use of com.github.anba.es6draft.runtime.Realm in project es6draft by anba.

the class LocaleTest method testLookup_en_GB.

@Test
public void testLookup_en_GB() throws Exception {
    String languageTag = "en-GB";
    Realm realm = newRealm(languageTag);
    assertEquals(languageTag, realm.getLocale().toLanguageTag());
    assertEquals("en-GB", resolvedLocaleLookup(realm, Intl.Collator));
    assertEquals("en-GB", resolvedLocaleLookup(realm, Intl.DateTimeFormat));
    assertEquals("en-GB", resolvedLocaleLookup(realm, Intl.NumberFormat));
}
Also used : Realm(com.github.anba.es6draft.runtime.Realm) Test(org.junit.Test)

Example 72 with Realm

use of com.github.anba.es6draft.runtime.Realm in project es6draft by anba.

the class TestGlobals method newGlobal.

public final GLOBAL newGlobal(Console console, TEST test) throws MalformedNameException, ResolutionException, IOException, URISyntaxException {
    RuntimeContext context = createContext(console, test);
    World world = new World(context);
    Realm realm = world.newInitializedRealm();
    // Evaluate additional initialization scripts and modules
    TestModuleLoader<?> moduleLoader = (TestModuleLoader<?>) world.getModuleLoader();
    for (ModuleRecord module : modules.allModules) {
        moduleLoader.defineFromTemplate(module, realm);
    }
    for (ModuleRecord module : modules.mainModules) {
        ModuleRecord testModule = moduleLoader.get(module.getSourceCodeId(), realm);
        testModule.instantiate();
        testModule.evaluate();
    }
    for (Script script : scripts) {
        script.evaluate(realm);
    }
    @SuppressWarnings("unchecked") GLOBAL global = (GLOBAL) realm.getGlobalObject();
    return global;
}
Also used : Script(com.github.anba.es6draft.Script) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) World(com.github.anba.es6draft.runtime.World) Realm(com.github.anba.es6draft.runtime.Realm)

Example 73 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 scriptInit
 *            the script declaration instantiation method
 * @param scriptBody
 *            the script body method
 * @param mv
 *            the instruction visitor
 */
private static void generateGlobalScriptEvaluation(Script node, MethodName scriptInit, MethodName scriptBody, ScriptEvalVisitor mv) {
    Variable<ExecutionContext> callerContext = mv.getCallerContext();
    Variable<com.github.anba.es6draft.Script> script = mv.getScript();
    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(scriptInit);
        /* steps 11-12 */
        mv.load(scriptCxt);
        mv.invoke(scriptBody);
        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 74 with Realm

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
 * @param interpreter
 *            the interpreter
 * @return the script evaluation result
 */
private Object evalScriptEvaluation(ExecutionContext cx, Script script, Interpreter interpreter) {
    // 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-19 */
    ExecutionContext evalCxt = newEvalExecutionContext(cx, script, varEnv, lexEnv);
    /* step 20 */
    EvalDeclarationInstantiation(evalCxt, parsedScript, varEnv, lexEnv);
    /* steps 21-25 */
    return parsedScript.accept(interpreter, evalCxt);
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ExecutionContext.newScriptExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext.newScriptExecutionContext) ExecutionContext.newEvalExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext.newEvalExecutionContext) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) Realm(com.github.anba.es6draft.runtime.Realm)

Example 75 with Realm

use of com.github.anba.es6draft.runtime.Realm in project es6draft by anba.

the class PromisePrototype method PerformPromiseThen.

/**
 * 25.4.5.3.1 PerformPromiseThen ( promise, onFulfilled, onRejected, resultCapability )
 *
 * @param <PROMISE>
 *            the promise type
 * @param cx
 *            the execution context
 * @param promise
 *            the promise object
 * @param onFulfilled
 *            the onFulfilled handler
 * @param onRejected
 *            the onRejected handler
 * @param resultCapability
 *            the new promise capability record
 * @return the new promise object
 */
public static <PROMISE extends ScriptObject> PROMISE PerformPromiseThen(ExecutionContext cx, PromiseObject promise, Object onFulfilled, Object onRejected, PromiseCapability<PROMISE> resultCapability) {
    /* step 3 */
    if (!IsCallable(onFulfilled)) {
        onFulfilled = null;
    }
    /* step 4 */
    if (!IsCallable(onRejected)) {
        onRejected = null;
    }
    ZoneObject currentZone = cx.getRealm().getCurrentZone();
    /* step 5 */
    PromiseReaction fulfillReaction = new PromiseReaction(resultCapability, PromiseReaction.Type.Fulfill, (Callable) onFulfilled, currentZone);
    /* step 6 */
    PromiseReaction rejectReaction = new PromiseReaction(resultCapability, PromiseReaction.Type.Reject, (Callable) onRejected, currentZone);
    /* step 7 */
    if (promise.getState() == PromiseObject.State.Pending) {
        promise.addFulfillReaction(fulfillReaction);
        promise.addRejectReaction(rejectReaction);
        promise.notifyRejectReaction(rejectReaction);
    } else /* step 8 */
    if (promise.getState() == PromiseObject.State.Fulfilled) {
        Object value = promise.getResult();
        Realm realm = cx.getRealm();
        realm.enqueuePromiseJob(new PromiseReactionJob(realm, fulfillReaction, value));
    } else /* step 9 */
    {
        assert promise.getState() == PromiseObject.State.Rejected;
        Object reason = promise.getResult();
        Realm realm = cx.getRealm();
        realm.enqueuePromiseJob(new PromiseReactionJob(realm, rejectReaction, reason));
        promise.notifyRejectReaction(rejectReaction);
    }
    /* step 11 */
    return resultCapability.getPromise();
}
Also used : ZoneObject(com.github.anba.es6draft.runtime.objects.zone.ZoneObject) PromiseReactionJob(com.github.anba.es6draft.runtime.objects.promise.PromiseAbstractOperations.PromiseReactionJob) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ZoneObject(com.github.anba.es6draft.runtime.objects.zone.ZoneObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) Realm(com.github.anba.es6draft.runtime.Realm)

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