package runner import ( "Moonshark/utils/logger" _ "embed" luajit "git.sharkk.net/Sky/LuaJIT-to-Go" ) //go:embed lua/sandbox.lua var sandboxLuaCode string //go:embed lua/json.lua var jsonLuaCode string //go:embed lua/sqlite.lua var sqliteLuaCode string //go:embed lua/fs.lua var fsLuaCode string //go:embed lua/util.lua var utilLuaCode string //go:embed lua/string.lua var stringLuaCode string //go:embed lua/table.lua var tableLuaCode string //go:embed lua/crypto.lua var cryptoLuaCode string //go:embed lua/time.lua var timeLuaCode string //go:embed lua/math.lua var mathLuaCode string //go:embed lua/env.lua var envLuaCode string //go:embed lua/http.lua var httpLuaCode string //go:embed lua/cookie.lua var cookieLuaCode string //go:embed lua/csrf.lua var csrfLuaCode string //go:embed lua/render.lua var renderLuaCode string //go:embed lua/session.lua var sessionLuaCode string // Module represents a Lua module to load type Module struct { name string code string global bool // true if module defines globals, false if it returns a table } var modules = []Module{ {"http", httpLuaCode, true}, {"cookie", cookieLuaCode, true}, {"session", sessionLuaCode, true}, {"csrf", csrfLuaCode, true}, {"render", renderLuaCode, true}, {"json", jsonLuaCode, true}, {"sqlite", sqliteLuaCode, false}, {"fs", fsLuaCode, true}, {"util", utilLuaCode, true}, {"string", stringLuaCode, false}, {"table", tableLuaCode, false}, {"crypto", cryptoLuaCode, true}, {"time", timeLuaCode, false}, {"math", mathLuaCode, false}, {"env", envLuaCode, true}, } // loadModule loads a single module into the Lua state func loadModule(state *luajit.State, m Module) error { if m.global { // Module defines globals directly, just execute it return state.DoString(m.code) } // Module returns a table, capture it and set as global if err := state.LoadString(m.code); err != nil { return err } if err := state.Call(0, 1); err != nil { return err } state.SetGlobal(m.name) return nil } // loadSandboxIntoState loads all modules and sandbox into a Lua state func loadSandboxIntoState(state *luajit.State, verbose bool) error { // Load all utility modules for _, module := range modules { if err := loadModule(state, module); err != nil { if verbose { logger.Errorf("Failed to load %s module: %v", module.name, err) } return err } if verbose { logger.Debugf("Loaded %s.lua", module.name) } } // Initialize module-specific globals if err := state.DoString(`__active_sqlite_connections = {}`); err != nil { return err } // Load sandbox last - defines __execute and core functions if verbose { logger.Debugf("Loading sandbox.lua") } return state.DoString(sandboxLuaCode) }