Skip to content

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 3 additions & 35 deletions jerry-core/parser/js/opcodes-dumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
Copy link
Member

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.

Copy link
Contributor Author

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.


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 ();
}

Expand Down
41 changes: 41 additions & 0 deletions main-linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "jerry.h"
Expand Down Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, it is better to just call exit (JERRY_STANDALONE_EXIT_CODE_FAIL) in the handler, as anyway, in case of assertion failure, engine should be terminated, indicating failure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also print out an Assert failed message with JERRY_ERROR_MSG?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is good idea to print message. Maybe, Script assertion failed to distinguish between script assertions and engine internal assertions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
Expand Down Expand Up @@ -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))
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; "
Expand Down