trace Math.random

This commit is contained in:
shaver@mozilla.org 2008-07-30 15:51:44 -07:00
parent 70faa51284
commit b8c38e8b76
5 changed files with 34 additions and 10 deletions

View File

@ -54,3 +54,4 @@ BUILTIN3(ConcatStrings, LO, LO, LO, LO, JSString*, JSContext*, JSString*
BUILTIN3(String_getelem, LO, LO, LO, LO, JSString*, JSContext*, JSString*, jsint, 1, 1)
BUILTIN2(String_fromCharCode, LO, LO, LO, JSString*, JSContext*, jsint, 1, 1)
BUILTIN2(String_p_charCodeAt, LO, LO, LO, jsint, JSString*, jsint, 1, 1)
BUILTIN1(Math_random, LO, F, jsdouble, JSRuntime*, 1, 1)

View File

@ -49,6 +49,7 @@
#include "jscntxt.h"
#include "nanojit/avmplus.h"
#include "nanojit/nanojit.h"
#include "jsmath.h"
#include "jsstr.h"
#include "jstracer.h"
@ -221,6 +222,16 @@ builtin_String_p_charCodeAt(JSString* str, jsint i)
return JSSTRING_CHARS(str)[i];
}
jsdouble FASTCALL
builtin_Math_random(JSRuntime* rt)
{
JS_LOCK_RUNTIME(rt);
js_random_init(rt);
jsdouble z = js_random_nextDouble(rt);
JS_UNLOCK_RUNTIME(rt);
return z;
}
#define LO ARGSIZE_LO
#define F ARGSIZE_F
#define Q ARGSIZE_Q

View File

@ -379,8 +379,8 @@ random_setSeed(JSRuntime *rt, int64 seed)
JSLL_AND(rt->rngSeed, tmp, rt->rngMask);
}
static void
random_init(JSRuntime *rt)
void
js_random_init(JSRuntime *rt)
{
int64 tmp, tmp2;
@ -425,8 +425,8 @@ random_next(JSRuntime *rt, int bits)
return retval;
}
static jsdouble
random_nextDouble(JSRuntime *rt)
jsdouble
js_random_nextDouble(JSRuntime *rt)
{
int64 tmp, tmp2;
jsdouble d;
@ -438,16 +438,16 @@ random_nextDouble(JSRuntime *rt)
return d / rt->rngDscale;
}
static JSBool
math_random(JSContext *cx, uintN argc, jsval *vp)
JSBool
js_math_random(JSContext *cx, uintN argc, jsval *vp)
{
JSRuntime *rt;
jsdouble z;
rt = cx->runtime;
JS_LOCK_RUNTIME(rt);
random_init(rt);
z = random_nextDouble(rt);
js_random_init(rt);
z = js_random_nextDouble(rt);
JS_UNLOCK_RUNTIME(rt);
return js_NewNumberInRootedValue(cx, z, vp);
}
@ -541,7 +541,7 @@ static JSFunctionSpec math_static_methods[] = {
JS_FN("max", math_max, 0, 2, 0),
JS_FN("min", math_min, 0, 2, 0),
JS_FN("pow", js_math_pow, 2, 2, 0),
JS_FN("random", math_random, 0, 0, 0),
JS_FN("random", js_math_random, 0, 0, 0),
JS_FN("round", math_round, 1, 1, 0),
JS_FN("sin", js_math_sin, 1, 1, 0),
JS_FN("sqrt", js_math_sqrt, 1, 1, 0),

View File

@ -52,6 +52,12 @@ extern JSClass js_MathClass;
extern JSObject *
js_InitMathClass(JSContext *cx, JSObject *obj);
extern void
js_random_init(JSRuntime *rt);
extern jsdouble
js_random_nextDouble(JSRuntime *rt);
JS_END_EXTERN_C
#endif /* jsmath_h___ */

View File

@ -2534,6 +2534,9 @@ js_str_fromCharCode(JSContext* cx, uintN argc, jsval* vp);
JSBool
js_str_charCodeAt(JSContext* cx, uintN argc, jsval* vp);
JSBool
js_math_random(JSContext* cx, uintN argc, jsval* vp);
bool TraceRecorder::record_JSOP_CALL()
{
uintN argc = GET_ARGC(cx->fp->regs->pc);
@ -2575,7 +2578,8 @@ bool TraceRecorder::record_JSOP_CALL()
{ js_str_substring, F_String_p_substring, "TC", "ii", FAIL_NULL, },
{ js_str_substring, F_String_p_substring_1, "TC", "i", FAIL_NULL, },
{ js_str_fromCharCode, F_String_fromCharCode, "C", "i", FAIL_NULL, },
{ js_str_charCodeAt, F_String_p_charCodeAt, "T", "i", FAIL_NEG, }
{ js_str_charCodeAt, F_String_p_charCodeAt, "T", "i", FAIL_NEG, },
{ js_math_random, F_Math_random, "R", "", INFALLIBLE, }
};
for (uintN i = 0; i < JS_ARRAY_LENGTH(knownNatives); i++) {
@ -2598,6 +2602,8 @@ bool TraceRecorder::record_JSOP_CALL()
*argp = cx_ins; \
} else if (argtype == 'T') { \
*argp = thisval_ins; \
} else if (argtype == 'R') { \
*argp = lir->insImmPtr((void*)cx->runtime); \
} else { \
JS_ASSERT(0 && "unknown prefix arg type"); \
} \