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
This commit is contained in:
Peter Gal
2015-06-17 17:04:13 +02:00
parent 61ab205130
commit 77b01a6473
3 changed files with 49 additions and 35 deletions
+3 -35
View File
@@ -383,28 +383,6 @@ create_op_meta_for_vlt (varg_list_type vlt, operand *res, operand *obj)
return ret;
}
static void
dump_assert (operand op)
{
switch (op.type)
{
case OPERAND_LITERAL:
{
const opcode_t opcode = getop_is_true_jmp_down (LITERAL_TO_REWRITE, 0, 2);
serializer_dump_op_meta (create_op_meta_100 (opcode, op.data.lit_id));
break;
}
case OPERAND_TMP:
{
const opcode_t opcode = getop_is_true_jmp_down (op.data.uid, 0, 2);
serializer_dump_op_meta (create_op_meta_000 (opcode));
break;
}
}
const opcode_t opcode = getop_exitval (1);
serializer_dump_op_meta (create_op_meta_000 (opcode));
}
static void
split_opcode_counter (opcode_counter_t oc, idx_t *id1, idx_t *id2)
{
@@ -742,25 +720,15 @@ dumper_finish_scope (void)
}
bool
dumper_is_intrinsic (operand obj)
dumper_is_intrinsic (operand /* obj */)
{
if (obj.type == OPERAND_LITERAL)
{
if (lit_literal_equal_type_zt (lit_get_literal_by_cp (obj.data.lit_id), (const ecma_char_t *) "assert"))
{
return true;
}
}
return false;
}
operand
dump_intrinsic (operand obj, operand arg)
dump_intrinsic (operand /* obj */, operand /* arg */)
{
JERRY_ASSERT (obj.type == OPERAND_LITERAL);
TODO (/* Rewrite when there will be more intrinsics. */)
JERRY_ASSERT (lit_literal_equal_type_zt (lit_get_literal_by_cp (obj.data.lit_id), (const ecma_char_t *) "assert"));
dump_assert (arg);
JERRY_UNREACHABLE ();
return dump_undefined_assignment_res ();
}
+41
View File
@@ -14,6 +14,7 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jerry.h"
@@ -106,6 +107,30 @@ read_sources (const char *script_file_names[],
}
}
/**
* Provide the 'assert' implementation for the engine.
*
* @return true - if the argument was not a boolean value or it was boolean true.
*/
static bool
assert_handler (const jerry_api_object_t *function_obj_p __attr_unused___, /** < function object */
const jerry_api_value_t *this_p __attr_unused___, /** < this arg */
jerry_api_value_t *ret_val_p __attr_unused___, /** < return argument */
const jerry_api_value_t args_p[], /** < function arguments */
const uint16_t args_cnt) /** < number of function arguments */
{
if (args_cnt > 0
&& args_p[0].type == JERRY_API_DATA_TYPE_BOOLEAN
&& args_p[0].v_bool != true)
{
JERRY_ERROR_MSG ("Script assertion failed\n");
exit (JERRY_STANDALONE_EXIT_CODE_FAIL);
}
return true;
} /* assert_handler */
int
main (int argc,
char **argv)
@@ -234,6 +259,22 @@ main (int argc,
jerry_init (flags);
jerry_api_object_t *global_obj_p = jerry_api_get_global ();
jerry_api_object_t *assert_func_p = jerry_api_create_external_function (assert_handler);
jerry_api_value_t assert_value;
assert_value.type = JERRY_API_DATA_TYPE_OBJECT;
assert_value.v_object = assert_func_p;
bool is_assert_added = jerry_api_set_object_field_value (global_obj_p, "assert", &assert_value);
jerry_api_release_value (&assert_value);
jerry_api_release_object (global_obj_p);
if (!is_assert_added)
{
JERRY_ERROR_MSG ("Failed to register 'assert' method.");
}
jerry_completion_code_t ret_code = JERRY_COMPLETION_CODE_OK;
if (!jerry_parse (source_p, source_size))
+5
View File
@@ -19,6 +19,11 @@
#include "test-common.h"
const char *test_source = (
"function assert (arg) { "
" if (!arg) { "
" throw Error('Assert failed');"
" } "
"} "
"this.t = 1; "
"function f () { "
"return this.t; "