use of com.github.anba.es6draft.runtime.modules.ResolvedBinding in project es6draft by anba.
the class ModuleDeclarationInstantiationGenerator method generate.
private void generate(Module module, SourceTextModuleRecord moduleRecord, ModuleDeclInitVisitor mv) {
Variable<ExecutionContext> context = mv.getExecutionContext();
Variable<SourceTextModuleRecord> moduleRec = mv.getModule();
Variable<LexicalEnvironment<ModuleEnvironmentRecord>> env = mv.getModuleEnvironment();
Variable<ModuleEnvironmentRecord> envRec = mv.newVariable("envRec", ModuleEnvironmentRecord.class);
getEnvironmentRecord(env, envRec, mv);
Variable<ResolvedBinding> resolved = mv.newVariable("resolved", ResolvedBinding.class);
Variable<ScriptObject> namespace = null;
Variable<FunctionObject> fo = null;
Variable<Undefined> undef = mv.newVariable("undef", Undefined.class);
mv.loadUndefined();
mv.store(undef);
/* step 1 */
for (ExportEntry exportEntry : moduleRecord.getIndirectExportEntries()) {
mv.lineInfo(exportEntry.getLine());
mv.load(moduleRec);
mv.aconst(exportEntry.getExportName());
mv.invoke(Methods.ModuleOperations_resolveExportOrThrow);
}
/* step 8 */
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.ModuleOperations_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.ModuleOperations_resolveImportOrThrow);
mv.store(resolved);
/* step 8.d.iii */
createImportBinding(context, envRec, importEntry.getLocalName(), resolved, mv);
}
}
/* step 9 (not applicable) */
/* step 10 */
List<StatementListItem> varDeclarations = VarScopedDeclarations(module);
/* step 11 */
HashSet<Name> declaredVarNames = new HashSet<>();
/* step 12 */
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 13 */
List<Declaration> lexDeclarations = LexicallyScopedDeclarations(module);
/* step 14 */
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);
}
}
}
mv._return();
}
use of com.github.anba.es6draft.runtime.modules.ResolvedBinding in project es6draft by anba.
the class ScriptLoading method getModuleExport.
/**
* Resolves and returns the exported binding from a module record.
*
* @param module
* the module record
* @param exportName
* the export name
* @return the exported value
* @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
*/
public static Object getModuleExport(ModuleRecord module, String exportName) throws IOException, MalformedNameException, ResolutionException {
// Throw if module isn't linked.
if (module.getRealm() == null) {
throw new IllegalArgumentException();
}
// Ensure the module is instantiated and evaluated.
module.instantiate();
module.evaluate();
ResolvedBinding export = module.resolveExport(exportName, new HashMap<>());
if (export == null) {
throw new ResolutionException(Messages.Key.ModulesUnresolvedExport, exportName);
}
if (export.isAmbiguous()) {
throw new ResolutionException(Messages.Key.ModulesAmbiguousExport, exportName);
}
ModuleRecord targetModule = export.getModule();
if (targetModule.getEnvironment() == null) {
throw new IllegalStateException();
}
if (export.isNameSpaceExport()) {
return GetModuleNamespace(module.getRealm().defaultContext(), targetModule);
}
return targetModule.getEnvironment().getEnvRec().getBindingValue(export.getBindingName(), true);
}
use of com.github.anba.es6draft.runtime.modules.ResolvedBinding in project es6draft by anba.
the class ModuleNamespaceObject method get.
/**
* 9.4.6.7 [[Get]] (P, Receiver)
*/
@Override
public Object get(ExecutionContext cx, String propertyKey, Object receiver) {
/* steps 1-2 (not applicable) */
/* step 3 */
Set<String> exports = this.exports;
/* step 4 */
if (!exports.contains(propertyKey)) {
return UNDEFINED;
}
/* step 5 */
ModuleRecord m = this.module;
/* step 6 */
ResolvedBinding binding;
try {
binding = m.resolveExport(propertyKey, new HashMap<>());
} catch (IOException e) {
throw Errors.newInternalError(cx, e, Messages.Key.ModulesIOException, e.getMessage());
} catch (ResolutionException | MalformedNameException | ParserException | CompilationException e) {
throw e.toScriptException(cx);
}
/* step 7 */
assert binding != null && !binding.isAmbiguous();
/* step 8 */
ModuleRecord targetModule = binding.getModule();
/* step 9 */
assert targetModule != null;
/* step 10 */
LexicalEnvironment<?> targetEnv = targetModule.getEnvironment();
/* step 11 */
if (targetEnv == null) {
throw newReferenceError(cx, Messages.Key.UninitializedBinding, binding.getBindingName());
}
/* step ? (Extension: Export From) */
if (binding.isNameSpaceExport()) {
try {
return GetModuleNamespace(cx, targetModule);
} catch (IOException e) {
throw Errors.newInternalError(cx, Messages.Key.ModulesIOException, e.getMessage());
} catch (MalformedNameException | ResolutionException e) {
throw e.toScriptException(cx);
}
}
/* step 12 */
EnvironmentRecord targetEnvRec = targetEnv.getEnvRec();
/* step 13 */
return targetEnvRec.getBindingValue(binding.getBindingName(), true);
}
use of com.github.anba.es6draft.runtime.modules.ResolvedBinding in project es6draft by anba.
the class ModuleOperations method resolveImportOrThrow.
/**
* 15.2.1.16.4.2 ModuleDeclarationEnvironmentSetup( module )
*
* @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 ResolvedBinding resolveImportOrThrow(SourceTextModuleRecord module, String moduleRequest, String importName) throws IOException, MalformedNameException, ResolutionException {
/* steps 8.a-b */
ModuleRecord importedModule = HostResolveImportedModule(module, moduleRequest);
/* step 8.c (not applicable) */
/* step 8.d.i */
ResolvedBinding resolution = importedModule.resolveExport(importName, new HashMap<>());
/* step 8.d.ii */
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;
}
Aggregations