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

Annotation of /trunk/js/json.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

1 siliconforks 507 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 siliconforks 460 * vim: set ts=8 sw=4 et tw=99:
3     *
4     * ***** BEGIN LICENSE BLOCK *****
5 siliconforks 332 * 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 siliconforks 507 #include <string.h>
42 siliconforks 332 #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 siliconforks 460 #include "jsfun.h"
50 siliconforks 332 #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 siliconforks 507 #include "jsstdint.h"
59 siliconforks 332 #include "jsutil.h"
60 siliconforks 460 #include "jsxml.h"
61 siliconforks 507 #include "jsvector.h"
62 siliconforks 332
63     #include "json.h"
64    
65 siliconforks 507 #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 siliconforks 332 JSClass js_JSONClass = {
87     js_JSON_str,
88     JSCLASS_HAS_CACHED_PROTO(JSProto_JSON),
89     JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
90 siliconforks 507 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, NULL,
91 siliconforks 332 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 siliconforks 460 jsval reviver = JSVAL_NULL;
100 siliconforks 507 JSAutoTempValueRooter tvr(cx, 1, &reviver);
101    
102 siliconforks 460 if (!JS_ConvertArguments(cx, argc, argv, "S / v", &s, &reviver))
103 siliconforks 332 return JS_FALSE;
104    
105     JSONParser *jp = js_BeginJSONParse(cx, vp);
106     JSBool ok = jp != NULL;
107     if (ok) {
108 siliconforks 507 const jschar *chars;
109     size_t length;
110     s->getCharsAndLength(chars, length);
111     ok = js_ConsumeJSONText(cx, jp, chars, length);
112 siliconforks 460 ok &= js_FinishJSONParse(cx, jp, reviver);
113 siliconforks 332 }
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 siliconforks 460 JSObject *replacer = NULL;
123     jsval space = JSVAL_NULL;
124 siliconforks 507 JSAutoTempValueRooter tvr(cx, replacer);
125     JSAutoTempValueRooter tvr2(cx, 1, &space);
126 siliconforks 460
127 siliconforks 332 // Must throw an Error if there isn't a first arg
128 siliconforks 460 if (!JS_ConvertArguments(cx, argc, argv, "v / o v", vp, &replacer, &space))
129 siliconforks 332 return JS_FALSE;
130    
131 siliconforks 507 JSCharBuffer cb(cx);
132 siliconforks 460
133 siliconforks 507 if (!js_Stringify(cx, vp, replacer, space, cb))
134 siliconforks 332 return JS_FALSE;
135    
136 siliconforks 460 // 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 siliconforks 507 if (!cb.empty()) {
140     JSString *str = js_NewStringFromCharBuffer(cx, cb);
141     if (!str)
142 siliconforks 460 return JS_FALSE;
143 siliconforks 507 *vp = STRING_TO_JSVAL(str);
144 siliconforks 460 } else {
145     *vp = JSVAL_VOID;
146 siliconforks 332 }
147    
148 siliconforks 460 return JS_TRUE;
149 siliconforks 332 }
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 siliconforks 460
157 siliconforks 332 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 siliconforks 460
162 siliconforks 332 return ok;
163     }
164    
165    
166 siliconforks 507 static const char quote = '\"';
167     static const char backslash = '\\';
168     static const char unicodeEscape[] = "\\u00";
169 siliconforks 332
170     static JSBool
171 siliconforks 507 write_string(JSContext *cx, JSCharBuffer &cb, const jschar *buf, uint32 len)
172 siliconforks 332 {
173 siliconforks 507 if (!cb.append(quote))
174 siliconforks 332 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 siliconforks 507 if (!cb.append(&buf[mark], i - mark) || !cb.append(backslash) ||
181     !cb.append(buf[i])) {
182 siliconforks 332 return JS_FALSE;
183     }
184     mark = i + 1;
185     } else if (buf[i] <= 31 || buf[i] == 127) {
186 siliconforks 507 if (!cb.append(&buf[mark], i - mark) ||
187     !js_AppendLiteral(cb, unicodeEscape)) {
188 siliconforks 332 return JS_FALSE;
189 siliconforks 507 }
190 siliconforks 399 char ubuf[3];
191     size_t len = JS_snprintf(ubuf, sizeof(ubuf), "%.2x", buf[i]);
192 siliconforks 332 JS_ASSERT(len == 2);
193 siliconforks 399 jschar wbuf[3];
194     size_t wbufSize = JS_ARRAY_LENGTH(wbuf);
195     if (!js_InflateStringToBuffer(cx, ubuf, len, wbuf, &wbufSize) ||
196 siliconforks 507 !cb.append(wbuf, wbufSize)) {
197 siliconforks 332 return JS_FALSE;
198 siliconforks 399 }
199 siliconforks 332 mark = i + 1;
200     }
201     }
202    
203 siliconforks 507 if (mark < len && !cb.append(&buf[mark], len - mark))
204 siliconforks 332 return JS_FALSE;
205    
206 siliconforks 507 return cb.append(quote);
207 siliconforks 460 }
208 siliconforks 332
209 siliconforks 460 class StringifyContext
210     {
211     public:
212 siliconforks 507 StringifyContext(JSContext *cx, JSCharBuffer &cb, JSObject *replacer)
213     : cb(cb), gap(cx), replacer(replacer), depth(0)
214     {}
215 siliconforks 460
216 siliconforks 507 JSCharBuffer &cb;
217     JSCharBuffer gap;
218 siliconforks 460 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 siliconforks 507 if (!scx->gap.empty()) {
231     if (!scx->cb.append('\n'))
232 siliconforks 460 return JS_FALSE;
233     for (uint32 i = 0; i < limit; i++) {
234 siliconforks 507 if (!scx->cb.append(scx->gap.begin(), scx->gap.end()))
235 siliconforks 460 return JS_FALSE;
236     }
237     }
238    
239 siliconforks 332 return JS_TRUE;
240     }
241    
242 siliconforks 460 static JSBool
243     JO(JSContext *cx, jsval *vp, StringifyContext *scx)
244 siliconforks 332 {
245 siliconforks 460 JSObject *obj = JSVAL_TO_OBJECT(*vp);
246 siliconforks 332
247 siliconforks 507 if (!scx->cb.append('{'))
248 siliconforks 332 return JS_FALSE;
249 siliconforks 399
250 siliconforks 460 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 siliconforks 332 JSObject *iterObj = NULL;
256 siliconforks 460 jsval *keySource = vp;
257     bool usingWhitelist = false;
258 siliconforks 332
259 siliconforks 460 // 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 siliconforks 332 }
265    
266 siliconforks 460 if (!js_ValueToIterator(cx, JSITER_ENUMERATE, keySource))
267     return JS_FALSE;
268     iterObj = JSVAL_TO_OBJECT(*keySource);
269 siliconforks 332
270     JSBool memberWritten = JS_FALSE;
271 siliconforks 460
272 siliconforks 507 bool ok = false;
273     while (true) {
274 siliconforks 332 outputValue = JSVAL_VOID;
275 siliconforks 507 if (!js_CallIteratorNext(cx, iterObj, &key))
276     goto error_break;
277 siliconforks 460 if (key == JSVAL_HOLE)
278     break;
279 siliconforks 332
280 siliconforks 460 jsuint index = 0;
281     if (usingWhitelist) {
282     // skip non-index properties
283     if (!js_IdIsIndex(key, &index))
284     continue;
285    
286     jsval newKey;
287 siliconforks 507 if (!scx->replacer->getProperty(cx, key, &newKey))
288     goto error_break;
289 siliconforks 460 key = newKey;
290     }
291    
292     JSString *ks;
293     if (JSVAL_IS_STRING(key)) {
294     ks = JSVAL_TO_STRING(key);
295 siliconforks 332 } else {
296 siliconforks 460 ks = js_ValueToString(cx, key);
297 siliconforks 507 if (!ks)
298     goto error_break;
299 siliconforks 460 }
300     JSAutoTempValueRooter keyStringRoot(cx, ks);
301 siliconforks 332
302 siliconforks 460 // 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 siliconforks 507 goto error_break;
309 siliconforks 332 }
310    
311 siliconforks 460 if (v != JSVAL_TRUE)
312     continue;
313    
314 siliconforks 507 if (!JS_GetPropertyById(cx, obj, id, &outputValue))
315     goto error_break;
316 siliconforks 332
317 siliconforks 507 if (JSVAL_IS_OBJECT(outputValue) && !js_TryJSON(cx, &outputValue))
318     goto error_break;
319 siliconforks 332
320 siliconforks 460 // 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 siliconforks 507 goto error_break;
324 siliconforks 460
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 siliconforks 332 continue;
330    
331     // output a comma unless this is the first member to write
332 siliconforks 507 if (memberWritten && !scx->cb.append(','))
333     goto error_break;
334 siliconforks 332 memberWritten = JS_TRUE;
335    
336 siliconforks 460 if (!WriteIndent(cx, scx, scx->depth))
337 siliconforks 507 goto error_break;
338 siliconforks 332
339 siliconforks 460 // Be careful below, this string is weakly rooted
340     JSString *s = js_ValueToString(cx, key);
341 siliconforks 507 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 siliconforks 460 }
352 siliconforks 507 }
353     ok = true;
354 siliconforks 332
355 siliconforks 507 error_break:
356 siliconforks 460 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 siliconforks 507 return scx->cb.append('}');
369 siliconforks 460 }
370    
371     static JSBool
372     JA(JSContext *cx, jsval *vp, StringifyContext *scx)
373     {
374     JSObject *obj = JSVAL_TO_OBJECT(*vp);
375    
376 siliconforks 507 if (!scx->cb.append('['))
377 siliconforks 460 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 siliconforks 507 if (!obj->getProperty(cx, id, &outputValue))
392 siliconforks 460 return JS_FALSE;
393    
394     if (!Str(cx, id, obj, scx, &outputValue))
395     return JS_FALSE;
396    
397     if (outputValue == JSVAL_VOID) {
398 siliconforks 507 if (!js_AppendLiteral(scx->cb, "null"))
399 siliconforks 460 return JS_FALSE;
400 siliconforks 332 }
401    
402 siliconforks 460 if (i < length - 1) {
403 siliconforks 507 if (!scx->cb.append(','))
404 siliconforks 460 return JS_FALSE;
405     if (!WriteIndent(cx, scx, scx->depth))
406     return JS_FALSE;
407     }
408     }
409 siliconforks 332
410 siliconforks 460 if (length != 0 && !WriteIndent(cx, scx, scx->depth - 1))
411     return JS_FALSE;
412 siliconforks 399
413 siliconforks 507 return scx->cb.append(']');
414 siliconforks 460 }
415 siliconforks 332
416 siliconforks 460 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 siliconforks 399
425 siliconforks 460 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 siliconforks 507 if (!holder->getProperty(cx, id, vp))
434 siliconforks 460 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 siliconforks 507 *vp = JSVAL_TO_OBJECT(*vp)->fslots[JSSLOT_PRIMITIVE_THIS];
447 siliconforks 460 }
448    
449     if (JSVAL_IS_STRING(*vp)) {
450 siliconforks 507 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 siliconforks 460 }
455    
456     if (JSVAL_IS_NULL(*vp)) {
457 siliconforks 507 return js_AppendLiteral(scx->cb, "null");
458 siliconforks 460 }
459    
460     if (JSVAL_IS_BOOLEAN(*vp)) {
461 siliconforks 507 return JSVAL_TO_BOOLEAN(*vp) ? js_AppendLiteral(scx->cb, "true")
462     : js_AppendLiteral(scx->cb, "false");
463 siliconforks 332 }
464    
465 siliconforks 460 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 siliconforks 507 return js_AppendLiteral(scx->cb, "null");
470 siliconforks 460 }
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 siliconforks 507 numStr = JS_dtostr(numBuf, sizeof numBuf, DTOSTR_STANDARD, 0, d);
475 siliconforks 460 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 siliconforks 507 return scx->cb.append(dstr, dbufSize);
486 siliconforks 460 }
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 siliconforks 507
498 siliconforks 460 *vp = JSVAL_VOID;
499     return JS_TRUE;
500     }
501    
502     static JSBool
503 siliconforks 507 InitializeGap(JSContext *cx, jsval space, JSCharBuffer &cb)
504 siliconforks 460 {
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 siliconforks 507 return js_ValueToCharBuffer(cx, space, cb);
509 siliconforks 460 }
510    
511     if (JSVAL_IS_STRING(space))
512 siliconforks 507 return js_ValueToCharBuffer(cx, space, cb);
513 siliconforks 460
514     if (JSVAL_IS_NUMBER(space)) {
515 siliconforks 507 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 siliconforks 460 return JS_FALSE;
521     }
522    
523     return JS_TRUE;
524 siliconforks 332 }
525    
526 siliconforks 460 JSBool
527     js_Stringify(JSContext *cx, jsval *vp, JSObject *replacer, jsval space,
528 siliconforks 507 JSCharBuffer &cb)
529 siliconforks 460 {
530 siliconforks 507 StringifyContext scx(cx, cb, replacer);
531     if (!InitializeGap(cx, space, scx.gap))
532 siliconforks 460 return JS_FALSE;
533    
534 siliconforks 507 JSObject *obj = js_NewObject(cx, &js_ObjectClass, NULL, NULL);
535 siliconforks 460 if (!obj)
536     return JS_FALSE;
537    
538 siliconforks 585 JSAutoTempValueRooter tvr(cx, obj);
539 siliconforks 507 if (!obj->defineProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.emptyAtom),
540     *vp, NULL, NULL, JSPROP_ENUMERATE)) {
541 siliconforks 460 return JS_FALSE;
542     }
543    
544     return Str(cx, ATOM_TO_JSID(cx->runtime->atomState.emptyAtom), obj, &scx, vp);
545     }
546    
547 siliconforks 332 // helper to determine whether a character could be part of a number
548 siliconforks 399 static JSBool IsNumChar(jschar c)
549 siliconforks 332 {
550     return ((c <= '9' && c >= '0') || c == '.' || c == '-' || c == '+' || c == 'e' || c == 'E');
551     }
552    
553 siliconforks 460 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 siliconforks 507
567     if (!holder->getProperty(cx, id, vp))
568 siliconforks 460 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 siliconforks 507
576 siliconforks 460 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 siliconforks 507 if (!obj->defineProperty(cx, index, propValue, NULL, NULL, JSPROP_ENUMERATE))
590 siliconforks 460 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 siliconforks 507 if (!obj->defineProperty(cx, idName, propValue, NULL, NULL, JSPROP_ENUMERATE))
608 siliconforks 460 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 siliconforks 507
636     JSObject *obj = js_NewObject(cx, &js_ObjectClass, NULL, NULL);
637 siliconforks 460 if (!obj)
638     return JS_FALSE;
639    
640     jsval v = OBJECT_TO_JSVAL(obj);
641     JSAutoTempValueRooter tvr(cx, 1, &v);
642 siliconforks 507 if (!obj->defineProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.emptyAtom),
643     *vp, NULL, NULL, JSPROP_ENUMERATE)) {
644 siliconforks 460 return JS_FALSE;
645     }
646    
647     return Walk(cx, ATOM_TO_JSID(cx->runtime->atomState.emptyAtom), obj, reviver, vp);
648     }
649    
650 siliconforks 332 JSONParser *
651     js_BeginJSONParse(JSContext *cx, jsval *rootVal)
652     {
653     if (!cx)
654     return NULL;
655    
656 siliconforks 399 JSObject *arr = js_NewArrayObject(cx, 0, NULL);
657 siliconforks 332 if (!arr)
658     return NULL;
659    
660 siliconforks 507 JSONParser *jp = cx->create<JSONParser>(cx);
661 siliconforks 332 if (!jp)
662 siliconforks 399 return NULL;
663 siliconforks 332
664     jp->objectStack = arr;
665 siliconforks 399 if (!js_AddRoot(cx, &jp->objectStack, "JSON parse stack"))
666 siliconforks 332 goto bad;
667    
668     jp->statep = jp->stateStack;
669     *jp->statep = JSON_PARSE_STATE_INIT;
670     jp->rootVal = rootVal;
671 siliconforks 399
672 siliconforks 460 return jp;
673 siliconforks 332
674     bad:
675 siliconforks 460 js_FinishJSONParse(cx, jp, JSVAL_NULL);
676 siliconforks 332 return NULL;
677     }
678    
679     JSBool
680 siliconforks 460 js_FinishJSONParse(JSContext *cx, JSONParser *jp, jsval reviver)
681 siliconforks 332 {
682     if (!jp)
683     return JS_TRUE;
684    
685 siliconforks 460 JSBool early_ok = JS_TRUE;
686 siliconforks 399
687 siliconforks 460 // 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 siliconforks 332 JSBool ok = *jp->statep == JSON_PARSE_STATE_FINISHED;
705 siliconforks 460 jsval *vp = jp->rootVal;
706 siliconforks 332
707 siliconforks 507 cx->destroy(jp);
708    
709 siliconforks 460 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 siliconforks 332 return ok;
721     }
722    
723     static JSBool
724 siliconforks 460 PushState(JSContext *cx, JSONParser *jp, JSONParserState state)
725 siliconforks 332 {
726 siliconforks 460 if (*jp->statep == JSON_PARSE_STATE_FINISHED) {
727     // extra input
728     JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
729 siliconforks 507 return JS_FALSE;
730 siliconforks 460 }
731 siliconforks 332
732     jp->statep++;
733 siliconforks 460 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 siliconforks 332
739     *jp->statep = state;
740    
741     return JS_TRUE;
742     }
743    
744     static JSBool
745 siliconforks 460 PopState(JSContext *cx, JSONParser *jp)
746 siliconforks 332 {
747     jp->statep--;
748     if (jp->statep < jp->stateStack) {
749     jp->statep = jp->stateStack;
750 siliconforks 460 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
751 siliconforks 332 return JS_FALSE;
752 siliconforks 399 }
753 siliconforks 332
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 siliconforks 399 ok = js_GetLengthProperty(cx, parent, &len);
767     if (ok) {
768 siliconforks 460 jsid index;
769     if (!js_IndexToId(cx, len, &index))
770     return JS_FALSE;
771 siliconforks 507 ok = parent->defineProperty(cx, index, value, NULL, NULL, JSPROP_ENUMERATE);
772 siliconforks 399 }
773 siliconforks 332 } else {
774 siliconforks 507 ok = JS_DefineUCProperty(cx, parent, jp->objectKey.begin(),
775     jp->objectKey.length(), value,
776 siliconforks 332 NULL, NULL, JSPROP_ENUMERATE);
777 siliconforks 507 jp->objectKey.clear();
778 siliconforks 332 }
779    
780     return ok;
781     }
782    
783     static JSBool
784     PushObject(JSContext *cx, JSONParser *jp, JSObject *obj)
785     {
786     jsuint len;
787 siliconforks 399 if (!js_GetLengthProperty(cx, jp->objectStack, &len))
788 siliconforks 332 return JS_FALSE;
789 siliconforks 460 if (len >= JSON_MAX_DEPTH) {
790     JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
791     return JS_FALSE;
792     }
793 siliconforks 332
794     jsval v = OBJECT_TO_JSVAL(obj);
795 siliconforks 399 JSAutoTempValueRooter tvr(cx, v);
796 siliconforks 332
797     // Check if this is the root object
798     if (len == 0) {
799 siliconforks 399 *jp->rootVal = v;
800     // This property must be enumerable to keep the array dense
801 siliconforks 507 if (!jp->objectStack->defineProperty(cx, INT_TO_JSID(0), *jp->rootVal,
802     NULL, NULL, JSPROP_ENUMERATE)) {
803 siliconforks 332 return JS_FALSE;
804 siliconforks 399 }
805 siliconforks 332 return JS_TRUE;
806     }
807    
808     jsval p;
809 siliconforks 507 if (!jp->objectStack->getProperty(cx, INT_TO_JSID(len - 1), &p))
810 siliconforks 332 return JS_FALSE;
811 siliconforks 399
812 siliconforks 332 JS_ASSERT(JSVAL_IS_OBJECT(p));
813     JSObject *parent = JSVAL_TO_OBJECT(p);
814 siliconforks 460 if (!PushValue(cx, jp, parent, v))
815 siliconforks 332 return JS_FALSE;
816    
817 siliconforks 399 // This property must be enumerable to keep the array dense
818 siliconforks 507 if (!jp->objectStack->defineProperty(cx, INT_TO_JSID(len), v,
819     NULL, NULL, JSPROP_ENUMERATE)) {
820 siliconforks 332 return JS_FALSE;
821 siliconforks 399 }
822 siliconforks 332
823     return JS_TRUE;
824     }
825    
826     static JSBool
827     OpenObject(JSContext *cx, JSONParser *jp)
828     {
829 siliconforks 507 JSObject *obj = js_NewObject(cx, &js_ObjectClass, NULL, NULL);
830 siliconforks 332 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 siliconforks 399 JSObject *arr = js_NewArrayObject(cx, 0, NULL);
841 siliconforks 332 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 siliconforks 399 if (!js_GetLengthProperty(cx, jp->objectStack, &len))
852 siliconforks 332 return JS_FALSE;
853 siliconforks 399 if (!js_SetLengthProperty(cx, jp->objectStack, len - 1))
854 siliconforks 332 return JS_FALSE;
855    
856     return JS_TRUE;
857     }
858    
859     static JSBool
860     CloseArray(JSContext *cx, JSONParser *jp)
861     {
862 siliconforks 399 return CloseObject(cx, jp);
863 siliconforks 332 }
864    
865     static JSBool
866 siliconforks 460 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 siliconforks 507 if (!jp->objectStack->getProperty(cx, INT_TO_JSID(len - 1), &o))
877 siliconforks 460 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 siliconforks 332 HandleNumber(JSContext *cx, JSONParser *jp, const jschar *buf, uint32 len)
890     {
891     const jschar *ep;
892 siliconforks 399 double val;
893 siliconforks 460 if (!js_strtod(cx, buf, buf + len, &ep, &val))
894 siliconforks 332 return JS_FALSE;
895 siliconforks 460 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 siliconforks 332
901 siliconforks 507 jsval numVal;
902 siliconforks 460 if (!JS_NewNumberValue(cx, val, &numVal))
903     return JS_FALSE;
904 siliconforks 507
905 siliconforks 460 return PushPrimitive(cx, jp, numVal);
906 siliconforks 332 }
907    
908     static JSBool
909     HandleString(JSContext *cx, JSONParser *jp, const jschar *buf, uint32 len)
910     {
911 siliconforks 399 JSString *str = js_NewStringCopyN(cx, buf, len);
912 siliconforks 460 if (!str)
913 siliconforks 332 return JS_FALSE;
914    
915 siliconforks 460 return PushPrimitive(cx, jp, STRING_TO_JSVAL(str));
916 siliconforks 332 }
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 siliconforks 460 if (tt != TOK_PRIMARY) {
924     // bad keyword
925     JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
926 siliconforks 332 return JS_FALSE;
927 siliconforks 460 }
928 siliconforks 332
929 siliconforks 460 if (buf[0] == 'n') {
930 siliconforks 332 keyword = JSVAL_NULL;
931 siliconforks 460 } else if (buf[0] == 't') {
932 siliconforks 332 keyword = JSVAL_TRUE;
933 siliconforks 460 } else if (buf[0] == 'f') {
934 siliconforks 332 keyword = JSVAL_FALSE;
935 siliconforks 460 } else {
936     JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
937 siliconforks 332 return JS_FALSE;
938 siliconforks 460 }
939 siliconforks 332
940 siliconforks 460 return PushPrimitive(cx, jp, keyword);
941 siliconforks 332 }
942    
943     static JSBool
944 siliconforks 399 HandleData(JSContext *cx, JSONParser *jp, JSONDataType type)
945 siliconforks 332 {
946 siliconforks 460 JSBool ok;
947 siliconforks 332
948 siliconforks 460 switch (type) {
949     case JSON_DATA_STRING:
950 siliconforks 507 ok = HandleString(cx, jp, jp->buffer.begin(), jp->buffer.length());
951 siliconforks 460 break;
952 siliconforks 399
953 siliconforks 460 case JSON_DATA_KEYSTRING:
954 siliconforks 507 ok = jp->objectKey.append(jp->buffer.begin(), jp->buffer.end());
955 siliconforks 460 break;
956 siliconforks 332
957 siliconforks 460 case JSON_DATA_NUMBER:
958 siliconforks 507 ok = HandleNumber(cx, jp, jp->buffer.begin(), jp->buffer.length());
959 siliconforks 460 break;
960 siliconforks 332
961 siliconforks 460 default:
962     JS_ASSERT(type == JSON_DATA_KEYWORD);
963 siliconforks 507 ok = HandleKeyword(cx, jp, jp->buffer.begin(), jp->buffer.length());
964 siliconforks 460 break;
965     }
966 siliconforks 332
967 siliconforks 507 if (ok)
968     jp->buffer.clear();
969 siliconforks 460 return ok;
970 siliconforks 332 }
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 siliconforks 460 PushState(cx, jp, JSON_PARSE_STATE_VALUE);
979 siliconforks 332 }
980    
981 siliconforks 399 for (i = 0; i < len; i++) {
982 siliconforks 332 jschar c = data[i];
983     switch (*jp->statep) {
984 siliconforks 460 case JSON_PARSE_STATE_VALUE:
985     if (c == ']') {
986     // empty array
987     if (!PopState(cx, jp))
988     return JS_FALSE;
989 siliconforks 332
990 siliconforks 460 if (*jp->statep != JSON_PARSE_STATE_ARRAY) {
991     JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
992     return JS_FALSE;
993 siliconforks 332 }
994    
995 siliconforks 460 if (!CloseArray(cx, jp) || !PopState(cx, jp))
996     return JS_FALSE;
997 siliconforks 332
998     break;
999 siliconforks 460 }
1000 siliconforks 332
1001 siliconforks 460 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 siliconforks 332
1007 siliconforks 460 if (c == '"') {
1008     *jp->statep = JSON_PARSE_STATE_STRING;
1009 siliconforks 332 break;
1010 siliconforks 460 }
1011 siliconforks 399
1012 siliconforks 460 if (IsNumChar(c)) {
1013     *jp->statep = JSON_PARSE_STATE_NUMBER;
1014 siliconforks 507 if (!jp->buffer.append(c))
1015     return JS_FALSE;
1016 siliconforks 332 break;
1017 siliconforks 460 }
1018 siliconforks 399
1019 siliconforks 460 if (JS7_ISLET(c)) {
1020     *jp->statep = JSON_PARSE_STATE_KEYWORD;
1021 siliconforks 507 if (!jp->buffer.append(c))
1022     return JS_FALSE;
1023 siliconforks 332 break;
1024 siliconforks 460 }
1025 siliconforks 399
1026 siliconforks 460 // 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 siliconforks 399
1042 siliconforks 460 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 siliconforks 332
1055 siliconforks 460 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 siliconforks 399
1068 siliconforks 460 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 siliconforks 332
1084 siliconforks 460 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 siliconforks 399
1093 siliconforks 460 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 siliconforks 332 } else {
1101 siliconforks 460 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 siliconforks 507 if (!jp->buffer.append(c))
1109     return JS_FALSE;
1110 siliconforks 460 }
1111     break;
1112 siliconforks 399
1113 siliconforks 460 case JSON_PARSE_STATE_STRING_ESCAPE:
1114     switch (c) {
1115     case '"':
1116     case '\\':
1117     case '/':
1118 siliconforks 332 break;
1119 siliconforks 460 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 siliconforks 332 } else {
1131 siliconforks 460 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
1132     return JS_FALSE;
1133 siliconforks 332 }
1134 siliconforks 460 }
1135 siliconforks 399
1136 siliconforks 507 if (!jp->buffer.append(c))
1137     return JS_FALSE;
1138 siliconforks 460 *jp->statep = JSON_PARSE_STATE_STRING;
1139     break;
1140 siliconforks 332
1141 siliconforks 460 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 siliconforks 507
1153 siliconforks 460 if (++(jp->numHex) == 4) {
1154 siliconforks 507 if (!jp->buffer.append(jp->hexChar))
1155     return JS_FALSE;
1156 siliconforks 460 jp->hexChar = 0;
1157     jp->numHex = 0;
1158     *jp->statep = JSON_PARSE_STATE_STRING;
1159     }
1160     break;
1161 siliconforks 399
1162 siliconforks 460 case JSON_PARSE_STATE_KEYWORD:
1163     if (JS7_ISLET(c)) {
1164 siliconforks 507 if (!jp->buffer.append(c))
1165     return JS_FALSE;
1166 siliconforks 460 } 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 siliconforks 507
1172 siliconforks 460 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 siliconforks 507 if (!jp->buffer.append(c))
1180     return JS_FALSE;
1181 siliconforks 460 } 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 siliconforks 332 }
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 siliconforks 460 JS_FN("parse", js_json_parse, 1, 0),
1221     JS_FN("stringify", js_json_stringify, 1, 0),
1222 siliconforks 332 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 siliconforks 399 JS_PropertyStub, JS_PropertyStub, 0))
1235 siliconforks 332 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