Search in sources :

Example 1 with ResolvedBinding

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();
}
Also used : SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name) HoistableDeclaration(com.github.anba.es6draft.ast.HoistableDeclaration) HoistableDeclaration(com.github.anba.es6draft.ast.HoistableDeclaration) Declaration(com.github.anba.es6draft.ast.Declaration) ModuleEnvironmentRecord(com.github.anba.es6draft.runtime.ModuleEnvironmentRecord) HashSet(java.util.HashSet) Undefined(com.github.anba.es6draft.runtime.types.Undefined) ExportEntry(com.github.anba.es6draft.runtime.modules.ExportEntry) ImportEntry(com.github.anba.es6draft.runtime.modules.ImportEntry) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) VariableStatement(com.github.anba.es6draft.ast.VariableStatement) LexicalEnvironment(com.github.anba.es6draft.runtime.LexicalEnvironment) ResolvedBinding(com.github.anba.es6draft.runtime.modules.ResolvedBinding) StatementListItem(com.github.anba.es6draft.ast.StatementListItem)

Example 2 with ResolvedBinding

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);
}
Also used : ResolutionException(com.github.anba.es6draft.runtime.modules.ResolutionException) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord) ResolvedBinding(com.github.anba.es6draft.runtime.modules.ResolvedBinding)

Example 3 with ResolvedBinding

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);
}
Also used : ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) HashMap(java.util.HashMap) MalformedNameException(com.github.anba.es6draft.runtime.modules.MalformedNameException) ToString(com.github.anba.es6draft.runtime.AbstractOperations.ToString) IOException(java.io.IOException) ResolutionException(com.github.anba.es6draft.runtime.modules.ResolutionException) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) ResolvedBinding(com.github.anba.es6draft.runtime.modules.ResolvedBinding) EnvironmentRecord(com.github.anba.es6draft.runtime.EnvironmentRecord)

Example 4 with ResolvedBinding

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;
}
Also used : ResolutionException(com.github.anba.es6draft.runtime.modules.ResolutionException) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord) ResolvedBinding(com.github.anba.es6draft.runtime.modules.ResolvedBinding)

Aggregations

ResolvedBinding (com.github.anba.es6draft.runtime.modules.ResolvedBinding)4 ModuleRecord (com.github.anba.es6draft.runtime.modules.ModuleRecord)3 ResolutionException (com.github.anba.es6draft.runtime.modules.ResolutionException)3 SourceTextModuleRecord (com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord)3 Declaration (com.github.anba.es6draft.ast.Declaration)1 HoistableDeclaration (com.github.anba.es6draft.ast.HoistableDeclaration)1 StatementListItem (com.github.anba.es6draft.ast.StatementListItem)1 VariableStatement (com.github.anba.es6draft.ast.VariableStatement)1 Name (com.github.anba.es6draft.ast.scope.Name)1 CompilationException (com.github.anba.es6draft.compiler.CompilationException)1 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)1 ParserException (com.github.anba.es6draft.parser.ParserException)1 ToString (com.github.anba.es6draft.runtime.AbstractOperations.ToString)1 EnvironmentRecord (com.github.anba.es6draft.runtime.EnvironmentRecord)1 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)1 LexicalEnvironment (com.github.anba.es6draft.runtime.LexicalEnvironment)1 ModuleEnvironmentRecord (com.github.anba.es6draft.runtime.ModuleEnvironmentRecord)1 ExportEntry (com.github.anba.es6draft.runtime.modules.ExportEntry)1 ImportEntry (com.github.anba.es6draft.runtime.modules.ImportEntry)1 MalformedNameException (com.github.anba.es6draft.runtime.modules.MalformedNameException)1