bool add_enum_to_lua(lua_State* L, const char* tname, ...) { // NOTE: Here's the Lua code we're building and executing to define the // enum. // // = setmetatable( {}, { // __index = { // = , // }, // ... // }, // __newindex = function(table, key, value) // error(\"Attempt to modify read-only table\") // end, // __metatable = false // }); va_list args; string code; char* ename; int evalue; code = tname; code += " = setmetatable({}, {" "__index = {"; // Iterate over the variadic arguments adding the enum values. va_start(args, tname); while((ename = va_arg(args, char*)) != 0) { evalue = va_arg(args, int); code += string(ename) + "=" + itos(evalue) + ","; } va_end(args); code += "}," "__newindex = function(table, key, value) error(\"Attempt to modify read-only table\") end," "__metatable = false} )"; // Execute lua code if ( luaL_loadbuffer(L, code.c_str(), code.length(), 0) || lua_pcall(L, 0, 0, 0) ) { fprintf(stderr, "%s\n\n%s\n", code.c_str(),lua_tostring(L, -1)); lua_pop(L, 1); return false; } return true; }