aboutsummaryrefslogtreecommitdiff
path: root/vis-lua.c
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2017-02-22 20:34:07 +0100
committerMarc André Tanner <mat@brain-dump.org>2017-02-22 20:34:07 +0100
commit065f819487aec6382ee9f87cb9c8d16d76b4b104 (patch)
tree3c0335ee6c56af3b7a69904d7a0957f489188921 /vis-lua.c
parentb8b374702e7f24401baa883a969eb7f4165ac696 (diff)
downloadvis-065f819487aec6382ee9f87cb9c8d16d76b4b104.tar.gz
vis-065f819487aec6382ee9f87cb9c8d16d76b4b104.tar.xz
vis-lua: expose type meta tables through vis.types
This should allow the Lua code to add new methods even if it has no existing object references.
Diffstat (limited to 'vis-lua.c')
-rw-r--r--vis-lua.c30
1 files changed, 25 insertions, 5 deletions
diff --git a/vis-lua.c b/vis-lua.c
index 43957af..46f3a24 100644
--- a/vis-lua.c
+++ b/vis-lua.c
@@ -276,6 +276,14 @@ static bool func_ref_get(lua_State *L, const void *addr) {
*/
static void obj_type_new(lua_State *L, const char *type) {
luaL_newmetatable(L, type);
+ lua_getglobal(L, "vis");
+ if (!lua_isnil(L, -1)) {
+ lua_getfield(L, -1, "types");
+ lua_pushvalue(L, -3);
+ lua_setfield(L, -2, type);
+ lua_pop(L, 1);
+ }
+ lua_pop(L, 1);
lua_getfield(L, LUA_REGISTRYINDEX, "vis.types");
lua_pushvalue(L, -2);
lua_pushstring(L, type);
@@ -527,6 +535,11 @@ static const char *keymapping(Vis *vis, const char *keys, const Arg *arg) {
* version information in `git describe` format, same as reported by `vis -v`.
*/
/***
+ * Lua API object types
+ * @field types meta tables of userdata objects used for type checking
+ * @internal
+ */
+/***
* User interface.
* @tfield Ui ui the user interface being used
*/
@@ -2243,6 +2256,16 @@ void vis_lua_init(Vis *vis) {
lua_newtable(L);
lua_setfield(L, LUA_REGISTRYINDEX, "vis.functions");
/* metatable used to type check user data */
+ obj_type_new(L, "vis");
+ luaL_setfuncs(L, vis_lua, 0);
+ lua_newtable(L);
+ lua_setfield(L, -2, "types");
+ /* create reference to main vis object, such that the further
+ * calls to obj_type_new can register the type meta tables in
+ * vis.types[name] */
+ obj_ref_new(L, vis, "vis");
+ lua_setglobal(L, "vis");
+
obj_type_new(L, "vis.file");
const struct {
@@ -2304,8 +2327,8 @@ void vis_lua_init(Vis *vis) {
obj_type_new(L, "vis.keyaction");
- obj_type_new(L, "vis");
- luaL_setfuncs(L, vis_lua, 0);
+ lua_getglobal(L, "vis");
+ lua_getmetatable(L, -1);
lua_pushstring(L, VERSION);
lua_setfield(L, -2, "VERSION");
@@ -2331,9 +2354,6 @@ void vis_lua_init(Vis *vis) {
lua_setfield(L, -2, "modes");
- obj_ref_new(L, vis, "vis");
- lua_setglobal(L, "vis");
-
if (!package_exist(vis, L, "visrc")) {
vis_info_show(vis, "WARNING: failed to load visrc.lua");
} else {