webmcp

changeset 135:663722e35330

Added pairs metamethod for (sparse) JSON objects
author jbe
date Mon Jul 28 03:00:40 2014 +0200 (2014-07-28)
parents a1507b499fa5
children 3cf5fcf2bd5f
files libraries/json/json.c
line diff
     1.1 --- a/libraries/json/json.c	Sun Jul 27 22:42:52 2014 +0200
     1.2 +++ b/libraries/json/json.c	Mon Jul 28 03:00:40 2014 +0200
     1.3 @@ -7,7 +7,8 @@
     1.4  #define JSON_UPVAL_SHADOWTBL lua_upvalueindex(2)
     1.5  #define JSON_UPVAL_TYPES     lua_upvalueindex(3)
     1.6  #define JSON_UPVAL_METATABLE lua_upvalueindex(4)
     1.7 -#define JSON_UPVAL_IPAIRS_ITERFUNC lua_upvalueindex(5)
     1.8 +#define JSON_UPVAL_PAIRS_ITERFUNC  lua_upvalueindex(5)
     1.9 +#define JSON_UPVAL_IPAIRS_ITERFUNC lua_upvalueindex(6)
    1.10  
    1.11  #define JSON_STATE_VALUE 0
    1.12  #define JSON_STATE_OBJECT_KEY 1
    1.13 @@ -355,6 +356,27 @@
    1.14    return 1;
    1.15  }
    1.16  
    1.17 +static int json_pairs_iterfunc(lua_State *L) {
    1.18 +  lua_settop(L, 2);
    1.19 +  lua_pushvalue(L, 1);
    1.20 +  lua_rawget(L, JSON_UPVAL_SHADOWTBL);
    1.21 +  if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
    1.22 +  lua_pushvalue(L, 2);
    1.23 +  if (!lua_next(L, -2)) return 0;
    1.24 +  if (lua_rawequal(L, -1, JSON_UPVAL_NULLMARK)) {
    1.25 +    lua_pop(L, 1);
    1.26 +    lua_pushnil(L);
    1.27 +  }
    1.28 +  return 2;
    1.29 +}
    1.30 +
    1.31 +static int json_pairs(lua_State *L) {
    1.32 +  lua_pushvalue(L, JSON_UPVAL_PAIRS_ITERFUNC);
    1.33 +  lua_pushvalue(L, 1);
    1.34 +  lua_pushnil(L);
    1.35 +  return 3;
    1.36 +}
    1.37 +
    1.38  static int json_ipairs_iterfunc(lua_State *L) {
    1.39    int idx;
    1.40    lua_settop(L, 2);
    1.41 @@ -392,6 +414,7 @@
    1.42    {"__len", json_len},
    1.43    {"__index", json_index},
    1.44    {"__newindex", json_newindex},
    1.45 +  {"__pairs", json_pairs},
    1.46    {"__ipairs", json_ipairs},
    1.47    {NULL, NULL}
    1.48  };
    1.49 @@ -410,21 +433,25 @@
    1.50    lua_setmetatable(L, 3);
    1.51    lua_setmetatable(L, 4);
    1.52    lua_newtable(L);  // 5: metatable for JSON objects and JSON arrays
    1.53 -  lua_pushvalue(L, 5);
    1.54 -  lua_setfield(L, 1, "metatable");
    1.55    lua_pushvalue(L, 2);
    1.56    lua_pushvalue(L, 3);
    1.57    lua_pushvalue(L, 4);
    1.58    lua_pushvalue(L, 5);
    1.59 -  lua_pushcclosure(L, json_ipairs_iterfunc, 4);  // 6: iteration function for ipairs
    1.60 +  lua_pushcclosure(L, json_pairs_iterfunc, 4);  // 6: iteration function for pairs
    1.61 +  lua_pushvalue(L, 2);
    1.62 +  lua_pushvalue(L, 3);
    1.63 +  lua_pushvalue(L, 4);
    1.64 +  lua_pushvalue(L, 5);
    1.65 +  lua_pushcclosure(L, json_ipairs_iterfunc, 4);  // 7: iteration function for ipairs
    1.66    lua_pushvalue(L, 5);
    1.67    lua_pushvalue(L, 2);
    1.68    lua_pushvalue(L, 3);
    1.69    lua_pushvalue(L, 4);
    1.70    lua_pushvalue(L, 5);
    1.71    lua_pushvalue(L, 6);
    1.72 -  luaL_setfuncs(L, json_metatable_functions, 5);
    1.73 -  lua_pop(L, 1);
    1.74 -  luaL_setfuncs(L, json_module_functions, 5);
    1.75 +  lua_pushvalue(L, 7);
    1.76 +  luaL_setfuncs(L, json_metatable_functions, 6);
    1.77 +  lua_setfield(L, 1, "metatable");
    1.78 +  luaL_setfuncs(L, json_module_functions, 6);
    1.79    return 1;
    1.80  }

Impressum / About Us