Skip to content

Commit afe3355

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 pgal.u-szeged@partner.samsung.com
1 parent de7b72d commit afe3355

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"
@@ -108,6 +109,30 @@ read_sources (const char *script_file_names[],
108109
}
109110
}
110111

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

239264
plugin_io_init ();
240265

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

243284
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)