Created
March 15, 2019 03:45
-
-
Save echiesse/adbd7cfbd2847f34cd2504ca5295830b to your computer and use it in GitHub Desktop.
Setting a metatable inside a table from C API
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
#include<stdio.h> | |
#include<stdlib.h> | |
#include<string.h> | |
#include<lua.h> | |
#include<lualib.h> | |
#include<lauxlib.h> | |
int main() | |
{ | |
char *scriptName = "script.lua"; | |
lua_State *L = luaL_newstate(); | |
luaL_openlibs(L); | |
lua_newtable(L); // table | |
lua_newtable(L); // table | subtable | |
lua_setfield(L, -2, "subtable"); // table | |
lua_getfield(L, -1, "subtable"); // table | subtable | |
lua_getfield(L, -2, "subtable"); // table | subtable | subtable | |
lua_setfield(L, -2, "__index"); // table | subtable | |
lua_setmetatable(L, -2); // table | |
lua_setglobal(L, "A"); | |
luaL_loadfile(L, scriptName); // scriptFunction | |
lua_call(L, 0, LUA_MULTRET, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment