| 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, |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 supportsWorkers = isWorker | 238 supportsWorkers = isWorker |
| 239 || (isWorkerDefined && IsolateNatives.thisScript != null); | 239 || (isWorkerDefined && IsolateNatives.thisScript != null); |
| 240 fromCommandLine = !isWindowDefined && !isWorker; | 240 fromCommandLine = !isWindowDefined && !isWorker; |
| 241 } | 241 } |
| 242 | 242 |
| 243 void _nativeInitWorkerMessageHandler() { | 243 void _nativeInitWorkerMessageHandler() { |
| 244 var function = JS('', | 244 var function = JS('', |
| 245 "(function (f, a) { return function (e) { f(a, e); }})(#, #)", | 245 "(function (f, a) { return function (e) { f(a, e); }})(#, #)", |
| 246 IsolateNatives._processWorkerMessage, | 246 IsolateNatives._processWorkerMessage, |
| 247 mainManager); | 247 mainManager); |
| 248 JS("void", r"self.onmessage = #", function); | 248 JS("void", r"#.onmessage = #", global, function); |
| 249 // 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 |
| 250 // print method knows what to call. | 250 // print method knows what to call. |
| 251 JS('', '''self.dartPrint = self.dartPrint || (function(serialize) { | 251 JS('', '''#.dartPrint = #.dartPrint || (function(serialize) { |
| 252 return function (object) { | 252 return function (object) { |
| 253 if (self.console && self.console.log) { | 253 var _self = #; |
| 254 self.console.log(object) | 254 if (_self.console && _self.console.log) { |
| 255 _self.console.log(object) |
| 255 } else { | 256 } else { |
| 256 self.postMessage(serialize(object)); | 257 _self.postMessage(serialize(object)); |
| 257 } | 258 } |
| 258 } | 259 } |
| 259 })(#)''', _serializePrintMessage); | 260 })(#)''', global, global, global, _serializePrintMessage); |
| 260 } | 261 } |
| 261 | 262 |
| 262 static _serializePrintMessage(object) { | 263 static _serializePrintMessage(object) { |
| 263 return _serializeMessage({"command": "print", "msg": object}); | 264 return _serializeMessage({"command": "print", "msg": object}); |
| 264 } | 265 } |
| 265 | 266 |
| 266 /** | 267 /** |
| 267 * Close the worker running this code if all isolates are done and | 268 * Close the worker running this code if all isolates are done and |
| 268 * there are no active async JavaScript tasks still running. | 269 * there are no active async JavaScript tasks still running. |
| 269 */ | 270 */ |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 /** Function called with an uncaught error. */ | 423 /** Function called with an uncaught error. */ |
| 423 void handleUncaughtError(error, StackTrace stackTrace) { | 424 void handleUncaughtError(error, StackTrace stackTrace) { |
| 424 // Just print the error if there is no error listener registered. | 425 // Just print the error if there is no error listener registered. |
| 425 if (errorPorts.isEmpty) { | 426 if (errorPorts.isEmpty) { |
| 426 // An uncaught error in the root isolate will terminate the program? | 427 // An uncaught error in the root isolate will terminate the program? |
| 427 if (errorsAreFatal && identical(this, _globalState.rootContext)) { | 428 if (errorsAreFatal && identical(this, _globalState.rootContext)) { |
| 428 // The error will be rethrown to reach the global scope, so | 429 // The error will be rethrown to reach the global scope, so |
| 429 // don't print it. | 430 // don't print it. |
| 430 return; | 431 return; |
| 431 } | 432 } |
| 432 if (JS('bool', 'self.console && self.console.error')) { | 433 if (JS('bool', '#.console && #.console.error', global, global)) { |
| 433 JS('void', 'self.console.error(#, #)', error, stackTrace); | 434 JS('void', '#.console.error(#, #)', global, error, stackTrace); |
| 434 } else { | 435 } else { |
| 435 print(error); | 436 print(error); |
| 436 if (stackTrace != null) print(stackTrace); | 437 if (stackTrace != null) print(stackTrace); |
| 437 } | 438 } |
| 438 return; | 439 return; |
| 439 } | 440 } |
| 440 List message = new List(2) | 441 List message = new List(2) |
| 441 ..[0] = error.toString() | 442 ..[0] = error.toString() |
| 442 ..[1] = (stackTrace == null) ? null : stackTrace.toString(); | 443 ..[1] = (stackTrace == null) ? null : stackTrace.toString(); |
| 443 for (SendPort port in errorPorts) port.send(message); | 444 for (SendPort port in errorPorts) port.send(message); |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 693 } | 694 } |
| 694 } | 695 } |
| 695 | 696 |
| 696 // "self" is a way to refer to the global context object that | 697 // "self" is a way to refer to the global context object that |
| 697 // works in HTML pages and in Web Workers. It does not work in d8, iojs | 698 // works in HTML pages and in Web Workers. It does not work in d8, iojs |
| 698 // and Firefox jsshell, because that would have been too easy. In iojs | 699 // and Firefox jsshell, because that would have been too easy. In iojs |
| 699 // "global" works. | 700 // "global" works. |
| 700 // | 701 // |
| 701 // See: http://www.w3.org/TR/workers/#the-global-scope | 702 // See: http://www.w3.org/TR/workers/#the-global-scope |
| 702 // and: http://www.w3.org/TR/Window/#dfn-self-attribute | 703 // and: http://www.w3.org/TR/Window/#dfn-self-attribute |
| 703 final _global = | 704 final global = |
| 704 JS("", "typeof global == 'undefined' ? self : global"); | 705 JS("", "typeof global == 'undefined' ? self : global"); |
| 705 | 706 |
| 706 /** A stub for interacting with the main manager. */ | 707 /** A stub for interacting with the main manager. */ |
| 707 class _MainManagerStub { | 708 class _MainManagerStub { |
| 708 void postMessage(msg) { | 709 void postMessage(msg) { |
| 709 JS("void", r"#.postMessage(#)", _global, msg); | 710 JS("void", r"#.postMessage(#)", global, msg); |
| 710 } | 711 } |
| 711 } | 712 } |
| 712 | 713 |
| 713 const String _SPAWNED_SIGNAL = "spawned"; | 714 const String _SPAWNED_SIGNAL = "spawned"; |
| 714 const String _SPAWN_FAILED_SIGNAL = "spawn failed"; | 715 const String _SPAWN_FAILED_SIGNAL = "spawn failed"; |
| 715 | 716 |
| 716 get globalWindow { | 717 get globalWindow { |
| 717 return JS('', "#.window", _global); | 718 return JS('', "#.window", global); |
| 718 } | 719 } |
| 719 | 720 |
| 720 get globalWorker { | 721 get globalWorker { |
| 721 return JS('', "#.Worker", _global); | 722 return JS('', "#.Worker", global); |
| 722 } | 723 } |
| 723 bool get globalPostMessageDefined { | 724 bool get globalPostMessageDefined { |
| 724 return JS('bool', "!!#.postMessage", _global); | 725 return JS('bool', "!!#.postMessage", global); |
| 725 } | 726 } |
| 726 | 727 |
| 727 typedef _MainFunction(); | 728 typedef _MainFunction(); |
| 728 typedef _MainFunctionArgs(args); | 729 typedef _MainFunctionArgs(args); |
| 729 typedef _MainFunctionArgsMessage(args, message); | 730 typedef _MainFunctionArgsMessage(args, message); |
| 730 | 731 |
| 731 /// Note: IsolateNatives depends on _globalState which is only set up correctly | 732 /// Note: IsolateNatives depends on _globalState which is only set up correctly |
| 732 /// when 'dart:isolate' has been imported. | 733 /// when 'dart:isolate' has been imported. |
| 733 class IsolateNatives { | 734 class IsolateNatives { |
| 734 | 735 |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 899 } else { | 900 } else { |
| 900 try { | 901 try { |
| 901 _consoleLog(msg); | 902 _consoleLog(msg); |
| 902 } catch (e, trace) { | 903 } catch (e, trace) { |
| 903 throw new Exception(trace); | 904 throw new Exception(trace); |
| 904 } | 905 } |
| 905 } | 906 } |
| 906 } | 907 } |
| 907 | 908 |
| 908 static void _consoleLog(msg) { | 909 static void _consoleLog(msg) { |
| 909 JS("void", r"self.console.log(#)", msg); | 910 JS("void", r"#.console.log(#)", global, msg); |
| 910 } | 911 } |
| 911 | 912 |
| 912 static _getJSFunctionFromName(String functionName) { | 913 static _getJSFunctionFromName(String functionName) { |
| 913 var globalFunctionsContainer = JS_EMBEDDED_GLOBAL("", GLOBAL_FUNCTIONS); | 914 var globalFunctionsContainer = JS_EMBEDDED_GLOBAL("", GLOBAL_FUNCTIONS); |
| 914 return JS("", "#[#]()", globalFunctionsContainer, functionName); | 915 return JS("", "#[#]()", globalFunctionsContainer, functionName); |
| 915 } | 916 } |
| 916 | 917 |
| 917 /** | 918 /** |
| 918 * Get a string name for the function, if possible. The result for | 919 * Get a string name for the function, if possible. The result for |
| 919 * anonymous functions is browser-dependent -- it may be "" or "anonymous" | 920 * anonymous functions is browser-dependent -- it may be "" or "anonymous" |
| (...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1365 | 1366 |
| 1366 void internalCallback() { | 1367 void internalCallback() { |
| 1367 _handle = null; | 1368 _handle = null; |
| 1368 leaveJsAsync(); | 1369 leaveJsAsync(); |
| 1369 callback(); | 1370 callback(); |
| 1370 } | 1371 } |
| 1371 | 1372 |
| 1372 enterJsAsync(); | 1373 enterJsAsync(); |
| 1373 | 1374 |
| 1374 _handle = JS( | 1375 _handle = JS( |
| 1375 'int', 'self.setTimeout(#, #)', internalCallback, milliseconds); | 1376 'int', '#.setTimeout(#, #)', global, internalCallback, milliseconds); |
| 1376 } else { | 1377 } else { |
| 1377 assert(milliseconds > 0); | 1378 assert(milliseconds > 0); |
| 1378 throw new UnsupportedError("Timer greater than 0."); | 1379 throw new UnsupportedError("Timer greater than 0."); |
| 1379 } | 1380 } |
| 1380 } | 1381 } |
| 1381 | 1382 |
| 1382 TimerImpl.periodic(int milliseconds, void callback(Timer timer)) | 1383 TimerImpl.periodic(int milliseconds, void callback(Timer timer)) |
| 1383 : _once = false { | 1384 : _once = false { |
| 1384 if (hasTimer()) { | 1385 if (hasTimer()) { |
| 1385 enterJsAsync(); | 1386 enterJsAsync(); |
| 1386 _handle = JS('int', 'self.setInterval(#, #)', | 1387 _handle = JS('int', '#.setInterval(#, #)', |
| 1387 () { callback(this); }, milliseconds); | 1388 global, () { callback(this); }, milliseconds); |
| 1388 } else { | 1389 } else { |
| 1389 throw new UnsupportedError("Periodic timer."); | 1390 throw new UnsupportedError("Periodic timer."); |
| 1390 } | 1391 } |
| 1391 } | 1392 } |
| 1392 | 1393 |
| 1393 void cancel() { | 1394 void cancel() { |
| 1394 if (hasTimer()) { | 1395 if (hasTimer()) { |
| 1395 if (_inEventLoop) { | 1396 if (_inEventLoop) { |
| 1396 throw new UnsupportedError("Timer in event loop cannot be canceled."); | 1397 throw new UnsupportedError("Timer in event loop cannot be canceled."); |
| 1397 } | 1398 } |
| 1398 if (_handle == null) return; | 1399 if (_handle == null) return; |
| 1399 leaveJsAsync(); | 1400 leaveJsAsync(); |
| 1400 if (_once) { | 1401 if (_once) { |
| 1401 JS('void', 'self.clearTimeout(#)', _handle); | 1402 JS('void', '#.clearTimeout(#)', global, _handle); |
| 1402 } else { | 1403 } else { |
| 1403 JS('void', 'self.clearInterval(#)', _handle); | 1404 JS('void', '#.clearInterval(#)', global, _handle); |
| 1404 } | 1405 } |
| 1405 _handle = null; | 1406 _handle = null; |
| 1406 } else { | 1407 } else { |
| 1407 throw new UnsupportedError("Canceling a timer."); | 1408 throw new UnsupportedError("Canceling a timer."); |
| 1408 } | 1409 } |
| 1409 } | 1410 } |
| 1410 | 1411 |
| 1411 bool get isActive => _handle != null; | 1412 bool get isActive => _handle != null; |
| 1412 } | 1413 } |
| 1413 | 1414 |
| 1414 bool hasTimer() { | 1415 bool hasTimer() { |
| 1415 return JS('', 'self.setTimeout') != null; | 1416 return JS('', '#.setTimeout', global) != null; |
| 1416 } | 1417 } |
| 1417 | 1418 |
| 1418 | 1419 |
| 1419 /** | 1420 /** |
| 1420 * Implementation class for [Capability]. | 1421 * Implementation class for [Capability]. |
| 1421 * | 1422 * |
| 1422 * It has the same name to make it harder for users to distinguish. | 1423 * It has the same name to make it harder for users to distinguish. |
| 1423 */ | 1424 */ |
| 1424 class CapabilityImpl implements Capability { | 1425 class CapabilityImpl implements Capability { |
| 1425 /** Internal random secret identifying the capability. */ | 1426 /** Internal random secret identifying the capability. */ |
| (...skipping 19 matching lines...) Expand all Loading... |
| 1445 } | 1446 } |
| 1446 | 1447 |
| 1447 bool operator==(Object other) { | 1448 bool operator==(Object other) { |
| 1448 if (identical(other, this)) return true; | 1449 if (identical(other, this)) return true; |
| 1449 if (other is CapabilityImpl) { | 1450 if (other is CapabilityImpl) { |
| 1450 return identical(_id, other._id); | 1451 return identical(_id, other._id); |
| 1451 } | 1452 } |
| 1452 return false; | 1453 return false; |
| 1453 } | 1454 } |
| 1454 } | 1455 } |
| OLD | NEW |