- '::' accesses a static/class member (here the module namespace ffi), '.'/':' sends to an instance. ffi is a module, so ffi::load is the canonical form; the native now accepts the receiver passed by '::'. - example, tests and README use ffi::load; '.load' still works (compat). - 69/69 tests
23 lines
688 B
Plaintext
23 lines
688 B
Plaintext
-- examples/ffi.cat -- call native code through the `ffi` library.
|
|
--
|
|
-- Like Python's ctypes/CDLL: load a shared object, bind a symbol with a
|
|
-- signature, and call it. libffi performs the ABI-correct call.
|
|
|
|
local m = ffi::load("libm.so.6")
|
|
local pow = m:func("pow", "double(double,double)")
|
|
print("pow(2,10) =", pow(2, 10))
|
|
|
|
local sqrt = m:func("sqrt", "double(double)")
|
|
print("sqrt(144) =", sqrt(144))
|
|
|
|
local c = ffi::load("libc.so.6")
|
|
local getpid = c:func("getpid", "int()")
|
|
local pid = getpid()
|
|
print("getpid > 0:", pid > 0)
|
|
|
|
local strlen = c:func("strlen", "long(string)")
|
|
print("strlen:", strlen("catscript"))
|
|
|
|
local abs = c:func("abs", "int(int)")
|
|
print("abs(-42) =", abs(-42))
|