Skip to content

Commit c3510fc

Browse files
szilagyiadamdbatyai
authored andcommitted
Improve typedArray get, set (#3023)
Here are the following changes: - The getter and setter methods are callback based now, and we can use them with the proper typedArray id - The typedArray set_element and get_element methods are using memcpy now. - There is a new struct which contains basic informations from typedArray, and we are using this in most of the prototype methods. - Eliminated ecma_op_typedarray_set_index_prop and ecma_op_typedarray_get_index_prop, because these methods also calculated the same informations which are in the new struct, so we use the new method instead. Co-authored-by: Robert Fancsik [email protected] JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi [email protected]
1 parent f1883b9 commit c3510fc

22 files changed

+928
-881
lines changed

jerry-core/api/jerry.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3426,7 +3426,7 @@ typedef struct
34263426
{
34273427
jerry_typedarray_type_t api_type; /**< api type */
34283428
ecma_builtin_id_t prototype_id; /**< prototype ID */
3429-
lit_magic_string_id_t lit_id; /**< literal ID */
3429+
ecma_typedarray_type_t id; /**< typedArray ID */
34303430
uint8_t element_size_shift; /**< element size shift */
34313431
} jerry_typedarray_mapping_t;
34323432

@@ -3437,7 +3437,7 @@ static jerry_typedarray_mapping_t jerry_typedarray_mappings[] =
34373437
{
34383438
#define TYPEDARRAY_ENTRY(NAME, LIT_NAME, SIZE_SHIFT) \
34393439
{ JERRY_TYPEDARRAY_ ## NAME, ECMA_BUILTIN_ID_ ## NAME ## ARRAY_PROTOTYPE, \
3440-
LIT_MAGIC_STRING_ ## LIT_NAME ## _ARRAY_UL, SIZE_SHIFT }
3440+
ECMA_ ## LIT_NAME ## _ARRAY, SIZE_SHIFT }
34413441

34423442
TYPEDARRAY_ENTRY (UINT8, UINT8, 0),
34433443
TYPEDARRAY_ENTRY (UINT8CLAMPED, UINT8_CLAMPED, 0),
@@ -3455,7 +3455,7 @@ static jerry_typedarray_mapping_t jerry_typedarray_mappings[] =
34553455
};
34563456

34573457
/**
3458-
* Helper function to get the TypedArray prototype, literal id, and element size shift
3458+
* Helper function to get the TypedArray prototype, typedArray id, and element size shift
34593459
* information.
34603460
*
34613461
* @return true - if the TypedArray information was found
@@ -3464,19 +3464,19 @@ static jerry_typedarray_mapping_t jerry_typedarray_mappings[] =
34643464
static bool
34653465
jerry_typedarray_find_by_type (jerry_typedarray_type_t type_name, /**< type of the TypedArray */
34663466
ecma_builtin_id_t *prototype_id, /**< [out] found prototype object id */
3467-
lit_magic_string_id_t *lit_id, /**< [out] found literal id */
3467+
ecma_typedarray_type_t *id, /**< [out] found typedArray id */
34683468
uint8_t *element_size_shift) /**< [out] found element size shift value */
34693469
{
34703470
JERRY_ASSERT (prototype_id != NULL);
3471-
JERRY_ASSERT (lit_id != NULL);
3471+
JERRY_ASSERT (id != NULL);
34723472
JERRY_ASSERT (element_size_shift != NULL);
34733473

34743474
for (uint32_t i = 0; i < sizeof (jerry_typedarray_mappings) / sizeof (jerry_typedarray_mappings[0]); i++)
34753475
{
34763476
if (type_name == jerry_typedarray_mappings[i].api_type)
34773477
{
34783478
*prototype_id = jerry_typedarray_mappings[i].prototype_id;
3479-
*lit_id = jerry_typedarray_mappings[i].lit_id;
3479+
*id = jerry_typedarray_mappings[i].id;
34803480
*element_size_shift = jerry_typedarray_mappings[i].element_size_shift;
34813481
return true;
34823482
}
@@ -3505,10 +3505,10 @@ jerry_create_typedarray (jerry_typedarray_type_t type_name, /**< type of TypedAr
35053505

35063506
#if ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
35073507
ecma_builtin_id_t prototype_id = 0;
3508-
lit_magic_string_id_t lit_id = 0;
3508+
ecma_typedarray_type_t id = 0;
35093509
uint8_t element_size_shift = 0;
35103510

3511-
if (!jerry_typedarray_find_by_type (type_name, &prototype_id, &lit_id, &element_size_shift))
3511+
if (!jerry_typedarray_find_by_type (type_name, &prototype_id, &id, &element_size_shift))
35123512
{
35133513
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG ("incorrect type for TypedArray.")));
35143514
}
@@ -3518,7 +3518,7 @@ jerry_create_typedarray (jerry_typedarray_type_t type_name, /**< type of TypedAr
35183518
ecma_value_t array_value = ecma_typedarray_create_object_with_length (length,
35193519
prototype_obj_p,
35203520
element_size_shift,
3521-
lit_id);
3521+
id);
35223522

35233523
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (array_value));
35243524

@@ -3549,10 +3549,10 @@ jerry_create_typedarray_for_arraybuffer_sz (jerry_typedarray_type_t type_name, /
35493549

35503550
#if ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
35513551
ecma_builtin_id_t prototype_id = 0;
3552-
lit_magic_string_id_t lit_id = 0;
3552+
ecma_typedarray_type_t id = 0;
35533553
uint8_t element_size_shift = 0;
35543554

3555-
if (!jerry_typedarray_find_by_type (type_name, &prototype_id, &lit_id, &element_size_shift))
3555+
if (!jerry_typedarray_find_by_type (type_name, &prototype_id, &id, &element_size_shift))
35563556
{
35573557
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG ("incorrect type for TypedArray.")));
35583558
}
@@ -3570,7 +3570,7 @@ jerry_create_typedarray_for_arraybuffer_sz (jerry_typedarray_type_t type_name, /
35703570
ecma_make_uint32_value (length)
35713571
};
35723572

3573-
ecma_value_t array_value = ecma_op_create_typedarray (arguments_p, 3, prototype_obj_p, element_size_shift, lit_id);
3573+
ecma_value_t array_value = ecma_op_create_typedarray (arguments_p, 3, prototype_obj_p, element_size_shift, id);
35743574
ecma_free_value (arguments_p[1]);
35753575
ecma_free_value (arguments_p[2]);
35763576

@@ -3627,11 +3627,11 @@ jerry_get_typedarray_type (jerry_value_t value) /**< object to get the TypedArra
36273627
}
36283628

36293629
ecma_object_t *array_p = ecma_get_object_from_value (value);
3630+
ecma_typedarray_type_t class_type = ecma_get_typedarray_id (array_p);
36303631

3631-
lit_magic_string_id_t class_name_id = ecma_object_get_class_name (array_p);
36323632
for (uint32_t i = 0; i < sizeof (jerry_typedarray_mappings) / sizeof (jerry_typedarray_mappings[0]); i++)
36333633
{
3634-
if (class_name_id == jerry_typedarray_mappings[i].lit_id)
3634+
if (class_type == jerry_typedarray_mappings[i].id)
36353635
{
36363636
return jerry_typedarray_mappings[i].api_type;
36373637
}

jerry-core/ecma/base/ecma-globals.h

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -835,12 +835,12 @@ typedef struct
835835
{
836836
uint8_t type; /**< pseudo array type, e.g. Arguments, TypedArray, ArrayIterator */
837837
uint8_t extra_info; /**< extra information about the object.
838-
* e.g. element_width_shift for typed arrays,
838+
* e.g. the specific builtin id for typed arrays,
839839
* [[IterationKind]] property for %Iterator% */
840840
union
841841
{
842842
uint16_t length; /**< for arguments: length of names */
843-
uint16_t class_id; /**< for typedarray: the specific class name */
843+
uint16_t class_id; /**< for typedarray: the specific class name id */
844844
uint16_t iterator_index; /**< for %Iterator%: [[%Iterator%NextIndex]] property */
845845
} u1;
846846
union
@@ -1619,6 +1619,32 @@ typedef struct
16191619

16201620
#if ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
16211621

1622+
/**
1623+
* Function callback descriptor of a %TypedArray% object getter
1624+
*/
1625+
typedef ecma_number_t (*ecma_typedarray_getter_fn_t)(lit_utf8_byte_t *src);
1626+
1627+
/**
1628+
* Function callback descriptor of a %TypedArray% object setter
1629+
*/
1630+
typedef void (*ecma_typedarray_setter_fn_t)(lit_utf8_byte_t *src, ecma_number_t value);
1631+
1632+
/**
1633+
* Builtin id for the different types of TypedArray's
1634+
*/
1635+
typedef enum
1636+
{
1637+
ECMA_INT8_ARRAY, /**< Int8Array */
1638+
ECMA_UINT8_ARRAY, /**< Uint8Array */
1639+
ECMA_UINT8_CLAMPED_ARRAY, /**< Uint8ClampedArray */
1640+
ECMA_INT16_ARRAY, /**< Int16Array */
1641+
ECMA_UINT16_ARRAY, /**< Uint16Array */
1642+
ECMA_INT32_ARRAY, /**< Int32Array */
1643+
ECMA_UINT32_ARRAY, /**< Uint32Array */
1644+
ECMA_FLOAT32_ARRAY, /**< Float32Array */
1645+
ECMA_FLOAT64_ARRAY, /**< Float64Array */
1646+
} ecma_typedarray_type_t;
1647+
16221648
/**
16231649
* Extra information for ArrayBuffers.
16241650
*/
@@ -1658,6 +1684,20 @@ typedef struct
16581684
ecma_length_t array_length; /**< the array length */
16591685
} ecma_extended_typedarray_object_t;
16601686

1687+
/**
1688+
* General structure for query %TypedArray% object's properties.
1689+
**/
1690+
typedef struct
1691+
{
1692+
ecma_object_t *typedarray_buffer_p; /**< pointer to the typedArray's arraybuffer */
1693+
lit_utf8_byte_t *buffer_p; /**< pointer to the arraybuffer's internal data buffer */
1694+
ecma_typedarray_type_t typedarray_id; /**< type of the typedArray */
1695+
uint32_t typedarray_length; /**< length of the typedArray */
1696+
ecma_length_t offset; /**< offset of the internal array buffer */
1697+
uint8_t shift; /**< the element size shift in the typedArray */
1698+
uint8_t element_size; /**< size of each element in the typedArray */
1699+
} ecma_typedarray_info_t;
1700+
16611701
#endif /* ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) */
16621702

16631703
#if ENABLED (JERRY_ES2015_BUILTIN_DATAVIEW)

jerry-core/ecma/builtin-objects/ecma-builtin-dataview-prototype.c

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ enum
4141
ECMA_DATAVIEW_PROTOTYPE_BYTE_OFFSET_GETTER,
4242
ECMA_DATAVIEW_PROTOTYPE_GET_INT8,
4343
ECMA_DATAVIEW_PROTOTYPE_GET_UINT8,
44+
ECMA_DATAVIEW_PROTOTYPE_GET_UINT8_CLAMPED, /* unused value */
4445
ECMA_DATAVIEW_PROTOTYPE_GET_INT16,
4546
ECMA_DATAVIEW_PROTOTYPE_GET_UINT16,
4647
ECMA_DATAVIEW_PROTOTYPE_GET_INT32,
@@ -51,6 +52,7 @@ enum
5152
#endif /* ENABLED (JERRY_NUMBER_TYPE_FLOAT64) */
5253
ECMA_DATAVIEW_PROTOTYPE_SET_INT8,
5354
ECMA_DATAVIEW_PROTOTYPE_SET_UINT8,
55+
ECMA_DATAVIEW_PROTOTYPE_SET_UINT8_CLAMPED, /* unused value */
5456
ECMA_DATAVIEW_PROTOTYPE_SET_INT16,
5557
ECMA_DATAVIEW_PROTOTYPE_SET_UINT16,
5658
ECMA_DATAVIEW_PROTOTYPE_SET_INT32,
@@ -75,23 +77,6 @@ enum
7577
* @{
7678
*/
7779

78-
/**
79-
* Corresponding typedarray mappings for the {get,set}{[U]int, Float}{8, 16, 32, 64} routines
80-
*/
81-
static const uint8_t ecma_dataview_type_mapping[] =
82-
{
83-
ECMA_BUILTIN_ID_INT8ARRAY,
84-
ECMA_BUILTIN_ID_UINT8ARRAY,
85-
ECMA_BUILTIN_ID_INT16ARRAY,
86-
ECMA_BUILTIN_ID_UINT16ARRAY,
87-
ECMA_BUILTIN_ID_INT32ARRAY,
88-
ECMA_BUILTIN_ID_UINT32ARRAY,
89-
ECMA_BUILTIN_ID_FLOAT32ARRAY,
90-
#if ENABLED (JERRY_NUMBER_TYPE_FLOAT64)
91-
ECMA_BUILTIN_ID_FLOAT64ARRAY,
92-
#endif /* ENABLED (JERRY_NUMBER_TYPE_FLOAT64) */
93-
};
94-
9580
/**
9681
* The DataView.prototype object's {buffer, byteOffset, byteLength} getters
9782
*
@@ -168,9 +153,9 @@ ecma_builtin_dataview_prototype_dispatch_routine (uint16_t builtin_routine_id, /
168153
case ECMA_DATAVIEW_PROTOTYPE_GET_UINT32:
169154
{
170155
ecma_value_t little_endian = arguments_number > 1 ? arguments_list_p[1] : ECMA_VALUE_FALSE;
171-
uint8_t type = ecma_dataview_type_mapping[builtin_routine_id - ECMA_DATAVIEW_PROTOTYPE_GET_INT8];
156+
ecma_typedarray_type_t id = (ecma_typedarray_type_t) (builtin_routine_id - ECMA_DATAVIEW_PROTOTYPE_GET_INT8);
172157

173-
return ecma_op_dataview_get_set_view_value (this_arg, byte_offset, little_endian, ECMA_VALUE_EMPTY, type);
158+
return ecma_op_dataview_get_set_view_value (this_arg, byte_offset, little_endian, ECMA_VALUE_EMPTY, id);
174159
}
175160
case ECMA_DATAVIEW_PROTOTYPE_SET_FLOAT32:
176161
#if ENABLED (JERRY_NUMBER_TYPE_FLOAT64)
@@ -183,25 +168,25 @@ ecma_builtin_dataview_prototype_dispatch_routine (uint16_t builtin_routine_id, /
183168
{
184169
ecma_value_t value_to_set = arguments_number > 1 ? arguments_list_p[1] : ECMA_VALUE_UNDEFINED;
185170
ecma_value_t little_endian = arguments_number > 2 ? arguments_list_p[2] : ECMA_VALUE_FALSE;
186-
uint8_t type = ecma_dataview_type_mapping[builtin_routine_id - ECMA_DATAVIEW_PROTOTYPE_SET_INT8];
171+
ecma_typedarray_type_t id = (ecma_typedarray_type_t) (builtin_routine_id - ECMA_DATAVIEW_PROTOTYPE_SET_INT8);
187172

188-
return ecma_op_dataview_get_set_view_value (this_arg, byte_offset, little_endian, value_to_set, type);
173+
return ecma_op_dataview_get_set_view_value (this_arg, byte_offset, little_endian, value_to_set, id);
189174
}
190175
case ECMA_DATAVIEW_PROTOTYPE_GET_INT8:
191176
case ECMA_DATAVIEW_PROTOTYPE_GET_UINT8:
192177
{
193-
uint8_t type = ecma_dataview_type_mapping[builtin_routine_id - ECMA_DATAVIEW_PROTOTYPE_GET_INT8];
178+
ecma_typedarray_type_t id = (ecma_typedarray_type_t) (builtin_routine_id - ECMA_DATAVIEW_PROTOTYPE_GET_INT8);
194179

195-
return ecma_op_dataview_get_set_view_value (this_arg, byte_offset, ECMA_VALUE_FALSE, ECMA_VALUE_EMPTY, type);
180+
return ecma_op_dataview_get_set_view_value (this_arg, byte_offset, ECMA_VALUE_FALSE, ECMA_VALUE_EMPTY, id);
196181
}
197182
default:
198183
{
199184
JERRY_ASSERT (builtin_routine_id == ECMA_DATAVIEW_PROTOTYPE_SET_INT8
200185
|| builtin_routine_id == ECMA_DATAVIEW_PROTOTYPE_SET_UINT8);
201186
ecma_value_t value_to_set = arguments_number > 1 ? arguments_list_p[1] : ECMA_VALUE_UNDEFINED;
202-
uint8_t type = ecma_dataview_type_mapping[builtin_routine_id - ECMA_DATAVIEW_PROTOTYPE_SET_INT8];
187+
ecma_typedarray_type_t id = (ecma_typedarray_type_t) (builtin_routine_id - ECMA_DATAVIEW_PROTOTYPE_SET_INT8);
203188

204-
return ecma_op_dataview_get_set_view_value (this_arg, byte_offset, ECMA_VALUE_FALSE, value_to_set, type);
189+
return ecma_op_dataview_get_set_view_value (this_arg, byte_offset, ECMA_VALUE_FALSE, value_to_set, id);
205190
}
206191
}
207192
} /* ecma_builtin_dataview_prototype_dispatch_routine */

0 commit comments

Comments
 (0)