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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
82 if (x == null || | 82 if (x == null || |
83 typeof(x) == 'string' || | 83 typeof(x) == 'string' || |
84 typeof(x) == 'number' || | 84 typeof(x) == 'number' || |
85 typeof(x) == 'boolean') { | 85 typeof(x) == 'boolean') { |
86 return x; | 86 return x; |
87 } | 87 } |
88 switch (x[0]) { | 88 switch (x[0]) { |
89 case 'map': return deserializeMap(x); | 89 case 'map': return deserializeMap(x); |
90 case 'sendport': return deserializeSendPort(x); | 90 case 'sendport': return deserializeSendPort(x); |
91 case 'list': return deserializeList(x); | 91 case 'list': return deserializeList(x); |
92 case 'funcref': return deserializeFunction(x); | |
vsm
2012/08/10 00:18:50
How about a more descriptive name? E.g., 'functio
Anton Muhin
2012/08/14 16:42:38
I can definitely do it, but I wished to minimize t
vsm
2012/08/15 15:35:38
It's fine for now.
On 2012/08/14 16:42:38, Anton
| |
92 default: throw 'unimplemented'; | 93 default: throw 'unimplemented'; |
93 } | 94 } |
94 } | 95 } |
95 | 96 |
96 function deserializeMap(x) { | 97 function deserializeMap(x) { |
97 var result = { }; | 98 var result = { }; |
98 var id = x[1]; | 99 var id = x[1]; |
99 var keys = x[2]; | 100 var keys = x[2]; |
100 var values = x[3]; | 101 var values = x[3]; |
101 for (var i = 0, length = keys.length; i < length; i++) { | 102 for (var i = 0, length = keys.length; i < length; i++) { |
(...skipping 22 matching lines...) Expand all Loading... | |
124 function deserializeList(x) { | 125 function deserializeList(x) { |
125 var values = x[2]; | 126 var values = x[2]; |
126 var length = values.length; | 127 var length = values.length; |
127 var result = new Array(length); | 128 var result = new Array(length); |
128 for (var i = 0; i < length; i++) { | 129 for (var i = 0; i < length; i++) { |
129 result[i] = deserializeHelper(values[i]); | 130 result[i] = deserializeHelper(values[i]); |
130 } | 131 } |
131 return result; | 132 return result; |
132 } | 133 } |
133 | 134 |
135 function deserializeFunction(x) { | |
136 var ref = x[1]; | |
137 var sendPort = deserializeSendPort(x[2]); | |
138 // Number of arguments is not used as as of now | |
vsm
2012/08/10 00:18:50
"as as" -> as
Anton Muhin
2012/08/14 16:42:38
Done.
| |
139 // we cannot find it out for Dart function in pure Dart. | |
140 return _makeFunctionFromRef(ref, sendPort); | |
141 } | |
142 | |
134 window.registerPort = function(name, port) { | 143 window.registerPort = function(name, port) { |
135 var stringified = JSON.stringify(serialize(port)); | 144 var stringified = JSON.stringify(serialize(port)); |
136 window.localStorage['dart-port:' + name] = stringified; | 145 window.localStorage['dart-port:' + name] = stringified; |
137 }; | 146 }; |
138 | 147 |
139 window.lookupPort = function(name) { | 148 window.lookupPort = function(name) { |
140 var stringified = window.localStorage['dart-port:' + name]; | 149 var stringified = window.localStorage['dart-port:' + name]; |
141 return deserialize(JSON.parse(stringified)); | 150 return deserialize(JSON.parse(stringified)); |
142 }; | 151 }; |
143 | 152 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
207 var source = target + '-result'; | 216 var source = target + '-result'; |
208 var result = null; | 217 var result = null; |
209 var listener = function (e) { | 218 var listener = function (e) { |
210 result = JSON.parse(e.data); | 219 result = JSON.parse(e.data); |
211 }; | 220 }; |
212 window.addEventListener(source, listener, false); | 221 window.addEventListener(source, listener, false); |
213 dispatchEvent(target, [source, serialized]); | 222 dispatchEvent(target, [source, serialized]); |
214 window.removeEventListener(source, listener, false); | 223 window.removeEventListener(source, listener, false); |
215 return deserialize(result); | 224 return deserialize(result); |
216 } | 225 } |
226 | |
227 // Leaking implementation. | |
228 // TODO: provide proper, backend-specific implementation. | |
229 function _makeFunctionFromRef(ref, sendPort) { | |
230 return function() { | |
231 return sendPort.callSync([ref, Array.prototype.slice.call(arguments)]); | |
vsm
2012/08/10 00:18:50
Don't the arguments and return value need to be se
Anton Muhin
2012/08/14 16:42:38
I don't remember all the details, but won't callSy
vsm
2012/08/15 15:35:38
Yes, I think you're correct.
On 2012/08/14 16:42:
| |
232 } | |
233 } | |
217 })(); | 234 })(); |
OLD | NEW |