Chromium Code Reviews| 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 * The js.dart library provides simple JavaScript invocation from Dart that | 6 * The js.dart library provides simple JavaScript invocation from Dart that |
| 7 * works on both Dartium and on other modern browsers via Dart2JS. | 7 * works on both Dartium and on other modern browsers via Dart2JS. |
| 8 * | 8 * |
| 9 * It provides a model based on scoped [JsObject] objects. Proxies give Dart | 9 * It provides a model based on scoped [JsObject] objects. Proxies give Dart |
| 10 * code access to JavaScript objects, fields, and functions as well as the | 10 * code access to JavaScript objects, fields, and functions as well as the |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 276 throw 'Invocation unsupported on non-function Dart proxies'; | 276 throw 'Invocation unsupported on non-function Dart proxies'; |
| 277 } | 277 } |
| 278 } catch (e) { | 278 } catch (e) { |
| 279 // TODO(vsm): callSync should just handle exceptions itself. | 279 // TODO(vsm): callSync should just handle exceptions itself. |
| 280 return ['throws', '$e']; | 280 return ['throws', '$e']; |
| 281 } | 281 } |
| 282 }); | 282 }); |
| 283 } | 283 } |
| 284 | 284 |
| 285 // Adds a new object to the table and return a new ID for it. | 285 // Adds a new object to the table and return a new ID for it. |
| 286 String add(x) { | 286 String add(x, {String id}) { |
| 287 // TODO(vsm): Cache x and reuse id. | 287 // TODO(vsm): Cache x and reuse id. |
| 288 final id = '$_name-${_nextId++}'; | 288 id = (id != null) ? id : '$_name-${_nextId++}'; |
| 289 _registry[id] = x; | 289 _registry[id] = x; |
| 290 return id; | 290 return id; |
| 291 } | 291 } |
| 292 | 292 |
| 293 // Gets an object by ID. | 293 // Gets an object by ID. |
| 294 Object get(String id) { | 294 Object get(String id) { |
| 295 return _registry[id]; | 295 return _registry[id]; |
| 296 } | 296 } |
| 297 | 297 |
| 298 // Gets the current number of objects kept alive by this table. | 298 // Gets the current number of objects kept alive by this table. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 340 } | 340 } |
| 341 | 341 |
| 342 _deserialize(var message) { | 342 _deserialize(var message) { |
| 343 deserializeFunction(message) { | 343 deserializeFunction(message) { |
| 344 var id = message[1]; | 344 var id = message[1]; |
| 345 var port = message[2]; | 345 var port = message[2]; |
| 346 if (port == _proxiedObjectTable.sendPort) { | 346 if (port == _proxiedObjectTable.sendPort) { |
| 347 // Local function. | 347 // Local function. |
| 348 return _proxiedObjectTable.get(id); | 348 return _proxiedObjectTable.get(id); |
| 349 } else { | 349 } else { |
| 350 // Remote function. Forward to its port. | 350 // Remote function. |
|
alexandre.ardhuin
2013/10/07 08:56:54
_proxiedObjectTable currently contains only Dart l
justinfagnani
2013/10/07 17:49:13
I'm ok with the name, since it's either holding pr
| |
| 351 return new JsFunction._internal(port, id); | 351 var jsFunction = _proxiedObjectTable.get(id); |
| 352 if (jsFunction == null) { | |
| 353 jsFunction = new JsFunction._internal(port, id); | |
| 354 _proxiedObjectTable.add(jsFunction, id: id); | |
| 355 } | |
| 356 return jsFunction; | |
| 352 } | 357 } |
| 353 } | 358 } |
| 354 | 359 |
| 355 deserializeObject(message) { | 360 deserializeObject(message) { |
| 356 var id = message[1]; | 361 var id = message[1]; |
| 357 var port = message[2]; | 362 var port = message[2]; |
| 358 if (port == _proxiedObjectTable.sendPort) { | 363 if (port == _proxiedObjectTable.sendPort) { |
| 359 // Local object. | 364 // Local object. |
| 360 return _proxiedObjectTable.get(id); | 365 return _proxiedObjectTable.get(id); |
| 361 } else { | 366 } else { |
| 362 // Remote object. | 367 // Remote object. |
| 363 return new JsObject._internal(port, id); | 368 var jsObject = _proxiedObjectTable.get(id); |
| 369 if (jsObject == null) { | |
| 370 jsObject = new JsObject._internal(port, id); | |
| 371 _proxiedObjectTable.add(jsObject, id: id); | |
| 372 } | |
| 373 return jsObject; | |
| 364 } | 374 } |
| 365 } | 375 } |
| 366 | 376 |
| 367 if (message == null) { | 377 if (message == null) { |
| 368 return null; // Convert undefined to null. | 378 return null; // Convert undefined to null. |
| 369 } else if (message is String || | 379 } else if (message is String || |
| 370 message is num || | 380 message is num || |
| 371 message is bool) { | 381 message is bool) { |
| 372 // Primitives are passed directly through. | 382 // Primitives are passed directly through. |
| 373 return message; | 383 return message; |
| 374 } else if (message is SendPortSync) { | 384 } else if (message is SendPortSync) { |
| 375 // Serialized type. | 385 // Serialized type. |
| 376 return message; | 386 return message; |
| 377 } | 387 } |
| 378 var tag = message[0]; | 388 var tag = message[0]; |
| 379 switch (tag) { | 389 switch (tag) { |
| 380 case 'funcref': return deserializeFunction(message); | 390 case 'funcref': return deserializeFunction(message); |
| 381 case 'objref': return deserializeObject(message); | 391 case 'objref': return deserializeObject(message); |
| 382 } | 392 } |
| 383 throw 'Unsupported serialized data: $message'; | 393 throw 'Unsupported serialized data: $message'; |
| 384 } | 394 } |
| OLD | NEW |