static jsdouble FASTCALL math_min_tn(jsdouble d, jsdouble p) { if (JSDOUBLE_IS_NaN(d) || JSDOUBLE_IS_NaN(p)) return js_NaN; if (p == 0 && p == d) { // Min prefers -0.0 to 0.0. if (js_copysign (1.0, p) == -1) return p; return d; } return (p < d) ? p : d; }
static jsdouble FASTCALL math_max_tn(jsdouble d, jsdouble p) { if (JSDOUBLE_IS_NaN(d) || JSDOUBLE_IS_NaN(p)) return js_NaN; if (p == 0 && p == d) { // Max prefers 0.0 to -0.0. if (js_copysign(1.0, d) == -1) return p; return d; } return (p > d) ? p : d; }
static JSBool math_exp(JSContext *cx, uintN argc, jsval *vp) { jsdouble x, z; if (argc == 0) { *vp = cx->runtime->NaNValue; return JS_TRUE; } x = js_ValueToNumber(cx, &vp[2]); if (JSVAL_IS_NULL(vp[2])) return JS_FALSE; #ifdef _WIN32 if (!JSDOUBLE_IS_NaN(x)) { if (x == js_PositiveInfinity) { *vp = cx->runtime->positiveInfinityValue; return JS_TRUE; } if (x == js_NegativeInfinity) { *vp = JSVAL_ZERO; return JS_TRUE; } } #endif z = exp(x); return js_NewNumberInRootedValue(cx, z, vp); }
JSBool js_math_min(JSContext *cx, uintN argc, jsval *vp) { jsdouble x, z = js_PositiveInfinity; jsval *argv; uintN i; if (argc == 0) { *vp = cx->runtime->positiveInfinityValue; return JS_TRUE; } argv = vp + 2; for (i = 0; i < argc; i++) { x = js_ValueToNumber(cx, &argv[i]); if (JSVAL_IS_NULL(argv[i])) return JS_FALSE; if (JSDOUBLE_IS_NaN(x)) { *vp = cx->runtime->NaNValue; return JS_TRUE; } if (x == 0 && x == z) { if (js_copysign(1.0, x) == -1) z = x; } else { z = (x < z) ? x : z; } } return js_NewNumberInRootedValue(cx, z, vp); }
static JSBool ValueIsLength(JSContext *cx, jsval v, jsuint *lengthp) { jsint i; jsdouble d; if (JSVAL_IS_INT(v)) { i = JSVAL_TO_INT(v); if (i < 0) { JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BAD_ARRAY_LENGTH); return JS_FALSE; } *lengthp = (jsuint) i; return JS_TRUE; } if (!js_ValueToNumber(cx, v, &d)) { JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BAD_ARRAY_LENGTH); return JS_FALSE; } if (!js_DoubleToECMAUint32(cx, d, (uint32 *)lengthp)) { JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BAD_ARRAY_LENGTH); return JS_FALSE; } if (JSDOUBLE_IS_NaN(d) || d != *lengthp) { JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BAD_ARRAY_LENGTH); return JS_FALSE; } return JS_TRUE; }
static JSBool math_min(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { jsdouble x, z = *cx->runtime->jsPositiveInfinity; uintN i; if (argc == 0) { *rval = DOUBLE_TO_JSVAL(cx->runtime->jsPositiveInfinity); return JS_TRUE; } for (i = 0; i < argc; i++) { if (!js_ValueToNumber(cx, argv[i], &x)) return JS_FALSE; if (JSDOUBLE_IS_NaN(x)) { *rval = DOUBLE_TO_JSVAL(cx->runtime->jsNaN); return JS_TRUE; } if ((x == 0) && (x == z) && (fd_copysign(1.0, x) == -1)) z = x; else z = (x < z) ? x : z; } return js_NewNumberValue(cx, z, rval); }
static JSBool math_exp(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { jsdouble x, z; if (!js_ValueToNumber(cx, argv[0], &x)) return JS_FALSE; #ifdef _WIN32 if (!JSDOUBLE_IS_NaN(x)) { if (x == *cx->runtime->jsPositiveInfinity) { *rval = DOUBLE_TO_JSVAL(cx->runtime->jsPositiveInfinity); return JS_TRUE; } if (x == *cx->runtime->jsNegativeInfinity) { *rval = JSVAL_ZERO; return JS_TRUE; } } #endif z = fd_exp(x); return js_NewNumberValue(cx, z, rval); }
JSBool js_ValueToBoolean(JSContext *cx, jsval v, JSBool *bp) { JSBool b; jsdouble d; #if defined XP_PC && defined _MSC_VER && _MSC_VER <= 800 /* MSVC1.5 coredumps */ if (!bp) return JS_TRUE; /* This should be an if-else chain, but MSVC1.5 crashes if it is. */ #define ELSE #else #define ELSE else #endif if (JSVAL_IS_NULL(v) || JSVAL_IS_VOID(v)) { /* Must return early to avoid falling thru to JSVAL_IS_OBJECT case. */ *bp = JS_FALSE; return JS_TRUE; } if (JSVAL_IS_OBJECT(v)) { if (!JSVERSION_IS_ECMA(cx->version)) { if (!OBJ_DEFAULT_VALUE(cx, JSVAL_TO_OBJECT(v), JSTYPE_BOOLEAN, &v)) return JS_FALSE; if (!JSVAL_IS_BOOLEAN(v)) v = JSVAL_TRUE; /* non-null object is true */ b = JSVAL_TO_BOOLEAN(v); } else { b = JS_TRUE; } } ELSE if (JSVAL_IS_STRING(v)) { b = JSSTRING_LENGTH(JSVAL_TO_STRING(v)) ? JS_TRUE : JS_FALSE; } ELSE if (JSVAL_IS_INT(v)) { b = JSVAL_TO_INT(v) ? JS_TRUE : JS_FALSE; } ELSE if (JSVAL_IS_DOUBLE(v)) { d = *JSVAL_TO_DOUBLE(v); b = (!JSDOUBLE_IS_NaN(d) && d != 0) ? JS_TRUE : JS_FALSE; } ELSE #if defined XP_PC && defined _MSC_VER && _MSC_VER <= 800 if (JSVAL_IS_BOOLEAN(v)) { b = JSVAL_TO_BOOLEAN(v); } #else { JS_ASSERT(JSVAL_IS_BOOLEAN(v)); b = JSVAL_TO_BOOLEAN(v); } #endif #undef ELSE *bp = b; return JS_TRUE; }
static jsdouble FASTCALL math_exp_tn(JSContext *cx, jsdouble d) { if (!JSDOUBLE_IS_NaN(d)) { if (d == js_PositiveInfinity) return js_PositiveInfinity; if (d == js_NegativeInfinity) return 0.0; } return exp(d); }
js_compare_atom_keys(const void *k1, const void *k2) { jsval v1, v2; v1 = (jsval)k1, v2 = (jsval)k2; if (JSVAL_IS_STRING(v1) && JSVAL_IS_STRING(v2)) return !js_CompareStrings(JSVAL_TO_STRING(v1), JSVAL_TO_STRING(v2)); if (JSVAL_IS_DOUBLE(v1) && JSVAL_IS_DOUBLE(v2)) { double d1 = *JSVAL_TO_DOUBLE(v1); double d2 = *JSVAL_TO_DOUBLE(v2); if (JSDOUBLE_IS_NaN(d1)) return JSDOUBLE_IS_NaN(d2); #ifdef XP_PC /* XXX MSVC miscompiles such that (NaN == 0) */ if (JSDOUBLE_IS_NaN(d2)) return JS_FALSE; #endif return d1 == d2; } return v1 == v2; }
static jsdouble FASTCALL math_exp_tn(JSContext *cx, jsdouble d) { if (!JSDOUBLE_IS_NaN(d)) { if (d == *cx->runtime->jsPositiveInfinity) { return *cx->runtime->jsPositiveInfinity; } if (d == *cx->runtime->jsNegativeInfinity) { return 0.0; } } return exp(d); }
static JSBool MatchDouble(JSDHashTable *table, const JSDHashEntryHdr *hdr, const void *key) { JSAtomHashEntry *entry = TO_ATOM_ENTRY(hdr); jsdouble d1, d2; JS_ASSERT(IS_DOUBLE_TABLE(table)); if (entry->keyAndFlags == 0) { /* See comments in MatchString. */ return JS_FALSE; } d1 = *(jsdouble *)ATOM_ENTRY_KEY(entry); d2 = *(jsdouble *)key; if (JSDOUBLE_IS_NaN(d1)) return JSDOUBLE_IS_NaN(d2); #if defined(XP_WIN) /* XXX MSVC miscompiles such that (NaN == 0) */ if (JSDOUBLE_IS_NaN(d2)) return JS_FALSE; #endif return d1 == d2; }
static int sort_compare(const void *a, const void *b, void *arg) { jsval av = *(const jsval *)a, bv = *(const jsval *)b; CompareArgs *ca = (CompareArgs *) arg; JSContext *cx = ca->context; jsdouble cmp = -1; jsval fval, argv[2], rval; JSBool ok; fval = ca->fval; if (fval == JSVAL_NULL) { JSString *astr, *bstr; if (av == bv) { cmp = 0; } else if (av == JSVAL_VOID || bv == JSVAL_VOID) { /* Put undefined properties at the end. */ cmp = (av == JSVAL_VOID) ? 1 : -1; } else if ((astr = js_ValueToString(cx, av)) != NULL && (bstr = js_ValueToString(cx, bv)) != NULL) { cmp = js_CompareStrings(astr, bstr); } else { ca->status = JS_FALSE; } } else { argv[0] = av; argv[1] = bv; ok = js_InternalCall(cx, OBJ_GET_PARENT(cx, JSVAL_TO_OBJECT(fval)), fval, 2, argv, &rval); if (ok) { ok = js_ValueToNumber(cx, rval, &cmp); /* Clamp cmp to -1, 0, 1. */ if (JSDOUBLE_IS_NaN(cmp)) { /* XXX report some kind of error here? ECMA talks about * 'consistent compare functions' that don't return NaN, but is * silent about what the result should be. So we currently * ignore it. */ cmp = 0; } else if (cmp != 0) { cmp = cmp > 0 ? 1 : -1; } } else { ca->status = ok; } } return (int)cmp; }
JSBool js_ValueToBoolean(JSContext *cx, jsval v, JSBool *bp) { JSBool b; jsdouble d; if (JSVAL_IS_NULL(v) || JSVAL_IS_VOID(v)) { b = JS_FALSE; } else if (JSVAL_IS_OBJECT(v)) { if (!JS_VERSION_IS_ECMA(cx)) { if (!OBJ_DEFAULT_VALUE(cx, JSVAL_TO_OBJECT(v), JSTYPE_BOOLEAN, &v)) return JS_FALSE; if (!JSVAL_IS_BOOLEAN(v)) v = JSVAL_TRUE; /* non-null object is true */ b = JSVAL_TO_BOOLEAN(v); } else { b = JS_TRUE; } } else if (JSVAL_IS_STRING(v)) { b = JSSTRING_LENGTH(JSVAL_TO_STRING(v)) ? JS_TRUE : JS_FALSE; } else if (JSVAL_IS_INT(v)) { b = JSVAL_TO_INT(v) ? JS_TRUE : JS_FALSE; } else if (JSVAL_IS_DOUBLE(v)) { d = *JSVAL_TO_DOUBLE(v); b = (!JSDOUBLE_IS_NaN(d) && d != 0) ? JS_TRUE : JS_FALSE; } else { JS_ASSERT(JSVAL_IS_BOOLEAN(v)); b = JSVAL_TO_BOOLEAN(v); } *bp = b; return JS_TRUE; }
JSBool JSSphere::Constructor(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { DOC_BEGIN("Constructs a sphere from center/radius, center/point on surface, from another sphere or an empty one."); DOC_PARAM("a sphere", "", DOC_TYPE_SPHERE); DOC_RESET; DOC_PARAM("center", "", DOC_TYPE_POINT3F); DOC_PARAM("radius", "", DOC_TYPE_FLOAT); DOC_RESET; DOC_PARAM("center", "", DOC_TYPE_POINT3F); DOC_PARAM("point on surface", "", DOC_TYPE_POINT3F); DOC_END; IF_NOISY2(AC_TRACE << "Constructor argc =" << argc << endl); if (JSA_GetClass(cx,obj) != Class()) { JS_ReportError(cx,"Constructor for %s bad object; did you forget a 'new'?",ClassName()); return JS_FALSE; } JSSphere * myNewObject = 0; JSSphere::NativeValuePtr myNewValue = JSSphere::NativeValuePtr(new dom::SimpleValue<asl::Sphere<Number> >(0)); asl::Sphere<Number> & myNewSphere = myNewValue->openWriteableValue(); // we close it only on success, otherwise we trash it anyway if (argc == 0) { // construct empty myNewObject=new JSSphere(myNewValue); } else { if (argc == 2) { JSObject * myObject = JSVector<asl::Point3<Number> >::Construct(cx, argv[0]); if (!myObject) { JS_ReportError(cx,"JSSphere::Constructor: first argument must be a normal vector of size 3",ClassName()); return JS_FALSE; } jsdouble myNumber; if (JS_ValueToNumber(cx,argv[1],&myNumber) && !JSDOUBLE_IS_NaN(myNumber)) { // construct from center/radius myNewSphere = asl::Sphere<Number>(JSVector<asl::Point3<Number> >::getNativeRef(cx,myObject), Number(myNumber)); } else { // construct from center/point on surface JSObject * myObject2 = JSVector<asl::Point3<Number> >::Construct(cx, argv[1]); if (!myObject2) { JS_ReportError(cx,"JSSphere::Constructor: second argument must be a distance number or point on the plane of size 3",ClassName()); return JS_FALSE; } myNewSphere = asl::Sphere<Number>(JSVector<asl::Point3<Number> >::getNativeRef(cx,myObject), JSVector<asl::Point3<Number> >::getNativeRef(cx,myObject2)); } myNewObject=new JSSphere(myNewValue); } else if (argc == 1) { // construct from one Sphere if (JSVAL_IS_VOID(argv[0])) { JS_ReportError(cx,"JSSphere::Constructor: bad argument #1 (undefined)"); return JS_FALSE; } JSObject * myArgument; if (!JS_ValueToObject(cx, argv[0], &myArgument)) { JS_ReportError(cx,"JSSphere::Constructor: bad argument type, Sphere expected"); return JS_FALSE; } if (JSA_GetClass(cx,myArgument) == Class()) { myNewSphere = getJSWrapper(cx, myArgument).getNative(); myNewObject=new JSSphere(myNewValue); } } else { JS_ReportError(cx,"Constructor for %s: bad number of arguments: expected 0,1 or 2, got %d",ClassName(), argc); return JS_FALSE; } } if (myNewObject) { JS_SetPrivate(cx,obj,myNewObject); myNewValue->closeWriteableValue(); return JS_TRUE; } JS_ReportError(cx,"JSSphere::Constructor: bad parameters"); return JS_FALSE; }