webmcp

annotate libraries/json/json.c @ 141:ca27aae3f1a1

Finished removing all upvalues from JSON library
author jbe
date Wed Jul 30 01:47:01 2014 +0200 (2014-07-30)
parents 641619d3fcb1
children a686ed2ce967
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@138 6 #define JSON_REGENT static char
jbe@138 7 #define JSON_REGREF void *
jbe@138 8
jbe@138 9 JSON_REGENT json_nullmark;
jbe@138 10 JSON_REGENT json_shadowtbl;
jbe@138 11 JSON_REGENT json_unknownmt;
jbe@138 12 JSON_REGENT json_objectmt;
jbe@138 13 JSON_REGENT json_arraymt;
jbe@138 14
jbe@138 15 #define json_regfetch(L, x) (lua_pushlightuserdata((L), &(x)), lua_rawget((L), LUA_REGISTRYINDEX))
jbe@138 16
jbe@138 17 #define json_regstore(L, x) (lua_pushlightuserdata(L, &(x)), lua_pushvalue(L, -2), lua_rawset(L, LUA_REGISTRYINDEX));
jbe@138 18
jbe@136 19 // marks a table as JSON object or JSON array:
jbe@136 20 // (returns its modified argument or a new table if argument is nil)
jbe@138 21 static int json_mark(lua_State *L, JSON_REGREF mt) {
jbe@136 22 // if argument is nil, then create new table:
jbe@136 23 if (lua_isnoneornil(L, 1)) {
jbe@136 24 lua_settop(L, 0);
jbe@136 25 lua_newtable(L);
jbe@136 26 // skip testing of existing shadow table:
jbe@136 27 goto json_object_create_shadow_table;
jbe@136 28 }
jbe@138 29 // check if shadow table already exists:
jbe@138 30 json_regfetch(L, json_shadowtbl);
jbe@136 31 lua_pushvalue(L, 1);
jbe@138 32 lua_rawget(L, -2);
jbe@136 33 if (lua_isnil(L, -1)) {
jbe@136 34 json_object_create_shadow_table:
jbe@136 35 // set shadow table:
jbe@136 36 lua_pushvalue(L, 1);
jbe@136 37 lua_newtable(L);
jbe@138 38 lua_rawset(L, -4);
jbe@136 39 }
jbe@138 40 // discard everything but table to return:
jbe@138 41 lua_settop(L, 1);
jbe@136 42 // set metatable:
jbe@138 43 json_regfetch(L, mt);
jbe@136 44 lua_setmetatable(L, 1);
jbe@138 45 // return table:
jbe@136 46 return 1;
jbe@136 47 }
jbe@136 48
jbe@136 49 // marks a table as JSON object:
jbe@136 50 // (returns its modified argument or a new table if argument is nil)
jbe@136 51 static int json_object(lua_State *L) {
jbe@138 52 return json_mark(L, &json_objectmt);
jbe@136 53 }
jbe@136 54
jbe@136 55 // marks a table as JSON array:
jbe@136 56 // (returns its modified argument or a new table if argument is nil)
jbe@136 57 static int json_array(lua_State *L) {
jbe@138 58 return json_mark(L, &json_arraymt);
jbe@136 59 }
jbe@136 60
jbe@124 61 #define JSON_STATE_VALUE 0
jbe@124 62 #define JSON_STATE_OBJECT_KEY 1
jbe@124 63 #define JSON_STATE_OBJECT_KEY_TERMINATOR 2
jbe@124 64 #define JSON_STATE_OBJECT_VALUE 3
jbe@124 65 #define JSON_STATE_OBJECT_SEPARATOR 4
jbe@124 66 #define JSON_STATE_ARRAY_VALUE 5
jbe@124 67 #define JSON_STATE_ARRAY_SEPARATOR 6
jbe@124 68 #define JSON_STATE_END 7
jbe@121 69
jbe@138 70 #define json_import_objectmt_idx 2
jbe@138 71 #define json_import_arraymt_idx 3
jbe@138 72 #define json_import_shadowtbl_idx 4
jbe@138 73 #define json_import_nullmark_idx 5
jbe@138 74
jbe@136 75 // decodes a JSON document:
jbe@121 76 static int json_import(lua_State *L) {
jbe@136 77 const char *str; // string to parse
jbe@136 78 size_t total; // total length of string to parse
jbe@136 79 size_t pos = 0; // current position in string to parse
jbe@136 80 size_t level = 0; // nested levels of objects/arrays currently being processed
jbe@136 81 int mode = JSON_STATE_VALUE; // state of parser
jbe@136 82 char c; // variable to store a single character to be processed
jbe@136 83 luaL_Buffer luabuf; // Lua buffer to decode (possibly escaped) strings
jbe@136 84 char *cbuf; // C buffer to decode (possibly escaped) strings
jbe@136 85 size_t writepos; // write position of decoded strings in C buffer
jbe@138 86 // limit stack to 1 element:
jbe@138 87 lua_settop(L, 1);
jbe@138 88 // push json_objectmt on stack position 2:
jbe@138 89 json_regfetch(L, json_objectmt);
jbe@138 90 // push json_arraymt on stack position 3:
jbe@138 91 json_regfetch(L, json_arraymt);
jbe@138 92 // push json_shadowtbl on stack position 4:
jbe@138 93 json_regfetch(L, json_shadowtbl);
jbe@138 94 // push json_nullmark on stack position 5:
jbe@138 95 json_regfetch(L, json_nullmark);
jbe@136 96 // require string as first argument:
jbe@136 97 str = luaL_checklstring(L, 1, &total);
jbe@136 98 // if string contains a NULL byte, this is a syntax error
jbe@136 99 if (strlen(str) != total) goto json_import_syntax_error;
jbe@136 100 // main loop of parser:
jbe@136 101 json_import_loop:
jbe@136 102 // skip whitespace and store next character in variable 'c':
jbe@121 103 while (c = str[pos], c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\f') pos++;
jbe@136 104 // switch statement to handle certain (single) characters:
jbe@121 105 switch (c) {
jbe@136 106 // handle end of JSON document:
jbe@121 107 case 0:
jbe@136 108 // if end of JSON document was expected, then return top element of stack as result:
jbe@124 109 if (mode == JSON_STATE_END) return 1;
jbe@136 110 // otherwise, the JSON document was malformed:
jbe@121 111 json_import_unexpected_eof:
jbe@121 112 lua_pushnil(L);
jbe@121 113 if (level == 0) lua_pushliteral(L, "Empty string");
jbe@121 114 else lua_pushliteral(L, "Unexpected end of JSON document");
jbe@121 115 return 2;
jbe@136 116 // new JSON object:
jbe@121 117 case '{':
jbe@136 118 // if a JSON object is not expected here, then return an error:
jbe@124 119 if (mode != JSON_STATE_VALUE && mode != JSON_STATE_OBJECT_VALUE && mode != JSON_STATE_ARRAY_VALUE)
jbe@121 120 goto json_import_syntax_error;
jbe@136 121 // consume input character:
jbe@121 122 pos++;
jbe@136 123 // create JSON object on stack:
jbe@136 124 lua_newtable(L);
jbe@136 125 // set metatable of JSON object:
jbe@138 126 lua_pushvalue(L, json_import_objectmt_idx);
jbe@125 127 lua_setmetatable(L, -2);
jbe@136 128 // create internal shadow table on stack:
jbe@136 129 lua_newtable(L);
jbe@138 130 // register internal shadow table (and cleanup stack afterwards):
jbe@123 131 lua_pushvalue(L, -2);
jbe@123 132 lua_pushvalue(L, -2);
jbe@138 133 lua_rawset(L, json_import_shadowtbl_idx);
jbe@136 134 // increment level:
jbe@121 135 level++;
jbe@136 136 // expect object key (or end of object) and continue with loop:
jbe@136 137 mode = JSON_STATE_OBJECT_KEY;
jbe@121 138 goto json_import_loop;
jbe@136 139 // new JSON array:
jbe@121 140 case '[':
jbe@136 141 // if a JSON array is not expected here, then return an error:
jbe@124 142 if (mode != JSON_STATE_VALUE && mode != JSON_STATE_OBJECT_VALUE && mode != JSON_STATE_ARRAY_VALUE)
jbe@121 143 goto json_import_syntax_error;
jbe@136 144 // consume input character:
jbe@121 145 pos++;
jbe@136 146 // create JSON array on stack:
jbe@136 147 lua_newtable(L);
jbe@136 148 // set metatable of JSON array:
jbe@138 149 lua_pushvalue(L, json_import_arraymt_idx);
jbe@125 150 lua_setmetatable(L, -2);
jbe@136 151 // create internal shadow table on stack:
jbe@136 152 lua_newtable(L);
jbe@138 153 // register internal shadow table (and cleanup stack afterwards):
jbe@123 154 lua_pushvalue(L, -2);
jbe@123 155 lua_pushvalue(L, -2);
jbe@138 156 lua_rawset(L, json_import_shadowtbl_idx);
jbe@140 157 // add nil as key (needed to keep stack balance) and as magic to detect arrays:
jbe@140 158 lua_pushnil(L);
jbe@136 159 // increment level:
jbe@121 160 level++;
jbe@136 161 // expect array value (or end of array) and continue with loop:
jbe@136 162 mode = JSON_STATE_ARRAY_VALUE;
jbe@121 163 goto json_import_loop;
jbe@136 164 // end of JSON object:
jbe@121 165 case '}':
jbe@136 166 // if end of JSON object is not expected here, then return an error:
jbe@124 167 if (mode != JSON_STATE_OBJECT_KEY && mode != JSON_STATE_OBJECT_SEPARATOR)
jbe@121 168 goto json_import_syntax_error;
jbe@136 169 // jump to common code for end of JSON object and JSON array:
jbe@121 170 goto json_import_close;
jbe@136 171 // end of JSON array:
jbe@121 172 case ']':
jbe@136 173 // if end of JSON array is not expected here, then return an error:
jbe@124 174 if (mode != JSON_STATE_ARRAY_VALUE && mode != JSON_STATE_ARRAY_SEPARATOR)
jbe@121 175 goto json_import_syntax_error;
jbe@140 176 // pop nil key/magic:
jbe@140 177 lua_pop(L, 1);
jbe@136 178 // continue with common code for end of JSON object and JSON array:
jbe@136 179 // common code for end of JSON object or JSON array:
jbe@121 180 json_import_close:
jbe@136 181 // consume input character:
jbe@121 182 pos++;
jbe@136 183 // pop shadow table:
jbe@136 184 lua_pop(L, 1);
jbe@136 185 // check if nested:
jbe@121 186 if (--level) {
jbe@136 187 // if nested, then check if outer(!) structure is an array or object:
jbe@140 188 if (lua_isnil(L, -2)) {
jbe@136 189 // select array value processing:
jbe@124 190 mode = JSON_STATE_ARRAY_VALUE;
jbe@121 191 } else {
jbe@136 192 // select object value processing:
jbe@124 193 mode = JSON_STATE_OBJECT_VALUE;
jbe@121 194 }
jbe@136 195 // store value in outer structure:
jbe@121 196 goto json_import_process_value;
jbe@121 197 }
jbe@136 198 // if not nested, then expect end of JSON document and continue with loop:
jbe@136 199 mode = JSON_STATE_END;
jbe@121 200 goto json_import_loop;
jbe@136 201 // key terminator:
jbe@121 202 case ':':
jbe@136 203 // if key terminator is not expected here, then return an error:
jbe@124 204 if (mode != JSON_STATE_OBJECT_KEY_TERMINATOR)
jbe@121 205 goto json_import_syntax_error;
jbe@136 206 // consume input character:
jbe@121 207 pos++;
jbe@136 208 // set state of parser and continue with loop:
jbe@124 209 mode = JSON_STATE_OBJECT_VALUE;
jbe@121 210 goto json_import_loop;
jbe@136 211 // value terminator (NOTE: trailing comma at end of value or key-value list is tolerated by this parser)
jbe@121 212 case ',':
jbe@136 213 // change parser state accordingly:
jbe@124 214 if (mode == JSON_STATE_OBJECT_SEPARATOR) {
jbe@124 215 mode = JSON_STATE_OBJECT_KEY;
jbe@124 216 } else if (mode == JSON_STATE_ARRAY_SEPARATOR) {
jbe@124 217 mode = JSON_STATE_ARRAY_VALUE;
jbe@121 218 } else {
jbe@136 219 // if value terminator is not expected here, then return an error:
jbe@136 220 goto json_import_syntax_error;
jbe@121 221 }
jbe@136 222 // consume input character:
jbe@121 223 pos++;
jbe@136 224 // continue with loop:
jbe@121 225 goto json_import_loop;
jbe@136 226 // string literal:
jbe@121 227 case '"':
jbe@136 228 // prepare buffer to decode string (with maximum possible length) and set write position to zero:
jbe@121 229 cbuf = luaL_buffinitsize(L, &luabuf, total-pos);
jbe@121 230 writepos = 0;
jbe@136 231 // consume quote character:
jbe@121 232 pos++;
jbe@136 233 // read next character until encountering end quote:
jbe@121 234 while ((c = str[pos++]) != '"') {
jbe@121 235 if (c == 0) {
jbe@136 236 // handle unexpected end-of-string:
jbe@121 237 goto json_import_unexpected_eof;
jbe@121 238 } else if (c < 32 || c == 127) {
jbe@136 239 // do not allow ASCII control characters:
jbe@136 240 // NOTE: illegal UTF-8 sequences and extended control characters are not sanitized
jbe@136 241 // by this parser to allow different encodings than Unicode
jbe@121 242 lua_pushnil(L);
jbe@121 243 lua_pushliteral(L, "Unexpected control character in JSON string");
jbe@121 244 return 2;
jbe@121 245 } else if (c == '\\') {
jbe@136 246 // read next char after backslash escape:
jbe@121 247 c = str[pos++];
jbe@121 248 switch (c) {
jbe@136 249 // unexpected end-of-string:
jbe@121 250 case 0:
jbe@121 251 goto json_import_unexpected_eof;
jbe@136 252 // unescaping of quotation mark, slash, and backslash:
jbe@121 253 case '"':
jbe@121 254 case '/':
jbe@121 255 case '\\':
jbe@121 256 cbuf[writepos++] = c;
jbe@121 257 break;
jbe@136 258 // unescaping of backspace:
jbe@121 259 case 'b':
jbe@121 260 cbuf[writepos++] = '\b';
jbe@121 261 break;
jbe@136 262 // unescaping of form-feed:
jbe@121 263 case 'f':
jbe@121 264 cbuf[writepos++] = '\f';
jbe@121 265 break;
jbe@136 266 // unescaping of new-line:
jbe@121 267 case 'n':
jbe@121 268 cbuf[writepos++] = '\n';
jbe@121 269 break;
jbe@136 270 // unescaping of carriage-return:
jbe@121 271 case 'r':
jbe@121 272 cbuf[writepos++] = '\r';
jbe@121 273 break;
jbe@136 274 // unescaping of tabulator:
jbe@121 275 case 't':
jbe@121 276 cbuf[writepos++] = '\t';
jbe@121 277 break;
jbe@136 278 // unescaping of UTF-16 characters
jbe@121 279 case 'u':
jbe@121 280 lua_pushnil(L);
jbe@121 281 lua_pushliteral(L, "JSON unicode escape sequences are not implemented yet"); // TODO
jbe@121 282 return 2;
jbe@136 283 // unexpected escape sequence:
jbe@121 284 default:
jbe@121 285 lua_pushnil(L);
jbe@121 286 lua_pushliteral(L, "Unexpected string escape sequence in JSON document");
jbe@121 287 return 2;
jbe@121 288 }
jbe@121 289 } else {
jbe@136 290 // normal character:
jbe@121 291 cbuf[writepos++] = c;
jbe@121 292 }
jbe@121 293 }
jbe@136 294 // process buffer to Lua string:
jbe@121 295 luaL_pushresultsize(&luabuf, writepos);
jbe@136 296 // continue with processing of decoded string:
jbe@121 297 goto json_import_process_value;
jbe@121 298 }
jbe@136 299 // process values whose type is is not deducible from a single character:
jbe@136 300 if ((c >= '0' && c <= '9') || c == '-' || c == '+') {
jbe@136 301 // numbers:
jbe@122 302 char *endptr;
jbe@122 303 double numval;
jbe@122 304 numval = strtod(str+pos, &endptr);
jbe@122 305 if (endptr == str+pos) goto json_import_syntax_error;
jbe@122 306 pos += endptr - (str+pos);
jbe@122 307 lua_pushnumber(L, numval);
jbe@122 308 } else if (!strncmp(str+pos, "true", 4)) {
jbe@136 309 // consume 4 input characters for "true":
jbe@121 310 pos += 4;
jbe@136 311 // put Lua true value on stack:
jbe@136 312 lua_pushboolean(L, 1);
jbe@121 313 } else if (!strncmp(str+pos, "false", 5)) {
jbe@136 314 // consume 5 input characters for "false":
jbe@121 315 pos += 5;
jbe@136 316 // put Lua false value on stack:
jbe@136 317 lua_pushboolean(L, 0);
jbe@121 318 } else if (!strncmp(str+pos, "null", 4)) {
jbe@136 319 // consume 4 input characters for "null":
jbe@136 320 pos += 4;
jbe@136 321 // put special null-marker on stack:
jbe@138 322 lua_pushvalue(L, json_import_nullmark_idx);
jbe@121 323 } else {
jbe@136 324 // all other cases are a syntax error:
jbe@121 325 goto json_import_syntax_error;
jbe@121 326 }
jbe@136 327 // process a decoded value or key value pair (expected on top of Lua stack):
jbe@136 328 json_import_process_value:
jbe@121 329 switch (mode) {
jbe@136 330 // an object key has been read:
jbe@124 331 case JSON_STATE_OBJECT_KEY:
jbe@136 332 // if an object key is not a string, then this is a syntax error:
jbe@121 333 if (lua_type(L, -1) != LUA_TSTRING) goto json_import_syntax_error;
jbe@136 334 // expect key terminator and continue with loop:
jbe@124 335 mode = JSON_STATE_OBJECT_KEY_TERMINATOR;
jbe@121 336 goto json_import_loop;
jbe@136 337 // a key value pair has been read:
jbe@124 338 case JSON_STATE_OBJECT_VALUE:
jbe@136 339 // store key value pair in outer shadow table:
jbe@130 340 lua_rawset(L, -3);
jbe@136 341 // expect value terminator (or end of object) and continue with loop:
jbe@124 342 mode = JSON_STATE_OBJECT_SEPARATOR;
jbe@121 343 goto json_import_loop;
jbe@136 344 // an array value has been read:
jbe@124 345 case JSON_STATE_ARRAY_VALUE:
jbe@136 346 // store value in outer shadow table:
jbe@140 347 lua_rawseti(L, -3, lua_rawlen(L, -3) + 1);
jbe@136 348 // expect value terminator (or end of object) and continue with loop:
jbe@124 349 mode = JSON_STATE_ARRAY_SEPARATOR;
jbe@121 350 goto json_import_loop;
jbe@136 351 // a single value has been read:
jbe@124 352 case JSON_STATE_VALUE:
jbe@136 353 // leave value on top of stack, expect end of JSON document, and continue with loop:
jbe@124 354 mode = JSON_STATE_END;
jbe@121 355 goto json_import_loop;
jbe@121 356 }
jbe@136 357 // syntax error handling (only reachable by goto statement):
jbe@136 358 json_import_syntax_error:
jbe@121 359 lua_pushnil(L);
jbe@121 360 lua_pushliteral(L, "Syntax error in JSON document");
jbe@121 361 return 2;
jbe@121 362 }
jbe@121 363
jbe@138 364 #define json_path_shadowtbl_idx 1
jbe@138 365 #define json_path_nullmark_idx 2
jbe@138 366 #define json_path_idxshift 2
jbe@138 367
jbe@137 368 // gets a value or its type from a JSON document (first argument)
jbe@137 369 // optionally using a path (variable number of keys after first argument):
jbe@137 370 static int json_path(lua_State *L, int type_mode) {
jbe@138 371 int stacktop;
jbe@138 372 int idx = 2 + json_path_idxshift;
jbe@138 373 // insert json_shadowtbl on stack at position 1:
jbe@138 374 json_regfetch(L, json_shadowtbl);
jbe@138 375 lua_insert(L, 1);
jbe@138 376 // insert json_nullmark on stack at position 2:
jbe@138 377 json_regfetch(L, json_nullmark);
jbe@138 378 lua_insert(L, 2);
jbe@137 379 // store number of arguments:
jbe@138 380 stacktop = lua_gettop(L);
jbe@137 381 // follow path, starting with first argument as "current value":
jbe@138 382 lua_pushvalue(L, 1 + json_path_idxshift);
jbe@137 383 // process each "path key":
jbe@138 384 while (idx <= stacktop) {
jbe@137 385 // if "current value" is nil, then the path cannot be walked and nil is returned:
jbe@137 386 if (lua_isnil(L, -1)) return 1;
jbe@137 387 // try to get shadow table of "current value":
jbe@130 388 lua_pushvalue(L, -1);
jbe@138 389 lua_rawget(L, json_path_shadowtbl_idx);
jbe@126 390 if (lua_isnil(L, -1)) {
jbe@137 391 // if no shadow table is found,
jbe@137 392 // drop nil from stack:
jbe@126 393 lua_pop(L, 1);
jbe@130 394 if (lua_type(L, -1) == LUA_TTABLE) {
jbe@137 395 // if "current value" is a table,
jbe@137 396 // get "next value" using the "path key":
jbe@130 397 lua_pushvalue(L, idx++);
jbe@130 398 lua_gettable(L, -2);
jbe@130 399 } else {
jbe@137 400 // if "current value" is not a table,
jbe@137 401 // then the path cannot be walked and nil is returned:
jbe@130 402 lua_pushnil(L);
jbe@137 403 return 1;
jbe@130 404 }
jbe@130 405 } else {
jbe@137 406 // if a shadow table is found,
jbe@137 407 // set "current value" to its shadow table:
jbe@130 408 lua_replace(L, -2);
jbe@137 409 // get "next value" using the "path key":
jbe@130 410 lua_pushvalue(L, idx++);
jbe@130 411 lua_rawget(L, -2);
jbe@126 412 }
jbe@137 413 // the "next value" replaces the "current value":
jbe@130 414 lua_replace(L, -2);
jbe@126 415 }
jbe@137 416 if (!type_mode) {
jbe@137 417 // if a value (and not its type) was requested,
jbe@137 418 // check if value is the null-marker, and store nil on top of Lua stack in that case:
jbe@138 419 if (lua_rawequal(L, -1, json_path_nullmark_idx)) lua_pushnil(L);
jbe@137 420 } else {
jbe@137 421 // if the type was requested,
jbe@137 422 // check if value is the null-marker:
jbe@138 423 if (lua_rawequal(L, -1, json_path_nullmark_idx)) {
jbe@137 424 // if yes, store string "null" on top of Lua stack:
jbe@130 425 lua_pushliteral(L, "null");
jbe@137 426 } else {
jbe@137 427 // otherwise,
jbe@138 428 // check if metatable indicates "object" or "array":
jbe@138 429 if (lua_getmetatable(L, -1)) {
jbe@138 430 json_regfetch(L, json_objectmt);
jbe@138 431 if (lua_rawequal(L, -2, -1)) {
jbe@138 432 // return string "object":
jbe@138 433 lua_pushliteral(L, "object");
jbe@138 434 return 1;
jbe@138 435 }
jbe@138 436 json_regfetch(L, json_arraymt);
jbe@138 437 if (lua_rawequal(L, -3, -1)) {
jbe@138 438 // return string "array":
jbe@138 439 lua_pushliteral(L, "array");
jbe@138 440 return 1;
jbe@138 441 }
jbe@138 442 lua_pop(L, 3);
jbe@138 443 }
jbe@138 444 // otherwise, get the Lua type:
jbe@138 445 lua_pushstring(L, lua_typename(L, lua_type(L, -1)));
jbe@126 446 }
jbe@126 447 }
jbe@137 448 // return the top most value on the Lua stack:
jbe@137 449 return 1;
jbe@130 450 }
jbe@130 451
jbe@137 452 // gets a value from a JSON document (first argument)
jbe@137 453 // optionally using a path (variable number of keys after first argument):
jbe@130 454 static int json_get(lua_State *L) {
jbe@137 455 return json_path(L, 0);
jbe@130 456 }
jbe@130 457
jbe@137 458 // gets a value's type from a JSON document (first argument)
jbe@137 459 // optionally using a path (variable number of keys after first argument):
jbe@130 460 static int json_type(lua_State *L) {
jbe@137 461 return json_path(L, 1);
jbe@130 462 }
jbe@130 463
jbe@137 464 // checks if a value in a JSON document (first argument) is null:
jbe@130 465 static int json_isnull(lua_State *L) {
jbe@137 466 const char *jsontype;
jbe@138 467 lua_pushcfunction(L, json_type);
jbe@137 468 lua_insert(L, 1);
jbe@137 469 lua_call(L, lua_gettop(L) - 1, 1);
jbe@137 470 jsontype = lua_tostring(L, -1);
jbe@137 471 if (jsontype && !strcmp(jsontype, "null")) lua_pushboolean(L, 1);
jbe@137 472 else lua_pushboolean(L, 0);
jbe@137 473 return 1;
jbe@130 474 }
jbe@130 475
jbe@138 476 #define json_setnull_unknownmt_idx 3
jbe@138 477 #define json_setnull_objectmt_idx 4
jbe@138 478 #define json_setnull_arraymt_idx 5
jbe@138 479 #define json_setnull_shadowtbl_idx 6
jbe@138 480
jbe@131 481 static int json_setnull(lua_State *L) {
jbe@138 482 // truncate stack to two elements:
jbe@131 483 lua_settop(L, 2);
jbe@138 484 // push json_unknownmt to stack position 3:
jbe@138 485 json_regfetch(L, json_unknownmt);
jbe@138 486 // push json_objectmt to stack position 4:
jbe@138 487 json_regfetch(L, json_objectmt);
jbe@138 488 // push json_arraymt to stack position 5:
jbe@138 489 json_regfetch(L, json_arraymt);
jbe@138 490 // push json_shadowtbl to stack position 6:
jbe@138 491 json_regfetch(L, json_shadowtbl);
jbe@138 492 //
jbe@138 493 lua_getmetatable(L, 1);
jbe@138 494 if (
jbe@138 495 !lua_rawequal(L, -1, json_setnull_unknownmt_idx) &&
jbe@138 496 !lua_rawequal(L, -1, json_setnull_objectmt_idx) &&
jbe@138 497 !lua_rawequal(L, -1, json_setnull_arraymt_idx)
jbe@138 498 ) {
jbe@138 499 lua_pushvalue(L, json_setnull_unknownmt_idx);
jbe@138 500 lua_setmetatable(L, 1);
jbe@138 501 }
jbe@131 502 lua_pushvalue(L, 1);
jbe@138 503 lua_rawget(L, json_setnull_shadowtbl_idx);
jbe@131 504 if (lua_isnil(L, -1)) {
jbe@131 505 lua_newtable(L);
jbe@131 506 lua_pushvalue(L, 1);
jbe@131 507 lua_pushvalue(L, -2);
jbe@138 508 lua_rawset(L, json_setnull_shadowtbl_idx);
jbe@131 509 }
jbe@131 510 lua_pushvalue(L, 2);
jbe@138 511 json_regfetch(L, json_nullmark);
jbe@131 512 lua_rawset(L, -3);
jbe@131 513 return 0;
jbe@131 514 }
jbe@131 515
jbe@130 516 static int json_len(lua_State *L) {
jbe@130 517 lua_settop(L, 1);
jbe@138 518 json_regfetch(L, json_shadowtbl);
jbe@130 519 lua_pushvalue(L, 1);
jbe@138 520 lua_rawget(L, -2);
jbe@138 521 lua_pushinteger(L, lua_rawlen(L, lua_isnil(L, -1) ? 1 : -1));
jbe@123 522 return 1;
jbe@123 523 }
jbe@123 524
jbe@141 525 #define json_index_nullmark_idx 3
jbe@141 526 #define json_index_shadowtbl_idx 4
jbe@141 527
jbe@130 528 static int json_index(lua_State *L) {
jbe@130 529 lua_settop(L, 2);
jbe@141 530 json_regfetch(L, json_nullmark); // on stack position 3
jbe@139 531 json_regfetch(L, json_shadowtbl);
jbe@130 532 lua_pushvalue(L, 1);
jbe@141 533 lua_rawget(L, json_index_shadowtbl_idx);
jbe@139 534 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
jbe@130 535 lua_pushvalue(L, 2);
jbe@130 536 lua_rawget(L, -2);
jbe@141 537 if (lua_rawequal(L, -1, json_index_nullmark_idx)) lua_pushnil(L);
jbe@127 538 return 1;
jbe@127 539 }
jbe@127 540
jbe@130 541 static int json_newindex(lua_State *L) {
jbe@130 542 lua_settop(L, 3);
jbe@139 543 json_regfetch(L, json_shadowtbl);
jbe@123 544 lua_pushvalue(L, 1);
jbe@139 545 lua_rawget(L, -1);
jbe@130 546 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
jbe@130 547 lua_replace(L, 1);
jbe@139 548 lua_settop(L, 3);
jbe@130 549 lua_rawset(L, 1);
jbe@121 550 return 1;
jbe@121 551 }
jbe@121 552
jbe@139 553 #define json_pairs_iterfunc_nullmark_idx 3
jbe@139 554 #define json_pairs_iterfunc_shadowtbl_idx 4
jbe@139 555
jbe@135 556 static int json_pairs_iterfunc(lua_State *L) {
jbe@135 557 lua_settop(L, 2);
jbe@139 558 json_regfetch(L, json_nullmark); // on stack position 3
jbe@139 559 json_regfetch(L, json_shadowtbl);
jbe@135 560 lua_pushvalue(L, 1);
jbe@139 561 lua_rawget(L, json_pairs_iterfunc_shadowtbl_idx);
jbe@135 562 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
jbe@135 563 lua_pushvalue(L, 2);
jbe@135 564 if (!lua_next(L, -2)) return 0;
jbe@139 565 if (lua_rawequal(L, -1, json_pairs_iterfunc_nullmark_idx)) {
jbe@135 566 lua_pop(L, 1);
jbe@135 567 lua_pushnil(L);
jbe@135 568 }
jbe@135 569 return 2;
jbe@135 570 }
jbe@135 571
jbe@135 572 static int json_pairs(lua_State *L) {
jbe@139 573 lua_pushcfunction(L, json_pairs_iterfunc);
jbe@135 574 lua_pushvalue(L, 1);
jbe@135 575 lua_pushnil(L);
jbe@135 576 return 3;
jbe@135 577 }
jbe@135 578
jbe@139 579 #define json_ipairs_iterfunc_nullmark_idx 3
jbe@139 580 #define json_ipairs_iterfunc_shadowtbl_idx 4
jbe@139 581
jbe@134 582 static int json_ipairs_iterfunc(lua_State *L) {
jbe@134 583 int idx;
jbe@134 584 lua_settop(L, 2);
jbe@139 585 json_regfetch(L, json_nullmark); // on stack position 3
jbe@139 586 json_regfetch(L, json_shadowtbl);
jbe@134 587 idx = lua_tointeger(L, 2) + 1;
jbe@134 588 lua_pushvalue(L, 1);
jbe@139 589 lua_rawget(L, json_ipairs_iterfunc_shadowtbl_idx);
jbe@134 590 if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found");
jbe@134 591 lua_rawgeti(L, -1, idx);
jbe@134 592 if (lua_isnil(L, -1)) return 0;
jbe@134 593 lua_pushinteger(L, idx);
jbe@139 594 if (lua_rawequal(L, -2, json_ipairs_iterfunc_nullmark_idx)) lua_pushnil(L);
jbe@134 595 else lua_pushvalue(L, -2);
jbe@134 596 return 2;
jbe@134 597 }
jbe@134 598
jbe@134 599 static int json_ipairs(lua_State *L) {
jbe@139 600 lua_pushcfunction(L, json_ipairs_iterfunc);
jbe@134 601 lua_pushvalue(L, 1);
jbe@134 602 lua_pushinteger(L, 0);
jbe@134 603 return 3;
jbe@134 604 }
jbe@134 605
jbe@121 606 static const struct luaL_Reg json_module_functions[] = {
jbe@133 607 {"object", json_object},
jbe@133 608 {"array", json_array},
jbe@121 609 {"import", json_import},
jbe@130 610 {"get", json_get},
jbe@127 611 {"type", json_type},
jbe@123 612 {"isnull", json_isnull},
jbe@131 613 {"setnull", json_setnull},
jbe@121 614 {NULL, NULL}
jbe@121 615 };
jbe@121 616
jbe@126 617 static const struct luaL_Reg json_metatable_functions[] = {
jbe@130 618 {"__len", json_len},
jbe@130 619 {"__index", json_index},
jbe@130 620 {"__newindex", json_newindex},
jbe@135 621 {"__pairs", json_pairs},
jbe@134 622 {"__ipairs", json_ipairs},
jbe@126 623 {NULL, NULL}
jbe@126 624 };
jbe@126 625
jbe@121 626 int luaopen_json(lua_State *L) {
jbe@126 627 lua_settop(L, 0);
jbe@138 628 lua_newtable(L); // library
jbe@138 629 lua_newtable(L);
jbe@138 630 luaL_setfuncs(L, json_metatable_functions, 0);
jbe@138 631 json_regstore(L, json_unknownmt);
jbe@138 632 lua_setfield(L, 1, "ambiguous_mt");
jbe@138 633 lua_newtable(L);
jbe@138 634 luaL_setfuncs(L, json_metatable_functions, 0);
jbe@138 635 json_regstore(L, json_objectmt);
jbe@138 636 lua_setfield(L, 1, "object_mt");
jbe@138 637 lua_newtable(L);
jbe@138 638 luaL_setfuncs(L, json_metatable_functions, 0);
jbe@138 639 json_regstore(L, json_arraymt);
jbe@138 640 lua_setfield(L, 1, "array_mt");
jbe@138 641 lua_newtable(L); // ephemeron table to store shadow tables for each JSON object/array to allow NULL values returned as nil
jbe@138 642 lua_newtable(L); // metatable for ephemeron table
jbe@121 643 lua_pushliteral(L, "__mode");
jbe@121 644 lua_pushliteral(L, "k");
jbe@138 645 lua_rawset(L, -3);
jbe@138 646 lua_setmetatable(L, -2);
jbe@138 647 json_regstore(L, json_shadowtbl);
jbe@138 648 lua_newtable(L);
jbe@138 649 json_regstore(L, json_nullmark);
jbe@138 650 lua_settop(L, 1);
jbe@138 651 luaL_setfuncs(L, json_module_functions, 0);
jbe@121 652 return 1;
jbe@121 653 }

Impressum / About Us