/[jscoverage]/trunk/js/json.cpp
ViewVC logotype

Contents of /trunk/js/json.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 507 - (show annotations)
Sun Jan 10 07:23:34 2010 UTC (9 years, 11 months ago) by siliconforks
File size: 36706 byte(s)
Update SpiderMonkey from Firefox 3.6rc1.

1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=99:
3 *
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 *
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
11 *
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
15 * License.
16 *
17 * The Original Code is SpiderMonkey JSON.
18 *
19 * The Initial Developer of the Original Code is
20 * Mozilla Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1998-1999
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 * Robert Sayre <sayrer@gmail.com>
26 *
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
38 *
39 * ***** END LICENSE BLOCK ***** */
40
41 #include <string.h>
42 #include "jsapi.h"
43 #include "jsarena.h"
44 #include "jsarray.h"
45 #include "jsatom.h"
46 #include "jsbool.h"
47 #include "jscntxt.h"
48 #include "jsdtoa.h"
49 #include "jsfun.h"
50 #include "jsinterp.h"
51 #include "jsiter.h"
52 #include "jsnum.h"
53 #include "jsobj.h"
54 #include "jsprf.h"
55 #include "jsscan.h"
56 #include "jsstr.h"
57 #include "jstypes.h"
58 #include "jsstdint.h"
59 #include "jsutil.h"
60 #include "jsxml.h"
61 #include "jsvector.h"
62
63 #include "json.h"
64
65 #include "jsatominlines.h"
66
67 struct JSONParser
68 {
69 JSONParser(JSContext *cx)
70 : hexChar(), numHex(), statep(), stateStack(), rootVal(), objectStack(),
71 objectKey(cx), buffer(cx)
72 {}
73
74 /* Used while handling \uNNNN in strings */
75 jschar hexChar;
76 uint8 numHex;
77
78 JSONParserState *statep;
79 JSONParserState stateStack[JSON_MAX_DEPTH];
80 jsval *rootVal;
81 JSObject *objectStack;
82 js::Vector<jschar, 8> objectKey;
83 js::Vector<jschar, 8> buffer;
84 };
85
86 JSClass js_JSONClass = {
87 js_JSON_str,
88 JSCLASS_HAS_CACHED_PROTO(JSProto_JSON),
89 JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
90 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, NULL,
91 JSCLASS_NO_OPTIONAL_MEMBERS
92 };
93
94 JSBool
95 js_json_parse(JSContext *cx, uintN argc, jsval *vp)
96 {
97 JSString *s = NULL;
98 jsval *argv = vp + 2;
99 jsval reviver = JSVAL_NULL;
100 JSAutoTempValueRooter tvr(cx, 1, &reviver);
101
102 if (!JS_ConvertArguments(cx, argc, argv, "S / v", &s, &reviver))
103 return JS_FALSE;
104
105 JSONParser *jp = js_BeginJSONParse(cx, vp);
106 JSBool ok = jp != NULL;
107 if (ok) {
108 const jschar *chars;
109 size_t length;
110 s->getCharsAndLength(chars, length);
111 ok = js_ConsumeJSONText(cx, jp, chars, length);
112 ok &= js_FinishJSONParse(cx, jp, reviver);
113 }
114
115 return ok;
116 }
117
118 JSBool
119 js_json_stringify(JSContext *cx, uintN argc, jsval *vp)
120 {
121 jsval *argv = vp + 2;
122 JSObject *replacer = NULL;
123 jsval space = JSVAL_NULL;
124 JSAutoTempValueRooter tvr(cx, replacer);
125 JSAutoTempValueRooter tvr2(cx, 1, &space);
126
127 // Must throw an Error if there isn't a first arg
128 if (!JS_ConvertArguments(cx, argc, argv, "v / o v", vp, &replacer, &space))
129 return JS_FALSE;
130
131 JSCharBuffer cb(cx);
132
133 if (!js_Stringify(cx, vp, replacer, space, cb))
134 return JS_FALSE;
135
136 // XXX This can never happen to nsJSON.cpp, but the JSON object
137 // needs to support returning undefined. So this is a little awkward
138 // for the API, because we want to support streaming writers.
139 if (!cb.empty()) {
140 JSString *str = js_NewStringFromCharBuffer(cx, cb);
141 if (!str)
142 return JS_FALSE;
143 *vp = STRING_TO_JSVAL(str);
144 } else {
145 *vp = JSVAL_VOID;
146 }
147
148 return JS_TRUE;
149 }
150
151 JSBool
152 js_TryJSON(JSContext *cx, jsval *vp)
153 {
154 // Checks whether the return value implements toJSON()
155 JSBool ok = JS_TRUE;
156
157 if (!JSVAL_IS_PRIMITIVE(*vp)) {
158 JSObject *obj = JSVAL_TO_OBJECT(*vp);
159 ok = js_TryMethod(cx, obj, cx->runtime->atomState.toJSONAtom, 0, NULL, vp);
160 }
161
162 return ok;
163 }
164
165
166 static const char quote = '\"';
167 static const char backslash = '\\';
168 static const char unicodeEscape[] = "\\u00";
169
170 static JSBool
171 write_string(JSContext *cx, JSCharBuffer &cb, const jschar *buf, uint32 len)
172 {
173 if (!cb.append(quote))
174 return JS_FALSE;
175
176 uint32 mark = 0;
177 uint32 i;
178 for (i = 0; i < len; ++i) {
179 if (buf[i] == quote || buf[i] == backslash) {
180 if (!cb.append(&buf[mark], i - mark) || !cb.append(backslash) ||
181 !cb.append(buf[i])) {
182 return JS_FALSE;
183 }
184 mark = i + 1;
185 } else if (buf[i] <= 31 || buf[i] == 127) {
186 if (!cb.append(&buf[mark], i - mark) ||
187 !js_AppendLiteral(cb, unicodeEscape)) {
188 return JS_FALSE;
189 }
190 char ubuf[3];
191 size_t len = JS_snprintf(ubuf, sizeof(ubuf), "%.2x", buf[i]);
192 JS_ASSERT(len == 2);
193 jschar wbuf[3];
194 size_t wbufSize = JS_ARRAY_LENGTH(wbuf);
195 if (!js_InflateStringToBuffer(cx, ubuf, len, wbuf, &wbufSize) ||
196 !cb.append(wbuf, wbufSize)) {
197 return JS_FALSE;
198 }
199 mark = i + 1;
200 }
201 }
202
203 if (mark < len && !cb.append(&buf[mark], len - mark))
204 return JS_FALSE;
205
206 return cb.append(quote);
207 }
208
209 class StringifyContext
210 {
211 public:
212 StringifyContext(JSContext *cx, JSCharBuffer &cb, JSObject *replacer)
213 : cb(cb), gap(cx), replacer(replacer), depth(0)
214 {}
215
216 JSCharBuffer &cb;
217 JSCharBuffer gap;
218 JSObject *replacer;
219 uint32 depth;
220 };
221
222 static JSBool CallReplacerFunction(JSContext *cx, jsid id, JSObject *holder,
223 StringifyContext *scx, jsval *vp);
224 static JSBool Str(JSContext *cx, jsid id, JSObject *holder,
225 StringifyContext *scx, jsval *vp, bool callReplacer = true);
226
227 static JSBool
228 WriteIndent(JSContext *cx, StringifyContext *scx, uint32 limit)
229 {
230 if (!scx->gap.empty()) {
231 if (!scx->cb.append('\n'))
232 return JS_FALSE;
233 for (uint32 i = 0; i < limit; i++) {
234 if (!scx->cb.append(scx->gap.begin(), scx->gap.end()))
235 return JS_FALSE;
236 }
237 }
238
239 return JS_TRUE;
240 }
241
242 static JSBool
243 JO(JSContext *cx, jsval *vp, StringifyContext *scx)
244 {
245 JSObject *obj = JSVAL_TO_OBJECT(*vp);
246
247 if (!scx->cb.append('{'))
248 return JS_FALSE;
249
250 jsval vec[3] = {JSVAL_NULL, JSVAL_NULL, JSVAL_NULL};
251 JSAutoTempValueRooter tvr(cx, 3, vec);
252 jsval& key = vec[0];
253 jsval& outputValue = vec[1];
254
255 JSObject *iterObj = NULL;
256 jsval *keySource = vp;
257 bool usingWhitelist = false;
258
259 // if the replacer is an array, we use the keys from it
260 if (scx->replacer && JS_IsArrayObject(cx, scx->replacer)) {
261 usingWhitelist = true;
262 vec[2] = OBJECT_TO_JSVAL(scx->replacer);
263 keySource = &vec[2];
264 }
265
266 if (!js_ValueToIterator(cx, JSITER_ENUMERATE, keySource))
267 return JS_FALSE;
268 iterObj = JSVAL_TO_OBJECT(*keySource);
269
270 JSBool memberWritten = JS_FALSE;
271
272 bool ok = false;
273 while (true) {
274 outputValue = JSVAL_VOID;
275 if (!js_CallIteratorNext(cx, iterObj, &key))
276 goto error_break;
277 if (key == JSVAL_HOLE)
278 break;
279
280 jsuint index = 0;
281 if (usingWhitelist) {
282 // skip non-index properties
283 if (!js_IdIsIndex(key, &index))
284 continue;
285
286 jsval newKey;
287 if (!scx->replacer->getProperty(cx, key, &newKey))
288 goto error_break;
289 key = newKey;
290 }
291
292 JSString *ks;
293 if (JSVAL_IS_STRING(key)) {
294 ks = JSVAL_TO_STRING(key);
295 } else {
296 ks = js_ValueToString(cx, key);
297 if (!ks)
298 goto error_break;
299 }
300 JSAutoTempValueRooter keyStringRoot(cx, ks);
301
302 // Don't include prototype properties, since this operation is
303 // supposed to be implemented as if by ES3.1 Object.keys()
304 jsid id;
305 jsval v = JS_FALSE;
306 if (!js_ValueToStringId(cx, STRING_TO_JSVAL(ks), &id) ||
307 !js_HasOwnProperty(cx, obj->map->ops->lookupProperty, obj, id, &v)) {
308 goto error_break;
309 }
310
311 if (v != JSVAL_TRUE)
312 continue;
313
314 if (!JS_GetPropertyById(cx, obj, id, &outputValue))
315 goto error_break;
316
317 if (JSVAL_IS_OBJECT(outputValue) && !js_TryJSON(cx, &outputValue))
318 goto error_break;
319
320 // call this here, so we don't write out keys if the replacer function
321 // wants to elide the value.
322 if (!CallReplacerFunction(cx, id, obj, scx, &outputValue))
323 goto error_break;
324
325 JSType type = JS_TypeOfValue(cx, outputValue);
326
327 // elide undefined values and functions and XML
328 if (outputValue == JSVAL_VOID || type == JSTYPE_FUNCTION || type == JSTYPE_XML)
329 continue;
330
331 // output a comma unless this is the first member to write
332 if (memberWritten && !scx->cb.append(','))
333 goto error_break;
334 memberWritten = JS_TRUE;
335
336 if (!WriteIndent(cx, scx, scx->depth))
337 goto error_break;
338
339 // Be careful below, this string is weakly rooted
340 JSString *s = js_ValueToString(cx, key);
341 if (!s)
342 goto error_break;
343
344 const jschar *chars;
345 size_t length;
346 s->getCharsAndLength(chars, length);
347 if (!write_string(cx, scx->cb, chars, length) ||
348 !scx->cb.append(':') ||
349 !Str(cx, id, obj, scx, &outputValue, false)) {
350 goto error_break;
351 }
352 }
353 ok = true;
354
355 error_break:
356 if (iterObj) {
357 // Always close the iterator, but make sure not to stomp on OK
358 JS_ASSERT(OBJECT_TO_JSVAL(iterObj) == *keySource);
359 ok &= js_CloseIterator(cx, *keySource);
360 }
361
362 if (!ok)
363 return JS_FALSE;
364
365 if (memberWritten && !WriteIndent(cx, scx, scx->depth - 1))
366 return JS_FALSE;
367
368 return scx->cb.append('}');
369 }
370
371 static JSBool
372 JA(JSContext *cx, jsval *vp, StringifyContext *scx)
373 {
374 JSObject *obj = JSVAL_TO_OBJECT(*vp);
375
376 if (!scx->cb.append('['))
377 return JS_FALSE;
378
379 jsuint length;
380 if (!js_GetLengthProperty(cx, obj, &length))
381 return JS_FALSE;
382
383 jsval outputValue = JSVAL_NULL;
384 JSAutoTempValueRooter tvr(cx, 1, &outputValue);
385
386 jsid id;
387 jsuint i;
388 for (i = 0; i < length; i++) {
389 id = INT_TO_JSID(i);
390
391 if (!obj->getProperty(cx, id, &outputValue))
392 return JS_FALSE;
393
394 if (!Str(cx, id, obj, scx, &outputValue))
395 return JS_FALSE;
396
397 if (outputValue == JSVAL_VOID) {
398 if (!js_AppendLiteral(scx->cb, "null"))
399 return JS_FALSE;
400 }
401
402 if (i < length - 1) {
403 if (!scx->cb.append(','))
404 return JS_FALSE;
405 if (!WriteIndent(cx, scx, scx->depth))
406 return JS_FALSE;
407 }
408 }
409
410 if (length != 0 && !WriteIndent(cx, scx, scx->depth - 1))
411 return JS_FALSE;
412
413 return scx->cb.append(']');
414 }
415
416 static JSBool
417 CallReplacerFunction(JSContext *cx, jsid id, JSObject *holder, StringifyContext *scx, jsval *vp)
418 {
419 if (scx->replacer && js_IsCallable(scx->replacer, cx)) {
420 jsval vec[2] = {ID_TO_VALUE(id), *vp};
421 if (!JS_CallFunctionValue(cx, holder, OBJECT_TO_JSVAL(scx->replacer), 2, vec, vp))
422 return JS_FALSE;
423 }
424
425 return JS_TRUE;
426 }
427
428 static JSBool
429 Str(JSContext *cx, jsid id, JSObject *holder, StringifyContext *scx, jsval *vp, bool callReplacer)
430 {
431 JS_CHECK_RECURSION(cx, return JS_FALSE);
432
433 if (!holder->getProperty(cx, id, vp))
434 return JS_FALSE;
435
436 if (!JSVAL_IS_PRIMITIVE(*vp) && !js_TryJSON(cx, vp))
437 return JS_FALSE;
438
439 if (callReplacer && !CallReplacerFunction(cx, id, holder, scx, vp))
440 return JS_FALSE;
441
442 // catches string and number objects with no toJSON
443 if (!JSVAL_IS_PRIMITIVE(*vp)) {
444 JSClass *clasp = OBJ_GET_CLASS(cx, JSVAL_TO_OBJECT(*vp));
445 if (clasp == &js_StringClass || clasp == &js_NumberClass)
446 *vp = JSVAL_TO_OBJECT(*vp)->fslots[JSSLOT_PRIMITIVE_THIS];
447 }
448
449 if (JSVAL_IS_STRING(*vp)) {
450 const jschar *chars;
451 size_t length;
452 JSVAL_TO_STRING(*vp)->getCharsAndLength(chars, length);
453 return write_string(cx, scx->cb, chars, length);
454 }
455
456 if (JSVAL_IS_NULL(*vp)) {
457 return js_AppendLiteral(scx->cb, "null");
458 }
459
460 if (JSVAL_IS_BOOLEAN(*vp)) {
461 return JSVAL_TO_BOOLEAN(*vp) ? js_AppendLiteral(scx->cb, "true")
462 : js_AppendLiteral(scx->cb, "false");
463 }
464
465 if (JSVAL_IS_NUMBER(*vp)) {
466 if (JSVAL_IS_DOUBLE(*vp)) {
467 jsdouble d = *JSVAL_TO_DOUBLE(*vp);
468 if (!JSDOUBLE_IS_FINITE(d))
469 return js_AppendLiteral(scx->cb, "null");
470 }
471
472 char numBuf[DTOSTR_STANDARD_BUFFER_SIZE], *numStr;
473 jsdouble d = JSVAL_IS_INT(*vp) ? jsdouble(JSVAL_TO_INT(*vp)) : *JSVAL_TO_DOUBLE(*vp);
474 numStr = JS_dtostr(numBuf, sizeof numBuf, DTOSTR_STANDARD, 0, d);
475 if (!numStr) {
476 JS_ReportOutOfMemory(cx);
477 return JS_FALSE;
478 }
479
480 jschar dstr[DTOSTR_STANDARD_BUFFER_SIZE];
481 size_t dbufSize = DTOSTR_STANDARD_BUFFER_SIZE;
482 if (!js_InflateStringToBuffer(cx, numStr, strlen(numStr), dstr, &dbufSize))
483 return JS_FALSE;
484
485 return scx->cb.append(dstr, dbufSize);
486 }
487
488 if (JSVAL_IS_OBJECT(*vp) && !VALUE_IS_FUNCTION(cx, *vp) && !VALUE_IS_XML(cx, *vp)) {
489 JSBool ok;
490
491 scx->depth++;
492 ok = (JS_IsArrayObject(cx, JSVAL_TO_OBJECT(*vp)) ? JA : JO)(cx, vp, scx);
493 scx->depth--;
494
495 return ok;
496 }
497
498 *vp = JSVAL_VOID;
499 return JS_TRUE;
500 }
501
502 static JSBool
503 InitializeGap(JSContext *cx, jsval space, JSCharBuffer &cb)
504 {
505 if (!JSVAL_IS_PRIMITIVE(space)) {
506 JSClass *clasp = OBJ_GET_CLASS(cx, JSVAL_TO_OBJECT(space));
507 if (clasp == &js_StringClass || clasp == &js_NumberClass)
508 return js_ValueToCharBuffer(cx, space, cb);
509 }
510
511 if (JSVAL_IS_STRING(space))
512 return js_ValueToCharBuffer(cx, space, cb);
513
514 if (JSVAL_IS_NUMBER(space)) {
515 jsdouble d = JSVAL_IS_INT(space)
516 ? JSVAL_TO_INT(space)
517 : js_DoubleToInteger(*JSVAL_TO_DOUBLE(space));
518 d = JS_MIN(10, d);
519 if (d >= 1 && !cb.appendN(' ', uint32(d)))
520 return JS_FALSE;
521 }
522
523 return JS_TRUE;
524 }
525
526 JSBool
527 js_Stringify(JSContext *cx, jsval *vp, JSObject *replacer, jsval space,
528 JSCharBuffer &cb)
529 {
530 // XXX stack
531 JSObject *stack = JS_NewArrayObject(cx, 0, NULL);
532 if (!stack)
533 return JS_FALSE;
534
535 StringifyContext scx(cx, cb, replacer);
536 if (!InitializeGap(cx, space, scx.gap))
537 return JS_FALSE;
538
539 JSObject *obj = js_NewObject(cx, &js_ObjectClass, NULL, NULL);
540 if (!obj)
541 return JS_FALSE;
542
543 if (!obj->defineProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.emptyAtom),
544 *vp, NULL, NULL, JSPROP_ENUMERATE)) {
545 return JS_FALSE;
546 }
547
548 return Str(cx, ATOM_TO_JSID(cx->runtime->atomState.emptyAtom), obj, &scx, vp);
549 }
550
551 // helper to determine whether a character could be part of a number
552 static JSBool IsNumChar(jschar c)
553 {
554 return ((c <= '9' && c >= '0') || c == '.' || c == '-' || c == '+' || c == 'e' || c == 'E');
555 }
556
557 static JSBool HandleData(JSContext *cx, JSONParser *jp, JSONDataType type);
558 static JSBool PopState(JSContext *cx, JSONParser *jp);
559
560 static JSBool
561 DestroyIdArrayOnError(JSContext *cx, JSIdArray *ida) {
562 JS_DestroyIdArray(cx, ida);
563 return JS_FALSE;
564 }
565
566 static JSBool
567 Walk(JSContext *cx, jsid id, JSObject *holder, jsval reviver, jsval *vp)
568 {
569 JS_CHECK_RECURSION(cx, return JS_FALSE);
570
571 if (!holder->getProperty(cx, id, vp))
572 return JS_FALSE;
573
574 JSObject *obj;
575
576 if (!JSVAL_IS_PRIMITIVE(*vp) && !js_IsCallable(obj = JSVAL_TO_OBJECT(*vp), cx)) {
577 jsval propValue = JSVAL_NULL;
578 JSAutoTempValueRooter tvr(cx, 1, &propValue);
579
580 if(OBJ_IS_ARRAY(cx, obj)) {
581 jsuint length = 0;
582 if (!js_GetLengthProperty(cx, obj, &length))
583 return JS_FALSE;
584
585 for (jsuint i = 0; i < length; i++) {
586 jsid index;
587 if (!js_IndexToId(cx, i, &index))
588 return JS_FALSE;
589
590 if (!Walk(cx, index, obj, reviver, &propValue))
591 return JS_FALSE;
592
593 if (!obj->defineProperty(cx, index, propValue, NULL, NULL, JSPROP_ENUMERATE))
594 return JS_FALSE;
595 }
596 } else {
597 JSIdArray *ida = JS_Enumerate(cx, obj);
598 if (!ida)
599 return JS_FALSE;
600
601 JSAutoTempValueRooter idaroot(cx, JS_ARRAY_LENGTH(ida), (jsval*)ida);
602
603 for(jsint i = 0; i < ida->length; i++) {
604 jsid idName = ida->vector[i];
605 if (!Walk(cx, idName, obj, reviver, &propValue))
606 return DestroyIdArrayOnError(cx, ida);
607 if (propValue == JSVAL_VOID) {
608 if (!js_DeleteProperty(cx, obj, idName, &propValue))
609 return DestroyIdArrayOnError(cx, ida);
610 } else {
611 if (!obj->defineProperty(cx, idName, propValue, NULL, NULL, JSPROP_ENUMERATE))
612 return DestroyIdArrayOnError(cx, ida);
613 }
614 }
615
616 JS_DestroyIdArray(cx, ida);
617 }
618 }
619
620 // return reviver.call(holder, key, value);
621 jsval value = *vp;
622 JSString *key = js_ValueToString(cx, ID_TO_VALUE(id));
623 if (!key)
624 return JS_FALSE;
625
626 jsval vec[2] = {STRING_TO_JSVAL(key), value};
627 jsval reviverResult;
628 if (!JS_CallFunctionValue(cx, holder, reviver, 2, vec, &reviverResult))
629 return JS_FALSE;
630
631 *vp = reviverResult;
632
633 return JS_TRUE;
634 }
635
636 static JSBool
637 Revive(JSContext *cx, jsval reviver, jsval *vp)
638 {
639
640 JSObject *obj = js_NewObject(cx, &js_ObjectClass, NULL, NULL);
641 if (!obj)
642 return JS_FALSE;
643
644 jsval v = OBJECT_TO_JSVAL(obj);
645 JSAutoTempValueRooter tvr(cx, 1, &v);
646 if (!obj->defineProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.emptyAtom),
647 *vp, NULL, NULL, JSPROP_ENUMERATE)) {
648 return JS_FALSE;
649 }
650
651 return Walk(cx, ATOM_TO_JSID(cx->runtime->atomState.emptyAtom), obj, reviver, vp);
652 }
653
654 JSONParser *
655 js_BeginJSONParse(JSContext *cx, jsval *rootVal)
656 {
657 if (!cx)
658 return NULL;
659
660 JSObject *arr = js_NewArrayObject(cx, 0, NULL);
661 if (!arr)
662 return NULL;
663
664 JSONParser *jp = cx->create<JSONParser>(cx);
665 if (!jp)
666 return NULL;
667
668 jp->objectStack = arr;
669 if (!js_AddRoot(cx, &jp->objectStack, "JSON parse stack"))
670 goto bad;
671
672 jp->statep = jp->stateStack;
673 *jp->statep = JSON_PARSE_STATE_INIT;
674 jp->rootVal = rootVal;
675
676 return jp;
677
678 bad:
679 js_FinishJSONParse(cx, jp, JSVAL_NULL);
680 return NULL;
681 }
682
683 JSBool
684 js_FinishJSONParse(JSContext *cx, JSONParser *jp, jsval reviver)
685 {
686 if (!jp)
687 return JS_TRUE;
688
689 JSBool early_ok = JS_TRUE;
690
691 // Check for unprocessed primitives at the root. This doesn't happen for
692 // strings because a closing quote triggers value processing.
693 if ((jp->statep - jp->stateStack) == 1) {
694 if (*jp->statep == JSON_PARSE_STATE_KEYWORD) {
695 early_ok = HandleData(cx, jp, JSON_DATA_KEYWORD);
696 if (early_ok)
697 PopState(cx, jp);
698 } else if (*jp->statep == JSON_PARSE_STATE_NUMBER) {
699 early_ok = HandleData(cx, jp, JSON_DATA_NUMBER);
700 if (early_ok)
701 PopState(cx, jp);
702 }
703 }
704
705 /* This internal API is infallible, in spite of its JSBool return type. */
706 js_RemoveRoot(cx->runtime, &jp->objectStack);
707
708 JSBool ok = *jp->statep == JSON_PARSE_STATE_FINISHED;
709 jsval *vp = jp->rootVal;
710
711 cx->destroy(jp);
712
713 if (!early_ok)
714 return JS_FALSE;
715
716 if (!ok) {
717 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
718 return JS_FALSE;
719 }
720
721 if (!JSVAL_IS_PRIMITIVE(reviver) && js_IsCallable(JSVAL_TO_OBJECT(reviver), cx))
722 ok = Revive(cx, reviver, vp);
723
724 return ok;
725 }
726
727 static JSBool
728 PushState(JSContext *cx, JSONParser *jp, JSONParserState state)
729 {
730 if (*jp->statep == JSON_PARSE_STATE_FINISHED) {
731 // extra input
732 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
733 return JS_FALSE;
734 }
735
736 jp->statep++;
737 if ((uint32)(jp->statep - jp->stateStack) >= JS_ARRAY_LENGTH(jp->stateStack)) {
738 // too deep
739 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
740 return JS_FALSE;
741 }
742
743 *jp->statep = state;
744
745 return JS_TRUE;
746 }
747
748 static JSBool
749 PopState(JSContext *cx, JSONParser *jp)
750 {
751 jp->statep--;
752 if (jp->statep < jp->stateStack) {
753 jp->statep = jp->stateStack;
754 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
755 return JS_FALSE;
756 }
757
758 if (*jp->statep == JSON_PARSE_STATE_INIT)
759 *jp->statep = JSON_PARSE_STATE_FINISHED;
760
761 return JS_TRUE;
762 }
763
764 static JSBool
765 PushValue(JSContext *cx, JSONParser *jp, JSObject *parent, jsval value)
766 {
767 JSBool ok;
768 if (OBJ_IS_ARRAY(cx, parent)) {
769 jsuint len;
770 ok = js_GetLengthProperty(cx, parent, &len);
771 if (ok) {
772 jsid index;
773 if (!js_IndexToId(cx, len, &index))
774 return JS_FALSE;
775 ok = parent->defineProperty(cx, index, value, NULL, NULL, JSPROP_ENUMERATE);
776 }
777 } else {
778 ok = JS_DefineUCProperty(cx, parent, jp->objectKey.begin(),
779 jp->objectKey.length(), value,
780 NULL, NULL, JSPROP_ENUMERATE);
781 jp->objectKey.clear();
782 }
783
784 return ok;
785 }
786
787 static JSBool
788 PushObject(JSContext *cx, JSONParser *jp, JSObject *obj)
789 {
790 jsuint len;
791 if (!js_GetLengthProperty(cx, jp->objectStack, &len))
792 return JS_FALSE;
793 if (len >= JSON_MAX_DEPTH) {
794 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
795 return JS_FALSE;
796 }
797
798 jsval v = OBJECT_TO_JSVAL(obj);
799 JSAutoTempValueRooter tvr(cx, v);
800
801 // Check if this is the root object
802 if (len == 0) {
803 *jp->rootVal = v;
804 // This property must be enumerable to keep the array dense
805 if (!jp->objectStack->defineProperty(cx, INT_TO_JSID(0), *jp->rootVal,
806 NULL, NULL, JSPROP_ENUMERATE)) {
807 return JS_FALSE;
808 }
809 return JS_TRUE;
810 }
811
812 jsval p;
813 if (!jp->objectStack->getProperty(cx, INT_TO_JSID(len - 1), &p))
814 return JS_FALSE;
815
816 JS_ASSERT(JSVAL_IS_OBJECT(p));
817 JSObject *parent = JSVAL_TO_OBJECT(p);
818 if (!PushValue(cx, jp, parent, v))
819 return JS_FALSE;
820
821 // This property must be enumerable to keep the array dense
822 if (!jp->objectStack->defineProperty(cx, INT_TO_JSID(len), v,
823 NULL, NULL, JSPROP_ENUMERATE)) {
824 return JS_FALSE;
825 }
826
827 return JS_TRUE;
828 }
829
830 static JSBool
831 OpenObject(JSContext *cx, JSONParser *jp)
832 {
833 JSObject *obj = js_NewObject(cx, &js_ObjectClass, NULL, NULL);
834 if (!obj)
835 return JS_FALSE;
836
837 return PushObject(cx, jp, obj);
838 }
839
840 static JSBool
841 OpenArray(JSContext *cx, JSONParser *jp)
842 {
843 // Add an array to an existing array or object
844 JSObject *arr = js_NewArrayObject(cx, 0, NULL);
845 if (!arr)
846 return JS_FALSE;
847
848 return PushObject(cx, jp, arr);
849 }
850
851 static JSBool
852 CloseObject(JSContext *cx, JSONParser *jp)
853 {
854 jsuint len;
855 if (!js_GetLengthProperty(cx, jp->objectStack, &len))
856 return JS_FALSE;
857 if (!js_SetLengthProperty(cx, jp->objectStack, len - 1))
858 return JS_FALSE;
859
860 return JS_TRUE;
861 }
862
863 static JSBool
864 CloseArray(JSContext *cx, JSONParser *jp)
865 {
866 return CloseObject(cx, jp);
867 }
868
869 static JSBool
870 PushPrimitive(JSContext *cx, JSONParser *jp, jsval value)
871 {
872 JSAutoTempValueRooter tvr(cx, 1, &value);
873
874 jsuint len;
875 if (!js_GetLengthProperty(cx, jp->objectStack, &len))
876 return JS_FALSE;
877
878 if (len > 0) {
879 jsval o;
880 if (!jp->objectStack->getProperty(cx, INT_TO_JSID(len - 1), &o))
881 return JS_FALSE;
882
883 JS_ASSERT(!JSVAL_IS_PRIMITIVE(o));
884 return PushValue(cx, jp, JSVAL_TO_OBJECT(o), value);
885 }
886
887 // root value must be primitive
888 *jp->rootVal = value;
889 return JS_TRUE;
890 }
891
892 static JSBool
893 HandleNumber(JSContext *cx, JSONParser *jp, const jschar *buf, uint32 len)
894 {
895 const jschar *ep;
896 double val;
897 if (!js_strtod(cx, buf, buf + len, &ep, &val))
898 return JS_FALSE;
899 if (ep != buf + len) {
900 // bad number input
901 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
902 return JS_FALSE;
903 }
904
905 jsval numVal;
906 if (!JS_NewNumberValue(cx, val, &numVal))
907 return JS_FALSE;
908
909 return PushPrimitive(cx, jp, numVal);
910 }
911
912 static JSBool
913 HandleString(JSContext *cx, JSONParser *jp, const jschar *buf, uint32 len)
914 {
915 JSString *str = js_NewStringCopyN(cx, buf, len);
916 if (!str)
917 return JS_FALSE;
918
919 return PushPrimitive(cx, jp, STRING_TO_JSVAL(str));
920 }
921
922 static JSBool
923 HandleKeyword(JSContext *cx, JSONParser *jp, const jschar *buf, uint32 len)
924 {
925 jsval keyword;
926 JSTokenType tt = js_CheckKeyword(buf, len);
927 if (tt != TOK_PRIMARY) {
928 // bad keyword
929 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
930 return JS_FALSE;
931 }
932
933 if (buf[0] == 'n') {
934 keyword = JSVAL_NULL;
935 } else if (buf[0] == 't') {
936 keyword = JSVAL_TRUE;
937 } else if (buf[0] == 'f') {
938 keyword = JSVAL_FALSE;
939 } else {
940 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
941 return JS_FALSE;
942 }
943
944 return PushPrimitive(cx, jp, keyword);
945 }
946
947 static JSBool
948 HandleData(JSContext *cx, JSONParser *jp, JSONDataType type)
949 {
950 JSBool ok;
951
952 switch (type) {
953 case JSON_DATA_STRING:
954 ok = HandleString(cx, jp, jp->buffer.begin(), jp->buffer.length());
955 break;
956
957 case JSON_DATA_KEYSTRING:
958 ok = jp->objectKey.append(jp->buffer.begin(), jp->buffer.end());
959 break;
960
961 case JSON_DATA_NUMBER:
962 ok = HandleNumber(cx, jp, jp->buffer.begin(), jp->buffer.length());
963 break;
964
965 default:
966 JS_ASSERT(type == JSON_DATA_KEYWORD);
967 ok = HandleKeyword(cx, jp, jp->buffer.begin(), jp->buffer.length());
968 break;
969 }
970
971 if (ok)
972 jp->buffer.clear();
973 return ok;
974 }
975
976 JSBool
977 js_ConsumeJSONText(JSContext *cx, JSONParser *jp, const jschar *data, uint32 len)
978 {
979 uint32 i;
980
981 if (*jp->statep == JSON_PARSE_STATE_INIT) {
982 PushState(cx, jp, JSON_PARSE_STATE_VALUE);
983 }
984
985 for (i = 0; i < len; i++) {
986 jschar c = data[i];
987 switch (*jp->statep) {
988 case JSON_PARSE_STATE_VALUE:
989 if (c == ']') {
990 // empty array
991 if (!PopState(cx, jp))
992 return JS_FALSE;
993
994 if (*jp->statep != JSON_PARSE_STATE_ARRAY) {
995 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
996 return JS_FALSE;
997 }
998
999 if (!CloseArray(cx, jp) || !PopState(cx, jp))
1000 return JS_FALSE;
1001
1002 break;
1003 }
1004
1005 if (c == '}') {
1006 // we should only find these in OBJECT_KEY state
1007 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
1008 return JS_FALSE;
1009 }
1010
1011 if (c == '"') {
1012 *jp->statep = JSON_PARSE_STATE_STRING;
1013 break;
1014 }
1015
1016 if (IsNumChar(c)) {
1017 *jp->statep = JSON_PARSE_STATE_NUMBER;
1018 if (!jp->buffer.append(c))
1019 return JS_FALSE;
1020 break;
1021 }
1022
1023 if (JS7_ISLET(c)) {
1024 *jp->statep = JSON_PARSE_STATE_KEYWORD;
1025 if (!jp->buffer.append(c))
1026 return JS_FALSE;
1027 break;
1028 }
1029
1030 // fall through in case the value is an object or array
1031 case JSON_PARSE_STATE_OBJECT_VALUE:
1032 if (c == '{') {
1033 *jp->statep = JSON_PARSE_STATE_OBJECT;
1034 if (!OpenObject(cx, jp) || !PushState(cx, jp, JSON_PARSE_STATE_OBJECT_PAIR))
1035 return JS_FALSE;
1036 } else if (c == '[') {
1037 *jp->statep = JSON_PARSE_STATE_ARRAY;
1038 if (!OpenArray(cx, jp) || !PushState(cx, jp, JSON_PARSE_STATE_VALUE))
1039 return JS_FALSE;
1040 } else if (!JS_ISXMLSPACE(c)) {
1041 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
1042 return JS_FALSE;
1043 }
1044 break;
1045
1046 case JSON_PARSE_STATE_OBJECT:
1047 if (c == '}') {
1048 if (!CloseObject(cx, jp) || !PopState(cx, jp))
1049 return JS_FALSE;
1050 } else if (c == ',') {
1051 if (!PushState(cx, jp, JSON_PARSE_STATE_OBJECT_PAIR))
1052 return JS_FALSE;
1053 } else if (c == ']' || !JS_ISXMLSPACE(c)) {
1054 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
1055 return JS_FALSE;
1056 }
1057 break;
1058
1059 case JSON_PARSE_STATE_ARRAY :
1060 if (c == ']') {
1061 if (!CloseArray(cx, jp) || !PopState(cx, jp))
1062 return JS_FALSE;
1063 } else if (c == ',') {
1064 if (!PushState(cx, jp, JSON_PARSE_STATE_VALUE))
1065 return JS_FALSE;
1066 } else if (!JS_ISXMLSPACE(c)) {
1067 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
1068 return JS_FALSE;
1069 }
1070 break;
1071
1072 case JSON_PARSE_STATE_OBJECT_PAIR :
1073 if (c == '"') {
1074 // we want to be waiting for a : when the string has been read
1075 *jp->statep = JSON_PARSE_STATE_OBJECT_IN_PAIR;
1076 if (!PushState(cx, jp, JSON_PARSE_STATE_STRING))
1077 return JS_FALSE;
1078 } else if (c == '}') {
1079 // pop off the object pair state and the object state
1080 if (!CloseObject(cx, jp) || !PopState(cx, jp) || !PopState(cx, jp))
1081 return JS_FALSE;
1082 } else if (c == ']' || !JS_ISXMLSPACE(c)) {
1083 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
1084 return JS_FALSE;
1085 }
1086 break;
1087
1088 case JSON_PARSE_STATE_OBJECT_IN_PAIR:
1089 if (c == ':') {
1090 *jp->statep = JSON_PARSE_STATE_VALUE;
1091 } else if (!JS_ISXMLSPACE(c)) {
1092 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
1093 return JS_FALSE;
1094 }
1095 break;
1096
1097 case JSON_PARSE_STATE_STRING:
1098 if (c == '"') {
1099 if (!PopState(cx, jp))
1100 return JS_FALSE;
1101 JSONDataType jdt;
1102 if (*jp->statep == JSON_PARSE_STATE_OBJECT_IN_PAIR) {
1103 jdt = JSON_DATA_KEYSTRING;
1104 } else {
1105 jdt = JSON_DATA_STRING;
1106 }
1107 if (!HandleData(cx, jp, jdt))
1108 return JS_FALSE;
1109 } else if (c == '\\') {
1110 *jp->statep = JSON_PARSE_STATE_STRING_ESCAPE;
1111 } else {
1112 if (!jp->buffer.append(c))
1113 return JS_FALSE;
1114 }
1115 break;
1116
1117 case JSON_PARSE_STATE_STRING_ESCAPE:
1118 switch (c) {
1119 case '"':
1120 case '\\':
1121 case '/':
1122 break;
1123 case 'b' : c = '\b'; break;
1124 case 'f' : c = '\f'; break;
1125 case 'n' : c = '\n'; break;
1126 case 'r' : c = '\r'; break;
1127 case 't' : c = '\t'; break;
1128 default :
1129 if (c == 'u') {
1130 jp->numHex = 0;
1131 jp->hexChar = 0;
1132 *jp->statep = JSON_PARSE_STATE_STRING_HEX;
1133 continue;
1134 } else {
1135 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
1136 return JS_FALSE;
1137 }
1138 }
1139
1140 if (!jp->buffer.append(c))
1141 return JS_FALSE;
1142 *jp->statep = JSON_PARSE_STATE_STRING;
1143 break;
1144
1145 case JSON_PARSE_STATE_STRING_HEX:
1146 if (('0' <= c) && (c <= '9')) {
1147 jp->hexChar = (jp->hexChar << 4) | (c - '0');
1148 } else if (('a' <= c) && (c <= 'f')) {
1149 jp->hexChar = (jp->hexChar << 4) | (c - 'a' + 0x0a);
1150 } else if (('A' <= c) && (c <= 'F')) {
1151 jp->hexChar = (jp->hexChar << 4) | (c - 'A' + 0x0a);
1152 } else {
1153 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
1154 return JS_FALSE;
1155 }
1156
1157 if (++(jp->numHex) == 4) {
1158 if (!jp->buffer.append(jp->hexChar))
1159 return JS_FALSE;
1160 jp->hexChar = 0;
1161 jp->numHex = 0;
1162 *jp->statep = JSON_PARSE_STATE_STRING;
1163 }
1164 break;
1165
1166 case JSON_PARSE_STATE_KEYWORD:
1167 if (JS7_ISLET(c)) {
1168 if (!jp->buffer.append(c))
1169 return JS_FALSE;
1170 } else {
1171 // this character isn't part of the keyword, process it again
1172 i--;
1173 if (!PopState(cx, jp))
1174 return JS_FALSE;
1175
1176 if (!HandleData(cx, jp, JSON_DATA_KEYWORD))
1177 return JS_FALSE;
1178 }
1179 break;
1180
1181 case JSON_PARSE_STATE_NUMBER:
1182 if (IsNumChar(c)) {
1183 if (!jp->buffer.append(c))
1184 return JS_FALSE;
1185 } else {
1186 // this character isn't part of the number, process it again
1187 i--;
1188 if (!PopState(cx, jp))
1189 return JS_FALSE;
1190 if (!HandleData(cx, jp, JSON_DATA_NUMBER))
1191 return JS_FALSE;
1192 }
1193 break;
1194
1195 case JSON_PARSE_STATE_FINISHED:
1196 if (!JS_ISXMLSPACE(c)) {
1197 // extra input
1198 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
1199 return JS_FALSE;
1200 }
1201 break;
1202
1203 default:
1204 JS_NOT_REACHED("Invalid JSON parser state");
1205 }
1206 }
1207
1208 return JS_TRUE;
1209 }
1210
1211 #if JS_HAS_TOSOURCE
1212 static JSBool
1213 json_toSource(JSContext *cx, uintN argc, jsval *vp)
1214 {
1215 *vp = ATOM_KEY(CLASS_ATOM(cx, JSON));
1216 return JS_TRUE;
1217 }
1218 #endif
1219
1220 static JSFunctionSpec json_static_methods[] = {
1221 #if JS_HAS_TOSOURCE
1222 JS_FN(js_toSource_str, json_toSource, 0, 0),
1223 #endif
1224 JS_FN("parse", js_json_parse, 1, 0),
1225 JS_FN("stringify", js_json_stringify, 1, 0),
1226 JS_FS_END
1227 };
1228
1229 JSObject *
1230 js_InitJSONClass(JSContext *cx, JSObject *obj)
1231 {
1232 JSObject *JSON;
1233
1234 JSON = JS_NewObject(cx, &js_JSONClass, NULL, obj);
1235 if (!JSON)
1236 return NULL;
1237 if (!JS_DefineProperty(cx, obj, js_JSON_str, OBJECT_TO_JSVAL(JSON),
1238 JS_PropertyStub, JS_PropertyStub, 0))
1239 return NULL;
1240
1241 if (!JS_DefineFunctions(cx, JSON, json_static_methods))
1242 return NULL;
1243
1244 return JSON;
1245 }

  ViewVC Help
Powered by ViewVC 1.1.24