Skip to content

Instantly share code, notes, and snippets.

@yihuang
Created January 1, 2016 10:13
Show Gist options
  • Select an option

  • Save yihuang/63290daa3e2fb607fee2 to your computer and use it in GitHub Desktop.

Select an option

Save yihuang/63290daa3e2fb607fee2 to your computer and use it in GitHub Desktop.
test lua intf, return pointer.
#include "LuaIntf/LuaIntf.h"
using namespace LuaIntf;
void* test_malloc(int n) {
return malloc(n);
}
void test_free(void* p) {
free(p);
}
void open(lua_State* L) {
LuaBinding(L).beginModule("test")
.addFunction("malloc", &test_malloc)
.addFunction("free", &test_free)
.endModule();
}
int main() {
LuaContext ctx;
open(ctx.state());
ctx.doString("test.free(test.malloc(10))");
}
// $ clang++ -std=c++11 test.cpp -llua
// $ ./a.out
// libc++abi.dylib: terminating with uncaught exception of type LuaIntf::LuaException: [string "test.free(test.malloc(10))"]:1: bad argument #-1 to 'malloc' (table expected, got nil)
[1] 12892 abort ./a.out
@VoidPointer1986
Copy link

You could define a LuaTypeMapping<void *> class, and create a lua table that contains low and high part of void *(consider 64bit platforms) in the push method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment