OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // Patch file for dart:convert library. | 5 // Patch file for dart:convert library. |
6 | 6 |
7 import 'dart:_js_helper' show argumentErrorValue, patch; | 7 import 'dart:_js_helper' show argumentErrorValue, patch; |
8 import 'dart:_foreign_helper' show JS; | 8 import 'dart:_foreign_helper' show JS; |
9 import 'dart:_interceptors' show JSExtendableArray; | 9 import 'dart:_interceptors' show JSExtendableArray; |
10 import 'dart:_internal' show MappedIterable, ListIterable; | 10 import 'dart:_internal' show MappedIterable, ListIterable; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 * in-place. | 51 * in-place. |
52 */ | 52 */ |
53 _convertJsonToDart(json, reviver(key, value)) { | 53 _convertJsonToDart(json, reviver(key, value)) { |
54 assert(reviver != null); | 54 assert(reviver != null); |
55 walk(e) { | 55 walk(e) { |
56 // JavaScript null, string, number, bool are in the correct representation. | 56 // JavaScript null, string, number, bool are in the correct representation. |
57 if (JS('bool', '# == null', e) || JS('bool', 'typeof # != "object"', e)) { | 57 if (JS('bool', '# == null', e) || JS('bool', 'typeof # != "object"', e)) { |
58 return e; | 58 return e; |
59 } | 59 } |
60 | 60 |
61 // This test is needed to avoid identifing '{"__proto__":[]}' as an Array. | 61 // This test is needed to avoid identifying '{"__proto__":[]}' as an Array. |
62 // TODO(sra): Replace this test with cheaper '#.constructor === Array' when | 62 // TODO(sra): Replace this test with cheaper '#.constructor === Array' when |
63 // bug 621 below is fixed. | 63 // bug 621 below is fixed. |
64 if (JS('bool', 'Object.getPrototypeOf(#) === Array.prototype', e)) { | 64 if (JS('bool', 'Object.getPrototypeOf(#) === Array.prototype', e)) { |
65 // In-place update of the elements since JS Array is a Dart List. | 65 // In-place update of the elements since JS Array is a Dart List. |
66 for (int i = 0; i < JS('int', '#.length', e); i++) { | 66 for (int i = 0; i < JS('int', '#.length', e); i++) { |
67 // Use JS indexing to avoid range checks. We know this is the only | 67 // Use JS indexing to avoid range checks. We know this is the only |
68 // reference to the list, but the compiler will likely never be able to | 68 // reference to the list, but the compiler will likely never be able to |
69 // tell that this instance of the list cannot have its length changed by | 69 // tell that this instance of the list cannot have its length changed by |
70 // the reviver even though it later will be passed to the reviver at the | 70 // the reviver even though it later will be passed to the reviver at the |
71 // outer level. | 71 // outer level. |
(...skipping 24 matching lines...) Expand all Loading... |
96 | 96 |
97 _convertJsonToDartLazy(object) { | 97 _convertJsonToDartLazy(object) { |
98 // JavaScript null and undefined are represented as null. | 98 // JavaScript null and undefined are represented as null. |
99 if (object == null) return null; | 99 if (object == null) return null; |
100 | 100 |
101 // JavaScript string, number, bool already has the correct representation. | 101 // JavaScript string, number, bool already has the correct representation. |
102 if (JS('bool', 'typeof # != "object"', object)) { | 102 if (JS('bool', 'typeof # != "object"', object)) { |
103 return object; | 103 return object; |
104 } | 104 } |
105 | 105 |
106 // This test is needed to avoid identifing '{"__proto__":[]}' as an array. | 106 // This test is needed to avoid identifying '{"__proto__":[]}' as an array. |
107 // TODO(sra): Replace this test with cheaper '#.constructor === Array' when | 107 // TODO(sra): Replace this test with cheaper '#.constructor === Array' when |
108 // bug https://code.google.com/p/v8/issues/detail?id=621 is fixed. | 108 // bug https://code.google.com/p/v8/issues/detail?id=621 is fixed. |
109 if (JS('bool', 'Object.getPrototypeOf(#) !== Array.prototype', object)) { | 109 if (JS('bool', 'Object.getPrototypeOf(#) !== Array.prototype', object)) { |
110 return new _JsonMap(object); | 110 return new _JsonMap(object); |
111 } | 111 } |
112 | 112 |
113 // Update the elements in place since JS arrays are Dart lists. | 113 // Update the elements in place since JS arrays are Dart lists. |
114 for (int i = 0; i < JS('int', '#.length', object); i++) { | 114 for (int i = 0; i < JS('int', '#.length', object); i++) { |
115 // Use JS indexing to avoid range checks. We know this is the only | 115 // Use JS indexing to avoid range checks. We know this is the only |
116 // reference to the list, but the compiler will likely never be able to | 116 // reference to the list, but the compiler will likely never be able to |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 return super.fuse(next); | 397 return super.fuse(next); |
398 } | 398 } |
399 | 399 |
400 // Currently not intercepting UTF8 decoding. | 400 // Currently not intercepting UTF8 decoding. |
401 @patch | 401 @patch |
402 static String _convertIntercepted( | 402 static String _convertIntercepted( |
403 bool allowMalformed, List<int> codeUnits, int start, int end) { | 403 bool allowMalformed, List<int> codeUnits, int start, int end) { |
404 return null; // This call was not intercepted. | 404 return null; // This call was not intercepted. |
405 } | 405 } |
406 } | 406 } |
OLD | NEW |