Search in sources :

Example 6 with Script

use of com.github.anba.es6draft.Script 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)

Example 7 with Script

use of com.github.anba.es6draft.Script in project es6draft by anba.

the class ShellGlobalObject method eval.

/**
     * Parses, compiles and executes the javascript source code.
     * 
     * @param source
     *            the source object
     * @param sourceCode
     *            the source code
     * @return the evaluation result
     * @throws ParserException
     *             if the source contains any syntax errors
     * @throws CompilationException
     *             if the parsed source could not be compiled
     */
public Object eval(Source source, String sourceCode) throws ParserException, CompilationException {
    Realm realm = getRealm();
    Script script = realm.getScriptLoader().script(source, sourceCode);
    return script.evaluate(realm);
}
Also used : Script(com.github.anba.es6draft.Script) Realm(com.github.anba.es6draft.runtime.Realm)

Example 8 with Script

use of com.github.anba.es6draft.Script in project es6draft by anba.

the class ShellGlobalObject method include.

/**
     * Parses, compiles and executes the javascript file. (Uses the script cache.)
     * 
     * @param file
     *            the path to the script file
     * @throws IOException
     *             if there was any I/O error
     * @throws ParserException
     *             if the source contains any syntax errors
     * @throws CompilationException
     *             if the parsed source could not be compiled
     */
public void include(Path file) throws IOException, ParserException, CompilationException {
    Realm realm = getRealm();
    ScriptCache scriptCache = getRuntimeContext().getScriptCache();
    Path path = getRuntimeContext().getBaseDirectory().resolve(file);
    Script script = scriptCache.get(realm.getScriptLoader(), path);
    script.evaluate(realm);
}
Also used : Path(java.nio.file.Path) Script(com.github.anba.es6draft.Script) ScriptCache(com.github.anba.es6draft.runtime.internal.ScriptCache) Realm(com.github.anba.es6draft.runtime.Realm)

Example 9 with Script

use of com.github.anba.es6draft.Script in project es6draft by anba.

the class Eval method PerformEval.

/**
     * 18.2.1.1 Runtime Semantics: PerformEval( x, evalRealm, strictCaller, direct)
     * 
     * @param cx
     *            the execution context
     * @param caller
     *            the caller execution context
     * @param source
     *            the source string
     * @param flags
     *            the eval flags
     * @return the evaluation result
     */
private static Object PerformEval(ExecutionContext cx, ExecutionContext caller, Object source, int flags) {
    boolean strictCaller = EvalFlags.Strict.isSet(flags);
    boolean direct = EvalFlags.Direct.isSet(flags);
    assert direct || cx == cx.getRealm().defaultContext() : "indirect eval with non-default context";
    /* step 1 */
    assert direct || !strictCaller;
    /* step 2 */
    if (!Type.isString(source)) {
        return source;
    }
    /* step 3 */
    Script script = script(cx, caller, Type.stringValue(source).toString(), flags);
    /* step 4 */
    if (script == null) {
        return UNDEFINED;
    }
    /* steps 5-23 */
    return script.evaluate(cx);
}
Also used : Script(com.github.anba.es6draft.Script)

Example 10 with Script

use of com.github.anba.es6draft.Script in project es6draft by anba.

the class Repl method eval.

/**
     * REPL: Eval
     * 
     * @param realm
     *            the realm instance
     * @param parsedScript
     *            the parsed script node
     * @return the evaluated script result
     */
private Object eval(Realm realm, com.github.anba.es6draft.ast.Script parsedScript) {
    String className = "#typein_" + scriptCounter.incrementAndGet();
    Script script;
    if (options.noInterpreter) {
        script = realm.getScriptLoader().compile(parsedScript, className);
    } else {
        script = realm.getScriptLoader().load(parsedScript, className);
    }
    return script.evaluate(realm);
}
Also used : Script(com.github.anba.es6draft.Script)

Aggregations

Script (com.github.anba.es6draft.Script)18 Realm (com.github.anba.es6draft.runtime.Realm)10 Source (com.github.anba.es6draft.runtime.internal.Source)8 Function (com.github.anba.es6draft.runtime.internal.Properties.Function)5 SharedFunctions.loadScript (com.github.anba.es6draft.repl.global.SharedFunctions.loadScript)4 SharedFunctions.relativePathToScript (com.github.anba.es6draft.repl.global.SharedFunctions.relativePathToScript)4 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)4 CompilationException (com.github.anba.es6draft.compiler.CompilationException)3 ParserException (com.github.anba.es6draft.parser.ParserException)3 RuntimeContext (com.github.anba.es6draft.runtime.internal.RuntimeContext)3 GlobalObject (com.github.anba.es6draft.runtime.objects.GlobalObject)3 BuiltinFunction (com.github.anba.es6draft.runtime.types.builtins.BuiltinFunction)3 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)3 IOException (java.io.IOException)3 ToSource (com.github.anba.es6draft.repl.SourceBuilder.ToSource)2 ToFlatString (com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString)2 World (com.github.anba.es6draft.runtime.World)2 ScriptException (com.github.anba.es6draft.runtime.internal.ScriptException)2 ScriptLoader (com.github.anba.es6draft.runtime.internal.ScriptLoader)2 ModuleLoader (com.github.anba.es6draft.runtime.modules.ModuleLoader)2