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 ]; | |
kasperl
2012/07/06 07:48:47
No tabs.
vsm
2012/07/06 08:10:02
Thanks - need to fix my js mode!
On 2012/07/06 07
| |
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) { |
121 var deserialized = deserialize(message); | 132 var deserialized = deserialize(message); |
122 var result = ReceivePortSync.map[id].callback(deserialized); | 133 var result = ReceivePortSync.map[id].callback(deserialized); |
kasperl
2012/07/06 07:48:47
We should really wrap this in a try-catch and find
vsm
2012/07/06 08:10:02
Added TODO.
On 2012/07/06 07:48:47, kasperl wrote
| |
123 return serialize(result); | 134 return serialize(result); |
124 }; | 135 }; |
125 | 136 |
137 ReceivePortSync.prototype.receive = function(callback) { | |
138 this.callback = callback; | |
139 }; | |
140 | |
141 ReceivePortSync.prototype.toSendPort = function() { | |
142 return new LocalSendPortSync(this); | |
143 }; | |
144 | |
145 ReceivePortSync.prototype.close = function() { | |
146 delete ReceivePortSync.map[this.id]; | |
147 }; | |
148 | |
126 if (navigator.webkitStartDart) { | 149 if (navigator.webkitStartDart) { |
127 window.addEventListener('js-sync-message', function(event) { | 150 window.addEventListener('js-sync-message', function(event) { |
128 var data = JSON.parse(event.data); | 151 var data = JSON.parse(event.data); |
129 var deserialized = deserialize(data.message); | 152 var deserialized = deserialize(data.message); |
130 var result = ReceivePortSync.map[data.id].callback(deserialized); | 153 var result = ReceivePortSync.map[data.id].callback(deserialized); |
kasperl
2012/07/06 07:48:47
We should really wrap this in a try-catch.
vsm
2012/07/06 08:10:02
TODO added to propagate exception.
On 2012/07/06
| |
131 var string = JSON.stringify(serialize(result)); | 154 dispatchEvent('js-result', serialize(result)); |
132 var event = document.createEvent('TextEvent'); | |
133 event.initTextEvent('js-result', false, false, window, string); | |
134 window.dispatchEvent(event); | |
135 }, false); | 155 }, false); |
136 } | 156 } |
157 | |
158 function LocalSendPortSync(receivePort) { | |
159 this.receivePort = receivePort; | |
160 } | |
161 | |
162 LocalSendPortSync.prototype = new SendPortSync(); | |
163 | |
164 LocalSendPortSync.prototype.callSync = function(message) { | |
165 // TODO(vsm): Do a direct deepcopy. | |
166 message = deserialize(serialize(message)); | |
167 return this.receivePort.callback(message); | |
168 } | |
169 | |
170 function Dart2JSSendPortSync(isolateId, portId) { | |
kasperl
2012/07/06 07:48:47
Dart2JsSendPortSync is more readable to me (Js rat
vsm
2012/07/06 08:10:02
Done.
| |
171 this.isolateId = isolateId; | |
172 this.portId = portId; | |
173 } | |
174 | |
175 Dart2JSSendPortSync.prototype = new SendPortSync(); | |
176 | |
177 function dispatchEvent(receiver, message) { | |
178 var string = JSON.stringify(message); | |
kasperl
2012/07/06 07:48:47
Weird indentation.
vsm
2012/07/06 08:10:02
Done.
| |
179 var event = document.createEvent('TextEvent'); | |
180 event.initTextEvent(receiver, false, false, window, string); | |
181 window.dispatchEvent(event); | |
182 } | |
183 | |
184 Dart2JSSendPortSync.prototype.callSync = function(message) { | |
185 var serialized = serialize(message); | |
186 var target = 'dart-port-' + this.isolateId + '-' + this.portId; | |
187 // TODO(vsm): Make this re-entrant. | |
188 // TODO(vsm): Set this up set once, on the first call. | |
189 var source = target + '-result'; | |
190 var result = null; | |
191 var listener = function (e) { | |
192 result = JSON.parse(e.data); | |
193 }; | |
194 window.addEventListener(source, listener, false); | |
195 dispatchEvent(target, [source, serialized]); | |
196 window.removeEventListener(source, listener, false); | |
197 return deserialize(result); | |
198 } | |
137 })(); | 199 })(); |
OLD | NEW |