Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 * A native object that is shared across isolates. This object is visible to all | 6 * A native object that is shared across isolates. This object is visible to all |
| 7 * isolates running on the same worker (either UI or background web worker). | 7 * isolates running on the same worker (either UI or background web worker). |
| 8 * | 8 * |
| 9 * This is code that is intended to 'escape' the isolate boundaries in order to | 9 * This is code that is intended to 'escape' the isolate boundaries in order to |
| 10 * implement the semantics of friendly isolates in JavaScript. Without this we | 10 * implement the semantics of friendly isolates in JavaScript. Without this we |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 159 } else { | 159 } else { |
| 160 // Nothing more to do. | 160 // Nothing more to do. |
| 161 return message; | 161 return message; |
| 162 } | 162 } |
| 163 } | 163 } |
| 164 | 164 |
| 165 /** Default worker. */ | 165 /** Default worker. */ |
| 166 class MainWorker { | 166 class MainWorker { |
| 167 int id = 0; | 167 int id = 0; |
| 168 void postMessage(msg) native "return \$globalThis.postMessage(msg);"; | 168 void postMessage(msg) native "return \$globalThis.postMessage(msg);"; |
| 169 void onmessage(f) native "\$globalThis.onmessage = f;"; | 169 void set onmessage(f) native "\$globalThis.onmessage = f;"; |
| 170 void terminate() {} | 170 void terminate() {} |
| 171 } | 171 } |
| 172 | 172 |
| 173 class _Worker native "*Worker" { | |
|
Jennifer Messerly
2011/12/22 19:49:44
I'd add a comment here that this type is defined i
| |
| 174 get id() native "return this.id;"; | |
| 175 void set id(i) native "this.id = i;"; | |
| 176 void set onmessage(f) native "this.onmessage = f;"; | |
| 177 void postMessage(msg) native "return this.postMessage(msg);"; | |
| 178 } | |
| 179 | |
| 173 /** Context information tracked for each isolate. */ | 180 /** Context information tracked for each isolate. */ |
| 174 class IsolateContext { | 181 class IsolateContext { |
| 175 /** Current isolate id. */ | 182 /** Current isolate id. */ |
| 176 int id; | 183 int id; |
| 177 | 184 |
| 178 /** Registry of receive ports currently active on this isolate. */ | 185 /** Registry of receive ports currently active on this isolate. */ |
| 179 Map<int, ReceivePort> ports; | 186 Map<int, ReceivePort> ports; |
| 180 | 187 |
| 181 /** Holds isolate globals (statics and top-level properties). */ | 188 /** Holds isolate globals (statics and top-level properties). */ |
| 182 var isolateStatics; // native object containing all globals of an isolate. | 189 var isolateStatics; // native object containing all globals of an isolate. |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 196 */ | 203 */ |
| 197 void eval(Function code) { | 204 void eval(Function code) { |
| 198 var old = _globalState.currentContext; | 205 var old = _globalState.currentContext; |
| 199 _globalState.currentContext = this; | 206 _globalState.currentContext = this; |
| 200 this._setGlobals(); | 207 this._setGlobals(); |
| 201 var result = null; | 208 var result = null; |
| 202 try { | 209 try { |
| 203 result = code(); | 210 result = code(); |
| 204 } finally { | 211 } finally { |
| 205 _globalState.currentContext = old; | 212 _globalState.currentContext = old; |
| 206 old._setGlobals(); | 213 if (old != null) old._setGlobals(); |
| 207 } | 214 } |
| 208 return result; | 215 return result; |
| 209 } | 216 } |
| 210 | 217 |
| 211 void _setGlobals() native @'$globals = this.isolateStatics;'; | 218 void _setGlobals() native @'$globals = this.isolateStatics;'; |
| 212 | 219 |
| 213 /** Lookup a port registered for this isolate. */ | 220 /** Lookup a port registered for this isolate. */ |
| 214 ReceivePort lookup(int id) => ports[id]; | 221 ReceivePort lookup(int id) => ports[id]; |
| 215 | 222 |
| 216 /** Register a port on this isolate. */ | 223 /** Register a port on this isolate. */ |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 285 /** | 292 /** |
| 286 * Call [_runHelper] but ensure that worker exceptions are propragated. Note | 293 * Call [_runHelper] but ensure that worker exceptions are propragated. Note |
| 287 * this is called from JavaScript (see $wrap_call in corejs.dart). | 294 * this is called from JavaScript (see $wrap_call in corejs.dart). |
| 288 */ | 295 */ |
| 289 void run() { | 296 void run() { |
| 290 if (!_globalState.isWorker) { | 297 if (!_globalState.isWorker) { |
| 291 _runHelper(); | 298 _runHelper(); |
| 292 } else { | 299 } else { |
| 293 try { | 300 try { |
| 294 _runHelper(); | 301 _runHelper(); |
| 295 } catch(e) { | 302 } catch(var e, var trace) { |
|
Jennifer Messerly
2011/12/22 19:49:44
Is trace actually working? if so ... that's cool :
| |
| 296 // TODO(floitsch): try to send stack-trace to the other side. | |
| 297 _globalState.mainWorker.postMessage(_serializeMessage( | 303 _globalState.mainWorker.postMessage(_serializeMessage( |
| 298 {'command': 'error', 'msg': "" + e })); | 304 {'command': 'error', 'msg': '$e\n$trace' })); |
| 299 } | 305 } |
| 300 } | 306 } |
| 301 } | 307 } |
| 302 } | 308 } |
| 303 | 309 |
| 304 /** An event in the top-level event queue. */ | 310 /** An event in the top-level event queue. */ |
| 305 class IsolateEvent { | 311 class IsolateEvent { |
| 306 IsolateContext isolate; | 312 IsolateContext isolate; |
| 307 Function fn; | 313 Function fn; |
| 308 String message; | 314 String message; |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 483 if (!src) { | 489 if (!src) { |
| 484 // TODO() | 490 // TODO() |
| 485 src = "FIXME:5407062" + "_" + Math.random().toString(); | 491 src = "FIXME:5407062" + "_" + Math.random().toString(); |
| 486 if (script) script.src = src; | 492 if (script) script.src = src; |
| 487 } | 493 } |
| 488 IsolateNatives._thisScriptCache = src; | 494 IsolateNatives._thisScriptCache = src; |
| 489 return src; | 495 return src; |
| 490 """; | 496 """; |
| 491 | 497 |
| 492 /** Starts a new worker with the given URL. */ | 498 /** Starts a new worker with the given URL. */ |
| 493 static _newWorker(url) native "return new Worker(url)"; | 499 static _Worker _newWorker(url) native "return new Worker(url);"; |
|
Jennifer Messerly
2011/12/22 19:49:44
we aren't very consistent about it, but personally
| |
| 494 | 500 |
| 495 /** | 501 /** |
| 496 * Spawns an isolate in a worker. [factoryName] is the Javascript constructor | 502 * Spawns an isolate in a worker. [factoryName] is the Javascript constructor |
| 497 * name for the isolate entry point class. | 503 * name for the isolate entry point class. |
| 498 */ | 504 */ |
| 499 static void _spawnWorker(factoryName, serializedReplyPort) { | 505 static void _spawnWorker(factoryName, serializedReplyPort) { |
| 500 var worker = _newWorker(_thisScript); | 506 final worker = _newWorker(_thisScript); |
|
Jennifer Messerly
2011/12/22 19:49:44
Are you using "final" to avoid dynamic dispatch? I
| |
| 501 // TODO(sigmund): make this work. | 507 worker.onmessage = (e) { _processWorkerMessage(worker, e); }; |
| 502 worker.onmessage = function(e) { | |
| 503 _processWorkerMessage(worker, e); | |
| 504 }; | |
| 505 var workerId = _globalState.nextWorkerId++; | 508 var workerId = _globalState.nextWorkerId++; |
| 506 // We also store the id on the worker itself so that we can unregister it. | 509 // We also store the id on the worker itself so that we can unregister it. |
| 507 worker.id = workerId; | 510 worker.id = workerId; |
| 508 _globalState.workers[workerId] = worker; | 511 _globalState.workers[workerId] = worker; |
| 509 worker.postMessage(_serializeMessage({ | 512 worker.postMessage(_serializeMessage({ |
| 510 'command': 'start', | 513 'command': 'start', |
| 511 'id': workerId, | 514 'id': workerId, |
| 512 'replyTo': serializedReplyPort, | 515 'replyTo': serializedReplyPort, |
| 513 'factoryName': factoryName })); | 516 'factoryName': factoryName })); |
| 514 } | 517 } |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 656 worker.postMessage(_serializeMessage({ | 659 worker.postMessage(_serializeMessage({ |
| 657 'command': 'message', | 660 'command': 'message', |
| 658 'workerId': workerId, | 661 'workerId': workerId, |
| 659 'isolateId': isolateId, | 662 'isolateId': isolateId, |
| 660 'portId': receivePortId, | 663 'portId': receivePortId, |
| 661 'msg': message, | 664 'msg': message, |
| 662 'replyTo': replyTo })); | 665 'replyTo': replyTo })); |
| 663 } | 666 } |
| 664 } | 667 } |
| 665 } | 668 } |
| OLD | NEW |