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

📄 scriptruntime.java

📁 java中比较著名的js引擎当属mozilla开源的rhino
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                        if ('0' <= digit && digit <= '9')                            digit -= '0';                        else if ('a' <= digit && digit <= 'z')                            digit -= 'a' - 10;                        else                            digit -= 'A' - 10;                        bitShiftInChar = radix;                    }                    bitShiftInChar >>= 1;                    boolean bit = (digit & bitShiftInChar) != 0;                    switch (state) {                      case SKIP_LEADING_ZEROS:                          if (bit) {                            --exactBitsLimit;                            sum = 1.0;                            state = FIRST_EXACT_53_BITS;                        }                        break;                      case FIRST_EXACT_53_BITS:                           sum *= 2.0;                        if (bit)                            sum += 1.0;                        --exactBitsLimit;                        if (exactBitsLimit == 0) {                            bit53 = bit;                            state = AFTER_BIT_53;                        }                        break;                      case AFTER_BIT_53:                        bit54 = bit;                        factor = 2.0;                        state = ZEROS_AFTER_54;                        break;                      case ZEROS_AFTER_54:                        if (bit) {                            state = MIXED_AFTER_54;                        }                        // fallthrough                      case MIXED_AFTER_54:                        factor *= 2;                        break;                    }                }                switch (state) {                  case SKIP_LEADING_ZEROS:                    sum = 0.0;                    break;                  case FIRST_EXACT_53_BITS:                  case AFTER_BIT_53:                    // do nothing                    break;                  case ZEROS_AFTER_54:                    // x1.1 -> x1 + 1 (round up)                    // x0.1 -> x0 (round down)                    if (bit54 & bit53)                        sum += 1.0;                    sum *= factor;                    break;                  case MIXED_AFTER_54:                    // x.100...1.. -> x + 1 (round up)                    // x.0anything -> x (round down)                    if (bit54)                        sum += 1.0;                    sum *= factor;                    break;                }            }            /* We don't worry about inaccurate numbers for any other base. */        }        return sum;    }    /**     * ToNumber applied to the String type     *     * See ECMA 9.3.1     */    public static double toNumber(String s) {        int len = s.length();        int start = 0;        char startChar;        for (;;) {            if (start == len) {                // Empty or contains only whitespace                return +0.0;            }            startChar = s.charAt(start);            if (!Character.isWhitespace(startChar))                break;            start++;        }        if (startChar == '0') {            if (start + 2 < len) {                int c1 = s.charAt(start + 1);                if (c1 == 'x' || c1 == 'X') {                    // A hexadecimal number                    return stringToNumber(s, start + 2, 16);                }            }        } else if (startChar == '+' || startChar == '-') {            if (start + 3 < len && s.charAt(start + 1) == '0') {                int c2 = s.charAt(start + 2);                if (c2 == 'x' || c2 == 'X') {                    // A hexadecimal number with sign                    double val = stringToNumber(s, start + 3, 16);                    return startChar == '-' ? -val : val;                }            }        }        int end = len - 1;        char endChar;        while (Character.isWhitespace(endChar = s.charAt(end)))            end--;        if (endChar == 'y') {            // check for "Infinity"            if (startChar == '+' || startChar == '-')                start++;            if (start + 7 == end && s.regionMatches(start, "Infinity", 0, 8))                return startChar == '-'                    ? Double.NEGATIVE_INFINITY                    : Double.POSITIVE_INFINITY;            return NaN;        }        // A non-hexadecimal, non-infinity number:        // just try a normal floating point conversion        String sub = s.substring(start, end+1);        if (MSJVM_BUG_WORKAROUNDS) {            // The MS JVM will accept non-conformant strings            // rather than throwing a NumberFormatException            // as it should.            for (int i=sub.length()-1; i >= 0; i--) {                char c = sub.charAt(i);                if (('0' <= c && c <= '9') || c == '.' ||                    c == 'e' || c == 'E'  ||                    c == '+' || c == '-')                    continue;                return NaN;            }        }        try {            return Double.valueOf(sub).doubleValue();        } catch (NumberFormatException ex) {            return NaN;        }    }    /**     * Helper function for builtin objects that use the varargs form.     * ECMA function formal arguments are undefined if not supplied;     * this function pads the argument array out to the expected     * length, if necessary.     */    public static Object[] padArguments(Object[] args, int count) {        if (count < args.length)            return args;        int i;        Object[] result = new Object[count];        for (i = 0; i < args.length; i++) {            result[i] = args[i];        }        for (; i < count; i++) {            result[i] = Undefined.instance;        }        return result;    }    /* Work around Microsoft Java VM bugs. */    private final static boolean MSJVM_BUG_WORKAROUNDS = true;    public static String escapeString(String s)    {        return escapeString(s, '"');    }    /**     * For escaping strings printed by object and array literals; not quite     * the same as 'escape.'     */    public static String escapeString(String s, char escapeQuote)    {        if (!(escapeQuote == '"' || escapeQuote == '\'')) Kit.codeBug();        StringBuffer sb = null;        for(int i = 0, L = s.length(); i != L; ++i) {            int c = s.charAt(i);            if (' ' <= c && c <= '~' && c != escapeQuote && c != '\\') {                // an ordinary print character (like C isprint()) and not "                // or \ .                if (sb != null) {                    sb.append((char)c);                }                continue;            }            if (sb == null) {                sb = new StringBuffer(L + 3);                sb.append(s);                sb.setLength(i);            }            int escape = -1;            switch (c) {                case '\b':  escape = 'b';  break;                case '\f':  escape = 'f';  break;                case '\n':  escape = 'n';  break;                case '\r':  escape = 'r';  break;                case '\t':  escape = 't';  break;                case 0xb:   escape = 'v';  break; // Java lacks \v.                case ' ':   escape = ' ';  break;                case '\\':  escape = '\\'; break;            }            if (escape >= 0) {                // an \escaped sort of character                sb.append('\\');                sb.append((char)escape);            } else if (c == escapeQuote) {                sb.append('\\');                sb.append(escapeQuote);            } else {                int hexSize;                if (c < 256) {                    // 2-digit hex                    sb.append("\\x");                    hexSize = 2;                } else {                    // Unicode.                    sb.append("\\u");                    hexSize = 4;                }                // append hexadecimal form of c left-padded with 0                for (int shift = (hexSize - 1) * 4; shift >= 0; shift -= 4) {                    int digit = 0xf & (c >> shift);                    int hc = (digit < 10) ? '0' + digit : 'a' - 10 + digit;                    sb.append((char)hc);                }            }        }        return (sb == null) ? s : sb.toString();    }    static boolean isValidIdentifierName(String s)    {        int L = s.length();        if (L == 0)            return false;        if (!Character.isJavaIdentifierStart(s.charAt(0)))            return false;        for (int i = 1; i != L; ++i) {            if (!Character.isJavaIdentifierPart(s.charAt(i)))                return false;        }        return !TokenStream.isKeyword(s);    }    /**     * Convert the value to a string.     *     * See ECMA 9.8.     */    public static String toString(Object val) {        for (;;) {            if (val == null) {                return "null";            }            if (val == Undefined.instance) {                return "undefined";            }            if (val instanceof String) {                return (String)val;            }            if (val instanceof Number) {                // XXX should we just teach NativeNumber.stringValue()                // about Numbers?                return numberToString(((Number)val).doubleValue(), 10);            }            if (val instanceof Scriptable) {                val = ((Scriptable) val).getDefaultValue(StringClass);                if (val instanceof Scriptable) {                    throw errorWithClassName("msg.primitive.expected", val);                }                continue;            }            return val.toString();        }    }    static String defaultObjectToString(Scriptable obj)    {        return "[object " + obj.getClassName() + ']';    }    public static String toString(Object[] args, int index)    {        return (index < args.length) ? toString(args[index]) : "undefined";    }    /**     * Optimized version of toString(Object) for numbers.     */    public static String toString(double val) {        return numberToString(val, 10);    }    public static String numberToString(double d, int base) {        if (d != d)            return "NaN";        if (d == Double.POSITIVE_INFINITY)            return "Infinity";        if (d == Double.NEGATIVE_INFINITY)            return "-Infinity";        if (d == 0.0)            return "0";        if ((base < 2) || (base > 36)) {            throw Context.reportRuntimeError1(                "msg.bad.radix", Integer.toString(base));        }        if (base != 10) {            return DToA.JS_dtobasestr(base, d);        } else {            StringBuffer result = new StringBuffer();            DToA.JS_dtostr(result, DToA.DTOSTR_STANDARD, 0, d);            return result.toString();        }    }    static String uneval(Context cx, Scriptable scope, Object value)    {        if (value == null) {            return "null";        }        if (value == Undefined.instance) {            return "undefined";        }        if (value instanceof String) {            String escaped = escapeString((String)value);            StringBuffer sb = new StringBuffer(escaped.length() + 2);            sb.append('\"');            sb.append(escaped);            sb.append('\"');            return sb.toString();        }        if (value instanceof Number) {            double d = ((Number)value).doubleValue();            if (d == 0 && 1 / d < 0) {                return "-0";            }            return toString(d);        }        if (value instanceof Boolean) {            return toString(value);        }        if (value instanceof Scriptable) {            Scriptable obj = (Scriptable)value;            Object v = ScriptableObject.getProperty(obj, "toSource");            if (v instanceof Function) {                Function f = (Function)v;                return toString(f.call(cx, scope, obj, emptyArgs));            }            return toString(value);        }        warnAboutNonJSObject(value);        return value.toString();    }    static String defaultObjectToSource(Context cx, Scriptable scope,                                        Scriptable thisObj, Object[] args)    {        boolean toplevel, iterating;        if (cx.iterating == null) {            toplevel = true;            iterating = false;            cx.iterating = new ObjToIntMap(31);        } else {            toplevel = false;            iterating = cx.iterating.has(thisObj);        }        StringBuffer result = new StringBuffer(128);        if (toplevel) {            result.append("(");        }

⌨️ 快捷键说明

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