lua如何嵌入java_嵌入时如何使用LuaJIT的ffi模块?
我正在尝试将LuaJIT嵌入到C应用程序中 . 代码是这样的:
#include
#include
#include
#include
#include
int barfunc(int foo)
{
/* a dummy function to test with FFI */
return foo + 1;
}
int
main(void)
{
int status, result;
lua_State *L;
L = luaL_newstate();
luaL_openlibs(L);
/* Load the file containing the script we are going to run */
status = luaL_loadfile(L, "hello.lua");
if (status) {
fprintf(stderr, "Couldnt load file: %s ", lua_tostring(L, -1));
exit(1);
}
/* Ask Lua to run our little script */
result = lua_pcall(L, 0, LUA_MULTRET, 0);
if (result) {
fprintf(stderr, "Failed to run script: %s ", lua_tostring(L, -1));
exit(1);
}
lua_close(L); /* Cya, Lua */
return 0;
}
Lua代码是这样的:
-- Test FFI
local ffi = require("ffi")
ffi.cdef[[
int barfunc(int foo);
]]
local barreturn = ffi.C.barfunc(253)
io.write(barreturn)
io.write( )
它报告错误如下:
Failed to run script: hello.lua:6: cannot resolve symbol barfunc.
我四处搜索,发现ffi模块上的文档确实很少 . 非常感谢 .
我正在尝试将LuaJIT嵌入到C应用程序中 . 代码是这样的: #include #include #include #include #include int barfunc(int foo) { /* a dummy function to test with FFI */ return foo + 1; } int main(void) { int status, result; lua_State *L; L = luaL_newstate(); luaL_openlibs(L); /* Load the file containing the script we are going to run */ status = luaL_loadfile(L, "hello.lua"); if (status) { fprintf(stderr, "Couldnt load file: %s ", lua_tostring(L, -1)); exit(1); } /* Ask Lua to run our little script */ result = lua_pcall(L, 0, LUA_MULTRET, 0); if (result) { fprintf(stderr, "Failed to run script: %s ", lua_tostring(L, -1)); exit(1); } lua_close(L); /* Cya, Lua */ return 0; } Lua代码是这样的: -- Test FFI local ffi = require("ffi") ffi.cdef[[ int barfunc(int foo); ]] local barreturn = ffi.C.barfunc(253) io.write(barreturn) io.write( ) 它报告错误如下: Failed to run script: hello.lua:6: cannot resolve symbol barfunc. 我四处搜索,发现ffi模块上的文档确实很少 . 非常感谢 .