Custom lua constructor with optional param name
| 9 | |
| 10 | // Custom lua constructor with optional param name |
| 11 | int lua_new_Parameter(lua_State *L) { |
| 12 | |
| 13 | const int args = lua_gettop(L); |
| 14 | if (args > 1) { |
| 15 | return luaL_argerror(L, args, "too many arguments"); |
| 16 | } |
| 17 | const char * name = nullptr; |
| 18 | if (args == 1) { |
| 19 | name = luaL_checkstring(L, 1); |
| 20 | } |
| 21 | |
| 22 | // This chunk is the same as the auto generated constructor |
| 23 | void *ud = lua_newuserdata(L, sizeof(Parameter)); |
| 24 | new (ud) Parameter(); |
| 25 | luaL_getmetatable(L, "Parameter"); |
| 26 | lua_setmetatable(L, -2); |
| 27 | |
| 28 | if (args == 0) { |
| 29 | // no arguments, nothing to do |
| 30 | return 1; |
| 31 | } |
| 32 | |
| 33 | if (!static_cast<Parameter*>(ud)->init(name)) { |
| 34 | return luaL_error(L, "No parameter: %s", name); |
| 35 | } |
| 36 | |
| 37 | return 1; |
| 38 | } |
| 39 | |
| 40 | // init by name |
| 41 | bool Parameter::init(const char *name) |
nothing calls this directly
no test coverage detected