Skip to content

Commit

Permalink
[MOD] Replace iris_lua_t::run() with iris_lua_t::load() + call().
Browse files Browse the repository at this point in the history
  • Loading branch information
paintdream committed Jun 8, 2023
1 parent 1e51c5d commit 6f684de
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 37 deletions.
47 changes: 14 additions & 33 deletions src/iris_lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,39 +81,6 @@ namespace iris {
return state;
}

// run a piece of code
template <typename return_t = void>
return_t run(std::string_view code) {
IRIS_PROFILE_SCOPE(__FUNCTION__);
auto guard = write_fence();
lua_State* L = state;
stack_guard_t stack_guard(L);

if (luaL_loadbuffer(L, code.data(), code.size(), "run") != LUA_OK) {
fprintf(stderr, "[ERROR] iris_lua_t::run() -> load code error: %s\n", lua_tostring(L, -1));
lua_pop(L, 1);
if constexpr (!std::is_void_v<return_t>) {
return return_t();
}
}

if (lua_pcall(L, 0, 1, 0) != LUA_OK) {
fprintf(stderr, "[ERROR] iris_lua_t::run() -> run code error: %s\n", lua_tostring(L, -1));
lua_pop(L, 1);
if constexpr (!std::is_void_v<return_t>) {
return return_t();
}
} else {
if constexpr (!std::is_void_v<return_t>) {
return_t ret = get_variable<return_t>(L, -1);
lua_pop(L, 1);
return ret;
} else {
lua_pop(L, 1);
}
}
}

// holding lua value
struct ref_t {
explicit ref_t(int v = LUA_REFNIL) noexcept : value(v) { assert(LUA_REFNIL == 0 || v != 0); }
Expand Down Expand Up @@ -277,6 +244,20 @@ namespace iris {
return refguard_t<sizeof...(args_t)>(state, args...);
}

ref_t load(std::string_view code, std::string_view name = "") {
auto guard = write_fence();
lua_State* L = state;
stack_guard_t stack_guard(L);

if (luaL_loadbuffer(L, code.data(), code.size(), name.data()) != LUA_OK) {
fprintf(stderr, "[ERROR] iris_lua_t::run() -> load code error: %s\n", lua_tostring(L, -1));
lua_pop(L, 1);
return ref_t();
}

return ref_t(luaL_ref(L, LUA_REGISTRYINDEX));
}

// a guard for checking stack balance
struct stack_guard_t {
#ifdef _DEBUG
Expand Down
8 changes: 4 additions & 4 deletions test/iris_lua_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ int main(void) {
warp.preempt();
#endif

bool ret = lua.run<bool>("\
bool ret = lua.call<bool>(lua.load("\
print(_VERSION)\n\
local a = example_t.create()\n\
local b = example_t.create()\n\
Expand Down Expand Up @@ -228,7 +228,7 @@ int main(void) {
for i = 1, #t do\n\
print(t[i])\n\
end\n\
return true\n");
return true\n"));
assert(ret);
auto tab = lua.make_table([](lua_t&& lua) {
lua.define("key", "value");
Expand All @@ -250,7 +250,7 @@ int main(void) {
lua.deref(tab);

#if USE_LUA_COROUTINE
lua.run<>("\n\
lua.call<void>(lua.load("\n\
local a = example_t.create()\n\
local coro = coroutine.create(function() \n\
print('coro get ' .. a.coro_get_int('hello')) \n\
Expand All @@ -259,7 +259,7 @@ int main(void) {
a.coro_get_none()\n\
print('coro finished')\n\
end)\n\
coroutine.resume(coro)\n");
coroutine.resume(coro)\n"));

warp.yield();
worker.join();
Expand Down

0 comments on commit 6f684de

Please sign in to comment.