diff --git a/jerry-core/parser/js/opcodes-dumper.cpp b/jerry-core/parser/js/opcodes-dumper.cpp index f515cfe988..16ce7bfece 100644 --- a/jerry-core/parser/js/opcodes-dumper.cpp +++ b/jerry-core/parser/js/opcodes-dumper.cpp @@ -383,28 +383,6 @@ create_op_meta_for_vlt (varg_list_type vlt, operand *res, operand *obj) return ret; } -static void -dump_assert (operand op) -{ - switch (op.type) - { - case OPERAND_LITERAL: - { - const opcode_t opcode = getop_is_true_jmp_down (LITERAL_TO_REWRITE, 0, 2); - serializer_dump_op_meta (create_op_meta_100 (opcode, op.data.lit_id)); - break; - } - case OPERAND_TMP: - { - const opcode_t opcode = getop_is_true_jmp_down (op.data.uid, 0, 2); - serializer_dump_op_meta (create_op_meta_000 (opcode)); - break; - } - } - const opcode_t opcode = getop_exitval (1); - serializer_dump_op_meta (create_op_meta_000 (opcode)); -} - static void split_opcode_counter (opcode_counter_t oc, idx_t *id1, idx_t *id2) { @@ -742,25 +720,15 @@ dumper_finish_scope (void) } bool -dumper_is_intrinsic (operand obj) +dumper_is_intrinsic (operand /* obj */) { - if (obj.type == OPERAND_LITERAL) - { - if (lit_literal_equal_type_zt (lit_get_literal_by_cp (obj.data.lit_id), (const ecma_char_t *) "assert")) - { - return true; - } - } return false; } operand -dump_intrinsic (operand obj, operand arg) +dump_intrinsic (operand /* obj */, operand /* arg */) { - JERRY_ASSERT (obj.type == OPERAND_LITERAL); - TODO (/* Rewrite when there will be more intrinsics. */) - JERRY_ASSERT (lit_literal_equal_type_zt (lit_get_literal_by_cp (obj.data.lit_id), (const ecma_char_t *) "assert")); - dump_assert (arg); + JERRY_UNREACHABLE (); return dump_undefined_assignment_res (); } diff --git a/main-linux.cpp b/main-linux.cpp index f4f59f0694..bf0f04870e 100644 --- a/main-linux.cpp +++ b/main-linux.cpp @@ -14,6 +14,7 @@ */ #include +#include #include #include "jerry.h" @@ -108,6 +109,30 @@ read_sources (const char *script_file_names[], } } +/** + * Provide the 'assert' implementation for the engine. + * + * @return true - if the argument was not a boolean value or it was boolean true. + */ +static bool +assert_handler (const jerry_api_object_t *function_obj_p __attr_unused___, /** < function object */ + const jerry_api_value_t *this_p __attr_unused___, /** < this arg */ + jerry_api_value_t *ret_val_p __attr_unused___, /** < return argument */ + const jerry_api_value_t args_p[], /** < function arguments */ + const uint16_t args_cnt) /** < number of function arguments */ +{ + if (args_cnt > 0 + && args_p[0].type == JERRY_API_DATA_TYPE_BOOLEAN + && args_p[0].v_bool != true) + { + JERRY_ERROR_MSG ("Script assertion failed\n"); + exit (JERRY_STANDALONE_EXIT_CODE_FAIL); + } + + return true; +} /* assert_handler */ + + int main (int argc, char **argv) @@ -238,6 +263,22 @@ main (int argc, plugin_io_init (); + jerry_api_object_t *global_obj_p = jerry_api_get_global (); + jerry_api_object_t *assert_func_p = jerry_api_create_external_function (assert_handler); + jerry_api_value_t assert_value; + assert_value.type = JERRY_API_DATA_TYPE_OBJECT; + assert_value.v_object = assert_func_p; + + bool is_assert_added = jerry_api_set_object_field_value (global_obj_p, "assert", &assert_value); + + jerry_api_release_value (&assert_value); + jerry_api_release_object (global_obj_p); + + if (!is_assert_added) + { + JERRY_ERROR_MSG ("Failed to register 'assert' method."); + } + jerry_completion_code_t ret_code = JERRY_COMPLETION_CODE_OK; if (!jerry_parse (source_p, source_size)) diff --git a/tests/unit/test-api.cpp b/tests/unit/test-api.cpp index 0e2bea50c6..683c7b82e9 100644 --- a/tests/unit/test-api.cpp +++ b/tests/unit/test-api.cpp @@ -19,6 +19,11 @@ #include "test-common.h" const char *test_source = ( + "function assert (arg) { " + " if (!arg) { " + " throw Error('Assert failed');" + " } " + "} " "this.t = 1; " "function f () { " "return this.t; "