Skip to content

Commit 221d2a4

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 221d2a4

File tree

3 files changed

+51
-35
lines changed

3 files changed

+51
-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: 43 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,32 @@ read_sources (const char *script_file_names[],
108109
}
109110
}
110111

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

239266
plugin_io_init ();
240267

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

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