| 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 /** | 5 /** |
| 6 * Concepts used here: | 6 * Concepts used here: |
| 7 * | 7 * |
| 8 * "manager" - A manager contains one or more isolates, schedules their | 8 * "manager" - A manager contains one or more isolates, schedules their |
| 9 * execution, and performs other plumbing on their behalf. The isolate | 9 * execution, and performs other plumbing on their behalf. The isolate |
| 10 * present at the creation of the manager is designated as its "root isolate". | 10 * present at the creation of the manager is designated as its "root isolate". |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 topEventLoop = new _EventLoop(); | 118 topEventLoop = new _EventLoop(); |
| 119 isolates = new Map<int, _IsolateContext>(); | 119 isolates = new Map<int, _IsolateContext>(); |
| 120 managers = new Map<int, _ManagerStub>(); | 120 managers = new Map<int, _ManagerStub>(); |
| 121 if (isWorker) { // "if we are not the main manager ourself" is the intent. | 121 if (isWorker) { // "if we are not the main manager ourself" is the intent. |
| 122 mainManager = new _MainManagerStub(); | 122 mainManager = new _MainManagerStub(); |
| 123 _nativeInitWorkerMessageHandler(); | 123 _nativeInitWorkerMessageHandler(); |
| 124 } | 124 } |
| 125 } | 125 } |
| 126 | 126 |
| 127 void _nativeDetectEnvironment() native @""" | 127 void _nativeDetectEnvironment() native @""" |
| 128 this.isWorker = typeof ($globalThis['importScripts']) != 'undefined'; | 128 this.isWorker = $isWorker; |
| 129 this.supportsWorkers = $supportsWorkers; |
| 129 this.fromCommandLine = typeof(window) == 'undefined'; | 130 this.fromCommandLine = typeof(window) == 'undefined'; |
| 130 this.supportsWorkers = this.isWorker || | |
| 131 ((typeof $globalThis['Worker']) != 'undefined'); | |
| 132 """; | 131 """; |
| 133 | 132 |
| 134 void _nativeInitWorkerMessageHandler() native @""" | 133 void _nativeInitWorkerMessageHandler() native @""" |
| 135 $globalThis.onmessage = function (e) { | 134 $globalThis.onmessage = function (e) { |
| 136 _IsolateNatives._processWorkerMessage(this.mainManager, e); | 135 _IsolateNatives._processWorkerMessage(this.mainManager, e); |
| 137 } | 136 } |
| 138 """ { | 137 """ { |
| 139 _IsolateNatives._processWorkerMessage(null, null); | 138 _IsolateNatives._processWorkerMessage(null, null); |
| 140 } | 139 } |
| 141 | 140 |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 'replyPort': _serializeMessage(replyPort)})); | 378 'replyPort': _serializeMessage(replyPort)})); |
| 380 } else { | 379 } else { |
| 381 _spawnWorker(factoryName, _serializeMessage(replyPort)); | 380 _spawnWorker(factoryName, _serializeMessage(replyPort)); |
| 382 } | 381 } |
| 383 } | 382 } |
| 384 | 383 |
| 385 /** | 384 /** |
| 386 * The src url for the script tag that loaded this code. Used to create | 385 * The src url for the script tag that loaded this code. Used to create |
| 387 * JavaScript workers. | 386 * JavaScript workers. |
| 388 */ | 387 */ |
| 389 static String get _thisScript() { | 388 static String get _thisScript() native @"return $thisScriptUrl"; |
| 390 if (_thisScriptCache == null) { | |
| 391 _thisScriptCache = _computeThisScript(); | |
| 392 } | |
| 393 return _thisScriptCache; | |
| 394 } | |
| 395 | |
| 396 static String _thisScriptCache; | |
| 397 | |
| 398 // TODO(sigmund): fix - this code should be run synchronously when loading the | |
| 399 // script. Running lazily on DOMContentLoaded will yield incorrect results. | |
| 400 static String _computeThisScript() native @""" | |
| 401 if (!$globalState.supportsWorkers || $globalState.isWorker) return (void 0); | |
| 402 | |
| 403 // TODO(5334778): Find a cross-platform non-brittle way of getting the | |
| 404 // currently running script. | |
| 405 var scripts = document.getElementsByTagName('script'); | |
| 406 // The scripts variable only contains the scripts that have already been | |
| 407 // executed. The last one is the currently running script. | |
| 408 var script = scripts[scripts.length - 1]; | |
| 409 var src = script && script.src; | |
| 410 if (!src) { | |
| 411 // TODO() | |
| 412 src = "FIXME:5407062" + "_" + Math.random().toString(); | |
| 413 if (script) script.src = src; | |
| 414 } | |
| 415 return src; | |
| 416 """; | |
| 417 | 389 |
| 418 /** Starts a new worker with the given URL. */ | 390 /** Starts a new worker with the given URL. */ |
| 419 static _WorkerStub _newWorker(url) native "return new Worker(url);"; | 391 static _WorkerStub _newWorker(url) native "return new Worker(url);"; |
| 420 | 392 |
| 421 /** | 393 /** |
| 422 * Spawns an isolate in a worker. [factoryName] is the Javascript constructor | 394 * Spawns an isolate in a worker. [factoryName] is the Javascript constructor |
| 423 * name for the isolate entry point class. | 395 * name for the isolate entry point class. |
| 424 */ | 396 */ |
| 425 static void _spawnWorker(factoryName, serializedReplyPort) { | 397 static void _spawnWorker(factoryName, serializedReplyPort) { |
| 426 final worker = _newWorker(_thisScript); | 398 final worker = _newWorker(_thisScript); |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 675 'command': 'start2', | 647 'command': 'start2', |
| 676 'id': workerId, | 648 'id': workerId, |
| 677 // Note: we serialize replyPort twice because the child worker needs to | 649 // Note: we serialize replyPort twice because the child worker needs to |
| 678 // first deserialize the worker id, before it can correctly deserialize | 650 // first deserialize the worker id, before it can correctly deserialize |
| 679 // the port (port deserialization is sensitive to what is the current | 651 // the port (port deserialization is sensitive to what is the current |
| 680 // workerId). | 652 // workerId). |
| 681 'replyTo': _serializeMessage(replyPort), | 653 'replyTo': _serializeMessage(replyPort), |
| 682 'functionName': functionName })); | 654 'functionName': functionName })); |
| 683 } | 655 } |
| 684 } | 656 } |
| OLD | NEW |