Search in sources :

Example 1 with ResolutionException

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

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

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

the class ScriptLoading method getModuleExport.

/**
 * Resolves and returns the exported binding from a module record.
 *
 * @param module
 *            the module record
 * @param exportName
 *            the export name
 * @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 Object getModuleExport(ModuleRecord module, String exportName) throws IOException, MalformedNameException, ResolutionException {
    // Throw if module isn't linked.
    if (module.getRealm() == null) {
        throw new IllegalArgumentException();
    }
    // Ensure the module is instantiated and evaluated.
    module.instantiate();
    module.evaluate();
    ResolvedBinding export = module.resolveExport(exportName, new HashMap<>());
    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.getEnvironment() == null) {
        throw new IllegalStateException();
    }
    if (export.isNameSpaceExport()) {
        return GetModuleNamespace(module.getRealm().defaultContext(), targetModule);
    }
    return targetModule.getEnvironment().getEnvRec().getBindingValue(export.getBindingName(), true);
}
Also used : ResolutionException(com.github.anba.es6draft.runtime.modules.ResolutionException) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord) ResolvedBinding(com.github.anba.es6draft.runtime.modules.ResolvedBinding)

Example 4 with ResolutionException

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

the class ModuleOperations method HostImportModuleDynamically.

/**
 * Runtime Semantics: HostImportModuleDynamically ( referencingScriptOrModule, specifier, promiseCapability )
 *
 * @param cx
 *            the execution context
 * @param specifier
 *            the module specifier
 * @param promiseCapability
 *            the promise capability
 */
private static void HostImportModuleDynamically(ExecutionContext cx, String specifier, PromiseCapability<PromiseObject> promiseCapability) {
    Source source = cx.getCurrentExecutable().getSource();
    assert source != null : "HostImportModuleDynamically is only called from compiled code";
    // FIXME: spec bug - cannot assert referencingScriptOrModule is non-null, e.g. consider:
    // Promise.resolve(`import("./t.js").catch(e => print("caught", e))`).then(eval);
    // Or:
    // Promise.resolve(`import("./t.js").catch(e => print("caught",
    // e))`).then(Function).then(Function.prototype.call.bind(Function.prototype.call));
    SourceIdentifier referredId = source.getSourceId();
    if (referredId == null) {
        ScriptException exception = newReferenceError(cx, Messages.Key.ModulesInvalidName, specifier);
        FinishDynamicImport(cx, specifier, promiseCapability, exception);
        return;
    }
    Realm realm = cx.getRealm();
    ModuleLoader moduleLoader = realm.getModuleLoader();
    SourceIdentifier moduleId;
    try {
        moduleId = moduleLoader.normalizeName(specifier, referredId);
    } catch (MalformedNameException e) {
        FinishDynamicImport(cx, specifier, promiseCapability, e.toScriptException(cx));
        return;
    }
    moduleLoader.resolveAsync(moduleId, realm).whenComplete((module, err) -> {
        realm.enqueueAsyncJob(() -> {
            Throwable error = err;
            if (module != null) {
                try {
                    module.instantiate();
                    module.evaluate();
                } catch (ScriptException | IOException | MalformedNameException | ResolutionException e) {
                    assert error == null;
                    error = e;
                }
            }
            if (error != null) {
                ScriptException exception = toScriptException(cx, error, moduleId, referredId);
                FinishDynamicImport(cx, specifier, promiseCapability, exception);
            } else {
                FinishDynamicImport(cx, specifier, promiseCapability, module, referredId);
            }
        });
    });
}
Also used : ResolutionException(com.github.anba.es6draft.runtime.modules.ResolutionException) ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) InternalThrowable(com.github.anba.es6draft.runtime.internal.InternalThrowable) MalformedNameException(com.github.anba.es6draft.runtime.modules.MalformedNameException) IOException(java.io.IOException) Realm(com.github.anba.es6draft.runtime.Realm) Source(com.github.anba.es6draft.runtime.internal.Source)

Example 5 with ResolutionException

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

the class ModuleOperations method toScriptException.

private static ScriptException toScriptException(ExecutionContext cx, Throwable e, SourceIdentifier moduleId, SourceIdentifier referredId) {
    if (e instanceof CompletionException && e.getCause() != null) {
        e = e.getCause();
    }
    ScriptException exception;
    if (e instanceof NoSuchFileException) {
        exception = new ResolutionException(Messages.Key.ModulesUnresolvedModule, moduleId.toString(), referredId.toString()).toScriptException(cx);
    } else if (e instanceof IOException) {
        exception = newInternalError(cx, e, Messages.Key.ModulesIOException, Objects.toString(e.getMessage(), ""));
    } else if (e instanceof InternalThrowable) {
        exception = ((InternalThrowable) e).toScriptException(cx);
    } else {
        cx.getRuntimeContext().getErrorReporter().accept(cx, e);
        exception = newInternalError(cx, e, Messages.Key.InternalError, Objects.toString(e.getMessage(), ""));
    }
    return exception;
}
Also used : ResolutionException(com.github.anba.es6draft.runtime.modules.ResolutionException) ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) InternalThrowable(com.github.anba.es6draft.runtime.internal.InternalThrowable) CompletionException(java.util.concurrent.CompletionException) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException)

Aggregations

ResolutionException (com.github.anba.es6draft.runtime.modules.ResolutionException)11 ModuleRecord (com.github.anba.es6draft.runtime.modules.ModuleRecord)8 IOException (java.io.IOException)6 MalformedNameException (com.github.anba.es6draft.runtime.modules.MalformedNameException)5 SourceTextModuleRecord (com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord)4 Realm (com.github.anba.es6draft.runtime.Realm)3 ScriptException (com.github.anba.es6draft.runtime.internal.ScriptException)3 ModuleExport (com.github.anba.es6draft.runtime.modules.ModuleExport)3 ResolvedBinding (com.github.anba.es6draft.runtime.modules.ResolvedBinding)3 CompilationException (com.github.anba.es6draft.compiler.CompilationException)2 ParserException (com.github.anba.es6draft.parser.ParserException)2 ToString (com.github.anba.es6draft.runtime.AbstractOperations.ToString)2 EnvironmentRecord (com.github.anba.es6draft.runtime.EnvironmentRecord)2 InternalThrowable (com.github.anba.es6draft.runtime.internal.InternalThrowable)2 Source (com.github.anba.es6draft.runtime.internal.Source)2 ModuleLoader (com.github.anba.es6draft.runtime.modules.ModuleLoader)2 SourceIdentifier (com.github.anba.es6draft.runtime.modules.SourceIdentifier)2 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)2 HashMap (java.util.HashMap)2 Script (com.github.anba.es6draft.Script)1