MCPcopy Create free account
hub / github.com/defold/defold / lua_cjson_new

Function lua_cjson_new

engine/script/src/luacjson/lua_cjson.c:1734–1811  ·  view source on GitHub ↗

Return cjson module table */

Source from the content-addressed store, hash-verified

1732
1733/* Return cjson module table */
1734static int lua_cjson_new(lua_State *l)
1735{
1736 luaL_Reg reg[] = {
1737 { "encode", json_encode },
1738 { "decode", json_decode },
1739 { "encode_empty_table_as_object", json_cfg_encode_empty_table_as_object },
1740 { "decode_array_with_array_mt", json_cfg_decode_array_with_array_mt },
1741 { "encode_sparse_array", json_cfg_encode_sparse_array },
1742 { "encode_max_depth", json_cfg_encode_max_depth },
1743 { "decode_max_depth", json_cfg_decode_max_depth },
1744 { "encode_number_precision", json_cfg_encode_number_precision },
1745 { "encode_keep_buffer", json_cfg_encode_keep_buffer },
1746 { "encode_invalid_numbers", json_cfg_encode_invalid_numbers },
1747 { "decode_invalid_numbers", json_cfg_decode_invalid_numbers },
1748 { "encode_escape_forward_slash", json_cfg_encode_escape_forward_slash },
1749 { "new", lua_cjson_new },
1750 { NULL, NULL }
1751 };
1752
1753 /* Initialise number conversions */
1754 fpconv_init();
1755
1756 /* Test if array metatables are in registry */
1757 lua_pushlightuserdata(l, json_lightudata_mask(&json_empty_array));
1758 lua_rawget(l, LUA_REGISTRYINDEX);
1759 if (lua_isnil(l, -1)) {
1760 /* Create array metatables.
1761 *
1762 * If multiple calls to lua_cjson_new() are made,
1763 * this prevents overriding the tables at the given
1764 * registry's index with a new one.
1765 */
1766 lua_pop(l, 1);
1767
1768 /* empty_array_mt */
1769 lua_pushlightuserdata(l, json_lightudata_mask(&json_empty_array));
1770 lua_newtable(l);
1771 lua_rawset(l, LUA_REGISTRYINDEX);
1772
1773 /* array_mt */
1774 lua_pushlightuserdata(l, json_lightudata_mask(&json_array));
1775 lua_newtable(l);
1776 lua_rawset(l, LUA_REGISTRYINDEX);
1777 }
1778
1779 /* cjson module table */
1780 lua_newtable(l);
1781
1782 /* Register functions with config data as upvalue */
1783 json_create_config(l);
1784 compat_luaL_setfuncs(l, reg, 1);
1785
1786 /* Set cjson.null */
1787 lua_pushlightuserdata(l, NULL);
1788 lua_setfield(l, -2, "null");
1789
1790 /* Set cjson.empty_array_mt */
1791 lua_pushlightuserdata(l, json_lightudata_mask(&json_empty_array));

Callers 2

lua_cjson_safe_newFunction · 0.85
luaopen_cjsonFunction · 0.85

Calls 7

lua_pushlightuserdataFunction · 0.85
lua_rawgetFunction · 0.85
lua_rawsetFunction · 0.85
json_create_configFunction · 0.85
compat_luaL_setfuncsFunction · 0.85
lua_setfieldFunction · 0.85
fpconv_initFunction · 0.70

Tested by

no test coverage detected