q66 / cffi-lua

A portable C FFI for Lua 5.1+
MIT License
176 stars 24 forks source link

Support passing string to `const void *` #8

Closed vsergeev closed 3 years ago

vsergeev commented 3 years ago
local ffi = jit and require('ffi') or require('cffi')

ffi.cdef[[
    //typedef intptr_t ssize_t;
    ssize_t write(int fd, const void *buf, size_t count);
]]

local s = "hello world\n"
ffi.C.write(1, s, #s) --> expected: no error, get: cannot convert 'string' to 'void const *'

The LuaJIT FFI will happily cast a string to a const void * (but not to void *, understandably), which is useful for interacting with C functions dealing with byte strings (e.g. file descriptor I/O, sockets, etc.). cffi-lua doesn't support this without an explicit cast ffi.cast('const void *', s).