Skip to content

Commit 77b01a6

Browse files
committed
Provide assert as an external method.
Removed the internal assert implementation from the engine and provide externally an assert function via api calls. JerryScript-DCO-1.0-Signed-off-by: Peter Gal [email protected]
1 parent 61ab205 commit 77b01a6

File tree

3 files changed

+49
-35
lines changed

3 files changed

+49
-35
lines changed

jerry-core/parser/js/opcodes-dumper.cpp

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -383,28 +383,6 @@ create_op_meta_for_vlt (varg_list_type vlt, operand *res, operand *obj)
383383
return ret;
384384
}
385385

386-
static void
387-
dump_assert (operand op)
388-
{
389-
switch (op.type)
390-
{
391-
case OPERAND_LITERAL:
392-
{
393-
const opcode_t opcode = getop_is_true_jmp_down (LITERAL_TO_REWRITE, 0, 2);
394-
serializer_dump_op_meta (create_op_meta_100 (opcode, op.data.lit_id));
395-
break;
396-
}
397-
case OPERAND_TMP:
398-
{
399-
const opcode_t opcode = getop_is_true_jmp_down (op.data.uid, 0, 2);
400-
serializer_dump_op_meta (create_op_meta_000 (opcode));
401-
break;
402-
}
403-
}
404-
const opcode_t opcode = getop_exitval (1);
405-
serializer_dump_op_meta (create_op_meta_000 (opcode));
406-
}
407-
408386
static void
409387
split_opcode_counter (opcode_counter_t oc, idx_t *id1, idx_t *id2)
410388
{
@@ -742,25 +720,15 @@ dumper_finish_scope (void)
742720
}
743721

744722
bool
745-
dumper_is_intrinsic (operand obj)
723+
dumper_is_intrinsic (operand /* obj */)
746724
{
747-
if (obj.type == OPERAND_LITERAL)
748-
{
749-
if (lit_literal_equal_type_zt (lit_get_literal_by_cp (obj.data.lit_id), (const ecma_char_t *) "assert"))
750-
{
751-
return true;
752-
}
753-
}
754725
return false;
755726
}
756727

757728
operand
758-
dump_intrinsic (operand obj, operand arg)
729+
dump_intrinsic (operand /* obj */, operand /* arg */)
759730
{
760-
JERRY_ASSERT (obj.type == OPERAND_LITERAL);
761-
TODO (/* Rewrite when there will be more intrinsics. */)
762-
JERRY_ASSERT (lit_literal_equal_type_zt (lit_get_literal_by_cp (obj.data.lit_id), (const ecma_char_t *) "assert"));
763-
dump_assert (arg);
731+
JERRY_UNREACHABLE ();
764732
return dump_undefined_assignment_res ();
765733
}
766734

main-linux.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
#include <stdio.h>
17+
#include <stdlib.h>
1718
#include <string.h>
1819

1920
#include "jerry.h"
@@ -106,6 +107,30 @@ read_sources (const char *script_file_names[],
106107
}
107108
}
108109

110+
/**
111+
* Provide the 'assert' implementation for the engine.
112+
*
113+
* @return true - if the argument was not a boolean value or it was boolean true.
114+
*/
115+
static bool
116+
assert_handler (const jerry_api_object_t *function_obj_p __attr_unused___, /** < function object */
117+
const jerry_api_value_t *this_p __attr_unused___, /** < this arg */
118+
jerry_api_value_t *ret_val_p __attr_unused___, /** < return argument */
119+
const jerry_api_value_t args_p[], /** < function arguments */
120+
const uint16_t args_cnt) /** < number of function arguments */
121+
{
122+
if (args_cnt > 0
123+
&& args_p[0].type == JERRY_API_DATA_TYPE_BOOLEAN
124+
&& args_p[0].v_bool != true)
125+
{
126+
JERRY_ERROR_MSG ("Script assertion failed\n");
127+
exit (JERRY_STANDALONE_EXIT_CODE_FAIL);
128+
}
129+
130+
return true;
131+
} /* assert_handler */
132+
133+
109134
int
110135
main (int argc,
111136
char **argv)
@@ -234,6 +259,22 @@ main (int argc,
234259

235260
jerry_init (flags);
236261

262+
jerry_api_object_t *global_obj_p = jerry_api_get_global ();
263+
jerry_api_object_t *assert_func_p = jerry_api_create_external_function (assert_handler);
264+
jerry_api_value_t assert_value;
265+
assert_value.type = JERRY_API_DATA_TYPE_OBJECT;
266+
assert_value.v_object = assert_func_p;
267+
268+
bool is_assert_added = jerry_api_set_object_field_value (global_obj_p, "assert", &assert_value);
269+
270+
jerry_api_release_value (&assert_value);
271+
jerry_api_release_object (global_obj_p);
272+
273+
if (!is_assert_added)
274+
{
275+
JERRY_ERROR_MSG ("Failed to register 'assert' method.");
276+
}
277+
237278
jerry_completion_code_t ret_code = JERRY_COMPLETION_CODE_OK;
238279

239280
if (!jerry_parse (source_p, source_size))

tests/unit/test-api.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
#include "test-common.h"
2020

2121
const char *test_source = (
22+
"function assert (arg) { "
23+
" if (!arg) { "
24+
" throw Error('Assert failed');"
25+
" } "
26+
"} "
2227
"this.t = 1; "
2328
"function f () { "
2429
"return this.t; "

0 commit comments

Comments
 (0)