Skip to content

Commit 29d058c

Browse files
jiangzidongzherczeg
authored andcommitted
Add ES2015 feature: ArrayBuffer (#1467)
This patch implements ArrayBuffer and ArrayBuffer.prototype built-in objects. JerryScript-DCO-1.0-Signed-off-by: Zidong Jiang zidong.jiang@intel.com
1 parent eccfc18 commit 29d058c

File tree

67 files changed

+845
-59
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+845
-59
lines changed

jerry-core/CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ set(JERRY_CORE_NAME jerry-core)
1818
project (${JERRY_CORE_NAME} C)
1919

2020
# Optional features
21-
set(FEATURE_PROFILE "full" CACHE STRING "Profile types: full, minimal")
21+
set(FEATURE_PROFILE "es5.1" CACHE STRING "Profile types: es5.1, minimal, es2015-subset")
2222
set(FEATURE_ERROR_MESSAGES OFF CACHE BOOL "Enable error messages?")
2323
set(FEATURE_VALGRIND OFF CACHE BOOL "Enable Valgrind support?")
2424
set(FEATURE_VALGRIND_FREYA OFF CACHE BOOL "Enable Valgrind-Freya support?")
@@ -118,9 +118,12 @@ if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
118118
endif()
119119

120120
# Profile modes
121+
set(CONFIG_DISABLE_ES2015
122+
CONFIG_DISABLE_ARRAYBUFFER_BUILTIN)
121123
# Minimal profile
122124
if(FEATURE_PROFILE STREQUAL "minimal")
123125
set(DEFINES_JERRY ${DEFINES_JERRY}
126+
${CONFIG_DISABLE_ES2015}
124127
CONFIG_DISABLE_NUMBER_BUILTIN
125128
CONFIG_DISABLE_STRING_BUILTIN
126129
CONFIG_DISABLE_BOOLEAN_BUILTIN
@@ -131,7 +134,10 @@ if(FEATURE_PROFILE STREQUAL "minimal")
131134
CONFIG_DISABLE_DATE_BUILTIN
132135
CONFIG_DISABLE_REGEXP_BUILTIN
133136
CONFIG_DISABLE_ANNEXB_BUILTIN)
134-
elseif(NOT FEATURE_PROFILE STREQUAL "full")
137+
elseif(FEATURE_PROFILE STREQUAL "es5.1")
138+
set(DEFINES_JERRY ${DEFINES_JERRY}
139+
${CONFIG_DISABLE_ES2015})
140+
elseif(NOT FEATURE_PROFILE STREQUAL "es2015-subset")
135141
message(FATAL_ERROR "FEATURE_PROFILE='${FEATURE_PROFILE}' isn't supported")
136142
endif()
137143

jerry-core/ecma/base/ecma-gc.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,29 +424,37 @@ ecma_gc_sweep (ecma_object_t *object_p) /**< object to free */
424424
case LIT_MAGIC_STRING_STRING_UL:
425425
case LIT_MAGIC_STRING_NUMBER_UL:
426426
{
427-
ecma_free_value (ext_object_p->u.class_prop.value);
427+
ecma_free_value (ext_object_p->u.class_prop.u.value);
428428
break;
429429
}
430430

431431
case LIT_MAGIC_STRING_DATE_UL:
432432
{
433433
ecma_number_t *num_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_number_t,
434-
ext_object_p->u.class_prop.value);
434+
ext_object_p->u.class_prop.u.value);
435435
ecma_dealloc_number (num_p);
436436
break;
437437
}
438438

439439
case LIT_MAGIC_STRING_REGEXP_UL:
440440
{
441441
ecma_compiled_code_t *bytecode_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_compiled_code_t,
442-
ext_object_p->u.class_prop.value);
442+
ext_object_p->u.class_prop.u.value);
443443
if (bytecode_p != NULL)
444444
{
445445
ecma_bytecode_deref (bytecode_p);
446446
}
447447
break;
448448
}
449-
449+
#ifndef CONFIG_DISABLE_ARRAYBUFFER_BUILTIN
450+
case LIT_MAGIC_STRING_ARRAY_BUFFER_UL:
451+
{
452+
ecma_length_t arraybuffer_length = ext_object_p->u.class_prop.u.length;
453+
size_t size = sizeof (ecma_extended_object_t) + arraybuffer_length;
454+
ecma_dealloc_extended_object ((ecma_extended_object_t *) object_p, size);
455+
return;
456+
}
457+
#endif /* CONFIG_DISABLE_ARRAYBUFFER_BUILTIN */
450458
default:
451459
{
452460
JERRY_UNREACHABLE ();

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,14 @@ typedef struct
626626
struct
627627
{
628628
uint16_t class_id; /**< class id of the object */
629-
ecma_value_t value; /**< value of the object (e.g. boolean, number, string, etc.) */
629+
/*
630+
* Description of extra fields. These extra fields depends on the class_id.
631+
*/
632+
union
633+
{
634+
ecma_value_t value; /**< value of the object (e.g. boolean, number, string, etc.) */
635+
uint32_t length; /**< length related property (e.g. length of ArrayBuffer) */
636+
} u;
630637
} class_prop;
631638

632639
/*

jerry-core/ecma/base/ecma-helpers.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,9 @@ ecma_create_named_accessor_property (ecma_object_t *object_p, /**< object */
630630
ecma_string_t *name_p, /**< property name */
631631
ecma_object_t *get_p, /**< getter */
632632
ecma_object_t *set_p, /**< setter */
633-
uint8_t prop_attributes) /**< property attributes */
633+
uint8_t prop_attributes, /**< property attributes */
634+
ecma_property_t **out_prop_p) /**< [out] the property is also returned
635+
* if this field is non-NULL */
634636
{
635637
JERRY_ASSERT (object_p != NULL && name_p != NULL);
636638
JERRY_ASSERT (ecma_find_named_property (object_p, name_p) == NULL);
@@ -650,7 +652,7 @@ ecma_create_named_accessor_property (ecma_object_t *object_p, /**< object */
650652
ECMA_SET_POINTER (value.getter_setter_pair.setter_p, set_p);
651653
#endif /* JERRY_CPOINTER_32_BIT */
652654

653-
return ecma_create_property (object_p, name_p, type_and_flags, value, NULL);
655+
return ecma_create_property (object_p, name_p, type_and_flags, value, out_prop_p);
654656
} /* ecma_create_named_accessor_property */
655657

656658
/**

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@ extern ecma_value_t *ecma_get_internal_property (ecma_object_t *, ecma_internal_
282282
extern ecma_property_value_t *
283283
ecma_create_named_data_property (ecma_object_t *, ecma_string_t *, uint8_t, ecma_property_t **);
284284
extern ecma_property_value_t *
285-
ecma_create_named_accessor_property (ecma_object_t *, ecma_string_t *, ecma_object_t *, ecma_object_t *, uint8_t);
285+
ecma_create_named_accessor_property (ecma_object_t *, ecma_string_t *, ecma_object_t *,
286+
ecma_object_t *, uint8_t, ecma_property_t **);
286287
extern ecma_property_t *
287288
ecma_find_named_property (ecma_object_t *, ecma_string_t *);
288289
extern ecma_property_value_t *

jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.inc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,5 @@ ROUTINE (LIT_MAGIC_STRING_REDUCE_RIGHT_UL, ecma_builtin_array_prototype_object_r
8383
#undef STRING_VALUE
8484
#undef OBJECT_VALUE
8585
#undef ROUTINE
86-
86+
#undef ACCESSOR_READ_WRITE
87+
#undef ACCESSOR_READ_ONLY

jerry-core/ecma/builtin-objects/ecma-builtin-array.inc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,5 @@ ROUTINE (LIT_MAGIC_STRING_IS_ARRAY_UL, ecma_builtin_array_object_is_array, 1, 1)
6262
#undef STRING_VALUE
6363
#undef OBJECT_VALUE
6464
#undef ROUTINE
65-
65+
#undef ACCESSOR_READ_WRITE
66+
#undef ACCESSOR_READ_ONLY
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
#include "ecma-builtin-helpers.h"
17+
#include "ecma-builtins.h"
18+
#include "ecma-exceptions.h"
19+
#include "ecma-globals.h"
20+
#include "ecma-helpers.h"
21+
#include "ecma-objects.h"
22+
#include "ecma-arraybuffer-object.h"
23+
#include "ecma-try-catch-macro.h"
24+
#include "jrt.h"
25+
#include "jrt-libc-includes.h"
26+
27+
#ifndef CONFIG_DISABLE_ARRAYBUFFER_BUILTIN
28+
29+
#define ECMA_BUILTINS_INTERNAL
30+
#include "ecma-builtins-internal.h"
31+
32+
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-arraybuffer-prototype.inc.h"
33+
#define BUILTIN_UNDERSCORED_ID arraybuffer_prototype
34+
#include "ecma-builtin-internal-routines-template.inc.h"
35+
36+
/** \addtogroup ecma ECMA
37+
* @{
38+
*
39+
* \addtogroup ecmabuiltins
40+
* @{
41+
*
42+
* \addtogroup arraybuffer prototype ECMA ArrayBuffer.prototype object built-in
43+
* @{
44+
*/
45+
46+
/**
47+
* The ArrayBuffer.prototype.bytelength accessor
48+
*
49+
* See also:
50+
* ES2015, 24.1.4.1
51+
*
52+
* @return ecma value
53+
* Returned value must be freed with ecma_free_value.
54+
*/
55+
static ecma_value_t
56+
ecma_builtin_arraybuffer_prototype_bytelength_getter (ecma_value_t this_arg) /**< this argument */
57+
{
58+
if (ecma_is_value_object (this_arg))
59+
{
60+
ecma_object_t *object_p = ecma_get_object_from_value (this_arg);
61+
62+
if (ecma_object_class_is (object_p, LIT_MAGIC_STRING_ARRAY_BUFFER_UL))
63+
{
64+
ecma_length_t len = ecma_arraybuffer_get_length (object_p);
65+
66+
return ecma_make_uint32_value (len);
67+
}
68+
}
69+
70+
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not a ArrayBuffer object."));
71+
} /* ecma_builtin_arraybuffer_prototype_bytelength_getter */
72+
73+
/**
74+
* The ArrayBuffer.prototype object's 'slice' routine
75+
*
76+
* See also:
77+
* ES2015, 24.1.4.3
78+
*
79+
* @return ecma value
80+
* Returned value must be freed with ecma_free_value.
81+
*/
82+
static ecma_value_t
83+
ecma_builtin_arraybuffer_prototype_object_slice (ecma_value_t this_arg, /**< this argument */
84+
ecma_value_t arg1, /**< routine's first argument */
85+
ecma_value_t arg2) /**< routine's second argument */
86+
{
87+
if (!ecma_is_value_object (this_arg))
88+
{
89+
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not object."));
90+
}
91+
92+
ecma_object_t *object_p = ecma_get_object_from_value (this_arg);
93+
94+
if (!ecma_object_class_is (object_p, LIT_MAGIC_STRING_ARRAY_BUFFER_UL))
95+
{
96+
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not an ArrayBuffer object."));
97+
}
98+
99+
ecma_length_t len = ecma_arraybuffer_get_length (object_p);
100+
101+
ecma_length_t start = 0, end = len;
102+
103+
ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY);
104+
105+
ECMA_OP_TO_NUMBER_TRY_CATCH (start_num,
106+
arg1,
107+
ret_value);
108+
109+
start = ecma_builtin_helper_array_index_normalize (start_num, len);
110+
111+
if (!ecma_is_value_undefined (arg2))
112+
{
113+
ECMA_OP_TO_NUMBER_TRY_CATCH (end_num,
114+
arg2,
115+
ret_value);
116+
117+
end = ecma_builtin_helper_array_index_normalize (end_num, len);
118+
119+
ECMA_OP_TO_NUMBER_FINALIZE (end_num);
120+
}
121+
122+
ECMA_OP_TO_NUMBER_FINALIZE (start_num);
123+
124+
JERRY_ASSERT (start <= len && end <= len);
125+
ecma_length_t new_len = (end >= start) ? (end - start) : 0;
126+
ecma_object_t *new_arraybuffer_p = ecma_arraybuffer_new_object (new_len);
127+
lit_utf8_byte_t *old_buf = ecma_arraybuffer_get_buffer (object_p);
128+
lit_utf8_byte_t *new_buf = ecma_arraybuffer_get_buffer (new_arraybuffer_p);
129+
130+
memcpy (new_buf, old_buf + start, new_len);
131+
132+
return ecma_make_object_value (new_arraybuffer_p);
133+
} /* ecma_builtin_arraybuffer_prototype_object_slice */
134+
135+
/**
136+
* @}
137+
* @}
138+
* @}
139+
*/
140+
141+
#endif /* !CONFIG_DISABLE_ARRAYBUFFER_BUILTIN */
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
#ifndef OBJECT_ID
17+
# define OBJECT_ID(builtin_object_id)
18+
#endif /* !OBJECT_ID */
19+
20+
#ifndef OBJECT_VALUE
21+
# define OBJECT_VALUE(name, obj_builtin_id, prop_attributes)
22+
#endif /* !OBJECT_VALUE */
23+
24+
#ifndef NUMBER_VALUE
25+
# define NUMBER_VALUE(name, number_value, prop_attributes)
26+
#endif /* !NUMBER_VALUE */
27+
28+
#ifndef ROUTINE
29+
# define ROUTINE(name, c_function_name, args_number, length_prop_value)
30+
#endif /* !ROUTINE */
31+
32+
#ifndef ACCESSOR_READ_WRITE
33+
# define ACCESSOR_READ_WRITE(name, c_getter_func_name, c_setter_func_name, prop_attributes)
34+
#endif /* !ROUTINE */
35+
36+
#ifndef ACCESSOR_READ_ONLY
37+
# define ACCESSOR_READ_ONLY(name, c_getter_func_name, prop_attributes)
38+
#endif /* !ACCESSOR_READ_ONLY */
39+
40+
/* Object identifier */
41+
OBJECT_ID (ECMA_BUILTIN_ID_ARRAYBUFFER_PROTOTYPE)
42+
43+
/* Object properties:
44+
* (property name, object pointer getter) */
45+
46+
OBJECT_VALUE (LIT_MAGIC_STRING_CONSTRUCTOR,
47+
ECMA_BUILTIN_ID_ARRAYBUFFER,
48+
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
49+
50+
/* Readonly accessor properties */
51+
ACCESSOR_READ_ONLY (LIT_MAGIC_STRING_BYTE_LENGTH_UL,
52+
ecma_builtin_arraybuffer_prototype_bytelength_getter,
53+
ECMA_PROPERTY_FIXED)
54+
55+
/* Routine properties:
56+
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */
57+
ROUTINE (LIT_MAGIC_STRING_SLICE, ecma_builtin_arraybuffer_prototype_object_slice, 2, 2)
58+
59+
#undef OBJECT_ID
60+
#undef SIMPLE_VALUE
61+
#undef NUMBER_VALUE
62+
#undef STRING_VALUE
63+
#undef OBJECT_VALUE
64+
#undef ROUTINE
65+
#undef ACCESSOR_READ_WRITE
66+
#undef ACCESSOR_READ_ONLY

0 commit comments

Comments
 (0)