Search in sources :

Example 1 with CompiledFunction

use of com.github.anba.es6draft.compiler.CompiledFunction 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

CompiledFunction (com.github.anba.es6draft.compiler.CompiledFunction)1 Source (com.github.anba.es6draft.runtime.internal.Source)1 ModuleSource (com.github.anba.es6draft.runtime.modules.ModuleSource)1