lua environmnents-related update
This commit is contained in:
@@ -25,8 +25,12 @@ lua::LuaState::LuaState() {
|
||||
|
||||
createFuncs();
|
||||
|
||||
lua_getglobal(L, "_G");
|
||||
lua_setglobal(L, ":G");
|
||||
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
||||
setglobal(envName(0));
|
||||
}
|
||||
|
||||
const std::string lua::LuaState::envName(int env) const {
|
||||
return "_ENV"+util::mangleid(env);
|
||||
}
|
||||
|
||||
lua::LuaState::~LuaState() {
|
||||
@@ -109,10 +113,13 @@ void lua::LuaState::createFuncs() {
|
||||
addfunc("set_block_user_bits", l_set_block_user_bits);
|
||||
}
|
||||
|
||||
void lua::LuaState::loadbuffer(const std::string& src, const std::string& file) {
|
||||
void lua::LuaState::loadbuffer(int env, const std::string& src, const std::string& file) {
|
||||
if (luaL_loadbuffer(L, src.c_str(), src.length(), file.c_str())) {
|
||||
throw lua::luaerror(lua_tostring(L, -1));
|
||||
}
|
||||
if (env && getglobal(envName(env))) {
|
||||
lua_setfenv(L, -2);
|
||||
}
|
||||
}
|
||||
|
||||
int lua::LuaState::call(int argc) {
|
||||
@@ -130,14 +137,14 @@ int lua::LuaState::callNoThrow(int argc) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lua::LuaState::eval(const std::string& src, const std::string& file) {
|
||||
int lua::LuaState::eval(int env, const std::string& src, const std::string& file) {
|
||||
auto srcText = "return ("+src+")";
|
||||
loadbuffer(srcText, file);
|
||||
loadbuffer(env, srcText, file);
|
||||
return call(0);
|
||||
}
|
||||
|
||||
int lua::LuaState::execute(const std::string& src, const std::string& file) {
|
||||
loadbuffer(src, file);
|
||||
int lua::LuaState::execute(int env, const std::string& src, const std::string& file) {
|
||||
loadbuffer(env, src, file);
|
||||
return callNoThrow(0);
|
||||
}
|
||||
|
||||
@@ -186,49 +193,24 @@ const std::string lua::LuaState::storeAnonymous() {
|
||||
return funcName;
|
||||
}
|
||||
|
||||
void lua::LuaState::initEnvironment() {
|
||||
lua_getglobal(L, "_G");
|
||||
lua_setfield(L, -1, ":G");
|
||||
}
|
||||
|
||||
int lua::LuaState::createEnvironment() {
|
||||
int id = nextEnvironment++;
|
||||
// globals table
|
||||
lua_createtable(L, 0, 1);
|
||||
|
||||
// metatable
|
||||
lua_createtable(L, 0, 1);
|
||||
|
||||
if (currentEnvironment != 0) {
|
||||
setEnvironment(0);
|
||||
}
|
||||
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
||||
lua_setfield(L, -2, "__index");
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
lua_createtable(L, 0, 0);
|
||||
initEnvironment();
|
||||
setglobal("_N"+util::mangleid(id));
|
||||
|
||||
setEnvironment(id);
|
||||
setglobal(envName(id));
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
void lua::LuaState::removeEnvironment(int id) {
|
||||
if (currentEnvironment == id) {
|
||||
setEnvironment(0);
|
||||
}
|
||||
lua_pushnil(L);
|
||||
setglobal("_N"+util::mangleid(id));
|
||||
}
|
||||
|
||||
int lua::LuaState::getEnvironment() const {
|
||||
return currentEnvironment;
|
||||
}
|
||||
|
||||
void lua::LuaState::setEnvironment(int id) {
|
||||
if (id == 0) {
|
||||
getglobal(":G");
|
||||
lua_setfenv(L, -1);
|
||||
} else {
|
||||
getglobal(":G");
|
||||
lua_getfield(L, -1, ("_N"+util::mangleid(id)).c_str());
|
||||
if (lua_isnil(L, -1)) {
|
||||
lua_pop(L, -1);
|
||||
throw luaerror("environment "+std::to_string(id)+" was not found");
|
||||
}
|
||||
lua_setfenv(L, -1);
|
||||
}
|
||||
setglobal(envName(id));
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ namespace lua {
|
||||
class LuaState {
|
||||
lua_State* L;
|
||||
int nextEnvironment = 1;
|
||||
int currentEnvironment = 0;
|
||||
|
||||
void logError(const std::string& text);
|
||||
void initEnvironment();
|
||||
@@ -25,7 +24,8 @@ namespace lua {
|
||||
LuaState();
|
||||
~LuaState();
|
||||
|
||||
void loadbuffer(const std::string& src, const std::string& file);
|
||||
const std::string envName(int env) const;
|
||||
void loadbuffer(int env, const std::string& src, const std::string& file);
|
||||
int gettop() const;
|
||||
int pushivec3(luaint x, luaint y, luaint z);
|
||||
int pushinteger(luaint x);
|
||||
@@ -35,8 +35,8 @@ namespace lua {
|
||||
luaint tointeger(int index);
|
||||
int call(int argc);
|
||||
int callNoThrow(int argc);
|
||||
int execute(const std::string& src, const std::string& file="<string>");
|
||||
int eval(const std::string& src, const std::string& file="<eval>");
|
||||
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 addfunc(const std::string& name, lua_CFunction func);
|
||||
bool getglobal(const std::string& name);
|
||||
@@ -47,9 +47,6 @@ namespace lua {
|
||||
void createFuncs();
|
||||
int createEnvironment();
|
||||
void removeEnvironment(int id);
|
||||
int getEnvironment() const;
|
||||
void setEnvironment(int id);
|
||||
|
||||
const std::string storeAnonymous();
|
||||
};
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ void load_script(fs::path name) {
|
||||
fs::path file = paths->getResources()/fs::path("scripts")/name;
|
||||
|
||||
std::string src = files::read_string(file);
|
||||
state->execute(src, file.u8string());
|
||||
state->execute(0, src, file.u8string());
|
||||
}
|
||||
|
||||
void scripting::initialize(Engine* engine) {
|
||||
@@ -58,35 +58,23 @@ void scripting::initialize(Engine* engine) {
|
||||
load_script(fs::path("stdlib.lua"));
|
||||
}
|
||||
|
||||
// todo: luaL state check
|
||||
runnable scripting::create_runnable(
|
||||
int env,
|
||||
const std::string& file,
|
||||
const std::string& src
|
||||
) {
|
||||
return [=](){
|
||||
state->execute(src, file);
|
||||
};
|
||||
}
|
||||
|
||||
runnable scripting::create_runnable(
|
||||
const std::string& file,
|
||||
const std::string& src,
|
||||
const Environment& env
|
||||
) {
|
||||
return [=](){
|
||||
int previous = state->getEnvironment();
|
||||
state->setEnvironment(env.getId());
|
||||
state->execute(src, file);
|
||||
state->setEnvironment(previous);
|
||||
state->execute(env, src, file);
|
||||
};
|
||||
}
|
||||
|
||||
wstringconsumer scripting::create_wstring_consumer(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
try {
|
||||
if (state->eval(src, file) == 0)
|
||||
if (state->eval(env, src, file) == 0)
|
||||
return [](const std::wstring& _) {};
|
||||
} catch (const lua::luaerror& err) {
|
||||
std::cerr << err.what() << std::endl;
|
||||
@@ -220,7 +208,7 @@ bool scripting::on_item_break_block(Player* player, const ItemDef* item, int x,
|
||||
void scripting::load_block_script(std::string prefix, fs::path file, block_funcs_set& funcsset) {
|
||||
std::string src = files::read_string(file);
|
||||
std::cout << "loading script " << file.u8string() << std::endl;
|
||||
state->execute(src, file.u8string());
|
||||
state->execute(0, src, file.u8string());
|
||||
|
||||
funcsset.init=state->rename("init", prefix+".init");
|
||||
funcsset.update=state->rename("on_update", prefix+".update");
|
||||
@@ -234,7 +222,7 @@ void scripting::load_block_script(std::string prefix, fs::path file, block_funcs
|
||||
void scripting::load_item_script(std::string prefix, fs::path file, item_funcs_set& funcsset) {
|
||||
std::string src = files::read_string(file);
|
||||
std::cout << "loading script " << file.u8string() << std::endl;
|
||||
state->execute(src, file.u8string());
|
||||
state->execute(0, src, file.u8string());
|
||||
funcsset.init=state->rename("init", prefix+".init");
|
||||
funcsset.on_use_on_block=state->rename("on_use_on_block", prefix+".useon");
|
||||
funcsset.on_block_break_by=state->rename("on_block_break_by", prefix+".blockbreakby");
|
||||
@@ -244,7 +232,7 @@ void scripting::load_world_script(std::string prefix, fs::path file) {
|
||||
std::string src = files::read_string(file);
|
||||
std::cout << "loading script " << file.u8string() << std::endl;
|
||||
|
||||
state->loadbuffer(src, file.u8string());
|
||||
state->loadbuffer(0, src, file.u8string());
|
||||
state->callNoThrow(0);
|
||||
|
||||
state->rename("init", prefix+".init");
|
||||
@@ -253,12 +241,12 @@ void scripting::load_world_script(std::string prefix, fs::path file) {
|
||||
state->rename("on_world_quit", prefix+".worldquit");
|
||||
}
|
||||
|
||||
void scripting::load_layout_script(fs::path file, uidocscript& script) {
|
||||
void scripting::load_layout_script(int env, fs::path file, uidocscript& script) {
|
||||
std::string src = files::read_string(file);
|
||||
std::cout << "loading script " << file.u8string() << std::endl;
|
||||
|
||||
script.environment = state->createEnvironment();
|
||||
state->loadbuffer(src, file.u8string());
|
||||
script.environment = env;
|
||||
state->loadbuffer(env, src, file.u8string());
|
||||
state->callNoThrow(0);
|
||||
script.onopen = state->hasglobal("on_open");
|
||||
script.onclose = state->hasglobal("on_close");
|
||||
|
||||
@@ -38,17 +38,13 @@ namespace scripting {
|
||||
void initialize(Engine* engine);
|
||||
|
||||
runnable create_runnable(
|
||||
int env,
|
||||
const std::string& filename,
|
||||
const std::string& source
|
||||
);
|
||||
|
||||
runnable create_runnable(
|
||||
const std::string& filename,
|
||||
const std::string& source,
|
||||
const Environment& env
|
||||
);
|
||||
|
||||
wstringconsumer create_wstring_consumer(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
@@ -69,6 +65,6 @@ namespace scripting {
|
||||
void load_block_script(std::string prefix, fs::path file, block_funcs_set& funcsset);
|
||||
void load_item_script(std::string prefix, fs::path file, item_funcs_set& funcsset);
|
||||
void load_world_script(std::string prefix, fs::path file);
|
||||
void load_layout_script(fs::path file, uidocscript& script);
|
||||
void load_layout_script(int env, fs::path file, uidocscript& script);
|
||||
void close();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user