int main (int argc, char **argv) { srand ((unsigned) jerry_port_get_current_time ()); if (argc <= 1 || (argc == 2 && (!strcmp ("-h", argv[1]) || !strcmp ("--help", argv[1])))) { print_help (argv[0]); return JERRY_STANDALONE_EXIT_CODE_OK; } jerry_init (JERRY_INIT_EMPTY); jerry_value_t ret_value = jerry_create_undefined (); for (int i = 1; i < argc; i++) { const char *file_name = argv[i]; size_t source_size; const jerry_char_t *source_p = read_file (file_name, &source_size); if (source_p == NULL) { ret_value = jerry_create_error (JERRY_ERROR_COMMON, (jerry_char_t *) ""); break; } else { ret_value = jerry_parse (source_p, source_size, false); if (!jerry_value_has_error_flag (ret_value)) { jerry_value_t func_val = ret_value; ret_value = jerry_run (func_val); jerry_release_value (func_val); } } if (jerry_value_has_error_flag (ret_value)) { break; } jerry_release_value (ret_value); ret_value = jerry_create_undefined (); } int ret_code = JERRY_STANDALONE_EXIT_CODE_OK; if (jerry_value_has_error_flag (ret_value)) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Unhandled exception: Script Error!\n"); ret_code = JERRY_STANDALONE_EXIT_CODE_FAIL; } jerry_release_value (ret_value); jerry_cleanup (); return ret_code; } /* main */
static bool InitJerry() { uint32_t jerry_flag = JERRY_FLAG_ABORT_ON_FAIL; #ifdef ENABLE_JERRY_MEM_STATS jerry_flag |= JERRY_FLAG_MEM_STATS; jerry_flag |= JERRY_FLAG_SHOW_OPCODES; #endif jerry_init(jerry_flag); InitJerryMagicStringEx(); if (!jerry_parse((jerry_api_char_t*)"", 0)) { DLOG("jerry_parse() failed"); return false; } if (jerry_run() != JERRY_COMPLETION_CODE_OK) { DLOG("jerry_run() failed"); return false; } return true; }
/** * Initialize JerryScript. */ static bool InitJerry(Environment* env) { // Set jerry run flags. uint32_t jerry_flag = JERRY_FLAG_EMPTY; if (env->config()->memstat) { jerry_flag |= JERRY_FLAG_MEM_STATS; } if (env->config()->show_opcode) { jerry_flag |= JERRY_FLAG_SHOW_OPCODES; } // Initialize jerry. jerry_init((jerry_flag_t) jerry_flag); // Set magic strings. InitJerryMagicStringEx(); // Do parse and run to generate initial javascript environment. jerry_api_object_t *err_obj_p = NULL; if (!jerry_parse((jerry_api_char_t*)"", 0, &err_obj_p)) { DLOG("jerry_parse() failed"); jerry_api_release_object (err_obj_p); return false; } jerry_api_value_t err_val; if (jerry_run(&err_val) != JERRY_COMPLETION_CODE_OK) { DLOG("jerry_run() failed"); jerry_api_release_value (&err_val); return false; } return true; }
/* * Init available js functions */ static void init_jerry () { jerry_init (JERRY_INIT_EMPTY); /* Create an empty JS object */ jerry_value_t object = jerry_create_object (); jerry_value_t func_obj; jerry_value_t prop_name; func_obj = jerry_create_external_function (set_led); prop_name = jerry_create_string ((const jerry_char_t *) "setLed"); jerry_set_property (object, prop_name, func_obj); jerry_release_value (prop_name); jerry_release_value (func_obj); func_obj = jerry_create_external_function (js_delay); prop_name = jerry_create_string ((const jerry_char_t *) "delay"); jerry_set_property (object, prop_name, func_obj); jerry_release_value (prop_name); jerry_release_value (func_obj); /* Wrap the JS object (not empty anymore) into a jerry api value */ jerry_value_t global_object = jerry_get_global_object (); /* Add the JS object to the global context */ prop_name = jerry_create_string ((const jerry_char_t *) "test"); jerry_set_property (global_object, prop_name, object); jerry_release_value (prop_name); jerry_release_value (object); jerry_release_value (global_object); } /* init_jerry */
void jerry_start () { srand ((unsigned) jerry_port_get_current_time ()); jerry_init (JERRY_INIT_EMPTY); jerry_value_t global_obj_val = jerry_get_global_object (); jerry_value_t print_func_name_val = jerry_create_string ((jerry_char_t *) "print"); print_function = jerry_get_property (global_obj_val, print_func_name_val); jerry_release_value (print_func_name_val); jerry_release_value (global_obj_val); }
int main (void) { jerry_init (JERRY_INIT_EMPTY); native_free_cb_call_count = 0; test_autorelease_val (); jerry_gc (); TEST_ASSERT (native_free_cb_call_count == 1); jerry_cleanup (); } /* main */
int main (int argc, char **argv) { (void) argc; (void) argv; jerry_char_t buffer[256]; jerry_size_t bytes_copied; const jerryx_module_resolver_t *resolver = &jerryx_module_native_resolver; jerry_value_t module_name; jerry_init (JERRY_INIT_EMPTY); /* Attempt to load a non-existing module. */ module_name = jerry_create_string ((jerry_char_t *) "some-unknown-module-name"); jerry_value_t module = jerryx_module_resolve (module_name, &resolver, 1); jerry_release_value (module_name); TEST_ASSERT (jerry_value_has_error_flag (module)); /* Retrieve the error message. */ jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "message"); jerry_value_t prop = jerry_get_property (module, prop_name); /* Assert that the error message is a string with specific contents. */ TEST_ASSERT (jerry_value_is_string (prop)); bytes_copied = jerry_substring_to_utf8_char_buffer (prop, 0, 254, buffer, 256); buffer[bytes_copied] = 0; TEST_ASSERT (!strcmp ((const char *) buffer, "Module not found")); /* Release the error message property name and value. */ jerry_release_value (prop); jerry_release_value (prop_name); /* Retrieve the moduleName property. */ prop_name = jerry_create_string ((const jerry_char_t *) "moduleName"); prop = jerry_get_property (module, prop_name); /* Assert that the moduleName property is a string containing the requested module name. */ TEST_ASSERT (jerry_value_is_string (prop)); bytes_copied = jerry_substring_to_utf8_char_buffer (prop, 0, 254, buffer, 256); buffer[bytes_copied] = 0; TEST_ASSERT (!strcmp ((const char *) buffer, "some-unknown-module-name")); /* Release everything. */ jerry_release_value (prop); jerry_release_value (prop_name); jerry_release_value (module); return 0; } /* main */
void app_start (int, char**) { // set 9600 baud rate for stdout pc.baud (9600); printf ("\r\nJerryScript in mbed\r\n"); printf (" build %s\r\n", jerry_build_date); printf (" hash %s\r\n", jerry_commit_hash); printf (" branch %s\r\n", jerry_branch_name); if (jerry_init () == 0) { minar::Scheduler::postCallback(jerry_loop).period(minar::milliseconds(100)); } }
int main (void) { TEST_INIT (); jerry_init (JERRY_INIT_EMPTY); jerry_value_t object = jerry_create_object (); jerry_value_t prop_name = jerry_create_string_from_utf8 ((jerry_char_t *) "something"); jerry_value_t prop_value = jerry_create_boolean (true); jerry_value_t proto_object = jerry_create_object (); /* Assert that an empty object does not have the property in question */ assert_boolean_and_release (jerry_has_property (object, prop_name), false); assert_boolean_and_release (jerry_has_own_property (object, prop_name), false); assert_boolean_and_release (jerry_set_prototype (object, proto_object), true); /* If the object has a prototype, that still means it doesn't have the property */ assert_boolean_and_release (jerry_has_property (object, prop_name), false); assert_boolean_and_release (jerry_has_own_property (object, prop_name), false); assert_boolean_and_release (jerry_set_property (proto_object, prop_name, prop_value), true); /* After setting the property on the prototype, it must be there, but not on the object */ assert_boolean_and_release (jerry_has_property (object, prop_name), true); assert_boolean_and_release (jerry_has_own_property (object, prop_name), false); TEST_ASSERT (jerry_delete_property (proto_object, prop_name)); assert_boolean_and_release (jerry_set_property (object, prop_name, prop_value), true); /* After relocating the property onto the object, it must be there */ assert_boolean_and_release (jerry_has_property (object, prop_name), true); assert_boolean_and_release (jerry_has_own_property (object, prop_name), true); jerry_release_value (object); jerry_release_value (prop_name); jerry_release_value (prop_value); jerry_release_value (proto_object); jerry_cleanup (); return 0; } /* main */
void main (void) { printf ("Jerry Compilation " __DATE__ " " __TIME__ "\n"); jerry_init (JERRY_INIT_EMPTY); jerry_value_t global_obj_val = jerry_get_global_object (); jerry_value_t print_func_name_val = jerry_create_string ((jerry_char_t *) "print"); print_function = jerry_get_property (global_obj_val, print_func_name_val); jerry_release_value (print_func_name_val); jerry_release_value (global_obj_val); if (jerry_value_has_error_flag (print_function)) { printf ("Error: could not look up print function, expression results won't be printed\n"); } shell_register_app_cmd_handler (shell_cmd_handler); shell_init ("js> ", commands); /* Don't call jerry_cleanup() here, as shell_init() returns after setting up background task to process shell input, and that task calls shell_cmd_handler(), etc. as callbacks. This processing happens in the infinite loop, so JerryScript doesn't need to be de-initialized. */ } /* main */
void main (void) { srand ((unsigned) jerry_port_get_current_time ()); uint32_t zephyr_ver = sys_kernel_version_get (); printf ("JerryScript build: " __DATE__ " " __TIME__ "\n"); printf ("JerryScript API %d.%d\n", JERRY_API_MAJOR_VERSION, JERRY_API_MINOR_VERSION); printf ("Zephyr version %d.%d.%d\n", (int)SYS_KERNEL_VER_MAJOR (zephyr_ver), (int)SYS_KERNEL_VER_MINOR (zephyr_ver), (int)SYS_KERNEL_VER_PATCHLEVEL (zephyr_ver)); zephyr_getline_init (); jerry_init (JERRY_INIT_EMPTY); register_js_function ("print", jerryx_handler_print); jerry_value_t global_obj_val = jerry_get_global_object (); jerry_value_t print_func_name_val = jerry_create_string ((jerry_char_t *) "print"); print_function = jerry_get_property (global_obj_val, print_func_name_val); jerry_release_value (print_func_name_val); jerry_release_value (global_obj_val); if (jerry_value_is_error (print_function)) { printf ("Error: could not look up print function, expression results won't be printed\n"); } while (1) { char *s; printf("js> "); fflush(stdout); s = zephyr_getline (); if (*s) { shell_cmd_handler (s); } } /* As we never retturn from REPL above, don't call jerry_cleanup() here. */ } /* main */
int main (void) { TEST_INIT (); jerry_init (JERRY_INIT_EMPTY); const jerry_char_t test_eval_function[] = "function demo(a) { return a + 1; }; demo"; test_entry_t entries[] = { ENTRY (JERRY_TYPE_NUMBER, jerry_create_number (-33.0)), ENTRY (JERRY_TYPE_NUMBER, jerry_create_number (3)), ENTRY (JERRY_TYPE_NUMBER, jerry_create_number_nan ()), ENTRY (JERRY_TYPE_NUMBER, jerry_create_number_infinity (false)), ENTRY (JERRY_TYPE_NUMBER, jerry_create_number_infinity (true)), ENTRY (JERRY_TYPE_BOOLEAN, jerry_create_boolean (true)), ENTRY (JERRY_TYPE_BOOLEAN, jerry_create_boolean (false)), ENTRY (JERRY_TYPE_UNDEFINED, jerry_create_undefined ()), ENTRY (JERRY_TYPE_OBJECT, jerry_create_object ()), ENTRY (JERRY_TYPE_OBJECT, jerry_create_array (10)), ENTRY (JERRY_TYPE_ERROR, jerry_create_error (JERRY_ERROR_TYPE, (const jerry_char_t *) "error")), ENTRY (JERRY_TYPE_NULL, jerry_create_null ()), ENTRY (JERRY_TYPE_FUNCTION, jerry_eval (test_eval_function, sizeof (test_eval_function) - 1, JERRY_PARSE_NO_OPTS)), ENTRY (JERRY_TYPE_FUNCTION, jerry_create_external_function (test_ext_function)), ENTRY (JERRY_TYPE_STRING, jerry_create_string (test_eval_function)), ENTRY (JERRY_TYPE_STRING, jerry_create_string ((jerry_char_t *) "")), }; for (size_t idx = 0; idx < sizeof (entries) / sizeof (entries[0]); idx++) { jerry_type_t type_info = jerry_value_get_type (entries[idx].value); TEST_ASSERT (type_info != JERRY_TYPE_NONE); TEST_ASSERT (type_info == entries[idx].type_info); jerry_release_value (entries[idx].value); } if (jerry_is_feature_enabled (JERRY_FEATURE_SYMBOL)) { jerry_value_t symbol_desc_value = jerry_create_string ((jerry_char_t *) "foo"); jerry_value_t symbol_value = jerry_create_symbol (symbol_desc_value); jerry_type_t type_info = jerry_value_get_type (symbol_value); TEST_ASSERT (type_info != JERRY_TYPE_NONE); TEST_ASSERT (type_info == JERRY_TYPE_SYMBOL); jerry_release_value (symbol_value); jerry_release_value (symbol_desc_value); } jerry_cleanup (); return 0; } /* main */
/** * Process 'generate' command. * * @return error code (0 - no error) */ static int process_generate (cli_state_t *cli_state_p, /**< cli state */ int argc, /**< number of arguments */ char *prog_name_p) /**< program name */ { (void) argc; bool is_save_literals_mode_in_c_format = false; uint32_t snapshot_flags = 0; jerry_init_flag_t flags = JERRY_INIT_EMPTY; const char *file_name_p = NULL; uint8_t *source_p = input_buffer; size_t source_length = 0; const char *save_literals_file_name_p = NULL; cli_change_opts (cli_state_p, generate_opts); for (int id = cli_consume_option (cli_state_p); id != CLI_OPT_END; id = cli_consume_option (cli_state_p)) { switch (id) { case OPT_GENERATE_HELP: { cli_help (prog_name_p, "generate", generate_opts); return JERRY_STANDALONE_EXIT_CODE_OK; } case OPT_GENERATE_STATIC: { snapshot_flags |= JERRY_SNAPSHOT_SAVE_STATIC; break; } case OPT_GENERATE_LITERAL_LIST: case OPT_GENERATE_LITERAL_C: { if (save_literals_file_name_p != NULL) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: literal file name already specified"); return JERRY_STANDALONE_EXIT_CODE_FAIL; } is_save_literals_mode_in_c_format = (id == OPT_GENERATE_LITERAL_C); save_literals_file_name_p = cli_consume_string (cli_state_p); break; } case OPT_GENERATE_SHOW_OP: { if (check_feature (JERRY_FEATURE_PARSER_DUMP, cli_state_p->arg)) { jerry_port_default_set_log_level (JERRY_LOG_LEVEL_DEBUG); flags |= JERRY_INIT_SHOW_OPCODES; } break; } case OPT_GENERATE_OUT: { output_file_name_p = cli_consume_string (cli_state_p); break; } case CLI_OPT_DEFAULT: { if (file_name_p != NULL) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: Exactly one input file must be specified\n"); return JERRY_STANDALONE_EXIT_CODE_FAIL; } file_name_p = cli_consume_string (cli_state_p); if (cli_state_p->error == NULL) { source_length = read_file (source_p, file_name_p); if (source_length == 0) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Input file is empty\n"); return JERRY_STANDALONE_EXIT_CODE_FAIL; } } break; } default: { cli_state_p->error = "Internal error"; break; } } } if (check_cli_error (cli_state_p)) { return JERRY_STANDALONE_EXIT_CODE_FAIL; } if (file_name_p == NULL) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: Exactly one input file must be specified\n"); return JERRY_STANDALONE_EXIT_CODE_FAIL; } jerry_init (flags); if (!jerry_is_valid_utf8_string (source_p, (jerry_size_t) source_length)) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: Input must be a valid UTF-8 string.\n"); return JERRY_STANDALONE_EXIT_CODE_FAIL; } jerry_value_t snapshot_result; snapshot_result = jerry_generate_snapshot ((jerry_char_t *) file_name_p, (size_t) strlen (file_name_p), (jerry_char_t *) source_p, source_length, snapshot_flags, output_buffer, sizeof (output_buffer) / sizeof (uint32_t)); if (jerry_value_has_error_flag (snapshot_result)) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: Generating snapshot failed!\n"); jerry_value_clear_error_flag (&snapshot_result); print_unhandled_exception (snapshot_result); jerry_release_value (snapshot_result); return JERRY_STANDALONE_EXIT_CODE_FAIL; } size_t snapshot_size = (size_t) jerry_get_number_value (snapshot_result); jerry_release_value (snapshot_result); FILE *snapshot_file_p = fopen (output_file_name_p, "w"); if (snapshot_file_p == NULL) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: Unable to write snapshot file: '%s'\n", output_file_name_p); return JERRY_STANDALONE_EXIT_CODE_FAIL; } fwrite (output_buffer, sizeof (uint8_t), snapshot_size, snapshot_file_p); fclose (snapshot_file_p); printf ("Created snapshot file: '%s' (%lu bytes)\n", output_file_name_p, (unsigned long) snapshot_size); if (save_literals_file_name_p != NULL) { const size_t literal_buffer_size = jerry_parse_and_save_literals ((jerry_char_t *) source_p, source_length, false, output_buffer, sizeof (output_buffer) / sizeof (uint32_t), is_save_literals_mode_in_c_format); if (literal_buffer_size == 0) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: Literal saving failed!\n"); return JERRY_STANDALONE_EXIT_CODE_FAIL; } FILE *literal_file_p = fopen (save_literals_file_name_p, "w"); if (literal_file_p == NULL) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: Unable to write literal file: '%s'\n", save_literals_file_name_p); return JERRY_STANDALONE_EXIT_CODE_FAIL; } fwrite (output_buffer, sizeof (uint8_t), literal_buffer_size, literal_file_p); fclose (literal_file_p); printf ("Created literal file: '%s' (%lu bytes)\n", save_literals_file_name_p, (unsigned long) literal_buffer_size); } return 0; } /* process_generate */
int main (void) { jerry_init (JERRY_INIT_EMPTY); /* Render strict-equal as a function. */ jerry_value_t parse_result = jerry_parse (NULL, 0, strict_equal_source, sizeof (strict_equal_source) - 1, JERRY_PARSE_STRICT_MODE); TEST_ASSERT (!jerry_value_is_error (parse_result)); jerry_value_t strict_equal = jerry_run (parse_result); TEST_ASSERT (!jerry_value_is_error (strict_equal)); jerry_release_value (parse_result); /* Create an object and associate some native data with it. */ jerry_value_t object = jerry_create_object (); jerry_set_object_native_pointer (object, &test_data, &test_info); /* Retrieve the object by its native pointer. */ jerry_value_t found_object; TEST_ASSERT (jerry_objects_foreach_by_native_info (&test_info, find_test_object_by_data, &found_object)); jerry_value_t args[2] = {object, found_object}; /* Assert that the correct object was retrieved. */ jerry_value_t undefined = jerry_create_undefined (); jerry_value_t strict_equal_result = jerry_call_function (strict_equal, undefined, args, 2); TEST_ASSERT (jerry_value_is_boolean (strict_equal_result) && jerry_get_boolean_value (strict_equal_result)); jerry_release_value (strict_equal_result); jerry_release_value (found_object); jerry_release_value (object); /* Collect garbage. */ jerry_gc (JERRY_GC_SEVERITY_LOW); /* Attempt to retrieve the object by its native pointer again. */ TEST_ASSERT (!jerry_objects_foreach_by_native_info (&test_info, find_test_object_by_data, &found_object)); /* Create an object and set a property on it. */ object = jerry_create_object (); jerry_value_t property_name = jerry_create_string ((jerry_char_t *) "xyzzy"); jerry_value_t property_value = jerry_create_number (42); jerry_release_value (jerry_set_property (object, property_name, property_value)); jerry_release_value (property_value); /* Retrieve the object by the presence of its property, placing it at args[1]. */ args[0] = property_name; TEST_ASSERT (jerry_objects_foreach (find_test_object_by_property, args)); /* Assert that the right object was retrieved and release both the original reference to it and the retrieved one. */ args[0] = object; strict_equal_result = jerry_call_function (strict_equal, undefined, args, 2); TEST_ASSERT (jerry_value_is_boolean (strict_equal_result) && jerry_get_boolean_value (strict_equal_result)); jerry_release_value (strict_equal_result); jerry_release_value (args[0]); jerry_release_value (args[1]); /* Collect garbage. */ jerry_gc (JERRY_GC_SEVERITY_LOW); /* Attempt to retrieve the object by the presence of its property again. */ args[0] = property_name; TEST_ASSERT (!jerry_objects_foreach (find_test_object_by_property, args)); jerry_release_value (property_name); jerry_release_value (undefined); jerry_release_value (strict_equal); jerry_cleanup (); } /* main */
/** * Process 'merge' command. * * @return error code (0 - no error) */ static int process_merge (cli_state_t *cli_state_p, /**< cli state */ int argc, /**< number of arguments */ char *prog_name_p) /**< program name */ { jerry_init (JERRY_INIT_EMPTY); uint8_t *input_pos_p = input_buffer; cli_change_opts (cli_state_p, merge_opts); const uint32_t *merge_buffers[argc]; size_t merge_buffer_sizes[argc]; uint32_t number_of_files = 0; for (int id = cli_consume_option (cli_state_p); id != CLI_OPT_END; id = cli_consume_option (cli_state_p)) { switch (id) { case OPT_MERGE_HELP: { cli_help (prog_name_p, "merge", merge_opts); return JERRY_STANDALONE_EXIT_CODE_OK; } case OPT_MERGE_OUT: { output_file_name_p = cli_consume_string (cli_state_p); break; } case CLI_OPT_DEFAULT: { const char *file_name_p = cli_consume_string (cli_state_p); if (cli_state_p->error == NULL) { size_t size = read_file (input_pos_p, file_name_p); if (size == 0) { return JERRY_STANDALONE_EXIT_CODE_FAIL; } merge_buffers[number_of_files] = (const uint32_t *) input_pos_p; merge_buffer_sizes[number_of_files] = size; number_of_files++; const uintptr_t mask = sizeof (uint32_t) - 1; input_pos_p = (uint8_t *) ((((uintptr_t) input_pos_p) + size + mask) & ~mask); } break; } default: { cli_state_p->error = "Internal error"; break; } } } if (check_cli_error (cli_state_p)) { return JERRY_STANDALONE_EXIT_CODE_FAIL; } if (number_of_files < 2) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: at least two input files must be passed.\n"); return JERRY_STANDALONE_EXIT_CODE_FAIL; } const char *error_p; size_t size = jerry_merge_snapshots (merge_buffers, merge_buffer_sizes, number_of_files, output_buffer, JERRY_BUFFER_SIZE, &error_p); if (size == 0) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: %s\n", error_p); return JERRY_STANDALONE_EXIT_CODE_FAIL; } FILE *file_p = fopen (output_file_name_p, "w"); if (file_p != NULL) { fwrite (output_buffer, 1u, size, file_p); fclose (file_p); } else { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: cannot open file: '%s'\n", output_file_name_p); } return JERRY_STANDALONE_EXIT_CODE_OK; } /* process_merge */
int main (void) { TEST_INIT (); jerry_init (JERRY_INIT_EMPTY); jerry_value_t global = jerry_get_global_object (); jerry_value_t callback_name = jerry_create_string ((jerry_char_t *) "callback"); jerry_value_t func = jerry_create_external_function (callback_func); jerry_value_t res = jerry_set_property (global, callback_name, func); TEST_ASSERT (!jerry_value_is_error (res)); jerry_release_value (res); jerry_release_value (func); jerry_release_value (callback_name); jerry_release_value (global); const char *inf_loop_code_src_p = ("while(true) {\n" " with ({}) {\n" " try {\n" " callback();\n" " } catch (e) {\n" " } finally {\n" " }\n" " }\n" "}"); jerry_value_t parsed_code_val = jerry_parse (NULL, 0, (jerry_char_t *) inf_loop_code_src_p, strlen (inf_loop_code_src_p), JERRY_PARSE_NO_OPTS); TEST_ASSERT (!jerry_value_is_error (parsed_code_val)); res = jerry_run (parsed_code_val); TEST_ASSERT (jerry_value_is_abort (res)); jerry_release_value (res); jerry_release_value (parsed_code_val); inf_loop_code_src_p = ("function f() {" " while(true) {\n" " with ({}) {\n" " try {\n" " callback();\n" " } catch (e) {\n" " } finally {\n" " }\n" " }\n" " }" "}\n" "function g() {\n" " for (a in { x:5 })\n" " f();\n" "}\n" "\n" "with({})\n" " f();\n"); parsed_code_val = jerry_parse (NULL, 0, (jerry_char_t *) inf_loop_code_src_p, strlen (inf_loop_code_src_p), JERRY_PARSE_NO_OPTS); TEST_ASSERT (!jerry_value_is_error (parsed_code_val)); res = jerry_run (parsed_code_val); TEST_ASSERT (jerry_value_is_abort (res)); jerry_release_value (res); jerry_release_value (parsed_code_val); /* Test flag overwrites. */ jerry_value_t value = jerry_create_string ((jerry_char_t *) "Error description"); TEST_ASSERT (!jerry_value_is_abort (value)); TEST_ASSERT (!jerry_value_is_error (value)); jerry_value_set_abort_flag (&value); TEST_ASSERT (jerry_value_is_abort (value)); TEST_ASSERT (jerry_value_is_error (value)); jerry_value_set_error_flag (&value); TEST_ASSERT (!jerry_value_is_abort (value)); TEST_ASSERT (jerry_value_is_error (value)); jerry_value_set_abort_flag (&value); TEST_ASSERT (jerry_value_is_abort (value)); TEST_ASSERT (jerry_value_is_error (value)); jerry_release_value (value); jerry_cleanup (); return 0; } /* main */
int main (void) { TEST_INIT (); jerry_init (JERRY_FLAG_EMPTY); bool is_ok, is_exception; ssize_t sz; jerry_api_value_t val_t, val_foo, val_bar, val_A, val_A_prototype, val_a, val_a_foo, val_value_field; jerry_api_value_t val_external, val_external_construct, val_call_external; jerry_api_object_t* global_obj_p, *obj_p; jerry_api_object_t* external_func_p, *external_construct_p; jerry_api_object_t* throw_test_handler_p; jerry_api_value_t res, args[2]; char buffer[32]; is_ok = jerry_parse ((jerry_api_char_t *)test_source, strlen (test_source)); JERRY_ASSERT (is_ok); is_ok = (jerry_run () == JERRY_COMPLETION_CODE_OK); JERRY_ASSERT (is_ok); global_obj_p = jerry_api_get_global (); // Test corner case for jerry_api_string_to_char_buffer test_api_init_api_value_string (&args[0], ""); sz = jerry_api_string_to_char_buffer (args[0].v_string, NULL, 0); JERRY_ASSERT (sz == 0); jerry_api_release_value (&args[0]); // Get global.t is_ok = jerry_api_get_object_field_value (global_obj_p, (jerry_api_char_t *)"t", &val_t); JERRY_ASSERT (is_ok && val_t.type == JERRY_API_DATA_TYPE_FLOAT64 && val_t.v_float64 == 1.0); jerry_api_release_value (&val_t); // Get global.foo is_ok = jerry_api_get_object_field_value (global_obj_p, (jerry_api_char_t *)"foo", &val_foo); JERRY_ASSERT (is_ok && val_foo.type == JERRY_API_DATA_TYPE_OBJECT); // Call foo (4, 2) test_api_init_api_value_float64 (&args[0], 4); test_api_init_api_value_float64 (&args[1], 2); is_ok = jerry_api_call_function (val_foo.v_object, NULL, &res, args, 2); JERRY_ASSERT (is_ok && res.type == JERRY_API_DATA_TYPE_FLOAT64 && res.v_float64 == 1.0); jerry_api_release_value (&res); // Get global.bar is_ok = jerry_api_get_object_field_value (global_obj_p, (jerry_api_char_t *)"bar", &val_bar); JERRY_ASSERT (is_ok && val_bar.type == JERRY_API_DATA_TYPE_OBJECT); // Call bar (4, 2) is_ok = jerry_api_call_function (val_bar.v_object, NULL, &res, args, 2); JERRY_ASSERT (is_ok && res.type == JERRY_API_DATA_TYPE_FLOAT64 && res.v_float64 == 5.0); jerry_api_release_value (&res); jerry_api_release_value (&val_bar); // Set global.t = "abcd" test_api_init_api_value_string (&args[0], "abcd"); is_ok = jerry_api_set_object_field_value (global_obj_p, (jerry_api_char_t *)"t", &args[0]); JERRY_ASSERT (is_ok); jerry_api_release_value (&args[0]); // Call foo (4, 2) is_ok = jerry_api_call_function (val_foo.v_object, NULL, &res, args, 2); JERRY_ASSERT (is_ok && res.type == JERRY_API_DATA_TYPE_STRING); sz = jerry_api_string_to_char_buffer (res.v_string, NULL, 0); JERRY_ASSERT (sz == -4); sz = jerry_api_string_to_char_buffer (res.v_string, (jerry_api_char_t *) buffer, -sz); JERRY_ASSERT (sz == 4); jerry_api_release_value (&res); JERRY_ASSERT (!strncmp (buffer, "abcd", (size_t) sz)); // Get global.A is_ok = jerry_api_get_object_field_value (global_obj_p, (jerry_api_char_t *)"A", &val_A); JERRY_ASSERT (is_ok && val_A.type == JERRY_API_DATA_TYPE_OBJECT); // Get A.prototype is_ok = jerry_api_is_constructor (val_A.v_object); JERRY_ASSERT (is_ok); is_ok = jerry_api_get_object_field_value (val_A.v_object, (jerry_api_char_t *) "prototype", &val_A_prototype); JERRY_ASSERT (is_ok && val_A_prototype.type == JERRY_API_DATA_TYPE_OBJECT); jerry_api_release_value (&val_A); // Set A.prototype.foo = global.foo is_ok = jerry_api_set_object_field_value (val_A_prototype.v_object, (jerry_api_char_t *) "foo", &val_foo); JERRY_ASSERT (is_ok); jerry_api_release_value (&val_A_prototype); jerry_api_release_value (&val_foo); // Get global.a is_ok = jerry_api_get_object_field_value (global_obj_p, (jerry_api_char_t *) "a", &val_a); JERRY_ASSERT (is_ok && val_a.type == JERRY_API_DATA_TYPE_OBJECT); // Get a.t is_ok = jerry_api_get_object_field_value (val_a.v_object, (jerry_api_char_t *) "t", &res); JERRY_ASSERT (is_ok && res.type == JERRY_API_DATA_TYPE_FLOAT64 && res.v_float64 == 12.0); jerry_api_release_value (&res); // Get a.foo is_ok = jerry_api_get_object_field_value (val_a.v_object, (jerry_api_char_t *) "foo", &val_a_foo); JERRY_ASSERT (is_ok && val_a_foo.type == JERRY_API_DATA_TYPE_OBJECT); // Call a.foo () is_ok = jerry_api_call_function (val_a_foo.v_object, val_a.v_object, &res, NULL, 0); JERRY_ASSERT (is_ok && res.type == JERRY_API_DATA_TYPE_FLOAT64 && res.v_float64 == 12.0); jerry_api_release_value (&res); jerry_api_release_value (&val_a_foo); jerry_api_release_value (&val_a); // Create native handler bound function object and set it to 'external' variable external_func_p = jerry_api_create_external_function (handler); JERRY_ASSERT (external_func_p != NULL && jerry_api_is_function (external_func_p) && jerry_api_is_constructor (external_func_p)); test_api_init_api_value_object (&val_external, external_func_p); is_ok = jerry_api_set_object_field_value (global_obj_p, (jerry_api_char_t *) "external", &val_external); JERRY_ASSERT (is_ok); jerry_api_release_value (&val_external); jerry_api_release_object (external_func_p); // Call 'call_external' function that should call external function created above is_ok = jerry_api_get_object_field_value (global_obj_p, (jerry_api_char_t *) "call_external", &val_call_external); JERRY_ASSERT (is_ok && val_call_external.type == JERRY_API_DATA_TYPE_OBJECT); is_ok = jerry_api_call_function (val_call_external.v_object, global_obj_p, &res, NULL, 0); jerry_api_release_value (&val_call_external); JERRY_ASSERT (is_ok && res.type == JERRY_API_DATA_TYPE_STRING); sz = jerry_api_string_to_char_buffer (res.v_string, NULL, 0); JERRY_ASSERT (sz == -19); sz = jerry_api_string_to_char_buffer (res.v_string, (jerry_api_char_t *) buffer, -sz); JERRY_ASSERT (sz == 19); jerry_api_release_value (&res); JERRY_ASSERT (!strncmp (buffer, "string from handler", (size_t) sz)); // Create native handler bound function object and set it to 'external_construct' variable external_construct_p = jerry_api_create_external_function (handler_construct); JERRY_ASSERT (external_construct_p != NULL && jerry_api_is_function (external_construct_p) && jerry_api_is_constructor (external_construct_p)); test_api_init_api_value_object (&val_external_construct, external_construct_p); is_ok = jerry_api_set_object_field_value (global_obj_p, (jerry_api_char_t *) "external_construct", &val_external_construct); JERRY_ASSERT (is_ok); jerry_api_release_value (&val_external_construct); jerry_api_release_object (external_construct_p); // Call external function created above, as constructor test_api_init_api_value_bool (&args[0], true); is_ok = jerry_api_construct_object (external_construct_p, &res, args, 1); JERRY_ASSERT (is_ok && res.type == JERRY_API_DATA_TYPE_OBJECT); is_ok = jerry_api_get_object_field_value (res.v_object, (jerry_api_char_t *)"value_field", &val_value_field); // Get 'value_field' of constructed object JERRY_ASSERT (is_ok && val_value_field.type == JERRY_API_DATA_TYPE_BOOLEAN && val_value_field.v_bool == true); jerry_api_release_value (&val_value_field); uintptr_t ptr; is_ok = jerry_api_get_object_native_handle (res.v_object, &ptr); JERRY_ASSERT (is_ok && ptr == (uintptr_t) 0x0012345678abcdefull); jerry_api_release_value (&res); // Test: Throwing exception from native handler. throw_test_handler_p = jerry_api_create_external_function (handler_throw_test); JERRY_ASSERT (throw_test_handler_p != NULL && jerry_api_is_function (throw_test_handler_p)); test_api_init_api_value_object (&val_t, throw_test_handler_p); is_ok = jerry_api_set_object_field_value (global_obj_p, (jerry_api_char_t *) "throw_test", &val_t); JERRY_ASSERT (is_ok); jerry_api_release_value (&val_t); jerry_api_release_object (throw_test_handler_p); is_ok = jerry_api_get_object_field_value (global_obj_p, (jerry_api_char_t *) "call_throw_test", &val_t); JERRY_ASSERT (is_ok && val_t.type == JERRY_API_DATA_TYPE_OBJECT); is_ok = jerry_api_call_function (val_t.v_object, global_obj_p, &res, NULL, 0); JERRY_ASSERT (is_ok); jerry_api_release_value (&val_t); jerry_api_release_value (&res); // Test: Unhandled exception in called function is_ok = jerry_api_get_object_field_value (global_obj_p, (jerry_api_char_t *) "throw_reference_error", &val_t); JERRY_ASSERT (is_ok && val_t.type == JERRY_API_DATA_TYPE_OBJECT); is_ok = jerry_api_call_function (val_t.v_object, global_obj_p, &res, NULL, 0); is_exception = !is_ok; JERRY_ASSERT (is_exception); jerry_api_release_value (&val_t); // 'res' should contain exception object JERRY_ASSERT (res.type == JERRY_API_DATA_TYPE_OBJECT); jerry_api_release_value (&res); // Test: Call of non-function obj_p = jerry_api_create_object (); is_ok = jerry_api_call_function (obj_p, global_obj_p, &res, NULL, 0); is_exception = !is_ok; JERRY_ASSERT (is_exception); // 'res' should contain exception object JERRY_ASSERT (res.type == JERRY_API_DATA_TYPE_OBJECT); jerry_api_release_value (&res); jerry_api_release_object (obj_p); // Test: Unhandled exception in function called, as constructor is_ok = jerry_api_get_object_field_value (global_obj_p, (jerry_api_char_t *) "throw_reference_error", &val_t); JERRY_ASSERT (is_ok && val_t.type == JERRY_API_DATA_TYPE_OBJECT); is_ok = jerry_api_construct_object (val_t.v_object, &res, NULL, 0); is_exception = !is_ok; JERRY_ASSERT (is_exception); jerry_api_release_value (&val_t); // 'res' should contain exception object JERRY_ASSERT (res.type == JERRY_API_DATA_TYPE_OBJECT); jerry_api_release_value (&res); // Test: Call of non-function as constructor obj_p = jerry_api_create_object (); is_ok = jerry_api_construct_object (obj_p, &res, NULL, 0); is_exception = !is_ok; JERRY_ASSERT (is_exception); // 'res' should contain exception object JERRY_ASSERT (res.type == JERRY_API_DATA_TYPE_OBJECT); jerry_api_release_value (&res); jerry_api_release_object (obj_p); // Test: Array Object API jerry_api_object_t *array_obj_p = jerry_api_create_array_object (10); jerry_api_value_t v_in; test_api_init_api_value_float64 (&v_in, 10.5); jerry_api_set_array_index_value (array_obj_p, 5, &v_in); jerry_api_value_t v_out; jerry_api_get_array_index_value (array_obj_p, 5, &v_out); JERRY_ASSERT (v_out.type == JERRY_API_DATA_TYPE_FLOAT64 && v_out.v_float64 == 10.5); jerry_api_release_object (array_obj_p); // Test: eval const char *eval_code_src_p = "(function () { return 123; })"; jerry_completion_code_t status = jerry_api_eval ((jerry_api_char_t *) eval_code_src_p, strlen (eval_code_src_p), false, true, &val_t); JERRY_ASSERT (status == JERRY_COMPLETION_CODE_OK); JERRY_ASSERT (val_t.type == JERRY_API_DATA_TYPE_OBJECT); JERRY_ASSERT (jerry_api_is_function (val_t.v_object)); is_ok = jerry_api_call_function (val_t.v_object, NULL, &res, NULL, 0); JERRY_ASSERT (is_ok); JERRY_ASSERT (res.type == JERRY_API_DATA_TYPE_FLOAT64 && res.v_float64 == 123.0); jerry_api_release_value (&res); jerry_api_release_value (&val_t); // cleanup. jerry_api_release_object (global_obj_p); jerry_cleanup (); JERRY_ASSERT (test_api_is_free_callback_was_called); // External Magic String jerry_init (JERRY_FLAG_SHOW_OPCODES); uint32_t num_magic_string_items = (uint32_t) (sizeof (magic_string_items) / sizeof (jerry_api_char_ptr_t)); jerry_register_external_magic_strings (magic_string_items, num_magic_string_items, magic_string_lengths); const char *ms_code_src_p = "var global = {}; var console = [1]; var process = 1;"; is_ok = jerry_parse ((jerry_api_char_t *) ms_code_src_p, strlen (ms_code_src_p)); JERRY_ASSERT (is_ok); is_ok = (jerry_run () == JERRY_COMPLETION_CODE_OK); JERRY_ASSERT (is_ok); jerry_cleanup (); // Dump / execute snapshot static uint8_t global_mode_snapshot_buffer[1024]; static uint8_t eval_mode_snapshot_buffer[1024]; const char *code_to_snapshot_p = "(function () { return 'string from snapshot'; }) ();"; jerry_init (JERRY_FLAG_SHOW_OPCODES); size_t global_mode_snapshot_size = jerry_parse_and_save_snapshot ((jerry_api_char_t *) code_to_snapshot_p, strlen (code_to_snapshot_p), true, global_mode_snapshot_buffer, sizeof (global_mode_snapshot_buffer)); JERRY_ASSERT (global_mode_snapshot_size != 0); jerry_cleanup (); jerry_init (JERRY_FLAG_SHOW_OPCODES); size_t eval_mode_snapshot_size = jerry_parse_and_save_snapshot ((jerry_api_char_t *) code_to_snapshot_p, strlen (code_to_snapshot_p), false, eval_mode_snapshot_buffer, sizeof (eval_mode_snapshot_buffer)); JERRY_ASSERT (eval_mode_snapshot_size != 0); jerry_cleanup (); jerry_init (JERRY_FLAG_SHOW_OPCODES); is_ok = (jerry_exec_snapshot (global_mode_snapshot_buffer, global_mode_snapshot_size, false, &res) == JERRY_COMPLETION_CODE_OK); JERRY_ASSERT (is_ok && res.type == JERRY_API_DATA_TYPE_UNDEFINED); jerry_api_release_value (&res); is_ok = (jerry_exec_snapshot (eval_mode_snapshot_buffer, eval_mode_snapshot_size, false, &res) == JERRY_COMPLETION_CODE_OK); JERRY_ASSERT (is_ok && res.type == JERRY_API_DATA_TYPE_STRING); sz = jerry_api_string_to_char_buffer (res.v_string, NULL, 0); JERRY_ASSERT (sz == -20); sz = jerry_api_string_to_char_buffer (res.v_string, (jerry_api_char_t *) buffer, -sz); JERRY_ASSERT (sz == 20); jerry_api_release_value (&res); JERRY_ASSERT (!strncmp (buffer, "string from snapshot", (size_t) sz)); jerry_cleanup (); return 0; }
int main (void) { jerry_init (JERRY_INIT_EMPTY); if (!jerry_is_feature_enabled (JERRY_FEATURE_TYPEDARRAY)) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "ArrayBuffer is disabled!\n"); jerry_cleanup (); return 0; } jerry_value_t function_val = jerry_create_external_function (assert_handler); register_js_value ("assert", function_val); jerry_release_value (function_val); /* Test array buffer queries */ { const char *eval_arraybuffer_src_p = "new ArrayBuffer (10)"; jerry_value_t eval_arraybuffer = jerry_eval ((jerry_char_t *) eval_arraybuffer_src_p, strlen (eval_arraybuffer_src_p), true); TEST_ASSERT (!jerry_value_is_error (eval_arraybuffer)); TEST_ASSERT (jerry_value_is_arraybuffer (eval_arraybuffer)); TEST_ASSERT (jerry_get_arraybuffer_byte_length (eval_arraybuffer) == 10); jerry_release_value (eval_arraybuffer); } /* Test array buffer creation */ { const uint32_t length = 15; jerry_value_t arraybuffer = jerry_create_arraybuffer (length); TEST_ASSERT (!jerry_value_is_error (arraybuffer)); TEST_ASSERT (jerry_value_is_arraybuffer (arraybuffer)); TEST_ASSERT (jerry_get_arraybuffer_byte_length (arraybuffer) == length); jerry_release_value (arraybuffer); } /* Test array buffer read operations */ for (uint8_t i = 0; i < 15; i++) { test_read_with_offset (i); } /* Test zero length ArrayBuffer read */ { const uint32_t length = 0; jerry_value_t arraybuffer = jerry_create_arraybuffer (length); TEST_ASSERT (!jerry_value_is_error (arraybuffer)); TEST_ASSERT (jerry_value_is_arraybuffer (arraybuffer)); TEST_ASSERT (jerry_get_arraybuffer_byte_length (arraybuffer) == length); uint8_t data[20]; memset (data, 11, 20); jerry_length_t bytes_read = jerry_arraybuffer_read (arraybuffer, 0, data, 20); TEST_ASSERT (bytes_read == 0); for (int i = 0; i < 20; i++) { TEST_ASSERT (data[i] == 11); } jerry_release_value (arraybuffer); } /* Test array buffer write operations */ for (uint8_t i = 0; i < 15; i++) { test_write_with_offset (i); } /* Test zero length ArrayBuffer write */ { const uint32_t length = 0; jerry_value_t arraybuffer = jerry_create_arraybuffer (length); TEST_ASSERT (!jerry_value_is_error (arraybuffer)); TEST_ASSERT (jerry_value_is_arraybuffer (arraybuffer)); TEST_ASSERT (jerry_get_arraybuffer_byte_length (arraybuffer) == length); uint8_t data[20]; memset (data, 11, 20); jerry_length_t bytes_written = jerry_arraybuffer_write (arraybuffer, 0, data, 20); TEST_ASSERT (bytes_written == 0); jerry_release_value (arraybuffer); } /* Test ArrayBuffer with buffer allocated externally */ { const uint32_t buffer_size = 15; const uint8_t base_value = 51; uint8_t buffer_p[buffer_size]; memset (buffer_p, base_value, buffer_size); jerry_value_t arrayb = jerry_create_arraybuffer_external (buffer_size, buffer_p, test_free_cb); uint8_t new_value = 123; jerry_length_t copied = jerry_arraybuffer_write (arrayb, 0, &new_value, 1); TEST_ASSERT (copied == 1); TEST_ASSERT (buffer_p[0] == new_value); TEST_ASSERT (jerry_get_arraybuffer_byte_length (arrayb) == buffer_size); for (uint32_t i = 1; i < buffer_size; i++) { TEST_ASSERT (buffer_p[i] == base_value); } uint8_t test_buffer[buffer_size]; jerry_length_t read = jerry_arraybuffer_read (arrayb, 0, test_buffer, buffer_size); TEST_ASSERT (read == buffer_size); TEST_ASSERT (test_buffer[0] == new_value); for (uint32_t i = 1; i < buffer_size; i++) { TEST_ASSERT (test_buffer[i] == base_value); } TEST_ASSERT (jerry_value_is_arraybuffer (arrayb)); jerry_release_value (arrayb); } /* Test ArrayBuffer external memory map/unmap */ { const uint32_t buffer_size = 20; uint8_t buffer_p[buffer_size]; { jerry_value_t input_buffer = jerry_create_arraybuffer_external (buffer_size, buffer_p, NULL); register_js_value ("input_buffer", input_buffer); jerry_release_value (input_buffer); } const char *eval_arraybuffer_src_p = ( "var array = new Uint8Array(input_buffer);" "for (var i = 0; i < array.length; i++)" "{" " array[i] = i * 2;" "};" "array.buffer"); jerry_value_t buffer = jerry_eval ((jerry_char_t *) eval_arraybuffer_src_p, strlen (eval_arraybuffer_src_p), true); TEST_ASSERT (!jerry_value_is_error (buffer)); TEST_ASSERT (jerry_value_is_arraybuffer (buffer)); TEST_ASSERT (jerry_get_arraybuffer_byte_length (buffer) == 20); uint8_t *const data = jerry_get_arraybuffer_pointer (buffer); /* test memory read */ for (int i = 0; i < 20; i++) { TEST_ASSERT (data[i] == (uint8_t) (i * 2)); } /* "upload" new data */ double sum = 0; for (int i = 0; i < 20; i++) { data[i] = (uint8_t)(i * 3); sum += data[i]; } jerry_release_value (buffer); const char *eval_test_arraybuffer_p = ( "var sum = 0;" "for (var i = 0; i < array.length; i++)" "{" " var expected = i * 3;" " assert(array[i] == expected, 'Array at index ' + i + ' was: ' + array[i] + ' should be: ' + expected);" " sum += array[i]" "};" "sum"); jerry_value_t res = jerry_eval ((jerry_char_t *) eval_test_arraybuffer_p, strlen (eval_test_arraybuffer_p), true); TEST_ASSERT (jerry_value_is_number (res)); TEST_ASSERT (jerry_get_number_value (res) == sum); jerry_release_value (res); jerry_release_value (buffer); } /* Test ArrayBuffer external with invalid arguments */ { jerry_value_t input_buffer = jerry_create_arraybuffer_external (0, NULL, NULL); TEST_ASSERT (jerry_value_is_error (input_buffer)); TEST_ASSERT (jerry_get_error_type (input_buffer) == JERRY_ERROR_RANGE); jerry_release_value (input_buffer); } jerry_cleanup (); TEST_ASSERT (callback_called == true); return 0; } /* main */
/** * JerryScript command main */ static int jerry_cmd_main (int argc, char *argv[]) { if (argc > JERRY_MAX_COMMAND_LINE_ARGS) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Too many command line arguments. Current maximum is %d\n", JERRY_MAX_COMMAND_LINE_ARGS); return JERRY_STANDALONE_EXIT_CODE_FAIL; } const char *file_names[JERRY_MAX_COMMAND_LINE_ARGS]; int i; int files_counter = 0; bool start_debug_server = false; uint16_t debug_port = 5001; jerry_init_flag_t flags = JERRY_INIT_EMPTY; for (i = 1; i < argc; i++) { if (!strcmp ("-h", argv[i]) || !strcmp ("--help", argv[i])) { print_help (argv[0]); return JERRY_STANDALONE_EXIT_CODE_OK; } else if (!strcmp ("--mem-stats", argv[i])) { jerry_log_level = JERRY_LOG_LEVEL_DEBUG; flags |= JERRY_INIT_MEM_STATS; } else if (!strcmp ("--mem-stats-separate", argv[i])) { jerry_log_level = JERRY_LOG_LEVEL_DEBUG; flags |= JERRY_INIT_MEM_STATS_SEPARATE; } else if (!strcmp ("--show-opcodes", argv[i])) { jerry_log_level = JERRY_LOG_LEVEL_DEBUG; flags |= JERRY_INIT_SHOW_OPCODES | JERRY_INIT_SHOW_REGEXP_OPCODES; } else if (!strcmp ("--log-level", argv[i])) { if (++i < argc && strlen (argv[i]) == 1 && argv[i][0] >='0' && argv[i][0] <= '3') { jerry_log_level = argv[i][0] - '0'; } else { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: wrong format or invalid argument\n"); return JERRY_STANDALONE_EXIT_CODE_FAIL; } } else if (!strcmp ("--start-debug-server", argv[i])) { start_debug_server = true; } else if (!strcmp ("--debug-server-port", argv[i])) { if (++i < argc) { debug_port = str_to_uint (argv[i]); } else { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: wrong format or invalid argument\n"); return JERRY_STANDALONE_EXIT_CODE_FAIL; } } else { file_names[files_counter++] = argv[i]; } } jerry_init (flags); if (start_debug_server) { jerry_debugger_init (debug_port); } register_js_function ("assert", jerryx_handler_assert); register_js_function ("gc", jerryx_handler_gc); register_js_function ("print", jerryx_handler_print); jerry_value_t ret_value = jerry_create_undefined (); if (files_counter == 0) { printf ("No input files, running a hello world demo:\n"); const jerry_char_t script[] = "var str = 'Hello World'; print(str + ' from JerryScript')"; size_t script_size = strlen ((const char *) script); ret_value = jerry_parse (script, script_size, false); if (!jerry_value_has_error_flag (ret_value)) { ret_value = jerry_run (ret_value); } } else { for (i = 0; i < files_counter; i++) { size_t source_size; const jerry_char_t *source_p = read_file (file_names[i], &source_size); if (source_p == NULL) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Source file load error\n"); return JERRY_STANDALONE_EXIT_CODE_FAIL; } ret_value = jerry_parse_named_resource ((jerry_char_t *) file_names[i], strlen (file_names[i]), source_p, source_size, false); if (!jerry_value_has_error_flag (ret_value)) { jerry_value_t func_val = ret_value; ret_value = jerry_run (func_val); jerry_release_value (func_val); } if (jerry_value_has_error_flag (ret_value)) { print_unhandled_exception (ret_value, source_p); jmem_heap_free_block ((void*) source_p, source_size); break; } jmem_heap_free_block ((void*) source_p, source_size); jerry_release_value (ret_value); ret_value = jerry_create_undefined (); } } int ret_code = JERRY_STANDALONE_EXIT_CODE_OK; if (jerry_value_has_error_flag (ret_value)) { ret_code = JERRY_STANDALONE_EXIT_CODE_FAIL; } jerry_release_value (ret_value); jerry_cleanup (); return ret_code; } /* jerry_cmd_main */
int main (int argc, char **argv) { if (argc > JERRY_MAX_COMMAND_LINE_ARGS) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: too many command line arguments: %d (JERRY_MAX_COMMAND_LINE_ARGS=%d)\n", argc, JERRY_MAX_COMMAND_LINE_ARGS); return JERRY_STANDALONE_EXIT_CODE_FAIL; } const char *file_names[JERRY_MAX_COMMAND_LINE_ARGS]; int i; int files_counter = 0; size_t max_data_bss_size, max_stack_size; jerry_get_memory_limits (&max_data_bss_size, &max_stack_size); // FIXME: // jrt_set_mem_limits (max_data_bss_size, max_stack_size); jerry_init_flag_t flags = JERRY_INIT_EMPTY; const char *exec_snapshot_file_names[JERRY_MAX_COMMAND_LINE_ARGS]; int exec_snapshots_count = 0; bool is_parse_only = false; bool is_save_snapshot_mode = false; bool is_save_snapshot_mode_for_global_or_eval = false; const char *save_snapshot_file_name_p = NULL; bool is_repl_mode = false; for (i = 1; i < argc; i++) { if (!strcmp ("-h", argv[i]) || !strcmp ("--help", argv[i])) { print_help (argv[0]); return JERRY_STANDALONE_EXIT_CODE_OK; } else if (!strcmp ("-v", argv[i]) || !strcmp ("--version", argv[i])) { jerry_port_console ("Version: \t%d.%d\n\n", JERRY_API_MAJOR_VERSION, JERRY_API_MINOR_VERSION); return JERRY_STANDALONE_EXIT_CODE_OK; } else if (!strcmp ("--mem-stats", argv[i])) { flags |= JERRY_INIT_MEM_STATS; jerry_port_default_set_log_level (JERRY_LOG_LEVEL_DEBUG); } else if (!strcmp ("--mem-stats-separate", argv[i])) { flags |= JERRY_INIT_MEM_STATS_SEPARATE; jerry_port_default_set_log_level (JERRY_LOG_LEVEL_DEBUG); } else if (!strcmp ("--parse-only", argv[i])) { is_parse_only = true; } else if (!strcmp ("--show-opcodes", argv[i])) { flags |= JERRY_INIT_SHOW_OPCODES; jerry_port_default_set_log_level (JERRY_LOG_LEVEL_DEBUG); } else if (!strcmp ("--show-regexp-opcodes", argv[i])) { flags |= JERRY_INIT_SHOW_REGEXP_OPCODES; jerry_port_default_set_log_level (JERRY_LOG_LEVEL_DEBUG); } else if (!strcmp ("--save-snapshot-for-global", argv[i]) || !strcmp ("--save-snapshot-for-eval", argv[i])) { is_save_snapshot_mode = true; is_save_snapshot_mode_for_global_or_eval = !strcmp ("--save-snapshot-for-global", argv[i]); if (save_snapshot_file_name_p != NULL) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: snapshot file name already specified\n"); print_usage (argv[0]); return JERRY_STANDALONE_EXIT_CODE_FAIL; } if (++i >= argc) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: no file specified for %s\n", argv[i - 1]); print_usage (argv[0]); return JERRY_STANDALONE_EXIT_CODE_FAIL; } save_snapshot_file_name_p = argv[i]; } else if (!strcmp ("--exec-snapshot", argv[i])) { if (++i >= argc) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: no file specified for %s\n", argv[i - 1]); print_usage (argv[0]); return JERRY_STANDALONE_EXIT_CODE_FAIL; } assert (exec_snapshots_count < JERRY_MAX_COMMAND_LINE_ARGS); exec_snapshot_file_names[exec_snapshots_count++] = argv[i]; } else if (!strcmp ("--log-level", argv[i])) { if (++i >= argc) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: no level specified for %s\n", argv[i - 1]); print_usage (argv[0]); return JERRY_STANDALONE_EXIT_CODE_FAIL; } if (strlen (argv[i]) != 1 || argv[i][0] < '0' || argv[i][0] > '3') { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: wrong format for %s\n", argv[i - 1]); print_usage (argv[0]); return JERRY_STANDALONE_EXIT_CODE_FAIL; } jerry_port_default_set_log_level (argv[i][0] - '0'); } else if (!strcmp ("--abort-on-fail", argv[i])) { jerry_port_default_set_abort_on_fail (true); } else if (!strncmp ("-", argv[i], 1)) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: unrecognized option: %s\n", argv[i]); print_usage (argv[0]); return JERRY_STANDALONE_EXIT_CODE_FAIL; } else { file_names[files_counter++] = argv[i]; } } if (is_save_snapshot_mode) { if (files_counter != 1) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: --save-snapshot argument works with exactly one script\n"); return JERRY_STANDALONE_EXIT_CODE_FAIL; } if (exec_snapshots_count != 0) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: --save-snapshot and --exec-snapshot options can't be passed simultaneously\n"); return JERRY_STANDALONE_EXIT_CODE_FAIL; } } if (files_counter == 0 && exec_snapshots_count == 0) { is_repl_mode = true; } jerry_init (flags); jerry_value_t global_obj_val = jerry_get_global_object (); jerry_value_t assert_value = jerry_create_external_function (assert_handler); jerry_value_t assert_func_name_val = jerry_create_string ((jerry_char_t *) "assert"); bool is_assert_added = jerry_set_property (global_obj_val, assert_func_name_val, assert_value); jerry_release_value (assert_func_name_val); jerry_release_value (assert_value); jerry_release_value (global_obj_val); if (!is_assert_added) { jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Warning: failed to register 'assert' method."); } jerry_value_t ret_value = jerry_create_undefined (); for (int i = 0; i < exec_snapshots_count; i++) { size_t snapshot_size; const uint8_t *snapshot_p = read_file (exec_snapshot_file_names[i], &snapshot_size); if (snapshot_p == NULL) { ret_value = jerry_create_error (JERRY_ERROR_COMMON, (jerry_char_t *) ""); } else { ret_value = jerry_exec_snapshot ((void *) snapshot_p, snapshot_size, true); } if (jerry_value_has_error_flag (ret_value)) { break; } } if (!jerry_value_has_error_flag (ret_value)) { for (int i = 0; i < files_counter; i++) { size_t source_size; const jerry_char_t *source_p = read_file (file_names[i], &source_size); if (source_p == NULL) { ret_value = jerry_create_error (JERRY_ERROR_COMMON, (jerry_char_t *) ""); } if (is_save_snapshot_mode) { static uint8_t snapshot_save_buffer[ JERRY_BUFFER_SIZE ]; size_t snapshot_size = jerry_parse_and_save_snapshot ((jerry_char_t *) source_p, source_size, is_save_snapshot_mode_for_global_or_eval, false, snapshot_save_buffer, JERRY_BUFFER_SIZE); if (snapshot_size == 0) { ret_value = jerry_create_error (JERRY_ERROR_COMMON, (jerry_char_t *) ""); } else { FILE *snapshot_file_p = fopen (save_snapshot_file_name_p, "w"); fwrite (snapshot_save_buffer, sizeof (uint8_t), snapshot_size, snapshot_file_p); fclose (snapshot_file_p); } } else { ret_value = jerry_parse (source_p, source_size, false); if (!jerry_value_has_error_flag (ret_value) && !is_parse_only) { jerry_value_t func_val = ret_value; ret_value = jerry_run (func_val); jerry_release_value (func_val); } } if (jerry_value_has_error_flag (ret_value)) { break; } } } if (is_repl_mode) { const char *prompt = "jerry> "; bool is_done = false; jerry_value_t global_obj_val = jerry_get_global_object (); jerry_value_t print_func_name_val = jerry_create_string ((jerry_char_t *) "print"); jerry_value_t print_function = jerry_get_property (global_obj_val, print_func_name_val); jerry_release_value (print_func_name_val); if (jerry_value_has_error_flag (print_function)) { return JERRY_STANDALONE_EXIT_CODE_FAIL; } if (!jerry_value_is_function (print_function)) { return JERRY_STANDALONE_EXIT_CODE_FAIL; } while (!is_done) { uint8_t *source_buffer_tail = buffer; size_t len = 0; jerry_port_console ("%s", prompt); /* Read a line */ while (true) { if (fread (source_buffer_tail, 1, 1, stdin) != 1 && len == 0) { is_done = true; break; } if (*source_buffer_tail == '\n') { break; } source_buffer_tail ++; len ++; } *source_buffer_tail = 0; if (len > 0) { /* Evaluate the line */ jerry_value_t ret_val_eval = jerry_eval (buffer, len, false); /* Print return value */ const jerry_value_t args[] = { ret_val_eval }; jerry_value_t ret_val_print = jerry_call_function (print_function, jerry_create_undefined (), args, 1); jerry_release_value (ret_val_print); jerry_release_value (ret_val_eval); } } jerry_release_value (global_obj_val); jerry_release_value (print_function); } int ret_code = JERRY_STANDALONE_EXIT_CODE_OK; if (jerry_value_has_error_flag (ret_value)) { jerry_value_clear_error_flag (&ret_value); jerry_value_t err_str_val = jerry_value_to_string (ret_value); jerry_char_t err_str_buf[256]; jerry_size_t err_str_size = jerry_get_string_size (err_str_val); assert (err_str_size < 256); jerry_size_t sz = jerry_string_to_char_buffer (err_str_val, err_str_buf, err_str_size); assert (sz == err_str_size); err_str_buf[err_str_size] = 0; jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Script Error: unhandled exception: %s\n", err_str_buf); jerry_release_value (err_str_val); ret_code = JERRY_STANDALONE_EXIT_CODE_FAIL; } jerry_release_value (ret_value); jerry_cleanup (); return ret_code; } /* main */
int main (void) { TEST_INIT (); bool is_ok; jerry_size_t sz; 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; jerry_value_t val_call_external; jerry_value_t global_obj_val, obj_val; jerry_value_t external_func_val, external_construct_val; jerry_value_t throw_test_handler_val; jerry_value_t parsed_code_val, proto_val, prim_val; jerry_value_t res, args[2]; char buffer[32]; is_ok = test_run_simple ("print ('Hello, World!');"); JERRY_ASSERT (is_ok); is_ok = test_run_simple ("throw 'Hello World';"); JERRY_ASSERT (!is_ok); jerry_init (JERRY_INIT_EMPTY); parsed_code_val = jerry_parse ((jerry_char_t *) test_source, strlen (test_source), false); JERRY_ASSERT (!jerry_value_has_error_flag (parsed_code_val)); res = jerry_run (parsed_code_val); JERRY_ASSERT (!jerry_value_has_error_flag (res)); jerry_release_value (res); jerry_release_value (parsed_code_val); global_obj_val = jerry_get_global_object (); // Test corner case for jerry_string_to_char_buffer args[0] = jerry_create_string ((jerry_char_t *) ""); sz = jerry_get_string_size (args[0]); JERRY_ASSERT (sz == 0); jerry_release_value (args[0]); // Get global.boo (non-existing field) val_t = get_property (global_obj_val, "boo"); JERRY_ASSERT (!jerry_value_has_error_flag (val_t)); JERRY_ASSERT (jerry_value_is_undefined (val_t)); // Get global.t val_t = get_property (global_obj_val, "t"); JERRY_ASSERT (!jerry_value_has_error_flag (val_t)); JERRY_ASSERT (jerry_value_is_number (val_t) && jerry_get_number_value (val_t) == 1.0); jerry_release_value (val_t); // Get global.foo val_foo = get_property (global_obj_val, "foo"); JERRY_ASSERT (!jerry_value_has_error_flag (val_foo)); JERRY_ASSERT (jerry_value_is_object (val_foo)); // Call foo (4, 2) args[0] = jerry_create_number (4); args[1] = jerry_create_number (2); res = jerry_call_function (val_foo, jerry_create_undefined (), args, 2); JERRY_ASSERT (!jerry_value_has_error_flag (res)); JERRY_ASSERT (jerry_value_is_number (res) && jerry_get_number_value (res) == 1.0); jerry_release_value (res); // Get global.bar val_bar = get_property (global_obj_val, "bar"); JERRY_ASSERT (!jerry_value_has_error_flag (val_bar)); JERRY_ASSERT (jerry_value_is_object (val_bar)); // Call bar (4, 2) res = jerry_call_function (val_bar, jerry_create_undefined (), args, 2); JERRY_ASSERT (!jerry_value_has_error_flag (res)); JERRY_ASSERT (jerry_value_is_number (res) && jerry_get_number_value (res) == 5.0); jerry_release_value (res); jerry_release_value (val_bar); // Set global.t = "abcd" jerry_release_value (args[0]); args[0] = jerry_create_string ((jerry_char_t *) "abcd"); res = set_property (global_obj_val, "t", args[0]); JERRY_ASSERT (!jerry_value_has_error_flag (res)); JERRY_ASSERT (jerry_get_boolean_value (res)); jerry_release_value (res); // Call foo (4, 2) res = jerry_call_function (val_foo, jerry_create_undefined (), args, 2); JERRY_ASSERT (!jerry_value_has_error_flag (res)); JERRY_ASSERT (jerry_value_is_string (res)); sz = jerry_get_string_size (res); JERRY_ASSERT (sz == 4); sz = jerry_string_to_char_buffer (res, (jerry_char_t *) buffer, sz); JERRY_ASSERT (sz == 4); jerry_release_value (res); JERRY_ASSERT (!strncmp (buffer, "abcd", (size_t) sz)); jerry_release_value (args[0]); jerry_release_value (args[1]); // Get global.A val_A = get_property (global_obj_val, "A"); JERRY_ASSERT (!jerry_value_has_error_flag (val_A)); JERRY_ASSERT (jerry_value_is_object (val_A)); // Get A.prototype is_ok = jerry_value_is_constructor (val_A); JERRY_ASSERT (is_ok); val_A_prototype = get_property (val_A, "prototype"); JERRY_ASSERT (!jerry_value_has_error_flag (val_A_prototype)); JERRY_ASSERT (jerry_value_is_object (val_A_prototype)); jerry_release_value (val_A); // Set A.prototype.foo = global.foo res = set_property (val_A_prototype, "foo", val_foo); JERRY_ASSERT (!jerry_value_has_error_flag (res)); JERRY_ASSERT (jerry_get_boolean_value (res)); jerry_release_value (res); jerry_release_value (val_A_prototype); jerry_release_value (val_foo); // Get global.a val_a = get_property (global_obj_val, "a"); JERRY_ASSERT (!jerry_value_has_error_flag (val_a)); JERRY_ASSERT (jerry_value_is_object (val_a)); // Get a.t res = get_property (val_a, "t"); JERRY_ASSERT (!jerry_value_has_error_flag (res)); JERRY_ASSERT (jerry_value_is_number (res) && jerry_get_number_value (res) == 12.0); jerry_release_value (res); // foreach properties val_p = get_property (global_obj_val, "p"); is_ok = jerry_foreach_object_property (val_p, foreach, (void *) "user_data"); JERRY_ASSERT (is_ok); // break foreach at third element int count = 0; is_ok = jerry_foreach_object_property (val_p, foreach_subset, &count); JERRY_ASSERT (is_ok); JERRY_ASSERT (count == 3); jerry_release_value (val_p); // foreach with throw test val_np = get_property (global_obj_val, "np"); is_ok = !jerry_foreach_object_property (val_np, foreach_exception, NULL); JERRY_ASSERT (is_ok); jerry_release_value (val_np); // Get a.foo val_a_foo = get_property (val_a, "foo"); JERRY_ASSERT (!jerry_value_has_error_flag (val_a_foo)); JERRY_ASSERT (jerry_value_is_object (val_a_foo)); // Call a.foo () res = jerry_call_function (val_a_foo, val_a, NULL, 0); JERRY_ASSERT (!jerry_value_has_error_flag (res)); JERRY_ASSERT (jerry_value_is_number (res) && jerry_get_number_value (res) == 12.0); jerry_release_value (res); jerry_release_value (val_a_foo); jerry_release_value (val_a); // Create native handler bound function object and set it to 'external' variable external_func_val = jerry_create_external_function (handler); JERRY_ASSERT (jerry_value_is_function (external_func_val) && jerry_value_is_constructor (external_func_val)); res = set_property (global_obj_val, "external", external_func_val); JERRY_ASSERT (!jerry_value_has_error_flag (res)); JERRY_ASSERT (jerry_get_boolean_value (res)); jerry_release_value (external_func_val); // Call 'call_external' function that should call external function created above val_call_external = get_property (global_obj_val, "call_external"); JERRY_ASSERT (!jerry_value_has_error_flag (val_call_external)); JERRY_ASSERT (jerry_value_is_object (val_call_external)); res = jerry_call_function (val_call_external, global_obj_val, NULL, 0); jerry_release_value (val_call_external); JERRY_ASSERT (!jerry_value_has_error_flag (res)); JERRY_ASSERT (jerry_value_is_string (res)); sz = jerry_get_string_size (res); JERRY_ASSERT (sz == 19); sz = jerry_string_to_char_buffer (res, (jerry_char_t *) buffer, sz); JERRY_ASSERT (sz == 19); jerry_release_value (res); JERRY_ASSERT (!strncmp (buffer, "string from handler", (size_t) sz)); // Create native handler bound function object and set it to 'external_construct' variable external_construct_val = jerry_create_external_function (handler_construct); JERRY_ASSERT (jerry_value_is_function (external_construct_val) && jerry_value_is_constructor (external_construct_val)); res = set_property (global_obj_val, "external_construct", external_construct_val); JERRY_ASSERT (!jerry_value_has_error_flag (res)); JERRY_ASSERT (jerry_get_boolean_value (res)); jerry_release_value (res); // Call external function created above, as constructor args[0] = jerry_create_boolean (true); res = jerry_construct_object (external_construct_val, args, 1); JERRY_ASSERT (!jerry_value_has_error_flag (res)); JERRY_ASSERT (jerry_value_is_object (res)); val_value_field = get_property (res, "value_field"); // Get 'value_field' of constructed object JERRY_ASSERT (!jerry_value_has_error_flag (val_value_field)); JERRY_ASSERT (jerry_value_is_boolean (val_value_field) && jerry_get_boolean_value (val_value_field)); jerry_release_value (val_value_field); jerry_release_value (external_construct_val); uintptr_t ptr; is_ok = jerry_get_object_native_handle (res, &ptr); JERRY_ASSERT (is_ok && ptr == (uintptr_t) 0x0012345678abcdefull); jerry_release_value (res); // Test: Throwing exception from native handler. throw_test_handler_val = jerry_create_external_function (handler_throw_test); JERRY_ASSERT (jerry_value_is_function (throw_test_handler_val)); res = set_property (global_obj_val, "throw_test", throw_test_handler_val); JERRY_ASSERT (!jerry_value_has_error_flag (res)); JERRY_ASSERT (jerry_get_boolean_value (res)); jerry_release_value (res); jerry_release_value (throw_test_handler_val); val_t = get_property (global_obj_val, "call_throw_test"); JERRY_ASSERT (!jerry_value_has_error_flag (val_t)); JERRY_ASSERT (jerry_value_is_object (val_t)); res = jerry_call_function (val_t, global_obj_val, NULL, 0); JERRY_ASSERT (!jerry_value_has_error_flag (res)); jerry_release_value (val_t); jerry_release_value (res); // Test: Unhandled exception in called function val_t = get_property (global_obj_val, "throw_reference_error"); JERRY_ASSERT (!jerry_value_has_error_flag (val_t)); JERRY_ASSERT (jerry_value_is_object (val_t)); res = jerry_call_function (val_t, global_obj_val, NULL, 0); JERRY_ASSERT (jerry_value_has_error_flag (res)); jerry_release_value (val_t); // 'res' should contain exception object JERRY_ASSERT (jerry_value_is_object (res)); jerry_release_value (res); // Test: Call of non-function obj_val = jerry_create_object (); res = jerry_call_function (obj_val, global_obj_val, NULL, 0); JERRY_ASSERT (jerry_value_has_error_flag (res)); // 'res' should contain exception object JERRY_ASSERT (jerry_value_is_object (res)); jerry_release_value (res); jerry_release_value (obj_val); // Test: Unhandled exception in function called, as constructor val_t = get_property (global_obj_val, "throw_reference_error"); JERRY_ASSERT (!jerry_value_has_error_flag (val_t)); JERRY_ASSERT (jerry_value_is_object (val_t)); res = jerry_construct_object (val_t, NULL, 0); JERRY_ASSERT (jerry_value_has_error_flag (res)); jerry_release_value (val_t); // 'res' should contain exception object JERRY_ASSERT (jerry_value_is_object (res)); jerry_release_value (res); // Test: Call of non-function as constructor obj_val = jerry_create_object (); res = jerry_construct_object (obj_val, NULL, 0); JERRY_ASSERT (jerry_value_has_error_flag (res)); // 'res' should contain exception object JERRY_ASSERT (jerry_value_is_object (res)); jerry_release_value (res); jerry_release_value (obj_val); // Test: Array Object API jerry_value_t array_obj_val = jerry_create_array (10); JERRY_ASSERT (jerry_value_is_array (array_obj_val)); JERRY_ASSERT (jerry_get_array_length (array_obj_val) == 10); jerry_value_t v_in = jerry_create_number (10.5); jerry_set_property_by_index (array_obj_val, 5, v_in); jerry_value_t v_out = jerry_get_property_by_index (array_obj_val, 5); JERRY_ASSERT (jerry_value_is_number (v_out) && jerry_get_number_value (v_out) == 10.5); jerry_release_value (v_in); jerry_release_value (v_out); jerry_release_value (array_obj_val); // Test: init property descriptor jerry_property_descriptor_t prop_desc; jerry_init_property_descriptor_fields (&prop_desc); JERRY_ASSERT (prop_desc.is_value_defined == false); JERRY_ASSERT (jerry_value_is_undefined (prop_desc.value)); JERRY_ASSERT (prop_desc.is_writable_defined == false); JERRY_ASSERT (prop_desc.is_writable == false); JERRY_ASSERT (prop_desc.is_enumerable_defined == false); JERRY_ASSERT (prop_desc.is_enumerable == false); JERRY_ASSERT (prop_desc.is_configurable_defined == false); JERRY_ASSERT (prop_desc.is_configurable == false); JERRY_ASSERT (prop_desc.is_get_defined == false); JERRY_ASSERT (jerry_value_is_undefined (prop_desc.getter)); JERRY_ASSERT (prop_desc.is_set_defined == false); JERRY_ASSERT (jerry_value_is_undefined (prop_desc.setter)); // Test: define own properties jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "my_defined_property"); prop_desc.is_value_defined = true; prop_desc.value = jerry_acquire_value (prop_name); res = jerry_define_own_property (global_obj_val, prop_name, &prop_desc); JERRY_ASSERT (!jerry_value_has_error_flag (res)); JERRY_ASSERT (jerry_value_is_boolean (res)); JERRY_ASSERT (jerry_get_boolean_value (res)); jerry_release_value (res); jerry_free_property_descriptor_fields (&prop_desc); // Test: get own property descriptor is_ok = jerry_get_own_property_descriptor (global_obj_val, prop_name, &prop_desc); JERRY_ASSERT (is_ok); JERRY_ASSERT (prop_desc.is_value_defined == true); JERRY_ASSERT (jerry_value_is_string (prop_desc.value)); JERRY_ASSERT (prop_desc.is_writable == false); JERRY_ASSERT (prop_desc.is_enumerable == false); JERRY_ASSERT (prop_desc.is_configurable == false); JERRY_ASSERT (prop_desc.is_get_defined == false); JERRY_ASSERT (jerry_value_is_undefined (prop_desc.getter)); JERRY_ASSERT (prop_desc.is_set_defined == false); JERRY_ASSERT (jerry_value_is_undefined (prop_desc.setter)); jerry_release_value (prop_name); jerry_free_property_descriptor_fields (&prop_desc); // Test: object keys res = jerry_get_object_keys (global_obj_val); JERRY_ASSERT (!jerry_value_has_error_flag (res)); JERRY_ASSERT (jerry_value_is_array (res)); jerry_release_value (res); // Test: jerry_value_to_primitive obj_val = jerry_eval ((jerry_char_t *) "new String ('hello')", 20, false); JERRY_ASSERT (!jerry_value_has_error_flag (obj_val)); JERRY_ASSERT (jerry_value_is_object (obj_val)); JERRY_ASSERT (!jerry_value_is_string (obj_val)); prim_val = jerry_value_to_primitive (obj_val); JERRY_ASSERT (!jerry_value_has_error_flag (prim_val)); JERRY_ASSERT (jerry_value_is_string (prim_val)); jerry_release_value (prim_val); // Test: jerry_get_prototype proto_val = jerry_get_prototype (obj_val); JERRY_ASSERT (!jerry_value_has_error_flag (proto_val)); JERRY_ASSERT (jerry_value_is_object (proto_val)); jerry_release_value (obj_val); // Test: jerry_set_prototype obj_val = jerry_create_object (); res = jerry_set_prototype (obj_val, jerry_create_null ()); JERRY_ASSERT (!jerry_value_has_error_flag (res)); JERRY_ASSERT (jerry_value_is_boolean (res)); JERRY_ASSERT (jerry_get_boolean_value (res)); res = jerry_set_prototype (obj_val, jerry_create_object ()); JERRY_ASSERT (!jerry_value_has_error_flag (res)); JERRY_ASSERT (jerry_value_is_boolean (res)); JERRY_ASSERT (jerry_get_boolean_value (res)); proto_val = jerry_get_prototype (obj_val); JERRY_ASSERT (!jerry_value_has_error_flag (proto_val)); JERRY_ASSERT (jerry_value_is_object (proto_val)); jerry_release_value (proto_val); jerry_release_value (obj_val); // Test: eval const char *eval_code_src_p = "(function () { return 123; })"; val_t = jerry_eval ((jerry_char_t *) eval_code_src_p, strlen (eval_code_src_p), true); JERRY_ASSERT (!jerry_value_has_error_flag (val_t)); JERRY_ASSERT (jerry_value_is_object (val_t)); JERRY_ASSERT (jerry_value_is_function (val_t)); res = jerry_call_function (val_t, jerry_create_undefined (), NULL, 0); JERRY_ASSERT (!jerry_value_has_error_flag (res)); JERRY_ASSERT (jerry_value_is_number (res) && jerry_get_number_value (res) == 123.0); jerry_release_value (res); jerry_release_value (val_t); // cleanup. jerry_release_value (global_obj_val); // TEST: run gc. jerry_gc (); jerry_cleanup (); JERRY_ASSERT (test_api_is_free_callback_was_called); // External Magic String jerry_init (JERRY_INIT_SHOW_OPCODES); uint32_t num_magic_string_items = (uint32_t) (sizeof (magic_string_items) / sizeof (jerry_char_ptr_t)); jerry_register_magic_strings (magic_string_items, num_magic_string_items, magic_string_lengths); const char *ms_code_src_p = "var global = {}; var console = [1]; var process = 1;"; parsed_code_val = jerry_parse ((jerry_char_t *) ms_code_src_p, strlen (ms_code_src_p), false); JERRY_ASSERT (!jerry_value_has_error_flag (parsed_code_val)); res = jerry_run (parsed_code_val); JERRY_ASSERT (!jerry_value_has_error_flag (res)); jerry_release_value (res); jerry_release_value (parsed_code_val); jerry_cleanup (); // Dump / execute snapshot // FIXME: support save/load snapshot for optimized parser if (false) { static uint8_t global_mode_snapshot_buffer[1024]; static uint8_t eval_mode_snapshot_buffer[1024]; const char *code_to_snapshot_p = "(function () { return 'string from snapshot'; }) ();"; jerry_init (JERRY_INIT_SHOW_OPCODES); size_t global_mode_snapshot_size = jerry_parse_and_save_snapshot ((jerry_char_t *) code_to_snapshot_p, strlen (code_to_snapshot_p), true, false, global_mode_snapshot_buffer, sizeof (global_mode_snapshot_buffer)); JERRY_ASSERT (global_mode_snapshot_size != 0); jerry_cleanup (); jerry_init (JERRY_INIT_SHOW_OPCODES); size_t eval_mode_snapshot_size = jerry_parse_and_save_snapshot ((jerry_char_t *) code_to_snapshot_p, strlen (code_to_snapshot_p), false, false, eval_mode_snapshot_buffer, sizeof (eval_mode_snapshot_buffer)); JERRY_ASSERT (eval_mode_snapshot_size != 0); jerry_cleanup (); jerry_init (JERRY_INIT_SHOW_OPCODES); res = jerry_exec_snapshot (global_mode_snapshot_buffer, global_mode_snapshot_size, false); JERRY_ASSERT (!jerry_value_has_error_flag (res)); JERRY_ASSERT (jerry_value_is_undefined (res)); jerry_release_value (res); res = jerry_exec_snapshot (eval_mode_snapshot_buffer, eval_mode_snapshot_size, false); JERRY_ASSERT (!jerry_value_has_error_flag (res)); JERRY_ASSERT (jerry_value_is_string (res)); sz = jerry_get_string_size (res); JERRY_ASSERT (sz == 20); sz = jerry_string_to_char_buffer (res, (jerry_char_t *) buffer, sz); JERRY_ASSERT (sz == 20); jerry_release_value (res); JERRY_ASSERT (!strncmp (buffer, "string from snapshot", (size_t) sz)); jerry_cleanup (); } return 0; } /* main */