Skip to content

Commit dee054e

Browse files
author
John Messerly
committed
fixes #415, correct type for map literals
Not sure why this wasn't implemented. Lists seem to be okay. [email protected] Review URL: https://codereview.chromium.org/1611753002 .
1 parent f43b756 commit dee054e

File tree

7 files changed

+54
-39
lines changed

7 files changed

+54
-39
lines changed

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -520,20 +520,26 @@ dart_library.library('dart/_runtime', null, /* Imports */[
520520
if (x == null) throwNullValueError();
521521
return x;
522522
}
523-
function map(values) {
524-
let map = collection.LinkedHashMap.new();
525-
if (Array.isArray(values)) {
526-
for (let i = 0, end = values.length - 1; i < end; i += 2) {
527-
let key = values[i];
528-
let value = values[i + 1];
529-
map.set(key, value);
530-
}
531-
} else if (typeof values === 'object') {
532-
for (let key of getOwnPropertyNames(values)) {
533-
map.set(key, values[key]);
523+
function map(values, K, V) {
524+
if (K === void 0) K = null;
525+
if (V === void 0) V = null;
526+
return (() => {
527+
if (K == null) K = dynamicR;
528+
if (V == null) V = dynamicR;
529+
let map = getGenericClass(collection.LinkedHashMap)(K, V).new();
530+
if (Array.isArray(values)) {
531+
for (let i = 0, end = values.length - 1; i < end; i += 2) {
532+
let key = values[i];
533+
let value = values[i + 1];
534+
map.set(key, value);
535+
}
536+
} else if (typeof values === 'object') {
537+
for (let key of getOwnPropertyNames(values)) {
538+
map.set(key, values[key]);
539+
}
534540
}
535-
}
536-
return map;
541+
return map;
542+
})();
537543
}
538544
function assert_(condition) {
539545
if (!condition) throwAssertionError();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ dart_library.library('dart/convert', null, /* Imports */[
6464
});
6565
dart.defineLazyProperties(Encoding, {
6666
get _nameToEncoding() {
67-
return dart.map({"iso_8859-1:1987": LATIN1, "iso-ir-100": LATIN1, "iso_8859-1": LATIN1, "iso-8859-1": LATIN1, latin1: LATIN1, l1: LATIN1, ibm819: LATIN1, cp819: LATIN1, csisolatin1: LATIN1, "iso-ir-6": ASCII, "ansi_x3.4-1968": ASCII, "ansi_x3.4-1986": ASCII, "iso_646.irv:1991": ASCII, "iso646-us": ASCII, "us-ascii": ASCII, us: ASCII, ibm367: ASCII, cp367: ASCII, csascii: ASCII, ascii: ASCII, csutf8: UTF8, "utf-8": UTF8});
67+
return dart.map({"iso_8859-1:1987": LATIN1, "iso-ir-100": LATIN1, "iso_8859-1": LATIN1, "iso-8859-1": LATIN1, latin1: LATIN1, l1: LATIN1, ibm819: LATIN1, cp819: LATIN1, csisolatin1: LATIN1, "iso-ir-6": ASCII, "ansi_x3.4-1968": ASCII, "ansi_x3.4-1986": ASCII, "iso_646.irv:1991": ASCII, "iso646-us": ASCII, "us-ascii": ASCII, us: ASCII, ibm367: ASCII, cp367: ASCII, csascii: ASCII, ascii: ASCII, csutf8: UTF8, "utf-8": UTF8}, core.String, Encoding);
6868
},
6969
set _nameToEncoding(_) {}
7070
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6190,7 +6190,7 @@ dart_library.library('dart/html', null, /* Imports */[
61906190
}, dart.void, [core.String, core.String]));
61916191
let formData = parts[dartx.join]('&');
61926192
if (requestHeaders == null) {
6193-
requestHeaders = dart.map();
6193+
requestHeaders = dart.map({}, core.String, core.String);
61946194
}
61956195
requestHeaders.putIfAbsent('Content-Type', dart.fn(() => 'application/x-www-form-urlencoded; charset=UTF-8', core.String, []));
61966196
return HttpRequest.request(url, {method: 'POST', withCredentials: withCredentials, responseType: responseType, requestHeaders: requestHeaders, sendData: formData, onProgress: onProgress});
@@ -6267,7 +6267,7 @@ dart_library.library('dart/html', null, /* Imports */[
62676267
}
62686268
}
62696269
get responseHeaders() {
6270-
let headers = dart.map();
6270+
let headers = dart.map({}, core.String, core.String);
62716271
let headersString = this.getAllResponseHeaders();
62726272
if (headersString == null) {
62736273
return headers;

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3204,7 +3204,8 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ClosureAnnotator {
32043204
JS.Expression emitMap() {
32053205
var entries = node.entries;
32063206
var mapArguments = null;
3207-
if (entries.isEmpty) {
3207+
var typeArgs = node.typeArguments;
3208+
if (entries.isEmpty && typeArgs == null) {
32083209
mapArguments = [];
32093210
} else if (entries.every((e) => e.key is StringLiteral)) {
32103211
// Use JS object literal notation if possible, otherwise use an array.
@@ -3223,8 +3224,11 @@ class JSCodegenVisitor extends GeneralizingAstVisitor with ClosureAnnotator {
32233224
}
32243225
mapArguments = new JS.ArrayInitializer(values);
32253226
}
3226-
// TODO(jmesserly): add generic types args.
3227-
return js.call('dart.map(#)', [mapArguments]);
3227+
var types = <JS.Expression>[];
3228+
if (typeArgs != null) {
3229+
types.addAll(typeArgs.arguments.map((e) => _emitTypeName(e.type)));
3230+
}
3231+
return js.call('dart.map(#, #)', [mapArguments, types]);
32283232
}
32293233
if (node.constKeyword != null) return _emitConst(emitMap);
32303234
return emitMap();

pkg/dev_compiler/test/codegen/expect/collection/src/wrappers.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ severe: [INVALID_METHOD_OVERRIDE] Invalid override. The type of _DelegatingItera
44
severe: [AnalyzerMessage] Missing concrete implementation of 'Iterable.expand' and 'Iterable.map' (package:collection/src/wrappers.dart, line 97, col 7)
55
severe: [INVALID_METHOD_OVERRIDE] Base class introduces an invalid override. The type of _DelegatingIterableBase.expand (((E) → Iterable<dynamic>) → Iterable<dynamic>) is not a subtype of Iterable<E>.expand (<T>((E) → Iterable<T>) → Iterable<T>). (package:collection/src/wrappers.dart, line 97, col 25)
66
severe: [INVALID_METHOD_OVERRIDE] Base class introduces an invalid override. The type of _DelegatingIterableBase.map (((E) → dynamic) → Iterable<dynamic>) is not a subtype of Iterable<E>.map (<T>((E) → T) → Iterable<T>). (package:collection/src/wrappers.dart, line 97, col 25)
7-
severe: [AnalyzerMessage] Missing concrete implementation of 'Iterable.map' and 'Iterable.expand' (package:collection/src/wrappers.dart, line 194, col 7)
7+
severe: [AnalyzerMessage] Missing concrete implementation of 'Iterable.expand' and 'Iterable.map' (package:collection/src/wrappers.dart, line 194, col 7)
88
severe: [INVALID_METHOD_OVERRIDE] Base class introduces an invalid override. The type of _DelegatingIterableBase.expand (((E) → Iterable<dynamic>) → Iterable<dynamic>) is not a subtype of Iterable<E>.expand (<T>((E) → Iterable<T>) → Iterable<T>). (package:collection/src/wrappers.dart, line 194, col 24)
99
severe: [INVALID_METHOD_OVERRIDE] Base class introduces an invalid override. The type of _DelegatingIterableBase.map (((E) → dynamic) → Iterable<dynamic>) is not a subtype of Iterable<E>.map (<T>((E) → T) → Iterable<T>). (package:collection/src/wrappers.dart, line 194, col 24)
1010
severe: [AnalyzerMessage] Missing concrete implementation of 'Iterable.expand' and 'Iterable.map' (package:collection/src/wrappers.dart, line 245, col 7)
1111
severe: [INVALID_METHOD_OVERRIDE] Base class introduces an invalid override. The type of _DelegatingIterableBase.expand (((E) → Iterable<dynamic>) → Iterable<dynamic>) is not a subtype of Iterable<E>.expand (<T>((E) → Iterable<T>) → Iterable<T>). (package:collection/src/wrappers.dart, line 245, col 26)
1212
severe: [INVALID_METHOD_OVERRIDE] Base class introduces an invalid override. The type of _DelegatingIterableBase.map (((E) → dynamic) → Iterable<dynamic>) is not a subtype of Iterable<E>.map (<T>((E) → T) → Iterable<T>). (package:collection/src/wrappers.dart, line 245, col 26)
13-
severe: [AnalyzerMessage] Missing concrete implementation of 'Iterable.map' and 'Iterable.expand' (package:collection/src/wrappers.dart, line 339, col 7)
13+
severe: [AnalyzerMessage] Missing concrete implementation of 'Iterable.expand' and 'Iterable.map' (package:collection/src/wrappers.dart, line 339, col 7)
1414
severe: [AnalyzerMessage] Missing concrete implementation of 'Iterable.expand' and 'Iterable.map' (package:collection/src/wrappers.dart, line 414, col 7)
1515
severe: [INVALID_METHOD_OVERRIDE] Base class introduces an invalid override. The type of _DelegatingIterableBase.expand (((V) → Iterable<dynamic>) → Iterable<dynamic>) is not a subtype of Iterable<V>.expand (<T>((V) → Iterable<T>) → Iterable<T>). (package:collection/src/wrappers.dart, line 414, col 25)
1616
severe: [INVALID_METHOD_OVERRIDE] Base class introduces an invalid override. The type of _DelegatingIterableBase.map (((V) → dynamic) → Iterable<dynamic>) is not a subtype of Iterable<V>.map (<T>((V) → T) → Iterable<T>). (package:collection/src/wrappers.dart, line 414, col 25)

pkg/dev_compiler/test/codegen/expect/language-all.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29718,10 +29718,10 @@ dart_library.library('language/class_syntax2_test', null, /* Imports */[
2971829718
dart.fn(main, dart.void, []);
2971929719
class Cool extends core.Object {
2972029720
Cool(option) {
29721-
this.thing = dart.notNull(option) ? dart.map() : dart.list([], core.String);
29721+
this.thing = dart.notNull(option) ? dart.map({}, core.String, core.String) : dart.list([], core.String);
2972229722
}
2972329723
alt(option) {
29724-
this.thing = !dart.notNull(option) ? dart.list([], core.String) : dart.map();
29724+
this.thing = !dart.notNull(option) ? dart.list([], core.String) : dart.map({}, core.String, core.String);
2972529725
}
2972629726
}
2972729727
dart.defineNamedConstructor(Cool, 'alt');
@@ -44700,9 +44700,10 @@ dart_library.library('language/const_map3_test_none_multi', null, /* Imports */[
4470044700
});
4470144701
dart_library.library('language/const_map4_test', null, /* Imports */[
4470244702
'dart/_runtime',
44703-
'expect/expect'
44703+
'expect/expect',
44704+
'dart/core'
4470444705
], /* Lazy imports */[
44705-
], function(exports, dart, expect) {
44706+
], function(exports, dart, expect, core) {
4470644707
'use strict';
4470744708
let dartx = dart.dartx;
4470844709
function main() {
@@ -44712,10 +44713,10 @@ dart_library.library('language/const_map4_test', null, /* Imports */[
4471244713
a = dart.const(dart.map([1, 'a', 2, 'b', 3, 'c']));
4471344714
b = dart.const(dart.map([1, 'a', 2, 'b', 3, 'c']));
4471444715
expect.Expect.equals(true, dart.equals(a, b));
44715-
a = dart.const(dart.map([1, 'a', 2, 'b', 3, 'c']));
44716+
a = dart.const(dart.map([1, 'a', 2, 'b', 3, 'c'], core.num, core.String));
4471644717
b = dart.const(dart.map([1, 'a', 2, 'b', 3, 'c']));
4471744718
expect.Expect.equals(false, dart.equals(a, b));
44718-
a = dart.const(dart.map([1, 'a', 2, 'b', 3, 'c']));
44719+
a = dart.const(dart.map([1, 'a', 2, 'b', 3, 'c'], dart.dynamic, dart.dynamic));
4471944720
b = dart.const(dart.map([1, 'a', 2, 'b', 3, 'c']));
4472044721
expect.Expect.equals(true, dart.equals(a, b));
4472144722
}
@@ -45320,8 +45321,8 @@ dart_library.library('language/const_types_test_none_multi', null, /* Imports */
4532045321
use(dart.const(dart.list([], Class$())));
4532145322
use(dart.const(dart.list([], Class$(core.int))));
4532245323
use(dart.const(dart.map()));
45323-
use(dart.const(dart.map()));
45324-
use(dart.const(dart.map()));
45324+
use(dart.const(dart.map({}, core.String, Class$())));
45325+
use(dart.const(dart.map({}, core.String, Class$(core.int))));
4532545326
use(dart.const(new (Class$())()));
4532645327
use(dart.const(new (Class$(core.int))()));
4532745328
use(dart.const(new (Class$()).named()));
@@ -45390,15 +45391,15 @@ dart_library.library('language/const_var_test', null, /* Imports */[
4539045391
let dartx = dart.dartx;
4539145392
const untypedTopLevel = 1;
4539245393
const typedTopLevel = 2;
45393-
const genericTopLevel = dart.const(dart.map());
45394+
const genericTopLevel = dart.const(dart.map({}, core.String, core.String));
4539445395
function main() {
4539545396
let untypedLocal = 3;
4539645397
let typedLocal = 4;
45397-
let genericLocal = dart.const(dart.map());
45398+
let genericLocal = dart.const(dart.map({}, core.String, core.String));
4539845399
dart.const([]);
4539945400
dart.const(dart.map());
4540045401
dart.const(dart.list([], core.int));
45401-
dart.const(dart.map());
45402+
dart.const(dart.map({}, core.String, core.int));
4540245403
dart.const(new Foo());
4540345404
dart.const(new (Foo$(core.int))());
4540445405
dart.const(new const_var_helper.Foo());
@@ -112000,7 +112001,7 @@ dart_library.library('language/map_literal7_test', null, /* Imports */[
112000112001
expect.Expect.isTrue(dart.is(m1, core.Map$(core.String, core.int)));
112001112002
expect.Expect.isTrue(dart.is(m1, core.Map$(core.int, dart.dynamic)));
112002112003
expect.Expect.isTrue(dart.is(m1, core.Map$(dart.dynamic, core.String)));
112003-
let m2 = dart.const(dart.map({"0": 0, "1": 1}));
112004+
let m2 = dart.const(dart.map({"0": 0, "1": 1}, core.String, core.int));
112004112005
expect.Expect.isTrue(dart.is(m2, core.Map));
112005112006
expect.Expect.isTrue(dart.is(m2, core.Map$(core.String, core.int)));
112006112007
expect.Expect.isFalse(dart.is(m2, core.Map$(core.int, dart.dynamic)));
@@ -112045,9 +112046,9 @@ dart_library.library('language/map_literal_syntax_test', null, /* Imports */[
112045112046
class Foo extends core.Object {
112046112047
Foo() {
112047112048
this.x = dart.map();
112048-
this.y = dart.map();
112049+
this.y = dart.map({}, core.String, core.int);
112049112050
this.z = dart.const(dart.map());
112050-
this.v = dart.const(dart.map());
112051+
this.v = dart.const(dart.map({}, core.String, core.int));
112051112052
}
112052112053
}
112053112054
dart.setSignature(Foo, {
@@ -136300,7 +136301,7 @@ dart_library.library('language/symbol_literal_test_none_multi', null, /* Imports
136300136301
dart.dcall(exports.check, dart.const(core.Symbol.new("==")), dart.const(new core.Symbol('==')));
136301136302
dart.dcall(exports.check, dart.const(core.Symbol.new("a.toString")), dart.const(new core.Symbol('a.toString')));
136302136303
expect.Expect.equals(1, testSwitch(dart.const(new core.Symbol('abc'))));
136303-
let m = dart.const(dart.map([dart.const(new core.Symbol('A')), 0, dart.const(new core.Symbol('B')), 1]));
136304+
let m = dart.const(dart.map([dart.const(new core.Symbol('A')), 0, dart.const(new core.Symbol('B')), 1], core.Symbol, core.int));
136304136305
expect.Expect.equals(1, m.get(dart.const(new core.Symbol('B'))));
136305136306
}
136306136307
dart.fn(main);
@@ -141313,7 +141314,7 @@ dart_library.library('language/type_variable_closure2_test', null, /* Imports */
141313141314
return dart.fn(() => dart.list([], T), core.List$(T), []);
141314141315
}
141315141316
map() {
141316-
return dart.fn(() => dart.map(), core.Map$(T, T), []);
141317+
return dart.fn(() => dart.map({}, T, T), core.Map$(T, T), []);
141317141318
}
141318141319
}
141319141320
dart.setSignature(C, {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,12 @@ notNull(x) => JS('', '''(() => {
246246
/// example `map()`.
247247
///
248248
// TODO(jmesserly): this could be faster
249-
map(values) => JS('', '''(() => {
250-
let map = $LinkedHashMap.new();
249+
// TODO(jmesserly): we can use default values `= dynamic` once #417 is fixed.
250+
// TODO(jmesserly): move this to classes for consistentcy with list literals?
251+
map(values, [K, V]) => JS('', '''(() => {
252+
if ($K == null) $K = $dynamicR;
253+
if ($V == null) $V = $dynamicR;
254+
let map = ${getGenericClass(LinkedHashMap)}($K, $V).new();
251255
if (Array.isArray($values)) {
252256
for (let i = 0, end = $values.length - 1; i < end; i += 2) {
253257
let key = $values[i];

0 commit comments

Comments
 (0)