2012-10-30 15:29:34 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2013-03-22 08:42:38 +00:00
|
|
|
// Flags: --allow-natives-syntax --expose-externalize-string
|
2012-10-30 15:29:34 +00:00
|
|
|
|
2012-11-02 14:46:57 +00:00
|
|
|
// Test JSON.stringify on the global object.
|
|
|
|
var a = 12345;
|
|
|
|
assertTrue(JSON.stringify(this).indexOf('"a":12345') > 0);
|
2013-03-20 14:07:30 +00:00
|
|
|
assertTrue(JSON.stringify(this, null, 0).indexOf('"a":12345') > 0);
|
2012-11-02 14:46:57 +00:00
|
|
|
|
|
|
|
// Test JSON.stringify of array in dictionary mode.
|
2013-03-20 14:07:30 +00:00
|
|
|
function TestStringify(expected, input) {
|
|
|
|
assertEquals(expected, JSON.stringify(input));
|
|
|
|
assertEquals(expected, JSON.stringify(input, null, 0));
|
|
|
|
}
|
|
|
|
|
2012-11-02 14:46:57 +00:00
|
|
|
var array_1 = [];
|
|
|
|
var array_2 = [];
|
2013-11-26 11:32:39 +00:00
|
|
|
array_1[1<<17] = 1;
|
|
|
|
array_2[1<<17] = function() { return 1; };
|
|
|
|
var nulls = "null,";
|
|
|
|
for (var i = 0; i < 17; i++) {
|
|
|
|
nulls += nulls;
|
2012-11-02 14:46:57 +00:00
|
|
|
}
|
2013-11-26 11:32:39 +00:00
|
|
|
|
2012-11-02 14:46:57 +00:00
|
|
|
expected_1 = '[' + nulls + '1]';
|
|
|
|
expected_2 = '[' + nulls + 'null]';
|
2013-03-20 14:07:30 +00:00
|
|
|
TestStringify(expected_1, array_1);
|
|
|
|
TestStringify(expected_2, array_2);
|
2012-11-02 14:46:57 +00:00
|
|
|
|
|
|
|
// Test JSValue with custom prototype.
|
|
|
|
var num_wrapper = Object(42);
|
|
|
|
num_wrapper.__proto__ = { __proto__: null,
|
|
|
|
toString: function() { return true; } };
|
2013-03-20 14:07:30 +00:00
|
|
|
TestStringify('1', num_wrapper);
|
2012-11-02 14:46:57 +00:00
|
|
|
|
|
|
|
var str_wrapper = Object('2');
|
|
|
|
str_wrapper.__proto__ = { __proto__: null,
|
|
|
|
toString: function() { return true; } };
|
2013-03-20 14:07:30 +00:00
|
|
|
TestStringify('"true"', str_wrapper);
|
2012-11-02 14:46:57 +00:00
|
|
|
|
|
|
|
var bool_wrapper = Object(false);
|
|
|
|
bool_wrapper.__proto__ = { __proto__: null,
|
|
|
|
toString: function() { return true; } };
|
|
|
|
// Note that toString function is not evaluated here!
|
2013-03-20 14:07:30 +00:00
|
|
|
TestStringify('false', bool_wrapper);
|
2012-11-02 14:46:57 +00:00
|
|
|
|
|
|
|
// Test getters.
|
|
|
|
var counter = 0;
|
|
|
|
var getter_obj = { get getter() {
|
|
|
|
counter++;
|
|
|
|
return 123;
|
|
|
|
} };
|
2013-03-20 14:07:30 +00:00
|
|
|
TestStringify('{"getter":123}', getter_obj);
|
|
|
|
assertEquals(2, counter);
|
2012-11-02 14:46:57 +00:00
|
|
|
|
|
|
|
// Test toJSON function.
|
|
|
|
var tojson_obj = { toJSON: function() {
|
|
|
|
counter++;
|
|
|
|
return [1, 2];
|
|
|
|
},
|
|
|
|
a: 1};
|
2013-03-20 14:07:30 +00:00
|
|
|
TestStringify('[1,2]', tojson_obj);
|
|
|
|
assertEquals(4, counter);
|
2012-11-02 14:46:57 +00:00
|
|
|
|
|
|
|
// Test that we don't recursively look for the toJSON function.
|
|
|
|
var tojson_proto_obj = { a: 'fail' };
|
|
|
|
tojson_proto_obj.__proto__ = { toJSON: function() {
|
|
|
|
counter++;
|
|
|
|
return tojson_obj;
|
|
|
|
} };
|
2013-03-20 14:07:30 +00:00
|
|
|
TestStringify('{"a":1}', tojson_proto_obj);
|
2012-11-02 14:46:57 +00:00
|
|
|
|
|
|
|
// Test toJSON produced by a getter.
|
|
|
|
var tojson_via_getter = { get toJSON() {
|
|
|
|
return function(x) {
|
|
|
|
counter++;
|
|
|
|
return 321;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
a: 1 };
|
2013-03-20 14:07:30 +00:00
|
|
|
TestStringify('321', tojson_via_getter);
|
2012-11-02 14:46:57 +00:00
|
|
|
|
2014-03-05 10:54:35 +00:00
|
|
|
assertThrows(function() {
|
|
|
|
JSON.stringify({ get toJSON() { throw "error"; } });
|
|
|
|
});
|
|
|
|
|
2012-11-02 14:46:57 +00:00
|
|
|
// Test toJSON with key.
|
|
|
|
tojson_obj = { toJSON: function(key) { return key + key; } };
|
|
|
|
var tojson_with_key_1 = { a: tojson_obj, b: tojson_obj };
|
2013-03-20 14:07:30 +00:00
|
|
|
TestStringify('{"a":"aa","b":"bb"}', tojson_with_key_1);
|
2012-11-02 14:46:57 +00:00
|
|
|
var tojson_with_key_2 = [ tojson_obj, tojson_obj ];
|
2013-03-20 14:07:30 +00:00
|
|
|
TestStringify('["00","11"]', tojson_with_key_2);
|
2012-11-02 14:46:57 +00:00
|
|
|
|
|
|
|
// Test toJSON with exception.
|
|
|
|
var tojson_ex = { toJSON: function(key) { throw "123" } };
|
|
|
|
assertThrows(function() { JSON.stringify(tojson_ex); });
|
2013-03-20 14:07:30 +00:00
|
|
|
assertThrows(function() { JSON.stringify(tojson_ex, null, 0); });
|
2012-11-02 14:46:57 +00:00
|
|
|
|
2012-11-05 10:53:56 +00:00
|
|
|
// Test toJSON with access to this.
|
|
|
|
var obj = { toJSON: function(key) { return this.a + key; }, a: "x" };
|
2013-03-20 14:07:30 +00:00
|
|
|
TestStringify('{"y":"xy"}', {y: obj});
|
2012-11-05 10:53:56 +00:00
|
|
|
|
2012-11-02 14:46:57 +00:00
|
|
|
// Test holes in arrays.
|
2012-10-30 15:29:34 +00:00
|
|
|
var fast_smi = [1, 2, 3, 4];
|
|
|
|
fast_smi.__proto__ = [7, 7, 7, 7];
|
|
|
|
delete fast_smi[2];
|
|
|
|
assertTrue(%HasFastSmiElements(fast_smi));
|
2013-03-20 14:07:30 +00:00
|
|
|
TestStringify("[1,2,7,4]", fast_smi);
|
2012-10-30 15:29:34 +00:00
|
|
|
|
|
|
|
var fast_double = [1.1, 2, 3, 4];
|
|
|
|
fast_double.__proto__ = [7, 7, 7, 7];
|
|
|
|
|
|
|
|
delete fast_double[2];
|
|
|
|
assertTrue(%HasFastDoubleElements(fast_double));
|
2013-03-20 14:07:30 +00:00
|
|
|
TestStringify("[1.1,2,7,4]", fast_double);
|
2012-10-30 15:29:34 +00:00
|
|
|
|
|
|
|
var fast_obj = [1, 2, {}, {}];
|
|
|
|
fast_obj.__proto__ = [7, 7, 7, 7];
|
|
|
|
|
|
|
|
delete fast_obj[2];
|
|
|
|
assertTrue(%HasFastObjectElements(fast_obj));
|
2013-03-20 14:07:30 +00:00
|
|
|
TestStringify("[1,2,7,{}]", fast_obj);
|
2012-11-05 12:59:35 +00:00
|
|
|
|
|
|
|
var getter_side_effect = { a: 1,
|
|
|
|
get b() {
|
|
|
|
delete this.a;
|
|
|
|
delete this.c;
|
|
|
|
this.e = 5;
|
|
|
|
return 2;
|
|
|
|
},
|
|
|
|
c: 3,
|
|
|
|
d: 4 };
|
|
|
|
assertEquals('{"a":1,"b":2,"d":4}', JSON.stringify(getter_side_effect));
|
|
|
|
assertEquals('{"b":2,"d":4,"e":5}', JSON.stringify(getter_side_effect));
|
|
|
|
|
2013-03-20 14:07:30 +00:00
|
|
|
getter_side_effect = { a: 1,
|
|
|
|
get b() {
|
|
|
|
delete this.a;
|
|
|
|
delete this.c;
|
|
|
|
this.e = 5;
|
|
|
|
return 2;
|
|
|
|
},
|
|
|
|
c: 3,
|
|
|
|
d: 4 };
|
|
|
|
assertEquals('{"a":1,"b":2,"d":4}',
|
|
|
|
JSON.stringify(getter_side_effect, null, 0));
|
|
|
|
assertEquals('{"b":2,"d":4,"e":5}',
|
|
|
|
JSON.stringify(getter_side_effect, null, 0));
|
|
|
|
|
2012-11-05 12:59:35 +00:00
|
|
|
var non_enum = {};
|
|
|
|
non_enum.a = 1;
|
|
|
|
Object.defineProperty(non_enum, "b", { value: 2, enumerable: false });
|
|
|
|
non_enum.c = 3;
|
2013-03-20 14:07:30 +00:00
|
|
|
TestStringify('{"a":1,"c":3}', non_enum);
|
2013-03-22 08:42:38 +00:00
|
|
|
|
|
|
|
var str = "external";
|
|
|
|
try {
|
|
|
|
externalizeString(str, true);
|
|
|
|
} catch (e) { }
|
|
|
|
TestStringify("\"external\"", str, null, 0);
|
2015-02-23 17:57:04 +00:00
|
|
|
|
|
|
|
var o = {};
|
|
|
|
o.somespecialproperty = 10;
|
|
|
|
o["\x19"] = 10;
|
|
|
|
assertThrows("JSON.parse('{\"somespecialproperty\":100, \"\x19\":10}')");
|