summaryrefslogtreecommitdiffstats
path: root/player/lua.c
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2014-02-26 22:32:57 +0100
committerwm4 <wm4@nowhere>2014-02-26 22:32:57 +0100
commit15134935602a546103a26f4b3ecc72da25896f09 (patch)
tree4eed0a52a150e8f13814d271b1da55e439285d8e /player/lua.c
parent5eab4f43ec562e4ea37943b5360475a9d6d9c83d (diff)
downloadmpv-15134935602a546103a26f4b3ecc72da25896f09.tar.bz2
mpv-15134935602a546103a26f4b3ecc72da25896f09.tar.xz
lua: mark table values returned by get_property_native with their type
Lua doesn't distinguish between arrays and maps on the language level; there are just tables. Use metatables to mark these tables with their actual types. In particular, it allows distinguishing empty arrays from empty tables.
Diffstat (limited to 'player/lua.c')
-rw-r--r--player/lua.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/player/lua.c b/player/lua.c
index 8b0a818ea5..27e411e7fe 100644
--- a/player/lua.c
+++ b/player/lua.c
@@ -238,6 +238,14 @@ static void *lua_thread(void *p)
lua_pushvalue(L, -1); // mp table table
lua_setfield(L, LUA_REGISTRYINDEX, "UNKNOWN_TYPE"); // mp table
lua_setfield(L, -2, "UNKNOWN_TYPE"); // mp
+ lua_newtable(L); // mp table
+ lua_pushvalue(L, -1); // mp table table
+ lua_setfield(L, LUA_REGISTRYINDEX, "MAP"); // mp table
+ lua_setfield(L, -2, "MAP"); // mp
+ lua_newtable(L); // mp table
+ lua_pushvalue(L, -1); // mp table table
+ lua_setfield(L, LUA_REGISTRYINDEX, "ARRAY"); // mp table
+ lua_setfield(L, -2, "ARRAY"); // mp
lua_pop(L, 1); // -
@@ -648,6 +656,8 @@ static bool pushnode(lua_State *L, mpv_node *node, int depth)
break;
case MPV_FORMAT_NODE_ARRAY:
lua_newtable(L); // table
+ lua_getfield(L, LUA_REGISTRYINDEX, "ARRAY"); // table mt
+ lua_setmetatable(L, -2); // table
for (int n = 0; n < node->u.list->num; n++) {
if (!pushnode(L, &node->u.list->values[n], depth)) // table value
return false;
@@ -656,6 +666,8 @@ static bool pushnode(lua_State *L, mpv_node *node, int depth)
break;
case MPV_FORMAT_NODE_MAP:
lua_newtable(L); // table
+ lua_getfield(L, LUA_REGISTRYINDEX, "MAP"); // table mt
+ lua_setmetatable(L, -2); // table
for (int n = 0; n < node->u.list->num; n++) {
lua_pushstring(L, node->u.list->keys[n]); // table key
if (!pushnode(L, &node->u.list->values[n], depth)) // table key value
@@ -666,7 +678,9 @@ static bool pushnode(lua_State *L, mpv_node *node, int depth)
default:
// unknown value - what do we do?
// for now, set a unique dummy value
+ lua_newtable(L); // table
lua_getfield(L, LUA_REGISTRYINDEX, "UNKNOWN_TYPE");
+ lua_setmetatable(L, -2); // table
break;
}
return true;