use of com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord in project es6draft by anba.
the class ScriptRuntime method resolveImportOrThrow.
/**
* 15.2.1.16.4 ModuleDeclarationInstantiation( ) Concrete Method
*
* @param module
* the module record
* @param moduleRequest
* the module specifier string
* @param importName
* the import name
* @return the resolved module import
* @throws IOException
* if there was any I/O error
* @throws MalformedNameException
* if the module specifier cannot be normalized
* @throws ResolutionException
* if the export cannot be resolved
*/
public static ModuleExport resolveImportOrThrow(SourceTextModuleRecord module, String moduleRequest, String importName) throws IOException, MalformedNameException, ResolutionException {
/* steps 10.a-b */
ModuleRecord importedModule = HostResolveImportedModule(module, moduleRequest);
/* steps 10.d.i-ii */
ModuleExport resolution = importedModule.resolveExport(importName, new HashMap<>(), new HashSet<>());
/* step 10.d.iii */
if (resolution == null) {
throw new ResolutionException(Messages.Key.ModulesUnresolvedImport, importName, importedModule.getSourceCodeId().toString());
}
if (resolution.isAmbiguous()) {
throw new ResolutionException(Messages.Key.ModulesAmbiguousImport, importName, importedModule.getSourceCodeId().toString());
}
return resolution;
}
use of com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord in project es6draft by anba.
the class NodeSourceTextModuleRecord method ParseModule.
/**
* ParseModule ( sourceText )
*
* @param scriptLoader
* the script loader
* @param sourceCodeId
* the source code identifier
* @param source
* the module source code
* @return the parsed module record
* @throws IOException
* if there was any I/O error
* @throws ParserException
* if the module source contains any syntax errors
* @throws CompilationException
* if the parsed module source cannot be compiled
*/
public static NodeSourceTextModuleRecord ParseModule(ScriptLoader scriptLoader, SourceIdentifier sourceCodeId, ModuleSource source) throws IOException, ParserException, CompilationException {
// Add an implicit "require" binding to the lexical environment of the module.
com.github.anba.es6draft.ast.Module parsedBody = scriptLoader.parseModule(source.toSource(), source.sourceCode());
ModuleScope moduleScope = parsedBody.getScope();
if (!moduleScope.isDeclared(new Name("require"))) {
moduleScope.addImplicitBinding(new Name("require"));
}
SourceTextModuleRecord sourceText = SourceTextModuleRecord.ParseModule(scriptLoader, sourceCodeId, parsedBody);
return new NodeSourceTextModuleRecord(sourceText);
}
use of com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord in project es6draft by anba.
the class ModuleDeclarationInstantiationGenerator method generate.
private void generate(Module module, SourceTextModuleRecord moduleRecord, InstructionVisitor mv) {
Variable<ExecutionContext> context = mv.getParameter(EXECUTION_CONTEXT, ExecutionContext.class);
Variable<SourceTextModuleRecord> moduleRec = mv.getParameter(MODULE, SourceTextModuleRecord.class);
Variable<LexicalEnvironment<ModuleEnvironmentRecord>> env = mv.getParameter(MODULE_ENV, LexicalEnvironment.class).uncheckedCast();
Variable<ModuleEnvironmentRecord> envRec = mv.newVariable("envRec", ModuleEnvironmentRecord.class);
getEnvironmentRecord(env, envRec, mv);
Variable<ModuleExport> resolved = mv.newVariable("resolved", ModuleExport.class);
Variable<ScriptObject> namespace = null;
Variable<FunctionObject> fo = null;
Variable<Undefined> undef = mv.newVariable("undef", Undefined.class);
mv.loadUndefined();
mv.store(undef);
/* step 9 */
for (ExportEntry exportEntry : moduleRecord.getIndirectExportEntries()) {
mv.lineInfo(exportEntry.getLine());
mv.load(moduleRec);
mv.aconst(exportEntry.getExportName());
mv.invoke(Methods.ScriptRuntime_resolveExportOrThrow);
}
/* step 12 */
for (ImportEntry importEntry : moduleRecord.getImportEntries()) {
mv.lineInfo(importEntry.getLine());
if (importEntry.isStarImport()) {
Name localName = new Name(importEntry.getLocalName());
BindingOp<ModuleEnvironmentRecord> op = BindingOp.of(envRec, localName);
op.createImmutableBinding(envRec, localName, true, mv);
mv.load(context);
mv.load(moduleRec);
mv.aconst(importEntry.getModuleRequest());
mv.invoke(Methods.ScriptRuntime_getModuleNamespace);
if (namespace == null) {
namespace = mv.newVariable("namespace", ScriptObject.class);
}
mv.store(namespace);
op.initializeBinding(envRec, localName, namespace, mv);
} else {
mv.load(moduleRec);
mv.aconst(importEntry.getModuleRequest());
mv.aconst(importEntry.getImportName());
mv.invoke(Methods.ScriptRuntime_resolveImportOrThrow);
mv.store(resolved);
createImportBinding(context, envRec, importEntry.getLocalName(), resolved, mv);
}
}
/* step 13 */
List<StatementListItem> varDeclarations = VarScopedDeclarations(module);
HashSet<Name> declaredVarNames = new HashSet<>();
/* step 14 */
for (StatementListItem d : varDeclarations) {
assert d instanceof VariableStatement;
for (Name dn : BoundNames((VariableStatement) d)) {
if (declaredVarNames.add(dn)) {
BindingOp<ModuleEnvironmentRecord> op = BindingOp.of(envRec, dn);
op.createMutableBinding(envRec, dn, false, mv);
op.initializeBinding(envRec, dn, undef, mv);
}
}
}
/* step 15 */
List<Declaration> lexDeclarations = LexicallyScopedDeclarations(module);
/* step 16 */
for (Declaration d : lexDeclarations) {
for (Name dn : BoundNames(d)) {
BindingOp<ModuleEnvironmentRecord> op = BindingOp.of(envRec, dn);
if (d.isConstDeclaration()) {
op.createImmutableBinding(envRec, dn, true, mv);
} else {
op.createMutableBinding(envRec, dn, false, mv);
}
if (d instanceof HoistableDeclaration) {
InstantiateFunctionObject(context, env, d, mv);
if (fo == null) {
fo = mv.newVariable("fo", FunctionObject.class);
}
mv.store(fo);
op.initializeBinding(envRec, dn, fo, mv);
}
}
}
/* step 17 */
mv._return();
}
use of com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord 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.SourceTextModuleRecord in project es6draft by anba.
the class NativeCode method loadModule.
/**
* Loads the javascript module file.
* <p>
* The script file is loaded as a native module with elevated privileges.
*
* @param realm
* the realm instance
* @param name
* the module name
* @return the native module record
* @throws IOException
* if there was any I/O error
* @throws URISyntaxException
* the URL is not a valid URI
* @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 static ModuleRecord loadModule(Realm realm, String name) throws IOException, URISyntaxException, MalformedNameException, ResolutionException {
RuntimeContext context = realm.getWorld().getContext();
URLModuleLoader urlLoader = new URLModuleLoader(context, createNativeScriptLoader(context));
URLSourceIdentifier sourceId = new URLSourceIdentifier(getScriptURL(name));
URLModuleSource source = new URLModuleSource(sourceId);
SourceTextModuleRecord module = urlLoader.define(sourceId, source, realm);
module.instantiate();
module.evaluate();
return module;
}
Aggregations