From 5548c75e5507da6a0f9fb6a45a07a5cc6edd9393 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sun, 19 Oct 2014 05:27:35 +0200 Subject: lua: expose JSON parser The JSON parser was introduced for the IPC protocol, but I guess it's useful here too. The motivation for this commit is the same as with 8e4fa5fc (again). --- player/lua.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'player') diff --git a/player/lua.c b/player/lua.c index b565dedfe2..306f3a66f5 100644 --- a/player/lua.c +++ b/player/lua.c @@ -40,6 +40,7 @@ #include "input/input.h" #include "options/path.h" #include "misc/bstr.h" +#include "misc/json.h" #include "osdep/timer.h" #include "osdep/threads.h" #include "stream/stream.h" @@ -1290,6 +1291,30 @@ done: } #endif +static int script_parse_json(lua_State *L) +{ + void *tmp = mp_lua_PITA(L); + char *text = talloc_strdup(tmp, luaL_checkstring(L, 1)); + bool trail = lua_toboolean(L, 2); + bool ok = false; + struct mpv_node node; + if (json_parse(tmp, &node, &text, 32) >= 0) { + json_skip_whitespace(&text); + ok = !text[0] || !trail; + } + if (ok) { + if (!pushnode(L, &node, 64)) + luaL_error(L, "stack overflow"); + lua_pushnil(L); + } else { + lua_pushnil(L); + lua_pushstring(L, "error"); + } + lua_pushstring(L, text); + talloc_free_children(tmp); + return 3; +} + #define FN_ENTRY(name) {#name, script_ ## name} struct fn_entry { const char *name; @@ -1339,6 +1364,7 @@ static const struct fn_entry utils_fns[] = { #if HAVE_POSIX_SPAWN FN_ENTRY(subprocess), #endif + FN_ENTRY(parse_json), {0} }; -- cgit v1.2.3