Skip to content

Commit

Permalink
[JSC] Added "user controlled buffer" support to ArrayBuffers, to allo…
Browse files Browse the repository at this point in the history
…w creating ArrayBuffers "around" user controlled buffer, without copying or freeing them
  • Loading branch information
MCE-KobyBo committed Feb 11, 2018
1 parent 7dbf2ec commit a5f9450
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
13 changes: 13 additions & 0 deletions Source/JavaScriptCore/runtime/ArrayBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ ArrayBuffer::ArrayBuffer(ArrayBufferContents&& contents)
, m_pinCount(0)
, m_isWasmMemory(false)
, m_locked(false)
, m_isApiUserControlledBuffer(false)
{
}

Expand Down Expand Up @@ -297,6 +298,18 @@ void ArrayBuffer::makeWasmMemory()
m_isWasmMemory = true;
}

void ArrayBuffer::makeApiUserControlledBuffer()
{
m_isApiUserControlledBuffer = true;

if (m_contents.m_shared) {
m_contents.m_shared->m_destructor = [](void*) {};
}
else {
m_contents.m_destructor = [](void*) {};
}
}

void ArrayBuffer::setSharingMode(ArrayBufferSharingMode newSharingMode)
{
if (newSharingMode == sharingMode())
Expand Down
17 changes: 15 additions & 2 deletions Source/JavaScriptCore/runtime/ArrayBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class SharedArrayBufferContents : public ThreadSafeRefCounted<SharedArrayBufferC
void* data() const { return m_data.getMayBeNull(); }

private:
friend class ArrayBuffer;

CagedPtr<Gigacage::Primitive, void> m_data;
ArrayBufferDestructorFunction m_destructor;
};
Expand Down Expand Up @@ -138,6 +140,9 @@ class ArrayBuffer : public GCIncomingRefCounted<ArrayBuffer> {
void makeWasmMemory();
inline bool isWasmMemory();

void makeApiUserControlledBuffer();
inline bool isApiUserControlledBuffer() const;

JS_EXPORT_PRIVATE bool transferTo(VM&, ArrayBufferContents&);
JS_EXPORT_PRIVATE bool shareWith(ArrayBufferContents&);

Expand All @@ -160,12 +165,13 @@ class ArrayBuffer : public GCIncomingRefCounted<ArrayBuffer> {
void notifyIncommingReferencesOfTransfer(VM&);

ArrayBufferContents m_contents;
unsigned m_pinCount : 30;
unsigned m_pinCount : 29;
bool m_isWasmMemory : 1;
// m_locked == true means that some API user fetched m_contents directly from a TypedArray object,
// the buffer is backed by a WebAssembly.Memory, or is a SharedArrayBuffer.
bool m_locked : 1;

bool m_isApiUserControlledBuffer : 1;

public:
Weak<JSArrayBuffer> m_wrapper;
};
Expand Down Expand Up @@ -239,6 +245,13 @@ bool ArrayBuffer::isWasmMemory()
return m_isWasmMemory;
}

bool ArrayBuffer::isApiUserControlledBuffer() const
{
return m_isApiUserControlledBuffer;
}



JS_EXPORT_PRIVATE ASCIILiteral errorMesasgeForTransfer(ArrayBuffer*);

} // namespace JSC
Expand Down

0 comments on commit a5f9450

Please sign in to comment.