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 29 matching lines...) Expand all Loading... | |
40 function SendPortSync() { | 40 function SendPortSync() { |
41 } | 41 } |
42 | 42 |
43 function ReceivePortSync() { | 43 function ReceivePortSync() { |
44 this.id = ReceivePortSync.id++; | 44 this.id = ReceivePortSync.id++; |
45 ReceivePortSync.map[this.id] = this; | 45 ReceivePortSync.map[this.id] = this; |
46 } | 46 } |
47 | 47 |
48 (function() { | 48 (function() { |
49 function serialize(message) { | 49 function serialize(message) { |
50 if (message == null) { | 50 var visited = []; |
51 return null; // Convert undefined to null. | 51 function checkedSerialization(obj, serializer) { |
52 } else if (typeof(message) == 'string' || | 52 for (var i = 0; i < visited.length; i++) { |
53 typeof(message) == 'number' || | 53 if (visited[i] === obj) { |
vsm
2012/07/13 14:10:40
An expando on obj to avoid the linear search?
Anton Muhin
2012/07/13 15:30:58
That's probably stupid, but for some reason I don'
vsm
2012/07/13 15:39:01
You may be right. From what I understand, deletin
Anton Muhin
2012/07/13 15:43:29
Good idea, done.
On 2012/07/13 15:39:01, vsm wrot
| |
54 typeof(message) == 'boolean') { | 54 return ['ref', i]; |
55 return message; | 55 } |
56 } else if (message instanceof Array) { | |
57 var values = new Array(message.length); | |
58 for (var i = 0; i < message.length; i++) { | |
59 values[i] = serialize(message[i]); | |
60 } | 56 } |
61 return [ 'list', message.length, values ]; | 57 var id = visited.length; |
62 } else if (message instanceof LocalSendPortSync) { | 58 visited.push(obj); |
63 return [ 'sendport', 'nativejs', message.receivePort.id ]; | 59 return serializer(id); |
64 } else if (message instanceof DartSendPortSync) { | 60 } |
65 return [ 'sendport', 'dart', message.isolateId, message.portId ]; | 61 |
66 } else { | 62 function do(message) { |
podivilov
2012/07/13 11:07:55
doSerialize maybe?
Anton Muhin
2012/07/13 15:30:58
Done.
| |
67 var id = 0; | 63 if (message == null) { |
68 var keys = Object.getOwnPropertyNames(message); | 64 return null; // Convert undefined to null. |
69 var values = new Array(keys.length); | 65 } else if (typeof(message) == 'string' || |
podivilov
2012/07/13 11:07:55
no else, please
Anton Muhin
2012/07/13 15:30:58
just keeping the way old code was structured.
| |
70 for (var i = 0; i < keys.length; i++) { | 66 typeof(message) == 'number' || |
podivilov
2012/07/13 11:07:55
wrong indentation
Anton Muhin
2012/07/13 15:30:58
Done.
| |
71 values[i] = serialize(message[keys[i]]); | 67 typeof(message) == 'boolean') { |
68 return message; | |
69 } else if (message instanceof Array) { | |
70 return checkedSerialization(message, function(id) { | |
podivilov
2012/07/13 11:07:55
up to you, but I think
var index = visited.indexOf
Anton Muhin
2012/07/13 15:30:58
Absolutely, thanks, I should learn JS core lib.
O
| |
71 var values = new Array(message.length); | |
72 for (var i = 0; i < message.length; i++) { | |
73 values[i] = do(message[i]); | |
74 } | |
75 return [ 'list', id, values ]; | |
76 }); | |
77 } else if (message instanceof LocalSendPortSync) { | |
78 return [ 'sendport', 'nativejs', message.receivePort.id ]; | |
79 } else if (message instanceof DartSendPortSync) { | |
80 return [ 'sendport', 'dart', message.isolateId, message.portId ]; | |
81 } else { | |
82 return checkedSerialization(message, function(id) { | |
83 var keys = Object.getOwnPropertyNames(message); | |
84 var values = new Array(keys.length); | |
85 for (var i = 0; i < keys.length; i++) { | |
86 values[i] = do(message[keys[i]]); | |
87 } | |
88 return [ 'map', id, keys, values ]; | |
89 }); | |
72 } | 90 } |
73 return [ 'map', id, keys, values ]; | |
74 } | 91 } |
92 return do(message); | |
75 } | 93 } |
76 | 94 |
77 function deserialize(message) { | 95 function deserialize(message) { |
78 return deserializeHelper(message); | 96 return deserializeHelper(message); |
79 } | 97 } |
80 | 98 |
81 function deserializeHelper(x) { | 99 function deserializeHelper(x) { |
82 if (x == null || | 100 if (x == null || |
83 typeof(x) == 'string' || | 101 typeof(x) == 'string' || |
84 typeof(x) == 'number' || | 102 typeof(x) == 'number' || |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 var result = null; | 226 var result = null; |
209 var listener = function (e) { | 227 var listener = function (e) { |
210 result = JSON.parse(e.data); | 228 result = JSON.parse(e.data); |
211 }; | 229 }; |
212 window.addEventListener(source, listener, false); | 230 window.addEventListener(source, listener, false); |
213 dispatchEvent(target, [source, serialized]); | 231 dispatchEvent(target, [source, serialized]); |
214 window.removeEventListener(source, listener, false); | 232 window.removeEventListener(source, listener, false); |
215 return deserialize(result); | 233 return deserialize(result); |
216 } | 234 } |
217 })(); | 235 })(); |
OLD | NEW |