Make the underlying buffer of the ArrayBuffer accessible. (#2836)
JerryScript-DCO-1.0-Signed-off-by: László Langó llango.u-szeged@partner.samsung.com
This commit is contained in:
@@ -6426,16 +6426,13 @@ jerry_arraybuffer_write (const jerry_value_t value,
|
|||||||
**Summary**
|
**Summary**
|
||||||
|
|
||||||
The function allows access to the contents of the Array Buffer directly.
|
The function allows access to the contents of the Array Buffer directly.
|
||||||
Only allowed for Array Buffers which were created with
|
|
||||||
[jerry_create_arraybuffer_external](#jerry_create_arraybuffer_external)
|
|
||||||
function calls. In any other case this function will return `NULL`.
|
|
||||||
|
|
||||||
After using the pointer the [jerry_release_value](#jerry_release_value)
|
|
||||||
function must be called.
|
|
||||||
|
|
||||||
**WARNING!** This operation is for expert use only! The programmer must
|
**WARNING!** This operation is for expert use only! The programmer must
|
||||||
ensure that the returned memory area is used correctly. That is
|
ensure that the returned memory area is used correctly. That is
|
||||||
there is no out of bounds reads or writes.
|
there is no out of bounds reads or writes. The lifetime of the underlying
|
||||||
|
data buffer is managed by the ArrayBuffer value. Make sure to acquire the
|
||||||
|
value with [`jerry_acquire_value`](#jerry_acquire_value) if the data
|
||||||
|
buffer is needed later.
|
||||||
|
|
||||||
**Prototype**
|
**Prototype**
|
||||||
|
|
||||||
@@ -6447,26 +6444,22 @@ jerry_get_arraybuffer_pointer (const jerry_value_t value);
|
|||||||
- `value` - Array Buffer object.
|
- `value` - Array Buffer object.
|
||||||
- return value
|
- return value
|
||||||
- pointer to the Array Buffer's data area.
|
- pointer to the Array Buffer's data area.
|
||||||
- NULL if the `value` is not an Array Buffer object with external memory.
|
- NULL if the `value` is not an Array Buffer object.
|
||||||
|
|
||||||
**Example**
|
**Example**
|
||||||
|
|
||||||
```c
|
```c
|
||||||
{
|
{
|
||||||
jerry_value_t buffer;
|
// create the ArrayBuffer
|
||||||
|
jerry_value_t buffer = jerry_create_arraybuffer (16);
|
||||||
// acquire buffer somewhere which was created by a jerry_create_array_buffer_external call.
|
|
||||||
|
|
||||||
uint8_t *const data = jerry_get_arraybuffer_pointer (buffer);
|
uint8_t *const data = jerry_get_arraybuffer_pointer (buffer);
|
||||||
|
|
||||||
for (int i = 0; i < 22; i++)
|
for (int i = 0; i < 16; i++)
|
||||||
{
|
{
|
||||||
data[i] = (uint8_t) (i + 4);
|
data[i] = (uint8_t) (i + 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
// required after jerry_get_arraybuffer_pointer call.
|
|
||||||
jerry_release_value (buffer);
|
|
||||||
|
|
||||||
// use the Array Buffer
|
// use the Array Buffer
|
||||||
|
|
||||||
// release buffer as it is not needed after this point
|
// release buffer as it is not needed after this point
|
||||||
|
|||||||
+9
-14
@@ -3198,34 +3198,29 @@ jerry_get_arraybuffer_byte_length (const jerry_value_t value) /**< ArrayBuffer *
|
|||||||
* Get a pointer for the start of the ArrayBuffer.
|
* Get a pointer for the start of the ArrayBuffer.
|
||||||
*
|
*
|
||||||
* Note:
|
* Note:
|
||||||
* * Only valid for ArrayBuffers created with jerry_create_arraybuffer_external.
|
|
||||||
* * This is a high-risk operation as the bounds are not checked
|
* * This is a high-risk operation as the bounds are not checked
|
||||||
* when accessing the pointer elements.
|
* when accessing the pointer elements.
|
||||||
* * jerry_release_value must be called on the ArrayBuffer when the pointer is no longer needed.
|
|
||||||
*
|
*
|
||||||
* @return pointer to the back-buffer of the ArrayBuffer.
|
* @return pointer to the back-buffer of the ArrayBuffer.
|
||||||
* pointer is NULL if the parameter is not an ArrayBuffer with external memory
|
* pointer is NULL if the parameter is not an ArrayBuffer
|
||||||
or it is not an ArrayBuffer at all.
|
|
||||||
*/
|
*/
|
||||||
uint8_t *
|
uint8_t *
|
||||||
jerry_get_arraybuffer_pointer (const jerry_value_t value) /**< Array Buffer to use */
|
jerry_get_arraybuffer_pointer (const jerry_value_t array_buffer) /**< Array Buffer to use */
|
||||||
{
|
{
|
||||||
jerry_assert_api_available ();
|
jerry_assert_api_available ();
|
||||||
|
|
||||||
#if ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
|
#if ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
|
||||||
if (!ecma_is_arraybuffer (value))
|
if (ecma_is_value_error_reference (array_buffer)
|
||||||
|
|| !ecma_is_arraybuffer (array_buffer))
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
ecma_object_t *buffer_p = ecma_get_object_from_value (value);
|
ecma_object_t *buffer_p = ecma_get_object_from_value (array_buffer);
|
||||||
if (ECMA_ARRAYBUFFER_HAS_EXTERNAL_MEMORY (buffer_p))
|
lit_utf8_byte_t *mem_buffer_p = ecma_arraybuffer_get_buffer (buffer_p);
|
||||||
{
|
return (uint8_t *const) mem_buffer_p;
|
||||||
jerry_acquire_value (value);
|
|
||||||
lit_utf8_byte_t *mem_buffer_p = ecma_arraybuffer_get_buffer (buffer_p);
|
|
||||||
return (uint8_t *const) mem_buffer_p;
|
|
||||||
}
|
|
||||||
#else /* !ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) */
|
#else /* !ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) */
|
||||||
JERRY_UNUSED (value);
|
JERRY_UNUSED (array_buffer);
|
||||||
#endif /* ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) */
|
#endif /* ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) */
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
@@ -326,8 +326,6 @@ main (void)
|
|||||||
sum += data[i];
|
sum += data[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
jerry_release_value (buffer);
|
|
||||||
|
|
||||||
const jerry_char_t eval_test_arraybuffer[] = TEST_STRING_LITERAL (
|
const jerry_char_t eval_test_arraybuffer[] = TEST_STRING_LITERAL (
|
||||||
"var sum = 0;"
|
"var sum = 0;"
|
||||||
"for (var i = 0; i < array.length; i++)"
|
"for (var i = 0; i < array.length; i++)"
|
||||||
|
|||||||
Reference in New Issue
Block a user