mascarenhas / alien Goto Github PK
View Code? Open in Web Editor NEWA C FFI for Lua
Home Page: http://mascarenhas.github.com/alien/
A C FFI for Lua
Home Page: http://mascarenhas.github.com/alien/
How to set the lua side of a void argument in C?
example:
C side:
cat libtest.c
int five(void) {
return 5;
}
lua side:
so = require"alien"
tst = so.load("test")
tst.five:types("int", "void")
assert( tst.five() == 5) -- error: two few arguments (function five)
assert( tst.five(1) == 5) -- error: paramenter 2 is of unknow type (function five)
take around the bug:
so = require"alien"
tst = so.load("test")
tst.five:types("int", "int")
assert( tst.five(0) == 5) -- ok
In alien_buffer_new we do not set lua_setuservalue (lua_setfenv on lua5.1)
In alien_buffer_get we call lua_getuservalue(L, 1); that return _G
And we lookup functions in global.
so call
buf:tostring(3,2) become to _G.tostring(buf, 3, 2)
Hi,
I'm doing af alien ffi wrapper for some library.
The library have a nasty habit of returning structs - like this:
struct version_info
{
int major;
int minor;
int micro;
};
struct version_info some_library_get_version(void)
{
struct version_info ver;
ver.major = MAJOR_VERSION;
ver.minor = MINOR_VERSION;
ver.micro = MICRO_VERSION;
return ver;
}
How do I represent this through lua/alien ?
Thanks in advance
/Uffe
Hi Fabio:
When I try to install alien using luarocks, it finds ffi ok, but I get an error that lua.h is not correct version, as follows:
Installing http://www.luarocks.org/repositories/rocks/alien-0.7.0-1.src.rock...
Using http://www.luarocks.org/repositories/rocks/alien-0.7.0-1.src.rock... switching to 'build' mode
Archive: /tmp/luarocks_luarocks-rock-alien-0.7.0-1-7965/alien-0.7.0-1.src.rock
...
checking ffi.h usability... yes
checking ffi.h presence... yes
checking for ffi.h... yes
checking for ffi_closure_alloc in -lffi... yes
...
checking Lua version is in range 501 <= v < 503... yes
checking lua.h usability... yes
checking lua.h presence... yes
checking for lua.h... yes
checking lualib.h usability... yes
checking lualib.h presence... yes
checking for lualib.h... yes
checking lua.h version is in range 501 <= v < 503... no
configure: error: in /tmp/luarocks_alien-0.7.0-1-9794/alien-0.7.0': configure: error: lua.h version not in desired range See
config.log' for more details
Error: Build error: Failed building.
So I look in lua.h,
% grep LUA_VERSION ~/SANDBOX/include/lua.h
which looks like lua.h is a valid version. Is there an issue with the build/install script? I've been able to successfully install other lua rocks that need compiling (eg NumLua).
Thanks,
--greg
I'm writing some Lua code (GNU Zile) that would gain a huge speed boost by using memrchr
to scan back through an alien.buffer
-- but memrchr
is a GNU extension not available on Mac OS X among others. I have a brute force reverse search implemented in Lua over an alien.array
, but I'd like to have that as a fallback preferring memrchr
from libc when available.
Unfortunately, alien
throws an error during parsing:
$ lua52 -l alien -e 'print (tostring (alien.default.memchr)))'
alien function memchr, library default
$ lua52 -l alien -e 'print (tostring (alien.default.memrchr)))'
/usr/local/opt/lua52/bin/lua: dlsym(RTLD_DEFAULT, memrchr): symbol not found
stack traceback:
[C]: in function '__index'
(command line):1: in main chunk
[C]: in ?
$ lua52 -l alien -e 'if false then print (tostring (alien.default.memrchr))) end'
/usr/local/opt/lua52/bin/lua: dlsym(RTLD_DEFAULT, memrchr): symbol not found
stack traceback:
[C]: in function '__index'
(command line):1: in main chunk
[C]: in ?
The best workaround I have is to write a test function in a string, and call loadstring() inside pcall to protect from the parse-time error.
Much better would be simply return nil
for missing symbols, and then I could write much cleaner code:
local def = alien.default
if def.memrchr ~= nil then
def.memrchr:types ("pointer", "pointer", "int", "size_t")
else
function memrchr (buf, ch, o) ... end
end
Thoughts? I'll try to write a patch if you agree this is a good way forward.
how to trancode alien to wasm, please?
I want to call dlls in lua by webassembly from browser, can it success?
just like lua-cjson-wasm, lua-wasm
https://github.com/mmaarouf/lua-cjson-wasm
https://github.com/throwarray/lua-wasm
It would be nice to have alien
support the C bool
type, automatically cast from/to Lua's native boolean
.
This adds Lua 5.3 support and fixes Mingwin support.
This https://github.com/mascarenhas/alien/blob/master/src/alien.lua#L23 breaks with recent versions of LuaJIT, because it is strict about escape sequences ("\."
is not a valid escape sequence). This is probably triggered by recent versions of Lua 5.2 as well.
Installing https://luarocks.org/alien-0.7.1-2.src.rock
./bootstrap && ./configure LUA=/usr/local/openresty/luajit//bin/luajit CPPFLAGS='-I/usr/local/openresty/luajit/include/luajit-2.1 -I/usr/include' LDFLAGS=-L/usr/lib/x86_64-linux-gnu --prefix=/home/ryan/.luarocks/lib/luarocks/rocks/alien/0.7.1-2 --libdir=/home/ryan/.luarocks/lib/luarocks/rocks/alien/0.7.1-2/lib --datadir=/home/ryan/.luarocks/lib/luarocks/rocks/alien/0.7.1-2/lua && make clean && make
bootstrap: running: libtoolize --quiet
bootstrap: running: autoreconf --symlink --install
configure.ac:11: installing 'build-aux/ar-lib'
configure.ac:11: installing 'build-aux/compile'
configure.ac:18: installing 'build-aux/config.guess'
configure.ac:18: installing 'build-aux/config.sub'
configure.ac:6: installing 'build-aux/install-sh'
configure.ac:6: installing 'build-aux/missing'
Makefile.am: installing 'build-aux/depcomp'
bootstrap: Done. Now you can run './configure'.
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking whether make supports nested variables... (cached) yes
checking for style of include used by make... GNU
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking whether gcc understands -c and -o together... yes
checking dependency style of gcc... gcc3
checking for ar... ar
checking the archiver (ar) interface... ar
checking for gcc... (cached) gcc
checking whether we are using the GNU C compiler... (cached) yes
checking whether gcc accepts -g... (cached) yes
checking for gcc option to accept ISO C89... (cached) none needed
checking whether gcc understands -c and -o together... (cached) yes
checking dependency style of gcc... (cached) gcc3
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking how to print strings... printf
checking for a sed that does not truncate output... /bin/sed
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for fgrep... /bin/grep -F
checking for ld used by gcc... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B
checking the name lister (/usr/bin/nm -B) interface... BSD nm
checking whether ln -s works... yes
checking the maximum length of command line arguments... 1572864
checking how to convert x86_64-pc-linux-gnu file names to x86_64-pc-linux-gnu format... func_convert_file_noop
checking how to convert x86_64-pc-linux-gnu file names to toolchain format... func_convert_file_noop
checking for /usr/bin/ld option to reload object files... -r
checking for objdump... objdump
checking how to recognize dependent libraries... pass_all
checking for dlltool... no
checking how to associate runtime and link libraries... printf %s\n
checking for archiver @FILE support... @
checking for strip... strip
checking for ranlib... ranlib
checking command to parse /usr/bin/nm -B output from gcc object... ok
checking for sysroot... no
checking for a working dd... /bin/dd
checking how to truncate binary pipes... /bin/dd bs=4096 count=1
checking for mt... mt
checking if mt is a manifest tool... no
checking how to run the C preprocessor... gcc -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for dlfcn.h... yes
checking for objdir... .libs
checking if gcc supports -fno-rtti -fno-exceptions... no
checking for gcc option to produce PIC... -fPIC -DPIC
checking if gcc PIC flag -fPIC -DPIC works... yes
checking if gcc static flag -static works... yes
checking if gcc supports -c -o file.o... yes
checking if gcc supports -c -o file.o... (cached) yes
checking whether the gcc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no
checking for ANSI C header files... (cached) yes
checking for size_t... yes
checking for working alloca.h... yes
checking for alloca... yes
checking ffi.h usability... yes
checking ffi.h presence... yes
checking for ffi.h... yes
checking for ffi_closure_alloc in -lffi... yes
checking for stdint.h... (cached) yes
checking if /usr/local/openresty/luajit//bin/luajit is a Lua interpreter... yes
checking whether /usr/local/openresty/luajit//bin/luajit version >= 5.1, < 5.4... yes
checking for /usr/local/openresty/luajit//bin/luajit version... 5.1
checking for /usr/local/openresty/luajit//bin/luajit platform... unknown
checking for /usr/local/openresty/luajit//bin/luajit script directory... ${prefix}/share/lua/5.1
checking for /usr/local/openresty/luajit//bin/luajit module directory... ${exec_prefix}/lib/lua/5.1
checking if LUA_VERSION is defined... yes
checking lua.h usability... yes
checking lua.h presence... yes
checking for lua.h... yes
checking lualib.h usability... yes
checking lualib.h presence... yes
checking for lualib.h... yes
checking lauxlib.h usability... yes
checking lauxlib.h presence... yes
checking for lauxlib.h... yes
checking luaconf.h usability... yes
checking luaconf.h presence... yes
checking for luaconf.h... yes
checking for Lua header version... 5.1
checking if Lua header version matches 5.1... yes
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating alien-0.7.1-1.rockspec
config.status: creating config.h
config.status: executing depfiles commands
config.status: executing libtool commands
test -z "tests/libalientest.la" || rm -f tests/libalientest.la
rm -f tests/so_locations
test -z "src/alien_c.la" || rm -f src/alien_c.la
rm -f src/so_locations
rm -rf .libs _libs
rm -rf src/.libs src/_libs
rm -rf tests/.libs tests/_libs
rm -f *.o
rm -f src/*.o
rm -f src/*.lo
rm -f tests/*.o
rm -f tests/*.lo
rm -f *.lo
make all-am
make[1]: Entering directory '/tmp/luarocks_alien-0.7.1-2-WRCouJ/alien'
CC src/alien.lo
src/alien.c:93:14: error: static declaration of ‘luaL_testudata’ follows non-static declaration
static void *luaL_testudata(lua_State *L, int ud, const char *tname) {
^
In file included from src/alien.c:67:0:
/usr/local/openresty/luajit/include/luajit-2.1/lauxlib.h:91:19: note: previous declaration of ‘luaL_testudata’ was here
LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname);
^
Makefile:597: recipe for target 'src/alien.lo' failed
make[1]: *** [src/alien.lo] Error 1
make[1]: Leaving directory '/tmp/luarocks_alien-0.7.1-2-WRCouJ/alien'
Makefile:411: recipe for target 'all' failed
make: *** [all] Error 2
Error: Build error: Failed building.
Run-Time Check Failure #3 - The variable 'args' is being used without being initialized.
If the target function have no parameters, then the args variable is not initialized.
Suggest fix is core.c line 737:
void **args=0;
alien.callback seem to be ignoring the parameter type info and always passing 0 parameters to the Lua calback function. I'm unsure how to fix this.
In "alien.c" in "alien_buffer_gc" @ line #934:
"lua_pop(L, 2);"
When "LUA_USE_APICHECK" is defined, this line will trigger a "invalid new top".
This because there is only one stack index (the __gc userdata argument) not two.
And actually since there is no return result this call is pointless.
The solution is to just delete this unnecessary "lua_pop(L, 2);" line.
Note to readers: In your debug Lua builds that use 3rd party libraries, it's a good idea to define LUA_USE_APICHECK as it will catch odd things like this that corrupt your Lua stack, Lua stack overflows, etc.
Running test_alien.lua by Lua compiled with LUA_USE_APICHECK fails:
Assertion failed: L->top < L->ci->top, file lapi.c, line 432
I reach the make step after bootstrap and configure are done successfully. The make step gives the error:
libtool: error: can't build i686-w64-mingw32 shared library unless -no-undefined is specified
How can I fix this?
On windows
is there an updated version of test_alien.lua calling the function getSPAMANDEGGS which fetches a structure which contains a nested structure. this function is defined in alientest.c but not used in test_alien.lua.
Good morning, Fabio.
I am trying to use Alien 0.7 but I have several difficulties:
If you need more details, I can give'em.
Thanks friend.
HERNAN CANO M
Systems Analyst
details and solution: http://lua-users.org/lists/lua-l/2010-09/msg00806.html
One API I use assume deregistration of callback: it expects that user will pass NULL as pointer to function. Unfortunatelly, LuaAlien does not allow it.
Now problem with Windows that there no config.h
files. And it require install mingw to be able use configure.
I add this config.h file to my project
/* confdefs.h */
#define PACKAGE_NAME "alien"
#define PACKAGE_TARNAME "alien"
#define PACKAGE_VERSION "0.7.1"
#define PACKAGE_STRING "alien 0.7.1"
#define PACKAGE_BUGREPORT "http://mascarenhas.github.com/alien"
#define PACKAGE_URL ""
#define PACKAGE "alien"
#define VERSION "0.7.1"
#define STDC_HEADERS 1
#define HAVE_SYS_TYPES_H 1
#define HAVE_SYS_STAT_H 1
#define HAVE_STDLIB_H 1
#define HAVE_STRING_H 1
#define HAVE_MEMORY_H 1
#define HAVE_STDINT_H 1
#define LT_OBJDIR ".libs/"
#define STDC_HEADERS 1
#define HAVE_FFI_H 1
And I can compile alien
with MSVC (with my fixes #49 , #50 , #51 ) and all tests pass.
Also I add rockspec file to be able install it on Windows.
build = {
type = "command",
build_command = "./bootstrap && ./configure LUA=$(LUA) CPPFLAGS='-I$(LUA_INCDIR) -I$(FFI_INCDIR)' LDFLAGS=-L$(FFI_LIBDIR) --prefix=$(PREFIX) --libdir=$(LIBDIR) --datadir=$(LUADIR) && make clean && make",
install_command = "make install",
copy_directories = {},
platforms = { windows = {
type = "builtin",
copy_directories = {},
modules = {
alien_c = {
sources = {"src/alien.c"},
defines = {"FFI_BUILDING", "WINDOWS"},
libraries = {"ffi"},
incdirs = {"$(FFI_INCDIR)"},
libdirs = {"$(FFI_LIBDIR)"}
},
alien = 'src/alien.lua',
}
}}
}
>luarocks install https://raw.githubusercontent.com/moteus/alien/master/rockspecs/alien-scm-0.rockspec
Cloning into 'alien'...
remote: Counting objects: 2559, done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 2559 (delta 17), reused 0 (delta 0), pack-reused 2527
Receiving objects: 100% (2559/2559), 2.58 MiB | 119.00 KiB/s, done.
Resolving deltas: 100% (1498/1498), done.
Checking connectivity... done.
cl /nologo /MD /O2 -c -Fosrc/alien.obj -Ic:/Lua/5.1/include/ src/alien.c -DFFI_BUILDING -DWINDOWS -Ic:/lua/external/include
alien.c
link -dll -def:alien_c.def -out:alien_c.dll c:/Lua/5.1/lib/lua51.lib src/alien.obj -libpath:c:/lua/external/lib ffi.lib
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
Creating library alien_c.lib and object alien_c.exp
alien scm-0 is now installed in c:\Lua\5.1\systree (license: MIT/X11)
Also I add travis file to run tests on Travis CI service.
Please put up-to-date documentation on github-pages, and remove the URL pointing to LuaForge, as that's clearly defunct now.
alien.c:358:54: error: ‘FFI_SYSV’ undeclared here (not in a function)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.