Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1209)

Side by Side Diff: runtime/bin/vmstats/packages/browser/dart.js

Issue 12377099: Added support for "bin-ified" vmstats web app source files (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 // Bootstrap support for Dart scripts on the page as this script.
6 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';
9 }
10 } else {
11 // TODO:
12 // - Support in-browser compilation.
13 // - Handle inline Dart scripts.
14 window.addEventListener("DOMContentLoaded", function (e) {
15 // Fall back to compiled JS. Run through all the scripts and
16 // replace them if they have a type that indicate that they source
17 // in Dart code.
18 //
19 // <script type="application/dart" src="..."></script>
20 //
21 var scripts = document.getElementsByTagName("script");
22 var length = scripts.length;
23 for (var i = 0; i < length; ++i) {
24 if (scripts[i].type == "application/dart") {
25 // Remap foo.dart to foo.dart.js.
26 if (scripts[i].src && scripts[i].src != '') {
27 var script = document.createElement('script');
28 script.src = scripts[i].src + '.js';
29 var parent = scripts[i].parentNode;
30 parent.replaceChild(script, scripts[i]);
31 }
32 }
33 }
34 }, false);
35 }
36
37 // ---------------------------------------------------------------------------
38 // Experimental support for JS interoperability
39 // ---------------------------------------------------------------------------
40 function SendPortSync() {
41 }
42
43 function ReceivePortSync() {
44 this.id = ReceivePortSync.id++;
45 ReceivePortSync.map[this.id] = this;
46 }
47
48 (function() {
49 // Serialize the following types as follows:
50 // - primitives / null: unchanged
51 // - lists: [ 'list', internal id, list of recursively serialized elements ]
52 // - maps: [ 'map', internal id, map of keys and recursively serialized value s ]
53 // - send ports: [ 'sendport', type, isolate id, port id ]
54 //
55 // Note, internal id's are for cycle detection.
56 function serialize(message) {
57 var visited = [];
58 function checkedSerialization(obj, serializer) {
59 // Implementation detail: for now use linear search.
60 // Another option is expando, but it may prohibit
61 // VM optimizations (like putting object into slow mode
62 // on property deletion.)
63 var id = visited.indexOf(obj);
64 if (id != -1) return [ 'ref', id ];
65 var id = visited.length;
66 visited.push(obj);
67 return serializer(id);
68 }
69
70 function doSerialize(message) {
71 if (message == null) {
72 return null; // Convert undefined to null.
73 } else if (typeof(message) == 'string' ||
74 typeof(message) == 'number' ||
75 typeof(message) == 'boolean') {
76 return message;
77 } else if (message instanceof Array) {
78 return checkedSerialization(message, function(id) {
79 var values = new Array(message.length);
80 for (var i = 0; i < message.length; i++) {
81 values[i] = doSerialize(message[i]);
82 }
83 return [ 'list', id, values ];
84 });
85 } else if (message instanceof LocalSendPortSync) {
86 return [ 'sendport', 'nativejs', message.receivePort.id ];
87 } else if (message instanceof DartSendPortSync) {
88 return [ 'sendport', 'dart', message.isolateId, message.portId ];
89 } else {
90 return checkedSerialization(message, function(id) {
91 var keys = Object.getOwnPropertyNames(message);
92 var values = new Array(keys.length);
93 for (var i = 0; i < keys.length; i++) {
94 values[i] = doSerialize(message[keys[i]]);
95 }
96 return [ 'map', id, keys, values ];
97 });
98 }
99 }
100 return doSerialize(message);
101 }
102
103 function deserialize(message) {
104 return deserializeHelper(message);
105 }
106
107 function deserializeHelper(message) {
108 if (message == null ||
109 typeof(message) == 'string' ||
110 typeof(message) == 'number' ||
111 typeof(message) == 'boolean') {
112 return message;
113 }
114 switch (message[0]) {
115 case 'map': return deserializeMap(message);
116 case 'sendport': return deserializeSendPort(message);
117 case 'list': return deserializeList(message);
118 default: throw 'unimplemented';
119 }
120 }
121
122 function deserializeMap(message) {
123 var result = { };
124 var id = message[1];
125 var keys = message[2];
126 var values = message[3];
127 for (var i = 0, length = keys.length; i < length; i++) {
128 var key = deserializeHelper(keys[i]);
129 var value = deserializeHelper(values[i]);
130 result[key] = value;
131 }
132 return result;
133 }
134
135 function deserializeSendPort(message) {
136 var tag = message[1];
137 switch (tag) {
138 case 'nativejs':
139 var id = message[2];
140 return new LocalSendPortSync(ReceivePortSync.map[id]);
141 case 'dart':
142 var isolateId = message[2];
143 var portId = message[3];
144 return new DartSendPortSync(isolateId, portId);
145 default:
146 throw 'Illegal SendPortSync type: $tag';
147 }
148 }
149
150 function deserializeList(message) {
151 var values = message[2];
152 var length = values.length;
153 var result = new Array(length);
154 for (var i = 0; i < length; i++) {
155 result[i] = deserializeHelper(values[i]);
156 }
157 return result;
158 }
159
160 window.registerPort = function(name, port) {
161 var stringified = JSON.stringify(serialize(port));
162 var attrName = 'dart-port:' + name;
163 document.documentElement.setAttribute(attrName, stringified);
164 };
165
166 window.lookupPort = function(name) {
167 var attrName = 'dart-port:' + name;
168 var stringified = document.documentElement.getAttribute(attrName);
169 return deserialize(JSON.parse(stringified));
170 };
171
172 ReceivePortSync.id = 0;
173 ReceivePortSync.map = {};
174
175 ReceivePortSync.dispatchCall = function(id, message) {
176 // TODO(vsm): Handle and propagate exceptions.
177 var deserialized = deserialize(message);
178 var result = ReceivePortSync.map[id].callback(deserialized);
179 return serialize(result);
180 };
181
182 ReceivePortSync.prototype.receive = function(callback) {
183 this.callback = callback;
184 };
185
186 ReceivePortSync.prototype.toSendPort = function() {
187 return new LocalSendPortSync(this);
188 };
189
190 ReceivePortSync.prototype.close = function() {
191 delete ReceivePortSync.map[this.id];
192 };
193
194 if (navigator.webkitStartDart) {
195 window.addEventListener('js-sync-message', function(event) {
196 var data = JSON.parse(getPortSyncEventData(event));
197 var deserialized = deserialize(data.message);
198 var result = ReceivePortSync.map[data.id].callback(deserialized);
199 // TODO(vsm): Handle and propagate exceptions.
200 dispatchEvent('js-result', serialize(result));
201 }, false);
202 }
203
204 function LocalSendPortSync(receivePort) {
205 this.receivePort = receivePort;
206 }
207
208 LocalSendPortSync.prototype = new SendPortSync();
209
210 LocalSendPortSync.prototype.callSync = function(message) {
211 // TODO(vsm): Do a direct deepcopy.
212 message = deserialize(serialize(message));
213 return this.receivePort.callback(message);
214 }
215
216 function DartSendPortSync(isolateId, portId) {
217 this.isolateId = isolateId;
218 this.portId = portId;
219 }
220
221 DartSendPortSync.prototype = new SendPortSync();
222
223 function dispatchEvent(receiver, message) {
224 var string = JSON.stringify(message);
225 var event = document.createEvent('CustomEvent');
226 event.initCustomEvent(receiver, false, false, string);
227 window.dispatchEvent(event);
228 }
229
230 function getPortSyncEventData(event) {
231 return event.detail;
232 }
233
234 DartSendPortSync.prototype.callSync = function(message) {
235 var serialized = serialize(message);
236 var target = 'dart-port-' + this.isolateId + '-' + this.portId;
237 // TODO(vsm): Make this re-entrant.
238 // TODO(vsm): Set this up set once, on the first call.
239 var source = target + '-result';
240 var result = null;
241 var listener = function (e) {
242 result = JSON.parse(getPortSyncEventData(e));
243 };
244 window.addEventListener(source, listener, false);
245 dispatchEvent(target, [source, serialized]);
246 window.removeEventListener(source, listener, false);
247 return deserialize(result);
248 }
249 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698