Search in sources :

Example 1 with URLSourceIdentifier

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;
}
Also used : SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord) URLSourceIdentifier(com.github.anba.es6draft.runtime.modules.loader.URLSourceIdentifier) URLModuleSource(com.github.anba.es6draft.runtime.modules.loader.URLModuleSource) URLModuleLoader(com.github.anba.es6draft.runtime.modules.loader.URLModuleLoader) IOException(java.io.IOException) URL(java.net.URL)

Example 2 with URLSourceIdentifier

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;
}
Also used : Path(java.nio.file.Path) Script(com.github.anba.es6draft.Script) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Source(com.github.anba.es6draft.runtime.internal.Source) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) URLSourceIdentifier(com.github.anba.es6draft.runtime.modules.loader.URLSourceIdentifier) RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) Map(java.util.Map) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader)

Example 3 with URLSourceIdentifier

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;
}
Also used : SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord) URLSourceIdentifier(com.github.anba.es6draft.runtime.modules.loader.URLSourceIdentifier) URLModuleSource(com.github.anba.es6draft.runtime.modules.loader.URLModuleSource) URLModuleLoader(com.github.anba.es6draft.runtime.modules.loader.URLModuleLoader)

Example 4 with URLSourceIdentifier

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);
}
Also used : Script(com.github.anba.es6draft.Script) URLSourceIdentifier(com.github.anba.es6draft.runtime.modules.loader.URLSourceIdentifier) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

URLSourceIdentifier (com.github.anba.es6draft.runtime.modules.loader.URLSourceIdentifier)4 Script (com.github.anba.es6draft.Script)2 SourceTextModuleRecord (com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord)2 URLModuleLoader (com.github.anba.es6draft.runtime.modules.loader.URLModuleLoader)2 URLModuleSource (com.github.anba.es6draft.runtime.modules.loader.URLModuleSource)2 IOException (java.io.IOException)2 URL (java.net.URL)2 RuntimeContext (com.github.anba.es6draft.runtime.internal.RuntimeContext)1 ScriptLoader (com.github.anba.es6draft.runtime.internal.ScriptLoader)1 Source (com.github.anba.es6draft.runtime.internal.Source)1 ModuleSource (com.github.anba.es6draft.runtime.modules.ModuleSource)1 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1