Skip to content

Commit

Permalink
use l.newtable/l.newuserdata
Browse files Browse the repository at this point in the history
  • Loading branch information
peacalm committed Jun 19, 2023
1 parent 783e8fe commit 206560f
Showing 1 changed file with 37 additions and 29 deletions.
66 changes: 37 additions & 29 deletions include/peacalm/luaw.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ class luaw {
// Should load base and package first for any preload.
luaL_requiref(L_, LUA_GNAME, luaopen_base, 1);
luaL_requiref(L_, LUA_LOADLIBNAME, luaopen_package, 1);
lua_getfield(L_, -1, "preload");
getfield(-1, "preload");
for (const luaL_Reg& l : o.libs_preload_) {
pushcfunction(l.func);
setfield(-2, l.name);
Expand All @@ -373,7 +373,7 @@ class luaw {
// Should load base and package first for any preload.
luaL_requiref(L_, LUA_GNAME, luaopen_base, 1);
luaL_requiref(L_, LUA_LOADLIBNAME, luaopen_package, 1);
lua_getfield(L_, -1, "preload");
getfield(-1, "preload");
static const luaL_Reg preloadlibs[] = {{LUA_COLIBNAME, luaopen_coroutine},
{LUA_TABLIBNAME, luaopen_table},
{LUA_IOLIBNAME, luaopen_io},
Expand Down Expand Up @@ -486,18 +486,16 @@ class luaw {
int rawgeti(int idx, lua_integer_t n) { return lua_rawgeti(L_, idx, n); }
int rawgetp(int idx, const void* p) { return lua_rawgetp(L_, idx, p); }
int rawgetfield(int idx, const char* k) {
PEACALM_LUAW_ASSERT(k);
push(k);
pushstring(k);
return rawget(idx);
}

void rawset(int idx) { lua_rawset(L_, idx); }
void rawseti(int idx, lua_integer_t n) { lua_rawseti(L_, idx, n); }
void rawsetp(int idx, const void* p) { lua_rawsetp(L_, idx, p); }
void rawsetfield(int idx, const char* k) {
PEACALM_LUAW_ASSERT(k);
int aidx = abs(idx);
push(k);
pushstring(k);
pushvalue(-2);
rawset(aidx);
pop();
Expand All @@ -510,6 +508,10 @@ class luaw {
/// Pop a table or nil and set it as metatable for value at idx.
void setmetatable(int idx) { lua_setmetatable(L_, idx); }

void newtable() { lua_newtable(L_); }
void* newuserdata(size_t size) { return lua_newuserdata(L_, size); }
lua_State* newthread() { return lua_newthread(L_); }

/// Whether the value at idx is indexable.
bool indexable(int idx = -1) const {
if (istable(idx)) return true;
Expand Down Expand Up @@ -766,7 +768,7 @@ class luaw {
/// or push a nil if the operation fails.
self_t& seek(const char* name, int idx = -1) {
if (name && (istable(idx) || indexable(idx))) {
lua_getfield(L_, idx, name);
getfield(idx, name);
} else {
pushnil();
}
Expand Down Expand Up @@ -854,6 +856,13 @@ class luaw {
setglobal(name);
}

/// Pushes the global environment onto the stack.
void pushglobaltable() { lua_pushglobaltable(L_); }

/// Pushes the thread represented by L onto the stack.
/// Returns 1 if this thread is the main thread of its state.
int pushthread() { return lua_pushthread(L_); }

/// Push a C closure function with n upvalues.
void pushcclosure(lua_cfunction_t f, int n) { lua_pushcclosure(L_, f, n); }

Expand All @@ -866,12 +875,11 @@ class luaw {
/// Push nil. Equivalent to `push(nullptr)`.
void pushnil() { lua_pushnil(L_); }

/// Pushes the thread represented by L onto the stack.
/// Returns 1 if this thread is the main thread of its state.
int pushthread() { return lua_pushthread(L_); }

/// Pushes the global environment onto the stack.
void pushglobaltable() { lua_pushglobaltable(L_); }
/// Return a pointer to the internal copy of the string.
const char* pushstring(const char* s) {
PEACALM_LUAW_ASSERT(s); // we forbit s to be NULL
return lua_pushstring(L_, s);
}

///////////////////////// touch table ////////////////////////////////////////

Expand All @@ -881,7 +889,7 @@ class luaw {
getglobal(name);
if (istable() || indexable_and_newindexable()) return *this;
pop();
lua_newtable(L_);
newtable();
pushvalue(-1); // make a copy
setglobal(name);
return *this;
Expand All @@ -898,7 +906,7 @@ class luaw {
getfield(aidx, name);
if (istable() || indexable_and_newindexable()) return *this;
pop();
lua_newtable(L_);
newtable();
setfield(aidx, name);
getfield(aidx, name);
return *this;
Expand All @@ -916,7 +924,7 @@ class luaw {
geti(aidx, n);
if (istable() || indexable_and_newindexable()) return *this;
pop();
lua_newtable(L_);
newtable();
seti(aidx, n);
geti(aidx, n);
return *this;
Expand All @@ -932,7 +940,7 @@ class luaw {
gettable(aidx);
if (istable() || indexable_and_newindexable()) return *this;
pop();
lua_newtable(L_);
newtable();
pushlightuserdata(p);
pushvalue(-2);
settable(aidx);
Expand All @@ -952,7 +960,7 @@ class luaw {
if (!getmetatable(idx)) {
int aidx = abs_index(idx);
if (!m.tname) {
lua_newtable(L_);
newtable();
} else {
luaL_newmetatable(L_, m.tname);
}
Expand Down Expand Up @@ -1916,7 +1924,7 @@ struct metatable_factory_base {
}

static void push_exclusive_metatable(luaw& l) {
lua_newtable(l.L());
l.newtable();
Derived::set_metamethods(l);
}
};
Expand Down Expand Up @@ -2353,7 +2361,7 @@ struct luaw::pusher<luaw::class_tag> {

template <typename SolidY, typename Y>
static void __push(luaw& l, Y&& v, std::false_type) {
void* p = lua_newuserdata(l.L(), sizeof(SolidY));
void* p = l.newuserdata(sizeof(SolidY));
new (p) SolidY(std::forward<Y>(v));
luaw::metatable_factory<SolidY*>::push_shared_metatable(l);
l.setmetatable(-2);
Expand All @@ -2366,7 +2374,7 @@ struct luaw::pusher<luaw::newtable_tag> {
static const size_t size = 1;

static int push(luaw& l, newtable_tag) {
lua_newtable(l.L());
l.newtable();
return 1;
}
};
Expand Down Expand Up @@ -2449,11 +2457,11 @@ struct luaw::pusher<Return (*)(Args...)> {
};

// object
auto faddr = static_cast<SolidF*>(lua_newuserdata(l.L(), sizeof(f)));
auto faddr = static_cast<SolidF*>(l.newuserdata(sizeof(f)));
new (faddr) SolidF(std::forward<F>(f));

// build metatable
lua_newtable(l.L());
l.newtable();

l.pushcfunction(__call);
l.setfield(-2, "__call");
Expand Down Expand Up @@ -2483,7 +2491,7 @@ struct luaw::pusher<Return (*)(Args...)> {
return ret_num;
};

auto faddr = static_cast<SolidF*>(lua_newuserdata(l.L(), sizeof(f)));
auto faddr = static_cast<SolidF*>(l.newuserdata(sizeof(f)));
new (faddr) SolidF(std::forward<F>(f));

l.pushcclosure(closure, 1);
Expand Down Expand Up @@ -2645,7 +2653,7 @@ struct luaw::pusher<std::pair<T, U>> {
static const size_t size = 1;

static int push(luaw& l, const std::pair<T, U>& p) {
lua_newtable(l.L());
l.newtable();
l.push(p.first);
l.rawseti(-2, 1);
l.push(p.second);
Expand All @@ -2659,7 +2667,7 @@ namespace luaw_detail {
// Implementation for all list like containers
template <typename Container>
static int __push_list(luaw& l, const Container& v) {
lua_newtable(l.L());
l.newtable();
int cnt = 0;
for (auto b = v.begin(), e = v.end(); b != e; ++b) {
l.push(*b);
Expand All @@ -2672,7 +2680,7 @@ static int __push_list(luaw& l, const Container& v) {
// Make a Key-True table in Lua to represent set
template <typename Container>
static int __push_set(luaw& l, const Container& v) {
lua_newtable(l.L());
l.newtable();
for (auto b = v.begin(), e = v.end(); b != e; ++b) {
l.push(*b);
lua_pushboolean(l.L(), 1);
Expand All @@ -2684,7 +2692,7 @@ static int __push_set(luaw& l, const Container& v) {
// Implementation for all map like containers
template <typename Container>
static int __push_map(luaw& l, const Container& v) {
lua_newtable(l.L());
l.newtable();
for (auto b = v.begin(), e = v.end(); b != e; ++b) {
l.push(b->first);
l.push(b->second);
Expand Down Expand Up @@ -3418,7 +3426,7 @@ class custom_luaw : public luaw {

void set_globale_metateble() {
pushglobaltable();
if (!getmetatable(-1)) { lua_newtable(L()); }
if (!getmetatable(-1)) { newtable(); }
pushcfunction(_G__index);
setfield(-2, "__index");
setmetatable(-2);
Expand Down

0 comments on commit 206560f

Please sign in to comment.