Search in sources :

Example 86 with Realm

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

the class LocaleTest method testLocaleWithPrivateExtension.

@Test
public void testLocaleWithPrivateExtension() throws Exception {
    String languageTag = "de-x-private";
    Realm realm = newRealm(languageTag);
    assertEquals(languageTag, realm.getLocale().toLanguageTag());
    for (Intl constructor : Intl.values()) {
        assertEquals("de", resolvedLocale(realm, constructor));
    }
}
Also used : Realm(com.github.anba.es6draft.runtime.Realm) Test(org.junit.Test)

Example 87 with Realm

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

the class LocaleTest method test_en_GB.

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

Example 88 with Realm

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

the class DateConstructor method construct.

/**
 * 20.3.2.1 Date (year, month [, date [, hours [, minutes [, seconds [, ms ] ] ] ] ] )<br>
 * 20.3.2.2 Date (value)<br>
 * 20.3.2.3 Date ( )<br>
 */
@Override
public DateObject construct(ExecutionContext callerContext, Constructor newTarget, Object... args) {
    ExecutionContext calleeContext = calleeContext();
    Realm realm = calleeContext.getRealm();
    /* steps 1-2 */
    int numberOfArgs = args.length;
    /* step 3 */
    final double dateValue;
    if (numberOfArgs >= 2) {
        // [20.3.2.1]
        /* step 3.a */
        double year = ToNumber(calleeContext, args[0]);
        /* step 3.b */
        double month = ToNumber(calleeContext, args[1]);
        /* step 3.c */
        double date = (args.length > 2 ? ToNumber(calleeContext, args[2]) : 1);
        /* step 3.d */
        double hour = (args.length > 3 ? ToNumber(calleeContext, args[3]) : 0);
        /* step 3.e */
        double min = (args.length > 4 ? ToNumber(calleeContext, args[4]) : 0);
        /* step 3.f */
        double sec = (args.length > 5 ? ToNumber(calleeContext, args[5]) : 0);
        /* step 3.g */
        double ms = (args.length > 6 ? ToNumber(calleeContext, args[6]) : 0);
        /* step 3.h */
        // ToInteger
        int intYear = (int) year;
        if (!Double.isNaN(year) && 0 <= intYear && intYear <= 99) {
            year = 1900 + intYear;
        }
        /* step 3.i */
        double finalDate = MakeDate(MakeDay(year, month, date), MakeTime(hour, min, sec, ms));
        /* step 3.k */
        dateValue = TimeClip(UTC(realm, finalDate));
    } else if (numberOfArgs == 1) {
        // [20.3.2.2]
        double tv;
        if (args[0] instanceof DateObject) {
            /* step 3.a */
            // TODO: spec improvement - inline thisTimeValue() call.
            tv = ((DateObject) args[0]).getDateValue();
        } else {
            /* step 3.b */
            Object v = ToPrimitive(calleeContext, args[0]);
            if (Type.isString(v)) {
                tv = (double) Properties.parse(calleeContext, null, v);
            } else {
                tv = ToNumber(calleeContext, v);
            }
        }
        dateValue = TimeClip(tv);
    } else {
        // [20.3.2.3]
        if (!calleeContext.getRealm().isGranted(Permission.CurrentTime)) {
            throw newTypeError(calleeContext, Messages.Key.NoPermission, "Date");
        }
        dateValue = System.currentTimeMillis();
    }
    DateObject obj = OrdinaryCreateFromConstructor(calleeContext, newTarget, Intrinsics.DatePrototype, DateObject::new);
    obj.setDateValue(dateValue);
    return obj;
/* step 4 (not applicable) */
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) Realm(com.github.anba.es6draft.runtime.Realm)

Example 89 with Realm

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

the class Repl method newRealm.

private Realm newRealm() {
    Supplier<RuntimeContext.Data> runtimeData;
    Function<Realm, ? extends RealmData> realmData;
    if (options.shellMode == ShellMode.Mozilla) {
        runtimeData = RuntimeContext.Data::new;
        realmData = MozShellRealmData::new;
    } else if (options.shellMode == ShellMode.V8) {
        runtimeData = RuntimeContext.Data::new;
        realmData = V8ShellRealmData::new;
    } else {
        runtimeData = ShellContextData::new;
        realmData = ShellRealmData::new;
    }
    BiFunction<RuntimeContext, ScriptLoader, ModuleLoader> moduleLoader;
    switch(options.moduleLoaderMode) {
        case Default:
            moduleLoader = FileModuleLoader::new;
            break;
        case Node:
            moduleLoader = NodeModuleLoader::new;
            break;
        case NodeStandard:
            moduleLoader = NodeStandardModuleLoader::new;
            break;
        default:
            throw new AssertionError();
    }
    /* @formatter:off */
    RuntimeContext context = new RuntimeContext.Builder().setBaseDirectory(Paths.get("").toAbsolutePath()).setRuntimeData(runtimeData).setRealmData(realmData).setModuleLoader(moduleLoader).setConsole(console).setErrorReporter(this::errorReporter).setWorkerErrorReporter(this::errorReporter).setOptions(compatibilityOptions(options)).setParserOptions(parserOptions(options)).setCompilerOptions(compilerOptions(options)).build();
    /* @formatter:on */
    World world = new World(context);
    Realm realm = new Realm(world);
    ExecutionContext cx = realm.defaultContext();
    // Add completion to console
    console.addCompleter(new ShellCompleter(realm));
    // Execute global specific initialization
    enqueueScriptJob(realm, () -> {
        InitializeHostDefinedRealm(realm);
        // Add global "arguments" property
        ScriptObject arguments = CreateArrayFromList(cx, options.arguments);
        CreateMethodProperty(cx, realm.getGlobalObject(), "arguments", arguments);
    });
    if (options.console) {
        enqueueScriptJob(realm, () -> {
            ScriptObject consoleObj = ConsoleObject.createConsole(realm, !options.noColor);
            CreateMethodProperty(cx, realm.getGlobalObject(), "console", consoleObj);
        });
    }
    // Run eval expressions and files
    for (EvalScript evalScript : options.evalScripts) {
        if (evalScript.getType() == EvalScript.Type.Module) {
            enqueueScriptJob(realm, () -> {
                ModuleSource moduleSource = evalScript.getModuleSource();
                SourceIdentifier moduleName = evalScript.getModuleName();
                try {
                    ModuleEvaluationJob(realm, moduleName, moduleSource);
                } catch (ParserException e) {
                    Source source = moduleSource.toSource();
                    String file = e.getFile();
                    if (source.getFile() != null && file.equals(source.getFile().toString())) {
                        throw new ParserExceptionWithSource(e, source, moduleSource.sourceCode());
                    }
                    Path filePath = Paths.get(file).toAbsolutePath();
                    Source errorSource = new Source(filePath, file, 1);
                    String code = new String(Files.readAllBytes(filePath), StandardCharsets.UTF_8);
                    throw new ParserExceptionWithSource(e, errorSource, code);
                }
            });
        } else {
            enqueueScriptJob(realm, () -> {
                Source source = evalScript.getSource();
                String sourceCode = evalScript.getSourceCode();
                try {
                    eval(realm, parse(realm, source, sourceCode));
                } catch (ParserException e) {
                    throw new ParserExceptionWithSource(e, source, sourceCode);
                }
            });
        }
    }
    return realm;
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) World(com.github.anba.es6draft.runtime.World) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) FileModuleSource(com.github.anba.es6draft.runtime.modules.loader.FileModuleSource) NodeStandardModuleLoader(com.github.anba.es6draft.repl.loader.NodeStandardModuleLoader) FileModuleLoader(com.github.anba.es6draft.runtime.modules.loader.FileModuleLoader) InitializeHostDefinedRealm(com.github.anba.es6draft.runtime.Realm.InitializeHostDefinedRealm) Realm(com.github.anba.es6draft.runtime.Realm) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) FileModuleSource(com.github.anba.es6draft.runtime.modules.loader.FileModuleSource) Path(java.nio.file.Path) ParserException(com.github.anba.es6draft.parser.ParserException) ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) NodeStandardModuleLoader(com.github.anba.es6draft.repl.loader.NodeStandardModuleLoader) FileModuleLoader(com.github.anba.es6draft.runtime.modules.loader.FileModuleLoader) NodeModuleLoader(com.github.anba.es6draft.repl.loader.NodeModuleLoader) NodeModuleLoader(com.github.anba.es6draft.repl.loader.NodeModuleLoader) FileSourceIdentifier(com.github.anba.es6draft.runtime.modules.loader.FileSourceIdentifier) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) RealmData(com.github.anba.es6draft.runtime.RealmData) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext)

Example 90 with Realm

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

the class Test262TestRealm method execute.

/**
 * Executes the supplied test object.
 *
 * @param test
 *            the test-info object
 * @throws IOException
 *             if there was any I/O error
 * @throws MalformedNameException
 *             if any imported module request cannot be normalized
 * @throws ParserException
 *             if the module source contains any syntax errors
 * @throws CompilationException
 *             if the parsed module source cannot be compiled
 */
void execute(Test262Info test) throws ParserException, CompilationException, IOException, MalformedNameException {
    // Return early if no source code is available.
    if (sourceCode == null) {
        return;
    }
    assert !test.isAsync() || async != null;
    Realm realm = get();
    // Parse and evaluate the test-script
    if (test.isModule()) {
        ModuleLoader moduleLoader = realm.getModuleLoader();
        String moduleName = test.toModuleName();
        SourceIdentifier moduleId = moduleLoader.normalizeName(moduleName, null);
        ModuleSource source = new StringModuleSource(moduleId, moduleName, sourceCode);
        ModuleRecord module = moduleLoader.define(moduleId, source, realm);
        try {
            module.instantiate();
        } catch (ResolutionException e) {
            throw e.toScriptException(realm.defaultContext());
        }
        // Return after module instantiation if we test for module resolution errors.
        if (test.getErrorPhase() == ErrorPhase.Resolution) {
            return;
        }
        try {
            module.evaluate();
        } catch (ResolutionException e) {
            throw e.toScriptException(realm.defaultContext());
        }
    } else {
        Source source = new Source(test.toFile(), test.getScript().toString(), 1);
        Script script = realm.getScriptLoader().script(source, sourceCode);
        script.evaluate(realm);
    }
    // Wait for pending jobs to finish
    waitForPendingJobs(realm, test);
}
Also used : ResolutionException(com.github.anba.es6draft.runtime.modules.ResolutionException) Script(com.github.anba.es6draft.Script) ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) Realm(com.github.anba.es6draft.runtime.Realm) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) Source(com.github.anba.es6draft.runtime.internal.Source) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource)

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