lua console library
This commit is contained in:
@@ -12,6 +12,10 @@ inline std::string LAMBDAS_TABLE = "$L";
|
||||
|
||||
static debug::Logger logger("lua-state");
|
||||
|
||||
namespace scripting {
|
||||
extern lua::LuaState* state;
|
||||
}
|
||||
|
||||
lua::luaerror::luaerror(const std::string& message) : std::runtime_error(message) {
|
||||
}
|
||||
|
||||
@@ -123,19 +127,20 @@ void lua::LuaState::remove(const std::string& name) {
|
||||
}
|
||||
|
||||
void lua::LuaState::createLibs() {
|
||||
openlib("audio", audiolib, 0);
|
||||
openlib("block", blocklib, 0);
|
||||
openlib("core", corelib, 0);
|
||||
openlib("file", filelib, 0);
|
||||
openlib("gui", guilib, 0);
|
||||
openlib("input", inputlib, 0);
|
||||
openlib("inventory", inventorylib, 0);
|
||||
openlib("item", itemlib, 0);
|
||||
openlib("json", jsonlib, 0);
|
||||
openlib("pack", packlib, 0);
|
||||
openlib("player", playerlib, 0);
|
||||
openlib("time", timelib, 0);
|
||||
openlib("world", worldlib, 0);
|
||||
openlib("audio", audiolib);
|
||||
openlib("block", blocklib);
|
||||
openlib("console", consolelib);
|
||||
openlib("core", corelib);
|
||||
openlib("file", filelib);
|
||||
openlib("gui", guilib);
|
||||
openlib("input", inputlib);
|
||||
openlib("inventory", inventorylib);
|
||||
openlib("item", itemlib);
|
||||
openlib("json", jsonlib);
|
||||
openlib("pack", packlib);
|
||||
openlib("player", playerlib);
|
||||
openlib("time", timelib);
|
||||
openlib("world", worldlib);
|
||||
|
||||
addfunc("print", lua_wrap_errors<l_print>);
|
||||
}
|
||||
@@ -364,9 +369,9 @@ bool lua::LuaState::isfunction(int idx) {
|
||||
return lua_isfunction(L, idx);
|
||||
}
|
||||
|
||||
void lua::LuaState::openlib(const std::string& name, const luaL_Reg* libfuncs, int nup) {
|
||||
void lua::LuaState::openlib(const std::string& name, const luaL_Reg* libfuncs) {
|
||||
lua_newtable(L);
|
||||
luaL_setfuncs(L, libfuncs, nup);
|
||||
luaL_setfuncs(L, libfuncs, 0);
|
||||
lua_setglobal(L, name.c_str());
|
||||
}
|
||||
|
||||
@@ -377,7 +382,7 @@ const std::string lua::LuaState::storeAnonymous() {
|
||||
return funcName;
|
||||
}
|
||||
|
||||
runnable lua::LuaState::createRunnable() {
|
||||
std::shared_ptr<std::string> lua::LuaState::createLambdaHandler() {
|
||||
auto ptr = reinterpret_cast<ptrdiff_t>(lua_topointer(L, -1));
|
||||
auto name = util::mangleid(ptr);
|
||||
lua_getglobal(L, LAMBDAS_TABLE.c_str());
|
||||
@@ -385,13 +390,17 @@ runnable lua::LuaState::createRunnable() {
|
||||
lua_setfield(L, -2, name.c_str());
|
||||
lua_pop(L, 2);
|
||||
|
||||
std::shared_ptr<std::string> funcptr(new std::string(name), [=](auto* name) {
|
||||
return std::shared_ptr<std::string>(new std::string(name), [=](auto* name) {
|
||||
lua_getglobal(L, LAMBDAS_TABLE.c_str());
|
||||
lua_pushnil(L);
|
||||
lua_setfield(L, -2, name->c_str());
|
||||
lua_pop(L, 1);
|
||||
delete name;
|
||||
});
|
||||
}
|
||||
|
||||
runnable lua::LuaState::createRunnable() {
|
||||
auto funcptr = createLambdaHandler();
|
||||
return [=]() {
|
||||
lua_getglobal(L, LAMBDAS_TABLE.c_str());
|
||||
lua_getfield(L, -1, funcptr->c_str());
|
||||
@@ -399,6 +408,23 @@ runnable lua::LuaState::createRunnable() {
|
||||
};
|
||||
}
|
||||
|
||||
scripting::common_func lua::LuaState::createLambda() {
|
||||
auto funcptr = createLambdaHandler();
|
||||
return [=](const std::vector<dynamic::Value>& args) {
|
||||
lua_getglobal(L, LAMBDAS_TABLE.c_str());
|
||||
lua_getfield(L, -1, funcptr->c_str());
|
||||
for (const auto& arg : args) {
|
||||
pushvalue(arg);
|
||||
}
|
||||
if (call(args.size())) {
|
||||
auto result = tovalue(-1);
|
||||
pop(1);
|
||||
return result;
|
||||
}
|
||||
return dynamic::Value(dynamic::NONE);
|
||||
};
|
||||
}
|
||||
|
||||
int lua::LuaState::createEnvironment(int parent) {
|
||||
int id = nextEnvironment++;
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include "lua_commons.hpp"
|
||||
|
||||
#include "../scripting_functional.hpp"
|
||||
#include "../../../data/dynamic.hpp"
|
||||
#include "../../../delegates.hpp"
|
||||
|
||||
@@ -26,6 +27,8 @@ namespace lua {
|
||||
void logError(const std::string& text);
|
||||
void removeLibFuncs(const char* libname, const char* funcs[]);
|
||||
void createLibs();
|
||||
|
||||
std::shared_ptr<std::string> createLambdaHandler();
|
||||
public:
|
||||
LuaState();
|
||||
~LuaState();
|
||||
@@ -60,7 +63,7 @@ namespace lua {
|
||||
int callNoThrow(int argc);
|
||||
int execute(int env, const std::string& src, const std::string& file="<string>");
|
||||
int eval(int env, const std::string& src, const std::string& file="<eval>");
|
||||
void openlib(const std::string& name, const luaL_Reg* libfuncs, int nup);
|
||||
void openlib(const std::string& name, const luaL_Reg* libfuncs);
|
||||
void addfunc(const std::string& name, lua_CFunction func);
|
||||
bool getglobal(const std::string& name);
|
||||
void setglobal(const std::string& name);
|
||||
@@ -68,6 +71,8 @@ namespace lua {
|
||||
bool rename(const std::string& from, const std::string& to);
|
||||
void remove(const std::string& name);;
|
||||
runnable createRunnable();
|
||||
scripting::common_func createLambda();
|
||||
|
||||
int createEnvironment(int parent);
|
||||
void removeEnvironment(int id);
|
||||
const std::string storeAnonymous();
|
||||
|
||||
@@ -20,6 +20,7 @@ extern const luaL_Reg timelib [];
|
||||
extern const luaL_Reg worldlib [];
|
||||
extern const luaL_Reg jsonlib [];
|
||||
extern const luaL_Reg inputlib [];
|
||||
extern const luaL_Reg consolelib [];
|
||||
|
||||
// Lua Overrides
|
||||
extern int l_print(lua_State* L);
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
#include "api_lua.hpp"
|
||||
#include "lua_commons.hpp"
|
||||
#include "LuaState.hpp"
|
||||
|
||||
#include "../scripting.hpp"
|
||||
#include "../../CommandsInterpreter.hpp"
|
||||
#include "../../../engine.hpp"
|
||||
#include "../../../coders/commons.hpp"
|
||||
|
||||
namespace scripting {
|
||||
extern lua::LuaState* state;
|
||||
}
|
||||
|
||||
using namespace scripting;
|
||||
|
||||
static int l_add_command(lua_State* L) {
|
||||
auto scheme = lua_tostring(L, 1);
|
||||
lua_pushvalue(L, 2);
|
||||
auto func = state->createLambda();
|
||||
try {
|
||||
engine->getCommandsInterpreter()->getRepository()->add(
|
||||
scheme, [func](auto, auto args, auto kwargs) {
|
||||
return func({args, kwargs});
|
||||
}
|
||||
);
|
||||
} catch (const parsing_error& err) {
|
||||
luaL_error(L, ("command scheme error:\n"+err.errorLog()).c_str());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_execute(lua_State* L) {
|
||||
auto prompt = lua_tostring(L, 1);
|
||||
auto result = engine->getCommandsInterpreter()->execute(prompt);
|
||||
state->pushvalue(result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const luaL_Reg consolelib [] = {
|
||||
{"add_command", lua_wrap_errors<l_add_command>},
|
||||
{"execute", lua_wrap_errors<l_execute>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
Reference in New Issue
Block a user