Skip to content

Commit 56c75ff

Browse files
author
John Messerly
committed
fix #159, static renames for caller/arguments
[email protected] Review URL: https://codereview.chromium.org/1111803005
1 parent dc4b3d5 commit 56c75ff

File tree

6 files changed

+47
-21
lines changed

6 files changed

+47
-21
lines changed

pkg/dev_compiler/lib/runtime/dart_runtime.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
var dart, _js_helper;
5+
var dart, _js_helper, _js_primitives;
66
(function (dart) {
77
'use strict';
88

@@ -760,7 +760,9 @@ var dart, _js_helper;
760760
let initMethod = proto[name];
761761
let ctor = function() { return initMethod.apply(this, arguments); }
762762
ctor.prototype = proto;
763-
clazz[name] = ctor;
763+
// Use defineProperty so we don't hit a property defined on Function,
764+
// like `caller` and `arguments`.
765+
defineProperty(clazz, name, { value: ctor, configurable: true });
764766
}
765767
dart.defineNamedConstructor = defineNamedConstructor;
766768

@@ -937,4 +939,7 @@ var dart, _js_helper;
937939
_js_helper = _js_helper || {};
938940
_js_helper.checkNum = notNull;
939941

942+
_js_primitives = _js_primitives || {};
943+
_js_primitives.printString = (s) => console.log(s);
944+
940945
})(dart || (dart = {}));

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,8 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
641641
for (FieldDeclaration member in staticFields) {
642642
for (VariableDeclaration field in member.fields.variables) {
643643
var fieldName = field.name.name;
644-
if (field.isConst || _isFieldInitConstant(field)) {
644+
if ((field.isConst || _isFieldInitConstant(field)) &&
645+
!JS.invalidStaticFieldName(fieldName)) {
645646
var init = _visit(field.initializer);
646647
if (init == null) init = new JS.LiteralNull();
647648
body.add(js.statement('#.# = #;', [name, fieldName, init]));
@@ -2363,18 +2364,8 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
23632364
JS.Expression _emitMemberName(String name,
23642365
{DartType type, bool unary: false, bool isStatic: false}) {
23652366

2366-
// Static methods skip most of the rename steps.
2367-
if (isStatic) {
2368-
if (JS.invalidStaticMethodName(name)) {
2369-
// Choose an string name. Use an invalid identifier so it won't conflict
2370-
// with any valid member names.
2371-
// TODO(jmesserly): this works around the problem, but I'm pretty sure we
2372-
// don't need it, as static methods seemed to work. The only concrete
2373-
// issue we saw was in the defineNamedConstructor helper function.
2374-
name = '$name*';
2375-
}
2376-
return _propertyName(name);
2377-
}
2367+
// Static members skip the rename steps.
2368+
if (isStatic) return _propertyName(name);
23782369

23792370
if (name.startsWith('_')) {
23802371
return _privateNames.putIfAbsent(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ bool invalidVariableName(String keyword, {bool strictMode: true}) {
231231
return false;
232232
}
233233

234-
/// Returns true for invalid static method names in strict mode.
234+
/// Returns true for invalid static field names in strict mode.
235235
/// In particular, "caller" "callee" and "arguments" cannot be used.
236-
bool invalidStaticMethodName(String name) {
236+
bool invalidStaticFieldName(String name) {
237237
switch (name) {
238238
case "arguments":
239239
case "caller":

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,38 @@ var names;
1313
return 456;
1414
}
1515
class Frame extends core.Object {
16-
['caller*'](arguments$) {
16+
caller(arguments$) {
1717
this.arguments = arguments$;
1818
}
19-
static ['callee*']() {
19+
static callee() {
2020
return null;
2121
}
2222
}
23-
dart.defineNamedConstructor(Frame, 'caller*');
23+
dart.defineNamedConstructor(Frame, 'caller');
24+
class Frame2 extends core.Object {}
25+
dart.defineLazyProperties(Frame2, {
26+
get caller() {
27+
return 100;
28+
},
29+
set caller(_) {},
30+
get arguments() {
31+
return 200;
32+
},
33+
set arguments(_) {}
34+
});
2435
// Function main: () → dynamic
2536
function main() {
2637
core.print(exports.exports);
2738
core.print(new Foo()[_foo$]());
2839
core.print(_foo());
2940
core.print(new Frame.caller([1, 2, 3]));
30-
let eval$ = dart.bind(Frame, 'callee*');
41+
let eval$ = dart.bind(Frame, 'callee');
3142
core.print(eval$);
43+
core.print(dart.notNull(Frame2.caller) + dart.notNull(Frame2.arguments));
3244
}
3345
// Exports:
3446
exports.Foo = Foo;
3547
exports.Frame = Frame;
48+
exports.Frame2 = Frame2;
3649
exports.main = main;
3750
})(names || (names = {}));

pkg/dev_compiler/test/codegen/names.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@ class Frame {
1616
static callee() => null;
1717
}
1818

19+
20+
class Frame2 {
21+
static int caller = 100;
22+
static int arguments = 200;
23+
}
24+
1925
main() {
2026
print(exports);
2127
print(new Foo()._foo());
2228
print(_foo());
2329
print(new Frame.caller([1,2,3]));
2430
var eval = Frame.callee;
2531
print(eval);
32+
print(Frame2.caller + Frame2.arguments);
2633
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Names test</title>
6+
</head>
7+
<body>
8+
<script type="application/dart" src="names.dart"></script>
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)