Search in sources :

Example 1 with ImportEntry

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

the class StaticSemantics method ImportEntriesForModule.

/**
     * 15.2.2.4 Static Semantics: ImportEntriesForModule
     * 
     * @param node
     *            the import clause node
     * @param module
     *            the module name
     * @param entries
     *            the list of import entries
     */
private static void ImportEntriesForModule(ImportClause node, String module, List<ImportEntry> entries) {
    BindingIdentifier defaultEntry = node.getDefaultEntry();
    if (defaultEntry != null) {
        String localName = defaultEntry.getName().getIdentifier();
        entries.add(new ImportEntry(defaultEntry, module, "default", localName));
    }
    BindingIdentifier nameSpace = node.getNameSpace();
    if (nameSpace != null) {
        String localName = nameSpace.getName().getIdentifier();
        entries.add(new ImportEntry(nameSpace, module, "*", localName));
    }
    for (ImportSpecifier specifier : node.getNamedImports()) {
        String importName = specifier.getImportName();
        String localName = specifier.getLocalName().getName().getIdentifier();
        entries.add(new ImportEntry(specifier, module, importName, localName));
    }
}
Also used : ImportEntry(com.github.anba.es6draft.runtime.modules.ImportEntry)

Example 2 with ImportEntry

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

Aggregations

ImportEntry (com.github.anba.es6draft.runtime.modules.ImportEntry)2 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 ModuleName (com.github.anba.es6draft.compiler.CodeGenerator.ModuleName)1 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)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 ModuleExport (com.github.anba.es6draft.runtime.modules.ModuleExport)1 SourceTextModuleRecord (com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord)1 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)1 Undefined (com.github.anba.es6draft.runtime.types.Undefined)1 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)1 HashSet (java.util.HashSet)1