OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // --------------------------------------------------------------------------- | 5 // --------------------------------------------------------------------------- |
6 // Support for JS interoperability | 6 // Support for JS interoperability |
7 // --------------------------------------------------------------------------- | 7 // --------------------------------------------------------------------------- |
8 function SendPortSync() { | 8 function SendPortSync() { |
9 } | 9 } |
10 | 10 |
11 function ReceivePortSync() { | 11 function ReceivePortSync() { |
12 this.id = ReceivePortSync.id++; | 12 this.id = ReceivePortSync.id++; |
13 ReceivePortSync.map[this.id] = this; | 13 ReceivePortSync.map[this.id] = this; |
14 } | 14 } |
15 | 15 |
16 // Type for remote proxies to Dart objects with dart2js. | 16 // Type for remote proxies to Dart objects with dart2js. |
17 function DartProxy(o) { | 17 function DartObject(o) { |
18 this.o = o; | 18 this.o = o; |
19 } | 19 } |
20 | 20 |
21 (function() { | 21 (function() { |
22 // Serialize the following types as follows: | 22 // Serialize the following types as follows: |
23 // - primitives / null: unchanged | 23 // - primitives / null: unchanged |
24 // - lists: [ 'list', internal id, list of recursively serialized elements ] | 24 // - lists: [ 'list', internal id, list of recursively serialized elements ] |
25 // - maps: [ 'map', internal id, map of keys and recursively serialized value s ] | 25 // - maps: [ 'map', internal id, map of keys and recursively serialized value s ] |
26 // - send ports: [ 'sendport', type, isolate id, port id ] | 26 // - send ports: [ 'sendport', type, isolate id, port id ] |
27 // | 27 // |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
243 this.port = new ReceivePortSync(); | 243 this.port = new ReceivePortSync(); |
244 this.sendPort = this.port.toSendPort(); | 244 this.sendPort = this.port.toSendPort(); |
245 } | 245 } |
246 | 246 |
247 // Number of valid IDs. This is the number of objects (global and local) | 247 // Number of valid IDs. This is the number of objects (global and local) |
248 // kept alive by this table. | 248 // kept alive by this table. |
249 ProxiedObjectTable.prototype.count = function () { | 249 ProxiedObjectTable.prototype.count = function () { |
250 return Object.keys(this.map).length; | 250 return Object.keys(this.map).length; |
251 } | 251 } |
252 | 252 |
253 var _dartRefPropertyName = "_$dart_ref"; | |
254 | |
253 // Adds an object to the table and return an ID for serialization. | 255 // Adds an object to the table and return an ID for serialization. |
254 ProxiedObjectTable.prototype.add = function (obj) { | 256 ProxiedObjectTable.prototype.add = function (obj) { |
255 for (var ref in this.map) { | 257 var ref = obj[_dartRefPropertyName]; |
256 var o = this.map[ref]; | 258 if (ref == null) { |
257 if (o === obj) { | 259 ref = this.name + '-' + this._nextId++; |
258 return ref; | 260 Object.defineProperty(obj, _dartRefPropertyName, { value: ref }); |
259 } | |
260 } | 261 } |
261 var ref = this.name + '-' + this._nextId++; | |
262 this.map[ref] = obj; | 262 this.map[ref] = obj; |
alexandre.ardhuin
2013/10/07 08:56:54
Move this line in the above "if" block.
justinfagnani
2013/10/07 17:49:13
Done.
| |
263 return ref; | 263 return ref; |
264 } | 264 } |
265 | 265 |
266 // Gets the object or function corresponding to this ID. | 266 // Gets the object or function corresponding to this ID. |
267 ProxiedObjectTable.prototype.get = function (id) { | 267 ProxiedObjectTable.prototype.get = function (id) { |
268 if (!this.map.hasOwnProperty(id)) { | 268 if (!this.map.hasOwnProperty(id)) { |
269 throw 'Proxy ' + id + ' has been invalidated.' | 269 throw 'Proxy ' + id + ' has been invalidated.' |
270 } | 270 } |
271 return this.map[id]; | 271 return this.map[id]; |
272 } | 272 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
321 return [ 'throws', e.toString() ]; | 321 return [ 'throws', e.toString() ]; |
322 } | 322 } |
323 }); | 323 }); |
324 } | 324 } |
325 | 325 |
326 // Singleton for local proxied objects. | 326 // Singleton for local proxied objects. |
327 var proxiedObjectTable = new ProxiedObjectTable(); | 327 var proxiedObjectTable = new ProxiedObjectTable(); |
328 proxiedObjectTable._initialize() | 328 proxiedObjectTable._initialize() |
329 | 329 |
330 // Type for remote proxies to Dart objects. | 330 // Type for remote proxies to Dart objects. |
331 function DartProxy(id, sendPort) { | 331 function DartObject(id, sendPort) { |
332 this.id = id; | 332 this.id = id; |
333 this.port = sendPort; | 333 this.port = sendPort; |
334 } | 334 } |
335 | 335 |
336 // Serializes JS types to SendPortSync format: | 336 // Serializes JS types to SendPortSync format: |
337 // - primitives -> primitives | 337 // - primitives -> primitives |
338 // - sendport -> sendport | 338 // - sendport -> sendport |
339 // - Function -> [ 'funcref', function-id, sendport ] | 339 // - Function -> [ 'funcref', function-id, sendport ] |
340 // - Object -> [ 'objref', object-id, sendport ] | 340 // - Object -> [ 'objref', object-id, sendport ] |
341 function serialize(message) { | 341 function serialize(message) { |
(...skipping 12 matching lines...) Expand all Loading... | |
354 // Remote function proxy. | 354 // Remote function proxy. |
355 var remoteId = message._dart_id; | 355 var remoteId = message._dart_id; |
356 var remoteSendPort = message._dart_port; | 356 var remoteSendPort = message._dart_port; |
357 return [ 'funcref', remoteId, remoteSendPort ]; | 357 return [ 'funcref', remoteId, remoteSendPort ]; |
358 } else { | 358 } else { |
359 // Local function proxy. | 359 // Local function proxy. |
360 return [ 'funcref', | 360 return [ 'funcref', |
361 proxiedObjectTable.add(message), | 361 proxiedObjectTable.add(message), |
362 proxiedObjectTable.sendPort ]; | 362 proxiedObjectTable.sendPort ]; |
363 } | 363 } |
364 } else if (message instanceof DartProxy) { | 364 } else if (message instanceof DartObject) { |
365 // Remote object proxy. | 365 // Remote object proxy. |
366 return [ 'objref', message.id, message.port ]; | 366 return [ 'objref', message.id, message.port ]; |
367 } else { | 367 } else { |
368 // Local object proxy. | 368 // Local object proxy. |
369 return [ 'objref', | 369 return [ 'objref', |
370 proxiedObjectTable.add(message), | 370 proxiedObjectTable.add(message), |
371 proxiedObjectTable.sendPort ]; | 371 proxiedObjectTable.sendPort ]; |
372 } | 372 } |
373 } | 373 } |
374 | 374 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
410 if (result[0] == 'throws') throw deserialize(result[1]); | 410 if (result[0] == 'throws') throw deserialize(result[1]); |
411 return deserialize(result[1]); | 411 return deserialize(result[1]); |
412 }; | 412 }; |
413 // Cache the remote id and port. | 413 // Cache the remote id and port. |
414 f._dart_id = id; | 414 f._dart_id = id; |
415 f._dart_port = port; | 415 f._dart_port = port; |
416 return f; | 416 return f; |
417 } | 417 } |
418 } | 418 } |
419 | 419 |
420 // Creates a DartProxy to forwards to the remote object. | 420 // Creates a DartObject to forwards to the remote object. |
421 function deserializeObject(message) { | 421 function deserializeObject(message) { |
422 var id = message[1]; | 422 var id = message[1]; |
423 var port = message[2]; | 423 var port = message[2]; |
424 // TODO(vsm): Add a more robust check for a local SendPortSync. | 424 // TODO(vsm): Add a more robust check for a local SendPortSync. |
425 if ("receivePort" in port) { | 425 if ("receivePort" in port) { |
426 // Local object. | 426 // Local object. |
427 return proxiedObjectTable.get(id); | 427 return proxiedObjectTable.get(id); |
428 } else { | 428 } else { |
429 // Remote object. | 429 // Remote object. |
430 return new DartProxy(id, port); | 430 return new DartObject(id, port); |
431 } | 431 } |
432 } | 432 } |
433 | 433 |
434 // Remote handler to construct a new JavaScript object given its | 434 // Remote handler to construct a new JavaScript object given its |
435 // serialized constructor and arguments. | 435 // serialized constructor and arguments. |
436 function construct(args) { | 436 function construct(args) { |
437 args = args.map(deserialize); | 437 args = args.map(deserialize); |
438 var constructor = args[0]; | 438 var constructor = args[0]; |
439 args = Array.prototype.slice.call(args, 1); | 439 args = Array.prototype.slice.call(args, 1); |
440 | 440 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
532 port.receive(f); | 532 port.receive(f); |
533 window.registerPort(name, port.toSendPort()); | 533 window.registerPort(name, port.toSendPort()); |
534 } | 534 } |
535 | 535 |
536 makeGlobalPort('dart-js-context', context); | 536 makeGlobalPort('dart-js-context', context); |
537 makeGlobalPort('dart-js-create', construct); | 537 makeGlobalPort('dart-js-create', construct); |
538 makeGlobalPort('dart-js-instanceof', proxyInstanceof); | 538 makeGlobalPort('dart-js-instanceof', proxyInstanceof); |
539 makeGlobalPort('dart-js-delete-property', proxyDeleteProperty); | 539 makeGlobalPort('dart-js-delete-property', proxyDeleteProperty); |
540 makeGlobalPort('dart-js-convert', proxyConvert); | 540 makeGlobalPort('dart-js-convert', proxyConvert); |
541 })(); | 541 })(); |
OLD | NEW |