OLD | NEW |
1 part of html_common; | 1 part of html_common; |
2 | 2 |
3 /// Converts a JavaScript object with properties into a Dart Map. | 3 /// Converts a JavaScript object with properties into a Dart Map. |
4 /// Not suitable for nested objects. | 4 /// Not suitable for nested objects. |
5 Map convertNativeToDart_Dictionary(object) { | 5 Map convertNativeToDart_Dictionary(object) { |
6 if (object == null) return null; | 6 if (object == null) return null; |
7 var dict = {}; | 7 var dict = {}; |
8 var keys = JS('JSExtendableArray', 'Object.getOwnPropertyNames(#)', object); | 8 var keys = JS('JSExtendableArray', 'Object.getOwnPropertyNames(#)', object); |
9 for (final key in keys) { | 9 for (final key in keys) { |
10 dict[key] = JS('var', '#[#]', object, key); | 10 dict[key] = JS('var', '#[#]', object, key); |
11 } | 11 } |
12 return dict; | 12 return dict; |
13 } | 13 } |
14 | 14 |
15 /// Converts a flat Dart map into a JavaScript object with properties. | 15 /// Converts a flat Dart map into a JavaScript object with properties. |
16 convertDartToNative_Dictionary(Map dict, [void postCreate(dynamic)]) { | 16 convertDartToNative_Dictionary(Map dict, [void postCreate(dynamic)]) { |
17 if (dict == null) return null; | 17 if (dict == null) return null; |
18 var object = JS('var', '{}'); | 18 var object = JS('var', '{}'); |
19 if (postCreate != null) { | 19 if (postCreate != null) { |
20 postCreate(object); | 20 postCreate(object); |
21 } | 21 } |
22 dict.forEach((String key, value) { | 22 dict.forEach((String key, value) { |
23 JS('void', '#[#] = #', object, key, value); | 23 JS('void', '#[#] = #', object, key, value); |
24 }); | 24 }); |
25 return object; | 25 return object; |
26 } | 26 } |
27 | 27 |
28 | |
29 /** | 28 /** |
30 * Ensures that the input is a JavaScript Array. | 29 * Ensures that the input is a JavaScript Array. |
31 * | 30 * |
32 * Creates a new JavaScript array if necessary, otherwise returns the original. | 31 * Creates a new JavaScript array if necessary, otherwise returns the original. |
33 */ | 32 */ |
34 List convertDartToNative_StringArray(List<String> input) { | 33 List convertDartToNative_StringArray(List<String> input) { |
35 // TODO(sra). Implement this. | 34 // TODO(sra). Implement this. |
36 return input; | 35 return input; |
37 } | 36 } |
38 | 37 |
39 DateTime convertNativeToDart_DateTime(date) { | 38 DateTime convertNativeToDart_DateTime(date) { |
40 var millisSinceEpoch = JS('int', '#.getTime()', date); | 39 var millisSinceEpoch = JS('int', '#.getTime()', date); |
41 return new DateTime.fromMillisecondsSinceEpoch(millisSinceEpoch, isUtc: true); | 40 return new DateTime.fromMillisecondsSinceEpoch(millisSinceEpoch, isUtc: true); |
42 } | 41 } |
43 | 42 |
44 convertDartToNative_DateTime(DateTime date) { | 43 convertDartToNative_DateTime(DateTime date) { |
45 return JS('', 'new Date(#)', date.millisecondsSinceEpoch); | 44 return JS('', 'new Date(#)', date.millisecondsSinceEpoch); |
46 } | 45 } |
47 | 46 |
48 convertDartToNative_PrepareForStructuredClone(value) => | 47 convertDartToNative_PrepareForStructuredClone(value) => |
49 new _StructuredCloneDart2Js().convertDartToNative_PrepareForStructuredClone(
value); | 48 new _StructuredCloneDart2Js() |
| 49 .convertDartToNative_PrepareForStructuredClone(value); |
50 | 50 |
51 convertNativeToDart_AcceptStructuredClone(object, {mustCopy: false}) => | 51 convertNativeToDart_AcceptStructuredClone(object, {mustCopy: false}) => |
52 new _AcceptStructuredCloneDart2Js().convertNativeToDart_AcceptStructuredClon
e(object, mustCopy: mustCopy); | 52 new _AcceptStructuredCloneDart2Js() |
| 53 .convertNativeToDart_AcceptStructuredClone(object, mustCopy: mustCopy); |
53 | 54 |
54 class _StructuredCloneDart2Js extends _StructuredClone { | 55 class _StructuredCloneDart2Js extends _StructuredClone { |
55 newJsMap() => JS('var', '{}'); | 56 newJsMap() => JS('var', '{}'); |
56 putIntoMap(map, key, value) => JS('void', '#[#] = #', map, key, value); | 57 putIntoMap(map, key, value) => JS('void', '#[#] = #', map, key, value); |
57 newJsList(length) => JS('JSExtendableArray', 'new Array(#)', length); | 58 newJsList(length) => JS('JSExtendableArray', 'new Array(#)', length); |
58 cloneNotRequired(e) => (e is NativeByteBuffer || e is NativeTypedData); | 59 cloneNotRequired(e) => (e is NativeByteBuffer || e is NativeTypedData); |
59 } | 60 } |
60 | 61 |
61 class _AcceptStructuredCloneDart2Js extends _AcceptStructuredClone { | 62 class _AcceptStructuredCloneDart2Js extends _AcceptStructuredClone { |
62 | |
63 List newJsList(length) => JS('JSExtendableArray', 'new Array(#)', length); | 63 List newJsList(length) => JS('JSExtendableArray', 'new Array(#)', length); |
64 List newDartList(length) => newJsList(length); | 64 List newDartList(length) => newJsList(length); |
65 bool identicalInJs(a, b) => identical(a, b); | 65 bool identicalInJs(a, b) => identical(a, b); |
66 | 66 |
67 void forEachJsField(object, action(key, value)) { | 67 void forEachJsField(object, action(key, value)) { |
68 for (final key in JS('JSExtendableArray', 'Object.keys(#)', object)) { | 68 for (final key in JS('JSExtendableArray', 'Object.keys(#)', object)) { |
69 action(key, JS('var', '#[#]', object, key)); | 69 action(key, JS('var', '#[#]', object, key)); |
70 } | 70 } |
71 } | 71 } |
72 } | 72 } |
73 | 73 |
74 bool isJavaScriptDate(value) => JS('bool', '# instanceof Date', value); | 74 bool isJavaScriptDate(value) => JS('bool', '# instanceof Date', value); |
75 bool isJavaScriptRegExp(value) => JS('bool', '# instanceof RegExp', value); | 75 bool isJavaScriptRegExp(value) => JS('bool', '# instanceof RegExp', value); |
76 bool isJavaScriptArray(value) => JS('bool', '# instanceof Array', value); | 76 bool isJavaScriptArray(value) => JS('bool', '# instanceof Array', value); |
77 bool isJavaScriptSimpleObject(value) { | 77 bool isJavaScriptSimpleObject(value) { |
78 var proto = JS('', 'Object.getPrototypeOf(#)', value); | 78 var proto = JS('', 'Object.getPrototypeOf(#)', value); |
79 return JS('bool', '# === Object.prototype', proto) || | 79 return JS('bool', '# === Object.prototype', proto) || |
80 JS('bool', '# === null', proto); | 80 JS('bool', '# === null', proto); |
81 } | 81 } |
| 82 |
82 bool isImmutableJavaScriptArray(value) => | 83 bool isImmutableJavaScriptArray(value) => |
83 JS('bool', r'!!(#.immutable$list)', value); | 84 JS('bool', r'!!(#.immutable$list)', value); |
84 bool isJavaScriptPromise(value) => | 85 bool isJavaScriptPromise(value) => |
85 JS('bool', r'typeof Promise != "undefined" && # instanceof Promise', value); | 86 JS('bool', r'typeof Promise != "undefined" && # instanceof Promise', value); |
86 | 87 |
87 Future convertNativePromiseToDartFuture(promise) { | 88 Future convertNativePromiseToDartFuture(promise) { |
88 var completer = new Completer(); | 89 var completer = new Completer(); |
89 var then = convertDartClosureToJS((result) => completer.complete(result), 1); | 90 var then = convertDartClosureToJS((result) => completer.complete(result), 1); |
90 var error = convertDartClosureToJS((result) => completer.completeError(result)
, 1); | 91 var error = |
| 92 convertDartClosureToJS((result) => completer.completeError(result), 1); |
91 var newPromise = JS('', '#.then(#)["catch"](#)', promise, then, error); | 93 var newPromise = JS('', '#.then(#)["catch"](#)', promise, then, error); |
92 return completer.future; | 94 return completer.future; |
93 } | 95 } |
OLD | NEW |