Search in sources :

Example 16 with Realm

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

the class ScriptEngineImpl method eval.

Object eval(Script script, ScriptContext context) throws javax.script.ScriptException {
    Realm realm = getEvalRealm(context);
    RuntimeContext runtimeContext = realm.getRuntimeContext();
    Console console = runtimeContext.getConsole();
    runtimeContext.setConsole(new ScriptingConsole(context));
    try {
        // Prepare a new execution context before calling the generated code.
        ExecutionContext evalCxt = newScriptingExecutionContext(realm, script, new LexicalEnvironment<>(realm.getGlobalEnv(), new ScriptContextEnvironmentRecord(realm.defaultContext(), context)));
        Object result = script.evaluate(evalCxt);
        realm.getWorld().runEventLoop();
        return TypeConverter.toJava(result);
    } catch (ScriptException e) {
        throw new javax.script.ScriptException(e);
    } finally {
        runtimeContext.setConsole(console);
    }
}
Also used : ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ExecutionContext.newScriptingExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext.newScriptingExecutionContext) Console(com.github.anba.es6draft.runtime.internal.Console) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) Realm(com.github.anba.es6draft.runtime.Realm)

Example 17 with Realm

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

the class ScriptEngineImpl method invoke.

private Object invoke(ScriptObject thisValue, String name, Object... args) throws javax.script.ScriptException, NoSuchMethodException {
    Realm realm = getEvalRealm(context);
    RuntimeContext runtimeContext = realm.getRuntimeContext();
    Console console = runtimeContext.getConsole();
    runtimeContext.setConsole(new ScriptingConsole(context));
    try {
        Object[] arguments = TypeConverter.fromJava(args);
        if (thisValue == null) {
            thisValue = realm.getGlobalThis();
        }
        ExecutionContext cx = realm.defaultContext();
        Object func = thisValue.get(cx, (Object) name, thisValue);
        if (!IsCallable(func)) {
            throw new NoSuchMethodException(name);
        }
        Object result = ((Callable) func).call(cx, thisValue, arguments);
        realm.getWorld().runEventLoop();
        return TypeConverter.toJava(result);
    } catch (ScriptException e) {
        throw new javax.script.ScriptException(e);
    } finally {
        runtimeContext.setConsole(console);
    }
}
Also used : ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ExecutionContext.newScriptingExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext.newScriptingExecutionContext) Console(com.github.anba.es6draft.runtime.internal.Console) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) Realm(com.github.anba.es6draft.runtime.Realm) IsCallable(com.github.anba.es6draft.runtime.AbstractOperations.IsCallable) Callable(com.github.anba.es6draft.runtime.types.Callable)

Example 18 with Realm

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

the class DefaultLocaleTimezone method testGreenwich.

@Test
public void testGreenwich() throws Exception {
    Realm realm = newRealm(TimeZone.getTimeZone("Greenwich"));
    assertEquals("UTC", resolvedTimeZone(realm));
}
Also used : Realm(com.github.anba.es6draft.runtime.Realm) Test(org.junit.Test)

Example 19 with Realm

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

the class DefaultLocaleTimezone method testAustralia_Tasmania.

@Test
public void testAustralia_Tasmania() throws Exception {
    Realm realm = newRealm(TimeZone.getTimeZone("Australia/Tasmania"));
    assertEquals("Australia/Hobart", resolvedTimeZone(realm));
}
Also used : Realm(com.github.anba.es6draft.runtime.Realm) Test(org.junit.Test)

Example 20 with Realm

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

the class DefaultLocaleTimezone method testGMT_minus_0.

@Test
public void testGMT_minus_0() throws Exception {
    Realm realm = newRealm(TimeZone.getTimeZone("GMT-0"));
    assertEquals("UTC", resolvedTimeZone(realm));
}
Also used : Realm(com.github.anba.es6draft.runtime.Realm) Test(org.junit.Test)

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