| rev | line source | 
| jbe@121 | 1 #include <lua.h> | 
| jbe@121 | 2 #include <lauxlib.h> | 
| jbe@122 | 3 #include <stdlib.h> | 
| jbe@121 | 4 #include <string.h> | 
| jbe@121 | 5 | 
| jbe@144 | 6 // maximum number of nested JSON values (objects and arrays): | 
| jbe@142 | 7 #define JSON_MAXDEPTH 100 | 
| jbe@142 | 8 | 
| jbe@144 | 9 // macros for usage of Lua registry: | 
| jbe@144 | 10 #define JSON_REGENT char | 
| jbe@145 | 11 #define JSON_REGPOINTER void * | 
| jbe@145 | 12 #define json_pushlightref(L, x) (lua_pushlightuserdata((L), &json_reference.x)) | 
| jbe@145 | 13 #define json_regpointer(x) (&json_registry.x) | 
| jbe@145 | 14 #define json_regfetchpointer(L, x) (lua_pushlightuserdata((L), (x)), lua_rawget((L), LUA_REGISTRYINDEX)) | 
| jbe@145 | 15 #define json_regfetch(L, x) (json_regfetchpointer(L, json_regpointer(x))) | 
| jbe@145 | 16 #define json_regstore(L, x) (lua_pushlightuserdata(L, json_regpointer(x)), lua_pushvalue(L, -2), lua_rawset(L, LUA_REGISTRYINDEX)); | 
| jbe@145 | 17 | 
| jbe@146 | 18 // generate dummy memory addresses that represent non-modifiable lightuserdata (dummy) objects: | 
| jbe@145 | 19 static struct { | 
| jbe@146 | 20   JSON_REGENT nullmark;  // magic value to indicate JSON null value in shadow table | 
| jbe@145 | 21 } json_reference; | 
| jbe@145 | 22 | 
| jbe@138 | 23 | 
| jbe@144 | 24 // generate dummy memory addresses that represent Lua objects | 
| jbe@145 | 25 // via lightuserdata keys and LUA_REGISTRYINDEX: | 
| jbe@144 | 26 static struct { | 
| jbe@145 | 27   JSON_REGENT shadowtbl;  // ephemeron table that maps tables to their corresponding shadow table | 
| jbe@145 | 28   JSON_REGENT unknownmt;  // metatable for tables that may be either JSON objects or JSON arrays | 
| jbe@145 | 29   JSON_REGENT objectmt;   // metatable for JSON objects | 
| jbe@145 | 30   JSON_REGENT arraymt;    // metatable for JSON arrays | 
| jbe@144 | 31 } json_registry; | 
| jbe@138 | 32 | 
| jbe@145 | 33 // marks a Lua table as JSON object or JSON array: | 
| jbe@136 | 34 // (returns its modified argument or a new table if argument is nil) | 
| jbe@145 | 35 static int json_mark(lua_State *L, JSON_REGPOINTER mt) { | 
| jbe@145 | 36   // check if argument is nil | 
| jbe@136 | 37   if (lua_isnoneornil(L, 1)) { | 
| jbe@145 | 38     // create new table at stack position 1: | 
| jbe@136 | 39     lua_settop(L, 0); | 
| jbe@136 | 40     lua_newtable(L); | 
| jbe@145 | 41     // create shadow table (leaving previously created table on stack position 1): | 
| jbe@144 | 42     json_regfetch(L, shadowtbl); | 
| jbe@136 | 43     lua_pushvalue(L, 1); | 
| jbe@136 | 44     lua_newtable(L); | 
| jbe@143 | 45     lua_rawset(L, -3); | 
| jbe@143 | 46   } else { | 
| jbe@145 | 47     // push shadow table on top of stack: | 
| jbe@144 | 48     json_regfetch(L, shadowtbl); | 
| jbe@143 | 49     lua_pushvalue(L, 1); | 
| jbe@143 | 50     lua_rawget(L, -2); | 
| jbe@145 | 51     // if shadow table does not exist: | 
| jbe@143 | 52     if (lua_isnil(L, -1)) { | 
| jbe@145 | 53       // create shadow table and leave it on top of stack: | 
| jbe@143 | 54       lua_newtable(L); | 
| jbe@143 | 55       lua_pushvalue(L, 1); | 
| jbe@143 | 56       lua_pushvalue(L, -2); | 
| jbe@143 | 57       lua_rawset(L, -5); | 
| jbe@143 | 58     } | 
| jbe@145 | 59     // move elements from original table to shadow table (that's expected on top of stack): | 
| jbe@143 | 60     for(lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) { | 
| jbe@143 | 61       lua_pushvalue(L, -2); | 
| jbe@143 | 62       lua_pushnil(L); | 
| jbe@143 | 63       lua_rawset(L, 1); | 
| jbe@143 | 64       lua_pushvalue(L, -2); | 
| jbe@143 | 65       lua_pushvalue(L, -2); | 
| jbe@143 | 66       lua_rawset(L, -5); | 
| jbe@143 | 67     } | 
| jbe@136 | 68   } | 
| jbe@138 | 69   // discard everything but table to return: | 
| jbe@138 | 70   lua_settop(L, 1); | 
| jbe@136 | 71   // set metatable: | 
| jbe@145 | 72   json_regfetchpointer(L, mt); | 
| jbe@136 | 73   lua_setmetatable(L, 1); | 
| jbe@138 | 74   // return table: | 
| jbe@136 | 75   return 1; | 
| jbe@136 | 76 } | 
| jbe@136 | 77 | 
| jbe@136 | 78 // marks a table as JSON object: | 
| jbe@136 | 79 // (returns its modified argument or a new table if argument is nil) | 
| jbe@136 | 80 static int json_object(lua_State *L) { | 
| jbe@145 | 81   return json_mark(L, json_regpointer(objectmt)); | 
| jbe@136 | 82 } | 
| jbe@136 | 83 | 
| jbe@136 | 84 // marks a table as JSON array: | 
| jbe@136 | 85 // (returns its modified argument or a new table if argument is nil) | 
| jbe@136 | 86 static int json_array(lua_State *L) { | 
| jbe@145 | 87   return json_mark(L, json_regpointer(arraymt)); | 
| jbe@136 | 88 } | 
| jbe@136 | 89 | 
| jbe@145 | 90 // internal states of JSON parser: | 
| jbe@124 | 91 #define JSON_STATE_VALUE 0 | 
| jbe@124 | 92 #define JSON_STATE_OBJECT_KEY 1 | 
| jbe@124 | 93 #define JSON_STATE_OBJECT_KEY_TERMINATOR 2 | 
| jbe@124 | 94 #define JSON_STATE_OBJECT_VALUE 3 | 
| jbe@124 | 95 #define JSON_STATE_OBJECT_SEPARATOR 4 | 
| jbe@124 | 96 #define JSON_STATE_ARRAY_VALUE 5 | 
| jbe@124 | 97 #define JSON_STATE_ARRAY_SEPARATOR 6 | 
| jbe@124 | 98 #define JSON_STATE_END 7 | 
| jbe@121 | 99 | 
| jbe@145 | 100 // special Lua stack indicies for json_import function: | 
| jbe@138 | 101 #define json_import_objectmt_idx 2 | 
| jbe@138 | 102 #define json_import_arraymt_idx 3 | 
| jbe@138 | 103 #define json_import_shadowtbl_idx 4 | 
| jbe@138 | 104 #define json_import_nullmark_idx 5 | 
| jbe@138 | 105 | 
| jbe@136 | 106 // decodes a JSON document: | 
| jbe@121 | 107 static int json_import(lua_State *L) { | 
| jbe@136 | 108   const char *str;   // string to parse | 
| jbe@136 | 109   size_t total;      // total length of string to parse | 
| jbe@136 | 110   size_t pos = 0;    // current position in string to parse | 
| jbe@136 | 111   size_t level = 0;  // nested levels of objects/arrays currently being processed | 
| jbe@145 | 112   int mode = JSON_STATE_VALUE;  // state of parser (i.e. "what's expected next?") | 
| jbe@136 | 113   char c;              // variable to store a single character to be processed | 
| jbe@145 | 114   luaL_Buffer luabuf;  // Lua buffer to decode JSON string values | 
| jbe@145 | 115   char *cbuf;          // C buffer to decode JSON string values | 
| jbe@136 | 116   size_t writepos;     // write position of decoded strings in C buffer | 
| jbe@147 | 117   // stack shall contain one function argument: | 
| jbe@138 | 118   lua_settop(L, 1); | 
| jbe@147 | 119   // push objectmt onto stack position 2: | 
| jbe@144 | 120   json_regfetch(L, objectmt); | 
| jbe@147 | 121   // push arraymt onto stack position 3: | 
| jbe@144 | 122   json_regfetch(L, arraymt); | 
| jbe@147 | 123   // push shadowtbl onto stack position 4: | 
| jbe@144 | 124   json_regfetch(L, shadowtbl); | 
| jbe@147 | 125   // push nullmark onto stack position 5: | 
| jbe@145 | 126   json_pushlightref(L, nullmark); | 
| jbe@136 | 127   // require string as first argument: | 
| jbe@136 | 128   str = luaL_checklstring(L, 1, &total); | 
| jbe@136 | 129   // if string contains a NULL byte, this is a syntax error | 
| jbe@136 | 130   if (strlen(str) != total) goto json_import_syntax_error; | 
| jbe@136 | 131   // main loop of parser: | 
| jbe@136 | 132   json_import_loop: | 
| jbe@136 | 133   // skip whitespace and store next character in variable 'c': | 
| jbe@146 | 134   while (c = str[pos], | 
| jbe@146 | 135     c == ' ' || | 
| jbe@146 | 136     c == '\f' || | 
| jbe@146 | 137     c == '\n' || | 
| jbe@146 | 138     c == '\r' || | 
| jbe@146 | 139     c == '\t' || | 
| jbe@146 | 140     c == '\v' | 
| jbe@146 | 141   ) pos++; | 
| jbe@136 | 142   // switch statement to handle certain (single) characters: | 
| jbe@121 | 143   switch (c) { | 
| jbe@136 | 144   // handle end of JSON document: | 
| jbe@121 | 145   case 0: | 
| jbe@136 | 146     // if end of JSON document was expected, then return top element of stack as result: | 
| jbe@124 | 147     if (mode == JSON_STATE_END) return 1; | 
| jbe@136 | 148     // otherwise, the JSON document was malformed: | 
| jbe@121 | 149     json_import_unexpected_eof: | 
| jbe@121 | 150     lua_pushnil(L); | 
| jbe@121 | 151     if (level == 0) lua_pushliteral(L, "Empty string"); | 
| jbe@121 | 152     else lua_pushliteral(L, "Unexpected end of JSON document"); | 
| jbe@121 | 153     return 2; | 
| jbe@136 | 154   // new JSON object: | 
| jbe@121 | 155   case '{': | 
| jbe@136 | 156     // if a JSON object is not expected here, then return an error: | 
| jbe@146 | 157     if ( | 
| jbe@146 | 158       mode != JSON_STATE_VALUE && | 
| jbe@146 | 159       mode != JSON_STATE_OBJECT_VALUE && | 
| jbe@146 | 160       mode != JSON_STATE_ARRAY_VALUE | 
| jbe@146 | 161     ) goto json_import_syntax_error; | 
| jbe@136 | 162     // create JSON object on stack: | 
| jbe@136 | 163     lua_newtable(L); | 
| jbe@136 | 164     // set metatable of JSON object: | 
| jbe@138 | 165     lua_pushvalue(L, json_import_objectmt_idx); | 
| jbe@125 | 166     lua_setmetatable(L, -2); | 
| jbe@136 | 167     // create internal shadow table on stack: | 
| jbe@136 | 168     lua_newtable(L); | 
| jbe@146 | 169     // register internal shadow table: | 
| jbe@123 | 170     lua_pushvalue(L, -2); | 
| jbe@123 | 171     lua_pushvalue(L, -2); | 
| jbe@138 | 172     lua_rawset(L, json_import_shadowtbl_idx); | 
| jbe@146 | 173     // expect object key (or end of object) to follow: | 
| jbe@136 | 174     mode = JSON_STATE_OBJECT_KEY; | 
| jbe@146 | 175     // jump to common code for opening JSON object and JSON array: | 
| jbe@142 | 176     goto json_import_open; | 
| jbe@136 | 177   // new JSON array: | 
| jbe@121 | 178   case '[': | 
| jbe@136 | 179     // if a JSON array is not expected here, then return an error: | 
| jbe@146 | 180     if ( | 
| jbe@146 | 181       mode != JSON_STATE_VALUE && | 
| jbe@146 | 182       mode != JSON_STATE_OBJECT_VALUE && | 
| jbe@146 | 183       mode != JSON_STATE_ARRAY_VALUE | 
| jbe@146 | 184     ) goto json_import_syntax_error; | 
| jbe@136 | 185     // create JSON array on stack: | 
| jbe@136 | 186     lua_newtable(L); | 
| jbe@136 | 187     // set metatable of JSON array: | 
| jbe@138 | 188     lua_pushvalue(L, json_import_arraymt_idx); | 
| jbe@125 | 189     lua_setmetatable(L, -2); | 
| jbe@136 | 190     // create internal shadow table on stack: | 
| jbe@136 | 191     lua_newtable(L); | 
| jbe@146 | 192     // register internal shadow table: | 
| jbe@123 | 193     lua_pushvalue(L, -2); | 
| jbe@123 | 194     lua_pushvalue(L, -2); | 
| jbe@138 | 195     lua_rawset(L, json_import_shadowtbl_idx); | 
| jbe@140 | 196     // add nil as key (needed to keep stack balance) and as magic to detect arrays: | 
| jbe@140 | 197     lua_pushnil(L); | 
| jbe@146 | 198     // expect array value (or end of array) to follow: | 
| jbe@142 | 199     mode = JSON_STATE_ARRAY_VALUE; | 
| jbe@142 | 200     // continue with common code for opening JSON object and JSON array: | 
| jbe@146 | 201   // common code for opening JSON object or JSON array: | 
| jbe@142 | 202   json_import_open: | 
| jbe@142 | 203     // limit nested levels: | 
| jbe@142 | 204     if (level >= JSON_MAXDEPTH) { | 
| jbe@142 | 205       lua_pushnil(L); | 
| jbe@142 | 206       lua_pushliteral(L, "Too many nested JSON levels"); | 
| jbe@142 | 207       return 2; | 
| jbe@142 | 208     } | 
| jbe@142 | 209     // additional buffer overflow protection: | 
| jbe@142 | 210     if (!lua_checkstack(L, LUA_MINSTACK)) | 
| jbe@142 | 211       return luaL_error(L, "Caught stack overflow in JSON import function (too many nested levels and stack size too small)"); | 
| jbe@136 | 212     // increment level: | 
| jbe@121 | 213     level++; | 
| jbe@142 | 214     // consume input character: | 
| jbe@142 | 215     pos++; | 
| jbe@121 | 216     goto json_import_loop; | 
| jbe@136 | 217   // end of JSON object: | 
| jbe@121 | 218   case '}': | 
| jbe@136 | 219     // if end of JSON object is not expected here, then return an error: | 
| jbe@146 | 220     if ( | 
| jbe@146 | 221       mode != JSON_STATE_OBJECT_KEY && | 
| jbe@146 | 222       mode != JSON_STATE_OBJECT_SEPARATOR | 
| jbe@146 | 223     ) goto json_import_syntax_error; | 
| jbe@136 | 224     // jump to common code for end of JSON object and JSON array: | 
| jbe@121 | 225     goto json_import_close; | 
| jbe@136 | 226   // end of JSON array: | 
| jbe@121 | 227   case ']': | 
| jbe@136 | 228     // if end of JSON array is not expected here, then return an error: | 
| jbe@146 | 229     if ( | 
| jbe@146 | 230       mode != JSON_STATE_ARRAY_VALUE && | 
| jbe@146 | 231       mode != JSON_STATE_ARRAY_SEPARATOR | 
| jbe@146 | 232     ) goto json_import_syntax_error; | 
| jbe@146 | 233     // pop nil key/magic (that was needed to keep stack balance): | 
| jbe@140 | 234     lua_pop(L, 1); | 
| jbe@136 | 235     // continue with common code for end of JSON object and JSON array: | 
| jbe@136 | 236   // common code for end of JSON object or JSON array: | 
| jbe@121 | 237   json_import_close: | 
| jbe@136 | 238     // consume input character: | 
| jbe@121 | 239     pos++; | 
| jbe@136 | 240     // pop shadow table: | 
| jbe@136 | 241     lua_pop(L, 1); | 
| jbe@136 | 242     // check if nested: | 
| jbe@121 | 243     if (--level) { | 
| jbe@146 | 244       // if nested, | 
| jbe@146 | 245       // check if outer(!) structure is an array or object: | 
| jbe@140 | 246       if (lua_isnil(L, -2)) { | 
| jbe@136 | 247         // select array value processing: | 
| jbe@124 | 248         mode = JSON_STATE_ARRAY_VALUE; | 
| jbe@121 | 249       } else { | 
| jbe@136 | 250         // select object value processing: | 
| jbe@124 | 251         mode = JSON_STATE_OBJECT_VALUE; | 
| jbe@121 | 252       } | 
| jbe@136 | 253       // store value in outer structure: | 
| jbe@121 | 254       goto json_import_process_value; | 
| jbe@121 | 255     } | 
| jbe@136 | 256     // if not nested, then expect end of JSON document and continue with loop: | 
| jbe@136 | 257     mode = JSON_STATE_END; | 
| jbe@121 | 258     goto json_import_loop; | 
| jbe@136 | 259   // key terminator: | 
| jbe@121 | 260   case ':': | 
| jbe@136 | 261     // if key terminator is not expected here, then return an error: | 
| jbe@124 | 262     if (mode != JSON_STATE_OBJECT_KEY_TERMINATOR) | 
| jbe@121 | 263       goto json_import_syntax_error; | 
| jbe@136 | 264     // consume input character: | 
| jbe@121 | 265     pos++; | 
| jbe@146 | 266     // expect object value to follow: | 
| jbe@124 | 267     mode = JSON_STATE_OBJECT_VALUE; | 
| jbe@146 | 268     // continue with loop: | 
| jbe@121 | 269     goto json_import_loop; | 
| jbe@136 | 270   // value terminator (NOTE: trailing comma at end of value or key-value list is tolerated by this parser) | 
| jbe@121 | 271   case ',': | 
| jbe@146 | 272     // branch according to parser state: | 
| jbe@124 | 273     if (mode == JSON_STATE_OBJECT_SEPARATOR) { | 
| jbe@146 | 274       // expect an object key to follow: | 
| jbe@124 | 275       mode = JSON_STATE_OBJECT_KEY; | 
| jbe@124 | 276     } else if (mode == JSON_STATE_ARRAY_SEPARATOR) { | 
| jbe@146 | 277       // expect an array value to follow: | 
| jbe@124 | 278       mode = JSON_STATE_ARRAY_VALUE; | 
| jbe@121 | 279     } else { | 
| jbe@136 | 280        // if value terminator is not expected here, then return an error: | 
| jbe@136 | 281        goto json_import_syntax_error; | 
| jbe@121 | 282     } | 
| jbe@136 | 283     // consume input character: | 
| jbe@121 | 284     pos++; | 
| jbe@136 | 285     // continue with loop: | 
| jbe@121 | 286     goto json_import_loop; | 
| jbe@136 | 287   // string literal: | 
| jbe@121 | 288   case '"': | 
| jbe@146 | 289     // consume quote character: | 
| jbe@146 | 290     pos++; | 
| jbe@136 | 291     // prepare buffer to decode string (with maximum possible length) and set write position to zero: | 
| jbe@121 | 292     cbuf = luaL_buffinitsize(L, &luabuf, total-pos); | 
| jbe@121 | 293     writepos = 0; | 
| jbe@146 | 294     // loop through the characters until encountering end quote: | 
| jbe@121 | 295     while ((c = str[pos++]) != '"') { | 
| jbe@121 | 296       if (c == 0) { | 
| jbe@146 | 297         // handle unexpected end of JSON document: | 
| jbe@121 | 298         goto json_import_unexpected_eof; | 
| jbe@121 | 299       } else if (c < 32 || c == 127) { | 
| jbe@136 | 300         // do not allow ASCII control characters: | 
| jbe@136 | 301         // NOTE: illegal UTF-8 sequences and extended control characters are not sanitized | 
| jbe@136 | 302         //       by this parser to allow different encodings than Unicode | 
| jbe@121 | 303         lua_pushnil(L); | 
| jbe@121 | 304         lua_pushliteral(L, "Unexpected control character in JSON string"); | 
| jbe@121 | 305         return 2; | 
| jbe@121 | 306       } else if (c == '\\') { | 
| jbe@136 | 307         // read next char after backslash escape: | 
| jbe@121 | 308         c = str[pos++]; | 
| jbe@121 | 309         switch (c) { | 
| jbe@136 | 310         // unexpected end-of-string: | 
| jbe@121 | 311         case 0: | 
| jbe@121 | 312           goto json_import_unexpected_eof; | 
| jbe@136 | 313         // unescaping of quotation mark, slash, and backslash: | 
| jbe@121 | 314         case '"': | 
| jbe@121 | 315         case '/': | 
| jbe@121 | 316         case '\\': | 
| jbe@121 | 317           cbuf[writepos++] = c; | 
| jbe@121 | 318           break; | 
| jbe@136 | 319         // unescaping of backspace: | 
| jbe@146 | 320         case 'b': cbuf[writepos++] = '\b'; break; | 
| jbe@136 | 321         // unescaping of form-feed: | 
| jbe@146 | 322         case 'f': cbuf[writepos++] = '\f'; break; | 
| jbe@136 | 323         // unescaping of new-line: | 
| jbe@146 | 324         case 'n': cbuf[writepos++] = '\n'; break; | 
| jbe@136 | 325         // unescaping of carriage-return: | 
| jbe@146 | 326         case 'r': cbuf[writepos++] = '\r'; break; | 
| jbe@136 | 327         // unescaping of tabulator: | 
| jbe@146 | 328         case 't': cbuf[writepos++] = '\t'; break; | 
| jbe@136 | 329         // unescaping of UTF-16 characters | 
| jbe@121 | 330         case 'u': | 
| jbe@121 | 331           lua_pushnil(L); | 
| jbe@121 | 332           lua_pushliteral(L, "JSON unicode escape sequences are not implemented yet");  // TODO | 
| jbe@121 | 333           return 2; | 
| jbe@136 | 334         // unexpected escape sequence: | 
| jbe@121 | 335         default: | 
| jbe@121 | 336           lua_pushnil(L); | 
| jbe@121 | 337           lua_pushliteral(L, "Unexpected string escape sequence in JSON document"); | 
| jbe@121 | 338           return 2; | 
| jbe@121 | 339         } | 
| jbe@121 | 340       } else { | 
| jbe@136 | 341         // normal character: | 
| jbe@121 | 342         cbuf[writepos++] = c; | 
| jbe@121 | 343       } | 
| jbe@121 | 344     } | 
| jbe@136 | 345     // process buffer to Lua string: | 
| jbe@121 | 346     luaL_pushresultsize(&luabuf, writepos); | 
| jbe@136 | 347     // continue with processing of decoded string: | 
| jbe@121 | 348     goto json_import_process_value; | 
| jbe@121 | 349   } | 
| jbe@136 | 350   // process values whose type is is not deducible from a single character: | 
| jbe@136 | 351   if ((c >= '0' && c <= '9') || c == '-' || c == '+') { | 
| jbe@146 | 352     // for numbers, | 
| jbe@146 | 353     // use strtod() call to parse a (double precision) floating point number: | 
| jbe@122 | 354     char *endptr; | 
| jbe@122 | 355     double numval; | 
| jbe@122 | 356     numval = strtod(str+pos, &endptr); | 
| jbe@146 | 357     // catch parsing errors: | 
| jbe@122 | 358     if (endptr == str+pos) goto json_import_syntax_error; | 
| jbe@146 | 359     // consume characters that were parsed: | 
| jbe@122 | 360     pos += endptr - (str+pos); | 
| jbe@146 | 361     // push parsed (double precision) floating point number on Lua stack: | 
| jbe@122 | 362     lua_pushnumber(L, numval); | 
| jbe@122 | 363   } else if (!strncmp(str+pos, "true", 4)) { | 
| jbe@136 | 364     // consume 4 input characters for "true": | 
| jbe@121 | 365     pos += 4; | 
| jbe@147 | 366     // put Lua true value onto stack: | 
| jbe@136 | 367     lua_pushboolean(L, 1); | 
| jbe@121 | 368   } else if (!strncmp(str+pos, "false", 5)) { | 
| jbe@136 | 369     // consume 5 input characters for "false": | 
| jbe@121 | 370     pos += 5; | 
| jbe@147 | 371     // put Lua false value onto stack: | 
| jbe@136 | 372     lua_pushboolean(L, 0); | 
| jbe@121 | 373   } else if (!strncmp(str+pos, "null", 4)) { | 
| jbe@136 | 374     // consume 4 input characters for "null": | 
| jbe@136 | 375     pos += 4; | 
| jbe@147 | 376     // put special null-marker onto stack: | 
| jbe@138 | 377     lua_pushvalue(L, json_import_nullmark_idx); | 
| jbe@121 | 378   } else { | 
| jbe@136 | 379     // all other cases are a syntax error: | 
| jbe@121 | 380     goto json_import_syntax_error; | 
| jbe@121 | 381   } | 
| jbe@136 | 382   // process a decoded value or key value pair (expected on top of Lua stack): | 
| jbe@136 | 383   json_import_process_value: | 
| jbe@121 | 384   switch (mode) { | 
| jbe@136 | 385   // an object key has been read: | 
| jbe@124 | 386   case JSON_STATE_OBJECT_KEY: | 
| jbe@136 | 387     // if an object key is not a string, then this is a syntax error: | 
| jbe@121 | 388     if (lua_type(L, -1) != LUA_TSTRING) goto json_import_syntax_error; | 
| jbe@146 | 389     // expect key terminator to follow: | 
| jbe@124 | 390     mode = JSON_STATE_OBJECT_KEY_TERMINATOR; | 
| jbe@146 | 391     // continue with loop: | 
| jbe@121 | 392     goto json_import_loop; | 
| jbe@136 | 393   // a key value pair has been read: | 
| jbe@124 | 394   case JSON_STATE_OBJECT_VALUE: | 
| jbe@136 | 395     // store key value pair in outer shadow table: | 
| jbe@130 | 396     lua_rawset(L, -3); | 
| jbe@146 | 397     // expect value terminator (or end of object) to follow: | 
| jbe@124 | 398     mode = JSON_STATE_OBJECT_SEPARATOR; | 
| jbe@146 | 399     // continue with loop: | 
| jbe@121 | 400     goto json_import_loop; | 
| jbe@136 | 401   // an array value has been read: | 
| jbe@124 | 402   case JSON_STATE_ARRAY_VALUE: | 
| jbe@136 | 403     // store value in outer shadow table: | 
| jbe@140 | 404     lua_rawseti(L, -3, lua_rawlen(L, -3) + 1); | 
| jbe@146 | 405     // expect value terminator (or end of object) to follow: | 
| jbe@124 | 406     mode = JSON_STATE_ARRAY_SEPARATOR; | 
| jbe@146 | 407     // continue with loop | 
| jbe@121 | 408     goto json_import_loop; | 
| jbe@136 | 409   // a single value has been read: | 
| jbe@124 | 410   case JSON_STATE_VALUE: | 
| jbe@136 | 411     // leave value on top of stack, expect end of JSON document, and continue with loop: | 
| jbe@124 | 412     mode = JSON_STATE_END; | 
| jbe@121 | 413     goto json_import_loop; | 
| jbe@121 | 414   } | 
| jbe@146 | 415   // syntax error handling (reachable by goto statement): | 
| jbe@136 | 416   json_import_syntax_error: | 
| jbe@121 | 417   lua_pushnil(L); | 
| jbe@121 | 418   lua_pushliteral(L, "Syntax error in JSON document"); | 
| jbe@121 | 419   return 2; | 
| jbe@121 | 420 } | 
| jbe@121 | 421 | 
| jbe@146 | 422 // special Lua stack indicies for json_path function: | 
| jbe@138 | 423 #define json_path_shadowtbl_idx 1 | 
| jbe@138 | 424 #define json_path_nullmark_idx 2 | 
| jbe@146 | 425 | 
| jbe@146 | 426 // stack offset of arguments to json_path function: | 
| jbe@138 | 427 #define json_path_idxshift 2 | 
| jbe@138 | 428 | 
| jbe@146 | 429 // gets a value or its type from a JSON document (passed as first argument) | 
| jbe@147 | 430 // using a path (passed as variable number of keys after first argument): | 
| jbe@137 | 431 static int json_path(lua_State *L, int type_mode) { | 
| jbe@146 | 432   int stacktop;                      // stack index of top of stack (after shifting) | 
| jbe@146 | 433   int idx = 2 + json_path_idxshift;  // stack index of current argument to process | 
| jbe@148 | 434   // insert shadowtbl into stack at position 1 (shifting the arguments): | 
| jbe@144 | 435   json_regfetch(L, shadowtbl); | 
| jbe@138 | 436   lua_insert(L, 1); | 
| jbe@148 | 437   // insert nullmark into stack at position 2 (shifting the arguments): | 
| jbe@145 | 438   json_pushlightref(L, nullmark); | 
| jbe@138 | 439   lua_insert(L, 2); | 
| jbe@146 | 440   // store stack index of top of stack: | 
| jbe@138 | 441   stacktop = lua_gettop(L); | 
| jbe@146 | 442   // use first argument as "current value" (stored on top of stack): | 
| jbe@138 | 443   lua_pushvalue(L, 1 + json_path_idxshift); | 
| jbe@146 | 444   // process each "path key" (2nd argument and following arguments): | 
| jbe@138 | 445   while (idx <= stacktop) { | 
| jbe@146 | 446     // if "current value" (on top of stack) is nil, then the path cannot be walked and nil is returned: | 
| jbe@137 | 447     if (lua_isnil(L, -1)) return 1; | 
| jbe@137 | 448     // try to get shadow table of "current value": | 
| jbe@130 | 449     lua_pushvalue(L, -1); | 
| jbe@138 | 450     lua_rawget(L, json_path_shadowtbl_idx); | 
| jbe@126 | 451     if (lua_isnil(L, -1)) { | 
| jbe@137 | 452       // if no shadow table is found, | 
| jbe@130 | 453       if (lua_type(L, -1) == LUA_TTABLE) { | 
| jbe@146 | 454         // and if "current value" is a table, | 
| jbe@146 | 455         // drop nil from stack: | 
| jbe@146 | 456         lua_pop(L, 1); | 
| jbe@137 | 457         // get "next value" using the "path key": | 
| jbe@130 | 458         lua_pushvalue(L, idx++); | 
| jbe@130 | 459         lua_gettable(L, -2); | 
| jbe@130 | 460       } else { | 
| jbe@137 | 461         // if "current value" is not a table, | 
| jbe@146 | 462         // then the path cannot be walked and nil (already on top of stack) is returned: | 
| jbe@137 | 463         return 1; | 
| jbe@130 | 464       } | 
| jbe@130 | 465     } else { | 
| jbe@137 | 466       // if a shadow table is found, | 
| jbe@137 | 467       // set "current value" to its shadow table: | 
| jbe@130 | 468       lua_replace(L, -2); | 
| jbe@137 | 469       // get "next value" using the "path key": | 
| jbe@130 | 470       lua_pushvalue(L, idx++); | 
| jbe@130 | 471       lua_rawget(L, -2); | 
| jbe@126 | 472     } | 
| jbe@137 | 473     // the "next value" replaces the "current value": | 
| jbe@130 | 474     lua_replace(L, -2); | 
| jbe@126 | 475   } | 
| jbe@137 | 476   if (!type_mode) { | 
| jbe@137 | 477     // if a value (and not its type) was requested, | 
| jbe@137 | 478     // check if value is the null-marker, and store nil on top of Lua stack in that case: | 
| jbe@138 | 479     if (lua_rawequal(L, -1, json_path_nullmark_idx)) lua_pushnil(L); | 
| jbe@137 | 480   } else { | 
| jbe@137 | 481     // if the type was requested, | 
| jbe@137 | 482     // check if value is the null-marker: | 
| jbe@138 | 483     if (lua_rawequal(L, -1, json_path_nullmark_idx)) { | 
| jbe@137 | 484       // if yes, store string "null" on top of Lua stack: | 
| jbe@130 | 485       lua_pushliteral(L, "null"); | 
| jbe@137 | 486     } else { | 
| jbe@137 | 487       // otherwise, | 
| jbe@138 | 488       // check if metatable indicates "object" or "array": | 
| jbe@138 | 489       if (lua_getmetatable(L, -1)) { | 
| jbe@144 | 490         json_regfetch(L, objectmt); | 
| jbe@138 | 491         if (lua_rawequal(L, -2, -1)) { | 
| jbe@146 | 492           // if value has metatable for JSON objects, | 
| jbe@138 | 493           // return string "object": | 
| jbe@138 | 494           lua_pushliteral(L, "object"); | 
| jbe@138 | 495           return 1; | 
| jbe@138 | 496         } | 
| jbe@144 | 497         json_regfetch(L, arraymt); | 
| jbe@138 | 498         if (lua_rawequal(L, -3, -1)) { | 
| jbe@146 | 499           // if value has metatable for JSON arrays, | 
| jbe@146 | 500           // return string "object": | 
| jbe@138 | 501           lua_pushliteral(L, "array"); | 
| jbe@138 | 502           return 1; | 
| jbe@138 | 503         } | 
| jbe@146 | 504         // remove 3 metatables (one of the value, two for comparison) from stack: | 
| jbe@138 | 505         lua_pop(L, 3); | 
| jbe@138 | 506       } | 
| jbe@138 | 507       // otherwise, get the Lua type: | 
| jbe@138 | 508       lua_pushstring(L, lua_typename(L, lua_type(L, -1))); | 
| jbe@126 | 509     } | 
| jbe@126 | 510   } | 
| jbe@137 | 511   // return the top most value on the Lua stack: | 
| jbe@137 | 512   return 1; | 
| jbe@130 | 513 } | 
| jbe@130 | 514 | 
| jbe@147 | 515 // gets a value from a JSON document (passed as first argument) | 
| jbe@147 | 516 // using a path (passed as variable number of keys after first argument): | 
| jbe@130 | 517 static int json_get(lua_State *L) { | 
| jbe@137 | 518   return json_path(L, 0); | 
| jbe@130 | 519 } | 
| jbe@130 | 520 | 
| jbe@147 | 521 // gets a value's type from a JSON document (passed as first argument) | 
| jbe@147 | 522 // using a path (variable number of keys after first argument): | 
| jbe@130 | 523 static int json_type(lua_State *L) { | 
| jbe@137 | 524   return json_path(L, 1); | 
| jbe@130 | 525 } | 
| jbe@130 | 526 | 
| jbe@147 | 527 // checks if a value in a JSON document (first argument) is | 
| jbe@147 | 528 // explicitly set to null: | 
| jbe@130 | 529 static int json_isnull(lua_State *L) { | 
| jbe@137 | 530   const char *jsontype; | 
| jbe@147 | 531   // call json_type function with variable arguments: | 
| jbe@138 | 532   lua_pushcfunction(L, json_type); | 
| jbe@137 | 533   lua_insert(L, 1); | 
| jbe@137 | 534   lua_call(L, lua_gettop(L) - 1, 1); | 
| jbe@147 | 535   // return true if result equals to string "null", otherwise return false: | 
| jbe@137 | 536   jsontype = lua_tostring(L, -1); | 
| jbe@137 | 537   if (jsontype && !strcmp(jsontype, "null")) lua_pushboolean(L, 1); | 
| jbe@137 | 538   else lua_pushboolean(L, 0); | 
| jbe@137 | 539   return 1; | 
| jbe@130 | 540 } | 
| jbe@130 | 541 | 
| jbe@146 | 542 // special Lua stack indicies for json_setnull function: | 
| jbe@138 | 543 #define json_setnull_unknownmt_idx 3 | 
| jbe@138 | 544 #define json_setnull_objectmt_idx 4 | 
| jbe@138 | 545 #define json_setnull_arraymt_idx 5 | 
| jbe@138 | 546 #define json_setnull_shadowtbl_idx 6 | 
| jbe@138 | 547 | 
| jbe@147 | 548 // sets a value in a JSON object or JSON array explicitly to null: | 
| jbe@147 | 549 // NOTE: JSON null is different than absence of a key | 
| jbe@131 | 550 static int json_setnull(lua_State *L) { | 
| jbe@147 | 551   // stack shall contain two function arguments: | 
| jbe@131 | 552   lua_settop(L, 2); | 
| jbe@148 | 553   // push unknownmt onto stack position 3: | 
| jbe@144 | 554   json_regfetch(L, unknownmt); | 
| jbe@148 | 555   // push objectmt onto stack position 4: | 
| jbe@144 | 556   json_regfetch(L, objectmt); | 
| jbe@148 | 557   // push arraymt onto stack position 5: | 
| jbe@144 | 558   json_regfetch(L, arraymt); | 
| jbe@148 | 559   // push shadowtbl onto stack position 6: | 
| jbe@144 | 560   json_regfetch(L, shadowtbl); | 
| jbe@147 | 561   // set metatable if necessary (leaves unknown number of elements on stack): | 
| jbe@138 | 562   if ( | 
| jbe@147 | 563     !lua_getmetatable(L, 1) || ( | 
| jbe@147 | 564       !lua_rawequal(L, -1, json_setnull_unknownmt_idx) && | 
| jbe@147 | 565       !lua_rawequal(L, -1, json_setnull_objectmt_idx) && | 
| jbe@147 | 566       !lua_rawequal(L, -1, json_setnull_arraymt_idx) | 
| jbe@147 | 567     ) | 
| jbe@138 | 568   ) { | 
| jbe@138 | 569     lua_pushvalue(L, json_setnull_unknownmt_idx); | 
| jbe@138 | 570     lua_setmetatable(L, 1); | 
| jbe@138 | 571   } | 
| jbe@147 | 572   // try to get shadow table: | 
| jbe@131 | 573   lua_pushvalue(L, 1); | 
| jbe@138 | 574   lua_rawget(L, json_setnull_shadowtbl_idx); | 
| jbe@131 | 575   if (lua_isnil(L, -1)) { | 
| jbe@147 | 576     // if no shadow table is found, | 
| jbe@147 | 577     // create new shadow table (and leave it on top of stack): | 
| jbe@131 | 578     lua_newtable(L); | 
| jbe@147 | 579     // register shadow table: | 
| jbe@131 | 580     lua_pushvalue(L, 1); | 
| jbe@131 | 581     lua_pushvalue(L, -2); | 
| jbe@138 | 582     lua_rawset(L, json_setnull_shadowtbl_idx); | 
| jbe@131 | 583   } | 
| jbe@147 | 584   // push key (second argument) and null-marker after shadow table onto stack: | 
| jbe@131 | 585   lua_pushvalue(L, 2); | 
| jbe@145 | 586   json_pushlightref(L, nullmark); | 
| jbe@147 | 587   // store key and null-marker in shadow table: | 
| jbe@131 | 588   lua_rawset(L, -3); | 
| jbe@147 | 589   // return nothing: | 
| jbe@131 | 590   return 0; | 
| jbe@131 | 591 } | 
| jbe@131 | 592 | 
| jbe@147 | 593 // returns the length of a JSON array (or zero for a table without numeric keys): | 
| jbe@130 | 594 static int json_len(lua_State *L) { | 
| jbe@147 | 595   // stack shall contain one function argument: | 
| jbe@130 | 596   lua_settop(L, 1); | 
| jbe@148 | 597   // try to get corresponding shadow table for first argument: | 
| jbe@144 | 598   json_regfetch(L, shadowtbl); | 
| jbe@130 | 599   lua_pushvalue(L, 1); | 
| jbe@138 | 600   lua_rawget(L, -2); | 
| jbe@147 | 601   // if shadow table does not exist, return length of argument, else length of shadow table: | 
| jbe@147 | 602   lua_pushnumber(L, lua_rawlen(L, lua_isnil(L, -1) ? 1 : -1)); | 
| jbe@123 | 603   return 1; | 
| jbe@123 | 604 } | 
| jbe@123 | 605 | 
| jbe@146 | 606 // special Lua stack indicies for json_index function: | 
| jbe@141 | 607 #define json_index_nullmark_idx 3 | 
| jbe@141 | 608 #define json_index_shadowtbl_idx 4 | 
| jbe@141 | 609 | 
| jbe@130 | 610 static int json_index(lua_State *L) { | 
| jbe@148 | 611   // stack shall contain two function arguments: | 
| jbe@130 | 612   lua_settop(L, 2); | 
| jbe@148 | 613   // push nullmark onto stack position 3: | 
| jbe@148 | 614   json_pushlightref(L, nullmark); | 
| jbe@148 | 615   // push shadowtbl onto stack position 4: | 
| jbe@144 | 616   json_regfetch(L, shadowtbl); | 
| jbe@148 | 617   // get corresponding shadow table for first argument: | 
| jbe@130 | 618   lua_pushvalue(L, 1); | 
| jbe@141 | 619   lua_rawget(L, json_index_shadowtbl_idx); | 
| jbe@148 | 620   // throw error if no shadow table was found: | 
| jbe@139 | 621   if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found"); | 
| jbe@148 | 622   // use key passed as second argument to lookup value in shadow table: | 
| jbe@130 | 623   lua_pushvalue(L, 2); | 
| jbe@130 | 624   lua_rawget(L, -2); | 
| jbe@148 | 625   // if value is null-marker, then push nil onto stack: | 
| jbe@141 | 626   if (lua_rawequal(L, -1, json_index_nullmark_idx)) lua_pushnil(L); | 
| jbe@148 | 627   // return either looked up value, or nil | 
| jbe@127 | 628   return 1; | 
| jbe@127 | 629 } | 
| jbe@127 | 630 | 
| jbe@130 | 631 static int json_newindex(lua_State *L) { | 
| jbe@148 | 632   // stack shall contain three function arguments: | 
| jbe@130 | 633   lua_settop(L, 3); | 
| jbe@148 | 634   // get corresponding shadow table for first argument: | 
| jbe@144 | 635   json_regfetch(L, shadowtbl); | 
| jbe@123 | 636   lua_pushvalue(L, 1); | 
| jbe@143 | 637   lua_rawget(L, -2); | 
| jbe@148 | 638   // throw error if no shadow table was found: | 
| jbe@130 | 639   if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found"); | 
| jbe@148 | 640   // replace first argument with shadow table: | 
| jbe@130 | 641   lua_replace(L, 1); | 
| jbe@148 | 642   // reset stack and use second and third argument to write to shadow table: | 
| jbe@139 | 643   lua_settop(L, 3); | 
| jbe@130 | 644   lua_rawset(L, 1); | 
| jbe@148 | 645   // return nothing: | 
| jbe@148 | 646   return 0; | 
| jbe@121 | 647 } | 
| jbe@121 | 648 | 
| jbe@146 | 649 // special Lua stack indicies for json_pairs_iterfunc function: | 
| jbe@139 | 650 #define json_pairs_iterfunc_nullmark_idx 3 | 
| jbe@139 | 651 #define json_pairs_iterfunc_shadowtbl_idx 4 | 
| jbe@139 | 652 | 
| jbe@135 | 653 static int json_pairs_iterfunc(lua_State *L) { | 
| jbe@149 | 654   // stack shall contain two function arguments: | 
| jbe@135 | 655   lua_settop(L, 2); | 
| jbe@149 | 656   // push nullmark onto stack position 3: | 
| jbe@149 | 657   json_pushlightref(L, nullmark); | 
| jbe@149 | 658   // push shadowtbl onto stack position 4: | 
| jbe@144 | 659   json_regfetch(L, shadowtbl); | 
| jbe@149 | 660   // get corresponding shadow table for first argument: | 
| jbe@135 | 661   lua_pushvalue(L, 1); | 
| jbe@139 | 662   lua_rawget(L, json_pairs_iterfunc_shadowtbl_idx); | 
| jbe@149 | 663   // throw error if no shadow table was found: | 
| jbe@135 | 664   if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found"); | 
| jbe@149 | 665   // get next key value pair from shadow table (using previous key from argument 2) | 
| jbe@149 | 666   // and return nothing if there is no next pair: | 
| jbe@135 | 667   lua_pushvalue(L, 2); | 
| jbe@135 | 668   if (!lua_next(L, -2)) return 0; | 
| jbe@149 | 669   // replace null-marker with nil: | 
| jbe@139 | 670   if (lua_rawequal(L, -1, json_pairs_iterfunc_nullmark_idx)) { | 
| jbe@135 | 671     lua_pop(L, 1); | 
| jbe@135 | 672     lua_pushnil(L); | 
| jbe@135 | 673   } | 
| jbe@149 | 674   // return key and value (or key and nil, if null-marker was found): | 
| jbe@135 | 675   return 2; | 
| jbe@135 | 676 } | 
| jbe@135 | 677 | 
| jbe@149 | 678 // returns a triple such that 'for key, value in pairs(obj) do ... end' | 
| jbe@149 | 679 // iterates through all key value pairs (including JSON null keys represented as Lua nil): | 
| jbe@135 | 680 static int json_pairs(lua_State *L) { | 
| jbe@149 | 681   // return triple of function json_pairs_iterfunc, first argument, and nil: | 
| jbe@139 | 682   lua_pushcfunction(L, json_pairs_iterfunc); | 
| jbe@135 | 683   lua_pushvalue(L, 1); | 
| jbe@135 | 684   lua_pushnil(L); | 
| jbe@135 | 685   return 3; | 
| jbe@135 | 686 } | 
| jbe@135 | 687 | 
| jbe@146 | 688 // special Lua stack indicies for json_ipairs_iterfunc function: | 
| jbe@139 | 689 #define json_ipairs_iterfunc_nullmark_idx 3 | 
| jbe@139 | 690 #define json_ipairs_iterfunc_shadowtbl_idx 4 | 
| jbe@139 | 691 | 
| jbe@134 | 692 static int json_ipairs_iterfunc(lua_State *L) { | 
| jbe@134 | 693   int idx; | 
| jbe@149 | 694   // stack shall contain two function arguments: | 
| jbe@134 | 695   lua_settop(L, 2); | 
| jbe@149 | 696   // push nullmark onto stack position 3: | 
| jbe@149 | 697   json_pushlightref(L, nullmark); | 
| jbe@149 | 698   // push shadowtbl onto stack position 4: | 
| jbe@144 | 699   json_regfetch(L, shadowtbl); | 
| jbe@149 | 700   // calculate new index by incrementing second argument: | 
| jbe@134 | 701   idx = lua_tointeger(L, 2) + 1; | 
| jbe@149 | 702   // get corresponding shadow table for first argument: | 
| jbe@134 | 703   lua_pushvalue(L, 1); | 
| jbe@139 | 704   lua_rawget(L, json_ipairs_iterfunc_shadowtbl_idx); | 
| jbe@149 | 705   // throw error if no shadow table was found: | 
| jbe@134 | 706   if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found"); | 
| jbe@149 | 707   // do integer lookup in shadow table: | 
| jbe@134 | 708   lua_rawgeti(L, -1, idx); | 
| jbe@149 | 709   // return nothing if there was no value: | 
| jbe@134 | 710   if (lua_isnil(L, -1)) return 0; | 
| jbe@149 | 711   // return new index and | 
| jbe@149 | 712   // either the looked up value if it is not equal to the null-marker | 
| jbe@149 | 713   // or nil instead of null-marker: | 
| jbe@134 | 714   lua_pushinteger(L, idx); | 
| jbe@139 | 715   if (lua_rawequal(L, -2, json_ipairs_iterfunc_nullmark_idx)) lua_pushnil(L); | 
| jbe@134 | 716   else lua_pushvalue(L, -2); | 
| jbe@134 | 717   return 2; | 
| jbe@134 | 718 } | 
| jbe@134 | 719 | 
| jbe@149 | 720 // returns a triple such that 'for idx, value in ipairs(ary) do ... end' | 
| jbe@149 | 721 // iterates through all values (including JSON null represented as Lua nil): | 
| jbe@134 | 722 static int json_ipairs(lua_State *L) { | 
| jbe@149 | 723   // return triple of function json_ipairs_iterfunc, first argument, and zero: | 
| jbe@139 | 724   lua_pushcfunction(L, json_ipairs_iterfunc); | 
| jbe@134 | 725   lua_pushvalue(L, 1); | 
| jbe@134 | 726   lua_pushinteger(L, 0); | 
| jbe@134 | 727   return 3; | 
| jbe@134 | 728 } | 
| jbe@134 | 729 | 
| jbe@149 | 730 // functions in library module: | 
| jbe@121 | 731 static const struct luaL_Reg json_module_functions[] = { | 
| jbe@133 | 732   {"object", json_object}, | 
| jbe@133 | 733   {"array", json_array}, | 
| jbe@121 | 734   {"import", json_import}, | 
| jbe@130 | 735   {"get", json_get}, | 
| jbe@127 | 736   {"type", json_type}, | 
| jbe@123 | 737   {"isnull", json_isnull}, | 
| jbe@131 | 738   {"setnull", json_setnull}, | 
| jbe@121 | 739   {NULL, NULL} | 
| jbe@121 | 740 }; | 
| jbe@121 | 741 | 
| jbe@149 | 742 // metamethods for JSON objects, JSON arrays, and unknown JSON collections (object or array): | 
| jbe@126 | 743 static const struct luaL_Reg json_metatable_functions[] = { | 
| jbe@130 | 744   {"__len", json_len}, | 
| jbe@130 | 745   {"__index", json_index}, | 
| jbe@130 | 746   {"__newindex", json_newindex}, | 
| jbe@135 | 747   {"__pairs", json_pairs}, | 
| jbe@134 | 748   {"__ipairs", json_ipairs}, | 
| jbe@126 | 749   {NULL, NULL} | 
| jbe@126 | 750 }; | 
| jbe@126 | 751 | 
| jbe@149 | 752 // initializes json library: | 
| jbe@121 | 753 int luaopen_json(lua_State *L) { | 
| jbe@149 | 754   // empty stack: | 
| jbe@126 | 755   lua_settop(L, 0); | 
| jbe@149 | 756   // push library module onto stack position 1: | 
| jbe@149 | 757   lua_newtable(L); | 
| jbe@149 | 758   // register library functions: | 
| jbe@149 | 759   luaL_setfuncs(L, json_module_functions, 0); | 
| jbe@149 | 760   // create and store unknownmt: | 
| jbe@138 | 761   lua_newtable(L); | 
| jbe@138 | 762   luaL_setfuncs(L, json_metatable_functions, 0); | 
| jbe@144 | 763   json_regstore(L, unknownmt); | 
| jbe@149 | 764   // create and store objectmt: | 
| jbe@138 | 765   lua_newtable(L); | 
| jbe@138 | 766   luaL_setfuncs(L, json_metatable_functions, 0); | 
| jbe@144 | 767   json_regstore(L, objectmt); | 
| jbe@149 | 768   // create and store arraymt: | 
| jbe@138 | 769   lua_newtable(L); | 
| jbe@138 | 770   luaL_setfuncs(L, json_metatable_functions, 0); | 
| jbe@144 | 771   json_regstore(L, arraymt); | 
| jbe@149 | 772   // create and store ephemeron table to store shadow tables for each JSON object/array | 
| jbe@149 | 773   // to allow NULL values returned as nil | 
| jbe@149 | 774   lua_newtable(L); | 
| jbe@138 | 775   lua_newtable(L);  // metatable for ephemeron table | 
| jbe@121 | 776   lua_pushliteral(L, "__mode"); | 
| jbe@121 | 777   lua_pushliteral(L, "k"); | 
| jbe@138 | 778   lua_rawset(L, -3); | 
| jbe@138 | 779   lua_setmetatable(L, -2); | 
| jbe@144 | 780   json_regstore(L, shadowtbl); | 
| jbe@149 | 781   // return library module stored on lowest stack position: | 
| jbe@138 | 782   lua_settop(L, 1); | 
| jbe@121 | 783   return 1; | 
| jbe@121 | 784 } |