From 2e051fcc4efcfe6af2d3ef22d6a4ba63c9f1c65d Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sat, 5 Apr 2025 11:49:48 +0530 Subject: Add package path to lua (only current build dir) --- CMakeLists.txt | 3 +++ TODO.org | 3 ++- lua/api.lua | 13 ------------- lua/null-browser/api.lua | 17 +++++++++++++++++ src/LuaRuntime.cpp | 23 ++++++++++++++++++++--- src/LuaRuntime.hpp | 12 +++++++++--- 6 files changed, 51 insertions(+), 20 deletions(-) delete mode 100644 lua/api.lua create mode 100644 lua/null-browser/api.lua diff --git a/CMakeLists.txt b/CMakeLists.txt index 7c2ce8e..8e1ceb3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,9 @@ find_package(PkgConfig REQUIRED) set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib") set(CMAKE_BUILD_WITH_INSTALL_RPATH ON) +# TODO: Please fix this. This sucks +add_compile_definitions(PROJECT_LUA_PATH="./lua/?.lua") + if(DEFINED ENV{RELEASE}) set(CMAKE_BUILD_TYPE Release) set(CMAKE_CXX_FLAGS "-O3 ${CMAKE_CXX_FLAGS}") diff --git a/TODO.org b/TODO.org index 28573e3..5c00f33 100644 --- a/TODO.org +++ b/TODO.org @@ -13,8 +13,9 @@ - [X] Multi-window - [X] New window on new window request - [X] Socket for opening window in current session (lua eval) -- [ ] Config loading and lua path +- [X] Config loading and lua path - [ ] Events/autocommands +- [ ] Url changed event - [ ] History storage - [ ] History completion - [ ] Log stdout, errors and results from lua somewhere diff --git a/lua/api.lua b/lua/api.lua deleted file mode 100644 index b6495e0..0000000 --- a/lua/api.lua +++ /dev/null @@ -1,13 +0,0 @@ -local function shallow_copy(t) - local t2 = {} - for k, v in pairs(t) do t2[k] = v end - return t2 -end - -web.event.add_listener = function(events, opts) - opts = shallow_copy(opts or {}) - if type(events) ~= "table" then events = { events } end - opts.events = events or {} - - __internals.register_event(opts) -end diff --git a/lua/null-browser/api.lua b/lua/null-browser/api.lua new file mode 100644 index 0000000..d2e0be7 --- /dev/null +++ b/lua/null-browser/api.lua @@ -0,0 +1,17 @@ +print("FOOOOOOOOOOBARRRRRR") + +local function shallow_copy(t) + local t2 = {} + for k, v in pairs(t) do t2[k] = v end + return t2 +end + +web.event = web.event or {} + +web.event.add_listener = function(events, opts) + opts = shallow_copy(opts or {}) + if type(events) ~= "table" then events = { events } end + opts.events = events or {} + + __internals.register_event(opts) +end diff --git a/src/LuaRuntime.cpp b/src/LuaRuntime.cpp index 1dc9a85..d9bc86e 100644 --- a/src/LuaRuntime.cpp +++ b/src/LuaRuntime.cpp @@ -1,6 +1,6 @@ -#include "WindowActionRouter.hpp" -#include "lua.h" #include +#include +#include #include extern "C" { #include @@ -8,12 +8,29 @@ extern "C" { #include "AsyncEventLoop.hpp" #include "LuaRuntime.hpp" +#include "WindowActionRouter.hpp" LuaRuntime::LuaRuntime() { state = luaL_newstate(); luaL_openlibs(state); - preserve_top(state, { init_web_lib(); }) + preserve_top(state, { init_web_lib(); }); + + auto lua_path = QString(PROJECT_LUA_PATH); + preserve_top(state, { + lua_getglobal(state, "package"); + lua_getfield(state, -1, "path"); + auto pkg_path = QString(lua_tostring(state, -1)) + ";" + lua_path; + lua_pop(state, 1); + lua_pushstring(state, pkg_path.toStdString().c_str()); + lua_setfield(state, -2, "path"); + + lua_getglobal(state, "require"); + lua_pushstring(state, "null-browser.api"); + if (lua_pcall(state, 1, 0, 0) != LUA_OK) { + qCritical() << "Unable to load browser api" << lua_tostring(state, -1); + } + }); } void LuaRuntime::start_event_loop() { diff --git a/src/LuaRuntime.hpp b/src/LuaRuntime.hpp index 74b7810..2b834aa 100644 --- a/src/LuaRuntime.hpp +++ b/src/LuaRuntime.hpp @@ -10,10 +10,16 @@ #include "utils.hpp" #include "widgets/WebViewStack.hpp" +#ifndef PROJECT_LUA_PATH +#define PROJECT_LUA_PATH "" +#endif + #define preserve_top(STATE, BODY) \ - const int __top = lua_gettop(STATE); \ - BODY; \ - lua_settop(STATE, __top); + { \ + const int __top = lua_gettop(STATE); \ + BODY; \ + lua_settop(STATE, __top); \ + }; class LuaRuntime : public QObject { Q_OBJECT -- cgit v1.3.1