| 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 /** Implementation of [Isolate2]. */ | 5 /** Implementation of [Isolate2]. */ |
| 6 class _Isolate2Impl implements Isolate2 { | 6 class _Isolate2Impl implements Isolate2 { |
| 7 SendPort sendPort; | 7 SendPort sendPort; |
| 8 | 8 |
| 9 _Isolate2Impl(this.sendPort); | 9 _Isolate2Impl(this.sendPort); |
| 10 } | 10 } |
| (...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 'replyPort': _serializeMessage(replyPort)})); | 329 'replyPort': _serializeMessage(replyPort)})); |
| 330 } else { | 330 } else { |
| 331 _spawnWorker(factoryName, _serializeMessage(replyPort)); | 331 _spawnWorker(factoryName, _serializeMessage(replyPort)); |
| 332 } | 332 } |
| 333 } | 333 } |
| 334 | 334 |
| 335 /** | 335 /** |
| 336 * The src url for the script tag that loaded this code. Used to create | 336 * The src url for the script tag that loaded this code. Used to create |
| 337 * JavaScript workers. | 337 * JavaScript workers. |
| 338 */ | 338 */ |
| 339 static String get _thisScript() => | 339 static String get _thisScript() { |
| 340 _thisScriptCache != null ? _thisScriptCache : _computeThisScript(); | 340 if (_thisScriptCache == null) { |
| 341 _thisScriptCache = _computeThisScript(); |
| 342 } |
| 343 return _thisScriptCache; |
| 344 } |
| 341 | 345 |
| 342 static String _thisScriptCache; | 346 static String _thisScriptCache; |
| 343 | 347 |
| 344 // TODO(sigmund): fix - this code should be run synchronously when loading the | 348 // TODO(sigmund): fix - this code should be run synchronously when loading the |
| 345 // script. Running lazily on DOMContentLoaded will yield incorrect results. | 349 // script. Running lazily on DOMContentLoaded will yield incorrect results. |
| 346 static String _computeThisScript() native @""" | 350 static String _computeThisScript() native @""" |
| 347 if (!$globalState.supportsWorkers || $globalState.isWorker) return null; | 351 if (!$globalState.supportsWorkers || $globalState.isWorker) return null; |
| 348 | 352 |
| 349 // TODO(5334778): Find a cross-platform non-brittle way of getting the | 353 // TODO(5334778): Find a cross-platform non-brittle way of getting the |
| 350 // currently running script. | 354 // currently running script. |
| 351 var scripts = document.getElementsByTagName('script'); | 355 var scripts = document.getElementsByTagName('script'); |
| 352 // The scripts variable only contains the scripts that have already been | 356 // The scripts variable only contains the scripts that have already been |
| 353 // executed. The last one is the currently running script. | 357 // executed. The last one is the currently running script. |
| 354 var script = scripts[scripts.length - 1]; | 358 var script = scripts[scripts.length - 1]; |
| 355 var src = script && script.src; | 359 var src = script && script.src; |
| 356 if (!src) { | 360 if (!src) { |
| 357 // TODO() | 361 // TODO() |
| 358 src = "FIXME:5407062" + "_" + Math.random().toString(); | 362 src = "FIXME:5407062" + "_" + Math.random().toString(); |
| 359 if (script) script.src = src; | 363 if (script) script.src = src; |
| 360 } | 364 } |
| 361 _IsolateNatives._thisScriptCache = src; | |
| 362 return src; | 365 return src; |
| 363 """; | 366 """; |
| 364 | 367 |
| 365 /** Starts a new worker with the given URL. */ | 368 /** Starts a new worker with the given URL. */ |
| 366 static _Worker _newWorker(url) native "return new Worker(url);"; | 369 static _Worker _newWorker(url) native "return new Worker(url);"; |
| 367 | 370 |
| 368 /** | 371 /** |
| 369 * Spawns an isolate in a worker. [factoryName] is the Javascript constructor | 372 * Spawns an isolate in a worker. [factoryName] is the Javascript constructor |
| 370 * name for the isolate entry point class. | 373 * name for the isolate entry point class. |
| 371 */ | 374 */ |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 599 } | 602 } |
| 600 | 603 |
| 601 /** | 604 /** |
| 602 * Spawns an isolate in a worker. [factoryName] is the Javascript constructor | 605 * Spawns an isolate in a worker. [factoryName] is the Javascript constructor |
| 603 * name for the isolate entry point class. | 606 * name for the isolate entry point class. |
| 604 */ | 607 */ |
| 605 static void _spawnWorker2(functionName, uri, replyPort) { | 608 static void _spawnWorker2(functionName, uri, replyPort) { |
| 606 // TODO(eub): convert to 'main' once we switch back to port at top-level. | 609 // TODO(eub): convert to 'main' once we switch back to port at top-level. |
| 607 if (functionName == null) functionName = 'isolateMain'; | 610 if (functionName == null) functionName = 'isolateMain'; |
| 608 if (uri == null) uri = _thisScript; | 611 if (uri == null) uri = _thisScript; |
| 612 if (!(new Uri.fromString(uri).isAbsolute())) { |
| 613 // The constructor of dom workers requires an absolute URL. If we use a |
| 614 // relative path we will get a DOM exception. |
| 615 String prefix = _thisScript.substring(0, _thisScript.lastIndexOf('/')); |
| 616 uri = "$prefix/$uri"; |
| 617 } |
| 609 final worker = _newWorker(uri); | 618 final worker = _newWorker(uri); |
| 610 worker.onmessage = (e) { _processWorkerMessage(worker, e); }; | 619 worker.onmessage = (e) { _processWorkerMessage(worker, e); }; |
| 611 var workerId = globalState.nextWorkerId++; | 620 var workerId = globalState.nextWorkerId++; |
| 612 // We also store the id on the worker itself so that we can unregister it. | 621 // We also store the id on the worker itself so that we can unregister it. |
| 613 worker.id = workerId; | 622 worker.id = workerId; |
| 614 globalState.workers[workerId] = worker; | 623 globalState.workers[workerId] = worker; |
| 615 worker.postMessage(_serializeMessage({ | 624 worker.postMessage(_serializeMessage({ |
| 616 'command': 'start2', | 625 'command': 'start2', |
| 617 'id': workerId, | 626 'id': workerId, |
| 618 // Note: we serialize replyPort twice because the child worker needs to | 627 // Note: we serialize replyPort twice because the child worker needs to |
| 619 // first deserialize the worker id, before it can correctly deserialize | 628 // first deserialize the worker id, before it can correctly deserialize |
| 620 // the port (port deserialization is sensitive to what is the current | 629 // the port (port deserialization is sensitive to what is the current |
| 621 // workerId). | 630 // workerId). |
| 622 'replyTo': _serializeMessage(replyPort), | 631 'replyTo': _serializeMessage(replyPort), |
| 623 'functionName': functionName })); | 632 'functionName': functionName })); |
| 624 } | 633 } |
| 625 } | 634 } |
| OLD | NEW |