use of com.github.anba.es6draft.runtime.modules.loader.URLSourceIdentifier in project es6draft by anba.
the class ScriptLoading method evalNativeModule.
/**
* Loads the javascript module file.
* <p>
* The script file is loaded as a native module with elevated privileges.
*
* @param realm
* the realm instance
* @param name
* the module name
* @return the native module record
* @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
* @throws ParserException
* if the module source contains any syntax errors
* @throws CompilationException
* if the parsed module source cannot be compiled
*/
public static ModuleRecord evalNativeModule(Realm realm, String name) throws IOException, MalformedNameException, ResolutionException {
URL scriptURL = RealmData.class.getResource("/scripts/" + name);
if (scriptURL == null) {
throw new IOException(String.format("module '%s' not found", name));
}
RuntimeContext context = realm.getRuntimeContext();
URLModuleLoader urlLoader = new URLModuleLoader(context, createNativeScriptLoader(context));
URLSourceIdentifier sourceId = new URLSourceIdentifier(scriptURL);
URLModuleSource source = new URLModuleSource(scriptURL, name);
SourceTextModuleRecord module = urlLoader.define(sourceId, source, realm);
module.instantiate();
module.evaluate();
return module;
}
use of com.github.anba.es6draft.runtime.modules.loader.URLSourceIdentifier in project es6draft by anba.
the class TestRealms method compileScripts.
private List<Script> compileScripts() throws IOException {
List<String> scriptNames = Resources.list(configuration, "scripts", emptyList());
if (scriptNames.isEmpty()) {
return Collections.emptyList();
}
Path basedir = getBaseDirectory();
RuntimeContext context = createContext();
ScriptLoader scriptLoader = new ScriptLoader(context);
ArrayList<Script> scripts = new ArrayList<>();
for (String scriptName : scriptNames) {
Map.Entry<Either<Path, URL>, InputStream> resourceScript = Resources.resourceScript(scriptName, basedir);
Source source = resourceScript.getKey().map(path -> new Source(path, scriptName, 1), url -> new Source(new URLSourceIdentifier(url), scriptName, 1));
Script script = scriptLoader.script(source, resourceScript.getValue());
scripts.add(script);
}
return scripts;
}
use of com.github.anba.es6draft.runtime.modules.loader.URLSourceIdentifier in project es6draft by anba.
the class NativeCode method loadModule.
/**
* Loads the javascript module file.
* <p>
* The script file is loaded as a native module with elevated privileges.
*
* @param realm
* the realm instance
* @param name
* the module name
* @return the native module record
* @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
* @throws ParserException
* if the module source contains any syntax errors
* @throws CompilationException
* if the parsed module source cannot be compiled
*/
public static ModuleRecord loadModule(Realm realm, String name) throws IOException, URISyntaxException, MalformedNameException, ResolutionException {
RuntimeContext context = realm.getWorld().getContext();
URLModuleLoader urlLoader = new URLModuleLoader(context, createNativeScriptLoader(context));
URLSourceIdentifier sourceId = new URLSourceIdentifier(getScriptURL(name));
URLModuleSource source = new URLModuleSource(sourceId);
SourceTextModuleRecord module = urlLoader.define(sourceId, source, realm);
module.instantiate();
module.evaluate();
return module;
}
use of com.github.anba.es6draft.runtime.modules.loader.URLSourceIdentifier in project es6draft by anba.
the class ScriptLoading method evalNative.
/**
* Parses, compiles and executes the javascript file. (Uses the script cache.)
* <p>
* The script file is loaded as a native script with elevated privileges.
*
* @param realm
* the realm instance
* @param name
* the script name
* @throws IOException
* if there was any I/O error
* @throws ParserException
* if the source contains any syntax errors
* @throws CompilationException
* if the parsed source could not be compiled
*/
public static void evalNative(Realm realm, String name) throws IOException, ParserException, CompilationException {
URL scriptURL = RealmData.class.getResource("/scripts/" + name);
if (scriptURL == null) {
throw new IOException(String.format("script '%s' not found", name));
}
RuntimeContext context = realm.getRuntimeContext();
ScriptCache scriptCache = context.getScriptCache();
URLSourceIdentifier sourceId = new URLSourceIdentifier(scriptURL);
Script script = scriptCache.get(sourceId, (source, id) -> createNativeScriptLoader(context).script(source, id.toUri().toURL()));
script.evaluate(realm);
}
Aggregations