still working on porting to lua 5.2
This commit is contained in:
@@ -9,7 +9,7 @@ static bool is_single_letter(const std::string &s) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
LuaConsole::LuaConsole() {
|
LuaConsole::LuaConsole() {
|
||||||
lua_state_ = lua_open();
|
lua_state_ = luaL_newstate();
|
||||||
clear();
|
clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
LuaSpecial LuaRegistry(LUA_REGISTRYINDEX);
|
LuaSpecial LuaRegistry(LUA_REGISTRYINDEX);
|
||||||
LuaSpecial LuaGlobals(LUA_GLOBALSINDEX);
|
|
||||||
LuaNilMarker LuaNil;
|
LuaNilMarker LuaNil;
|
||||||
LuaNewTableMarker LuaNewTable;
|
LuaNewTableMarker LuaNewTable;
|
||||||
|
|
||||||
@@ -39,8 +38,8 @@ lua_Number LuaStack::cknumber(LuaSlot s) const {
|
|||||||
std::string LuaStack::ckstring(LuaSlot s) const {
|
std::string LuaStack::ckstring(LuaSlot s) const {
|
||||||
luaL_checktype(L_, s, LUA_TSTRING);
|
luaL_checktype(L_, s, LUA_TSTRING);
|
||||||
size_t len;
|
size_t len;
|
||||||
const char *s = lua_tolstring(L_, s, &len);
|
const char *str = lua_tolstring(L_, s, &len);
|
||||||
return std::string(s, len);
|
return std::string(str, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
lua_State *LuaStack::ckthread(LuaSlot s) const {
|
lua_State *LuaStack::ckthread(LuaSlot s) const {
|
||||||
@@ -104,12 +103,6 @@ void LuaStack::pop_any_value(std::string &s) const {
|
|||||||
lua_pop(L_, 1);
|
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 {
|
void LuaStack::clearmetatable(LuaSlot tab) const {
|
||||||
lua_pushnil(L_);
|
lua_pushnil(L_);
|
||||||
lua_setmetatable(L_, tab);
|
lua_setmetatable(L_, tab);
|
||||||
@@ -156,29 +149,37 @@ lua_State *LuaStack::newthread(LuaSlot target) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void LuaStack::makeclass(LuaSlot classtab, LuaSlot classname) 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.
|
// Validate the class name.
|
||||||
if (equal(classname, "_G")) {
|
LS.checkstring(classname);
|
||||||
|
if (LS.equal(classname, "_G")) {
|
||||||
luaL_error(L_, "_G is explicitly not allowed as a class name");
|
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.
|
// Get the classtab from the global environment.
|
||||||
// Create it if it doesn't exist.
|
// Create it if it doesn't exist.
|
||||||
rawget(classtab, LuaGlobals, classname);
|
LS.rawget(classtab, globtab, classname);
|
||||||
if (isnil(classtab)) {
|
if (LS.isnil(classtab)) {
|
||||||
newtable(classtab);
|
LS.newtable(classtab);
|
||||||
rawset(LuaGlobals, classname, classtab);
|
LS.rawset(globtab, classname, classtab);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the name isn't bound to a table, abort.
|
// 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());
|
luaL_error(L_, "%s is not a class", ckstring(classname).c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Repair the special fields.
|
// Repair the special fields.
|
||||||
rawset(classtab, "__index", classtab);
|
LS.rawset(classtab, "__index", classtab);
|
||||||
rawset(classtab, "__class", classname);
|
LS.rawset(classtab, "__class", classname);
|
||||||
|
|
||||||
|
// Put the stack back.
|
||||||
|
LS.result();
|
||||||
}
|
}
|
||||||
|
|
||||||
void LuaStack::makesubtable(LuaSlot sub, LuaSlot tab, const char *name) const {
|
void LuaStack::makesubtable(LuaSlot sub, LuaSlot tab, const char *name) const {
|
||||||
|
|||||||
@@ -156,6 +156,7 @@
|
|||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
#include "lauxlib.h"
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -192,7 +193,6 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
extern LuaSpecial LuaRegistry;
|
extern LuaSpecial LuaRegistry;
|
||||||
extern LuaSpecial LuaGlobals;
|
|
||||||
|
|
||||||
class LuaUpvalue : public LuaSlot {
|
class LuaUpvalue : public LuaSlot {
|
||||||
public:
|
public:
|
||||||
@@ -404,12 +404,12 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool equal(LuaSlot v1, LuaSlot v2) const {
|
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 {
|
bool equal(LuaSlot v1, const char *name) const {
|
||||||
push_any_value(name);
|
push_any_value(name);
|
||||||
bool result = lua_equal(L_, v1, -1);
|
bool result = lua_compare(L_, v1, -1, LUA_OPEQ);
|
||||||
lua_pop(L_, 1);
|
lua_pop(L_, 1);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user