Search in sources :

Example 6 with Function

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

the class MozShellFunctions method run.

/**
     * shell-function: {@code run(file)}
     *
     * @param cx
     *            the execution context
     * @param fileName
     *            the file to evaluate
     * @return the execution time in milli-seconds
     */
@Function(name = "run", arity = 1)
public double run(ExecutionContext cx, String fileName) {
    long start = System.nanoTime();
    Path file = Paths.get(fileName);
    loadScript(cx, file, absolutePath(cx, file));
    long end = System.nanoTime();
    return (double) TimeUnit.NANOSECONDS.toMillis(end - start);
}
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) BuiltinFunction(com.github.anba.es6draft.runtime.types.builtins.BuiltinFunction)

Example 7 with Function

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

the class ShellFunctions method compile.

/**
     * shell-function: {@code compile(filename)}
     *
     * @param cx
     *            the execution context
     * @param filename
     *            the file to load
     * @return the status message
     */
@Function(name = "compile", arity = 1)
public String compile(ExecutionContext cx, String filename) {
    try {
        Path file = absolutePath(cx, Paths.get(filename));
        cx.getRealm().getScriptLoader().script(new Source(file, filename, 1), file);
    } catch (ParserException | CompilationException | IOException e) {
        return "error: " + e.getMessage();
    }
    return "success";
}
Also used : SharedFunctions.absolutePath(com.github.anba.es6draft.repl.global.SharedFunctions.absolutePath) Path(java.nio.file.Path) ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) IOException(java.io.IOException) 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) Function(com.github.anba.es6draft.runtime.internal.Properties.Function)

Example 8 with Function

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

the class ShellFunctions method dump.

/**
     * shell-function: {@code dump(object)}
     * 
     * @param cx
     *            the execution context
     * @param object
     *            the object to inspect
     */
@Function(name = "dump", arity = 1)
public void dump(ExecutionContext cx, Object object) {
    String id;
    if (!Type.isType(object)) {
        // foreign object or null
        id = Objects.toString(object, "<null>");
    } else if (!Type.isObject(object)) {
        // primitive value
        id = String.format("%s (%s)", object.toString(), object.getClass().getSimpleName());
    } else {
        // script objects
        id = String.format("%s@%x", object.getClass().getSimpleName(), System.identityHashCode(object));
    }
    PrintWriter writer = cx.getRuntimeContext().getConsole().writer();
    writer.println(id);
}
Also used : ToFlatString(com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString) PrintWriter(java.io.PrintWriter) Function(com.github.anba.es6draft.runtime.internal.Properties.Function)

Example 9 with Function

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

the class ShellFunctions method dumpScope.

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

Example 10 with Function

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

the class ShellFunctions method evalScript.

/**
     * shell-function: {@code evalScript(sourceString, [options])}
     * 
     * @param cx
     *            the execution context
     * @param sourceString
     *            the source string
     * @param options
     *            the options object (optional)
     * @return the evaluation result
     */
@Function(name = "evalScript", arity = 1)
public Object evalScript(ExecutionContext cx, String sourceString, Object options) {
    String name = "";
    int line = 1;
    Realm realm = cx.getRealm();
    if (Type.isObject(options)) {
        ScriptObject opts = Type.objectValue(options);
        Object fileName = Get(cx, opts, "fileName");
        if (!Type.isUndefined(fileName)) {
            name = ToFlatString(cx, fileName);
        }
        Object lineNumber = Get(cx, opts, "lineNumber");
        if (!Type.isUndefined(lineNumber)) {
            line = ToInt32(cx, lineNumber);
        }
        Object g = Get(cx, opts, "global");
        if (!Type.isUndefined(g)) {
            if (!(g instanceof GlobalObject)) {
                throw Errors.newError(cx, "invalid global argument");
            }
            realm = ((GlobalObject) g).getRealm();
        }
        Object r = Get(cx, opts, "realm");
        if (!Type.isUndefined(r)) {
            if (!(r instanceof RealmObject)) {
                throw Errors.newError(cx, "invalid realm argument");
            }
            realm = ((RealmObject) r).getRealm();
        }
    }
    Source source = new Source(name, line);
    Script script = realm.getScriptLoader().script(source, sourceString);
    return script.evaluate(realm);
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) GlobalObject(com.github.anba.es6draft.runtime.objects.GlobalObject) 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) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) WeakMapObject(com.github.anba.es6draft.runtime.objects.collection.WeakMapObject) ErrorObject(com.github.anba.es6draft.runtime.objects.ErrorObject) GlobalObject(com.github.anba.es6draft.runtime.objects.GlobalObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) ArrayBufferObject(com.github.anba.es6draft.runtime.objects.binary.ArrayBufferObject) RealmObject(com.github.anba.es6draft.runtime.objects.reflect.RealmObject) RealmObject(com.github.anba.es6draft.runtime.objects.reflect.RealmObject) ToFlatString(com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString) Realm(com.github.anba.es6draft.runtime.Realm) 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) 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