Index: lib/isolate/frog/messages.dart |
diff --git a/frog/lib/isolate_serialization.dart b/lib/isolate/frog/messages.dart |
similarity index 86% |
rename from frog/lib/isolate_serialization.dart |
rename to lib/isolate/frog/messages.dart |
index 17cc532be9ee4d89abc761e34ba79f4177a60aa3..e59f814c922e23df179543a4a9688d4de0daa6c9 100644 |
--- a/frog/lib/isolate_serialization.dart |
+++ b/lib/isolate/frog/messages.dart |
@@ -2,14 +2,36 @@ |
// for details. All rights reserved. Use of this source code is governed by a |
// BSD-style license that can be found in the LICENSE file. |
-/** |
- * Abstract visitor for dart objects that can be passed as messages between any |
- * isolates. |
- */ |
-class MessageTraverser { |
- static bool isPrimitive(x) { |
- return (x === null) || (x is String) || (x is num) || (x is bool); |
+// Defines message visitors, serialization, and deserialization. |
+#library("isolate.frog.messages"); |
+ |
+#import("dart:isolate"); |
+#import("ports.dart"); |
+#import("isolateimpl.dart"); |
+ |
+/** Serialize [message] (or simulate serialization). */ |
+serialize(message) { |
+ if (globalState.needSerialization) { |
+ return new Serializer().traverse(message); |
+ } else { |
+ return new Copier().traverse(message); |
} |
+} |
+ |
+/** Deserialize [message] (or simulate deserialization). */ |
+deserialize(message) { |
+ if (globalState.needSerialization) { |
+ return new Deserializer().deserialize(message); |
+ } else { |
+ // Nothing more to do. |
+ return message; |
+ } |
+} |
+ |
+/** Abstract visitor for dart objects that can be sent as isolate messages. */ |
+class MessageTraverser { |
+ |
+ List _taggedObjects; |
MessageTraverser(); |
@@ -68,8 +90,6 @@ class MessageTraverser { |
abstract visitReceivePort(ReceivePortImpl x); |
abstract visitReceivePortSingleShot(ReceivePortSingleShotImpl x); |
- List _taggedObjects; |
- |
_clearAttachedInfo(var o) native |
"o['__MessageTraverser__attached_info__'] = (void 0);"; |
@@ -84,8 +104,13 @@ class MessageTraverser { |
if (p is WorkerSendPort) return visitWorkerSendPort(p); |
throw "Illegal underlying port $p"; |
} |
+ |
+ static bool isPrimitive(x) { |
+ return (x === null) || (x is String) || (x is num) || (x is bool); |
+ } |
} |
+ |
/** A visitor that recursively copies a message. */ |
class Copier extends MessageTraverser { |
Copier() : super(); |
@@ -150,6 +175,8 @@ class Copier extends MessageTraverser { |
/** Visitor that serializes a message as a JSON array. */ |
class Serializer extends MessageTraverser { |
+ int _nextFreeRefId = 0; |
+ |
Serializer() : super(); |
visitPrimitive(x) => x; |
@@ -178,7 +205,7 @@ class Serializer extends MessageTraverser { |
} |
visitNativeJsSendPort(NativeJsSendPort port) { |
- return ['sendport', _globalState.currentWorkerId, |
+ return ['sendport', globalState.currentWorkerId, |
port._isolateId, port._receivePort._id]; |
} |
@@ -212,50 +239,12 @@ class Serializer extends MessageTraverser { |
} |
return result; |
} |
- |
- int _nextFreeRefId = 0; |
} |
-/** Visitor that finds all unresolved [SendPort]s in a message. */ |
-class PendingSendPortFinder extends MessageTraverser { |
- List<Future<SendPort>> ports; |
- PendingSendPortFinder() : super(), ports = []; |
- |
- visitPrimitive(x) {} |
- visitNativeJsSendPort(NativeJsSendPort port) {} |
- visitWorkerSendPort(WorkerSendPort port) {} |
- visitReceivePort(ReceivePortImpl port) {} |
- visitReceivePortSingleShot(ReceivePortSingleShotImpl port) {} |
- |
- visitList(List list) { |
- final visited = _getInfo(list); |
- if (visited !== null) return; |
- _attachInfo(list, true); |
- // TODO(sigmund): replace with the following: (bug #1660) |
- // list.forEach(_dispatch); |
- list.forEach((e) => _dispatch(e)); |
- } |
- |
- visitMap(Map map) { |
- final visited = _getInfo(map); |
- if (visited !== null) return; |
- |
- _attachInfo(map, true); |
- // TODO(sigmund): replace with the following: (bug #1660) |
- // map.getValues().forEach(_dispatch); |
- map.getValues().forEach((e) => _dispatch(e)); |
- } |
- |
- visitBufferingSendPort(BufferingSendPort port) { |
- if (port._port == null) { |
- ports.add(port._futurePort); |
- } |
- } |
-} |
- |
- |
/** Deserializes arrays created with [Serializer]. */ |
class Deserializer { |
+ Map<int, Dynamic> _deserialized; |
+ |
Deserializer(); |
static bool isPrimitive(x) { |
@@ -323,8 +312,8 @@ class Deserializer { |
int receivePortId = x[3]; |
// If two isolates are in the same worker, we use NativeJsSendPorts to |
// deliver messages directly without using postMessage. |
- if (workerId == _globalState.currentWorkerId) { |
- var isolate = _globalState.isolates[isolateId]; |
+ if (workerId == globalState.currentWorkerId) { |
+ var isolate = globalState.isolates[isolateId]; |
if (isolate == null) return null; // Isolate has been closed. |
var receivePort = isolate.lookup(receivePortId); |
return new NativeJsSendPort(receivePort, isolateId); |
@@ -332,6 +321,4 @@ class Deserializer { |
return new WorkerSendPort(workerId, isolateId, receivePortId); |
} |
} |
- |
- Map<int, Dynamic> _deserialized; |
} |