Search in sources :

Example 1 with ModuleSource

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

the class Test262GlobalObject method evalModule.

/**
     * Parses, compiles and executes the javascript module file.
     * 
     * @param moduleName
     *            the module name
     * @param sourceCode
     *            the source code
     * @param sourceLine
     *            the source line offset
     * @throws ParserException
     *             if the source contains any syntax errors
     * @throws CompilationException
     *             if the parsed source could not be compiled
     * @throws MalformedNameException
     *             if the module name cannot be normalized
     * @throws ResolutionException
     *             if the module exports cannot be resolved
     * @throws IOException
     *             if there was any I/O error
     */
void evalModule(String moduleName, String sourceCode, int sourceLine) throws ParserException, CompilationException, MalformedNameException, ResolutionException, IOException {
    ModuleLoader moduleLoader = getRealm().getModuleLoader();
    SourceIdentifier moduleId = moduleLoader.normalizeName(moduleName, null);
    ModuleSource source = new StringModuleSource(moduleId, sourceCode, sourceLine);
    ModuleRecord module = moduleLoader.define(moduleId, source, getRealm());
    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) 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)

Example 2 with ModuleSource

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

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

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

the class NodeModuleRecord method ParseModule.

/**
     * ParseModule ( sourceText )
     * 
     * @param scriptLoader
     *            the script loader
     * @param identifier
     *            the source code identifier
     * @param moduleSource
     *            the module source code
     * @return the parsed module record
     * @throws IOException
     *             if there was any I/O error
     * @throws ParserException
     *             if the module source contains any syntax errors
     * @throws CompilationException
     *             if the parsed module source cannot be compiled
     */
public static NodeModuleRecord ParseModule(ScriptLoader scriptLoader, SourceIdentifier identifier, ModuleSource moduleSource) throws IOException, ParserException, CompilationException {
    Source source = moduleSource.toSource();
    String sourceCode = moduleSource.sourceCode();
    if (identifier.toUri().toString().endsWith(".json")) {
        String jsonScript = JSONParser.parse(sourceCode, new ScriptJSONBuilder());
        sourceCode = String.format("module.exports = %s", jsonScript);
    }
    String parameters = "exports, require, module, __filename, __dirname";
    CompiledFunction function = scriptLoader.function(source, parameters, sourceCode);
    return new NodeModuleRecord(identifier, source, function);
}
Also used : CompiledFunction(com.github.anba.es6draft.compiler.CompiledFunction) Source(com.github.anba.es6draft.runtime.internal.Source) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource)

Aggregations

ModuleSource (com.github.anba.es6draft.runtime.modules.ModuleSource)4 ModuleLoader (com.github.anba.es6draft.runtime.modules.ModuleLoader)3 SourceIdentifier (com.github.anba.es6draft.runtime.modules.SourceIdentifier)3 Source (com.github.anba.es6draft.runtime.internal.Source)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 CompiledFunction (com.github.anba.es6draft.compiler.CompiledFunction)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 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)1 Realm (com.github.anba.es6draft.runtime.Realm)1 InitializeHostDefinedRealm (com.github.anba.es6draft.runtime.Realm.InitializeHostDefinedRealm)1 World (com.github.anba.es6draft.runtime.World)1 DebugInfo (com.github.anba.es6draft.runtime.internal.DebugInfo)1