Skip to content

Commit 2852a78

Browse files
committed
[ES2015 profile]add TypedArray intrinsic object
* add %TypedArray% intrinsic object * implement Int8Array * will implement other types and prototype functions in the following patches. JerryScript-DCO-1.0-Signed-off-by: Zidong Jiang [email protected]
1 parent 212f35c commit 2852a78

Some content is hidden

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

43 files changed

+2326
-119
lines changed

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

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
#include "vm-defines.h"
3232
#include "vm-stack.h"
3333

34+
#ifndef CONFIG_DISABLE_ARRAYBUFFER_BUILTIN
35+
#include "ecma-typedarray-object.h"
36+
#endif
37+
3438
#define JERRY_INTERNAL
3539
#include "jerry-internal.h"
3640

@@ -259,14 +263,35 @@ ecma_gc_mark (ecma_object_t *object_p) /**< object to mark from */
259263

260264
switch (ecma_get_object_type (object_p))
261265
{
262-
case ECMA_OBJECT_TYPE_ARGUMENTS:
266+
case ECMA_OBJECT_TYPE_PSEUDO_ARRAY:
263267
{
264268
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
265269

266-
ecma_object_t *lex_env_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_object_t,
267-
ext_object_p->u.arguments.lex_env_cp);
270+
switch (ext_object_p->u.pseudo_array.type)
271+
{
272+
case ECMA_PSEUDO_ARRAY_ARGUMENTS:
273+
{
274+
ecma_object_t *lex_env_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_object_t,
275+
ext_object_p->u.pseudo_array.u2.lex_env_cp);
276+
277+
ecma_gc_set_object_visited (lex_env_p, true);
278+
break;
279+
}
280+
#ifndef CONFIG_DISABLE_TYPEDARRAY_BUILTIN
281+
case ECMA_PSEUDO_ARRAY_TYPEDARRAY:
282+
case ECMA_PSEUDO_ARRAY_TYPEDARRAY_WITH_INFO:
283+
{
284+
ecma_gc_set_object_visited (ecma_typedarray_get_arraybuffer (object_p), true);
285+
break;
286+
}
287+
#endif /* !CONFIG_DISABLE_TYPEDARRAY_BUILTIN */
288+
default:
289+
{
290+
JERRY_UNREACHABLE ();
291+
break;
292+
}
293+
}
268294

269-
ecma_gc_set_object_visited (lex_env_p, true);
270295
break;
271296
}
272297
case ECMA_OBJECT_TYPE_BOUND_FUNCTION:
@@ -503,25 +528,52 @@ ecma_gc_sweep (ecma_object_t *object_p) /**< object to free */
503528
return;
504529
}
505530

506-
if (object_type == ECMA_OBJECT_TYPE_ARGUMENTS)
531+
if (object_type == ECMA_OBJECT_TYPE_PSEUDO_ARRAY)
507532
{
508533
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
509534

510-
ecma_length_t formal_params_number = ext_object_p->u.arguments.length;
511-
jmem_cpointer_t *arg_Literal_p = (jmem_cpointer_t *) (ext_object_p + 1);
512-
513-
for (ecma_length_t i = 0; i < formal_params_number; i++)
535+
switch (ext_object_p->u.pseudo_array.type)
514536
{
515-
if (arg_Literal_p[i] != JMEM_CP_NULL)
537+
case ECMA_PSEUDO_ARRAY_ARGUMENTS:
538+
{
539+
ecma_length_t formal_params_number = ext_object_p->u.pseudo_array.u1.length;
540+
jmem_cpointer_t *arg_Literal_p = (jmem_cpointer_t *) (ext_object_p + 1);
541+
542+
for (ecma_length_t i = 0; i < formal_params_number; i++)
543+
{
544+
if (arg_Literal_p[i] != JMEM_CP_NULL)
545+
{
546+
ecma_string_t *name_p = JMEM_CP_GET_NON_NULL_POINTER (ecma_string_t, arg_Literal_p[i]);
547+
ecma_deref_ecma_string (name_p);
548+
}
549+
}
550+
551+
size_t formal_params_size = formal_params_number * sizeof (jmem_cpointer_t);
552+
ecma_dealloc_extended_object (ext_object_p, sizeof (ecma_extended_object_t) + formal_params_size);
553+
return;
554+
}
555+
#ifndef CONFIG_DISABLE_TYPEDARRAY_BUILTIN
556+
case ECMA_PSEUDO_ARRAY_TYPEDARRAY:
516557
{
517-
ecma_string_t *name_p = JMEM_CP_GET_NON_NULL_POINTER (ecma_string_t, arg_Literal_p[i]);
518-
ecma_deref_ecma_string (name_p);
558+
ecma_dealloc_extended_object ((ecma_extended_object_t *) object_p,
559+
sizeof (ecma_extended_object_t));
560+
return;
561+
}
562+
case ECMA_PSEUDO_ARRAY_TYPEDARRAY_WITH_INFO:
563+
{
564+
ecma_dealloc_extended_object ((ecma_extended_object_t *) object_p,
565+
sizeof (ecma_extended_typedarray_object_t));
566+
return;
567+
}
568+
#endif /* !CONFIG_DISABLE_TYPEDARRAY_BUILTIN */
569+
default:
570+
{
571+
JERRY_UNREACHABLE ();
572+
break;
519573
}
520574
}
521575

522-
size_t formal_params_size = formal_params_number * sizeof (jmem_cpointer_t);
523-
ecma_dealloc_extended_object (ext_object_p, sizeof (ecma_extended_object_t) + formal_params_size);
524-
return;
576+
JERRY_UNREACHABLE ();
525577
}
526578

527579
if (object_type == ECMA_OBJECT_TYPE_BOUND_FUNCTION)

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

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ typedef enum
204204

205205
ECMA_INTERNAL_PROPERTY_NATIVE_HANDLE, /**< native handle associated with an object */
206206
ECMA_INTERNAL_PROPERTY_FREE_CALLBACK, /**< object's native free callback */
207-
208207
ECMA_SPECIAL_PROPERTY__COUNT /**< Number of special property types */
209208
} ecma_internal_property_id_t;
210209

@@ -291,6 +290,11 @@ typedef enum
291290
*/
292291
#define ECMA_PROPERTY_TYPE_NOT_FOUND ECMA_PROPERTY_TYPE_DELETED
293292

293+
/**
294+
* Type of property not found and no more searching in the proto chain.
295+
*/
296+
#define ECMA_PROPERTY_TYPE_NOT_FOUND_AND_STOP ECMA_PROPERTY_TYPE_HASHMAP
297+
294298
/**
295299
* Property flag list (for ECMA_PROPERTY_TYPE_NAMEDDATA
296300
* and ECMA_PROPERTY_TYPE_NAMEDACCESSOR).
@@ -321,6 +325,12 @@ typedef enum
321325
#define ECMA_PROPERTY_CONFIGURABLE_WRITABLE \
322326
(ECMA_PROPERTY_FLAG_CONFIGURABLE | ECMA_PROPERTY_FLAG_WRITABLE)
323327

328+
/**
329+
* Property flags enumerable, writable.
330+
*/
331+
#define ECMA_PROPERTY_ENUMERABLE_WRITABLE \
332+
(ECMA_PROPERTY_FLAG_ENUMERABLE | ECMA_PROPERTY_FLAG_WRITABLE)
333+
324334
/**
325335
* No attributes can be changed for this property.
326336
*/
@@ -488,7 +498,7 @@ typedef struct
488498
} ecma_extended_property_ref_t;
489499

490500
/**
491-
* Option flags for ecma_op_object_get_property
501+
* Option flags for ecma_op_object_get_property.
492502
*/
493503
typedef enum
494504
{
@@ -498,7 +508,7 @@ typedef enum
498508
} ecma_property_get_option_bits_t;
499509

500510
/**
501-
* Internal object types
511+
* Internal object types.
502512
*/
503513
typedef enum
504514
{
@@ -509,13 +519,25 @@ typedef enum
509519
ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION = 3, /**< External (host) function object */
510520
ECMA_OBJECT_TYPE_ARRAY = 4, /**< Array object (15.4) */
511521
ECMA_OBJECT_TYPE_BOUND_FUNCTION = 5, /**< Function objects (15.3), created through 15.3.4.5 routine */
512-
ECMA_OBJECT_TYPE_ARGUMENTS = 6, /**< Arguments object (10.6) */
522+
ECMA_OBJECT_TYPE_PSEUDO_ARRAY = 6, /**< Array-like object, such as Arguments object (10.6) */
513523

514-
ECMA_OBJECT_TYPE__MAX = ECMA_OBJECT_TYPE_ARGUMENTS /**< maximum value */
524+
ECMA_OBJECT_TYPE__MAX = ECMA_OBJECT_TYPE_PSEUDO_ARRAY /**< maximum value */
515525
} ecma_object_type_t;
516526

517527
/**
518-
* Types of lexical environments
528+
* Types of objects with class property.
529+
*/
530+
typedef enum
531+
{
532+
ECMA_PSEUDO_ARRAY_ARGUMENTS = 0, /**< Arguments object (10.6) */
533+
ECMA_PSEUDO_ARRAY_TYPEDARRAY = 1, /**< TypedArray which does NOT need extra space to store length and offset */
534+
ECMA_PSEUDO_ARRAY_TYPEDARRAY_WITH_INFO = 2, /**< TypedArray which NEEDS extra space to store length and offset */
535+
536+
ECMA_PSEUDO_ARRAY__MAX = ECMA_PSEUDO_ARRAY_TYPEDARRAY_WITH_INFO /**< maximum value */
537+
} ecma_pseudo_array_type_t;
538+
539+
/**
540+
* Types of lexical environments.
519541
*/
520542
typedef enum
521543
{
@@ -629,13 +651,14 @@ typedef struct
629651
struct
630652
{
631653
uint16_t class_id; /**< class id of the object */
654+
632655
/*
633656
* Description of extra fields. These extra fields depends on the class_id.
634657
*/
635658
union
636659
{
637660
ecma_value_t value; /**< value of the object (e.g. boolean, number, string, etc.) */
638-
uint32_t length; /**< length related property (e.g. length of ArrayBuffer) */
661+
uint32_t length; /**< length related property (e.g. length of ArrayBuffer) */
639662
} u;
640663
} class_prop;
641664

@@ -658,13 +681,24 @@ typedef struct
658681
} array;
659682

660683
/*
661-
* Description of arguments objects.
684+
* Description of pseudo array objects.
662685
*/
663686
struct
664687
{
665-
ecma_value_t lex_env_cp; /**< lexical environment */
666-
uint32_t length; /**< length of names */
667-
} arguments;
688+
uint8_t type; /**< pseudo array type, e.g. Arguments, TypedArray*/
689+
uint8_t extra_info; /**< extra infomations about the object.
690+
* e.g. element_width_shift for typed arrays */
691+
union
692+
{
693+
uint16_t length; /**< for arguments: length of names */
694+
uint16_t class_id; /**< for typedarray: the specific class name */
695+
} u1;
696+
union
697+
{
698+
ecma_value_t lex_env_cp; /**< for arguments: lexical environment */
699+
ecma_value_t arraybuffer; /**< for typedarray: internal arraybuffer */
700+
} u2;
701+
} pseudo_array;
668702

669703
/*
670704
* Description of bound function object.
@@ -1121,6 +1155,21 @@ typedef struct
11211155

11221156
#endif /* !CONFIG_ECMA_LCACHE_DISABLE */
11231157

1158+
#ifndef CONFIG_DISABLE_TYPEDARRAY_BUILTIN
1159+
1160+
/**
1161+
* Some internal properties of TypedArray object.
1162+
* It is only used when the offset is not 0, and
1163+
* the array-length is not buffer-length / element_size.
1164+
*/
1165+
typedef struct
1166+
{
1167+
ecma_extended_object_t extended_object; /**< extended object part */
1168+
ecma_length_t byte_offset; /**< the byteoffset of the above arraybuffer */
1169+
ecma_length_t array_length; /**< the array length */
1170+
} ecma_extended_typedarray_object_t;
1171+
1172+
#endif /* !CONFIG_DISABLE_TYPEDARRAY_BUILTIN */
11241173
/**
11251174
* @}
11261175
* @}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ OBJECT_VALUE (LIT_MAGIC_STRING_ARRAY_BUFFER_UL,
174174
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
175175
#endif /* !CONFIG_DISABLE_ARRAYBUFFER_BUILTIN */
176176

177+
#ifndef CONFIG_DISABLE_TYPEDARRAY_BUILTIN
178+
OBJECT_VALUE (LIT_MAGIC_STRING_INT8_ARRAY_UL,
179+
ECMA_BUILTIN_ID_INT8ARRAY,
180+
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
181+
#endif /* !CONFIG_DISABLE_ARRAYBUFFER_BUILTIN */
177182
/* Routine properties:
178183
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */
179184

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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-builtins.h"
17+
18+
#ifndef CONFIG_DISABLE_TYPEDARRAY_BUILTIN
19+
20+
#define ECMA_BUILTINS_INTERNAL
21+
#include "ecma-builtins-internal.h"
22+
23+
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-int8array-prototype.inc.h"
24+
#define BUILTIN_UNDERSCORED_ID int8array_prototype
25+
#include "ecma-builtin-internal-routines-template.inc.h"
26+
27+
/** \addtogroup ecma ECMA
28+
* @{
29+
*
30+
* \addtogroup ecmabuiltins
31+
* @{
32+
*
33+
* \addtogroup int8array prototype ECMA Int8Array.prototype object built-in
34+
* @{
35+
*/
36+
37+
/**
38+
* @}
39+
* @}
40+
* @}
41+
*/
42+
43+
#endif /* !CONFIG_DISABLE_TYPEDARRAY_BUILTIN */
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
/*
17+
* Int8Array prototype description
18+
*/
19+
20+
#ifndef OBJECT_ID
21+
# define OBJECT_ID(builtin_object_id)
22+
#endif /* !OBJECT_ID */
23+
24+
#ifndef NUMBER_VALUE
25+
# define NUMBER_VALUE(name, number_value, prop_attributes)
26+
#endif /* !NUMBER_VALUE */
27+
28+
#ifndef STRING_VALUE
29+
# define STRING_VALUE(name, magic_string_id, prop_attributes)
30+
#endif /* !STRING_VALUE */
31+
32+
#ifndef OBJECT_VALUE
33+
# define OBJECT_VALUE(name, obj_builtin_id, prop_attributes)
34+
#endif /* !OBJECT_VALUE */
35+
36+
#ifndef ROUTINE
37+
# define ROUTINE(name, c_function_name, args_number, length_prop_value)
38+
#endif /* !ROUTINE */
39+
40+
/* Object identifier */
41+
OBJECT_ID (ECMA_BUILTIN_ID_TYPEDARRAY_PROTOTYPE)
42+
43+
/* ES2015 22.2.3.4 */
44+
OBJECT_VALUE (LIT_MAGIC_STRING_CONSTRUCTOR,
45+
ECMA_BUILTIN_ID_INT8ARRAY,
46+
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
47+
48+
/* ES2015 22.2.6.1 */
49+
NUMBER_VALUE (LIT_MAGIC_STRING_BYTES_PER_ELEMENT_U,
50+
1,
51+
ECMA_PROPERTY_FIXED)
52+
53+
#undef OBJECT_ID
54+
#undef SIMPLE_VALUE
55+
#undef NUMBER_VALUE
56+
#undef STRING_VALUE
57+
#undef OBJECT_VALUE
58+
#undef ROUTINE
59+
#undef ACCESSOR_READ_WRITE
60+
#undef ACCESSOR_READ_ONLY

0 commit comments

Comments
 (0)