From d451678436d8ee5c011486fb520b0523cdfb3d17 Mon Sep 17 00:00:00 2001 From: Paul Kulchenko Date: Sat, 5 Jun 2021 17:33:04 -0700 Subject: [PATCH] Add sqlite3 support to Lua scripts in redbean. --- tool/net/lsqlite3.c | 25 ++++++++++++++++++------- tool/net/net.mk | 3 +++ tool/net/redbean.c | 3 +++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/tool/net/lsqlite3.c b/tool/net/lsqlite3.c index 8688c6e09..fd40e4b1b 100644 --- a/tool/net/lsqlite3.c +++ b/tool/net/lsqlite3.c @@ -26,13 +26,17 @@ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ************************************************************************/ -#include -#include -#include +// attribution in binary form +asm(".ident\t\"\\n\\n\ +lsqlite3 (MIT License)\\n\ +Copyright 2002-2016 Tiago Dionizio, Doug Currie\""); +asm(".include \"libc/disclaimer.inc\""); -#define LUA_LIB -#include "lua.h" -#include "lauxlib.h" +#include "libc/calls/weirdtypes.h" +#include "libc/mem/mem.h" + +#include "third_party/lua/lua.h" +#include "third_party/lua/lauxlib.h" #if LUA_VERSION_NUM > 501 /* @@ -56,7 +60,7 @@ #endif #endif -#include "sqlite3.h" +#include "third_party/sqlite3/sqlite3.h" /* compile time features */ #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK) @@ -1219,6 +1223,10 @@ static int db_create_collation(lua_State *L) { return 0; } +// comment out `db_load_extension`, as cosmo can't resolve +// `sqlite3_enable_load_extension`, since it's built with +// `SQLITE_OMIT_LOAD_EXTENSION`, which disables extensions. +#if 0 /* Thanks to Wolfgang Oertl... */ static int db_load_extension(lua_State *L) { @@ -1246,6 +1254,7 @@ static int db_load_extension(lua_State *L) { sqlite3_free(errmsg); return 2; } +#endif /* ** trace callback: @@ -2238,7 +2247,9 @@ static const luaL_Reg dblib[] = { {"create_function", db_create_function }, {"create_aggregate", db_create_aggregate }, {"create_collation", db_create_collation }, +#if 0 {"load_extension", db_load_extension }, +#endif {"trace", db_trace }, {"progress_handler", db_progress_handler }, diff --git a/tool/net/net.mk b/tool/net/net.mk index 06005d929..1c82ce58f 100644 --- a/tool/net/net.mk +++ b/tool/net/net.mk @@ -42,6 +42,7 @@ TOOL_NET_DIRECTDEPS = \ NET_HTTP \ THIRD_PARTY_GETOPT \ THIRD_PARTY_LUA \ + THIRD_PARTY_SQLITE3 \ THIRD_PARTY_REGEX \ THIRD_PARTY_ZLIB \ TOOL_DECODE_LIB @@ -51,6 +52,7 @@ TOOL_NET_DEPS := \ o/$(MODE)/tool/net/net.pkg: \ $(TOOL_NET_OBJS) \ + o/tool/net/lsqlite3.o \ $(foreach x,$(TOOL_NET_DIRECTDEPS),$($(x)_A).pkg) o/$(MODE)/tool/net/%.com.dbg: \ @@ -64,6 +66,7 @@ o/$(MODE)/tool/net/%.com.dbg: \ o/$(MODE)/tool/net/redbean.com.dbg: \ $(TOOL_NET_DEPS) \ o/$(MODE)/tool/net/redbean.o \ + o/$(MODE)/tool/net/lsqlite3.o \ o/$(MODE)/tool/net/net.pkg \ $(CRT) \ $(APE) diff --git a/tool/net/redbean.c b/tool/net/redbean.c index f0dab0f8d..444f0c7d7 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -3646,8 +3646,11 @@ static const luaL_Reg kLuaFuncs[] = { {"popcnt", LuaPopcnt}, // }; +extern int luaopen_lsqlite3(lua_State *L); + static const luaL_Reg kLuaLibs[] = { {"re", LuaRe}, // + {"lsqlite3", luaopen_lsqlite3}, }; static void LuaSetArgv(lua_State *L) {