Add container related API function (#4666)

The new function returns with an array, containing the
elements of the given Container or Container iterator.

JerryScript-DCO-1.0-Signed-off-by: Bela Toth tbela@inf.u-szeged.hu
This commit is contained in:
Tóth Béla
2021-06-16 07:41:13 +02:00
committed by GitHub
parent 5729dd8cec
commit 9471515dae
6 changed files with 321 additions and 1 deletions
+69
View File
@@ -12011,3 +12011,72 @@ main (void)
return 0;
}
```
# Container Functions
## jerry_get_array_from_container
**Summary**
Return a new array containing elements from a Container, or a Container Iterator. Sets the is_key_value_p to true.
if the container object contains key-value structure and false if not.
*Notes*
- The return value will be an empty array if the Map/Set or Iterator object was empty or finished.
- This API function depends on a build option (`JERRY_BUILTIN_CONTAINER`) and can be checked
runtime with the `JERRY_FEATURE_MAP, JERRY_FEATURE_SET, JERRY_FEATURE_WEAKMAP, JERRY_FEATURE_WEAKSET`
feature enum values.
see: [jerry_is_feature_enabled](#jerry_is_feature_enabled).
- The es.next profile enables this by default.
*New in version [[NEXT_RELEASE]]*.
**Prototype**
```c
jerry_value_t
jerry_get_array_from_container(jerry_value_t value,
bool *is_key_value_p);
```
- `value` - Map/Set or iterator object
- `is_key_value` - Will be set to `true` if the given container has key-value pairs, `false` otherwise.
- return value
- jerry_value_t containing an array of values from the Map/Set or iterator object
- Error if the `value` is nor a Container or a Container Iterator.
- `undefined` if the `value` is undefined/null.
**Example**
[doctest]: # ()
```c
#include "jerryscript.h"
int
main (void)
{
jerry_init (JERRY_INIT_EMPTY);
jerry_char_t src[] = "var map = new Map(); map.set(1,2); map.entries()";
jerry_value_t iterable = jerry_eval (src, sizeof (src) - 1, JERRY_PARSE_NO_OPTS);
bool is_key_value_container = false;
jerry_value_t buffer_from_map = jerry_get_array_from_container (iterable, &is_key_value_container);
/*
The buffer_from_map contains two elements: 1 and 2, which is the key/value pair of the only item in the set.
is_key_value set to true, as the original is a key-value structure (a Map Iterator)
*/
jerry_release_value (iterable);
jerry_release_value (buffer_from_map);
jerry_cleanup ();
return 0;
}
```
**See also**
- [jerry_create_container](#jerry_create_container)
- [jerry_container_type_t](#jerry_container_type_t)