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 library dart._isolate_helper; | 5 library dart._isolate_helper; |
6 | 6 |
7 import 'dart:_js_embedded_names' show | 7 import 'dart:_js_embedded_names' show |
8 CLASS_ID_EXTRACTOR, | 8 CLASS_ID_EXTRACTOR, |
9 CLASS_FIELDS_EXTRACTOR, | 9 CLASS_FIELDS_EXTRACTOR, |
10 CURRENT_SCRIPT, | 10 CURRENT_SCRIPT, |
11 GLOBAL_FUNCTIONS, | 11 GLOBAL_FUNCTIONS, |
12 INITIALIZE_EMPTY_INSTANCE, | 12 INITIALIZE_EMPTY_INSTANCE, |
13 INSTANCE_FROM_CLASS_ID; | 13 INSTANCE_FROM_CLASS_ID; |
14 | 14 |
15 import 'dart:async'; | 15 import 'dart:async'; |
16 import 'dart:collection' show Queue, HashMap; | 16 import 'dart:collection' show Queue, HashMap; |
17 import 'dart:isolate'; | 17 import 'dart:isolate'; |
18 import 'dart:_native_typed_data' show NativeByteBuffer, NativeTypedData; | 18 import 'dart:_native_typed_data' show NativeByteBuffer, NativeTypedData; |
19 | 19 |
20 import 'dart:_js_helper' show | 20 import 'dart:_js_helper' show |
21 InternalMap, | 21 InternalMap, |
22 Null, | 22 Null, |
23 Primitives, | 23 Primitives, |
24 convertDartClosureToJS, | |
25 random64; | 24 random64; |
26 | 25 |
27 import 'dart:_foreign_helper' show DART_CLOSURE_TO_JS, | 26 import 'dart:_foreign_helper' show JS, |
28 JS, | |
29 JS_CREATE_ISOLATE, | 27 JS_CREATE_ISOLATE, |
30 JS_CURRENT_ISOLATE_CONTEXT, | 28 JS_CURRENT_ISOLATE_CONTEXT, |
31 JS_CURRENT_ISOLATE, | 29 JS_CURRENT_ISOLATE, |
32 JS_EMBEDDED_GLOBAL, | 30 JS_EMBEDDED_GLOBAL, |
33 JS_SET_CURRENT_ISOLATE, | 31 JS_SET_CURRENT_ISOLATE, |
34 IsolateContext; | 32 IsolateContext; |
35 | 33 |
36 import 'dart:_interceptors' show Interceptor, | 34 import 'dart:_interceptors' show Interceptor, |
37 JSArray, | 35 JSArray, |
38 JSExtendableArray, | 36 JSExtendableArray, |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 | 236 |
239 isWorker = !isWindowDefined && globalPostMessageDefined; | 237 isWorker = !isWindowDefined && globalPostMessageDefined; |
240 supportsWorkers = isWorker | 238 supportsWorkers = isWorker |
241 || (isWorkerDefined && IsolateNatives.thisScript != null); | 239 || (isWorkerDefined && IsolateNatives.thisScript != null); |
242 fromCommandLine = !isWindowDefined && !isWorker; | 240 fromCommandLine = !isWindowDefined && !isWorker; |
243 } | 241 } |
244 | 242 |
245 void _nativeInitWorkerMessageHandler() { | 243 void _nativeInitWorkerMessageHandler() { |
246 var function = JS('', | 244 var function = JS('', |
247 "(function (f, a) { return function (e) { f(a, e); }})(#, #)", | 245 "(function (f, a) { return function (e) { f(a, e); }})(#, #)", |
248 DART_CLOSURE_TO_JS(IsolateNatives._processWorkerMessage), | 246 IsolateNatives._processWorkerMessage, |
249 mainManager); | 247 mainManager); |
250 JS("void", r"self.onmessage = #", function); | 248 JS("void", r"self.onmessage = #", function); |
251 // We ensure dartPrint is defined so that the implementation of the Dart | 249 // We ensure dartPrint is defined so that the implementation of the Dart |
252 // print method knows what to call. | 250 // print method knows what to call. |
253 JS('', '''self.dartPrint = self.dartPrint || (function(serialize) { | 251 JS('', '''self.dartPrint = self.dartPrint || (function(serialize) { |
254 return function (object) { | 252 return function (object) { |
255 if (self.console && self.console.log) { | 253 if (self.console && self.console.log) { |
256 self.console.log(object) | 254 self.console.log(object) |
257 } else { | 255 } else { |
258 self.postMessage(serialize(object)); | 256 self.postMessage(serialize(object)); |
259 } | 257 } |
260 } | 258 } |
261 })(#)''', DART_CLOSURE_TO_JS(_serializePrintMessage)); | 259 })(#)''', _serializePrintMessage); |
262 } | 260 } |
263 | 261 |
264 static _serializePrintMessage(object) { | 262 static _serializePrintMessage(object) { |
265 return _serializeMessage({"command": "print", "msg": object}); | 263 return _serializeMessage({"command": "print", "msg": object}); |
266 } | 264 } |
267 | 265 |
268 /** | 266 /** |
269 * Close the worker running this code if all isolates are done and | 267 * Close the worker running this code if all isolates are done and |
270 * there are no active async JavaScript tasks still running. | 268 * there are no active async JavaScript tasks still running. |
271 */ | 269 */ |
(...skipping 821 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1093 // top-level or static methods, and the trampoline allows us to capture | 1091 // top-level or static methods, and the trampoline allows us to capture |
1094 // arguments and values which can be passed to a static method. | 1092 // arguments and values which can be passed to a static method. |
1095 final onerrorTrampoline = JS( | 1093 final onerrorTrampoline = JS( |
1096 '', | 1094 '', |
1097 ''' | 1095 ''' |
1098 (function (f, u, c) { | 1096 (function (f, u, c) { |
1099 return function(e) { | 1097 return function(e) { |
1100 return f(e, u, c) | 1098 return f(e, u, c) |
1101 } | 1099 } |
1102 })(#, #, #)''', | 1100 })(#, #, #)''', |
1103 DART_CLOSURE_TO_JS(workerOnError), uri, onError); | 1101 workerOnError, uri, onError); |
1104 JS('void', '#.onerror = #', worker, onerrorTrampoline); | 1102 JS('void', '#.onerror = #', worker, onerrorTrampoline); |
1105 | 1103 |
1106 var processWorkerMessageTrampoline = JS( | 1104 var processWorkerMessageTrampoline = JS( |
1107 '', | 1105 '', |
1108 """ | 1106 """ |
1109 (function (f, a) { | 1107 (function (f, a) { |
1110 return function (e) { | 1108 return function (e) { |
1111 // We can stop listening for errors when the first message is received as | 1109 // We can stop listening for errors when the first message is received as |
1112 // we only listen for messages to determine if the uri was bad. | 1110 // we only listen for messages to determine if the uri was bad. |
1113 e.onerror = null; | 1111 e.onerror = null; |
1114 return f(a, e); | 1112 return f(a, e); |
1115 } | 1113 } |
1116 })(#, #)""", | 1114 })(#, #)""", |
1117 DART_CLOSURE_TO_JS(_processWorkerMessage), | 1115 _processWorkerMessage, |
1118 worker); | 1116 worker); |
1119 JS('void', '#.onmessage = #', worker, processWorkerMessageTrampoline); | 1117 JS('void', '#.onmessage = #', worker, processWorkerMessageTrampoline); |
1120 var workerId = _globalState.nextManagerId++; | 1118 var workerId = _globalState.nextManagerId++; |
1121 // We also store the id on the worker itself so that we can unregister it. | 1119 // We also store the id on the worker itself so that we can unregister it. |
1122 workerIds[worker] = workerId; | 1120 workerIds[worker] = workerId; |
1123 _globalState.managers[workerId] = worker; | 1121 _globalState.managers[workerId] = worker; |
1124 JS('void', '#.postMessage(#)', worker, _serializeMessage({ | 1122 JS('void', '#.postMessage(#)', worker, _serializeMessage({ |
1125 'command': 'start', | 1123 'command': 'start', |
1126 'id': workerId, | 1124 'id': workerId, |
1127 // Note: we serialize replyPort twice because the child worker needs to | 1125 // Note: we serialize replyPort twice because the child worker needs to |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1360 } else if (hasTimer()) { | 1358 } else if (hasTimer()) { |
1361 | 1359 |
1362 void internalCallback() { | 1360 void internalCallback() { |
1363 _handle = null; | 1361 _handle = null; |
1364 leaveJsAsync(); | 1362 leaveJsAsync(); |
1365 callback(); | 1363 callback(); |
1366 } | 1364 } |
1367 | 1365 |
1368 enterJsAsync(); | 1366 enterJsAsync(); |
1369 | 1367 |
1370 _handle = JS('int', 'self.setTimeout(#, #)', | 1368 _handle = JS( |
1371 convertDartClosureToJS(internalCallback, 0), | 1369 'int', 'self.setTimeout(#, #)', internalCallback, milliseconds); |
1372 milliseconds); | |
1373 } else { | 1370 } else { |
1374 assert(milliseconds > 0); | 1371 assert(milliseconds > 0); |
1375 throw new UnsupportedError("Timer greater than 0."); | 1372 throw new UnsupportedError("Timer greater than 0."); |
1376 } | 1373 } |
1377 } | 1374 } |
1378 | 1375 |
1379 TimerImpl.periodic(int milliseconds, void callback(Timer timer)) | 1376 TimerImpl.periodic(int milliseconds, void callback(Timer timer)) |
1380 : _once = false { | 1377 : _once = false { |
1381 if (hasTimer()) { | 1378 if (hasTimer()) { |
1382 enterJsAsync(); | 1379 enterJsAsync(); |
1383 _handle = JS('int', 'self.setInterval(#, #)', | 1380 _handle = JS('int', 'self.setInterval(#, #)', |
1384 convertDartClosureToJS(() { callback(this); }, 0), | 1381 () { callback(this); }, milliseconds); |
1385 milliseconds); | |
1386 } else { | 1382 } else { |
1387 throw new UnsupportedError("Periodic timer."); | 1383 throw new UnsupportedError("Periodic timer."); |
1388 } | 1384 } |
1389 } | 1385 } |
1390 | 1386 |
1391 void cancel() { | 1387 void cancel() { |
1392 if (hasTimer()) { | 1388 if (hasTimer()) { |
1393 if (_inEventLoop) { | 1389 if (_inEventLoop) { |
1394 throw new UnsupportedError("Timer in event loop cannot be canceled."); | 1390 throw new UnsupportedError("Timer in event loop cannot be canceled."); |
1395 } | 1391 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1443 } | 1439 } |
1444 | 1440 |
1445 bool operator==(Object other) { | 1441 bool operator==(Object other) { |
1446 if (identical(other, this)) return true; | 1442 if (identical(other, this)) return true; |
1447 if (other is CapabilityImpl) { | 1443 if (other is CapabilityImpl) { |
1448 return identical(_id, other._id); | 1444 return identical(_id, other._id); |
1449 } | 1445 } |
1450 return false; | 1446 return false; |
1451 } | 1447 } |
1452 } | 1448 } |
OLD | NEW |