From 15134935602a546103a26f4b3ecc72da25896f09 Mon Sep 17 00:00:00 2001 From: wm4 Date: Wed, 26 Feb 2014 22:32:57 +0100 Subject: 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. --- player/lua.c | 14 ++++++++++++++ player/lua/defaults.lua | 9 ++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) (limited to 'player') 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 -- cgit v1.2.3