| OLD | NEW |
| 1 var _isolate_helper; | 1 var _isolate_helper; |
| 2 (function(exports) { | 2 (function(exports) { |
| 3 'use strict'; | 3 'use strict'; |
| 4 // Function _callInIsolate: (_IsolateContext, Function) → dynamic | |
| 5 function _callInIsolate(isolate, function) { | |
| 6 let result = isolate.eval(function); | |
| 7 exports._globalState.topEventLoop.run(); | |
| 8 return result; | |
| 9 } | |
| 10 let _activeJsAsyncCount = Symbol('_activeJsAsyncCount'); | |
| 11 // Function enterJsAsync: () → dynamic | |
| 12 function enterJsAsync() { | |
| 13 exports._globalState.topEventLoop[_activeJsAsyncCount] = dart.notNull(export
s._globalState.topEventLoop[_activeJsAsyncCount]) + 1; | |
| 14 } | |
| 15 // Function leaveJsAsync: () → dynamic | |
| 16 function leaveJsAsync() { | |
| 17 exports._globalState.topEventLoop[_activeJsAsyncCount] = dart.notNull(export
s._globalState.topEventLoop[_activeJsAsyncCount]) - 1; | |
| 18 dart.assert(dart.notNull(exports._globalState.topEventLoop[_activeJsAsyncCou
nt]) >= 0); | |
| 19 } | |
| 20 // Function isWorker: () → bool | |
| 21 function isWorker() { | |
| 22 return exports._globalState.isWorker; | |
| 23 } | |
| 24 // Function _currentIsolate: () → _IsolateContext | |
| 25 function _currentIsolate() { | |
| 26 return exports._globalState.currentContext; | |
| 27 } | |
| 28 // Function startRootIsolate: (dynamic, dynamic) → void | |
| 29 function startRootIsolate(entry, args) { | |
| 30 args = args; | |
| 31 if (args === null) | |
| 32 args = new List.from([]); | |
| 33 if (!dart.is(args, core.List)) { | |
| 34 throw new core.ArgumentError(`Arguments to main must be a List: ${args}`); | |
| 35 } | |
| 36 exports._globalState = new _Manager(dart.as(entry, core.Function)); | |
| 37 if (exports._globalState.isWorker) | |
| 38 return; | |
| 39 let rootContext = new _IsolateContext(); | |
| 40 exports._globalState.rootContext = rootContext; | |
| 41 exports._globalState.currentContext = rootContext; | |
| 42 if (dart.is(entry, _MainFunctionArgs)) { | |
| 43 rootContext.eval(() => { | |
| 44 dart.dinvokef(entry, args); | |
| 45 }); | |
| 46 } else if (dart.is(entry, _MainFunctionArgsMessage)) { | |
| 47 rootContext.eval(() => { | |
| 48 dart.dinvokef(entry, args, null); | |
| 49 }); | |
| 50 } else { | |
| 51 rootContext.eval(dart.as(entry, core.Function)); | |
| 52 } | |
| 53 exports._globalState.topEventLoop.run(); | |
| 54 } | |
| 55 dart.copyProperties(exports, { | |
| 56 get _globalState() { | |
| 57 return dart.as(init.globalState, _Manager); | |
| 58 }, | |
| 59 set _globalState(val) { | |
| 60 init.globalState = val; | |
| 61 } | |
| 62 }); | |
| 63 let _nativeDetectEnvironment = Symbol('_nativeDetectEnvironment'); | |
| 64 let _nativeInitWorkerMessageHandler = Symbol('_nativeInitWorkerMessageHandler'
); | |
| 65 let _processWorkerMessage = Symbol('_processWorkerMessage'); | |
| 66 let _serializePrintMessage = Symbol('_serializePrintMessage'); | |
| 67 class _Manager extends dart.Object { | |
| 68 get useWorkers() { | |
| 69 return this.supportsWorkers; | |
| 70 } | |
| 71 _Manager(entry) { | |
| 72 this.entry = entry; | |
| 73 this.nextIsolateId = 0; | |
| 74 this.currentManagerId = 0; | |
| 75 this.nextManagerId = 1; | |
| 76 this.currentContext = null; | |
| 77 this.rootContext = null; | |
| 78 this.topEventLoop = null; | |
| 79 this.fromCommandLine = null; | |
| 80 this.isWorker = null; | |
| 81 this.supportsWorkers = null; | |
| 82 this.isolates = null; | |
| 83 this.mainManager = null; | |
| 84 this.managers = null; | |
| 85 this[_nativeDetectEnvironment](); | |
| 86 this.topEventLoop = new _EventLoop(); | |
| 87 this.isolates = new core.Map(); | |
| 88 this.managers = new core.Map(); | |
| 89 if (this.isWorker) { | |
| 90 this.mainManager = new _MainManagerStub(); | |
| 91 this[_nativeInitWorkerMessageHandler](); | |
| 92 } | |
| 93 } | |
| 94 [_nativeDetectEnvironment]() { | |
| 95 let isWindowDefined = exports.globalWindow !== null; | |
| 96 let isWorkerDefined = exports.globalWorker !== null; | |
| 97 this.isWorker = !dart.notNull(isWindowDefined) && dart.notNull(exports.glo
balPostMessageDefined); | |
| 98 this.supportsWorkers = dart.notNull(this.isWorker) || dart.notNull(isWorke
rDefined) && dart.notNull(IsolateNatives.thisScript !== null); | |
| 99 this.fromCommandLine = !dart.notNull(isWindowDefined) && !dart.notNull(thi
s.isWorker); | |
| 100 } | |
| 101 [_nativeInitWorkerMessageHandler]() { | |
| 102 let function = function(f, a) { | |
| 103 return function(e) { | |
| 104 f(a, e); | |
| 105 }; | |
| 106 }(_foreign_helper.DART_CLOSURE_TO_JS(IsolateNatives[_processWorkerMessage]
), this.mainManager); | |
| 107 self.onmessage = function; | |
| 108 self.dartPrint = self.dartPrint || function(serialize) { | |
| 109 return function(object) { | |
| 110 if (self.console && self.console.log) { | |
| 111 self.console.log(object); | |
| 112 } else { | |
| 113 self.postMessage(serialize(object)); | |
| 114 } | |
| 115 }; | |
| 116 }(_foreign_helper.DART_CLOSURE_TO_JS(_serializePrintMessage)); | |
| 117 } | |
| 118 static [_serializePrintMessage](object) { | |
| 119 return _serializeMessage(dart.map({command: "print", msg: object})); | |
| 120 } | |
| 121 maybeCloseWorker() { | |
| 122 if (dart.notNull(this.isWorker) && dart.notNull(this.isolates.isEmpty) &&
this.topEventLoop[_activeJsAsyncCount] === 0) { | |
| 123 this.mainManager.postMessage(_serializeMessage(dart.map({command: 'close
'}))); | |
| 124 } | |
| 125 } | |
| 126 } | |
| 127 let _scheduledControlEvents = Symbol('_scheduledControlEvents'); | |
| 128 let _isExecutingEvent = Symbol('_isExecutingEvent'); | |
| 129 let _id = Symbol('_id'); | |
| 130 let _updateGlobalState = Symbol('_updateGlobalState'); | |
| 131 let _setGlobals = Symbol('_setGlobals'); | |
| 132 let _addRegistration = Symbol('_addRegistration'); | |
| 133 class _IsolateContext extends dart.Object { | |
| 134 _IsolateContext() { | |
| 135 this.id = (($tmp) => exports._globalState.nextIsolateId = dart.notNull($tm
p) + 1, $tmp).bind(this)(exports._globalState.nextIsolateId); | |
| 136 this.ports = new core.Map(); | |
| 137 this.weakPorts = new core.Set(); | |
| 138 this.isolateStatics = _foreign_helper.JS_CREATE_ISOLATE(); | |
| 139 this.controlPort = new RawReceivePortImpl._controlPort(); | |
| 140 this.pauseCapability = new isolate.Capability(); | |
| 141 this.terminateCapability = new isolate.Capability(); | |
| 142 this.delayedEvents = dart.as(new List.from([]), core.List$(_IsolateEvent))
; | |
| 143 this.pauseTokens = dart.as(new core.Set(), core.Set$(isolate.Capability)); | |
| 144 this.errorPorts = dart.as(new core.Set(), core.Set$(isolate.SendPort)); | |
| 145 this.initialized = false; | |
| 146 this.isPaused = false; | |
| 147 this.doneHandlers = null; | |
| 148 this[_scheduledControlEvents] = null; | |
| 149 this[_isExecutingEvent] = false; | |
| 150 this.errorsAreFatal = true; | |
| 151 this.registerWeak(this.controlPort[_id], this.controlPort); | |
| 152 } | |
| 153 addPause(authentification, resume) { | |
| 154 if (!dart.equals(this.pauseCapability, authentification)) | |
| 155 return; | |
| 156 if (dart.notNull(this.pauseTokens.add(resume)) && !dart.notNull(this.isPau
sed)) { | |
| 157 this.isPaused = true; | |
| 158 } | |
| 159 this[_updateGlobalState](); | |
| 160 } | |
| 161 removePause(resume) { | |
| 162 if (!dart.notNull(this.isPaused)) | |
| 163 return; | |
| 164 this.pauseTokens.remove(resume); | |
| 165 if (this.pauseTokens.isEmpty) { | |
| 166 while (this.delayedEvents.isNotEmpty) { | |
| 167 let event = this.delayedEvents.removeLast(); | |
| 168 exports._globalState.topEventLoop.prequeue(event); | |
| 169 } | |
| 170 this.isPaused = false; | |
| 171 } | |
| 172 this[_updateGlobalState](); | |
| 173 } | |
| 174 addDoneListener(responsePort) { | |
| 175 if (this.doneHandlers === null) { | |
| 176 this.doneHandlers = new List.from([]); | |
| 177 } | |
| 178 if (dart.dinvoke(this.doneHandlers, 'contains', responsePort)) | |
| 179 return; | |
| 180 dart.dinvoke(this.doneHandlers, 'add', responsePort); | |
| 181 } | |
| 182 removeDoneListener(responsePort) { | |
| 183 if (this.doneHandlers === null) | |
| 184 return; | |
| 185 dart.dinvoke(this.doneHandlers, 'remove', responsePort); | |
| 186 } | |
| 187 setErrorsFatal(authentification, errorsAreFatal) { | |
| 188 if (!dart.equals(this.terminateCapability, authentification)) | |
| 189 return; | |
| 190 this.errorsAreFatal = errorsAreFatal; | |
| 191 } | |
| 192 handlePing(responsePort, pingType) { | |
| 193 if (pingType === isolate.Isolate.IMMEDIATE || pingType === isolate.Isolate
.BEFORE_NEXT_EVENT && !dart.notNull(this[_isExecutingEvent])) { | |
| 194 responsePort.send(null); | |
| 195 return; | |
| 196 } | |
| 197 // Function respond: () → void | |
| 198 function respond() { | |
| 199 responsePort.send(null); | |
| 200 } | |
| 201 if (pingType === isolate.Isolate.AS_EVENT) { | |
| 202 exports._globalState.topEventLoop.enqueue(this, respond, "ping"); | |
| 203 return; | |
| 204 } | |
| 205 dart.assert(pingType === isolate.Isolate.BEFORE_NEXT_EVENT); | |
| 206 if (this[_scheduledControlEvents] === null) { | |
| 207 this[_scheduledControlEvents] = new collection.Queue(); | |
| 208 } | |
| 209 dart.dinvoke(this[_scheduledControlEvents], 'addLast', respond); | |
| 210 } | |
| 211 handleKill(authentification, priority) { | |
| 212 if (!dart.equals(this.terminateCapability, authentification)) | |
| 213 return; | |
| 214 if (priority === isolate.Isolate.IMMEDIATE || priority === isolate.Isolate
.BEFORE_NEXT_EVENT && !dart.notNull(this[_isExecutingEvent])) { | |
| 215 this.kill(); | |
| 216 return; | |
| 217 } | |
| 218 if (priority === isolate.Isolate.AS_EVENT) { | |
| 219 exports._globalState.topEventLoop.enqueue(this, this.kill, "kill"); | |
| 220 return; | |
| 221 } | |
| 222 dart.assert(priority === isolate.Isolate.BEFORE_NEXT_EVENT); | |
| 223 if (this[_scheduledControlEvents] === null) { | |
| 224 this[_scheduledControlEvents] = new collection.Queue(); | |
| 225 } | |
| 226 dart.dinvoke(this[_scheduledControlEvents], 'addLast', this.kill); | |
| 227 } | |
| 228 addErrorListener(port) { | |
| 229 this.errorPorts.add(port); | |
| 230 } | |
| 231 removeErrorListener(port) { | |
| 232 this.errorPorts.remove(port); | |
| 233 } | |
| 234 handleUncaughtError(error, stackTrace) { | |
| 235 if (this.errorPorts.isEmpty) { | |
| 236 if (dart.notNull(this.errorsAreFatal) && dart.notNull(core.identical(thi
s, exports._globalState.rootContext))) { | |
| 237 return; | |
| 238 } | |
| 239 if (self.console && self.console.error) { | |
| 240 self.console.error(error, stackTrace); | |
| 241 } else { | |
| 242 core.print(error); | |
| 243 if (stackTrace !== null) | |
| 244 core.print(stackTrace); | |
| 245 } | |
| 246 return; | |
| 247 } | |
| 248 let message = new core.List(2); | |
| 249 message.set(0, dart.dinvoke(error, 'toString')); | |
| 250 message.set(1, stackTrace === null ? null : stackTrace.toString()); | |
| 251 for (let port of this.errorPorts) | |
| 252 port.send(message); | |
| 253 } | |
| 254 eval(code) { | |
| 255 let old = exports._globalState.currentContext; | |
| 256 exports._globalState.currentContext = this; | |
| 257 this._setGlobals(); | |
| 258 let result = null; | |
| 259 this[_isExecutingEvent] = true; | |
| 260 try { | |
| 261 result = dart.dinvokef(code); | |
| 262 } catch (e) { | |
| 263 let s = dart.stackTrace(e); | |
| 264 this.handleUncaughtError(e, s); | |
| 265 if (this.errorsAreFatal) { | |
| 266 this.kill(); | |
| 267 if (core.identical(this, exports._globalState.rootContext)) { | |
| 268 throw e; | |
| 269 } | |
| 270 } | |
| 271 } | |
| 272 finally { | |
| 273 this[_isExecutingEvent] = false; | |
| 274 exports._globalState.currentContext = old; | |
| 275 if (old !== null) | |
| 276 old._setGlobals(); | |
| 277 if (this[_scheduledControlEvents] !== null) { | |
| 278 while (dart.dload(this[_scheduledControlEvents], 'isNotEmpty')) { | |
| 279 dart.dinvokef(dart.dinvoke(this[_scheduledControlEvents], 'removeFir
st')); | |
| 280 } | |
| 281 } | |
| 282 } | |
| 283 return result; | |
| 284 } | |
| 285 [_setGlobals]() { | |
| 286 _foreign_helper.JS_SET_CURRENT_ISOLATE(this.isolateStatics); | |
| 287 } | |
| 288 handleControlMessage(message) { | |
| 289 switch (dart.dindex(message, 0)) { | |
| 290 case "pause": | |
| 291 this.addPause(dart.as(dart.dindex(message, 1), isolate.Capability), da
rt.as(dart.dindex(message, 2), isolate.Capability)); | |
| 292 break; | |
| 293 case "resume": | |
| 294 this.removePause(dart.as(dart.dindex(message, 1), isolate.Capability))
; | |
| 295 break; | |
| 296 case 'add-ondone': | |
| 297 this.addDoneListener(dart.as(dart.dindex(message, 1), isolate.SendPort
)); | |
| 298 break; | |
| 299 case 'remove-ondone': | |
| 300 this.removeDoneListener(dart.as(dart.dindex(message, 1), isolate.SendP
ort)); | |
| 301 break; | |
| 302 case 'set-errors-fatal': | |
| 303 this.setErrorsFatal(dart.as(dart.dindex(message, 1), isolate.Capabilit
y), dart.as(dart.dindex(message, 2), core.bool)); | |
| 304 break; | |
| 305 case "ping": | |
| 306 this.handlePing(dart.as(dart.dindex(message, 1), isolate.SendPort), da
rt.as(dart.dindex(message, 2), core.int)); | |
| 307 break; | |
| 308 case "kill": | |
| 309 this.handleKill(dart.as(dart.dindex(message, 1), isolate.Capability),
dart.as(dart.dindex(message, 2), core.int)); | |
| 310 break; | |
| 311 case "getErrors": | |
| 312 this.addErrorListener(dart.as(dart.dindex(message, 1), isolate.SendPor
t)); | |
| 313 break; | |
| 314 case "stopErrors": | |
| 315 this.removeErrorListener(dart.as(dart.dindex(message, 1), isolate.Send
Port)); | |
| 316 break; | |
| 317 default: | |
| 318 } | |
| 319 } | |
| 320 lookup(portId) { | |
| 321 return this.ports.get(portId); | |
| 322 } | |
| 323 [_addRegistration](portId, port) { | |
| 324 if (this.ports.containsKey(portId)) { | |
| 325 throw new core.Exception("Registry: ports must be registered only once."
); | |
| 326 } | |
| 327 this.ports.set(portId, port); | |
| 328 } | |
| 329 register(portId, port) { | |
| 330 this[_addRegistration](portId, port); | |
| 331 this[_updateGlobalState](); | |
| 332 } | |
| 333 registerWeak(portId, port) { | |
| 334 this.weakPorts.add(portId); | |
| 335 this[_addRegistration](portId, port); | |
| 336 } | |
| 337 [_updateGlobalState]() { | |
| 338 if (dart.notNull(this.ports.length) - dart.notNull(this.weakPorts.length)
> 0 || dart.notNull(this.isPaused) || !dart.notNull(this.initialized)) { | |
| 339 exports._globalState.isolates.set(this.id, this); | |
| 340 } else { | |
| 341 this.kill(); | |
| 342 } | |
| 343 } | |
| 344 kill() { | |
| 345 if (this[_scheduledControlEvents] !== null) { | |
| 346 dart.dinvoke(this[_scheduledControlEvents], 'clear'); | |
| 347 } | |
| 348 for (let port of this.ports.values) { | |
| 349 dart.dinvoke(port, '_close'); | |
| 350 } | |
| 351 this.ports.clear(); | |
| 352 this.weakPorts.clear(); | |
| 353 exports._globalState.isolates.remove(this.id); | |
| 354 this.errorPorts.clear(); | |
| 355 if (this.doneHandlers !== null) { | |
| 356 for (let port of this.doneHandlers) { | |
| 357 port.send(null); | |
| 358 } | |
| 359 this.doneHandlers = null; | |
| 360 } | |
| 361 } | |
| 362 unregister(portId) { | |
| 363 this.ports.remove(portId); | |
| 364 this.weakPorts.remove(portId); | |
| 365 this[_updateGlobalState](); | |
| 366 } | |
| 367 } | |
| 368 let _runHelper = Symbol('_runHelper'); | |
| 369 class _EventLoop extends dart.Object { | |
| 370 _EventLoop() { | |
| 371 this.events = new collection.Queue(); | |
| 372 this[_activeJsAsyncCount] = 0; | |
| 373 } | |
| 374 enqueue(isolate, fn, msg) { | |
| 375 this.events.addLast(new _IsolateEvent(dart.as(isolate, _IsolateContext), d
art.as(fn, core.Function), dart.as(msg, core.String))); | |
| 376 } | |
| 377 prequeue(event) { | |
| 378 this.events.addFirst(event); | |
| 379 } | |
| 380 dequeue() { | |
| 381 if (this.events.isEmpty) | |
| 382 return null; | |
| 383 return this.events.removeFirst(); | |
| 384 } | |
| 385 checkOpenReceivePortsFromCommandLine() { | |
| 386 if (dart.notNull(exports._globalState.rootContext !== null) && dart.notNul
l(exports._globalState.isolates.containsKey(exports._globalState.rootContext.id)
) && dart.notNull(exports._globalState.fromCommandLine) && dart.notNull(exports.
_globalState.rootContext.ports.isEmpty)) { | |
| 387 throw new core.Exception("Program exited with open ReceivePorts."); | |
| 388 } | |
| 389 } | |
| 390 runIteration() { | |
| 391 let event = this.dequeue(); | |
| 392 if (event === null) { | |
| 393 this.checkOpenReceivePortsFromCommandLine(); | |
| 394 exports._globalState.maybeCloseWorker(); | |
| 395 return false; | |
| 396 } | |
| 397 event.process(); | |
| 398 return true; | |
| 399 } | |
| 400 [_runHelper]() { | |
| 401 if (exports.globalWindow !== null) { | |
| 402 // Function next: () → void | |
| 403 function next() { | |
| 404 if (!dart.notNull(this.runIteration())) | |
| 405 return; | |
| 406 async.Timer.run(next); | |
| 407 } | |
| 408 next(); | |
| 409 } else { | |
| 410 while (this.runIteration()) { | |
| 411 } | |
| 412 } | |
| 413 } | |
| 414 run() { | |
| 415 if (!dart.notNull(exports._globalState.isWorker)) { | |
| 416 this[_runHelper](); | |
| 417 } else { | |
| 418 try { | |
| 419 this[_runHelper](); | |
| 420 } catch (e) { | |
| 421 let trace = dart.stackTrace(e); | |
| 422 exports._globalState.mainManager.postMessage(_serializeMessage(dart.ma
p({command: 'error', msg: `${e}\n${trace}`}))); | |
| 423 } | |
| 424 | |
| 425 } | |
| 426 } | |
| 427 } | |
| 428 class _IsolateEvent extends dart.Object { | |
| 429 _IsolateEvent(isolate, fn, message) { | |
| 430 this.isolate = isolate; | |
| 431 this.fn = fn; | |
| 432 this.message = message; | |
| 433 } | |
| 434 process() { | |
| 435 if (this.isolate.isPaused) { | |
| 436 this.isolate.delayedEvents.add(this); | |
| 437 return; | |
| 438 } | |
| 439 this.isolate.eval(this.fn); | |
| 440 } | |
| 441 } | |
| 442 class _MainManagerStub extends dart.Object { | |
| 443 postMessage(msg) { | |
| 444 _js_helper.requiresPreamble(); | |
| 445 self.postMessage(msg); | |
| 446 } | |
| 447 } | |
| 448 let _SPAWNED_SIGNAL = "spawned"; | |
| 449 let _SPAWN_FAILED_SIGNAL = "spawn failed"; | |
| 450 dart.copyProperties(exports, { | |
| 451 get globalWindow() { | |
| 452 _js_helper.requiresPreamble(); | |
| 453 return self.window; | |
| 454 }, | |
| 455 get globalWorker() { | |
| 456 _js_helper.requiresPreamble(); | |
| 457 return self.Worker; | |
| 458 }, | |
| 459 get globalPostMessageDefined() { | |
| 460 _js_helper.requiresPreamble(); | |
| 461 return !!self.postMessage; | |
| 462 } | |
| 463 }); | |
| 464 let _getEventData = Symbol('_getEventData'); | |
| 465 let _log = Symbol('_log'); | |
| 466 let _consoleLog = Symbol('_consoleLog'); | |
| 467 let _getJSFunctionFromName = Symbol('_getJSFunctionFromName'); | |
| 468 let _getJSFunctionName = Symbol('_getJSFunctionName'); | |
| 469 let _allocate = Symbol('_allocate'); | |
| 470 let _startWorker = Symbol('_startWorker'); | |
| 471 let _startNonWorker = Symbol('_startNonWorker'); | |
| 472 let _startIsolate = Symbol('_startIsolate'); | |
| 473 let _spawnWorker = Symbol('_spawnWorker'); | |
| 474 class IsolateNatives extends dart.Object { | |
| 475 static computeThisScript() { | |
| 476 let currentScript = _foreign_helper.JS_EMBEDDED_GLOBAL('', dart.as(_js_emb
edded_names.CURRENT_SCRIPT, core.String)); | |
| 477 if (currentScript !== null) { | |
| 478 return String(currentScript.src); | |
| 479 } | |
| 480 if (_js_helper.Primitives.isD8) | |
| 481 return computeThisScriptD8(); | |
| 482 if (_js_helper.Primitives.isJsshell) | |
| 483 return computeThisScriptJsshell(); | |
| 484 if (exports._globalState.isWorker) | |
| 485 return computeThisScriptFromTrace(); | |
| 486 return null; | |
| 487 } | |
| 488 static computeThisScriptJsshell() { | |
| 489 return dart.as(thisFilename(), core.String); | |
| 490 } | |
| 491 static computeThisScriptD8() { | |
| 492 return computeThisScriptFromTrace(); | |
| 493 } | |
| 494 static computeThisScriptFromTrace() { | |
| 495 let stack = new Error().stack; | |
| 496 if (stack === null) { | |
| 497 stack = function() { | |
| 498 try { | |
| 499 throw new Error(); | |
| 500 } catch (e) { | |
| 501 return e.stack; | |
| 502 } | |
| 503 | |
| 504 }(); | |
| 505 if (stack === null) | |
| 506 throw new core.UnsupportedError('No stack trace'); | |
| 507 } | |
| 508 let pattern = null, matches = null; | |
| 509 pattern = new RegExp("^ *at [^(]*\\((.*):[0-9]*:[0-9]*\\)$", "m"); | |
| 510 matches = stack.match(pattern); | |
| 511 if (matches !== null) | |
| 512 return matches[1]; | |
| 513 pattern = new RegExp("^[^@]*@(.*):[0-9]*$", "m"); | |
| 514 matches = stack.match(pattern); | |
| 515 if (matches !== null) | |
| 516 return matches[1]; | |
| 517 throw new core.UnsupportedError(`Cannot extract URI from "${stack}"`); | |
| 518 } | |
| 519 static [_getEventData](e) { | |
| 520 return e.data; | |
| 521 } | |
| 522 static [_processWorkerMessage](sender, e) { | |
| 523 let msg = _deserializeMessage(_getEventData(e)); | |
| 524 switch (dart.dindex(msg, 'command')) { | |
| 525 case 'start': | |
| 526 exports._globalState.currentManagerId = dart.as(dart.dindex(msg, 'id')
, core.int); | |
| 527 let functionName = dart.as(dart.dindex(msg, 'functionName'), core.Stri
ng); | |
| 528 let entryPoint = dart.as(functionName === null ? exports._globalState.
entry : _getJSFunctionFromName(functionName), core.Function); | |
| 529 let args = dart.dindex(msg, 'args'); | |
| 530 let message = _deserializeMessage(dart.dindex(msg, 'msg')); | |
| 531 let isSpawnUri = dart.dindex(msg, 'isSpawnUri'); | |
| 532 let startPaused = dart.dindex(msg, 'startPaused'); | |
| 533 let replyTo = _deserializeMessage(dart.dindex(msg, 'replyTo')); | |
| 534 let context = new _IsolateContext(); | |
| 535 exports._globalState.topEventLoop.enqueue(context, () => { | |
| 536 _startIsolate(entryPoint, dart.as(args, core.List$(core.String)), me
ssage, dart.as(isSpawnUri, core.bool), dart.as(startPaused, core.bool), dart.as(
replyTo, isolate.SendPort)); | |
| 537 }, 'worker-start'); | |
| 538 exports._globalState.currentContext = context; | |
| 539 exports._globalState.topEventLoop.run(); | |
| 540 break; | |
| 541 case 'spawn-worker': | |
| 542 if (enableSpawnWorker !== null) | |
| 543 handleSpawnWorkerRequest(msg); | |
| 544 break; | |
| 545 case 'message': | |
| 546 let port = dart.as(dart.dindex(msg, 'port'), isolate.SendPort); | |
| 547 if (port !== null) { | |
| 548 dart.dinvoke(dart.dindex(msg, 'port'), 'send', dart.dindex(msg, 'msg
')); | |
| 549 } | |
| 550 exports._globalState.topEventLoop.run(); | |
| 551 break; | |
| 552 case 'close': | |
| 553 exports._globalState.managers.remove(workerIds.get(sender)); | |
| 554 sender.terminate(); | |
| 555 exports._globalState.topEventLoop.run(); | |
| 556 break; | |
| 557 case 'log': | |
| 558 _log(dart.dindex(msg, 'msg')); | |
| 559 break; | |
| 560 case 'print': | |
| 561 if (exports._globalState.isWorker) { | |
| 562 exports._globalState.mainManager.postMessage(_serializeMessage(dart.
map({command: 'print', msg: msg}))); | |
| 563 } else { | |
| 564 core.print(dart.dindex(msg, 'msg')); | |
| 565 } | |
| 566 break; | |
| 567 case 'error': | |
| 568 throw dart.dindex(msg, 'msg'); | |
| 569 } | |
| 570 } | |
| 571 static handleSpawnWorkerRequest(msg) { | |
| 572 let replyPort = dart.dindex(msg, 'replyPort'); | |
| 573 spawn(dart.as(dart.dindex(msg, 'functionName'), core.String), dart.as(dart
.dindex(msg, 'uri'), core.String), dart.as(dart.dindex(msg, 'args'), core.List$(
core.String)), dart.dindex(msg, 'msg'), false, dart.as(dart.dindex(msg, 'isSpawn
Uri'), core.bool), dart.as(dart.dindex(msg, 'startPaused'), core.bool)).then((ms
g) => { | |
| 574 dart.dinvoke(replyPort, 'send', msg); | |
| 575 }, { | |
| 576 onError: (errorMessage) => { | |
| 577 dart.dinvoke(replyPort, 'send', new List.from([_SPAWN_FAILED_SIGNAL, e
rrorMessage])); | |
| 578 } | |
| 579 }); | |
| 580 } | |
| 581 static [_log](msg) { | |
| 582 if (exports._globalState.isWorker) { | |
| 583 exports._globalState.mainManager.postMessage(_serializeMessage(dart.map(
{command: 'log', msg: msg}))); | |
| 584 } else { | |
| 585 try { | |
| 586 _consoleLog(msg); | |
| 587 } catch (e) { | |
| 588 let trace = dart.stackTrace(e); | |
| 589 throw new core.Exception(trace); | |
| 590 } | |
| 591 | |
| 592 } | |
| 593 } | |
| 594 static [_consoleLog](msg) { | |
| 595 _js_helper.requiresPreamble(); | |
| 596 self.console.log(msg); | |
| 597 } | |
| 598 static [_getJSFunctionFromName](functionName) { | |
| 599 let globalFunctionsContainer = _foreign_helper.JS_EMBEDDED_GLOBAL("", dart
.as(_js_embedded_names.GLOBAL_FUNCTIONS, core.String)); | |
| 600 return globalFunctionsContainer[functionName](); | |
| 601 } | |
| 602 static [_getJSFunctionName](f) { | |
| 603 return dart.as(dart.is(f, _js_helper.Closure) ? f.$name : null, core.Strin
g); | |
| 604 } | |
| 605 static [_allocate](ctor) { | |
| 606 return new ctor(); | |
| 607 } | |
| 608 static spawnFunction(topLevelFunction, message, startPaused) { | |
| 609 IsolateNatives.enableSpawnWorker = true; | |
| 610 let name = _getJSFunctionName(topLevelFunction); | |
| 611 if (name === null) { | |
| 612 throw new core.UnsupportedError("only top-level functions can be spawned
."); | |
| 613 } | |
| 614 let isLight = false; | |
| 615 let isSpawnUri = false; | |
| 616 return spawn(name, null, null, message, isLight, isSpawnUri, startPaused); | |
| 617 } | |
| 618 static spawnUri(uri, args, message, startPaused) { | |
| 619 IsolateNatives.enableSpawnWorker = true; | |
| 620 let isLight = false; | |
| 621 let isSpawnUri = true; | |
| 622 return spawn(null, uri.toString(), args, message, isLight, isSpawnUri, sta
rtPaused); | |
| 623 } | |
| 624 static spawn(functionName, uri, args, message, isLight, isSpawnUri, startPau
sed) { | |
| 625 if (dart.notNull(uri !== null) && dart.notNull(uri.endsWith(".dart"))) | |
| 626 uri = ".js"; | |
| 627 let port = new isolate.ReceivePort(); | |
| 628 let completer = dart.as(new async.Completer(), async.Completer$(core.List)
); | |
| 629 port.first.then(((msg) => { | |
| 630 if (dart.equals(dart.dindex(msg, 0), _SPAWNED_SIGNAL)) { | |
| 631 completer.complete(msg); | |
| 632 } else { | |
| 633 dart.assert(dart.equals(dart.dindex(msg, 0), _SPAWN_FAILED_SIGNAL)); | |
| 634 completer.completeError(dart.dindex(msg, 1)); | |
| 635 } | |
| 636 }).bind(this)); | |
| 637 let signalReply = port.sendPort; | |
| 638 if (dart.notNull(exports._globalState.useWorkers) && !dart.notNull(isLight
)) { | |
| 639 _startWorker(functionName, uri, args, message, isSpawnUri, startPaused,
signalReply, ((message) => completer.completeError(message)).bind(this)); | |
| 640 } else { | |
| 641 _startNonWorker(functionName, uri, args, message, isSpawnUri, startPause
d, signalReply); | |
| 642 } | |
| 643 return completer.future; | |
| 644 } | |
| 645 static [_startWorker](functionName, uri, args, message, isSpawnUri, startPau
sed, replyPort, onError) { | |
| 646 if (args !== null) | |
| 647 args = new core.List.from(args); | |
| 648 if (exports._globalState.isWorker) { | |
| 649 exports._globalState.mainManager.postMessage(_serializeMessage(dart.map(
{command: 'spawn-worker', functionName: functionName, args: args, msg: message,
uri: uri, isSpawnUri: isSpawnUri, startPaused: startPaused, replyPort: replyPort
}))); | |
| 650 } else { | |
| 651 _spawnWorker(functionName, uri, args, message, isSpawnUri, startPaused,
replyPort, onError); | |
| 652 } | |
| 653 } | |
| 654 static [_startNonWorker](functionName, uri, args, message, isSpawnUri, start
Paused, replyPort) { | |
| 655 if (uri !== null) { | |
| 656 throw new core.UnsupportedError("Currently spawnUri is not supported wit
hout web workers."); | |
| 657 } | |
| 658 message = _clone(message); | |
| 659 if (args !== null) | |
| 660 args = new core.List.from(args); | |
| 661 exports._globalState.topEventLoop.enqueue(new _IsolateContext(), () => { | |
| 662 let func = _getJSFunctionFromName(functionName); | |
| 663 _startIsolate(dart.as(func, core.Function), args, message, isSpawnUri, s
tartPaused, replyPort); | |
| 664 }, 'nonworker start'); | |
| 665 } | |
| 666 static get currentIsolate() { | |
| 667 let context = dart.as(_foreign_helper.JS_CURRENT_ISOLATE_CONTEXT(), _Isola
teContext); | |
| 668 return new isolate.Isolate(context.controlPort.sendPort, {pauseCapability:
context.pauseCapability, terminateCapability: context.terminateCapability}); | |
| 669 } | |
| 670 static [_startIsolate](topLevel, args, message, isSpawnUri, startPaused, rep
lyTo) { | |
| 671 let context = dart.as(_foreign_helper.JS_CURRENT_ISOLATE_CONTEXT(), _Isola
teContext); | |
| 672 _js_helper.Primitives.initializeStatics(context.id); | |
| 673 replyTo.send(new List.from([_SPAWNED_SIGNAL, context.controlPort.sendPort,
context.pauseCapability, context.terminateCapability])); | |
| 674 // Function runStartFunction: () → void | |
| 675 function runStartFunction() { | |
| 676 context.initialized = true; | |
| 677 if (!dart.notNull(isSpawnUri)) { | |
| 678 dart.dinvokef(topLevel, message); | |
| 679 } else if (dart.is(topLevel, _MainFunctionArgsMessage)) { | |
| 680 dart.dinvokef(topLevel, args, message); | |
| 681 } else if (dart.is(topLevel, _MainFunctionArgs)) { | |
| 682 dart.dinvokef(topLevel, args); | |
| 683 } else { | |
| 684 dart.dinvokef(topLevel); | |
| 685 } | |
| 686 } | |
| 687 if (startPaused) { | |
| 688 context.addPause(context.pauseCapability, context.pauseCapability); | |
| 689 exports._globalState.topEventLoop.enqueue(context, runStartFunction, 'st
art isolate'); | |
| 690 } else { | |
| 691 runStartFunction(); | |
| 692 } | |
| 693 } | |
| 694 static [_spawnWorker](functionName, uri, args, message, isSpawnUri, startPau
sed, replyPort, onError) { | |
| 695 if (uri === null) | |
| 696 uri = thisScript; | |
| 697 let worker = new Worker(uri); | |
| 698 let onerrorTrampoline = function(f, u, c) { | |
| 699 return function(e) { | |
| 700 return f(e, u, c); | |
| 701 }; | |
| 702 }(_foreign_helper.DART_CLOSURE_TO_JS(workerOnError), uri, onError); | |
| 703 worker.onerror = onerrorTrampoline; | |
| 704 let processWorkerMessageTrampoline = function(f, a) { | |
| 705 return function(e) { | |
| 706 e.onerror = null; | |
| 707 return f(a, e); | |
| 708 }; | |
| 709 }(_foreign_helper.DART_CLOSURE_TO_JS(_processWorkerMessage), worker); | |
| 710 worker.onmessage = processWorkerMessageTrampoline; | |
| 711 let workerId = (($tmp) => exports._globalState.nextManagerId = dart.notNul
l($tmp) + 1, $tmp).bind(this)(exports._globalState.nextManagerId); | |
| 712 workerIds.set(worker, workerId); | |
| 713 exports._globalState.managers.set(workerId, worker); | |
| 714 worker.postMessage(_serializeMessage(dart.map({command: 'start', id: worke
rId, replyTo: _serializeMessage(replyPort), args: args, msg: _serializeMessage(m
essage), isSpawnUri: isSpawnUri, startPaused: startPaused, functionName: functio
nName}))); | |
| 715 } | |
| 716 static workerOnError(event, uri, onError) { | |
| 717 event.preventDefault(); | |
| 718 let message = dart.as(event.message, core.String); | |
| 719 if (message === null) { | |
| 720 message = `Error spawning worker for ${uri}`; | |
| 721 } else { | |
| 722 message = `Error spawning worker for ${uri} (${message})`; | |
| 723 } | |
| 724 onError(message); | |
| 725 return true; | |
| 726 } | |
| 727 } | |
| 728 IsolateNatives.enableSpawnWorker = null; | |
| 729 dart.defineLazyProperties(IsolateNatives, { | |
| 730 get thisScript() { | |
| 731 return computeThisScript(); | |
| 732 }, | |
| 733 set thisScript(_) {}, | |
| 734 get workerIds() { | |
| 735 return new core.Expando(); | |
| 736 } | |
| 737 }); | |
| 738 let _isolateId = Symbol('_isolateId'); | |
| 739 let _checkReplyTo = Symbol('_checkReplyTo'); | |
| 740 class _BaseSendPort extends dart.Object { | |
| 741 _BaseSendPort($_isolateId) { | |
| 742 this[_isolateId] = $_isolateId; | |
| 743 } | |
| 744 [_checkReplyTo](replyTo) { | |
| 745 if (dart.notNull(replyTo !== null) && dart.notNull(!dart.is(replyTo, _Nati
veJsSendPort)) && dart.notNull(!dart.is(replyTo, _WorkerSendPort))) { | |
| 746 throw new core.Exception("SendPort.send: Illegal replyTo port type"); | |
| 747 } | |
| 748 } | |
| 749 } | |
| 750 let _receivePort = Symbol('_receivePort'); | |
| 751 let _isClosed = Symbol('_isClosed'); | |
| 752 class _NativeJsSendPort extends _BaseSendPort { | |
| 753 _NativeJsSendPort($_receivePort, isolateId) { | |
| 754 this[_receivePort] = $_receivePort; | |
| 755 super._BaseSendPort(isolateId); | |
| 756 } | |
| 757 send(message) { | |
| 758 let isolate = exports._globalState.isolates.get(this[_isolateId]); | |
| 759 if (isolate === null) | |
| 760 return; | |
| 761 if (this[_receivePort][_isClosed]) | |
| 762 return; | |
| 763 let msg = _clone(message); | |
| 764 if (dart.equals(isolate.controlPort, this[_receivePort])) { | |
| 765 isolate.handleControlMessage(msg); | |
| 766 return; | |
| 767 } | |
| 768 exports._globalState.topEventLoop.enqueue(isolate, (() => { | |
| 769 if (!dart.notNull(this[_receivePort][_isClosed])) { | |
| 770 this[_receivePort]._add(msg); | |
| 771 } | |
| 772 }).bind(this), `receive ${message}`); | |
| 773 } | |
| 774 ['=='](other) { | |
| 775 return dart.notNull(dart.is(other, _NativeJsSendPort)) && dart.notNull(dar
t.equals(this[_receivePort], dart.dload(other, '_receivePort'))); | |
| 776 } | |
| 777 get hashCode() { | |
| 778 return this[_receivePort][_id]; | |
| 779 } | |
| 780 } | |
| 781 let _workerId = Symbol('_workerId'); | |
| 782 let _receivePortId = Symbol('_receivePortId'); | |
| 783 class _WorkerSendPort extends _BaseSendPort { | |
| 784 _WorkerSendPort($_workerId, isolateId, $_receivePortId) { | |
| 785 this[_workerId] = $_workerId; | |
| 786 this[_receivePortId] = $_receivePortId; | |
| 787 super._BaseSendPort(isolateId); | |
| 788 } | |
| 789 send(message) { | |
| 790 let workerMessage = _serializeMessage(dart.map({command: 'message', port:
this, msg: message})); | |
| 791 if (exports._globalState.isWorker) { | |
| 792 exports._globalState.mainManager.postMessage(workerMessage); | |
| 793 } else { | |
| 794 let manager = exports._globalState.managers.get(this[_workerId]); | |
| 795 if (manager !== null) { | |
| 796 manager.postMessage(workerMessage); | |
| 797 } | |
| 798 } | |
| 799 } | |
| 800 ['=='](other) { | |
| 801 return dart.notNull(dart.is(other, _WorkerSendPort)) && this[_workerId] ==
= dart.dload(other, '_workerId') && this[_isolateId] === dart.dload(other, '_iso
lateId') && this[_receivePortId] === dart.dload(other, '_receivePortId'); | |
| 802 } | |
| 803 get hashCode() { | |
| 804 return dart.notNull(this[_workerId]) << 16 ^ dart.notNull(this[_isolateId]
) << 8 ^ dart.notNull(this[_receivePortId]); | |
| 805 } | |
| 806 } | |
| 807 let _handler = Symbol('_handler'); | |
| 808 let _close = Symbol('_close'); | |
| 809 let _add = Symbol('_add'); | |
| 810 class RawReceivePortImpl extends dart.Object { | |
| 811 RawReceivePortImpl($_handler) { | |
| 812 this[_handler] = $_handler; | |
| 813 this[_id] = (($tmp) => _nextFreeId = dart.notNull($tmp) + 1, $tmp)(_nextFr
eeId); | |
| 814 this[_isClosed] = false; | |
| 815 exports._globalState.currentContext.register(this[_id], this); | |
| 816 } | |
| 817 RawReceivePortImpl$weak($_handler) { | |
| 818 this[_handler] = $_handler; | |
| 819 this[_id] = (($tmp) => _nextFreeId = dart.notNull($tmp) + 1, $tmp)(_nextFr
eeId); | |
| 820 this[_isClosed] = false; | |
| 821 exports._globalState.currentContext.registerWeak(this[_id], this); | |
| 822 } | |
| 823 RawReceivePortImpl$_controlPort() { | |
| 824 this[_handler] = null; | |
| 825 this[_id] = 0; | |
| 826 this[_isClosed] = false; | |
| 827 } | |
| 828 set handler(newHandler) { | |
| 829 this[_handler] = newHandler; | |
| 830 } | |
| 831 [_close]() { | |
| 832 this[_isClosed] = true; | |
| 833 this[_handler] = null; | |
| 834 } | |
| 835 close() { | |
| 836 if (this[_isClosed]) | |
| 837 return; | |
| 838 this[_isClosed] = true; | |
| 839 this[_handler] = null; | |
| 840 exports._globalState.currentContext.unregister(this[_id]); | |
| 841 } | |
| 842 [_add](dataEvent) { | |
| 843 if (this[_isClosed]) | |
| 844 return; | |
| 845 dart.dinvokef(this[_handler], dataEvent); | |
| 846 } | |
| 847 get sendPort() { | |
| 848 return new _NativeJsSendPort(this, exports._globalState.currentContext.id)
; | |
| 849 } | |
| 850 } | |
| 851 dart.defineNamedConstructor(RawReceivePortImpl, 'weak'); | |
| 852 dart.defineNamedConstructor(RawReceivePortImpl, '_controlPort'); | |
| 853 RawReceivePortImpl._nextFreeId = 1; | |
| 854 let _rawPort = Symbol('_rawPort'); | |
| 855 let _controller = Symbol('_controller'); | |
| 856 class ReceivePortImpl extends async.Stream { | |
| 857 ReceivePortImpl() { | |
| 858 this.ReceivePortImpl$fromRawReceivePort(new RawReceivePortImpl(null)); | |
| 859 } | |
| 860 ReceivePortImpl$weak() { | |
| 861 this.ReceivePortImpl$fromRawReceivePort(new RawReceivePortImpl.weak(null))
; | |
| 862 } | |
| 863 ReceivePortImpl$fromRawReceivePort($_rawPort) { | |
| 864 this[_rawPort] = $_rawPort; | |
| 865 this[_controller] = null; | |
| 866 super.Stream(); | |
| 867 this[_controller] = new async.StreamController({onCancel: this.close, sync
: true}); | |
| 868 this[_rawPort].handler = this[_controller].add; | |
| 869 } | |
| 870 listen(onData, opt$) { | |
| 871 let onError = opt$.onError === void 0 ? null : opt$.onError; | |
| 872 let onDone = opt$.onDone === void 0 ? null : opt$.onDone; | |
| 873 let cancelOnError = opt$.cancelOnError === void 0 ? null : opt$.cancelOnEr
ror; | |
| 874 return this[_controller].stream.listen(onData, {onError: onError, onDone:
onDone, cancelOnError: cancelOnError}); | |
| 875 } | |
| 876 close() { | |
| 877 this[_rawPort].close(); | |
| 878 this[_controller].close(); | |
| 879 } | |
| 880 get sendPort() { | |
| 881 return this[_rawPort].sendPort; | |
| 882 } | |
| 883 } | |
| 884 dart.defineNamedConstructor(ReceivePortImpl, 'weak'); | |
| 885 dart.defineNamedConstructor(ReceivePortImpl, 'fromRawReceivePort'); | |
| 886 let _once = Symbol('_once'); | |
| 887 let _inEventLoop = Symbol('_inEventLoop'); | |
| 888 let _handle = Symbol('_handle'); | |
| 889 class TimerImpl extends dart.Object { | |
| 890 TimerImpl(milliseconds, callback) { | |
| 891 this[_once] = true; | |
| 892 this[_inEventLoop] = false; | |
| 893 this[_handle] = null; | |
| 894 if (milliseconds === 0 && (!dart.notNull(hasTimer()) || dart.notNull(expor
ts._globalState.isWorker))) { | |
| 895 // Function internalCallback: () → void | |
| 896 function internalCallback() { | |
| 897 this[_handle] = null; | |
| 898 callback(); | |
| 899 } | |
| 900 this[_handle] = 1; | |
| 901 exports._globalState.topEventLoop.enqueue(exports._globalState.currentCo
ntext, internalCallback, 'timer'); | |
| 902 this[_inEventLoop] = true; | |
| 903 } else if (hasTimer()) { | |
| 904 // Function internalCallback: () → void | |
| 905 function internalCallback() { | |
| 906 this[_handle] = null; | |
| 907 leaveJsAsync(); | |
| 908 callback(); | |
| 909 } | |
| 910 enterJsAsync(); | |
| 911 this[_handle] = self.setTimeout(_js_helper.convertDartClosureToJS(intern
alCallback, 0), milliseconds); | |
| 912 } else { | |
| 913 dart.assert(dart.notNull(milliseconds) > 0); | |
| 914 throw new core.UnsupportedError("Timer greater than 0."); | |
| 915 } | |
| 916 } | |
| 917 TimerImpl$periodic(milliseconds, callback) { | |
| 918 this[_once] = false; | |
| 919 this[_inEventLoop] = false; | |
| 920 this[_handle] = null; | |
| 921 if (hasTimer()) { | |
| 922 enterJsAsync(); | |
| 923 this[_handle] = self.setInterval(_js_helper.convertDartClosureToJS((() =
> { | |
| 924 callback(this); | |
| 925 }).bind(this), 0), milliseconds); | |
| 926 } else { | |
| 927 throw new core.UnsupportedError("Periodic timer."); | |
| 928 } | |
| 929 } | |
| 930 cancel() { | |
| 931 if (hasTimer()) { | |
| 932 if (this[_inEventLoop]) { | |
| 933 throw new core.UnsupportedError("Timer in event loop cannot be cancele
d."); | |
| 934 } | |
| 935 if (this[_handle] === null) | |
| 936 return; | |
| 937 leaveJsAsync(); | |
| 938 if (this[_once]) { | |
| 939 self.clearTimeout(this[_handle]); | |
| 940 } else { | |
| 941 self.clearInterval(this[_handle]); | |
| 942 } | |
| 943 this[_handle] = null; | |
| 944 } else { | |
| 945 throw new core.UnsupportedError("Canceling a timer."); | |
| 946 } | |
| 947 } | |
| 948 get isActive() { | |
| 949 return this[_handle] !== null; | |
| 950 } | |
| 951 } | |
| 952 dart.defineNamedConstructor(TimerImpl, 'periodic'); | |
| 953 // Function hasTimer: () → bool | |
| 954 function hasTimer() { | |
| 955 _js_helper.requiresPreamble(); | |
| 956 return self.setTimeout !== null; | |
| 957 } | |
| 958 class CapabilityImpl extends dart.Object { | |
| 959 CapabilityImpl() { | |
| 960 this.CapabilityImpl$_internal(_js_helper.random64()); | |
| 961 } | |
| 962 CapabilityImpl$_internal($_id) { | |
| 963 this[_id] = $_id; | |
| 964 } | |
| 965 get hashCode() { | |
| 966 let hash = this[_id]; | |
| 967 hash = dart.notNull(hash) >> 0 ^ (dart.notNull(hash) / 4294967296).truncat
e(); | |
| 968 hash = ~dart.notNull(hash) + (dart.notNull(hash) << 15) & 4294967295; | |
| 969 hash = dart.notNull(hash) >> 12; | |
| 970 hash = dart.notNull(hash) * 5 & 4294967295; | |
| 971 hash = dart.notNull(hash) >> 4; | |
| 972 hash = dart.notNull(hash) * 2057 & 4294967295; | |
| 973 hash = dart.notNull(hash) >> 16; | |
| 974 return hash; | |
| 975 } | |
| 976 ['=='](other) { | |
| 977 if (core.identical(other, this)) | |
| 978 return true; | |
| 979 if (dart.is(other, CapabilityImpl)) { | |
| 980 return core.identical(this[_id], other[_id]); | |
| 981 } | |
| 982 return false; | |
| 983 } | |
| 984 } | |
| 985 dart.defineNamedConstructor(CapabilityImpl, '_internal'); | |
| 986 // Function _serializeMessage: (dynamic) → dynamic | 4 // Function _serializeMessage: (dynamic) → dynamic |
| 987 function _serializeMessage(message) { | 5 function _serializeMessage(message) { |
| 988 return new _Serializer().serialize(message); | 6 return new _Serializer().serialize(message); |
| 989 } | 7 } |
| 990 // Function _deserializeMessage: (dynamic) → dynamic | 8 // Function _deserializeMessage: (dynamic) → dynamic |
| 991 function _deserializeMessage(message) { | 9 function _deserializeMessage(message) { |
| 992 return new _Deserializer().deserialize(message); | 10 return new _Deserializer().deserialize(message); |
| 993 } | 11 } |
| 994 // Function _clone: (dynamic) → dynamic | 12 // Function _clone: (dynamic) → dynamic |
| 995 function _clone(message) { | 13 function _clone(message) { |
| 996 let serializer = new _Serializer({serializeSendPorts: false}); | 14 let serializer = new _Serializer({serializeSendPorts: false}); |
| 997 let deserializer = new _Deserializer(); | 15 let deserializer = new _Deserializer(); |
| 998 return deserializer.deserialize(serializer.serialize(message)); | 16 return deserializer.deserialize(serializer.serialize(message)); |
| 999 } | 17 } |
| 1000 let _serializeSendPorts = Symbol('_serializeSendPorts'); | 18 let _serializeSendPorts = Symbol('_serializeSendPorts'); |
| 1001 class _Serializer extends dart.Object { | 19 let _workerId = Symbol('_workerId'); |
| 20 let _isolateId = Symbol('_isolateId'); |
| 21 let _receivePortId = Symbol('_receivePortId'); |
| 22 let _receivePort = Symbol('_receivePort'); |
| 23 let _id = Symbol('_id'); |
| 24 class _Serializer extends core.Object { |
| 1002 _Serializer(opt$) { | 25 _Serializer(opt$) { |
| 1003 let serializeSendPorts = opt$.serializeSendPorts === void 0 ? true : opt$.
serializeSendPorts; | 26 let serializeSendPorts = opt$.serializeSendPorts === void 0 ? true : opt$.
serializeSendPorts; |
| 1004 this.serializedObjectIds = new core.Map.identity(); | 27 this.serializedObjectIds = new core.Map.identity(); |
| 1005 this[_serializeSendPorts] = dart.as(serializeSendPorts, core.bool); | 28 this[_serializeSendPorts] = dart.as(serializeSendPorts, core.bool); |
| 1006 } | 29 } |
| 1007 serialize(x) { | 30 serialize(x) { |
| 1008 if (this.isPrimitive(x)) | 31 if (this.isPrimitive(x)) |
| 1009 return this.serializePrimitive(x); | 32 return this.serializePrimitive(x); |
| 1010 let serializationId = this.serializedObjectIds.get(x); | 33 let serializationId = this.serializedObjectIds.get(x); |
| 1011 if (serializationId !== null) | 34 if (serializationId !== null) |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1126 } | 149 } |
| 1127 serializeDartObject(x) { | 150 serializeDartObject(x) { |
| 1128 let classExtractor = _foreign_helper.JS_EMBEDDED_GLOBAL('', dart.as(_js_em
bedded_names.CLASS_ID_EXTRACTOR, core.String)); | 151 let classExtractor = _foreign_helper.JS_EMBEDDED_GLOBAL('', dart.as(_js_em
bedded_names.CLASS_ID_EXTRACTOR, core.String)); |
| 1129 let fieldsExtractor = _foreign_helper.JS_EMBEDDED_GLOBAL('', dart.as(_js_e
mbedded_names.CLASS_FIELDS_EXTRACTOR, core.String)); | 152 let fieldsExtractor = _foreign_helper.JS_EMBEDDED_GLOBAL('', dart.as(_js_e
mbedded_names.CLASS_FIELDS_EXTRACTOR, core.String)); |
| 1130 let classId = classExtractor(x); | 153 let classId = classExtractor(x); |
| 1131 let fields = dart.as(fieldsExtractor(x), core.List); | 154 let fields = dart.as(fieldsExtractor(x), core.List); |
| 1132 return new List.from(['dart', classId, this.serializeArrayInPlace(dart.as(
fields, _interceptors.JSArray))]); | 155 return new List.from(['dart', classId, this.serializeArrayInPlace(dart.as(
fields, _interceptors.JSArray))]); |
| 1133 } | 156 } |
| 1134 } | 157 } |
| 1135 let _adjustSendPorts = Symbol('_adjustSendPorts'); | 158 let _adjustSendPorts = Symbol('_adjustSendPorts'); |
| 1136 class _Deserializer extends dart.Object { | 159 class _Deserializer extends core.Object { |
| 1137 _Deserializer(opt$) { | 160 _Deserializer(opt$) { |
| 1138 let adjustSendPorts = opt$.adjustSendPorts === void 0 ? true : opt$.adjust
SendPorts; | 161 let adjustSendPorts = opt$.adjustSendPorts === void 0 ? true : opt$.adjust
SendPorts; |
| 1139 this.deserializedObjects = new core.List(); | 162 this.deserializedObjects = new core.List(); |
| 1140 this[_adjustSendPorts] = dart.as(adjustSendPorts, core.bool); | 163 this[_adjustSendPorts] = dart.as(adjustSendPorts, core.bool); |
| 1141 } | 164 } |
| 1142 deserialize(x) { | 165 deserialize(x) { |
| 1143 if (this.isPrimitive(x)) | 166 if (this.isPrimitive(x)) |
| 1144 return this.deserializePrimitive(x); | 167 return this.deserializePrimitive(x); |
| 1145 if (!dart.is(x, _interceptors.JSArray)) | 168 if (!dart.is(x, _interceptors.JSArray)) |
| 1146 throw new core.ArgumentError(`Bad serialized message: ${x}`); | 169 throw new core.ArgumentError(`Bad serialized message: ${x}`); |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1289 let classId = dart.as(dart.dindex(x, 1), core.String); | 312 let classId = dart.as(dart.dindex(x, 1), core.String); |
| 1290 let fields = dart.as(dart.dindex(x, 2), core.List); | 313 let fields = dart.as(dart.dindex(x, 2), core.List); |
| 1291 let instanceFromClassId = _foreign_helper.JS_EMBEDDED_GLOBAL('', dart.as(_
js_embedded_names.INSTANCE_FROM_CLASS_ID, core.String)); | 314 let instanceFromClassId = _foreign_helper.JS_EMBEDDED_GLOBAL('', dart.as(_
js_embedded_names.INSTANCE_FROM_CLASS_ID, core.String)); |
| 1292 let initializeObject = _foreign_helper.JS_EMBEDDED_GLOBAL('', dart.as(_js_
embedded_names.INITIALIZE_EMPTY_INSTANCE, core.String)); | 315 let initializeObject = _foreign_helper.JS_EMBEDDED_GLOBAL('', dart.as(_js_
embedded_names.INITIALIZE_EMPTY_INSTANCE, core.String)); |
| 1293 let emptyInstance = instanceFromClassId(classId); | 316 let emptyInstance = instanceFromClassId(classId); |
| 1294 this.deserializedObjects.add(emptyInstance); | 317 this.deserializedObjects.add(emptyInstance); |
| 1295 this.deserializeArrayInPlace(dart.as(fields, _interceptors.JSArray)); | 318 this.deserializeArrayInPlace(dart.as(fields, _interceptors.JSArray)); |
| 1296 return initializeObject(classId, emptyInstance, fields); | 319 return initializeObject(classId, emptyInstance, fields); |
| 1297 } | 320 } |
| 1298 } | 321 } |
| 322 // Function _callInIsolate: (_IsolateContext, Function) → dynamic |
| 323 function _callInIsolate(isolate, function) { |
| 324 let result = isolate.eval(function); |
| 325 exports._globalState.topEventLoop.run(); |
| 326 return result; |
| 327 } |
| 328 let _activeJsAsyncCount = Symbol('_activeJsAsyncCount'); |
| 329 // Function enterJsAsync: () → dynamic |
| 330 function enterJsAsync() { |
| 331 exports._globalState.topEventLoop[_activeJsAsyncCount] = dart.notNull(export
s._globalState.topEventLoop[_activeJsAsyncCount]) + 1; |
| 332 } |
| 333 // Function leaveJsAsync: () → dynamic |
| 334 function leaveJsAsync() { |
| 335 exports._globalState.topEventLoop[_activeJsAsyncCount] = dart.notNull(export
s._globalState.topEventLoop[_activeJsAsyncCount]) - 1; |
| 336 dart.assert(dart.notNull(exports._globalState.topEventLoop[_activeJsAsyncCou
nt]) >= 0); |
| 337 } |
| 338 // Function isWorker: () → bool |
| 339 function isWorker() { |
| 340 return exports._globalState.isWorker; |
| 341 } |
| 342 // Function _currentIsolate: () → _IsolateContext |
| 343 function _currentIsolate() { |
| 344 return exports._globalState.currentContext; |
| 345 } |
| 346 // Function startRootIsolate: (dynamic, dynamic) → void |
| 347 function startRootIsolate(entry, args) { |
| 348 args = args; |
| 349 if (args === null) |
| 350 args = new List.from([]); |
| 351 if (!dart.is(args, core.List)) { |
| 352 throw new core.ArgumentError(`Arguments to main must be a List: ${args}`); |
| 353 } |
| 354 exports._globalState = new _Manager(dart.as(entry, core.Function)); |
| 355 if (exports._globalState.isWorker) |
| 356 return; |
| 357 let rootContext = new _IsolateContext(); |
| 358 exports._globalState.rootContext = rootContext; |
| 359 exports._globalState.currentContext = rootContext; |
| 360 if (dart.is(entry, _MainFunctionArgs)) { |
| 361 rootContext.eval(() => { |
| 362 dart.dinvokef(entry, args); |
| 363 }); |
| 364 } else if (dart.is(entry, _MainFunctionArgsMessage)) { |
| 365 rootContext.eval(() => { |
| 366 dart.dinvokef(entry, args, null); |
| 367 }); |
| 368 } else { |
| 369 rootContext.eval(dart.as(entry, core.Function)); |
| 370 } |
| 371 exports._globalState.topEventLoop.run(); |
| 372 } |
| 373 dart.copyProperties(exports, { |
| 374 get _globalState() { |
| 375 return dart.as(init.globalState, _Manager); |
| 376 }, |
| 377 set _globalState(val) { |
| 378 init.globalState = val; |
| 379 } |
| 380 }); |
| 381 let _nativeDetectEnvironment = Symbol('_nativeDetectEnvironment'); |
| 382 let _nativeInitWorkerMessageHandler = Symbol('_nativeInitWorkerMessageHandler'
); |
| 383 let _processWorkerMessage = Symbol('_processWorkerMessage'); |
| 384 let _serializePrintMessage = Symbol('_serializePrintMessage'); |
| 385 class _Manager extends core.Object { |
| 386 get useWorkers() { |
| 387 return this.supportsWorkers; |
| 388 } |
| 389 _Manager(entry) { |
| 390 this.entry = entry; |
| 391 this.nextIsolateId = 0; |
| 392 this.currentManagerId = 0; |
| 393 this.nextManagerId = 1; |
| 394 this.currentContext = null; |
| 395 this.rootContext = null; |
| 396 this.topEventLoop = null; |
| 397 this.fromCommandLine = null; |
| 398 this.isWorker = null; |
| 399 this.supportsWorkers = null; |
| 400 this.isolates = null; |
| 401 this.mainManager = null; |
| 402 this.managers = null; |
| 403 this[_nativeDetectEnvironment](); |
| 404 this.topEventLoop = new _EventLoop(); |
| 405 this.isolates = new core.Map(); |
| 406 this.managers = new core.Map(); |
| 407 if (this.isWorker) { |
| 408 this.mainManager = new _MainManagerStub(); |
| 409 this[_nativeInitWorkerMessageHandler](); |
| 410 } |
| 411 } |
| 412 [_nativeDetectEnvironment]() { |
| 413 let isWindowDefined = exports.globalWindow !== null; |
| 414 let isWorkerDefined = exports.globalWorker !== null; |
| 415 this.isWorker = !dart.notNull(isWindowDefined) && dart.notNull(exports.glo
balPostMessageDefined); |
| 416 this.supportsWorkers = dart.notNull(this.isWorker) || dart.notNull(isWorke
rDefined) && dart.notNull(IsolateNatives.thisScript !== null); |
| 417 this.fromCommandLine = !dart.notNull(isWindowDefined) && !dart.notNull(thi
s.isWorker); |
| 418 } |
| 419 [_nativeInitWorkerMessageHandler]() { |
| 420 let function = function(f, a) { |
| 421 return function(e) { |
| 422 f(a, e); |
| 423 }; |
| 424 }(_foreign_helper.DART_CLOSURE_TO_JS(IsolateNatives[_processWorkerMessage]
), this.mainManager); |
| 425 self.onmessage = function; |
| 426 self.dartPrint = self.dartPrint || function(serialize) { |
| 427 return function(object) { |
| 428 if (self.console && self.console.log) { |
| 429 self.console.log(object); |
| 430 } else { |
| 431 self.postMessage(serialize(object)); |
| 432 } |
| 433 }; |
| 434 }(_foreign_helper.DART_CLOSURE_TO_JS(_serializePrintMessage)); |
| 435 } |
| 436 static [_serializePrintMessage](object) { |
| 437 return _serializeMessage(dart.map({command: "print", msg: object})); |
| 438 } |
| 439 maybeCloseWorker() { |
| 440 if (dart.notNull(this.isWorker) && dart.notNull(this.isolates.isEmpty) &&
this.topEventLoop[_activeJsAsyncCount] === 0) { |
| 441 this.mainManager.postMessage(_serializeMessage(dart.map({command: 'close
'}))); |
| 442 } |
| 443 } |
| 444 } |
| 445 let _scheduledControlEvents = Symbol('_scheduledControlEvents'); |
| 446 let _isExecutingEvent = Symbol('_isExecutingEvent'); |
| 447 let _updateGlobalState = Symbol('_updateGlobalState'); |
| 448 let _setGlobals = Symbol('_setGlobals'); |
| 449 let _addRegistration = Symbol('_addRegistration'); |
| 450 class _IsolateContext extends core.Object { |
| 451 _IsolateContext() { |
| 452 this.id = (($tmp) => exports._globalState.nextIsolateId = dart.notNull($tm
p) + 1, $tmp).bind(this)(exports._globalState.nextIsolateId); |
| 453 this.ports = new core.Map(); |
| 454 this.weakPorts = new core.Set(); |
| 455 this.isolateStatics = _foreign_helper.JS_CREATE_ISOLATE(); |
| 456 this.controlPort = new RawReceivePortImpl._controlPort(); |
| 457 this.pauseCapability = new isolate.Capability(); |
| 458 this.terminateCapability = new isolate.Capability(); |
| 459 this.delayedEvents = dart.as(new List.from([]), core.List$(_IsolateEvent))
; |
| 460 this.pauseTokens = dart.as(new core.Set(), core.Set$(isolate.Capability)); |
| 461 this.errorPorts = dart.as(new core.Set(), core.Set$(isolate.SendPort)); |
| 462 this.initialized = false; |
| 463 this.isPaused = false; |
| 464 this.doneHandlers = null; |
| 465 this[_scheduledControlEvents] = null; |
| 466 this[_isExecutingEvent] = false; |
| 467 this.errorsAreFatal = true; |
| 468 this.registerWeak(this.controlPort[_id], this.controlPort); |
| 469 } |
| 470 addPause(authentification, resume) { |
| 471 if (!dart.equals(this.pauseCapability, authentification)) |
| 472 return; |
| 473 if (dart.notNull(this.pauseTokens.add(resume)) && !dart.notNull(this.isPau
sed)) { |
| 474 this.isPaused = true; |
| 475 } |
| 476 this[_updateGlobalState](); |
| 477 } |
| 478 removePause(resume) { |
| 479 if (!dart.notNull(this.isPaused)) |
| 480 return; |
| 481 this.pauseTokens.remove(resume); |
| 482 if (this.pauseTokens.isEmpty) { |
| 483 while (this.delayedEvents.isNotEmpty) { |
| 484 let event = this.delayedEvents.removeLast(); |
| 485 exports._globalState.topEventLoop.prequeue(event); |
| 486 } |
| 487 this.isPaused = false; |
| 488 } |
| 489 this[_updateGlobalState](); |
| 490 } |
| 491 addDoneListener(responsePort) { |
| 492 if (this.doneHandlers === null) { |
| 493 this.doneHandlers = new List.from([]); |
| 494 } |
| 495 if (dart.dinvoke(this.doneHandlers, 'contains', responsePort)) |
| 496 return; |
| 497 dart.dinvoke(this.doneHandlers, 'add', responsePort); |
| 498 } |
| 499 removeDoneListener(responsePort) { |
| 500 if (this.doneHandlers === null) |
| 501 return; |
| 502 dart.dinvoke(this.doneHandlers, 'remove', responsePort); |
| 503 } |
| 504 setErrorsFatal(authentification, errorsAreFatal) { |
| 505 if (!dart.equals(this.terminateCapability, authentification)) |
| 506 return; |
| 507 this.errorsAreFatal = errorsAreFatal; |
| 508 } |
| 509 handlePing(responsePort, pingType) { |
| 510 if (pingType === isolate.Isolate.IMMEDIATE || pingType === isolate.Isolate
.BEFORE_NEXT_EVENT && !dart.notNull(this[_isExecutingEvent])) { |
| 511 responsePort.send(null); |
| 512 return; |
| 513 } |
| 514 // Function respond: () → void |
| 515 function respond() { |
| 516 responsePort.send(null); |
| 517 } |
| 518 if (pingType === isolate.Isolate.AS_EVENT) { |
| 519 exports._globalState.topEventLoop.enqueue(this, respond, "ping"); |
| 520 return; |
| 521 } |
| 522 dart.assert(pingType === isolate.Isolate.BEFORE_NEXT_EVENT); |
| 523 if (this[_scheduledControlEvents] === null) { |
| 524 this[_scheduledControlEvents] = new collection.Queue(); |
| 525 } |
| 526 dart.dinvoke(this[_scheduledControlEvents], 'addLast', respond); |
| 527 } |
| 528 handleKill(authentification, priority) { |
| 529 if (!dart.equals(this.terminateCapability, authentification)) |
| 530 return; |
| 531 if (priority === isolate.Isolate.IMMEDIATE || priority === isolate.Isolate
.BEFORE_NEXT_EVENT && !dart.notNull(this[_isExecutingEvent])) { |
| 532 this.kill(); |
| 533 return; |
| 534 } |
| 535 if (priority === isolate.Isolate.AS_EVENT) { |
| 536 exports._globalState.topEventLoop.enqueue(this, this.kill, "kill"); |
| 537 return; |
| 538 } |
| 539 dart.assert(priority === isolate.Isolate.BEFORE_NEXT_EVENT); |
| 540 if (this[_scheduledControlEvents] === null) { |
| 541 this[_scheduledControlEvents] = new collection.Queue(); |
| 542 } |
| 543 dart.dinvoke(this[_scheduledControlEvents], 'addLast', this.kill); |
| 544 } |
| 545 addErrorListener(port) { |
| 546 this.errorPorts.add(port); |
| 547 } |
| 548 removeErrorListener(port) { |
| 549 this.errorPorts.remove(port); |
| 550 } |
| 551 handleUncaughtError(error, stackTrace) { |
| 552 if (this.errorPorts.isEmpty) { |
| 553 if (dart.notNull(this.errorsAreFatal) && dart.notNull(core.identical(thi
s, exports._globalState.rootContext))) { |
| 554 return; |
| 555 } |
| 556 if (self.console && self.console.error) { |
| 557 self.console.error(error, stackTrace); |
| 558 } else { |
| 559 core.print(error); |
| 560 if (stackTrace !== null) |
| 561 core.print(stackTrace); |
| 562 } |
| 563 return; |
| 564 } |
| 565 let message = new core.List(2); |
| 566 message.set(0, dart.dinvoke(error, 'toString')); |
| 567 message.set(1, stackTrace === null ? null : stackTrace.toString()); |
| 568 for (let port of this.errorPorts) |
| 569 port.send(message); |
| 570 } |
| 571 eval(code) { |
| 572 let old = exports._globalState.currentContext; |
| 573 exports._globalState.currentContext = this; |
| 574 this._setGlobals(); |
| 575 let result = null; |
| 576 this[_isExecutingEvent] = true; |
| 577 try { |
| 578 result = dart.dinvokef(code); |
| 579 } catch (e) { |
| 580 let s = dart.stackTrace(e); |
| 581 this.handleUncaughtError(e, s); |
| 582 if (this.errorsAreFatal) { |
| 583 this.kill(); |
| 584 if (core.identical(this, exports._globalState.rootContext)) { |
| 585 throw e; |
| 586 } |
| 587 } |
| 588 } |
| 589 finally { |
| 590 this[_isExecutingEvent] = false; |
| 591 exports._globalState.currentContext = old; |
| 592 if (old !== null) |
| 593 old._setGlobals(); |
| 594 if (this[_scheduledControlEvents] !== null) { |
| 595 while (dart.dload(this[_scheduledControlEvents], 'isNotEmpty')) { |
| 596 dart.dinvokef(dart.dinvoke(this[_scheduledControlEvents], 'removeFir
st')); |
| 597 } |
| 598 } |
| 599 } |
| 600 return result; |
| 601 } |
| 602 [_setGlobals]() { |
| 603 _foreign_helper.JS_SET_CURRENT_ISOLATE(this.isolateStatics); |
| 604 } |
| 605 handleControlMessage(message) { |
| 606 switch (dart.dindex(message, 0)) { |
| 607 case "pause": |
| 608 this.addPause(dart.as(dart.dindex(message, 1), isolate.Capability), da
rt.as(dart.dindex(message, 2), isolate.Capability)); |
| 609 break; |
| 610 case "resume": |
| 611 this.removePause(dart.as(dart.dindex(message, 1), isolate.Capability))
; |
| 612 break; |
| 613 case 'add-ondone': |
| 614 this.addDoneListener(dart.as(dart.dindex(message, 1), isolate.SendPort
)); |
| 615 break; |
| 616 case 'remove-ondone': |
| 617 this.removeDoneListener(dart.as(dart.dindex(message, 1), isolate.SendP
ort)); |
| 618 break; |
| 619 case 'set-errors-fatal': |
| 620 this.setErrorsFatal(dart.as(dart.dindex(message, 1), isolate.Capabilit
y), dart.as(dart.dindex(message, 2), core.bool)); |
| 621 break; |
| 622 case "ping": |
| 623 this.handlePing(dart.as(dart.dindex(message, 1), isolate.SendPort), da
rt.as(dart.dindex(message, 2), core.int)); |
| 624 break; |
| 625 case "kill": |
| 626 this.handleKill(dart.as(dart.dindex(message, 1), isolate.Capability),
dart.as(dart.dindex(message, 2), core.int)); |
| 627 break; |
| 628 case "getErrors": |
| 629 this.addErrorListener(dart.as(dart.dindex(message, 1), isolate.SendPor
t)); |
| 630 break; |
| 631 case "stopErrors": |
| 632 this.removeErrorListener(dart.as(dart.dindex(message, 1), isolate.Send
Port)); |
| 633 break; |
| 634 default: |
| 635 } |
| 636 } |
| 637 lookup(portId) { |
| 638 return this.ports.get(portId); |
| 639 } |
| 640 [_addRegistration](portId, port) { |
| 641 if (this.ports.containsKey(portId)) { |
| 642 throw new core.Exception("Registry: ports must be registered only once."
); |
| 643 } |
| 644 this.ports.set(portId, port); |
| 645 } |
| 646 register(portId, port) { |
| 647 this[_addRegistration](portId, port); |
| 648 this[_updateGlobalState](); |
| 649 } |
| 650 registerWeak(portId, port) { |
| 651 this.weakPorts.add(portId); |
| 652 this[_addRegistration](portId, port); |
| 653 } |
| 654 [_updateGlobalState]() { |
| 655 if (dart.notNull(this.ports.length) - dart.notNull(this.weakPorts.length)
> 0 || dart.notNull(this.isPaused) || !dart.notNull(this.initialized)) { |
| 656 exports._globalState.isolates.set(this.id, this); |
| 657 } else { |
| 658 this.kill(); |
| 659 } |
| 660 } |
| 661 kill() { |
| 662 if (this[_scheduledControlEvents] !== null) { |
| 663 dart.dinvoke(this[_scheduledControlEvents], 'clear'); |
| 664 } |
| 665 for (let port of this.ports.values) { |
| 666 dart.dinvoke(port, '_close'); |
| 667 } |
| 668 this.ports.clear(); |
| 669 this.weakPorts.clear(); |
| 670 exports._globalState.isolates.remove(this.id); |
| 671 this.errorPorts.clear(); |
| 672 if (this.doneHandlers !== null) { |
| 673 for (let port of this.doneHandlers) { |
| 674 port.send(null); |
| 675 } |
| 676 this.doneHandlers = null; |
| 677 } |
| 678 } |
| 679 unregister(portId) { |
| 680 this.ports.remove(portId); |
| 681 this.weakPorts.remove(portId); |
| 682 this[_updateGlobalState](); |
| 683 } |
| 684 } |
| 685 let _runHelper = Symbol('_runHelper'); |
| 686 class _EventLoop extends core.Object { |
| 687 _EventLoop() { |
| 688 this.events = new collection.Queue(); |
| 689 this[_activeJsAsyncCount] = 0; |
| 690 } |
| 691 enqueue(isolate, fn, msg) { |
| 692 this.events.addLast(new _IsolateEvent(dart.as(isolate, _IsolateContext), d
art.as(fn, core.Function), dart.as(msg, core.String))); |
| 693 } |
| 694 prequeue(event) { |
| 695 this.events.addFirst(event); |
| 696 } |
| 697 dequeue() { |
| 698 if (this.events.isEmpty) |
| 699 return null; |
| 700 return this.events.removeFirst(); |
| 701 } |
| 702 checkOpenReceivePortsFromCommandLine() { |
| 703 if (dart.notNull(exports._globalState.rootContext !== null) && dart.notNul
l(exports._globalState.isolates.containsKey(exports._globalState.rootContext.id)
) && dart.notNull(exports._globalState.fromCommandLine) && dart.notNull(exports.
_globalState.rootContext.ports.isEmpty)) { |
| 704 throw new core.Exception("Program exited with open ReceivePorts."); |
| 705 } |
| 706 } |
| 707 runIteration() { |
| 708 let event = this.dequeue(); |
| 709 if (event === null) { |
| 710 this.checkOpenReceivePortsFromCommandLine(); |
| 711 exports._globalState.maybeCloseWorker(); |
| 712 return false; |
| 713 } |
| 714 event.process(); |
| 715 return true; |
| 716 } |
| 717 [_runHelper]() { |
| 718 if (exports.globalWindow !== null) { |
| 719 // Function next: () → void |
| 720 function next() { |
| 721 if (!dart.notNull(this.runIteration())) |
| 722 return; |
| 723 async.Timer.run(next); |
| 724 } |
| 725 next(); |
| 726 } else { |
| 727 while (this.runIteration()) { |
| 728 } |
| 729 } |
| 730 } |
| 731 run() { |
| 732 if (!dart.notNull(exports._globalState.isWorker)) { |
| 733 this[_runHelper](); |
| 734 } else { |
| 735 try { |
| 736 this[_runHelper](); |
| 737 } catch (e) { |
| 738 let trace = dart.stackTrace(e); |
| 739 exports._globalState.mainManager.postMessage(_serializeMessage(dart.ma
p({command: 'error', msg: `${e}\n${trace}`}))); |
| 740 } |
| 741 |
| 742 } |
| 743 } |
| 744 } |
| 745 class _IsolateEvent extends core.Object { |
| 746 _IsolateEvent(isolate, fn, message) { |
| 747 this.isolate = isolate; |
| 748 this.fn = fn; |
| 749 this.message = message; |
| 750 } |
| 751 process() { |
| 752 if (this.isolate.isPaused) { |
| 753 this.isolate.delayedEvents.add(this); |
| 754 return; |
| 755 } |
| 756 this.isolate.eval(this.fn); |
| 757 } |
| 758 } |
| 759 class _MainManagerStub extends core.Object { |
| 760 postMessage(msg) { |
| 761 _js_helper.requiresPreamble(); |
| 762 self.postMessage(msg); |
| 763 } |
| 764 } |
| 765 let _SPAWNED_SIGNAL = "spawned"; |
| 766 let _SPAWN_FAILED_SIGNAL = "spawn failed"; |
| 767 dart.copyProperties(exports, { |
| 768 get globalWindow() { |
| 769 _js_helper.requiresPreamble(); |
| 770 return self.window; |
| 771 }, |
| 772 get globalWorker() { |
| 773 _js_helper.requiresPreamble(); |
| 774 return self.Worker; |
| 775 }, |
| 776 get globalPostMessageDefined() { |
| 777 _js_helper.requiresPreamble(); |
| 778 return !!self.postMessage; |
| 779 } |
| 780 }); |
| 781 let _getEventData = Symbol('_getEventData'); |
| 782 let _log = Symbol('_log'); |
| 783 let _consoleLog = Symbol('_consoleLog'); |
| 784 let _getJSFunctionFromName = Symbol('_getJSFunctionFromName'); |
| 785 let _getJSFunctionName = Symbol('_getJSFunctionName'); |
| 786 let _allocate = Symbol('_allocate'); |
| 787 let _startWorker = Symbol('_startWorker'); |
| 788 let _startNonWorker = Symbol('_startNonWorker'); |
| 789 let _startIsolate = Symbol('_startIsolate'); |
| 790 let _spawnWorker = Symbol('_spawnWorker'); |
| 791 class IsolateNatives extends core.Object { |
| 792 static computeThisScript() { |
| 793 let currentScript = _foreign_helper.JS_EMBEDDED_GLOBAL('', dart.as(_js_emb
edded_names.CURRENT_SCRIPT, core.String)); |
| 794 if (currentScript !== null) { |
| 795 return String(currentScript.src); |
| 796 } |
| 797 if (_js_helper.Primitives.isD8) |
| 798 return computeThisScriptD8(); |
| 799 if (_js_helper.Primitives.isJsshell) |
| 800 return computeThisScriptJsshell(); |
| 801 if (exports._globalState.isWorker) |
| 802 return computeThisScriptFromTrace(); |
| 803 return null; |
| 804 } |
| 805 static computeThisScriptJsshell() { |
| 806 return dart.as(thisFilename(), core.String); |
| 807 } |
| 808 static computeThisScriptD8() { |
| 809 return computeThisScriptFromTrace(); |
| 810 } |
| 811 static computeThisScriptFromTrace() { |
| 812 let stack = new Error().stack; |
| 813 if (stack === null) { |
| 814 stack = function() { |
| 815 try { |
| 816 throw new Error(); |
| 817 } catch (e) { |
| 818 return e.stack; |
| 819 } |
| 820 |
| 821 }(); |
| 822 if (stack === null) |
| 823 throw new core.UnsupportedError('No stack trace'); |
| 824 } |
| 825 let pattern = null, matches = null; |
| 826 pattern = new RegExp("^ *at [^(]*\\((.*):[0-9]*:[0-9]*\\)$", "m"); |
| 827 matches = stack.match(pattern); |
| 828 if (matches !== null) |
| 829 return matches[1]; |
| 830 pattern = new RegExp("^[^@]*@(.*):[0-9]*$", "m"); |
| 831 matches = stack.match(pattern); |
| 832 if (matches !== null) |
| 833 return matches[1]; |
| 834 throw new core.UnsupportedError(`Cannot extract URI from "${stack}"`); |
| 835 } |
| 836 static [_getEventData](e) { |
| 837 return e.data; |
| 838 } |
| 839 static [_processWorkerMessage](sender, e) { |
| 840 let msg = _deserializeMessage(_getEventData(e)); |
| 841 switch (dart.dindex(msg, 'command')) { |
| 842 case 'start': |
| 843 exports._globalState.currentManagerId = dart.as(dart.dindex(msg, 'id')
, core.int); |
| 844 let functionName = dart.as(dart.dindex(msg, 'functionName'), core.Stri
ng); |
| 845 let entryPoint = dart.as(functionName === null ? exports._globalState.
entry : _getJSFunctionFromName(functionName), core.Function); |
| 846 let args = dart.dindex(msg, 'args'); |
| 847 let message = _deserializeMessage(dart.dindex(msg, 'msg')); |
| 848 let isSpawnUri = dart.dindex(msg, 'isSpawnUri'); |
| 849 let startPaused = dart.dindex(msg, 'startPaused'); |
| 850 let replyTo = _deserializeMessage(dart.dindex(msg, 'replyTo')); |
| 851 let context = new _IsolateContext(); |
| 852 exports._globalState.topEventLoop.enqueue(context, () => { |
| 853 _startIsolate(entryPoint, dart.as(args, core.List$(core.String)), me
ssage, dart.as(isSpawnUri, core.bool), dart.as(startPaused, core.bool), dart.as(
replyTo, isolate.SendPort)); |
| 854 }, 'worker-start'); |
| 855 exports._globalState.currentContext = context; |
| 856 exports._globalState.topEventLoop.run(); |
| 857 break; |
| 858 case 'spawn-worker': |
| 859 if (enableSpawnWorker !== null) |
| 860 handleSpawnWorkerRequest(msg); |
| 861 break; |
| 862 case 'message': |
| 863 let port = dart.as(dart.dindex(msg, 'port'), isolate.SendPort); |
| 864 if (port !== null) { |
| 865 dart.dinvoke(dart.dindex(msg, 'port'), 'send', dart.dindex(msg, 'msg
')); |
| 866 } |
| 867 exports._globalState.topEventLoop.run(); |
| 868 break; |
| 869 case 'close': |
| 870 exports._globalState.managers.remove(workerIds.get(sender)); |
| 871 sender.terminate(); |
| 872 exports._globalState.topEventLoop.run(); |
| 873 break; |
| 874 case 'log': |
| 875 _log(dart.dindex(msg, 'msg')); |
| 876 break; |
| 877 case 'print': |
| 878 if (exports._globalState.isWorker) { |
| 879 exports._globalState.mainManager.postMessage(_serializeMessage(dart.
map({command: 'print', msg: msg}))); |
| 880 } else { |
| 881 core.print(dart.dindex(msg, 'msg')); |
| 882 } |
| 883 break; |
| 884 case 'error': |
| 885 throw dart.dindex(msg, 'msg'); |
| 886 } |
| 887 } |
| 888 static handleSpawnWorkerRequest(msg) { |
| 889 let replyPort = dart.dindex(msg, 'replyPort'); |
| 890 spawn(dart.as(dart.dindex(msg, 'functionName'), core.String), dart.as(dart
.dindex(msg, 'uri'), core.String), dart.as(dart.dindex(msg, 'args'), core.List$(
core.String)), dart.dindex(msg, 'msg'), false, dart.as(dart.dindex(msg, 'isSpawn
Uri'), core.bool), dart.as(dart.dindex(msg, 'startPaused'), core.bool)).then((ms
g) => { |
| 891 dart.dinvoke(replyPort, 'send', msg); |
| 892 }, { |
| 893 onError: (errorMessage) => { |
| 894 dart.dinvoke(replyPort, 'send', new List.from([_SPAWN_FAILED_SIGNAL, e
rrorMessage])); |
| 895 } |
| 896 }); |
| 897 } |
| 898 static [_log](msg) { |
| 899 if (exports._globalState.isWorker) { |
| 900 exports._globalState.mainManager.postMessage(_serializeMessage(dart.map(
{command: 'log', msg: msg}))); |
| 901 } else { |
| 902 try { |
| 903 _consoleLog(msg); |
| 904 } catch (e) { |
| 905 let trace = dart.stackTrace(e); |
| 906 throw new core.Exception(trace); |
| 907 } |
| 908 |
| 909 } |
| 910 } |
| 911 static [_consoleLog](msg) { |
| 912 _js_helper.requiresPreamble(); |
| 913 self.console.log(msg); |
| 914 } |
| 915 static [_getJSFunctionFromName](functionName) { |
| 916 let globalFunctionsContainer = _foreign_helper.JS_EMBEDDED_GLOBAL("", dart
.as(_js_embedded_names.GLOBAL_FUNCTIONS, core.String)); |
| 917 return globalFunctionsContainer[functionName](); |
| 918 } |
| 919 static [_getJSFunctionName](f) { |
| 920 return dart.as(dart.is(f, _js_helper.Closure) ? f.$name : null, core.Strin
g); |
| 921 } |
| 922 static [_allocate](ctor) { |
| 923 return new ctor(); |
| 924 } |
| 925 static spawnFunction(topLevelFunction, message, startPaused) { |
| 926 IsolateNatives.enableSpawnWorker = true; |
| 927 let name = _getJSFunctionName(topLevelFunction); |
| 928 if (name === null) { |
| 929 throw new core.UnsupportedError("only top-level functions can be spawned
."); |
| 930 } |
| 931 let isLight = false; |
| 932 let isSpawnUri = false; |
| 933 return spawn(name, null, null, message, isLight, isSpawnUri, startPaused); |
| 934 } |
| 935 static spawnUri(uri, args, message, startPaused) { |
| 936 IsolateNatives.enableSpawnWorker = true; |
| 937 let isLight = false; |
| 938 let isSpawnUri = true; |
| 939 return spawn(null, uri.toString(), args, message, isLight, isSpawnUri, sta
rtPaused); |
| 940 } |
| 941 static spawn(functionName, uri, args, message, isLight, isSpawnUri, startPau
sed) { |
| 942 if (dart.notNull(uri !== null) && dart.notNull(uri.endsWith(".dart"))) |
| 943 uri = ".js"; |
| 944 let port = new isolate.ReceivePort(); |
| 945 let completer = dart.as(new async.Completer(), async.Completer$(core.List)
); |
| 946 port.first.then(((msg) => { |
| 947 if (dart.equals(dart.dindex(msg, 0), _SPAWNED_SIGNAL)) { |
| 948 completer.complete(msg); |
| 949 } else { |
| 950 dart.assert(dart.equals(dart.dindex(msg, 0), _SPAWN_FAILED_SIGNAL)); |
| 951 completer.completeError(dart.dindex(msg, 1)); |
| 952 } |
| 953 }).bind(this)); |
| 954 let signalReply = port.sendPort; |
| 955 if (dart.notNull(exports._globalState.useWorkers) && !dart.notNull(isLight
)) { |
| 956 _startWorker(functionName, uri, args, message, isSpawnUri, startPaused,
signalReply, ((message) => completer.completeError(message)).bind(this)); |
| 957 } else { |
| 958 _startNonWorker(functionName, uri, args, message, isSpawnUri, startPause
d, signalReply); |
| 959 } |
| 960 return completer.future; |
| 961 } |
| 962 static [_startWorker](functionName, uri, args, message, isSpawnUri, startPau
sed, replyPort, onError) { |
| 963 if (args !== null) |
| 964 args = new core.List.from(args); |
| 965 if (exports._globalState.isWorker) { |
| 966 exports._globalState.mainManager.postMessage(_serializeMessage(dart.map(
{command: 'spawn-worker', functionName: functionName, args: args, msg: message,
uri: uri, isSpawnUri: isSpawnUri, startPaused: startPaused, replyPort: replyPort
}))); |
| 967 } else { |
| 968 _spawnWorker(functionName, uri, args, message, isSpawnUri, startPaused,
replyPort, onError); |
| 969 } |
| 970 } |
| 971 static [_startNonWorker](functionName, uri, args, message, isSpawnUri, start
Paused, replyPort) { |
| 972 if (uri !== null) { |
| 973 throw new core.UnsupportedError("Currently spawnUri is not supported wit
hout web workers."); |
| 974 } |
| 975 message = _clone(message); |
| 976 if (args !== null) |
| 977 args = new core.List.from(args); |
| 978 exports._globalState.topEventLoop.enqueue(new _IsolateContext(), () => { |
| 979 let func = _getJSFunctionFromName(functionName); |
| 980 _startIsolate(dart.as(func, core.Function), args, message, isSpawnUri, s
tartPaused, replyPort); |
| 981 }, 'nonworker start'); |
| 982 } |
| 983 static get currentIsolate() { |
| 984 let context = dart.as(_foreign_helper.JS_CURRENT_ISOLATE_CONTEXT(), _Isola
teContext); |
| 985 return new isolate.Isolate(context.controlPort.sendPort, {pauseCapability:
context.pauseCapability, terminateCapability: context.terminateCapability}); |
| 986 } |
| 987 static [_startIsolate](topLevel, args, message, isSpawnUri, startPaused, rep
lyTo) { |
| 988 let context = dart.as(_foreign_helper.JS_CURRENT_ISOLATE_CONTEXT(), _Isola
teContext); |
| 989 _js_helper.Primitives.initializeStatics(context.id); |
| 990 replyTo.send(new List.from([_SPAWNED_SIGNAL, context.controlPort.sendPort,
context.pauseCapability, context.terminateCapability])); |
| 991 // Function runStartFunction: () → void |
| 992 function runStartFunction() { |
| 993 context.initialized = true; |
| 994 if (!dart.notNull(isSpawnUri)) { |
| 995 dart.dinvokef(topLevel, message); |
| 996 } else if (dart.is(topLevel, _MainFunctionArgsMessage)) { |
| 997 dart.dinvokef(topLevel, args, message); |
| 998 } else if (dart.is(topLevel, _MainFunctionArgs)) { |
| 999 dart.dinvokef(topLevel, args); |
| 1000 } else { |
| 1001 dart.dinvokef(topLevel); |
| 1002 } |
| 1003 } |
| 1004 if (startPaused) { |
| 1005 context.addPause(context.pauseCapability, context.pauseCapability); |
| 1006 exports._globalState.topEventLoop.enqueue(context, runStartFunction, 'st
art isolate'); |
| 1007 } else { |
| 1008 runStartFunction(); |
| 1009 } |
| 1010 } |
| 1011 static [_spawnWorker](functionName, uri, args, message, isSpawnUri, startPau
sed, replyPort, onError) { |
| 1012 if (uri === null) |
| 1013 uri = thisScript; |
| 1014 let worker = new Worker(uri); |
| 1015 let onerrorTrampoline = function(f, u, c) { |
| 1016 return function(e) { |
| 1017 return f(e, u, c); |
| 1018 }; |
| 1019 }(_foreign_helper.DART_CLOSURE_TO_JS(workerOnError), uri, onError); |
| 1020 worker.onerror = onerrorTrampoline; |
| 1021 let processWorkerMessageTrampoline = function(f, a) { |
| 1022 return function(e) { |
| 1023 e.onerror = null; |
| 1024 return f(a, e); |
| 1025 }; |
| 1026 }(_foreign_helper.DART_CLOSURE_TO_JS(_processWorkerMessage), worker); |
| 1027 worker.onmessage = processWorkerMessageTrampoline; |
| 1028 let workerId = (($tmp) => exports._globalState.nextManagerId = dart.notNul
l($tmp) + 1, $tmp).bind(this)(exports._globalState.nextManagerId); |
| 1029 workerIds.set(worker, workerId); |
| 1030 exports._globalState.managers.set(workerId, worker); |
| 1031 worker.postMessage(_serializeMessage(dart.map({command: 'start', id: worke
rId, replyTo: _serializeMessage(replyPort), args: args, msg: _serializeMessage(m
essage), isSpawnUri: isSpawnUri, startPaused: startPaused, functionName: functio
nName}))); |
| 1032 } |
| 1033 static workerOnError(event, uri, onError) { |
| 1034 event.preventDefault(); |
| 1035 let message = dart.as(event.message, core.String); |
| 1036 if (message === null) { |
| 1037 message = `Error spawning worker for ${uri}`; |
| 1038 } else { |
| 1039 message = `Error spawning worker for ${uri} (${message})`; |
| 1040 } |
| 1041 onError(message); |
| 1042 return true; |
| 1043 } |
| 1044 } |
| 1045 IsolateNatives.enableSpawnWorker = null; |
| 1046 dart.defineLazyProperties(IsolateNatives, { |
| 1047 get thisScript() { |
| 1048 return computeThisScript(); |
| 1049 }, |
| 1050 set thisScript(_) {}, |
| 1051 get workerIds() { |
| 1052 return new core.Expando(); |
| 1053 } |
| 1054 }); |
| 1055 let _checkReplyTo = Symbol('_checkReplyTo'); |
| 1056 class _BaseSendPort extends core.Object { |
| 1057 _BaseSendPort($_isolateId) { |
| 1058 this[_isolateId] = $_isolateId; |
| 1059 } |
| 1060 [_checkReplyTo](replyTo) { |
| 1061 if (dart.notNull(replyTo !== null) && dart.notNull(!dart.is(replyTo, _Nati
veJsSendPort)) && dart.notNull(!dart.is(replyTo, _WorkerSendPort))) { |
| 1062 throw new core.Exception("SendPort.send: Illegal replyTo port type"); |
| 1063 } |
| 1064 } |
| 1065 } |
| 1066 let _isClosed = Symbol('_isClosed'); |
| 1067 class _NativeJsSendPort extends _BaseSendPort { |
| 1068 _NativeJsSendPort($_receivePort, isolateId) { |
| 1069 this[_receivePort] = $_receivePort; |
| 1070 super._BaseSendPort(isolateId); |
| 1071 } |
| 1072 send(message) { |
| 1073 let isolate = exports._globalState.isolates.get(this[_isolateId]); |
| 1074 if (isolate === null) |
| 1075 return; |
| 1076 if (this[_receivePort][_isClosed]) |
| 1077 return; |
| 1078 let msg = _clone(message); |
| 1079 if (dart.equals(isolate.controlPort, this[_receivePort])) { |
| 1080 isolate.handleControlMessage(msg); |
| 1081 return; |
| 1082 } |
| 1083 exports._globalState.topEventLoop.enqueue(isolate, (() => { |
| 1084 if (!dart.notNull(this[_receivePort][_isClosed])) { |
| 1085 this[_receivePort]._add(msg); |
| 1086 } |
| 1087 }).bind(this), `receive ${message}`); |
| 1088 } |
| 1089 ['=='](other) { |
| 1090 return dart.notNull(dart.is(other, _NativeJsSendPort)) && dart.notNull(dar
t.equals(this[_receivePort], dart.dload(other, '_receivePort'))); |
| 1091 } |
| 1092 get hashCode() { |
| 1093 return this[_receivePort][_id]; |
| 1094 } |
| 1095 } |
| 1096 class _WorkerSendPort extends _BaseSendPort { |
| 1097 _WorkerSendPort($_workerId, isolateId, $_receivePortId) { |
| 1098 this[_workerId] = $_workerId; |
| 1099 this[_receivePortId] = $_receivePortId; |
| 1100 super._BaseSendPort(isolateId); |
| 1101 } |
| 1102 send(message) { |
| 1103 let workerMessage = _serializeMessage(dart.map({command: 'message', port:
this, msg: message})); |
| 1104 if (exports._globalState.isWorker) { |
| 1105 exports._globalState.mainManager.postMessage(workerMessage); |
| 1106 } else { |
| 1107 let manager = exports._globalState.managers.get(this[_workerId]); |
| 1108 if (manager !== null) { |
| 1109 manager.postMessage(workerMessage); |
| 1110 } |
| 1111 } |
| 1112 } |
| 1113 ['=='](other) { |
| 1114 return dart.notNull(dart.is(other, _WorkerSendPort)) && this[_workerId] ==
= dart.dload(other, '_workerId') && this[_isolateId] === dart.dload(other, '_iso
lateId') && this[_receivePortId] === dart.dload(other, '_receivePortId'); |
| 1115 } |
| 1116 get hashCode() { |
| 1117 return dart.notNull(this[_workerId]) << 16 ^ dart.notNull(this[_isolateId]
) << 8 ^ dart.notNull(this[_receivePortId]); |
| 1118 } |
| 1119 } |
| 1120 let _handler = Symbol('_handler'); |
| 1121 let _close = Symbol('_close'); |
| 1122 let _add = Symbol('_add'); |
| 1123 class RawReceivePortImpl extends core.Object { |
| 1124 RawReceivePortImpl($_handler) { |
| 1125 this[_handler] = $_handler; |
| 1126 this[_id] = (($tmp) => _nextFreeId = dart.notNull($tmp) + 1, $tmp)(_nextFr
eeId); |
| 1127 this[_isClosed] = false; |
| 1128 exports._globalState.currentContext.register(this[_id], this); |
| 1129 } |
| 1130 RawReceivePortImpl$weak($_handler) { |
| 1131 this[_handler] = $_handler; |
| 1132 this[_id] = (($tmp) => _nextFreeId = dart.notNull($tmp) + 1, $tmp)(_nextFr
eeId); |
| 1133 this[_isClosed] = false; |
| 1134 exports._globalState.currentContext.registerWeak(this[_id], this); |
| 1135 } |
| 1136 RawReceivePortImpl$_controlPort() { |
| 1137 this[_handler] = null; |
| 1138 this[_id] = 0; |
| 1139 this[_isClosed] = false; |
| 1140 } |
| 1141 set handler(newHandler) { |
| 1142 this[_handler] = newHandler; |
| 1143 } |
| 1144 [_close]() { |
| 1145 this[_isClosed] = true; |
| 1146 this[_handler] = null; |
| 1147 } |
| 1148 close() { |
| 1149 if (this[_isClosed]) |
| 1150 return; |
| 1151 this[_isClosed] = true; |
| 1152 this[_handler] = null; |
| 1153 exports._globalState.currentContext.unregister(this[_id]); |
| 1154 } |
| 1155 [_add](dataEvent) { |
| 1156 if (this[_isClosed]) |
| 1157 return; |
| 1158 dart.dinvokef(this[_handler], dataEvent); |
| 1159 } |
| 1160 get sendPort() { |
| 1161 return new _NativeJsSendPort(this, exports._globalState.currentContext.id)
; |
| 1162 } |
| 1163 } |
| 1164 dart.defineNamedConstructor(RawReceivePortImpl, 'weak'); |
| 1165 dart.defineNamedConstructor(RawReceivePortImpl, '_controlPort'); |
| 1166 RawReceivePortImpl._nextFreeId = 1; |
| 1167 let _rawPort = Symbol('_rawPort'); |
| 1168 let _controller = Symbol('_controller'); |
| 1169 class ReceivePortImpl extends async.Stream { |
| 1170 ReceivePortImpl() { |
| 1171 this.ReceivePortImpl$fromRawReceivePort(new RawReceivePortImpl(null)); |
| 1172 } |
| 1173 ReceivePortImpl$weak() { |
| 1174 this.ReceivePortImpl$fromRawReceivePort(new RawReceivePortImpl.weak(null))
; |
| 1175 } |
| 1176 ReceivePortImpl$fromRawReceivePort($_rawPort) { |
| 1177 this[_rawPort] = $_rawPort; |
| 1178 this[_controller] = null; |
| 1179 super.Stream(); |
| 1180 this[_controller] = new async.StreamController({onCancel: this.close, sync
: true}); |
| 1181 this[_rawPort].handler = this[_controller].add; |
| 1182 } |
| 1183 listen(onData, opt$) { |
| 1184 let onError = opt$.onError === void 0 ? null : opt$.onError; |
| 1185 let onDone = opt$.onDone === void 0 ? null : opt$.onDone; |
| 1186 let cancelOnError = opt$.cancelOnError === void 0 ? null : opt$.cancelOnEr
ror; |
| 1187 return this[_controller].stream.listen(onData, {onError: onError, onDone:
onDone, cancelOnError: cancelOnError}); |
| 1188 } |
| 1189 close() { |
| 1190 this[_rawPort].close(); |
| 1191 this[_controller].close(); |
| 1192 } |
| 1193 get sendPort() { |
| 1194 return this[_rawPort].sendPort; |
| 1195 } |
| 1196 } |
| 1197 dart.defineNamedConstructor(ReceivePortImpl, 'weak'); |
| 1198 dart.defineNamedConstructor(ReceivePortImpl, 'fromRawReceivePort'); |
| 1199 let _once = Symbol('_once'); |
| 1200 let _inEventLoop = Symbol('_inEventLoop'); |
| 1201 let _handle = Symbol('_handle'); |
| 1202 class TimerImpl extends core.Object { |
| 1203 TimerImpl(milliseconds, callback) { |
| 1204 this[_once] = true; |
| 1205 this[_inEventLoop] = false; |
| 1206 this[_handle] = null; |
| 1207 if (milliseconds === 0 && (!dart.notNull(hasTimer()) || dart.notNull(expor
ts._globalState.isWorker))) { |
| 1208 // Function internalCallback: () → void |
| 1209 function internalCallback() { |
| 1210 this[_handle] = null; |
| 1211 callback(); |
| 1212 } |
| 1213 this[_handle] = 1; |
| 1214 exports._globalState.topEventLoop.enqueue(exports._globalState.currentCo
ntext, internalCallback, 'timer'); |
| 1215 this[_inEventLoop] = true; |
| 1216 } else if (hasTimer()) { |
| 1217 // Function internalCallback: () → void |
| 1218 function internalCallback() { |
| 1219 this[_handle] = null; |
| 1220 leaveJsAsync(); |
| 1221 callback(); |
| 1222 } |
| 1223 enterJsAsync(); |
| 1224 this[_handle] = self.setTimeout(_js_helper.convertDartClosureToJS(intern
alCallback, 0), milliseconds); |
| 1225 } else { |
| 1226 dart.assert(dart.notNull(milliseconds) > 0); |
| 1227 throw new core.UnsupportedError("Timer greater than 0."); |
| 1228 } |
| 1229 } |
| 1230 TimerImpl$periodic(milliseconds, callback) { |
| 1231 this[_once] = false; |
| 1232 this[_inEventLoop] = false; |
| 1233 this[_handle] = null; |
| 1234 if (hasTimer()) { |
| 1235 enterJsAsync(); |
| 1236 this[_handle] = self.setInterval(_js_helper.convertDartClosureToJS((() =
> { |
| 1237 callback(this); |
| 1238 }).bind(this), 0), milliseconds); |
| 1239 } else { |
| 1240 throw new core.UnsupportedError("Periodic timer."); |
| 1241 } |
| 1242 } |
| 1243 cancel() { |
| 1244 if (hasTimer()) { |
| 1245 if (this[_inEventLoop]) { |
| 1246 throw new core.UnsupportedError("Timer in event loop cannot be cancele
d."); |
| 1247 } |
| 1248 if (this[_handle] === null) |
| 1249 return; |
| 1250 leaveJsAsync(); |
| 1251 if (this[_once]) { |
| 1252 self.clearTimeout(this[_handle]); |
| 1253 } else { |
| 1254 self.clearInterval(this[_handle]); |
| 1255 } |
| 1256 this[_handle] = null; |
| 1257 } else { |
| 1258 throw new core.UnsupportedError("Canceling a timer."); |
| 1259 } |
| 1260 } |
| 1261 get isActive() { |
| 1262 return this[_handle] !== null; |
| 1263 } |
| 1264 } |
| 1265 dart.defineNamedConstructor(TimerImpl, 'periodic'); |
| 1266 // Function hasTimer: () → bool |
| 1267 function hasTimer() { |
| 1268 _js_helper.requiresPreamble(); |
| 1269 return self.setTimeout !== null; |
| 1270 } |
| 1271 class CapabilityImpl extends core.Object { |
| 1272 CapabilityImpl() { |
| 1273 this.CapabilityImpl$_internal(_js_helper.random64()); |
| 1274 } |
| 1275 CapabilityImpl$_internal($_id) { |
| 1276 this[_id] = $_id; |
| 1277 } |
| 1278 get hashCode() { |
| 1279 let hash = this[_id]; |
| 1280 hash = dart.notNull(hash) >> 0 ^ (dart.notNull(hash) / 4294967296).truncat
e(); |
| 1281 hash = ~dart.notNull(hash) + (dart.notNull(hash) << 15) & 4294967295; |
| 1282 hash = dart.notNull(hash) >> 12; |
| 1283 hash = dart.notNull(hash) * 5 & 4294967295; |
| 1284 hash = dart.notNull(hash) >> 4; |
| 1285 hash = dart.notNull(hash) * 2057 & 4294967295; |
| 1286 hash = dart.notNull(hash) >> 16; |
| 1287 return hash; |
| 1288 } |
| 1289 ['=='](other) { |
| 1290 if (core.identical(other, this)) |
| 1291 return true; |
| 1292 if (dart.is(other, CapabilityImpl)) { |
| 1293 return core.identical(this[_id], other[_id]); |
| 1294 } |
| 1295 return false; |
| 1296 } |
| 1297 } |
| 1298 dart.defineNamedConstructor(CapabilityImpl, '_internal'); |
| 1299 // Exports: | 1299 // Exports: |
| 1300 exports.enterJsAsync = enterJsAsync; | 1300 exports.enterJsAsync = enterJsAsync; |
| 1301 exports.leaveJsAsync = leaveJsAsync; | 1301 exports.leaveJsAsync = leaveJsAsync; |
| 1302 exports.isWorker = isWorker; | 1302 exports.isWorker = isWorker; |
| 1303 exports.startRootIsolate = startRootIsolate; | 1303 exports.startRootIsolate = startRootIsolate; |
| 1304 exports.globalWindow = globalWindow; | 1304 exports.globalWindow = globalWindow; |
| 1305 exports.globalWorker = globalWorker; | 1305 exports.globalWorker = globalWorker; |
| 1306 exports.globalPostMessageDefined = globalPostMessageDefined; | 1306 exports.globalPostMessageDefined = globalPostMessageDefined; |
| 1307 exports.IsolateNatives = IsolateNatives; | 1307 exports.IsolateNatives = IsolateNatives; |
| 1308 exports.RawReceivePortImpl = RawReceivePortImpl; | 1308 exports.RawReceivePortImpl = RawReceivePortImpl; |
| 1309 exports.ReceivePortImpl = ReceivePortImpl; | 1309 exports.ReceivePortImpl = ReceivePortImpl; |
| 1310 exports.TimerImpl = TimerImpl; | 1310 exports.TimerImpl = TimerImpl; |
| 1311 exports.hasTimer = hasTimer; | 1311 exports.hasTimer = hasTimer; |
| 1312 exports.CapabilityImpl = CapabilityImpl; | 1312 exports.CapabilityImpl = CapabilityImpl; |
| 1313 })(_isolate_helper || (_isolate_helper = {})); | 1313 })(_isolate_helper || (_isolate_helper = {})); |
| OLD | NEW |