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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
81 } | 81 } |
82 return [ 'list', id, values ]; | 82 return [ 'list', id, values ]; |
83 }); | 83 }); |
84 } else if (message instanceof LocalSendPortSync) { | 84 } else if (message instanceof LocalSendPortSync) { |
85 return [ 'sendport', 'nativejs', message.receivePort.id ]; | 85 return [ 'sendport', 'nativejs', message.receivePort.id ]; |
86 } else if (message instanceof DartSendPortSync) { | 86 } else if (message instanceof DartSendPortSync) { |
87 return [ 'sendport', 'dart', message.isolateId, message.portId ]; | 87 return [ 'sendport', 'dart', message.isolateId, message.portId ]; |
88 } else if (message instanceof Function) { | 88 } else if (message instanceof Function) { |
89 return [ 'funcref', functionRefTable.makeRef(message), | 89 return [ 'funcref', functionRefTable.makeRef(message), |
90 doSerialize(functionRefTable.sendPort) ]; | 90 doSerialize(functionRefTable.sendPort) ]; |
91 } else if (message instanceof HTMLElement) { | |
92 var id = elementId(message); | |
93 // Verify that the element is connected to the document. | |
94 // Otherwise, we will not be able to find it on the other side. | |
95 getElement(id); | |
96 return [ 'element', id ]; | |
91 } else if (message instanceof DartProxy) { | 97 } else if (message instanceof DartProxy) { |
92 return [ 'objref', message._id, doSerialize(message._port) ]; | 98 return [ 'objref', message._id, doSerialize(message._port) ]; |
93 } else if (message.__proto__ != {}.__proto__) { | 99 } else if (message.__proto__ != {}.__proto__) { |
94 // TODO(vsm): Is the above portable and what we want? | 100 // TODO(vsm): Is the above portable and what we want? |
95 // Proxy non-map Objects. | 101 // Proxy non-map Objects. |
96 return [ 'objref', jsRefTable.makeRef(message), | 102 return [ 'objref', jsRefTable.makeRef(message), |
97 doSerialize(jsRefTable.sendPort) ]; | 103 doSerialize(jsRefTable.sendPort) ]; |
98 } else { | 104 } else { |
99 return checkedSerialization(message, function(id) { | 105 return checkedSerialization(message, function(id) { |
100 var keys = Object.getOwnPropertyNames(message); | 106 var keys = Object.getOwnPropertyNames(message); |
(...skipping 18 matching lines...) Expand all Loading... | |
119 typeof(message) == 'number' || | 125 typeof(message) == 'number' || |
120 typeof(message) == 'boolean') { | 126 typeof(message) == 'boolean') { |
121 return message; | 127 return message; |
122 } | 128 } |
123 switch (message[0]) { | 129 switch (message[0]) { |
124 case 'map': return deserializeMap(message); | 130 case 'map': return deserializeMap(message); |
125 case 'sendport': return deserializeSendPort(message); | 131 case 'sendport': return deserializeSendPort(message); |
126 case 'list': return deserializeList(message); | 132 case 'list': return deserializeList(message); |
127 case 'funcref': return deserializeFunction(message); | 133 case 'funcref': return deserializeFunction(message); |
128 case 'objref': return deserializeProxy(message); | 134 case 'objref': return deserializeProxy(message); |
135 case 'element': return deserializeElement(message); | |
129 default: throw 'unimplemented'; | 136 default: throw 'unimplemented'; |
130 } | 137 } |
131 } | 138 } |
132 | 139 |
133 function deserializeMap(message) { | 140 function deserializeMap(message) { |
134 var result = { }; | 141 var result = { }; |
135 var id = message[1]; | 142 var id = message[1]; |
136 var keys = message[2]; | 143 var keys = message[2]; |
137 var values = message[3]; | 144 var values = message[3]; |
138 for (var i = 0, length = keys.length; i < length; i++) { | 145 for (var i = 0, length = keys.length; i < length; i++) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
180 var id = message[1]; | 187 var id = message[1]; |
181 var port = deserializeSendPort(message[2]); | 188 var port = deserializeSendPort(message[2]); |
182 if (port instanceof LocalSendPortSync) { | 189 if (port instanceof LocalSendPortSync) { |
183 return jsRefTable.map[id]; | 190 return jsRefTable.map[id]; |
184 } else if (port instanceof DartSendPortSync) { | 191 } else if (port instanceof DartSendPortSync) { |
185 return new DartProxy(port, id); | 192 return new DartProxy(port, id); |
186 } | 193 } |
187 throw 'Illegal proxy object: ' + message; | 194 throw 'Illegal proxy object: ' + message; |
188 } | 195 } |
189 | 196 |
197 function deserializeElement(message) { | |
198 var id = message[1]; | |
199 return getElement(id); | |
200 } | |
201 | |
190 window.registerPort = function(name, port) { | 202 window.registerPort = function(name, port) { |
191 var stringified = JSON.stringify(serialize(port)); | 203 var stringified = JSON.stringify(serialize(port)); |
192 window.localStorage['dart-port:' + name] = stringified; | 204 window.localStorage['dart-port:' + name] = stringified; |
193 }; | 205 }; |
194 | 206 |
195 window.lookupPort = function(name) { | 207 window.lookupPort = function(name) { |
196 var stringified = window.localStorage['dart-port:' + name]; | 208 var stringified = window.localStorage['dart-port:' + name]; |
197 return deserialize(JSON.parse(stringified)); | 209 return deserialize(JSON.parse(stringified)); |
198 }; | 210 }; |
199 | 211 |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
369 this._id = id; | 381 this._id = id; |
370 } | 382 } |
371 | 383 |
372 // Leaking implementation. | 384 // Leaking implementation. |
373 // TODO(vsm): provide proper, backend-specific implementation. | 385 // TODO(vsm): provide proper, backend-specific implementation. |
374 function _makeFunctionFromRef(ref, sendPort) { | 386 function _makeFunctionFromRef(ref, sendPort) { |
375 return function() { | 387 return function() { |
376 return sendPort.callSync([ref, Array.prototype.slice.call(arguments)]); | 388 return sendPort.callSync([ref, Array.prototype.slice.call(arguments)]); |
377 } | 389 } |
378 } | 390 } |
391 | |
392 var localNextElementId = 0; | |
393 var _DART_ID = 'data-dart_id'; | |
394 | |
395 function elementId(e) { | |
396 if (e.hasAttribute(_DART_ID)) return e.getAttribute(_DART_ID); | |
397 var id = (localNextElementId++).toString(); | |
398 e.setAttribute(_DART_ID, id); | |
399 return id; | |
400 } | |
401 | |
402 function getElement(id) { | |
403 var list = document.querySelectorAll('[' + _DART_ID + '="' + id + '"]'); | |
404 | |
405 if (list.length > 1) throw 'Non unique ID: ' + id; | |
406 if (list.length == 0) throw 'Element must be attached to the document: ' + i d; | |
Emily Fortuna
2012/08/25 00:21:04
80 char
| |
407 return list[0]; | |
408 } | |
379 })(); | 409 })(); |
OLD | NEW |