未验证 提交 a6a7d8c7 编写于 作者: L László Langó 提交者: GitHub

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
上级 c4ac67d4
......@@ -6426,16 +6426,13 @@ jerry_arraybuffer_write (const jerry_value_t value,
**Summary**
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
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**
......@@ -6447,26 +6444,22 @@ jerry_get_arraybuffer_pointer (const jerry_value_t value);
- `value` - Array Buffer object.
- return value
- 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**
```c
{
jerry_value_t buffer;
// acquire buffer somewhere which was created by a jerry_create_array_buffer_external call.
// create the ArrayBuffer
jerry_value_t buffer = jerry_create_arraybuffer (16);
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);
}
// required after jerry_get_arraybuffer_pointer call.
jerry_release_value (buffer);
// use the Array Buffer
// release buffer as it is not needed after this point
......
......@@ -3198,34 +3198,29 @@ jerry_get_arraybuffer_byte_length (const jerry_value_t value) /**< ArrayBuffer *
* Get a pointer for the start of the ArrayBuffer.
*
* Note:
* * Only valid for ArrayBuffers created with jerry_create_arraybuffer_external.
* * This is a high-risk operation as the bounds are not checked
* 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.
* pointer is NULL if the parameter is not an ArrayBuffer with external memory
or it is not an ArrayBuffer at all.
* pointer is NULL if the parameter is not an ArrayBuffer
*/
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 ();
#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;
}
ecma_object_t *buffer_p = ecma_get_object_from_value (value);
if (ECMA_ARRAYBUFFER_HAS_EXTERNAL_MEMORY (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;
}
ecma_object_t *buffer_p = ecma_get_object_from_value (array_buffer);
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) */
JERRY_UNUSED (value);
JERRY_UNUSED (array_buffer);
#endif /* ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) */
return NULL;
......
......@@ -326,8 +326,6 @@ main (void)
sum += data[i];
}
jerry_release_value (buffer);
const jerry_char_t eval_test_arraybuffer[] = TEST_STRING_LITERAL (
"var sum = 0;"
"for (var i = 0; i < array.length; i++)"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册