| 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 return new SendPortSync(this); | 58 return new SendPortSync(this); |
| 59 }; | 59 }; |
| 60 | 60 |
| 61 ReceivePortSync.prototype.close = function() { | 61 ReceivePortSync.prototype.close = function() { |
| 62 delete ReceivePortSync.map[this.id]; | 62 delete ReceivePortSync.map[this.id]; |
| 63 }; | 63 }; |
| 64 | 64 |
| 65 // TODO(kasperl): Hide these serialization methods so they | 65 // TODO(kasperl): Hide these serialization methods so they |
| 66 // do not clutter up the global scope. | 66 // do not clutter up the global scope. |
| 67 function _serialize(message) { | 67 function _serialize(message) { |
| 68 if (message == null || | 68 if (message == null) { |
| 69 typeof(message) == 'string' || | 69 return null; // Convert undefined to null. |
| 70 typeof(message) == 'number' || | 70 } else if (typeof(message) == 'string' || |
| 71 typeof(message) == 'boolean') { | 71 typeof(message) == 'number' || |
| 72 typeof(message) == 'boolean') { |
| 72 return message; | 73 return message; |
| 73 } else if (message instanceof SendPortSync) { | 74 } else if (message instanceof SendPortSync) { |
| 74 return [ 'sendport', message.receivePort.id ]; | 75 return [ 'sendport', message.receivePort.id ]; |
| 75 } else { | 76 } else { |
| 76 var id = 0; | 77 var id = 0; |
| 77 var keys = Object.getOwnPropertyNames(message); | 78 var keys = Object.getOwnPropertyNames(message); |
| 78 var values = new Array(keys.length); | 79 var values = new Array(keys.length); |
| 79 for (var i = 0; i < keys.length; i++) { | 80 for (var i = 0; i < keys.length; i++) { |
| 80 values[i] = message[keys[i]]; | 81 values[i] = message[keys[i]]; |
| 81 } | 82 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 110 var value = _deserializeHelper(values[i]); | 111 var value = _deserializeHelper(values[i]); |
| 111 result[key] = value; | 112 result[key] = value; |
| 112 } | 113 } |
| 113 return result; | 114 return result; |
| 114 } | 115 } |
| 115 | 116 |
| 116 function registerPort(name, port) { | 117 function registerPort(name, port) { |
| 117 var stringified = JSON.stringify(_serialize(port)); | 118 var stringified = JSON.stringify(_serialize(port)); |
| 118 window.localStorage['dart-port:' + name] = stringified; | 119 window.localStorage['dart-port:' + name] = stringified; |
| 119 } | 120 } |
| 121 |
| 122 if (navigator.webkitStartDart) { |
| 123 window.addEventListener('js-sync-message', function(event) { |
| 124 var data = JSON.parse(event.data); |
| 125 var deserialized = _deserialize(data.message); |
| 126 var result = ReceivePortSync.map[data.id].callback(deserialized); |
| 127 var string = JSON.stringify(_serialize(result)); |
| 128 var event = document.createEvent('TextEvent'); |
| 129 event.initTextEvent('js-result', false, false, window, string); |
| 130 window.dispatchEvent(event); |
| 131 }, false); |
| 132 } |
| OLD | NEW |