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

📄 scriptruntime.java

📁 java中比较著名的js引擎当属mozilla开源的rhino
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        result.append('{');        // Make sure cx.iterating is set to null when done        // so we don't leak memory        try {            if (!iterating) {                cx.iterating.intern(thisObj); // stop recursion.                Object[] ids = thisObj.getIds();                for(int i=0; i < ids.length; i++) {                    if (i > 0)                        result.append(", ");                    Object id = ids[i];                    Object value;                    if (id instanceof Integer) {                        int intId = ((Integer)id).intValue();                        value = thisObj.get(intId, thisObj);                        result.append(intId);                    } else {                        String strId = (String)id;                        value = thisObj.get(strId, thisObj);                        if (ScriptRuntime.isValidIdentifierName(strId)) {                            result.append(strId);                        } else {                            result.append('\'');                            result.append(                                ScriptRuntime.escapeString(strId, '\''));                            result.append('\'');                        }                    }                    result.append(':');                    result.append(ScriptRuntime.uneval(cx, scope, value));                }            }        } finally {            if (toplevel) {                cx.iterating = null;            }        }        result.append('}');        if (toplevel) {            result.append(')');        }        return result.toString();    }    public static Scriptable toObject(Scriptable scope, Object val)    {        if (val instanceof Scriptable) {            return (Scriptable)val;        }        return toObject(Context.getContext(), scope, val);    }    public static Scriptable toObjectOrNull(Context cx, Object obj)    {        if (obj instanceof Scriptable) {            return (Scriptable)obj;        } else if (obj != null && obj != Undefined.instance) {            return toObject(cx, getTopCallScope(cx), obj);        }        return null;    }    /**     * @deprecated Use {@link #toObject(Scriptable, Object)} instead.     */    public static Scriptable toObject(Scriptable scope, Object val,                                      Class staticClass)    {        if (val instanceof Scriptable) {            return (Scriptable)val;        }        return toObject(Context.getContext(), scope, val);    }    /**     * Convert the value to an object.     *     * See ECMA 9.9.     */    public static Scriptable toObject(Context cx, Scriptable scope, Object val)    {        if (val instanceof Scriptable) {            return (Scriptable) val;        }        if (val == null) {            throw typeError0("msg.null.to.object");        }        if (val == Undefined.instance) {            throw typeError0("msg.undef.to.object");        }        String className = val instanceof String ? "String" :                           val instanceof Number ? "Number" :                           val instanceof Boolean ? "Boolean" :                           null;        if (className != null) {            Object[] args = { val };            scope = ScriptableObject.getTopLevelScope(scope);            return newObject(cx, scope, className, args);        }        // Extension: Wrap as a LiveConnect object.        Object wrapped = cx.getWrapFactory().wrap(cx, scope, val, null);        if (wrapped instanceof Scriptable)            return (Scriptable) wrapped;        throw errorWithClassName("msg.invalid.type", val);    }    /**     * @deprecated Use {@link #toObject(Context, Scriptable, Object)} instead.     */    public static Scriptable toObject(Context cx, Scriptable scope, Object val,                                      Class staticClass)    {        return toObject(cx, scope, val);    }    /**     * @deprecated The method is only present for compatibility.     */    public static Object call(Context cx, Object fun, Object thisArg,                              Object[] args, Scriptable scope)    {        if (!(fun instanceof Function)) {            throw notFunctionError(toString(fun));        }        Function function = (Function)fun;        Scriptable thisObj = toObjectOrNull(cx, thisArg);        if (thisObj == null) {            throw undefCallError(thisObj, "function");        }        return function.call(cx, scope, thisObj, args);    }    public static Scriptable newObject(Context cx, Scriptable scope,                                       String constructorName, Object[] args)    {        scope = ScriptableObject.getTopLevelScope(scope);        Function ctor = getExistingCtor(cx, scope, constructorName);        if (args == null) { args = ScriptRuntime.emptyArgs; }        return ctor.construct(cx, scope, args);    }    /**     *     * See ECMA 9.4.     */    public static double toInteger(Object val) {        return toInteger(toNumber(val));    }    // convenience method    public static double toInteger(double d) {        // if it's NaN        if (d != d)            return +0.0;        if (d == 0.0 ||            d == Double.POSITIVE_INFINITY ||            d == Double.NEGATIVE_INFINITY)            return d;        if (d > 0.0)            return Math.floor(d);        else            return Math.ceil(d);    }    public static double toInteger(Object[] args, int index) {        return (index < args.length) ? toInteger(args[index]) : +0.0;    }    /**     *     * See ECMA 9.5.     */    public static int toInt32(Object val)    {        // short circuit for common integer values        if (val instanceof Integer)            return ((Integer)val).intValue();        return toInt32(toNumber(val));    }    public static int toInt32(Object[] args, int index) {        return (index < args.length) ? toInt32(args[index]) : 0;    }    public static int toInt32(double d) {        int id = (int)d;        if (id == d) {            // This covers -0.0 as well            return id;        }        if (d != d            || d == Double.POSITIVE_INFINITY            || d == Double.NEGATIVE_INFINITY)        {            return 0;        }        d = (d >= 0) ? Math.floor(d) : Math.ceil(d);        double two32 = 4294967296.0;        d = Math.IEEEremainder(d, two32);        // (double)(long)d == d should hold here        long l = (long)d;        // returning (int)d does not work as d can be outside int range        // but the result must always be 32 lower bits of l        return (int)l;    }    /**     * See ECMA 9.6.     * @return long value representing 32 bits unsigned integer     */    public static long toUint32(double d) {        long l = (long)d;        if (l == d) {            // This covers -0.0 as well            return l & 0xffffffffL;        }        if (d != d            || d == Double.POSITIVE_INFINITY            || d == Double.NEGATIVE_INFINITY)        {            return 0;        }        d = (d >= 0) ? Math.floor(d) : Math.ceil(d);        // 0x100000000 gives me a numeric overflow...        double two32 = 4294967296.0;        l = (long)Math.IEEEremainder(d, two32);        return l & 0xffffffffL;    }    public static long toUint32(Object val) {        return toUint32(toNumber(val));    }    /**     *     * See ECMA 9.7.     */    public static char toUint16(Object val) {        double d = toNumber(val);        int i = (int)d;        if (i == d) {            return (char)i;        }        if (d != d            || d == Double.POSITIVE_INFINITY            || d == Double.NEGATIVE_INFINITY)        {            return 0;        }        d = (d >= 0) ? Math.floor(d) : Math.ceil(d);        int int16 = 0x10000;        i = (int)Math.IEEEremainder(d, int16);        return (char)i;    }    // XXX: this is until setDefaultNamespace will learn how to store NS    // properly and separates namespace form Scriptable.get etc.    private static final String DEFAULT_NS_TAG = "__default_namespace__";    public static Object setDefaultNamespace(Object namespace, Context cx)    {        Scriptable scope = cx.currentActivationCall;        if (scope == null) {            scope = getTopCallScope(cx);        }        XMLLib xmlLib = currentXMLLib(cx);        Object ns = xmlLib.toDefaultXmlNamespace(cx, namespace);        // XXX : this should be in separated namesapce from Scriptable.get/put        if (!scope.has(DEFAULT_NS_TAG, scope)) {            // XXX: this is racy of cause            ScriptableObject.defineProperty(scope, DEFAULT_NS_TAG, ns,                                            ScriptableObject.PERMANENT                                            | ScriptableObject.DONTENUM);        } else {            scope.put(DEFAULT_NS_TAG, scope, ns);        }        return Undefined.instance;    }    public static Object searchDefaultNamespace(Context cx)    {        Scriptable scope = cx.currentActivationCall;        if (scope == null) {            scope = getTopCallScope(cx);        }        Object nsObject;        for (;;) {            Scriptable parent = scope.getParentScope();            if (parent == null) {                nsObject = ScriptableObject.getProperty(scope, DEFAULT_NS_TAG);                if (nsObject == Scriptable.NOT_FOUND) {                    return null;                }                break;            }            nsObject = scope.get(DEFAULT_NS_TAG, scope);            if (nsObject != Scriptable.NOT_FOUND) {                break;            }            scope = parent;        }        return nsObject;    }    public static Object getTopLevelProp(Scriptable scope, String id) {        scope = ScriptableObject.getTopLevelScope(scope);        return ScriptableObject.getProperty(scope, id);    }    static Function getExistingCtor(Context cx, Scriptable scope,                                    String constructorName)    {        Object ctorVal = ScriptableObject.getProperty(scope, constructorName);        if (ctorVal instanceof Function) {            return (Function)ctorVal;        }        if (ctorVal == Scriptable.NOT_FOUND) {            throw Context.reportRuntimeError1(                "msg.ctor.not.found", constructorName);        } else {            throw Context.reportRuntimeError1(                "msg.not.ctor", constructorName);        }    }    /**     * Return -1L if str is not an index or the index value as lower 32     * bits of the result.     */    private static long indexFromString(String str)    {        // The length of the decimal string representation of        //  Integer.MAX_VALUE, 2147483647        final int MAX_VALUE_LENGTH = 10;        int len = str.length();        if (len > 0) {            int i = 0;            boolean negate = false;            int c = str.charAt(0);            if (c == '-') {                if (len > 1) {                    c = str.charAt(1);                    i = 1;                    negate = true;                }            }            c -= '0';            if (0 <= c && c <= 9                && len <= (negate ? MAX_VALUE_LENGTH + 1 : MAX_VALUE_LENGTH))            {                // Use negative numbers to accumulate index to handle                // Integer.MIN_VALUE that is greater by 1 in absolute value                // then Integer.MAX_VALUE                int index = -c;                int oldIndex = 0;                i++;                if (index != 0) {                    // Note that 00, 01, 000 etc. are not indexes                    while (i != len && 0 <= (c = str.charAt(i) - '0') && c <= 9)                    {                        oldIndex = index;                        index = 10 * index - c;                        i++;                    }                }                // Make sure all characters were consumed and that it couldn't                // have overflowed.                if (i == len &&

⌨️ 快捷键说明

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