Lines Matching refs:l
194 static json_config_t *json_fetch_config(lua_State *l) in json_fetch_config() argument
198 cfg = lua_touserdata(l, lua_upvalueindex(1)); in json_fetch_config()
200 luaL_error(l, "BUG: Unable to fetch CJSON configuration"); in json_fetch_config()
208 static json_config_t *json_arg_init(lua_State *l, int args) in json_arg_init() argument
210 luaL_argcheck(l, lua_gettop(l) <= args, args + 1, in json_arg_init()
213 while (lua_gettop(l) < args) in json_arg_init()
214 lua_pushnil(l); in json_arg_init()
216 return json_fetch_config(l); in json_arg_init()
220 static int json_integer_option(lua_State *l, int optindex, int *setting, in json_integer_option() argument
226 if (!lua_isnil(l, optindex)) { in json_integer_option()
227 value = luaL_checkinteger(l, optindex); in json_integer_option()
229 luaL_argcheck(l, min <= value && value <= max, 1, errmsg); in json_integer_option()
233 lua_pushinteger(l, *setting); in json_integer_option()
239 static int json_enum_option(lua_State *l, int optindex, int *setting, in json_enum_option() argument
249 if (!lua_isnil(l, optindex)) { in json_enum_option()
250 if (bool_true && lua_isboolean(l, optindex)) in json_enum_option()
251 *setting = lua_toboolean(l, optindex) * bool_true; in json_enum_option()
253 *setting = luaL_checkoption(l, optindex, NULL, options); in json_enum_option()
257 lua_pushboolean(l, *setting); in json_enum_option()
259 lua_pushstring(l, options[*setting]); in json_enum_option()
268 static int json_cfg_encode_sparse_array(lua_State *l) in json_cfg_encode_sparse_array() argument
270 json_config_t *cfg = json_arg_init(l, 3); in json_cfg_encode_sparse_array()
272 json_enum_option(l, 1, &cfg->encode_sparse_convert, NULL, 1); in json_cfg_encode_sparse_array()
273 json_integer_option(l, 2, &cfg->encode_sparse_ratio, 0, INT_MAX); in json_cfg_encode_sparse_array()
274 json_integer_option(l, 3, &cfg->encode_sparse_safe, 0, INT_MAX); in json_cfg_encode_sparse_array()
281 static int json_cfg_encode_max_depth(lua_State *l) in json_cfg_encode_max_depth() argument
283 json_config_t *cfg = json_arg_init(l, 1); in json_cfg_encode_max_depth()
285 return json_integer_option(l, 1, &cfg->encode_max_depth, 1, INT_MAX); in json_cfg_encode_max_depth()
290 static int json_cfg_decode_max_depth(lua_State *l) in json_cfg_decode_max_depth() argument
292 json_config_t *cfg = json_arg_init(l, 1); in json_cfg_decode_max_depth()
294 return json_integer_option(l, 1, &cfg->decode_max_depth, 1, INT_MAX); in json_cfg_decode_max_depth()
298 static int json_cfg_encode_number_precision(lua_State *l) in json_cfg_encode_number_precision() argument
300 json_config_t *cfg = json_arg_init(l, 1); in json_cfg_encode_number_precision()
302 return json_integer_option(l, 1, &cfg->encode_number_precision, 1, 14); in json_cfg_encode_number_precision()
306 static int json_cfg_encode_keep_buffer(lua_State *l) in json_cfg_encode_keep_buffer() argument
308 json_config_t *cfg = json_arg_init(l, 1); in json_cfg_encode_keep_buffer()
313 json_enum_option(l, 1, &cfg->encode_keep_buffer, NULL, 1); in json_cfg_encode_keep_buffer()
327 void json_verify_invalid_number_setting(lua_State *l, int *setting) in json_verify_invalid_number_setting() argument
331 luaL_error(l, "Infinity, NaN, and/or hexadecimal numbers are not supported."); in json_verify_invalid_number_setting()
335 #define json_verify_invalid_number_setting(l, s) do { } while(0) argument
338 static int json_cfg_encode_invalid_numbers(lua_State *l) in json_cfg_encode_invalid_numbers() argument
341 json_config_t *cfg = json_arg_init(l, 1); in json_cfg_encode_invalid_numbers()
343 json_enum_option(l, 1, &cfg->encode_invalid_numbers, options, 1); in json_cfg_encode_invalid_numbers()
345 json_verify_invalid_number_setting(l, &cfg->encode_invalid_numbers); in json_cfg_encode_invalid_numbers()
350 static int json_cfg_decode_invalid_numbers(lua_State *l) in json_cfg_decode_invalid_numbers() argument
352 json_config_t *cfg = json_arg_init(l, 1); in json_cfg_decode_invalid_numbers()
354 json_enum_option(l, 1, &cfg->decode_invalid_numbers, NULL, 1); in json_cfg_decode_invalid_numbers()
356 json_verify_invalid_number_setting(l, &cfg->encode_invalid_numbers); in json_cfg_decode_invalid_numbers()
361 static int json_destroy_config(lua_State *l) in json_destroy_config() argument
365 cfg = lua_touserdata(l, 1); in json_destroy_config()
373 static void json_create_config(lua_State *l) in json_create_config() argument
378 cfg = lua_newuserdata(l, sizeof(*cfg)); in json_create_config()
381 lua_newtable(l); in json_create_config()
382 lua_pushcfunction(l, json_destroy_config); in json_create_config()
383 lua_setfield(l, -2, "__gc"); in json_create_config()
384 lua_setmetatable(l, -2); in json_create_config()
448 static void json_encode_exception(lua_State *l, json_config_t *cfg, strbuf_t *json, int lindex, in json_encode_exception() argument
453 luaL_error(l, "Cannot serialise %s: %s", in json_encode_exception()
454 lua_typename(l, lua_type(l, lindex)), reason); in json_encode_exception()
463 static void json_append_string(lua_State *l, strbuf_t *json, int lindex) in json_append_string() argument
470 str = lua_tolstring(l, lindex, &len); in json_append_string()
493 static int lua_array_length(lua_State *l, json_config_t *cfg, strbuf_t *json) in lua_array_length() argument
502 lua_pushnil(l); in lua_array_length()
504 while (lua_next(l, -2) != 0) { in lua_array_length()
506 if (lua_type(l, -2) == LUA_TNUMBER && in lua_array_length()
507 (k = lua_tonumber(l, -2))) { in lua_array_length()
513 lua_pop(l, 1); in lua_array_length()
519 lua_pop(l, 2); in lua_array_length()
528 json_encode_exception(l, cfg, json, -1, "excessively sparse array"); in lua_array_length()
536 static void json_check_encode_depth(lua_State *l, json_config_t *cfg, in json_check_encode_depth() argument
549 if (current_depth <= cfg->encode_max_depth && lua_checkstack(l, 3)) in json_check_encode_depth()
555 luaL_error(l, "Cannot serialise, excessive nesting (%d)", in json_check_encode_depth()
559 static void json_append_data(lua_State *l, json_config_t *cfg,
566 static void json_append_array(lua_State *l, json_config_t *cfg, int current_depth, in json_append_array() argument
580 lua_rawgeti(l, -1, i); in json_append_array()
581 json_append_data(l, cfg, current_depth, json); in json_append_array()
582 lua_pop(l, 1); in json_append_array()
588 static void json_append_number(lua_State *l, json_config_t *cfg, in json_append_number() argument
591 double num = lua_tonumber(l, lindex); in json_append_number()
597 json_encode_exception(l, cfg, json, lindex, "must not be NaN or Inf"); in json_append_number()
618 static void json_append_object(lua_State *l, json_config_t *cfg, in json_append_object() argument
626 lua_pushnil(l); in json_append_object()
629 while (lua_next(l, -2) != 0) { in json_append_object()
636 keytype = lua_type(l, -2); in json_append_object()
639 json_append_number(l, cfg, json, -2); in json_append_object()
642 json_append_string(l, json, -2); in json_append_object()
645 json_encode_exception(l, cfg, json, -2, in json_append_object()
651 json_append_data(l, cfg, current_depth, json); in json_append_object()
652 lua_pop(l, 1); in json_append_object()
660 static void json_append_data(lua_State *l, json_config_t *cfg, in json_append_data() argument
665 switch (lua_type(l, -1)) { in json_append_data()
667 json_append_string(l, json, -1); in json_append_data()
670 json_append_number(l, cfg, json, -1); in json_append_data()
673 if (lua_toboolean(l, -1)) in json_append_data()
680 json_check_encode_depth(l, cfg, current_depth, json); in json_append_data()
681 len = lua_array_length(l, cfg, json); in json_append_data()
683 json_append_array(l, cfg, current_depth, json, len); in json_append_data()
685 json_append_object(l, cfg, current_depth, json); in json_append_data()
691 if (lua_touserdata(l, -1) == NULL) { in json_append_data()
698 json_encode_exception(l, cfg, json, -1, "type not supported"); in json_append_data()
703 static int json_encode(lua_State *l) in json_encode() argument
705 json_config_t *cfg = json_fetch_config(l); in json_encode()
711 luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument"); in json_encode()
723 json_append_data(l, cfg, 0, encode_buf); in json_encode()
726 lua_pushlstring(l, json, len); in json_encode()
736 static void json_process_value(lua_State *l, json_parse_t *json,
1099 static void json_throw_parse_error(lua_State *l, json_parse_t *json, in json_throw_parse_error() argument
1112 luaL_error(l, "Expected %s but found %s at character %d", in json_throw_parse_error()
1121 static void json_decode_descend(lua_State *l, json_parse_t *json, int slots) in json_decode_descend() argument
1126 lua_checkstack(l, slots)) { in json_decode_descend()
1131 luaL_error(l, "Found too many nested data structures (%d) at character %d", in json_decode_descend()
1135 static void json_parse_object_context(lua_State *l, json_parse_t *json) in json_parse_object_context() argument
1141 json_decode_descend(l, json, 3); in json_parse_object_context()
1143 lua_newtable(l); in json_parse_object_context()
1155 json_throw_parse_error(l, json, "object key string", &token); in json_parse_object_context()
1158 lua_pushlstring(l, token.value.string, token.string_len); in json_parse_object_context()
1162 json_throw_parse_error(l, json, "colon", &token); in json_parse_object_context()
1166 json_process_value(l, json, &token); in json_parse_object_context()
1169 lua_rawset(l, -3); in json_parse_object_context()
1179 json_throw_parse_error(l, json, "comma or object end", &token); in json_parse_object_context()
1186 static void json_parse_array_context(lua_State *l, json_parse_t *json) in json_parse_array_context() argument
1193 json_decode_descend(l, json, 2); in json_parse_array_context()
1195 lua_newtable(l); in json_parse_array_context()
1206 json_process_value(l, json, &token); in json_parse_array_context()
1207 lua_rawseti(l, -2, i); /* arr[i] = value */ in json_parse_array_context()
1217 json_throw_parse_error(l, json, "comma or array end", &token); in json_parse_array_context()
1224 static void json_process_value(lua_State *l, json_parse_t *json, in json_process_value() argument
1229 lua_pushlstring(l, token->value.string, token->string_len); in json_process_value()
1232 lua_pushnumber(l, token->value.number); in json_process_value()
1235 lua_pushboolean(l, token->value.boolean); in json_process_value()
1238 json_parse_object_context(l, json); in json_process_value()
1241 json_parse_array_context(l, json); in json_process_value()
1246 lua_pushlightuserdata(l, NULL); in json_process_value()
1249 json_throw_parse_error(l, json, "value", token); in json_process_value()
1253 static int json_decode(lua_State *l) in json_decode() argument
1259 luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument"); in json_decode()
1261 json.cfg = json_fetch_config(l); in json_decode()
1262 json.data = luaL_checklstring(l, 1, &json_len); in json_decode()
1272 luaL_error(l, "JSON parser does not support UTF-16 or UTF-32"); in json_decode()
1280 json_process_value(l, &json, &token); in json_decode()
1286 json_throw_parse_error(l, &json, "the end", &token); in json_decode()
1300 static void luaL_setfuncs (lua_State *l, const luaL_Reg *reg, int nup) in luaL_setfuncs() argument
1304 luaL_checkstack(l, nup, "too many upvalues"); in luaL_setfuncs()
1307 lua_pushvalue(l, -nup); in luaL_setfuncs()
1308 lua_pushcclosure(l, reg->func, nup); /* closure with those upvalues */ in luaL_setfuncs()
1309 lua_setfield(l, -(nup + 2), reg->name); in luaL_setfuncs()
1311 lua_pop(l, nup); /* remove upvalues */ in luaL_setfuncs()
1318 static int json_protect_conversion(lua_State *l) in json_protect_conversion() argument
1323 luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument"); in json_protect_conversion()
1326 lua_pushvalue(l, lua_upvalueindex(1)); in json_protect_conversion()
1327 lua_insert(l, 1); in json_protect_conversion()
1328 err = lua_pcall(l, 1, 1, 0); in json_protect_conversion()
1333 lua_pushnil(l); in json_protect_conversion()
1334 lua_insert(l, -2); in json_protect_conversion()
1340 return luaL_error(l, "Memory allocation error in CJSON protected call"); in json_protect_conversion()
1344 static int lua_cjson_new(lua_State *l) in lua_cjson_new() argument
1364 lua_newtable(l); in lua_cjson_new()
1367 json_create_config(l); in lua_cjson_new()
1368 luaL_setfuncs(l, reg, 1); in lua_cjson_new()
1371 lua_pushlightuserdata(l, NULL); in lua_cjson_new()
1372 lua_setfield(l, -2, "null"); in lua_cjson_new()
1375 lua_pushliteral(l, CJSON_MODNAME); in lua_cjson_new()
1376 lua_setfield(l, -2, "_NAME"); in lua_cjson_new()
1377 lua_pushliteral(l, CJSON_VERSION); in lua_cjson_new()
1378 lua_setfield(l, -2, "_VERSION"); in lua_cjson_new()
1384 static int lua_cjson_safe_new(lua_State *l) in lua_cjson_safe_new() argument
1389 lua_cjson_new(l); in lua_cjson_safe_new()
1392 lua_pushcfunction(l, lua_cjson_safe_new); in lua_cjson_safe_new()
1393 lua_setfield(l, -2, "new"); in lua_cjson_safe_new()
1396 lua_getfield(l, -1, func[i]); in lua_cjson_safe_new()
1397 lua_pushcclosure(l, json_protect_conversion, 1); in lua_cjson_safe_new()
1398 lua_setfield(l, -2, func[i]); in lua_cjson_safe_new()
1404 int luaopen_cjson(lua_State *l) in luaopen_cjson() argument
1406 lua_cjson_new(l); in luaopen_cjson()
1410 lua_pushvalue(l, -1); in luaopen_cjson()
1411 lua_setglobal(l, CJSON_MODNAME); in luaopen_cjson()
1418 int luaopen_cjson_safe(lua_State *l) in luaopen_cjson_safe() argument
1420 lua_cjson_safe_new(l); in luaopen_cjson_safe()