Javascript bug while loading large object in JSON format

13 views
Skip to first unread message

Pollex Alex

unread,
Feb 12, 2010, 6:13:39 AM2/12/10
to js...@googlegroups.com
Hello, Shanti and everybody!

I've found a consistent bug while trying to load a large object in
JSON format like this:
-----
obj = {
'key_1' : 'value_1',
'key_2' : 'value_2',
...
'key_32768' : 'value_32768',
'key_32769' : 'value_32769',
...
}
-----

Starting with key_32768, the Javascript parser *reverses* key<=>value
pairs and treats the rest as if it was written:
----
'value_32768' : 'key_32768',
'value_32769' : 'key_32769',
...
----

You may test it yourselves with the script "json_bug.js":
-----
function save(obj, filename) {
var file = new Stream(filename, "w");
file.write("obj={\n");
for (var key in obj)
file.write("\""+key+"\" : \""+obj[key]+"\",\n");
file.write("};");
file.close();
}

if (!system.exists("json_bug1.js")) {
var obj = {};
for (var i = 1; i <= 33000; i++)
obj['any_key_'+i] = 'any_value_'+i;
save(obj, "json_bug1.js");
} else {
load("json_bug1.js");
save(obj, "json_bug2.js");
}
-----

On the first run the script builds a large object and writes it to
file "json_bug1.js" in JSON format.
On the second run the script loads "json_bug1.js" and writes it to
another file "json_bug2.js".
Now view "json_bug2.js" and see keys<=>values reversed after key_32768 !

This bug is also present in Kmeleon browser (http://kmeleon.sourceforge.net/)
but absent from Mozilla Firefox (I've not tested other Mozilla applications).
I guess this is due to some unclear bugs in Microsoft libraries from
the MSVC compiler.

Can you fix it please once and forever ?
I wonder why Firefox doesn't have this bug ?

Truly yours,
Pollex.

Michael Iola

unread,
Feb 15, 2010, 4:33:01 AM2/15/10
to js...@googlegroups.com
Hi Pollex
I tested script json_bug.js with jsdb.exe 1.8.0.1 downloaded from jsdb.org
and I don't see the bug. Files json_bug1.js and json_bug2.js have the same data.
Please tell me which version of jsdb are you using and under what compiler / platform.

Michael

2010/2/12 Pollex Alex <poll...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "JSDB" group.
To post to this group, send email to js...@googlegroups.com.
To unsubscribe from this group, send email to jsdb+uns...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jsdb?hl=en.


Pollex Alex

unread,
Feb 15, 2010, 6:37:10 AM2/15/10
to js...@googlegroups.com
Thank you Michael!

Indeed, the latest jsdb version 1.8.0.1 doesn't have this bug,
but I've tested versions 1.7.2 and earlier, which all do have it !

I wonder whether it is due to Mozilla code and how could it survive
for such a long time ?

Pollex

Shanti Rao

unread,
Feb 16, 2010, 12:37:38 PM2/16/10
to JSDB
This was a known problem with the SpiderMonkey engine. If you follow
news:mozilla.dev.tech.js-engine, there have been discussions on how to
rewrite the hash tables to allow big objects. Apparently, that change
made it into version 1.8. Firefox has been using 1.8 for a long time
now.

You're probably wondering: why did it take JSDB so long to catch up?

Mostly because I've been chasing my toddler around instead of writing
code. Sorry. But my firm, Raosoft, Inc. has always had a policy of
being slightly behind the bleeding edge in delivering software. We
value robustness and reliability. SpiderMonkey 1.8 went through some
big changes, especially with the JIT compiler. The configure script is
an outright nightmare -- JSDB 1.8 still doesn't compile on SunOS. With
JSDB 1.7.2, bugs were discovered about once a month. Not many. JSDB
1.8 is revealing some unpleasant surprises in the Mozilla engine, so
we'll have to make the bug reporting process more formal.

Shanti

Reply all
Reply to author
Forward
0 new messages