Skip to content

Commit b76bd72

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 b76bd72

File tree

3 files changed

+59
-35
lines changed

3 files changed

+59
-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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,42 @@ 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+
130+
if (args_cnt > 1
131+
&& args_p[1].type == JERRY_API_DATA_TYPE_STRING)
132+
{
133+
ret_val_p->v_string = args_p[1].v_string;
134+
}
135+
else
136+
{
137+
ret_val_p->v_string = jerry_api_create_string ("Assert failed");
138+
}
139+
140+
return false;
141+
}
142+
143+
return true;
144+
} /* assert_handler */
145+
146+
111147
int
112148
main (int argc,
113149
char **argv)
@@ -238,6 +274,18 @@ main (int argc,
238274

239275
plugin_io_init ();
240276

277+
jerry_api_object_t *global_obj_p = jerry_api_get_global ();
278+
jerry_api_object_t *assert_func_p = jerry_api_create_external_function (assert_handler);
279+
jerry_api_value_t assert_value;
280+
assert_value.type = JERRY_API_DATA_TYPE_OBJECT;
281+
assert_value.v_object = assert_func_p;
282+
283+
bool is_assert_added = jerry_api_set_object_field_value (global_obj_p, "assert", &assert_value);
284+
if (!is_assert_added)
285+
{
286+
JERRY_ERROR_MSG ("Failed to register 'assert' method.");
287+
}
288+
241289
jerry_completion_code_t ret_code = JERRY_COMPLETION_CODE_OK;
242290

243291
if (!jerry_parse (source_p, source_size))
@@ -253,6 +301,9 @@ main (int argc,
253301
}
254302
}
255303

304+
jerry_api_release_value (&assert_value);
305+
jerry_api_release_object (global_obj_p);
306+
256307
jerry_cleanup ();
257308

258309
#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)