still working on porting to lua 5.2

This commit is contained in:
2021-02-28 14:24:59 -05:00
parent 37c3bf8cc1
commit b23728f525
3 changed files with 24 additions and 23 deletions

View File

@@ -9,7 +9,7 @@ static bool is_single_letter(const std::string &s) {
}
LuaConsole::LuaConsole() {
lua_state_ = lua_open();
lua_state_ = luaL_newstate();
clear();
}

View File

@@ -3,7 +3,6 @@
#include <iostream>
LuaSpecial LuaRegistry(LUA_REGISTRYINDEX);
LuaSpecial LuaGlobals(LUA_GLOBALSINDEX);
LuaNilMarker LuaNil;
LuaNewTableMarker LuaNewTable;
@@ -39,8 +38,8 @@ lua_Number LuaStack::cknumber(LuaSlot s) const {
std::string LuaStack::ckstring(LuaSlot s) const {
luaL_checktype(L_, s, LUA_TSTRING);
size_t len;
const char *s = lua_tolstring(L_, s, &len);
return std::string(s, len);
const char *str = lua_tolstring(L_, s, &len);
return std::string(str, len);
}
lua_State *LuaStack::ckthread(LuaSlot s) const {
@@ -104,12 +103,6 @@ void LuaStack::pop_any_value(std::string &s) const {
lua_pop(L_, 1);
}
std::string LuaStack::ckstring(LuaSlot s) const {
size_t len;
const char *str = luaL_cklstring(L_, s, &len);
return std::string(str, len);
}
void LuaStack::clearmetatable(LuaSlot tab) const {
lua_pushnil(L_);
lua_setmetatable(L_, tab);
@@ -156,29 +149,37 @@ lua_State *LuaStack::newthread(LuaSlot target) const {
}
void LuaStack::makeclass(LuaSlot classtab, LuaSlot classname) const {
checkstring(classname);
LuaVar globtab;
LuaStack LS(L_, globtab);
// Special case: if the classname is _G, detect and error.
if (equal(classname, "_G")) {
// Validate the class name.
LS.checkstring(classname);
if (LS.equal(classname, "_G")) {
luaL_error(L_, "_G is explicitly not allowed as a class name");
}
// Fetch the global environment from the registry.
LS.rawget(globtab, LuaRegistry, LUA_RIDX_GLOBALS);
// Get the classtab from the global environment.
// Create it if it doesn't exist.
rawget(classtab, LuaGlobals, classname);
if (isnil(classtab)) {
newtable(classtab);
rawset(LuaGlobals, classname, classtab);
LS.rawget(classtab, globtab, classname);
if (LS.isnil(classtab)) {
LS.newtable(classtab);
LS.rawset(globtab, classname, classtab);
}
// If the name isn't bound to a table, abort.
if (!istable(classtab)) {
if (!LS.istable(classtab)) {
luaL_error(L_, "%s is not a class", ckstring(classname).c_str());
}
// Repair the special fields.
rawset(classtab, "__index", classtab);
rawset(classtab, "__class", classname);
LS.rawset(classtab, "__index", classtab);
LS.rawset(classtab, "__class", classname);
// Put the stack back.
LS.result();
}
void LuaStack::makesubtable(LuaSlot sub, LuaSlot tab, const char *name) const {

View File

@@ -156,6 +156,7 @@
extern "C" {
#include "lua.h"
#include "lauxlib.h"
}
#include <string>
@@ -192,7 +193,6 @@ public:
};
extern LuaSpecial LuaRegistry;
extern LuaSpecial LuaGlobals;
class LuaUpvalue : public LuaSlot {
public:
@@ -404,12 +404,12 @@ public:
}
bool equal(LuaSlot v1, LuaSlot v2) const {
return lua_equal(L_, v1, v2);
return lua_compare(L_, v1, v2, LUA_OPEQ);
}
bool equal(LuaSlot v1, const char *name) const {
push_any_value(name);
bool result = lua_equal(L_, v1, -1);
bool result = lua_compare(L_, v1, -1, LUA_OPEQ);
lua_pop(L_, 1);
return result;
}