Search in sources :

Example 11 with FunctionObject

use of com.github.anba.es6draft.runtime.types.builtins.FunctionObject 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 12 with FunctionObject

use of com.github.anba.es6draft.runtime.types.builtins.FunctionObject in project es6draft by anba.

the class MozShellFunctions method debugInfo.

private static DebugInfo debugInfo(ExecutionContext caller, Object... args) {
    if (args.length == 0) {
        FunctionObject currentFunction = caller.getCurrentFunction();
        Executable currentExec = caller.getCurrentExecutable();
        if (currentFunction != null && currentFunction.getExecutable() == currentExec) {
            return currentFunction.getCode().debugInfo();
        } else if (currentExec != null && currentExec.getSourceObject() != null) {
            return currentExec.getSourceObject().debugInfo();
        }
    } else if (args[0] instanceof FunctionObject) {
        return ((FunctionObject) args[0]).getCode().debugInfo();
    }
    return null;
}
Also used : FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) BoundFunctionObject(com.github.anba.es6draft.runtime.types.builtins.BoundFunctionObject) Executable(com.github.anba.es6draft.Executable)

Example 13 with FunctionObject

use of com.github.anba.es6draft.runtime.types.builtins.FunctionObject in project es6draft by anba.

the class Bootstrap method superSetup.

@SuppressWarnings("unused")
private static MethodHandle superSetup(MutableCallSite callsite, Constructor constructor, ExecutionContext cx, Constructor newTarget, Object[] arguments) {
    MethodHandle target, test;
    if (constructor instanceof FunctionObject && constructor instanceof Constructor) {
        FunctionObject fn = (FunctionObject) constructor;
        test = MethodHandles.insertArguments(testFunctionObjectMH, 1, fn.getMethodInfo());
        target = fn.getConstructMethod();
    } else if (constructor instanceof BuiltinConstructor) {
        BuiltinConstructor fn = (BuiltinConstructor) constructor;
        test = MethodHandles.insertArguments(testBuiltinFunctionMH, 1, fn.getMethodInfo());
        target = fn.getConstructMethod();
    } else {
        target = test = null;
    }
    if (test != null) {
        test = test.asType(test.type().changeParameterType(0, Constructor.class));
    }
    return setCallSiteTarget(callsite, target, test, superGenericMH);
}
Also used : BuiltinConstructor(com.github.anba.es6draft.runtime.types.builtins.BuiltinConstructor) Constructor(com.github.anba.es6draft.runtime.types.Constructor) BuiltinConstructor(com.github.anba.es6draft.runtime.types.builtins.BuiltinConstructor) CheckConstructor(com.github.anba.es6draft.runtime.internal.ScriptRuntime.CheckConstructor) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) MethodHandle(java.lang.invoke.MethodHandle)

Example 14 with FunctionObject

use of com.github.anba.es6draft.runtime.types.builtins.FunctionObject in project es6draft by anba.

the class Bootstrap method constructSetup.

@SuppressWarnings("unused")
private static MethodHandle constructSetup(MutableCallSite callsite, Object constructor, ExecutionContext cx, Object[] arguments) {
    MethodHandle target, test;
    if (constructor instanceof FunctionObject && constructor instanceof Constructor) {
        FunctionObject fn = (FunctionObject) constructor;
        test = MethodHandles.insertArguments(testFunctionObjectMH, 1, fn.getMethodInfo());
        target = fn.getConstructMethod();
    } else if (constructor instanceof BuiltinConstructor) {
        BuiltinConstructor fn = (BuiltinConstructor) constructor;
        test = MethodHandles.insertArguments(testBuiltinFunctionMH, 1, fn.getMethodInfo());
        target = fn.getConstructMethod();
    } else {
        target = test = null;
    }
    if (target != null) {
        // Insert constructor as newTarget argument.
        target = target.asType(target.type().changeParameterType(2, constructor.getClass()));
        target = MethodHandles.permuteArguments(target, target.type().dropParameterTypes(2, 3), 0, 1, 0, 2);
    }
    return setCallSiteTarget(callsite, target, test, constructGenericMH);
}
Also used : BuiltinConstructor(com.github.anba.es6draft.runtime.types.builtins.BuiltinConstructor) Constructor(com.github.anba.es6draft.runtime.types.Constructor) BuiltinConstructor(com.github.anba.es6draft.runtime.types.builtins.BuiltinConstructor) CheckConstructor(com.github.anba.es6draft.runtime.internal.ScriptRuntime.CheckConstructor) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) MethodHandle(java.lang.invoke.MethodHandle)

Example 15 with FunctionObject

use of com.github.anba.es6draft.runtime.types.builtins.FunctionObject in project es6draft by anba.

the class RuntimeFunctions method IsFunctionExpression.

/**
     * Native function: {@code %IsFunctionExpression(<function>)}.
     * <p>
     * Returns {@code true} if <var>function</var> is a function expression.
     * 
     * @param function
     *            the function object
     * @return {@code true} if <var>function</var> is a function expression
     */
public static boolean IsFunctionExpression(Callable function) {
    if (!(function instanceof FunctionObject)) {
        return false;
    }
    FunctionObject funObj = (FunctionObject) function;
    RuntimeInfo.Function code = funObj.getCode();
    if (code == null) {
        return false;
    }
    return code.is(RuntimeInfo.FunctionFlags.Expression) && !code.is(RuntimeInfo.FunctionFlags.Arrow);
}
Also used : FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject)

Aggregations

FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)15 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)7 Name (com.github.anba.es6draft.ast.scope.Name)6 Declaration (com.github.anba.es6draft.ast.Declaration)5 HoistableDeclaration (com.github.anba.es6draft.ast.HoistableDeclaration)5 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)5 LexicalEnvironment (com.github.anba.es6draft.runtime.LexicalEnvironment)5 HashSet (java.util.HashSet)5 StatementListItem (com.github.anba.es6draft.ast.StatementListItem)4 DeclarativeEnvironmentRecord (com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)4 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)4 ArrayDeque (java.util.ArrayDeque)4 FunctionDeclaration (com.github.anba.es6draft.ast.FunctionDeclaration)3 VariableDeclaration (com.github.anba.es6draft.ast.VariableDeclaration)3 VariableStatement (com.github.anba.es6draft.ast.VariableStatement)3 ScriptName (com.github.anba.es6draft.compiler.CodeGenerator.ScriptName)3 GlobalEnvironmentRecord (com.github.anba.es6draft.runtime.GlobalEnvironmentRecord)3 Constructor (com.github.anba.es6draft.runtime.types.Constructor)3 Undefined (com.github.anba.es6draft.runtime.types.Undefined)3 MethodHandle (java.lang.invoke.MethodHandle)3