Skip to content

Commit 63d4e39

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 63d4e39

File tree

5 files changed

+294
-0
lines changed

5 files changed

+294
-0
lines changed

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

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

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"
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 array = [Uint8Array, Uint16Array, Uint32Array,
16+
Float32Array, Float64Array,
17+
Int8Array, Int16Array, Int32Array];
18+
19+
array.forEach (function(e){
20+
try {
21+
e.prototype.entries.call (undefined);
22+
assert (false);
23+
}
24+
catch (e) {
25+
assert (e instanceof TypeError)
26+
}
27+
});
28+
29+
var buffer = new ArrayBuffer(8);
30+
var array = new Uint8Array(buffer);
31+
array.set([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+
/* Test empty typedArray */
49+
var buffer = new ArrayBuffer(0);
50+
var array = new Uint8Array(buffer);
51+
array.set([]);
52+
53+
iterator = array.entries ();
54+
current_item = iterator.next ();
55+
56+
assert (current_item.value === undefined);
57+
assert (current_item.done === true);
58+
59+
assert ([].entries ().toString () === "[object Array Iterator]");
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 array = [Uint8Array, Uint16Array, Uint32Array,
16+
Float32Array, Float64Array,
17+
Int8Array, Int16Array, Int32Array];
18+
19+
array.forEach (function(e){
20+
try {
21+
e.prototype.keys.call (undefined);
22+
assert (false);
23+
}
24+
catch (e) {
25+
assert (e instanceof TypeError)
26+
}
27+
});
28+
29+
var buffer = new ArrayBuffer(8);
30+
var array = new Uint8Array(buffer);
31+
array.set([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+
/* Test empty typedArray */
48+
var buffer = new ArrayBuffer(0);
49+
var array = new Uint8Array(buffer);
50+
array.set([]);
51+
52+
iterator = array.keys ();
53+
current_item = iterator.next ();
54+
55+
assert (current_item.value === undefined);
56+
assert (current_item.done === true);
57+
58+
assert ([].keys ().toString () === "[object Array Iterator]");
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 array = [Uint8Array, Uint16Array, Uint32Array,
16+
Float32Array, Float64Array,
17+
Int8Array, Int16Array, Int32Array];
18+
19+
array.forEach (function(e){
20+
try {
21+
e.prototype.values.call (undefined);
22+
assert (false);
23+
}
24+
catch (e) {
25+
assert (e instanceof TypeError)
26+
}
27+
});
28+
29+
var buffer = new ArrayBuffer(8);
30+
var array = new Uint8Array(buffer);
31+
array.set([1, 2, 3, 4, 5, 6, 7, 8]);
32+
33+
var iterator = array.values ();
34+
35+
/* The initial value of the @@iterator property is the same function object
36+
as the initial value of the Array.prototype.values property. */
37+
var symbol_iterator = array[Symbol.iterator] ();
38+
39+
var current_item = iterator.next ();
40+
var symbol_current_item = symbol_iterator.next ();
41+
42+
for (var i = 0; i < array.length; i++) {
43+
assert (current_item.value === array[i]);
44+
assert (current_item.done === false);
45+
46+
assert (current_item.value === symbol_current_item.value);
47+
assert (current_item.done === symbol_current_item.done);
48+
49+
current_item = iterator.next ();
50+
symbol_current_item = symbol_iterator.next ();
51+
}
52+
53+
assert (current_item.value === undefined);
54+
assert (current_item.done === true);
55+
assert (current_item.value === symbol_current_item.value);
56+
assert (current_item.done === symbol_current_item.done);
57+
58+
/* Test empty typedArray */
59+
var buffer = new ArrayBuffer(0);
60+
var array = new Uint8Array(buffer);
61+
array.set([]);
62+
63+
iterator = array.values ();
64+
current_item = iterator.next ();
65+
66+
assert (current_item.value === undefined);
67+
assert (current_item.done === true);
68+
69+
assert ([].values ().toString () === "[object Array Iterator]");

0 commit comments

Comments
 (0)