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:
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user