From 754932cef433ad89721c997ff5ff954c51f7b722 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Sat, 9 Aug 2025 18:07:43 +0800 Subject: [PATCH] feature: add bson_to_lightuserdata api. see #2074 (#2075) --- lualib-src/lua-bson.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index 29d6c70c..4abf4824 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -327,6 +327,7 @@ append_one(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth) case LUA_TNUMBER: append_number(bs, L, key, sz); break; + case LUA_TLIGHTUSERDATA: case LUA_TUSERDATA: { append_key(bs, L, BSON_DOCUMENT, key, sz); int32_t * doc = (int32_t*)lua_touserdata(L,-1); @@ -978,6 +979,13 @@ lencode(lua_State *L) { return 1; } +static int +lto_lightuserdata(lua_State *L) { + void *p = lua_touserdata(L, 1); + lua_pushlightuserdata(L, p); + return 1; +} + static int encode_bson_byorder(lua_State *L) { int n = lua_gettop(L); @@ -1340,6 +1348,7 @@ luaopen_bson(lua_State *L) { { "objectid", lobjectid }, { "int64", lint64 }, { "decode", ldecode }, + { "to_lightuserdata", lto_lightuserdata }, { NULL, NULL }, };