Skip Menu |
 

This queue is for tickets about the JavaScript-V8 CPAN distribution.

Report information
The Basics
Id: 103943
Status: open
Priority: 0/
Queue: JavaScript-V8

People
Owner: Nobody in particular
Requestors: tildedave [...] gmail.com
Cc:
AdminCc:

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



Subject: JSON::true / JSON::false converted to empty JavaScript object
Date: Fri, 24 Apr 2015 08:47:43 -0700
To: bug-JavaScript-V8 [...] rt.cpan.org
From: Dave King <tildedave [...] gmail.com>
Download (untitled) / with headers
text/plain 912b
It seems that the JSON::true and JSON::false objects are embedded into JavaScript as empty objects. I understand why this happens - the V8 embedding sees these as objects (because they are), but this behavior seems counterintuitive enough and not the right thing. Could the library override the conversions for these special objects, or barring that, provide some way to do custom conversions so that clients could override conversions for their own objects? Here's a snippet that demonstrates the non-intuitive behavior: use JavaScript::V8; use JSON; my $v8context = JavaScript::V8::Context->new(); $v8context->bind(f => JSON::false); my $res = $v8context->eval('(function() { return (f ? 1 : 0) })()'); print "$res\n"; # returns 1, because JSON::false is bound to the empty object and so truthy my $res = $v8context->eval('typeof f'); print "$res\n"; # prints 'object', not 'boolean' -- Regards, Dave
Download (untitled) / with headers
text/plain 1.2k
On Fri Apr 24 11:47:59 2015, tildedave@gmail.com wrote: Show quoted text
> It seems that the JSON::true and JSON::false objects are embedded into > JavaScript as empty objects. I understand why this happens - the V8 > embedding sees these as objects (because they are), but this behavior > seems counterintuitive enough and not the right thing. > > Could the library override the conversions for these special objects, > or barring that, provide some way to do custom conversions so that > clients could override conversions for their own objects? > > Here's a snippet that demonstrates the non-intuitive behavior: > > use JavaScript::V8; > use JSON; > > my $v8context = JavaScript::V8::Context->new(); > $v8context->bind(f => JSON::false); > my $res = $v8context->eval('(function() { return (f ? 1 : 0) })()'); > print "$res\n"; > # returns 1, because JSON::false is bound to the empty object and so truthy > > my $res = $v8context->eval('typeof f'); > print "$res\n"; > # prints 'object', not 'boolean' >
I've forked DGL's GitHub fork of this code and submitted a pull request[1] that adds Perl JSON::PP::Boolean object -> Javascript boolean primitive handling. It also adds test cases for JSON and JSON::XS. Cheers Brad [1] https://github.com/dgl/javascript-v8/pull/17


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.