Search in sources :

Example 1 with ExportEntry

use of com.github.anba.es6draft.runtime.modules.ExportEntry 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 ExportEntry

use of com.github.anba.es6draft.runtime.modules.ExportEntry 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();
}
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) ModuleName(com.github.anba.es6draft.compiler.CodeGenerator.ModuleName) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name) ModuleExport(com.github.anba.es6draft.runtime.modules.ModuleExport) 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) StatementListItem(com.github.anba.es6draft.ast.StatementListItem)

Example 3 with ExportEntry

use of com.github.anba.es6draft.runtime.modules.ExportEntry in project es6draft by anba.

the class StaticSemantics method ExportEntries.

/**
 * 15.2.1.7 Static Semantics: ExportEntries<br>
 * 15.2.3.5 Static Semantics: ExportEntries
 *
 * @param node
 *            the module node
 * @return the list of export entries
 */
public static List<ExportEntry> ExportEntries(Module node) {
    ArrayList<ExportEntry> entries = new ArrayList<>();
    for (ModuleItem item : node.getStatements()) {
        if (item instanceof ExportDeclaration) {
            ExportDeclaration exportDecl = (ExportDeclaration) item;
            switch(exportDecl.getType()) {
                case All:
                    {
                        String module = exportDecl.getModuleSpecifier();
                        entries.add(new ExportEntry(item, module, "*", null, null));
                        break;
                    }
                case External:
                    {
                        String module = exportDecl.getModuleSpecifier();
                        ExportEntriesForModule(exportDecl.getExportClause(), module, entries);
                        break;
                    }
                case Local:
                    ExportEntriesForModule(exportDecl.getExportClause(), null, entries);
                    break;
                case Variable:
                    for (Name name : BoundNames(exportDecl.getVariableStatement())) {
                        String id = name.getIdentifier();
                        entries.add(new ExportEntry(item, null, null, id, id));
                    }
                    break;
                case Declaration:
                    for (Name name : BoundNames(exportDecl.getDeclaration())) {
                        String id = name.getIdentifier();
                        entries.add(new ExportEntry(item, null, null, id, id));
                    }
                    break;
                case DefaultHoistableDeclaration:
                    {
                        Name localName = BoundName(exportDecl.getHoistableDeclaration());
                        entries.add(new ExportEntry(item, null, null, localName.getIdentifier(), "default"));
                        break;
                    }
                case DefaultClassDeclaration:
                    {
                        Name localName = BoundName(exportDecl.getClassDeclaration());
                        entries.add(new ExportEntry(item, null, null, localName.getIdentifier(), "default"));
                        break;
                    }
                case DefaultExpression:
                    {
                        Name localName = BoundName(exportDecl.getExpression().getBinding());
                        entries.add(new ExportEntry(item, null, null, localName.getIdentifier(), "default"));
                        break;
                    }
                default:
                    throw new AssertionError();
            }
        }
    }
    return entries;
}
Also used : ExportEntry(com.github.anba.es6draft.runtime.modules.ExportEntry) ArrayList(java.util.ArrayList) InlineArrayList(com.github.anba.es6draft.runtime.internal.InlineArrayList) Name(com.github.anba.es6draft.ast.scope.Name)

Example 4 with ExportEntry

use of com.github.anba.es6draft.runtime.modules.ExportEntry in project es6draft by anba.

the class StaticSemantics method ExportEntriesForModule.

/**
 * 15.2.3.6 Static Semantics: ExportEntriesForModule
 *
 * @param node
 *            the exports clause node
 * @param module
 *            the module name
 * @param entries
 *            the list of export entries
 */
private static void ExportEntriesForModule(ExportClause node, String module, List<ExportEntry> entries) {
    if (module == null) {
        assert node.getDefaultEntry() == null;
        assert node.getNameSpace() == null;
        for (ExportSpecifier specifier : node.getExports()) {
            String localName = specifier.getSourceName();
            String importName = null;
            String exportName = specifier.getExportName();
            entries.add(new ExportEntry(specifier, module, importName, localName, exportName));
        }
    } else {
        IdentifierName defaultEntry = node.getDefaultEntry();
        if (defaultEntry != null) {
            String exportName = defaultEntry.getName();
            entries.add(new ExportEntry(defaultEntry, module, "default", null, exportName));
        }
        IdentifierName nameSpace = node.getNameSpace();
        if (nameSpace != null) {
            String exportName = nameSpace.getName();
            entries.add(new ExportEntry(nameSpace, module, "*", null, exportName));
        }
        for (ExportSpecifier specifier : node.getExports()) {
            String localName = null;
            String importName = specifier.getSourceName();
            String exportName = specifier.getExportName();
            entries.add(new ExportEntry(specifier, module, importName, localName, exportName));
        }
    }
}
Also used : ExportEntry(com.github.anba.es6draft.runtime.modules.ExportEntry)

Aggregations

ExportEntry (com.github.anba.es6draft.runtime.modules.ExportEntry)4 Name (com.github.anba.es6draft.ast.scope.Name)3 Declaration (com.github.anba.es6draft.ast.Declaration)2 HoistableDeclaration (com.github.anba.es6draft.ast.HoistableDeclaration)2 StatementListItem (com.github.anba.es6draft.ast.StatementListItem)2 VariableStatement (com.github.anba.es6draft.ast.VariableStatement)2 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)2 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)2 LexicalEnvironment (com.github.anba.es6draft.runtime.LexicalEnvironment)2 ModuleEnvironmentRecord (com.github.anba.es6draft.runtime.ModuleEnvironmentRecord)2 ImportEntry (com.github.anba.es6draft.runtime.modules.ImportEntry)2 SourceTextModuleRecord (com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord)2 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)2 Undefined (com.github.anba.es6draft.runtime.types.Undefined)2 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)2 HashSet (java.util.HashSet)2 ModuleName (com.github.anba.es6draft.compiler.CodeGenerator.ModuleName)1 InlineArrayList (com.github.anba.es6draft.runtime.internal.InlineArrayList)1 ModuleExport (com.github.anba.es6draft.runtime.modules.ModuleExport)1 ResolvedBinding (com.github.anba.es6draft.runtime.modules.ResolvedBinding)1