ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 1 | // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 28 | // This file relies on the fact that the following declarations have been made |
| 29 | // in runtime.js: |
| 30 | // var $Array = global.Array; |
| 31 | // var $String = global.String; |
| 32 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 33 | var $JSON = global.JSON; |
| 34 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 35 | // ------------------------------------------------------------------- |
| 36 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 37 | function Revive(holder, name, reviver) { |
| 38 | var val = holder[name]; |
| 39 | if (IS_OBJECT(val)) { |
| 40 | if (IS_ARRAY(val)) { |
| 41 | var length = val.length; |
| 42 | for (var i = 0; i < length; i++) { |
| 43 | var newElement = Revive(val, $String(i), reviver); |
| 44 | val[i] = newElement; |
| 45 | } |
| 46 | } else { |
| 47 | for (var p in val) { |
whesse@chromium.org | 7a392b3 | 2011-01-31 11:30:36 +0000 | [diff] [blame] | 48 | if (%_CallFunction(val, p, ObjectHasOwnProperty)) { |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 49 | var newElement = Revive(val, p, reviver); |
| 50 | if (IS_UNDEFINED(newElement)) { |
| 51 | delete val[p]; |
| 52 | } else { |
| 53 | val[p] = newElement; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 59 | return %_CallFunction(holder, name, val, reviver); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | function JSONParse(text, reviver) { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 63 | var unfiltered = %ParseJson(TO_STRING_INLINE(text)); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 64 | if (IS_SPEC_FUNCTION(reviver)) { |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 65 | return Revive({'': unfiltered}, '', reviver); |
| 66 | } else { |
| 67 | return unfiltered; |
| 68 | } |
| 69 | } |
| 70 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 71 | function SerializeArray(value, replacer, stack, indent, gap) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 72 | if (!%PushIfAbsent(stack, value)) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 73 | throw MakeTypeError('circular_structure', $Array()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 74 | } |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 75 | var stepback = indent; |
| 76 | indent += gap; |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 77 | var partial = new InternalArray(); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 78 | var len = value.length; |
| 79 | for (var i = 0; i < len; i++) { |
| 80 | var strP = JSONSerialize($String(i), value, replacer, stack, |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 81 | indent, gap); |
| 82 | if (IS_UNDEFINED(strP)) { |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 83 | strP = "null"; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 84 | } |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 85 | partial.push(strP); |
| 86 | } |
| 87 | var final; |
| 88 | if (gap == "") { |
| 89 | final = "[" + partial.join(",") + "]"; |
| 90 | } else if (partial.length > 0) { |
| 91 | var separator = ",\n" + indent; |
| 92 | final = "[\n" + indent + partial.join(separator) + "\n" + |
| 93 | stepback + "]"; |
| 94 | } else { |
| 95 | final = "[]"; |
| 96 | } |
| 97 | stack.pop(); |
| 98 | return final; |
| 99 | } |
| 100 | |
| 101 | function SerializeObject(value, replacer, stack, indent, gap) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 102 | if (!%PushIfAbsent(stack, value)) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 103 | throw MakeTypeError('circular_structure', $Array()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 104 | } |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 105 | var stepback = indent; |
| 106 | indent += gap; |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 107 | var partial = new InternalArray(); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 108 | if (IS_ARRAY(replacer)) { |
| 109 | var length = replacer.length; |
| 110 | for (var i = 0; i < length; i++) { |
whesse@chromium.org | 7a392b3 | 2011-01-31 11:30:36 +0000 | [diff] [blame] | 111 | if (%_CallFunction(replacer, i, ObjectHasOwnProperty)) { |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 112 | var p = replacer[i]; |
| 113 | var strP = JSONSerialize(p, value, replacer, stack, indent, gap); |
| 114 | if (!IS_UNDEFINED(strP)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 115 | var member = %QuoteJSONString(p) + ":"; |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 116 | if (gap != "") member += " "; |
| 117 | member += strP; |
| 118 | partial.push(member); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } else { |
| 123 | for (var p in value) { |
whesse@chromium.org | 7a392b3 | 2011-01-31 11:30:36 +0000 | [diff] [blame] | 124 | if (%_CallFunction(value, p, ObjectHasOwnProperty)) { |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 125 | var strP = JSONSerialize(p, value, replacer, stack, indent, gap); |
| 126 | if (!IS_UNDEFINED(strP)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 127 | var member = %QuoteJSONString(p) + ":"; |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 128 | if (gap != "") member += " "; |
| 129 | member += strP; |
| 130 | partial.push(member); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | var final; |
| 136 | if (gap == "") { |
| 137 | final = "{" + partial.join(",") + "}"; |
| 138 | } else if (partial.length > 0) { |
| 139 | var separator = ",\n" + indent; |
| 140 | final = "{\n" + indent + partial.join(separator) + "\n" + |
| 141 | stepback + "}"; |
| 142 | } else { |
| 143 | final = "{}"; |
| 144 | } |
| 145 | stack.pop(); |
| 146 | return final; |
| 147 | } |
| 148 | |
| 149 | function JSONSerialize(key, holder, replacer, stack, indent, gap) { |
| 150 | var value = holder[key]; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 151 | if (IS_SPEC_OBJECT(value)) { |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 152 | var toJSON = value.toJSON; |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 153 | if (IS_SPEC_FUNCTION(toJSON)) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 154 | value = %_CallFunction(value, key, toJSON); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 155 | } |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 156 | } |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 157 | if (IS_SPEC_FUNCTION(replacer)) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 158 | value = %_CallFunction(holder, key, value, replacer); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 159 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 160 | if (IS_STRING(value)) { |
| 161 | return %QuoteJSONString(value); |
| 162 | } else if (IS_NUMBER(value)) { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 163 | return JSON_NUMBER_TO_STRING(value); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 164 | } else if (IS_BOOLEAN(value)) { |
| 165 | return value ? "true" : "false"; |
| 166 | } else if (IS_NULL(value)) { |
| 167 | return "null"; |
| 168 | } else if (IS_SPEC_OBJECT(value) && !(typeof value == "function")) { |
| 169 | // Non-callable object. If it's a primitive wrapper, it must be unwrapped. |
| 170 | if (IS_ARRAY(value)) { |
| 171 | return SerializeArray(value, replacer, stack, indent, gap); |
| 172 | } else if (IS_NUMBER_WRAPPER(value)) { |
| 173 | value = ToNumber(value); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 174 | return JSON_NUMBER_TO_STRING(value); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 175 | } else if (IS_STRING_WRAPPER(value)) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 176 | return %QuoteJSONString(ToString(value)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 177 | } else if (IS_BOOLEAN_WRAPPER(value)) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 178 | return %_ValueOf(value) ? "true" : "false"; |
| 179 | } else { |
| 180 | return SerializeObject(value, replacer, stack, indent, gap); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 183 | // Undefined or a callable object. |
| 184 | return void 0; |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 185 | } |
| 186 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 187 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 188 | function JSONStringify(value, replacer, space) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 189 | if (%_ArgumentsLength() == 1) { |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 190 | return %BasicJSONStringify(value); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 191 | } |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 192 | if (IS_OBJECT(space)) { |
| 193 | // Unwrap 'space' if it is wrapped |
| 194 | if (IS_NUMBER_WRAPPER(space)) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 195 | space = ToNumber(space); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 196 | } else if (IS_STRING_WRAPPER(space)) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 197 | space = ToString(space); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | var gap; |
| 201 | if (IS_NUMBER(space)) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 202 | space = MathMax(0, MathMin(ToInteger(space), 10)); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 203 | gap = %_SubString(" ", 0, space); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 204 | } else if (IS_STRING(space)) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 205 | if (space.length > 10) { |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 206 | gap = %_SubString(space, 0, 10); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 207 | } else { |
| 208 | gap = space; |
| 209 | } |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 210 | } else { |
| 211 | gap = ""; |
| 212 | } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 213 | return JSONSerialize('', {'': value}, replacer, new InternalArray(), "", gap); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 214 | } |
| 215 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 216 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 217 | // ------------------------------------------------------------------- |
| 218 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 219 | function SetUpJSON() { |
| 220 | %CheckIsBootstrapping(); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 221 | |
| 222 | // Set up non-enumerable properties of the JSON object. |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 223 | InstallFunctions($JSON, DONT_ENUM, $Array( |
| 224 | "parse", JSONParse, |
| 225 | "stringify", JSONStringify |
| 226 | )); |
| 227 | } |
| 228 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 229 | SetUpJSON(); |
| 230 | |
| 231 | |
| 232 | // ------------------------------------------------------------------- |
| 233 | // JSON Builtins |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 234 | |
| 235 | function JSONSerializeAdapter(key, object) { |
| 236 | var holder = {}; |
| 237 | holder[key] = object; |
| 238 | // No need to pass the actual holder since there is no replacer function. |
| 239 | return JSONSerialize(key, holder, void 0, new InternalArray(), "", ""); |
| 240 | } |