Skip to content

Commit 308fc59

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 308fc59

File tree

3 files changed

+50
-35
lines changed

3 files changed

+50
-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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,33 @@ read_sources (const char *script_file_names[],
108108
}
109109
}
110110

111+
/**
112+
* Provide the 'assert' implementation for the engine.
113+
*
114+
* @return false - if the argument was a boolean value and it was false.
115+
* true - otherwise
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, /** < 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+
ret_val_p->type = JERRY_API_DATA_TYPE_STRING;
129+
ret_val_p->v_string = jerry_api_create_string ("Assert failed");
130+
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,18 @@ 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+
if (!is_assert_added)
276+
{
277+
JERRY_ERROR_MSG ("Failed to register 'assert' method.");
278+
}
279+
241280
jerry_completion_code_t ret_code = JERRY_COMPLETION_CODE_OK;
242281

243282
if (!jerry_parse (source_p, source_size))
@@ -253,6 +292,9 @@ main (int argc,
253292
}
254293
}
255294

295+
jerry_api_release_value (&assert_value);
296+
jerry_api_release_object (global_obj_p);
297+
256298
jerry_cleanup ();
257299

258300
#ifdef JERRY_ENABLE_LOG

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)