use of com.github.anba.es6draft.runtime.modules.SourceIdentifier in project es6draft by anba.
the class TestGlobals method compileModules.
private PreloadModules compileModules() throws IOException, MalformedNameException {
List<?> moduleNames = configuration.getList("modules", emptyList());
if (moduleNames.isEmpty()) {
return new PreloadModules(Collections.<ModuleRecord>emptyList(), Collections.<ModuleRecord>emptyList());
}
RuntimeContext context = createContext();
ScriptLoader scriptLoader = new ScriptLoader(context);
TestModuleLoader<?> moduleLoader = this.moduleLoader.apply(context, scriptLoader);
ArrayList<ModuleRecord> modules = new ArrayList<>();
for (String moduleName : nonEmpty(moduleNames)) {
SourceIdentifier moduleId = moduleLoader.normalizeName(moduleName, null);
modules.add(moduleLoader.load(moduleId));
}
return new PreloadModules(modules, moduleLoader.getModules());
}
use of com.github.anba.es6draft.runtime.modules.SourceIdentifier in project es6draft by anba.
the class Repl method newRealm.
private Realm newRealm() {
ObjectAllocator<? extends ShellGlobalObject> allocator;
if (options.shellMode == ShellMode.Mozilla) {
allocator = MozShellGlobalObject::new;
} else if (options.shellMode == ShellMode.V8) {
allocator = V8ShellGlobalObject::new;
} else {
allocator = SimpleShellGlobalObject::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()).setGlobalAllocator(allocator).setModuleLoader(moduleLoader).setConsole(console).setWorkerErrorReporter(this::errorReporter).setOptions(compatibilityOptions(options)).setParserOptions(parserOptions(options)).setCompilerOptions(compilerOptions(options)).build();
/* @formatter:on */
World world = new World(context);
Realm realm = world.newRealm();
ExecutionContext cx = realm.defaultContext();
// Add completion to console
console.addCompleter(new ShellCompleter(realm));
// Execute global specific initialization
enqueueScriptTask(realm, () -> {
InitializeHostDefinedRealm(realm);
// Add global "arguments" property
ScriptObject arguments = CreateArrayFromList(cx, options.arguments);
CreateMethodProperty(cx, realm.getGlobalObject(), "arguments", arguments);
});
if (options.console) {
enqueueScriptTask(realm, () -> {
ScriptObject console = ConsoleObject.createConsole(realm);
CreateMethodProperty(cx, realm.getGlobalObject(), "console", console);
});
}
if (options.moduleLoaderMode == ModuleLoaderMode.Node) {
// TODO: Add default initialize(Realm) method to ModuleLoader interface?
enqueueScriptTask(realm, () -> {
NodeModuleLoader nodeLoader = (NodeModuleLoader) world.getModuleLoader();
nodeLoader.initialize(realm);
});
}
// Run eval expressions and files
for (EvalScript evalScript : options.evalScripts) {
if (options.module) {
enqueueScriptTask(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 (file.equals(source.getFileString())) {
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 {
enqueueScriptTask(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.modules.SourceIdentifier in project es6draft by anba.
the class ShellFunctions method disassemble.
/**
* shell-function: {@code disassemble([function])}
*
* @param cx
* the execution context
* @param caller
* the caller context
* @param args
* the arguments
* @throws IOException
* if there was any I/O error
* @throws MalformedNameException
* if the module name cannot be normalized
*/
@Function(name = "disassemble", arity = 1)
public void disassemble(ExecutionContext cx, ExecutionContext caller, Object... args) throws IOException, MalformedNameException {
DebugInfo debugInfo = null;
if (args.length == 0) {
FunctionObject currentFunction = caller.getCurrentFunction();
Executable currentExec = caller.getCurrentExecutable();
if (currentFunction != null && currentFunction.getExecutable() == currentExec) {
debugInfo = currentFunction.getCode().debugInfo();
} else if (currentExec != null && currentExec.getSourceObject() != null) {
debugInfo = currentExec.getSourceObject().debugInfo();
}
} else if (args[0] instanceof FunctionObject) {
debugInfo = ((FunctionObject) args[0]).getCode().debugInfo();
} else {
String sourceCode = ToFlatString(cx, args[0]);
boolean isModule = false;
if (args.length > 1 && Type.isObject(args[1])) {
isModule = ToBoolean(Get(cx, Type.objectValue(args[1]), "module"));
}
ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
if (isModule) {
ModuleLoader moduleLoader = cx.getRealm().getModuleLoader();
SourceIdentifier identifier = moduleLoader.normalizeName("disassemble", null);
ModuleSource src = new StringModuleSource(identifier, sourceCode);
SourceTextModuleRecord module = ParseModule(scriptLoader, identifier, src);
debugInfo = module.getScriptCode().getSourceObject().debugInfo();
} else {
Source source = new Source("<disassemble>", 1);
Script script = scriptLoader.compile(scriptLoader.parseScript(source, sourceCode), "#disassemble");
debugInfo = script.getSourceObject().debugInfo();
}
}
if (debugInfo != null) {
PrintWriter writer = cx.getRuntimeContext().getConsole().writer();
for (DebugInfo.Method method : debugInfo.getMethods()) {
writer.println(method.disassemble());
}
}
}
use of com.github.anba.es6draft.runtime.modules.SourceIdentifier in project es6draft by anba.
the class ShellGlobalObject method eval.
/**
* Parses, compiles and executes the javascript module file.
*
* @param moduleName
* the unnormalized module name
* @throws IOException
* if there was any I/O error
* @throws MalformedNameException
* if any imported module request cannot be normalized
* @throws ResolutionException
* if any export binding cannot be resolved
* @throws ParserException
* if the module source contains any syntax errors
* @throws CompilationException
* if the parsed module source cannot be compiled
*/
public void eval(String moduleName) throws IOException, MalformedNameException, ResolutionException, ParserException, CompilationException {
Realm realm = getRealm();
ModuleLoader moduleLoader = realm.getModuleLoader();
SourceIdentifier moduleId = moduleLoader.normalizeName(moduleName, null);
ModuleRecord module = moduleLoader.resolve(moduleId, realm);
module.instantiate();
module.evaluate();
}
use of com.github.anba.es6draft.runtime.modules.SourceIdentifier 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());
}
}
Aggregations