Search in sources :

Example 6 with ResolutionException

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

the class NodeModuleLoader method createModuleObject.

private ScriptObject createModuleObject(NodeModuleRecord module, Realm realm) {
    Constructor moduleConstructor = moduleConstructors.computeIfAbsent(realm, r -> {
        try {
            ModuleRecord mod = ScriptLoading.evalNativeModule(r, "module.jsm");
            return (Constructor) ScriptLoading.getModuleExport(mod, "default");
        } catch (MalformedNameException | ResolutionException e) {
            throw e.toScriptException(r.defaultContext());
        } catch (IOException e) {
            throw Errors.newError(r.defaultContext(), e.getMessage());
        }
    });
    Path file = module.getSource().getFile();
    String fileName = Objects.toString(file, "");
    String dirName = file != null ? Objects.toString(file.getParent(), "") : "";
    return Construct(realm.defaultContext(), moduleConstructor, fileName, dirName);
}
Also used : ResolutionException(com.github.anba.es6draft.runtime.modules.ResolutionException) Path(java.nio.file.Path) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) Constructor(com.github.anba.es6draft.runtime.types.Constructor) MalformedNameException(com.github.anba.es6draft.runtime.modules.MalformedNameException) IOException(java.io.IOException)

Example 7 with ResolutionException

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

the class ModuleNamespaceObject method get.

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

Example 8 with ResolutionException

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

Example 9 with ResolutionException

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

the class Test262TestRealm method execute.

/**
 * Executes the supplied test object.
 *
 * @param test
 *            the test-info object
 * @throws IOException
 *             if there was any I/O error
 * @throws MalformedNameException
 *             if any imported module request cannot be normalized
 * @throws ParserException
 *             if the module source contains any syntax errors
 * @throws CompilationException
 *             if the parsed module source cannot be compiled
 */
void execute(Test262Info test) throws ParserException, CompilationException, IOException, MalformedNameException {
    // Return early if no source code is available.
    if (sourceCode == null) {
        return;
    }
    assert !test.isAsync() || async != null;
    Realm realm = get();
    // Parse and evaluate the test-script
    if (test.isModule()) {
        ModuleLoader moduleLoader = realm.getModuleLoader();
        String moduleName = test.toModuleName();
        SourceIdentifier moduleId = moduleLoader.normalizeName(moduleName, null);
        ModuleSource source = new StringModuleSource(moduleId, moduleName, sourceCode);
        ModuleRecord module = moduleLoader.define(moduleId, source, realm);
        try {
            module.instantiate();
        } catch (ResolutionException e) {
            throw e.toScriptException(realm.defaultContext());
        }
        // Return after module instantiation if we test for module resolution errors.
        if (test.getErrorPhase() == ErrorPhase.Resolution) {
            return;
        }
        try {
            module.evaluate();
        } catch (ResolutionException e) {
            throw e.toScriptException(realm.defaultContext());
        }
    } else {
        Source source = new Source(test.toFile(), test.getScript().toString(), 1);
        Script script = realm.getScriptLoader().script(source, sourceCode);
        script.evaluate(realm);
    }
    // Wait for pending jobs to finish
    waitForPendingJobs(realm, test);
}
Also used : ResolutionException(com.github.anba.es6draft.runtime.modules.ResolutionException) Script(com.github.anba.es6draft.Script) ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) Realm(com.github.anba.es6draft.runtime.Realm) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) Source(com.github.anba.es6draft.runtime.internal.Source) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource)

Example 10 with ResolutionException

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

the class ModuleOperations method FinishDynamicImport.

/**
 * Runtime Semantics: FinishDynamicImport ( referencingScriptOrModule, specifier, promiseCapability, completion )
 */
private static void FinishDynamicImport(ExecutionContext cx, String specifier, PromiseCapability<PromiseObject> promiseCapability, ModuleRecord module, SourceIdentifier referredId) {
    ScriptObject namespace;
    try {
        // FIXME: spec issue - module namespace with "then" property vs. promise thenables
        namespace = GetModuleNamespace(cx, module);
    } catch (ScriptException | IOException | MalformedNameException | ResolutionException e) {
        ScriptException exception = toScriptException(cx, e, module.getSourceCodeId(), referredId);
        promiseCapability.getReject().call(cx, UNDEFINED, exception.getValue());
        return;
    }
    promiseCapability.getResolve().call(cx, UNDEFINED, namespace);
}
Also used : ResolutionException(com.github.anba.es6draft.runtime.modules.ResolutionException) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) MalformedNameException(com.github.anba.es6draft.runtime.modules.MalformedNameException) 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