value-array-proxy.js (1326B)
1 // Copyright (C) 2016 the V8 project authors. All rights reserved. 2 // This code is governed by the BSD license found in the LICENSE file. 3 /*--- 4 esid: sec-serializejsonarray 5 description: > 6 Proxy of an array is treated as an array. 7 info: | 8 JSON.stringify ( value [ , replacer [ , space ] ] ) 9 10 [...] 11 12. Return ? SerializeJSONProperty(the empty String, wrapper). 12 13 SerializeJSONProperty ( key, holder ) 14 15 [...] 16 10. If Type(value) is Object and IsCallable(value) is false, then 17 a. Let isArray be ? IsArray(value). 18 b. If isArray is true, return ? SerializeJSONArray(value). 19 20 SerializeJSONArray ( value ) 21 22 [...] 23 6. Let len be ? LengthOfArrayLike(value). 24 7. Let index be 0. 25 8. Repeat, while index < len 26 a. Let strP be ? SerializeJSONProperty(! ToString(index), value). 27 features: [Proxy] 28 ---*/ 29 30 var arrayProxy = new Proxy([], { 31 get: function(_target, key) { 32 if (key === 'length') return 2; 33 return Number(key); 34 }, 35 }); 36 37 assert.sameValue( 38 JSON.stringify(arrayProxy), '[0,1]', 'proxy for an array' 39 ); 40 assert.sameValue( 41 JSON.stringify([[arrayProxy]]), '[[[0,1]]]', 'proxy for an array (nested)' 42 ); 43 44 var arrayProxyProxy = new Proxy(arrayProxy, {}); 45 assert.sameValue( 46 JSON.stringify([[arrayProxyProxy]]), 47 '[[[0,1]]]', 48 'proxy for a proxy for an array (nested)' 49 ); 50 51 reportCompare(0, 0);