Search in sources :

Example 11 with Script

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

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

the class ShellGlobalObject method eval.

/**
     * Parses, compiles and executes the javascript file.
     * 
     * @param fileName
     *            the file name for the script file
     * @param file
     *            the absolute path to the file
     * @return the evaluation result
     * @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 Object eval(Path fileName, Path file) throws IOException, ParserException, CompilationException {
    Realm realm = getRealm();
    Source source = new Source(file, fileName.toString(), 1);
    Script script = realm.getScriptLoader().script(source, file);
    return script.evaluate(realm);
}
Also used : Script(com.github.anba.es6draft.Script) Realm(com.github.anba.es6draft.runtime.Realm) Source(com.github.anba.es6draft.runtime.internal.Source)

Example 13 with Script

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

the class MozShellFunctions method evaluate.

/**
     * shell-function: {@code evaluate(code, [options])}
     *
     * @param cx
     *            the execution context
     * @param caller
     *            the caller context
     * @param code
     *            the source code to evaluate
     * @param options
     *            additional options object
     * @return the eval result value
     */
@Function(name = "evaluate", arity = 2)
public Object evaluate(ExecutionContext cx, ExecutionContext caller, Object code, Object options) {
    if (!(Type.isString(code) && (Type.isUndefined(options) || Type.isObject(options)))) {
        throw Errors.newError(cx, "invalid arguments");
    }
    String sourceCode = Type.stringValue(code).toString();
    String sourceName = "@evaluate";
    int sourceLine = 1;
    boolean noScriptRval = false;
    boolean catchTermination = false;
    Realm realm = cx.getRealm();
    if (Type.isObject(options)) {
        ScriptObject opts = Type.objectValue(options);
        Object fileName = Get(cx, opts, "fileName");
        if (!Type.isUndefined(fileName)) {
            sourceName = Type.isNull(fileName) ? "" : ToFlatString(cx, fileName);
        }
        Object lineNumber = Get(cx, opts, "lineNumber");
        if (!Type.isUndefined(lineNumber)) {
            sourceLine = ToInt32(cx, lineNumber);
        }
        Object g = Get(cx, opts, "global");
        if (!Type.isUndefined(g)) {
            ScriptObject obj = ToObject(cx, g);
            if (!(obj instanceof GlobalObject)) {
                throw Errors.newError(cx, "invalid global argument");
            }
            realm = ((GlobalObject) obj).getRealm();
        }
        noScriptRval = ToBoolean(Get(cx, opts, "noScriptRval"));
        catchTermination = ToBoolean(Get(cx, opts, "catchTermination"));
    }
    Source source = new Source(cx.getRealm().sourceInfo(caller), sourceName, sourceLine);
    try {
        Script script = realm.getScriptLoader().script(source, sourceCode);
        Object result = script.evaluate(realm);
        return (!noScriptRval ? result : UNDEFINED);
    } catch (ParserException | CompilationException e) {
        // Create a script exception from the requested code realm, not from the caller's realm.
        throw e.toScriptException(realm.defaultContext());
    } catch (ScriptException | StackOverflowError e) {
        throw e;
    } catch (Error | Exception e) {
        if (catchTermination) {
            return "terminated";
        }
        throw Errors.newError(cx, Objects.toString(e.getMessage(), ""));
    }
}
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) ToSource(com.github.anba.es6draft.repl.SourceBuilder.ToSource) Source(com.github.anba.es6draft.runtime.internal.Source) URISyntaxException(java.net.URISyntaxException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) ParserException(com.github.anba.es6draft.parser.ParserException) IOException(java.io.IOException) MalformedNameException(com.github.anba.es6draft.runtime.modules.MalformedNameException) ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ProxyObject(com.github.anba.es6draft.runtime.types.builtins.ProxyObject) GlobalObject(com.github.anba.es6draft.runtime.objects.GlobalObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) WeakMapObject(com.github.anba.es6draft.runtime.objects.collection.WeakMapObject) BoundFunctionObject(com.github.anba.es6draft.runtime.types.builtins.BoundFunctionObject) Realm(com.github.anba.es6draft.runtime.Realm) Function(com.github.anba.es6draft.runtime.internal.Properties.Function) BuiltinFunction(com.github.anba.es6draft.runtime.types.builtins.BuiltinFunction)

Example 14 with Script

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

the class NativeCode method load.

/**
     * Parses, compiles and executes the javascript file. (Uses the script cache.)
     * <p>
     * The script file is loaded as a native script with elevated privileges.
     * 
     * @param realm
     *            the realm instance
     * @param name
     *            the script name
     * @throws IOException
     *             if there was any I/O error
     * @throws URISyntaxException
     *             the URL is not a valid URI
     * @throws ParserException
     *             if the source contains any syntax errors
     * @throws CompilationException
     *             if the parsed source could not be compiled
     */
public static void load(Realm realm, String name) throws IOException, URISyntaxException, ParserException, CompilationException {
    RuntimeContext context = realm.getWorld().getContext();
    ScriptCache scriptCache = context.getScriptCache();
    Script script = scriptCache.get(createNativeScriptLoader(context), getScriptURL(name));
    script.evaluate(realm);
}
Also used : Script(com.github.anba.es6draft.Script)

Example 15 with Script

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

the class ScriptCache method get.

/**
     * Compiles {@code file} to a {@link Script} and caches the result.
     * 
     * @param scriptLoader
     *            the script loader
     * @param file
     *            the script file path
     * @return the compiled script
     * @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 Script get(ScriptLoader scriptLoader, Path file) throws IOException, ParserException, CompilationException {
    CacheKey cacheKey = keyFor(file);
    Script cachedScript = cache.get(cacheKey);
    if (cachedScript != null) {
        return cachedScript;
    }
    Source source = new Source(file, Objects.requireNonNull(file.getFileName()).toString(), 1);
    Script script = scriptLoader.script(source, file);
    cache.put(cacheKey, script);
    return script;
}
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