Title: [291229] trunk
Revision
291229
Author
[email protected]
Date
2022-03-14 08:42:30 -0700 (Mon, 14 Mar 2022)

Log Message

[GLib] Expose typed arrays in the public API
https://bugs.webkit.org/show_bug.cgi?id=197535

Reviewed by Carlos Garcia Campos.

This adds a set of new functions to operate on JSCValue objects which refer to typed
arrays in the _javascript_ side of the world. Typed array values can be created with an
existing ArrayBuffer to hold the contents, in which case custom allocations are possible;
or letting JSC handle allocation. Operating on typed arrays is expected to be done on an
element-by-element basis, and in general it is recommended to use the functions dealing
with the "length" (number of elements) of typed arrays; nevertheless it is also possible
to retrieve their "size" and "offset" over the underlying ArrayBuffer, and/or retrieving
said ArrayBuffer.

Source/_javascript_Core:

* API/glib/JSCValue.cpp:
(toTypedArrayType):
(toJSCTypedArrayType):
(jsc_value_new_typed_array):
(jsc_value_new_typed_array_with_buffer):
(jsc_value_is_typed_array):
(jsc_value_typed_array_get_buffer):
(jsc_value_typed_array_get_data):
(jsc_value_typed_array_get_length):
(jsc_value_typed_array_get_offset):
(jsc_value_typed_array_get_size):
(jsc_value_typed_array_get_type):
* API/glib/JSCValue.h:
* API/glib/docs/jsc-glib-4.0-sections.txt:

Tools:

* TestWebKitAPI/Tests/_javascript_Core/glib/TestJSC.cpp: Added test for typed arrays.
(elementSize):
(testJSCTypedArray):
(main):

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/API/glib/JSCValue.cpp (291228 => 291229)


--- trunk/Source/_javascript_Core/API/glib/JSCValue.cpp	2022-03-14 14:03:22 UTC (rev 291228)
+++ trunk/Source/_javascript_Core/API/glib/JSCValue.cpp	2022-03-14 15:42:30 UTC (rev 291229)
@@ -32,6 +32,7 @@
 #include "JSTypedArray.h"
 #include "LiteralParser.h"
 #include "OpaqueJSString.h"
+#include "TypedArrayType.h"
 #include <gobject/gvaluecollector.h>
 #include <wtf/glib/GRefPtr.h>
 #include <wtf/glib/GUniquePtr.h>
@@ -1613,7 +1614,7 @@
  * jsc_value_array_buffer_get_size:
  * @value: A #JSCValue
  *
- * Gets the size of the array buffer.
+ * Gets the size in bytes of the array buffer.
  *
  * Obtains the size in bytes of the memory region that holds the contents of
  * an %ArrayBuffer.
@@ -1641,6 +1642,427 @@
 }
 
 /**
+ * JSCTypedArrayType:
+ * @JSC_TYPED_ARRAY_NONE: Not a typed array, or type unsupported.
+ * @JSC_TYPED_ARRAY_INT8: Array elements are 8-bit signed integers (int8_t).
+ * @JSC_TYPED_ARRAY_INT16: Array elements are 16-bit signed integers (int16_t).
+ * @JSC_TYPED_ARRAY_INT32: Array elements are 32-bit signed integers (int32_t).
+ * @JSC_TYPED_ARRAY_INT64: Array elements are 64-bit signed integers (int64_t).
+ * @JSC_TYPED_ARRAY_UINT8: Array elements are 8-bit unsigned integers (uint8_t).
+ * @JSC_TYPED_ARRAY_UINT8_CLAMPED: Array elements are 8-bit unsigned integers (uint8_t).
+ * @JSC_TYPED_ARRAY_UINT16: Array elements are 16-bit unsigned integers (uint16_t).
+ * @JSC_TYPED_ARRAY_UINT32: Array elements are 32-bit unsigned integers (uint32_t).
+ * @JSC_TYPED_ARRAY_UINT64: Array elements are 64-bit unsigned integers (uint64_t).
+ * @JSC_TYPED_ARRAY_FLOAT32: Array elements are 32-bit floating point numbers (float).
+ * @JSC_TYPED_ARRAY_FLOAT64: Array elements are 64-bit floating point numbers (double).
+ *
+ * Possible types of the elements contained in a typed array.
+ *
+ * Since: 2.38
+ */
+
+static JSTypedArrayType toTypedArrayType(JSCTypedArrayType type)
+{
+    switch (type) {
+    case JSC_TYPED_ARRAY_NONE:
+        return kJSTypedArrayTypeNone;
+    case JSC_TYPED_ARRAY_INT8:
+        return kJSTypedArrayTypeInt8Array;
+    case JSC_TYPED_ARRAY_INT16:
+        return kJSTypedArrayTypeInt16Array;
+    case JSC_TYPED_ARRAY_INT32:
+        return kJSTypedArrayTypeInt32Array;
+    case JSC_TYPED_ARRAY_INT64:
+        return kJSTypedArrayTypeBigInt64Array;
+    case JSC_TYPED_ARRAY_UINT8:
+        return kJSTypedArrayTypeUint8Array;
+    case JSC_TYPED_ARRAY_UINT8_CLAMPED:
+        return kJSTypedArrayTypeUint8ClampedArray;
+    case JSC_TYPED_ARRAY_UINT16:
+        return kJSTypedArrayTypeUint16Array;
+    case JSC_TYPED_ARRAY_UINT32:
+        return kJSTypedArrayTypeUint32Array;
+    case JSC_TYPED_ARRAY_UINT64:
+        return kJSTypedArrayTypeBigUint64Array;
+    case JSC_TYPED_ARRAY_FLOAT32:
+        return kJSTypedArrayTypeFloat32Array;
+    case JSC_TYPED_ARRAY_FLOAT64:
+        return kJSTypedArrayTypeFloat64Array;
+    }
+    RELEASE_ASSERT_NOT_REACHED();
+}
+
+static JSC::TypedArrayType toJSCTypedArrayType(JSCTypedArrayType type)
+{
+    switch (type) {
+    case JSC_TYPED_ARRAY_NONE:
+        return JSC::TypedArrayType::NotTypedArray;
+    case JSC_TYPED_ARRAY_INT8:
+        return JSC::TypedArrayType::TypeInt8;
+    case JSC_TYPED_ARRAY_INT16:
+        return JSC::TypedArrayType::TypeInt16;
+    case JSC_TYPED_ARRAY_INT32:
+        return JSC::TypedArrayType::TypeInt32;
+    case JSC_TYPED_ARRAY_INT64:
+        return JSC::TypedArrayType::TypeBigInt64;
+    case JSC_TYPED_ARRAY_UINT8:
+        return JSC::TypedArrayType::TypeUint8;
+    case JSC_TYPED_ARRAY_UINT8_CLAMPED:
+        return JSC::TypedArrayType::TypeUint8Clamped;
+    case JSC_TYPED_ARRAY_UINT16:
+        return JSC::TypedArrayType::TypeUint16;
+    case JSC_TYPED_ARRAY_UINT32:
+        return JSC::TypedArrayType::TypeUint32;
+    case JSC_TYPED_ARRAY_UINT64:
+        return JSC::TypedArrayType::TypeBigUint64;
+    case JSC_TYPED_ARRAY_FLOAT32:
+        return JSC::TypedArrayType::TypeFloat32;
+    case JSC_TYPED_ARRAY_FLOAT64:
+        return JSC::TypedArrayType::TypeFloat64;
+    }
+    RELEASE_ASSERT_NOT_REACHED();
+}
+
+/**
+ * jsc_value_new_typed_array:
+ * @context: a #JSCContext
+ * @type: the type of array elements
+ * @length: number of elements in the array
+ *
+ * Create a new typed array containing a given amount of elements.
+ *
+ * Create a #JSCValue referencing a new typed array with space for @length
+ * elements of a given @type. As all typed arrays must have an associated
+ * `ArrayBuffer`, a new one of suitable size will be allocated to store
+ * the elements, which will be initialized to zero.
+ *
+ * The @type must *not* be %JSC_TYPED_ARRAY_NONE.
+ *
+ * Returns: (transfer full): a #JSCValue
+ *
+ * Since: 2.38
+ */
+JSCValue* jsc_value_new_typed_array(JSCContext* context, JSCTypedArrayType type, gsize count)
+{
+    g_return_val_if_fail(JSC_IS_CONTEXT(context), nullptr);
+    g_return_val_if_fail(type != JSC_TYPED_ARRAY_NONE, nullptr);
+
+    auto* jsContext = jscContextGetJSContext(context);
+
+    JSValueRef exception = nullptr;
+    auto* jsTypedArray = JSObjectMakeTypedArray(jsContext, toTypedArrayType(type), count, &exception);
+    if (jscContextHandleExceptionIfNeeded(context, exception))
+        return nullptr;
+
+    return jscContextGetOrCreateValue(context, jsTypedArray).leakRef();
+}
+
+/**
+ * jsc_value_new_typed_array_with_buffer:
+ * @array_buffer: a #JSCValue.
+ * @type: type of array elements.
+ * @offset: offset, in bytes.
+ * @length: number of array elements, or `-1`.
+ *
+ * Create a new typed array value with elements from an array buffer.
+ *
+ * Create a #JSCValue referencing a new typed array value containing
+ * elements of the given @type, where the elements are stored at the memory
+ * region represented by the @array_buffer.
+ *
+ * The @type must *not* be %JSC_TYPED_ARRAY_NONE.
+ *
+ * The @offset and @length parameters can be used to indicate which part of
+ * the array buffer can be accessed through the typed array. If both are
+ * omitted (passing zero as @offset, and `-1` as @length), the whole
+ * @array_buffer is exposed through the typed array. Omitting the @length
+ * with a non-zero @offset will expose the remainder of the @array_buffer
+ * starting at the indicated offset.
+ *
+ * Returns: (transfer full): a #JSCValue
+ *
+ * Since: 2.38
+ */
+JSCValue* jsc_value_new_typed_array_with_buffer(JSCValue* arrayBuffer, JSCTypedArrayType type, gsize offset, gssize length)
+{
+    g_return_val_if_fail(JSC_IS_VALUE(arrayBuffer), nullptr);
+    g_return_val_if_fail(jsc_value_is_array_buffer(arrayBuffer), nullptr);
+    g_return_val_if_fail(type != JSC_TYPED_ARRAY_NONE, nullptr);
+    g_return_val_if_fail(length >= -1, nullptr);
+
+    auto* jsContext = jscContextGetJSContext(arrayBuffer->priv->context.get());
+
+    JSValueRef exception = nullptr;
+    auto* jsObject = JSValueToObject(jsContext, arrayBuffer->priv->jsValue, &exception);
+    if (jscContextHandleExceptionIfNeeded(arrayBuffer->priv->context.get(), exception))
+        return nullptr;
+
+    if (length < 0) {
+        const auto bufferByteSize = JSObjectGetArrayBufferByteLength(jsContext, jsObject, &exception);
+        if (jscContextHandleExceptionIfNeeded(arrayBuffer->priv->context.get(), exception))
+            return nullptr;
+
+        const auto remainingBytes = bufferByteSize - offset;
+        const auto elementByteSize = JSC::elementSize(toJSCTypedArrayType(type));
+        if (remainingBytes >= elementByteSize)
+            length = remainingBytes / elementByteSize;
+        else
+            length = 0;
+    }
+
+    auto* jsTypedArray = JSObjectMakeTypedArrayWithArrayBufferAndOffset(jsContext, toTypedArrayType(type), jsObject, offset, length, &exception);
+    if (jscContextHandleExceptionIfNeeded(arrayBuffer->priv->context.get(), exception))
+        return nullptr;
+
+    return jscContextGetOrCreateValue(arrayBuffer->priv->context.get(), jsTypedArray).leakRef();
+}
+
+/**
+ * jsc_value_is_typed_array:
+ * @value: a #JSCValue
+ *
+ * Determines whether a value is a typed array.
+ *
+ * Returns: Whether @value is a typed array.
+ *
+ * Since: 2.38
+ */
+gboolean jsc_value_is_typed_array(JSCValue* value)
+{
+    g_return_val_if_fail(JSC_IS_VALUE(value), FALSE);
+
+    return jsc_value_typed_array_get_type(value) != JSC_TYPED_ARRAY_NONE;
+}
+
+/**
+ * jsc_value_typed_array_get_type:
+ * @value: a #JSCValue
+ *
+ * Gets the type of elements contained in a typed array.
+ *
+ * Returns: type of the elements, or %JSC_TYPED_ARRAY_NONE if @value is not a typed array.
+ *
+ * Since: 2.38
+ */
+JSCTypedArrayType jsc_value_typed_array_get_type(JSCValue *value)
+{
+    g_return_val_if_fail(JSC_IS_VALUE(value), JSC_TYPED_ARRAY_NONE);
+
+    JSC::JSGlobalObject* globalObject = toJS(jscContextGetJSContext(value->priv->context.get()));
+    JSC::VM& vm = globalObject->vm();
+    JSC::JSLockHolder locker(vm);
+
+    JSC::JSValue jsValue = toJS(globalObject, value->priv->jsValue);
+    if (!jsValue.isObject())
+        return JSC_TYPED_ARRAY_NONE;
+
+    switch (jsValue.getObject()->classInfo(vm)->typedArrayStorageType) {
+    case JSC::TypeDataView:
+    case JSC::NotTypedArray:
+        return JSC_TYPED_ARRAY_NONE;
+    case JSC::TypeInt8:
+        return JSC_TYPED_ARRAY_INT8;
+    case JSC::TypeUint8:
+        return JSC_TYPED_ARRAY_UINT8;
+    case JSC::TypeUint8Clamped:
+        return JSC_TYPED_ARRAY_UINT8_CLAMPED;
+    case JSC::TypeInt16:
+        return JSC_TYPED_ARRAY_INT16;
+    case JSC::TypeUint16:
+        return JSC_TYPED_ARRAY_UINT16;
+    case JSC::TypeInt32:
+        return JSC_TYPED_ARRAY_INT32;
+    case JSC::TypeUint32:
+        return JSC_TYPED_ARRAY_UINT32;
+    case JSC::TypeFloat32:
+        return JSC_TYPED_ARRAY_FLOAT32;
+    case JSC::TypeFloat64:
+        return JSC_TYPED_ARRAY_FLOAT64;
+    case JSC::TypeBigInt64:
+        return JSC_TYPED_ARRAY_INT64;
+    case JSC::TypeBigUint64:
+        return JSC_TYPED_ARRAY_UINT64;
+    }
+
+    RELEASE_ASSERT_NOT_REACHED();
+}
+
+/**
+ * jsc_value_typed_array_get_data:
+ * @value: a #JSCValue
+ * @length: (nullable) (out): location to return the number of elements contained
+ *
+ * Obtains a pointer to the memory region that holds the elements of the typed
+ * array; modifications done to them will be visible to _javascript_ code. If
+ * @length is not %NULL, the number of elements contained in the typed array
+ * are also stored in the pointed location.
+ *
+ * The returned pointer needs to be casted to the appropriate type (see
+ * #JSCTypedArrayType), and has the `offset` over the underlying array
+ * buffer data applied—that is, points to the first element of the typed
+ * array:
+ *
+ * |[<!-- language="C" -->
+ * if (jsc_value_typed_array_get_type(value) != JSC_TYPED_ARRAY_UINT32)
+ *     g_error ("Only arrays of uint32_t are supported");
+ *
+ * gsize count = 0;
+ * uint32_t *elements = jsc_value_typed_array_get_contents (value, &count);
+ * for (gsize i = 0; i < count; i++)
+ *      g_print ("index %zu, value %" PRIu32 "\n", i, elements[i]);
+ * ]|
+ *
+ * Note that the pointer returned by this function is not guaranteed to remain
+ * the same after calls to other JSC API functions. See
+ * jsc_value_array_buffer_get_data() for details.
+ *
+ * Returns: (transfer none): pointer to memory.
+ *
+ * Since: 2.38
+ */
+gpointer jsc_value_typed_array_get_data(JSCValue* value, gsize* length)
+{
+    g_return_val_if_fail(JSC_IS_VALUE(value), nullptr);
+
+    auto* jsContext = jscContextGetJSContext(value->priv->context.get());
+
+    JSValueRef exception = nullptr;
+    auto* jsObject = JSValueToObject(jsContext, value->priv->jsValue, &exception);
+    if (jscContextHandleExceptionIfNeeded(value->priv->context.get(), exception))
+        return nullptr;
+
+    auto* bytes = JSObjectGetTypedArrayBytesPtr(jsContext, jsObject, &exception);
+    if (jscContextHandleExceptionIfNeeded(value->priv->context.get(), exception))
+        return nullptr;
+
+    // Pointer may have an offset, which we want to return directly a pointer to the actual data.
+    const auto offset = JSObjectGetTypedArrayByteOffset(jsContext, jsObject, &exception);
+    if (jscContextHandleExceptionIfNeeded(value->priv->context.get(), exception))
+        return nullptr;
+
+    if (length) {
+        const auto numElements = JSObjectGetTypedArrayLength(jsContext, jsObject, &exception);
+        if (jscContextHandleExceptionIfNeeded(value->priv->context.get(), exception))
+            return nullptr;
+        *length = numElements;
+    }
+
+    return static_cast<uint8_t*>(bytes) + offset;
+}
+
+/**
+ * jsc_value_typed_array_get_length:
+ * @value: a #JSCValue
+ *
+ * Gets the number of elements in a typed array.
+ *
+ * Returns: number of elements.
+ *
+ * Since: 2.38
+ */
+gsize jsc_value_typed_array_get_length(JSCValue* value)
+{
+    g_return_val_if_fail(JSC_IS_VALUE(value), 0);
+
+    auto* jsContext = jscContextGetJSContext(value->priv->context.get());
+
+    JSValueRef exception = nullptr;
+    auto* jsObject = JSValueToObject(jsContext, value->priv->jsValue, &exception);
+    if (jscContextHandleExceptionIfNeeded(value->priv->context.get(), exception))
+        return 0;
+
+    const auto length = JSObjectGetTypedArrayLength(jsContext, jsObject, &exception);
+    if (jscContextHandleExceptionIfNeeded(value->priv->context.get(), exception))
+        return 0;
+
+    return length;
+}
+
+/**
+ * jsc_value_typed_array_get_size:
+ * @value: a #JSCValue
+ *
+ * Gets the size of a typed array.
+ *
+ * Returns: size, in bytes.
+ *
+ * Since: 2.38
+ */
+gsize jsc_value_typed_array_get_size(JSCValue* value)
+{
+    g_return_val_if_fail(JSC_IS_VALUE(value), 0);
+
+    auto* jsContext = jscContextGetJSContext(value->priv->context.get());
+
+    JSValueRef exception = nullptr;
+    auto* jsObject = JSValueToObject(jsContext, value->priv->jsValue, &exception);
+    if (jscContextHandleExceptionIfNeeded(value->priv->context.get(), exception))
+        return 0;
+
+    const auto size = JSObjectGetTypedArrayByteLength(jsContext, jsObject, &exception);
+    if (jscContextHandleExceptionIfNeeded(value->priv->context.get(), exception))
+        return 0;
+
+    return size;
+}
+
+/**
+ * jsc_value_typed_array_get_offset:
+ * @value: A #JSCValue
+ *
+ * Gets the offset over the underlying array buffer data.
+ *
+ * Returns: offset, in bytes.
+ *
+ * Since: 2.38
+ */
+gsize jsc_value_typed_array_get_offset(JSCValue* value)
+{
+    g_return_val_if_fail(JSC_IS_VALUE(value), 0);
+
+    auto* jsContext = jscContextGetJSContext(value->priv->context.get());
+
+    JSValueRef exception = nullptr;
+    auto* jsObject = JSValueToObject(jsContext, value->priv->jsValue, &exception);
+    if (jscContextHandleExceptionIfNeeded(value->priv->context.get(), exception))
+        return 0;
+
+    const auto offset = JSObjectGetTypedArrayByteOffset(jsContext, jsObject, &exception);
+    if (jscContextHandleExceptionIfNeeded(value->priv->context.get(), exception))
+        return 0;
+
+    return offset;
+}
+
+/**
+ * jsc_value_typed_array_get_buffer:
+ * @value: a #JSCValue
+ *
+ * Obtain the %ArrayBuffer for the memory region of the typed array elements.
+ *
+ * Returns: (transfer full): A #JSCValue
+ *
+ * Since: 2.38
+ */
+JSCValue* jsc_value_typed_array_get_buffer(JSCValue* value)
+{
+    g_return_val_if_fail(JSC_IS_VALUE(value), nullptr);
+
+    auto* jsContext = jscContextGetJSContext(value->priv->context.get());
+
+    JSValueRef exception = nullptr;
+    auto* jsObject = JSValueToObject(jsContext, value->priv->jsValue, &exception);
+    if (jscContextHandleExceptionIfNeeded(value->priv->context.get(), exception))
+        return nullptr;
+
+    auto* jsArrayBuffer = JSObjectGetTypedArrayBuffer(jsContext, jsObject, &exception);
+    if (jscContextHandleExceptionIfNeeded(value->priv->context.get(), exception))
+        return nullptr;
+
+    return jscContextGetOrCreateValue(value->priv->context.get(), jsArrayBuffer).leakRef();
+}
+
+/**
  * jsc_value_new_from_json:
  * @context: a #JSCContext
  * @json: the JSON string to be parsed

Modified: trunk/Source/_javascript_Core/API/glib/JSCValue.h (291228 => 291229)


--- trunk/Source/_javascript_Core/API/glib/JSCValue.h	2022-03-14 14:03:22 UTC (rev 291228)
+++ trunk/Source/_javascript_Core/API/glib/JSCValue.h	2022-03-14 15:42:30 UTC (rev 291229)
@@ -49,6 +49,21 @@
     JSC_VALUE_PROPERTY_WRITABLE     = 1 << 2
 } JSCValuePropertyFlags;
 
+typedef enum {
+    JSC_TYPED_ARRAY_NONE = 0,
+    JSC_TYPED_ARRAY_INT8,
+    JSC_TYPED_ARRAY_INT16,
+    JSC_TYPED_ARRAY_INT32,
+    JSC_TYPED_ARRAY_INT64,
+    JSC_TYPED_ARRAY_UINT8,
+    JSC_TYPED_ARRAY_UINT8_CLAMPED,
+    JSC_TYPED_ARRAY_UINT16,
+    JSC_TYPED_ARRAY_UINT32,
+    JSC_TYPED_ARRAY_UINT64,
+    JSC_TYPED_ARRAY_FLOAT32,
+    JSC_TYPED_ARRAY_FLOAT64,
+} JSCTypedArrayType;
+
 struct _JSCValue {
     GObject parent;
 
@@ -263,7 +278,40 @@
 JSC_API gsize
 jsc_value_array_buffer_get_size           (JSCValue             *value);
 
+JSC_API JSCValue *
+jsc_value_new_typed_array                 (JSCContext           *context,
+                                           JSCTypedArrayType     type,
+                                           gsize                 length);
+
+JSC_API JSCValue *
+jsc_value_new_typed_array_with_buffer     (JSCValue             *array_buffer,
+                                           JSCTypedArrayType     type,
+                                           gsize                 offset,
+                                           gssize                length);
+
 JSC_API gboolean
+jsc_value_is_typed_array                  (JSCValue             *value);
+
+JSC_API JSCTypedArrayType
+jsc_value_typed_array_get_type            (JSCValue             *value);
+
+JSC_API gpointer
+jsc_value_typed_array_get_data            (JSCValue             *value,
+                                           gsize                *length);
+
+JSC_API gsize
+jsc_value_typed_array_get_length          (JSCValue             *value);
+
+JSC_API gsize
+jsc_value_typed_array_get_size            (JSCValue             *value);
+
+JSC_API gsize
+jsc_value_typed_array_get_offset          (JSCValue             *value);
+
+JSC_API JSCValue *
+jsc_value_typed_array_get_buffer          (JSCValue             *value);
+
+JSC_API gboolean
 jsc_value_is_constructor                  (JSCValue             *value);
 
 JSC_API JSCValue *

Modified: trunk/Source/_javascript_Core/API/glib/docs/jsc-glib-4.0-sections.txt (291228 => 291229)


--- trunk/Source/_javascript_Core/API/glib/docs/jsc-glib-4.0-sections.txt	2022-03-14 14:03:22 UTC (rev 291228)
+++ trunk/Source/_javascript_Core/API/glib/docs/jsc-glib-4.0-sections.txt	2022-03-14 15:42:30 UTC (rev 291229)
@@ -69,6 +69,7 @@
 <TITLE>JSCValue</TITLE>
 JSCValue
 JSCValuePropertyFlags
+JSCTypedArrayType
 jsc_value_get_context
 jsc_value_new_undefined
 jsc_value_is_undefined
@@ -114,6 +115,15 @@
 jsc_value_is_array_buffer
 jsc_value_array_buffer_get_data
 jsc_value_array_buffer_get_size
+jsc_value_new_typed_array
+jsc_value_new_typed_array_with_buffer
+jsc_value_is_typed_array
+jsc_value_typed_array_get_type
+jsc_value_typed_array_get_data
+jsc_value_typed_array_get_size
+jsc_value_typed_array_get_offset
+jsc_value_typed_array_get_length
+jsc_value_typed_array_get_buffer
 jsc_value_is_constructor
 jsc_value_constructor_call
 jsc_value_constructor_callv

Modified: trunk/Source/_javascript_Core/ChangeLog (291228 => 291229)


--- trunk/Source/_javascript_Core/ChangeLog	2022-03-14 14:03:22 UTC (rev 291228)
+++ trunk/Source/_javascript_Core/ChangeLog	2022-03-14 15:42:30 UTC (rev 291229)
@@ -1,3 +1,34 @@
+2022-03-14  Adrian Perez de Castro  <[email protected]>
+
+        [GLib] Expose typed arrays in the public API
+        https://bugs.webkit.org/show_bug.cgi?id=197535
+
+        Reviewed by Carlos Garcia Campos.
+
+        This adds a set of new functions to operate on JSCValue objects which refer to typed
+        arrays in the _javascript_ side of the world. Typed array values can be created with an
+        existing ArrayBuffer to hold the contents, in which case custom allocations are possible;
+        or letting JSC handle allocation. Operating on typed arrays is expected to be done on an
+        element-by-element basis, and in general it is recommended to use the functions dealing
+        with the "length" (number of elements) of typed arrays; nevertheless it is also possible
+        to retrieve their "size" and "offset" over the underlying ArrayBuffer, and/or retrieving
+        said ArrayBuffer.
+
+        * API/glib/JSCValue.cpp:
+        (toTypedArrayType):
+        (toJSCTypedArrayType):
+        (jsc_value_new_typed_array):
+        (jsc_value_new_typed_array_with_buffer):
+        (jsc_value_is_typed_array):
+        (jsc_value_typed_array_get_buffer):
+        (jsc_value_typed_array_get_data):
+        (jsc_value_typed_array_get_length):
+        (jsc_value_typed_array_get_offset):
+        (jsc_value_typed_array_get_size):
+        (jsc_value_typed_array_get_type):
+        * API/glib/JSCValue.h:
+        * API/glib/docs/jsc-glib-4.0-sections.txt:
+
 2022-03-13  Angelos Oikonomopoulos  <[email protected]>
 
         [JSC] Use addressTempRegister in loadValue(void *) on 32 bits

Modified: trunk/Tools/ChangeLog (291228 => 291229)


--- trunk/Tools/ChangeLog	2022-03-14 14:03:22 UTC (rev 291228)
+++ trunk/Tools/ChangeLog	2022-03-14 15:42:30 UTC (rev 291229)
@@ -1,3 +1,24 @@
+2022-03-14  Adrian Perez de Castro  <[email protected]>
+
+        [GLib] Expose typed arrays in the public API
+        https://bugs.webkit.org/show_bug.cgi?id=197535
+
+        Reviewed by Carlos Garcia Campos.
+
+        This adds a set of new functions to operate on JSCValue objects which refer to typed
+        arrays in the _javascript_ side of the world. Typed array values can be created with an
+        existing ArrayBuffer to hold the contents, in which case custom allocations are possible;
+        or letting JSC handle allocation. Operating on typed arrays is expected to be done on an
+        element-by-element basis, and in general it is recommended to use the functions dealing
+        with the "length" (number of elements) of typed arrays; nevertheless it is also possible
+        to retrieve their "size" and "offset" over the underlying ArrayBuffer, and/or retrieving
+        said ArrayBuffer.
+
+        * TestWebKitAPI/Tests/_javascript_Core/glib/TestJSC.cpp: Added test for typed arrays.
+        (elementSize):
+        (testJSCTypedArray):
+        (main):
+
 2022-03-14  Kimmo Kinnunen  <[email protected]>
 
         WebGL context should use discrete gpu if the context is created when the window is on external monitor, ANGLE Metal backend

Modified: trunk/Tools/TestWebKitAPI/Tests/_javascript_Core/glib/TestJSC.cpp (291228 => 291229)


--- trunk/Tools/TestWebKitAPI/Tests/_javascript_Core/glib/TestJSC.cpp	2022-03-14 14:03:22 UTC (rev 291228)
+++ trunk/Tools/TestWebKitAPI/Tests/_javascript_Core/glib/TestJSC.cpp	2022-03-14 15:42:30 UTC (rev 291229)
@@ -2971,6 +2971,355 @@
     }
 }
 
+static constexpr size_t elementSize(JSCTypedArrayType type)
+{
+    switch (type) {
+    case JSC_TYPED_ARRAY_INT8: return sizeof(int8_t);
+    case JSC_TYPED_ARRAY_INT16: return sizeof(int16_t);
+    case JSC_TYPED_ARRAY_INT32: return sizeof(int32_t);
+    case JSC_TYPED_ARRAY_INT64: return sizeof(int64_t);
+    case JSC_TYPED_ARRAY_UINT8: return sizeof(uint8_t);
+    case JSC_TYPED_ARRAY_UINT8_CLAMPED: return sizeof(uint8_t);
+    case JSC_TYPED_ARRAY_UINT16: return sizeof(uint16_t);
+    case JSC_TYPED_ARRAY_UINT32: return sizeof(uint32_t);
+    case JSC_TYPED_ARRAY_UINT64: return sizeof(uint64_t);
+    case JSC_TYPED_ARRAY_FLOAT32: return sizeof(float);
+    case JSC_TYPED_ARRAY_FLOAT64: return sizeof(double);
+    case JSC_TYPED_ARRAY_NONE: break;
+    }
+    RELEASE_ASSERT_NOT_REACHED();
+}
+
+void testJSCTypedArray()
+{
+    static const struct {
+        const char* name;
+        JSCTypedArrayType type;
+    } typeMap[] = {
+        { "Int8Array", JSC_TYPED_ARRAY_INT8 },
+        { "Int16Array", JSC_TYPED_ARRAY_INT16 },
+        { "Int32Array", JSC_TYPED_ARRAY_INT32 },
+        { "BigInt64Array", JSC_TYPED_ARRAY_INT64 },
+        { "Uint8Array", JSC_TYPED_ARRAY_UINT8 },
+        { "Uint8ClampedArray", JSC_TYPED_ARRAY_UINT8_CLAMPED },
+        { "Uint16Array", JSC_TYPED_ARRAY_UINT16 },
+        { "Uint32Array", JSC_TYPED_ARRAY_UINT32 },
+        { "BigUint64Array", JSC_TYPED_ARRAY_UINT64 },
+        { "Float32Array", JSC_TYPED_ARRAY_FLOAT32 },
+        { "Float64Array", JSC_TYPED_ARRAY_FLOAT64 },
+    };
+
+    for (const auto& entry : typeMap) {
+        LeakChecker checker;
+        auto context = adoptGRef(jsc_context_new());
+        checker.watch(context.get());
+        ExceptionHandler exceptionHandler(context.get());
+
+        // Create in JS, check types using the API.
+        const bool isBigInt = g_str_has_prefix(entry.name, "Big");
+        GUniquePtr<char> script(g_strdup_printf("const arr = new %s(1); arr[0] = 42%s; arr;", entry.name, isBigInt ? "n" : ""));
+        auto value = adoptGRef(jsc_context_evaluate(context.get(), script.get(), -1));
+        checker.watch(value.get());
+
+        g_assert_true(jsc_value_is_typed_array(value.get()));
+        g_assert_cmpuint(jsc_value_typed_array_get_length(value.get()), ==, 1);
+        g_assert_cmpuint(jsc_value_typed_array_get_offset(value.get()), ==, 0);
+        g_assert_cmpuint(jsc_value_typed_array_get_type(value.get()), ==, entry.type);
+        g_assert_cmpuint(jsc_value_typed_array_get_size(value.get()), ==, elementSize(entry.type));
+    }
+
+    for (const auto& entry : typeMap) {
+        LeakChecker checker;
+        auto context = adoptGRef(jsc_context_new());
+        checker.watch(context.get());
+        ExceptionHandler exceptionHandler(context.get());
+
+        // Create using the API, check type in JS.
+        auto value = adoptGRef(jsc_value_new_typed_array(context.get(), entry.type, 1));
+        checker.watch(value.get());
+
+        g_assert_true(jsc_value_is_typed_array(value.get()));
+        g_assert_cmpuint(jsc_value_typed_array_get_length(value.get()), ==, 1);
+        g_assert_cmpuint(jsc_value_typed_array_get_offset(value.get()), ==, 0);
+        g_assert_cmpuint(jsc_value_typed_array_get_type(value.get()), ==, entry.type);
+        g_assert_cmpuint(jsc_value_typed_array_get_size(value.get()), ==, elementSize(entry.type));
+
+        jsc_context_set_value(context.get(), "arr", value.get());
+
+        value = adoptGRef(jsc_context_evaluate(context.get(), "arr.constructor.name;", -1));
+        checker.watch(value.get());
+        g_assert_true(jsc_value_is_string(value.get()));
+        GUniquePtr<char> typeName(jsc_value_to_string(value.get()));
+        g_assert_cmpstr(typeName.get(), ==, entry.name);
+
+        value = adoptGRef(jsc_context_evaluate(context.get(), "arr.length;", -1));
+        checker.watch(value.get());
+        g_assert_true(jsc_value_is_number(value.get()));
+        g_assert_cmpint(jsc_value_to_int32(value.get()), == , 1);
+    }
+
+    {
+        LeakChecker checker;
+        auto context = adoptGRef(jsc_context_new());
+        checker.watch(context.get());
+        ExceptionHandler exceptionHandler(context.get());
+
+        int data = ""
+        auto value = adoptGRef(jsc_value_new_array_buffer(context.get(), &data, sizeof(data), nullptr, nullptr));
+        g_assert_false(jsc_value_is_typed_array(value.get()));
+        g_assert_cmpuint(jsc_value_typed_array_get_type(value.get()), ==, JSC_TYPED_ARRAY_NONE);
+    }
+
+    {
+        LeakChecker checker;
+        auto context = adoptGRef(jsc_context_new());
+        checker.watch(context.get());
+        ExceptionHandler exceptionHandler(context.get());
+
+        auto value = adoptGRef(jsc_value_new_number(context.get(), 1.23));
+        g_assert_false(jsc_value_is_typed_array(value.get()));
+        g_assert_cmpuint(jsc_value_typed_array_get_type(value.get()), ==, JSC_TYPED_ARRAY_NONE);
+    }
+
+    {
+        LeakChecker checker;
+        auto context = adoptGRef(jsc_context_new());
+        checker.watch(context.get());
+        ExceptionHandler exceptionHandler(context.get());
+
+        int16_t point3d[3] = { -1, 10, 42 };
+        auto value = adoptGRef(jsc_value_new_array_buffer(context.get(), point3d, sizeof(point3d), nullptr, nullptr));
+        checker.watch(value.get());
+
+        value = adoptGRef(jsc_value_new_typed_array_with_buffer(value.get(), JSC_TYPED_ARRAY_INT16, 0, -1));
+        checker.watch(value.get());
+
+        g_assert_cmpuint(jsc_value_typed_array_get_length(value.get()), ==, G_N_ELEMENTS(point3d));
+        g_assert_cmpuint(jsc_value_typed_array_get_size(value.get()), ==, sizeof(point3d));
+        g_assert_cmpuint(jsc_value_typed_array_get_offset(value.get()), ==, 0);
+
+        jsc_context_set_value(context.get(), "p", value.get());
+        value = adoptGRef(jsc_context_evaluate(context.get(), "p[0] + p[1] * p[2];", -1));
+        checker.watch(value.get());
+
+        g_assert_true(jsc_value_is_number(value.get()));
+        g_assert_cmpint(jsc_value_to_int32(value.get()), ==, point3d[0] + point3d[1] * point3d[2]);
+
+        point3d[0] = -20;
+        point3d[1] = -11;
+
+        value = adoptGRef(jsc_context_evaluate(context.get(), "p[0] + p[1] * p[2];", -1));
+        checker.watch(value.get());
+
+        g_assert_true(jsc_value_is_number(value.get()));
+        g_assert_cmpint(jsc_value_to_int32(value.get()), ==, point3d[0] + point3d[1] * point3d[2]);
+    }
+
+    {
+        LeakChecker checker;
+        auto context = adoptGRef(jsc_context_new());
+        checker.watch(context.get());
+        ExceptionHandler exceptionHandler(context.get());
+
+        auto value = adoptGRef(jsc_context_evaluate(context.get(), "let data = "" 150, 42); data;", -1));
+        checker.watch(value.get());
+
+        g_assert_true(jsc_value_is_typed_array(value.get()));
+        g_assert_cmpuint(jsc_value_typed_array_get_type(value.get()), ==, JSC_TYPED_ARRAY_INT32);
+        g_assert_cmpuint(jsc_value_typed_array_get_offset(value.get()), ==, 0);
+
+        gsize length = 0;
+        auto* items = static_cast<int32_t*>(jsc_value_typed_array_get_data(value.get(), &length));
+        g_assert_cmpuint(length, ==, 3);
+        g_assert_cmpuint(jsc_value_typed_array_get_size(value.get()), ==, sizeof(int32_t) * 3);
+
+        int32_t sum = 0;
+        for (auto i = 0; i < length; i++) {
+            sum += items[i];
+            items[i] = length - i;
+        }
+        g_assert_cmpint(sum, ==, -122);
+
+        value = adoptGRef(jsc_context_evaluate(context.get(), "data[0] == 3 && data[1] == 2 && data[2] == 1;", -1));
+        checker.watch(value.get());
+
+        g_assert_true(jsc_value_is_boolean(value.get()));
+        g_assert_true(jsc_value_to_boolean(value.get()));
+    }
+
+    {
+        LeakChecker checker;
+        auto context = adoptGRef(jsc_context_new());
+        checker.watch(context.get());
+        ExceptionHandler exceptionHandler(context.get());
+
+        auto value = adoptGRef(jsc_context_evaluate(context.get(), "let data = "" BigInt64Array(1); data;", -1));
+        checker.watch(value.get());
+
+        g_assert_true(jsc_value_is_typed_array(value.get()));
+        g_assert_cmpuint(jsc_value_typed_array_get_type(value.get()), ==, JSC_TYPED_ARRAY_INT64);
+        g_assert_cmpuint(jsc_value_typed_array_get_size(value.get()), ==, sizeof(int64_t));
+
+        value = adoptGRef(jsc_value_typed_array_get_buffer(value.get()));
+        checker.watch(value.get());
+
+        g_assert_true(jsc_value_is_array_buffer(value.get()));
+        gsize size = 0;
+        auto* data = "" &size));
+        g_assert_cmpuint(size, ==, sizeof(int64_t));
+
+        const int64_t zero = 0;
+        g_assert_cmpmem(data, sizeof(int64_t), &zero, sizeof(zero));
+
+        *data = ""
+        value = adoptGRef(jsc_context_evaluate(context.get(), "data[0] == 0xC0FFEE;", -1));
+        checker.watch(value.get());
+
+        g_assert_true(jsc_value_is_boolean(value.get()));
+        g_assert_true(jsc_value_to_boolean(value.get()));
+    }
+
+    {
+        LeakChecker checker;
+        auto context = adoptGRef(jsc_context_new());
+        checker.watch(context.get());
+        ExceptionHandler exceptionHandler(context.get());
+
+        uint8_t data[10];
+        for (auto i = 0; i < sizeof(data); i++)
+            data[i] = 42 + i;
+
+        auto value = adoptGRef(jsc_value_new_array_buffer(context.get(), data, sizeof(data), nullptr, nullptr));
+        checker.watch(value.get());
+
+        jsc_context_set_value(context.get(), "data", value.get());
+        value = adoptGRef(jsc_context_evaluate(context.get(), "new Uint8Array(data, 4, 2);", -1));
+        checker.watch(value.get());
+
+        g_assert_true(jsc_value_is_typed_array(value.get()));
+        g_assert_cmpuint(jsc_value_typed_array_get_type(value.get()), ==, JSC_TYPED_ARRAY_UINT8);
+        g_assert_cmpuint(jsc_value_typed_array_get_size(value.get()), ==, sizeof(uint8_t) * 2);
+        g_assert_cmpuint(jsc_value_typed_array_get_offset(value.get()), ==, sizeof(uint8_t) * 4);
+
+        gsize length = 0;
+        uint8_t* bytes = static_cast<uint8_t*>(jsc_value_typed_array_get_data(value.get(), &length));
+        g_assert_cmpuint(length, ==, 2);
+        g_assert_true(bytes == data + 4);
+    }
+
+    {
+        LeakChecker checker;
+        auto context = adoptGRef(jsc_context_new());
+        checker.watch(context.get());
+        ExceptionHandler exceptionHandler(context.get());
+
+        uint8_t data[10];
+        for (auto i = 0; i < sizeof(data); i++)
+            data[i] = 42 + i;
+
+        auto value = adoptGRef(jsc_value_new_array_buffer(context.get(), data, sizeof(data), nullptr, nullptr));
+        checker.watch(value.get());
+
+        value = adoptGRef(jsc_value_new_typed_array_with_buffer(value.get(), JSC_TYPED_ARRAY_UINT8, 4, 2));
+        checker.watch(value.get());
+
+        g_assert_true(jsc_value_is_typed_array(value.get()));
+        g_assert_cmpuint(jsc_value_typed_array_get_type(value.get()), ==, JSC_TYPED_ARRAY_UINT8);
+        g_assert_cmpuint(jsc_value_typed_array_get_size(value.get()), ==, sizeof(uint8_t) * 2);
+        g_assert_cmpuint(jsc_value_typed_array_get_offset(value.get()), ==, sizeof(uint8_t) * 4);
+
+        gsize length = 0;
+        uint8_t* bytes = static_cast<uint8_t*>(jsc_value_typed_array_get_data(value.get(), &length));
+        g_assert_cmpuint(length, ==, 2);
+        g_assert_true(bytes == data + 4);
+    }
+
+    static const struct {
+        JSCTypedArrayType type;
+        size_t size;
+        size_t offset;
+        ssize_t length;
+        ssize_t expectedLength;
+    } validOffsetsAndSizes[] = {
+        { JSC_TYPED_ARRAY_UINT8,  10, 0, -1, 10 },
+        { JSC_TYPED_ARRAY_UINT16, 10, 0, -1,  5 },
+        { JSC_TYPED_ARRAY_UINT32, 10, 0, -1,  2 },
+        { JSC_TYPED_ARRAY_UINT64, 10, 0, -1,  1 },
+        { JSC_TYPED_ARRAY_UINT64, 10, 8, -1,  0 },
+        { JSC_TYPED_ARRAY_UINT8,  10, 1, -1,  9 },
+        { JSC_TYPED_ARRAY_UINT16, 10, 2, -1,  4 },
+        { JSC_TYPED_ARRAY_UINT32, 10, 4, -1,  1 },
+        { JSC_TYPED_ARRAY_UINT64, 10, 8, -1,  0 },
+        { JSC_TYPED_ARRAY_UINT16, 20, 8,  6,  6 },
+        { JSC_TYPED_ARRAY_UINT32, 20, 8,  0,  0 },
+        { JSC_TYPED_ARRAY_UINT32, 20, 0,  0,  0 },
+    };
+
+    for (const auto& item : validOffsetsAndSizes) {
+        LeakChecker checker;
+        auto context = adoptGRef(jsc_context_new());
+        checker.watch(context.get());
+        ExceptionHandler exceptionHandler(context.get());
+
+        auto* data = ""
+        auto value = adoptGRef(jsc_value_new_array_buffer(context.get(), data, item.size, g_free, data));
+        checker.watch(value.get());
+
+        value = adoptGRef(jsc_value_new_typed_array_with_buffer(value.get(), item.type, item.offset, item.length));
+        checker.watch(value.get());
+
+        g_assert_cmpuint(jsc_value_typed_array_get_type(value.get()), ==, item.type);
+        g_assert_cmpuint(jsc_value_typed_array_get_offset(value.get()), ==, item.offset);
+        g_assert_cmpuint(jsc_value_typed_array_get_length(value.get()), ==, item.expectedLength);
+        g_assert_cmpuint(jsc_value_typed_array_get_size(value.get()), ==, elementSize(item.type) * item.expectedLength);
+    }
+
+    static const struct {
+        JSCTypedArrayType type;
+        size_t size;
+        size_t offset;
+        ssize_t length;
+    } invalidOffsetsAndSizes[] = {
+        { JSC_TYPED_ARRAY_UINT8,  1, 2, -1 },
+        { JSC_TYPED_ARRAY_UINT8,  1, 2,  1 },
+        { JSC_TYPED_ARRAY_UINT8,  1, 2,  0 },
+        { JSC_TYPED_ARRAY_UINT16, 4, 1, -1 },
+        { JSC_TYPED_ARRAY_UINT16, 4, 1,  1 },
+        { JSC_TYPED_ARRAY_UINT16, 4, 1,  0 },
+        { JSC_TYPED_ARRAY_UINT32, 0, 1, -1 },
+        { JSC_TYPED_ARRAY_UINT32, 5, 4,  1 },
+    };
+
+    for (const auto& item : invalidOffsetsAndSizes) {
+        LeakChecker checker;
+        auto context = adoptGRef(jsc_context_new());
+        checker.watch(context.get());
+        ExceptionHandler exceptionHandler(context.get());
+
+        auto* data = ""
+        auto value = adoptGRef(jsc_value_new_array_buffer(context.get(), data, item.size, g_free, data));
+        checker.watch(value.get());
+
+        bool didThrow = false;
+        g_assert_throw_begin(exceptionHandler, didThrow);
+        value = adoptGRef(jsc_value_new_typed_array_with_buffer(value.get(), item.type, item.offset, item.length));
+        g_assert_did_throw(exceptionHandler, didThrow);
+    }
+
+    {
+        LeakChecker checker;
+        auto context = adoptGRef(jsc_context_new());
+        checker.watch(context.get());
+        ExceptionHandler exceptionHandler(context.get());
+
+        // Allocation too big, cannot be indexed with a 32-bit integer.
+        bool didThrow = false;
+        g_assert_throw_begin(exceptionHandler, didThrow);
+        auto value = adoptGRef(jsc_value_new_typed_array(context.get(), JSC_TYPED_ARRAY_UINT64, SIZE_MAX));
+        g_assert_did_throw(exceptionHandler, didThrow);
+    }
+}
+
 typedef struct {
     Foo parent;
     int bar;
@@ -4332,6 +4681,7 @@
     g_test_add_func("/jsc/object", testJSCObject);
     g_test_add_func("/jsc/class", testJSCClass);
     g_test_add_func("/jsc/array-buffer", testJSCArrayBuffer);
+    g_test_add_func("/jsc/typed-array", testJSCTypedArray);
     g_test_add_func("/jsc/prototypes", testJSCPrototypes);
     g_test_add_func("/jsc/exceptions", testJSCExceptions);
     g_test_add_func("/jsc/promises", testJSCPromises);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to