Debian Codegraph @

rhino/testsrc/tests/js1_5/Regress/regress-450369.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 var gTestfile = 'regress-450369.js';
7 //-----------------------------------------------------------------------------
8 var BUGNUMBER = 450369;
9 var summary = 'Crash with JIT and json2.js';
10 var actual = 'No Crash';
11 var expect = 'No Crash';
12
13 jit(true);
14
15 /*
16 json2.js
17 2007-11-06
18
19 Public Domain
20
21 See http://www.JSON.org/js.html
22
23 This file creates a global JSON object containing two methods:
24
25 JSON.stringify(value, whitelist)
26 value any JavaScript value, usually an object or array.
27
28 whitelist an optional that determines how object values are
29 stringified.
30
31 This method produces a JSON text from a JavaScript value.
32 There are three possible ways to stringify an object, depending
33 on the optional whitelist parameter.
34
35 If an object has a toJSON method, then the toJSON() method will be
36 called. The value returned from the toJSON method will be
37 stringified.
38
39 Otherwise, if the optional whitelist parameter is an array, then
40 the elements of the array will be used to select members of the
41 object for stringification.
42
43 Otherwise, if there is no whitelist parameter, then all of the
44 members of the object will be stringified.
45
46 Values that do not have JSON representaions, such as undefined or
47 functions, will not be serialized. Such values in objects will be
48 dropped, in arrays will be replaced with null. JSON.stringify()
49 returns undefined. Dates will be stringified as quoted ISO dates.
50
51 Example:
52
53 var text = JSON.stringify(['e', {pluribus: 'unum'}]);
54 // text is '["e",{"pluribus":"unum"}]'
55
56 JSON.parse(text, filter)
57 This method parses a JSON text to produce an object or
58 array. It can throw a SyntaxError exception.
59
60 The optional filter parameter is a function that can filter and
61 transform the results. It receives each of the keys and values, and
62 its return value is used instead of the original value. If it
63 returns what it received, then structure is not modified. If it
64 returns undefined then the member is deleted.
65
66 Example:
67
68 // Parse the text. If a key contains the string 'date' then
69 // convert the value to a date.
70
71 myData = JSON.parse(text, function (key, value) {
72 return key.indexOf('date') >= 0 ? new Date(value) : value;
73 });
74
75 This is a reference implementation. You are free to copy, modify, or
76 redistribute.
77
78 Use your own copy. It is extremely unwise to load third party
79 code into your pages.
80 */
81
82 /*jslint evil: true */
83 /*extern JSON */
84
85 if (!this.emulatedJSON) {
86
87 emulatedJSON = function () {
88
89 function f(n) { // Format integers to have at least two digits.
90 return n < 10 ? '0' + n : n;
91 }
92
93 Date.prototype.toJSON = function () {
94
95 // Eventually, this method will be based on the date.toISOString method.
96
97 return this.getUTCFullYear() + '-' +
98 f(this.getUTCMonth() + 1) + '-' +
99 f(this.getUTCDate()) + 'T' +
100 f(this.getUTCHours()) + ':' +
101 f(this.getUTCMinutes()) + ':' +
102 f(this.getUTCSeconds()) + 'Z';
103 };
104
105
106 var m = { // table of character substitutions
107 '\b': '\\b',
108 '\t': '\\t',
109 '\n': '\\n',
110 '\f': '\\f',
111 '\r': '\\r',
112 '"' : '\\"',
113 '\\': '\\\\'
114 };
115
116 function stringify(value, whitelist) {
117 var a, // The array holding the partial texts.
118 i, // The loop counter.
119 k, // The member key.
120 l, // Length.
121 r = /["\\\x00-\x1f\x7f-\x9f]/g,
122 v; // The member value.
123
124 switch (typeof value) {
125 case 'string':
126
127 // If the string contains no control characters, no quote characters, and no
128 // backslash characters, then we can safely slap some quotes around it.
129 // Otherwise we must also replace the offending characters with safe sequences.
130
131 return r.test(value) ?
132 '"' + value.replace(r, function (a) {
133 var c = m[a];
134 if (c) {
135 return c;
136 }
137 c = a.charCodeAt();
138 return '\\u00' + Math.floor(c / 16).toString(16) +
139 (c % 16).toString(16);
140 }) + '"' :
141 '"' + value + '"';
142
143 case 'number':
144
145 // JSON numbers must be finite. Encode non-finite numbers as null.
146
147 return isFinite(value) ? String(value) : 'null';
148
149 case 'boolean':
150 case 'null':
151 return String(value);
152
153 case 'object':
154
155 // Due to a specification blunder in ECMAScript,
156 // typeof null is 'object', so watch out for that case.
157
158 if (!value) {
159 return 'null';
160 }
161
162 // If the object has a toJSON method, call it, and stringify the result.
163
164 if (typeof value.toJSON === 'function') {
165 return stringify(value.toJSON());
166 }
167 a = [];
168 if (typeof value.length === 'number' &&
169 !(value.propertyIsEnumerable('length'))) {
170
171 // The object is an array. Stringify every element. Use null as a placeholder
172 // for non-JSON values.
173
174 l = value.length;
175 for (i = 0; i < l; i += 1) {
176 a.push(stringify(value[i], whitelist) || 'null');
177 }
178
179 // Join all of the elements together and wrap them in brackets.
180
181 return '[' + a.join(',') + ']';
182 }
183 if (whitelist) {
184
185 // If a whitelist (array of keys) is provided, use it to select the components
186 // of the object.
187
188 l = whitelist.length;
189 for (i = 0; i < l; i += 1) {
190 k = whitelist[i];
191 if (typeof k === 'string') {
192 v = stringify(value[k], whitelist);
193 if (v) {
194 a.push(stringify(k) + ':' + v);
195 }
196 }
197 }
198 } else {
199
200 // Otherwise, iterate through all of the keys in the object.
201
202 for (k in value) {
203 if (typeof k === 'string') {
204 v = stringify(value[k], whitelist);
205 if (v) {
206 a.push(stringify(k) + ':' + v);
207 }
208 }
209 }
210 }
211
212 // Join all of the member texts together and wrap them in braces.
213
214 return '{' + a.join(',') + '}';
215 }
216 return undefined;
217 }
218
219 return {
220 stringify: stringify,
221 parse: function (text, filter) {
222 var j;
223
224 function walk(k, v) {
225 var i, n;
226 if (v && typeof v === 'object') {
227 for (i in v) {
228 if (Object.prototype.hasOwnProperty.apply(v, [i])) {
229 n = walk(i, v[i]);
230 if (n !== undefined) {
231 v[i] = n;
232 }
233 }
234 }
235 }
236 return filter(k, v);
237 }
238
239
240 // Parsing happens in three stages. In the first stage, we run the text against
241 // regular expressions that look for non-JSON patterns. We are especially
242 // concerned with '()' and 'new' because they can cause invocation, and '='
243 // because it can cause mutation. But just to be safe, we want to reject all
244 // unexpected forms.
245
246 // We split the first stage into 4 regexp operations in order to work around
247 // crippling inefficiencies in IE's and Safari's regexp engines. First we
248 // replace all backslash pairs with '@' (a non-JSON character). Second, we
249 // replace all simple value tokens with ']' characters. Third, we delete all
250 // open brackets that follow a colon or comma or that begin the text. Finally,
251 // we look to see that the remaining characters are only whitespace or ']' or
252 // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
253
254 if (/^[\],:{}\s]*$/.test(text.replace(/\\./g, '@').
255 replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(:?[eE][+\-]?\d+)?/g, ']').
256 replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
257
258 // In the second stage we use the eval function to compile the text into a
259 // JavaScript structure. The '{' operator is subject to a syntactic ambiguity
260 // in JavaScript: it can begin a block or an object literal. We wrap the text
261 // in parens to eliminate the ambiguity.
262
263 j = eval('(' + text + ')');
264
265 // In the optional third stage, we recursively walk the new structure, passing
266 // each name/value pair to a filter function for possible transformation.
267
268 return typeof filter === 'function' ? walk('', j) : j;
269 }
270
271 // If the text is not JSON parseable, then a SyntaxError is thrown.
272
273 throw new SyntaxError('parseJSON');
274 }
275 };
276 }();
277 }
278
279
280 //-----------------------------------------------------------------------------
281 test();
282 //-----------------------------------------------------------------------------
283
284 jit(false);
285
286 function test()
287 {
288 enterFunc ('test');
289 printBugNumber(BUGNUMBER);
290 printStatus (summary);
291
292
293 var testPairs = [
294 ["{}", {}],
295 ["[]", []],
296 ['{"foo":"bar"}', {"foo":"bar"}],
297 ['{"null":null}', {"null":null}],
298 ['{"five":5}', {"five":5}],
299 ]
300
301 var a = [];
302 for (var i=0; i < testPairs.length; i++) {
303 var pair = testPairs[i];
304 var s = emulatedJSON.stringify(pair[1])
305 a[i] = s;
306 }
307 print(a.join("\n"));
308
309 reportCompare(expect, actual, summary);
310
311 exitFunc ('test');
312 }
313