|
16 | 16 | #include "jerryscript.h"
|
17 | 17 | #include "test-common.h"
|
18 | 18 |
|
| 19 | +static bool |
| 20 | +count_objects (jerry_value_t object, void *user_arg) |
| 21 | +{ |
| 22 | + (void) object; |
| 23 | + TEST_ASSERT (user_arg != NULL); |
| 24 | + |
| 25 | + int *counter = (int *) user_arg; |
| 26 | + |
| 27 | + (*counter)++; |
| 28 | + return true; |
| 29 | +} /* count_objects */ |
| 30 | + |
| 31 | +static void |
| 32 | +test_container (void) |
| 33 | +{ |
| 34 | + jerry_value_t global = jerry_get_global_object (); |
| 35 | + jerry_value_t map_str = jerry_create_string ((const jerry_char_t *) "Map"); |
| 36 | + jerry_value_t map_result = jerry_get_property (global, map_str); |
| 37 | + jerry_type_t type = jerry_value_get_type (map_result); |
| 38 | + |
| 39 | + jerry_release_value (map_result); |
| 40 | + jerry_release_value (map_str); |
| 41 | + jerry_release_value (global); |
| 42 | + |
| 43 | + /* If there is no Map function this is not an es2015 profile build, skip this test case. */ |
| 44 | + if (type != JERRY_TYPE_FUNCTION) |
| 45 | + { |
| 46 | + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Container based test is disabled!\n"); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + const char *eval_str = "new Map ([[1, 2], [3, 4]])"; |
| 51 | + { |
| 52 | + /* Make sure that the Map and it's prototype object/function is initialized. */ |
| 53 | + jerry_value_t result = jerry_eval ((const jerry_char_t *) eval_str, sizeof (eval_str) - 1, 0); |
| 54 | + jerry_release_value (result); |
| 55 | + } |
| 56 | + |
| 57 | + /* Get the number of iterable objects. */ |
| 58 | + int start_count = 0; |
| 59 | + jerry_objects_foreach (count_objects, &start_count); |
| 60 | + |
| 61 | + /* Create another map. */ |
| 62 | + jerry_value_t result = jerry_eval ((const jerry_char_t *) eval_str, sizeof (eval_str) - 1, 0); |
| 63 | + |
| 64 | + /* Get the current number of objects. */ |
| 65 | + int end_count = 0; |
| 66 | + jerry_objects_foreach (count_objects, &end_count); |
| 67 | + |
| 68 | + /* As only one Map was created the number of iterable objects should be incremented only by one. */ |
| 69 | + TEST_ASSERT (end_count > start_count); |
| 70 | + TEST_ASSERT ((end_count - start_count) == 1); |
| 71 | + |
| 72 | + jerry_release_value (result); |
| 73 | +} /* test_container */ |
| 74 | + |
| 75 | +static void |
| 76 | +test_internal_prop (void) |
| 77 | +{ |
| 78 | + /* Make sure that the object is initialized in the engine. */ |
| 79 | + { |
| 80 | + jerry_value_t object = jerry_create_object (); |
| 81 | + jerry_release_value (object); |
| 82 | + } |
| 83 | + |
| 84 | + /* Get the number of iterable objects. */ |
| 85 | + int before_object_count = 0; |
| 86 | + jerry_objects_foreach (count_objects, &before_object_count); |
| 87 | + |
| 88 | + jerry_value_t object = jerry_create_object (); |
| 89 | + |
| 90 | + /* After creating the object, the number of objects is incremented by one. */ |
| 91 | + int after_object_count = 0; |
| 92 | + { |
| 93 | + jerry_objects_foreach (count_objects, &after_object_count); |
| 94 | + |
| 95 | + TEST_ASSERT (after_object_count > before_object_count); |
| 96 | + TEST_ASSERT ((after_object_count - before_object_count) == 1); |
| 97 | + } |
| 98 | + |
| 99 | + jerry_value_t internal_prop_name = jerry_create_string ((const jerry_char_t *) "hidden_foo"); |
| 100 | + jerry_value_t internal_prop_object = jerry_create_object (); |
| 101 | + bool internal_result = jerry_set_internal_property (object, internal_prop_name, internal_prop_object); |
| 102 | + TEST_ASSERT (internal_result == true); |
| 103 | + jerry_release_value (internal_prop_name); |
| 104 | + jerry_release_value (internal_prop_object); |
| 105 | + |
| 106 | + /* After adding an internal property object, the number of object is incremented by one. */ |
| 107 | + { |
| 108 | + int after_internal_count = 0; |
| 109 | + jerry_objects_foreach (count_objects, &after_internal_count); |
| 110 | + |
| 111 | + TEST_ASSERT (after_internal_count > after_object_count); |
| 112 | + TEST_ASSERT ((after_internal_count - after_object_count) == 1); |
| 113 | + } |
| 114 | + |
| 115 | + jerry_release_value (object); |
| 116 | +} /* test_internal_prop */ |
| 117 | + |
19 | 118 | static int test_data = 1;
|
20 | 119 |
|
21 | 120 | static void free_test_data (void *data_p)
|
@@ -133,5 +232,11 @@ main (void)
|
133 | 232 | jerry_release_value (property_name);
|
134 | 233 | jerry_release_value (undefined);
|
135 | 234 | jerry_release_value (strict_equal);
|
| 235 | + |
| 236 | + test_container (); |
| 237 | + test_internal_prop (); |
| 238 | + |
136 | 239 | jerry_cleanup ();
|
| 240 | + |
| 241 | + return 0; |
137 | 242 | } /* main */
|
0 commit comments