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

Contents of /trunk/js/json.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 585 - (show annotations)
Sun Sep 12 15:13:23 2010 UTC (8 years, 9 months ago) by siliconforks
File size: 36633 byte(s)
Update to SpiderMonkey from Firefox 3.6.9.

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 StringifyContext scx(cx, cb, replacer);
531 if (!InitializeGap(cx, space, scx.gap))
532 return JS_FALSE;
533
534 JSObject *obj = js_NewObject(cx, &js_ObjectClass, NULL, NULL);
535 if (!obj)
536 return JS_FALSE;
537
538 JSAutoTempValueRooter tvr(cx, obj);
539 if (!obj->defineProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.emptyAtom),
540 *vp, NULL, NULL, JSPROP_ENUMERATE)) {
541 return JS_FALSE;
542 }
543
544 return Str(cx, ATOM_TO_JSID(cx->runtime->atomState.emptyAtom), obj, &scx, vp);
545 }
546
547 // helper to determine whether a character could be part of a number
548 static JSBool IsNumChar(jschar c)
549 {
550 return ((c <= '9' && c >= '0') || c == '.' || c == '-' || c == '+' || c == 'e' || c == 'E');
551 }
552
553 static JSBool HandleData(JSContext *cx, JSONParser *jp, JSONDataType type);
554 static JSBool PopState(JSContext *cx, JSONParser *jp);
555
556 static JSBool
557 DestroyIdArrayOnError(JSContext *cx, JSIdArray *ida) {
558 JS_DestroyIdArray(cx, ida);
559 return JS_FALSE;
560 }
561
562 static JSBool
563 Walk(JSContext *cx, jsid id, JSObject *holder, jsval reviver, jsval *vp)
564 {
565 JS_CHECK_RECURSION(cx, return JS_FALSE);
566
567 if (!holder->getProperty(cx, id, vp))
568 return JS_FALSE;
569
570 JSObject *obj;
571
572 if (!JSVAL_IS_PRIMITIVE(*vp) && !js_IsCallable(obj = JSVAL_TO_OBJECT(*vp), cx)) {
573 jsval propValue = JSVAL_NULL;
574 JSAutoTempValueRooter tvr(cx, 1, &propValue);
575
576 if(OBJ_IS_ARRAY(cx, obj)) {
577 jsuint length = 0;
578 if (!js_GetLengthProperty(cx, obj, &length))
579 return JS_FALSE;
580
581 for (jsuint i = 0; i < length; i++) {
582 jsid index;
583 if (!js_IndexToId(cx, i, &index))
584 return JS_FALSE;
585
586 if (!Walk(cx, index, obj, reviver, &propValue))
587 return JS_FALSE;
588
589 if (!obj->defineProperty(cx, index, propValue, NULL, NULL, JSPROP_ENUMERATE))
590 return JS_FALSE;
591 }
592 } else {
593 JSIdArray *ida = JS_Enumerate(cx, obj);
594 if (!ida)
595 return JS_FALSE;
596
597 JSAutoTempValueRooter idaroot(cx, JS_ARRAY_LENGTH(ida), (jsval*)ida);
598
599 for(jsint i = 0; i < ida->length; i++) {
600 jsid idName = ida->vector[i];
601 if (!Walk(cx, idName, obj, reviver, &propValue))
602 return DestroyIdArrayOnError(cx, ida);
603 if (propValue == JSVAL_VOID) {
604 if (!js_DeleteProperty(cx, obj, idName, &propValue))
605 return DestroyIdArrayOnError(cx, ida);
606 } else {
607 if (!obj->defineProperty(cx, idName, propValue, NULL, NULL, JSPROP_ENUMERATE))
608 return DestroyIdArrayOnError(cx, ida);
609 }
610 }
611
612 JS_DestroyIdArray(cx, ida);
613 }
614 }
615
616 // return reviver.call(holder, key, value);
617 jsval value = *vp;
618 JSString *key = js_ValueToString(cx, ID_TO_VALUE(id));
619 if (!key)
620 return JS_FALSE;
621
622 jsval vec[2] = {STRING_TO_JSVAL(key), value};
623 jsval reviverResult;
624 if (!JS_CallFunctionValue(cx, holder, reviver, 2, vec, &reviverResult))
625 return JS_FALSE;
626
627 *vp = reviverResult;
628
629 return JS_TRUE;
630 }
631
632 static JSBool
633 Revive(JSContext *cx, jsval reviver, jsval *vp)
634 {
635
636 JSObject *obj = js_NewObject(cx, &js_ObjectClass, NULL, NULL);
637 if (!obj)
638 return JS_FALSE;
639
640 jsval v = OBJECT_TO_JSVAL(obj);
641 JSAutoTempValueRooter tvr(cx, 1, &v);
642 if (!obj->defineProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.emptyAtom),
643 *vp, NULL, NULL, JSPROP_ENUMERATE)) {
644 return JS_FALSE;
645 }
646
647 return Walk(cx, ATOM_TO_JSID(cx->runtime->atomState.emptyAtom), obj, reviver, vp);
648 }
649
650 JSONParser *
651 js_BeginJSONParse(JSContext *cx, jsval *rootVal)
652 {
653 if (!cx)
654 return NULL;
655
656 JSObject *arr = js_NewArrayObject(cx, 0, NULL);
657 if (!arr)
658 return NULL;
659
660 JSONParser *jp = cx->create<JSONParser>(cx);
661 if (!jp)
662 return NULL;
663
664 jp->objectStack = arr;
665 if (!js_AddRoot(cx, &jp->objectStack, "JSON parse stack"))
666 goto bad;
667
668 jp->statep = jp->stateStack;
669 *jp->statep = JSON_PARSE_STATE_INIT;
670 jp->rootVal = rootVal;
671
672 return jp;
673
674 bad:
675 js_FinishJSONParse(cx, jp, JSVAL_NULL);
676 return NULL;
677 }
678
679 JSBool
680 js_FinishJSONParse(JSContext *cx, JSONParser *jp, jsval reviver)
681 {
682 if (!jp)
683 return JS_TRUE;
684
685 JSBool early_ok = JS_TRUE;
686
687 // Check for unprocessed primitives at the root. This doesn't happen for
688 // strings because a closing quote triggers value processing.
689 if ((jp->statep - jp->stateStack) == 1) {
690 if (*jp->statep == JSON_PARSE_STATE_KEYWORD) {
691 early_ok = HandleData(cx, jp, JSON_DATA_KEYWORD);
692 if (early_ok)
693 PopState(cx, jp);
694 } else if (*jp->statep == JSON_PARSE_STATE_NUMBER) {
695 early_ok = HandleData(cx, jp, JSON_DATA_NUMBER);
696 if (early_ok)
697 PopState(cx, jp);
698 }
699 }
700
701 /* This internal API is infallible, in spite of its JSBool return type. */
702 js_RemoveRoot(cx->runtime, &jp->objectStack);
703
704 JSBool ok = *jp->statep == JSON_PARSE_STATE_FINISHED;
705 jsval *vp = jp->rootVal;
706
707 cx->destroy(jp);
708
709 if (!early_ok)
710 return JS_FALSE;
711
712 if (!ok) {
713 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
714 return JS_FALSE;
715 }
716
717 if (!JSVAL_IS_PRIMITIVE(reviver) && js_IsCallable(JSVAL_TO_OBJECT(reviver), cx))
718 ok = Revive(cx, reviver, vp);
719
720 return ok;
721 }
722
723 static JSBool
724 PushState(JSContext *cx, JSONParser *jp, JSONParserState state)
725 {
726 if (*jp->statep == JSON_PARSE_STATE_FINISHED) {
727 // extra input
728 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
729 return JS_FALSE;
730 }
731
732 jp->statep++;
733 if ((uint32)(jp->statep - jp->stateStack) >= JS_ARRAY_LENGTH(jp->stateStack)) {
734 // too deep
735 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
736 return JS_FALSE;
737 }
738
739 *jp->statep = state;
740
741 return JS_TRUE;
742 }
743
744 static JSBool
745 PopState(JSContext *cx, JSONParser *jp)
746 {
747 jp->statep--;
748 if (jp->statep < jp->stateStack) {
749 jp->statep = jp->stateStack;
750 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
751 return JS_FALSE;
752 }
753
754 if (*jp->statep == JSON_PARSE_STATE_INIT)
755 *jp->statep = JSON_PARSE_STATE_FINISHED;
756
757 return JS_TRUE;
758 }
759
760 static JSBool
761 PushValue(JSContext *cx, JSONParser *jp, JSObject *parent, jsval value)
762 {
763 JSBool ok;
764 if (OBJ_IS_ARRAY(cx, parent)) {
765 jsuint len;
766 ok = js_GetLengthProperty(cx, parent, &len);
767 if (ok) {
768 jsid index;
769 if (!js_IndexToId(cx, len, &index))
770 return JS_FALSE;
771 ok = parent->defineProperty(cx, index, value, NULL, NULL, JSPROP_ENUMERATE);
772 }
773 } else {
774 ok = JS_DefineUCProperty(cx, parent, jp->objectKey.begin(),
775 jp->objectKey.length(), value,
776 NULL, NULL, JSPROP_ENUMERATE);
777 jp->objectKey.clear();
778 }
779
780 return ok;
781 }
782
783 static JSBool
784 PushObject(JSContext *cx, JSONParser *jp, JSObject *obj)
785 {
786 jsuint len;
787 if (!js_GetLengthProperty(cx, jp->objectStack, &len))
788 return JS_FALSE;
789 if (len >= JSON_MAX_DEPTH) {
790 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
791 return JS_FALSE;
792 }
793
794 jsval v = OBJECT_TO_JSVAL(obj);
795 JSAutoTempValueRooter tvr(cx, v);
796
797 // Check if this is the root object
798 if (len == 0) {
799 *jp->rootVal = v;
800 // This property must be enumerable to keep the array dense
801 if (!jp->objectStack->defineProperty(cx, INT_TO_JSID(0), *jp->rootVal,
802 NULL, NULL, JSPROP_ENUMERATE)) {
803 return JS_FALSE;
804 }
805 return JS_TRUE;
806 }
807
808 jsval p;
809 if (!jp->objectStack->getProperty(cx, INT_TO_JSID(len - 1), &p))
810 return JS_FALSE;
811
812 JS_ASSERT(JSVAL_IS_OBJECT(p));
813 JSObject *parent = JSVAL_TO_OBJECT(p);
814 if (!PushValue(cx, jp, parent, v))
815 return JS_FALSE;
816
817 // This property must be enumerable to keep the array dense
818 if (!jp->objectStack->defineProperty(cx, INT_TO_JSID(len), v,
819 NULL, NULL, JSPROP_ENUMERATE)) {
820 return JS_FALSE;
821 }
822
823 return JS_TRUE;
824 }
825
826 static JSBool
827 OpenObject(JSContext *cx, JSONParser *jp)
828 {
829 JSObject *obj = js_NewObject(cx, &js_ObjectClass, NULL, NULL);
830 if (!obj)
831 return JS_FALSE;
832
833 return PushObject(cx, jp, obj);
834 }
835
836 static JSBool
837 OpenArray(JSContext *cx, JSONParser *jp)
838 {
839 // Add an array to an existing array or object
840 JSObject *arr = js_NewArrayObject(cx, 0, NULL);
841 if (!arr)
842 return JS_FALSE;
843
844 return PushObject(cx, jp, arr);
845 }
846
847 static JSBool
848 CloseObject(JSContext *cx, JSONParser *jp)
849 {
850 jsuint len;
851 if (!js_GetLengthProperty(cx, jp->objectStack, &len))
852 return JS_FALSE;
853 if (!js_SetLengthProperty(cx, jp->objectStack, len - 1))
854 return JS_FALSE;
855
856 return JS_TRUE;
857 }
858
859 static JSBool
860 CloseArray(JSContext *cx, JSONParser *jp)
861 {
862 return CloseObject(cx, jp);
863 }
864
865 static JSBool
866 PushPrimitive(JSContext *cx, JSONParser *jp, jsval value)
867 {
868 JSAutoTempValueRooter tvr(cx, 1, &value);
869
870 jsuint len;
871 if (!js_GetLengthProperty(cx, jp->objectStack, &len))
872 return JS_FALSE;
873
874 if (len > 0) {
875 jsval o;
876 if (!jp->objectStack->getProperty(cx, INT_TO_JSID(len - 1), &o))
877 return JS_FALSE;
878
879 JS_ASSERT(!JSVAL_IS_PRIMITIVE(o));
880 return PushValue(cx, jp, JSVAL_TO_OBJECT(o), value);
881 }
882
883 // root value must be primitive
884 *jp->rootVal = value;
885 return JS_TRUE;
886 }
887
888 static JSBool
889 HandleNumber(JSContext *cx, JSONParser *jp, const jschar *buf, uint32 len)
890 {
891 const jschar *ep;
892 double val;
893 if (!js_strtod(cx, buf, buf + len, &ep, &val))
894 return JS_FALSE;
895 if (ep != buf + len) {
896 // bad number input
897 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
898 return JS_FALSE;
899 }
900
901 jsval numVal;
902 if (!JS_NewNumberValue(cx, val, &numVal))
903 return JS_FALSE;
904
905 return PushPrimitive(cx, jp, numVal);
906 }
907
908 static JSBool
909 HandleString(JSContext *cx, JSONParser *jp, const jschar *buf, uint32 len)
910 {
911 JSString *str = js_NewStringCopyN(cx, buf, len);
912 if (!str)
913 return JS_FALSE;
914
915 return PushPrimitive(cx, jp, STRING_TO_JSVAL(str));
916 }
917
918 static JSBool
919 HandleKeyword(JSContext *cx, JSONParser *jp, const jschar *buf, uint32 len)
920 {
921 jsval keyword;
922 JSTokenType tt = js_CheckKeyword(buf, len);
923 if (tt != TOK_PRIMARY) {
924 // bad keyword
925 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
926 return JS_FALSE;
927 }
928
929 if (buf[0] == 'n') {
930 keyword = JSVAL_NULL;
931 } else if (buf[0] == 't') {
932 keyword = JSVAL_TRUE;
933 } else if (buf[0] == 'f') {
934 keyword = JSVAL_FALSE;
935 } else {
936 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
937 return JS_FALSE;
938 }
939
940 return PushPrimitive(cx, jp, keyword);
941 }
942
943 static JSBool
944 HandleData(JSContext *cx, JSONParser *jp, JSONDataType type)
945 {
946 JSBool ok;
947
948 switch (type) {
949 case JSON_DATA_STRING:
950 ok = HandleString(cx, jp, jp->buffer.begin(), jp->buffer.length());
951 break;
952
953 case JSON_DATA_KEYSTRING:
954 ok = jp->objectKey.append(jp->buffer.begin(), jp->buffer.end());
955 break;
956
957 case JSON_DATA_NUMBER:
958 ok = HandleNumber(cx, jp, jp->buffer.begin(), jp->buffer.length());
959 break;
960
961 default:
962 JS_ASSERT(type == JSON_DATA_KEYWORD);
963 ok = HandleKeyword(cx, jp, jp->buffer.begin(), jp->buffer.length());
964 break;
965 }
966
967 if (ok)
968 jp->buffer.clear();
969 return ok;
970 }
971
972 JSBool
973 js_ConsumeJSONText(JSContext *cx, JSONParser *jp, const jschar *data, uint32 len)
974 {
975 uint32 i;
976
977 if (*jp->statep == JSON_PARSE_STATE_INIT) {
978 PushState(cx, jp, JSON_PARSE_STATE_VALUE);
979 }
980
981 for (i = 0; i < len; i++) {
982 jschar c = data[i];
983 switch (*jp->statep) {
984 case JSON_PARSE_STATE_VALUE:
985 if (c == ']') {
986 // empty array
987 if (!PopState(cx, jp))
988 return JS_FALSE;
989
990 if (*jp->statep != JSON_PARSE_STATE_ARRAY) {
991 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
992 return JS_FALSE;
993 }
994
995 if (!CloseArray(cx, jp) || !PopState(cx, jp))
996 return JS_FALSE;
997
998 break;
999 }
1000
1001 if (c == '}') {
1002 // we should only find these in OBJECT_KEY state
1003 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
1004 return JS_FALSE;
1005 }
1006
1007 if (c == '"') {
1008 *jp->statep = JSON_PARSE_STATE_STRING;
1009 break;
1010 }
1011
1012 if (IsNumChar(c)) {
1013 *jp->statep = JSON_PARSE_STATE_NUMBER;
1014 if (!jp->buffer.append(c))
1015 return JS_FALSE;
1016 break;
1017 }
1018
1019 if (JS7_ISLET(c)) {
1020 *jp->statep = JSON_PARSE_STATE_KEYWORD;
1021 if (!jp->buffer.append(c))
1022 return JS_FALSE;
1023 break;
1024 }
1025
1026 // fall through in case the value is an object or array
1027 case JSON_PARSE_STATE_OBJECT_VALUE:
1028 if (c == '{') {
1029 *jp->statep = JSON_PARSE_STATE_OBJECT;
1030 if (!OpenObject(cx, jp) || !PushState(cx, jp, JSON_PARSE_STATE_OBJECT_PAIR))
1031 return JS_FALSE;
1032 } else if (c == '[') {
1033 *jp->statep = JSON_PARSE_STATE_ARRAY;
1034 if (!OpenArray(cx, jp) || !PushState(cx, jp, JSON_PARSE_STATE_VALUE))
1035 return JS_FALSE;
1036 } else if (!JS_ISXMLSPACE(c)) {
1037 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
1038 return JS_FALSE;
1039 }
1040 break;
1041
1042 case JSON_PARSE_STATE_OBJECT:
1043 if (c == '}') {
1044 if (!CloseObject(cx, jp) || !PopState(cx, jp))
1045 return JS_FALSE;
1046 } else if (c == ',') {
1047 if (!PushState(cx, jp, JSON_PARSE_STATE_OBJECT_PAIR))
1048 return JS_FALSE;
1049 } else if (c == ']' || !JS_ISXMLSPACE(c)) {
1050 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
1051 return JS_FALSE;
1052 }
1053 break;
1054
1055 case JSON_PARSE_STATE_ARRAY :
1056 if (c == ']') {
1057 if (!CloseArray(cx, jp) || !PopState(cx, jp))
1058 return JS_FALSE;
1059 } else if (c == ',') {
1060 if (!PushState(cx, jp, JSON_PARSE_STATE_VALUE))
1061 return JS_FALSE;
1062 } else if (!JS_ISXMLSPACE(c)) {
1063 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
1064 return JS_FALSE;
1065 }
1066 break;
1067
1068 case JSON_PARSE_STATE_OBJECT_PAIR :
1069 if (c == '"') {
1070 // we want to be waiting for a : when the string has been read
1071 *jp->statep = JSON_PARSE_STATE_OBJECT_IN_PAIR;
1072 if (!PushState(cx, jp, JSON_PARSE_STATE_STRING))
1073 return JS_FALSE;
1074 } else if (c == '}') {
1075 // pop off the object pair state and the object state
1076 if (!CloseObject(cx, jp) || !PopState(cx, jp) || !PopState(cx, jp))
1077 return JS_FALSE;
1078 } else if (c == ']' || !JS_ISXMLSPACE(c)) {
1079 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
1080 return JS_FALSE;
1081 }
1082 break;
1083
1084 case JSON_PARSE_STATE_OBJECT_IN_PAIR:
1085 if (c == ':') {
1086 *jp->statep = JSON_PARSE_STATE_VALUE;
1087 } else if (!JS_ISXMLSPACE(c)) {
1088 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
1089 return JS_FALSE;
1090 }
1091 break;
1092
1093 case JSON_PARSE_STATE_STRING:
1094 if (c == '"') {
1095 if (!PopState(cx, jp))
1096 return JS_FALSE;
1097 JSONDataType jdt;
1098 if (*jp->statep == JSON_PARSE_STATE_OBJECT_IN_PAIR) {
1099 jdt = JSON_DATA_KEYSTRING;
1100 } else {
1101 jdt = JSON_DATA_STRING;
1102 }
1103 if (!HandleData(cx, jp, jdt))
1104 return JS_FALSE;
1105 } else if (c == '\\') {
1106 *jp->statep = JSON_PARSE_STATE_STRING_ESCAPE;
1107 } else {
1108 if (!jp->buffer.append(c))
1109 return JS_FALSE;
1110 }
1111 break;
1112
1113 case JSON_PARSE_STATE_STRING_ESCAPE:
1114 switch (c) {
1115 case '"':
1116 case '\\':
1117 case '/':
1118 break;
1119 case 'b' : c = '\b'; break;
1120 case 'f' : c = '\f'; break;
1121 case 'n' : c = '\n'; break;
1122 case 'r' : c = '\r'; break;
1123 case 't' : c = '\t'; break;
1124 default :
1125 if (c == 'u') {
1126 jp->numHex = 0;
1127 jp->hexChar = 0;
1128 *jp->statep = JSON_PARSE_STATE_STRING_HEX;
1129 continue;
1130 } else {
1131 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
1132 return JS_FALSE;
1133 }
1134 }
1135
1136 if (!jp->buffer.append(c))
1137 return JS_FALSE;
1138 *jp->statep = JSON_PARSE_STATE_STRING;
1139 break;
1140
1141 case JSON_PARSE_STATE_STRING_HEX:
1142 if (('0' <= c) && (c <= '9')) {
1143 jp->hexChar = (jp->hexChar << 4) | (c - '0');
1144 } else if (('a' <= c) && (c <= 'f')) {
1145 jp->hexChar = (jp->hexChar << 4) | (c - 'a' + 0x0a);
1146 } else if (('A' <= c) && (c <= 'F')) {
1147 jp->hexChar = (jp->hexChar << 4) | (c - 'A' + 0x0a);
1148 } else {
1149 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
1150 return JS_FALSE;
1151 }
1152
1153 if (++(jp->numHex) == 4) {
1154 if (!jp->buffer.append(jp->hexChar))
1155 return JS_FALSE;
1156 jp->hexChar = 0;
1157 jp->numHex = 0;
1158 *jp->statep = JSON_PARSE_STATE_STRING;
1159 }
1160 break;
1161
1162 case JSON_PARSE_STATE_KEYWORD:
1163 if (JS7_ISLET(c)) {
1164 if (!jp->buffer.append(c))
1165 return JS_FALSE;
1166 } else {
1167 // this character isn't part of the keyword, process it again
1168 i--;
1169 if (!PopState(cx, jp))
1170 return JS_FALSE;
1171
1172 if (!HandleData(cx, jp, JSON_DATA_KEYWORD))
1173 return JS_FALSE;
1174 }
1175 break;
1176
1177 case JSON_PARSE_STATE_NUMBER:
1178 if (IsNumChar(c)) {
1179 if (!jp->buffer.append(c))
1180 return JS_FALSE;
1181 } else {
1182 // this character isn't part of the number, process it again
1183 i--;
1184 if (!PopState(cx, jp))
1185 return JS_FALSE;
1186 if (!HandleData(cx, jp, JSON_DATA_NUMBER))
1187 return JS_FALSE;
1188 }
1189 break;
1190
1191 case JSON_PARSE_STATE_FINISHED:
1192 if (!JS_ISXMLSPACE(c)) {
1193 // extra input
1194 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
1195 return JS_FALSE;
1196 }
1197 break;
1198
1199 default:
1200 JS_NOT_REACHED("Invalid JSON parser state");
1201 }
1202 }
1203
1204 return JS_TRUE;
1205 }
1206
1207 #if JS_HAS_TOSOURCE
1208 static JSBool
1209 json_toSource(JSContext *cx, uintN argc, jsval *vp)
1210 {
1211 *vp = ATOM_KEY(CLASS_ATOM(cx, JSON));
1212 return JS_TRUE;
1213 }
1214 #endif
1215
1216 static JSFunctionSpec json_static_methods[] = {
1217 #if JS_HAS_TOSOURCE
1218 JS_FN(js_toSource_str, json_toSource, 0, 0),
1219 #endif
1220 JS_FN("parse", js_json_parse, 1, 0),
1221 JS_FN("stringify", js_json_stringify, 1, 0),
1222 JS_FS_END
1223 };
1224
1225 JSObject *
1226 js_InitJSONClass(JSContext *cx, JSObject *obj)
1227 {
1228 JSObject *JSON;
1229
1230 JSON = JS_NewObject(cx, &js_JSONClass, NULL, obj);
1231 if (!JSON)
1232 return NULL;
1233 if (!JS_DefineProperty(cx, obj, js_JSON_str, OBJECT_TO_JSVAL(JSON),
1234 JS_PropertyStub, JS_PropertyStub, 0))
1235 return NULL;
1236
1237 if (!JS_DefineFunctions(cx, JSON, json_static_methods))
1238 return NULL;
1239
1240 return JSON;
1241 }

  ViewVC Help
Powered by ViewVC 1.1.24