-
Notifications
You must be signed in to change notification settings - Fork 683
Make the assert an external method #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include "jerry.h" | ||
|
@@ -108,6 +109,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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe, it is better to just call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also print out an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is good idea to print message. Maybe, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good, will update the PR in a jiffy. |
||
} /* assert_handler */ | ||
|
||
|
||
int | ||
main (int argc, | ||
char **argv) | ||
|
@@ -238,6 +263,22 @@ main (int argc, | |
|
||
plugin_io_init (); | ||
|
||
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)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we remove these two functions at all? They seems unnecessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe that would be better in a different PR where all intrinsic like things are removed.