Search in sources :

Example 11 with ScriptLoader

use of com.github.anba.es6draft.runtime.internal.ScriptLoader in project es6draft by anba.

the class TestGlobals method compileScripts.

private List<Script> compileScripts() throws IOException {
    List<?> scriptNames = configuration.getList("scripts", emptyList());
    if (scriptNames.isEmpty()) {
        return Collections.emptyList();
    }
    Path basedir = getBaseDirectory();
    RuntimeContext context = createContext();
    ScriptLoader scriptLoader = new ScriptLoader(context);
    ArrayList<Script> scripts = new ArrayList<>();
    for (String scriptName : nonEmpty(scriptNames)) {
        Source source = new Source(Resources.resourcePath(scriptName, basedir), scriptName, 1);
        Script script = scriptLoader.script(source, Resources.resource(scriptName, basedir));
        scripts.add(script);
    }
    return scripts;
}
Also used : Path(java.nio.file.Path) Script(com.github.anba.es6draft.Script) ArrayList(java.util.ArrayList) RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader) Source(com.github.anba.es6draft.runtime.internal.Source)

Example 12 with ScriptLoader

use of com.github.anba.es6draft.runtime.internal.ScriptLoader 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)
@AliasFunction(name = "dis")
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 {
            debugInfo = currentExec.getRuntimeObject().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 moduleSource = new StringModuleSource(identifier, "disassemble", sourceCode);
            SourceTextModuleRecord module = ParseModule(scriptLoader, identifier, moduleSource);
            debugInfo = module.getScriptCode().getRuntimeObject().debugInfo();
        } else {
            Source source = new Source("<disassemble>", 1);
            Script script = scriptLoader.compile(scriptLoader.parseScript(source, sourceCode), "#disassemble");
            debugInfo = script.getRuntimeObject().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.loadScript(com.github.anba.es6draft.repl.functions.SharedFunctions.loadScript) CompiledScript(com.github.anba.es6draft.compiler.CompiledScript) SharedFunctions.relativePathToScript(com.github.anba.es6draft.repl.functions.SharedFunctions.relativePathToScript) Script(com.github.anba.es6draft.Script) ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) 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) ToSource(com.github.anba.es6draft.repl.SourceBuilder.ToSource) 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) AliasFunction(com.github.anba.es6draft.runtime.internal.Properties.AliasFunction) AliasFunction(com.github.anba.es6draft.runtime.internal.Properties.AliasFunction)

Example 13 with ScriptLoader

use of com.github.anba.es6draft.runtime.internal.ScriptLoader in project es6draft by anba.

the class TestRealms method compileModules.

private PreloadModules compileModules() throws IOException, MalformedNameException {
    List<String> moduleNames = Resources.list(configuration, "modules", emptyList());
    if (moduleNames.isEmpty()) {
        return new PreloadModules(Collections.<ModuleRecord>emptyList(), Collections.<ModuleRecord>emptyList());
    }
    Path basedir = getBaseDirectory();
    RuntimeContext context = createContext();
    ScriptLoader scriptLoader = new ScriptLoader(context);
    TestModuleLoader<?> moduleLoader = getModuleLoader().apply(context, scriptLoader);
    ArrayList<ModuleRecord> modules = new ArrayList<>();
    for (String moduleName : moduleNames) {
        Map.Entry<Path, String> resourceModule = Resources.resourceModule(moduleName);
        ModuleRecord module;
        if (resourceModule == null) {
            SourceIdentifier moduleId = moduleLoader.normalizeName(moduleName, null);
            module = moduleLoader.load(moduleId);
        } else {
            Path modulePath = basedir.resolve(resourceModule.getKey());
            String sourceCode = resourceModule.getValue();
            ResourceModuleSource moduleSource = new ResourceModuleSource(modulePath, sourceCode);
            FileSourceIdentifier sourceId = new FileSourceIdentifier(modulePath);
            module = moduleLoader.defineUnlinked(sourceId, moduleSource);
        }
        modules.add(module);
    }
    return new PreloadModules(modules, moduleLoader.getModules());
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) FileSourceIdentifier(com.github.anba.es6draft.runtime.modules.loader.FileSourceIdentifier) URLSourceIdentifier(com.github.anba.es6draft.runtime.modules.loader.URLSourceIdentifier) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) Map(java.util.Map) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader) FileSourceIdentifier(com.github.anba.es6draft.runtime.modules.loader.FileSourceIdentifier)

Example 14 with ScriptLoader

use of com.github.anba.es6draft.runtime.internal.ScriptLoader in project es6draft by anba.

the class Test262Parser method runTest.

@Test
public void runTest() throws Throwable {
    RuntimeContext context = realms.newContext(new SystemConsole(), test);
    ScriptLoader loader = new ScriptLoader(context);
    Source source = new Source(test.toFile(), test.getScript().toString(), 1);
    String sourceCode = new String(Files.readAllBytes(test.toFile()), StandardCharsets.UTF_8);
    if (test.isModule()) {
        loader.parseModule(source, sourceCode);
    } else {
        loader.parseScript(source, sourceCode);
    }
}
Also used : RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) SystemConsole(com.github.anba.es6draft.util.SystemConsole) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader) Source(com.github.anba.es6draft.runtime.internal.Source) Test(org.junit.Test)

Example 15 with ScriptLoader

use of com.github.anba.es6draft.runtime.internal.ScriptLoader in project es6draft by anba.

the class FunctionConstructor method CreateDynamicFunction.

/**
 * 19.2.1.1.1 RuntimeSemantics: CreateDynamicFunction(constructor, newTarget, kind, args)
 *
 * @param callerContext
 *            the caller execution context
 * @param cx
 *            the execution context
 * @param newTarget
 *            the newTarget constructor function
 * @param kind
 *            the function kind
 * @param args
 *            the function arguments
 * @return the new function object
 */
public static FunctionObject CreateDynamicFunction(ExecutionContext callerContext, ExecutionContext cx, Constructor newTarget, SourceKind kind, Object... args) {
    /* steps 1-6 (not applicable) */
    /* steps 7-9 */
    ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
    FunctionCompiler compiler;
    Intrinsics fallbackProto;
    switch(kind) {
        case AsyncFunction:
            compiler = scriptLoader::asyncFunction;
            fallbackProto = Intrinsics.AsyncFunctionPrototype;
            break;
        case AsyncGenerator:
            compiler = scriptLoader::asyncGenerator;
            fallbackProto = Intrinsics.AsyncGenerator;
            break;
        case Function:
            compiler = scriptLoader::function;
            fallbackProto = Intrinsics.FunctionPrototype;
            break;
        case Generator:
            compiler = scriptLoader::generator;
            fallbackProto = Intrinsics.Generator;
            break;
        default:
            throw new AssertionError();
    }
    /* steps 10-15 */
    int argCount = args.length;
    String parameters, bodyText;
    if (argCount == 0) {
        parameters = "";
        bodyText = "";
    } else if (argCount == 1) {
        parameters = "";
        bodyText = ToFlatString(cx, args[0]);
    } else {
        StrBuilder sb = new StrBuilder(cx);
        Object firstArg = args[0];
        sb.append(ToFlatString(cx, firstArg));
        int k = 2;
        for (; k < argCount; ++k) {
            Object nextArg = args[k - 1];
            String nextArgString = ToFlatString(cx, nextArg);
            sb.append(',').append(nextArgString);
        }
        parameters = sb.toString();
        bodyText = ToFlatString(cx, args[k - 1]);
    }
    /* steps 16-17, 19-28 */
    Source source = functionSource(kind, callerContext);
    CompiledFunction compiledFunction;
    try {
        compiledFunction = compiler.compile(source, parameters, bodyText);
    } catch (ParserException | CompilationException e) {
        throw e.toScriptException(cx);
    }
    /* step 29 */
    ScriptObject proto = GetPrototypeFromConstructor(cx, newTarget, fallbackProto);
    /* steps 18, 30-38 */
    return CreateDynamicFunction(cx, kind, compiledFunction, proto);
}
Also used : CompiledFunction(com.github.anba.es6draft.compiler.CompiledFunction) ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ToFlatString(com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString) Source(com.github.anba.es6draft.runtime.internal.Source) Intrinsics(com.github.anba.es6draft.runtime.types.Intrinsics) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader) StrBuilder(com.github.anba.es6draft.runtime.internal.StrBuilder)

Aggregations

ScriptLoader (com.github.anba.es6draft.runtime.internal.ScriptLoader)15 Source (com.github.anba.es6draft.runtime.internal.Source)13 CompilationException (com.github.anba.es6draft.compiler.CompilationException)6 ParserException (com.github.anba.es6draft.parser.ParserException)6 Intrinsics (com.github.anba.es6draft.runtime.types.Intrinsics)6 Script (com.github.anba.es6draft.Script)5 RuntimeContext (com.github.anba.es6draft.runtime.internal.RuntimeContext)5 RuntimeInfo (com.github.anba.es6draft.runtime.internal.RuntimeInfo)5 ModuleSource (com.github.anba.es6draft.runtime.modules.ModuleSource)5 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)5 GlobalEnvironmentRecord (com.github.anba.es6draft.runtime.GlobalEnvironmentRecord)4 SourceIdentifier (com.github.anba.es6draft.runtime.modules.SourceIdentifier)4 StringModuleSource (com.github.anba.es6draft.runtime.modules.loader.StringModuleSource)4 FunctionConstructor.functionSource (com.github.anba.es6draft.runtime.objects.FunctionConstructor.functionSource)4 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)4 ArrayList (java.util.ArrayList)4 ToFlatString (com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString)3 Function (com.github.anba.es6draft.runtime.internal.Properties.Function)3 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)3 Path (java.nio.file.Path)3