More work refactoring ckint/tryint/isint

This commit is contained in:
2024-03-15 12:50:08 -04:00
parent 26d0715deb
commit 3d6b5224f3
4 changed files with 64 additions and 51 deletions

View File

@@ -623,59 +623,66 @@ void HttpClientRequest::set_content(const eng::string &content) {
}
void HttpClientRequest::set_verify_certificate(LuaCoreStack &LS, LuaSlot val) {
if (!LS.isboolean(val)) {
auto tval = LS.tryboolean(val);
if (!tval) {
check_fail(util::ss("verifycertificate must be a boolean"));
return;
}
set_verify_certificate(LS.ckboolean(val));
set_verify_certificate(*tval);
}
void HttpClientRequest::set_method(LuaCoreStack &LS, LuaSlot val) {
if (!LS.isstring(val)) {
auto tval = LS.trystring(val);
if (!tval) {
check_fail(util::ss("method must be a string"));
return;
}
set_method(LS.ckstring(val));
set_method(*tval);
}
void HttpClientRequest::set_host(LuaCoreStack &LS, LuaSlot val) {
if (!LS.isstring(val)) {
auto tval = LS.trystring(val);
if (!tval) {
check_fail(util::ss("host must be a string"));
return;
}
set_host(LS.ckstring(val));
set_host(*tval);
}
void HttpClientRequest::set_port(LuaCoreStack &LS, LuaSlot val) {
if (!LS.isint(val)) {
auto tval = LS.tryint(val);
if (!tval) {
check_fail(util::ss("port must be an int"));
return;
}
set_port(LS.ckint(val));
set_port(*tval);
}
void HttpClientRequest::set_path(LuaCoreStack &LS, LuaSlot val) {
if (!LS.isstring(val)) {
auto tval = LS.trystring(val);
if (!tval) {
check_fail(util::ss("path must be a string"));
return;
}
set_path(LS.ckstring(val));
set_path(*tval);
}
void HttpClientRequest::set_param(LuaCoreStack &LS, LuaSlot key, LuaSlot val) {
if (!LS.isstring(key)) {
auto tkey = LS.trystring(key);
auto tval = LS.trystring(val);
if (!tkey) {
check_fail(util::ss("url parameter key must be a string"));
return;
}
if (!LS.isstring(val)) {
if (!tval) {
check_fail(util::ss("url parameter val must be a string"));
return;
}
set_param(LS.ckstring(key), LS.ckstring(val));
set_param(*tkey, *tval);
}
void HttpClientRequest::set_params(LuaCoreStack &LS0, LuaSlot tab) {
if (!LS0.istable(tab)) {
if (!LS0.trytable(tab)) {
check_fail(util::ss("params must be a table"));
return;
}
@@ -688,27 +695,30 @@ void HttpClientRequest::set_params(LuaCoreStack &LS0, LuaSlot tab) {
}
void HttpClientRequest::set_url(LuaCoreStack &LS, LuaSlot val) {
if (!LS.isstring(val)) {
auto tval = LS.trystring(val);
if (!tval) {
check_fail(util::ss("url must be a string"));
return;
}
set_url(LS.ckstring(val));
set_url(*tval);
}
void HttpClientRequest::set_mime_type(LuaCoreStack &LS, LuaSlot val) {
if (!LS.isstring(val)) {
auto tval = LS.trystring(val);
if (!tval) {
check_fail(util::ss("mime type must be a string"));
return;
}
set_mime_type(LS.ckstring(val));
set_mime_type(*tval);
}
void HttpClientRequest::set_content(LuaCoreStack &LS, LuaSlot val) {
if (!LS.isstring(val)) {
auto tval = LS.trystring(val);
if (!tval) {
check_fail(util::ss("content must be a string"));
return;
}
set_content(LS.ckstring(val));
set_content(*tval);
}
void HttpClientRequest::set_jsonvalue(LuaCoreStack &LS, LuaSlot val) {
@@ -976,15 +986,16 @@ void HttpServerResponse::set_content(const eng::string &content) {
void HttpServerResponse::set_status(LuaCoreStack &LS, LuaSlot val) {
int status = 0;
if (LS.isstring(val)) {
eng::string s = LS.ckstring(val);
status = status_code_from_string(s);
auto vstring = LS.trystring(val);
auto vint = LS.tryint(val);
if (vstring) {
status = status_code_from_string(*vstring);
if (status == 0) {
check_fail(util::ss("unrecognized status code: ", s));
check_fail(util::ss("unrecognized status code: ", *vstring));
return;
}
} else if (LS.isint(val)) {
status = LS.ckint(val);
} else if (vint) {
status = *vint;
} else {
check_fail(util::ss("status must be an integer"));
return;
@@ -993,27 +1004,30 @@ void HttpServerResponse::set_status(LuaCoreStack &LS, LuaSlot val) {
}
void HttpServerResponse::set_max_age(LuaCoreStack &LS, LuaSlot val) {
if (!LS.isint(val)) {
auto vint = LS.tryint(val);
if (!vint) {
check_fail(util::ss("max-age must be an int"));
return;
}
set_max_age(LS.ckint(val));
set_max_age(*vint);
}
void HttpServerResponse::set_mime_type(LuaCoreStack &LS, LuaSlot val) {
if (!LS.isstring(val)) {
auto vstring = LS.trystring(val);
if (!vstring) {
check_fail(util::ss("mime type must be a string"));
return;
}
set_mime_type(LS.ckstring(val));
set_mime_type(*vstring);
}
void HttpServerResponse::set_content(LuaCoreStack &LS, LuaSlot val) {
if (!LS.isstring(val)) {
auto vstring = LS.trystring(val);
if (!vstring) {
check_fail(util::ss("content must be a string"));
return;
}
set_content(LS.ckstring(val));
set_content(*vstring);
}
void HttpServerResponse::set_jsonvalue(LuaCoreStack &LS, LuaSlot val) {

View File

@@ -120,18 +120,18 @@ public:
// assign one.
int tabnum = 0;
LS.rawget(lchpos, tabchpos_, value);
if (!LS.isnumber(lchpos)) {
auto chpos = LS.tryint(lchpos);
if (!chpos) {
// First time. Record the character position where the
// table first appears in the output stream.
LS.rawset(tabchpos_, value, int((*output_).tellp()));
} else {
int chpos = LS.ckint(lchpos);
tabnum = chpos_to_tabnum_[chpos];
tabnum = chpos_to_tabnum_[*chpos];
if (tabnum == 0) {
// Second time. The table is already in the output,
// but it hasn't been assigned a number. Assign one.
tabnum = next_id_++;
chpos_to_tabnum_[chpos] = tabnum;
chpos_to_tabnum_[*chpos] = tabnum;
}
}

View File

@@ -44,20 +44,19 @@ static bool equivalent_values(LuaCoreStack &MLS, LuaSlot mval, LuaSlot mtnmap,
switch (MLS.xtype(mval)) {
case LUA_TBOOLEAN: {
if (SLS.type(sval) != LUA_TBOOLEAN) return false;
return MLS.ckboolean(mval) == SLS.ckboolean(sval);
return MLS.tryboolean(mval) == SLS.tryboolean(sval);
}
case LUA_TNUMBER: {
if (SLS.type(sval) != LUA_TNUMBER) return false;
return MLS.cknumber(mval) == SLS.cknumber(sval);
return MLS.trynumber(mval) == SLS.trynumber(sval);
}
case LUA_TSTRING: {
// This could be faster if I used lua_tolstring directly.
if (SLS.type(sval) != LUA_TSTRING) return false;
return MLS.ckstring(mval) == SLS.ckstring(sval);
return MLS.trystring(mval) == SLS.trystring(sval);
}
case LUA_TLIGHTUSERDATA: {
if (SLS.type(sval) != LUA_TLIGHTUSERDATA) return false;
return MLS.cktoken(mval) == SLS.cktoken(sval);
return MLS.trytoken(mval) == SLS.trytoken(sval);
}
case LUA_TFUNCTION: {
// Cannot really compare. Just return true if the types match.
@@ -95,22 +94,22 @@ static void transmit_value(LuaCoreStack &MLS, LuaSlot mval, LuaSlot mtnmap, Stre
switch (MLS.xtype(mval)) {
case LUA_TBOOLEAN: {
sb->write_uint8(LUA_TBOOLEAN);
sb->write_bool(MLS.ckboolean(mval));
sb->write_bool(*MLS.tryboolean(mval));
return;
}
case LUA_TNUMBER: {
sb->write_uint8(LUA_TNUMBER);
sb->write_double(MLS.cknumber(mval));
sb->write_double(*MLS.trynumber(mval));
return;
}
case LUA_TSTRING: {
sb->write_uint8(LUA_TSTRING);
sb->write_string(MLS.ckstring(mval));
sb->write_string(*MLS.trystring(mval));
return;
}
case LUA_TLIGHTUSERDATA: {
sb->write_uint8(LUA_TLIGHTUSERDATA);
sb->write_uint64(MLS.cktoken(mval).value);
sb->write_uint64((*MLS.trytoken(mval)).value);
return;
}
case LUA_TT_GENERAL: {

View File

@@ -139,13 +139,13 @@ void World::pair_lua_tables(const IdVector &basis, lua_State *master) {
if (MLS.isnumber(midx)) continue;
// If the synch table doesn't have a number, skip.
SLS.rawget(sidx, stnmap, stab);
if (!SLS.isnumber(sidx)) continue;
int idx = SLS.ckinteger(sidx);
assert((idx >= 1) && (idx <= s_ntables));
auto idx = SLS.trynumber(sidx);
if (!idx) continue;
assert((*idx >= 1) && (*idx <= s_ntables));
// Pair the tables.
MLS.rawset(mtnmap, mtab, idx);
MLS.rawset(mntmap, idx, mtab);
paired[idx] = true;
MLS.rawset(mtnmap, mtab, *idx);
MLS.rawset(mntmap, *idx, mtab);
paired[*idx] = true;
// Potentially pair the metatables.
MLS.getmetatable(mval, mtab);
if (MLS.istable(mval)) {