Search in sources :

Example 6 with ScriptLoader

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

the class ShellFunctions method disScript.

/**
 * shell-function: {@code disScript(sourceCode)}
 *
 * @param cx
 *            the execution context
 * @param sourceCode
 *            the script source code
 * @throws ReflectiveOperationException
 *             if there was an error getting the class bytes
 */
@Function(name = "disScript", arity = 1)
public void disScript(ExecutionContext cx, String sourceCode) throws ReflectiveOperationException {
    ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
    Source source = new Source("<disassemble>", 1);
    CompiledScript script = scriptLoader.compile(scriptLoader.parseScript(source, sourceCode), "#disassemble");
    byte[] classBytes = DebugInfo.classBytes(script.getClass());
    String disassembled = Code.toByteCode(classBytes, true);
    cx.getRuntimeContext().getConsole().writer().println(disassembled);
}
Also used : CompiledScript(com.github.anba.es6draft.compiler.CompiledScript) 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) ToSource(com.github.anba.es6draft.repl.SourceBuilder.ToSource) Source(com.github.anba.es6draft.runtime.internal.Source) Function(com.github.anba.es6draft.runtime.internal.Properties.Function) AliasFunction(com.github.anba.es6draft.runtime.internal.Properties.AliasFunction)

Example 7 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)
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 ScriptLoader

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

the class AsyncFunctionConstructor 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 args
     *            the function arguments
     * @return the new generator function object
     */
private static OrdinaryAsyncFunction CreateDynamicFunction(ExecutionContext callerContext, ExecutionContext cx, Constructor newTarget, Object... args) {
    /* step 1 (not applicable) */
    /* step 2 (not applicable) */
    /* step 3 */
    Intrinsics fallbackProto = Intrinsics.AsyncFunction;
    /* steps 4-10 */
    String[] sourceText = functionSourceText(cx, args);
    String parameters = sourceText[0], bodyText = sourceText[1];
    /* steps 11, 13-20 */
    Source source = functionSource(SourceKind.AsyncFunction, cx.getRealm(), callerContext);
    RuntimeInfo.Function function;
    try {
        ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
        function = scriptLoader.asyncFunction(source, parameters, bodyText).getFunction();
    } catch (ParserException | CompilationException e) {
        throw e.toScriptException(cx);
    }
    /* step 12 */
    boolean strict = function.isStrict();
    /* steps 21-22 */
    ScriptObject proto = GetPrototypeFromConstructor(cx, newTarget, fallbackProto);
    /* step 23 */
    OrdinaryAsyncFunction f = FunctionAllocate(cx, proto, strict, FunctionKind.Normal);
    /* steps 24-25 */
    LexicalEnvironment<GlobalEnvironmentRecord> scope = f.getRealm().getGlobalEnv();
    /* step 26 */
    FunctionInitialize(f, FunctionKind.Normal, function, scope, newFunctionExecutable(source));
    /* steps 27-28 (not applicable) */
    /* step 29 */
    SetFunctionName(f, "anonymous");
    /* step 30 */
    return f;
}
Also used : ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) RuntimeInfo(com.github.anba.es6draft.runtime.internal.RuntimeInfo) GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) FunctionConstructor.functionSource(com.github.anba.es6draft.runtime.objects.FunctionConstructor.functionSource) Source(com.github.anba.es6draft.runtime.internal.Source) Intrinsics(com.github.anba.es6draft.runtime.types.Intrinsics) OrdinaryAsyncFunction(com.github.anba.es6draft.runtime.types.builtins.OrdinaryAsyncFunction) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader)

Example 9 with ScriptLoader

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

the class GeneratorFunctionConstructor method CreateDynamicConstructorGenerator.

private static OrdinaryConstructorGenerator CreateDynamicConstructorGenerator(ExecutionContext callerContext, ExecutionContext cx, Constructor newTarget, Object... args) {
    /* step 1 (not applicable) */
    /* step 2 (not applicable) */
    /* step 3 */
    Intrinsics fallbackProto = Intrinsics.Generator;
    /* steps 4-10 */
    String[] sourceText = functionSourceText(cx, args);
    String parameters = sourceText[0], bodyText = sourceText[1];
    /* steps 11, 13-20 */
    Source source = functionSource(SourceKind.Generator, cx.getRealm(), callerContext);
    RuntimeInfo.Function function;
    try {
        ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
        function = scriptLoader.generator(source, parameters, bodyText).getFunction();
    } catch (ParserException | CompilationException e) {
        throw e.toScriptException(cx);
    }
    /* step 12 */
    boolean strict = function.isStrict();
    /* steps 21-22 */
    ScriptObject proto = GetPrototypeFromConstructor(cx, newTarget, fallbackProto);
    /* step 23 */
    OrdinaryConstructorGenerator f = OrdinaryConstructorGenerator.FunctionAllocate(cx, proto, strict, FunctionKind.Normal);
    /* steps 24-25 */
    LexicalEnvironment<GlobalEnvironmentRecord> scope = f.getRealm().getGlobalEnv();
    /* step 26 */
    FunctionInitialize(f, FunctionKind.Normal, function, scope, newFunctionExecutable(source));
    /* step 27 */
    OrdinaryObject prototype = ObjectCreate(cx, Intrinsics.GeneratorPrototype);
    MakeConstructor(f, true, prototype);
    /* step 28 (not applicable) */
    /* step 29 */
    SetFunctionName(f, "anonymous");
    /* step 30 */
    return f;
}
Also used : ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) RuntimeInfo(com.github.anba.es6draft.runtime.internal.RuntimeInfo) GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) OrdinaryConstructorGenerator(com.github.anba.es6draft.runtime.types.builtins.OrdinaryConstructorGenerator) FunctionConstructor.functionSource(com.github.anba.es6draft.runtime.objects.FunctionConstructor.functionSource) Source(com.github.anba.es6draft.runtime.internal.Source) Intrinsics(com.github.anba.es6draft.runtime.types.Intrinsics) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader)

Example 10 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 args
     *            the function arguments
     * @return the new function object
     */
private static FunctionObject CreateDynamicFunction(ExecutionContext callerContext, ExecutionContext cx, Constructor newTarget, Object... args) {
    /* step 1 (not applicable) */
    /* step 2 */
    Intrinsics fallbackProto = Intrinsics.FunctionPrototype;
    /* step 3 (not applicable) */
    /* steps 4-10 */
    String[] sourceText = functionSourceText(cx, args);
    String parameters = sourceText[0], bodyText = sourceText[1];
    /* steps 11, 13-20 */
    Source source = functionSource(SourceKind.Function, cx.getRealm(), callerContext);
    RuntimeInfo.Function function;
    try {
        ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
        function = scriptLoader.function(source, parameters, bodyText).getFunction();
    } catch (ParserException | CompilationException e) {
        throw e.toScriptException(cx);
    }
    /* steps 12, 21-30 */
    return CreateDynamicFunction(cx, source, function, newTarget, fallbackProto);
}
Also used : ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) RuntimeInfo(com.github.anba.es6draft.runtime.internal.RuntimeInfo) Intrinsics(com.github.anba.es6draft.runtime.types.Intrinsics) ToFlatString(com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString) Source(com.github.anba.es6draft.runtime.internal.Source) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader)

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