mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-11 05:51:51 +00:00
move $L, $C tables to registry
This commit is contained in:
parent
84a47b9298
commit
66413531ff
5 changed files with 94 additions and 40 deletions
|
|
@ -123,10 +123,10 @@ void lua::init_state(State* L, StateType stateType) {
|
||||||
setglobal(L, env_name(0));
|
setglobal(L, env_name(0));
|
||||||
|
|
||||||
createtable(L, 0, 0);
|
createtable(L, 0, 0);
|
||||||
setglobal(L, LAMBDAS_TABLE);
|
setregistry(L, LAMBDAS_TABLE);
|
||||||
|
|
||||||
createtable(L, 0, 0);
|
createtable(L, 0, 0);
|
||||||
setglobal(L, CHUNKS_TABLE);
|
setregistry(L, CHUNKS_TABLE);
|
||||||
|
|
||||||
initialize_libs_extends(L);
|
initialize_libs_extends(L);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -220,7 +220,7 @@ void lua::dump_stack(State* L) {
|
||||||
static std::shared_ptr<std::string> create_lambda_handler(State* L) {
|
static std::shared_ptr<std::string> create_lambda_handler(State* L) {
|
||||||
auto ptr = reinterpret_cast<ptrdiff_t>(topointer(L, -1));
|
auto ptr = reinterpret_cast<ptrdiff_t>(topointer(L, -1));
|
||||||
auto name = util::mangleid(ptr);
|
auto name = util::mangleid(ptr);
|
||||||
requireglobal(L, LAMBDAS_TABLE);
|
reguireregistry(L, LAMBDAS_TABLE);
|
||||||
pushvalue(L, -2);
|
pushvalue(L, -2);
|
||||||
setfield(L, name);
|
setfield(L, name);
|
||||||
pop(L, 2);
|
pop(L, 2);
|
||||||
|
|
@ -229,7 +229,7 @@ static std::shared_ptr<std::string> create_lambda_handler(State* L) {
|
||||||
new std::string(name),
|
new std::string(name),
|
||||||
[=](std::string* name) {
|
[=](std::string* name) {
|
||||||
auto L = lua::get_main_state();
|
auto L = lua::get_main_state();
|
||||||
requireglobal(L, LAMBDAS_TABLE);
|
reguireregistry(L, LAMBDAS_TABLE);
|
||||||
pushnil(L);
|
pushnil(L);
|
||||||
setfield(L, *name);
|
setfield(L, *name);
|
||||||
pop(L);
|
pop(L);
|
||||||
|
|
@ -242,8 +242,9 @@ runnable lua::create_runnable(State* L) {
|
||||||
auto funcptr = create_lambda_handler(L);
|
auto funcptr = create_lambda_handler(L);
|
||||||
return [=]() {
|
return [=]() {
|
||||||
auto L = lua::get_main_state();
|
auto L = lua::get_main_state();
|
||||||
if (!get_from(L, LAMBDAS_TABLE, *funcptr, false))
|
if (!get_from_registry(L, LAMBDAS_TABLE, *funcptr, false)) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
call_nothrow(L, 0, 0);
|
call_nothrow(L, 0, 0);
|
||||||
pop(L);
|
pop(L);
|
||||||
};
|
};
|
||||||
|
|
@ -252,8 +253,9 @@ runnable lua::create_runnable(State* L) {
|
||||||
KeyCallback lua::create_simple_handler(State* L) {
|
KeyCallback lua::create_simple_handler(State* L) {
|
||||||
auto funcptr = create_lambda_handler(L);
|
auto funcptr = create_lambda_handler(L);
|
||||||
return [=]() -> bool {
|
return [=]() -> bool {
|
||||||
if (!get_from(L, LAMBDAS_TABLE, *funcptr, false))
|
if (!get_from_registry(L, LAMBDAS_TABLE, *funcptr, false)) {
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
int top = gettop(L) - 1;
|
int top = gettop(L) - 1;
|
||||||
if (call_nothrow(L, 0)) {
|
if (call_nothrow(L, 0)) {
|
||||||
int nres = gettop(L) - top;
|
int nres = gettop(L) - top;
|
||||||
|
|
@ -271,8 +273,9 @@ KeyCallback lua::create_simple_handler(State* L) {
|
||||||
scripting::common_func lua::create_lambda(State* L) {
|
scripting::common_func lua::create_lambda(State* L) {
|
||||||
auto funcptr = create_lambda_handler(L);
|
auto funcptr = create_lambda_handler(L);
|
||||||
return [=](const std::vector<dv::value>& args) -> dv::value {
|
return [=](const std::vector<dv::value>& args) -> dv::value {
|
||||||
if (!get_from(L, LAMBDAS_TABLE, *funcptr, false))
|
if (!get_from_registry(L, LAMBDAS_TABLE, *funcptr, false)) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
}
|
||||||
int top = gettop(L) - 1;
|
int top = gettop(L) - 1;
|
||||||
for (const auto& arg : args) {
|
for (const auto& arg : args) {
|
||||||
pushvalue(L, arg);
|
pushvalue(L, arg);
|
||||||
|
|
@ -294,8 +297,9 @@ scripting::common_func lua::create_lambda(State* L) {
|
||||||
scripting::common_func lua::create_lambda_nothrow(State* L) {
|
scripting::common_func lua::create_lambda_nothrow(State* L) {
|
||||||
auto funcptr = create_lambda_handler(L);
|
auto funcptr = create_lambda_handler(L);
|
||||||
return [=](const std::vector<dv::value>& args) -> dv::value {
|
return [=](const std::vector<dv::value>& args) -> dv::value {
|
||||||
if (!get_from(L, LAMBDAS_TABLE, *funcptr, false))
|
if (!get_from_registry(L, LAMBDAS_TABLE, *funcptr, false)) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
}
|
||||||
int top = gettop(L) - 1;
|
int top = gettop(L) - 1;
|
||||||
for (const auto& arg : args) {
|
for (const auto& arg : args) {
|
||||||
pushvalue(L, arg);
|
pushvalue(L, arg);
|
||||||
|
|
|
||||||
|
|
@ -20,15 +20,27 @@ namespace lua {
|
||||||
std::string env_name(int env);
|
std::string env_name(int env);
|
||||||
void dump_stack(lua::State*);
|
void dump_stack(lua::State*);
|
||||||
|
|
||||||
inline bool getglobal(lua::State* L, const std::string& name) {
|
inline bool isnoneornil(lua::State* L, int idx) {
|
||||||
lua_getglobal(L, name.c_str());
|
return lua_isnoneornil(L, idx);
|
||||||
if (isnil(L, -1)) {
|
}
|
||||||
|
|
||||||
|
inline bool getfield(lua::State* L, const std::string& name, int idx = -1) {
|
||||||
|
lua_getfield(L, idx, name.c_str());
|
||||||
|
if (isnoneornil(L, -1)) {
|
||||||
pop(L);
|
pop(L);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline bool getglobal(lua::State* L, const std::string& name) {
|
||||||
|
return getfield(L, name, LUA_GLOBALSINDEX);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool getregistry(lua::State* L, const std::string& name) {
|
||||||
|
return getfield(L, name, LUA_REGISTRYINDEX);
|
||||||
|
}
|
||||||
|
|
||||||
inline int requireglobal(lua::State* L, const std::string& name) {
|
inline int requireglobal(lua::State* L, const std::string& name) {
|
||||||
if (getglobal(L, name)) {
|
if (getglobal(L, name)) {
|
||||||
return 1;
|
return 1;
|
||||||
|
|
@ -37,6 +49,14 @@ namespace lua {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline int reguireregistry(lua::State* L, const std::string& name) {
|
||||||
|
if (getregistry(L, name)) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
throw std::runtime_error("registry entry " + name + " not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
inline bool hasglobal(lua::State* L, const std::string& name) {
|
inline bool hasglobal(lua::State* L, const std::string& name) {
|
||||||
lua_getglobal(L, name.c_str());
|
lua_getglobal(L, name.c_str());
|
||||||
if (isnil(L, -1)) {
|
if (isnil(L, -1)) {
|
||||||
|
|
@ -203,9 +223,6 @@ namespace lua {
|
||||||
inline int pushglobals(lua::State* L) {
|
inline int pushglobals(lua::State* L) {
|
||||||
return pushvalue(L, LUA_GLOBALSINDEX);
|
return pushvalue(L, LUA_GLOBALSINDEX);
|
||||||
}
|
}
|
||||||
inline bool isnoneornil(lua::State* L, int idx) {
|
|
||||||
return lua_isnoneornil(L, idx);
|
|
||||||
}
|
|
||||||
inline bool isboolean(lua::State* L, int idx) {
|
inline bool isboolean(lua::State* L, int idx) {
|
||||||
return lua_isboolean(L, idx);
|
return lua_isboolean(L, idx);
|
||||||
}
|
}
|
||||||
|
|
@ -267,6 +284,9 @@ namespace lua {
|
||||||
inline void setglobal(lua::State* L, const std::string& name) {
|
inline void setglobal(lua::State* L, const std::string& name) {
|
||||||
lua_setglobal(L, name.c_str());
|
lua_setglobal(L, name.c_str());
|
||||||
}
|
}
|
||||||
|
inline void setregistry(lua::State* L, const std::string& key) {
|
||||||
|
lua_setfield(L, LUA_REGISTRYINDEX, key.c_str());
|
||||||
|
}
|
||||||
template <class T>
|
template <class T>
|
||||||
inline T* touserdata(lua::State* L, int idx) {
|
inline T* touserdata(lua::State* L, int idx) {
|
||||||
if (void* rawptr = lua_touserdata(L, idx)) {
|
if (void* rawptr = lua_touserdata(L, idx)) {
|
||||||
|
|
@ -461,15 +481,6 @@ namespace lua {
|
||||||
|
|
||||||
[[nodiscard]] dv::value tovalue(lua::State*, int idx);
|
[[nodiscard]] dv::value tovalue(lua::State*, int idx);
|
||||||
|
|
||||||
inline bool getfield(lua::State* L, const std::string& name, int idx = -1) {
|
|
||||||
lua_getfield(L, idx, name.c_str());
|
|
||||||
if (isnoneornil(L, -1)) {
|
|
||||||
pop(L);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline int requirefield(
|
inline int requirefield(
|
||||||
lua::State* L, const std::string& name, int idx = -1
|
lua::State* L, const std::string& name, int idx = -1
|
||||||
) {
|
) {
|
||||||
|
|
@ -551,7 +562,46 @@ namespace lua {
|
||||||
setfield(L, name);
|
setfield(L, name);
|
||||||
pop(L, 2);
|
pop(L, 2);
|
||||||
} else {
|
} else {
|
||||||
throw std::runtime_error("table " + tableName + " not found");
|
throw std::runtime_error("global table " + tableName + " not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void store_in_registry(
|
||||||
|
lua::State* L, const std::string& tableName, const std::string& name
|
||||||
|
) {
|
||||||
|
if (getregistry(L, tableName)) {
|
||||||
|
pushvalue(L, -2);
|
||||||
|
setfield(L, name);
|
||||||
|
pop(L, 2);
|
||||||
|
} else {
|
||||||
|
throw std::runtime_error("table " + tableName + " not found in registry");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int get_from(
|
||||||
|
lua::State* L,
|
||||||
|
const std::string& tableName,
|
||||||
|
const std::string& name,
|
||||||
|
bool required,
|
||||||
|
int idx,
|
||||||
|
std::string_view context
|
||||||
|
) {
|
||||||
|
if (getfield(L, tableName, idx)) {
|
||||||
|
if (getfield(L, name)) {
|
||||||
|
return 1;
|
||||||
|
} else if (required) {
|
||||||
|
pop(L);
|
||||||
|
throw std::runtime_error(
|
||||||
|
std::string(context) + " table " + tableName +
|
||||||
|
" has no member " + name
|
||||||
|
);
|
||||||
|
}
|
||||||
|
pop(L);
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
throw std::runtime_error(
|
||||||
|
std::string(context) + " table " + tableName + " not found"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -561,20 +611,20 @@ namespace lua {
|
||||||
const std::string& name,
|
const std::string& name,
|
||||||
bool required = false
|
bool required = false
|
||||||
) {
|
) {
|
||||||
if (getglobal(L, tableName)) {
|
return get_from(
|
||||||
if (getfield(L, name)) {
|
L, tableName, name, required, LUA_GLOBALSINDEX, "global"
|
||||||
return 1;
|
);
|
||||||
} else if (required) {
|
}
|
||||||
pop(L);
|
|
||||||
throw std::runtime_error(
|
inline int get_from_registry(
|
||||||
"table " + tableName + " has no member " + name
|
lua::State* L,
|
||||||
);
|
const std::string& tableName,
|
||||||
}
|
const std::string& name,
|
||||||
pop(L);
|
bool required = false
|
||||||
return 0;
|
) {
|
||||||
} else {
|
return get_from(
|
||||||
throw std::runtime_error("table " + tableName + " not found");
|
L, tableName, name, required, LUA_REGISTRYINDEX, "registry"
|
||||||
}
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
int call(lua::State*, int argc, int nresults = -1);
|
int call(lua::State*, int argc, int nresults = -1);
|
||||||
|
|
|
||||||
|
|
@ -742,7 +742,7 @@ void scripting::load_entity_component(
|
||||||
std::string src = io::read_string(file);
|
std::string src = io::read_string(file);
|
||||||
logger.info() << "script (component) " << file.string();
|
logger.info() << "script (component) " << file.string();
|
||||||
lua::loadbuffer(L, *env, src, fileName);
|
lua::loadbuffer(L, *env, src, fileName);
|
||||||
lua::store_in(L, lua::CHUNKS_TABLE, name);
|
lua::store_in_registry(L, lua::CHUNKS_TABLE, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::load_world_script(
|
void scripting::load_world_script(
|
||||||
|
|
|
||||||
|
|
@ -94,7 +94,7 @@ static void create_component(
|
||||||
auto compenv = create_component_environment(
|
auto compenv = create_component_environment(
|
||||||
std::move(parentEnv), -1, component.name
|
std::move(parentEnv), -1, component.name
|
||||||
);
|
);
|
||||||
lua::get_from(L, lua::CHUNKS_TABLE, component.name, true);
|
lua::get_from_registry(L, lua::CHUNKS_TABLE, component.name, true);
|
||||||
lua::pushenv(L, *compenv);
|
lua::pushenv(L, *compenv);
|
||||||
|
|
||||||
if (args != nullptr) {
|
if (args != nullptr) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue