Reviewers: adamk,
Message:
PTAL
Description:
JSON.stringify should handle the replacer before the space
BUG=v8:4227
LOG=N
R=adamk
CQ_INCLUDE_TRYBOTS=tryserver.chromium.linux:linux_chromium_rel_ng;tryserver.blink:linux_blink_rel
Please review this at https://codereview.chromium.org/1200373003/
Base URL: https://chromium.googlesource.com/v8/v8.git@master
Affected files (+47, -21 lines):
M src/json.js
A test/mjsunit/json-replacer-order.js
Index: src/json.js
diff --git a/src/json.js b/src/json.js
index
8b5f2294491433b26cd3b026b768772f0ada0697..ee3ee2d995acaaf2faf8b3613c17d5cccf5faa13
100644
--- a/src/json.js
+++ b/src/json.js
@@ -183,27 +183,6 @@ function JSONStringify(value, replacer, space) {
if (%_ArgumentsLength() == 1) {
return %BasicJSONStringify(value);
}
- if (IS_OBJECT(space)) {
- // Unwrap 'space' if it is wrapped
- if (IS_NUMBER_WRAPPER(space)) {
- space = $toNumber(space);
- } else if (IS_STRING_WRAPPER(space)) {
- space = $toString(space);
- }
- }
- var gap;
- if (IS_NUMBER(space)) {
- space = MathMax(0, MathMin($toInteger(space), 10));
- gap = %_SubString(" ", 0, space);
- } else if (IS_STRING(space)) {
- if (space.length > 10) {
- gap = %_SubString(space, 0, 10);
- } else {
- gap = space;
- }
- } else {
- gap = "";
- }
if (IS_ARRAY(replacer)) {
// Deduplicate replacer array items.
var property_list = new InternalArray();
@@ -226,6 +205,27 @@ function JSONStringify(value, replacer, space) {
}
replacer = property_list;
}
+ if (IS_OBJECT(space)) {
+ // Unwrap 'space' if it is wrapped
+ if (IS_NUMBER_WRAPPER(space)) {
+ space = $toNumber(space);
+ } else if (IS_STRING_WRAPPER(space)) {
+ space = $toString(space);
+ }
+ }
+ var gap;
+ if (IS_NUMBER(space)) {
+ space = MathMax(0, MathMin($toInteger(space), 10));
+ gap = %_SubString(" ", 0, space);
+ } else if (IS_STRING(space)) {
+ if (space.length > 10) {
+ gap = %_SubString(space, 0, 10);
+ } else {
+ gap = space;
+ }
+ } else {
+ gap = "";
+ }
return JSONSerialize('', {'': value}, replacer, new InternalArray(), "",
gap);
}
Index: test/mjsunit/json-replacer-order.js
diff --git a/test/mjsunit/json-replacer-order.js
b/test/mjsunit/json-replacer-order.js
new file mode 100644
index
0000000000000000000000000000000000000000..8cb64414e726cbe908e82942524b43956aa4f727
--- /dev/null
+++ b/test/mjsunit/json-replacer-order.js
@@ -0,0 +1,26 @@
+// Copyright 2015 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// http://ecma-international.org/ecma-262/6.0/#sec-json.stringify
+// Step 4.b.iii.5.f.i
+
+var log = [];
+
+var replacer = Object.defineProperty([], 0, {
+ get() {
+ log.push('get 0');
+ }
+});
+var space = Object.defineProperty(new String, 'toString', {
+ value() {
+ log.push('toString');
+ return '';
+ }
+});
+
+JSON.stringify('', replacer, space);
+
+assertEquals(2, log.length);
+assertEquals('get 0', log[0]);
+assertEquals('toString', log[1]);
--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.