| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // Patch file for the dart:isolate library. | 5 // Patch file for the dart:isolate library. |
| 6 | 6 |
| 7 #import("dart:uri"); | 7 #import("dart:uri"); |
| 8 | 8 |
| 9 // Source in shared isolate serialization implementation. | |
| 10 #source("../../../js_isolate_impl/isolate.dart"); | |
| 11 | |
| 12 /** | 9 /** |
| 13 * Called by the compiler to support switching | 10 * Called by the compiler to support switching |
| 14 * between isolates when we get a callback from the DOM. | 11 * between isolates when we get a callback from the DOM. |
| 15 */ | 12 */ |
| 16 void _callInIsolate(_IsolateContext isolate, Function function) { | 13 void _callInIsolate(_IsolateContext isolate, Function function) { |
| 17 isolate.eval(function); | 14 isolate.eval(function); |
| 18 _globalState.topEventLoop.run(); | 15 _globalState.topEventLoop.run(); |
| 19 } | 16 } |
| 20 | 17 |
| 21 /** | 18 /** |
| (...skipping 992 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1014 | 1011 |
| 1015 // only visible for testing purposes | 1012 // only visible for testing purposes |
| 1016 static serialize(x) { | 1013 static serialize(x) { |
| 1017 _Serializer serializer = new _JsSerializer(); | 1014 _Serializer serializer = new _JsSerializer(); |
| 1018 _Deserializer deserializer = new _JsDeserializer(); | 1015 _Deserializer deserializer = new _JsDeserializer(); |
| 1019 return deserializer.deserialize(serializer.traverse(x)); | 1016 return deserializer.deserialize(serializer.traverse(x)); |
| 1020 } | 1017 } |
| 1021 } | 1018 } |
| 1022 | 1019 |
| 1023 /******************************************************** | 1020 /******************************************************** |
| 1021 Inserted from lib/isolate/serialization.dart |
| 1022 ********************************************************/ |
| 1023 |
| 1024 class _MessageTraverserVisitedMap { |
| 1025 |
| 1026 operator[](var object) => null; |
| 1027 void operator[]=(var object, var info) { } |
| 1028 |
| 1029 void reset() { } |
| 1030 void cleanup() { } |
| 1031 |
| 1032 } |
| 1033 |
| 1034 /** Abstract visitor for dart objects that can be sent as isolate messages. */ |
| 1035 class _MessageTraverser { |
| 1036 |
| 1037 _MessageTraverserVisitedMap _visited; |
| 1038 _MessageTraverser() : _visited = new _MessageTraverserVisitedMap(); |
| 1039 |
| 1040 /** Visitor's entry point. */ |
| 1041 traverse(var x) { |
| 1042 if (isPrimitive(x)) return visitPrimitive(x); |
| 1043 _visited.reset(); |
| 1044 var result; |
| 1045 try { |
| 1046 result = _dispatch(x); |
| 1047 } finally { |
| 1048 _visited.cleanup(); |
| 1049 } |
| 1050 return result; |
| 1051 } |
| 1052 |
| 1053 _dispatch(var x) { |
| 1054 if (isPrimitive(x)) return visitPrimitive(x); |
| 1055 if (x is List) return visitList(x); |
| 1056 if (x is Map) return visitMap(x); |
| 1057 if (x is SendPort) return visitSendPort(x); |
| 1058 if (x is SendPortSync) return visitSendPortSync(x); |
| 1059 |
| 1060 // Overridable fallback. |
| 1061 return visitObject(x); |
| 1062 } |
| 1063 |
| 1064 abstract visitPrimitive(x); |
| 1065 abstract visitList(List x); |
| 1066 abstract visitMap(Map x); |
| 1067 abstract visitSendPort(SendPort x); |
| 1068 abstract visitSendPortSync(SendPortSync x); |
| 1069 |
| 1070 visitObject(Object x) { |
| 1071 // TODO(floitsch): make this a real exception. (which one)? |
| 1072 throw "Message serialization: Illegal value $x passed"; |
| 1073 } |
| 1074 |
| 1075 static bool isPrimitive(x) { |
| 1076 return (x === null) || (x is String) || (x is num) || (x is bool); |
| 1077 } |
| 1078 } |
| 1079 |
| 1080 |
| 1081 /** A visitor that recursively copies a message. */ |
| 1082 class _Copier extends _MessageTraverser { |
| 1083 |
| 1084 visitPrimitive(x) => x; |
| 1085 |
| 1086 List visitList(List list) { |
| 1087 List copy = _visited[list]; |
| 1088 if (copy !== null) return copy; |
| 1089 |
| 1090 int len = list.length; |
| 1091 |
| 1092 // TODO(floitsch): we loose the generic type of the List. |
| 1093 copy = new List(len); |
| 1094 _visited[list] = copy; |
| 1095 for (int i = 0; i < len; i++) { |
| 1096 copy[i] = _dispatch(list[i]); |
| 1097 } |
| 1098 return copy; |
| 1099 } |
| 1100 |
| 1101 Map visitMap(Map map) { |
| 1102 Map copy = _visited[map]; |
| 1103 if (copy !== null) return copy; |
| 1104 |
| 1105 // TODO(floitsch): we loose the generic type of the map. |
| 1106 copy = new Map(); |
| 1107 _visited[map] = copy; |
| 1108 map.forEach((key, val) { |
| 1109 copy[_dispatch(key)] = _dispatch(val); |
| 1110 }); |
| 1111 return copy; |
| 1112 } |
| 1113 |
| 1114 } |
| 1115 |
| 1116 /** Visitor that serializes a message as a JSON array. */ |
| 1117 class _Serializer extends _MessageTraverser { |
| 1118 int _nextFreeRefId = 0; |
| 1119 |
| 1120 visitPrimitive(x) => x; |
| 1121 |
| 1122 visitList(List list) { |
| 1123 int copyId = _visited[list]; |
| 1124 if (copyId !== null) return ['ref', copyId]; |
| 1125 |
| 1126 int id = _nextFreeRefId++; |
| 1127 _visited[list] = id; |
| 1128 var jsArray = _serializeList(list); |
| 1129 // TODO(floitsch): we are losing the generic type. |
| 1130 return ['list', id, jsArray]; |
| 1131 } |
| 1132 |
| 1133 visitMap(Map map) { |
| 1134 int copyId = _visited[map]; |
| 1135 if (copyId !== null) return ['ref', copyId]; |
| 1136 |
| 1137 int id = _nextFreeRefId++; |
| 1138 _visited[map] = id; |
| 1139 var keys = _serializeList(map.getKeys()); |
| 1140 var values = _serializeList(map.getValues()); |
| 1141 // TODO(floitsch): we are losing the generic type. |
| 1142 return ['map', id, keys, values]; |
| 1143 } |
| 1144 |
| 1145 _serializeList(List list) { |
| 1146 int len = list.length; |
| 1147 var result = new List(len); |
| 1148 for (int i = 0; i < len; i++) { |
| 1149 result[i] = _dispatch(list[i]); |
| 1150 } |
| 1151 return result; |
| 1152 } |
| 1153 } |
| 1154 |
| 1155 /** Deserializes arrays created with [_Serializer]. */ |
| 1156 class _Deserializer { |
| 1157 Map<int, Dynamic> _deserialized; |
| 1158 |
| 1159 _Deserializer(); |
| 1160 |
| 1161 static bool isPrimitive(x) { |
| 1162 return (x === null) || (x is String) || (x is num) || (x is bool); |
| 1163 } |
| 1164 |
| 1165 deserialize(x) { |
| 1166 if (isPrimitive(x)) return x; |
| 1167 // TODO(floitsch): this should be new HashMap<int, var|Dynamic>() |
| 1168 _deserialized = new HashMap(); |
| 1169 return _deserializeHelper(x); |
| 1170 } |
| 1171 |
| 1172 _deserializeHelper(x) { |
| 1173 if (isPrimitive(x)) return x; |
| 1174 assert(x is List); |
| 1175 switch (x[0]) { |
| 1176 case 'ref': return _deserializeRef(x); |
| 1177 case 'list': return _deserializeList(x); |
| 1178 case 'map': return _deserializeMap(x); |
| 1179 case 'sendport': return deserializeSendPort(x); |
| 1180 default: return deserializeObject(x); |
| 1181 } |
| 1182 } |
| 1183 |
| 1184 _deserializeRef(List x) { |
| 1185 int id = x[1]; |
| 1186 var result = _deserialized[id]; |
| 1187 assert(result !== null); |
| 1188 return result; |
| 1189 } |
| 1190 |
| 1191 List _deserializeList(List x) { |
| 1192 int id = x[1]; |
| 1193 // We rely on the fact that Dart-lists are directly mapped to Js-arrays. |
| 1194 List dartList = x[2]; |
| 1195 _deserialized[id] = dartList; |
| 1196 int len = dartList.length; |
| 1197 for (int i = 0; i < len; i++) { |
| 1198 dartList[i] = _deserializeHelper(dartList[i]); |
| 1199 } |
| 1200 return dartList; |
| 1201 } |
| 1202 |
| 1203 Map _deserializeMap(List x) { |
| 1204 Map result = new Map(); |
| 1205 int id = x[1]; |
| 1206 _deserialized[id] = result; |
| 1207 List keys = x[2]; |
| 1208 List values = x[3]; |
| 1209 int len = keys.length; |
| 1210 assert(len == values.length); |
| 1211 for (int i = 0; i < len; i++) { |
| 1212 var key = _deserializeHelper(keys[i]); |
| 1213 var value = _deserializeHelper(values[i]); |
| 1214 result[key] = value; |
| 1215 } |
| 1216 return result; |
| 1217 } |
| 1218 |
| 1219 abstract deserializeSendPort(List x); |
| 1220 |
| 1221 deserializeObject(List x) { |
| 1222 // TODO(floitsch): Use real exception (which one?). |
| 1223 throw "Unexpected serialized object"; |
| 1224 } |
| 1225 } |
| 1226 |
| 1227 /******************************************************** |
| 1024 Inserted from lib/isolate/dart2js/timer_provider.dart | 1228 Inserted from lib/isolate/dart2js/timer_provider.dart |
| 1025 ********************************************************/ | 1229 ********************************************************/ |
| 1026 | 1230 |
| 1027 // We don't want to import the DOM library just because of window.setTimeout, | 1231 // We don't want to import the DOM library just because of window.setTimeout, |
| 1028 // so we reconstruct the Window class here. The only conflict that could happen | 1232 // so we reconstruct the Window class here. The only conflict that could happen |
| 1029 // with the other DOMWindow class would be because of subclasses. | 1233 // with the other DOMWindow class would be because of subclasses. |
| 1030 // Currently, none of the two Dart classes have subclasses. | 1234 // Currently, none of the two Dart classes have subclasses. |
| 1031 typedef void _TimeoutHandler(); | 1235 typedef void _TimeoutHandler(); |
| 1032 | 1236 |
| 1033 class _Window native "@*DOMWindow" { | 1237 class _Window native "@*DOMWindow" { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 1057 _window.clearTimeout(_handle); | 1261 _window.clearTimeout(_handle); |
| 1058 } else { | 1262 } else { |
| 1059 _window.clearInterval(_handle); | 1263 _window.clearInterval(_handle); |
| 1060 } | 1264 } |
| 1061 } | 1265 } |
| 1062 } | 1266 } |
| 1063 | 1267 |
| 1064 Timer _timerFactory(int millis, void callback(Timer timer), bool repeating) => | 1268 Timer _timerFactory(int millis, void callback(Timer timer), bool repeating) => |
| 1065 repeating ? new _Timer.repeating(millis, callback) | 1269 repeating ? new _Timer.repeating(millis, callback) |
| 1066 : new _Timer(millis, callback); | 1270 : new _Timer(millis, callback); |
| OLD | NEW |