Search in sources :

Example 1 with ModuleRecord

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

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

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

the class NodeModuleLoader method initialize.

/**
     * Initializes this module loader.
     * 
     * @param realm
     *            the realm instance
     * @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
     */
public void initialize(Realm realm) throws IOException, URISyntaxException, MalformedNameException, ResolutionException {
    ModuleRecord module = NativeCode.loadModule(realm, "module.jsm");
    Constructor moduleConstructor = NativeCode.getModuleExport(module, "default", Constructor.class);
    setModuleConstructor(moduleConstructor);
}
Also used : ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) Constructor(com.github.anba.es6draft.runtime.types.Constructor)

Example 4 with ModuleRecord

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

the class Test262GlobalObject method evalModule.

/**
     * Parses, compiles and executes the javascript module file.
     * 
     * @param moduleName
     *            the module name
     * @param sourceCode
     *            the source code
     * @param sourceLine
     *            the source line offset
     * @throws ParserException
     *             if the source contains any syntax errors
     * @throws CompilationException
     *             if the parsed source could not be compiled
     * @throws MalformedNameException
     *             if the module name cannot be normalized
     * @throws ResolutionException
     *             if the module exports cannot be resolved
     * @throws IOException
     *             if there was any I/O error
     */
void evalModule(String moduleName, String sourceCode, int sourceLine) throws ParserException, CompilationException, MalformedNameException, ResolutionException, IOException {
    ModuleLoader moduleLoader = getRealm().getModuleLoader();
    SourceIdentifier moduleId = moduleLoader.normalizeName(moduleName, null);
    ModuleSource source = new StringModuleSource(moduleId, sourceCode, sourceLine);
    ModuleRecord module = moduleLoader.define(moduleId, source, getRealm());
    module.instantiate();
    module.evaluate();
}
Also used : ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) 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)

Example 5 with ModuleRecord

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

the class TestGlobals method compileModules.

private PreloadModules compileModules() throws IOException, MalformedNameException {
    List<?> moduleNames = configuration.getList("modules", emptyList());
    if (moduleNames.isEmpty()) {
        return new PreloadModules(Collections.<ModuleRecord>emptyList(), Collections.<ModuleRecord>emptyList());
    }
    RuntimeContext context = createContext();
    ScriptLoader scriptLoader = new ScriptLoader(context);
    TestModuleLoader<?> moduleLoader = this.moduleLoader.apply(context, scriptLoader);
    ArrayList<ModuleRecord> modules = new ArrayList<>();
    for (String moduleName : nonEmpty(moduleNames)) {
        SourceIdentifier moduleId = moduleLoader.normalizeName(moduleName, null);
        modules.add(moduleLoader.load(moduleId));
    }
    return new PreloadModules(modules, moduleLoader.getModules());
}
Also used : ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) ArrayList(java.util.ArrayList) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader)

Aggregations

ModuleRecord (com.github.anba.es6draft.runtime.modules.ModuleRecord)10 Realm (com.github.anba.es6draft.runtime.Realm)4 SourceIdentifier (com.github.anba.es6draft.runtime.modules.SourceIdentifier)4 ModuleExport (com.github.anba.es6draft.runtime.modules.ModuleExport)3 ModuleLoader (com.github.anba.es6draft.runtime.modules.ModuleLoader)3 ResolutionException (com.github.anba.es6draft.runtime.modules.ResolutionException)3 SourceTextModuleRecord (com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord)3 RuntimeContext (com.github.anba.es6draft.runtime.internal.RuntimeContext)2 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)2 IOException (java.io.IOException)2 Script (com.github.anba.es6draft.Script)1 CompilationException (com.github.anba.es6draft.compiler.CompilationException)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 World (com.github.anba.es6draft.runtime.World)1 Function (com.github.anba.es6draft.runtime.internal.Properties.Function)1 ScriptLoader (com.github.anba.es6draft.runtime.internal.ScriptLoader)1 MalformedNameException (com.github.anba.es6draft.runtime.modules.MalformedNameException)1