Add allocate/free callbacks to ArrayBuffers (#4801)

Larger buffer allocations will throw error instead of calling jerry_fatal.

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2021-10-28 13:51:34 +02:00
committed by GitHub
parent d2388e907f
commit a024eb2118
19 changed files with 1365 additions and 716 deletions
+10 -16
View File
@@ -19,6 +19,7 @@
#include "ecma-alloc.h"
#include "ecma-array-object.h"
#include "ecma-arraybuffer-object.h"
#include "ecma-builtin-handlers.h"
#include "ecma-container-object.h"
#include "ecma-function-object.h"
@@ -1854,25 +1855,18 @@ ecma_gc_free_object (ecma_object_t *object_p) /**< object to free */
case ECMA_OBJECT_CLASS_SHARED_ARRAY_BUFFER:
#endif /* JERRY_BUILTIN_SHAREDARRAYBUFFER */
{
uint32_t arraybuffer_length = ext_object_p->u.cls.u3.length;
if (ECMA_ARRAYBUFFER_HAS_EXTERNAL_MEMORY (ext_object_p))
if (!(ECMA_ARRAYBUFFER_GET_FLAGS (ext_object_p) & ECMA_ARRAYBUFFER_HAS_POINTER))
{
ext_object_size = sizeof (ecma_arraybuffer_external_info);
/* Call external free callback if any. */
ecma_arraybuffer_external_info *array_p = (ecma_arraybuffer_external_info *) ext_object_p;
if (array_p->free_cb != NULL)
{
array_p->free_cb (array_p->buffer_p);
}
}
else
{
ext_object_size += arraybuffer_length;
ext_object_size += ext_object_p->u.cls.u3.length;
break;
}
ext_object_size = sizeof (ecma_arraybuffer_pointer_t);
if (ECMA_ARRAYBUFFER_GET_FLAGS (ext_object_p) & ECMA_ARRAYBUFFER_ALLOCATED)
{
ecma_arraybuffer_release_buffer (object_p);
}
break;
}
#endif /* JERRY_BUILTIN_TYPEDARRAY */
+8 -23
View File
@@ -2084,35 +2084,24 @@ typedef enum
} ecma_typedarray_flag_t;
/**
* ArrayBuffers flags.
* Array buffer flags.
*/
typedef enum
{
ECMA_ARRAYBUFFER_INTERNAL_MEMORY = 0u, /* ArrayBuffer memory is handled internally. */
ECMA_ARRAYBUFFER_EXTERNAL_MEMORY = (1u << 0), /* ArrayBuffer created via jerry_create_arraybuffer_external. */
ECMA_ARRAYBUFFER_DETACHED = (1u << 1), /* ArrayBuffer has been detached */
ECMA_ARRAYBUFFER_HAS_POINTER = (1u << 0), /* ArrayBuffer has a buffer pointer. */
ECMA_ARRAYBUFFER_ALLOCATED = (1u << 1), /* ArrayBuffer memory is allocated */
ECMA_ARRAYBUFFER_DETACHED = (1u << 2), /* ArrayBuffer has been detached */
} ecma_arraybuffer_flag_t;
/**
* Check whether the ArrayBuffer has external underlying buffer
*/
#define ECMA_ARRAYBUFFER_HAS_EXTERNAL_MEMORY(object_p) \
((((ecma_extended_object_t *) object_p)->u.cls.u1.array_buffer_flags & ECMA_ARRAYBUFFER_EXTERNAL_MEMORY) != 0)
/**
* Struct to store information for ArrayBuffers with external memory.
*
* The following elements are stored in Jerry memory.
*
* buffer_p - pointer to the external memory.
* free_cb - pointer to a callback function which is called when the ArrayBuffer is freed.
* Structure for array buffers with a backing store pointer.
*/
typedef struct
{
ecma_extended_object_t extended_object; /**< extended object part */
void *buffer_p; /**< external buffer pointer */
jerry_value_free_callback_t free_cb; /**< the free callback for the above buffer pointer */
} ecma_arraybuffer_external_info;
void *buffer_p; /**< pointer to the backing store of the array buffer object */
void *arraybuffer_user_p; /**< user pointer passed to the free callback */
} ecma_arraybuffer_pointer_t;
/**
* Some internal properties of TypedArray object.
@@ -2132,10 +2121,6 @@ typedef struct
typedef struct
{
ecma_object_t *array_buffer_p; /**< pointer to the typedArray's [[ViewedArrayBuffer]] internal slot */
lit_utf8_byte_t *buffer_p; /**< pointer to the underlying raw data buffer.
* Note:
* - This address is increased by the [ByteOffset]] internal property.
* - This address must be used during indexed read/write operation. */
ecma_typedarray_type_t id; /**< [[TypedArrayName]] internal slot */
uint32_t length; /**< [[ByteLength]] internal slot */
uint32_t offset; /**< [[ByteOffset]] internal slot. */
@@ -63,6 +63,10 @@ ecma_init (void)
#if JERRY_ESNEXT
JERRY_CONTEXT (current_new_target_p) = NULL;
#endif /* JERRY_ESNEXT */
#if JERRY_BUILTIN_TYPEDARRAY
JERRY_CONTEXT (arraybuffer_compact_allocation_limit) = 256;
#endif /* JERRY_BUILTIN_TYPEDARRAY */
} /* ecma_init */
/**