Skip to content

Commit de6df99

Browse files
author
John Messerly
committed
fixes #226 super == and fixes #227 escape \r \f
[email protected] Review URL: https://codereview.chromium.org/1183453005.
1 parent 7d8b38a commit de6df99

File tree

5 files changed

+72
-1
lines changed

5 files changed

+72
-1
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,6 +1938,8 @@ class JSCodegenVisitor extends GeneralizingAstVisitor {
19381938
// We can also do this if both sides are the same primitive type.
19391939
if (_canUsePrimitiveEquality(left, right)) {
19401940
code = op.type == TokenType.EQ_EQ ? '# == #' : '# != #';
1941+
} else if (left is SuperExpression) {
1942+
return _emitSend(left, op.lexeme, [right]);
19411943
} else {
19421944
var bang = op.type == TokenType.BANG_EQ ? '!' : '';
19431945
code = '${bang}dart.equals(#, #)';

pkg/dev_compiler/lib/src/js/builder.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,11 @@ class JsBuilder {
300300
String escaped = value.replaceAll('\\', '\\\\');
301301
// Do not escape unicode characters and ' because they are allowed in the
302302
// string literal anyway.
303-
escaped = escaped.replaceAllMapped(new RegExp('\n|$quote|\b|\t|\v'), (m) {
303+
var re = new RegExp('\n|\r|$quote|\b|\f|\t|\v');
304+
escaped = escaped.replaceAllMapped(re, (m) {
304305
switch (m.group(0)) {
305306
case "\n" : return r"\n";
307+
case "\r" : return r"\r";
306308
// Quotes are only replaced if they conflict with the containing quote
307309
case '"': return r'\"';
308310
case "'": return r"\'";

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,28 @@ dart_library.library('misc', null, /* Imports */[
2222
return Generic;
2323
});
2424
let Generic = Generic$();
25+
class Base extends core.Object {
26+
Base() {
27+
this.x = 1;
28+
this.y = 2;
29+
}
30+
['=='](obj) {
31+
return dart.is(obj, Base) && dart.equals(dart.dload(obj, 'x'), this.x) && dart.equals(dart.dload(obj, 'y'), this.y);
32+
}
33+
}
34+
class Derived extends core.Object {
35+
Derived() {
36+
this.z = 3;
37+
}
38+
['=='](obj) {
39+
return dart.is(obj, Derived) && dart.equals(dart.dload(obj, 'z'), this.z) && super['=='](obj);
40+
}
41+
}
42+
function _isWhitespace(ch) {
43+
return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
44+
}
45+
dart.fn(_isWhitespace, core.bool, [core.String]);
46+
let _escapeMap = dart.const(dart.map({'\n': '\\n', '\r': '\\r', '\f': '\\f', '\b': '\\b', '\t': '\\t', '\v': '\\v', '': '\\x7F'}));
2547
function main() {
2648
core.print(dart.toString(1));
2749
core.print(dart.toString(1.0));
@@ -30,11 +52,14 @@ dart_library.library('misc', null, /* Imports */[
3052
core.print(dart.equals(x, core.Object));
3153
core.print(dart.equals(x, Generic));
3254
core.print(new (Generic$(core.int))().type);
55+
core.print(dart.equals(new Derived(), new Derived()));
3356
}
3457
dart.fn(main);
3558
// Exports:
3659
exports.UNINITIALIZED = UNINITIALIZED;
3760
exports.Generic$ = Generic$;
3861
exports.Generic = Generic;
62+
exports.Base = Base;
63+
exports.Derived = Derived;
3964
exports.main = main;
4065
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
11
// Messages from compiling misc.dart
2+
info: line 19, column 27 of test/codegen/misc.dart: [DynamicInvoke] obj.x requires dynamic invoke
3+
return obj is Base && obj.x == x && obj.y == y;
4+
^^^^^
5+
info: line 19, column 41 of test/codegen/misc.dart: [DynamicInvoke] obj.y requires dynamic invoke
6+
return obj is Base && obj.x == x && obj.y == y;
7+
^^^^^
8+
info: line 25, column 30 of test/codegen/misc.dart: [DynamicInvoke] obj.z requires dynamic invoke
9+
return obj is Derived && obj.z == z && super == obj;
10+
^^^^^

pkg/dev_compiler/test/codegen/misc.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,37 @@ class Generic<T> {
1111
Type get type => Generic;
1212
}
1313

14+
// super ==
15+
// https://github.com/dart-lang/dev_compiler/issues/226
16+
class Base {
17+
int x = 1, y = 2;
18+
operator==(obj) {
19+
return obj is Base && obj.x == x && obj.y == y;
20+
}
21+
}
22+
class Derived {
23+
int z = 3;
24+
operator==(obj) {
25+
return obj is Derived && obj.z == z && super == obj;
26+
}
27+
}
28+
29+
// string escape tests
30+
// https://github.com/dart-lang/dev_compiler/issues/227
31+
bool _isWhitespace(String ch) =>
32+
ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
33+
34+
const _escapeMap = const {
35+
'\n': r'\n',
36+
'\r': r'\r',
37+
'\f': r'\f',
38+
'\b': r'\b',
39+
'\t': r'\t',
40+
'\v': r'\v',
41+
'\x7F': r'\x7F', // delete
42+
};
43+
44+
1445
main() {
1546
// Number literals in call expressions.
1647
print(1.toString());
@@ -24,4 +55,6 @@ main() {
2455

2556
// Should be Generic<dynamic>
2657
print(new Generic<int>().type);
58+
59+
print(new Derived() == new Derived()); // true
2760
}

0 commit comments

Comments
 (0)