From a9a6fbd8b597203083eb30e408d3982b01e611b8 Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Sun, 1 Apr 2012 12:50:56 +0400 Subject: [PATCH] Lua tweaks: a couple of functions, dfusion tweak, backtrace metadata. --- library/LuaTools.cpp | 2 +- library/lua/dfhack.lua | 28 ++++++++++++++++++++++++++-- plugins/Dfusion/luafiles/init.lua | 4 ++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/library/LuaTools.cpp b/library/LuaTools.cpp index b493f7611..68cd8aa33 100644 --- a/library/LuaTools.cpp +++ b/library/LuaTools.cpp @@ -189,7 +189,7 @@ bool DFHack::Lua::Require(color_ostream &out, lua_State *state, static bool load_with_env(color_ostream &out, lua_State *state, const std::string &code, int eidx) { - if (luaL_loadstring(state, code.c_str()) != LUA_OK) + if (luaL_loadbuffer(state, code.data(), code.size(), "=(interactive)") != LUA_OK) { report_error(out, state); return false; diff --git a/library/lua/dfhack.lua b/library/lua/dfhack.lua index 33d774321..ffb415646 100644 --- a/library/lua/dfhack.lua +++ b/library/lua/dfhack.lua @@ -1,5 +1,29 @@ -- Common startup file for all dfhack plugins with lua support +-- The global dfhack table is already created by C++ init code. --- The global dfhack table is already created by C++ init --- code. Feed it back to the require() mechanism. +function mkmodule(module,env) + local pkg = package.loaded[module] + if pkg == nil then + pkg = {} + else + if type(pkg) ~= 'table' then + error("Not a table in package.loaded["..module.."]") + end + end + setmetatable(pkg, { __index = (env or _G) }) + return pkg +end + +function reload(module) + if type(package.loaded[module]) ~= 'table' then + error("Module not loaded: "..module) + end + local path,err = package.searchpath(module,package.path) + if not path then + error(err) + end + dofile(path) +end + +-- Feed the table back to the require() mechanism. return dfhack diff --git a/plugins/Dfusion/luafiles/init.lua b/plugins/Dfusion/luafiles/init.lua index 42b8dd68c..0cab4c31d 100644 --- a/plugins/Dfusion/luafiles/init.lua +++ b/plugins/Dfusion/luafiles/init.lua @@ -1,3 +1,7 @@ +Console.print = dfhack.print +Console.println = dfhack.println +Console.printerr = dfhack.printerr + function err(msg) --make local maybe... print(msg) print(debug.traceback())