Skip to content

Commit 7b39ded

Browse files
fishythefishcommit-bot@chromium.org
authored andcommitted
[dart2js] Define canonical recipes in terms of recipe_syntax.
Change-Id: I4e5e4fea81dbef9bf0d2994d8dcbec23a4f1fb01 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/107309 Commit-Queue: Mayank Patke <[email protected]> Reviewed-by: Stephen Adams <[email protected]>
1 parent 069d76a commit 7b39ded

4 files changed

Lines changed: 185 additions & 10 deletions

File tree

sdk/lib/_internal/js_runtime/lib/rti.dart

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -463,13 +463,15 @@ class _Universe {
463463
// for the proposed type.
464464
// * `createXXX` to create the type if it does not exist.
465465

466-
static String _canonicalRecipeOfDynamic() => '@';
467-
static String _canonicalRecipeOfVoid() => '~';
468-
static String _canonicalRecipeOfNever() => '0&';
469-
static String _canonicalRecipeOfAny() => '1&';
466+
static String _canonicalRecipeOfDynamic() => Recipe.pushDynamicString;
467+
static String _canonicalRecipeOfVoid() => Recipe.pushVoidString;
468+
static String _canonicalRecipeOfNever() =>
469+
Recipe.pushNeverExtensionString + Recipe.extensionOpString;
470+
static String _canonicalRecipeOfAny() =>
471+
Recipe.pushAnyExtensionString + Recipe.extensionOpString;
470472

471473
static String _canonicalRecipeOfFutureOr(Rti baseType) =>
472-
'${Rti._getCanonicalRecipe(baseType)}/';
474+
Rti._getCanonicalRecipe(baseType) + Recipe.wrapFutureOrString;
473475

474476
static Rti _lookupDynamicRti(universe) {
475477
return _lookupTerminalRti(
@@ -527,7 +529,7 @@ class _Universe {
527529
Rti argument = _castToRti(_Utils.arrayAt(arguments, i));
528530
String subrecipe = Rti._getCanonicalRecipe(argument);
529531
s += sep + subrecipe;
530-
sep = ',';
532+
sep = Recipe.separatorString;
531533
}
532534
return s;
533535
}
@@ -537,7 +539,9 @@ class _Universe {
537539
String s = _Utils.asString(name);
538540
int length = _Utils.arrayLength(arguments);
539541
if (length != 0) {
540-
s += '<' + _canonicalRecipeJoin(arguments) + '>';
542+
s += Recipe.startTypeArgumentsString +
543+
_canonicalRecipeJoin(arguments) +
544+
Recipe.endTypeArgumentsString;
541545
}
542546
return s;
543547
}
@@ -563,8 +567,11 @@ class _Universe {
563567

564568
static String _canonicalRecipeOfBinding(Rti base, Object arguments) {
565569
String s = Rti._getCanonicalRecipe(base);
566-
s += ';'; // TODO(sra): Omit when base encoding is Rti without ToType.
567-
s += '<' + _canonicalRecipeJoin(arguments) + '>';
570+
s += Recipe
571+
.toTypeString; // TODO(sra): Omit when base encoding is Rti without ToType.
572+
s += Recipe.startTypeArgumentsString +
573+
_canonicalRecipeJoin(arguments) +
574+
Recipe.endTypeArgumentsString;
568575
return s;
569576
}
570577

@@ -1117,6 +1124,10 @@ class _Utils {
11171124
}
11181125
// -------- Entry points for testing -------------------------------------------
11191126

1127+
String testingCanonicalRecipe(rti) {
1128+
return Rti._getCanonicalRecipe(rti);
1129+
}
1130+
11201131
String testingRtiToString(rti) {
11211132
return _rtiToString(_castToRti(rti), null);
11221133
}

sdk/lib/_internal/js_runtime/lib/shared/recipe_syntax.dart

Lines changed: 128 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,53 @@ abstract class Recipe {
1313
// Operators.
1414

1515
static const int separator = _comma;
16+
static const String separatorString = _commaString;
1617

1718
static const int toType = _semicolon;
19+
static const String toTypeString = _semicolonString;
1820

1921
static const int pushDynamic = _at;
22+
static const String pushDynamicString = _atString;
2023
static const int pushVoid = _tilde;
24+
static const String pushVoidString = _tildeString;
2125

2226
static const int wrapStar = _asterisk;
27+
static const String wrapStarString = _asteriskString;
2328
static const int wrapQuestion = _question;
29+
static const String wrapQuestionString = _questionString;
2430
static const int wrapFutureOr = _slash;
31+
static const String wrapFutureOrString = _slashString;
2532

2633
static const int startTypeArguments = _lessThan;
34+
static const String startTypeArgumentsString = _lessThanString;
2735
static const int endTypeArguments = _greaterThan;
36+
static const String endTypeArgumentsString = _greaterThanString;
2837

2938
static const int startFunctionArguments = _leftParen;
39+
static const String startFunctionArgumentsString = _leftParenString;
3040
static const int endFunctionArguments = _rightParen;
41+
static const String endFunctionArgumentsString = _rightParenString;
3142
static const int startOptionalGroup = _leftBracket;
43+
static const String startOptionalGroupString = _leftBracketString;
3244
static const int endOptionalGroup = _rightBracket;
45+
static const String endOptionalGroupString = _rightBracketString;
3346
static const int startNamedGroup = _leftBrace;
47+
static const String startNamedGroupString = _leftBraceString;
3448
static const int endNamedGroup = _rightBrace;
49+
static const String endNamedGroupString = _rightBraceString;
3550
static const int nameSeparator = _colon;
51+
static const String nameSeparatorString = _colonString;
3652

3753
static const int genericFunctionTypeParameterIndex = _circumflex;
54+
static const String genericFunctionTypeParameterIndexString =
55+
_circumflexString;
3856

3957
static const int extensionOp = _ampersand;
58+
static const String extensionOpString = _ampersandString;
4059
static const int pushNeverExtension = 0;
60+
static const String pushNeverExtensionString = '$pushNeverExtension';
4161
static const int pushAnyExtension = 1;
62+
static const String pushAnyExtensionString = '$pushAnyExtension';
4263

4364
// Number and name components.
4465

@@ -54,50 +75,156 @@ abstract class Recipe {
5475

5576
// Private names.
5677

57-
static const int _formfeed = 0x0C; // '\f' in string literal.
78+
static const int _formfeed = 0x0C;
79+
static const String _formfeedString = '\f';
5880

5981
static const int _space = 0x20;
82+
static const String _spaceString = ' ';
6083
static const int _exclamation = 0x21;
84+
static const String _exclamationString = '!';
6185
static const int _hash = 0x23;
86+
static const String _hashString = '#';
6287
static const int _dollar = 0x24;
88+
static const String _dollarString = r'$';
6389
static const int _percent = 0x25;
90+
static const String _percentString = '%';
6491
static const int _ampersand = 0x26;
92+
static const String _ampersandString = '&';
6593
static const int _apostrophe = 0x27;
94+
static const String _apostropheString = "'";
6695
static const int _leftParen = 0x28;
96+
static const String _leftParenString = '(';
6797
static const int _rightParen = 0x29;
98+
static const String _rightParenString = ')';
6899
static const int _asterisk = 0x2A;
100+
static const String _asteriskString = '*';
69101
static const int _plus = 0x2B;
102+
static const String _plusString = '+';
70103
static const int _comma = 0x2C;
104+
static const String _commaString = ',';
71105
static const int _minus = 0x2D;
106+
static const String _minusString = '-';
72107
static const int _period = 0x2E;
108+
static const String _periodString = '.';
73109
static const int _slash = 0x2F;
110+
static const String _slashString = '/';
74111

75112
static const int _digit0 = 0x30;
76113
static const int _digit9 = 0x39;
77114

78115
static const int _colon = 0x3A;
116+
static const String _colonString = ':';
79117
static const int _semicolon = 0x3B;
118+
static const String _semicolonString = ';';
80119
static const int _lessThan = 0x3C;
120+
static const String _lessThanString = '<';
81121
static const int _equals = 0x3D;
122+
static const String _equalsString = '=';
82123
static const int _greaterThan = 0x3E;
124+
static const String _greaterThanString = '>';
83125
static const int _question = 0x3F;
126+
static const String _questionString = '?';
84127
static const int _at = 0x40;
128+
static const String _atString = '@';
85129

86130
static const int _uppercaseA = 0x41;
87131
static const int _uppercaseZ = 0x5A;
88132

89133
static const int _leftBracket = 0x5B;
134+
static const String _leftBracketString = '[';
90135
static const int _backslash = 0x5C;
136+
static const String _backslashString = r'\';
91137
static const int _rightBracket = 0x5D;
138+
static const String _rightBracketString = ']';
92139
static const int _circumflex = 0x5E;
140+
static const String _circumflexString = '^';
93141
static const int _underscore = 0x5F;
142+
static const String _underscoreString = '_';
94143
static const int _backtick = 0x60;
144+
static const String _backtickString = '`';
95145

96146
static const int _lowercaseA = 0x61;
97147
static const int _lowercaseZ = 0x7A;
98148

99149
static const int _leftBrace = 0x7B;
150+
static const String _leftBraceString = '{';
100151
static const int _vertical = 0x7C;
152+
static const String _verticalString = '|';
101153
static const int _rightBrace = 0x7D;
154+
static const String _rightBraceString = '}';
102155
static const int _tilde = 0x7E;
156+
static const String _tildeString = '~';
157+
158+
static void testEquivalence() {
159+
void test(String label, int charCode, String str) {
160+
if (String.fromCharCode(charCode) != str) {
161+
throw StateError("$label: String.fromCharCode($charCode) != $str");
162+
}
163+
}
164+
165+
void testExtension(String label, int op, String str) {
166+
if ('$op' != str) {
167+
throw StateError("$label: $op.toString() != $str");
168+
}
169+
}
170+
171+
test("separator", separator, separatorString);
172+
test("toType", toType, toTypeString);
173+
test("pushDynamic", pushDynamic, pushDynamicString);
174+
test("pushVoid", pushVoid, pushVoidString);
175+
test("wrapStar", wrapStar, wrapStarString);
176+
test("wrapQuestion", wrapQuestion, wrapQuestionString);
177+
test("wrapFutureOr", wrapFutureOr, wrapFutureOrString);
178+
test("startTypeArguments", startTypeArguments, startTypeArgumentsString);
179+
test("endTypeArguments", endTypeArguments, endTypeArgumentsString);
180+
test("startFunctionArguments", startFunctionArguments,
181+
startFunctionArgumentsString);
182+
test("endFunctionArguments", endFunctionArguments,
183+
endFunctionArgumentsString);
184+
test("startOptionalGroup", startOptionalGroup, startOptionalGroupString);
185+
test("endOptionalGroup", endOptionalGroup, endOptionalGroupString);
186+
test("startNamedGroup", startNamedGroup, startNamedGroupString);
187+
test("endNamedGroup", endNamedGroup, endNamedGroupString);
188+
test("nameSeparator", nameSeparator, nameSeparatorString);
189+
test("genericFunctionTypeParameterIndex", genericFunctionTypeParameterIndex,
190+
genericFunctionTypeParameterIndexString);
191+
test("extensionOp", extensionOp, extensionOpString);
192+
testExtension(
193+
"pushNeverExtension", pushNeverExtension, pushNeverExtensionString);
194+
testExtension("pushAnyExtension", pushAnyExtension, pushAnyExtensionString);
195+
196+
test("_formfeed", _formfeed, _formfeedString);
197+
test("_space", _space, _spaceString);
198+
test("_exclamation", _exclamation, _exclamationString);
199+
test("_hash", _hash, _hashString);
200+
test("_dollar", _dollar, _dollarString);
201+
test("_percent", _percent, _percentString);
202+
test("_ampersand", _ampersand, _ampersandString);
203+
test("_apostrophe", _apostrophe, _apostropheString);
204+
test("_leftParen", _leftParen, _leftParenString);
205+
test("_rightParen", _rightParen, _rightParenString);
206+
test("_asterisk", _asterisk, _asteriskString);
207+
test("_plus", _plus, _plusString);
208+
test("_comma", _comma, _commaString);
209+
test("_minus", _minus, _minusString);
210+
test("_period", _period, _periodString);
211+
test("_slash", _slash, _slashString);
212+
test("_colon", _colon, _colonString);
213+
test("_semicolon", _semicolon, _semicolonString);
214+
test("_lessThan", _lessThan, _lessThanString);
215+
test("_equals", _equals, _equalsString);
216+
test("_greaterThan", _greaterThan, _greaterThanString);
217+
test("_question", _question, _questionString);
218+
test("_at", _at, _atString);
219+
test("_leftBracket", _leftBracket, _leftBracketString);
220+
test("_backslash", _backslash, _backslashString);
221+
test("_rightBracket", _rightBracket, _rightBracketString);
222+
test("_circumflex", _circumflex, _circumflexString);
223+
test("_underscore", _underscore, _underscoreString);
224+
test("_backtick", _backtick, _backtickString);
225+
test("_leftBrace", _leftBrace, _leftBraceString);
226+
test("_vertical", _vertical, _verticalString);
227+
test("_rightBrace", _rightBrace, _rightBraceString);
228+
test("_tilde", _tilde, _tildeString);
229+
}
103230
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:_rti' as rti;
6+
import "package:expect/expect.dart";
7+
8+
final universe = rti.testingCreateUniverse();
9+
10+
main() {
11+
test('@', '@');
12+
test('~', '~');
13+
test('0&', '0&');
14+
test('1&', '1&');
15+
test('int', 'int');
16+
test('int/', 'int/');
17+
test('List<int>', 'List<int>');
18+
test('Foo<bool,Bar<int,double>>', 'Foo<bool,Bar<int,double>>');
19+
test('@;<int,bool>', '@<int><bool>');
20+
}
21+
22+
String canonicalize(String recipe) {
23+
var t = rti.testingUniverseEval(universe, recipe);
24+
return rti.testingCanonicalRecipe(t);
25+
}
26+
27+
void test(String expected, String recipe) =>
28+
Expect.equals(expected, canonicalize(recipe));
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:_recipe_syntax';
6+
7+
main() {
8+
Recipe.testEquivalence();
9+
}

0 commit comments

Comments
 (0)