Added new 'type' function, other mods
This commit is contained in:
@@ -10,3 +10,18 @@ void Gui::add_menu_item(const std::string &id, const std::string &label) {
|
|||||||
elts_.push_back(elt);
|
elts_.push_back(elt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LuaDefine(gui_create, "c") {
|
||||||
|
LuaRet lgui;
|
||||||
|
LuaStack LS(L, lgui);
|
||||||
|
LS.newpointer<Gui>(lgui, new Gui, true);
|
||||||
|
return LS.result();
|
||||||
|
}
|
||||||
|
|
||||||
|
LuaDefine(gui_add_menu_item, "c") {
|
||||||
|
LuaArg lgui, lid;
|
||||||
|
LuaStack LS(L, lgui, lid);
|
||||||
|
Gui *gui = LS.ckuserdata<Gui>(lgui);
|
||||||
|
std::string id = LS.ckstring(lid);
|
||||||
|
gui->add_menu_item(id, id);
|
||||||
|
return LS.result();
|
||||||
|
}
|
||||||
|
|||||||
@@ -48,34 +48,23 @@ void LuaConsole::add(std::string line) {
|
|||||||
raw_input_ += line;
|
raw_input_ += line;
|
||||||
raw_input_ += '\n';
|
raw_input_ += '\n';
|
||||||
lines_ += 1;
|
lines_ += 1;
|
||||||
char cmd = raw_input_[0];
|
|
||||||
|
|
||||||
// All commands are a single character.
|
// Try to interpret it as a special command.
|
||||||
if ((raw_input_[1] != ' ')&&(raw_input_[1] != '\n')) {
|
if (lines_ == 1) {
|
||||||
action_ = DO_SYNTAX;
|
split_words();
|
||||||
syntax_ = "Commands are a single character followed by arguments";
|
if ((words_.size() >= 1)&&(words_[0].size() == 1)) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Analyze non-lua commands.
|
|
||||||
if ((cmd != 'r') && (cmd != 'l')) {
|
|
||||||
lua_expression_ = "";
|
|
||||||
if (lines_ == 1) {
|
|
||||||
action_ = DO_COMMAND;
|
action_ = DO_COMMAND;
|
||||||
split_words();
|
return;
|
||||||
} else {
|
|
||||||
action_ = DO_SYNTAX;
|
|
||||||
syntax_ = "slash command has more than one line";
|
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
words_.clear();
|
||||||
|
|
||||||
// Strip the leading punctuation from lua commands.
|
// Strip the leading punctuation from lua commands.
|
||||||
std::string partial;
|
std::string partial;
|
||||||
if (cmd == 'r') {
|
if (raw_input_[0] == '=') {
|
||||||
partial = std::string("return ") + raw_input_.substr(2);
|
partial = std::string("return ") + raw_input_.substr(1);
|
||||||
} else {
|
} else {
|
||||||
partial = raw_input_.substr(2);
|
partial = raw_input_;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Analyze lua expressions.
|
// Analyze lua expressions.
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ class LuaConsole {
|
|||||||
public:
|
public:
|
||||||
enum {
|
enum {
|
||||||
DO_NOTHING, // We need more input text.
|
DO_NOTHING, // We need more input text.
|
||||||
DO_COMMAND, // We have a valid non-lua command.
|
DO_COMMAND, // We have a valid slash command.
|
||||||
DO_LUA, // We have a valid lua expression.
|
DO_LUA, // We have a valid lua expression.
|
||||||
DO_SYNTAX, // We have a syntax error.
|
DO_SYNTAX, // We have a syntax error.
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "luastack.hpp"
|
#include "luastack.hpp"
|
||||||
|
#include "util.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
LuaSpecial LuaRegistry(LUA_REGISTRYINDEX);
|
LuaSpecial LuaRegistry(LUA_REGISTRYINDEX);
|
||||||
@@ -24,11 +25,10 @@ int LuaStack::collect_tagged_pointer(lua_State *L) {
|
|||||||
if (p==0) {
|
if (p==0) {
|
||||||
luaL_error(L, "lua deleter function received a non-userdata");
|
luaL_error(L, "lua deleter function received a non-userdata");
|
||||||
}
|
}
|
||||||
if (p->ptr == 0) {
|
if (p->ptr != 0) {
|
||||||
luaL_error(L, "lua object already deleted");
|
p->del(p->ptr);
|
||||||
|
p->ptr = 0;
|
||||||
}
|
}
|
||||||
p->del(p->ptr);
|
|
||||||
p->ptr = 0;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,7 +37,7 @@ void LuaStack::register_all_userdata(lua_State *L) {
|
|||||||
LuaStack LS(L, tab, lud);
|
LuaStack LS(L, tab, lud);
|
||||||
auto regs = LuaFunctionReg::all();
|
auto regs = LuaFunctionReg::all();
|
||||||
for (const LuaFunctionReg *r : regs) {
|
for (const LuaFunctionReg *r : regs) {
|
||||||
const std::string &name = r->get_name();
|
const std::string &name = util::tolower(r->get_name());
|
||||||
lua_CFunction tag = r->get_func();
|
lua_CFunction tag = r->get_func();
|
||||||
std::string mode = r->get_mode();
|
std::string mode = r->get_mode();
|
||||||
if (mode.find('t') != std::string::npos) { // Register type
|
if (mode.find('t') != std::string::npos) { // Register type
|
||||||
@@ -209,4 +209,18 @@ void LuaStack::check_nret(int xnret, int otop, int nret) const {
|
|||||||
luaL_error(L_, "expected %d return values", xnret);
|
luaL_error(L_, "expected %d return values", xnret);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LuaDefine(system_type, "f") {
|
||||||
|
LuaArg obj;
|
||||||
|
LuaRet tname;
|
||||||
|
LuaVar mt;
|
||||||
|
LuaStack LS(L, obj, tname, mt);
|
||||||
|
int type = LS.type(obj);
|
||||||
|
if (type == LUA_TUSERDATA) {
|
||||||
|
LS.getmetatable(mt, obj);
|
||||||
|
LS.getfield(tname, mt, "type");
|
||||||
|
} else {
|
||||||
|
LS.set(tname, lua_typename(L, type));
|
||||||
|
}
|
||||||
|
return LS.result();
|
||||||
|
}
|
||||||
|
|||||||
@@ -12,6 +12,20 @@
|
|||||||
|
|
||||||
namespace util {
|
namespace util {
|
||||||
|
|
||||||
|
std::string tolower(std::string input) {
|
||||||
|
for (int i = 0; i < int(input.size()); i++) {
|
||||||
|
input[i] = std::tolower(input[i]);
|
||||||
|
}
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string toupper(std::string input) {
|
||||||
|
for (int i = 0; i < int(input.size()); i++) {
|
||||||
|
input[i] = std::toupper(input[i]);
|
||||||
|
}
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
int64_t strtoint(const std::string &value, int64_t errval) {
|
int64_t strtoint(const std::string &value, int64_t errval) {
|
||||||
char *endptr;
|
char *endptr;
|
||||||
int64_t result = strtoll(value.c_str(), &endptr, 10);
|
int64_t result = strtoll(value.c_str(), &endptr, 10);
|
||||||
|
|||||||
@@ -14,6 +14,10 @@ namespace util {
|
|||||||
using stringvec = std::vector<std::string>;
|
using stringvec = std::vector<std::string>;
|
||||||
using stringset = std::set<std::string>;
|
using stringset = std::set<std::string>;
|
||||||
|
|
||||||
|
// String to lowercase/uppercase
|
||||||
|
std::string tolower(std::string input);
|
||||||
|
std::string toupper(std::string input);
|
||||||
|
|
||||||
// String to integer. Returns errval if the number is not parseable.
|
// String to integer. Returns errval if the number is not parseable.
|
||||||
int64_t strtoint(const std::string &value, int64_t errval);
|
int64_t strtoint(const std::string &value, int64_t errval);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user