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

Side by Side Diff: client/dart.js

Issue 10834426: Support general serializing of functions between JS and Dart (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address review comments Created 8 years, 4 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
« no previous file with comments | « no previous file | lib/html/dart2js/html_dart2js.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 28 matching lines...) Expand all
39 // --------------------------------------------------------------------------- 39 // ---------------------------------------------------------------------------
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 // Track proxied functions.
50 // TODO: Fix leaks, particularly in dart2js case.
51 var functionRefMap = {};
52
53 var nextFunctionRefId = 0;
54
55 function functionRefDispatch(message) {
56 var id = message[0];
57 var args = message[1];
58 var f = functionRefMap[id];
59 // TODO: Should we capture this automatically?
60 return f.apply(null, args);
61 }
62
63 var functionRefPort = null;
64
65 function makeFunctionRef(f) {
66 if (functionRefPort == null) {
67 var port = new ReceivePortSync();
68 port.receive(functionRefDispatch);
69 functionRefPort = port.toSendPort();
70 }
71 var ref = 'func-ref-' + (nextFunctionRefId++);
72 functionRefMap[ref] = f;
73 return ref;
74 }
75
49 function serialize(message) { 76 function serialize(message) {
50 var visited = []; 77 var visited = [];
51 function checkedSerialization(obj, serializer) { 78 function checkedSerialization(obj, serializer) {
52 // Implementation detail: for now use linear search. 79 // Implementation detail: for now use linear search.
53 // Another option is expando, but it may prohibit 80 // Another option is expando, but it may prohibit
54 // VM optimizations (like putting object into slow mode 81 // VM optimizations (like putting object into slow mode
55 // on property deletion.) 82 // on property deletion.)
56 var id = visited.indexOf(obj); 83 var id = visited.indexOf(obj);
57 if (id != -1) return [ 'ref', id ]; 84 if (id != -1) return [ 'ref', id ];
58 var id = visited.length; 85 var id = visited.length;
(...skipping 13 matching lines...) Expand all
72 var values = new Array(message.length); 99 var values = new Array(message.length);
73 for (var i = 0; i < message.length; i++) { 100 for (var i = 0; i < message.length; i++) {
74 values[i] = doSerialize(message[i]); 101 values[i] = doSerialize(message[i]);
75 } 102 }
76 return [ 'list', id, values ]; 103 return [ 'list', id, values ];
77 }); 104 });
78 } else if (message instanceof LocalSendPortSync) { 105 } else if (message instanceof LocalSendPortSync) {
79 return [ 'sendport', 'nativejs', message.receivePort.id ]; 106 return [ 'sendport', 'nativejs', message.receivePort.id ];
80 } else if (message instanceof DartSendPortSync) { 107 } else if (message instanceof DartSendPortSync) {
81 return [ 'sendport', 'dart', message.isolateId, message.portId ]; 108 return [ 'sendport', 'dart', message.isolateId, message.portId ];
109 } else if (message instanceof Function) {
110 return [ 'funcref', makeFunctionRef(message),
111 doSerialize(functionRefPort) ];
82 } else { 112 } else {
83 return checkedSerialization(message, function(id) { 113 return checkedSerialization(message, function(id) {
84 var keys = Object.getOwnPropertyNames(message); 114 var keys = Object.getOwnPropertyNames(message);
85 var values = new Array(keys.length); 115 var values = new Array(keys.length);
86 for (var i = 0; i < keys.length; i++) { 116 for (var i = 0; i < keys.length; i++) {
87 values[i] = doSerialize(message[keys[i]]); 117 values[i] = doSerialize(message[keys[i]]);
88 } 118 }
89 return [ 'map', id, keys, values ]; 119 return [ 'map', id, keys, values ];
90 }); 120 });
91 } 121 }
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 } 274 }
245 275
246 // Leaking implementation. 276 // Leaking implementation.
247 // TODO: provide proper, backend-specific implementation. 277 // TODO: provide proper, backend-specific implementation.
248 function _makeFunctionFromRef(ref, sendPort) { 278 function _makeFunctionFromRef(ref, sendPort) {
249 return function() { 279 return function() {
250 return sendPort.callSync([ref, Array.prototype.slice.call(arguments)]); 280 return sendPort.callSync([ref, Array.prototype.slice.call(arguments)]);
251 } 281 }
252 } 282 }
253 })(); 283 })();
OLDNEW
« no previous file with comments | « no previous file | lib/html/dart2js/html_dart2js.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698