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 "config.h" 17425bb815Sopenharmony_ci#include "jerryscript.h" 18425bb815Sopenharmony_ci#include "jerryscript-port.h" 19425bb815Sopenharmony_ci#include "jerryscript-port-default.h" 20425bb815Sopenharmony_ci#include "test-common.h" 21425bb815Sopenharmony_ci#include <gtest/gtest.h> 22425bb815Sopenharmony_ci 23425bb815Sopenharmony_ci/** Test in Proxy on C side. Equivalent test code in JS: 24425bb815Sopenharmony_ci 25425bb815Sopenharmony_civar demo = 0.0; 26425bb815Sopenharmony_ci 27425bb815Sopenharmony_civar target = {}; 28425bb815Sopenharmony_civar handler = { 29425bb815Sopenharmony_ci get: function (target, name, recv) { 30425bb815Sopenharmony_ci assert (typeof (target) === 'object'); 31425bb815Sopenharmony_ci assert (name === 'value'); 32425bb815Sopenharmony_ci assert (typeof (recv) === 'object'); 33425bb815Sopenharmony_ci return demo++; 34425bb815Sopenharmony_ci } 35425bb815Sopenharmony_ci 36425bb815Sopenharmony_ci set: function (target, name, value, recv) { 37425bb815Sopenharmony_ci assert (typeof (target) === 'object'); 38425bb815Sopenharmony_ci assert (name === 'value'); 39425bb815Sopenharmony_ci assert (typeof (recv) === 'object'); 40425bb815Sopenharmony_ci demo = 55; 41425bb815Sopenharmony_ci return demo; 42425bb815Sopenharmony_ci } 43425bb815Sopenharmony_ci}; 44425bb815Sopenharmony_ci 45425bb815Sopenharmony_civar pdemo = new Proxy(target, handler); 46425bb815Sopenharmony_ci 47425bb815Sopenharmony_ciassert (pdemo.value === 1.0); 48425bb815Sopenharmony_ciassert (pdemo.value === 1.0); 49425bb815Sopenharmony_ciassert (pdemo.value === 2.0); 50425bb815Sopenharmony_ci 51425bb815Sopenharmony_cipdemo.value = 55; 52425bb815Sopenharmony_ci 53425bb815Sopenharmony_ciassert (pdemo.value === 56); 54425bb815Sopenharmony_ci 55425bb815Sopenharmony_cipdemo.value = 12; 56425bb815Sopenharmony_ci 57425bb815Sopenharmony_ciassert (pdemo.value === 13); 58425bb815Sopenharmony_ci */ 59425bb815Sopenharmony_ci 60425bb815Sopenharmony_cistatic int demo_value = 0; 61425bb815Sopenharmony_ci 62425bb815Sopenharmony_cistatic jerry_value_t 63425bb815Sopenharmony_cihandler_get (const jerry_value_t function_obj, /**< function object */ 64425bb815Sopenharmony_ci const jerry_value_t this_val, /**< this arg */ 65425bb815Sopenharmony_ci const jerry_value_t args_p[], /**< function arguments */ 66425bb815Sopenharmony_ci const jerry_length_t args_count) /**< number of function arguments */ 67425bb815Sopenharmony_ci{ 68425bb815Sopenharmony_ci JERRY_UNUSED (function_obj); 69425bb815Sopenharmony_ci JERRY_UNUSED (this_val); 70425bb815Sopenharmony_ci 71425bb815Sopenharmony_ci TEST_ASSERT (args_count == 3); 72425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (args_p[0])); /* target */ 73425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_string (args_p[1])); /* P */ 74425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (args_p[2])); /* receiver */ 75425bb815Sopenharmony_ci 76425bb815Sopenharmony_ci const char expected[] = "value"; 77425bb815Sopenharmony_ci char buffer[10]; 78425bb815Sopenharmony_ci jerry_size_t copied = jerry_string_to_char_buffer (args_p[1], (jerry_char_t *) buffer, 10); 79425bb815Sopenharmony_ci 80425bb815Sopenharmony_ci TEST_ASSERT (copied == 5); 81425bb815Sopenharmony_ci TEST_ASSERT (strncmp (expected, buffer, 5) == 0); 82425bb815Sopenharmony_ci 83425bb815Sopenharmony_ci demo_value++; 84425bb815Sopenharmony_ci 85425bb815Sopenharmony_ci return jerry_create_number (demo_value); 86425bb815Sopenharmony_ci} /* handler_get */ 87425bb815Sopenharmony_ci 88425bb815Sopenharmony_cistatic jerry_value_t 89425bb815Sopenharmony_cihandler_set (const jerry_value_t function_obj, /**< function object */ 90425bb815Sopenharmony_ci const jerry_value_t this_val, /**< this arg */ 91425bb815Sopenharmony_ci const jerry_value_t args_p[], /**< function arguments */ 92425bb815Sopenharmony_ci const jerry_length_t args_count) /**< number of function arguments */ 93425bb815Sopenharmony_ci{ 94425bb815Sopenharmony_ci JERRY_UNUSED (function_obj); 95425bb815Sopenharmony_ci JERRY_UNUSED (this_val); 96425bb815Sopenharmony_ci JERRY_UNUSED (args_p); 97425bb815Sopenharmony_ci JERRY_UNUSED (args_count); 98425bb815Sopenharmony_ci 99425bb815Sopenharmony_ci TEST_ASSERT (args_count == 4); 100425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (args_p[0])); /* target */ 101425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_string (args_p[1])); /* P */ 102425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_number (args_p[2])); /* V */ 103425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (args_p[3])); /* receiver */ 104425bb815Sopenharmony_ci 105425bb815Sopenharmony_ci const char expected[] = "value"; 106425bb815Sopenharmony_ci char buffer[10]; 107425bb815Sopenharmony_ci jerry_size_t copied = jerry_string_to_char_buffer (args_p[1], (jerry_char_t *) buffer, 10); 108425bb815Sopenharmony_ci 109425bb815Sopenharmony_ci TEST_ASSERT (copied == 5); 110425bb815Sopenharmony_ci TEST_ASSERT (strncmp (expected, buffer, 5) == 0); 111425bb815Sopenharmony_ci 112425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_number (args_p[2])); 113425bb815Sopenharmony_ci demo_value = (int) jerry_get_number_value (args_p[2]); 114425bb815Sopenharmony_ci 115425bb815Sopenharmony_ci return jerry_create_number (demo_value); 116425bb815Sopenharmony_ci} /* handler_set */ 117425bb815Sopenharmony_ci 118425bb815Sopenharmony_cistatic void 119425bb815Sopenharmony_ciset_property (jerry_value_t target, /**< target object */ 120425bb815Sopenharmony_ci const char *name_p, /**< name of the property */ 121425bb815Sopenharmony_ci jerry_value_t value) /**< value of the property */ 122425bb815Sopenharmony_ci{ 123425bb815Sopenharmony_ci jerry_value_t name_val = jerry_create_string ((const jerry_char_t *) name_p); 124425bb815Sopenharmony_ci jerry_value_t result_val = jerry_set_property (target, name_val, value); 125425bb815Sopenharmony_ci 126425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_boolean (result_val)); 127425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_boolean_value (result_val)); 128425bb815Sopenharmony_ci jerry_release_value (name_val); 129425bb815Sopenharmony_ci} /* set_property */ 130425bb815Sopenharmony_ci 131425bb815Sopenharmony_cistatic jerry_value_t 132425bb815Sopenharmony_ciget_property (jerry_value_t target, /**< target object */ 133425bb815Sopenharmony_ci const char *name_p) /**< name of the property */ 134425bb815Sopenharmony_ci{ 135425bb815Sopenharmony_ci jerry_value_t name_val = jerry_create_string ((const jerry_char_t *) name_p); 136425bb815Sopenharmony_ci jerry_value_t result_val = jerry_get_property (target, name_val); 137425bb815Sopenharmony_ci 138425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (result_val)); 139425bb815Sopenharmony_ci jerry_release_value (name_val); 140425bb815Sopenharmony_ci return result_val; 141425bb815Sopenharmony_ci} /* get_property */ 142425bb815Sopenharmony_ci 143425bb815Sopenharmony_cistatic void 144425bb815Sopenharmony_ciset_function (jerry_value_t target, /**< target object */ 145425bb815Sopenharmony_ci const char *name_p, /**< name of the function */ 146425bb815Sopenharmony_ci jerry_external_handler_t handler_p) /**< function callback */ 147425bb815Sopenharmony_ci{ 148425bb815Sopenharmony_ci jerry_value_t function_val = jerry_create_external_function (handler_p); 149425bb815Sopenharmony_ci set_property (target, name_p, function_val); 150425bb815Sopenharmony_ci jerry_release_value (function_val); 151425bb815Sopenharmony_ci} /* set_function */ 152425bb815Sopenharmony_ci 153425bb815Sopenharmony_ciclass ProxyTest : public testing::Test{ 154425bb815Sopenharmony_cipublic: 155425bb815Sopenharmony_ci static void SetUpTestCase() 156425bb815Sopenharmony_ci { 157425bb815Sopenharmony_ci GTEST_LOG_(INFO) << "ProxyTest SetUpTestCase"; 158425bb815Sopenharmony_ci } 159425bb815Sopenharmony_ci 160425bb815Sopenharmony_ci static void TearDownTestCase() 161425bb815Sopenharmony_ci { 162425bb815Sopenharmony_ci GTEST_LOG_(INFO) << "ProxyTest TearDownTestCase"; 163425bb815Sopenharmony_ci } 164425bb815Sopenharmony_ci 165425bb815Sopenharmony_ci void SetUp() override {} 166425bb815Sopenharmony_ci void TearDown() override {} 167425bb815Sopenharmony_ci 168425bb815Sopenharmony_ci}; 169425bb815Sopenharmony_cistatic constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024; 170425bb815Sopenharmony_cistatic void* context_alloc_fn(size_t size, void* cb_data) 171425bb815Sopenharmony_ci{ 172425bb815Sopenharmony_ci (void)cb_data; 173425bb815Sopenharmony_ci size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size; 174425bb815Sopenharmony_ci return malloc(newSize); 175425bb815Sopenharmony_ci} 176425bb815Sopenharmony_ciHWTEST_F(ProxyTest, Test001, testing::ext::TestSize.Level1) 177425bb815Sopenharmony_ci{ 178425bb815Sopenharmony_ci TEST_INIT (); 179425bb815Sopenharmony_ci 180425bb815Sopenharmony_ci if (!jerry_is_feature_enabled (JERRY_FEATURE_PROXY)) 181425bb815Sopenharmony_ci { 182425bb815Sopenharmony_ci printf ("Skipping test, Proxy not enabled\n"); 183425bb815Sopenharmony_ci } 184425bb815Sopenharmony_ci else{ 185425bb815Sopenharmony_ci jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL); 186425bb815Sopenharmony_ci jerry_port_default_set_current_context (ctx_p); 187425bb815Sopenharmony_ci 188425bb815Sopenharmony_ci jerry_init (JERRY_INIT_EMPTY); 189425bb815Sopenharmony_ci 190425bb815Sopenharmony_ci jerry_value_t handler = jerry_create_object (); 191425bb815Sopenharmony_ci { 192425bb815Sopenharmony_ci set_function (handler, "get", handler_get); 193425bb815Sopenharmony_ci set_function (handler, "set", handler_set); 194425bb815Sopenharmony_ci } 195425bb815Sopenharmony_ci 196425bb815Sopenharmony_ci jerry_value_t target = jerry_create_object (); 197425bb815Sopenharmony_ci jerry_value_t proxy = jerry_create_proxy (target, handler); 198425bb815Sopenharmony_ci { 199425bb815Sopenharmony_ci jerry_value_t global = jerry_get_global_object (); 200425bb815Sopenharmony_ci set_property (global, "pdemo", proxy); 201425bb815Sopenharmony_ci jerry_release_value (global); 202425bb815Sopenharmony_ci } 203425bb815Sopenharmony_ci 204425bb815Sopenharmony_ci const jerry_char_t get_value_src[] = TEST_STRING_LITERAL ("pdemo.value"); 205425bb815Sopenharmony_ci jerry_value_t parsed_get_code_val = jerry_parse (NULL, 206425bb815Sopenharmony_ci 0, 207425bb815Sopenharmony_ci get_value_src, 208425bb815Sopenharmony_ci sizeof (get_value_src) - 1, 209425bb815Sopenharmony_ci JERRY_PARSE_NO_OPTS); 210425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (parsed_get_code_val)); 211425bb815Sopenharmony_ci 212425bb815Sopenharmony_ci { 213425bb815Sopenharmony_ci jerry_value_t res = jerry_run (parsed_get_code_val); 214425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_number (res)); 215425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_number_value (res) == 1.0); 216425bb815Sopenharmony_ci jerry_release_value (res); 217425bb815Sopenharmony_ci } 218425bb815Sopenharmony_ci 219425bb815Sopenharmony_ci { 220425bb815Sopenharmony_ci jerry_value_t res = get_property (proxy, "value"); 221425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_number (res)); 222425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_number_value (res) == 2.0); 223425bb815Sopenharmony_ci jerry_release_value (res); 224425bb815Sopenharmony_ci } 225425bb815Sopenharmony_ci 226425bb815Sopenharmony_ci { 227425bb815Sopenharmony_ci jerry_value_t res = jerry_run (parsed_get_code_val); 228425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_number (res)); 229425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_number_value (res) == 3.0); 230425bb815Sopenharmony_ci jerry_release_value (res); 231425bb815Sopenharmony_ci } 232425bb815Sopenharmony_ci 233425bb815Sopenharmony_ci const jerry_char_t set_value_src[] = TEST_STRING_LITERAL ("pdemo.value = 55"); 234425bb815Sopenharmony_ci jerry_value_t parsed_set_code_val = jerry_parse (NULL, 235425bb815Sopenharmony_ci 0, 236425bb815Sopenharmony_ci set_value_src, 237425bb815Sopenharmony_ci sizeof (set_value_src) - 1, 238425bb815Sopenharmony_ci JERRY_PARSE_NO_OPTS); 239425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (parsed_set_code_val)); 240425bb815Sopenharmony_ci 241425bb815Sopenharmony_ci { 242425bb815Sopenharmony_ci jerry_value_t res = jerry_run (parsed_set_code_val); 243425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_number (res)); 244425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_number_value (res) == 55); 245425bb815Sopenharmony_ci jerry_release_value (res); 246425bb815Sopenharmony_ci } 247425bb815Sopenharmony_ci 248425bb815Sopenharmony_ci { 249425bb815Sopenharmony_ci jerry_value_t res = jerry_run (parsed_get_code_val); 250425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_number (res)); 251425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_number_value (res) == 56); 252425bb815Sopenharmony_ci jerry_release_value (res); 253425bb815Sopenharmony_ci } 254425bb815Sopenharmony_ci 255425bb815Sopenharmony_ci { 256425bb815Sopenharmony_ci jerry_value_t new_value = jerry_create_number (12); 257425bb815Sopenharmony_ci set_property (proxy, "value", new_value); 258425bb815Sopenharmony_ci jerry_release_value (new_value); 259425bb815Sopenharmony_ci } 260425bb815Sopenharmony_ci 261425bb815Sopenharmony_ci { 262425bb815Sopenharmony_ci jerry_value_t res = get_property (proxy, "value"); 263425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_number (res)); 264425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_number_value (res) == 13.0); 265425bb815Sopenharmony_ci jerry_release_value (res); 266425bb815Sopenharmony_ci } 267425bb815Sopenharmony_ci 268425bb815Sopenharmony_ci jerry_release_value (parsed_set_code_val); 269425bb815Sopenharmony_ci jerry_release_value (parsed_get_code_val); 270425bb815Sopenharmony_ci jerry_release_value (proxy); 271425bb815Sopenharmony_ci jerry_release_value (target); 272425bb815Sopenharmony_ci jerry_release_value (handler); 273425bb815Sopenharmony_ci 274425bb815Sopenharmony_ci jerry_cleanup (); 275425bb815Sopenharmony_ci free (ctx_p); 276425bb815Sopenharmony_ci } 277425bb815Sopenharmony_ci} 278