Skip to content

Commit f88d2b4

Browse files
spacewanderagentzh
authored andcommitted
feature: ported to the ARM64 platform by masking off the bits higher than 47-bit in the lightud.
Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
1 parent dca43df commit f88d2b4

1 file changed

Lines changed: 14 additions & 9 deletions

File tree

lua_cjson.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
*/
3838

3939
#include <assert.h>
40+
#include <stdint.h>
4041
#include <string.h>
4142
#include <math.h>
4243
#include <limits.h>
@@ -92,6 +93,9 @@
9293
#define strcasecmp _stricmp
9394
#endif
9495

96+
#define json_lightudata_mask(ludata) \
97+
((void *) ((uintptr_t) (ludata) & ((1UL << 47) - 1)))
98+
9599
static const char * const *json_empty_array;
96100
static const char * const *json_array;
97101

@@ -733,7 +737,7 @@ static void json_append_data(lua_State *l, json_config_t *cfg,
733737
has_metatable = lua_getmetatable(l, -1);
734738

735739
if (has_metatable) {
736-
lua_pushlightuserdata(l, &json_array);
740+
lua_pushlightuserdata(l, json_lightudata_mask(&json_array));
737741
lua_rawget(l, LUA_REGISTRYINDEX);
738742
as_array = lua_rawequal(l, -1, -2);
739743
lua_pop(l, 2);
@@ -750,7 +754,8 @@ static void json_append_data(lua_State *l, json_config_t *cfg,
750754
} else {
751755
if (has_metatable) {
752756
lua_getmetatable(l, -1);
753-
lua_pushlightuserdata(l, &json_empty_array);
757+
lua_pushlightuserdata(l, json_lightudata_mask(
758+
&json_empty_array));
754759
lua_rawget(l, LUA_REGISTRYINDEX);
755760
as_array = lua_rawequal(l, -1, -2);
756761
lua_pop(l, 2); /* pop pointer + metatable */
@@ -1277,7 +1282,7 @@ static void json_parse_array_context(lua_State *l, json_parse_t *json)
12771282

12781283
/* set array_mt on the table at the top of the stack */
12791284
if (json->cfg->decode_array_with_array_mt) {
1280-
lua_pushlightuserdata(l, &json_array);
1285+
lua_pushlightuserdata(l, json_lightudata_mask(&json_array));
12811286
lua_rawget(l, LUA_REGISTRYINDEX);
12821287
lua_setmetatable(l, -2);
12831288
}
@@ -1455,7 +1460,7 @@ static int lua_cjson_new(lua_State *l)
14551460
fpconv_init();
14561461

14571462
/* Test if array metatables are in registry */
1458-
lua_pushlightuserdata(l, &json_empty_array);
1463+
lua_pushlightuserdata(l, json_lightudata_mask(&json_empty_array));
14591464
lua_rawget(l, LUA_REGISTRYINDEX);
14601465
if (lua_isnil(l, -1)) {
14611466
/* Create array metatables.
@@ -1467,12 +1472,12 @@ static int lua_cjson_new(lua_State *l)
14671472
lua_pop(l, 1);
14681473

14691474
/* empty_array_mt */
1470-
lua_pushlightuserdata(l, &json_empty_array);
1475+
lua_pushlightuserdata(l, json_lightudata_mask(&json_empty_array));
14711476
lua_newtable(l);
14721477
lua_rawset(l, LUA_REGISTRYINDEX);
14731478

14741479
/* array_mt */
1475-
lua_pushlightuserdata(l, &json_array);
1480+
lua_pushlightuserdata(l, json_lightudata_mask(&json_array));
14761481
lua_newtable(l);
14771482
lua_rawset(l, LUA_REGISTRYINDEX);
14781483
}
@@ -1489,17 +1494,17 @@ static int lua_cjson_new(lua_State *l)
14891494
lua_setfield(l, -2, "null");
14901495

14911496
/* Set cjson.empty_array_mt */
1492-
lua_pushlightuserdata(l, &json_empty_array);
1497+
lua_pushlightuserdata(l, json_lightudata_mask(&json_empty_array));
14931498
lua_rawget(l, LUA_REGISTRYINDEX);
14941499
lua_setfield(l, -2, "empty_array_mt");
14951500

14961501
/* Set cjson.array_mt */
1497-
lua_pushlightuserdata(l, &json_array);
1502+
lua_pushlightuserdata(l, json_lightudata_mask(&json_array));
14981503
lua_rawget(l, LUA_REGISTRYINDEX);
14991504
lua_setfield(l, -2, "array_mt");
15001505

15011506
/* Set cjson.empty_array */
1502-
lua_pushlightuserdata(l, &json_array);
1507+
lua_pushlightuserdata(l, json_lightudata_mask(&json_array));
15031508
lua_setfield(l, -2, "empty_array");
15041509

15051510
/* Set module name / version fields */

0 commit comments

Comments
 (0)