Attached is a patch for this. I do not have a test example, but I have seen it occur with object key
strings (i.e. quoted) that contain only numbers when passing these to a Perl function from
JavaScript. It does *not* occur with simple encode/decode calls.
--- PJS_TypeConversion.c.old 2010-02-20 14:52:47.000000000 -0700
+++ PJS_TypeConversion.c 2011-05-15 22:10:37.000000000 -0600
@@ -261,10 +261,12 @@ JSBool JSVALToSV(JSContext *cx, HV *seen
else if (JSVAL_IS_DOUBLE(v)) {
sv_setnv(*sv, *JSVAL_TO_DOUBLE(v));
}
- else if (JSVAL_IS_STRING(v)) {
+ /* Perl doesn't mind if this is a number, and neither does JavaScript,
+ so I'm not sure why this enforced string. --dbrian */
+ else if (JSVAL_IS_STRING(v) || JSVAL_IS_NUMBER(v)) {
/* XXX: review this, JS_GetStringBytes twice causing assertaion failure */
#ifdef JS_C_STRINGS_ARE_UTF8
- char *tmp = JS_smprintf("%hs", JS_GetStringChars(JSVAL_TO_STRING(v)));
+ char *tmp = JS_smprintf("%hs", JS_GetStringChars(JS_ValueToString(cx,v)));
sv_setpv(*sv, tmp);
SvUTF8_on(*sv);
free(tmp);
@@ -298,7 +300,7 @@ JSBool JSVALToSV(JSContext *cx, HV *seen
{
jsval dvalue;
if (OBJ_DEFAULT_VALUE(cx, object, JSTYPE_OBJECT, &dvalue) &&
- JSVAL_IS_STRING(dvalue)) {
+ JSVAL_IS_STRING(dvalue) || JSVAL_IS_NUMBER(dvalue)) {
sv_setpv(*sv, JS_GetStringBytes(JSVAL_TO_STRING(dvalue)));
return JS_TRUE;
}
@@ -479,12 +481,12 @@ SV *JSHASHToSV(JSContext *cx, HV *seen,
JS_IdToValue(cx, (prop_arr->vector)[idx], &key);
- if(JSVAL_IS_STRING(key)) {
+ if(JSVAL_IS_STRING(key) || JSVAL_IS_NUMBER(key)) {
jsval value;
SV *val_sv;
SV *js_key_sv = sv_newmortal();
- char *js_key = JS_GetStringBytes(JSVAL_TO_STRING(key));
+ char *js_key = JS_GetStringBytes(JS_ValueToString(cx,key));
sv_setpv(js_key_sv, js_key);
#ifdef JS_C_STRINGS_ARE_UTF8