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);
}
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;
}
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);
}
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);
}
});
});
}
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;
}
Aggregations