Skip to content

Commit

Permalink
Make isolate patch play nice with web workers.
Browse files Browse the repository at this point in the history
Closes dart-lang/sdk#32438

Change-Id: I14163ef868365bf5faecfdfaccb54047391e0f80
Reviewed-on: https://dart-review.googlesource.com/48520
Commit-Queue: Sigmund Cherem <[email protected]>
Reviewed-by: Stephen Adams <[email protected]>
  • Loading branch information
sigmundch authored and [email protected] committed Mar 28, 2018
1 parent be3baa0 commit 2de8241
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
10 changes: 9 additions & 1 deletion sdk/lib/_internal/js_runtime/lib/isolate_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,15 @@ class IsolateNatives {
* pass messages along to the isolate running in the worker.
*/
static void _processWorkerMessage(/* Worker */ sender, e) {
var msg = _deserializeMessage(_getEventData(e));
// Since we are listening on a global event, be graceful about other
// messages that may not belong to the isolate communication.
// See Issue #32438
var data = _getEventData(e);
if (!_isIsolateMessage(data)) return;

var msg = _deserializeMessage(data);
if (msg is! JSObject && msg is! Map) return;

switch (msg['command']) {
case 'start':
_globalState.currentManagerId = msg['id'];
Expand Down
33 changes: 29 additions & 4 deletions sdk/lib/_internal/js_runtime/lib/isolate_serialization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,31 @@ _deserializeMessage(message) {
return new _Deserializer().deserialize(message);
}

bool _isIsolateMessage(message) {
if (_isPrimitive(message)) return true;
if (message is! JSArray) return false;
if (message.isEmpty) return false;
switch (message.first) {
case "ref":
case "buffer":
case "typed":
case "fixed":
case "extendable":
case "mutable":
case "const":
case "map":
case "sendport":
case "raw sendport":
case "js-object":
case "function":
case "capability":
case "dart":
return true;
default:
return false;
}
}

/// Clones the message.
///
/// Contrary to a `_deserializeMessage(_serializeMessage(x))` the `_clone`
Expand All @@ -33,7 +58,7 @@ class _Serializer {

/// Returns a message that can be transmitted through web-worker channels.
serialize(x) {
if (isPrimitive(x)) return serializePrimitive(x);
if (_isPrimitive(x)) return serializePrimitive(x);

int serializationId = serializedObjectIds[x];
if (serializationId != null) return makeRef(serializationId);
Expand Down Expand Up @@ -73,7 +98,6 @@ class _Serializer {

makeRef(int serializationId) => ["ref", serializationId];

bool isPrimitive(x) => x == null || x is String || x is num || x is bool;
serializePrimitive(primitive) => primitive;

serializeByteBuffer(NativeByteBuffer buffer) {
Expand Down Expand Up @@ -190,7 +214,7 @@ class _Deserializer {

/// Returns a message that can be transmitted through web-worker channels.
deserialize(x) {
if (isPrimitive(x)) return deserializePrimitive(x);
if (_isPrimitive(x)) return deserializePrimitive(x);

if (x is! JSArray) throw new ArgumentError("Bad serialized message: $x");

Expand Down Expand Up @@ -228,7 +252,6 @@ class _Deserializer {
}
}

bool isPrimitive(x) => x == null || x is String || x is num || x is bool;
deserializePrimitive(x) => x;

// ['ref', id].
Expand Down Expand Up @@ -385,3 +408,5 @@ class _Deserializer {
'', '#(#, #, #)', initializeObject, classId, emptyInstance, fields);
}
}

bool _isPrimitive(x) => x == null || x is String || x is num || x is bool;

0 comments on commit 2de8241

Please sign in to comment.