Skip to content

Commit 4636181

Browse files
committed
Add keys, values, entires functions for typedArray
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 9d4c231 commit 4636181

File tree

5 files changed

+405
-0
lines changed

5 files changed

+405
-0
lines changed

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

Lines changed: 90 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,95 @@ 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+
ecma_value_t this_obj = ecma_op_to_object (this_arg);
339+
340+
if (ECMA_IS_VALUE_ERROR (this_obj))
341+
{
342+
return this_obj;
343+
}
344+
345+
ecma_object_t *obj_p = ecma_get_object_from_value (this_obj);
346+
347+
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_ARRAY_ITERATOR_PROTOTYPE);
348+
349+
ecma_value_t ret_value = ecma_op_create_iterator_object (this_obj,
350+
prototype_obj_p,
351+
ECMA_PSEUDO_ARRAY_ITERATOR,
352+
type);
353+
ecma_deref_object (obj_p);
354+
355+
return ret_value;
356+
} /* ecma_builtin_array_iterators_helper */
357+
358+
/**
359+
* The %TypedArray%.prototype object's 'keys' routine
360+
*
361+
* See also:
362+
* ES2015, 22.2.3.15
363+
*
364+
* @return ecma value
365+
* Returned value must be freed with ecma_free_value.
366+
*/
367+
static ecma_value_t
368+
ecma_builtin_typedarray_prototype_keys (ecma_value_t this_arg) /**< this argument */
369+
{
370+
return ecma_builtin_typedarray_iterators_helper (this_arg, ECMA_ITERATOR_KEYS);
371+
} /* ecma_builtin_typedarray_prototype_keys */
372+
373+
/**
374+
* The %TypedArray%.prototype object's 'values' routine
375+
*
376+
* See also:
377+
* ES2015, 22.2.3.29
378+
*
379+
* @return ecma value
380+
* Returned value must be freed with ecma_free_value.
381+
*/
382+
static ecma_value_t
383+
ecma_builtin_typedarray_prototype_values (ecma_value_t this_arg) /**< this argument */
384+
{
385+
return ecma_builtin_typedarray_iterators_helper (this_arg, ECMA_ITERATOR_VALUES);
386+
} /* ecma_builtin_typedarray_prototype_values */
387+
388+
/**
389+
* The %TypedArray%.prototype object's 'entries' routine
390+
*
391+
* See also:
392+
* ES2015, 22.2.3.6
393+
*
394+
* @return ecma value
395+
* Returned value must be freed with ecma_free_value.
396+
*/
397+
static ecma_value_t
398+
ecma_builtin_typedarray_prototype_entries (ecma_value_t this_arg) /**< this argument */
399+
{
400+
return ecma_builtin_typedarray_iterators_helper (this_arg, ECMA_ITERATOR_KEYS_VALUES);
401+
} /* ecma_builtin_typedarray_prototype_entries */
402+
403+
#endif /* ENABLED (JERRY_ES2015_BUILTIN_ITERATOR) */
404+
315405
/**
316406
* The %TypedArray%.prototype object's 'map' routine
317407
*

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ 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+
80+
#endif /* ENABLED (JERRY_ES2015_BUILTIN_ITERATOR) */
81+
7482
#endif /* ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) */
7583

7684
#include "ecma-builtin-helpers-macro-undefs.inc.h"
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
try {
16+
var array = [Uint8Array, Uint16Array, Uint32Array,
17+
Float32Array, Float64Array,
18+
Int8Array, Int16Array, Int32Array];
19+
20+
array.forEach (function(e){
21+
e.prototype.entries.call (undefined);
22+
assert (false);
23+
});
24+
25+
} catch (e) {
26+
assert (e instanceof TypeError)
27+
}
28+
29+
var buffer = new ArrayBuffer(8);
30+
var array = new Uint8Array(buffer);
31+
array = [1, 2, 3, 4, 5, 6, 7, 8];
32+
33+
var iterator = array.entries ();
34+
35+
var current_item = iterator.next ();
36+
37+
for (var i = 0; i < array.length; i++) {
38+
assert (current_item.value[0] === i);
39+
assert (current_item.value[1] === array[i]);
40+
assert (current_item.done === false);
41+
42+
current_item = iterator.next ();
43+
}
44+
45+
assert (current_item.value === undefined);
46+
assert (current_item.done === true);
47+
48+
function foo_error () {
49+
throw new ReferenceError ("foo");
50+
}
51+
52+
array = [1, 2, 3, 4, 5, 6, 7];
53+
54+
['0', '3', '5'].forEach (function (e) {
55+
Object.defineProperty (array, e, { 'get' : foo_error });
56+
})
57+
58+
iterator = array.entries ();
59+
60+
var expected_values = [2, 3, 5, 7];
61+
var expected_indices = [1, 2, 4, 6];
62+
var expected_values_idx = 0;
63+
64+
for (var i = 0; i < array.length; i++) {
65+
try {
66+
current_item = iterator.next ();
67+
assert (current_item.value[0] === expected_indices[expected_values_idx]);
68+
assert (current_item.value[1] === expected_values[expected_values_idx]);
69+
assert (current_item.done === false);
70+
expected_values_idx++;
71+
} catch (e) {
72+
assert (e instanceof ReferenceError);
73+
assert (e.message === "foo");
74+
}
75+
}
76+
77+
current_item = iterator.next ();
78+
assert (current_item.value === undefined);
79+
assert (current_item.done === true);
80+
81+
/* Test empty array */
82+
array = [];
83+
84+
iterator = array.entries ();
85+
current_item = iterator.next ();
86+
87+
assert (current_item.value === undefined);
88+
assert (current_item.done === true);
89+
90+
/* Test delete elements after the iterator has been constructed */
91+
92+
array = [0, 1, 2, 3, 4, 5];
93+
iterator = array.entries ();
94+
95+
for (var i = 0; i < array.length; i++) {
96+
current_item = iterator.next ();
97+
assert (current_item.value[0] === i);
98+
assert (current_item.value[1] === array[i]);
99+
assert (current_item.done === false);
100+
array.pop();
101+
}
102+
103+
assert ([].entries ().toString () === "[object Array Iterator]");
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
try {
16+
var array = [Uint8Array, Uint16Array, Uint32Array,
17+
Float32Array, Float64Array,
18+
Int8Array, Int16Array, Int32Array];
19+
20+
array.forEach (function(e){
21+
e.prototype.keys.call (undefined);
22+
assert (false);
23+
});
24+
25+
} catch (e) {
26+
assert (e instanceof TypeError)
27+
}
28+
29+
var buffer = new ArrayBuffer(8);
30+
var array = new Uint8Array(buffer);
31+
array = [1, 2, 3, 4, 5, 6, 7, 8];
32+
33+
var iterator = array.keys ();
34+
35+
var current_item = iterator.next ();
36+
37+
for (var i = 0; i < array.length; i++) {
38+
assert (current_item.value === i);
39+
assert (current_item.done === false);
40+
41+
current_item = iterator.next ();
42+
}
43+
44+
assert (current_item.value === undefined);
45+
assert (current_item.done === true);
46+
47+
function foo_error () {
48+
throw new ReferenceError ("foo");
49+
}
50+
51+
array = [1, 2, 3, 4, 5, 6, 7];
52+
53+
['0', '3', '5'].forEach (function (e) {
54+
Object.defineProperty (array, e, { 'get' : foo_error });
55+
})
56+
57+
iterator = array.keys ();
58+
59+
for (var i = 0; i < array.length; i++) {
60+
try {
61+
current_item = iterator.next ();
62+
assert (current_item.value === i);
63+
assert (current_item.done === false);
64+
} catch (e) {
65+
assert (e instanceof ReferenceError);
66+
assert (e.message === "foo");
67+
}
68+
}
69+
70+
current_item = iterator.next ();
71+
assert (current_item.value === undefined);
72+
assert (current_item.done === true);
73+
74+
/* Test empty array */
75+
array = [];
76+
77+
iterator = array.keys ();
78+
current_item = iterator.next ();
79+
80+
assert (current_item.value === undefined);
81+
assert (current_item.done === true);
82+
83+
/* Test delete elements after the iterator has been constructed */
84+
85+
array = [0, 1, 2, 3, 4, 5];
86+
iterator = array.keys ();
87+
88+
for (var i = 0; i < array.length; i++) {
89+
current_item = iterator.next ();
90+
assert (current_item.value === i);
91+
assert (current_item.done === false);
92+
array.pop();
93+
}
94+
95+
assert ([].keys ().toString () === "[object Array Iterator]");

0 commit comments

Comments
 (0)