Search in sources :

Example 1 with SourceTextModuleRecord

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

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

the class NodeSourceTextModuleRecord method ParseModule.

/**
     * ParseModule ( sourceText )
     * 
     * @param scriptLoader
     *            the script loader
     * @param sourceCodeId
     *            the source code identifier
     * @param source
     *            the module source code
     * @return the parsed module record
     * @throws IOException
     *             if there was any I/O error
     * @throws ParserException
     *             if the module source contains any syntax errors
     * @throws CompilationException
     *             if the parsed module source cannot be compiled
     */
public static NodeSourceTextModuleRecord ParseModule(ScriptLoader scriptLoader, SourceIdentifier sourceCodeId, ModuleSource source) throws IOException, ParserException, CompilationException {
    // Add an implicit "require" binding to the lexical environment of the module.
    com.github.anba.es6draft.ast.Module parsedBody = scriptLoader.parseModule(source.toSource(), source.sourceCode());
    ModuleScope moduleScope = parsedBody.getScope();
    if (!moduleScope.isDeclared(new Name("require"))) {
        moduleScope.addImplicitBinding(new Name("require"));
    }
    SourceTextModuleRecord sourceText = SourceTextModuleRecord.ParseModule(scriptLoader, sourceCodeId, parsedBody);
    return new NodeSourceTextModuleRecord(sourceText);
}
Also used : SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord) ModuleScope(com.github.anba.es6draft.ast.scope.ModuleScope) Name(com.github.anba.es6draft.ast.scope.Name)

Example 3 with SourceTextModuleRecord

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

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

the class ShellFunctions method disassemble.

/**
     * shell-function: {@code disassemble([function])}
     * 
     * @param cx
     *            the execution context
     * @param caller
     *            the caller context
     * @param args
     *            the arguments
     * @throws IOException
     *             if there was any I/O error
     * @throws MalformedNameException
     *             if the module name cannot be normalized
     */
@Function(name = "disassemble", arity = 1)
public void disassemble(ExecutionContext cx, ExecutionContext caller, Object... args) throws IOException, MalformedNameException {
    DebugInfo debugInfo = null;
    if (args.length == 0) {
        FunctionObject currentFunction = caller.getCurrentFunction();
        Executable currentExec = caller.getCurrentExecutable();
        if (currentFunction != null && currentFunction.getExecutable() == currentExec) {
            debugInfo = currentFunction.getCode().debugInfo();
        } else if (currentExec != null && currentExec.getSourceObject() != null) {
            debugInfo = currentExec.getSourceObject().debugInfo();
        }
    } else if (args[0] instanceof FunctionObject) {
        debugInfo = ((FunctionObject) args[0]).getCode().debugInfo();
    } else {
        String sourceCode = ToFlatString(cx, args[0]);
        boolean isModule = false;
        if (args.length > 1 && Type.isObject(args[1])) {
            isModule = ToBoolean(Get(cx, Type.objectValue(args[1]), "module"));
        }
        ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
        if (isModule) {
            ModuleLoader moduleLoader = cx.getRealm().getModuleLoader();
            SourceIdentifier identifier = moduleLoader.normalizeName("disassemble", null);
            ModuleSource src = new StringModuleSource(identifier, sourceCode);
            SourceTextModuleRecord module = ParseModule(scriptLoader, identifier, src);
            debugInfo = module.getScriptCode().getSourceObject().debugInfo();
        } else {
            Source source = new Source("<disassemble>", 1);
            Script script = scriptLoader.compile(scriptLoader.parseScript(source, sourceCode), "#disassemble");
            debugInfo = script.getSourceObject().debugInfo();
        }
    }
    if (debugInfo != null) {
        PrintWriter writer = cx.getRuntimeContext().getConsole().writer();
        for (DebugInfo.Method method : debugInfo.getMethods()) {
            writer.println(method.disassemble());
        }
    }
}
Also used : SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord) SharedFunctions.relativePathToScript(com.github.anba.es6draft.repl.global.SharedFunctions.relativePathToScript) SharedFunctions.loadScript(com.github.anba.es6draft.repl.global.SharedFunctions.loadScript) Script(com.github.anba.es6draft.Script) ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) ToFlatString(com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) Source(com.github.anba.es6draft.runtime.internal.Source) Executable(com.github.anba.es6draft.Executable) DebugInfo(com.github.anba.es6draft.runtime.internal.DebugInfo) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) PrintWriter(java.io.PrintWriter) Function(com.github.anba.es6draft.runtime.internal.Properties.Function)

Example 5 with SourceTextModuleRecord

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

the class NativeCode method loadModule.

/**
     * Loads the javascript module file.
     * <p>
     * The script file is loaded as a native module with elevated privileges.
     * 
     * @param realm
     *            the realm instance
     * @param name
     *            the module name
     * @return the native module record
     * @throws IOException
     *             if there was any I/O error
     * @throws URISyntaxException
     *             the URL is not a valid URI
     * @throws MalformedNameException
     *             if any imported module request cannot be normalized
     * @throws ResolutionException
     *             if any export binding cannot be resolved
     * @throws ParserException
     *             if the module source contains any syntax errors
     * @throws CompilationException
     *             if the parsed module source cannot be compiled
     */
public static ModuleRecord loadModule(Realm realm, String name) throws IOException, URISyntaxException, MalformedNameException, ResolutionException {
    RuntimeContext context = realm.getWorld().getContext();
    URLModuleLoader urlLoader = new URLModuleLoader(context, createNativeScriptLoader(context));
    URLSourceIdentifier sourceId = new URLSourceIdentifier(getScriptURL(name));
    URLModuleSource source = new URLModuleSource(sourceId);
    SourceTextModuleRecord module = urlLoader.define(sourceId, source, realm);
    module.instantiate();
    module.evaluate();
    return module;
}
Also used : SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord) URLSourceIdentifier(com.github.anba.es6draft.runtime.modules.loader.URLSourceIdentifier) URLModuleSource(com.github.anba.es6draft.runtime.modules.loader.URLModuleSource) URLModuleLoader(com.github.anba.es6draft.runtime.modules.loader.URLModuleLoader)

Aggregations

SourceTextModuleRecord (com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord)6 Name (com.github.anba.es6draft.ast.scope.Name)2 ModuleExport (com.github.anba.es6draft.runtime.modules.ModuleExport)2 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)2 Executable (com.github.anba.es6draft.Executable)1 Script (com.github.anba.es6draft.Script)1 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 ModuleScope (com.github.anba.es6draft.ast.scope.ModuleScope)1 ModuleName (com.github.anba.es6draft.compiler.CodeGenerator.ModuleName)1 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)1 SharedFunctions.loadScript (com.github.anba.es6draft.repl.global.SharedFunctions.loadScript)1 SharedFunctions.relativePathToScript (com.github.anba.es6draft.repl.global.SharedFunctions.relativePathToScript)1 ToFlatString (com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString)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 DebugInfo (com.github.anba.es6draft.runtime.internal.DebugInfo)1