Search in sources :

Example 1 with ModuleEnvironmentRecord

use of com.github.anba.es6draft.runtime.ModuleEnvironmentRecord in project es6draft by anba.

the class SourceTextModuleRecord method instantiate.

/**
     * 15.2.1.16.4 ModuleDeclarationInstantiation( ) Concrete Method
     */
@Override
public void instantiate() throws IOException, MalformedNameException, ResolutionException {
    /* step 1 */
    SourceTextModuleRecord module = this;
    /* step 2 */
    Realm realm = module.realm;
    /* step 3 */
    assert realm != null : "module is not linked";
    /* step 4 */
    Module code = module.scriptCode;
    /* step 5 */
    if (module.environment != null) {
        return;
    }
    /* step 6 */
    LexicalEnvironment<ModuleEnvironmentRecord> env = newModuleEnvironment(realm.getGlobalEnv());
    /* step 7 */
    module.environment = env;
    /* step 8 */
    for (String required : module.requestedModules) {
        /* step 8.a (note) */
        /* steps 8.b-c */
        ModuleRecord requiredModule = HostResolveImportedModule(module, required);
        /* steps 8.d-e */
        requiredModule.instantiate();
    }
    /* steps 9-17 */
    ExecutionContext context = newModuleDeclarationExecutionContext(realm, code);
    code.getModuleBody().moduleDeclarationInstantiation(context, this, env);
    module.instantiated = true;
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ExecutionContext.newModuleDeclarationExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext.newModuleDeclarationExecutionContext) ExecutionContext.newModuleExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext.newModuleExecutionContext) HostResolveImportedModule(com.github.anba.es6draft.runtime.modules.ModuleSemantics.HostResolveImportedModule) Module(com.github.anba.es6draft.Module) Realm(com.github.anba.es6draft.runtime.Realm) ModuleEnvironmentRecord(com.github.anba.es6draft.runtime.ModuleEnvironmentRecord)

Example 2 with ModuleEnvironmentRecord

use of com.github.anba.es6draft.runtime.ModuleEnvironmentRecord 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 3 with ModuleEnvironmentRecord

use of com.github.anba.es6draft.runtime.ModuleEnvironmentRecord in project es6draft by anba.

the class SourceTextModuleRecord method ModuleDeclarationEnvironmentSetup.

/**
 * 15.2.1.16.4.2 ModuleDeclarationEnvironmentSetup( module )
 */
private static void ModuleDeclarationEnvironmentSetup(SourceTextModuleRecord module) throws IOException, ResolutionException, MalformedNameException {
    /* step 3 */
    Realm realm = module.realm;
    /* step 4 */
    assert realm != null : "module is not linked";
    /* step 5 */
    LexicalEnvironment<ModuleEnvironmentRecord> env = newModuleEnvironment(realm.getGlobalEnv());
    /* step 6 */
    module.environment = env;
    /* steps 1-2, 7-14 (generated code) */
    Module code = module.scriptCode;
    ExecutionContext context = newModuleDeclarationExecutionContext(realm, code);
    code.getModuleBody().moduleDeclarationInstantiation(context, module, env);
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ExecutionContext.newModuleDeclarationExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext.newModuleDeclarationExecutionContext) ExecutionContext.newModuleExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext.newModuleExecutionContext) HostResolveImportedModule(com.github.anba.es6draft.runtime.modules.ModuleSemantics.HostResolveImportedModule) Module(com.github.anba.es6draft.Module) Realm(com.github.anba.es6draft.runtime.Realm) ModuleEnvironmentRecord(com.github.anba.es6draft.runtime.ModuleEnvironmentRecord)

Example 4 with ModuleEnvironmentRecord

use of com.github.anba.es6draft.runtime.ModuleEnvironmentRecord in project es6draft by anba.

the class NodeSourceTextModuleRecord method instantiate.

@Override
public void instantiate() throws IOException, MalformedNameException, ResolutionException {
    SourceTextModuleRecord.Status previousState = module.getStatus();
    module.instantiate();
    SourceTextModuleRecord.Status newState = module.getStatus();
    // Add "require" function when module is instantiated.
    if (newState != previousState && newState == SourceTextModuleRecord.Status.Instantiated) {
        ModuleEnvironmentRecord envRec = module.getEnvironment().getEnvRec();
        if (!envRec.hasBinding("require")) {
            envRec.createImmutableBinding("require", true);
            envRec.initializeBinding("require", NodeFunctions.createRequireFunction(this));
        }
    }
}
Also used : SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord) ModuleEnvironmentRecord(com.github.anba.es6draft.runtime.ModuleEnvironmentRecord)

Example 5 with ModuleEnvironmentRecord

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

ModuleEnvironmentRecord (com.github.anba.es6draft.runtime.ModuleEnvironmentRecord)5 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)4 SourceTextModuleRecord (com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord)3 Module (com.github.anba.es6draft.Module)2 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 Name (com.github.anba.es6draft.ast.scope.Name)2 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)2 ExecutionContext.newModuleDeclarationExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext.newModuleDeclarationExecutionContext)2 ExecutionContext.newModuleExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext.newModuleExecutionContext)2 LexicalEnvironment (com.github.anba.es6draft.runtime.LexicalEnvironment)2 Realm (com.github.anba.es6draft.runtime.Realm)2 ExportEntry (com.github.anba.es6draft.runtime.modules.ExportEntry)2 ImportEntry (com.github.anba.es6draft.runtime.modules.ImportEntry)2 HostResolveImportedModule (com.github.anba.es6draft.runtime.modules.ModuleSemantics.HostResolveImportedModule)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