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

Unified Diff: lib/src/compiler/code_generator.dart

Issue 1976383002: enable top-level @JS() getters (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 4 years, 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/runtime/dart_sdk.js ('k') | test/browser/language_tests.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/compiler/code_generator.dart
diff --git a/lib/src/compiler/code_generator.dart b/lib/src/compiler/code_generator.dart
index d07bfb06619611d3aa7cb7238e7f19185343f0e9..ed3d017fa31159a6bf1b1081c5e101c300cad977 100644
--- a/lib/src/compiler/code_generator.dart
+++ b/lib/src/compiler/code_generator.dart
@@ -81,6 +81,9 @@ class CodeGenerator extends GeneralizingAstVisitor
/// In an async* function, this represents the stream controller parameter.
JS.TemporaryId _asyncStarController;
+ /// The top-level reference to 'self' if this is a library tagged with @JS()
+ JS.TemporaryId _self;
+
final _privateNames =
new HashMap<LibraryElement, HashMap<String, JS.TemporaryId>>();
final _initializingFormalTemps =
@@ -219,6 +222,11 @@ class CodeGenerator extends GeneralizingAstVisitor
items.add(new JS.ExportDeclaration(
js.call('const # = Object.create(null)', [_dartxVar])));
}
+
+ if (findAnnotation(library, isPublicJSAnnotation) != null) {
+ _self = new JS.TemporaryId('self');
+ items.add(js.statement('const # = window;', [_self]));
+ }
}
// Collect all Element -> Node mappings, in case we need to forward declare
@@ -2325,6 +2333,13 @@ class CodeGenerator extends GeneralizingAstVisitor
}
JS.PropertyAccess _emitTopLevelName(Element e, {String suffix: ''}) {
+ if (e is TopLevelVariableElement &&
+ e.getter != null &&
+ findAnnotation(e.getter, isPublicJSAnnotation) != null) {
+ var annotationName = getAnnotationName(e.getter, isPublicJSAnnotation);
+ var name = js.string(annotationName ?? e.name);
+ return new JS.PropertyAccess(_self, name);
+ }
String name = getJSExportName(e) + suffix;
return new JS.PropertyAccess(
emitLibraryName(e.library), _propertyName(name));
@@ -2525,7 +2540,7 @@ class CodeGenerator extends GeneralizingAstVisitor
var memberName = _emitMemberName(name, type: type, isStatic: isStatic);
JS.Expression jsTarget = _visit(target);
- if (DynamicInvoke.get(target)) {
+ if (DynamicInvoke.get(target) || DynamicInvoke.get(node.methodName)) {
if (typeArgs != null) {
return js.call('dart.dgsend(#, #, #, #)',
[jsTarget, new JS.ArrayInitializer(typeArgs), memberName, args]);
@@ -2539,16 +2554,8 @@ class CodeGenerator extends GeneralizingAstVisitor
}
jsTarget = new JS.PropertyAccess(jsTarget, memberName);
-
if (typeArgs != null) jsTarget = new JS.Call(jsTarget, typeArgs);
- if (DynamicInvoke.get(node.methodName)) {
- // This is a dynamic call to a statically known target. For example:
- // class Foo { Function bar; }
- // new Foo().bar(); // dynamic call
- return js.call('dart.dcall(#, #)', [jsTarget, args]);
- }
-
return new JS.Call(jsTarget, args);
}
@@ -3027,7 +3034,7 @@ class CodeGenerator extends GeneralizingAstVisitor
if (findAnnotation(classElem, isPublicJSAnnotation) != null) {
var annotationName = getAnnotationName(classElem, isPublicJSAnnotation);
var typeName = js.string(annotationName ?? classElem.name);
- return new JS.PropertyAccess(new JS.Identifier('self'), typeName);
+ return new JS.PropertyAccess(_self, typeName);
}
var typeName = _emitType(type);
if (name != null || element.isFactory) {
« no previous file with comments | « lib/runtime/dart_sdk.js ('k') | test/browser/language_tests.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698