OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 // Bootstrap support for Dart scripts on the page as this script. | 5 // Bootstrap support for Dart scripts on the page as this script. |
6 if (navigator.webkitStartDart) { | 6 if (navigator.webkitStartDart) { |
7 if (!navigator.webkitStartDart()) { | 7 if (!navigator.webkitStartDart()) { |
8 document.body.innerHTML = 'This build has expired. Please download a new Da
rtium at http://www.dartlang.org/dartium/index.html'; | 8 document.body.innerHTML = 'This build has expired. Please download a new Da
rtium at http://www.dartlang.org/dartium/index.html'; |
9 } | 9 } |
10 } else { | 10 } else { |
(...skipping 15 matching lines...) Expand all Loading... |
26 if (scripts[i].src && scripts[i].src != '') { | 26 if (scripts[i].src && scripts[i].src != '') { |
27 var script = document.createElement('script'); | 27 var script = document.createElement('script'); |
28 script.src = scripts[i].src + '.js'; | 28 script.src = scripts[i].src + '.js'; |
29 var parent = scripts[i].parentNode; | 29 var parent = scripts[i].parentNode; |
30 parent.replaceChild(script, scripts[i]); | 30 parent.replaceChild(script, scripts[i]); |
31 } | 31 } |
32 } | 32 } |
33 } | 33 } |
34 }, false); | 34 }, false); |
35 } | 35 } |
| 36 |
| 37 // --------------------------------------------------------------------------- |
| 38 // Experimental support for JS interoperability |
| 39 // --------------------------------------------------------------------------- |
| 40 |
| 41 function SendPortSync(receivePort) { |
| 42 this.receivePort = receivePort; |
| 43 } |
| 44 |
| 45 function ReceivePortSync() { |
| 46 this.id = ReceivePortSync.id++; |
| 47 ReceivePortSync.map[this.id] = this; |
| 48 } |
| 49 |
| 50 ReceivePortSync.id = 0; |
| 51 ReceivePortSync.map = {}; |
| 52 |
| 53 ReceivePortSync.prototype.receive = function(callback) { |
| 54 this.callback = callback; |
| 55 }; |
| 56 |
| 57 ReceivePortSync.prototype.toSendPort = function() { |
| 58 return new SendPortSync(this); |
| 59 }; |
| 60 |
| 61 ReceivePortSync.prototype.close = function() { |
| 62 delete ReceivePortSync.map[this.id]; |
| 63 }; |
| 64 |
| 65 // TODO(kasperl): Hide these serialization methods so they |
| 66 // do not clutter up the global scope. |
| 67 function _serialize(message) { |
| 68 if (message == null || |
| 69 typeof(message) == 'string' || |
| 70 typeof(message) == 'number' || |
| 71 typeof(message) == 'boolean') { |
| 72 return message; |
| 73 } else if (message instanceof SendPortSync) { |
| 74 return [ 'sendport', message.receivePort.id ]; |
| 75 } else { |
| 76 var id = 0; |
| 77 var keys = Object.getOwnPropertyNames(message); |
| 78 var values = new Array(keys.length); |
| 79 for (var i = 0; i < keys.length; i++) { |
| 80 values[i] = message[keys[i]]; |
| 81 } |
| 82 return [ 'map', id, keys, values ]; |
| 83 } |
| 84 } |
| 85 |
| 86 function _deserialize(message) { |
| 87 return _deserializeHelper(message); |
| 88 } |
| 89 |
| 90 function _deserializeHelper(x) { |
| 91 if (x == null || |
| 92 typeof(x) == 'string' || |
| 93 typeof(x) == 'number' || |
| 94 typeof(x) == 'boolean') { |
| 95 return x; |
| 96 } |
| 97 switch (x[0]) { |
| 98 case 'map': return _deserializeMap(x); |
| 99 default: throw 'unimplemented'; |
| 100 } |
| 101 } |
| 102 |
| 103 function _deserializeMap(x) { |
| 104 var result = { }; |
| 105 var id = x[1]; |
| 106 var keys = x[2]; |
| 107 var values = x[3]; |
| 108 for (var i = 0, length = keys.length; i < length; i++) { |
| 109 var key = _deserializeHelper(keys[i]); |
| 110 var value = _deserializeHelper(values[i]); |
| 111 result[key] = value; |
| 112 } |
| 113 return result; |
| 114 } |
| 115 |
| 116 function registerPort(name, port) { |
| 117 var stringified = JSON.stringify(_serialize(port)); |
| 118 window.localStorage['dart-port:' + name] = stringified; |
| 119 } |
OLD | NEW |