webmcp
changeset 149:5229687c7601
Removed metatable entries in JSON library module (since they should not be set directly); Added documentation to JSON library
author | jbe |
---|---|
date | Wed Jul 30 22:56:21 2014 +0200 (2014-07-30) |
parents | 5354d836e6fb |
children | 35fb4f92a887 |
files | libraries/json/json.c |
line diff
1.1 --- a/libraries/json/json.c Wed Jul 30 22:32:33 2014 +0200 1.2 +++ b/libraries/json/json.c Wed Jul 30 22:56:21 2014 +0200 1.3 @@ -651,22 +651,34 @@ 1.4 #define json_pairs_iterfunc_shadowtbl_idx 4 1.5 1.6 static int json_pairs_iterfunc(lua_State *L) { 1.7 + // stack shall contain two function arguments: 1.8 lua_settop(L, 2); 1.9 - json_pushlightref(L, nullmark); // on stack position 3 1.10 + // push nullmark onto stack position 3: 1.11 + json_pushlightref(L, nullmark); 1.12 + // push shadowtbl onto stack position 4: 1.13 json_regfetch(L, shadowtbl); 1.14 + // get corresponding shadow table for first argument: 1.15 lua_pushvalue(L, 1); 1.16 lua_rawget(L, json_pairs_iterfunc_shadowtbl_idx); 1.17 + // throw error if no shadow table was found: 1.18 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found"); 1.19 + // get next key value pair from shadow table (using previous key from argument 2) 1.20 + // and return nothing if there is no next pair: 1.21 lua_pushvalue(L, 2); 1.22 if (!lua_next(L, -2)) return 0; 1.23 + // replace null-marker with nil: 1.24 if (lua_rawequal(L, -1, json_pairs_iterfunc_nullmark_idx)) { 1.25 lua_pop(L, 1); 1.26 lua_pushnil(L); 1.27 } 1.28 + // return key and value (or key and nil, if null-marker was found): 1.29 return 2; 1.30 } 1.31 1.32 +// returns a triple such that 'for key, value in pairs(obj) do ... end' 1.33 +// iterates through all key value pairs (including JSON null keys represented as Lua nil): 1.34 static int json_pairs(lua_State *L) { 1.35 + // return triple of function json_pairs_iterfunc, first argument, and nil: 1.36 lua_pushcfunction(L, json_pairs_iterfunc); 1.37 lua_pushvalue(L, 1); 1.38 lua_pushnil(L); 1.39 @@ -679,28 +691,43 @@ 1.40 1.41 static int json_ipairs_iterfunc(lua_State *L) { 1.42 int idx; 1.43 + // stack shall contain two function arguments: 1.44 lua_settop(L, 2); 1.45 - json_pushlightref(L, nullmark); // on stack position 3 1.46 + // push nullmark onto stack position 3: 1.47 + json_pushlightref(L, nullmark); 1.48 + // push shadowtbl onto stack position 4: 1.49 json_regfetch(L, shadowtbl); 1.50 + // calculate new index by incrementing second argument: 1.51 idx = lua_tointeger(L, 2) + 1; 1.52 + // get corresponding shadow table for first argument: 1.53 lua_pushvalue(L, 1); 1.54 lua_rawget(L, json_ipairs_iterfunc_shadowtbl_idx); 1.55 + // throw error if no shadow table was found: 1.56 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found"); 1.57 + // do integer lookup in shadow table: 1.58 lua_rawgeti(L, -1, idx); 1.59 + // return nothing if there was no value: 1.60 if (lua_isnil(L, -1)) return 0; 1.61 + // return new index and 1.62 + // either the looked up value if it is not equal to the null-marker 1.63 + // or nil instead of null-marker: 1.64 lua_pushinteger(L, idx); 1.65 if (lua_rawequal(L, -2, json_ipairs_iterfunc_nullmark_idx)) lua_pushnil(L); 1.66 else lua_pushvalue(L, -2); 1.67 return 2; 1.68 } 1.69 1.70 +// returns a triple such that 'for idx, value in ipairs(ary) do ... end' 1.71 +// iterates through all values (including JSON null represented as Lua nil): 1.72 static int json_ipairs(lua_State *L) { 1.73 + // return triple of function json_ipairs_iterfunc, first argument, and zero: 1.74 lua_pushcfunction(L, json_ipairs_iterfunc); 1.75 lua_pushvalue(L, 1); 1.76 lua_pushinteger(L, 0); 1.77 return 3; 1.78 } 1.79 1.80 +// functions in library module: 1.81 static const struct luaL_Reg json_module_functions[] = { 1.82 {"object", json_object}, 1.83 {"array", json_array}, 1.84 @@ -712,6 +739,7 @@ 1.85 {NULL, NULL} 1.86 }; 1.87 1.88 +// metamethods for JSON objects, JSON arrays, and unknown JSON collections (object or array): 1.89 static const struct luaL_Reg json_metatable_functions[] = { 1.90 {"__len", json_len}, 1.91 {"__index", json_index}, 1.92 @@ -721,29 +749,36 @@ 1.93 {NULL, NULL} 1.94 }; 1.95 1.96 +// initializes json library: 1.97 int luaopen_json(lua_State *L) { 1.98 + // empty stack: 1.99 lua_settop(L, 0); 1.100 - lua_newtable(L); // library 1.101 + // push library module onto stack position 1: 1.102 + lua_newtable(L); 1.103 + // register library functions: 1.104 + luaL_setfuncs(L, json_module_functions, 0); 1.105 + // create and store unknownmt: 1.106 lua_newtable(L); 1.107 luaL_setfuncs(L, json_metatable_functions, 0); 1.108 json_regstore(L, unknownmt); 1.109 - lua_setfield(L, 1, "ambiguous_mt"); 1.110 + // create and store objectmt: 1.111 lua_newtable(L); 1.112 luaL_setfuncs(L, json_metatable_functions, 0); 1.113 json_regstore(L, objectmt); 1.114 - lua_setfield(L, 1, "object_mt"); 1.115 + // create and store arraymt: 1.116 lua_newtable(L); 1.117 luaL_setfuncs(L, json_metatable_functions, 0); 1.118 json_regstore(L, arraymt); 1.119 - lua_setfield(L, 1, "array_mt"); 1.120 - lua_newtable(L); // ephemeron table to store shadow tables for each JSON object/array to allow NULL values returned as nil 1.121 + // create and store ephemeron table to store shadow tables for each JSON object/array 1.122 + // to allow NULL values returned as nil 1.123 + lua_newtable(L); 1.124 lua_newtable(L); // metatable for ephemeron table 1.125 lua_pushliteral(L, "__mode"); 1.126 lua_pushliteral(L, "k"); 1.127 lua_rawset(L, -3); 1.128 lua_setmetatable(L, -2); 1.129 json_regstore(L, shadowtbl); 1.130 + // return library module stored on lowest stack position: 1.131 lua_settop(L, 1); 1.132 - luaL_setfuncs(L, json_module_functions, 0); 1.133 return 1; 1.134 }