summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--README.md5
-rw-r--r--lib.c25
2 files changed, 29 insertions, 1 deletions
diff --git a/README.md b/README.md
index 7ceb8df..549459e 100644
--- a/README.md
+++ b/README.md
@@ -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`.
diff --git a/lib.c b/lib.c
index 93798b8..c1080e3 100644
--- a/lib.c
+++ b/lib.c
@@ -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 }
};