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 19 matching lines...) Expand all Loading... |
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 | 36 |
37 // --------------------------------------------------------------------------- | 37 // --------------------------------------------------------------------------- |
38 // Experimental support for JS interoperability | 38 // Experimental support for JS interoperability |
39 // --------------------------------------------------------------------------- | 39 // --------------------------------------------------------------------------- |
40 function SendPortSync(receivePort) { | 40 function SendPortSync() { |
41 this.receivePort = receivePort; | |
42 } | 41 } |
43 | 42 |
44 function ReceivePortSync() { | 43 function ReceivePortSync() { |
45 this.id = ReceivePortSync.id++; | 44 this.id = ReceivePortSync.id++; |
46 ReceivePortSync.map[this.id] = this; | 45 ReceivePortSync.map[this.id] = this; |
47 } | 46 } |
48 | 47 |
49 ReceivePortSync.id = 0; | |
50 ReceivePortSync.map = {}; | |
51 | |
52 ReceivePortSync.prototype.receive = function(callback) { | |
53 this.callback = callback; | |
54 }; | |
55 | |
56 ReceivePortSync.prototype.toSendPort = function() { | |
57 return new SendPortSync(this); | |
58 }; | |
59 | |
60 ReceivePortSync.prototype.close = function() { | |
61 delete ReceivePortSync.map[this.id]; | |
62 }; | |
63 | |
64 (function() { | 48 (function() { |
65 function serialize(message) { | 49 function serialize(message) { |
66 if (message == null) { | 50 if (message == null) { |
67 return null; // Convert undefined to null. | 51 return null; // Convert undefined to null. |
68 } else if (typeof(message) == 'string' || | 52 } else if (typeof(message) == 'string' || |
69 typeof(message) == 'number' || | 53 typeof(message) == 'number' || |
70 typeof(message) == 'boolean') { | 54 typeof(message) == 'boolean') { |
71 return message; | 55 return message; |
72 } else if (message instanceof SendPortSync) { | 56 } else if (message instanceof LocalSendPortSync) { |
73 return [ 'sendport', message.receivePort.id ]; | 57 return [ 'sendport', 'nativejs', message.receivePort.id ]; |
| 58 } else if (message instanceof Dart2JsSendPortSync) { |
| 59 return [ 'sendport', 'dart2js', message.receivePort.isolateId, |
| 60 message.receivePort.portId ]; |
74 } else { | 61 } else { |
75 var id = 0; | 62 var id = 0; |
76 var keys = Object.getOwnPropertyNames(message); | 63 var keys = Object.getOwnPropertyNames(message); |
77 var values = new Array(keys.length); | 64 var values = new Array(keys.length); |
78 for (var i = 0; i < keys.length; i++) { | 65 for (var i = 0; i < keys.length; i++) { |
79 values[i] = message[keys[i]]; | 66 values[i] = message[keys[i]]; |
80 } | 67 } |
81 return [ 'map', id, keys, values ]; | 68 return [ 'map', id, keys, values ]; |
82 } | 69 } |
83 } | 70 } |
84 | 71 |
85 function deserialize(message) { | 72 function deserialize(message) { |
86 return deserializeHelper(message); | 73 return deserializeHelper(message); |
87 } | 74 } |
88 | 75 |
89 function deserializeHelper(x) { | 76 function deserializeHelper(x) { |
90 if (x == null || | 77 if (x == null || |
91 typeof(x) == 'string' || | 78 typeof(x) == 'string' || |
92 typeof(x) == 'number' || | 79 typeof(x) == 'number' || |
93 typeof(x) == 'boolean') { | 80 typeof(x) == 'boolean') { |
94 return x; | 81 return x; |
95 } | 82 } |
96 switch (x[0]) { | 83 switch (x[0]) { |
97 case 'map': return deserializeMap(x); | 84 case 'map': return deserializeMap(x); |
| 85 case 'sendport': return deserializeSendPort(x); |
98 default: throw 'unimplemented'; | 86 default: throw 'unimplemented'; |
99 } | 87 } |
100 } | 88 } |
101 | 89 |
102 function deserializeMap(x) { | 90 function deserializeMap(x) { |
103 var result = { }; | 91 var result = { }; |
104 var id = x[1]; | 92 var id = x[1]; |
105 var keys = x[2]; | 93 var keys = x[2]; |
106 var values = x[3]; | 94 var values = x[3]; |
107 for (var i = 0, length = keys.length; i < length; i++) { | 95 for (var i = 0, length = keys.length; i < length; i++) { |
108 var key = deserializeHelper(keys[i]); | 96 var key = deserializeHelper(keys[i]); |
109 var value = deserializeHelper(values[i]); | 97 var value = deserializeHelper(values[i]); |
110 result[key] = value; | 98 result[key] = value; |
111 } | 99 } |
112 return result; | 100 return result; |
113 } | 101 } |
114 | 102 |
| 103 function deserializeSendPort(x) { |
| 104 var tag = x[1]; |
| 105 switch (tag) { |
| 106 case 'nativejs': |
| 107 var id = x[2]; |
| 108 return new LocalSendPortSync(id); |
| 109 case 'dart2js': |
| 110 var isolateId = x[2]; |
| 111 var portId = x[3]; |
| 112 return new Dart2JsSendPortSync(isolateId, portId); |
| 113 default: |
| 114 throw 'Illegal SendPortSync type: $tag'; |
| 115 } |
| 116 } |
| 117 |
115 window.registerPort = function(name, port) { | 118 window.registerPort = function(name, port) { |
116 var stringified = JSON.stringify(serialize(port)); | 119 var stringified = JSON.stringify(serialize(port)); |
117 window.localStorage['dart-port:' + name] = stringified; | 120 window.localStorage['dart-port:' + name] = stringified; |
118 }; | 121 }; |
119 | 122 |
| 123 window.lookupPort = function(name) { |
| 124 var stringified = window.localStorage['dart-port:' + name]; |
| 125 return deserialize(JSON.parse(stringified)); |
| 126 }; |
| 127 |
| 128 ReceivePortSync.id = 0; |
| 129 ReceivePortSync.map = {}; |
| 130 |
120 ReceivePortSync.dispatchCall = function(id, message) { | 131 ReceivePortSync.dispatchCall = function(id, message) { |
| 132 // TODO(vsm): Handle and propagate exceptions. |
121 var deserialized = deserialize(message); | 133 var deserialized = deserialize(message); |
122 var result = ReceivePortSync.map[id].callback(deserialized); | 134 var result = ReceivePortSync.map[id].callback(deserialized); |
123 return serialize(result); | 135 return serialize(result); |
124 }; | 136 }; |
125 | 137 |
| 138 ReceivePortSync.prototype.receive = function(callback) { |
| 139 this.callback = callback; |
| 140 }; |
| 141 |
| 142 ReceivePortSync.prototype.toSendPort = function() { |
| 143 return new LocalSendPortSync(this); |
| 144 }; |
| 145 |
| 146 ReceivePortSync.prototype.close = function() { |
| 147 delete ReceivePortSync.map[this.id]; |
| 148 }; |
| 149 |
126 if (navigator.webkitStartDart) { | 150 if (navigator.webkitStartDart) { |
127 window.addEventListener('js-sync-message', function(event) { | 151 window.addEventListener('js-sync-message', function(event) { |
128 var data = JSON.parse(event.data); | 152 var data = JSON.parse(event.data); |
129 var deserialized = deserialize(data.message); | 153 var deserialized = deserialize(data.message); |
130 var result = ReceivePortSync.map[data.id].callback(deserialized); | 154 var result = ReceivePortSync.map[data.id].callback(deserialized); |
131 var string = JSON.stringify(serialize(result)); | 155 // TODO(vsm): Handle and propagate exceptions. |
132 var event = document.createEvent('TextEvent'); | 156 dispatchEvent('js-result', serialize(result)); |
133 event.initTextEvent('js-result', false, false, window, string); | |
134 window.dispatchEvent(event); | |
135 }, false); | 157 }, false); |
136 } | 158 } |
| 159 |
| 160 function LocalSendPortSync(receivePort) { |
| 161 this.receivePort = receivePort; |
| 162 } |
| 163 |
| 164 LocalSendPortSync.prototype = new SendPortSync(); |
| 165 |
| 166 LocalSendPortSync.prototype.callSync = function(message) { |
| 167 // TODO(vsm): Do a direct deepcopy. |
| 168 message = deserialize(serialize(message)); |
| 169 return this.receivePort.callback(message); |
| 170 } |
| 171 |
| 172 function Dart2JsSendPortSync(isolateId, portId) { |
| 173 this.isolateId = isolateId; |
| 174 this.portId = portId; |
| 175 } |
| 176 |
| 177 Dart2JsSendPortSync.prototype = new SendPortSync(); |
| 178 |
| 179 function dispatchEvent(receiver, message) { |
| 180 var string = JSON.stringify(message); |
| 181 var event = document.createEvent('TextEvent'); |
| 182 event.initTextEvent(receiver, false, false, window, string); |
| 183 window.dispatchEvent(event); |
| 184 } |
| 185 |
| 186 Dart2JsSendPortSync.prototype.callSync = function(message) { |
| 187 var serialized = serialize(message); |
| 188 var target = 'dart-port-' + this.isolateId + '-' + this.portId; |
| 189 // TODO(vsm): Make this re-entrant. |
| 190 // TODO(vsm): Set this up set once, on the first call. |
| 191 var source = target + '-result'; |
| 192 var result = null; |
| 193 var listener = function (e) { |
| 194 result = JSON.parse(e.data); |
| 195 }; |
| 196 window.addEventListener(source, listener, false); |
| 197 dispatchEvent(target, [source, serialized]); |
| 198 window.removeEventListener(source, listener, false); |
| 199 return deserialize(result); |
| 200 } |
137 })(); | 201 })(); |
OLD | NEW |