add general assert builtin
This commit is contained in:
parent
e2a1e5ff8d
commit
ddf92e1009
2 changed files with 23 additions and 0 deletions
20
core/vm.go
20
core/vm.go
|
|
@ -470,6 +470,26 @@ var DefaultGlobals = map[string]Value{
|
|||
nil,
|
||||
true,
|
||||
},
|
||||
"assert": &BuiltinFunctionValue{
|
||||
"assert",
|
||||
&FunctionSignature{
|
||||
[]TypeSignature{
|
||||
&BooleanSignature{},
|
||||
},
|
||||
&NilSignature{},
|
||||
},
|
||||
func(vm *VM, this Value, params []Value) (Value, error) {
|
||||
b := params[0].(*BoolValue)
|
||||
|
||||
if !b.Boolean {
|
||||
return nil, errors.New(fmt.Sprintf("assertion failed: %s", b))
|
||||
}
|
||||
|
||||
return &NilValue{}, nil
|
||||
},
|
||||
nil,
|
||||
false,
|
||||
},
|
||||
"assertEq": &BuiltinFunctionValue{
|
||||
"assertEq",
|
||||
&FunctionSignature{
|
||||
|
|
|
|||
|
|
@ -8,3 +8,6 @@ assertEq([3, 1, 4, 1], [3, 1, 4, 1])
|
|||
|
||||
# Inequality
|
||||
assertNotEq(2, 3)
|
||||
|
||||
assert(1 < 2)
|
||||
assert(1.0 < 2.0)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue