Search in sources :

Example 11 with Function

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

the class ScriptingFunctions method print.

/**
     * builtin-function: {@code print(message)}
     *
     * @param cx
     *            the execution context
     * @param messages
     *            the string to print
     */
@Function(name = "print", arity = 1)
public void print(ExecutionContext cx, String... messages) {
    PrintWriter writer = cx.getRuntimeContext().getConsole().writer();
    writer.println(Strings.concatWith(' ', messages));
}
Also used : PrintWriter(java.io.PrintWriter) Function(com.github.anba.es6draft.runtime.internal.Properties.Function)

Example 12 with Function

use of com.github.anba.es6draft.runtime.internal.Properties.Function 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 13 with Function

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

the class ShellFunctions method loadRelativeToScript.

/**
     * shell-function: {@code loadRelativeToScript(filename)}
     * 
     * @param cx
     *            the execution context
     * @param caller
     *            the caller execution context
     * @param filename
     *            the file to load
     */
@Function(name = "loadRelativeToScript", arity = 1)
public void loadRelativeToScript(ExecutionContext cx, ExecutionContext caller, String filename) {
    Path file = Paths.get(filename);
    loadScript(cx, file, relativePathToScript(cx, caller, file));
}
Also used : SharedFunctions.absolutePath(com.github.anba.es6draft.repl.global.SharedFunctions.absolutePath) Path(java.nio.file.Path) Function(com.github.anba.es6draft.runtime.internal.Properties.Function)

Example 14 with Function

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

the class ShellFunctions method dumpSymbolRegistry.

/**
     * shell-function: {@code dumpSymbolRegistry()}
     * 
     * @param cx
     *            the execution context
     */
@Function(name = "dumpSymbolRegistry", arity = 0)
public void dumpSymbolRegistry(ExecutionContext cx) {
    PrintWriter writer = cx.getRuntimeContext().getConsole().writer();
    writer.println(cx.getRealm().getSymbolRegistry().toString());
}
Also used : PrintWriter(java.io.PrintWriter) Function(com.github.anba.es6draft.runtime.internal.Properties.Function)

Example 15 with Function

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

the class ShellFunctions method putstr.

/**
     * shell-function: {@code putstr(message)}
     *
     * @param cx
     *            the execution context
     * @param message
     *            the message to write
     */
@Function(name = "putstr", arity = 1)
public void putstr(ExecutionContext cx, String message) {
    PrintWriter writer = cx.getRuntimeContext().getConsole().writer();
    writer.print(message);
    writer.flush();
}
Also used : PrintWriter(java.io.PrintWriter) Function(com.github.anba.es6draft.runtime.internal.Properties.Function)

Aggregations

Function (com.github.anba.es6draft.runtime.internal.Properties.Function)22 PrintWriter (java.io.PrintWriter)12 Source (com.github.anba.es6draft.runtime.internal.Source)6 BuiltinFunction (com.github.anba.es6draft.runtime.types.builtins.BuiltinFunction)6 Script (com.github.anba.es6draft.Script)5 SharedFunctions.absolutePath (com.github.anba.es6draft.repl.global.SharedFunctions.absolutePath)5 Realm (com.github.anba.es6draft.runtime.Realm)5 Path (java.nio.file.Path)5 SharedFunctions.loadScript (com.github.anba.es6draft.repl.global.SharedFunctions.loadScript)4 SharedFunctions.relativePathToScript (com.github.anba.es6draft.repl.global.SharedFunctions.relativePathToScript)4 CompilationException (com.github.anba.es6draft.compiler.CompilationException)3 ParserException (com.github.anba.es6draft.parser.ParserException)3 ToFlatString (com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString)3 ModuleLoader (com.github.anba.es6draft.runtime.modules.ModuleLoader)3 ModuleSource (com.github.anba.es6draft.runtime.modules.ModuleSource)3 StringModuleSource (com.github.anba.es6draft.runtime.modules.loader.StringModuleSource)3 GlobalObject (com.github.anba.es6draft.runtime.objects.GlobalObject)3 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)3 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)3 IOException (java.io.IOException)3