diff options
author | wm4 <wm4@nowhere> | 2014-02-26 22:32:57 +0100 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2014-02-26 22:32:57 +0100 |
commit | 15134935602a546103a26f4b3ecc72da25896f09 (patch) | |
tree | 4eed0a52a150e8f13814d271b1da55e439285d8e /player | |
parent | 5eab4f43ec562e4ea37943b5360475a9d6d9c83d (diff) | |
download | mpv-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')
-rw-r--r-- | player/lua.c | 14 | ||||
-rw-r--r-- | player/lua/defaults.lua | 9 |
2 files changed, 22 insertions, 1 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; diff --git a/player/lua/defaults.lua b/player/lua/defaults.lua index f8fe05ff82..0a04e26761 100644 --- a/player/lua/defaults.lua +++ b/player/lua/defaults.lua @@ -1,4 +1,11 @@ -mp.UNKNOWN_TYPE = "this value is inserted if the C type is not supported" +mp.UNKNOWN_TYPE.info = "this value is inserted if the C type is not supported" +mp.UNKNOWN_TYPE.type = "UNKNOWN_TYPE" + +mp.ARRAY.info = "native array" +mp.ARRAY.type = "ARRAY" + +mp.MAP.info = "native map" +mp.MAP.type = "MAP" function mp.get_script_name() return mp.script_name |