- delete the nil type; catscript has no nil and no null - add the empty object (Object with no members), exposed as the global 'empty'; it is a shared immutable singleton - 'nil' is no longer a keyword: it is an undefined global, which raises - undefined global -> error (consistent with a missing attribute) - only 'false' is falsy (numbers/objects are truthy); booleans kept - OP_NIL -> OP_EMPTY; compiler, VM, stdlib, toml, ffi, generator, file, list, selector, value all migrated - rename the table type to Object - std/ front-end and examples/tests migrated to 'empty' - 84/84 tests
51 lines
1.5 KiB
Plaintext
51 lines
1.5 KiB
Plaintext
-- tests/ffi.cat -- edge cases for the `ffi` library.
|
|
--
|
|
-- Deterministic output (no getpid): loads, symbol binding, signatures,
|
|
-- return conversions, and the empty-on-error contract.
|
|
|
|
local m = ffi::load("libm.so.6")
|
|
local c = ffi::load("libc.so.6")
|
|
|
|
-- load failure / bad library -> empty
|
|
local bad = ffi::load("no_such_library_xyz.so.999")
|
|
print("bad load empty:", bad == empty)
|
|
print("bad load type:", type(bad))
|
|
|
|
-- symbol binding
|
|
local pow = m:func("pow", "double(double,double)")
|
|
print("pow bound:", pow ~= empty)
|
|
print("missing sym empty:", m:func("no_such_symbol_xyz", "int()") == empty)
|
|
|
|
-- invalid signature -> empty
|
|
print("bad sig empty:", m:func("pow", "not a signature") == empty)
|
|
print("bad rettype empty:", m:func("pow", "nope(double)") == empty)
|
|
|
|
-- return conversions
|
|
print("pow 2,10:", pow(2, 10))
|
|
print("pow 3,4:", pow(3, 4))
|
|
local floor = m:func("floor", "double(double)")
|
|
print("floor 3.7:", floor(3.7))
|
|
local sqrt = m:func("sqrt", "double(double)")
|
|
print("sqrt 144:", sqrt(144))
|
|
|
|
-- int return
|
|
local abs = c:func("abs", "int(int)")
|
|
print("abs -42:", abs(-42))
|
|
|
|
-- long + string arg
|
|
local strlen = c:func("strlen", "long(string)")
|
|
print("strlen abc:", strlen("abc"))
|
|
print("strlen empty:", strlen(""))
|
|
|
|
-- void return -> empty
|
|
local srand = c:func("srand", "void(uint32)")
|
|
print("void ret empty:", srand(1) == empty)
|
|
|
|
-- pointer-ish spelling accepted
|
|
local strchr = c:func("strchr", "string(string,int)")
|
|
print("strchr bound:", strchr ~= empty)
|
|
|
|
-- library is callable like :func
|
|
local cosf = m("cos", "double(double)")
|
|
print("cos 0:", cosf(0))
|