| OLD | NEW |
| (Empty) |
| 1 dart_library.library('dart/_isolate_helper', null, /* Imports */[ | |
| 2 'dart/_runtime', | |
| 3 'dart/core', | |
| 4 'dart/_native_typed_data', | |
| 5 'dart/_interceptors', | |
| 6 'dart/_js_helper', | |
| 7 'dart/isolate', | |
| 8 'dart/_foreign_helper', | |
| 9 'dart/_js_embedded_names', | |
| 10 'dart/collection', | |
| 11 'dart/async' | |
| 12 ], /* Lazy imports */[ | |
| 13 ], function(exports, dart, core, _native_typed_data, _interceptors, _js_helper,
isolate, _foreign_helper, _js_embedded_names, collection, async) { | |
| 14 'use strict'; | |
| 15 let dartx = dart.dartx; | |
| 16 function _serializeMessage(message) { | |
| 17 return new _Serializer().serialize(message); | |
| 18 } | |
| 19 dart.fn(_serializeMessage); | |
| 20 function _deserializeMessage(message) { | |
| 21 return new _Deserializer().deserialize(message); | |
| 22 } | |
| 23 dart.fn(_deserializeMessage); | |
| 24 function _clone(message) { | |
| 25 let serializer = new _Serializer({serializeSendPorts: false}); | |
| 26 let deserializer = new _Deserializer(); | |
| 27 return deserializer.deserialize(serializer.serialize(message)); | |
| 28 } | |
| 29 dart.fn(_clone); | |
| 30 const _serializeSendPorts = Symbol('_serializeSendPorts'); | |
| 31 const _workerId = Symbol('_workerId'); | |
| 32 const _isolateId = Symbol('_isolateId'); | |
| 33 const _receivePortId = Symbol('_receivePortId'); | |
| 34 const _id = Symbol('_id'); | |
| 35 const _receivePort = Symbol('_receivePort'); | |
| 36 class _Serializer extends core.Object { | |
| 37 _Serializer(opts) { | |
| 38 let serializeSendPorts = opts && 'serializeSendPorts' in opts ? opts.seria
lizeSendPorts : true; | |
| 39 this.serializedObjectIds = core.Map$(dart.dynamic, core.int).identity(); | |
| 40 this[_serializeSendPorts] = dart.as(serializeSendPorts, core.bool); | |
| 41 } | |
| 42 serialize(x) { | |
| 43 if (dart.notNull(this.isPrimitive(x))) return this.serializePrimitive(x); | |
| 44 let serializationId = this.serializedObjectIds[dartx.get](x); | |
| 45 if (serializationId != null) return this.makeRef(serializationId); | |
| 46 serializationId = this.serializedObjectIds[dartx.length]; | |
| 47 this.serializedObjectIds[dartx.set](x, serializationId); | |
| 48 if (dart.is(x, _native_typed_data.NativeByteBuffer)) return this.serialize
ByteBuffer(x); | |
| 49 if (dart.is(x, _native_typed_data.NativeTypedData)) return this.serializeT
ypedData(x); | |
| 50 if (dart.is(x, _interceptors.JSIndexable)) return this.serializeJSIndexabl
e(x); | |
| 51 if (dart.is(x, _js_helper.InternalMap)) return this.serializeMap(x); | |
| 52 if (dart.is(x, _interceptors.JSObject)) return this.serializeJSObject(x); | |
| 53 if (dart.is(x, _interceptors.Interceptor)) this.unsupported(x); | |
| 54 if (dart.is(x, isolate.RawReceivePort)) { | |
| 55 this.unsupported(x, "RawReceivePorts can't be transmitted:"); | |
| 56 } | |
| 57 if (dart.is(x, _NativeJsSendPort)) return this.serializeJsSendPort(x); | |
| 58 if (dart.is(x, _WorkerSendPort)) return this.serializeWorkerSendPort(x); | |
| 59 if (dart.is(x, core.Function)) return this.serializeClosure(x); | |
| 60 return this.serializeDartObject(x); | |
| 61 } | |
| 62 unsupported(x, message) { | |
| 63 if (message === void 0) message = null; | |
| 64 if (message == null) message = "Can't transmit:"; | |
| 65 dart.throw(new core.UnsupportedError(`${message} ${x}`)); | |
| 66 } | |
| 67 makeRef(serializationId) { | |
| 68 return dart.list(["ref", serializationId], core.Object); | |
| 69 } | |
| 70 isPrimitive(x) { | |
| 71 return x == null || typeof x == 'string' || typeof x == 'number' || typeof
x == 'boolean'; | |
| 72 } | |
| 73 serializePrimitive(primitive) { | |
| 74 return primitive; | |
| 75 } | |
| 76 serializeByteBuffer(buffer) { | |
| 77 return dart.list(["buffer", buffer], core.Object); | |
| 78 } | |
| 79 serializeTypedData(data) { | |
| 80 return dart.list(["typed", data], core.Object); | |
| 81 } | |
| 82 serializeJSIndexable(indexable) { | |
| 83 dart.assert(!(typeof indexable == 'string')); | |
| 84 let serialized = dart.as(this.serializeArray(dart.as(indexable, _intercept
ors.JSArray)), core.List); | |
| 85 if (dart.is(indexable, _interceptors.JSFixedArray)) return dart.list(["fix
ed", serialized], core.Object); | |
| 86 if (dart.is(indexable, _interceptors.JSExtendableArray)) return dart.list(
["extendable", serialized], core.Object); | |
| 87 if (dart.is(indexable, _interceptors.JSMutableArray)) return dart.list(["m
utable", serialized], core.Object); | |
| 88 if (dart.is(indexable, _interceptors.JSArray)) return dart.list(["const",
serialized], core.Object); | |
| 89 this.unsupported(indexable, "Can't serialize indexable: "); | |
| 90 return null; | |
| 91 } | |
| 92 serializeArray(x) { | |
| 93 let serialized = []; | |
| 94 serialized[dartx.length] = x[dartx.length]; | |
| 95 for (let i = 0; i < dart.notNull(x[dartx.length]); i++) { | |
| 96 serialized[dartx.set](i, this.serialize(x[dartx.get](i))); | |
| 97 } | |
| 98 return serialized; | |
| 99 } | |
| 100 serializeArrayInPlace(x) { | |
| 101 for (let i = 0; i < dart.notNull(x[dartx.length]); i++) { | |
| 102 x[dartx.set](i, this.serialize(x[dartx.get](i))); | |
| 103 } | |
| 104 return x; | |
| 105 } | |
| 106 serializeMap(x) { | |
| 107 let serializeTearOff = dart.bind(this, 'serialize'); | |
| 108 return dart.list(['map', x[dartx.keys][dartx.map](dart.as(serializeTearOff
, dart.functionType(dart.dynamic, [dart.dynamic])))[dartx.toList](), x[dartx.val
ues][dartx.map](dart.as(serializeTearOff, dart.functionType(dart.dynamic, [dart.
dynamic])))[dartx.toList]()], core.Object); | |
| 109 } | |
| 110 serializeJSObject(x) { | |
| 111 if (!!x.constructor && x.constructor !== Object) { | |
| 112 this.unsupported(x, "Only plain JS Objects are supported:"); | |
| 113 } | |
| 114 let keys = dart.as(Object.keys(x), core.List); | |
| 115 let values = []; | |
| 116 values[dartx.length] = keys[dartx.length]; | |
| 117 for (let i = 0; i < dart.notNull(keys[dartx.length]); i++) { | |
| 118 values[dartx.set](i, this.serialize(x[keys[dartx.get](i)])); | |
| 119 } | |
| 120 return dart.list(['js-object', keys, values], core.Object); | |
| 121 } | |
| 122 serializeWorkerSendPort(x) { | |
| 123 if (dart.notNull(this[_serializeSendPorts])) { | |
| 124 return dart.list(['sendport', x[_workerId], x[_isolateId], x[_receivePor
tId]], core.Object); | |
| 125 } | |
| 126 return dart.list(['raw sendport', x], core.Object); | |
| 127 } | |
| 128 serializeJsSendPort(x) { | |
| 129 if (dart.notNull(this[_serializeSendPorts])) { | |
| 130 let workerId = exports._globalState.currentManagerId; | |
| 131 return dart.list(['sendport', workerId, x[_isolateId], x[_receivePort][_
id]], core.Object); | |
| 132 } | |
| 133 return dart.list(['raw sendport', x], core.Object); | |
| 134 } | |
| 135 serializeCapability(x) { | |
| 136 return dart.list(['capability', x[_id]], core.Object); | |
| 137 } | |
| 138 serializeClosure(x) { | |
| 139 let name = IsolateNatives._getJSFunctionName(x); | |
| 140 if (name == null) { | |
| 141 this.unsupported(x, "Closures can't be transmitted:"); | |
| 142 } | |
| 143 return dart.list(['function', name], core.String); | |
| 144 } | |
| 145 serializeDartObject(x) { | |
| 146 let classExtractor = _foreign_helper.JS_EMBEDDED_GLOBAL('', _js_embedded_n
ames.CLASS_ID_EXTRACTOR); | |
| 147 let fieldsExtractor = _foreign_helper.JS_EMBEDDED_GLOBAL('', _js_embedded_
names.CLASS_FIELDS_EXTRACTOR); | |
| 148 let classId = classExtractor(x); | |
| 149 let fields = dart.as(fieldsExtractor(x), core.List); | |
| 150 return ['dart', classId, this.serializeArrayInPlace(dart.as(fields, _inter
ceptors.JSArray))]; | |
| 151 } | |
| 152 } | |
| 153 dart.setSignature(_Serializer, { | |
| 154 constructors: () => ({_Serializer: [_Serializer, [], {serializeSendPorts: da
rt.dynamic}]}), | |
| 155 methods: () => ({ | |
| 156 serialize: [dart.dynamic, [dart.dynamic]], | |
| 157 unsupported: [dart.void, [dart.dynamic], [core.String]], | |
| 158 makeRef: [dart.dynamic, [core.int]], | |
| 159 isPrimitive: [core.bool, [dart.dynamic]], | |
| 160 serializePrimitive: [dart.dynamic, [dart.dynamic]], | |
| 161 serializeByteBuffer: [dart.dynamic, [_native_typed_data.NativeByteBuffer]]
, | |
| 162 serializeTypedData: [dart.dynamic, [_native_typed_data.NativeTypedData]], | |
| 163 serializeJSIndexable: [dart.dynamic, [_interceptors.JSIndexable]], | |
| 164 serializeArray: [dart.dynamic, [_interceptors.JSArray]], | |
| 165 serializeArrayInPlace: [dart.dynamic, [_interceptors.JSArray]], | |
| 166 serializeMap: [dart.dynamic, [core.Map]], | |
| 167 serializeJSObject: [dart.dynamic, [_interceptors.JSObject]], | |
| 168 serializeWorkerSendPort: [dart.dynamic, [_WorkerSendPort]], | |
| 169 serializeJsSendPort: [dart.dynamic, [_NativeJsSendPort]], | |
| 170 serializeCapability: [dart.dynamic, [CapabilityImpl]], | |
| 171 serializeClosure: [dart.dynamic, [core.Function]], | |
| 172 serializeDartObject: [dart.dynamic, [dart.dynamic]] | |
| 173 }) | |
| 174 }); | |
| 175 const _adjustSendPorts = Symbol('_adjustSendPorts'); | |
| 176 class _Deserializer extends core.Object { | |
| 177 _Deserializer(opts) { | |
| 178 let adjustSendPorts = opts && 'adjustSendPorts' in opts ? opts.adjustSendP
orts : true; | |
| 179 this.deserializedObjects = core.List.new(); | |
| 180 this[_adjustSendPorts] = dart.as(adjustSendPorts, core.bool); | |
| 181 } | |
| 182 deserialize(x) { | |
| 183 if (dart.notNull(this.isPrimitive(x))) return this.deserializePrimitive(x)
; | |
| 184 if (!dart.is(x, _interceptors.JSArray)) dart.throw(new core.ArgumentError(
`Bad serialized message: ${x}`)); | |
| 185 switch (dart.dload(x, 'first')) { | |
| 186 case "ref": | |
| 187 { | |
| 188 return this.deserializeRef(x); | |
| 189 } | |
| 190 case "buffer": | |
| 191 { | |
| 192 return this.deserializeByteBuffer(x); | |
| 193 } | |
| 194 case "typed": | |
| 195 { | |
| 196 return this.deserializeTypedData(x); | |
| 197 } | |
| 198 case "fixed": | |
| 199 { | |
| 200 return this.deserializeFixed(x); | |
| 201 } | |
| 202 case "extendable": | |
| 203 { | |
| 204 return this.deserializeExtendable(x); | |
| 205 } | |
| 206 case "mutable": | |
| 207 { | |
| 208 return this.deserializeMutable(x); | |
| 209 } | |
| 210 case "const": | |
| 211 { | |
| 212 return this.deserializeConst(x); | |
| 213 } | |
| 214 case "map": | |
| 215 { | |
| 216 return this.deserializeMap(x); | |
| 217 } | |
| 218 case "sendport": | |
| 219 { | |
| 220 return this.deserializeSendPort(x); | |
| 221 } | |
| 222 case "raw sendport": | |
| 223 { | |
| 224 return this.deserializeRawSendPort(x); | |
| 225 } | |
| 226 case "js-object": | |
| 227 { | |
| 228 return this.deserializeJSObject(x); | |
| 229 } | |
| 230 case "function": | |
| 231 { | |
| 232 return this.deserializeClosure(x); | |
| 233 } | |
| 234 case "dart": | |
| 235 { | |
| 236 return this.deserializeDartObject(x); | |
| 237 } | |
| 238 default: | |
| 239 { | |
| 240 dart.throw(`couldn't deserialize: ${x}`); | |
| 241 } | |
| 242 } | |
| 243 } | |
| 244 isPrimitive(x) { | |
| 245 return x == null || typeof x == 'string' || typeof x == 'number' || typeof
x == 'boolean'; | |
| 246 } | |
| 247 deserializePrimitive(x) { | |
| 248 return x; | |
| 249 } | |
| 250 deserializeRef(x) { | |
| 251 dart.assert(dart.equals(dart.dindex(x, 0), 'ref')); | |
| 252 let serializationId = dart.as(dart.dindex(x, 1), core.int); | |
| 253 return this.deserializedObjects[dartx.get](serializationId); | |
| 254 } | |
| 255 deserializeByteBuffer(x) { | |
| 256 dart.assert(dart.equals(dart.dindex(x, 0), 'buffer')); | |
| 257 let result = dart.as(dart.dindex(x, 1), _native_typed_data.NativeByteBuffe
r); | |
| 258 this.deserializedObjects[dartx.add](result); | |
| 259 return result; | |
| 260 } | |
| 261 deserializeTypedData(x) { | |
| 262 dart.assert(dart.equals(dart.dindex(x, 0), 'typed')); | |
| 263 let result = dart.as(dart.dindex(x, 1), _native_typed_data.NativeTypedData
); | |
| 264 this.deserializedObjects[dartx.add](result); | |
| 265 return result; | |
| 266 } | |
| 267 deserializeArrayInPlace(x) { | |
| 268 for (let i = 0; i < dart.notNull(x[dartx.length]); i++) { | |
| 269 x[dartx.set](i, this.deserialize(x[dartx.get](i))); | |
| 270 } | |
| 271 return x; | |
| 272 } | |
| 273 deserializeFixed(x) { | |
| 274 dart.assert(dart.equals(dart.dindex(x, 0), 'fixed')); | |
| 275 let result = dart.as(dart.dindex(x, 1), core.List); | |
| 276 this.deserializedObjects[dartx.add](result); | |
| 277 return _interceptors.JSArray.markFixed(this.deserializeArrayInPlace(dart.a
s(result, _interceptors.JSArray))); | |
| 278 } | |
| 279 deserializeExtendable(x) { | |
| 280 dart.assert(dart.equals(dart.dindex(x, 0), 'extendable')); | |
| 281 let result = dart.as(dart.dindex(x, 1), core.List); | |
| 282 this.deserializedObjects[dartx.add](result); | |
| 283 return _interceptors.JSArray.markGrowable(this.deserializeArrayInPlace(dar
t.as(result, _interceptors.JSArray))); | |
| 284 } | |
| 285 deserializeMutable(x) { | |
| 286 dart.assert(dart.equals(dart.dindex(x, 0), 'mutable')); | |
| 287 let result = dart.as(dart.dindex(x, 1), core.List); | |
| 288 this.deserializedObjects[dartx.add](result); | |
| 289 return this.deserializeArrayInPlace(dart.as(result, _interceptors.JSArray)
); | |
| 290 } | |
| 291 deserializeConst(x) { | |
| 292 dart.assert(dart.equals(dart.dindex(x, 0), 'const')); | |
| 293 let result = dart.as(dart.dindex(x, 1), core.List); | |
| 294 this.deserializedObjects[dartx.add](result); | |
| 295 return _interceptors.JSArray.markFixed(this.deserializeArrayInPlace(dart.a
s(result, _interceptors.JSArray))); | |
| 296 } | |
| 297 deserializeMap(x) { | |
| 298 dart.assert(dart.equals(dart.dindex(x, 0), 'map')); | |
| 299 let keys = dart.as(dart.dindex(x, 1), core.List); | |
| 300 let values = dart.as(dart.dindex(x, 2), core.List); | |
| 301 let result = dart.map(); | |
| 302 this.deserializedObjects[dartx.add](result); | |
| 303 keys = keys[dartx.map](dart.bind(this, 'deserialize'))[dartx.toList](); | |
| 304 for (let i = 0; i < dart.notNull(keys[dartx.length]); i++) { | |
| 305 result[dartx.set](keys[dartx.get](i), this.deserialize(values[dartx.get]
(i))); | |
| 306 } | |
| 307 return result; | |
| 308 } | |
| 309 deserializeSendPort(x) { | |
| 310 dart.assert(dart.equals(dart.dindex(x, 0), 'sendport')); | |
| 311 let managerId = dart.as(dart.dindex(x, 1), core.int); | |
| 312 let isolateId = dart.as(dart.dindex(x, 2), core.int); | |
| 313 let receivePortId = dart.as(dart.dindex(x, 3), core.int); | |
| 314 let result = null; | |
| 315 if (managerId == exports._globalState.currentManagerId) { | |
| 316 let isolate = exports._globalState.isolates[dartx.get](isolateId); | |
| 317 if (isolate == null) return null; | |
| 318 let receivePort = isolate.lookup(receivePortId); | |
| 319 if (receivePort == null) return null; | |
| 320 result = new _NativeJsSendPort(receivePort, isolateId); | |
| 321 } else { | |
| 322 result = new _WorkerSendPort(managerId, isolateId, receivePortId); | |
| 323 } | |
| 324 this.deserializedObjects[dartx.add](result); | |
| 325 return result; | |
| 326 } | |
| 327 deserializeRawSendPort(x) { | |
| 328 dart.assert(dart.equals(dart.dindex(x, 0), 'raw sendport')); | |
| 329 let result = dart.as(dart.dindex(x, 1), isolate.SendPort); | |
| 330 this.deserializedObjects[dartx.add](result); | |
| 331 return result; | |
| 332 } | |
| 333 deserializeJSObject(x) { | |
| 334 dart.assert(dart.equals(dart.dindex(x, 0), 'js-object')); | |
| 335 let keys = dart.as(dart.dindex(x, 1), core.List); | |
| 336 let values = dart.as(dart.dindex(x, 2), core.List); | |
| 337 let o = {}; | |
| 338 this.deserializedObjects[dartx.add](o); | |
| 339 for (let i = 0; i < dart.notNull(keys[dartx.length]); i++) { | |
| 340 o[keys[dartx.get](i)] = this.deserialize(values[dartx.get](i)); | |
| 341 } | |
| 342 return o; | |
| 343 } | |
| 344 deserializeClosure(x) { | |
| 345 dart.assert(dart.equals(dart.dindex(x, 0), 'function')); | |
| 346 let name = dart.as(dart.dindex(x, 1), core.String); | |
| 347 let result = dart.as(IsolateNatives._getJSFunctionFromName(name), core.Fun
ction); | |
| 348 this.deserializedObjects[dartx.add](result); | |
| 349 return result; | |
| 350 } | |
| 351 deserializeDartObject(x) { | |
| 352 dart.assert(dart.equals(dart.dindex(x, 0), 'dart')); | |
| 353 let classId = dart.as(dart.dindex(x, 1), core.String); | |
| 354 let fields = dart.as(dart.dindex(x, 2), core.List); | |
| 355 let instanceFromClassId = _foreign_helper.JS_EMBEDDED_GLOBAL('', _js_embed
ded_names.INSTANCE_FROM_CLASS_ID); | |
| 356 let initializeObject = _foreign_helper.JS_EMBEDDED_GLOBAL('', _js_embedded
_names.INITIALIZE_EMPTY_INSTANCE); | |
| 357 let emptyInstance = instanceFromClassId(classId); | |
| 358 this.deserializedObjects[dartx.add](emptyInstance); | |
| 359 this.deserializeArrayInPlace(dart.as(fields, _interceptors.JSArray)); | |
| 360 return initializeObject(classId, emptyInstance, fields); | |
| 361 } | |
| 362 } | |
| 363 dart.setSignature(_Deserializer, { | |
| 364 constructors: () => ({_Deserializer: [_Deserializer, [], {adjustSendPorts: d
art.dynamic}]}), | |
| 365 methods: () => ({ | |
| 366 deserialize: [dart.dynamic, [dart.dynamic]], | |
| 367 isPrimitive: [core.bool, [dart.dynamic]], | |
| 368 deserializePrimitive: [dart.dynamic, [dart.dynamic]], | |
| 369 deserializeRef: [dart.dynamic, [dart.dynamic]], | |
| 370 deserializeByteBuffer: [_native_typed_data.NativeByteBuffer, [dart.dynamic
]], | |
| 371 deserializeTypedData: [_native_typed_data.NativeTypedData, [dart.dynamic]]
, | |
| 372 deserializeArrayInPlace: [core.List, [_interceptors.JSArray]], | |
| 373 deserializeFixed: [core.List, [dart.dynamic]], | |
| 374 deserializeExtendable: [core.List, [dart.dynamic]], | |
| 375 deserializeMutable: [core.List, [dart.dynamic]], | |
| 376 deserializeConst: [core.List, [dart.dynamic]], | |
| 377 deserializeMap: [core.Map, [dart.dynamic]], | |
| 378 deserializeSendPort: [isolate.SendPort, [dart.dynamic]], | |
| 379 deserializeRawSendPort: [isolate.SendPort, [dart.dynamic]], | |
| 380 deserializeJSObject: [dart.dynamic, [dart.dynamic]], | |
| 381 deserializeClosure: [core.Function, [dart.dynamic]], | |
| 382 deserializeDartObject: [dart.dynamic, [dart.dynamic]] | |
| 383 }) | |
| 384 }); | |
| 385 function _callInIsolate(isolate, func) { | |
| 386 let result = isolate.eval(func); | |
| 387 exports._globalState.topEventLoop.run(); | |
| 388 return result; | |
| 389 } | |
| 390 dart.fn(_callInIsolate, () => dart.definiteFunctionType(dart.dynamic, [_Isolat
eContext, core.Function])); | |
| 391 const _activeJsAsyncCount = Symbol('_activeJsAsyncCount'); | |
| 392 function enterJsAsync() { | |
| 393 let o = exports._globalState.topEventLoop; | |
| 394 o[_activeJsAsyncCount] = dart.notNull(o[_activeJsAsyncCount]) + 1; | |
| 395 } | |
| 396 dart.fn(enterJsAsync); | |
| 397 function leaveJsAsync() { | |
| 398 let o = exports._globalState.topEventLoop; | |
| 399 o[_activeJsAsyncCount] = dart.notNull(o[_activeJsAsyncCount]) - 1; | |
| 400 dart.assert(dart.notNull(exports._globalState.topEventLoop[_activeJsAsyncCou
nt]) >= 0); | |
| 401 } | |
| 402 dart.fn(leaveJsAsync); | |
| 403 function isWorker() { | |
| 404 return exports._globalState.isWorker; | |
| 405 } | |
| 406 dart.fn(isWorker, core.bool, []); | |
| 407 function _currentIsolate() { | |
| 408 return exports._globalState.currentContext; | |
| 409 } | |
| 410 dart.fn(_currentIsolate, () => dart.definiteFunctionType(_IsolateContext, []))
; | |
| 411 function startRootIsolate(entry, args) { | |
| 412 args = args; | |
| 413 if (args == null) args = []; | |
| 414 if (!dart.is(args, core.List)) { | |
| 415 dart.throw(new core.ArgumentError(`Arguments to main must be a List: ${arg
s}`)); | |
| 416 } | |
| 417 exports._globalState = new _Manager(dart.as(entry, core.Function)); | |
| 418 if (dart.notNull(exports._globalState.isWorker)) return; | |
| 419 let rootContext = new _IsolateContext(); | |
| 420 exports._globalState.rootContext = rootContext; | |
| 421 exports._globalState.currentContext = rootContext; | |
| 422 if (dart.is(entry, _MainFunctionArgs)) { | |
| 423 rootContext.eval(dart.fn(() => { | |
| 424 dart.dcall(entry, args); | |
| 425 })); | |
| 426 } else if (dart.is(entry, _MainFunctionArgsMessage)) { | |
| 427 rootContext.eval(dart.fn(() => { | |
| 428 dart.dcall(entry, args, null); | |
| 429 })); | |
| 430 } else { | |
| 431 rootContext.eval(dart.as(entry, core.Function)); | |
| 432 } | |
| 433 exports._globalState.topEventLoop.run(); | |
| 434 } | |
| 435 dart.fn(startRootIsolate, dart.void, [dart.dynamic, dart.dynamic]); | |
| 436 dart.copyProperties(exports, { | |
| 437 get _globalState() { | |
| 438 return dart.as(dart.globalState, _Manager); | |
| 439 }, | |
| 440 set _globalState(val) { | |
| 441 dart.globalState = val; | |
| 442 } | |
| 443 }); | |
| 444 const _nativeDetectEnvironment = Symbol('_nativeDetectEnvironment'); | |
| 445 const _nativeInitWorkerMessageHandler = Symbol('_nativeInitWorkerMessageHandle
r'); | |
| 446 class _Manager extends core.Object { | |
| 447 get useWorkers() { | |
| 448 return this.supportsWorkers; | |
| 449 } | |
| 450 _Manager(entry) { | |
| 451 this.entry = entry; | |
| 452 this.nextIsolateId = 0; | |
| 453 this.currentManagerId = 0; | |
| 454 this.nextManagerId = 1; | |
| 455 this.currentContext = null; | |
| 456 this.rootContext = null; | |
| 457 this.topEventLoop = null; | |
| 458 this.fromCommandLine = null; | |
| 459 this.isWorker = null; | |
| 460 this.supportsWorkers = null; | |
| 461 this.isolates = null; | |
| 462 this.mainManager = null; | |
| 463 this.managers = null; | |
| 464 this[_nativeDetectEnvironment](); | |
| 465 this.topEventLoop = new _EventLoop(); | |
| 466 this.isolates = core.Map$(core.int, _IsolateContext).new(); | |
| 467 this.managers = core.Map$(core.int, dart.dynamic).new(); | |
| 468 if (dart.notNull(this.isWorker)) { | |
| 469 this.mainManager = new _MainManagerStub(); | |
| 470 this[_nativeInitWorkerMessageHandler](); | |
| 471 } | |
| 472 } | |
| 473 [_nativeDetectEnvironment]() { | |
| 474 let isWindowDefined = exports.globalWindow != null; | |
| 475 let isWorkerDefined = exports.globalWorker != null; | |
| 476 this.isWorker = !isWindowDefined && dart.notNull(exports.globalPostMessage
Defined); | |
| 477 this.supportsWorkers = dart.notNull(this.isWorker) || isWorkerDefined && I
solateNatives.thisScript != null; | |
| 478 this.fromCommandLine = !isWindowDefined && !dart.notNull(this.isWorker); | |
| 479 } | |
| 480 [_nativeInitWorkerMessageHandler]() { | |
| 481 let func = (function(f, a) { | |
| 482 return function(e) { | |
| 483 f(a, e); | |
| 484 }; | |
| 485 })(IsolateNatives._processWorkerMessage, this.mainManager); | |
| 486 self.onmessage = func; | |
| 487 self.dartPrint = self.dartPrint || (function(serialize) { | |
| 488 return function(object) { | |
| 489 if (self.console && self.console.log) { | |
| 490 self.console.log(object); | |
| 491 } else { | |
| 492 self.postMessage(serialize(object)); | |
| 493 } | |
| 494 }; | |
| 495 })(_Manager._serializePrintMessage); | |
| 496 } | |
| 497 static _serializePrintMessage(object) { | |
| 498 return _serializeMessage(dart.map({command: "print", msg: object})); | |
| 499 } | |
| 500 maybeCloseWorker() { | |
| 501 if (dart.notNull(this.isWorker) && dart.notNull(this.isolates[dartx.isEmpt
y]) && this.topEventLoop[_activeJsAsyncCount] == 0) { | |
| 502 this.mainManager.postMessage(_serializeMessage(dart.map({command: 'close
'}))); | |
| 503 } | |
| 504 } | |
| 505 } | |
| 506 dart.setSignature(_Manager, { | |
| 507 constructors: () => ({_Manager: [_Manager, [core.Function]]}), | |
| 508 methods: () => ({ | |
| 509 [_nativeDetectEnvironment]: [dart.void, []], | |
| 510 [_nativeInitWorkerMessageHandler]: [dart.void, []], | |
| 511 maybeCloseWorker: [dart.void, []] | |
| 512 }), | |
| 513 statics: () => ({_serializePrintMessage: [dart.dynamic, [dart.dynamic]]}), | |
| 514 names: ['_serializePrintMessage'] | |
| 515 }); | |
| 516 const _scheduledControlEvents = Symbol('_scheduledControlEvents'); | |
| 517 const _isExecutingEvent = Symbol('_isExecutingEvent'); | |
| 518 const _updateGlobalState = Symbol('_updateGlobalState'); | |
| 519 const _setGlobals = Symbol('_setGlobals'); | |
| 520 const _addRegistration = Symbol('_addRegistration'); | |
| 521 const _close = Symbol('_close'); | |
| 522 class _IsolateContext extends core.Object { | |
| 523 _IsolateContext() { | |
| 524 this.id = (() => { | |
| 525 let o = exports._globalState, x = o.nextIsolateId; | |
| 526 o.nextIsolateId = dart.notNull(x) + 1; | |
| 527 return x; | |
| 528 })(); | |
| 529 this.ports = core.Map$(core.int, RawReceivePortImpl).new(); | |
| 530 this.weakPorts = core.Set$(core.int).new(); | |
| 531 this.isolateStatics = _foreign_helper.JS_CREATE_ISOLATE(); | |
| 532 this.controlPort = new RawReceivePortImpl._controlPort(); | |
| 533 this.pauseCapability = isolate.Capability.new(); | |
| 534 this.terminateCapability = isolate.Capability.new(); | |
| 535 this.delayedEvents = dart.list([], _IsolateEvent); | |
| 536 this.pauseTokens = core.Set$(isolate.Capability).new(); | |
| 537 this.errorPorts = core.Set$(isolate.SendPort).new(); | |
| 538 this.initialized = false; | |
| 539 this.isPaused = false; | |
| 540 this.doneHandlers = null; | |
| 541 this[_scheduledControlEvents] = null; | |
| 542 this[_isExecutingEvent] = false; | |
| 543 this.errorsAreFatal = true; | |
| 544 this.registerWeak(this.controlPort[_id], this.controlPort); | |
| 545 } | |
| 546 addPause(authentification, resume) { | |
| 547 if (!dart.equals(this.pauseCapability, authentification)) return; | |
| 548 if (dart.notNull(this.pauseTokens.add(resume)) && !dart.notNull(this.isPau
sed)) { | |
| 549 this.isPaused = true; | |
| 550 } | |
| 551 this[_updateGlobalState](); | |
| 552 } | |
| 553 removePause(resume) { | |
| 554 if (!dart.notNull(this.isPaused)) return; | |
| 555 this.pauseTokens.remove(resume); | |
| 556 if (dart.notNull(this.pauseTokens.isEmpty)) { | |
| 557 while (dart.notNull(this.delayedEvents[dartx.isNotEmpty])) { | |
| 558 let event = this.delayedEvents[dartx.removeLast](); | |
| 559 exports._globalState.topEventLoop.prequeue(event); | |
| 560 } | |
| 561 this.isPaused = false; | |
| 562 } | |
| 563 this[_updateGlobalState](); | |
| 564 } | |
| 565 addDoneListener(responsePort) { | |
| 566 if (this.doneHandlers == null) { | |
| 567 this.doneHandlers = []; | |
| 568 } | |
| 569 if (dart.notNull(dart.as(dart.dsend(this.doneHandlers, 'contains', respons
ePort), core.bool))) return; | |
| 570 dart.dsend(this.doneHandlers, 'add', responsePort); | |
| 571 } | |
| 572 removeDoneListener(responsePort) { | |
| 573 if (this.doneHandlers == null) return; | |
| 574 dart.dsend(this.doneHandlers, 'remove', responsePort); | |
| 575 } | |
| 576 setErrorsFatal(authentification, errorsAreFatal) { | |
| 577 if (!dart.equals(this.terminateCapability, authentification)) return; | |
| 578 this.errorsAreFatal = errorsAreFatal; | |
| 579 } | |
| 580 handlePing(responsePort, pingType) { | |
| 581 if (pingType == isolate.Isolate.IMMEDIATE || pingType == isolate.Isolate.B
EFORE_NEXT_EVENT && !dart.notNull(this[_isExecutingEvent])) { | |
| 582 responsePort.send(null); | |
| 583 return; | |
| 584 } | |
| 585 function respond() { | |
| 586 responsePort.send(null); | |
| 587 } | |
| 588 dart.fn(respond, dart.void, []); | |
| 589 if (pingType == isolate.Isolate.AS_EVENT) { | |
| 590 exports._globalState.topEventLoop.enqueue(this, respond, "ping"); | |
| 591 return; | |
| 592 } | |
| 593 dart.assert(pingType == isolate.Isolate.BEFORE_NEXT_EVENT); | |
| 594 if (this[_scheduledControlEvents] == null) { | |
| 595 this[_scheduledControlEvents] = collection.Queue.new(); | |
| 596 } | |
| 597 dart.dsend(this[_scheduledControlEvents], 'addLast', respond); | |
| 598 } | |
| 599 handleKill(authentification, priority) { | |
| 600 if (!dart.equals(this.terminateCapability, authentification)) return; | |
| 601 if (priority == isolate.Isolate.IMMEDIATE || priority == isolate.Isolate.B
EFORE_NEXT_EVENT && !dart.notNull(this[_isExecutingEvent])) { | |
| 602 this.kill(); | |
| 603 return; | |
| 604 } | |
| 605 if (priority == isolate.Isolate.AS_EVENT) { | |
| 606 exports._globalState.topEventLoop.enqueue(this, dart.bind(this, 'kill'),
"kill"); | |
| 607 return; | |
| 608 } | |
| 609 dart.assert(priority == isolate.Isolate.BEFORE_NEXT_EVENT); | |
| 610 if (this[_scheduledControlEvents] == null) { | |
| 611 this[_scheduledControlEvents] = collection.Queue.new(); | |
| 612 } | |
| 613 dart.dsend(this[_scheduledControlEvents], 'addLast', dart.bind(this, 'kill
')); | |
| 614 } | |
| 615 addErrorListener(port) { | |
| 616 this.errorPorts.add(port); | |
| 617 } | |
| 618 removeErrorListener(port) { | |
| 619 this.errorPorts.remove(port); | |
| 620 } | |
| 621 handleUncaughtError(error, stackTrace) { | |
| 622 if (dart.notNull(this.errorPorts.isEmpty)) { | |
| 623 if (dart.notNull(this.errorsAreFatal) && core.identical(this, exports._g
lobalState.rootContext)) { | |
| 624 return; | |
| 625 } | |
| 626 if (self.console && self.console.error) { | |
| 627 self.console.error(error, stackTrace); | |
| 628 } else { | |
| 629 core.print(error); | |
| 630 if (stackTrace != null) core.print(stackTrace); | |
| 631 } | |
| 632 return; | |
| 633 } | |
| 634 let message = core.List.new(2); | |
| 635 message[dartx.set](0, dart.toString(error)); | |
| 636 message[dartx.set](1, stackTrace == null ? null : dart.toString(stackTrace
)); | |
| 637 for (let port of this.errorPorts) | |
| 638 port.send(message); | |
| 639 } | |
| 640 eval(code) { | |
| 641 let old = exports._globalState.currentContext; | |
| 642 exports._globalState.currentContext = this; | |
| 643 this[_setGlobals](); | |
| 644 let result = null; | |
| 645 this[_isExecutingEvent] = true; | |
| 646 try { | |
| 647 result = dart.dcall(code); | |
| 648 } catch (e) { | |
| 649 let s = dart.stackTrace(e); | |
| 650 this.handleUncaughtError(e, s); | |
| 651 if (dart.notNull(this.errorsAreFatal)) { | |
| 652 this.kill(); | |
| 653 if (core.identical(this, exports._globalState.rootContext)) { | |
| 654 throw e; | |
| 655 } | |
| 656 } | |
| 657 } | |
| 658 finally { | |
| 659 this[_isExecutingEvent] = false; | |
| 660 exports._globalState.currentContext = old; | |
| 661 if (old != null) old[_setGlobals](); | |
| 662 if (this[_scheduledControlEvents] != null) { | |
| 663 while (dart.notNull(dart.as(dart.dload(this[_scheduledControlEvents],
'isNotEmpty'), core.bool))) { | |
| 664 dart.dcall(dart.dsend(this[_scheduledControlEvents], 'removeFirst'))
; | |
| 665 } | |
| 666 } | |
| 667 } | |
| 668 return result; | |
| 669 } | |
| 670 [_setGlobals]() { | |
| 671 _foreign_helper.JS_SET_CURRENT_ISOLATE(this.isolateStatics); | |
| 672 } | |
| 673 handleControlMessage(message) { | |
| 674 switch (dart.dindex(message, 0)) { | |
| 675 case "pause": | |
| 676 { | |
| 677 this.addPause(dart.as(dart.dindex(message, 1), isolate.Capability), da
rt.as(dart.dindex(message, 2), isolate.Capability)); | |
| 678 break; | |
| 679 } | |
| 680 case "resume": | |
| 681 { | |
| 682 this.removePause(dart.as(dart.dindex(message, 1), isolate.Capability))
; | |
| 683 break; | |
| 684 } | |
| 685 case 'add-ondone': | |
| 686 { | |
| 687 this.addDoneListener(dart.as(dart.dindex(message, 1), isolate.SendPort
)); | |
| 688 break; | |
| 689 } | |
| 690 case 'remove-ondone': | |
| 691 { | |
| 692 this.removeDoneListener(dart.as(dart.dindex(message, 1), isolate.SendP
ort)); | |
| 693 break; | |
| 694 } | |
| 695 case 'set-errors-fatal': | |
| 696 { | |
| 697 this.setErrorsFatal(dart.as(dart.dindex(message, 1), isolate.Capabilit
y), dart.as(dart.dindex(message, 2), core.bool)); | |
| 698 break; | |
| 699 } | |
| 700 case "ping": | |
| 701 { | |
| 702 this.handlePing(dart.as(dart.dindex(message, 1), isolate.SendPort), da
rt.as(dart.dindex(message, 2), core.int)); | |
| 703 break; | |
| 704 } | |
| 705 case "kill": | |
| 706 { | |
| 707 this.handleKill(dart.as(dart.dindex(message, 1), isolate.Capability),
dart.as(dart.dindex(message, 2), core.int)); | |
| 708 break; | |
| 709 } | |
| 710 case "getErrors": | |
| 711 { | |
| 712 this.addErrorListener(dart.as(dart.dindex(message, 1), isolate.SendPor
t)); | |
| 713 break; | |
| 714 } | |
| 715 case "stopErrors": | |
| 716 { | |
| 717 this.removeErrorListener(dart.as(dart.dindex(message, 1), isolate.Send
Port)); | |
| 718 break; | |
| 719 } | |
| 720 default: | |
| 721 } | |
| 722 } | |
| 723 lookup(portId) { | |
| 724 return this.ports[dartx.get](portId); | |
| 725 } | |
| 726 [_addRegistration](portId, port) { | |
| 727 if (dart.notNull(this.ports[dartx.containsKey](portId))) { | |
| 728 dart.throw(core.Exception.new("Registry: ports must be registered only o
nce.")); | |
| 729 } | |
| 730 this.ports[dartx.set](portId, port); | |
| 731 } | |
| 732 register(portId, port) { | |
| 733 this[_addRegistration](portId, port); | |
| 734 this[_updateGlobalState](); | |
| 735 } | |
| 736 registerWeak(portId, port) { | |
| 737 this.weakPorts.add(portId); | |
| 738 this[_addRegistration](portId, port); | |
| 739 } | |
| 740 [_updateGlobalState]() { | |
| 741 if (dart.notNull(this.ports[dartx.length]) - dart.notNull(this.weakPorts.l
ength) > 0 || dart.notNull(this.isPaused) || !dart.notNull(this.initialized)) { | |
| 742 exports._globalState.isolates[dartx.set](this.id, this); | |
| 743 } else { | |
| 744 this.kill(); | |
| 745 } | |
| 746 } | |
| 747 kill() { | |
| 748 if (this[_scheduledControlEvents] != null) { | |
| 749 dart.dsend(this[_scheduledControlEvents], 'clear'); | |
| 750 } | |
| 751 for (let port of this.ports[dartx.values]) { | |
| 752 port[_close](); | |
| 753 } | |
| 754 this.ports[dartx.clear](); | |
| 755 this.weakPorts.clear(); | |
| 756 exports._globalState.isolates[dartx.remove](this.id); | |
| 757 this.errorPorts.clear(); | |
| 758 if (this.doneHandlers != null) { | |
| 759 for (let port of dart.as(this.doneHandlers, core.Iterable)) { | |
| 760 dart.as(port, isolate.SendPort); | |
| 761 port.send(null); | |
| 762 } | |
| 763 this.doneHandlers = null; | |
| 764 } | |
| 765 } | |
| 766 unregister(portId) { | |
| 767 this.ports[dartx.remove](portId); | |
| 768 this.weakPorts.remove(portId); | |
| 769 this[_updateGlobalState](); | |
| 770 } | |
| 771 } | |
| 772 _IsolateContext[dart.implements] = () => [_foreign_helper.IsolateContext]; | |
| 773 dart.setSignature(_IsolateContext, { | |
| 774 constructors: () => ({_IsolateContext: [_IsolateContext, []]}), | |
| 775 methods: () => ({ | |
| 776 addPause: [dart.void, [isolate.Capability, isolate.Capability]], | |
| 777 removePause: [dart.void, [isolate.Capability]], | |
| 778 addDoneListener: [dart.void, [isolate.SendPort]], | |
| 779 removeDoneListener: [dart.void, [isolate.SendPort]], | |
| 780 setErrorsFatal: [dart.void, [isolate.Capability, core.bool]], | |
| 781 handlePing: [dart.void, [isolate.SendPort, core.int]], | |
| 782 handleKill: [dart.void, [isolate.Capability, core.int]], | |
| 783 addErrorListener: [dart.void, [isolate.SendPort]], | |
| 784 removeErrorListener: [dart.void, [isolate.SendPort]], | |
| 785 handleUncaughtError: [dart.void, [dart.dynamic, core.StackTrace]], | |
| 786 eval: [dart.dynamic, [core.Function]], | |
| 787 [_setGlobals]: [dart.void, []], | |
| 788 handleControlMessage: [dart.void, [dart.dynamic]], | |
| 789 lookup: [RawReceivePortImpl, [core.int]], | |
| 790 [_addRegistration]: [dart.void, [core.int, RawReceivePortImpl]], | |
| 791 register: [dart.void, [core.int, RawReceivePortImpl]], | |
| 792 registerWeak: [dart.void, [core.int, RawReceivePortImpl]], | |
| 793 [_updateGlobalState]: [dart.void, []], | |
| 794 kill: [dart.void, []], | |
| 795 unregister: [dart.void, [core.int]] | |
| 796 }) | |
| 797 }); | |
| 798 const _runHelper = Symbol('_runHelper'); | |
| 799 class _EventLoop extends core.Object { | |
| 800 _EventLoop() { | |
| 801 this.events = collection.Queue$(_IsolateEvent).new(); | |
| 802 this[_activeJsAsyncCount] = 0; | |
| 803 } | |
| 804 enqueue(isolate, fn, msg) { | |
| 805 this.events.addLast(new _IsolateEvent(dart.as(isolate, _IsolateContext), d
art.as(fn, core.Function), dart.as(msg, core.String))); | |
| 806 } | |
| 807 prequeue(event) { | |
| 808 this.events.addFirst(event); | |
| 809 } | |
| 810 dequeue() { | |
| 811 if (dart.notNull(this.events.isEmpty)) return null; | |
| 812 return this.events.removeFirst(); | |
| 813 } | |
| 814 checkOpenReceivePortsFromCommandLine() { | |
| 815 if (exports._globalState.rootContext != null && dart.notNull(exports._glob
alState.isolates[dartx.containsKey](exports._globalState.rootContext.id)) && dar
t.notNull(exports._globalState.fromCommandLine) && dart.notNull(exports._globalS
tate.rootContext.ports[dartx.isEmpty])) { | |
| 816 dart.throw(core.Exception.new("Program exited with open ReceivePorts."))
; | |
| 817 } | |
| 818 } | |
| 819 runIteration() { | |
| 820 let event = this.dequeue(); | |
| 821 if (event == null) { | |
| 822 this.checkOpenReceivePortsFromCommandLine(); | |
| 823 exports._globalState.maybeCloseWorker(); | |
| 824 return false; | |
| 825 } | |
| 826 event.process(); | |
| 827 return true; | |
| 828 } | |
| 829 [_runHelper]() { | |
| 830 if (exports.globalWindow != null) { | |
| 831 const next = (function() { | |
| 832 if (!dart.notNull(this.runIteration())) return; | |
| 833 async.Timer.run(next); | |
| 834 }).bind(this); | |
| 835 dart.fn(next); | |
| 836 next(); | |
| 837 } else { | |
| 838 while (dart.notNull(this.runIteration())) { | |
| 839 } | |
| 840 } | |
| 841 } | |
| 842 run() { | |
| 843 if (!dart.notNull(exports._globalState.isWorker)) { | |
| 844 this[_runHelper](); | |
| 845 } else { | |
| 846 try { | |
| 847 this[_runHelper](); | |
| 848 } catch (e) { | |
| 849 let trace = dart.stackTrace(e); | |
| 850 exports._globalState.mainManager.postMessage(_serializeMessage(dart.ma
p({command: 'error', msg: `${e}\n${trace}`}))); | |
| 851 } | |
| 852 | |
| 853 } | |
| 854 } | |
| 855 } | |
| 856 dart.setSignature(_EventLoop, { | |
| 857 constructors: () => ({_EventLoop: [_EventLoop, []]}), | |
| 858 methods: () => ({ | |
| 859 enqueue: [dart.void, [dart.dynamic, dart.dynamic, dart.dynamic]], | |
| 860 prequeue: [dart.void, [_IsolateEvent]], | |
| 861 dequeue: [_IsolateEvent, []], | |
| 862 checkOpenReceivePortsFromCommandLine: [dart.void, []], | |
| 863 runIteration: [core.bool, []], | |
| 864 [_runHelper]: [dart.void, []], | |
| 865 run: [dart.void, []] | |
| 866 }) | |
| 867 }); | |
| 868 class _IsolateEvent extends core.Object { | |
| 869 _IsolateEvent(isolate, fn, message) { | |
| 870 this.isolate = isolate; | |
| 871 this.fn = fn; | |
| 872 this.message = message; | |
| 873 } | |
| 874 process() { | |
| 875 if (dart.notNull(this.isolate.isPaused)) { | |
| 876 this.isolate.delayedEvents[dartx.add](this); | |
| 877 return; | |
| 878 } | |
| 879 this.isolate.eval(this.fn); | |
| 880 } | |
| 881 } | |
| 882 dart.setSignature(_IsolateEvent, { | |
| 883 constructors: () => ({_IsolateEvent: [_IsolateEvent, [_IsolateContext, core.
Function, core.String]]}), | |
| 884 methods: () => ({process: [dart.void, []]}) | |
| 885 }); | |
| 886 const _global = typeof global == 'undefined' ? self : global; | |
| 887 class _MainManagerStub extends core.Object { | |
| 888 postMessage(msg) { | |
| 889 _global.postMessage(msg); | |
| 890 } | |
| 891 } | |
| 892 dart.setSignature(_MainManagerStub, { | |
| 893 methods: () => ({postMessage: [dart.void, [dart.dynamic]]}) | |
| 894 }); | |
| 895 const _SPAWNED_SIGNAL = "spawned"; | |
| 896 const _SPAWN_FAILED_SIGNAL = "spawn failed"; | |
| 897 dart.copyProperties(exports, { | |
| 898 get globalWindow() { | |
| 899 return _global.window; | |
| 900 }, | |
| 901 get globalWorker() { | |
| 902 return _global.Worker; | |
| 903 }, | |
| 904 get globalPostMessageDefined() { | |
| 905 return !!_global.postMessage; | |
| 906 } | |
| 907 }); | |
| 908 const _MainFunction = dart.typedef('_MainFunction', () => dart.functionType(da
rt.dynamic, [])); | |
| 909 const _MainFunctionArgs = dart.typedef('_MainFunctionArgs', () => dart.functio
nType(dart.dynamic, [dart.dynamic])); | |
| 910 const _MainFunctionArgsMessage = dart.typedef('_MainFunctionArgsMessage', () =
> dart.functionType(dart.dynamic, [dart.dynamic, dart.dynamic])); | |
| 911 class IsolateNatives extends core.Object { | |
| 912 static computeThisScript() { | |
| 913 let currentScript = document.currentScript; | |
| 914 if (currentScript != null) { | |
| 915 return String(currentScript.src); | |
| 916 } | |
| 917 if (dart.notNull(_js_helper.Primitives.isD8)) return IsolateNatives.comput
eThisScriptD8(); | |
| 918 if (dart.notNull(_js_helper.Primitives.isJsshell)) return IsolateNatives.c
omputeThisScriptJsshell(); | |
| 919 if (exports._globalState != null && dart.notNull(exports._globalState.isWo
rker)) { | |
| 920 return IsolateNatives.computeThisScriptFromTrace(); | |
| 921 } | |
| 922 return null; | |
| 923 } | |
| 924 static computeThisScriptJsshell() { | |
| 925 return dart.as(thisFilename(), core.String); | |
| 926 } | |
| 927 static computeThisScriptD8() { | |
| 928 return IsolateNatives.computeThisScriptFromTrace(); | |
| 929 } | |
| 930 static computeThisScriptFromTrace() { | |
| 931 let stack = new Error().stack; | |
| 932 if (stack == null) { | |
| 933 stack = (function() { | |
| 934 try { | |
| 935 throw new Error(); | |
| 936 } catch (e) { | |
| 937 return e.stack; | |
| 938 } | |
| 939 | |
| 940 })(); | |
| 941 if (stack == null) dart.throw(new core.UnsupportedError('No stack trace'
)); | |
| 942 } | |
| 943 let pattern = null, matches = null; | |
| 944 pattern = new RegExp("^ *at [^(]*\\((.*):[0-9]*:[0-9]*\\)$", "m"); | |
| 945 matches = stack.match(pattern); | |
| 946 if (matches != null) return matches[1]; | |
| 947 pattern = new RegExp("^[^@]*@(.*):[0-9]*$", "m"); | |
| 948 matches = stack.match(pattern); | |
| 949 if (matches != null) return matches[1]; | |
| 950 dart.throw(new core.UnsupportedError(`Cannot extract URI from "${stack}"`)
); | |
| 951 } | |
| 952 static _getEventData(e) { | |
| 953 return e.data; | |
| 954 } | |
| 955 static _processWorkerMessage(sender, e) { | |
| 956 let msg = _deserializeMessage(IsolateNatives._getEventData(e)); | |
| 957 switch (dart.dindex(msg, 'command')) { | |
| 958 case 'start': | |
| 959 { | |
| 960 exports._globalState.currentManagerId = dart.as(dart.dindex(msg, 'id')
, core.int); | |
| 961 let functionName = dart.as(dart.dindex(msg, 'functionName'), core.Stri
ng); | |
| 962 let entryPoint = dart.as(functionName == null ? exports._globalState.e
ntry : IsolateNatives._getJSFunctionFromName(functionName), core.Function); | |
| 963 let args = dart.dindex(msg, 'args'); | |
| 964 let message = _deserializeMessage(dart.dindex(msg, 'msg')); | |
| 965 let isSpawnUri = dart.dindex(msg, 'isSpawnUri'); | |
| 966 let startPaused = dart.dindex(msg, 'startPaused'); | |
| 967 let replyTo = _deserializeMessage(dart.dindex(msg, 'replyTo')); | |
| 968 let context = new _IsolateContext(); | |
| 969 exports._globalState.topEventLoop.enqueue(context, dart.fn(() => { | |
| 970 IsolateNatives._startIsolate(entryPoint, dart.as(args, core.List$(co
re.String)), message, dart.as(isSpawnUri, core.bool), dart.as(startPaused, core.
bool), dart.as(replyTo, isolate.SendPort)); | |
| 971 }), 'worker-start'); | |
| 972 exports._globalState.currentContext = context; | |
| 973 exports._globalState.topEventLoop.run(); | |
| 974 break; | |
| 975 } | |
| 976 case 'spawn-worker': | |
| 977 { | |
| 978 if (IsolateNatives.enableSpawnWorker != null) IsolateNatives.handleSpa
wnWorkerRequest(msg); | |
| 979 break; | |
| 980 } | |
| 981 case 'message': | |
| 982 { | |
| 983 let port = dart.as(dart.dindex(msg, 'port'), isolate.SendPort); | |
| 984 if (port != null) { | |
| 985 dart.dsend(dart.dindex(msg, 'port'), 'send', dart.dindex(msg, 'msg')
); | |
| 986 } | |
| 987 exports._globalState.topEventLoop.run(); | |
| 988 break; | |
| 989 } | |
| 990 case 'close': | |
| 991 { | |
| 992 exports._globalState.managers[dartx.remove](IsolateNatives.workerIds.g
et(sender)); | |
| 993 sender.terminate(); | |
| 994 exports._globalState.topEventLoop.run(); | |
| 995 break; | |
| 996 } | |
| 997 case 'log': | |
| 998 { | |
| 999 IsolateNatives._log(dart.dindex(msg, 'msg')); | |
| 1000 break; | |
| 1001 } | |
| 1002 case 'print': | |
| 1003 { | |
| 1004 if (dart.notNull(exports._globalState.isWorker)) { | |
| 1005 exports._globalState.mainManager.postMessage(_serializeMessage(dart.
map({command: 'print', msg: msg}))); | |
| 1006 } else { | |
| 1007 core.print(dart.dindex(msg, 'msg')); | |
| 1008 } | |
| 1009 break; | |
| 1010 } | |
| 1011 case 'error': | |
| 1012 { | |
| 1013 dart.throw(dart.dindex(msg, 'msg')); | |
| 1014 } | |
| 1015 } | |
| 1016 } | |
| 1017 static handleSpawnWorkerRequest(msg) { | |
| 1018 let replyPort = dart.dindex(msg, 'replyPort'); | |
| 1019 IsolateNatives.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.dinde
x(msg, 'isSpawnUri'), core.bool), dart.as(dart.dindex(msg, 'startPaused'), core.
bool)).then(dart.fn(msg => { | |
| 1020 dart.dsend(replyPort, 'send', msg); | |
| 1021 }, dart.dynamic, [core.List]), {onError: dart.fn(errorMessage => { | |
| 1022 dart.dsend(replyPort, 'send', dart.list([_SPAWN_FAILED_SIGNAL, errorMe
ssage], core.String)); | |
| 1023 }, dart.dynamic, [core.String])}); | |
| 1024 } | |
| 1025 static _log(msg) { | |
| 1026 if (dart.notNull(exports._globalState.isWorker)) { | |
| 1027 exports._globalState.mainManager.postMessage(_serializeMessage(dart.map(
{command: 'log', msg: msg}))); | |
| 1028 } else { | |
| 1029 try { | |
| 1030 IsolateNatives._consoleLog(msg); | |
| 1031 } catch (e) { | |
| 1032 let trace = dart.stackTrace(e); | |
| 1033 dart.throw(core.Exception.new(trace)); | |
| 1034 } | |
| 1035 | |
| 1036 } | |
| 1037 } | |
| 1038 static _consoleLog(msg) { | |
| 1039 self.console.log(msg); | |
| 1040 } | |
| 1041 static _getJSFunctionFromName(functionName) { | |
| 1042 let globalFunctionsContainer = _foreign_helper.JS_EMBEDDED_GLOBAL("", _js_
embedded_names.GLOBAL_FUNCTIONS); | |
| 1043 return globalFunctionsContainer[functionName](); | |
| 1044 } | |
| 1045 static _getJSFunctionName(f) { | |
| 1046 return dart.as(f.$name, core.String); | |
| 1047 } | |
| 1048 static _allocate(ctor) { | |
| 1049 return new ctor(); | |
| 1050 } | |
| 1051 static spawnFunction(topLevelFunction, message, startPaused) { | |
| 1052 IsolateNatives.enableSpawnWorker = true; | |
| 1053 let name = IsolateNatives._getJSFunctionName(topLevelFunction); | |
| 1054 if (name == null) { | |
| 1055 dart.throw(new core.UnsupportedError("only top-level functions can be sp
awned.")); | |
| 1056 } | |
| 1057 let isLight = false; | |
| 1058 let isSpawnUri = false; | |
| 1059 return IsolateNatives.spawn(name, null, null, message, isLight, isSpawnUri
, startPaused); | |
| 1060 } | |
| 1061 static spawnUri(uri, args, message, startPaused) { | |
| 1062 IsolateNatives.enableSpawnWorker = true; | |
| 1063 let isLight = false; | |
| 1064 let isSpawnUri = true; | |
| 1065 return IsolateNatives.spawn(null, dart.toString(uri), args, message, isLig
ht, isSpawnUri, startPaused); | |
| 1066 } | |
| 1067 static spawn(functionName, uri, args, message, isLight, isSpawnUri, startPau
sed) { | |
| 1068 if (uri != null && dart.notNull(uri[dartx.endsWith](".dart"))) { | |
| 1069 uri = dart.notNull(uri) + ".js"; | |
| 1070 } | |
| 1071 let port = isolate.ReceivePort.new(); | |
| 1072 let completer = async.Completer$(core.List).new(); | |
| 1073 port.first.then(dart.fn(msg => { | |
| 1074 if (dart.equals(dart.dindex(msg, 0), _SPAWNED_SIGNAL)) { | |
| 1075 completer.complete(msg); | |
| 1076 } else { | |
| 1077 dart.assert(dart.equals(dart.dindex(msg, 0), _SPAWN_FAILED_SIGNAL)); | |
| 1078 completer.completeError(dart.dindex(msg, 1)); | |
| 1079 } | |
| 1080 })); | |
| 1081 let signalReply = port.sendPort; | |
| 1082 if (dart.notNull(exports._globalState.useWorkers) && !dart.notNull(isLight
)) { | |
| 1083 IsolateNatives._startWorker(functionName, uri, args, message, isSpawnUri
, startPaused, signalReply, dart.fn(message => completer.completeError(message),
dart.void, [core.String])); | |
| 1084 } else { | |
| 1085 IsolateNatives._startNonWorker(functionName, uri, args, message, isSpawn
Uri, startPaused, signalReply); | |
| 1086 } | |
| 1087 return completer.future; | |
| 1088 } | |
| 1089 static _startWorker(functionName, uri, args, message, isSpawnUri, startPause
d, replyPort, onError) { | |
| 1090 if (args != null) args = core.List$(core.String).from(args); | |
| 1091 if (dart.notNull(exports._globalState.isWorker)) { | |
| 1092 exports._globalState.mainManager.postMessage(_serializeMessage(dart.map(
{command: 'spawn-worker', functionName: functionName, args: args, msg: message,
uri: uri, isSpawnUri: isSpawnUri, startPaused: startPaused, replyPort: replyPort
}))); | |
| 1093 } else { | |
| 1094 IsolateNatives._spawnWorker(functionName, uri, args, message, isSpawnUri
, startPaused, replyPort, onError); | |
| 1095 } | |
| 1096 } | |
| 1097 static _startNonWorker(functionName, uri, args, message, isSpawnUri, startPa
used, replyPort) { | |
| 1098 if (uri != null) { | |
| 1099 dart.throw(new core.UnsupportedError("Currently spawnUri is not supporte
d without web workers.")); | |
| 1100 } | |
| 1101 message = _clone(message); | |
| 1102 if (args != null) args = core.List$(core.String).from(args); | |
| 1103 exports._globalState.topEventLoop.enqueue(new _IsolateContext(), dart.fn((
) => { | |
| 1104 let func = IsolateNatives._getJSFunctionFromName(functionName); | |
| 1105 IsolateNatives._startIsolate(dart.as(func, core.Function), args, message
, isSpawnUri, startPaused, replyPort); | |
| 1106 }), 'nonworker start'); | |
| 1107 } | |
| 1108 static get currentIsolate() { | |
| 1109 let context = dart.as(_foreign_helper.JS_CURRENT_ISOLATE_CONTEXT(), _Isola
teContext); | |
| 1110 return new isolate.Isolate(context.controlPort.sendPort, {pauseCapability:
context.pauseCapability, terminateCapability: context.terminateCapability}); | |
| 1111 } | |
| 1112 static _startIsolate(topLevel, args, message, isSpawnUri, startPaused, reply
To) { | |
| 1113 let context = dart.as(_foreign_helper.JS_CURRENT_ISOLATE_CONTEXT(), _Isola
teContext); | |
| 1114 _js_helper.Primitives.initializeStatics(context.id); | |
| 1115 replyTo.send(dart.list([_SPAWNED_SIGNAL, context.controlPort.sendPort, con
text.pauseCapability, context.terminateCapability], core.Object)); | |
| 1116 function runStartFunction() { | |
| 1117 context.initialized = true; | |
| 1118 if (!dart.notNull(isSpawnUri)) { | |
| 1119 dart.dcall(topLevel, message); | |
| 1120 } else if (dart.is(topLevel, _MainFunctionArgsMessage)) { | |
| 1121 dart.dcall(topLevel, args, message); | |
| 1122 } else if (dart.is(topLevel, _MainFunctionArgs)) { | |
| 1123 dart.dcall(topLevel, args); | |
| 1124 } else { | |
| 1125 dart.dcall(topLevel); | |
| 1126 } | |
| 1127 } | |
| 1128 dart.fn(runStartFunction, dart.void, []); | |
| 1129 if (dart.notNull(startPaused)) { | |
| 1130 context.addPause(context.pauseCapability, context.pauseCapability); | |
| 1131 exports._globalState.topEventLoop.enqueue(context, runStartFunction, 'st
art isolate'); | |
| 1132 } else { | |
| 1133 runStartFunction(); | |
| 1134 } | |
| 1135 } | |
| 1136 static _spawnWorker(functionName, uri, args, message, isSpawnUri, startPause
d, replyPort, onError) { | |
| 1137 if (uri == null) uri = IsolateNatives.thisScript; | |
| 1138 let worker = new Worker(uri); | |
| 1139 let onerrorTrampoline = (function(f, u, c) { | |
| 1140 return function(e) { | |
| 1141 return f(e, u, c); | |
| 1142 }; | |
| 1143 })(IsolateNatives.workerOnError, uri, onError); | |
| 1144 worker.onerror = onerrorTrampoline; | |
| 1145 let processWorkerMessageTrampoline = (function(f, a) { | |
| 1146 return function(e) { | |
| 1147 e.onerror = null; | |
| 1148 return f(a, e); | |
| 1149 }; | |
| 1150 })(IsolateNatives._processWorkerMessage, worker); | |
| 1151 worker.onmessage = processWorkerMessageTrampoline; | |
| 1152 let o = exports._globalState; | |
| 1153 let workerId = o.nextManagerId; | |
| 1154 o.nextManagerId = dart.notNull(workerId) + 1; | |
| 1155 IsolateNatives.workerIds.set(worker, workerId); | |
| 1156 exports._globalState.managers[dartx.set](workerId, worker); | |
| 1157 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}))); | |
| 1158 } | |
| 1159 static workerOnError(event, uri, onError) { | |
| 1160 event.preventDefault(); | |
| 1161 let message = dart.as(event.message, core.String); | |
| 1162 if (message == null) { | |
| 1163 message = `Error spawning worker for ${uri}`; | |
| 1164 } else { | |
| 1165 message = `Error spawning worker for ${uri} (${message})`; | |
| 1166 } | |
| 1167 onError(message); | |
| 1168 return true; | |
| 1169 } | |
| 1170 } | |
| 1171 dart.setSignature(IsolateNatives, { | |
| 1172 statics: () => ({ | |
| 1173 computeThisScript: [core.String, []], | |
| 1174 computeThisScriptJsshell: [core.String, []], | |
| 1175 computeThisScriptD8: [core.String, []], | |
| 1176 computeThisScriptFromTrace: [core.String, []], | |
| 1177 _getEventData: [dart.dynamic, [dart.dynamic]], | |
| 1178 _processWorkerMessage: [dart.void, [dart.dynamic, dart.dynamic]], | |
| 1179 handleSpawnWorkerRequest: [dart.dynamic, [dart.dynamic]], | |
| 1180 _log: [dart.dynamic, [dart.dynamic]], | |
| 1181 _consoleLog: [dart.void, [dart.dynamic]], | |
| 1182 _getJSFunctionFromName: [dart.dynamic, [core.String]], | |
| 1183 _getJSFunctionName: [core.String, [core.Function]], | |
| 1184 _allocate: [dart.dynamic, [dart.dynamic]], | |
| 1185 spawnFunction: [async.Future$(core.List), [dart.functionType(dart.void, [d
art.dynamic]), dart.dynamic, core.bool]], | |
| 1186 spawnUri: [async.Future$(core.List), [core.Uri, core.List$(core.String), d
art.dynamic, core.bool]], | |
| 1187 spawn: [async.Future$(core.List), [core.String, core.String, core.List$(co
re.String), dart.dynamic, core.bool, core.bool, core.bool]], | |
| 1188 _startWorker: [dart.void, [core.String, core.String, core.List$(core.Strin
g), dart.dynamic, core.bool, core.bool, isolate.SendPort, dart.functionType(dart
.void, [core.String])]], | |
| 1189 _startNonWorker: [dart.void, [core.String, core.String, core.List$(core.St
ring), dart.dynamic, core.bool, core.bool, isolate.SendPort]], | |
| 1190 _startIsolate: [dart.void, [core.Function, core.List$(core.String), dart.d
ynamic, core.bool, core.bool, isolate.SendPort]], | |
| 1191 _spawnWorker: [dart.void, [dart.dynamic, core.String, core.List$(core.Stri
ng), dart.dynamic, core.bool, core.bool, isolate.SendPort, dart.functionType(dar
t.void, [core.String])]], | |
| 1192 workerOnError: [core.bool, [dart.dynamic, core.String, dart.functionType(d
art.void, [core.String])]] | |
| 1193 }), | |
| 1194 names: ['computeThisScript', 'computeThisScriptJsshell', 'computeThisScriptD
8', 'computeThisScriptFromTrace', '_getEventData', '_processWorkerMessage', 'han
dleSpawnWorkerRequest', '_log', '_consoleLog', '_getJSFunctionFromName', '_getJS
FunctionName', '_allocate', 'spawnFunction', 'spawnUri', 'spawn', '_startWorker'
, '_startNonWorker', '_startIsolate', '_spawnWorker', 'workerOnError'] | |
| 1195 }); | |
| 1196 IsolateNatives.enableSpawnWorker = null; | |
| 1197 dart.defineLazyProperties(IsolateNatives, { | |
| 1198 get thisScript() { | |
| 1199 return IsolateNatives.computeThisScript(); | |
| 1200 }, | |
| 1201 set thisScript(_) {}, | |
| 1202 get workerIds() { | |
| 1203 return new (core.Expando$(core.int))(); | |
| 1204 } | |
| 1205 }); | |
| 1206 const _checkReplyTo = Symbol('_checkReplyTo'); | |
| 1207 class _BaseSendPort extends core.Object { | |
| 1208 _BaseSendPort(isolateId) { | |
| 1209 this[_isolateId] = isolateId; | |
| 1210 } | |
| 1211 [_checkReplyTo](replyTo) { | |
| 1212 if (replyTo != null && !dart.is(replyTo, _NativeJsSendPort) && !dart.is(re
plyTo, _WorkerSendPort)) { | |
| 1213 dart.throw(core.Exception.new("SendPort.send: Illegal replyTo port type"
)); | |
| 1214 } | |
| 1215 } | |
| 1216 } | |
| 1217 _BaseSendPort[dart.implements] = () => [isolate.SendPort]; | |
| 1218 dart.setSignature(_BaseSendPort, { | |
| 1219 constructors: () => ({_BaseSendPort: [_BaseSendPort, [core.int]]}), | |
| 1220 methods: () => ({[_checkReplyTo]: [dart.void, [isolate.SendPort]]}) | |
| 1221 }); | |
| 1222 const _isClosed = Symbol('_isClosed'); | |
| 1223 const _add = Symbol('_add'); | |
| 1224 class _NativeJsSendPort extends _BaseSendPort { | |
| 1225 _NativeJsSendPort(receivePort, isolateId) { | |
| 1226 this[_receivePort] = receivePort; | |
| 1227 super._BaseSendPort(isolateId); | |
| 1228 } | |
| 1229 send(message) { | |
| 1230 let isolate = exports._globalState.isolates[dartx.get](this[_isolateId]); | |
| 1231 if (isolate == null) return; | |
| 1232 if (dart.notNull(this[_receivePort][_isClosed])) return; | |
| 1233 let msg = _clone(message); | |
| 1234 if (dart.equals(isolate.controlPort, this[_receivePort])) { | |
| 1235 isolate.handleControlMessage(msg); | |
| 1236 return; | |
| 1237 } | |
| 1238 exports._globalState.topEventLoop.enqueue(isolate, dart.fn(() => { | |
| 1239 if (!dart.notNull(this[_receivePort][_isClosed])) { | |
| 1240 this[_receivePort][_add](msg); | |
| 1241 } | |
| 1242 }), `receive ${message}`); | |
| 1243 } | |
| 1244 ['=='](other) { | |
| 1245 return dart.is(other, _NativeJsSendPort) && dart.equals(this[_receivePort]
, other[_receivePort]); | |
| 1246 } | |
| 1247 get hashCode() { | |
| 1248 return this[_receivePort][_id]; | |
| 1249 } | |
| 1250 } | |
| 1251 _NativeJsSendPort[dart.implements] = () => [isolate.SendPort]; | |
| 1252 dart.setSignature(_NativeJsSendPort, { | |
| 1253 constructors: () => ({_NativeJsSendPort: [_NativeJsSendPort, [RawReceivePort
Impl, core.int]]}), | |
| 1254 methods: () => ({send: [dart.void, [dart.dynamic]]}) | |
| 1255 }); | |
| 1256 class _WorkerSendPort extends _BaseSendPort { | |
| 1257 _WorkerSendPort(workerId, isolateId, receivePortId) { | |
| 1258 this[_workerId] = workerId; | |
| 1259 this[_receivePortId] = receivePortId; | |
| 1260 super._BaseSendPort(isolateId); | |
| 1261 } | |
| 1262 send(message) { | |
| 1263 let workerMessage = _serializeMessage(dart.map({command: 'message', port:
this, msg: message})); | |
| 1264 if (dart.notNull(exports._globalState.isWorker)) { | |
| 1265 exports._globalState.mainManager.postMessage(workerMessage); | |
| 1266 } else { | |
| 1267 let manager = exports._globalState.managers[dartx.get](this[_workerId]); | |
| 1268 if (manager != null) { | |
| 1269 manager.postMessage(workerMessage); | |
| 1270 } | |
| 1271 } | |
| 1272 } | |
| 1273 ['=='](other) { | |
| 1274 return dart.is(other, _WorkerSendPort) && this[_workerId] == other[_worker
Id] && this[_isolateId] == other[_isolateId] && this[_receivePortId] == other[_r
eceivePortId]; | |
| 1275 } | |
| 1276 get hashCode() { | |
| 1277 return dart.notNull(this[_workerId]) << 16 ^ dart.notNull(this[_isolateId]
) << 8 ^ dart.notNull(this[_receivePortId]); | |
| 1278 } | |
| 1279 } | |
| 1280 _WorkerSendPort[dart.implements] = () => [isolate.SendPort]; | |
| 1281 dart.setSignature(_WorkerSendPort, { | |
| 1282 constructors: () => ({_WorkerSendPort: [_WorkerSendPort, [core.int, core.int
, core.int]]}), | |
| 1283 methods: () => ({send: [dart.void, [dart.dynamic]]}) | |
| 1284 }); | |
| 1285 const _handler = Symbol('_handler'); | |
| 1286 class RawReceivePortImpl extends core.Object { | |
| 1287 RawReceivePortImpl(handler) { | |
| 1288 this[_handler] = handler; | |
| 1289 this[_id] = (() => { | |
| 1290 let x = RawReceivePortImpl._nextFreeId; | |
| 1291 RawReceivePortImpl._nextFreeId = dart.notNull(x) + 1; | |
| 1292 return x; | |
| 1293 })(); | |
| 1294 this[_isClosed] = false; | |
| 1295 exports._globalState.currentContext.register(this[_id], this); | |
| 1296 } | |
| 1297 weak(handler) { | |
| 1298 this[_handler] = handler; | |
| 1299 this[_id] = (() => { | |
| 1300 let x = RawReceivePortImpl._nextFreeId; | |
| 1301 RawReceivePortImpl._nextFreeId = dart.notNull(x) + 1; | |
| 1302 return x; | |
| 1303 })(); | |
| 1304 this[_isClosed] = false; | |
| 1305 exports._globalState.currentContext.registerWeak(this[_id], this); | |
| 1306 } | |
| 1307 _controlPort() { | |
| 1308 this[_handler] = null; | |
| 1309 this[_id] = 0; | |
| 1310 this[_isClosed] = false; | |
| 1311 } | |
| 1312 set handler(newHandler) { | |
| 1313 this[_handler] = newHandler; | |
| 1314 } | |
| 1315 [_close]() { | |
| 1316 this[_isClosed] = true; | |
| 1317 this[_handler] = null; | |
| 1318 } | |
| 1319 close() { | |
| 1320 if (dart.notNull(this[_isClosed])) return; | |
| 1321 this[_isClosed] = true; | |
| 1322 this[_handler] = null; | |
| 1323 exports._globalState.currentContext.unregister(this[_id]); | |
| 1324 } | |
| 1325 [_add](dataEvent) { | |
| 1326 if (dart.notNull(this[_isClosed])) return; | |
| 1327 dart.dcall(this[_handler], dataEvent); | |
| 1328 } | |
| 1329 get sendPort() { | |
| 1330 return new _NativeJsSendPort(this, exports._globalState.currentContext.id)
; | |
| 1331 } | |
| 1332 } | |
| 1333 RawReceivePortImpl[dart.implements] = () => [isolate.RawReceivePort]; | |
| 1334 dart.defineNamedConstructor(RawReceivePortImpl, 'weak'); | |
| 1335 dart.defineNamedConstructor(RawReceivePortImpl, '_controlPort'); | |
| 1336 dart.setSignature(RawReceivePortImpl, { | |
| 1337 constructors: () => ({ | |
| 1338 RawReceivePortImpl: [RawReceivePortImpl, [core.Function]], | |
| 1339 weak: [RawReceivePortImpl, [core.Function]], | |
| 1340 _controlPort: [RawReceivePortImpl, []] | |
| 1341 }), | |
| 1342 methods: () => ({ | |
| 1343 [_close]: [dart.void, []], | |
| 1344 close: [dart.void, []], | |
| 1345 [_add]: [dart.void, [dart.dynamic]] | |
| 1346 }) | |
| 1347 }); | |
| 1348 RawReceivePortImpl._nextFreeId = 1; | |
| 1349 const _rawPort = Symbol('_rawPort'); | |
| 1350 const _controller = Symbol('_controller'); | |
| 1351 class ReceivePortImpl extends async.Stream { | |
| 1352 ReceivePortImpl() { | |
| 1353 this.fromRawReceivePort(new RawReceivePortImpl(null)); | |
| 1354 } | |
| 1355 weak() { | |
| 1356 this.fromRawReceivePort(new RawReceivePortImpl.weak(null)); | |
| 1357 } | |
| 1358 fromRawReceivePort(rawPort) { | |
| 1359 this[_rawPort] = rawPort; | |
| 1360 this[_controller] = null; | |
| 1361 super.Stream(); | |
| 1362 this[_controller] = async.StreamController.new({onCancel: dart.bind(this,
'close'), sync: true}); | |
| 1363 this[_rawPort].handler = dart.bind(this[_controller], 'add'); | |
| 1364 } | |
| 1365 listen(onData, opts) { | |
| 1366 let onError = opts && 'onError' in opts ? opts.onError : null; | |
| 1367 let onDone = opts && 'onDone' in opts ? opts.onDone : null; | |
| 1368 let cancelOnError = opts && 'cancelOnError' in opts ? opts.cancelOnError :
null; | |
| 1369 return this[_controller].stream.listen(onData, {onError: onError, onDone:
onDone, cancelOnError: cancelOnError}); | |
| 1370 } | |
| 1371 close() { | |
| 1372 this[_rawPort].close(); | |
| 1373 this[_controller].close(); | |
| 1374 } | |
| 1375 get sendPort() { | |
| 1376 return this[_rawPort].sendPort; | |
| 1377 } | |
| 1378 } | |
| 1379 ReceivePortImpl[dart.implements] = () => [isolate.ReceivePort]; | |
| 1380 dart.defineNamedConstructor(ReceivePortImpl, 'weak'); | |
| 1381 dart.defineNamedConstructor(ReceivePortImpl, 'fromRawReceivePort'); | |
| 1382 dart.setSignature(ReceivePortImpl, { | |
| 1383 constructors: () => ({ | |
| 1384 ReceivePortImpl: [ReceivePortImpl, []], | |
| 1385 weak: [ReceivePortImpl, []], | |
| 1386 fromRawReceivePort: [ReceivePortImpl, [isolate.RawReceivePort]] | |
| 1387 }), | |
| 1388 methods: () => ({ | |
| 1389 listen: [async.StreamSubscription, [dart.functionType(dart.void, [dart.dyn
amic])], {onError: core.Function, onDone: dart.functionType(dart.void, []), canc
elOnError: core.bool}], | |
| 1390 close: [dart.void, []] | |
| 1391 }) | |
| 1392 }); | |
| 1393 const _once = Symbol('_once'); | |
| 1394 const _inEventLoop = Symbol('_inEventLoop'); | |
| 1395 const _handle = Symbol('_handle'); | |
| 1396 class TimerImpl extends core.Object { | |
| 1397 TimerImpl(milliseconds, callback) { | |
| 1398 this[_once] = true; | |
| 1399 this[_inEventLoop] = false; | |
| 1400 this[_handle] = null; | |
| 1401 if (milliseconds == 0 && (!dart.notNull(hasTimer()) || dart.notNull(export
s._globalState.isWorker))) { | |
| 1402 const internalCallback = (function() { | |
| 1403 this[_handle] = null; | |
| 1404 callback(); | |
| 1405 }).bind(this); | |
| 1406 dart.fn(internalCallback, dart.void, []); | |
| 1407 this[_handle] = 1; | |
| 1408 exports._globalState.topEventLoop.enqueue(exports._globalState.currentCo
ntext, internalCallback, 'timer'); | |
| 1409 this[_inEventLoop] = true; | |
| 1410 } else if (dart.notNull(hasTimer())) { | |
| 1411 const internalCallback = (function() { | |
| 1412 this[_handle] = null; | |
| 1413 leaveJsAsync(); | |
| 1414 callback(); | |
| 1415 }).bind(this); | |
| 1416 dart.fn(internalCallback, dart.void, []); | |
| 1417 enterJsAsync(); | |
| 1418 this[_handle] = self.setTimeout(internalCallback, milliseconds); | |
| 1419 } else { | |
| 1420 dart.assert(dart.notNull(milliseconds) > 0); | |
| 1421 dart.throw(new core.UnsupportedError("Timer greater than 0.")); | |
| 1422 } | |
| 1423 } | |
| 1424 periodic(milliseconds, callback) { | |
| 1425 this[_once] = false; | |
| 1426 this[_inEventLoop] = false; | |
| 1427 this[_handle] = null; | |
| 1428 if (dart.notNull(hasTimer())) { | |
| 1429 enterJsAsync(); | |
| 1430 this[_handle] = self.setInterval(dart.fn(() => { | |
| 1431 callback(this); | |
| 1432 }), milliseconds); | |
| 1433 } else { | |
| 1434 dart.throw(new core.UnsupportedError("Periodic timer.")); | |
| 1435 } | |
| 1436 } | |
| 1437 cancel() { | |
| 1438 if (dart.notNull(hasTimer())) { | |
| 1439 if (dart.notNull(this[_inEventLoop])) { | |
| 1440 dart.throw(new core.UnsupportedError("Timer in event loop cannot be ca
nceled.")); | |
| 1441 } | |
| 1442 if (this[_handle] == null) return; | |
| 1443 leaveJsAsync(); | |
| 1444 if (dart.notNull(this[_once])) { | |
| 1445 self.clearTimeout(this[_handle]); | |
| 1446 } else { | |
| 1447 self.clearInterval(this[_handle]); | |
| 1448 } | |
| 1449 this[_handle] = null; | |
| 1450 } else { | |
| 1451 dart.throw(new core.UnsupportedError("Canceling a timer.")); | |
| 1452 } | |
| 1453 } | |
| 1454 get isActive() { | |
| 1455 return this[_handle] != null; | |
| 1456 } | |
| 1457 } | |
| 1458 TimerImpl[dart.implements] = () => [async.Timer]; | |
| 1459 dart.defineNamedConstructor(TimerImpl, 'periodic'); | |
| 1460 dart.setSignature(TimerImpl, { | |
| 1461 constructors: () => ({ | |
| 1462 TimerImpl: [TimerImpl, [core.int, dart.functionType(dart.void, [])]], | |
| 1463 periodic: [TimerImpl, [core.int, dart.functionType(dart.void, [async.Timer
])]] | |
| 1464 }), | |
| 1465 methods: () => ({cancel: [dart.void, []]}) | |
| 1466 }); | |
| 1467 function hasTimer() { | |
| 1468 return self.setTimeout != null; | |
| 1469 } | |
| 1470 dart.fn(hasTimer, core.bool, []); | |
| 1471 class CapabilityImpl extends core.Object { | |
| 1472 CapabilityImpl() { | |
| 1473 this._internal(_js_helper.random64()); | |
| 1474 } | |
| 1475 _internal(id) { | |
| 1476 this[_id] = id; | |
| 1477 } | |
| 1478 get hashCode() { | |
| 1479 let hash = this[_id]; | |
| 1480 hash = dart.notNull(hash) >> 0 ^ (dart.notNull(hash) / 4294967296)[dartx.t
runcate](); | |
| 1481 hash = ~dart.notNull(hash) + (dart.notNull(hash) << 15) & 4294967295; | |
| 1482 hash = dart.notNull(hash) ^ dart.notNull(hash) >> 12; | |
| 1483 hash = dart.notNull(hash) * 5 & 4294967295; | |
| 1484 hash = dart.notNull(hash) ^ dart.notNull(hash) >> 4; | |
| 1485 hash = dart.notNull(hash) * 2057 & 4294967295; | |
| 1486 hash = dart.notNull(hash) ^ dart.notNull(hash) >> 16; | |
| 1487 return hash; | |
| 1488 } | |
| 1489 ['=='](other) { | |
| 1490 if (core.identical(other, this)) return true; | |
| 1491 if (dart.is(other, CapabilityImpl)) { | |
| 1492 return core.identical(this[_id], other[_id]); | |
| 1493 } | |
| 1494 return false; | |
| 1495 } | |
| 1496 } | |
| 1497 CapabilityImpl[dart.implements] = () => [isolate.Capability]; | |
| 1498 dart.defineNamedConstructor(CapabilityImpl, '_internal'); | |
| 1499 dart.setSignature(CapabilityImpl, { | |
| 1500 constructors: () => ({ | |
| 1501 CapabilityImpl: [CapabilityImpl, []], | |
| 1502 _internal: [CapabilityImpl, [core.int]] | |
| 1503 }), | |
| 1504 methods: () => ({'==': [core.bool, [core.Object]]}) | |
| 1505 }); | |
| 1506 // Exports: | |
| 1507 exports.enterJsAsync = enterJsAsync; | |
| 1508 exports.leaveJsAsync = leaveJsAsync; | |
| 1509 exports.isWorker = isWorker; | |
| 1510 exports.startRootIsolate = startRootIsolate; | |
| 1511 exports.IsolateNatives = IsolateNatives; | |
| 1512 exports.RawReceivePortImpl = RawReceivePortImpl; | |
| 1513 exports.ReceivePortImpl = ReceivePortImpl; | |
| 1514 exports.TimerImpl = TimerImpl; | |
| 1515 exports.hasTimer = hasTimer; | |
| 1516 exports.CapabilityImpl = CapabilityImpl; | |
| 1517 }); | |
| OLD | NEW |