From 5640581cbe41dc792fc1f711bb0278e22cf524a2 Mon Sep 17 00:00:00 2001 From: Jeff Walden Date: Wed, 10 Aug 2011 14:54:52 -0700 Subject: [PATCH] Bug 686582 - Make the JSON Walk function do its walking over array elements using getElement instead of getProperty, a first step toward actually using element-based methods now being specialized. r=dvander --HG-- extra : rebase_source : 9816241003454d8b23c8903c97656993cb834ea1 --- js/src/json.cpp | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/js/src/json.cpp b/js/src/json.cpp index 03e5fb7b0ceb..ed65c3fa69fc 100644 --- a/js/src/json.cpp +++ b/js/src/json.cpp @@ -281,12 +281,33 @@ class CycleDetector JSObject *const obj; }; +template +class KeyStringifier { +}; + +template<> +class KeyStringifier { + public: + static JSString *toString(JSContext *cx, uint32 index) { + return IndexToString(cx, index); + } +}; + +template<> +class KeyStringifier { + public: + static JSString *toString(JSContext *cx, jsid id) { + return IdToString(cx, id); + } +}; + /* * ES5 15.12.3 Str, steps 2-4, extracted to enable preprocessing of property * values when stringifying objects in JO. */ +template static bool -PreprocessValue(JSContext *cx, JSObject *holder, jsid key, Value *vp, StringifyContext *scx) +PreprocessValue(JSContext *cx, JSObject *holder, KeyType key, Value *vp, StringifyContext *scx) { JSString *keyStr = NULL; @@ -298,7 +319,7 @@ PreprocessValue(JSContext *cx, JSObject *holder, jsid key, Value *vp, StringifyC return false; if (js_IsCallable(toJSON)) { - keyStr = IdToString(cx, key); + keyStr = KeyStringifier::toString(cx, key); if (!keyStr) return false; @@ -319,7 +340,7 @@ PreprocessValue(JSContext *cx, JSObject *holder, jsid key, Value *vp, StringifyC /* Step 3. */ if (scx->replacer && scx->replacer->isCallable()) { if (!keyStr) { - keyStr = IdToString(cx, key); + keyStr = KeyStringifier::toString(cx, key); if (!keyStr) return false; } @@ -495,18 +516,16 @@ JA(JSContext *cx, JSObject *obj, StringifyContext *scx) /* Steps 7-10. */ Value outputValue; - for (jsuint i = 0; i < length; i++) { - jsid id = INT_TO_JSID(i); - + for (uint32 i = 0; i < length; i++) { /* * Steps 8a-8c. Again note how the call to the spec's Str method * is broken up into getting the property, running it past toJSON * and the replacer and maybe unboxing, and interpreting some * values as |null| in separate steps. */ - if (!obj->getProperty(cx, id, &outputValue)) + if (!obj->getElement(cx, i, &outputValue)) return JS_FALSE; - if (!PreprocessValue(cx, obj, id, &outputValue, scx)) + if (!PreprocessValue(cx, obj, i, &outputValue, scx)) return JS_FALSE; if (IsFilteredValue(outputValue)) { if (!scx->sb.append("null"))