Index: client/dart.js |
diff --git a/client/dart.js b/client/dart.js |
index 711aeee5702478258e964aad47a196e109154701..1985ee133b7c5452763c32ee3a42130968d9ebb6 100644 |
--- a/client/dart.js |
+++ b/client/dart.js |
@@ -46,31 +46,66 @@ function ReceivePortSync() { |
} |
(function() { |
- // Track proxied functions. |
- // TODO: Fix leaks, particularly in dart2js case. |
- var functionRefMap = {}; |
- |
- var nextFunctionRefId = 0; |
- |
- function functionRefDispatch(message) { |
- var id = message[0]; |
- var args = message[1]; |
- var f = functionRefMap[id]; |
- // TODO: Should we capture this automatically? |
- return f.apply(null, args); |
+ function RefTable(name) { |
+ // TODO: Fix leaks, particularly in dart2js case. |
Emily Fortuna
2012/08/21 23:39:56
TODO(vsm):
vsm
2012/08/22 00:04:28
Done.
|
+ this.name = name; |
+ this.map = {}; |
+ this.id = 0; |
+ this.initialized = false; |
+ } |
+ |
+ RefTable.prototype.nextId = function () { return this.id++; } |
+ |
+ RefTable.prototype.makeRef = function (obj) { |
+ this.initializeOnce(); |
+ // TODO: Cache refs for each obj. |
Emily Fortuna
2012/08/21 23:39:56
add username to TODO :-)
vsm
2012/08/22 00:04:28
Done.
|
+ var ref = this.name + '-' + this.nextId(); |
+ this.map[ref] = obj; |
Emily Fortuna
2012/08/21 23:39:56
Is this for improved debugging experience (could b
vsm
2012/08/22 00:04:28
Primarily debugging ease. Makes it easier to tell
|
+ return ref; |
} |
- var functionRefPort = null; |
- |
- function makeFunctionRef(f) { |
- if (functionRefPort == null) { |
- var port = new ReceivePortSync(); |
- port.receive(functionRefDispatch); |
- functionRefPort = port.toSendPort(); |
+ RefTable.prototype.initializeOnce = function () { |
+ if (!this.initialized) { |
+ this.initialize(); |
} |
- var ref = 'func-ref-' + (nextFunctionRefId++); |
- functionRefMap[ref] = f; |
- return ref; |
+ this.initialized = true; |
+ } |
+ |
+ // Overridable initialization on first use hook. |
+ RefTable.prototype.initialize = function () { |
+ } |
Emily Fortuna
2012/08/21 23:39:56
move this up to the previous line? http://www.corp
vsm
2012/08/22 00:04:28
Done.
|
+ |
+ RefTable.prototype.get = function (ref) { |
+ return this.map[ref]; |
+ } |
+ |
+ function FunctionRefTable() { |
+ } |
Emily Fortuna
2012/08/21 23:39:56
probably can move this } up onto the previous line
vsm
2012/08/22 00:04:28
Done.
|
+ FunctionRefTable.prototype = new RefTable('func-ref'); |
+ |
+ FunctionRefTable.prototype.initialize = function () { |
+ var receivePort = new ReceivePortSync(); |
+ map = this.map; |
+ receivePort.receive(function (message) { |
+ var id = message[0]; |
+ var args = message[1]; |
+ var f = map[id]; |
+ // TODO: Should we capture this automatically? |
Emily Fortuna
2012/08/21 23:39:56
TODO(vsm):
vsm
2012/08/22 00:04:28
Done.
|
+ return f.apply(null, args); |
+ }); |
+ this.port = receivePort.toSendPort(); |
+ } |
+ |
+ var functionRefTable = new FunctionRefTable(); |
+ |
+ function JSRefTable() { |
+ } |
+ JSRefTable.prototype = new RefTable('js-ref'); |
Emily Fortuna
2012/08/21 23:39:56
add an extra whitespace line above
vsm
2012/08/22 00:04:28
Done.
|
+ var jsRefTable = new JSRefTable(); |
+ |
+ function DartProxy(id) { |
+ // TODO(vsm): Set isolate id. |
+ this.id = id; |
} |
function serialize(message) { |
@@ -107,8 +142,14 @@ function ReceivePortSync() { |
} else if (message instanceof DartSendPortSync) { |
return [ 'sendport', 'dart', message.isolateId, message.portId ]; |
} else if (message instanceof Function) { |
- return [ 'funcref', makeFunctionRef(message), |
- doSerialize(functionRefPort) ]; |
+ return [ 'funcref', functionRefTable.makeRef(message), |
+ doSerialize(functionRefTable.port) ]; |
+ } else if (message instanceof DartProxy) { |
+ return [ 'objref', 'dart', message.id ]; |
+ } else if (message.__proto__ != {}.__proto) { |
+ // TODO(vsm): Is the above portable and what we want? |
Emily Fortuna
2012/08/21 23:39:56
remove tabs here
vsm
2012/08/22 00:04:28
Done.
|
+ // Proxy non-map Objects. |
+ return [ 'objref', 'nativejs', jsRefTable.makeRef(message) ]; |
Emily Fortuna
2012/08/21 23:39:56
I thought all objects in JS could be examined as m
vsm
2012/08/22 00:04:28
Right. The proto check in the preceding if is try
|
} else { |
return checkedSerialization(message, function(id) { |
var keys = Object.getOwnPropertyNames(message); |
@@ -139,6 +180,7 @@ function ReceivePortSync() { |
case 'sendport': return deserializeSendPort(x); |
case 'list': return deserializeList(x); |
case 'funcref': return deserializeFunction(x); |
+ case 'objref': return deserializeProxy(x); |
default: throw 'unimplemented'; |
} |
} |
@@ -189,6 +231,18 @@ function ReceivePortSync() { |
return _makeFunctionFromRef(ref, sendPort); |
} |
+ function deserializeProxy(x) { |
+ var tag = x[1]; |
+ if (tag == 'nativejs') { |
+ var id = x[2]; |
+ return jsRefTable.map[id]; |
+ } else if (tag == 'dart') { |
+ var id = x[2]; |
+ return new DartProxy(id); |
+ } |
+ throw 'Illegal proxy object: ' + x; |
+ } |
+ |
window.registerPort = function(name, port) { |
var stringified = JSON.stringify(serialize(port)); |
window.localStorage['dart-port:' + name] = stringified; |