private static double stack_double(CallFrame frame, int i) { Object x = frame.stack[i]; if (x != UniqueTag.DOUBLE_MARK) { return ScriptRuntime.toNumber(x); } else { return frame.sDbl[i]; } }
public static double toNumber(Object[] args, int index) { return (index < args.length) ? toNumber(args[index]) : NaN; }
/** * Convert the value to a JavaScript Number value. * <p> * Returns a Java double for the JavaScript Number. * <p> * See ECMA 9.3. * * @param value a JavaScript value * @return the corresponding double value converted using * the ECMA rules */ public static double toNumber(Object value) { return ScriptRuntime.toNumber(value); }
private static int stack_int32(CallFrame frame, int i) { Object x = frame.stack[i]; double value; if (x == UniqueTag.DOUBLE_MARK) { value = frame.sDbl[i]; } else { value = ScriptRuntime.toNumber(x); } return ScriptRuntime.toInt32(value); }
/** * * 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)); }
private static int js_lastIndexOf(String target, Object[] args) { String search = ScriptRuntime.toString(args, 0); double end = ScriptRuntime.toNumber(args, 1); if (end != end || end > target.length()) end = target.length(); else if (end < 0) end = 0; return target.lastIndexOf(search, (int)end); }
@Override protected void setInstanceIdValue(int id, Object value) { if (id == Id_lastIndex) { lastIndex = ScriptRuntime.toNumber(value); return; } super.setInstanceIdValue(id, value); }
/** * * See ECMA 9.4. */ public static double toInteger(Object val) { return toInteger(toNumber(val)); }
public static long toUint32(Object val) { return toUint32(toNumber(val)); }
private static long toArrayIndex(String id) { double d = ScriptRuntime.toNumber(id); if (d == d) { long index = ScriptRuntime.toUint32(d); if (index == d && index != 4294967295L) { // Assume that ScriptRuntime.toString(index) is the same // as java.lang.Long.toString(index) for long if (Long.toString(index).equals(id)) { return index; } } } return -1; }
public static boolean cmp_LE(Object val1, Object val2) { double d1, d2; if (val1 instanceof Number && val2 instanceof Number) { d1 = ((Number)val1).doubleValue(); d2 = ((Number)val2).doubleValue(); } else { if (val1 instanceof Scriptable) val1 = ((Scriptable) val1).getDefaultValue(NumberClass); if (val2 instanceof Scriptable) val2 = ((Scriptable) val2).getDefaultValue(NumberClass); if (val1 instanceof String && val2 instanceof String) { return ((String)val1).compareTo((String)val2) <= 0; } d1 = toNumber(val1); d2 = toNumber(val2); } return d1 <= d2; }
/** * * 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; }
public static boolean cmp_LT(Object val1, Object val2) { double d1, d2; if (val1 instanceof Number && val2 instanceof Number) { d1 = ((Number)val1).doubleValue(); d2 = ((Number)val2).doubleValue(); } else { if (val1 instanceof Scriptable) val1 = ((Scriptable) val1).getDefaultValue(NumberClass); if (val2 instanceof Scriptable) val2 = ((Scriptable) val2).getDefaultValue(NumberClass); if (val1 instanceof String && val2 instanceof String) { return ((String)val1).compareTo((String)val2) < 0; } d1 = toNumber(val1); d2 = toNumber(val2); } return d1 < d2; }
public int compare(final Object x, final Object y) { // sort undefined to end if (x == y) { return 0; } else if (y == Undefined.instance || y == Scriptable.NOT_FOUND) { return -1; } else if (x == Undefined.instance || x == Scriptable.NOT_FOUND) { return 1; } cmpBuf[0] = x; cmpBuf[1] = y; Object ret = jsCompareFunction.call(cx, scope, funThis, cmpBuf); final double d = ScriptRuntime.toNumber(ret); if (d < 0) { return -1; } else if (d > 0) { return +1; } return 0; // ??? double and 0??? } };
private static boolean eqString(String x, Object y) { for (;;) { if (y == null || y == Undefined.instance) { return false; } else if (y instanceof String) { return x.equals(y); } else if (y instanceof Number) { return toNumber(x) == ((Number)y).doubleValue(); } else if (y instanceof Boolean) { return toNumber(x) == (((Boolean)y).booleanValue() ? 1.0 : 0.0); } else if (y instanceof Scriptable) { if (y instanceof ScriptableObject) { Object test = ((ScriptableObject)y).equivalentValues(x); if (test != Scriptable.NOT_FOUND) { return ((Boolean)test).booleanValue(); } } y = toPrimitive(y); continue; } else { warnAboutNonJSObject(y); return false; } } } public static boolean shallowEq(Object x, Object y)
private static double date_msecFromArgs(Object[] args) { double array[] = new double[MAXARGS]; int loop; double d; for (loop = 0; loop < MAXARGS; loop++) { if (loop < args.length) { d = ScriptRuntime.toNumber(args[loop]); if (d != d || Double.isInfinite(d)) { return ScriptRuntime.NaN; } array[loop] = ScriptRuntime.toInteger(args[loop]); } else { if (loop == 2) { array[loop] = 1; /* Default the date argument to 1. */ } else { array[loop] = 0; } } } /* adjust 2-digit years into the 20th century */ if (array[0] >= 0 && array[0] <= 99) array[0] += 1900; return date_msecFromDate(array[0], array[1], array[2], array[3], array[4], array[5], array[6]); }
/** * Convert the value to a number. * * See ECMA 9.3. */ public static double toNumber(Object val) { for (;;) { if (val instanceof Number) return ((Number) val).doubleValue(); if (val == null) return +0.0; if (val == Undefined.instance) return NaN; if (val instanceof String) return toNumber((String) val); if (val instanceof Boolean) return ((Boolean) val).booleanValue() ? 1 : +0.0; if (val instanceof Scriptable) { val = ((Scriptable) val).getDefaultValue(NumberClass); if (val instanceof Scriptable) throw errorWithClassName("msg.primitive.expected", val); continue; } warnAboutNonJSObject(val); return NaN; } }
static boolean eqNumber(double x, Object y) { for (;;) { if (y == null || y == Undefined.instance) { return false; } else if (y instanceof Number) { return x == ((Number)y).doubleValue(); } else if (y instanceof String) { return x == toNumber(y); } else if (y instanceof Boolean) { return x == (((Boolean)y).booleanValue() ? 1.0 : +0.0); } else if (y instanceof Scriptable) { if (y instanceof ScriptableObject) { Object xval = wrapNumber(x); Object test = ((ScriptableObject)y).equivalentValues(xval); if (test != Scriptable.NOT_FOUND) { return ((Boolean)test).booleanValue(); } } y = toPrimitive(y); } else { warnAboutNonJSObject(y); return false; } } }
public static Object refIncrDecr(Ref ref, Context cx, int incrDecrMask) { Object value = ref.get(cx); boolean post = ((incrDecrMask & Node.POST_FLAG) != 0); double number; if (value instanceof Number) { number = ((Number)value).doubleValue(); } else { number = toNumber(value); if (post) { // convert result to number value = wrapNumber(number); } } if ((incrDecrMask & Node.DECR_FLAG) == 0) { ++number; } else { --number; } Number result = wrapNumber(number); ref.set(cx, result); if (post) { return value; } else { return result; } }
public static Object elemIncrDecr(Object obj, Object index, Context cx, int incrDecrMask) { Object value = getObjectElem(obj, index, cx); boolean post = ((incrDecrMask & Node.POST_FLAG) != 0); double number; if (value instanceof Number) { number = ((Number)value).doubleValue(); } else { number = toNumber(value); if (post) { // convert result to number value = wrapNumber(number); } } if ((incrDecrMask & Node.DECR_FLAG) == 0) { ++number; } else { --number; } Number result = wrapNumber(number); setObjectElem(obj, index, result, cx); if (post) { return value; } else { return result; } }