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