webmcp
changeset 129:453d8f8fbace
Extended function json.type(...) to accept two arguments, to allow return of "null"
author | jbe |
---|---|
date | Sun Jul 27 15:03:21 2014 +0200 (2014-07-27) |
parents | c507f8d62931 |
children | 209f8ce39c5a |
files | libraries/json/json.c |
line diff
1.1 --- a/libraries/json/json.c Sun Jul 27 14:44:20 2014 +0200 1.2 +++ b/libraries/json/json.c Sun Jul 27 15:03:21 2014 +0200 1.3 @@ -266,10 +266,26 @@ 1.4 } 1.5 1.6 static int json_type(lua_State *L) { 1.7 - lua_settop(L, 1); 1.8 - lua_pushvalue(L, 1); 1.9 + if (lua_gettop(L) >= 2) { 1.10 + lua_pushvalue(L, 1); 1.11 + lua_rawget(L, JSON_UPVAL_NULLS); 1.12 + lua_pushvalue(L, 2); 1.13 + lua_rawget(L, -2); 1.14 + if (lua_toboolean(L, -1)) { 1.15 + lua_getmetatable(L, 1); 1.16 + if (lua_rawequal(L, -1, JSON_UPVAL_METATABLE)) { 1.17 + lua_pushliteral(L, "null"); 1.18 + return 1; 1.19 + } 1.20 + } 1.21 + lua_settop(L, 2); 1.22 + lua_rawget(L, 1); 1.23 + } else { 1.24 + lua_settop(L, 1); 1.25 + } 1.26 + lua_pushvalue(L, -1); 1.27 lua_rawget(L, JSON_UPVAL_TYPES); 1.28 - if (lua_isnil(L, -1)) lua_pushstring(L, lua_typename(L, lua_type(L, 1))); 1.29 + if (lua_isnil(L, -1)) lua_pushstring(L, lua_typename(L, lua_type(L, -2))); 1.30 return 1; 1.31 } 1.32