Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(468)

Side by Side Diff: pkg/compiler/lib/src/js_backend/native_data.dart

Issue 2150313003: Add JSNative utility class with static methods methods to efficiently manipulate typed JSInterop ob… (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Fix analyzer warnings in js_util_test, skip js_util_test in csp mode and baseline expectations for … Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 library js_backend.native_data; 5 library js_backend.native_data;
6 6
7 import '../common.dart'; 7 import '../common.dart';
8 import '../elements/elements.dart' 8 import '../elements/elements.dart'
9 show ClassElement, Element, FieldElement, FunctionElement, MemberElement; 9 show ClassElement, Element, FieldElement, FunctionElement, MemberElement;
10 import '../native/behavior.dart' show NativeBehavior; 10 import '../native/behavior.dart' show NativeBehavior;
(...skipping 17 matching lines...) Expand all
28 <FunctionElement, NativeBehavior>{}; 28 <FunctionElement, NativeBehavior>{};
29 29
30 /// Cache for [NativeBehavior]s for reading from native fields. 30 /// Cache for [NativeBehavior]s for reading from native fields.
31 Map<MemberElement, NativeBehavior> nativeFieldLoadBehavior = 31 Map<MemberElement, NativeBehavior> nativeFieldLoadBehavior =
32 <FieldElement, NativeBehavior>{}; 32 <FieldElement, NativeBehavior>{};
33 33
34 /// Cache for [NativeBehavior]s for writing to native fields. 34 /// Cache for [NativeBehavior]s for writing to native fields.
35 Map<MemberElement, NativeBehavior> nativeFieldStoreBehavior = 35 Map<MemberElement, NativeBehavior> nativeFieldStoreBehavior =
36 <FieldElement, NativeBehavior>{}; 36 <FieldElement, NativeBehavior>{};
37 37
38 /// Prefix used to escape JS names that are not valid Dart names
39 /// when using JSInterop.
40 static const String _jsInteropEscapePrefix = r'JS$';
41
38 /// Returns `true` if [element] is explicitly marked as part of JsInterop. 42 /// Returns `true` if [element] is explicitly marked as part of JsInterop.
39 bool _isJsInterop(Element element) { 43 bool _isJsInterop(Element element) {
40 return jsInteropNames.containsKey(element.declaration); 44 return jsInteropNames.containsKey(element.declaration);
41 } 45 }
42 46
43 /// Returns [element] as an explicit part of JsInterop. The js interop name is 47 /// Returns [element] as an explicit part of JsInterop. The js interop name is
44 /// expected to be computed later. 48 /// expected to be computed later.
45 void markAsJsInterop(Element element) { 49 void markAsJsInterop(Element element) {
46 jsInteropNames[element.declaration] = null; 50 jsInteropNames[element.declaration] = null;
47 } 51 }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 90
87 String _jsNameHelper(Element element) { 91 String _jsNameHelper(Element element) {
88 String jsInteropName = jsInteropNames[element.declaration]; 92 String jsInteropName = jsInteropNames[element.declaration];
89 assert(invariant(element, !(_isJsInterop(element) && jsInteropName == null), 93 assert(invariant(element, !(_isJsInterop(element) && jsInteropName == null),
90 message: 94 message:
91 'Element $element is js interop but js interop name has not yet ' 95 'Element $element is js interop but js interop name has not yet '
92 'been computed.')); 96 'been computed.'));
93 if (jsInteropName != null && jsInteropName.isNotEmpty) { 97 if (jsInteropName != null && jsInteropName.isNotEmpty) {
94 return jsInteropName; 98 return jsInteropName;
95 } 99 }
96 return element.isLibrary ? 'self' : element.name; 100 return element.isLibrary ? 'self' : getUnescapedJSInteropName(element.name);
97 } 101 }
98 102
99 /// Computes the name for [element] to use in the generated JavaScript. This 103 /// Computes the name for [element] to use in the generated JavaScript. This
100 /// is either given through a native annotation or a js interop annotation. 104 /// is either given through a native annotation or a js interop annotation.
101 String getFixedBackendName(Element element) { 105 String getFixedBackendName(Element element) {
102 String name = nativeMemberName[element.declaration]; 106 String name = nativeMemberName[element.declaration];
103 if (name == null && isJsInterop(element)) { 107 if (name == null && isJsInterop(element)) {
104 // If an element isJsInterop but _isJsInterop is false that means it is 108 // If an element isJsInterop but _isJsInterop is false that means it is
105 // considered interop as the parent class is interop. 109 // considered interop as the parent class is interop.
106 name = _jsNameHelper( 110 name = _jsNameHelper(
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 /// Registers the [behavior] for reading from the native [field]. 213 /// Registers the [behavior] for reading from the native [field].
210 void setNativeFieldLoadBehavior(FieldElement field, NativeBehavior behavior) { 214 void setNativeFieldLoadBehavior(FieldElement field, NativeBehavior behavior) {
211 nativeFieldLoadBehavior[field] = behavior; 215 nativeFieldLoadBehavior[field] = behavior;
212 } 216 }
213 217
214 /// Registers the [behavior] for writing to the native [field]. 218 /// Registers the [behavior] for writing to the native [field].
215 void setNativeFieldStoreBehavior( 219 void setNativeFieldStoreBehavior(
216 FieldElement field, NativeBehavior behavior) { 220 FieldElement field, NativeBehavior behavior) {
217 nativeFieldStoreBehavior[field] = behavior; 221 nativeFieldStoreBehavior[field] = behavior;
218 } 222 }
223
224 /// Apply JS$ escaping scheme to convert possible escaped Dart names into
225 /// JS names.
226 String getUnescapedJSInteropName(String name) {
227 return name.startsWith(_jsInteropEscapePrefix)
228 ? name.substring(_jsInteropEscapePrefix.length)
229 : name;
230 }
219 } 231 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698