1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation 2425bb815Sopenharmony_ci * 3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License. 5425bb815Sopenharmony_ci * You may obtain a copy of the License at 6425bb815Sopenharmony_ci * 7425bb815Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8425bb815Sopenharmony_ci * 9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS 11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and 13425bb815Sopenharmony_ci * limitations under the License. 14425bb815Sopenharmony_ci */ 15425bb815Sopenharmony_ci 16425bb815Sopenharmony_ci#include "jerryscript.h" 17425bb815Sopenharmony_ci#include "jerryscript-port.h" 18425bb815Sopenharmony_ci#include "jerryscript-port-default.h" 19425bb815Sopenharmony_ci#include "test-common.h" 20425bb815Sopenharmony_ci#include <gtest/gtest.h> 21425bb815Sopenharmony_ci 22425bb815Sopenharmony_cistatic bool 23425bb815Sopenharmony_cicount_objects (jerry_value_t object, void *user_arg) 24425bb815Sopenharmony_ci{ 25425bb815Sopenharmony_ci (void) object; 26425bb815Sopenharmony_ci TEST_ASSERT (user_arg != NULL); 27425bb815Sopenharmony_ci 28425bb815Sopenharmony_ci int *counter = (int *) user_arg; 29425bb815Sopenharmony_ci 30425bb815Sopenharmony_ci (*counter)++; 31425bb815Sopenharmony_ci return true; 32425bb815Sopenharmony_ci} /* count_objects */ 33425bb815Sopenharmony_ci 34425bb815Sopenharmony_cistatic void 35425bb815Sopenharmony_citest_container (void) 36425bb815Sopenharmony_ci{ 37425bb815Sopenharmony_ci jerry_value_t global = jerry_get_global_object (); 38425bb815Sopenharmony_ci jerry_value_t map_str = jerry_create_string ((const jerry_char_t *) "Map"); 39425bb815Sopenharmony_ci jerry_value_t map_result = jerry_get_property (global, map_str); 40425bb815Sopenharmony_ci jerry_type_t type = jerry_value_get_type (map_result); 41425bb815Sopenharmony_ci 42425bb815Sopenharmony_ci jerry_release_value (map_result); 43425bb815Sopenharmony_ci jerry_release_value (map_str); 44425bb815Sopenharmony_ci jerry_release_value (global); 45425bb815Sopenharmony_ci 46425bb815Sopenharmony_ci /* If there is no Map function this is not an es2015 profile build, skip this test case. */ 47425bb815Sopenharmony_ci if (type != JERRY_TYPE_FUNCTION) 48425bb815Sopenharmony_ci { 49425bb815Sopenharmony_ci jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Container based test is disabled!\n"); 50425bb815Sopenharmony_ci return; 51425bb815Sopenharmony_ci } 52425bb815Sopenharmony_ci 53425bb815Sopenharmony_ci { 54425bb815Sopenharmony_ci /* Create a "DEMO" array which will be used for the Map below. */ 55425bb815Sopenharmony_ci const char array_str[] = "var DEMO = [[1, 2], [3, 4]]; DEMO"; 56425bb815Sopenharmony_ci jerry_value_t array = jerry_eval ((const jerry_char_t *) array_str, sizeof (array_str) - 1, 0); 57425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (array)); 58425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (array)); 59425bb815Sopenharmony_ci jerry_release_value (array); 60425bb815Sopenharmony_ci } 61425bb815Sopenharmony_ci 62425bb815Sopenharmony_ci const char eval_str[] = "new Map (DEMO)"; 63425bb815Sopenharmony_ci { 64425bb815Sopenharmony_ci /* Make sure that the Map and it's prototype object/function is initialized. */ 65425bb815Sopenharmony_ci jerry_value_t result = jerry_eval ((const jerry_char_t *) eval_str, sizeof (eval_str) - 1, 0); 66425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (result)); 67425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (result)); 68425bb815Sopenharmony_ci jerry_release_value (result); 69425bb815Sopenharmony_ci } 70425bb815Sopenharmony_ci 71425bb815Sopenharmony_ci /* Do a bit of cleaning to clear up old objects. */ 72425bb815Sopenharmony_ci jerry_gc (JERRY_GC_PRESSURE_LOW); 73425bb815Sopenharmony_ci 74425bb815Sopenharmony_ci /* Get the number of iterable objects. */ 75425bb815Sopenharmony_ci int start_count = 0; 76425bb815Sopenharmony_ci jerry_objects_foreach (count_objects, &start_count); 77425bb815Sopenharmony_ci 78425bb815Sopenharmony_ci /* Create another map. */ 79425bb815Sopenharmony_ci jerry_value_t result = jerry_eval ((const jerry_char_t *) eval_str, sizeof (eval_str) - 1, 0); 80425bb815Sopenharmony_ci 81425bb815Sopenharmony_ci /* Remove any old/unused objects. */ 82425bb815Sopenharmony_ci jerry_gc (JERRY_GC_PRESSURE_LOW); 83425bb815Sopenharmony_ci 84425bb815Sopenharmony_ci /* Get the current number of objects. */ 85425bb815Sopenharmony_ci int end_count = 0; 86425bb815Sopenharmony_ci jerry_objects_foreach (count_objects, &end_count); 87425bb815Sopenharmony_ci 88425bb815Sopenharmony_ci /* As only one Map was created the number of available iterable objects should be incremented only by one. */ 89425bb815Sopenharmony_ci ASSERT_TRUE (!(end_count > start_count)); 90425bb815Sopenharmony_ci ASSERT_TRUE (!((end_count - start_count) == 1)); 91425bb815Sopenharmony_ci 92425bb815Sopenharmony_ci jerry_release_value (result); 93425bb815Sopenharmony_ci} /* test_container */ 94425bb815Sopenharmony_ci 95425bb815Sopenharmony_cistatic void 96425bb815Sopenharmony_citest_internal_prop (void) 97425bb815Sopenharmony_ci{ 98425bb815Sopenharmony_ci /* Make sure that the object is initialized in the engine. */ 99425bb815Sopenharmony_ci { 100425bb815Sopenharmony_ci jerry_value_t object = jerry_create_object (); 101425bb815Sopenharmony_ci jerry_release_value (object); 102425bb815Sopenharmony_ci } 103425bb815Sopenharmony_ci 104425bb815Sopenharmony_ci /* Get the number of iterable objects. */ 105425bb815Sopenharmony_ci int before_object_count = 0; 106425bb815Sopenharmony_ci jerry_objects_foreach (count_objects, &before_object_count); 107425bb815Sopenharmony_ci 108425bb815Sopenharmony_ci jerry_value_t object = jerry_create_object (); 109425bb815Sopenharmony_ci 110425bb815Sopenharmony_ci /* After creating the object, the number of objects is incremented by one. */ 111425bb815Sopenharmony_ci int after_object_count = 0; 112425bb815Sopenharmony_ci { 113425bb815Sopenharmony_ci jerry_objects_foreach (count_objects, &after_object_count); 114425bb815Sopenharmony_ci 115425bb815Sopenharmony_ci TEST_ASSERT (after_object_count > before_object_count); 116425bb815Sopenharmony_ci TEST_ASSERT ((after_object_count - before_object_count) == 1); 117425bb815Sopenharmony_ci } 118425bb815Sopenharmony_ci 119425bb815Sopenharmony_ci jerry_value_t internal_prop_name = jerry_create_string ((const jerry_char_t *) "hidden_foo"); 120425bb815Sopenharmony_ci jerry_value_t internal_prop_object = jerry_create_object (); 121425bb815Sopenharmony_ci bool internal_result = jerry_set_internal_property (object, internal_prop_name, internal_prop_object); 122425bb815Sopenharmony_ci TEST_ASSERT (internal_result == true); 123425bb815Sopenharmony_ci jerry_release_value (internal_prop_name); 124425bb815Sopenharmony_ci jerry_release_value (internal_prop_object); 125425bb815Sopenharmony_ci 126425bb815Sopenharmony_ci /* After adding an internal property object, the number of object is incremented by one. */ 127425bb815Sopenharmony_ci { 128425bb815Sopenharmony_ci int after_internal_count = 0; 129425bb815Sopenharmony_ci jerry_objects_foreach (count_objects, &after_internal_count); 130425bb815Sopenharmony_ci 131425bb815Sopenharmony_ci TEST_ASSERT (after_internal_count > after_object_count); 132425bb815Sopenharmony_ci TEST_ASSERT ((after_internal_count - after_object_count) == 1); 133425bb815Sopenharmony_ci } 134425bb815Sopenharmony_ci 135425bb815Sopenharmony_ci jerry_release_value (object); 136425bb815Sopenharmony_ci} /* test_internal_prop */ 137425bb815Sopenharmony_ci 138425bb815Sopenharmony_cistatic int test_data = 1; 139425bb815Sopenharmony_ci 140425bb815Sopenharmony_cistatic void free_test_data (void *data_p) 141425bb815Sopenharmony_ci{ 142425bb815Sopenharmony_ci TEST_ASSERT ((int *) data_p == &test_data); 143425bb815Sopenharmony_ci} /* free_test_data */ 144425bb815Sopenharmony_ci 145425bb815Sopenharmony_cistatic const jerry_object_native_info_t test_info = 146425bb815Sopenharmony_ci{ 147425bb815Sopenharmony_ci .free_cb = free_test_data 148425bb815Sopenharmony_ci}; 149425bb815Sopenharmony_ci 150425bb815Sopenharmony_cistatic const jerry_char_t strict_equal_source[] = "var x = function(a, b) {return a === b;}; x"; 151425bb815Sopenharmony_ci 152425bb815Sopenharmony_cistatic bool 153425bb815Sopenharmony_cifind_test_object_by_data (const jerry_value_t candidate, 154425bb815Sopenharmony_ci void *object_data_p, 155425bb815Sopenharmony_ci void *context_p) 156425bb815Sopenharmony_ci{ 157425bb815Sopenharmony_ci if (object_data_p == &test_data) 158425bb815Sopenharmony_ci { 159425bb815Sopenharmony_ci *((jerry_value_t *) context_p) = jerry_acquire_value (candidate); 160425bb815Sopenharmony_ci return false; 161425bb815Sopenharmony_ci } 162425bb815Sopenharmony_ci return true; 163425bb815Sopenharmony_ci} /* find_test_object_by_data */ 164425bb815Sopenharmony_ci 165425bb815Sopenharmony_cistatic bool 166425bb815Sopenharmony_cifind_test_object_by_property (const jerry_value_t candidate, 167425bb815Sopenharmony_ci void *context_p) 168425bb815Sopenharmony_ci{ 169425bb815Sopenharmony_ci jerry_value_t *args_p = (jerry_value_t *) context_p; 170425bb815Sopenharmony_ci jerry_value_t result = jerry_has_property (candidate, args_p[0]); 171425bb815Sopenharmony_ci 172425bb815Sopenharmony_ci bool has_property = (!jerry_value_is_error (result) && jerry_get_boolean_value (result)); 173425bb815Sopenharmony_ci 174425bb815Sopenharmony_ci /* If the object has the desired property, store a new reference to it in args_p[1]. */ 175425bb815Sopenharmony_ci if (has_property) 176425bb815Sopenharmony_ci { 177425bb815Sopenharmony_ci args_p[1] = jerry_acquire_value (candidate); 178425bb815Sopenharmony_ci } 179425bb815Sopenharmony_ci 180425bb815Sopenharmony_ci jerry_release_value (result); 181425bb815Sopenharmony_ci 182425bb815Sopenharmony_ci /* Stop iterating if we've found our object. */ 183425bb815Sopenharmony_ci return !has_property; 184425bb815Sopenharmony_ci} /* find_test_object_by_property */ 185425bb815Sopenharmony_ciclass ObjectsForeachTest : public testing::Test{ 186425bb815Sopenharmony_cipublic: 187425bb815Sopenharmony_ci static void SetUpTestCase() 188425bb815Sopenharmony_ci { 189425bb815Sopenharmony_ci GTEST_LOG_(INFO) << "ObjectsForeachTest SetUpTestCase"; 190425bb815Sopenharmony_ci } 191425bb815Sopenharmony_ci 192425bb815Sopenharmony_ci static void TearDownTestCase() 193425bb815Sopenharmony_ci { 194425bb815Sopenharmony_ci GTEST_LOG_(INFO) << "ObjectsForeachTest TearDownTestCase"; 195425bb815Sopenharmony_ci } 196425bb815Sopenharmony_ci 197425bb815Sopenharmony_ci void SetUp() override {} 198425bb815Sopenharmony_ci void TearDown() override {} 199425bb815Sopenharmony_ci 200425bb815Sopenharmony_ci}; 201425bb815Sopenharmony_cistatic constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024; 202425bb815Sopenharmony_cistatic void* context_alloc_fn(size_t size, void* cb_data) 203425bb815Sopenharmony_ci{ 204425bb815Sopenharmony_ci (void)cb_data; 205425bb815Sopenharmony_ci size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size; 206425bb815Sopenharmony_ci return malloc(newSize); 207425bb815Sopenharmony_ci} 208425bb815Sopenharmony_ciHWTEST_F(ObjectsForeachTest, Test001, testing::ext::TestSize.Level1) 209425bb815Sopenharmony_ci{ 210425bb815Sopenharmony_ci jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL); 211425bb815Sopenharmony_ci jerry_port_default_set_current_context (ctx_p); 212425bb815Sopenharmony_ci jerry_init (JERRY_INIT_EMPTY); 213425bb815Sopenharmony_ci 214425bb815Sopenharmony_ci /* Render strict-equal as a function. */ 215425bb815Sopenharmony_ci jerry_value_t parse_result = jerry_parse (NULL, 216425bb815Sopenharmony_ci 0, 217425bb815Sopenharmony_ci strict_equal_source, 218425bb815Sopenharmony_ci sizeof (strict_equal_source) - 1, 219425bb815Sopenharmony_ci JERRY_PARSE_STRICT_MODE); 220425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (parse_result)); 221425bb815Sopenharmony_ci jerry_value_t strict_equal = jerry_run (parse_result); 222425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (strict_equal)); 223425bb815Sopenharmony_ci jerry_release_value (parse_result); 224425bb815Sopenharmony_ci 225425bb815Sopenharmony_ci /* Create an object and associate some native data with it. */ 226425bb815Sopenharmony_ci jerry_value_t object = jerry_create_object (); 227425bb815Sopenharmony_ci jerry_set_object_native_pointer (object, &test_data, &test_info); 228425bb815Sopenharmony_ci 229425bb815Sopenharmony_ci /* Retrieve the object by its native pointer. */ 230425bb815Sopenharmony_ci 231425bb815Sopenharmony_ci jerry_value_t found_object; 232425bb815Sopenharmony_ci TEST_ASSERT (jerry_objects_foreach_by_native_info (&test_info, find_test_object_by_data, &found_object)); 233425bb815Sopenharmony_ci jerry_value_t args[2] = {object, found_object}; 234425bb815Sopenharmony_ci 235425bb815Sopenharmony_ci /* Assert that the correct object was retrieved. */ 236425bb815Sopenharmony_ci jerry_value_t undefined = jerry_create_undefined (); 237425bb815Sopenharmony_ci jerry_value_t strict_equal_result = jerry_call_function (strict_equal, undefined, args, 2); 238425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_boolean (strict_equal_result) && jerry_get_boolean_value (strict_equal_result)); 239425bb815Sopenharmony_ci jerry_release_value (strict_equal_result); 240425bb815Sopenharmony_ci jerry_release_value (found_object); 241425bb815Sopenharmony_ci jerry_release_value (object); 242425bb815Sopenharmony_ci 243425bb815Sopenharmony_ci /* Collect garbage. */ 244425bb815Sopenharmony_ci jerry_gc (JERRY_GC_PRESSURE_LOW); 245425bb815Sopenharmony_ci 246425bb815Sopenharmony_ci /* Attempt to retrieve the object by its native pointer again. */ 247425bb815Sopenharmony_ci TEST_ASSERT (!jerry_objects_foreach_by_native_info (&test_info, find_test_object_by_data, &found_object)); 248425bb815Sopenharmony_ci 249425bb815Sopenharmony_ci /* Create an object and set a property on it. */ 250425bb815Sopenharmony_ci object = jerry_create_object (); 251425bb815Sopenharmony_ci jerry_value_t property_name = jerry_create_string ((jerry_char_t *) "xyzzy"); 252425bb815Sopenharmony_ci jerry_value_t property_value = jerry_create_number (42); 253425bb815Sopenharmony_ci jerry_release_value (jerry_set_property (object, property_name, property_value)); 254425bb815Sopenharmony_ci jerry_release_value (property_value); 255425bb815Sopenharmony_ci 256425bb815Sopenharmony_ci /* Retrieve the object by the presence of its property, placing it at args[1]. */ 257425bb815Sopenharmony_ci args[0] = property_name; 258425bb815Sopenharmony_ci TEST_ASSERT (jerry_objects_foreach (find_test_object_by_property, args)); 259425bb815Sopenharmony_ci 260425bb815Sopenharmony_ci /* Assert that the right object was retrieved and release both the original reference to it and the retrieved one. */ 261425bb815Sopenharmony_ci args[0] = object; 262425bb815Sopenharmony_ci strict_equal_result = jerry_call_function (strict_equal, undefined, args, 2); 263425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_boolean (strict_equal_result) && jerry_get_boolean_value (strict_equal_result)); 264425bb815Sopenharmony_ci jerry_release_value (strict_equal_result); 265425bb815Sopenharmony_ci jerry_release_value (args[0]); 266425bb815Sopenharmony_ci jerry_release_value (args[1]); 267425bb815Sopenharmony_ci 268425bb815Sopenharmony_ci /* Collect garbage. */ 269425bb815Sopenharmony_ci jerry_gc (JERRY_GC_PRESSURE_LOW); 270425bb815Sopenharmony_ci 271425bb815Sopenharmony_ci /* Attempt to retrieve the object by the presence of its property again. */ 272425bb815Sopenharmony_ci args[0] = property_name; 273425bb815Sopenharmony_ci TEST_ASSERT (!jerry_objects_foreach (find_test_object_by_property, args)); 274425bb815Sopenharmony_ci 275425bb815Sopenharmony_ci jerry_release_value (property_name); 276425bb815Sopenharmony_ci jerry_release_value (undefined); 277425bb815Sopenharmony_ci jerry_release_value (strict_equal); 278425bb815Sopenharmony_ci 279425bb815Sopenharmony_ci test_container (); 280425bb815Sopenharmony_ci test_internal_prop (); 281425bb815Sopenharmony_ci 282425bb815Sopenharmony_ci jerry_cleanup (); 283425bb815Sopenharmony_ci free (ctx_p); 284425bb815Sopenharmony_ci} 285