Title: [208002] trunk/Source/WebCore
Revision
208002
Author
commit-qu...@webkit.org
Date
2016-10-27 13:36:12 -0700 (Thu, 27 Oct 2016)

Log Message

BufferSource should behave as an union
https://bugs.webkit.org/show_bug.cgi?id=164056

Patch by Zan Dobersek <zdober...@igalia.com> on 2016-10-27
Reviewed by Chris Dumez.

WebIDL typedefs BufferSource as (ArrayBufferView or ArrayBuffer).
To follow that definition, IDLBufferSource is now type-aliased
to IDLUnion<IDLInterface<ArrayBufferView>, IDLInterface<ArrayBuffer>>.

Converter<IDLBufferSource> template specialization can now be
removed since the default specialization for IDLUnion will be
used.

C++ implementations still work through a BufferSource object.
That class now has an implicit constructor that consumes the
Variant object. The data() and length() methods on the class
now iterate the variant to find an existing object that can
provide a pointer to the data or the length of it.

* Modules/mediasource/SourceBuffer.cpp:
(WebCore::SourceBuffer::appendBuffer):
* bindings/generic/IDLTypes.h:
* bindings/js/BufferSource.h:
(WebCore::BufferSource::BufferSource):
(WebCore::BufferSource::data):
(WebCore::BufferSource::length):
* bindings/js/JSDOMConvert.h:
(WebCore::Converter<IDLBufferSource>::convert): Deleted.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (208001 => 208002)


--- trunk/Source/WebCore/ChangeLog	2016-10-27 20:23:06 UTC (rev 208001)
+++ trunk/Source/WebCore/ChangeLog	2016-10-27 20:36:12 UTC (rev 208002)
@@ -1,3 +1,34 @@
+2016-10-27  Zan Dobersek  <zdober...@igalia.com>
+
+        BufferSource should behave as an union
+        https://bugs.webkit.org/show_bug.cgi?id=164056
+
+        Reviewed by Chris Dumez.
+
+        WebIDL typedefs BufferSource as (ArrayBufferView or ArrayBuffer).
+        To follow that definition, IDLBufferSource is now type-aliased
+        to IDLUnion<IDLInterface<ArrayBufferView>, IDLInterface<ArrayBuffer>>.
+
+        Converter<IDLBufferSource> template specialization can now be
+        removed since the default specialization for IDLUnion will be
+        used.
+
+        C++ implementations still work through a BufferSource object.
+        That class now has an implicit constructor that consumes the
+        Variant object. The data() and length() methods on the class
+        now iterate the variant to find an existing object that can
+        provide a pointer to the data or the length of it.
+
+        * Modules/mediasource/SourceBuffer.cpp:
+        (WebCore::SourceBuffer::appendBuffer):
+        * bindings/generic/IDLTypes.h:
+        * bindings/js/BufferSource.h:
+        (WebCore::BufferSource::BufferSource):
+        (WebCore::BufferSource::data):
+        (WebCore::BufferSource::length):
+        * bindings/js/JSDOMConvert.h:
+        (WebCore::Converter<IDLBufferSource>::convert): Deleted.
+
 2016-10-27  Chris Dumez  <cdu...@apple.com>
 
         Merge Element::ShadowRootMode and ShadowRoot::Mode enumerations

Modified: trunk/Source/WebCore/Modules/mediasource/SourceBuffer.cpp (208001 => 208002)


--- trunk/Source/WebCore/Modules/mediasource/SourceBuffer.cpp	2016-10-27 20:23:06 UTC (rev 208001)
+++ trunk/Source/WebCore/Modules/mediasource/SourceBuffer.cpp	2016-10-27 20:36:12 UTC (rev 208002)
@@ -234,7 +234,7 @@
 
 ExceptionOr<void> SourceBuffer::appendBuffer(const BufferSource& data)
 {
-    return appendBufferInternal(static_cast<const unsigned char*>(data.data), data.length);
+    return appendBufferInternal(static_cast<const unsigned char*>(data.data()), data.length());
 }
 
 void SourceBuffer::resetParserState()

Modified: trunk/Source/WebCore/bindings/generic/IDLTypes.h (208001 => 208002)


--- trunk/Source/WebCore/bindings/generic/IDLTypes.h	2016-10-27 20:23:06 UTC (rev 208001)
+++ trunk/Source/WebCore/bindings/generic/IDLTypes.h	2016-10-27 20:36:12 UTC (rev 208002)
@@ -30,12 +30,13 @@
 #include <wtf/text/WTFString.h>
 
 namespace JSC {
+class ArrayBuffer;
+class ArrayBufferView;
 class JSValue;
 }
 
 namespace WebCore {
 
-class BufferSource;
 template <typename Value> class DOMPromise;
 
 template<typename T>
@@ -137,7 +138,7 @@
     static double extractValueFromNullable(double value) { return value; }
 };
 
-struct IDLBufferSource : IDLType<BufferSource> { };
+using IDLBufferSource = IDLUnion<IDLInterface<JSC::ArrayBufferView>, IDLInterface<JSC::ArrayBuffer>>;
 
 // Helper predicates
 

Modified: trunk/Source/WebCore/bindings/js/BufferSource.h (208001 => 208002)


--- trunk/Source/WebCore/bindings/js/BufferSource.h	2016-10-27 20:23:06 UTC (rev 208001)
+++ trunk/Source/WebCore/bindings/js/BufferSource.h	2016-10-27 20:36:12 UTC (rev 208002)
@@ -25,12 +25,35 @@
 
 #pragma once
 
+#include <runtime/ArrayBuffer.h>
+#include <runtime/ArrayBufferView.h>
+#include <wtf/RefPtr.h>
+#include <wtf/Variant.h>
+
 namespace WebCore {
 
 class BufferSource {
 public:
-    const uint8_t* data;
-    size_t length;
+    BufferSource(WTF::Variant<RefPtr<JSC::ArrayBufferView>, RefPtr<JSC::ArrayBuffer>>&& variant)
+        : m_variant(WTFMove(variant))
+    { }
+
+    const uint8_t* data() const
+    {
+        return WTF::visit([](auto& buffer) -> const uint8_t* {
+            return static_cast<const uint8_t*>(buffer->data());
+        }, m_variant);
+    }
+
+    size_t length() const
+    {
+        return WTF::visit([](auto& buffer) -> size_t {
+            return buffer->byteLength();
+        }, m_variant);
+    }
+
+private:
+    WTF::Variant<RefPtr<JSC::ArrayBufferView>, RefPtr<JSC::ArrayBuffer>> m_variant;
 };
 
 } // namespace WebCore

Modified: trunk/Source/WebCore/bindings/js/JSDOMConvert.h (208001 => 208002)


--- trunk/Source/WebCore/bindings/js/JSDOMConvert.h	2016-10-27 20:23:06 UTC (rev 208001)
+++ trunk/Source/WebCore/bindings/js/JSDOMConvert.h	2016-10-27 20:36:12 UTC (rev 208002)
@@ -1031,27 +1031,6 @@
 };
 
 // MARK: -
-// MARK: BufferSource type
-
-template<> struct Converter<IDLBufferSource> : DefaultConverter<IDLBufferSource> {
-    using ReturnType = BufferSource;
-
-    static ReturnType convert(JSC::ExecState& state, JSC::JSValue value)
-    {
-        JSC::VM& vm = state.vm();
-        auto scope = DECLARE_THROW_SCOPE(vm);
-
-        if (JSC::ArrayBuffer* buffer = JSC::toArrayBuffer(value))
-            return { static_cast<uint8_t*>(buffer->data()), buffer->byteLength() };
-        if (RefPtr<JSC::ArrayBufferView> bufferView = toArrayBufferView(value))
-            return { static_cast<uint8_t*>(bufferView->baseAddress()), bufferView->byteLength() };
-
-        throwTypeError(&state, scope, ASCIILiteral("Only ArrayBuffer and ArrayBufferView objects can be passed as BufferSource arguments"));
-        return { nullptr, 0 };
-    }
-};
-
-// MARK: -
 // MARK: Date type
 
 template<> struct Converter<IDLDate> : DefaultConverter<IDLDate> {
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to