Skip Menu |

This queue is for tickets about the JavaScript CPAN distribution.

Report information
The Basics
Id: 32869
Status: open
Priority: 0/
Queue: JavaScript

People
Owner: CLAESJAC [...] cpan.org
Requestors: CRAKRJACK [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: 1.04
Fixed in: (no value)



Subject: JavaScript refuses to convert non-strings to hash keys, even though perl supports it
The JavaScript-to-Perl type conversion refuses to convert anything but a string into a perl hash key, even though perl is willing to convert almost anything into a hash key. Even with "use strict" enabled, perl will silently convert "undef" into an empty string when assigning a hash key. Integer and decimal values are automatically converted into their string equivalents as well. PJS_TypeConversion.c should do the same thing so that translating javascript objects to perl has the same flexibility as translating perl structures to perl.
On Fri Feb 01 21:12:30 2008, CRAKRJACK wrote: Show quoted text
> The JavaScript-to-Perl type conversion refuses to convert anything but a > string into a perl hash key, even though perl is willing to convert > almost anything into a hash key. > > Even with "use strict" enabled, perl will silently convert "undef" into > an empty string when assigning a hash key. Integer and decimal values > are automatically converted into their string equivalents as well. > PJS_TypeConversion.c should do the same thing so that translating > javascript objects to perl has the same flexibility as translating perl > structures to perl. >
Hi, do you have a testcase which shows the bug? Thanks, Claes
On Fri Feb 01 21:12:30 2008, CRAKRJACK wrote: Show quoted text
> The JavaScript-to-Perl type conversion refuses to convert anything but a > string into a perl hash key, even though perl is willing to convert > almost anything into a hash key. > > Even with "use strict" enabled, perl will silently convert "undef" into > an empty string when assigning a hash key. Integer and decimal values > are automatically converted into their string equivalents as well. > PJS_TypeConversion.c should do the same thing so that translating > javascript objects to perl has the same flexibility as translating perl > structures to perl. >
Hi, do you have a testcase which shows the bug? Thanks, Claes
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.
Subject: patch.txt
--- 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