| 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 19 matching lines...) Expand all Loading... |
| 30 parent.replaceChild(script, scripts[i]); | 30 parent.replaceChild(script, scripts[i]); |
| 31 } | 31 } |
| 32 } | 32 } |
| 33 } | 33 } |
| 34 }, false); | 34 }, false); |
| 35 } | 35 } |
| 36 | 36 |
| 37 // --------------------------------------------------------------------------- | 37 // --------------------------------------------------------------------------- |
| 38 // Experimental support for JS interoperability | 38 // Experimental support for JS interoperability |
| 39 // --------------------------------------------------------------------------- | 39 // --------------------------------------------------------------------------- |
| 40 | |
| 41 function SendPortSync(receivePort) { | 40 function SendPortSync(receivePort) { |
| 42 this.receivePort = receivePort; | 41 this.receivePort = receivePort; |
| 43 } | 42 } |
| 44 | 43 |
| 45 function ReceivePortSync() { | 44 function ReceivePortSync() { |
| 46 this.id = ReceivePortSync.id++; | 45 this.id = ReceivePortSync.id++; |
| 47 ReceivePortSync.map[this.id] = this; | 46 ReceivePortSync.map[this.id] = this; |
| 48 } | 47 } |
| 49 | 48 |
| 50 ReceivePortSync.id = 0; | 49 ReceivePortSync.id = 0; |
| 51 ReceivePortSync.map = {}; | 50 ReceivePortSync.map = {}; |
| 52 | 51 |
| 53 ReceivePortSync.prototype.receive = function(callback) { | 52 ReceivePortSync.prototype.receive = function(callback) { |
| 54 this.callback = callback; | 53 this.callback = callback; |
| 55 }; | 54 }; |
| 56 | 55 |
| 57 ReceivePortSync.prototype.toSendPort = function() { | 56 ReceivePortSync.prototype.toSendPort = function() { |
| 58 return new SendPortSync(this); | 57 return new SendPortSync(this); |
| 59 }; | 58 }; |
| 60 | 59 |
| 61 ReceivePortSync.prototype.close = function() { | 60 ReceivePortSync.prototype.close = function() { |
| 62 delete ReceivePortSync.map[this.id]; | 61 delete ReceivePortSync.map[this.id]; |
| 63 }; | 62 }; |
| 64 | 63 |
| 65 // TODO(kasperl): Hide these serialization methods so they | 64 (function() { |
| 66 // do not clutter up the global scope. | 65 function serialize(message) { |
| 67 function _serialize(message) { | 66 if (message == null) { |
| 68 if (message == null) { | 67 return null; // Convert undefined to null. |
| 69 return null; // Convert undefined to null. | 68 } else if (typeof(message) == 'string' || |
| 70 } else if (typeof(message) == 'string' || | 69 typeof(message) == 'number' || |
| 71 typeof(message) == 'number' || | 70 typeof(message) == 'boolean') { |
| 72 typeof(message) == 'boolean') { | 71 return message; |
| 73 return message; | 72 } else if (message instanceof SendPortSync) { |
| 74 } else if (message instanceof SendPortSync) { | 73 return [ 'sendport', message.receivePort.id ]; |
| 75 return [ 'sendport', message.receivePort.id ]; | 74 } else { |
| 76 } else { | 75 var id = 0; |
| 77 var id = 0; | 76 var keys = Object.getOwnPropertyNames(message); |
| 78 var keys = Object.getOwnPropertyNames(message); | 77 var values = new Array(keys.length); |
| 79 var values = new Array(keys.length); | 78 for (var i = 0; i < keys.length; i++) { |
| 80 for (var i = 0; i < keys.length; i++) { | 79 values[i] = message[keys[i]]; |
| 81 values[i] = message[keys[i]]; | 80 } |
| 81 return [ 'map', id, keys, values ]; |
| 82 } | 82 } |
| 83 return [ 'map', id, keys, values ]; | |
| 84 } | 83 } |
| 85 } | |
| 86 | 84 |
| 87 function _deserialize(message) { | 85 function deserialize(message) { |
| 88 return _deserializeHelper(message); | 86 return deserializeHelper(message); |
| 89 } | 87 } |
| 90 | 88 |
| 91 function _deserializeHelper(x) { | 89 function deserializeHelper(x) { |
| 92 if (x == null || | 90 if (x == null || |
| 93 typeof(x) == 'string' || | 91 typeof(x) == 'string' || |
| 94 typeof(x) == 'number' || | 92 typeof(x) == 'number' || |
| 95 typeof(x) == 'boolean') { | 93 typeof(x) == 'boolean') { |
| 96 return x; | 94 return x; |
| 95 } |
| 96 switch (x[0]) { |
| 97 case 'map': return deserializeMap(x); |
| 98 default: throw 'unimplemented'; |
| 99 } |
| 97 } | 100 } |
| 98 switch (x[0]) { | 101 |
| 99 case 'map': return _deserializeMap(x); | 102 function deserializeMap(x) { |
| 100 default: throw 'unimplemented'; | 103 var result = { }; |
| 104 var id = x[1]; |
| 105 var keys = x[2]; |
| 106 var values = x[3]; |
| 107 for (var i = 0, length = keys.length; i < length; i++) { |
| 108 var key = deserializeHelper(keys[i]); |
| 109 var value = deserializeHelper(values[i]); |
| 110 result[key] = value; |
| 111 } |
| 112 return result; |
| 101 } | 113 } |
| 102 } | |
| 103 | 114 |
| 104 function _deserializeMap(x) { | 115 window.registerPort = function(name, port) { |
| 105 var result = { }; | 116 var stringified = JSON.stringify(serialize(port)); |
| 106 var id = x[1]; | 117 window.localStorage['dart-port:' + name] = stringified; |
| 107 var keys = x[2]; | 118 }; |
| 108 var values = x[3]; | 119 |
| 109 for (var i = 0, length = keys.length; i < length; i++) { | 120 ReceivePortSync.dispatchCall = function(id, message) { |
| 110 var key = _deserializeHelper(keys[i]); | 121 var deserialized = deserialize(message); |
| 111 var value = _deserializeHelper(values[i]); | 122 var result = ReceivePortSync.map[id].callback(deserialized); |
| 112 result[key] = value; | 123 return serialize(result); |
| 124 }; |
| 125 |
| 126 if (navigator.webkitStartDart) { |
| 127 window.addEventListener('js-sync-message', function(event) { |
| 128 var data = JSON.parse(event.data); |
| 129 var deserialized = deserialize(data.message); |
| 130 var result = ReceivePortSync.map[data.id].callback(deserialized); |
| 131 var string = JSON.stringify(serialize(result)); |
| 132 var event = document.createEvent('TextEvent'); |
| 133 event.initTextEvent('js-result', false, false, window, string); |
| 134 window.dispatchEvent(event); |
| 135 }, false); |
| 113 } | 136 } |
| 114 return result; | 137 })(); |
| 115 } | |
| 116 | |
| 117 function registerPort(name, port) { | |
| 118 var stringified = JSON.stringify(_serialize(port)); | |
| 119 window.localStorage['dart-port:' + name] = stringified; | |
| 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 |