2009-06-10 18:29:44 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2009-04-21 12:09:27 -07:00
|
|
|
* vim: set ts=8 sw=4 et tw=99:
|
|
|
|
*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
2008-10-06 13:54:12 -07:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is SpiderMonkey JSON.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Mozilla Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998-1999
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Robert Sayre <sayrer@gmail.com>
|
2009-11-30 08:15:10 -08:00
|
|
|
* Dave Camp <dcamp@mozilla.com>
|
2008-10-06 13:54:12 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2009-07-27 21:10:12 -07:00
|
|
|
#include <string.h>
|
2008-10-06 13:54:12 -07:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsarena.h"
|
|
|
|
#include "jsarray.h"
|
|
|
|
#include "jsatom.h"
|
|
|
|
#include "jsbool.h"
|
|
|
|
#include "jscntxt.h"
|
|
|
|
#include "jsdtoa.h"
|
2009-05-07 13:28:21 -07:00
|
|
|
#include "jsfun.h"
|
2008-10-06 13:54:12 -07:00
|
|
|
#include "jsinterp.h"
|
|
|
|
#include "jsiter.h"
|
|
|
|
#include "jsnum.h"
|
|
|
|
#include "jsobj.h"
|
|
|
|
#include "jsprf.h"
|
|
|
|
#include "jsscan.h"
|
|
|
|
#include "jsstr.h"
|
|
|
|
#include "jstypes.h"
|
2009-03-18 11:38:16 -07:00
|
|
|
#include "jsstdint.h"
|
2008-10-06 13:54:12 -07:00
|
|
|
#include "jsutil.h"
|
2009-05-07 13:28:21 -07:00
|
|
|
#include "jsxml.h"
|
2009-08-13 17:22:55 -07:00
|
|
|
#include "jsvector.h"
|
2008-10-06 13:54:12 -07:00
|
|
|
|
|
|
|
#include "json.h"
|
|
|
|
|
2009-07-13 14:55:04 -07:00
|
|
|
#include "jsatominlines.h"
|
2010-04-10 16:16:35 -07:00
|
|
|
#include "jsobjinlines.h"
|
2009-07-13 14:55:04 -07:00
|
|
|
|
2010-01-11 09:52:21 -08:00
|
|
|
using namespace js;
|
|
|
|
|
2009-09-23 11:16:30 -07:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable:4351)
|
|
|
|
#endif
|
|
|
|
|
2009-08-13 17:22:55 -07:00
|
|
|
struct JSONParser
|
|
|
|
{
|
|
|
|
JSONParser(JSContext *cx)
|
2009-08-20 15:21:14 -07:00
|
|
|
: hexChar(), numHex(), statep(), stateStack(), rootVal(), objectStack(),
|
2010-08-24 08:50:54 -07:00
|
|
|
objectKey(cx), buffer(cx), suppressErrors(false)
|
2009-08-13 17:22:55 -07:00
|
|
|
{}
|
|
|
|
|
|
|
|
/* Used while handling \uNNNN in strings */
|
|
|
|
jschar hexChar;
|
|
|
|
uint8 numHex;
|
|
|
|
|
|
|
|
JSONParserState *statep;
|
|
|
|
JSONParserState stateStack[JSON_MAX_DEPTH];
|
2010-07-14 23:19:36 -07:00
|
|
|
Value *rootVal;
|
2009-08-13 17:22:55 -07:00
|
|
|
JSObject *objectStack;
|
2009-09-01 18:46:19 -07:00
|
|
|
js::Vector<jschar, 8> objectKey;
|
|
|
|
js::Vector<jschar, 8> buffer;
|
2010-08-24 08:50:54 -07:00
|
|
|
bool suppressErrors;
|
2009-08-13 17:22:55 -07:00
|
|
|
};
|
|
|
|
|
2009-09-23 11:16:30 -07:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
Class js_JSONClass = {
|
2008-10-06 13:54:12 -07:00
|
|
|
js_JSON_str,
|
|
|
|
JSCLASS_HAS_CACHED_PROTO(JSProto_JSON),
|
2010-06-12 09:29:04 -07:00
|
|
|
PropertyStub, /* addProperty */
|
|
|
|
PropertyStub, /* delProperty */
|
|
|
|
PropertyStub, /* getProperty */
|
|
|
|
PropertyStub, /* setProperty */
|
|
|
|
EnumerateStub,
|
|
|
|
ResolveStub,
|
|
|
|
ConvertStub
|
2008-10-06 13:54:12 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
js_json_parse(JSContext *cx, uintN argc, Value *vp)
|
2008-10-06 13:54:12 -07:00
|
|
|
{
|
|
|
|
JSString *s = NULL;
|
2010-07-14 23:19:36 -07:00
|
|
|
Value *argv = vp + 2;
|
|
|
|
AutoValueRooter reviver(cx);
|
2009-07-27 18:40:12 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!JS_ConvertArguments(cx, argc, Jsvalify(argv), "S / v", &s, reviver.addr()))
|
2008-10-06 13:54:12 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
JSONParser *jp = js_BeginJSONParse(cx, vp);
|
|
|
|
JSBool ok = jp != NULL;
|
|
|
|
if (ok) {
|
2009-08-13 17:22:55 -07:00
|
|
|
const jschar *chars;
|
|
|
|
size_t length;
|
|
|
|
s->getCharsAndLength(chars, length);
|
|
|
|
ok = js_ConsumeJSONText(cx, jp, chars, length);
|
2010-04-01 13:54:03 -07:00
|
|
|
ok &= !!js_FinishJSONParse(cx, jp, reviver.value());
|
2008-10-06 13:54:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
js_json_stringify(JSContext *cx, uintN argc, Value *vp)
|
2008-10-06 13:54:12 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
Value *argv = vp + 2;
|
|
|
|
AutoValueRooter space(cx);
|
2010-03-29 19:47:40 -07:00
|
|
|
AutoObjectRooter replacer(cx);
|
2009-02-13 12:34:39 -08:00
|
|
|
|
2008-10-06 13:54:12 -07:00
|
|
|
// Must throw an Error if there isn't a first arg
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!JS_ConvertArguments(cx, argc, Jsvalify(argv), "v / o v", vp, replacer.addr(), space.addr()))
|
2008-10-06 13:54:12 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2009-08-14 16:10:59 -07:00
|
|
|
JSCharBuffer cb(cx);
|
2009-02-13 12:34:39 -08:00
|
|
|
|
2010-03-29 19:47:40 -07:00
|
|
|
if (!js_Stringify(cx, vp, replacer.object(), space.value(), cb))
|
2009-03-02 14:00:28 -08:00
|
|
|
return JS_FALSE;
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2009-05-07 13:28:21 -07:00
|
|
|
// XXX This can never happen to nsJSON.cpp, but the JSON object
|
|
|
|
// needs to support returning undefined. So this is a little awkward
|
|
|
|
// for the API, because we want to support streaming writers.
|
2009-08-13 17:22:55 -07:00
|
|
|
if (!cb.empty()) {
|
|
|
|
JSString *str = js_NewStringFromCharBuffer(cx, cb);
|
|
|
|
if (!str)
|
2009-05-07 13:28:21 -07:00
|
|
|
return JS_FALSE;
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setString(str);
|
2009-05-07 13:28:21 -07:00
|
|
|
} else {
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setUndefined();
|
2009-05-07 13:28:21 -07:00
|
|
|
}
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2009-05-07 13:28:21 -07:00
|
|
|
return JS_TRUE;
|
2008-10-06 13:54:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
js_TryJSON(JSContext *cx, Value *vp)
|
2008-10-06 13:54:12 -07:00
|
|
|
{
|
|
|
|
// Checks whether the return value implements toJSON()
|
|
|
|
JSBool ok = JS_TRUE;
|
2009-03-02 14:00:28 -08:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (vp->isObject()) {
|
|
|
|
JSObject *obj = &vp->toObject();
|
2008-10-06 13:54:12 -07:00
|
|
|
ok = js_TryMethod(cx, obj, cx->runtime->atomState.toJSONAtom, 0, NULL, vp);
|
|
|
|
}
|
2009-03-02 14:00:28 -08:00
|
|
|
|
2008-10-06 13:54:12 -07:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-13 17:22:55 -07:00
|
|
|
static const char quote = '\"';
|
|
|
|
static const char backslash = '\\';
|
|
|
|
static const char unicodeEscape[] = "\\u00";
|
2008-10-06 13:54:12 -07:00
|
|
|
|
|
|
|
static JSBool
|
2009-08-14 16:10:59 -07:00
|
|
|
write_string(JSContext *cx, JSCharBuffer &cb, const jschar *buf, uint32 len)
|
2008-10-06 13:54:12 -07:00
|
|
|
{
|
2009-08-13 17:22:55 -07:00
|
|
|
if (!cb.append(quote))
|
2008-10-06 13:54:12 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
uint32 mark = 0;
|
|
|
|
uint32 i;
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
if (buf[i] == quote || buf[i] == backslash) {
|
2009-08-13 17:22:55 -07:00
|
|
|
if (!cb.append(&buf[mark], i - mark) || !cb.append(backslash) ||
|
|
|
|
!cb.append(buf[i])) {
|
2008-10-06 13:54:12 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
mark = i + 1;
|
|
|
|
} else if (buf[i] <= 31 || buf[i] == 127) {
|
2009-08-20 15:21:14 -07:00
|
|
|
if (!cb.append(&buf[mark], i - mark) ||
|
2009-08-13 17:22:55 -07:00
|
|
|
!js_AppendLiteral(cb, unicodeEscape)) {
|
2008-10-06 13:54:12 -07:00
|
|
|
return JS_FALSE;
|
2009-08-13 17:22:55 -07:00
|
|
|
}
|
2008-11-11 15:09:20 -08:00
|
|
|
char ubuf[3];
|
|
|
|
size_t len = JS_snprintf(ubuf, sizeof(ubuf), "%.2x", buf[i]);
|
2008-10-06 13:54:12 -07:00
|
|
|
JS_ASSERT(len == 2);
|
2008-11-11 15:09:20 -08:00
|
|
|
jschar wbuf[3];
|
|
|
|
size_t wbufSize = JS_ARRAY_LENGTH(wbuf);
|
|
|
|
if (!js_InflateStringToBuffer(cx, ubuf, len, wbuf, &wbufSize) ||
|
2009-08-13 17:22:55 -07:00
|
|
|
!cb.append(wbuf, wbufSize)) {
|
2008-10-06 13:54:12 -07:00
|
|
|
return JS_FALSE;
|
2008-11-11 15:09:20 -08:00
|
|
|
}
|
2008-10-06 13:54:12 -07:00
|
|
|
mark = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-13 17:22:55 -07:00
|
|
|
if (mark < len && !cb.append(&buf[mark], len - mark))
|
2008-10-06 13:54:12 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2009-08-13 17:22:55 -07:00
|
|
|
return cb.append(quote);
|
2008-10-06 13:54:12 -07:00
|
|
|
}
|
|
|
|
|
2009-05-07 13:28:21 -07:00
|
|
|
class StringifyContext
|
2009-03-02 14:00:28 -08:00
|
|
|
{
|
2009-05-07 13:28:21 -07:00
|
|
|
public:
|
2009-08-14 16:10:59 -07:00
|
|
|
StringifyContext(JSContext *cx, JSCharBuffer &cb, JSObject *replacer)
|
2010-07-13 21:32:28 -07:00
|
|
|
: cb(cb), gap(cx), replacer(replacer), depth(0), objectStack(cx)
|
2009-08-13 17:22:55 -07:00
|
|
|
{}
|
2009-02-13 12:34:39 -08:00
|
|
|
|
2010-07-13 21:32:28 -07:00
|
|
|
bool initializeGap(JSContext *cx, const Value &space) {
|
|
|
|
AutoValueRooter gapValue(cx, space);
|
|
|
|
|
|
|
|
if (space.isObject()) {
|
|
|
|
JSObject &obj = space.toObject();
|
|
|
|
Class *clasp = obj.getClass();
|
|
|
|
if (clasp == &js_NumberClass || clasp == &js_StringClass)
|
|
|
|
*gapValue.addr() = obj.getPrimitiveThis();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gapValue.value().isString()) {
|
|
|
|
if (!js_ValueToCharBuffer(cx, gapValue.value(), gap))
|
|
|
|
return false;
|
2010-08-09 16:39:19 -07:00
|
|
|
if (gap.length() > 10)
|
|
|
|
gap.resize(10);
|
|
|
|
} else if (gapValue.value().isNumber()) {
|
2010-07-13 21:32:28 -07:00
|
|
|
jsdouble d = gapValue.value().isInt32()
|
|
|
|
? gapValue.value().toInt32()
|
|
|
|
: js_DoubleToInteger(gapValue.value().toDouble());
|
|
|
|
d = JS_MIN(10, d);
|
2010-08-09 16:39:19 -07:00
|
|
|
if (d >= 1 && !gap.appendN(' ', uint32(d)))
|
2010-07-13 21:32:28 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool initializeStack() {
|
|
|
|
return objectStack.init(16);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
~StringifyContext() { JS_ASSERT(objectStack.empty()); }
|
|
|
|
#endif
|
|
|
|
|
2009-08-14 16:10:59 -07:00
|
|
|
JSCharBuffer &cb;
|
|
|
|
JSCharBuffer gap;
|
2009-05-07 13:28:21 -07:00
|
|
|
JSObject *replacer;
|
|
|
|
uint32 depth;
|
2010-07-13 21:32:28 -07:00
|
|
|
HashSet<JSObject *> objectStack;
|
2009-05-07 13:28:21 -07:00
|
|
|
};
|
2009-02-13 12:34:39 -08:00
|
|
|
|
2009-05-18 17:12:51 -07:00
|
|
|
static JSBool CallReplacerFunction(JSContext *cx, jsid id, JSObject *holder,
|
2010-07-14 23:19:36 -07:00
|
|
|
StringifyContext *scx, Value *vp);
|
2009-05-18 17:12:51 -07:00
|
|
|
static JSBool Str(JSContext *cx, jsid id, JSObject *holder,
|
2010-07-14 23:19:36 -07:00
|
|
|
StringifyContext *scx, Value *vp, bool callReplacer = true);
|
2009-05-07 13:28:21 -07:00
|
|
|
|
|
|
|
static JSBool
|
|
|
|
WriteIndent(JSContext *cx, StringifyContext *scx, uint32 limit)
|
|
|
|
{
|
2009-08-13 17:22:55 -07:00
|
|
|
if (!scx->gap.empty()) {
|
|
|
|
if (!scx->cb.append('\n'))
|
2009-05-07 13:28:21 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
for (uint32 i = 0; i < limit; i++) {
|
2009-08-13 17:22:55 -07:00
|
|
|
if (!scx->cb.append(scx->gap.begin(), scx->gap.end()))
|
2009-05-07 13:28:21 -07:00
|
|
|
return JS_FALSE;
|
2009-02-13 12:34:39 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-07 13:28:21 -07:00
|
|
|
return JS_TRUE;
|
2009-02-13 12:34:39 -08:00
|
|
|
}
|
|
|
|
|
2010-07-13 21:32:28 -07:00
|
|
|
class CycleDetector
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CycleDetector(StringifyContext *scx, JSObject *obj)
|
|
|
|
: objectStack(scx->objectStack), obj(obj) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool init(JSContext *cx) {
|
|
|
|
HashSet<JSObject *>::AddPtr ptr = objectStack.lookupForAdd(obj);
|
|
|
|
if (ptr) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_CYCLIC_VALUE, js_object_str);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return objectStack.add(ptr, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
~CycleDetector() {
|
|
|
|
objectStack.remove(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
HashSet<JSObject *> &objectStack;
|
|
|
|
JSObject *const obj;
|
|
|
|
};
|
|
|
|
|
2009-02-13 12:34:39 -08:00
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
JO(JSContext *cx, Value *vp, StringifyContext *scx)
|
2008-10-06 13:54:12 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *obj = &vp->toObject();
|
2009-05-07 13:28:21 -07:00
|
|
|
|
2010-07-13 21:32:28 -07:00
|
|
|
CycleDetector detect(scx, obj);
|
|
|
|
if (!detect.init(cx))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2009-08-13 17:22:55 -07:00
|
|
|
if (!scx->cb.append('{'))
|
2008-10-06 13:54:12 -07:00
|
|
|
return JS_FALSE;
|
2008-10-08 10:31:07 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
Value vec[3] = { NullValue(), NullValue(), NullValue() };
|
2010-03-29 19:47:40 -07:00
|
|
|
AutoArrayRooter tvr(cx, JS_ARRAY_LENGTH(vec), vec);
|
2010-07-14 23:19:36 -07:00
|
|
|
Value& outputValue = vec[0];
|
|
|
|
Value& whitelistElement = vec[1];
|
|
|
|
AutoIdRooter idr(cx);
|
|
|
|
jsid& id = *idr.addr();
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
Value *keySource = vp;
|
2009-05-07 13:28:21 -07:00
|
|
|
bool usingWhitelist = false;
|
|
|
|
|
|
|
|
// if the replacer is an array, we use the keys from it
|
|
|
|
if (scx->replacer && JS_IsArrayObject(cx, scx->replacer)) {
|
|
|
|
usingWhitelist = true;
|
2010-07-14 23:19:36 -07:00
|
|
|
vec[2].setObject(*scx->replacer);
|
|
|
|
keySource = &vec[2];
|
2008-10-06 13:54:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSBool memberWritten = JS_FALSE;
|
2010-08-09 16:39:19 -07:00
|
|
|
AutoIdVector props(cx);
|
|
|
|
if (!GetPropertyNames(cx, &keySource->toObject(), JSITER_OWNONLY, props))
|
2010-04-24 11:18:10 -07:00
|
|
|
return JS_FALSE;
|
2009-05-07 13:28:21 -07:00
|
|
|
|
2010-08-09 16:39:19 -07:00
|
|
|
for (size_t i = 0, len = props.length(); i < len; i++) {
|
2010-07-14 23:19:36 -07:00
|
|
|
outputValue.setUndefined();
|
2009-05-07 13:28:21 -07:00
|
|
|
|
2010-04-24 11:18:10 -07:00
|
|
|
if (!usingWhitelist) {
|
2010-08-09 16:39:19 -07:00
|
|
|
if (!js_ValueToStringId(cx, IdToValue(props[i]), &id))
|
2010-04-24 11:18:10 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
} else {
|
2009-05-07 13:28:21 -07:00
|
|
|
// skip non-index properties
|
2010-04-24 11:18:10 -07:00
|
|
|
jsuint index = 0;
|
2010-08-09 16:39:19 -07:00
|
|
|
if (!js_IdIsIndex(props[i], &index))
|
2009-05-07 13:28:21 -07:00
|
|
|
continue;
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2010-08-09 16:39:19 -07:00
|
|
|
if (!scx->replacer->getProperty(cx, props[i], &whitelistElement))
|
2010-04-24 11:18:10 -07:00
|
|
|
return JS_FALSE;
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2010-04-24 11:18:10 -07:00
|
|
|
if (!js_ValueToStringId(cx, whitelistElement, &id))
|
|
|
|
return JS_FALSE;
|
2009-05-07 13:28:21 -07:00
|
|
|
}
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2010-04-24 11:18:10 -07:00
|
|
|
// We should have a string id by this point. Either from
|
2010-04-24 16:05:48 -07:00
|
|
|
// JS_Enumerate's id array, or by converting an element
|
|
|
|
// of the whitelist.
|
2010-07-14 23:19:36 -07:00
|
|
|
JS_ASSERT(JSID_IS_ATOM(id));
|
2009-06-05 12:56:45 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!JS_GetPropertyById(cx, obj, id, Jsvalify(&outputValue)))
|
2010-04-24 11:18:10 -07:00
|
|
|
return JS_FALSE;
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (outputValue.isObjectOrNull() && !js_TryJSON(cx, &outputValue))
|
2010-04-24 11:18:10 -07:00
|
|
|
return JS_FALSE;
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2009-05-18 17:12:51 -07:00
|
|
|
// call this here, so we don't write out keys if the replacer function
|
|
|
|
// wants to elide the value.
|
|
|
|
if (!CallReplacerFunction(cx, id, obj, scx, &outputValue))
|
2010-04-24 11:18:10 -07:00
|
|
|
return JS_FALSE;
|
2009-05-18 17:12:51 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JSType type = JS_TypeOfValue(cx, Jsvalify(outputValue));
|
2009-02-24 21:53:49 -08:00
|
|
|
|
|
|
|
// elide undefined values and functions and XML
|
2010-07-14 23:19:36 -07:00
|
|
|
if (outputValue.isUndefined() || type == JSTYPE_FUNCTION || type == JSTYPE_XML)
|
2008-10-06 13:54:12 -07:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// output a comma unless this is the first member to write
|
2009-10-17 23:30:40 -07:00
|
|
|
if (memberWritten && !scx->cb.append(','))
|
2010-04-24 11:18:10 -07:00
|
|
|
return JS_FALSE;
|
2008-10-06 13:54:12 -07:00
|
|
|
memberWritten = JS_TRUE;
|
|
|
|
|
2009-05-07 13:28:21 -07:00
|
|
|
if (!WriteIndent(cx, scx, scx->depth))
|
2010-04-24 11:18:10 -07:00
|
|
|
return JS_FALSE;
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2009-05-07 13:28:21 -07:00
|
|
|
// Be careful below, this string is weakly rooted
|
2010-07-14 23:19:36 -07:00
|
|
|
JSString *s = js_ValueToString(cx, IdToValue(id));
|
2009-10-17 23:30:40 -07:00
|
|
|
if (!s)
|
2010-04-24 11:18:10 -07:00
|
|
|
return JS_FALSE;
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2009-08-13 17:22:55 -07:00
|
|
|
const jschar *chars;
|
|
|
|
size_t length;
|
|
|
|
s->getCharsAndLength(chars, length);
|
2009-10-17 23:30:40 -07:00
|
|
|
if (!write_string(cx, scx->cb, chars, length) ||
|
|
|
|
!scx->cb.append(':') ||
|
2010-08-09 16:39:19 -07:00
|
|
|
!(scx->gap.empty() || scx->cb.append(' ')) ||
|
2010-06-24 13:32:07 -07:00
|
|
|
!Str(cx, id, obj, scx, &outputValue, true)) {
|
2010-04-24 11:18:10 -07:00
|
|
|
return JS_FALSE;
|
2009-10-17 23:30:40 -07:00
|
|
|
}
|
|
|
|
}
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2009-05-07 13:28:21 -07:00
|
|
|
if (memberWritten && !WriteIndent(cx, scx, scx->depth - 1))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2009-08-13 17:22:55 -07:00
|
|
|
return scx->cb.append('}');
|
2009-05-07 13:28:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
JA(JSContext *cx, Value *vp, StringifyContext *scx)
|
2009-05-07 13:28:21 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *obj = &vp->toObject();
|
2009-05-07 13:28:21 -07:00
|
|
|
|
2010-07-13 21:32:28 -07:00
|
|
|
CycleDetector detect(scx, obj);
|
|
|
|
if (!detect.init(cx))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2009-08-13 17:22:55 -07:00
|
|
|
if (!scx->cb.append('['))
|
2009-05-07 13:28:21 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
jsuint length;
|
|
|
|
if (!js_GetLengthProperty(cx, obj, &length))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-04-09 12:38:27 -07:00
|
|
|
if (length != 0 && !WriteIndent(cx, scx, scx->depth))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
AutoValueRooter outputValue(cx);
|
2009-05-07 13:28:21 -07:00
|
|
|
|
|
|
|
jsid id;
|
|
|
|
jsuint i;
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
id = INT_TO_JSID(i);
|
|
|
|
|
2010-03-29 19:47:40 -07:00
|
|
|
if (!obj->getProperty(cx, id, outputValue.addr()))
|
2009-05-07 13:28:21 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-03-29 19:47:40 -07:00
|
|
|
if (!Str(cx, id, obj, scx, outputValue.addr()))
|
2009-05-07 13:28:21 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (outputValue.value().isUndefined()) {
|
2009-08-13 17:22:55 -07:00
|
|
|
if (!js_AppendLiteral(scx->cb, "null"))
|
2009-05-07 13:28:21 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < length - 1) {
|
2009-08-13 17:22:55 -07:00
|
|
|
if (!scx->cb.append(','))
|
2009-05-07 13:28:21 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
if (!WriteIndent(cx, scx, scx->depth))
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length != 0 && !WriteIndent(cx, scx, scx->depth - 1))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2009-08-13 17:22:55 -07:00
|
|
|
return scx->cb.append(']');
|
2009-05-07 13:28:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
CallReplacerFunction(JSContext *cx, jsid id, JSObject *holder, StringifyContext *scx, Value *vp)
|
2009-05-18 17:12:51 -07:00
|
|
|
{
|
2010-02-22 22:25:09 -08:00
|
|
|
if (scx->replacer && scx->replacer->isCallable()) {
|
2010-07-14 23:19:36 -07:00
|
|
|
Value vec[2] = { IdToValue(id), *vp};
|
|
|
|
if (!JS_CallFunctionValue(cx, holder, OBJECT_TO_JSVAL(scx->replacer),
|
|
|
|
2, Jsvalify(vec), Jsvalify(vp))) {
|
2009-05-18 17:12:51 -07:00
|
|
|
return JS_FALSE;
|
2010-07-14 23:19:36 -07:00
|
|
|
}
|
2009-05-18 17:12:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
Str(JSContext *cx, jsid id, JSObject *holder, StringifyContext *scx, Value *vp, bool callReplacer)
|
2009-05-07 13:28:21 -07:00
|
|
|
{
|
|
|
|
JS_CHECK_RECURSION(cx, return JS_FALSE);
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (vp->isObject() && !js_TryJSON(cx, vp))
|
2009-05-07 13:28:21 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2009-05-18 17:12:51 -07:00
|
|
|
if (callReplacer && !CallReplacerFunction(cx, id, holder, scx, vp))
|
|
|
|
return JS_FALSE;
|
2009-05-07 13:28:21 -07:00
|
|
|
|
|
|
|
// catches string and number objects with no toJSON
|
2010-07-14 23:19:36 -07:00
|
|
|
if (vp->isObject()) {
|
|
|
|
JSObject *obj = &vp->toObject();
|
|
|
|
Class *clasp = obj->getClass();
|
2009-05-07 13:28:21 -07:00
|
|
|
if (clasp == &js_StringClass || clasp == &js_NumberClass)
|
2010-07-14 23:19:36 -07:00
|
|
|
*vp = obj->getPrimitiveThis();
|
2009-05-07 13:28:21 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (vp->isString()) {
|
2009-08-13 17:22:55 -07:00
|
|
|
const jschar *chars;
|
|
|
|
size_t length;
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->toString()->getCharsAndLength(chars, length);
|
2009-08-13 17:22:55 -07:00
|
|
|
return write_string(cx, scx->cb, chars, length);
|
2009-05-07 13:28:21 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (vp->isNull()) {
|
2009-08-13 17:22:55 -07:00
|
|
|
return js_AppendLiteral(scx->cb, "null");
|
2009-05-07 13:28:21 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (vp->isBoolean()) {
|
|
|
|
return vp->toBoolean() ? js_AppendLiteral(scx->cb, "true")
|
|
|
|
: js_AppendLiteral(scx->cb, "false");
|
2009-05-07 13:28:21 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (vp->isNumber()) {
|
|
|
|
if (vp->isDouble()) {
|
|
|
|
jsdouble d = vp->toDouble();
|
2009-05-07 13:28:21 -07:00
|
|
|
if (!JSDOUBLE_IS_FINITE(d))
|
2009-08-13 17:22:55 -07:00
|
|
|
return js_AppendLiteral(scx->cb, "null");
|
2009-05-07 13:28:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
char numBuf[DTOSTR_STANDARD_BUFFER_SIZE], *numStr;
|
2010-07-14 23:19:36 -07:00
|
|
|
jsdouble d = vp->isInt32() ? jsdouble(vp->toInt32()) : vp->toDouble();
|
2010-03-22 16:21:10 -07:00
|
|
|
numStr = js_dtostr(JS_THREAD_DATA(cx)->dtoaState, numBuf, sizeof numBuf,
|
|
|
|
DTOSTR_STANDARD, 0, d);
|
2009-05-07 13:28:21 -07:00
|
|
|
if (!numStr) {
|
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
jschar dstr[DTOSTR_STANDARD_BUFFER_SIZE];
|
|
|
|
size_t dbufSize = DTOSTR_STANDARD_BUFFER_SIZE;
|
|
|
|
if (!js_InflateStringToBuffer(cx, numStr, strlen(numStr), dstr, &dbufSize))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2009-08-13 17:22:55 -07:00
|
|
|
return scx->cb.append(dstr, dbufSize);
|
2009-05-07 13:28:21 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (vp->isObject() && !IsFunctionObject(*vp) && !IsXML(*vp)) {
|
2009-05-07 13:28:21 -07:00
|
|
|
JSBool ok;
|
|
|
|
|
|
|
|
scx->depth++;
|
2010-07-14 23:19:36 -07:00
|
|
|
ok = (JS_IsArrayObject(cx, &vp->toObject()) ? JA : JO)(cx, vp, scx);
|
2009-05-07 13:28:21 -07:00
|
|
|
scx->depth--;
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
2009-07-27 18:40:12 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setUndefined();
|
2009-05-07 13:28:21 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2009-02-13 12:34:39 -08:00
|
|
|
JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
js_Stringify(JSContext *cx, Value *vp, JSObject *replacer, const Value &space,
|
2009-08-14 16:10:59 -07:00
|
|
|
JSCharBuffer &cb)
|
|
|
|
{
|
2009-08-13 17:22:55 -07:00
|
|
|
StringifyContext scx(cx, cb, replacer);
|
2010-07-13 21:32:28 -07:00
|
|
|
if (!scx.initializeGap(cx, space) || !scx.initializeStack())
|
2009-05-07 13:28:21 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-06-18 17:43:02 -07:00
|
|
|
JSObject *obj = NewBuiltinClassInstance(cx, &js_ObjectClass);
|
2009-05-07 13:28:21 -07:00
|
|
|
if (!obj)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-04-25 05:51:05 -07:00
|
|
|
AutoObjectRooter tvr(cx, obj);
|
2009-08-11 13:05:44 -07:00
|
|
|
if (!obj->defineProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.emptyAtom),
|
2009-08-26 14:28:36 -07:00
|
|
|
*vp, NULL, NULL, JSPROP_ENUMERATE)) {
|
2009-05-07 13:28:21 -07:00
|
|
|
return JS_FALSE;
|
2009-02-13 12:34:39 -08:00
|
|
|
}
|
|
|
|
|
2009-05-07 13:28:21 -07:00
|
|
|
return Str(cx, ATOM_TO_JSID(cx->runtime->atomState.emptyAtom), obj, &scx, vp);
|
2008-10-06 13:54:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// helper to determine whether a character could be part of a number
|
2008-10-08 10:31:07 -07:00
|
|
|
static JSBool IsNumChar(jschar c)
|
2008-10-06 13:54:12 -07:00
|
|
|
{
|
|
|
|
return ((c <= '9' && c >= '0') || c == '.' || c == '-' || c == '+' || c == 'e' || c == 'E');
|
|
|
|
}
|
|
|
|
|
2009-02-13 12:34:39 -08:00
|
|
|
static JSBool HandleData(JSContext *cx, JSONParser *jp, JSONDataType type);
|
2009-03-02 14:00:28 -08:00
|
|
|
static JSBool PopState(JSContext *cx, JSONParser *jp);
|
2009-02-13 12:34:39 -08:00
|
|
|
|
2010-03-29 19:47:40 -07:00
|
|
|
static bool
|
2010-07-14 23:19:36 -07:00
|
|
|
Walk(JSContext *cx, jsid id, JSObject *holder, const Value &reviver, Value *vp)
|
2009-03-03 09:55:11 -08:00
|
|
|
{
|
2010-03-29 19:47:40 -07:00
|
|
|
JS_CHECK_RECURSION(cx, return false);
|
2009-08-20 15:21:14 -07:00
|
|
|
|
2009-08-11 13:05:44 -07:00
|
|
|
if (!holder->getProperty(cx, id, vp))
|
2010-03-29 19:47:40 -07:00
|
|
|
return false;
|
2009-03-03 09:55:11 -08:00
|
|
|
|
|
|
|
JSObject *obj;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (vp->isObject() && !(obj = &vp->toObject())->isCallable()) {
|
|
|
|
AutoValueRooter propValue(cx);
|
2009-07-27 18:40:12 -07:00
|
|
|
|
2010-03-04 20:44:09 -08:00
|
|
|
if(obj->isArray()) {
|
2009-03-03 09:55:11 -08:00
|
|
|
jsuint length = 0;
|
|
|
|
if (!js_GetLengthProperty(cx, obj, &length))
|
2010-03-29 19:47:40 -07:00
|
|
|
return false;
|
2009-03-03 09:55:11 -08:00
|
|
|
|
|
|
|
for (jsuint i = 0; i < length; i++) {
|
|
|
|
jsid index;
|
|
|
|
if (!js_IndexToId(cx, i, &index))
|
2010-03-29 19:47:40 -07:00
|
|
|
return false;
|
2009-03-03 09:55:11 -08:00
|
|
|
|
2010-03-29 19:47:40 -07:00
|
|
|
if (!Walk(cx, index, obj, reviver, propValue.addr()))
|
|
|
|
return false;
|
2009-03-03 09:55:11 -08:00
|
|
|
|
2010-03-29 19:47:40 -07:00
|
|
|
if (!obj->defineProperty(cx, index, propValue.value(), NULL, NULL, JSPROP_ENUMERATE))
|
|
|
|
return false;
|
2009-03-03 09:55:11 -08:00
|
|
|
}
|
|
|
|
} else {
|
2010-08-09 16:39:19 -07:00
|
|
|
AutoIdVector props(cx);
|
|
|
|
if (!GetPropertyNames(cx, obj, JSITER_OWNONLY, props))
|
2010-03-29 19:47:40 -07:00
|
|
|
return false;
|
|
|
|
|
2010-08-09 16:39:19 -07:00
|
|
|
for (size_t i = 0, len = props.length(); i < len; i++) {
|
|
|
|
jsid idName = props[i];
|
2010-03-29 19:47:40 -07:00
|
|
|
if (!Walk(cx, idName, obj, reviver, propValue.addr()))
|
|
|
|
return false;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (propValue.value().isUndefined()) {
|
2010-03-29 19:47:40 -07:00
|
|
|
if (!js_DeleteProperty(cx, obj, idName, propValue.addr()))
|
|
|
|
return false;
|
2009-03-03 09:55:11 -08:00
|
|
|
} else {
|
2010-03-29 19:47:40 -07:00
|
|
|
if (!obj->defineProperty(cx, idName, propValue.value(), NULL, NULL,
|
|
|
|
JSPROP_ENUMERATE)) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-03-03 09:55:11 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return reviver.call(holder, key, value);
|
2010-07-14 23:19:36 -07:00
|
|
|
const Value &value = *vp;
|
|
|
|
JSString *key = js_ValueToString(cx, IdToValue(id));
|
2009-03-03 09:55:11 -08:00
|
|
|
if (!key)
|
2010-03-29 19:47:40 -07:00
|
|
|
return false;
|
2009-03-03 09:55:11 -08:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
Value vec[2] = { StringValue(key), value };
|
|
|
|
Value reviverResult;
|
|
|
|
if (!JS_CallFunctionValue(cx, holder, Jsvalify(reviver),
|
|
|
|
2, Jsvalify(vec), Jsvalify(&reviverResult))) {
|
2010-03-29 19:47:40 -07:00
|
|
|
return false;
|
2010-07-14 23:19:36 -07:00
|
|
|
}
|
2009-03-03 09:55:11 -08:00
|
|
|
|
|
|
|
*vp = reviverResult;
|
2010-03-29 19:47:40 -07:00
|
|
|
return true;
|
2009-03-03 09:55:11 -08:00
|
|
|
}
|
|
|
|
|
2010-08-24 08:50:54 -07:00
|
|
|
static JSBool
|
|
|
|
JSONParseError(JSONParser *jp, JSContext *cx)
|
|
|
|
{
|
|
|
|
if (!jp->suppressErrors)
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_JSON_BAD_PARSE);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-03-29 19:47:40 -07:00
|
|
|
static bool
|
2010-07-14 23:19:36 -07:00
|
|
|
Revive(JSContext *cx, const Value &reviver, Value *vp)
|
2009-03-03 09:55:11 -08:00
|
|
|
{
|
2009-07-27 18:40:12 -07:00
|
|
|
|
2010-06-18 17:43:02 -07:00
|
|
|
JSObject *obj = NewBuiltinClassInstance(cx, &js_ObjectClass);
|
2009-03-03 09:55:11 -08:00
|
|
|
if (!obj)
|
2010-03-29 19:47:40 -07:00
|
|
|
return false;
|
2009-03-03 09:55:11 -08:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
AutoObjectRooter tvr(cx, obj);
|
2009-08-11 13:05:44 -07:00
|
|
|
if (!obj->defineProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.emptyAtom),
|
2009-08-26 14:28:36 -07:00
|
|
|
*vp, NULL, NULL, JSPROP_ENUMERATE)) {
|
2010-03-29 19:47:40 -07:00
|
|
|
return false;
|
2009-03-03 09:55:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Walk(cx, ATOM_TO_JSID(cx->runtime->atomState.emptyAtom), obj, reviver, vp);
|
|
|
|
}
|
|
|
|
|
2008-10-06 13:54:12 -07:00
|
|
|
JSONParser *
|
2010-08-25 15:50:32 -07:00
|
|
|
js_BeginJSONParse(JSContext *cx, Value *rootVal, bool suppressErrors /*= false*/)
|
2008-10-06 13:54:12 -07:00
|
|
|
{
|
|
|
|
if (!cx)
|
|
|
|
return NULL;
|
|
|
|
|
2008-10-12 17:48:48 -07:00
|
|
|
JSObject *arr = js_NewArrayObject(cx, 0, NULL);
|
2008-10-06 13:54:12 -07:00
|
|
|
if (!arr)
|
|
|
|
return NULL;
|
|
|
|
|
2009-09-01 18:46:19 -07:00
|
|
|
JSONParser *jp = cx->create<JSONParser>(cx);
|
2008-10-06 13:54:12 -07:00
|
|
|
if (!jp)
|
2008-10-08 10:31:07 -07:00
|
|
|
return NULL;
|
2008-10-06 13:54:12 -07:00
|
|
|
|
|
|
|
jp->objectStack = arr;
|
2010-06-07 17:05:02 -07:00
|
|
|
if (!JS_AddNamedObjectRoot(cx, &jp->objectStack, "JSON parse stack"))
|
2008-10-06 13:54:12 -07:00
|
|
|
goto bad;
|
|
|
|
|
|
|
|
jp->statep = jp->stateStack;
|
|
|
|
*jp->statep = JSON_PARSE_STATE_INIT;
|
|
|
|
jp->rootVal = rootVal;
|
2010-08-24 08:50:54 -07:00
|
|
|
jp->suppressErrors = suppressErrors;
|
2008-11-07 15:10:39 -08:00
|
|
|
|
2008-10-06 13:54:12 -07:00
|
|
|
return jp;
|
2009-04-21 12:09:27 -07:00
|
|
|
|
2008-10-06 13:54:12 -07:00
|
|
|
bad:
|
2010-07-14 23:19:36 -07:00
|
|
|
js_FinishJSONParse(cx, jp, NullValue());
|
2008-10-06 13:54:12 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-03-29 19:47:40 -07:00
|
|
|
bool
|
2010-07-14 23:19:36 -07:00
|
|
|
js_FinishJSONParse(JSContext *cx, JSONParser *jp, const Value &reviver)
|
2008-10-06 13:54:12 -07:00
|
|
|
{
|
|
|
|
if (!jp)
|
2010-03-29 19:47:40 -07:00
|
|
|
return true;
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2009-04-21 18:09:44 -07:00
|
|
|
JSBool early_ok = JS_TRUE;
|
2009-04-21 12:09:27 -07:00
|
|
|
|
2009-02-13 12:34:39 -08:00
|
|
|
// Check for unprocessed primitives at the root. This doesn't happen for
|
|
|
|
// strings because a closing quote triggers value processing.
|
|
|
|
if ((jp->statep - jp->stateStack) == 1) {
|
|
|
|
if (*jp->statep == JSON_PARSE_STATE_KEYWORD) {
|
2009-04-21 18:09:44 -07:00
|
|
|
early_ok = HandleData(cx, jp, JSON_DATA_KEYWORD);
|
2009-04-21 22:50:51 -07:00
|
|
|
if (early_ok)
|
2009-03-02 14:00:28 -08:00
|
|
|
PopState(cx, jp);
|
2009-02-13 12:34:39 -08:00
|
|
|
} else if (*jp->statep == JSON_PARSE_STATE_NUMBER) {
|
2009-04-21 18:09:44 -07:00
|
|
|
early_ok = HandleData(cx, jp, JSON_DATA_NUMBER);
|
2009-04-21 22:50:51 -07:00
|
|
|
if (early_ok)
|
2009-03-02 14:00:28 -08:00
|
|
|
PopState(cx, jp);
|
2009-02-13 12:34:39 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-30 20:26:06 -07:00
|
|
|
// This internal API is infallible, in spite of its JSBool return type.
|
2009-04-21 12:09:27 -07:00
|
|
|
js_RemoveRoot(cx->runtime, &jp->objectStack);
|
2009-03-12 00:27:15 -07:00
|
|
|
|
2010-03-29 19:47:40 -07:00
|
|
|
bool ok = *jp->statep == JSON_PARSE_STATE_FINISHED;
|
2010-07-14 23:19:36 -07:00
|
|
|
Value *vp = jp->rootVal;
|
2009-08-13 17:22:55 -07:00
|
|
|
|
2010-08-24 08:50:54 -07:00
|
|
|
if (!early_ok) {
|
|
|
|
ok = false;
|
|
|
|
} else if (!ok) {
|
|
|
|
JSONParseError(jp, cx);
|
|
|
|
} else if (reviver.isObject() && reviver.toObject().isCallable()) {
|
|
|
|
ok = Revive(cx, reviver, vp);
|
2010-08-23 22:38:19 -07:00
|
|
|
}
|
2009-03-02 14:00:28 -08:00
|
|
|
|
2010-08-24 08:50:54 -07:00
|
|
|
cx->destroy(jp);
|
2009-03-03 09:55:11 -08:00
|
|
|
|
2008-10-06 13:54:12 -07:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2009-03-02 14:00:28 -08:00
|
|
|
PushState(JSContext *cx, JSONParser *jp, JSONParserState state)
|
2008-10-06 13:54:12 -07:00
|
|
|
{
|
2009-03-02 14:00:28 -08:00
|
|
|
if (*jp->statep == JSON_PARSE_STATE_FINISHED) {
|
|
|
|
// extra input
|
2010-08-24 08:50:54 -07:00
|
|
|
return JSONParseError(jp, cx);
|
2009-03-02 14:00:28 -08:00
|
|
|
}
|
2008-10-06 13:54:12 -07:00
|
|
|
|
|
|
|
jp->statep++;
|
2009-03-02 14:00:28 -08:00
|
|
|
if ((uint32)(jp->statep - jp->stateStack) >= JS_ARRAY_LENGTH(jp->stateStack)) {
|
|
|
|
// too deep
|
2010-08-24 08:50:54 -07:00
|
|
|
return JSONParseError(jp, cx);
|
2009-03-02 14:00:28 -08:00
|
|
|
}
|
2008-10-06 13:54:12 -07:00
|
|
|
|
|
|
|
*jp->statep = state;
|
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2009-03-02 14:00:28 -08:00
|
|
|
PopState(JSContext *cx, JSONParser *jp)
|
2008-10-06 13:54:12 -07:00
|
|
|
{
|
|
|
|
jp->statep--;
|
|
|
|
if (jp->statep < jp->stateStack) {
|
|
|
|
jp->statep = jp->stateStack;
|
2010-08-24 08:50:54 -07:00
|
|
|
return JSONParseError(jp, cx);
|
2008-10-08 10:31:07 -07:00
|
|
|
}
|
2008-10-06 13:54:12 -07:00
|
|
|
|
|
|
|
if (*jp->statep == JSON_PARSE_STATE_INIT)
|
|
|
|
*jp->statep = JSON_PARSE_STATE_FINISHED;
|
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
PushValue(JSContext *cx, JSONParser *jp, JSObject *parent, const Value &value)
|
2008-10-06 13:54:12 -07:00
|
|
|
{
|
|
|
|
JSBool ok;
|
2010-03-04 20:44:09 -08:00
|
|
|
if (parent->isArray()) {
|
2008-10-06 13:54:12 -07:00
|
|
|
jsuint len;
|
2008-10-12 17:48:48 -07:00
|
|
|
ok = js_GetLengthProperty(cx, parent, &len);
|
2008-11-07 15:10:39 -08:00
|
|
|
if (ok) {
|
2009-03-03 09:55:11 -08:00
|
|
|
jsid index;
|
|
|
|
if (!js_IndexToId(cx, len, &index))
|
|
|
|
return JS_FALSE;
|
2009-08-26 14:28:36 -07:00
|
|
|
ok = parent->defineProperty(cx, index, value, NULL, NULL, JSPROP_ENUMERATE);
|
2008-11-07 15:10:39 -08:00
|
|
|
}
|
2008-10-06 13:54:12 -07:00
|
|
|
} else {
|
2009-08-13 17:22:55 -07:00
|
|
|
ok = JS_DefineUCProperty(cx, parent, jp->objectKey.begin(),
|
2010-07-14 23:19:36 -07:00
|
|
|
jp->objectKey.length(), Jsvalify(value),
|
2008-10-06 13:54:12 -07:00
|
|
|
NULL, NULL, JSPROP_ENUMERATE);
|
2009-08-13 17:22:55 -07:00
|
|
|
jp->objectKey.clear();
|
2008-10-06 13:54:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
PushObject(JSContext *cx, JSONParser *jp, JSObject *obj)
|
|
|
|
{
|
|
|
|
jsuint len;
|
2008-10-12 17:48:48 -07:00
|
|
|
if (!js_GetLengthProperty(cx, jp->objectStack, &len))
|
2008-10-06 13:54:12 -07:00
|
|
|
return JS_FALSE;
|
2010-08-24 08:50:54 -07:00
|
|
|
if (len >= JSON_MAX_DEPTH)
|
|
|
|
return JSONParseError(jp, cx);
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
AutoObjectRooter tvr(cx, obj);
|
|
|
|
Value v = ObjectOrNullValue(obj);
|
2008-10-06 13:54:12 -07:00
|
|
|
|
|
|
|
// Check if this is the root object
|
|
|
|
if (len == 0) {
|
2008-10-08 10:31:07 -07:00
|
|
|
*jp->rootVal = v;
|
2008-11-07 15:10:39 -08:00
|
|
|
// This property must be enumerable to keep the array dense
|
2009-08-11 13:05:44 -07:00
|
|
|
if (!jp->objectStack->defineProperty(cx, INT_TO_JSID(0), *jp->rootVal,
|
2009-08-26 14:28:36 -07:00
|
|
|
NULL, NULL, JSPROP_ENUMERATE)) {
|
2008-10-06 13:54:12 -07:00
|
|
|
return JS_FALSE;
|
2008-11-07 15:10:39 -08:00
|
|
|
}
|
2008-10-06 13:54:12 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
Value p;
|
2009-08-11 13:05:44 -07:00
|
|
|
if (!jp->objectStack->getProperty(cx, INT_TO_JSID(len - 1), &p))
|
2008-10-06 13:54:12 -07:00
|
|
|
return JS_FALSE;
|
2008-11-07 15:10:39 -08:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *parent = &p.toObject();
|
2009-03-02 14:00:28 -08:00
|
|
|
if (!PushValue(cx, jp, parent, v))
|
2008-10-06 13:54:12 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2008-11-07 15:10:39 -08:00
|
|
|
// This property must be enumerable to keep the array dense
|
2009-08-11 13:05:44 -07:00
|
|
|
if (!jp->objectStack->defineProperty(cx, INT_TO_JSID(len), v,
|
2009-08-26 14:28:36 -07:00
|
|
|
NULL, NULL, JSPROP_ENUMERATE)) {
|
2008-10-06 13:54:12 -07:00
|
|
|
return JS_FALSE;
|
2008-11-07 15:10:39 -08:00
|
|
|
}
|
2008-10-06 13:54:12 -07:00
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
OpenObject(JSContext *cx, JSONParser *jp)
|
|
|
|
{
|
2010-06-18 17:43:02 -07:00
|
|
|
JSObject *obj = NewBuiltinClassInstance(cx, &js_ObjectClass);
|
2008-10-06 13:54:12 -07:00
|
|
|
if (!obj)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
return PushObject(cx, jp, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
OpenArray(JSContext *cx, JSONParser *jp)
|
|
|
|
{
|
|
|
|
// Add an array to an existing array or object
|
2008-10-12 17:48:48 -07:00
|
|
|
JSObject *arr = js_NewArrayObject(cx, 0, NULL);
|
2008-10-06 13:54:12 -07:00
|
|
|
if (!arr)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
return PushObject(cx, jp, arr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
CloseObject(JSContext *cx, JSONParser *jp)
|
|
|
|
{
|
|
|
|
jsuint len;
|
2008-10-12 17:48:48 -07:00
|
|
|
if (!js_GetLengthProperty(cx, jp->objectStack, &len))
|
2008-10-06 13:54:12 -07:00
|
|
|
return JS_FALSE;
|
2008-10-12 17:48:48 -07:00
|
|
|
if (!js_SetLengthProperty(cx, jp->objectStack, len - 1))
|
2008-10-06 13:54:12 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
CloseArray(JSContext *cx, JSONParser *jp)
|
|
|
|
{
|
2008-10-09 11:18:31 -07:00
|
|
|
return CloseObject(cx, jp);
|
2008-10-06 13:54:12 -07:00
|
|
|
}
|
|
|
|
|
2009-03-02 14:00:28 -08:00
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
PushPrimitive(JSContext *cx, JSONParser *jp, const Value &value)
|
2009-03-02 14:00:28 -08:00
|
|
|
{
|
2010-03-29 19:47:40 -07:00
|
|
|
AutoValueRooter tvr(cx, value);
|
2009-03-02 14:00:28 -08:00
|
|
|
|
|
|
|
jsuint len;
|
|
|
|
if (!js_GetLengthProperty(cx, jp->objectStack, &len))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
if (len > 0) {
|
2010-07-14 23:19:36 -07:00
|
|
|
Value o;
|
2009-08-11 13:05:44 -07:00
|
|
|
if (!jp->objectStack->getProperty(cx, INT_TO_JSID(len - 1), &o))
|
2009-03-02 14:00:28 -08:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
return PushValue(cx, jp, &o.toObject(), value);
|
2009-03-02 14:00:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// root value must be primitive
|
|
|
|
*jp->rootVal = value;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2008-10-06 13:54:12 -07:00
|
|
|
static JSBool
|
|
|
|
HandleNumber(JSContext *cx, JSONParser *jp, const jschar *buf, uint32 len)
|
|
|
|
{
|
|
|
|
const jschar *ep;
|
2008-10-08 10:31:07 -07:00
|
|
|
double val;
|
2009-03-02 14:00:28 -08:00
|
|
|
if (!js_strtod(cx, buf, buf + len, &ep, &val))
|
2008-10-06 13:54:12 -07:00
|
|
|
return JS_FALSE;
|
2009-03-02 14:00:28 -08:00
|
|
|
if (ep != buf + len) {
|
|
|
|
// bad number input
|
2010-08-24 08:50:54 -07:00
|
|
|
return JSONParseError(jp, cx);
|
2009-03-02 14:00:28 -08:00
|
|
|
}
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
return PushPrimitive(cx, jp, DoubleValue(val));
|
2008-10-06 13:54:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
HandleString(JSContext *cx, JSONParser *jp, const jschar *buf, uint32 len)
|
|
|
|
{
|
2008-10-12 17:48:48 -07:00
|
|
|
JSString *str = js_NewStringCopyN(cx, buf, len);
|
2009-02-13 12:34:39 -08:00
|
|
|
if (!str)
|
2008-10-06 13:54:12 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
return PushPrimitive(cx, jp, StringValue(str));
|
2008-10-06 13:54:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
HandleKeyword(JSContext *cx, JSONParser *jp, const jschar *buf, uint32 len)
|
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
Value keyword;
|
2010-03-21 16:07:48 -07:00
|
|
|
TokenKind tt = js_CheckKeyword(buf, len);
|
2009-03-02 14:00:28 -08:00
|
|
|
if (tt != TOK_PRIMARY) {
|
|
|
|
// bad keyword
|
2010-08-24 08:50:54 -07:00
|
|
|
return JSONParseError(jp, cx);
|
2009-03-02 14:00:28 -08:00
|
|
|
}
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2009-03-02 14:00:28 -08:00
|
|
|
if (buf[0] == 'n') {
|
2010-07-14 23:19:36 -07:00
|
|
|
keyword.setNull();
|
2009-03-02 14:00:28 -08:00
|
|
|
} else if (buf[0] == 't') {
|
2010-07-14 23:19:36 -07:00
|
|
|
keyword.setBoolean(true);
|
2009-03-02 14:00:28 -08:00
|
|
|
} else if (buf[0] == 'f') {
|
2010-07-14 23:19:36 -07:00
|
|
|
keyword.setBoolean(false);
|
2009-03-02 14:00:28 -08:00
|
|
|
} else {
|
2010-08-24 08:50:54 -07:00
|
|
|
return JSONParseError(jp, cx);
|
2009-03-02 14:00:28 -08:00
|
|
|
}
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2009-03-02 14:00:28 -08:00
|
|
|
return PushPrimitive(cx, jp, keyword);
|
2008-10-06 13:54:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2008-11-07 15:10:39 -08:00
|
|
|
HandleData(JSContext *cx, JSONParser *jp, JSONDataType type)
|
2008-10-06 13:54:12 -07:00
|
|
|
{
|
2009-04-21 12:09:27 -07:00
|
|
|
JSBool ok;
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2009-04-21 12:09:27 -07:00
|
|
|
switch (type) {
|
|
|
|
case JSON_DATA_STRING:
|
2009-08-14 16:10:59 -07:00
|
|
|
ok = HandleString(cx, jp, jp->buffer.begin(), jp->buffer.length());
|
2009-04-21 12:09:27 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case JSON_DATA_KEYSTRING:
|
2009-08-13 17:22:55 -07:00
|
|
|
ok = jp->objectKey.append(jp->buffer.begin(), jp->buffer.end());
|
2009-04-21 12:09:27 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case JSON_DATA_NUMBER:
|
2009-08-14 16:10:59 -07:00
|
|
|
ok = HandleNumber(cx, jp, jp->buffer.begin(), jp->buffer.length());
|
2009-04-21 12:09:27 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
JS_ASSERT(type == JSON_DATA_KEYWORD);
|
2009-08-14 16:10:59 -07:00
|
|
|
ok = HandleKeyword(cx, jp, jp->buffer.begin(), jp->buffer.length());
|
2009-04-21 12:09:27 -07:00
|
|
|
break;
|
|
|
|
}
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2009-08-13 17:22:55 -07:00
|
|
|
if (ok)
|
|
|
|
jp->buffer.clear();
|
2009-04-21 12:09:27 -07:00
|
|
|
return ok;
|
2008-10-06 13:54:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
js_ConsumeJSONText(JSContext *cx, JSONParser *jp, const jschar *data, uint32 len)
|
|
|
|
{
|
|
|
|
uint32 i;
|
|
|
|
|
|
|
|
if (*jp->statep == JSON_PARSE_STATE_INIT) {
|
2009-03-02 14:00:28 -08:00
|
|
|
PushState(cx, jp, JSON_PARSE_STATE_VALUE);
|
2008-10-06 13:54:12 -07:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:31:07 -07:00
|
|
|
for (i = 0; i < len; i++) {
|
2008-10-06 13:54:12 -07:00
|
|
|
jschar c = data[i];
|
|
|
|
switch (*jp->statep) {
|
2009-03-02 14:00:28 -08:00
|
|
|
case JSON_PARSE_STATE_VALUE:
|
|
|
|
if (c == ']') {
|
|
|
|
// empty array
|
|
|
|
if (!PopState(cx, jp))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-08-24 08:50:54 -07:00
|
|
|
if (*jp->statep != JSON_PARSE_STATE_ARRAY)
|
|
|
|
return JSONParseError(jp, cx);
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2009-03-02 14:00:28 -08:00
|
|
|
if (!CloseArray(cx, jp) || !PopState(cx, jp))
|
|
|
|
return JS_FALSE;
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2009-03-02 14:00:28 -08:00
|
|
|
break;
|
|
|
|
}
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2009-03-02 14:00:28 -08:00
|
|
|
if (c == '}') {
|
|
|
|
// we should only find these in OBJECT_KEY state
|
2010-08-24 08:50:54 -07:00
|
|
|
return JSONParseError(jp, cx);
|
2009-03-02 14:00:28 -08:00
|
|
|
}
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2009-03-02 14:00:28 -08:00
|
|
|
if (c == '"') {
|
|
|
|
*jp->statep = JSON_PARSE_STATE_STRING;
|
2008-10-06 13:54:12 -07:00
|
|
|
break;
|
2009-03-02 14:00:28 -08:00
|
|
|
}
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2009-03-02 14:00:28 -08:00
|
|
|
if (IsNumChar(c)) {
|
|
|
|
*jp->statep = JSON_PARSE_STATE_NUMBER;
|
2009-08-13 17:22:55 -07:00
|
|
|
if (!jp->buffer.append(c))
|
|
|
|
return JS_FALSE;
|
2008-10-06 13:54:12 -07:00
|
|
|
break;
|
2009-03-02 14:00:28 -08:00
|
|
|
}
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2009-03-02 14:00:28 -08:00
|
|
|
if (JS7_ISLET(c)) {
|
|
|
|
*jp->statep = JSON_PARSE_STATE_KEYWORD;
|
2009-08-13 17:22:55 -07:00
|
|
|
if (!jp->buffer.append(c))
|
|
|
|
return JS_FALSE;
|
2008-10-06 13:54:12 -07:00
|
|
|
break;
|
2009-03-02 14:00:28 -08:00
|
|
|
}
|
2008-10-08 10:31:07 -07:00
|
|
|
|
2009-03-02 14:00:28 -08:00
|
|
|
// fall through in case the value is an object or array
|
|
|
|
case JSON_PARSE_STATE_OBJECT_VALUE:
|
|
|
|
if (c == '{') {
|
|
|
|
*jp->statep = JSON_PARSE_STATE_OBJECT;
|
|
|
|
if (!OpenObject(cx, jp) || !PushState(cx, jp, JSON_PARSE_STATE_OBJECT_PAIR))
|
|
|
|
return JS_FALSE;
|
|
|
|
} else if (c == '[') {
|
|
|
|
*jp->statep = JSON_PARSE_STATE_ARRAY;
|
|
|
|
if (!OpenArray(cx, jp) || !PushState(cx, jp, JSON_PARSE_STATE_VALUE))
|
|
|
|
return JS_FALSE;
|
|
|
|
} else if (!JS_ISXMLSPACE(c)) {
|
2010-08-24 08:50:54 -07:00
|
|
|
return JSONParseError(jp, cx);
|
2009-03-02 14:00:28 -08:00
|
|
|
}
|
|
|
|
break;
|
2008-10-08 10:31:07 -07:00
|
|
|
|
2009-03-02 14:00:28 -08:00
|
|
|
case JSON_PARSE_STATE_OBJECT:
|
|
|
|
if (c == '}') {
|
|
|
|
if (!CloseObject(cx, jp) || !PopState(cx, jp))
|
|
|
|
return JS_FALSE;
|
|
|
|
} else if (c == ',') {
|
|
|
|
if (!PushState(cx, jp, JSON_PARSE_STATE_OBJECT_PAIR))
|
|
|
|
return JS_FALSE;
|
|
|
|
} else if (c == ']' || !JS_ISXMLSPACE(c)) {
|
2010-08-24 08:50:54 -07:00
|
|
|
return JSONParseError(jp, cx);
|
2009-03-02 14:00:28 -08:00
|
|
|
}
|
|
|
|
break;
|
2008-10-08 10:31:07 -07:00
|
|
|
|
2010-08-24 08:50:54 -07:00
|
|
|
case JSON_PARSE_STATE_ARRAY:
|
2009-03-02 14:00:28 -08:00
|
|
|
if (c == ']') {
|
|
|
|
if (!CloseArray(cx, jp) || !PopState(cx, jp))
|
|
|
|
return JS_FALSE;
|
|
|
|
} else if (c == ',') {
|
|
|
|
if (!PushState(cx, jp, JSON_PARSE_STATE_VALUE))
|
|
|
|
return JS_FALSE;
|
|
|
|
} else if (!JS_ISXMLSPACE(c)) {
|
2010-08-24 08:50:54 -07:00
|
|
|
return JSONParseError(jp, cx);
|
2009-03-02 14:00:28 -08:00
|
|
|
}
|
|
|
|
break;
|
2008-10-08 10:31:07 -07:00
|
|
|
|
2010-08-24 08:50:54 -07:00
|
|
|
case JSON_PARSE_STATE_OBJECT_PAIR:
|
2009-03-02 14:00:28 -08:00
|
|
|
if (c == '"') {
|
|
|
|
// we want to be waiting for a : when the string has been read
|
|
|
|
*jp->statep = JSON_PARSE_STATE_OBJECT_IN_PAIR;
|
|
|
|
if (!PushState(cx, jp, JSON_PARSE_STATE_STRING))
|
|
|
|
return JS_FALSE;
|
|
|
|
} else if (c == '}') {
|
|
|
|
// pop off the object pair state and the object state
|
|
|
|
if (!CloseObject(cx, jp) || !PopState(cx, jp) || !PopState(cx, jp))
|
|
|
|
return JS_FALSE;
|
|
|
|
} else if (c == ']' || !JS_ISXMLSPACE(c)) {
|
2010-08-24 08:50:54 -07:00
|
|
|
return JSONParseError(jp, cx);
|
2009-03-02 14:00:28 -08:00
|
|
|
}
|
|
|
|
break;
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2009-03-02 14:00:28 -08:00
|
|
|
case JSON_PARSE_STATE_OBJECT_IN_PAIR:
|
|
|
|
if (c == ':') {
|
|
|
|
*jp->statep = JSON_PARSE_STATE_VALUE;
|
|
|
|
} else if (!JS_ISXMLSPACE(c)) {
|
2010-08-24 08:50:54 -07:00
|
|
|
return JSONParseError(jp, cx);
|
2009-03-02 14:00:28 -08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSON_PARSE_STATE_STRING:
|
|
|
|
if (c == '"') {
|
|
|
|
if (!PopState(cx, jp))
|
|
|
|
return JS_FALSE;
|
|
|
|
JSONDataType jdt;
|
|
|
|
if (*jp->statep == JSON_PARSE_STATE_OBJECT_IN_PAIR) {
|
|
|
|
jdt = JSON_DATA_KEYSTRING;
|
|
|
|
} else {
|
|
|
|
jdt = JSON_DATA_STRING;
|
|
|
|
}
|
|
|
|
if (!HandleData(cx, jp, jdt))
|
|
|
|
return JS_FALSE;
|
|
|
|
} else if (c == '\\') {
|
|
|
|
*jp->statep = JSON_PARSE_STATE_STRING_ESCAPE;
|
2010-04-01 13:20:35 -07:00
|
|
|
} else if (c < 31) {
|
|
|
|
// The JSON lexical grammer does not allow a JSONStringCharacter to be
|
|
|
|
// any of the Unicode characters U+0000 thru U+001F (control characters).
|
2010-08-24 08:50:54 -07:00
|
|
|
return JSONParseError(jp, cx);
|
2009-03-02 14:00:28 -08:00
|
|
|
} else {
|
2009-08-13 17:22:55 -07:00
|
|
|
if (!jp->buffer.append(c))
|
|
|
|
return JS_FALSE;
|
2009-03-02 14:00:28 -08:00
|
|
|
}
|
|
|
|
break;
|
2008-10-08 10:31:07 -07:00
|
|
|
|
2009-03-02 14:00:28 -08:00
|
|
|
case JSON_PARSE_STATE_STRING_ESCAPE:
|
|
|
|
switch (c) {
|
|
|
|
case '"':
|
|
|
|
case '\\':
|
|
|
|
case '/':
|
|
|
|
break;
|
|
|
|
case 'b' : c = '\b'; break;
|
|
|
|
case 'f' : c = '\f'; break;
|
|
|
|
case 'n' : c = '\n'; break;
|
|
|
|
case 'r' : c = '\r'; break;
|
|
|
|
case 't' : c = '\t'; break;
|
|
|
|
default :
|
|
|
|
if (c == 'u') {
|
2008-10-06 13:54:12 -07:00
|
|
|
jp->numHex = 0;
|
2009-03-02 14:00:28 -08:00
|
|
|
jp->hexChar = 0;
|
|
|
|
*jp->statep = JSON_PARSE_STATE_STRING_HEX;
|
|
|
|
continue;
|
|
|
|
} else {
|
2010-08-24 08:50:54 -07:00
|
|
|
return JSONParseError(jp, cx);
|
2008-10-06 13:54:12 -07:00
|
|
|
}
|
2009-03-02 14:00:28 -08:00
|
|
|
}
|
2008-10-08 10:31:07 -07:00
|
|
|
|
2009-08-13 17:22:55 -07:00
|
|
|
if (!jp->buffer.append(c))
|
|
|
|
return JS_FALSE;
|
2009-03-02 14:00:28 -08:00
|
|
|
*jp->statep = JSON_PARSE_STATE_STRING;
|
|
|
|
break;
|
2008-10-08 10:31:07 -07:00
|
|
|
|
2009-03-02 14:00:28 -08:00
|
|
|
case JSON_PARSE_STATE_STRING_HEX:
|
|
|
|
if (('0' <= c) && (c <= '9')) {
|
|
|
|
jp->hexChar = (jp->hexChar << 4) | (c - '0');
|
|
|
|
} else if (('a' <= c) && (c <= 'f')) {
|
|
|
|
jp->hexChar = (jp->hexChar << 4) | (c - 'a' + 0x0a);
|
|
|
|
} else if (('A' <= c) && (c <= 'F')) {
|
|
|
|
jp->hexChar = (jp->hexChar << 4) | (c - 'A' + 0x0a);
|
|
|
|
} else {
|
2010-08-24 08:50:54 -07:00
|
|
|
return JSONParseError(jp, cx);
|
2009-03-02 14:00:28 -08:00
|
|
|
}
|
2009-07-27 18:40:12 -07:00
|
|
|
|
2009-03-02 14:00:28 -08:00
|
|
|
if (++(jp->numHex) == 4) {
|
2009-08-13 17:22:55 -07:00
|
|
|
if (!jp->buffer.append(jp->hexChar))
|
|
|
|
return JS_FALSE;
|
2009-03-02 14:00:28 -08:00
|
|
|
jp->hexChar = 0;
|
|
|
|
jp->numHex = 0;
|
|
|
|
*jp->statep = JSON_PARSE_STATE_STRING;
|
|
|
|
}
|
|
|
|
break;
|
2008-10-08 10:31:07 -07:00
|
|
|
|
2009-03-02 14:00:28 -08:00
|
|
|
case JSON_PARSE_STATE_KEYWORD:
|
|
|
|
if (JS7_ISLET(c)) {
|
2009-08-13 17:22:55 -07:00
|
|
|
if (!jp->buffer.append(c))
|
|
|
|
return JS_FALSE;
|
2009-03-02 14:00:28 -08:00
|
|
|
} else {
|
|
|
|
// this character isn't part of the keyword, process it again
|
|
|
|
i--;
|
|
|
|
if (!PopState(cx, jp))
|
|
|
|
return JS_FALSE;
|
2009-07-27 18:40:12 -07:00
|
|
|
|
2009-03-02 14:00:28 -08:00
|
|
|
if (!HandleData(cx, jp, JSON_DATA_KEYWORD))
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
break;
|
2008-10-08 10:31:07 -07:00
|
|
|
|
2009-03-02 14:00:28 -08:00
|
|
|
case JSON_PARSE_STATE_NUMBER:
|
|
|
|
if (IsNumChar(c)) {
|
2009-08-13 17:22:55 -07:00
|
|
|
if (!jp->buffer.append(c))
|
|
|
|
return JS_FALSE;
|
2009-03-02 14:00:28 -08:00
|
|
|
} else {
|
|
|
|
// this character isn't part of the number, process it again
|
|
|
|
i--;
|
|
|
|
if (!PopState(cx, jp))
|
|
|
|
return JS_FALSE;
|
|
|
|
if (!HandleData(cx, jp, JSON_DATA_NUMBER))
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
break;
|
2008-10-06 13:54:12 -07:00
|
|
|
|
2009-03-02 14:00:28 -08:00
|
|
|
case JSON_PARSE_STATE_FINISHED:
|
|
|
|
if (!JS_ISXMLSPACE(c)) {
|
|
|
|
// extra input
|
2010-08-24 08:50:54 -07:00
|
|
|
return JSONParseError(jp, cx);
|
2009-03-02 14:00:28 -08:00
|
|
|
}
|
|
|
|
break;
|
2008-10-08 10:31:07 -07:00
|
|
|
|
2009-03-02 14:00:28 -08:00
|
|
|
default:
|
|
|
|
JS_NOT_REACHED("Invalid JSON parser state");
|
2009-04-21 12:09:27 -07:00
|
|
|
}
|
2008-10-06 13:54:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if JS_HAS_TOSOURCE
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
json_toSource(JSContext *cx, uintN argc, Value *vp)
|
2008-10-06 13:54:12 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setString(ATOM_TO_STRING(CLASS_ATOM(cx, JSON)));
|
2008-10-06 13:54:12 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static JSFunctionSpec json_static_methods[] = {
|
|
|
|
#if JS_HAS_TOSOURCE
|
|
|
|
JS_FN(js_toSource_str, json_toSource, 0, 0),
|
|
|
|
#endif
|
2010-04-01 13:19:45 -07:00
|
|
|
JS_FN("parse", js_json_parse, 2, 0),
|
|
|
|
JS_FN("stringify", js_json_stringify, 3, 0),
|
2008-10-06 13:54:12 -07:00
|
|
|
JS_FS_END
|
|
|
|
};
|
|
|
|
|
|
|
|
JSObject *
|
|
|
|
js_InitJSONClass(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
JSObject *JSON;
|
|
|
|
|
2010-08-09 09:11:22 -07:00
|
|
|
JSON = NewNonFunction<WithProto::Class>(cx, &js_JSONClass, NULL, obj);
|
2008-10-06 13:54:12 -07:00
|
|
|
if (!JSON)
|
|
|
|
return NULL;
|
|
|
|
if (!JS_DefineProperty(cx, obj, js_JSON_str, OBJECT_TO_JSVAL(JSON),
|
2008-10-15 22:20:57 -07:00
|
|
|
JS_PropertyStub, JS_PropertyStub, 0))
|
2008-10-06 13:54:12 -07:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!JS_DefineFunctions(cx, JSON, json_static_methods))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return JSON;
|
|
|
|
}
|