aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lua_cjson.c17
-rw-r--r--tests/agentzh.t28
2 files changed, 40 insertions, 5 deletions
diff --git a/lua_cjson.c b/lua_cjson.c
index 3a77644..1695979 100644
--- a/lua_cjson.c
+++ b/lua_cjson.c
@@ -1401,10 +1401,21 @@ static int lua_cjson_new(lua_State *l)
1401 /* Initialise number conversions */ 1401 /* Initialise number conversions */
1402 fpconv_init(); 1402 fpconv_init();
1403 1403
1404 /* Create empty array metatable */ 1404 /* Test if empty array metatable is in registry */
1405 lua_pushlightuserdata(l, &json_empty_array); 1405 lua_pushlightuserdata(l, &json_empty_array);
1406 lua_newtable(l); 1406 lua_rawget(l, LUA_REGISTRYINDEX);
1407 lua_rawset(l, LUA_REGISTRYINDEX); 1407 if (lua_isnil(l, -1)) {
1408 /* Create empty array metatable.
1409 *
1410 * If multiple calls to lua_cjson_new() are made,
1411 * this prevents overriding the table at the given
1412 * registry's index with a new one.
1413 */
1414 lua_pop(l, 1);
1415 lua_pushlightuserdata(l, &json_empty_array);
1416 lua_newtable(l);
1417 lua_rawset(l, LUA_REGISTRYINDEX);
1418 }
1408 1419
1409 /* cjson module table */ 1420 /* cjson module table */
1410 lua_newtable(l); 1421 lua_newtable(l);
diff --git a/tests/agentzh.t b/tests/agentzh.t
index e76f910..3b0ecf1 100644
--- a/tests/agentzh.t
+++ b/tests/agentzh.t
@@ -113,7 +113,31 @@ print(cjson.encode(data))
113 113
114 114
115 115
116=== TEST 9: & in JSON 116=== TEST 9: multiple calls to lua_cjson_new (1/2)
117--- lua
118local cjson = require "cjson"
119package.loaded["cjson"] = nil
120require "cjson"
121local arr = setmetatable({}, cjson.empty_array_mt)
122print(cjson.encode(arr))
123--- out
124[]
125
126
127
128=== TEST 10: multiple calls to lua_cjson_new (2/2)
129--- lua
130local cjson = require "cjson.safe"
131-- load another cjson instance (not in package.loaded)
132require "cjson"
133local arr = setmetatable({}, cjson.empty_array_mt)
134print(cjson.encode(arr))
135--- out
136[]
137
138
139
140=== TEST 11: & in JSON
117--- lua 141--- lua
118local cjson = require "cjson" 142local cjson = require "cjson"
119local a="[\"a=1&b=2\"]" 143local a="[\"a=1&b=2\"]"
@@ -124,7 +148,7 @@ print(cjson.encode(b))
124 148
125 149
126 150
127=== TEST 10: default and max precision 151=== TEST 12: default and max precision
128--- lua 152--- lua
129local math = require "math" 153local math = require "math"
130local cjson = require "cjson" 154local cjson = require "cjson"