Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 8ae984c

Browse files
rakudramacommit-bot@chromium.org
authored andcommitted
[dart2js] Use a Symbol to access JavaScript Array Rti
Use a JavaScript Symbol() for Array rti property. This makes the property non-enumerable, avoiding making the property visible via JS-interop. The code is feature-tested so that IE11 still works with an enumerable String property. Bug: 40535 Change-Id: Ic8b64dac5751989fe4b8d023b0626c3db04676c6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/137245 Commit-Queue: Stephen Adams <[email protected]> Reviewed-by: Sigmund Cherem <[email protected]>
1 parent a54a5ee commit 8ae984c

File tree

12 files changed

+40
-13
lines changed

12 files changed

+40
-13
lines changed

pkg/compiler/lib/src/js_backend/namer.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ class Namer extends ModularNamer {
266266

267267
// 15.1.4 Constructor Properties of the Global Object
268268
"Object", "Function", "Array", "String", "Boolean", "Number", "Date",
269-
"RegExp", "Error", "EvalError", "RangeError", "ReferenceError",
269+
"RegExp", "Symbol", "Error", "EvalError", "RangeError", "ReferenceError",
270270
"SyntaxError", "TypeError", "URIError",
271271

272272
// 15.1.5 Other Properties of the Global Object

pkg/compiler/lib/src/js_emitter/startup_emitter/fragment_emitter.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1942,7 +1942,13 @@ class FragmentEmitter {
19421942
globals.add(new js.Property(js.string(LEAF_TAGS), new js.LiteralNull()));
19431943
}
19441944

1945-
js.ObjectInitializer globalsObject = new js.ObjectInitializer(globals);
1945+
globals.add(js.Property(
1946+
js.string(ARRAY_RTI_PROPERTY),
1947+
js.js(r'typeof Symbol == "function" && typeof Symbol() == "symbol"'
1948+
r' ? Symbol("$ti")'
1949+
r' : "$ti"')));
1950+
1951+
js.ObjectInitializer globalsObject = js.ObjectInitializer(globals);
19461952

19471953
return js.js.statement('var init = #;', globalsObject);
19481954
}

pkg/compiler/lib/src/js_emitter/startup_emitter/model_emitter.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'dart:math' show Random;
99

1010
import 'package:js_runtime/shared/embedded_names.dart'
1111
show
12+
ARRAY_RTI_PROPERTY,
1213
DEFERRED_INITIALIZED,
1314
DEFERRED_LIBRARY_PARTS,
1415
DEFERRED_PART_URIS,

pkg/dev_compiler/tool/dart2js_nnbd_sdk_error_golden.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
ERROR|COMPILE_TIME_ERROR|BODY_MIGHT_COMPLETE_NORMALLY|lib/_internal/js_runtime/lib/js_helper.dart|2688|17|17|The body might complete normally, which would cause 'null' to be returned, but the return type is a potentially non-nullable type.
2-
ERROR|COMPILE_TIME_ERROR|BODY_MIGHT_COMPLETE_NORMALLY|lib/_internal/js_runtime/lib/js_helper.dart|3628|5|11|The body might complete normally, which would cause 'null' to be returned, but the return type is a potentially non-nullable type.
3-
ERROR|COMPILE_TIME_ERROR|BODY_MIGHT_COMPLETE_NORMALLY|lib/_internal/js_runtime/lib/js_helper.dart|3656|5|6|The body might complete normally, which would cause 'null' to be returned, but the return type is a potentially non-nullable type.
1+
ERROR|COMPILE_TIME_ERROR|BODY_MIGHT_COMPLETE_NORMALLY|lib/_internal/js_runtime/lib/js_helper.dart|2689|17|17|The body might complete normally, which would cause 'null' to be returned, but the return type is a potentially non-nullable type.
2+
ERROR|COMPILE_TIME_ERROR|BODY_MIGHT_COMPLETE_NORMALLY|lib/_internal/js_runtime/lib/js_helper.dart|3629|5|11|The body might complete normally, which would cause 'null' to be returned, but the return type is a potentially non-nullable type.
3+
ERROR|COMPILE_TIME_ERROR|BODY_MIGHT_COMPLETE_NORMALLY|lib/_internal/js_runtime/lib/js_helper.dart|3657|5|6|The body might complete normally, which would cause 'null' to be returned, but the return type is a potentially non-nullable type.
44
ERROR|COMPILE_TIME_ERROR|INCONSISTENT_INHERITANCE|lib/_internal/js_runtime/lib/interceptors.dart|1637|7|5|Superinterfaces don't have a valid override for '&': int.& (int Function(int)), JSNumber.& (num Function(num)).
55
ERROR|COMPILE_TIME_ERROR|INCONSISTENT_INHERITANCE|lib/_internal/js_runtime/lib/interceptors.dart|1637|7|5|Superinterfaces don't have a valid override for '<<': int.<< (int Function(int)), JSNumber.<< (num Function(num)).
66
ERROR|COMPILE_TIME_ERROR|INCONSISTENT_INHERITANCE|lib/_internal/js_runtime/lib/interceptors.dart|1637|7|5|Superinterfaces don't have a valid override for '>>': int.>> (int Function(int)), JSNumber.>> (num Function(num)).

sdk/lib/_internal/js_runtime/lib/js_helper.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ library _js_helper;
88

99
import 'dart:_js_embedded_names'
1010
show
11+
ARRAY_RTI_PROPERTY,
1112
CURRENT_SCRIPT,
1213
DEFERRED_LIBRARY_PARTS,
1314
DEFERRED_PART_URIS,

sdk/lib/_internal/js_runtime/lib/js_rti.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,15 @@ getMangledTypeName(Type t) {
9898
Object setRuntimeTypeInfo(Object target, var rti) {
9999
if (JS_GET_FLAG('USE_NEW_RTI')) {
100100
assert(rti != null);
101+
var rtiProperty = JS_EMBEDDED_GLOBAL('', ARRAY_RTI_PROPERTY);
102+
JS('var', r'#[#] = #', target, rtiProperty, rti);
103+
return target;
101104
} else {
102105
assert(rti == null || isJsArray(rti));
106+
String rtiName = JS_GET_NAME(JsGetName.RTI_NAME);
107+
JS('var', r'#[#] = #', target, rtiName, rti);
108+
return target;
103109
}
104-
String rtiName = JS_GET_NAME(JsGetName.RTI_NAME);
105-
JS('var', r'#[#] = #', target, rtiName, rti);
106-
return target;
107110
}
108111

109112
/// Returns the runtime type information of [target]. The returned value is a

sdk/lib/_internal/js_runtime/lib/rti.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import 'dart:_js_embedded_names'
3030
JsBuiltin,
3131
JsGetName,
3232
RtiUniverseFieldNames,
33+
ARRAY_RTI_PROPERTY,
3334
CONSTRUCTOR_RTI_CACHE_PROPERTY_NAME,
3435
RTI_UNIVERSE,
3536
TYPES;
@@ -685,7 +686,7 @@ Rti _arrayInstanceType(object) {
685686
// IE11 we would have to synthesise a String property-name with almost zero
686687
// chance of conflict.
687688

688-
var rti = JS('', r'#[#]', object, JS_GET_NAME(JsGetName.RTI_NAME));
689+
var rti = JS('', r'#[#]', object, JS_EMBEDDED_GLOBAL('', ARRAY_RTI_PROPERTY));
689690
var defaultRti = getJSArrayInteropRti();
690691

691692
// Case 3.

sdk/lib/_internal/js_runtime/lib/shared/embedded_names.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ const GET_ISOLATE_TAG = 'getIsolateTag';
123123
// [INTERCEPTORS_BY_TAG] and [LEAF_TAGS].
124124
const ISOLATE_TAG = 'isolateTag';
125125

126+
/// An embedded global that contains the property used to store type information
127+
/// on JavaScript Array instances. This is a Symbol (except for IE11, where is
128+
/// is a String).
129+
const ARRAY_RTI_PROPERTY = 'arrayRti';
130+
126131
/// This embedded global (a function) returns the isolate-specific dispatch-tag
127132
/// that is used to accelerate interceptor calls.
128133
const DISPATCH_PROPERTY_NAME = "dispatchPropertyName";

sdk_nnbd/lib/_internal/js_runtime/lib/js_helper.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ library _js_helper;
66

77
import 'dart:_js_embedded_names'
88
show
9+
ARRAY_RTI_PROPERTY,
910
CURRENT_SCRIPT,
1011
DEFERRED_LIBRARY_PARTS,
1112
DEFERRED_PART_URIS,

sdk_nnbd/lib/_internal/js_runtime/lib/js_rti.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,15 @@ getMangledTypeName(Type t) {
9696
Object setRuntimeTypeInfo(Object target, var rti) {
9797
if (JS_GET_FLAG('USE_NEW_RTI')) {
9898
assert(rti != null);
99+
var rtiProperty = JS_EMBEDDED_GLOBAL('', ARRAY_RTI_PROPERTY);
100+
JS('var', r'#[#] = #', target, rtiProperty, rti);
101+
return target;
99102
} else {
100103
assert(rti == null || isJsArray(rti));
104+
String rtiName = JS_GET_NAME(JsGetName.RTI_NAME);
105+
JS('var', r'#[#] = #', target, rtiName, rti);
106+
return target;
101107
}
102-
String rtiName = JS_GET_NAME(JsGetName.RTI_NAME);
103-
JS('var', r'#[#] = #', target, rtiName, rti);
104-
return target;
105108
}
106109

107110
/// Returns the runtime type information of [target]. The returned value is a

0 commit comments

Comments
 (0)