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.
This commit is contained in:
wm4 2014-02-26 22:32:57 +01:00
parent 5eab4f43ec
commit 1513493560
2 changed files with 22 additions and 1 deletions

View File

@ -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;

View File

@ -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