Bug 1312817 - support {window,worker}.postMessage() of WebAssembly.Module, r=qdot, r=luke

This commit is contained in:
Andrea Marchesini 2016-10-27 20:50:23 +02:00
Родитель f6a19be2ac
Коммит 937298e1fb
4 изменённых файлов: 176 добавлений и 28 удалений

Просмотреть файл

@ -997,6 +997,41 @@ WriteFormData(JSStructuredCloneWriter* aWriter,
return aFormData->ForEach(Closure::Write, &closure);
}
JSObject*
ReadWasmModule(JSContext* aCx,
uint32_t aIndex,
StructuredCloneHolder* aHolder)
{
MOZ_ASSERT(aHolder);
MOZ_ASSERT(aIndex < aHolder->WasmModules().Length());
MOZ_ASSERT(aHolder->CloneScope() == StructuredCloneHolder::StructuredCloneScope::SameProcessSameThread ||
aHolder->CloneScope() == StructuredCloneHolder::StructuredCloneScope::SameProcessDifferentThread);
RefPtr<JS::WasmModule> wasmModule = aHolder->WasmModules()[aIndex];
return wasmModule->createObject(aCx);
}
bool
WriteWasmModule(JSStructuredCloneWriter* aWriter,
JS::WasmModule* aWasmModule,
StructuredCloneHolder* aHolder)
{
MOZ_ASSERT(aWriter);
MOZ_ASSERT(aWasmModule);
MOZ_ASSERT(aHolder);
MOZ_ASSERT(aHolder->CloneScope() == StructuredCloneHolder::StructuredCloneScope::SameProcessSameThread ||
aHolder->CloneScope() == StructuredCloneHolder::StructuredCloneScope::SameProcessDifferentThread);
// We store the position of the wasmModule in the array as index.
if (JS_WriteUint32Pair(aWriter, SCTAG_DOM_WASM,
aHolder->WasmModules().Length())) {
aHolder->WasmModules().AppendElement(aWasmModule);
return true;
}
return false;
}
} // anonymous namespace
JSObject*
@ -1033,7 +1068,11 @@ StructuredCloneHolder::CustomReadHandler(JSContext* aCx,
// aIndex is the index of the cloned image.
return ImageBitmap::ReadStructuredClone(aCx, aReader,
parent, GetSurfaces(), aIndex);
}
}
if (aTag == SCTAG_DOM_WASM) {
return ReadWasmModule(aCx, aIndex, this);
}
return ReadFullySerializableObjects(aCx, aReader, aTag);
}
@ -1095,6 +1134,16 @@ StructuredCloneHolder::CustomWriteHandler(JSContext* aCx,
}
}
// See if this is a WasmModule.
if ((mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread ||
mStructuredCloneScope == StructuredCloneScope::SameProcessDifferentThread) &&
JS::IsWasmModuleObject(aObj)) {
RefPtr<JS::WasmModule> module = JS::GetWasmModule(aObj);
MOZ_ASSERT(module);
return WriteWasmModule(aWriter, module, this);
}
return WriteFullySerializableObjects(aCx, aWriter, aObj);
}

Просмотреть файл

@ -6,6 +6,7 @@
#ifndef mozilla_dom_StructuredCloneHolder_h
#define mozilla_dom_StructuredCloneHolder_h
#include "jsapi.h"
#include "js/StructuredClone.h"
#include "mozilla/Move.h"
#include "mozilla/UniquePtr.h"
@ -178,6 +179,7 @@ public:
bool HasClonedDOMObjects() const
{
return !mBlobImplArray.IsEmpty() ||
!mWasmModuleArray.IsEmpty() ||
!mClonedSurfaces.IsEmpty();
}
@ -187,6 +189,12 @@ public:
return mBlobImplArray;
}
nsTArray<RefPtr<JS::WasmModule>>& WasmModules()
{
MOZ_ASSERT(mSupportsCloning, "WasmModules cannot be taken/set if cloning is not supported.");
return mWasmModuleArray;
}
StructuredCloneScope CloneScope() const
{
return mStructuredCloneScope;
@ -292,6 +300,9 @@ protected:
// Used for cloning blobs in the structured cloning algorithm.
nsTArray<RefPtr<BlobImpl>> mBlobImplArray;
// Used for cloning JS::WasmModules in the structured cloning algorithm.
nsTArray<RefPtr<JS::WasmModule>> mWasmModuleArray;
// This is used for sharing the backend of ImageBitmaps.
// The DataSourceSurface object must be thread-safely reference-counted.
// The DataSourceSurface object will not be written ever via any ImageBitmap

Просмотреть файл

@ -11,13 +11,17 @@
<script type="application/javascript;version=1.7">
function setup_tests() {
SpecialPowers.pushPrefEnv({"set": [["dom.input.dirpicker", true]]}, next);
SpecialPowers.pushPrefEnv({"set": [["dom.input.dirpicker", true],
["javascript.options.wasm", true]]}, next);
}
function getType(a) {
if (a === null || a === undefined)
return 'null';
if (a instanceof WebAssembly.Module)
return "wasm";
if (Array.isArray(a))
return 'array';
@ -59,21 +63,32 @@ function compare(a, b) {
return;
}
if (type == 'wasm') {
var wasmA = new WebAssembly.Instance(a);
ok(wasmA instanceof WebAssembly.Instance, "got an instance");
var wasmB = new WebAssembly.Instance(b);
ok(wasmB instanceof WebAssembly.Instance, "got an instance");
ok(wasmA.exports.foo() === wasmB.exports.foo(), "Same result!");
ok(wasmB.exports.foo() === 42, "We want 42");
}
if (type != 'null') {
is (a.toSource(), b.toSource(), 'Matching using toSource()');
}
}
var clonableObjects = [
'hello world',
123,
null,
true,
new Date(),
[ 1, 'test', true, new Date() ],
{ a: true, b: null, c: new Date(), d: [ true, false, {} ] },
new Blob([123], { type: 'plain/text' }),
new ImageData(2, 2),
{ target: 'all', data: 'hello world' },
{ target: 'all', data: 123 },
{ target: 'all', data: null },
{ target: 'all', data: true },
{ target: 'all', data: new Date() },
{ target: 'all', data: [ 1, 'test', true, new Date() ] },
{ target: 'all', data: { a: true, b: null, c: new Date(), d: [ true, false, {} ] } },
{ target: 'all', data: new Blob([123], { type: 'plain/text' }) },
{ target: 'all', data: new ImageData(2, 2) },
];
function create_fileList() {
@ -88,7 +103,7 @@ function create_fileList() {
var domFile = fileList.files[0];
is(domFile.name, "prefs.js", "fileName should be prefs.js");
clonableObjects.push(fileList.files);
clonableObjects.push({ target: 'all', data: fileList.files });
script.destroy();
next();
}
@ -115,7 +130,7 @@ function create_directory() {
is(list.length, 1, "This list has 1 element");
ok(list[0] instanceof Directory, "We have a directory.");
clonableObjects.push(list[0]);
clonableObjects.push({ target: 'all', data: list[0] });
script.destroy();
next();
});
@ -125,14 +140,41 @@ function create_directory() {
script.sendAsyncMessage("dir.open");
}
function runTests(obj) {
ok(('clonableObjects' in obj) &&
('transferableObjects' in obj) &&
(obj.clonableObjects || obj.transferableObjects), "We must run some test!");
function create_wasmModule() {
info("Checking if we can play with WebAssembly...");
// cloning tests
if (!SpecialPowers.Cu.getJSTestingFunctions().wasmIsSupported()) {
next();
return;
}
ok(WebAssembly, "WebAssembly object should exist");
ok(WebAssembly.compile, "WebAssembly.compile function should exist");
const wasmTextToBinary = SpecialPowers.unwrap(SpecialPowers.Cu.getJSTestingFunctions().wasmTextToBinary);
const fooModuleCode = wasmTextToBinary(`(module
(func $foo (result i32) (i32.const 42))
(export "foo" $foo)
)`, 'new-format');
WebAssembly.compile(fooModuleCode).then((m) => {
ok(m instanceof WebAssembly.Module, "The WasmModule has been compiled.");
clonableObjects.push({ target: 'sameProcess', data: m });
next();
}, () => {
ok(false, "The compilation of the wasmModule failed.");
});
}
function runTests(obj) {
ok(('clonableObjectsEveryWhere' in obj) &&
('clonableObjectsSameProcess' in obj) &&
('transferableObjects' in obj) &&
(obj.clonableObjectsEveryWhere || obj.clonableObjectsSameProcess || obj.transferableObjects), "We must run some test!");
// cloning tests - everyWhere
new Promise(function(resolve, reject) {
if (!obj.clonableObjects) {
if (!obj.clonableObjectsEveryWhere) {
resolve();
return;
}
@ -146,8 +188,13 @@ function runTests(obj) {
var object = clonableObjects[clonableObjectsId++];
obj.send(object, []).then(function(received) {
compare(received.data, object);
if (object.target != 'all') {
runClonableTest();
return;
}
obj.send(object.data, []).then(function(received) {
compare(received.data, object.data);
runClonableTest();
});
}
@ -155,6 +202,38 @@ function runTests(obj) {
runClonableTest();
})
// clonable same process
.then(function() {
return new Promise(function(resolve, reject) {
if (!obj.clonableObjectsSameProcess) {
resolve();
return;
}
var clonableObjectsId = 0;
function runClonableTest() {
if (clonableObjectsId >= clonableObjects.length) {
resolve();
return;
}
var object = clonableObjects[clonableObjectsId++];
if (object.target != 'sameProcess') {
runClonableTest();
return;
}
obj.send(object.data, []).then(function(received) {
compare(received.data, object.data);
runClonableTest();
});
}
runClonableTest();
});
})
// transfering tests
.then(function() {
if (!obj.transferableObjects) {
@ -234,7 +313,8 @@ function test_windowToWindow() {
}
runTests({
clonableObjects: true,
clonableObjectsEveryWhere: true,
clonableObjectsSameProcess: true,
transferableObjects: true,
send: function(what, ports) {
return new Promise(function(r, rr) {
@ -287,7 +367,8 @@ function test_windowToIframeURL(url) {
ifr.src = url;
ifr.onload = function() {
runTests({
clonableObjects: true,
clonableObjectsEveryWhere: true,
clonableObjectsSameProcess: false,
transferableObjects: true,
send: function(what, ports) {
return new Promise(function(r, rr) {
@ -334,7 +415,8 @@ function test_workers() {
}
runTests({
clonableObjects: true,
clonableObjectsEveryWhere: true,
clonableObjectsSameProcess: true,
transferableObjects: true,
send: function(what, ports) {
return new Promise(function(r, rr) {
@ -377,7 +459,8 @@ function test_broadcastChannel() {
}
runTests({
clonableObjects: true,
clonableObjectsEveryWhere: true,
clonableObjectsSameProcess: false,
transferableObjects: false,
send: function(what, ports) {
return new Promise(function(r, rr) {
@ -422,7 +505,8 @@ function test_broadcastChannel_inWorkers() {
}
runTests({
clonableObjects: true,
clonableObjectsEveryWhere: true,
clonableObjectsSameProcess: false,
transferableObjects: false,
send: function(what, ports) {
return new Promise(function(r, rr) {
@ -463,7 +547,8 @@ function test_messagePort() {
}
runTests({
clonableObjects: true,
clonableObjectsEveryWhere: true,
clonableObjectsSameProcess: false,
transferableObjects: true,
send: function(what, ports) {
return new Promise(function(r, rr) {
@ -508,7 +593,8 @@ function test_messagePort_inWorkers() {
}
runTests({
clonableObjects: true,
clonableObjectsEveryWhere: true,
clonableObjectsSameProcess: false,
transferableObjects: true,
send: function(what, ports) {
return new Promise(function(r, rr) {
@ -535,6 +621,7 @@ var tests = [
create_fileList,
create_directory,
create_wasmModule,
test_windowToWindow,
test_windowToIframe,

Просмотреть файл

@ -46,6 +46,7 @@ StructuredCloneData::Copy(const StructuredCloneData& aData)
BlobImpls().AppendElements(aData.BlobImpls());
MOZ_ASSERT(GetSurfaces().IsEmpty());
MOZ_ASSERT(WasmModules().IsEmpty());
mInitialized = true;