0

I have a big object constructed from C++ (that I have exposed to Lua) and I want to be processed in Lua.

I can pass any simple type to a Lua function (int, string) using lua_pushinteger, lua_pushstring but not a C++ class. I have tried lua_pushlightuserdata by pushing a pointer to my object but no luck.

function process(object) --object is the c++ passed function
--return processed data
end

How can I do that? Is that even possible?

Please note that I want to pass to Lua a specific instance constructed in C++. I can expose the constructor and simply use class but I will need to make the class a singleton. This is not acceptable.

cprogrammer
  • 5,503
  • 3
  • 36
  • 56
  • [Here](https://stackoverflow.com/questions/52564364/automated-lua-binding-using-c/52564775#52564775) is an example of registering a class type using Lua Bridge. Note that you can override the `__call` operator to prevent a class from being constructed (or in your case return the Singleton, see : DoNotCreate and DoNotGarbageCollect in linked example). From there you would get a LuaRef to the `process` function and pass the object as a parameter. LuaBridge automatically converts the pointer to a LuaRef (lightuserdata) that can be used in the lua script. – James Poag Oct 18 '18 at 17:22
  • Note that when you call a member function in Lua, you use the `object:memberfun(...)` syntax as `:` passes the `object` as a hidden `this` parmeter. – James Poag Oct 18 '18 at 17:24
  • @JamesPoag I did registered the class successfully. Did mentioned "(that I have exposed (the class) to Lua)". The only problem I have is I cannot pass the class as argument to a function. Once again, I can pass simple types (int, string) nut not a class. – cprogrammer Oct 18 '18 at 17:30
  • Are you passing a Pointer? – James Poag Oct 18 '18 at 17:33
  • Yes. I am passing a pointer (to my class) to lua_pushlightuserdata . lua_pushlightuserdata only accepts a pointer. – cprogrammer Oct 18 '18 at 17:35
  • What is the code you are calling to pass the object? – James Poag Oct 18 '18 at 17:35
  • lua_getglobal(L, "process"); lua_pushlightuserdata (L, new A()); <--- peseudo-code lua_call(L, 1, 1); /* 1 argument, return 1 result */ – cprogrammer Oct 18 '18 at 17:42
  • Are you using Lua Bridge? – James Poag Oct 18 '18 at 17:46
  • If you registered the class using Luabridge, then when you push an object using luabridge it will set a lot of 'metadata' information. This means that instead of using raw LUA calls, you should pipe everything through LuaRef – James Poag Oct 18 '18 at 17:50

1 Answers1

1

If you've registered your class using LuaBridge, then you have to push the Object pointer into the Lua State machine using the LuaBridge methods. LuaBridge pushes the pointer and then sets meta information on the object using lua_setmetatable. This metatable contains the actual description of the object.

using namespace luabridge;
// L = lua_state*

// Register class
getGlobalNamespace(L)
.beginClass<MyObjectClass>("MyObjectClass")
    .addData<float>("value", &MyObjectClass::value)
    .addFunction("method", &MyObjectClass::method)
.endClass();

MyObjectClass myObject; // single instance

// lookup script function in global table
LuaRef processFunc = getGlobal(L, "process");

if(processFunc.isFunction()){
     processFunc(&myObject);
}

Lua script

function process(object) --object is the c++ passed function
   local a = object:method("params")
   return object.value * a;
end
James Poag
  • 2,320
  • 1
  • 13
  • 20