Skip to content

Commit ea577d6

Browse files
szilagyiadamrerobika
authored andcommitted
Add keys, values, entires functions for typedArray (#2925)
Extended the typedArray functionality based on ES2015 Co-authored-by: Tibor Dusnoki tdusnoki@inf.u-szeged.hu JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi aszilagy@inf.u-szeged.hu
1 parent 434994e commit ea577d6

File tree

6 files changed

+319
-2
lines changed

6 files changed

+319
-2
lines changed

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "jrt-libc-includes.h"
3030
#include "ecma-gc.h"
3131
#include "jmem.h"
32+
#include "ecma-iterator-object.h"
3233

3334
#if ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
3435

@@ -312,6 +313,91 @@ ecma_builtin_typedarray_prototype_for_each (ecma_value_t this_arg, /**< this arg
312313
TYPEDARRAY_ROUTINE_FOREACH);
313314
} /* ecma_builtin_typedarray_prototype_for_each */
314315

316+
317+
#if ENABLED (JERRY_ES2015_BUILTIN_ITERATOR)
318+
/**
319+
* Helper function for typedArray.prototype object's {'keys', 'values', 'entries', '@@iterator'}
320+
* routines common parts.
321+
*
322+
* See also:
323+
* ECMA-262 v6, 22.2.3.15
324+
* ECMA-262 v6, 22.2.3.29
325+
* ECMA-262 v6, 22.2.3.6
326+
* ECMA-262 v6, 22.1.3.30
327+
*
328+
* Note:
329+
* Returned value must be freed with ecma_free_value.
330+
*
331+
* @return iterator result object, if success
332+
* error - otherwise
333+
*/
334+
static ecma_value_t
335+
ecma_builtin_typedarray_iterators_helper (ecma_value_t this_arg, /**< this argument */
336+
uint8_t type) /**< any combination of ecma_iterator_type_t bits */
337+
{
338+
if (!ecma_is_typedarray (this_arg))
339+
{
340+
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not a TypedArray."));
341+
}
342+
343+
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_ARRAY_ITERATOR_PROTOTYPE);
344+
345+
return ecma_op_create_iterator_object (this_arg,
346+
prototype_obj_p,
347+
ECMA_PSEUDO_ARRAY_ITERATOR,
348+
type);
349+
} /* ecma_builtin_typedarray_iterators_helper */
350+
351+
/**
352+
* The %TypedArray%.prototype object's 'keys' routine
353+
*
354+
* See also:
355+
* ES2015, 22.2.3.15
356+
* ES2015, 22.1.3.30
357+
*
358+
* @return ecma value
359+
* Returned value must be freed with ecma_free_value.
360+
*/
361+
static ecma_value_t
362+
ecma_builtin_typedarray_prototype_keys (ecma_value_t this_arg) /**< this argument */
363+
{
364+
return ecma_builtin_typedarray_iterators_helper (this_arg, ECMA_ITERATOR_KEYS);
365+
} /* ecma_builtin_typedarray_prototype_keys */
366+
367+
/**
368+
* The %TypedArray%.prototype object's 'values' and @@iterator routines
369+
*
370+
* See also:
371+
* ES2015, 22.2.3.29
372+
* ES2015, 22.1.3.30
373+
*
374+
* @return ecma value
375+
* Returned value must be freed with ecma_free_value.
376+
*/
377+
static ecma_value_t
378+
ecma_builtin_typedarray_prototype_values (ecma_value_t this_arg) /**< this argument */
379+
{
380+
return ecma_builtin_typedarray_iterators_helper (this_arg, ECMA_ITERATOR_VALUES);
381+
} /* ecma_builtin_typedarray_prototype_values */
382+
383+
/**
384+
* The %TypedArray%.prototype object's 'entries' routine
385+
*
386+
* See also:
387+
* ES2015, 22.2.3.6
388+
* ES2015, 22.1.3.30
389+
*
390+
* @return ecma value
391+
* Returned value must be freed with ecma_free_value.
392+
*/
393+
static ecma_value_t
394+
ecma_builtin_typedarray_prototype_entries (ecma_value_t this_arg) /**< this argument */
395+
{
396+
return ecma_builtin_typedarray_iterators_helper (this_arg, ECMA_ITERATOR_KEYS_VALUES);
397+
} /* ecma_builtin_typedarray_prototype_entries */
398+
399+
#endif /* ENABLED (JERRY_ES2015_BUILTIN_ITERATOR) */
400+
315401
/**
316402
* The %TypedArray%.prototype object's 'map' routine
317403
*

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ ROUTINE (LIT_MAGIC_STRING_SORT, ecma_builtin_typedarray_prototype_sort, 1, 1)
7171
ROUTINE (LIT_MAGIC_STRING_FIND, ecma_builtin_typedarray_prototype_find, 2, 1)
7272
ROUTINE (LIT_MAGIC_STRING_FIND_INDEX, ecma_builtin_typedarray_prototype_find_index, 2, 1)
7373

74+
#if ENABLED (JERRY_ES2015_BUILTIN_ITERATOR)
75+
76+
ROUTINE (LIT_MAGIC_STRING_KEYS, ecma_builtin_typedarray_prototype_keys, 0, 0)
77+
ROUTINE (LIT_MAGIC_STRING_VALUES, ecma_builtin_typedarray_prototype_values, 0, 0)
78+
ROUTINE (LIT_MAGIC_STRING_ENTRIES, ecma_builtin_typedarray_prototype_entries, 0, 0)
79+
ROUTINE (LIT_GLOBAL_SYMBOL_ITERATOR, ecma_builtin_typedarray_prototype_values, 0, 0)
80+
81+
#endif /* ENABLED (JERRY_ES2015_BUILTIN_ITERATOR) */
82+
7483
#endif /* ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) */
7584

7685
#include "ecma-builtin-helpers-macro-undefs.inc.h"

jerry-core/lit/lit-magic-strings.inc.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TO_JSON_UL, "toJSON")
338338
#endif
339339
#if ENABLED (JERRY_BUILTIN_ARRAY) && ENABLED (JERRY_ES2015_BUILTIN_ITERATOR) \
340340
|| ENABLED (JERRY_ES2015_BUILTIN_ITERATOR) && ENABLED (JERRY_ES2015_BUILTIN_MAP) \
341-
|| ENABLED (JERRY_ES2015_BUILTIN_ITERATOR) && ENABLED (JERRY_ES2015_BUILTIN_SET)
341+
|| ENABLED (JERRY_ES2015_BUILTIN_ITERATOR) && ENABLED (JERRY_ES2015_BUILTIN_SET) \
342+
|| ENABLED (JERRY_ES2015_BUILTIN_ITERATOR) && ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
342343
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_VALUES, "values")
343344
#endif
344345
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_BOOLEAN_UL, "Boolean")
@@ -361,7 +362,8 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_DEFAULT, "default")
361362
#endif
362363
#if ENABLED (JERRY_BUILTIN_ARRAY) && ENABLED (JERRY_ES2015_BUILTIN_ITERATOR) \
363364
|| ENABLED (JERRY_ES2015_BUILTIN_ITERATOR) && ENABLED (JERRY_ES2015_BUILTIN_MAP) \
364-
|| ENABLED (JERRY_ES2015_BUILTIN_ITERATOR) && ENABLED (JERRY_ES2015_BUILTIN_SET)
365+
|| ENABLED (JERRY_ES2015_BUILTIN_ITERATOR) && ENABLED (JERRY_ES2015_BUILTIN_SET) \
366+
|| ENABLED (JERRY_ES2015_BUILTIN_ITERATOR) && ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
365367
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_ENTRIES, "entries")
366368
#endif
367369
#if ENABLED (JERRY_BUILTIN_ARRAY) \
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
var normal_typedarrays = [
16+
new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]),
17+
new Uint16Array([1, 2, 3, 4, 5, 6, 7, 8]),
18+
new Uint32Array([1, 2, 3, 4, 5, 6, 7, 8]),
19+
new Float32Array([1, 2, 3, 4, 5, 6, 7, 8]),
20+
new Float64Array([1, 2, 3, 4, 5, 6, 7, 8]),
21+
new Int8Array([1, 2, 3, 4, 5, 6, 7, 8]),
22+
new Int16Array([1, 2, 3, 4, 5, 6, 7, 8]),
23+
new Int32Array([1, 2, 3, 4, 5, 6, 7, 8])
24+
];
25+
26+
normal_typedarrays.forEach (function(e){
27+
try {
28+
e.prototype.entries.call (undefined);
29+
assert (false);
30+
}
31+
catch (e) {
32+
assert (e instanceof TypeError)
33+
}
34+
});
35+
36+
normal_typedarrays.forEach(function(e){
37+
var iterator = e.entries ();
38+
var current_item = iterator.next ();
39+
40+
for (var i = 0; i < e.length; i++) {
41+
assert (current_item.value[0] === i);
42+
assert (current_item.value[1] === e[i]);
43+
assert (current_item.done === false);
44+
45+
current_item = iterator.next ();
46+
}
47+
48+
assert (current_item.value === undefined);
49+
assert (current_item.done === true);
50+
});
51+
52+
var empty_typedarrays = [
53+
new Uint8Array([]),
54+
new Uint16Array([]),
55+
new Uint32Array([]),
56+
new Float32Array([]),
57+
new Float64Array([]),
58+
new Int8Array([]),
59+
new Int16Array([]),
60+
new Int32Array([])
61+
];
62+
63+
empty_typedarrays.forEach(function (e){
64+
iterator = e.entries ();
65+
current_item = iterator.next ();
66+
67+
assert (current_item.value === undefined);
68+
assert (current_item.done === true);
69+
});
70+
71+
assert ([].entries ().toString () === "[object Array Iterator]");
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
var normal_typedarrays = [
16+
new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]),
17+
new Uint16Array([1, 2, 3, 4, 5, 6, 7, 8]),
18+
new Uint32Array([1, 2, 3, 4, 5, 6, 7, 8]),
19+
new Float32Array([1, 2, 3, 4, 5, 6, 7, 8]),
20+
new Float64Array([1, 2, 3, 4, 5, 6, 7, 8]),
21+
new Int8Array([1, 2, 3, 4, 5, 6, 7, 8]),
22+
new Int16Array([1, 2, 3, 4, 5, 6, 7, 8]),
23+
new Int32Array([1, 2, 3, 4, 5, 6, 7, 8])
24+
];
25+
26+
normal_typedarrays.forEach (function(e){
27+
try {
28+
e.prototype.keys.call (undefined);
29+
assert (false);
30+
}
31+
catch (e) {
32+
assert (e instanceof TypeError)
33+
}
34+
});
35+
36+
normal_typedarrays.forEach(function (e){
37+
var iterator = e.keys ();
38+
var current_item = iterator.next ();
39+
40+
for (var i = 0; i < e.length; i++) {
41+
assert (current_item.value === i);
42+
assert (current_item.done === false);
43+
44+
current_item = iterator.next ();
45+
}
46+
47+
assert (current_item.value === undefined);
48+
assert (current_item.done === true);
49+
});
50+
51+
var empty_typedarrays = [
52+
new Uint8Array([]),
53+
new Uint16Array([]),
54+
new Uint32Array([]),
55+
new Float32Array([]),
56+
new Float64Array([]),
57+
new Int8Array([]),
58+
new Int16Array([]),
59+
new Int32Array([])
60+
];
61+
62+
empty_typedarrays.forEach(function (e){
63+
iterator = e.keys ();
64+
current_item = iterator.next ();
65+
66+
assert (current_item.value === undefined);
67+
assert (current_item.done === true);
68+
});
69+
70+
assert ([].keys ().toString () === "[object Array Iterator]");
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
var normal_typedarrays = [
16+
new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]),
17+
new Uint16Array([1, 2, 3, 4, 5, 6, 7, 8]),
18+
new Uint32Array([1, 2, 3, 4, 5, 6, 7, 8]),
19+
new Float32Array([1, 2, 3, 4, 5, 6, 7, 8]),
20+
new Float64Array([1, 2, 3, 4, 5, 6, 7, 8]),
21+
new Int8Array([1, 2, 3, 4, 5, 6, 7, 8]),
22+
new Int16Array([1, 2, 3, 4, 5, 6, 7, 8]),
23+
new Int32Array([1, 2, 3, 4, 5, 6, 7, 8])
24+
];
25+
26+
normal_typedarrays.forEach (function(e){
27+
try {
28+
e.prototype.values.call (undefined);
29+
assert (false);
30+
}
31+
catch (e) {
32+
assert (e instanceof TypeError)
33+
}
34+
});
35+
36+
normal_typedarrays.forEach(function (e){
37+
var iterator = e.values ();
38+
var symbol_iterator = e[Symbol.iterator] ();
39+
40+
var current_item = iterator.next ();
41+
var symbol_current_item = symbol_iterator.next ();
42+
43+
for (var i = 0; i < e.length; i++) {
44+
assert (current_item.value === e[i]);
45+
assert (current_item.done === false);
46+
47+
assert (current_item.value === symbol_current_item.value);
48+
assert (current_item.done === symbol_current_item.done);
49+
50+
current_item = iterator.next ();
51+
symbol_current_item = symbol_iterator.next ();
52+
}
53+
54+
assert (current_item.value === undefined);
55+
assert (current_item.done === true);
56+
assert (current_item.value === symbol_current_item.value);
57+
assert (current_item.done === symbol_current_item.done)
58+
});
59+
60+
var empty_typedarrays = [
61+
new Uint8Array([]),
62+
new Uint16Array([]),
63+
new Uint32Array([]),
64+
new Float32Array([]),
65+
new Float64Array([]),
66+
new Int8Array([]),
67+
new Int16Array([]),
68+
new Int32Array([])
69+
];
70+
71+
empty_typedarrays.forEach(function (e){
72+
iterator = e.values ();
73+
current_item = iterator.next ();
74+
75+
assert (current_item.value === undefined);
76+
assert (current_item.done === true);
77+
});
78+
79+
assert ([].values ().toString () === "[object Array Iterator]");

0 commit comments

Comments
 (0)