Search in sources :

Example 1 with ModuleExport

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

the class NativeCode method getModuleExport.

/**
     * Resolves and returns the exported binding from a module record.
     * 
     * @param <T>
     *            the object type
     * @param module
     *            the module record
     * @param exportName
     *            the export name
     * @param clazz
     *            the expected class
     * @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 <T> T getModuleExport(ModuleRecord module, String exportName, Class<T> clazz) throws IOException, MalformedNameException, ResolutionException {
    ModuleExport export = module.resolveExport(exportName, new HashMap<>(), new HashSet<>());
    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.isInstantiated() || !targetModule.isEvaluated()) {
        throw new IllegalStateException();
    }
    if (export.isNameSpaceExport()) {
        Realm realm = module.getRealm();
        if (realm == null) {
            throw new IllegalArgumentException();
        }
        ScriptObject namespace = GetModuleNamespace(realm.defaultContext(), targetModule);
        return clazz.cast(namespace);
    }
    LexicalEnvironment<?> targetEnv = targetModule.getEnvironment();
    if (targetEnv == null) {
        throw new ResolutionException(Messages.Key.UninitializedModuleBinding, export.getBindingName(), targetModule.getSourceCodeId().toString());
    }
    Object bindingValue = targetEnv.getEnvRec().getBindingValue(export.getBindingName(), true);
    return clazz.cast(bindingValue);
}
Also used : ResolutionException(com.github.anba.es6draft.runtime.modules.ResolutionException) ModuleExport(com.github.anba.es6draft.runtime.modules.ModuleExport) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) Realm(com.github.anba.es6draft.runtime.Realm)

Example 2 with ModuleExport

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

the class ScriptRuntime method resolveImportOrThrow.

/**
     * 15.2.1.16.4 ModuleDeclarationInstantiation( ) Concrete Method
     * 
     * @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 ModuleExport resolveImportOrThrow(SourceTextModuleRecord module, String moduleRequest, String importName) throws IOException, MalformedNameException, ResolutionException {
    /* steps 10.a-b */
    ModuleRecord importedModule = HostResolveImportedModule(module, moduleRequest);
    /* steps 10.d.i-ii */
    ModuleExport resolution = importedModule.resolveExport(importName, new HashMap<>(), new HashSet<>());
    /* step 10.d.iii */
    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) ModuleExport(com.github.anba.es6draft.runtime.modules.ModuleExport) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord)

Example 3 with ModuleExport

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

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

the class ModuleNamespaceObject method getValue.

/** 9.4.6.8 [[Get]] (P, Receiver) */
@Override
protected Object getValue(ExecutionContext cx, String propertyKey, Object receiver) {
    /* step 1 (not applicable) */
    /* step 2 (not applicable) */
    /* step 3 */
    Set<String> exports = this.exports;
    /* step 4 */
    if (!exports.contains(propertyKey)) {
        return UNDEFINED;
    }
    /* step 5 */
    ModuleRecord m = this.module;
    /* steps 6-8 */
    ModuleExport binding;
    try {
        /* steps 6, 8 */
        binding = m.resolveExport(propertyKey, new HashMap<>(), new HashSet<>());
    } catch (IOException e) {
        /* step 7 */
        throw Errors.newInternalError(cx, e, Messages.Key.ModulesIOException, e.getMessage());
    } catch (ResolutionException | MalformedNameException e) {
        /* step 7 */
        throw e.toScriptException(cx);
    } catch (ParserException | CompilationException e) {
        /* step 7 */
        throw e.toScriptException(cx);
    }
    /* step 8 */
    assert binding != null && !binding.isAmbiguous();
    /* step 9 */
    ModuleRecord targetModule = binding.getModule();
    /* step 10 */
    assert targetModule != null;
    /* step 11 */
    LexicalEnvironment<?> targetEnv = targetModule.getEnvironment();
    /* step 12 */
    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 13 */
    EnvironmentRecord targetEnvRec = targetEnv.getEnvRec();
    /* step 14 */
    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) ModuleExport(com.github.anba.es6draft.runtime.modules.ModuleExport) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) EnvironmentRecord(com.github.anba.es6draft.runtime.EnvironmentRecord) HashSet(java.util.HashSet)

Aggregations

ModuleExport (com.github.anba.es6draft.runtime.modules.ModuleExport)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 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)2 HashSet (java.util.HashSet)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 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