Search in sources :

Example 6 with SourceIdentifier

use of com.github.anba.es6draft.runtime.modules.SourceIdentifier 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());
}
Also used : ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) ArrayList(java.util.ArrayList) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader)

Example 7 with SourceIdentifier

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

the class Repl method newRealm.

private Realm newRealm() {
    ObjectAllocator<? extends ShellGlobalObject> allocator;
    if (options.shellMode == ShellMode.Mozilla) {
        allocator = MozShellGlobalObject::new;
    } else if (options.shellMode == ShellMode.V8) {
        allocator = V8ShellGlobalObject::new;
    } else {
        allocator = SimpleShellGlobalObject::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()).setGlobalAllocator(allocator).setModuleLoader(moduleLoader).setConsole(console).setWorkerErrorReporter(this::errorReporter).setOptions(compatibilityOptions(options)).setParserOptions(parserOptions(options)).setCompilerOptions(compilerOptions(options)).build();
    /* @formatter:on */
    World world = new World(context);
    Realm realm = world.newRealm();
    ExecutionContext cx = realm.defaultContext();
    // Add completion to console
    console.addCompleter(new ShellCompleter(realm));
    // Execute global specific initialization
    enqueueScriptTask(realm, () -> {
        InitializeHostDefinedRealm(realm);
        // Add global "arguments" property
        ScriptObject arguments = CreateArrayFromList(cx, options.arguments);
        CreateMethodProperty(cx, realm.getGlobalObject(), "arguments", arguments);
    });
    if (options.console) {
        enqueueScriptTask(realm, () -> {
            ScriptObject console = ConsoleObject.createConsole(realm);
            CreateMethodProperty(cx, realm.getGlobalObject(), "console", console);
        });
    }
    if (options.moduleLoaderMode == ModuleLoaderMode.Node) {
        // TODO: Add default initialize(Realm) method to ModuleLoader interface?
        enqueueScriptTask(realm, () -> {
            NodeModuleLoader nodeLoader = (NodeModuleLoader) world.getModuleLoader();
            nodeLoader.initialize(realm);
        });
    }
    // Run eval expressions and files
    for (EvalScript evalScript : options.evalScripts) {
        if (options.module) {
            enqueueScriptTask(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 (file.equals(source.getFileString())) {
                        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 {
            enqueueScriptTask(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 : Path(java.nio.file.Path) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) 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) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) World(com.github.anba.es6draft.runtime.World) MozShellGlobalObject(com.github.anba.es6draft.repl.global.MozShellGlobalObject) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) NodeStandardModuleLoader(com.github.anba.es6draft.repl.loader.NodeStandardModuleLoader) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) 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)

Example 8 with SourceIdentifier

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

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

use of com.github.anba.es6draft.runtime.modules.SourceIdentifier 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)

Aggregations

SourceIdentifier (com.github.anba.es6draft.runtime.modules.SourceIdentifier)11 ModuleLoader (com.github.anba.es6draft.runtime.modules.ModuleLoader)5 ModuleRecord (com.github.anba.es6draft.runtime.modules.ModuleRecord)4 Realm (com.github.anba.es6draft.runtime.Realm)3 ModuleSource (com.github.anba.es6draft.runtime.modules.ModuleSource)3 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)2 Function (com.github.anba.es6draft.runtime.internal.Properties.Function)2 ScriptLoader (com.github.anba.es6draft.runtime.internal.ScriptLoader)2 SourceTextModuleRecord (com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord)2 StringModuleSource (com.github.anba.es6draft.runtime.modules.loader.StringModuleSource)2 Executable (com.github.anba.es6draft.Executable)1 Script (com.github.anba.es6draft.Script)1 ParserException (com.github.anba.es6draft.parser.ParserException)1 MozShellGlobalObject (com.github.anba.es6draft.repl.global.MozShellGlobalObject)1 SharedFunctions.loadScript (com.github.anba.es6draft.repl.global.SharedFunctions.loadScript)1 SharedFunctions.relativePathToScript (com.github.anba.es6draft.repl.global.SharedFunctions.relativePathToScript)1 NodeModuleLoader (com.github.anba.es6draft.repl.loader.NodeModuleLoader)1 NodeStandardModuleLoader (com.github.anba.es6draft.repl.loader.NodeStandardModuleLoader)1 ToFlatString (com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString)1 InitializeHostDefinedRealm (com.github.anba.es6draft.runtime.Realm.InitializeHostDefinedRealm)1