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));
}
}
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));
}
}
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) */
}
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;
}
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);
}
Aggregations