diff options
-rw-r--r-- | README.md | 5 | ||||
-rw-r--r-- | lib.c | 25 |
2 files changed, 29 insertions, 1 deletions
@@ -1108,3 +1108,8 @@ Throws an exception if the given prototype value is not an object. Pause execution for the given amount of milliseconds. Returns `false` if an invalid value was passed, otherwise `true`. + +#### 6.56. `assert(cond[, message])` + +Raise an exception with the given `message` parameter if the value in `cond` +is not truish. When `message` is omitted, the default value is `Assertion failed`. @@ -2338,6 +2338,28 @@ uc_sleep(uc_vm *vm, size_t nargs) return ucv_boolean_new(true); } +static uc_value_t * +uc_assert(uc_vm *vm, size_t nargs) +{ + uc_value_t *cond = uc_get_arg(0); + uc_value_t *msg = uc_get_arg(1); + bool freeable = false; + char *s; + + if (!uc_val_is_truish(cond)) { + s = msg ? uc_cast_string(vm, &msg, &freeable) : "Assertion failed"; + + uc_vm_raise_exception(vm, EXCEPTION_USER, "%s", s); + + if (freeable) + free(s); + + return NULL; + } + + return ucv_get(cond); +} + static const uc_cfunction_list functions[] = { { "chr", uc_chr }, { "delete", uc_delete }, @@ -2387,7 +2409,8 @@ static const uc_cfunction_list functions[] = { { "system", uc_system }, { "trace", uc_trace }, { "proto", uc_proto }, - { "sleep", uc_sleep } + { "sleep", uc_sleep }, + { "assert", uc_assert } }; |