WebKit Bugzilla
New
Browse
Search+
Log In
×
Sign in with GitHub
or
Remember my login
Create Account
·
Forgot Password
Forgotten password account recovery
[patch]
WIP Patch v3
bug-197535-20220308010749.patch (text/plain), 16.16 KB, created by
Adrian Perez
on 2022-03-07 15:07:51 PST
(
hide
)
Description:
WIP Patch v3
Filename:
MIME Type:
Creator:
Adrian Perez
Created:
2022-03-07 15:07:51 PST
Size:
16.16 KB
patch
obsolete
>Subversion Revision: 290877 >diff --git a/Source/JavaScriptCore/ChangeLog b/Source/JavaScriptCore/ChangeLog >index 954fcb0689ad423249777c198f8b03c5ec7cdf98..3f266ed6ce6a06fb3aed715777526f8585eb4222 100644 >--- a/Source/JavaScriptCore/ChangeLog >+++ b/Source/JavaScriptCore/ChangeLog >@@ -1,3 +1,31 @@ >+2022-03-07 Adrian Perez de Castro <aperez@igalia.com> >+ >+ [GLib] Expose typed arrays in the public API >+ https://bugs.webkit.org/show_bug.cgi?id=197535 >+ >+ Reviewed by NOBODY (OOPS!). >+ >+ 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, hence the functions dealing with the "count" of elements inside >+ them; in order to work on a byte-by-byte fashion it is possible to retrieve an ArrayBuffer >+ representing the memory region for the typed array elements. >+ >+ * 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_type): >+ (jsc_value_typed_array_get_data): >+ (jsc_value_typed_array_get_count): >+ (jsc_value_typed_array_get_buffer): >+ * API/glib/JSCValue.h: >+ * API/glib/docs/jsc-glib-4.0-sections.txt: >+ > 2022-03-03 Adrian Perez de Castro <aperez@igalia.com> > > [GLib] Expose ArrayBuffer in the public API >diff --git a/Source/JavaScriptCore/API/glib/JSCValue.cpp b/Source/JavaScriptCore/API/glib/JSCValue.cpp >index cf5bb569ac4ade29ead53a2e2307c705f07d4c3b..8feac35b45effcf4f27624f28904469e2eee54a3 100644 >--- a/Source/JavaScriptCore/API/glib/JSCValue.cpp >+++ b/Source/JavaScriptCore/API/glib/JSCValue.cpp >@@ -1631,6 +1631,295 @@ gsize jsc_value_array_buffer_get_length(JSCValue* value) > return length; > } > >+/** >+ * 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 JSCTypedArrayType toJSCTypedArrayType(JSTypedArrayType type) >+{ >+ switch (type) { >+ case kJSTypedArrayTypeNone: >+ case kJSTypedArrayTypeArrayBuffer: return JSC_TYPED_ARRAY_NONE; >+ case kJSTypedArrayTypeInt8Array: return JSC_TYPED_ARRAY_INT8; >+ case kJSTypedArrayTypeInt16Array: return JSC_TYPED_ARRAY_INT16; >+ case kJSTypedArrayTypeInt32Array: return JSC_TYPED_ARRAY_INT32; >+ case kJSTypedArrayTypeBigInt64Array: return JSC_TYPED_ARRAY_INT64; >+ case kJSTypedArrayTypeUint8Array: return JSC_TYPED_ARRAY_UINT8; >+ case kJSTypedArrayTypeUint8ClampedArray: return JSC_TYPED_ARRAY_UINT8_CLAMPED; >+ case kJSTypedArrayTypeUint16Array: return JSC_TYPED_ARRAY_UINT16; >+ case kJSTypedArrayTypeUint32Array: return JSC_TYPED_ARRAY_UINT32; >+ case kJSTypedArrayTypeBigUint64Array: return JSC_TYPED_ARRAY_UINT64; >+ case kJSTypedArrayTypeFloat32Array: return JSC_TYPED_ARRAY_FLOAT32; >+ case kJSTypedArrayTypeFloat64Array: return JSC_TYPED_ARRAY_FLOAT64; >+ } >+ RELEASE_ASSERT_NOT_REACHED(); >+} >+ >+/** >+ * jsc_value_new_typed_array: >+ * @context: a #JSCContext >+ * @type: the type of array elements >+ * @count: 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 @count >+ * elements of a given @type. A new memory region 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); >+ >+ 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: the type of elements in the array >+ * >+ * 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. >+ * >+ * Returns: (transfer full): a #JSCValue >+ * >+ * Since: 2.38 >+ */ >+JSCValue* jsc_value_new_typed_array_with_buffer(JSCValue* arrayBuffer, JSCTypedArrayType type) >+{ >+ 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); >+ >+ 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; >+ >+ auto* jsTypedArray = JSObjectMakeTypedArrayWithArrayBuffer(jsContext, toTypedArrayType(type), jsObject, &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); >+ >+ auto* jsContext = jscContextGetJSContext(value->priv->context.get()); >+ >+ JSValueRef exception = nullptr; >+ auto type = JSValueGetTypedArrayType(jsContext, value->priv->jsValue, &exception); >+ if (jscContextHandleExceptionIfNeeded(value->priv->context.get(), exception)) >+ return FALSE; >+ >+ return type != kJSTypedArrayTypeNone && type != kJSTypedArrayTypeArrayBuffer; >+} >+ >+/** >+ * jsc_value_typed_array_get_type: >+ * @value: a #JSCValue >+ * >+ * Returns the type of the elements contained in 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); >+ >+ auto* jsContext = jscContextGetJSContext(value->priv->context.get()); >+ >+ JSValueRef exception = nullptr; >+ auto type = JSValueGetTypedArrayType(jsContext, value->priv->jsValue, &exception); >+ if (jscContextHandleExceptionIfNeeded(value->priv->context.get(), exception)) >+ return JSC_TYPED_ARRAY_NONE; >+ >+ return toJSCTypedArrayType(type); >+} >+ >+/** >+ * jsc_value_typed_array_get_data: >+ * @value: a #JSCValue >+ * @count: (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 >+ * @count 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): >+ * >+ * |[<!-- 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* count) >+{ >+ 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* ptr = 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 (count) { >+ const auto c = JSObjectGetTypedArrayLength(jsContext, jsObject, &exception); >+ if (jscContextHandleExceptionIfNeeded(value->priv->context.get(), exception)) >+ return nullptr; >+ *count = c; >+ } >+ >+ return static_cast<uint8_t*>(ptr) + offset; >+} >+ >+/** >+ * jsc_value_typed_array_get_count: >+ * @value: a #JSCValue >+ * >+ * Returns the number of elements contained in a typed array. >+ * >+ * Since: 2.38 >+ */ >+gsize jsc_value_typed_array_get_count(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 count = JSObjectGetTypedArrayLength(jsContext, jsObject, &exception); >+ if (jscContextHandleExceptionIfNeeded(value->priv->context.get(), exception)) >+ return 0; >+ >+ return count; >+} >+ >+/** >+ * 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 >diff --git a/Source/JavaScriptCore/API/glib/JSCValue.h b/Source/JavaScriptCore/API/glib/JSCValue.h >index ab9c3d842cac710d072d8289b604a6ccdfa7e35c..a3dde8cc0c2288832313faae30976407ffb97edd 100644 >--- a/Source/JavaScriptCore/API/glib/JSCValue.h >+++ b/Source/JavaScriptCore/API/glib/JSCValue.h >@@ -49,6 +49,21 @@ typedef enum { > 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,6 +278,31 @@ jsc_value_array_buffer_get_data (JSCValue *value, > JSC_API gsize > jsc_value_array_buffer_get_length (JSCValue *value); > >+JSC_API JSCValue * >+jsc_value_new_typed_array (JSCContext *context, >+ JSCTypedArrayType type, >+ gsize count); >+ >+JSC_API JSCValue * >+jsc_value_new_typed_array_with_buffer (JSCValue *array_buffer, >+ JSCTypedArrayType type); >+ >+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 *count); >+ >+JSC_API gsize >+jsc_value_typed_array_get_count (JSCValue *value); >+ >+JSC_API JSCValue * >+jsc_value_typed_array_get_buffer (JSCValue *value); >+ > JSC_API gboolean > jsc_value_is_constructor (JSCValue *value); > >diff --git a/Source/JavaScriptCore/API/glib/docs/jsc-glib-4.0-sections.txt b/Source/JavaScriptCore/API/glib/docs/jsc-glib-4.0-sections.txt >index 2076c60d256faf127eeed396b507f4c6c864aaa7..052d118b77ce7fd893b4ee63d85620d2c0772b57 100644 >--- a/Source/JavaScriptCore/API/glib/docs/jsc-glib-4.0-sections.txt >+++ b/Source/JavaScriptCore/API/glib/docs/jsc-glib-4.0-sections.txt >@@ -69,6 +69,7 @@ jsc_context_get_type > <TITLE>JSCValue</TITLE> > JSCValue > JSCValuePropertyFlags >+JSCTypedArrayType > jsc_value_get_context > jsc_value_new_undefined > jsc_value_is_undefined >@@ -114,6 +115,13 @@ jsc_value_new_array_buffer > jsc_value_is_array_buffer > jsc_value_array_buffer_get_data > jsc_value_array_buffer_get_length >+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_count >+jsc_value_typed_array_get_buffer > jsc_value_is_constructor > jsc_value_constructor_call > jsc_value_constructor_callv
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Formatted Diff
|
Diff
Attachments on
bug 197535
:
368836
|
368852
|
369379
|
369480
|
454040
|
454112
|
454538
|
454578