diff --git a/dom/base/StructuredCloneHolder.cpp b/dom/base/StructuredCloneHolder.cpp index 9e725f0470f7..31e2d521263b 100644 --- a/dom/base/StructuredCloneHolder.cpp +++ b/dom/base/StructuredCloneHolder.cpp @@ -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 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 module = JS::GetWasmModule(aObj); + MOZ_ASSERT(module); + + return WriteWasmModule(aWriter, module, this); + } + return WriteFullySerializableObjects(aCx, aWriter, aObj); } diff --git a/dom/base/StructuredCloneHolder.h b/dom/base/StructuredCloneHolder.h index 2fbb608c7cb9..e6953d0f36f8 100644 --- a/dom/base/StructuredCloneHolder.h +++ b/dom/base/StructuredCloneHolder.h @@ -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>& 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> mBlobImplArray; + // Used for cloning JS::WasmModules in the structured cloning algorithm. + nsTArray> 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 diff --git a/dom/base/test/test_postMessages.html b/dom/base/test/test_postMessages.html index 7ccfc244416f..a322f20b2019 100644 --- a/dom/base/test/test_postMessages.html +++ b/dom/base/test/test_postMessages.html @@ -11,13 +11,17 @@