webmcp

view libraries/json/json.c @ 142:a686ed2ce967

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

Impressum / About Us