Make isolate patch play nice with web workers.

Closes https://github.com/dart-lang/sdk/issues/32438

Change-Id: I14163ef868365bf5faecfdfaccb54047391e0f80
Reviewed-on: https://dart-review.googlesource.com/48520
Commit-Queue: Sigmund Cherem <sigmund@google.com>
Reviewed-by: Stephen Adams <sra@google.com>
This commit is contained in:
Sigmund Cherem 2018-03-28 19:14:18 +00:00 committed by commit-bot@chromium.org
parent be3baa0b8f
commit 2de82410e0
2 changed files with 38 additions and 5 deletions

View file

@ -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'];

View file

@ -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`
@ -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);
@ -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) {
@ -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");
@ -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].
@ -385,3 +408,5 @@ class _Deserializer {
'', '#(#, #, #)', initializeObject, classId, emptyInstance, fields);
}
}
bool _isPrimitive(x) => x == null || x is String || x is num || x is bool;