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