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 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 if (x == null || | 101 if (x == null || |
102 typeof(x) == 'string' || | 102 typeof(x) == 'string' || |
103 typeof(x) == 'number' || | 103 typeof(x) == 'number' || |
104 typeof(x) == 'boolean') { | 104 typeof(x) == 'boolean') { |
105 return x; | 105 return x; |
106 } | 106 } |
107 switch (x[0]) { | 107 switch (x[0]) { |
108 case 'map': return deserializeMap(x); | 108 case 'map': return deserializeMap(x); |
109 case 'sendport': return deserializeSendPort(x); | 109 case 'sendport': return deserializeSendPort(x); |
110 case 'list': return deserializeList(x); | 110 case 'list': return deserializeList(x); |
| 111 case 'funcref': return deserializeFunction(x); |
111 default: throw 'unimplemented'; | 112 default: throw 'unimplemented'; |
112 } | 113 } |
113 } | 114 } |
114 | 115 |
115 function deserializeMap(x) { | 116 function deserializeMap(x) { |
116 var result = { }; | 117 var result = { }; |
117 var id = x[1]; | 118 var id = x[1]; |
118 var keys = x[2]; | 119 var keys = x[2]; |
119 var values = x[3]; | 120 var values = x[3]; |
120 for (var i = 0, length = keys.length; i < length; i++) { | 121 for (var i = 0, length = keys.length; i < length; i++) { |
(...skipping 22 matching lines...) Expand all Loading... |
143 function deserializeList(x) { | 144 function deserializeList(x) { |
144 var values = x[2]; | 145 var values = x[2]; |
145 var length = values.length; | 146 var length = values.length; |
146 var result = new Array(length); | 147 var result = new Array(length); |
147 for (var i = 0; i < length; i++) { | 148 for (var i = 0; i < length; i++) { |
148 result[i] = deserializeHelper(values[i]); | 149 result[i] = deserializeHelper(values[i]); |
149 } | 150 } |
150 return result; | 151 return result; |
151 } | 152 } |
152 | 153 |
| 154 function deserializeFunction(x) { |
| 155 var ref = x[1]; |
| 156 var sendPort = deserializeSendPort(x[2]); |
| 157 // Number of arguments is not used as of now |
| 158 // we cannot find it out for Dart function in pure Dart. |
| 159 return _makeFunctionFromRef(ref, sendPort); |
| 160 } |
| 161 |
153 window.registerPort = function(name, port) { | 162 window.registerPort = function(name, port) { |
154 var stringified = JSON.stringify(serialize(port)); | 163 var stringified = JSON.stringify(serialize(port)); |
155 window.localStorage['dart-port:' + name] = stringified; | 164 window.localStorage['dart-port:' + name] = stringified; |
156 }; | 165 }; |
157 | 166 |
158 window.lookupPort = function(name) { | 167 window.lookupPort = function(name) { |
159 var stringified = window.localStorage['dart-port:' + name]; | 168 var stringified = window.localStorage['dart-port:' + name]; |
160 return deserialize(JSON.parse(stringified)); | 169 return deserialize(JSON.parse(stringified)); |
161 }; | 170 }; |
162 | 171 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 var source = target + '-result'; | 235 var source = target + '-result'; |
227 var result = null; | 236 var result = null; |
228 var listener = function (e) { | 237 var listener = function (e) { |
229 result = JSON.parse(e.data); | 238 result = JSON.parse(e.data); |
230 }; | 239 }; |
231 window.addEventListener(source, listener, false); | 240 window.addEventListener(source, listener, false); |
232 dispatchEvent(target, [source, serialized]); | 241 dispatchEvent(target, [source, serialized]); |
233 window.removeEventListener(source, listener, false); | 242 window.removeEventListener(source, listener, false); |
234 return deserialize(result); | 243 return deserialize(result); |
235 } | 244 } |
| 245 |
| 246 // Leaking implementation. |
| 247 // TODO: provide proper, backend-specific implementation. |
| 248 function _makeFunctionFromRef(ref, sendPort) { |
| 249 return function() { |
| 250 return sendPort.callSync([ref, Array.prototype.slice.call(arguments)]); |
| 251 } |
| 252 } |
236 })(); | 253 })(); |
OLD | NEW |