Search in sources :

Example 1 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 2 with Function

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

the class AtomicsTestFunctions method evalInWorker.

/**
     * shell-function: {@code evalInWorker(sourceString)}
     * 
     * @param cx
     *            the execution context
     * @param caller
     *            the caller execution context
     * @param sourceString
     *            the script source code
     * @return {@code true} if a new script worker was started, otherwise returns {@code false}
     */
@Function(name = "evalInWorker", arity = 1)
public boolean evalInWorker(ExecutionContext cx, ExecutionContext caller, String sourceString) {
    Source baseSource = Objects.requireNonNull(cx.getRealm().sourceInfo(caller));
    try {
        // TODO: Initialize extensions (console.jsm, window timers?).
        CompletableFuture.supplyAsync(() -> {
            // Set 'executor' to null so it doesn't get shared with the current runtime context.
            /* @formatter:off */
            RuntimeContext context = new RuntimeContext.Builder(cx.getRuntimeContext()).setExecutor(null).build();
            /* @formatter:on */
            World world = new World(context);
            Realm realm;
            try {
                realm = world.newInitializedRealm();
            } catch (IOException | URISyntaxException e) {
                throw new CompletionException(e);
            }
            // Bind test functions to this instance.
            realm.createGlobalProperties(this, AtomicsTestFunctions.class);
            // TODO: Add proper abstraction.
            ModuleLoader moduleLoader = world.getModuleLoader();
            if (moduleLoader instanceof NodeModuleLoader) {
                try {
                    ((NodeModuleLoader) moduleLoader).initialize(realm);
                } catch (IOException | URISyntaxException | MalformedNameException | ResolutionException e) {
                    throw new CompletionException(e);
                }
            }
            // Evaluate the script source code and then run pending jobs.
            Source source = new Source(baseSource, "evalInWorker-script", 1);
            Script script = realm.getScriptLoader().script(source, sourceString);
            Object result = script.evaluate(realm);
            world.runEventLoop();
            return result;
        }, cx.getRuntimeContext().getWorkerExecutor()).whenComplete((r, e) -> {
            if (e instanceof CompletionException) {
                Throwable cause = ((CompletionException) e).getCause();
                cx.getRuntimeContext().getWorkerErrorReporter().accept(cx, (cause != null ? cause : e));
            } else if (e != null) {
                cx.getRuntimeContext().getWorkerErrorReporter().accept(cx, e);
            }
        });
        return true;
    } catch (RejectedExecutionException e) {
        return false;
    }
}
Also used : Script(com.github.anba.es6draft.Script) ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) NodeModuleLoader(com.github.anba.es6draft.repl.loader.NodeModuleLoader) NodeModuleLoader(com.github.anba.es6draft.repl.loader.NodeModuleLoader) World(com.github.anba.es6draft.runtime.World) Source(com.github.anba.es6draft.runtime.internal.Source) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) CompletionException(java.util.concurrent.CompletionException) SharedArrayBufferObject(com.github.anba.es6draft.runtime.objects.atomics.SharedArrayBufferObject) RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) Realm(com.github.anba.es6draft.runtime.Realm) Function(com.github.anba.es6draft.runtime.internal.Properties.Function)

Example 3 with Function

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

the class BaseShellFunctions method load.

/**
     * shell-function: {@code load(filename)}
     *
     * @param cx
     *            the execution context
     * @param filename
     *            the file to load
     */
@Function(name = "load", arity = 1)
public void load(ExecutionContext cx, String filename) {
    Path file = Paths.get(filename);
    loadScript(cx, file, absolutePath(cx, 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 4 with Function

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

the class BaseShellFunctions method print.

/**
     * shell-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 5 with Function

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

the class MozShellFunctions method evalcx.

/**
     * shell-function: {@code evalcx(s, [o])}
     *
     * @param cx
     *            the execution context
     * @param caller
     *            the caller context
     * @param sourceCode
     *            the source to evaluate
     * @param o
     *            the global object
     * @return the eval result value
     */
@Function(name = "evalcx", arity = 1)
public Object evalcx(ExecutionContext cx, ExecutionContext caller, String sourceCode, Object o) {
    ScriptObject global;
    if (Type.isUndefinedOrNull(o)) {
        global = newGlobal(cx);
    } else {
        global = ToObject(cx, o);
    }
    if (sourceCode.isEmpty() || "lazy".equals(sourceCode)) {
        return global;
    }
    if (!(global instanceof GlobalObject)) {
        throw Errors.newError(cx, "invalid global argument");
    }
    Source source = new Source(cx.getRealm().sourceInfo(caller), "evalcx", 1);
    Realm realm = ((GlobalObject) global).getRealm();
    try {
        Script script = realm.getScriptLoader().script(source, sourceCode);
        return script.evaluate(realm);
    } catch (ParserException | CompilationException e) {
        // Create a script exception from the requested code realm, not from the caller's realm.
        throw e.toScriptException(realm.defaultContext());
    }
}
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) Script(com.github.anba.es6draft.Script) SharedFunctions.loadScript(com.github.anba.es6draft.repl.global.SharedFunctions.loadScript) ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) Realm(com.github.anba.es6draft.runtime.Realm) 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) BuiltinFunction(com.github.anba.es6draft.runtime.types.builtins.BuiltinFunction)

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