Skip to content

Commit 1e43b3a

Browse files
author
John Messerly
committed
[refactor] js_codegen: remove currentClass, simplify visitSimpleIdentifier
We were tracking currentClass for no real reason. Also attempted to discern what visitSimpleIdentifier was actually doing. It now prefers the original element (the "field" not the synthetic getter/setter) whenever possible. Previously was a mismash. This little change tripped on the (larger, not fixed yet) issue #138 ... but I think I found a way around the issue for now, by tweaking the private TypeDataArray class. [email protected] Review URL: https://codereview.chromium.org/1095563003
1 parent 9efc804 commit 1e43b3a

File tree

4 files changed

+47
-36
lines changed

4 files changed

+47
-36
lines changed

pkg/dev_compiler/lib/runtime/dart/_native_typed_data.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -582,9 +582,6 @@ var _native_typed_data;
582582
dart.defineNamedConstructor(NativeByteData, 'view');
583583
let _setRangeFast = Symbol('_setRangeFast');
584584
class NativeTypedArray extends NativeTypedData {
585-
get length() {
586-
return dart.as(this.length, core.int);
587-
}
588585
[_setRangeFast](start, end, source, skipCount) {
589586
let targetLength = this.length;
590587
this[_checkIndex](start, dart.notNull(targetLength) + 1);
@@ -606,6 +603,9 @@ var _native_typed_data;
606603
}
607604
NativeTypedArray[dart.implements] = () => [_js_helper.JavaScriptIndexingBehavior];
608605
class NativeTypedArrayOfDouble extends dart.mixin(NativeTypedArray, collection.ListMixin$(core.double), _internal.FixedLengthListMixin$(core.double)) {
606+
get [core.$length]() {
607+
return dart.as(this.length, core.int);
608+
}
609609
[core.$get](index) {
610610
this[_checkIndex](index, this[core.$length]);
611611
return this[index];
@@ -625,6 +625,9 @@ var _native_typed_data;
625625
}
626626
}
627627
class NativeTypedArrayOfInt extends dart.mixin(NativeTypedArray, collection.ListMixin$(core.int), _internal.FixedLengthListMixin$(core.int)) {
628+
get [core.$length]() {
629+
return dart.as(this.length, core.int);
630+
}
628631
[core.$set](index, value) {
629632
this[_checkIndex](index, this[core.$length]);
630633
this[index] = value;

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

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
6565
/// The variable for the current catch clause
6666
SimpleIdentifier _catchParameter;
6767

68-
ClassDeclaration currentClass;
6968
ConstantEvaluator _constEvaluator;
7069

7170
final _exports = new Set<String>();
@@ -319,8 +318,6 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
319318
var jsName = getAnnotationValue(node, _isJsNameAnnotation);
320319
if (jsName != null) return _emitJsType(node.name.name, jsName);
321320

322-
currentClass = node;
323-
324321
var ctors = <ConstructorDeclaration>[];
325322
var fields = <FieldDeclaration>[];
326323
var staticFields = <FieldDeclaration>[];
@@ -337,7 +334,6 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
337334

338335
var body =
339336
_finishClassMembers(classElem, classExpr, ctors, fields, staticFields);
340-
currentClass = null;
341337

342338
return _finishClassDef(type, body);
343339
}
@@ -1016,49 +1012,55 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
10161012
/// going through the qualified library name if necessary.
10171013
@override
10181014
JS.Expression visitSimpleIdentifier(SimpleIdentifier node) {
1019-
var e = node.staticElement;
1020-
if (e == null) {
1015+
var accessor = node.staticElement;
1016+
if (accessor == null) {
10211017
return js.commentExpression(
10221018
'Unimplemented unknown name', new JS.Identifier(node.name));
10231019
}
10241020

1025-
var variable = e is PropertyAccessorElement ? e.variable : e;
1026-
var name = variable.name;
1021+
// Get the original declaring element. If we had a property accessor, this
1022+
// indirects back to a (possibly synthetic) field.
1023+
var element = accessor;
1024+
if (element is PropertyAccessorElement) element = accessor.variable;
1025+
var name = element.name;
10271026

10281027
// library member
1029-
if (e.enclosingElement is CompilationUnitElement &&
1030-
(e.library != libraryInfo.library ||
1031-
variable is TopLevelVariableElement && !variable.isConst)) {
1032-
return js.call('#.#', [_libraryName(e.library), name]);
1028+
if (element.enclosingElement is CompilationUnitElement &&
1029+
(element.library != libraryInfo.library ||
1030+
element is TopLevelVariableElement && !element.isConst)) {
1031+
return js.call('#.#', [_libraryName(element.library), name]);
10331032
}
10341033

1035-
// instance member
1036-
if (currentClass != null && _needsImplicitThis(e)) {
1037-
return js.call(
1038-
'this.#', _emitMemberName(name, type: currentClass.element.type));
1039-
}
1034+
// Unqualified class member. This could mean implicit-this, or implicit
1035+
// call to a static from the same class.
1036+
if (element is ClassMemberElement && element is! ConstructorElement) {
1037+
bool isStatic = element.isStatic;
1038+
var type = element.enclosingElement.type;
10401039

1041-
// static member
1042-
if (e is ExecutableElement &&
1043-
e.isStatic &&
1044-
variable.enclosingElement is ClassElement) {
1045-
var className = (variable.enclosingElement as ClassElement).name;
1046-
return js.call('#.#', [className, _emitMemberName(name, isStatic: true)]);
1040+
// For instance methods, we add implicit-this.
1041+
// For static methods, we add the raw type name, without generics or
1042+
// library prefix. We don't need those because static calls can't use
1043+
// the generic type.
1044+
var target = isStatic ? new JS.Identifier(type.name) : new JS.This();
1045+
var member = _emitMemberName(name, isStatic: isStatic, type: type);
1046+
return new JS.PropertyAccess(target, member);
10471047
}
10481048

10491049
// initializing formal parameter, e.g. `Point(this.x)`
1050-
if (e is ParameterElement && e.isInitializingFormal && e.isPrivate) {
1050+
if (element is ParameterElement &&
1051+
element.isInitializingFormal &&
1052+
element.isPrivate) {
10511053
/// Rename private names so they don't shadow the private field symbol.
10521054
/// The renamer would handle this, but it would prefer to rename the
10531055
/// temporary used for the private symbol. Instead rename the parameter.
1054-
return _getTemp(e, '${name.substring(1)}');
1056+
return _getTemp(element, '${name.substring(1)}');
10551057
}
10561058

1057-
if (_isTemporary(e)) {
1059+
if (_isTemporary(element)) {
10581060
if (name[0] == '#') {
10591061
return new JS.InterpolatedExpression(name.substring(1));
10601062
} else {
1061-
return _getTemp(e, name);
1063+
return _getTemp(element, name);
10621064
}
10631065
}
10641066

@@ -2312,10 +2314,6 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ConversionVisitor {
23122314
}
23132315

23142316
DartType getStaticType(Expression e) => rules.getStaticType(e);
2315-
2316-
static bool _needsImplicitThis(Element e) =>
2317-
e is PropertyAccessorElement && !e.variable.isStatic ||
2318-
e is ClassMemberElement && !e.isStatic && e is! ConstructorElement;
23192317
}
23202318

23212319
class JSGenerator extends CodeGenerator {

pkg/dev_compiler/test/generated_sdk/lib/_internal/compiler/js_lib/native_typed_data.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,8 @@ class NativeByteData extends NativeTypedData implements ByteData {
857857

858858
abstract class NativeTypedArray extends NativeTypedData
859859
implements JavaScriptIndexingBehavior {
860-
int get length => JS('JSUInt32', '#.length', this);
860+
// TODO(jmesserly): moved `length` to subclass to (somewhat) mitigate
861+
// <https://github.com/dart-lang/dev_compiler/issues/138>
861862

862863
void _setRangeFast(int start, int end,
863864
NativeTypedArray source, int skipCount) {
@@ -887,6 +888,8 @@ abstract class NativeTypedArrayOfDouble
887888
extends NativeTypedArray
888889
with ListMixin<double>, FixedLengthListMixin<double> {
889890

891+
int get length => JS('JSUInt32', '#.length', this);
892+
890893
num operator[](int index) {
891894
_checkIndex(index, length);
892895
return JS('num', '#[#]', this, index);
@@ -912,6 +915,8 @@ abstract class NativeTypedArrayOfInt
912915
with ListMixin<int>, FixedLengthListMixin<int>
913916
implements List<int> {
914917

918+
int get length => JS('JSUInt32', '#.length', this);
919+
915920
// operator[]() is not here since different versions have different return
916921
// types
917922

pkg/dev_compiler/tool/input_sdk/private/native_typed_data.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,8 @@ class NativeByteData extends NativeTypedData implements ByteData {
857857

858858
abstract class NativeTypedArray extends NativeTypedData
859859
implements JavaScriptIndexingBehavior {
860-
int get length => JS('JSUInt32', '#.length', this);
860+
// TODO(jmesserly): moved `length` to subclass to (somewhat) mitigate
861+
// <https://github.com/dart-lang/dev_compiler/issues/138>
861862

862863
void _setRangeFast(int start, int end,
863864
NativeTypedArray source, int skipCount) {
@@ -887,6 +888,8 @@ abstract class NativeTypedArrayOfDouble
887888
extends NativeTypedArray
888889
with ListMixin<double>, FixedLengthListMixin<double> {
889890

891+
int get length => JS('JSUInt32', '#.length', this);
892+
890893
num operator[](int index) {
891894
_checkIndex(index, length);
892895
return JS('num', '#[#]', this, index);
@@ -912,6 +915,8 @@ abstract class NativeTypedArrayOfInt
912915
with ListMixin<int>, FixedLengthListMixin<int>
913916
implements List<int> {
914917

918+
int get length => JS('JSUInt32', '#.length', this);
919+
915920
// operator[]() is not here since different versions have different return
916921
// types
917922

0 commit comments

Comments
 (0)