webmcp

changeset 127:83aced09adc7

New function json.type(...)
author jbe
date Sun Jul 27 14:38:00 2014 +0200 (2014-07-27)
parents bccaa05aada7
children c507f8d62931
files libraries/json/json.c
line diff
     1.1 --- a/libraries/json/json.c	Sun Jul 27 03:54:39 2014 +0200
     1.2 +++ b/libraries/json/json.c	Sun Jul 27 14:38:00 2014 +0200
     1.3 @@ -3,8 +3,9 @@
     1.4  #include <stdlib.h>
     1.5  #include <string.h>
     1.6  
     1.7 -#define JSON_UPVAL_NULLS lua_upvalueindex(1)
     1.8 -#define JSON_UPVAL_METATABLE lua_upvalueindex(2)
     1.9 +#define JSON_UPVAL_TYPES lua_upvalueindex(1)
    1.10 +#define JSON_UPVAL_NULLS lua_upvalueindex(2)
    1.11 +#define JSON_UPVAL_METATABLE lua_upvalueindex(3)
    1.12  
    1.13  #define JSON_STATE_VALUE 0
    1.14  #define JSON_STATE_OBJECT_KEY 1
    1.15 @@ -46,6 +47,9 @@
    1.16      lua_newtable(L);  // the actual JSON object
    1.17      lua_pushvalue(L, JSON_UPVAL_METATABLE);
    1.18      lua_setmetatable(L, -2);
    1.19 +    lua_pushvalue(L, -1);
    1.20 +    lua_pushliteral(L, "object");
    1.21 +    lua_rawset(L, JSON_UPVAL_TYPES);
    1.22      lua_newtable(L);  // stores set of NULL values
    1.23      lua_pushvalue(L, -2);
    1.24      lua_pushvalue(L, -2);
    1.25 @@ -60,6 +64,9 @@
    1.26      lua_newtable(L);  // the actual JSON array
    1.27      lua_pushvalue(L, JSON_UPVAL_METATABLE);
    1.28      lua_setmetatable(L, -2);
    1.29 +    lua_pushvalue(L, -1);
    1.30 +    lua_pushliteral(L, "array");
    1.31 +    lua_rawset(L, JSON_UPVAL_TYPES);
    1.32      lua_newtable(L);  // stores set of NULL values
    1.33      lua_pushvalue(L, -2);
    1.34      lua_pushvalue(L, -2);
    1.35 @@ -218,18 +225,16 @@
    1.36  static int json_arylen(lua_State *L) {
    1.37    int lower, middle;
    1.38    int upper = 1;
    1.39 -  // TODO: check input for valid array!
    1.40    lua_settop(L, 1);
    1.41    lua_pushvalue(L, 1);
    1.42    lua_rawget(L, JSON_UPVAL_NULLS);
    1.43 -  if (lua_isnil(L, -1)) goto json_arylen_default;
    1.44 +  if (lua_isnil(L, 2)) goto json_arylen_default;
    1.45    lua_pushnil(L);
    1.46 -  if (!lua_next(L, -2)) goto json_arylen_default;
    1.47 -  lua_pop(L, 2);
    1.48 +  if (!lua_next(L, 2)) goto json_arylen_default;
    1.49 +  lua_settop(L, 2);
    1.50    while (1) {
    1.51      lua_rawgeti(L, 1, upper);
    1.52      if (lua_isnil(L, -1)) {
    1.53 -      // TODO: require metatable set?
    1.54        lua_pop(L, 1);
    1.55        lua_pushinteger(L, upper);
    1.56        lua_rawget(L, 2);
    1.57 @@ -245,7 +250,6 @@
    1.58      middle = (lower + upper) / 2;
    1.59      lua_rawgeti(L, 1, middle);
    1.60      if (lua_isnil(L, -1)) {
    1.61 -      // TODO: require metatable set?
    1.62        lua_pop(L, 1);
    1.63        lua_pushinteger(L, middle);
    1.64        lua_rawget(L, 2);
    1.65 @@ -261,8 +265,16 @@
    1.66    return 1;
    1.67  }
    1.68  
    1.69 +static int json_type(lua_State *L) {
    1.70 +  lua_settop(L, 1);
    1.71 +  lua_rawget(L, JSON_UPVAL_TYPES);
    1.72 +  return 1;
    1.73 +}
    1.74 +
    1.75  static int json_isnull(lua_State *L) {
    1.76 -  // TODO: require metatable set?
    1.77 +  lua_settop(L, 2);
    1.78 +  lua_getmetatable(L, 1);
    1.79 +  if (!lua_rawequal(L, -1, JSON_UPVAL_METATABLE)) goto json_isnull_false;
    1.80    lua_pushvalue(L, 1);
    1.81    lua_rawget(L, JSON_UPVAL_NULLS);
    1.82    if (lua_isnil(L, -1)) goto json_isnull_false;
    1.83 @@ -276,7 +288,7 @@
    1.84  
    1.85  static const struct luaL_Reg json_module_functions[] = {
    1.86    {"import", json_import},
    1.87 -  {"arylen", json_arylen},
    1.88 +  {"type", json_type},
    1.89    {"isnull", json_isnull},
    1.90    {NULL, NULL}
    1.91  };
    1.92 @@ -289,18 +301,22 @@
    1.93  int luaopen_json(lua_State *L) {
    1.94    lua_settop(L, 0);
    1.95    lua_newtable(L);  // 1: library table on stack position
    1.96 -  lua_newtable(L);  // 2: ephemeron table to store a set of keys associated with JSON-null values
    1.97 -  lua_newtable(L);  // 3: meta table for ephemeron table
    1.98 +  lua_newtable(L);  // 2: ephemeron table to store the type of the JSON object/array
    1.99 +  lua_newtable(L);  // 3: ephemeron table to store a set of keys associated with JSON-null values
   1.100 +  lua_newtable(L);  // 4: metatable for ephemeron tables
   1.101    lua_pushliteral(L, "__mode");
   1.102    lua_pushliteral(L, "k");
   1.103 -  lua_rawset(L, 3);
   1.104 +  lua_rawset(L, 4);
   1.105 +  lua_pushvalue(L, 4);  // 5: cloned metatable reference
   1.106    lua_setmetatable(L, 2);
   1.107 -  lua_newtable(L);  // 3: metatable for JSON objects and JSON arrays
   1.108 -  lua_pushvalue(L, 3);
   1.109 +  lua_setmetatable(L, 3);
   1.110 +  lua_newtable(L);  // 4: metatable for JSON objects and JSON arrays
   1.111 +  lua_pushvalue(L, 4);
   1.112    lua_setfield(L, 1, "metatable");
   1.113    lua_pushvalue(L, 2);
   1.114    lua_pushvalue(L, 3);
   1.115 -  luaL_setfuncs(L, json_metatable_functions, 2);
   1.116 -  luaL_setfuncs(L, json_module_functions, 2);
   1.117 +  lua_pushvalue(L, 4);
   1.118 +  luaL_setfuncs(L, json_metatable_functions, 3);
   1.119 +  luaL_setfuncs(L, json_module_functions, 3);
   1.120    return 1;
   1.121  }

Impressum / About Us