⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 context.java

📁 javascript语言的解释器源码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
     * All characters of the reader are consumed.     *     * @param scope the scope to execute in     * @param in the Reader to get JavaScript source from     * @param sourceName a string describing the source, such as a filename     * @param lineno the starting line number     * @param securityDomain an arbitrary object that specifies security     *        information about the origin or owner of the script. For     *        implementations that don't care about security, this value     *        may be null.     * @return the result of evaluating the source     *     * @exception IOException if an IOException was generated by the Reader     */    public final Object evaluateReader(Scriptable scope, Reader in,                                       String sourceName, int lineno,                                       Object securityDomain)        throws IOException    {        Script script = compileReader(scope, in, sourceName, lineno,                                      securityDomain);        if (script != null) {            return script.exec(this, scope);        } else {            return null;        }    }    /**     * Check whether a string is ready to be compiled.     * <p>     * stringIsCompilableUnit is intended to support interactive compilation of     * javascript.  If compiling the string would result in an error     * that might be fixed by appending more source, this method     * returns false.  In every other case, it returns true.     * <p>     * Interactive shells may accumulate source lines, using this     * method after each new line is appended to check whether the     * statement being entered is complete.     *     * @param source the source buffer to check     * @return whether the source is ready for compilation     * @since 1.4 Release 2     */    public final boolean stringIsCompilableUnit(String source)    {        boolean errorseen = false;        CompilerEnvirons compilerEnv = new CompilerEnvirons();        compilerEnv.initFromContext(this);        // no source name or source text manager, because we're just        // going to throw away the result.        compilerEnv.setGeneratingSource(false);        Parser p = new Parser(compilerEnv, DefaultErrorReporter.instance);        try {            p.parse(source, null, 1);        } catch (EvaluatorException ee) {            errorseen = true;        }        // Return false only if an error occurred as a result of reading past        // the end of the file, i.e. if the source could be fixed by        // appending more source.        if (errorseen && p.eof())            return false;        else            return true;    }    /**     * @deprecated     * @see #compileReader(Reader in, String sourceName, int lineno,     *                     Object securityDomain)     */    public final Script compileReader(Scriptable scope, Reader in,                                      String sourceName, int lineno,                                      Object securityDomain)        throws IOException    {        return compileReader(in, sourceName, lineno, securityDomain);    }    /**     * Compiles the source in the given reader.     * <p>     * Returns a script that may later be executed.     * Will consume all the source in the reader.     *     * @param in the input reader     * @param sourceName a string describing the source, such as a filename     * @param lineno the starting line number for reporting errors     * @param securityDomain an arbitrary object that specifies security     *        information about the origin or owner of the script. For     *        implementations that don't care about security, this value     *        may be null.     * @return a script that may later be executed     * @exception IOException if an IOException was generated by the Reader     * @see org.mozilla.javascript.Script     */    public final Script compileReader(Reader in, String sourceName,                                      int lineno, Object securityDomain)        throws IOException    {        if (lineno < 0) {            // For compatibility IllegalArgumentException can not be thrown here            lineno = 0;        }        return (Script) compileImpl(null, in, null, sourceName, lineno,                                    securityDomain, false, null, null);    }    /**     * Compiles the source in the given string.     * <p>     * Returns a script that may later be executed.     *     * @param source the source string     * @param sourceName a string describing the source, such as a filename     * @param lineno the starting line number for reporting errors     * @param securityDomain an arbitrary object that specifies security     *        information about the origin or owner of the script. For     *        implementations that don't care about security, this value     *        may be null.     * @return a script that may later be executed     * @see org.mozilla.javascript.Script     */    public final Script compileString(String source,                                      String sourceName, int lineno,                                      Object securityDomain)    {        if (lineno < 0) {            // For compatibility IllegalArgumentException can not be thrown here            lineno = 0;        }        return compileString(source, null, null, sourceName, lineno,                             securityDomain);    }    final Script compileString(String source,                               Interpreter compiler,                               ErrorReporter compilationErrorReporter,                               String sourceName, int lineno,                               Object securityDomain)    {        try {            return (Script) compileImpl(null, null, source, sourceName, lineno,                                        securityDomain, false,                                        compiler, compilationErrorReporter);        } catch (IOException ex) {            // Should not happen when dealing with source as string            throw new RuntimeException();        }    }    /**     * Compile a JavaScript function.     * <p>     * The function source must be a function definition as defined by     * ECMA (e.g., "function f(a) { return a; }").     *     * @param scope the scope to compile relative to     * @param source the function definition source     * @param sourceName a string describing the source, such as a filename     * @param lineno the starting line number     * @param securityDomain an arbitrary object that specifies security     *        information about the origin or owner of the script. For     *        implementations that don't care about security, this value     *        may be null.     * @return a Function that may later be called     * @see org.mozilla.javascript.Function     */    public final Function compileFunction(Scriptable scope, String source,                                          String sourceName, int lineno,                                          Object securityDomain)    {        return compileFunction(scope, source, null, null, sourceName, lineno,                               securityDomain);    }    final Function compileFunction(Scriptable scope, String source,                                   Interpreter compiler,                                   ErrorReporter compilationErrorReporter,                                   String sourceName, int lineno,                                   Object securityDomain)    {        try {            return (Function) compileImpl(scope, null, source, sourceName,                                          lineno, securityDomain, true,                                          compiler, compilationErrorReporter);        }        catch (IOException ioe) {            // Should never happen because we just made the reader            // from a String            throw new RuntimeException();        }    }    /**     * Decompile the script.     * <p>     * The canonical source of the script is returned.     *     * @param script the script to decompile     * @param indent the number of spaces to indent the result     * @return a string representing the script source     */    public final String decompileScript(Script script, int indent)    {        NativeFunction scriptImpl = (NativeFunction) script;        return scriptImpl.decompile(indent, 0);    }    /**     * Decompile a JavaScript Function.     * <p>     * Decompiles a previously compiled JavaScript function object to     * canonical source.     * <p>     * Returns function body of '[native code]' if no decompilation     * information is available.     *     * @param fun the JavaScript function to decompile     * @param indent the number of spaces to indent the result     * @return a string representing the function source     */    public final String decompileFunction(Function fun, int indent)    {        if (fun instanceof BaseFunction)            return ((BaseFunction)fun).decompile(indent, 0);        else            return "function " + fun.getClassName() +                   "() {\n\t[native code]\n}\n";    }    /**     * Decompile the body of a JavaScript Function.     * <p>     * Decompiles the body a previously compiled JavaScript Function     * object to canonical source, omitting the function header and     * trailing brace.     *     * Returns '[native code]' if no decompilation information is available.     *     * @param fun the JavaScript function to decompile     * @param indent the number of spaces to indent the result     * @return a string representing the function body source.     */    public final String decompileFunctionBody(Function fun, int indent)    {        if (fun instanceof BaseFunction) {            BaseFunction bf = (BaseFunction)fun;            return bf.decompile(indent, Decompiler.ONLY_BODY_FLAG);        }        // ALERT: not sure what the right response here is.        return "[native code]\n";    }    /**     * Create a new JavaScript object.     *     * Equivalent to evaluating "new Object()".     * @param scope the scope to search for the constructor and to evaluate     *              against     * @return the new object     */    public final Scriptable newObject(Scriptable scope)    {        return newObject(scope, "Object", ScriptRuntime.emptyArgs);    }    /**     * Create a new JavaScript object by executing the named constructor.     *     * The call <code>newObject(scope, "Foo")</code> is equivalent to     * evaluating "new Foo()".     *     * @param scope the scope to search for the constructor and to evaluate against     * @param constructorName the name of the constructor to call     * @return the new object     */    public final Scriptable newObject(Scriptable scope, String constructorName)    {        return newObject(scope, constructorName, ScriptRuntime.emptyArgs);    }    /**     * Creates a new JavaScript object by executing the named constructor.     *     * Searches <code>scope</code> for the named constructor, calls it with     * the given arguments, and returns the result.<p>     *     * The code     * <pre>     * Object[] args = { "a", "b" };     * newObject(scope, "Foo", args)</pre>     * is equivalent to evaluating "new Foo('a', 'b')", assuming that the Foo     * constructor has been defined in <code>scope</code>.     *     * @param scope The scope to search for the constructor and to evaluate     *              against     * @param constructorName the name of the constructor to call     * @param args the array of arguments for the constructor     * @return the new object     */    public final Scriptable newObject(Scriptable scope, String constructorName,                                      Object[] args)    {        scope = ScriptableObject.getTopLevelScope(scope);        Function ctor = ScriptRuntime.getExistingCtor(this, scope,                                                      constructorName);        if (args == null) { args = ScriptRuntime.emptyArgs; }        return ctor.construct(this, scope, args);    }    /**     * Create an array with a specified initial length.     * <p>     * @param scope the scope to create the object in     * @param length the initial length (JavaScript arrays may have     *               additional properties added dynamically).     * @return the new array object     */    public final Scriptable newArray(Scriptable scope, int length)    {        NativeArray result = new NativeArray(length);        ScriptRuntime.setObjectProtoAndParent(result, scope);        return result;    }    /**     * Create an array with a set of initial elements.     *     * @param scope the scope to create the object in.     * @param elements the initial elements. Each object in this array     *                 must be an acceptable JavaScript type and type     *                 of array should be exactly Object[], not     *                 SomeObjectSubclass[].     * @return the new array object.     */    public final Scriptable newArray(Scriptable scope, Object[] elements)    {        if (elements.getClass().getComponentType() != ScriptRuntime.ObjectClass)            throw new IllegalArgumentException();        NativeArray result = new NativeArray(elements);        ScriptRuntime.setObjectProtoAndParent(result, scope);        return result;    }    /**     * Get the elements of a JavaScript array.     * <p>     * If the object defines a length property convertible to double number,     * then the number is converted Uint32 value as defined in Ecma 9.6     * and Java array of that size is allocated.     * The array is initialized with the values obtained by     * calling get() on object for each value of i in [0,length-1]. If     * there is not a defined value for a property the Undefined value     * is used to initialize the corresponding element in the array. The     * Java array is then returned.     * If the object doesn't define a length property or it is not a number,     * empty array is returned.     * @param object the JavaScript array or array-like object     * @return a Java array of objects     * @since 1.4 release 2     */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -