Search in sources :

Example 6 with ModuleLoader

use of com.github.anba.es6draft.runtime.modules.ModuleLoader 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) AliasFunction(com.github.anba.es6draft.runtime.internal.Properties.AliasFunction)

Example 7 with ModuleLoader

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

the class ShellFunctions method disassemble.

/**
     * shell-function: {@code disassemble([function])}
     * 
     * @param cx
     *            the execution context
     * @param caller
     *            the caller context
     * @param args
     *            the arguments
     * @throws IOException
     *             if there was any I/O error
     * @throws MalformedNameException
     *             if the module name cannot be normalized
     */
@Function(name = "disassemble", arity = 1)
public void disassemble(ExecutionContext cx, ExecutionContext caller, Object... args) throws IOException, MalformedNameException {
    DebugInfo debugInfo = null;
    if (args.length == 0) {
        FunctionObject currentFunction = caller.getCurrentFunction();
        Executable currentExec = caller.getCurrentExecutable();
        if (currentFunction != null && currentFunction.getExecutable() == currentExec) {
            debugInfo = currentFunction.getCode().debugInfo();
        } else if (currentExec != null && currentExec.getSourceObject() != null) {
            debugInfo = currentExec.getSourceObject().debugInfo();
        }
    } else if (args[0] instanceof FunctionObject) {
        debugInfo = ((FunctionObject) args[0]).getCode().debugInfo();
    } else {
        String sourceCode = ToFlatString(cx, args[0]);
        boolean isModule = false;
        if (args.length > 1 && Type.isObject(args[1])) {
            isModule = ToBoolean(Get(cx, Type.objectValue(args[1]), "module"));
        }
        ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
        if (isModule) {
            ModuleLoader moduleLoader = cx.getRealm().getModuleLoader();
            SourceIdentifier identifier = moduleLoader.normalizeName("disassemble", null);
            ModuleSource src = new StringModuleSource(identifier, sourceCode);
            SourceTextModuleRecord module = ParseModule(scriptLoader, identifier, src);
            debugInfo = module.getScriptCode().getSourceObject().debugInfo();
        } else {
            Source source = new Source("<disassemble>", 1);
            Script script = scriptLoader.compile(scriptLoader.parseScript(source, sourceCode), "#disassemble");
            debugInfo = script.getSourceObject().debugInfo();
        }
    }
    if (debugInfo != null) {
        PrintWriter writer = cx.getRuntimeContext().getConsole().writer();
        for (DebugInfo.Method method : debugInfo.getMethods()) {
            writer.println(method.disassemble());
        }
    }
}
Also used : SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord) SharedFunctions.relativePathToScript(com.github.anba.es6draft.repl.global.SharedFunctions.relativePathToScript) SharedFunctions.loadScript(com.github.anba.es6draft.repl.global.SharedFunctions.loadScript) Script(com.github.anba.es6draft.Script) ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) ToFlatString(com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) Source(com.github.anba.es6draft.runtime.internal.Source) Executable(com.github.anba.es6draft.Executable) DebugInfo(com.github.anba.es6draft.runtime.internal.DebugInfo) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) PrintWriter(java.io.PrintWriter) Function(com.github.anba.es6draft.runtime.internal.Properties.Function)

Example 8 with ModuleLoader

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

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

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

the class Repl method newRealm.

private Realm newRealm() {
    Supplier<RuntimeContext.Data> runtimeData;
    Function<Realm, ? extends RealmData> realmData;
    if (options.shellMode == ShellMode.Mozilla) {
        runtimeData = RuntimeContext.Data::new;
        realmData = MozShellRealmData::new;
    } else if (options.shellMode == ShellMode.V8) {
        runtimeData = RuntimeContext.Data::new;
        realmData = V8ShellRealmData::new;
    } else {
        runtimeData = ShellContextData::new;
        realmData = ShellRealmData::new;
    }
    BiFunction<RuntimeContext, ScriptLoader, ModuleLoader> moduleLoader;
    switch(options.moduleLoaderMode) {
        case Default:
            moduleLoader = FileModuleLoader::new;
            break;
        case Node:
            moduleLoader = NodeModuleLoader::new;
            break;
        case NodeStandard:
            moduleLoader = NodeStandardModuleLoader::new;
            break;
        default:
            throw new AssertionError();
    }
    /* @formatter:off */
    RuntimeContext context = new RuntimeContext.Builder().setBaseDirectory(Paths.get("").toAbsolutePath()).setRuntimeData(runtimeData).setRealmData(realmData).setModuleLoader(moduleLoader).setConsole(console).setErrorReporter(this::errorReporter).setWorkerErrorReporter(this::errorReporter).setOptions(compatibilityOptions(options)).setParserOptions(parserOptions(options)).setCompilerOptions(compilerOptions(options)).build();
    /* @formatter:on */
    World world = new World(context);
    Realm realm = new Realm(world);
    ExecutionContext cx = realm.defaultContext();
    // Add completion to console
    console.addCompleter(new ShellCompleter(realm));
    // Execute global specific initialization
    enqueueScriptJob(realm, () -> {
        InitializeHostDefinedRealm(realm);
        // Add global "arguments" property
        ScriptObject arguments = CreateArrayFromList(cx, options.arguments);
        CreateMethodProperty(cx, realm.getGlobalObject(), "arguments", arguments);
    });
    if (options.console) {
        enqueueScriptJob(realm, () -> {
            ScriptObject consoleObj = ConsoleObject.createConsole(realm, !options.noColor);
            CreateMethodProperty(cx, realm.getGlobalObject(), "console", consoleObj);
        });
    }
    // Run eval expressions and files
    for (EvalScript evalScript : options.evalScripts) {
        if (evalScript.getType() == EvalScript.Type.Module) {
            enqueueScriptJob(realm, () -> {
                ModuleSource moduleSource = evalScript.getModuleSource();
                SourceIdentifier moduleName = evalScript.getModuleName();
                try {
                    ModuleEvaluationJob(realm, moduleName, moduleSource);
                } catch (ParserException e) {
                    Source source = moduleSource.toSource();
                    String file = e.getFile();
                    if (source.getFile() != null && file.equals(source.getFile().toString())) {
                        throw new ParserExceptionWithSource(e, source, moduleSource.sourceCode());
                    }
                    Path filePath = Paths.get(file).toAbsolutePath();
                    Source errorSource = new Source(filePath, file, 1);
                    String code = new String(Files.readAllBytes(filePath), StandardCharsets.UTF_8);
                    throw new ParserExceptionWithSource(e, errorSource, code);
                }
            });
        } else {
            enqueueScriptJob(realm, () -> {
                Source source = evalScript.getSource();
                String sourceCode = evalScript.getSourceCode();
                try {
                    eval(realm, parse(realm, source, sourceCode));
                } catch (ParserException e) {
                    throw new ParserExceptionWithSource(e, source, sourceCode);
                }
            });
        }
    }
    return realm;
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) World(com.github.anba.es6draft.runtime.World) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) FileModuleSource(com.github.anba.es6draft.runtime.modules.loader.FileModuleSource) NodeStandardModuleLoader(com.github.anba.es6draft.repl.loader.NodeStandardModuleLoader) FileModuleLoader(com.github.anba.es6draft.runtime.modules.loader.FileModuleLoader) InitializeHostDefinedRealm(com.github.anba.es6draft.runtime.Realm.InitializeHostDefinedRealm) Realm(com.github.anba.es6draft.runtime.Realm) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) FileModuleSource(com.github.anba.es6draft.runtime.modules.loader.FileModuleSource) Path(java.nio.file.Path) ParserException(com.github.anba.es6draft.parser.ParserException) ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) NodeStandardModuleLoader(com.github.anba.es6draft.repl.loader.NodeStandardModuleLoader) FileModuleLoader(com.github.anba.es6draft.runtime.modules.loader.FileModuleLoader) NodeModuleLoader(com.github.anba.es6draft.repl.loader.NodeModuleLoader) NodeModuleLoader(com.github.anba.es6draft.repl.loader.NodeModuleLoader) FileSourceIdentifier(com.github.anba.es6draft.runtime.modules.loader.FileSourceIdentifier) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) RealmData(com.github.anba.es6draft.runtime.RealmData) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext)

Aggregations

ModuleLoader (com.github.anba.es6draft.runtime.modules.ModuleLoader)12 SourceIdentifier (com.github.anba.es6draft.runtime.modules.SourceIdentifier)11 Realm (com.github.anba.es6draft.runtime.Realm)7 ModuleRecord (com.github.anba.es6draft.runtime.modules.ModuleRecord)7 Function (com.github.anba.es6draft.runtime.internal.Properties.Function)6 SourceTextModuleRecord (com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord)6 Source (com.github.anba.es6draft.runtime.internal.Source)5 ModuleSource (com.github.anba.es6draft.runtime.modules.ModuleSource)5 StringModuleSource (com.github.anba.es6draft.runtime.modules.loader.StringModuleSource)5 Script (com.github.anba.es6draft.Script)4 IOException (java.io.IOException)3 Executable (com.github.anba.es6draft.Executable)2 NodeModuleLoader (com.github.anba.es6draft.repl.loader.NodeModuleLoader)2 World (com.github.anba.es6draft.runtime.World)2 DebugInfo (com.github.anba.es6draft.runtime.internal.DebugInfo)2 AliasFunction (com.github.anba.es6draft.runtime.internal.Properties.AliasFunction)2 ScriptLoader (com.github.anba.es6draft.runtime.internal.ScriptLoader)2 ResolutionException (com.github.anba.es6draft.runtime.modules.ResolutionException)2 FileSourceIdentifier (com.github.anba.es6draft.runtime.modules.loader.FileSourceIdentifier)2 RealmObject (com.github.anba.es6draft.runtime.objects.reflect.RealmObject)2