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 <gtest/gtest.h> 21425bb815Sopenharmony_ci#include "test-common.h" 22425bb815Sopenharmony_ci 23425bb815Sopenharmony_ciconst jerry_char_t test_source[] = TEST_STRING_LITERAL ( 24425bb815Sopenharmony_ci "function assert (arg) { " 25425bb815Sopenharmony_ci " if (!arg) { " 26425bb815Sopenharmony_ci " throw Error('Assert failed');" 27425bb815Sopenharmony_ci " } " 28425bb815Sopenharmony_ci "} " 29425bb815Sopenharmony_ci "this.t = 1; " 30425bb815Sopenharmony_ci "function f () { " 31425bb815Sopenharmony_ci "return this.t; " 32425bb815Sopenharmony_ci "} " 33425bb815Sopenharmony_ci "this.foo = f; " 34425bb815Sopenharmony_ci "this.bar = function (a) { " 35425bb815Sopenharmony_ci "return a + t; " 36425bb815Sopenharmony_ci "}; " 37425bb815Sopenharmony_ci "function A () { " 38425bb815Sopenharmony_ci "this.t = 12; " 39425bb815Sopenharmony_ci "} " 40425bb815Sopenharmony_ci "this.A = A; " 41425bb815Sopenharmony_ci "this.a = new A (); " 42425bb815Sopenharmony_ci "function call_external () { " 43425bb815Sopenharmony_ci " return this.external ('1', true); " 44425bb815Sopenharmony_ci "} " 45425bb815Sopenharmony_ci "function call_throw_test() { " 46425bb815Sopenharmony_ci " var catched = false; " 47425bb815Sopenharmony_ci " try { " 48425bb815Sopenharmony_ci " this.throw_test(); " 49425bb815Sopenharmony_ci " } catch (e) { " 50425bb815Sopenharmony_ci " catched = true; " 51425bb815Sopenharmony_ci " assert(e.name == 'TypeError'); " 52425bb815Sopenharmony_ci " assert(e.message == 'error'); " 53425bb815Sopenharmony_ci " } " 54425bb815Sopenharmony_ci " assert(catched); " 55425bb815Sopenharmony_ci "} " 56425bb815Sopenharmony_ci "function throw_reference_error() { " 57425bb815Sopenharmony_ci " throw new ReferenceError ();" 58425bb815Sopenharmony_ci "} " 59425bb815Sopenharmony_ci "p = {'alpha':32, 'bravo':false, 'charlie':{}, 'delta':123.45, 'echo':'foobar'};" 60425bb815Sopenharmony_ci "np = {}; Object.defineProperty (np, 'foxtrot', { " 61425bb815Sopenharmony_ci "get: function() { throw 'error'; }, enumerable: true }) " 62425bb815Sopenharmony_ci); 63425bb815Sopenharmony_ci 64425bb815Sopenharmony_cibool test_api_is_free_callback_was_called = false; 65425bb815Sopenharmony_ci 66425bb815Sopenharmony_cistatic jerry_value_t 67425bb815Sopenharmony_cihandler (const jerry_value_t func_obj_val, /**< function object */ 68425bb815Sopenharmony_ci const jerry_value_t this_val, /**< this value */ 69425bb815Sopenharmony_ci const jerry_value_t args_p[], /**< arguments list */ 70425bb815Sopenharmony_ci const jerry_length_t args_cnt) /**< arguments length */ 71425bb815Sopenharmony_ci{ 72425bb815Sopenharmony_ci char buffer[32]; 73425bb815Sopenharmony_ci jerry_size_t sz; 74425bb815Sopenharmony_ci 75425bb815Sopenharmony_ci printf ("ok %u %u %p %u\n", 76425bb815Sopenharmony_ci (unsigned int) func_obj_val, (unsigned int) this_val, (void *) args_p, (unsigned int) args_cnt); 77425bb815Sopenharmony_ci 78425bb815Sopenharmony_ci TEST_ASSERT (args_cnt == 2); 79425bb815Sopenharmony_ci 80425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_string (args_p[0])); 81425bb815Sopenharmony_ci sz = jerry_get_string_size (args_p[0]); 82425bb815Sopenharmony_ci TEST_ASSERT (sz == 1); 83425bb815Sopenharmony_ci sz = jerry_string_to_char_buffer (args_p[0], 84425bb815Sopenharmony_ci (jerry_char_t *) buffer, 85425bb815Sopenharmony_ci sz); 86425bb815Sopenharmony_ci TEST_ASSERT (sz == 1); 87425bb815Sopenharmony_ci TEST_ASSERT (!strncmp (buffer, "1", (size_t) sz)); 88425bb815Sopenharmony_ci 89425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_boolean (args_p[1])); 90425bb815Sopenharmony_ci 91425bb815Sopenharmony_ci return jerry_create_string ((jerry_char_t *) "string from handler"); 92425bb815Sopenharmony_ci} /* handler */ 93425bb815Sopenharmony_ci 94425bb815Sopenharmony_cistatic jerry_value_t 95425bb815Sopenharmony_cihandler_throw_test (const jerry_value_t func_obj_val, /**< function object */ 96425bb815Sopenharmony_ci const jerry_value_t this_val, /**< this value */ 97425bb815Sopenharmony_ci const jerry_value_t args_p[], /**< arguments list */ 98425bb815Sopenharmony_ci const jerry_length_t args_cnt) /**< arguments length */ 99425bb815Sopenharmony_ci{ 100425bb815Sopenharmony_ci printf ("ok %u %u %p %u\n", 101425bb815Sopenharmony_ci (unsigned int) func_obj_val, (unsigned int) this_val, (void *) args_p, (unsigned int) args_cnt); 102425bb815Sopenharmony_ci 103425bb815Sopenharmony_ci return jerry_create_error (JERRY_ERROR_TYPE, (jerry_char_t *) "error"); 104425bb815Sopenharmony_ci} /* handler_throw_test */ 105425bb815Sopenharmony_ci 106425bb815Sopenharmony_cistatic void 107425bb815Sopenharmony_cihandler_construct_1_freecb (void *native_p) 108425bb815Sopenharmony_ci{ 109425bb815Sopenharmony_ci TEST_ASSERT ((uintptr_t) native_p == (uintptr_t) 0x0000000000000000ull); 110425bb815Sopenharmony_ci printf ("ok object free callback\n"); 111425bb815Sopenharmony_ci 112425bb815Sopenharmony_ci test_api_is_free_callback_was_called = true; 113425bb815Sopenharmony_ci} /* handler_construct_1_freecb */ 114425bb815Sopenharmony_ci 115425bb815Sopenharmony_cistatic void 116425bb815Sopenharmony_cihandler_construct_2_freecb (void *native_p) 117425bb815Sopenharmony_ci{ 118425bb815Sopenharmony_ci TEST_ASSERT ((uintptr_t) native_p == (uintptr_t) 0x0012345678abcdefull); 119425bb815Sopenharmony_ci printf ("ok object free callback\n"); 120425bb815Sopenharmony_ci 121425bb815Sopenharmony_ci test_api_is_free_callback_was_called = true; 122425bb815Sopenharmony_ci} /* handler_construct_2_freecb */ 123425bb815Sopenharmony_ci 124425bb815Sopenharmony_ci/** 125425bb815Sopenharmony_ci * The name of the jerry_object_native_info_t struct. 126425bb815Sopenharmony_ci */ 127425bb815Sopenharmony_ci#define JERRY_NATIVE_HANDLE_INFO_FOR_CTYPE(c_type) _jerry_object_native_info_##c_type 128425bb815Sopenharmony_ci 129425bb815Sopenharmony_ci/** 130425bb815Sopenharmony_ci * Define a native pointer's type based on the C type and free callback. 131425bb815Sopenharmony_ci */ 132425bb815Sopenharmony_ci#define JERRY_DEFINE_NATIVE_HANDLE_INFO(c_type, native_free_cb) \ 133425bb815Sopenharmony_ci static const jerry_object_native_info_t JERRY_NATIVE_HANDLE_INFO_FOR_CTYPE (c_type) = \ 134425bb815Sopenharmony_ci { \ 135425bb815Sopenharmony_ci .free_cb = (jerry_object_native_free_callback_t) native_free_cb \ 136425bb815Sopenharmony_ci } 137425bb815Sopenharmony_ci 138425bb815Sopenharmony_ciJERRY_DEFINE_NATIVE_HANDLE_INFO (bind1, handler_construct_1_freecb); 139425bb815Sopenharmony_ciJERRY_DEFINE_NATIVE_HANDLE_INFO (bind2, handler_construct_2_freecb); 140425bb815Sopenharmony_ciJERRY_DEFINE_NATIVE_HANDLE_INFO (bind3, NULL); 141425bb815Sopenharmony_ci 142425bb815Sopenharmony_cistatic jerry_value_t 143425bb815Sopenharmony_cihandler_construct (const jerry_value_t func_obj_val, /**< function object */ 144425bb815Sopenharmony_ci const jerry_value_t this_val, /**< this value */ 145425bb815Sopenharmony_ci const jerry_value_t args_p[], /**< arguments list */ 146425bb815Sopenharmony_ci const jerry_length_t args_cnt) /**< arguments length */ 147425bb815Sopenharmony_ci{ 148425bb815Sopenharmony_ci printf ("ok construct %u %u %p %u\n", 149425bb815Sopenharmony_ci (unsigned int) func_obj_val, (unsigned int) this_val, (void *) args_p, (unsigned int) args_cnt); 150425bb815Sopenharmony_ci 151425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (this_val)); 152425bb815Sopenharmony_ci 153425bb815Sopenharmony_ci TEST_ASSERT (args_cnt == 1); 154425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_boolean (args_p[0])); 155425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_boolean_value (args_p[0]) == true); 156425bb815Sopenharmony_ci 157425bb815Sopenharmony_ci jerry_value_t field_name = jerry_create_string ((jerry_char_t *) "value_field"); 158425bb815Sopenharmony_ci jerry_value_t res = jerry_set_property (this_val, field_name, args_p[0]); 159425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (res)); 160425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_boolean (res) && jerry_get_boolean_value (res)); 161425bb815Sopenharmony_ci jerry_release_value (res); 162425bb815Sopenharmony_ci jerry_release_value (field_name); 163425bb815Sopenharmony_ci 164425bb815Sopenharmony_ci /* Set a native pointer. */ 165425bb815Sopenharmony_ci jerry_set_object_native_pointer (this_val, 166425bb815Sopenharmony_ci (void *) 0x0000000000000000ull, 167425bb815Sopenharmony_ci &JERRY_NATIVE_HANDLE_INFO_FOR_CTYPE (bind1)); 168425bb815Sopenharmony_ci 169425bb815Sopenharmony_ci /* Check that the native pointer was set. */ 170425bb815Sopenharmony_ci void *ptr = NULL; 171425bb815Sopenharmony_ci bool is_ok = jerry_get_object_native_pointer (this_val, &ptr, &JERRY_NATIVE_HANDLE_INFO_FOR_CTYPE (bind1)); 172425bb815Sopenharmony_ci TEST_ASSERT (is_ok 173425bb815Sopenharmony_ci && (uintptr_t) ptr == (uintptr_t) 0x0000000000000000ull); 174425bb815Sopenharmony_ci 175425bb815Sopenharmony_ci /* Set a second native pointer. */ 176425bb815Sopenharmony_ci jerry_set_object_native_pointer (this_val, 177425bb815Sopenharmony_ci (void *) 0x0012345678abcdefull, 178425bb815Sopenharmony_ci &JERRY_NATIVE_HANDLE_INFO_FOR_CTYPE (bind2)); 179425bb815Sopenharmony_ci 180425bb815Sopenharmony_ci /* Check that a second native pointer was set. */ 181425bb815Sopenharmony_ci is_ok = jerry_get_object_native_pointer (this_val, &ptr, &JERRY_NATIVE_HANDLE_INFO_FOR_CTYPE (bind2)); 182425bb815Sopenharmony_ci TEST_ASSERT (is_ok 183425bb815Sopenharmony_ci && (uintptr_t) ptr == (uintptr_t) 0x0012345678abcdefull); 184425bb815Sopenharmony_ci 185425bb815Sopenharmony_ci /* Check that the first native pointer is still set. */ 186425bb815Sopenharmony_ci is_ok = jerry_get_object_native_pointer (this_val, &ptr, &JERRY_NATIVE_HANDLE_INFO_FOR_CTYPE (bind1)); 187425bb815Sopenharmony_ci TEST_ASSERT (is_ok 188425bb815Sopenharmony_ci && (uintptr_t) ptr == (uintptr_t) 0x0000000000000000ull); 189425bb815Sopenharmony_ci return jerry_create_boolean (true); 190425bb815Sopenharmony_ci} /* handler_construct */ 191425bb815Sopenharmony_ci 192425bb815Sopenharmony_ci/** 193425bb815Sopenharmony_ci * Extended Magic Strings 194425bb815Sopenharmony_ci */ 195425bb815Sopenharmony_ci#define JERRY_MAGIC_STRING_ITEMS \ 196425bb815Sopenharmony_ci JERRY_MAGIC_STRING_DEF (GLOBAL, global) \ 197425bb815Sopenharmony_ci JERRY_MAGIC_STRING_DEF (GREEK_ZERO_SIGN, \xed\xa0\x80\xed\xb6\x8a) \ 198425bb815Sopenharmony_ci JERRY_MAGIC_STRING_DEF (CONSOLE, console) 199425bb815Sopenharmony_ci 200425bb815Sopenharmony_ci#define JERRY_MAGIC_STRING_DEF(NAME, STRING) \ 201425bb815Sopenharmony_ci static const char jerry_magic_string_ex_ ## NAME[] = # STRING; 202425bb815Sopenharmony_ci 203425bb815Sopenharmony_ciJERRY_MAGIC_STRING_ITEMS 204425bb815Sopenharmony_ci 205425bb815Sopenharmony_ci#undef JERRY_MAGIC_STRING_DEF 206425bb815Sopenharmony_ci 207425bb815Sopenharmony_ciconst jerry_length_t magic_string_lengths[] = 208425bb815Sopenharmony_ci{ 209425bb815Sopenharmony_ci#define JERRY_MAGIC_STRING_DEF(NAME, STRING) \ 210425bb815Sopenharmony_ci (jerry_length_t) (sizeof (jerry_magic_string_ex_ ## NAME) - 1u), 211425bb815Sopenharmony_ci 212425bb815Sopenharmony_ci JERRY_MAGIC_STRING_ITEMS 213425bb815Sopenharmony_ci 214425bb815Sopenharmony_ci#undef JERRY_MAGIC_STRING_DEF 215425bb815Sopenharmony_ci}; 216425bb815Sopenharmony_ci 217425bb815Sopenharmony_ciconst jerry_char_t *magic_string_items[] = 218425bb815Sopenharmony_ci{ 219425bb815Sopenharmony_ci#define JERRY_MAGIC_STRING_DEF(NAME, STRING) \ 220425bb815Sopenharmony_ci (const jerry_char_t *) jerry_magic_string_ex_ ## NAME, 221425bb815Sopenharmony_ci 222425bb815Sopenharmony_ci JERRY_MAGIC_STRING_ITEMS 223425bb815Sopenharmony_ci 224425bb815Sopenharmony_ci#undef JERRY_MAGIC_STRING_DEF 225425bb815Sopenharmony_ci}; 226425bb815Sopenharmony_ci 227425bb815Sopenharmony_cistatic bool 228425bb815Sopenharmony_ciforeach (const jerry_value_t name, /**< field name */ 229425bb815Sopenharmony_ci const jerry_value_t value, /**< field value */ 230425bb815Sopenharmony_ci void *user_data) /**< user data */ 231425bb815Sopenharmony_ci{ 232425bb815Sopenharmony_ci char str_buf_p[128]; 233425bb815Sopenharmony_ci jerry_size_t sz = jerry_string_to_char_buffer (name, (jerry_char_t *) str_buf_p, 128); 234425bb815Sopenharmony_ci str_buf_p[sz] = '\0'; 235425bb815Sopenharmony_ci 236425bb815Sopenharmony_ci TEST_ASSERT (!strncmp ((const char *) user_data, "user_data", 9)); 237425bb815Sopenharmony_ci TEST_ASSERT (sz > 0); 238425bb815Sopenharmony_ci 239425bb815Sopenharmony_ci if (!strncmp (str_buf_p, "alpha", (size_t) sz)) 240425bb815Sopenharmony_ci { 241425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_number (value)); 242425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_number_value (value) == 32.0); 243425bb815Sopenharmony_ci return true; 244425bb815Sopenharmony_ci } 245425bb815Sopenharmony_ci else if (!strncmp (str_buf_p, "bravo", (size_t) sz)) 246425bb815Sopenharmony_ci { 247425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_boolean (value)); 248425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_boolean_value (value) == false); 249425bb815Sopenharmony_ci return true; 250425bb815Sopenharmony_ci } 251425bb815Sopenharmony_ci else if (!strncmp (str_buf_p, "charlie", (size_t) sz)) 252425bb815Sopenharmony_ci { 253425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (value)); 254425bb815Sopenharmony_ci return true; 255425bb815Sopenharmony_ci } 256425bb815Sopenharmony_ci else if (!strncmp (str_buf_p, "delta", (size_t) sz)) 257425bb815Sopenharmony_ci { 258425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_number (value)); 259425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_number_value (value) == 123.45); 260425bb815Sopenharmony_ci return true; 261425bb815Sopenharmony_ci } 262425bb815Sopenharmony_ci else if (!strncmp (str_buf_p, "echo", (size_t) sz)) 263425bb815Sopenharmony_ci { 264425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_string (value)); 265425bb815Sopenharmony_ci jerry_size_t echo_sz = jerry_string_to_char_buffer (value, 266425bb815Sopenharmony_ci (jerry_char_t *) str_buf_p, 267425bb815Sopenharmony_ci 128); 268425bb815Sopenharmony_ci str_buf_p[echo_sz] = '\0'; 269425bb815Sopenharmony_ci TEST_ASSERT (!strncmp (str_buf_p, "foobar", (size_t) echo_sz)); 270425bb815Sopenharmony_ci return true; 271425bb815Sopenharmony_ci } 272425bb815Sopenharmony_ci 273425bb815Sopenharmony_ci TEST_ASSERT (false); 274425bb815Sopenharmony_ci return false; 275425bb815Sopenharmony_ci} /* foreach */ 276425bb815Sopenharmony_ci 277425bb815Sopenharmony_cistatic bool 278425bb815Sopenharmony_ciforeach_exception (const jerry_value_t name, /**< field name */ 279425bb815Sopenharmony_ci const jerry_value_t value, /**< field value */ 280425bb815Sopenharmony_ci void *user_data) /**< user data */ 281425bb815Sopenharmony_ci{ 282425bb815Sopenharmony_ci JERRY_UNUSED (value); 283425bb815Sopenharmony_ci JERRY_UNUSED (user_data); 284425bb815Sopenharmony_ci char str_buf_p[128]; 285425bb815Sopenharmony_ci jerry_size_t sz = jerry_string_to_char_buffer (name, (jerry_char_t *) str_buf_p, 128); 286425bb815Sopenharmony_ci str_buf_p[sz] = '\0'; 287425bb815Sopenharmony_ci 288425bb815Sopenharmony_ci if (!strncmp (str_buf_p, "foxtrot", (size_t) sz)) 289425bb815Sopenharmony_ci { 290425bb815Sopenharmony_ci TEST_ASSERT (false); 291425bb815Sopenharmony_ci } 292425bb815Sopenharmony_ci 293425bb815Sopenharmony_ci return true; 294425bb815Sopenharmony_ci} /* foreach_exception */ 295425bb815Sopenharmony_ci 296425bb815Sopenharmony_cistatic bool 297425bb815Sopenharmony_ciforeach_subset (const jerry_value_t name, /**< field name */ 298425bb815Sopenharmony_ci const jerry_value_t value, /**< field value */ 299425bb815Sopenharmony_ci void *user_data) /**< user data */ 300425bb815Sopenharmony_ci{ 301425bb815Sopenharmony_ci JERRY_UNUSED (name); 302425bb815Sopenharmony_ci JERRY_UNUSED (value); 303425bb815Sopenharmony_ci int *count_p = (int *) (user_data); 304425bb815Sopenharmony_ci 305425bb815Sopenharmony_ci if (*count_p == 3) 306425bb815Sopenharmony_ci { 307425bb815Sopenharmony_ci return false; 308425bb815Sopenharmony_ci } 309425bb815Sopenharmony_ci (*count_p)++; 310425bb815Sopenharmony_ci return true; 311425bb815Sopenharmony_ci} /* foreach_subset */ 312425bb815Sopenharmony_ci 313425bb815Sopenharmony_cistatic jerry_value_t 314425bb815Sopenharmony_ciget_property (const jerry_value_t obj_val, /**< object value */ 315425bb815Sopenharmony_ci const char *str_p) /**< property name */ 316425bb815Sopenharmony_ci{ 317425bb815Sopenharmony_ci jerry_value_t prop_name_val = jerry_create_string ((const jerry_char_t *) str_p); 318425bb815Sopenharmony_ci jerry_value_t ret_val = jerry_get_property (obj_val, prop_name_val); 319425bb815Sopenharmony_ci jerry_release_value (prop_name_val); 320425bb815Sopenharmony_ci return ret_val; 321425bb815Sopenharmony_ci} /* get_property */ 322425bb815Sopenharmony_ci 323425bb815Sopenharmony_cistatic jerry_value_t 324425bb815Sopenharmony_ciset_property (const jerry_value_t obj_val, /**< object value */ 325425bb815Sopenharmony_ci const char *str_p, /**< property name */ 326425bb815Sopenharmony_ci const jerry_value_t val) /**< value to set */ 327425bb815Sopenharmony_ci{ 328425bb815Sopenharmony_ci jerry_value_t prop_name_val = jerry_create_string ((const jerry_char_t *) str_p); 329425bb815Sopenharmony_ci jerry_value_t ret_val = jerry_set_property (obj_val, prop_name_val, val); 330425bb815Sopenharmony_ci jerry_release_value (prop_name_val); 331425bb815Sopenharmony_ci return ret_val; 332425bb815Sopenharmony_ci} /* set_property */ 333425bb815Sopenharmony_ci 334425bb815Sopenharmony_cistatic bool 335425bb815Sopenharmony_citest_run_simple (const char *script_p) /**< source code to run */ 336425bb815Sopenharmony_ci{ 337425bb815Sopenharmony_ci size_t script_size = strlen (script_p); 338425bb815Sopenharmony_ci 339425bb815Sopenharmony_ci return jerry_run_simple ((const jerry_char_t *) script_p, script_size, JERRY_INIT_EMPTY); 340425bb815Sopenharmony_ci} /* test_run_simple */ 341425bb815Sopenharmony_ci 342425bb815Sopenharmony_ciclass ApiTest : public testing::Test{ 343425bb815Sopenharmony_cipublic: 344425bb815Sopenharmony_ci static void SetUpTestCase() 345425bb815Sopenharmony_ci { 346425bb815Sopenharmony_ci GTEST_LOG_(INFO) << "ApiTest SetUpTestCase"; 347425bb815Sopenharmony_ci } 348425bb815Sopenharmony_ci 349425bb815Sopenharmony_ci static void TearDownTestCase() 350425bb815Sopenharmony_ci { 351425bb815Sopenharmony_ci GTEST_LOG_(INFO) << "ApiTest TearDownTestCase"; 352425bb815Sopenharmony_ci } 353425bb815Sopenharmony_ci 354425bb815Sopenharmony_ci void SetUp() override {} 355425bb815Sopenharmony_ci void TearDown() override {} 356425bb815Sopenharmony_ci 357425bb815Sopenharmony_ci}; 358425bb815Sopenharmony_cistatic constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024; 359425bb815Sopenharmony_cistatic void* context_alloc_fn(size_t size, void* cb_data) 360425bb815Sopenharmony_ci{ 361425bb815Sopenharmony_ci (void)cb_data; 362425bb815Sopenharmony_ci size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size; 363425bb815Sopenharmony_ci return malloc(newSize); 364425bb815Sopenharmony_ci} 365425bb815Sopenharmony_ciHWTEST_F(ApiTest, Test001, testing::ext::TestSize.Level1) 366425bb815Sopenharmony_ci{ 367425bb815Sopenharmony_ci jerry_context_t* ctx_p = jerry_create_context(1024 * 1024 * 50, context_alloc_fn, NULL); 368425bb815Sopenharmony_ci jerry_port_default_set_current_context(ctx_p); 369425bb815Sopenharmony_ci TEST_INIT (); 370425bb815Sopenharmony_ci 371425bb815Sopenharmony_ci bool is_ok; 372425bb815Sopenharmony_ci jerry_size_t sz, cesu8_sz; 373425bb815Sopenharmony_ci jerry_length_t cesu8_length; 374425bb815Sopenharmony_ci jerry_value_t val_t, val_foo, val_bar, val_A, val_A_prototype, val_a, val_a_foo, val_value_field, val_p, val_np; 375425bb815Sopenharmony_ci jerry_value_t val_call_external; 376425bb815Sopenharmony_ci jerry_value_t global_obj_val, obj_val; 377425bb815Sopenharmony_ci jerry_value_t external_func_val, external_construct_val; 378425bb815Sopenharmony_ci jerry_value_t throw_test_handler_val; 379425bb815Sopenharmony_ci jerry_value_t parsed_code_val, proto_val, prim_val; 380425bb815Sopenharmony_ci jerry_value_t res, args[2]; 381425bb815Sopenharmony_ci double number_val; 382425bb815Sopenharmony_ci char buffer[32]; 383425bb815Sopenharmony_ci 384425bb815Sopenharmony_ci is_ok = test_run_simple ("throw 'Hello World';"); 385425bb815Sopenharmony_ci TEST_ASSERT (!is_ok); 386425bb815Sopenharmony_ci jerry_init (JERRY_INIT_EMPTY); 387425bb815Sopenharmony_ci 388425bb815Sopenharmony_ci parsed_code_val = jerry_parse (NULL, 389425bb815Sopenharmony_ci 0, 390425bb815Sopenharmony_ci test_source, 391425bb815Sopenharmony_ci sizeof (test_source) - 1, 392425bb815Sopenharmony_ci JERRY_PARSE_NO_OPTS); 393425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (parsed_code_val)); 394425bb815Sopenharmony_ci 395425bb815Sopenharmony_ci res = jerry_run (parsed_code_val); 396425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (res)); 397425bb815Sopenharmony_ci jerry_release_value (res); 398425bb815Sopenharmony_ci jerry_release_value (parsed_code_val); 399425bb815Sopenharmony_ci 400425bb815Sopenharmony_ci global_obj_val = jerry_get_global_object (); 401425bb815Sopenharmony_ci 402425bb815Sopenharmony_ci /* Get global.boo (non-existing field) */ 403425bb815Sopenharmony_ci val_t = get_property (global_obj_val, "boo"); 404425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (val_t)); 405425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_undefined (val_t)); 406425bb815Sopenharmony_ci 407425bb815Sopenharmony_ci /* Get global.t */ 408425bb815Sopenharmony_ci val_t = get_property (global_obj_val, "t"); 409425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (val_t)); 410425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_number (val_t) 411425bb815Sopenharmony_ci && jerry_get_number_value (val_t) == 1.0); 412425bb815Sopenharmony_ci jerry_release_value (val_t); 413425bb815Sopenharmony_ci 414425bb815Sopenharmony_ci /* Get global.foo */ 415425bb815Sopenharmony_ci val_foo = get_property (global_obj_val, "foo"); 416425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (val_foo)); 417425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (val_foo)); 418425bb815Sopenharmony_ci 419425bb815Sopenharmony_ci /* Call foo (4, 2) */ 420425bb815Sopenharmony_ci args[0] = jerry_create_number (4); 421425bb815Sopenharmony_ci args[1] = jerry_create_number (2); 422425bb815Sopenharmony_ci res = jerry_call_function (val_foo, jerry_create_undefined (), args, 2); 423425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (res)); 424425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_number (res) 425425bb815Sopenharmony_ci && jerry_get_number_value (res) == 1.0); 426425bb815Sopenharmony_ci jerry_release_value (res); 427425bb815Sopenharmony_ci 428425bb815Sopenharmony_ci /* Get global.bar */ 429425bb815Sopenharmony_ci val_bar = get_property (global_obj_val, "bar"); 430425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (val_bar)); 431425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (val_bar)); 432425bb815Sopenharmony_ci 433425bb815Sopenharmony_ci /* Call bar (4, 2) */ 434425bb815Sopenharmony_ci res = jerry_call_function (val_bar, jerry_create_undefined (), args, 2); 435425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (res)); 436425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_number (res) 437425bb815Sopenharmony_ci && jerry_get_number_value (res) == 5.0); 438425bb815Sopenharmony_ci jerry_release_value (res); 439425bb815Sopenharmony_ci jerry_release_value (val_bar); 440425bb815Sopenharmony_ci 441425bb815Sopenharmony_ci /* Set global.t = "abcd" */ 442425bb815Sopenharmony_ci jerry_release_value (args[0]); 443425bb815Sopenharmony_ci args[0] = jerry_create_string ((jerry_char_t *) "abcd"); 444425bb815Sopenharmony_ci res = set_property (global_obj_val, "t", args[0]); 445425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (res)); 446425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_boolean_value (res)); 447425bb815Sopenharmony_ci jerry_release_value (res); 448425bb815Sopenharmony_ci 449425bb815Sopenharmony_ci /* Call foo (4, 2) */ 450425bb815Sopenharmony_ci res = jerry_call_function (val_foo, jerry_create_undefined (), args, 2); 451425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (res)); 452425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_string (res)); 453425bb815Sopenharmony_ci sz = jerry_get_string_size (res); 454425bb815Sopenharmony_ci TEST_ASSERT (sz == 4); 455425bb815Sopenharmony_ci sz = jerry_string_to_char_buffer (res, (jerry_char_t *) buffer, sz); 456425bb815Sopenharmony_ci TEST_ASSERT (sz == 4); 457425bb815Sopenharmony_ci jerry_release_value (res); 458425bb815Sopenharmony_ci TEST_ASSERT (!strncmp (buffer, "abcd", (size_t) sz)); 459425bb815Sopenharmony_ci jerry_release_value (args[0]); 460425bb815Sopenharmony_ci jerry_release_value (args[1]); 461425bb815Sopenharmony_ci 462425bb815Sopenharmony_ci /* Get global.A */ 463425bb815Sopenharmony_ci val_A = get_property (global_obj_val, "A"); 464425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (val_A)); 465425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (val_A)); 466425bb815Sopenharmony_ci 467425bb815Sopenharmony_ci /* Get A.prototype */ 468425bb815Sopenharmony_ci is_ok = jerry_value_is_constructor (val_A); 469425bb815Sopenharmony_ci TEST_ASSERT (is_ok); 470425bb815Sopenharmony_ci val_A_prototype = get_property (val_A, "prototype"); 471425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (val_A_prototype)); 472425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (val_A_prototype)); 473425bb815Sopenharmony_ci jerry_release_value (val_A); 474425bb815Sopenharmony_ci 475425bb815Sopenharmony_ci /* Set A.prototype.foo = global.foo */ 476425bb815Sopenharmony_ci res = set_property (val_A_prototype, "foo", val_foo); 477425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (res)); 478425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_boolean_value (res)); 479425bb815Sopenharmony_ci jerry_release_value (res); 480425bb815Sopenharmony_ci jerry_release_value (val_A_prototype); 481425bb815Sopenharmony_ci jerry_release_value (val_foo); 482425bb815Sopenharmony_ci 483425bb815Sopenharmony_ci /* Get global.a */ 484425bb815Sopenharmony_ci val_a = get_property (global_obj_val, "a"); 485425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (val_a)); 486425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (val_a)); 487425bb815Sopenharmony_ci 488425bb815Sopenharmony_ci /* Get a.t */ 489425bb815Sopenharmony_ci res = get_property (val_a, "t"); 490425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (res)); 491425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_number (res) 492425bb815Sopenharmony_ci && jerry_get_number_value (res) == 12.0); 493425bb815Sopenharmony_ci jerry_release_value (res); 494425bb815Sopenharmony_ci 495425bb815Sopenharmony_ci /* foreach properties */ 496425bb815Sopenharmony_ci val_p = get_property (global_obj_val, "p"); 497425bb815Sopenharmony_ci is_ok = jerry_foreach_object_property (val_p, foreach, (void *) "user_data"); 498425bb815Sopenharmony_ci TEST_ASSERT (is_ok); 499425bb815Sopenharmony_ci 500425bb815Sopenharmony_ci /* break foreach at third element */ 501425bb815Sopenharmony_ci int count = 0; 502425bb815Sopenharmony_ci is_ok = jerry_foreach_object_property (val_p, foreach_subset, &count); 503425bb815Sopenharmony_ci TEST_ASSERT (is_ok); 504425bb815Sopenharmony_ci TEST_ASSERT (count == 3); 505425bb815Sopenharmony_ci jerry_release_value (val_p); 506425bb815Sopenharmony_ci 507425bb815Sopenharmony_ci /* foreach with throw test */ 508425bb815Sopenharmony_ci val_np = get_property (global_obj_val, "np"); 509425bb815Sopenharmony_ci is_ok = !jerry_foreach_object_property (val_np, foreach_exception, NULL); 510425bb815Sopenharmony_ci TEST_ASSERT (is_ok); 511425bb815Sopenharmony_ci jerry_release_value (val_np); 512425bb815Sopenharmony_ci 513425bb815Sopenharmony_ci /* Get a.foo */ 514425bb815Sopenharmony_ci val_a_foo = get_property (val_a, "foo"); 515425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (val_a_foo)); 516425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (val_a_foo)); 517425bb815Sopenharmony_ci 518425bb815Sopenharmony_ci /* Call a.foo () */ 519425bb815Sopenharmony_ci res = jerry_call_function (val_a_foo, val_a, NULL, 0); 520425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (res)); 521425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_number (res) 522425bb815Sopenharmony_ci && jerry_get_number_value (res) == 12.0); 523425bb815Sopenharmony_ci jerry_release_value (res); 524425bb815Sopenharmony_ci jerry_release_value (val_a_foo); 525425bb815Sopenharmony_ci 526425bb815Sopenharmony_ci jerry_release_value (val_a); 527425bb815Sopenharmony_ci 528425bb815Sopenharmony_ci /* Create native handler bound function object and set it to 'external' variable */ 529425bb815Sopenharmony_ci external_func_val = jerry_create_external_function (handler); 530425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_function (external_func_val) 531425bb815Sopenharmony_ci && jerry_value_is_constructor (external_func_val)); 532425bb815Sopenharmony_ci 533425bb815Sopenharmony_ci res = set_property (global_obj_val, "external", external_func_val); 534425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (res)); 535425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_boolean_value (res)); 536425bb815Sopenharmony_ci jerry_release_value (external_func_val); 537425bb815Sopenharmony_ci 538425bb815Sopenharmony_ci /* Call 'call_external' function that should call external function created above */ 539425bb815Sopenharmony_ci val_call_external = get_property (global_obj_val, "call_external"); 540425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (val_call_external)); 541425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (val_call_external)); 542425bb815Sopenharmony_ci res = jerry_call_function (val_call_external, global_obj_val, NULL, 0); 543425bb815Sopenharmony_ci jerry_release_value (val_call_external); 544425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (res)); 545425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_string (res)); 546425bb815Sopenharmony_ci sz = jerry_get_string_size (res); 547425bb815Sopenharmony_ci TEST_ASSERT (sz == 19); 548425bb815Sopenharmony_ci sz = jerry_string_to_char_buffer (res, (jerry_char_t *) buffer, sz); 549425bb815Sopenharmony_ci TEST_ASSERT (sz == 19); 550425bb815Sopenharmony_ci jerry_release_value (res); 551425bb815Sopenharmony_ci TEST_ASSERT (!strncmp (buffer, "string from handler", (size_t) sz)); 552425bb815Sopenharmony_ci 553425bb815Sopenharmony_ci /* Create native handler bound function object and set it to 'external_construct' variable */ 554425bb815Sopenharmony_ci external_construct_val = jerry_create_external_function (handler_construct); 555425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_function (external_construct_val) 556425bb815Sopenharmony_ci && jerry_value_is_constructor (external_construct_val)); 557425bb815Sopenharmony_ci 558425bb815Sopenharmony_ci res = set_property (global_obj_val, "external_construct", external_construct_val); 559425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (res)); 560425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_boolean_value (res)); 561425bb815Sopenharmony_ci jerry_release_value (res); 562425bb815Sopenharmony_ci 563425bb815Sopenharmony_ci /* Call external function created above, as constructor */ 564425bb815Sopenharmony_ci args[0] = jerry_create_boolean (true); 565425bb815Sopenharmony_ci res = jerry_construct_object (external_construct_val, args, 1); 566425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (res)); 567425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (res)); 568425bb815Sopenharmony_ci val_value_field = get_property (res, "value_field"); 569425bb815Sopenharmony_ci 570425bb815Sopenharmony_ci /* Get 'value_field' of constructed object */ 571425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (val_value_field)); 572425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_boolean (val_value_field) 573425bb815Sopenharmony_ci && jerry_get_boolean_value (val_value_field)); 574425bb815Sopenharmony_ci jerry_release_value (val_value_field); 575425bb815Sopenharmony_ci jerry_release_value (external_construct_val); 576425bb815Sopenharmony_ci 577425bb815Sopenharmony_ci void *ptr = NULL; 578425bb815Sopenharmony_ci is_ok = jerry_get_object_native_pointer (res, &ptr, &JERRY_NATIVE_HANDLE_INFO_FOR_CTYPE (bind2)); 579425bb815Sopenharmony_ci TEST_ASSERT (is_ok 580425bb815Sopenharmony_ci && (uintptr_t) ptr == (uintptr_t) 0x0012345678abcdefull); 581425bb815Sopenharmony_ci 582425bb815Sopenharmony_ci /* Passing NULL for info_p is allowed. */ 583425bb815Sopenharmony_ci is_ok = jerry_get_object_native_pointer (res, &ptr, NULL); 584425bb815Sopenharmony_ci TEST_ASSERT (!is_ok); 585425bb815Sopenharmony_ci 586425bb815Sopenharmony_ci jerry_release_value (res); 587425bb815Sopenharmony_ci 588425bb815Sopenharmony_ci /* Test: It is ok to set native pointer's free callback as NULL. */ 589425bb815Sopenharmony_ci jerry_value_t obj_freecb = jerry_create_object (); 590425bb815Sopenharmony_ci jerry_set_object_native_pointer (obj_freecb, 591425bb815Sopenharmony_ci (void *) 0x1234, 592425bb815Sopenharmony_ci &JERRY_NATIVE_HANDLE_INFO_FOR_CTYPE (bind3)); 593425bb815Sopenharmony_ci 594425bb815Sopenharmony_ci jerry_release_value (obj_freecb); 595425bb815Sopenharmony_ci 596425bb815Sopenharmony_ci /* Test: Throwing exception from native handler. */ 597425bb815Sopenharmony_ci throw_test_handler_val = jerry_create_external_function (handler_throw_test); 598425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_function (throw_test_handler_val)); 599425bb815Sopenharmony_ci 600425bb815Sopenharmony_ci res = set_property (global_obj_val, "throw_test", throw_test_handler_val); 601425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (res)); 602425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_boolean_value (res)); 603425bb815Sopenharmony_ci jerry_release_value (res); 604425bb815Sopenharmony_ci jerry_release_value (throw_test_handler_val); 605425bb815Sopenharmony_ci 606425bb815Sopenharmony_ci val_t = get_property (global_obj_val, "call_throw_test"); 607425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (val_t)); 608425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (val_t)); 609425bb815Sopenharmony_ci 610425bb815Sopenharmony_ci res = jerry_call_function (val_t, global_obj_val, NULL, 0); 611425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (res)); 612425bb815Sopenharmony_ci jerry_release_value (val_t); 613425bb815Sopenharmony_ci jerry_release_value (res); 614425bb815Sopenharmony_ci 615425bb815Sopenharmony_ci /* Test: Unhandled exception in called function */ 616425bb815Sopenharmony_ci val_t = get_property (global_obj_val, "throw_reference_error"); 617425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (val_t)); 618425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (val_t)); 619425bb815Sopenharmony_ci 620425bb815Sopenharmony_ci res = jerry_call_function (val_t, global_obj_val, NULL, 0); 621425bb815Sopenharmony_ci 622425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_error (res)); 623425bb815Sopenharmony_ci jerry_release_value (val_t); 624425bb815Sopenharmony_ci 625425bb815Sopenharmony_ci /* 'res' should contain exception object */ 626425bb815Sopenharmony_ci res = jerry_get_value_from_error (res, true); 627425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (res)); 628425bb815Sopenharmony_ci jerry_release_value (res); 629425bb815Sopenharmony_ci 630425bb815Sopenharmony_ci /* Test: Call of non-function */ 631425bb815Sopenharmony_ci obj_val = jerry_create_object (); 632425bb815Sopenharmony_ci res = jerry_call_function (obj_val, global_obj_val, NULL, 0); 633425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_error (res)); 634425bb815Sopenharmony_ci 635425bb815Sopenharmony_ci /* 'res' should contain exception object */ 636425bb815Sopenharmony_ci res = jerry_get_value_from_error (res, true); 637425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (res)); 638425bb815Sopenharmony_ci jerry_release_value (res); 639425bb815Sopenharmony_ci 640425bb815Sopenharmony_ci jerry_release_value (obj_val); 641425bb815Sopenharmony_ci 642425bb815Sopenharmony_ci /* Test: Unhandled exception in function called, as constructor */ 643425bb815Sopenharmony_ci val_t = get_property (global_obj_val, "throw_reference_error"); 644425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (val_t)); 645425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (val_t)); 646425bb815Sopenharmony_ci 647425bb815Sopenharmony_ci res = jerry_construct_object (val_t, NULL, 0); 648425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_error (res)); 649425bb815Sopenharmony_ci jerry_release_value (val_t); 650425bb815Sopenharmony_ci 651425bb815Sopenharmony_ci /* 'res' should contain exception object */ 652425bb815Sopenharmony_ci res = jerry_get_value_from_error (res, true); 653425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (res)); 654425bb815Sopenharmony_ci jerry_release_value (res); 655425bb815Sopenharmony_ci 656425bb815Sopenharmony_ci /* Test: Call of non-function as constructor */ 657425bb815Sopenharmony_ci obj_val = jerry_create_object (); 658425bb815Sopenharmony_ci res = jerry_construct_object (obj_val, NULL, 0); 659425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_error (res)); 660425bb815Sopenharmony_ci 661425bb815Sopenharmony_ci /* 'res' should contain exception object */ 662425bb815Sopenharmony_ci res = jerry_get_value_from_error (res, true); 663425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (res)); 664425bb815Sopenharmony_ci jerry_release_value (res); 665425bb815Sopenharmony_ci 666425bb815Sopenharmony_ci jerry_release_value (obj_val); 667425bb815Sopenharmony_ci 668425bb815Sopenharmony_ci /* Test: Array Object API */ 669425bb815Sopenharmony_ci jerry_value_t array_obj_val = jerry_create_array (10); 670425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_array (array_obj_val)); 671425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_array_length (array_obj_val) == 10); 672425bb815Sopenharmony_ci 673425bb815Sopenharmony_ci jerry_value_t v_in = jerry_create_number (10.5); 674425bb815Sopenharmony_ci res = jerry_set_property_by_index (array_obj_val, 5, v_in); 675425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (res)); 676425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_boolean (res) && jerry_get_boolean_value (res)); 677425bb815Sopenharmony_ci jerry_release_value (res); 678425bb815Sopenharmony_ci jerry_value_t v_out = jerry_get_property_by_index (array_obj_val, 5); 679425bb815Sopenharmony_ci 680425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_number (v_out) 681425bb815Sopenharmony_ci && jerry_get_number_value (v_out) == 10.5); 682425bb815Sopenharmony_ci 683425bb815Sopenharmony_ci jerry_delete_property_by_index (array_obj_val, 5); 684425bb815Sopenharmony_ci jerry_value_t v_und = jerry_get_property_by_index (array_obj_val, 5); 685425bb815Sopenharmony_ci 686425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_undefined (v_und)); 687425bb815Sopenharmony_ci 688425bb815Sopenharmony_ci jerry_release_value (v_in); 689425bb815Sopenharmony_ci jerry_release_value (v_out); 690425bb815Sopenharmony_ci jerry_release_value (v_und); 691425bb815Sopenharmony_ci jerry_release_value (array_obj_val); 692425bb815Sopenharmony_ci 693425bb815Sopenharmony_ci /* Test: object keys */ 694425bb815Sopenharmony_ci res = jerry_get_object_keys (global_obj_val); 695425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (res)); 696425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_array (res)); 697425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_array_length (res) == 15); 698425bb815Sopenharmony_ci jerry_release_value (res); 699425bb815Sopenharmony_ci 700425bb815Sopenharmony_ci /* Test: jerry_value_to_primitive */ 701425bb815Sopenharmony_ci obj_val = jerry_eval ((jerry_char_t *) "new String ('hello')", 20, JERRY_PARSE_NO_OPTS); 702425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (obj_val)); 703425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (obj_val)); 704425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_string (obj_val)); 705425bb815Sopenharmony_ci prim_val = jerry_value_to_primitive (obj_val); 706425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (prim_val)); 707425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_string (prim_val)); 708425bb815Sopenharmony_ci jerry_release_value (prim_val); 709425bb815Sopenharmony_ci 710425bb815Sopenharmony_ci /* Test: jerry_get_prototype */ 711425bb815Sopenharmony_ci proto_val = jerry_get_prototype (jerry_create_undefined ()); 712425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_error (proto_val)); 713425bb815Sopenharmony_ci jerry_value_t error = jerry_get_value_from_error (proto_val, true); 714425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_error_type (error) == JERRY_ERROR_TYPE); 715425bb815Sopenharmony_ci jerry_release_value (error); 716425bb815Sopenharmony_ci 717425bb815Sopenharmony_ci proto_val = jerry_get_prototype (obj_val); 718425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (proto_val)); 719425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (proto_val)); 720425bb815Sopenharmony_ci jerry_release_value (proto_val); 721425bb815Sopenharmony_ci jerry_release_value (obj_val); 722425bb815Sopenharmony_ci 723425bb815Sopenharmony_ci if (jerry_is_feature_enabled (JERRY_FEATURE_PROXY)) 724425bb815Sopenharmony_ci { 725425bb815Sopenharmony_ci jerry_value_t target = jerry_create_object (); 726425bb815Sopenharmony_ci jerry_value_t handler = jerry_create_object (); 727425bb815Sopenharmony_ci jerry_value_t proxy = jerry_create_proxy (target, handler); 728425bb815Sopenharmony_ci jerry_value_t obj_proto = jerry_eval ((jerry_char_t *) "Object.prototype", 16, JERRY_PARSE_NO_OPTS); 729425bb815Sopenharmony_ci 730425bb815Sopenharmony_ci jerry_release_value (target); 731425bb815Sopenharmony_ci jerry_release_value (handler); 732425bb815Sopenharmony_ci proto_val = jerry_get_prototype (proxy); 733425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (proto_val)); 734425bb815Sopenharmony_ci TEST_ASSERT (proto_val == obj_proto); 735425bb815Sopenharmony_ci jerry_release_value (proto_val); 736425bb815Sopenharmony_ci jerry_release_value (obj_proto); 737425bb815Sopenharmony_ci jerry_release_value (proxy); 738425bb815Sopenharmony_ci } 739425bb815Sopenharmony_ci 740425bb815Sopenharmony_ci /* Test: jerry_set_prototype */ 741425bb815Sopenharmony_ci obj_val = jerry_create_object (); 742425bb815Sopenharmony_ci res = jerry_set_prototype (obj_val, jerry_create_null ()); 743425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (res)); 744425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_boolean (res)); 745425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_boolean_value (res)); 746425bb815Sopenharmony_ci 747425bb815Sopenharmony_ci jerry_value_t new_proto = jerry_create_object (); 748425bb815Sopenharmony_ci res = jerry_set_prototype (obj_val, new_proto); 749425bb815Sopenharmony_ci jerry_release_value (new_proto); 750425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (res)); 751425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_boolean (res)); 752425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_boolean_value (res)); 753425bb815Sopenharmony_ci proto_val = jerry_get_prototype (obj_val); 754425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (proto_val)); 755425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (proto_val)); 756425bb815Sopenharmony_ci jerry_release_value (proto_val); 757425bb815Sopenharmony_ci jerry_release_value (obj_val); 758425bb815Sopenharmony_ci 759425bb815Sopenharmony_ci if (jerry_is_feature_enabled (JERRY_FEATURE_PROXY)) 760425bb815Sopenharmony_ci { 761425bb815Sopenharmony_ci jerry_value_t target = jerry_create_object (); 762425bb815Sopenharmony_ci jerry_value_t handler = jerry_create_object (); 763425bb815Sopenharmony_ci jerry_value_t proxy = jerry_create_proxy (target, handler); 764425bb815Sopenharmony_ci new_proto = jerry_eval ((jerry_char_t *) "Function.prototype", 18, JERRY_PARSE_NO_OPTS); 765425bb815Sopenharmony_ci 766425bb815Sopenharmony_ci res = jerry_set_prototype (proxy, new_proto); 767425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (res)); 768425bb815Sopenharmony_ci jerry_value_t target_proto = jerry_get_prototype (target); 769425bb815Sopenharmony_ci TEST_ASSERT (target_proto == new_proto); 770425bb815Sopenharmony_ci 771425bb815Sopenharmony_ci jerry_release_value (target); 772425bb815Sopenharmony_ci jerry_release_value (handler); 773425bb815Sopenharmony_ci jerry_release_value (proxy); 774425bb815Sopenharmony_ci jerry_release_value (new_proto); 775425bb815Sopenharmony_ci jerry_release_value (target_proto); 776425bb815Sopenharmony_ci } 777425bb815Sopenharmony_ci 778425bb815Sopenharmony_ci /* Test: eval */ 779425bb815Sopenharmony_ci const jerry_char_t eval_code_src1[] = "(function () { return 123; })"; 780425bb815Sopenharmony_ci val_t = jerry_eval (eval_code_src1, sizeof (eval_code_src1) - 1, JERRY_PARSE_STRICT_MODE); 781425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (val_t)); 782425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (val_t)); 783425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_function (val_t)); 784425bb815Sopenharmony_ci 785425bb815Sopenharmony_ci res = jerry_call_function (val_t, jerry_create_undefined (), NULL, 0); 786425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (res)); 787425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_number (res) 788425bb815Sopenharmony_ci && jerry_get_number_value (res) == 123.0); 789425bb815Sopenharmony_ci jerry_release_value (res); 790425bb815Sopenharmony_ci 791425bb815Sopenharmony_ci jerry_release_value (val_t); 792425bb815Sopenharmony_ci 793425bb815Sopenharmony_ci /* cleanup. */ 794425bb815Sopenharmony_ci jerry_release_value (global_obj_val); 795425bb815Sopenharmony_ci 796425bb815Sopenharmony_ci /* Test: run gc. */ 797425bb815Sopenharmony_ci jerry_gc (JERRY_GC_PRESSURE_LOW); 798425bb815Sopenharmony_ci 799425bb815Sopenharmony_ci /* Test: spaces */ 800425bb815Sopenharmony_ci const jerry_char_t eval_code_src2[] = "\x0a \x0b \x0c \xc2\xa0 \xe2\x80\xa8 \xe2\x80\xa9 \xef\xbb\xbf 4321"; 801425bb815Sopenharmony_ci val_t = jerry_eval (eval_code_src2, sizeof (eval_code_src2) - 1, JERRY_PARSE_STRICT_MODE); 802425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (val_t)); 803425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_number (val_t) 804425bb815Sopenharmony_ci && jerry_get_number_value (val_t) == 4321.0); 805425bb815Sopenharmony_ci jerry_release_value (val_t); 806425bb815Sopenharmony_ci 807425bb815Sopenharmony_ci /* Test: number */ 808425bb815Sopenharmony_ci val_t = jerry_create_number (6.25); 809425bb815Sopenharmony_ci number_val = jerry_get_number_value (val_t); 810425bb815Sopenharmony_ci TEST_ASSERT (number_val * 3 == 18.75); 811425bb815Sopenharmony_ci jerry_release_value (val_t); 812425bb815Sopenharmony_ci 813425bb815Sopenharmony_ci val_t = jerry_create_number_infinity (true); 814425bb815Sopenharmony_ci number_val = jerry_get_number_value (val_t); 815425bb815Sopenharmony_ci TEST_ASSERT (number_val * 3 == number_val && number_val != 0.0); 816425bb815Sopenharmony_ci jerry_release_value (val_t); 817425bb815Sopenharmony_ci 818425bb815Sopenharmony_ci val_t = jerry_create_number_nan (); 819425bb815Sopenharmony_ci number_val = jerry_get_number_value (val_t); 820425bb815Sopenharmony_ci TEST_ASSERT (number_val != number_val); 821425bb815Sopenharmony_ci jerry_release_value (val_t); 822425bb815Sopenharmony_ci 823425bb815Sopenharmony_ci /* Test: create function */ 824425bb815Sopenharmony_ci const jerry_char_t func_resource[] = "unknown"; 825425bb815Sopenharmony_ci const jerry_char_t func_arg_list[] = "a , b,c"; 826425bb815Sopenharmony_ci const jerry_char_t func_src[] = " return 5 + a+\nb+c"; 827425bb815Sopenharmony_ci 828425bb815Sopenharmony_ci jerry_value_t func_val = jerry_parse_function (func_resource, 829425bb815Sopenharmony_ci sizeof (func_resource) - 1, 830425bb815Sopenharmony_ci func_arg_list, 831425bb815Sopenharmony_ci sizeof (func_arg_list) - 1, 832425bb815Sopenharmony_ci func_src, 833425bb815Sopenharmony_ci sizeof (func_src) - 1, 834425bb815Sopenharmony_ci JERRY_PARSE_NO_OPTS); 835425bb815Sopenharmony_ci 836425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (func_val)); 837425bb815Sopenharmony_ci 838425bb815Sopenharmony_ci jerry_value_t func_args[3] = 839425bb815Sopenharmony_ci { 840425bb815Sopenharmony_ci jerry_create_number (4), 841425bb815Sopenharmony_ci jerry_create_number (6), 842425bb815Sopenharmony_ci jerry_create_number (-2) 843425bb815Sopenharmony_ci }; 844425bb815Sopenharmony_ci 845425bb815Sopenharmony_ci val_t = jerry_call_function (func_val, func_args[0], func_args, 3); 846425bb815Sopenharmony_ci number_val = jerry_get_number_value (val_t); 847425bb815Sopenharmony_ci TEST_ASSERT (number_val == 13.0); 848425bb815Sopenharmony_ci 849425bb815Sopenharmony_ci jerry_release_value (val_t); 850425bb815Sopenharmony_ci jerry_release_value (func_val); 851425bb815Sopenharmony_ci 852425bb815Sopenharmony_ci jerry_cleanup (); 853425bb815Sopenharmony_ci 854425bb815Sopenharmony_ci TEST_ASSERT (test_api_is_free_callback_was_called); 855425bb815Sopenharmony_ci 856425bb815Sopenharmony_ci /* Test: jerry_get_value_from_error */ 857425bb815Sopenharmony_ci { 858425bb815Sopenharmony_ci jerry_init (JERRY_INIT_EMPTY); 859425bb815Sopenharmony_ci jerry_value_t num_val = jerry_create_number (123); 860425bb815Sopenharmony_ci num_val = jerry_create_error_from_value (num_val, true); 861425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_error (num_val)); 862425bb815Sopenharmony_ci jerry_value_t num2_val = jerry_get_value_from_error (num_val, false); 863425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_error (num_val)); 864425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (num2_val)); 865425bb815Sopenharmony_ci double num = jerry_get_number_value (num2_val); 866425bb815Sopenharmony_ci TEST_ASSERT (num == 123); 867425bb815Sopenharmony_ci num2_val = jerry_get_value_from_error (num_val, true); 868425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (num2_val)); 869425bb815Sopenharmony_ci num = jerry_get_number_value (num2_val); 870425bb815Sopenharmony_ci TEST_ASSERT (num == 123); 871425bb815Sopenharmony_ci jerry_release_value (num2_val); 872425bb815Sopenharmony_ci jerry_cleanup (); 873425bb815Sopenharmony_ci } 874425bb815Sopenharmony_ci 875425bb815Sopenharmony_ci /* Test parsing/executing scripts with lexically scoped global variables multiple times. */ 876425bb815Sopenharmony_ci if (jerry_is_feature_enabled (JERRY_FEATURE_SYMBOL)) 877425bb815Sopenharmony_ci { 878425bb815Sopenharmony_ci jerry_init (JERRY_INIT_EMPTY); 879425bb815Sopenharmony_ci const jerry_char_t scoped_src_p[] = "let a;"; 880425bb815Sopenharmony_ci jerry_value_t parse_result = jerry_parse (NULL, 881425bb815Sopenharmony_ci 0, 882425bb815Sopenharmony_ci scoped_src_p, 883425bb815Sopenharmony_ci sizeof (scoped_src_p) - 1, 884425bb815Sopenharmony_ci JERRY_PARSE_NO_OPTS); 885425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (parse_result)); 886425bb815Sopenharmony_ci jerry_release_value (parse_result); 887425bb815Sopenharmony_ci 888425bb815Sopenharmony_ci parse_result = jerry_parse (NULL, 889425bb815Sopenharmony_ci 0, 890425bb815Sopenharmony_ci scoped_src_p, 891425bb815Sopenharmony_ci sizeof (scoped_src_p) - 1, 892425bb815Sopenharmony_ci JERRY_PARSE_NO_OPTS); 893425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (parse_result)); 894425bb815Sopenharmony_ci 895425bb815Sopenharmony_ci jerry_value_t run_result = jerry_run (parse_result); 896425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (run_result)); 897425bb815Sopenharmony_ci jerry_release_value (run_result); 898425bb815Sopenharmony_ci 899425bb815Sopenharmony_ci /* Should be a syntax error due to redeclaration. */ 900425bb815Sopenharmony_ci run_result = jerry_run (parse_result); 901425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_error (run_result)); 902425bb815Sopenharmony_ci jerry_release_value (run_result); 903425bb815Sopenharmony_ci jerry_release_value (parse_result); 904425bb815Sopenharmony_ci 905425bb815Sopenharmony_ci /* The variable should have no effect on parsing. */ 906425bb815Sopenharmony_ci parse_result = jerry_parse (NULL, 907425bb815Sopenharmony_ci 0, 908425bb815Sopenharmony_ci scoped_src_p, 909425bb815Sopenharmony_ci sizeof (scoped_src_p) - 1, 910425bb815Sopenharmony_ci JERRY_PARSE_NO_OPTS); 911425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (parse_result)); 912425bb815Sopenharmony_ci jerry_release_value (parse_result); 913425bb815Sopenharmony_ci jerry_cleanup (); 914425bb815Sopenharmony_ci } 915425bb815Sopenharmony_ci 916425bb815Sopenharmony_ci /* Test: parser error location */ 917425bb815Sopenharmony_ci if (jerry_is_feature_enabled (JERRY_FEATURE_ERROR_MESSAGES)) 918425bb815Sopenharmony_ci { 919425bb815Sopenharmony_ci jerry_init (JERRY_INIT_SHOW_OPCODES); 920425bb815Sopenharmony_ci 921425bb815Sopenharmony_ci const jerry_char_t parser_err_src[] = "b = 'hello';\nvar a = (;"; 922425bb815Sopenharmony_ci parsed_code_val = jerry_parse (NULL, 923425bb815Sopenharmony_ci 0, 924425bb815Sopenharmony_ci parser_err_src, 925425bb815Sopenharmony_ci sizeof (parser_err_src) - 1, 926425bb815Sopenharmony_ci JERRY_PARSE_NO_OPTS); 927425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_error (parsed_code_val)); 928425bb815Sopenharmony_ci parsed_code_val = jerry_get_value_from_error (parsed_code_val, true); 929425bb815Sopenharmony_ci jerry_value_t err_str_val = jerry_value_to_string (parsed_code_val); 930425bb815Sopenharmony_ci jerry_size_t err_str_size = jerry_get_string_size (err_str_val); 931425bb815Sopenharmony_ci jerry_char_t err_str_buf[256]; 932425bb815Sopenharmony_ci sz = jerry_string_to_char_buffer (err_str_val, err_str_buf, err_str_size); 933425bb815Sopenharmony_ci err_str_buf[sz] = 0; 934425bb815Sopenharmony_ci 935425bb815Sopenharmony_ci jerry_release_value (err_str_val); 936425bb815Sopenharmony_ci jerry_release_value (parsed_code_val); 937425bb815Sopenharmony_ci TEST_ASSERT (!strcmp ((char *) err_str_buf, 938425bb815Sopenharmony_ci "SyntaxError: Primary expression expected. [<anonymous>:2:10]")); 939425bb815Sopenharmony_ci 940425bb815Sopenharmony_ci const jerry_char_t file_str[] = "filename.js"; 941425bb815Sopenharmony_ci parsed_code_val = jerry_parse (file_str, 942425bb815Sopenharmony_ci sizeof (file_str) - 1, 943425bb815Sopenharmony_ci parser_err_src, 944425bb815Sopenharmony_ci sizeof (parser_err_src) - 1, 945425bb815Sopenharmony_ci JERRY_PARSE_NO_OPTS); 946425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_error (parsed_code_val)); 947425bb815Sopenharmony_ci parsed_code_val = jerry_get_value_from_error (parsed_code_val, true); 948425bb815Sopenharmony_ci err_str_val = jerry_value_to_string (parsed_code_val); 949425bb815Sopenharmony_ci err_str_size = jerry_get_string_size (err_str_val); 950425bb815Sopenharmony_ci 951425bb815Sopenharmony_ci sz = jerry_string_to_char_buffer (err_str_val, err_str_buf, err_str_size); 952425bb815Sopenharmony_ci err_str_buf[sz] = 0; 953425bb815Sopenharmony_ci 954425bb815Sopenharmony_ci jerry_release_value (err_str_val); 955425bb815Sopenharmony_ci jerry_release_value (parsed_code_val); 956425bb815Sopenharmony_ci TEST_ASSERT (!strcmp ((char *) err_str_buf, 957425bb815Sopenharmony_ci "SyntaxError: Primary expression expected. [filename.js:2:10]")); 958425bb815Sopenharmony_ci 959425bb815Sopenharmony_ci const jerry_char_t eval_err_src[] = "eval(\"var b;\\nfor (,); \");"; 960425bb815Sopenharmony_ci parsed_code_val = jerry_parse (file_str, 961425bb815Sopenharmony_ci sizeof (file_str), 962425bb815Sopenharmony_ci eval_err_src, 963425bb815Sopenharmony_ci sizeof (eval_err_src) - 1, 964425bb815Sopenharmony_ci JERRY_PARSE_NO_OPTS); 965425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (parsed_code_val)); 966425bb815Sopenharmony_ci 967425bb815Sopenharmony_ci res = jerry_run (parsed_code_val); 968425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_error (res)); 969425bb815Sopenharmony_ci res = jerry_get_value_from_error (res, true); 970425bb815Sopenharmony_ci err_str_val = jerry_value_to_string (res); 971425bb815Sopenharmony_ci err_str_size = jerry_get_string_size (err_str_val); 972425bb815Sopenharmony_ci 973425bb815Sopenharmony_ci sz = jerry_string_to_char_buffer (err_str_val, err_str_buf, err_str_size); 974425bb815Sopenharmony_ci err_str_buf[sz] = 0; 975425bb815Sopenharmony_ci 976425bb815Sopenharmony_ci jerry_release_value (err_str_val); 977425bb815Sopenharmony_ci jerry_release_value (parsed_code_val); 978425bb815Sopenharmony_ci jerry_release_value (res); 979425bb815Sopenharmony_ci TEST_ASSERT (!strcmp ((char *) err_str_buf, 980425bb815Sopenharmony_ci "SyntaxError: Primary expression expected. [<eval>:2:6]")); 981425bb815Sopenharmony_ci 982425bb815Sopenharmony_ci jerry_cleanup (); 983425bb815Sopenharmony_ci } 984425bb815Sopenharmony_ci 985425bb815Sopenharmony_ci /* External Magic String */ 986425bb815Sopenharmony_ci jerry_init (JERRY_INIT_SHOW_OPCODES); 987425bb815Sopenharmony_ci 988425bb815Sopenharmony_ci uint32_t num_magic_string_items = (uint32_t) (sizeof (magic_string_items) / sizeof (jerry_char_t *)); 989425bb815Sopenharmony_ci jerry_register_magic_strings (magic_string_items, 990425bb815Sopenharmony_ci num_magic_string_items, 991425bb815Sopenharmony_ci magic_string_lengths); 992425bb815Sopenharmony_ci 993425bb815Sopenharmony_ci const jerry_char_t ms_code_src[] = "var global = {}; var console = [1]; var process = 1;"; 994425bb815Sopenharmony_ci parsed_code_val = jerry_parse (NULL, 995425bb815Sopenharmony_ci 0, 996425bb815Sopenharmony_ci ms_code_src, 997425bb815Sopenharmony_ci sizeof (ms_code_src) - 1, 998425bb815Sopenharmony_ci JERRY_PARSE_NO_OPTS); 999425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (parsed_code_val)); 1000425bb815Sopenharmony_ci 1001425bb815Sopenharmony_ci res = jerry_run (parsed_code_val); 1002425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (res)); 1003425bb815Sopenharmony_ci jerry_release_value (res); 1004425bb815Sopenharmony_ci jerry_release_value (parsed_code_val); 1005425bb815Sopenharmony_ci 1006425bb815Sopenharmony_ci /* call jerry_create_string functions which will returns with the registered external magic strings */ 1007425bb815Sopenharmony_ci args[0] = jerry_create_string ((jerry_char_t *) "console"); 1008425bb815Sopenharmony_ci args[1] = jerry_create_string ((jerry_char_t *) "\xed\xa0\x80\xed\xb6\x8a"); /**< greek zero sign */ 1009425bb815Sopenharmony_ci 1010425bb815Sopenharmony_ci cesu8_length = jerry_get_string_length (args[0]); 1011425bb815Sopenharmony_ci cesu8_sz = jerry_get_string_size (args[0]); 1012425bb815Sopenharmony_ci 1013425bb815Sopenharmony_ci JERRY_VLA (char, string_console, cesu8_sz); 1014425bb815Sopenharmony_ci jerry_string_to_char_buffer (args[0], (jerry_char_t *) string_console, cesu8_sz); 1015425bb815Sopenharmony_ci 1016425bb815Sopenharmony_ci TEST_ASSERT (!strncmp (string_console, "console", cesu8_sz)); 1017425bb815Sopenharmony_ci TEST_ASSERT (cesu8_length == 7); 1018425bb815Sopenharmony_ci TEST_ASSERT (cesu8_length == cesu8_sz); 1019425bb815Sopenharmony_ci 1020425bb815Sopenharmony_ci jerry_release_value (args[0]); 1021425bb815Sopenharmony_ci 1022425bb815Sopenharmony_ci const jerry_char_t test_magic_str_access_src[] = "'console'.charAt(6) == 'e'"; 1023425bb815Sopenharmony_ci res = jerry_eval (test_magic_str_access_src, 1024425bb815Sopenharmony_ci sizeof (test_magic_str_access_src) - 1, 1025425bb815Sopenharmony_ci JERRY_PARSE_NO_OPTS); 1026425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_boolean (res)); 1027425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_boolean_value (res) == true); 1028425bb815Sopenharmony_ci 1029425bb815Sopenharmony_ci jerry_release_value (res); 1030425bb815Sopenharmony_ci 1031425bb815Sopenharmony_ci cesu8_length = jerry_get_string_length (args[1]); 1032425bb815Sopenharmony_ci cesu8_sz = jerry_get_string_size (args[1]); 1033425bb815Sopenharmony_ci 1034425bb815Sopenharmony_ci JERRY_VLA (char, string_greek_zero_sign, cesu8_sz); 1035425bb815Sopenharmony_ci jerry_string_to_char_buffer (args[1], (jerry_char_t *) string_greek_zero_sign, cesu8_sz); 1036425bb815Sopenharmony_ci 1037425bb815Sopenharmony_ci TEST_ASSERT (!strncmp (string_greek_zero_sign, "\xed\xa0\x80\xed\xb6\x8a", cesu8_sz)); 1038425bb815Sopenharmony_ci TEST_ASSERT (cesu8_length == 2); 1039425bb815Sopenharmony_ci TEST_ASSERT (cesu8_sz == 6); 1040425bb815Sopenharmony_ci 1041425bb815Sopenharmony_ci jerry_release_value (args[1]); 1042425bb815Sopenharmony_ci 1043425bb815Sopenharmony_ci { 1044425bb815Sopenharmony_ci /*json parser check*/ 1045425bb815Sopenharmony_ci const char data_check[]="John"; 1046425bb815Sopenharmony_ci jerry_value_t key = jerry_create_string ((const jerry_char_t *) "name"); 1047425bb815Sopenharmony_ci const jerry_char_t data[] = "{\"name\": \"John\", \"age\": 5}"; 1048425bb815Sopenharmony_ci jerry_value_t parsed_json = jerry_json_parse (data, sizeof (data) - 1); 1049425bb815Sopenharmony_ci jerry_value_t has_prop_js = jerry_has_property (parsed_json, key); 1050425bb815Sopenharmony_ci TEST_ASSERT (jerry_get_boolean_value (has_prop_js)); 1051425bb815Sopenharmony_ci jerry_release_value (has_prop_js); 1052425bb815Sopenharmony_ci jerry_value_t parsed_data = jerry_get_property (parsed_json, key); 1053425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_string (parsed_data) == true); 1054425bb815Sopenharmony_ci jerry_size_t buff_size = jerry_get_string_size (parsed_data); 1055425bb815Sopenharmony_ci JERRY_VLA (char, buff, buff_size + 1); 1056425bb815Sopenharmony_ci jerry_string_to_char_buffer (parsed_data, (jerry_char_t *) buff, buff_size); 1057425bb815Sopenharmony_ci buff[buff_size] = '\0'; 1058425bb815Sopenharmony_ci TEST_ASSERT (strcmp (data_check, buff) == false); 1059425bb815Sopenharmony_ci jerry_release_value (parsed_json); 1060425bb815Sopenharmony_ci jerry_release_value (key); 1061425bb815Sopenharmony_ci jerry_release_value (parsed_data); 1062425bb815Sopenharmony_ci } 1063425bb815Sopenharmony_ci 1064425bb815Sopenharmony_ci /*json stringify test*/ 1065425bb815Sopenharmony_ci { 1066425bb815Sopenharmony_ci jerry_value_t obj = jerry_create_object (); 1067425bb815Sopenharmony_ci char check_value[] = "{\"name\":\"John\"}"; 1068425bb815Sopenharmony_ci jerry_value_t key = jerry_create_string ((const jerry_char_t *) "name"); 1069425bb815Sopenharmony_ci jerry_value_t value = jerry_create_string ((const jerry_char_t *) "John"); 1070425bb815Sopenharmony_ci res = jerry_set_property (obj, key, value); 1071425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (res)); 1072425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_boolean (res) && jerry_get_boolean_value (res)); 1073425bb815Sopenharmony_ci jerry_release_value (res); 1074425bb815Sopenharmony_ci jerry_value_t stringified = jerry_json_stringify (obj); 1075425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_string (stringified)); 1076425bb815Sopenharmony_ci jerry_size_t buff_size = jerry_get_string_size (stringified); 1077425bb815Sopenharmony_ci JERRY_VLA (char, buff, buff_size + 1); 1078425bb815Sopenharmony_ci jerry_string_to_char_buffer (stringified, (jerry_char_t *) buff, buff_size); 1079425bb815Sopenharmony_ci buff[buff_size] = '\0'; 1080425bb815Sopenharmony_ci TEST_ASSERT (strcmp ((const char *) check_value, (const char *) buff) == 0); 1081425bb815Sopenharmony_ci jerry_release_value (stringified); 1082425bb815Sopenharmony_ci jerry_release_value (obj); 1083425bb815Sopenharmony_ci jerry_release_value (key); 1084425bb815Sopenharmony_ci jerry_release_value (value); 1085425bb815Sopenharmony_ci } 1086425bb815Sopenharmony_ci jerry_cleanup (); 1087425bb815Sopenharmony_ci free(ctx_p); 1088425bb815Sopenharmony_ci} 1089