Last active
February 26, 2023 17:33
-
-
Save huxuan/929e4abc7e8883f4bce47b02faf54db5 to your computer and use it in GitHub Desktop.
Hello World for LuaJIT FFI/C++ binding.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Act as a source file in existing project. | |
#include "hello.h" | |
const char* Hello::World() | |
{ | |
return "Hello World!\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Act as a header file in existing project. | |
class Hello { | |
public: | |
const char* World(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Lua part binding, load necessary interface. | |
ffi = require('ffi') | |
ffi.cdef[[ | |
typedef struct Hello Hello; | |
Hello* Hello_new(); | |
const char* Hello_World(Hello*); | |
void Hello_gc(Hello*); | |
]] | |
hello = ffi.load('hello') | |
hello_index = { | |
World = hello.Hello_World | |
} | |
hello_mt = ffi.metatype('Hello', { | |
__index = hello_index | |
}) | |
Hello = ffi.gc(hello.Hello_new(), hello.Hello_gc) | |
return Hello |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// C++ part binding, expose necessary interface to C. | |
#include "hello.h" | |
extern "C" { | |
Hello* Hello_new(){ | |
return new Hello; | |
} | |
const char* Hello_World(Hello* self){ | |
return self->World(); | |
} | |
void Hello_gc(Hello* self) { | |
delete self; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Demo how to use binding in lua project. | |
hello = require('hello') | |
ffi = require('ffi') | |
io.write(ffi.string(hello:World())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: lib run | |
lib: | |
g++ -shared -fPIC -o libhello.so libhello.cpp hello.cpp | |
run: | |
luajit main.lua | |
clean: | |
rm *.so |
在lua里调用 hello:World() 之前 collectgarbage 会崩溃,请问你遇到过吗?
`hello = require('hello');collectgarbage("collect");
hello:World(); ---- 崩溃
`
不好意思,这个是好几年前的代码了,已经很久不搞这些了 - -
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
在lua里调用 hello:World() 之前 collectgarbage 会崩溃,请问你遇到过吗?
`hello = require('hello');
collectgarbage("collect");
hello:World(); ---- 崩溃
`