Skip to content

Commit beff8d1

Browse files
committed
add ES6 feature: ArrayBuffer
JerryScript-DCO-1.0-Signed-off-by: Zidong Jiang [email protected]
1 parent 7b2fc4f commit beff8d1

File tree

53 files changed

+904
-53
lines changed

Some content is hidden

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

53 files changed

+904
-53
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "re-compiler.h"
3232
#include "vm-defines.h"
3333
#include "vm-stack.h"
34+
#include "jmem-heap.h"
3435

3536
#define JERRY_INTERNAL
3637
#include "jerry-internal.h"
@@ -427,6 +428,15 @@ ecma_gc_sweep (ecma_object_t *object_p) /**< object to free */
427428
ecma_free_value (ext_object_p->u.class_prop.value);
428429
break;
429430
}
431+
case LIT_MAGIC_STRING_ARRAY_BUFFER_UL:
432+
{
433+
/* free both the buffer and arraybuffer struct */
434+
ecma_arraybuffer_t *arraybuffer_p =
435+
(ecma_arraybuffer_t *) ecma_get_pointer_from_ecma_value (ext_object_p->u.class_prop.value);
436+
jmem_heap_free_block (arraybuffer_p->buffer_p, arraybuffer_p->length);
437+
jmem_heap_free_block (arraybuffer_p, sizeof (ecma_arraybuffer_t));
438+
break;
439+
}
430440

431441
case LIT_MAGIC_STRING_DATE_UL:
432442
{

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,15 @@ typedef struct
11111111

11121112
#endif /* !CONFIG_ECMA_LCACHE_DISABLE */
11131113

1114+
/**
1115+
* ArrayBuffer struct
1116+
*/
1117+
typedef struct
1118+
{
1119+
uint32_t length;
1120+
lit_utf8_byte_t *buffer_p;
1121+
} ecma_arraybuffer_t;
1122+
11141123
/**
11151124
* @}
11161125
* @}

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

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -73,44 +73,7 @@ ecma_get_value_type_field (ecma_value_t value) /**< ecma value */
7373
return value & ECMA_VALUE_TYPE_MASK;
7474
} /* ecma_get_value_type_field */
7575

76-
/**
77-
* Convert a pointer into an ecma value.
78-
*
79-
* @return ecma value
80-
*/
81-
static inline ecma_value_t __attr_pure___ __attr_always_inline___
82-
ecma_pointer_to_ecma_value (const void *ptr) /**< pointer */
83-
{
84-
#ifdef ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY
85-
86-
uintptr_t uint_ptr = (uintptr_t) ptr;
87-
JERRY_ASSERT ((uint_ptr & ECMA_VALUE_FULL_MASK) == 0);
88-
return (ecma_value_t) uint_ptr;
89-
90-
#else /* !ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY */
91-
92-
jmem_cpointer_t ptr_cp;
93-
ECMA_SET_NON_NULL_POINTER (ptr_cp, ptr);
94-
return ((ecma_value_t) ptr_cp) << ECMA_VALUE_SHIFT;
9576

96-
#endif /* ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY */
97-
} /* ecma_pointer_to_ecma_value */
98-
99-
/**
100-
* Get a pointer from an ecma value
101-
*
102-
* @return pointer
103-
*/
104-
static inline void * __attr_pure___ __attr_always_inline___
105-
ecma_get_pointer_from_ecma_value (ecma_value_t value) /**< value */
106-
{
107-
#ifdef ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY
108-
return (void *) (uintptr_t) ((value) & ~ECMA_VALUE_FULL_MASK);
109-
#else /* !ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY */
110-
return ECMA_GET_NON_NULL_POINTER (ecma_number_t,
111-
value >> ECMA_VALUE_SHIFT);
112-
#endif /* ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY */
113-
} /* ecma_get_pointer_from_ecma_value */
11477

11578
/**
11679
* Check if the value is direct ecma-value.

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: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,46 @@
112112
}
113113

114114
/* ecma-helpers-value.c */
115+
116+
/**
117+
* Convert a pointer into an ecma value.
118+
*
119+
* @return ecma value
120+
*/
121+
static inline ecma_value_t __attr_pure___ __attr_always_inline___
122+
ecma_pointer_to_ecma_value (const void *ptr) /**< pointer */
123+
{
124+
#ifdef ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY
125+
126+
uintptr_t uint_ptr = (uintptr_t) ptr;
127+
JERRY_ASSERT ((uint_ptr & ECMA_VALUE_FULL_MASK) == 0);
128+
return (ecma_value_t) uint_ptr;
129+
130+
#else /* !ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY */
131+
132+
jmem_cpointer_t ptr_cp;
133+
ECMA_SET_NON_NULL_POINTER (ptr_cp, ptr);
134+
return ((ecma_value_t) ptr_cp) << ECMA_VALUE_SHIFT;
135+
136+
#endif /* ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY */
137+
} /* ecma_pointer_to_ecma_value */
138+
139+
/**
140+
* Get a pointer from an ecma value
141+
*
142+
* @return pointer
143+
*/
144+
static inline void * __attr_pure___ __attr_always_inline___
145+
ecma_get_pointer_from_ecma_value (ecma_value_t value) /**< value */
146+
{
147+
#ifdef ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY
148+
return (void *) (uintptr_t) ((value) & ~ECMA_VALUE_FULL_MASK);
149+
#else /* !ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY */
150+
return ECMA_GET_NON_NULL_POINTER (ecma_number_t,
151+
value >> ECMA_VALUE_SHIFT);
152+
#endif /* ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY */
153+
} /* ecma_get_pointer_from_ecma_value */
154+
115155
extern bool ecma_is_value_direct (ecma_value_t) __attr_pure___;
116156
extern bool ecma_is_value_simple (ecma_value_t) __attr_pure___;
117157
extern bool ecma_is_value_empty (ecma_value_t) __attr_pure___;
@@ -281,7 +321,8 @@ extern ecma_value_t *ecma_get_internal_property (ecma_object_t *, ecma_internal_
281321
extern ecma_property_value_t *
282322
ecma_create_named_data_property (ecma_object_t *, ecma_string_t *, uint8_t, ecma_property_t **);
283323
extern ecma_property_value_t *
284-
ecma_create_named_accessor_property (ecma_object_t *, ecma_string_t *, ecma_object_t *, ecma_object_t *, uint8_t);
324+
ecma_create_named_accessor_property (ecma_object_t *, ecma_string_t *, ecma_object_t *,
325+
ecma_object_t *, uint8_t, ecma_property_t **);
285326
extern ecma_property_t *
286327
ecma_find_named_property (ecma_object_t *, ecma_string_t *);
287328
extern ecma_property_value_t *

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,6 @@ 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_VALUE
87+
#undef ACCESSOR_RO_VALUE
88+
#undef ACCESSOR_WO_VALUE

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,6 @@ 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_VALUE
66+
#undef ACCESSOR_RO_VALUE
67+
#undef ACCESSOR_WO_VALUE
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/* Copyright 2016 Intel Corporation
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-alloc.h"
17+
#include "ecma-array-object.h"
18+
#include "ecma-builtin-helpers.h"
19+
#include "ecma-builtins.h"
20+
#include "ecma-conversion.h"
21+
#include "ecma-exceptions.h"
22+
#include "ecma-function-object.h"
23+
#include "ecma-gc.h"
24+
#include "ecma-globals.h"
25+
#include "ecma-helpers.h"
26+
#include "ecma-objects.h"
27+
#include "ecma-arraybuffer-object.h"
28+
#include "ecma-try-catch-macro.h"
29+
#include "jrt.h"
30+
#include "jrt-libc-includes.h"
31+
32+
33+
34+
#ifndef CONFIG_DISABLE_ARRAYBUFFER_BUILTIN
35+
36+
#define ECMA_BUILTINS_INTERNAL
37+
#include "ecma-builtins-internal.h"
38+
39+
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-arraybuffer-prototype.inc.h"
40+
#define BUILTIN_UNDERSCORED_ID arraybuffer_prototype
41+
#include "ecma-builtin-internal-routines-template.inc.h"
42+
43+
/** \addtogroup ecma ECMA
44+
* @{
45+
*
46+
* \addtogroup ecmabuiltins
47+
* @{
48+
*
49+
* \addtogroup arraybuffer prototype ECMA ArrayBuffer.prototype object built-in
50+
* @{
51+
*/
52+
53+
static ecma_value_t
54+
ecma_builtin_arraybuffer_prototype_bytelength_getter (ecma_value_t this_arg)
55+
{
56+
if (ecma_is_value_object (this_arg))
57+
{
58+
ecma_object_t *object_p = ecma_get_object_from_value (this_arg);
59+
if (ecma_object_class_is (object_p, LIT_MAGIC_STRING_ARRAY_BUFFER_UL))
60+
{
61+
ecma_length_t len = ecma_arraybuffer_get_length (object_p);
62+
63+
return ecma_make_uint32_value (len);
64+
}
65+
66+
}
67+
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not a ArrayBuffer object."));
68+
} /* ecma_builtin_arraybuffer_prototype_bytelength_getter */
69+
70+
/**
71+
* The ArrayBuffer.prototype object's 'slice' routine
72+
*
73+
* See also:
74+
* es6, 24.1.4.3
75+
*
76+
* @return ecma value
77+
* Returned value must be freed with ecma_free_value.
78+
*/
79+
static ecma_value_t
80+
ecma_builtin_arraybuffer_prototype_object_slice (ecma_value_t this_arg, /**< this argument */
81+
ecma_value_t arg1, /**< routine's first argument */
82+
ecma_value_t arg2) /**< routine's second argument */
83+
{
84+
if (!ecma_is_value_object (this_arg))
85+
{
86+
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not object."));
87+
}
88+
89+
ecma_object_t *object_p = ecma_get_object_from_value (this_arg);
90+
91+
if (!ecma_object_class_is (object_p, LIT_MAGIC_STRING_ARRAY_BUFFER_UL))
92+
{
93+
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not an ArrayBuffer object."));
94+
}
95+
96+
ecma_length_t len = ecma_arraybuffer_get_length (object_p);
97+
98+
ecma_length_t start = 0, end = len;
99+
100+
ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY);
101+
102+
ECMA_OP_TO_NUMBER_TRY_CATCH (start_num,
103+
arg1,
104+
ret_value);
105+
106+
start = ecma_builtin_helper_array_index_normalize (start_num, len);
107+
108+
if (ecma_is_value_undefined (arg2))
109+
{
110+
end = len;
111+
}
112+
else
113+
{
114+
ECMA_OP_TO_NUMBER_TRY_CATCH (end_num,
115+
arg2,
116+
ret_value);
117+
118+
end = ecma_builtin_helper_array_index_normalize (end_num, len);
119+
120+
ECMA_OP_TO_NUMBER_FINALIZE (end_num);
121+
}
122+
123+
ECMA_OP_TO_NUMBER_FINALIZE (start_num);
124+
125+
JERRY_ASSERT (start <= len && end <= len);
126+
ecma_length_t new_len = (end >= start) ? (end - start) : 0;
127+
ecma_object_t *new_arraybuffer_p = ecma_arraybuffer_new_object (new_len);
128+
lit_utf8_byte_t *old_buf = ecma_arraybuffer_get_buffer (object_p);
129+
lit_utf8_byte_t *new_buf = ecma_arraybuffer_get_buffer (new_arraybuffer_p);
130+
131+
memcpy (new_buf, old_buf + start, new_len);
132+
133+
return ecma_make_object_value (new_arraybuffer_p);
134+
} /* ecma_builtin_arraybuffer_prototype_object_slice */
135+
136+
/**
137+
* @}
138+
* @}
139+
* @}
140+
*/
141+
142+
#endif /* !CONFIG_DISABLE_ARRAYBUFFER_BUILTIN */
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* Copyright 2016 Intel Corporation
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_VALUE
33+
# define ACCESSOR_VALUE(name, c_getter_func_name, c_setter_func_name, prop_attributes)
34+
#endif /* !ROUTINE */
35+
36+
#ifndef ACCESSOR_RO_VALUE
37+
# define ACCESSOR_RO_VALUE(name, c_getter_func_name, prop_attributes)
38+
#endif /* !ACCESSOR_RO_VALUE */
39+
40+
#ifndef ACCESSOR_WO_VALUE
41+
# define ACCESSOR_WO_VALUE(name, c_setter_func_name, prop_attributes)
42+
#endif /* !ACCESSOR_WO_VALUE */
43+
44+
/* Object identifier */
45+
OBJECT_ID (ECMA_BUILTIN_ID_ARRAYBUFFER_PROTOTYPE)
46+
47+
/* Object properties:
48+
* (property name, object pointer getter) */
49+
50+
OBJECT_VALUE (LIT_MAGIC_STRING_CONSTRUCTOR,
51+
ECMA_BUILTIN_ID_ARRAYBUFFER,
52+
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
53+
54+
55+
56+
/* Routine properties:
57+
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */
58+
ROUTINE (LIT_MAGIC_STRING_SLICE, ecma_builtin_arraybuffer_prototype_object_slice, 2, 2)
59+
60+
/* readonly accessor properties */
61+
ACCESSOR_RO_VALUE (LIT_MAGIC_STRING_BYTE_LENGTH_UL,
62+
ecma_builtin_arraybuffer_prototype_bytelength_getter,
63+
ECMA_PROPERTY_FIXED)
64+
65+
#undef OBJECT_ID
66+
#undef SIMPLE_VALUE
67+
#undef NUMBER_VALUE
68+
#undef STRING_VALUE
69+
#undef OBJECT_VALUE
70+
#undef ROUTINE
71+
#undef ACCESSOR_VALUE
72+
#undef ACCESSOR_RO_VALUE
73+
#undef ACCESSOR_WO_VALUE

0 commit comments

Comments
 (0)