Search in sources :

Example 6 with ModuleRecord

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

the class ShellGlobalObject method eval.

/**
     * Parses, compiles and executes the javascript module file.
     * 
     * @param moduleName
     *            the unnormalized module name
     * @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 void eval(String moduleName) throws IOException, MalformedNameException, ResolutionException, ParserException, CompilationException {
    Realm realm = getRealm();
    ModuleLoader moduleLoader = realm.getModuleLoader();
    SourceIdentifier moduleId = moduleLoader.normalizeName(moduleName, null);
    ModuleRecord module = moduleLoader.resolve(moduleId, realm);
    module.instantiate();
    module.evaluate();
}
Also used : 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)

Example 7 with ModuleRecord

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

the class ConsoleObject method createConsole.

/**
     * Creates a new {@code console} object.
     * 
     * @param realm
     *            the realm instance
     * @return the console object
     * @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 static ScriptObject createConsole(Realm realm) throws IOException, URISyntaxException, MalformedNameException, ResolutionException {
    ExecutionContext cx = realm.defaultContext();
    ModuleRecord module = NativeCode.loadModule(realm, "console.jsm");
    ScriptObject console = NativeCode.getModuleExport(module, "default", ScriptObject.class);
    Callable inspectFn = Properties.createFunction(cx, new InspectFunction(), InspectFunction.class);
    CreateMethodProperty(cx, console, "_inspect", inspectFn);
    return console;
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) Callable(com.github.anba.es6draft.runtime.types.Callable)

Example 8 with ModuleRecord

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

the class ShellFunctions method loadModule.

/**
     * shell-function: {@code loadModule(moduleName, [realmObject])}
     * 
     * @param cx
     *            the execution context
     * @param moduleName
     *            the module name
     * @param realmObject
     *            the optional realm object
     * @return the module namespace object
     * @throws MalformedNameException
     *             if any imported module request cannot be normalized
     * @throws ResolutionException
     *             if any export binding cannot be resolved
     */
@Function(name = "loadModule", arity = 1)
public ScriptObject loadModule(ExecutionContext cx, String moduleName, Object realmObject) throws MalformedNameException, ResolutionException {
    Realm realm;
    if (!Type.isUndefined(realmObject)) {
        if (!(realmObject instanceof RealmObject)) {
            throw Errors.newTypeError(cx, Messages.Key.IncompatibleObject);
        }
        realm = ((RealmObject) realmObject).getRealm();
    } else {
        realm = cx.getRealm();
    }
    try {
        ModuleLoader moduleLoader = realm.getModuleLoader();
        SourceIdentifier moduleId = moduleLoader.normalizeName(moduleName, null);
        ModuleRecord module = moduleLoader.resolve(moduleId, realm);
        module.instantiate();
        module.evaluate();
        return GetModuleNamespace(cx, module);
    } catch (IOException e) {
        throw Errors.newInternalError(cx, e, Messages.Key.ModulesIOException, e.getMessage());
    }
}
Also used : ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) RealmObject(com.github.anba.es6draft.runtime.objects.reflect.RealmObject) IOException(java.io.IOException) Realm(com.github.anba.es6draft.runtime.Realm) Function(com.github.anba.es6draft.runtime.internal.Properties.Function)

Example 9 with ModuleRecord

use of com.github.anba.es6draft.runtime.modules.ModuleRecord 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 10 with ModuleRecord

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

the class TestGlobals method newGlobal.

public final GLOBAL newGlobal(Console console, TEST test) throws MalformedNameException, ResolutionException, IOException, URISyntaxException {
    RuntimeContext context = createContext(console, test);
    World world = new World(context);
    Realm realm = world.newInitializedRealm();
    // Evaluate additional initialization scripts and modules
    TestModuleLoader<?> moduleLoader = (TestModuleLoader<?>) world.getModuleLoader();
    for (ModuleRecord module : modules.allModules) {
        moduleLoader.defineFromTemplate(module, realm);
    }
    for (ModuleRecord module : modules.mainModules) {
        ModuleRecord testModule = moduleLoader.get(module.getSourceCodeId(), realm);
        testModule.instantiate();
        testModule.evaluate();
    }
    for (Script script : scripts) {
        script.evaluate(realm);
    }
    @SuppressWarnings("unchecked") GLOBAL global = (GLOBAL) realm.getGlobalObject();
    return global;
}
Also used : Script(com.github.anba.es6draft.Script) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) World(com.github.anba.es6draft.runtime.World) Realm(com.github.anba.es6draft.runtime.Realm)

Aggregations

ModuleRecord (com.github.anba.es6draft.runtime.modules.ModuleRecord)10 Realm (com.github.anba.es6draft.runtime.Realm)4 SourceIdentifier (com.github.anba.es6draft.runtime.modules.SourceIdentifier)4 ModuleExport (com.github.anba.es6draft.runtime.modules.ModuleExport)3 ModuleLoader (com.github.anba.es6draft.runtime.modules.ModuleLoader)3 ResolutionException (com.github.anba.es6draft.runtime.modules.ResolutionException)3 SourceTextModuleRecord (com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord)3 RuntimeContext (com.github.anba.es6draft.runtime.internal.RuntimeContext)2 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)2 IOException (java.io.IOException)2 Script (com.github.anba.es6draft.Script)1 CompilationException (com.github.anba.es6draft.compiler.CompilationException)1 ParserException (com.github.anba.es6draft.parser.ParserException)1 ToString (com.github.anba.es6draft.runtime.AbstractOperations.ToString)1 EnvironmentRecord (com.github.anba.es6draft.runtime.EnvironmentRecord)1 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)1 World (com.github.anba.es6draft.runtime.World)1 Function (com.github.anba.es6draft.runtime.internal.Properties.Function)1 ScriptLoader (com.github.anba.es6draft.runtime.internal.ScriptLoader)1 MalformedNameException (com.github.anba.es6draft.runtime.modules.MalformedNameException)1