Skip to content

Commit 98b21b0

Browse files
author
Olivier Chafik
committed
Revert "Use a symbol for static length/name (and other Function properties) for Closure, to avoid ES5->ES6 lowering bug (google/closure-compiler#1460)."
This reverts commit a637e1b048487629d0d44620eb41e703531ce18d.
1 parent 84043ff commit 98b21b0

File tree

5 files changed

+28
-109
lines changed

5 files changed

+28
-109
lines changed

pkg/dev_compiler/lib/src/codegen/js_codegen.dart

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ClosureAnnotator {
8686
final _moduleItems = <JS.Statement>[];
8787
final _temps = new HashMap<Element, JS.TemporaryId>();
8888
final _qualifiedIds = new List<Tuple2<Element, JS.MaybeQualifiedId>>();
89-
final _topLevelExtensionNames = new Set<String>();
9089

9190
/// The name for the library's exports inside itself.
9291
/// `exports` was chosen as the most similar to ES module patterns.
@@ -169,11 +168,6 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ClosureAnnotator {
169168

170169
// Flush any unwritten fields/properties.
171170
_flushLibraryProperties(_moduleItems);
172-
// TODO(ochafik): Refactor to merge this with the dartxNames codepath.
173-
if (_topLevelExtensionNames.isNotEmpty) {
174-
_moduleItems.add(_emitExtensionNamesDeclaration(
175-
_topLevelExtensionNames.map(js.string).toList()));
176-
}
177171

178172
// Mark all qualified names as qualified or not, depending on if they need
179173
// to be loaded lazily or not.
@@ -681,13 +675,6 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ClosureAnnotator {
681675
}
682676
}
683677

684-
JS.Statement _emitExtensionNamesDeclaration(List<JS.Expression> names) =>
685-
js.statement('dart.defineExtensionNames(#)',
686-
[new JS.ArrayInitializer(names.toList(), multiline: true)]);
687-
688-
JS.Expression _emitExtensionName(String name) =>
689-
js.call('dartx.#', _propertyName(name));
690-
691678
_isQualifiedPath(JS.Expression node) =>
692679
node is JS.Identifier ||
693680
node is JS.PropertyAccess &&
@@ -732,7 +719,8 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ClosureAnnotator {
732719
}
733720
}
734721
if (dartxNames.isNotEmpty) {
735-
body.add(_emitExtensionNamesDeclaration(dartxNames));
722+
body.add(js.statement('dart.defineExtensionNames(#)',
723+
[new JS.ArrayInitializer(dartxNames, multiline: true)]));
736724
}
737725
}
738726

@@ -2271,7 +2259,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ClosureAnnotator {
22712259
isLoaded && (field.isConst || _constField.isFieldInitConstant(field));
22722260

22732261
var fieldName = field.name.name;
2274-
if (eagerInit && !JS.invalidStaticFieldName(fieldName, options)) {
2262+
if (eagerInit && !JS.invalidStaticFieldName(fieldName)) {
22752263
return annotateVariable(
22762264
js.statement('#.# = #;', [
22772265
classElem.name,
@@ -2331,7 +2319,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ClosureAnnotator {
23312319
field.element);
23322320
}
23332321

2334-
if (eagerInit && !JS.invalidStaticFieldName(fieldName, options)) {
2322+
if (eagerInit && !JS.invalidStaticFieldName(fieldName)) {
23352323
return annotateVariable(
23362324
js.statement('# = #;', [_visit(field.name), jsInit]), field.element);
23372325
}
@@ -3405,17 +3393,8 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ClosureAnnotator {
34053393
bool unary: false,
34063394
bool isStatic: false,
34073395
bool allowExtensions: true}) {
3408-
// Static members skip the rename steps, except for Function properties.
3409-
if (isStatic) {
3410-
// Avoid colliding with names on Function that are disallowed in ES6,
3411-
// or where we need to work around transpilers.
3412-
if (invalidStaticFieldName(name, options)) {
3413-
_topLevelExtensionNames.add(name);
3414-
return _emitExtensionName(name);
3415-
} else {
3416-
return _propertyName(name);
3417-
}
3418-
}
3396+
// Static members skip the rename steps.
3397+
if (isStatic) return _propertyName(name);
34193398

34203399
if (name.startsWith('_')) {
34213400
return _privateNames.putIfAbsent(
@@ -3442,7 +3421,7 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ClosureAnnotator {
34423421
if (allowExtensions &&
34433422
_extensionTypes.contains(baseType.element) &&
34443423
!_isObjectProperty(name)) {
3445-
return _emitExtensionName(name);
3424+
return js.call('dartx.#', _propertyName(name));
34463425
}
34473426

34483427
return _propertyName(name);

pkg/dev_compiler/lib/src/codegen/js_names.dart

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import 'dart:collection';
66

77
import '../js/js_ast.dart';
8-
import '../options.dart';
98

109
/// Unique instance for temporary variables. Will be renamed consistently
1110
/// across the entire file. Different instances will be named differently
@@ -284,20 +283,14 @@ bool invalidVariableName(String keyword, {bool strictMode: true}) {
284283
return false;
285284
}
286285

287-
/// Returns true for invalid static field names in strict mode or for some
288-
/// transpilers (e.g. when doing ES6->ES5 lowering with the Closure Compiler).
286+
/// Returns true for invalid static field names in strict mode.
289287
/// In particular, "caller" "callee" and "arguments" cannot be used.
290-
bool invalidStaticFieldName(String name, CodegenOptions options) {
288+
bool invalidStaticFieldName(String name) {
291289
switch (name) {
292290
case "arguments":
293291
case "caller":
294292
case "callee":
295293
return true;
296-
// Workarounds for Closure:
297-
// (see https://github.com/google/closure-compiler/issues/1460)
298-
case "name":
299-
case "length":
300-
return options.closure;
301294
}
302295
return false;
303296
}

pkg/dev_compiler/test/codegen/closure.dart

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
library test;
22
import 'dart:js';
33

4+
45
typedef void Callback({int i});
56

67
class Foo<T> {
@@ -14,7 +15,7 @@ class Foo<T> {
1415
factory Foo.build() => new Foo(1, null);
1516

1617
untyped_method(a, b) {}
17-
18+
1819
T pass(T t) => t;
1920

2021
String typed_method(
@@ -28,15 +29,6 @@ class Foo<T> {
2829

2930
static named_params(a, {b, c}) {}
3031

31-
// Avoid colliding with Function.name & Function.length, as Closure fails to
32-
// lower these to ES6 (https://github.com/google/closure-compiler/issues/1460)
33-
static name() => 'Foo.name()';
34-
static length() => 'Foo.length()';
35-
36-
static arguments() => 'Foo.arguments()';
37-
static caller() => 'Foo.caller()';
38-
static callee() => 'Foo.callee()';
39-
4032
nullary_method() {}
4133

4234
function_params(int f(x, [y]), g(x, {String y, z}), Callback cb) {
@@ -60,14 +52,8 @@ class Baz extends Foo<int> with Bar {
6052
Baz(int i) : super(i, 123);
6153
}
6254

63-
void main(args) {
64-
print(Foo.name());
65-
print(Foo.length());
66-
print(Foo.arguments());
67-
print(Foo.caller());
68-
print(Foo.callee());
69-
}
55+
void main(args) {}
7056

7157
const String some_top_level_constant = "abc";
7258
final String some_top_level_final = "abc";
73-
String some_top_level_var = "abc";
59+
String some_top_level_var = "abc";

pkg/dev_compiler/test/codegen/expect/closure.js

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,6 @@ dart_library.library('closure', null, /* Imports */[
6767
let b = opts && 'b' in opts ? opts.b : null;
6868
let c = opts && 'c' in opts ? opts.c : null;
6969
}
70-
static [dartx.name]() {
71-
return 'Foo.name()';
72-
}
73-
static [dartx.length]() {
74-
return 'Foo.length()';
75-
}
76-
static [dartx.arguments]() {
77-
return 'Foo.arguments()';
78-
}
79-
static [dartx.caller]() {
80-
return 'Foo.caller()';
81-
}
82-
static [dartx.callee]() {
83-
return 'Foo.callee()';
84-
}
8570
nullary_method() {}
8671
/**
8772
* @param {function(?, ?=):?number} f
@@ -119,15 +104,8 @@ dart_library.library('closure', null, /* Imports */[
119104
nullary_method: [dart.dynamic, []],
120105
function_params: [dart.dynamic, [dart.functionType(core.int, [dart.dynamic], [dart.dynamic]), dart.functionType(dart.dynamic, [dart.dynamic], {y: core.String, z: dart.dynamic}), Callback]]
121106
}),
122-
statics: () => ({
123-
named_params: [dart.dynamic, [dart.dynamic], {b: dart.dynamic, c: dart.dynamic}],
124-
[dartx.name]: [dart.dynamic, []],
125-
[dartx.length]: [dart.dynamic, []],
126-
[dartx.arguments]: [dart.dynamic, []],
127-
[dartx.caller]: [dart.dynamic, []],
128-
[dartx.callee]: [dart.dynamic, []]
129-
}),
130-
names: ['named_params', dartx.name, dartx.length, dartx.arguments, dartx.caller, dartx.callee]
107+
statics: () => ({named_params: [dart.dynamic, [dart.dynamic], {b: dart.dynamic, c: dart.dynamic}]}),
108+
names: ['named_params']
131109
});
132110
/** @final {string} */
133111
Foo.some_static_constant = "abc";
@@ -151,11 +129,6 @@ dart_library.library('closure', null, /* Imports */[
151129
});
152130
/** @param {?} args */
153131
function main(args) {
154-
core.print(Foo[dartx.name]());
155-
core.print(Foo[dartx.length]());
156-
core.print(Foo[dartx.arguments]());
157-
core.print(Foo[dartx.caller]());
158-
core.print(Foo[dartx.callee]());
159132
}
160133
dart.fn(main, dart.void, [dart.dynamic]);
161134
/** @final {string} */
@@ -164,13 +137,6 @@ dart_library.library('closure', null, /* Imports */[
164137
exports.some_top_level_final = "abc";
165138
/** @type {string} */
166139
exports.some_top_level_var = "abc";
167-
dart.defineExtensionNames([
168-
"name",
169-
"length",
170-
"arguments",
171-
"caller",
172-
"callee"
173-
]);
174140
// Exports:
175141
exports.Callback = Callback;
176142
exports.Foo$ = Foo$;

pkg/dev_compiler/test/codegen/expect/names.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,45 +20,40 @@ dart_library.library('names', null, /* Imports */[
2020
}
2121
dart.fn(_foo);
2222
class Frame extends core.Object {
23-
[dartx.caller](arguments$) {
23+
caller(arguments$) {
2424
this.arguments = arguments$;
2525
}
26-
static [dartx.callee]() {
26+
static callee() {
2727
return null;
2828
}
2929
}
30-
dart.defineNamedConstructor(Frame, dartx.caller);
30+
dart.defineNamedConstructor(Frame, 'caller');
3131
dart.setSignature(Frame, {
32-
constructors: () => ({[dartx.caller]: [Frame, [core.List]]}),
33-
statics: () => ({[dartx.callee]: [dart.dynamic, []]}),
34-
names: [dartx.callee]
32+
constructors: () => ({caller: [Frame, [core.List]]}),
33+
statics: () => ({callee: [dart.dynamic, []]}),
34+
names: ['callee']
3535
});
3636
class Frame2 extends core.Object {}
3737
dart.defineLazyProperties(Frame2, {
38-
get [dartx.caller]() {
38+
get caller() {
3939
return 100;
4040
},
41-
set [dartx.caller](_) {},
42-
get [dartx.arguments]() {
41+
set caller(_) {},
42+
get arguments() {
4343
return 200;
4444
},
45-
set [dartx.arguments](_) {}
45+
set arguments(_) {}
4646
});
4747
function main() {
4848
core.print(exports.exports);
4949
core.print(new Foo()[_foo$]());
5050
core.print(_foo());
51-
core.print(new Frame[dartx.caller]([1, 2, 3]));
52-
let eval$ = Frame[dartx.callee];
51+
core.print(new Frame.caller([1, 2, 3]));
52+
let eval$ = Frame.callee;
5353
core.print(eval$);
54-
core.print(dart.notNull(Frame2[dartx.caller]) + dart.notNull(Frame2[dartx.arguments]));
54+
core.print(dart.notNull(Frame2.caller) + dart.notNull(Frame2.arguments));
5555
}
5656
dart.fn(main);
57-
dart.defineExtensionNames([
58-
"caller",
59-
"callee",
60-
"arguments"
61-
]);
6257
// Exports:
6358
exports.Foo = Foo;
6459
exports.Frame = Frame;

0 commit comments

Comments
 (0)