📄 scriptruntime.java
字号:
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; // Wrapped Java objects won't have "toSource" and will report // errors for get()s of nonexistent name, so use has() first if (ScriptableObject.hasProperty(obj, "toSource")) { 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("("); } 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++) { Object id = ids[i]; Object value; if (id instanceof Integer) { int intId = ((Integer)id).intValue(); value = thisObj.get(intId, thisObj); if (value == Scriptable.NOT_FOUND) continue; // a property has been removed if (i > 0) result.append(", "); result.append(intId); } else { String strId = (String)id; value = thisObj.get(strId, thisObj); if (value == Scriptable.NOT_FOUND) continue; // a property has been removed if (i > 0) result.append(", "); 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) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -