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, id) { |
255 for (var ref in this.map) { | 257 if (id != null) { |
256 var o = this.map[ref]; | 258 this.map[id] = obj; |
257 if (o === obj) { | 259 return id; |
258 return ref; | 260 } else { |
261 var ref = obj[_dartRefPropertyName]; | |
262 if (ref == null) { | |
263 ref = this.name + '-' + this._nextId++; | |
264 this.map[ref] = obj; | |
265 Object.defineProperty(obj, _dartRefPropertyName, { value: ref }); | |
ahe
2013/10/11 12:54:40
Why are you using Object.defineProperty?
alexandre.ardhuin
2013/10/11 13:52:53
To avoid that this property shows up during proper
| |
259 } | 266 } |
267 return ref; | |
260 } | 268 } |
261 var ref = this.name + '-' + this._nextId++; | |
262 this.map[ref] = obj; | |
263 return ref; | |
264 } | 269 } |
265 | 270 |
266 // Gets the object or function corresponding to this ID. | 271 // Gets the object or function corresponding to this ID. |
272 ProxiedObjectTable.prototype.contains = function (id) { | |
273 return this.map.hasOwnProperty(id); | |
274 } | |
275 | |
276 // Gets the object or function corresponding to this ID. | |
267 ProxiedObjectTable.prototype.get = function (id) { | 277 ProxiedObjectTable.prototype.get = function (id) { |
268 if (!this.map.hasOwnProperty(id)) { | 278 if (!this.map.hasOwnProperty(id)) { |
269 throw 'Proxy ' + id + ' has been invalidated.' | 279 throw 'Proxy ' + id + ' has been invalidated.' |
270 } | 280 } |
271 return this.map[id]; | 281 return this.map[id]; |
272 } | 282 } |
273 | 283 |
274 ProxiedObjectTable.prototype._initialize = function () { | 284 ProxiedObjectTable.prototype._initialize = function () { |
275 // Configure this table's port to forward methods, getters, and setters | 285 // Configure this table's port to forward methods, getters, and setters |
276 // from the remote proxy to the local object. | 286 // from the remote proxy to the local object. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
321 return [ 'throws', e.toString() ]; | 331 return [ 'throws', e.toString() ]; |
322 } | 332 } |
323 }); | 333 }); |
324 } | 334 } |
325 | 335 |
326 // Singleton for local proxied objects. | 336 // Singleton for local proxied objects. |
327 var proxiedObjectTable = new ProxiedObjectTable(); | 337 var proxiedObjectTable = new ProxiedObjectTable(); |
328 proxiedObjectTable._initialize() | 338 proxiedObjectTable._initialize() |
329 | 339 |
330 // Type for remote proxies to Dart objects. | 340 // Type for remote proxies to Dart objects. |
331 function DartProxy(id, sendPort) { | 341 function DartObject(id, sendPort) { |
332 this.id = id; | 342 this.id = id; |
333 this.port = sendPort; | 343 this.port = sendPort; |
334 } | 344 } |
335 | 345 |
336 // Serializes JS types to SendPortSync format: | 346 // Serializes JS types to SendPortSync format: |
337 // - primitives -> primitives | 347 // - primitives -> primitives |
338 // - sendport -> sendport | 348 // - sendport -> sendport |
339 // - Function -> [ 'funcref', function-id, sendport ] | 349 // - Function -> [ 'funcref', function-id, sendport ] |
340 // - Object -> [ 'objref', object-id, sendport ] | 350 // - Object -> [ 'objref', object-id, sendport ] |
341 function serialize(message) { | 351 function serialize(message) { |
(...skipping 12 matching lines...) Expand all Loading... | |
354 // Remote function proxy. | 364 // Remote function proxy. |
355 var remoteId = message._dart_id; | 365 var remoteId = message._dart_id; |
356 var remoteSendPort = message._dart_port; | 366 var remoteSendPort = message._dart_port; |
357 return [ 'funcref', remoteId, remoteSendPort ]; | 367 return [ 'funcref', remoteId, remoteSendPort ]; |
358 } else { | 368 } else { |
359 // Local function proxy. | 369 // Local function proxy. |
360 return [ 'funcref', | 370 return [ 'funcref', |
361 proxiedObjectTable.add(message), | 371 proxiedObjectTable.add(message), |
362 proxiedObjectTable.sendPort ]; | 372 proxiedObjectTable.sendPort ]; |
363 } | 373 } |
364 } else if (message instanceof DartProxy) { | 374 } else if (message instanceof DartObject) { |
365 // Remote object proxy. | 375 // Remote object proxy. |
366 return [ 'objref', message.id, message.port ]; | 376 return [ 'objref', message.id, message.port ]; |
367 } else { | 377 } else { |
368 // Local object proxy. | 378 // Local object proxy. |
369 return [ 'objref', | 379 return [ 'objref', |
370 proxiedObjectTable.add(message), | 380 proxiedObjectTable.add(message), |
371 proxiedObjectTable.sendPort ]; | 381 proxiedObjectTable.sendPort ]; |
372 } | 382 } |
373 } | 383 } |
374 | 384 |
(...skipping 20 matching lines...) Expand all Loading... | |
395 // Create a local function that forwards to the remote function. | 405 // Create a local function that forwards to the remote function. |
396 function deserializeFunction(message) { | 406 function deserializeFunction(message) { |
397 var id = message[1]; | 407 var id = message[1]; |
398 var port = message[2]; | 408 var port = message[2]; |
399 // TODO(vsm): Add a more robust check for a local SendPortSync. | 409 // TODO(vsm): Add a more robust check for a local SendPortSync. |
400 if ("receivePort" in port) { | 410 if ("receivePort" in port) { |
401 // Local function. | 411 // Local function. |
402 return proxiedObjectTable.get(id); | 412 return proxiedObjectTable.get(id); |
403 } else { | 413 } else { |
404 // Remote function. Forward to its port. | 414 // Remote function. Forward to its port. |
415 if (proxiedObjectTable.contains(id)) { | |
416 return proxiedObjectTable.get(id); | |
417 } | |
405 var f = function () { | 418 var f = function () { |
406 var args = Array.prototype.slice.apply(arguments); | 419 var args = Array.prototype.slice.apply(arguments); |
407 args.splice(0, 0, this); | 420 args.splice(0, 0, this); |
408 args = args.map(serialize); | 421 args = args.map(serialize); |
409 var result = port.callSync([id, '#call', args]); | 422 var result = port.callSync([id, '#call', args]); |
410 if (result[0] == 'throws') throw deserialize(result[1]); | 423 if (result[0] == 'throws') throw deserialize(result[1]); |
411 return deserialize(result[1]); | 424 return deserialize(result[1]); |
412 }; | 425 }; |
413 // Cache the remote id and port. | 426 // Cache the remote id and port. |
414 f._dart_id = id; | 427 f._dart_id = id; |
415 f._dart_port = port; | 428 f._dart_port = port; |
429 proxiedObjectTable.add(f, id); | |
416 return f; | 430 return f; |
417 } | 431 } |
418 } | 432 } |
419 | 433 |
420 // Creates a DartProxy to forwards to the remote object. | 434 // Creates a DartObject to forwards to the remote object. |
421 function deserializeObject(message) { | 435 function deserializeObject(message) { |
422 var id = message[1]; | 436 var id = message[1]; |
423 var port = message[2]; | 437 var port = message[2]; |
424 // TODO(vsm): Add a more robust check for a local SendPortSync. | 438 // TODO(vsm): Add a more robust check for a local SendPortSync. |
425 if ("receivePort" in port) { | 439 if ("receivePort" in port) { |
426 // Local object. | 440 // Local object. |
427 return proxiedObjectTable.get(id); | 441 return proxiedObjectTable.get(id); |
428 } else { | 442 } else { |
429 // Remote object. | 443 // Remote object. |
430 return new DartProxy(id, port); | 444 if (proxiedObjectTable.contains(id)) { |
445 return proxiedObjectTable.get(id); | |
446 } | |
447 proxy = new DartObject(id, port); | |
448 proxiedObjectTable.add(proxy, id); | |
449 return proxy; | |
431 } | 450 } |
432 } | 451 } |
433 | 452 |
434 // Remote handler to construct a new JavaScript object given its | 453 // Remote handler to construct a new JavaScript object given its |
435 // serialized constructor and arguments. | 454 // serialized constructor and arguments. |
436 function construct(args) { | 455 function construct(args) { |
437 args = args.map(deserialize); | 456 args = args.map(deserialize); |
438 var constructor = args[0]; | 457 var constructor = args[0]; |
439 args = Array.prototype.slice.call(args, 1); | 458 args = Array.prototype.slice.call(args, 1); |
440 | 459 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
532 port.receive(f); | 551 port.receive(f); |
533 window.registerPort(name, port.toSendPort()); | 552 window.registerPort(name, port.toSendPort()); |
534 } | 553 } |
535 | 554 |
536 makeGlobalPort('dart-js-context', context); | 555 makeGlobalPort('dart-js-context', context); |
537 makeGlobalPort('dart-js-create', construct); | 556 makeGlobalPort('dart-js-create', construct); |
538 makeGlobalPort('dart-js-instanceof', proxyInstanceof); | 557 makeGlobalPort('dart-js-instanceof', proxyInstanceof); |
539 makeGlobalPort('dart-js-delete-property', proxyDeleteProperty); | 558 makeGlobalPort('dart-js-delete-property', proxyDeleteProperty); |
540 makeGlobalPort('dart-js-convert', proxyConvert); | 559 makeGlobalPort('dart-js-convert', proxyConvert); |
541 })(); | 560 })(); |
OLD | NEW |