Add assert function to stdlib. Useful for tests
This commit is contained in:
parent
7ea2c3e193
commit
781b3865c0
3 changed files with 14 additions and 0 deletions
|
@ -17,6 +17,7 @@ enum class ErrorCode {
|
|||
ArgumentCountMismatch,
|
||||
IOError,
|
||||
Interrupt,
|
||||
AssertionFailed,
|
||||
};
|
||||
|
||||
void seterr(const char* err);
|
||||
|
|
|
@ -53,6 +53,17 @@ Result<Value> stdlib_println(const Array& params) {
|
|||
|
||||
Result<Value> stdlib_prn(const Array& params) { return ERROR(NotImplemented); }
|
||||
|
||||
Result<Value> stdlib_assert(const Array& params) {
|
||||
for (uint64_t i = 0; i < params.size(); i++) {
|
||||
Value param = TRY(params.get(i));
|
||||
if (!param.is<Bool>()) return ERROR(AssertionFailed);
|
||||
auto v = param.to<Bool>()->value();
|
||||
if (!v) return ERROR(AssertionFailed);
|
||||
}
|
||||
|
||||
return Value(TRY(Nil::create()));
|
||||
}
|
||||
|
||||
#define STDLIB_FUNCTION(name, id) \
|
||||
[(uint64_t)StdlibFunctionId::id] = {#name, StdlibFunctionId::id, \
|
||||
stdlib_##name}
|
||||
|
@ -62,6 +73,7 @@ static StdlibFunctionEntry function_entries[] = {
|
|||
STDLIB_FUNCTION(print, Print),
|
||||
STDLIB_FUNCTION(println, PrintLn),
|
||||
STDLIB_FUNCTION(prn, Prn),
|
||||
STDLIB_FUNCTION(assert, Assert),
|
||||
[(uint64_t)StdlibFunctionId::Max] = {0, StdlibFunctionId::Max,
|
||||
stdlib_unknown},
|
||||
};
|
||||
|
|
|
@ -9,6 +9,7 @@ enum class StdlibFunctionId : uint64_t {
|
|||
Print,
|
||||
PrintLn,
|
||||
Prn,
|
||||
Assert,
|
||||
Max,
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue