From 4bea7961671ec9b8f90467b21b6a72df5fd47a53 Mon Sep 17 00:00:00 2001 From: Matthew Berry Date: Mon, 26 Feb 2024 20:33:49 +0000 Subject: [PATCH] [js_runtime/js_dev_runtime] Prefer Array.isArray over checking prototype Closes https://github.com/dart-lang/sdk/pull/54990 GitOrigin-RevId: 9c51a86c10e70c5f92e8b72894f8d3de138cd17b Change-Id: I4b74f3762ec720b0204331d0945da046be99ba33 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/353784 Reviewed-by: Sigmund Cherem Reviewed-by: Stephen Adams Commit-Queue: Sigmund Cherem --- .../_internal/js_dev_runtime/patch/convert_patch.dart | 10 ++-------- sdk/lib/_internal/js_runtime/lib/convert_patch.dart | 11 ++--------- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/sdk/lib/_internal/js_dev_runtime/patch/convert_patch.dart b/sdk/lib/_internal/js_dev_runtime/patch/convert_patch.dart index ccff79da207..0a72a3a4665 100644 --- a/sdk/lib/_internal/js_dev_runtime/patch/convert_patch.dart +++ b/sdk/lib/_internal/js_dev_runtime/patch/convert_patch.dart @@ -54,10 +54,7 @@ _convertJsonToDart(json, reviver(Object? key, Object? value)) { return e; } - // This test is needed to avoid identifying '{"__proto__":[]}' as an Array. - // TODO(sra): Replace this test with cheaper '#.constructor === Array' when - // bug 621 below is fixed. - if (JS('!', 'Object.getPrototypeOf(#) === Array.prototype', e)) { + if (JS('!', 'Array.isArray(#)', e)) { // In-place update of the elements since JS Array is a Dart List. for (int i = 0; i < JS('!', '#.length', e); i++) { // Use JS indexing to avoid range checks. We know this is the only @@ -99,10 +96,7 @@ _convertJsonToDartLazy(object) { return object; } - // This test is needed to avoid identifying '{"__proto__":[]}' as an array. - // TODO(sra): Replace this test with cheaper '#.constructor === Array' when - // bug https://code.google.com/p/v8/issues/detail?id=621 is fixed. - if (JS('!', 'Object.getPrototypeOf(#) !== Array.prototype', object)) { + if (JS('!', '!Array.isArray(#)', object)) { return _JsonMap(object); } diff --git a/sdk/lib/_internal/js_runtime/lib/convert_patch.dart b/sdk/lib/_internal/js_runtime/lib/convert_patch.dart index 856f29d225b..16ddaa2972a 100644 --- a/sdk/lib/_internal/js_runtime/lib/convert_patch.dart +++ b/sdk/lib/_internal/js_runtime/lib/convert_patch.dart @@ -54,10 +54,7 @@ _convertJsonToDart(json, reviver(Object? key, Object? value)) { return e; } - // This test is needed to avoid identifying '{"__proto__":[]}' as an Array. - // TODO(sra): Replace this test with cheaper '#.constructor === Array' when - // bug 621 below is fixed. - if (JS('bool', 'Object.getPrototypeOf(#) === Array.prototype', e)) { + if (JS('bool', 'Array.isArray(#)', e)) { // In-place update of the elements since JS Array is a Dart List. for (int i = 0; i < JS('int', '#.length', e); i++) { // Use JS indexing to avoid range checks. We know this is the only @@ -99,11 +96,7 @@ _convertJsonToDartLazy(object) { return object; } - // This test is needed to avoid identifying '{"__proto__":[]}' as an array. - // TODO(sra): Replace this test with cheaper '#.constructor === Array' when - // bug https://code.google.com/p/v8/issues/detail?id=621 is fixed. - if (JS( - 'bool', 'Object.getPrototypeOf(#) !== Array.prototype', object)) { + if (JS('bool', '!Array.isArray(#)', object)) { return _JsonMap(object); }