Skip to content

Commit

Permalink
Fixed issues when providing ropes to some functions
Browse files Browse the repository at this point in the history
  • Loading branch information
MCJack123 committed Apr 19, 2024
1 parent 3bdb768 commit 17e9b19
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Computer.cpp
Expand Up @@ -684,7 +684,7 @@ void runComputer(Computer * self, const path_t& bios_name, const std::string& bi
while (status == LUA_YIELD && self->running == 1) {
status = lua_resume(self->coro, NULL, narg);
if (status == LUA_YIELD) {
if (lua_gettop(self->coro) && lua_isstring(self->coro, -1)) narg = getNextEvent(self->coro, std::string(lua_tostring(self->coro, -1), lua_rawlen(self->coro, -1)));
if (lua_gettop(self->coro) && lua_isstring(self->coro, -1)) narg = getNextEvent(self->coro, tostring(self->coro, -1));
else narg = getNextEvent(self->coro, "");
} else if (status != 0 && self->running == 1) {
// Catch runtime error
Expand Down
8 changes: 4 additions & 4 deletions src/apis/config.cpp
Expand Up @@ -137,7 +137,7 @@ static int config_set(lua_State *L) {
// trying to deallocate unallocated/invalid memory. The only workaround I've found is to make the static variables
// dynamic, thus telling MSVC to keep its grubby hands off allocation/deallocation. It's an unfortunate situation,
// but there's really no way around it.
std::string * message = new std::string("A script is attempting to change the default mount mode to " + (lua_isnumber(L, 2) ? std::to_string(lua_tointeger(L, 2)) : std::string(lua_tostring(L, 2))) + ". This will allow any script to access any part of your REAL computer that is not blacklisted. Do you want to allow this change?");
std::string * message = new std::string("A script is attempting to change the default mount mode to " + (lua_isnumber(L, 2) ? std::to_string(lua_tointeger(L, 2)) : tostring(L, 2)) + ". This will allow any script to access any part of your REAL computer that is not blacklisted. Do you want to allow this change?");
data.message = message->c_str();
data.numbuttons = 2;
SDL_MessageBoxButtonData buttons[2];
Expand Down Expand Up @@ -165,7 +165,7 @@ static int config_set(lua_State *L) {
} else luaL_error(L, "Configuration option 'mount_mode' is protected");
} setConfigSetting(disable_lua51_features, boolean);
else if (strcmp(name, "default_computer_settings") == 0)
config.default_computer_settings = std::string(luaL_checkstring(L, 2), lua_rawlen(L, 2));
config.default_computer_settings = checkstring(L, 2);
setConfigSetting(logErrors, boolean);
setConfigSettingI(computerSpaceLimit);
setConfigSettingI(maximumFilesOpen);
Expand Down Expand Up @@ -205,7 +205,7 @@ static int config_set(lua_State *L) {
setConfigSetting(standardsMode, boolean);
setConfigSetting(useHardwareRenderer, boolean);
else if (strcmp(name, "preferredHardwareDriver") == 0)
config.preferredHardwareDriver = std::string(luaL_checkstring(L, 2), lua_rawlen(L, 2));
config.preferredHardwareDriver = checkstring(L, 2);
setConfigSetting(useVsync, boolean);
setConfigSetting(http_websocket_enabled, boolean);
setConfigSettingI(http_max_websockets);
Expand Down Expand Up @@ -246,7 +246,7 @@ static int config_set(lua_State *L) {
switch (std::get<0>(userConfig[name])) {
case 0: config.pluginData[name] = lua_toboolean(L, 2) ? "true" : "false"; break;
case 1: config.pluginData[name] = std::to_string(luaL_checkinteger(L, 2)); break;
case 2: config.pluginData[name] = std::string(luaL_checkstring(L, 2), lua_rawlen(L, 2)); break;
case 2: config.pluginData[name] = checkstring(L, 2); break;
case 3: return luaL_error(L, "Invalid type"); // maybe fix this later?
}
if (std::get<1>(userConfig[name]) != nullptr) {
Expand Down
2 changes: 1 addition & 1 deletion src/apis/http_emscripten.cpp
Expand Up @@ -281,7 +281,7 @@ int http_checkURL(lua_State *L) {
luaL_checkstring(L, 1);
http_param_t * param = new http_param_t;
param->comp = get_comp(L);
param->url = std::string(lua_tostring(L, 1), lua_rawlen(L, 1));
param->url = tostring(L, 1);
std::thread th(checkThread, param);
setThreadName(th, "HTTP Check Thread");
th.detach();
Expand Down
4 changes: 2 additions & 2 deletions src/apis/os.cpp
Expand Up @@ -26,15 +26,15 @@ static int os_getComputerLabel(lua_State *L) {
static int os_setComputerLabel(lua_State *L) {
lastCFunction = __func__;
Computer * comp = get_comp(L);
comp->config->label = std::string(luaL_optstring(L, 1, ""), lua_isstring(L, 1) ? lua_rawlen(L, 1) : 0);
comp->config->label = tostring(L, 1, "");
if (comp->term != NULL) comp->term->setLabel(comp->config->label.empty() ? "CraftOS Terminal: " + std::string(comp->isDebugger ? "Debugger" : "Computer") + " " + std::to_string(comp->id) : "CraftOS Terminal: " + asciify(comp->config->label));
return 0;
}

static int os_queueEvent(lua_State *L) {
lastCFunction = __func__;
Computer * computer = get_comp(L);
const std::string name = std::string(luaL_checkstring(L, 1), lua_rawlen(L, 1));
const std::string name = checkstring(L, 1);
if (!lua_checkstack(computer->paramQueue, 1)) luaL_error(L, "Could not allocate space for event");
lua_State *param = lua_newthread(computer->paramQueue);
lua_remove(L, 1);
Expand Down
2 changes: 1 addition & 1 deletion src/apis/peripheral.cpp
Expand Up @@ -17,7 +17,7 @@ static int peripheral_isPresent(lua_State *L) {
luaL_checkstring(L, 1);
Computer * computer = get_comp(L);
std::lock_guard<std::mutex> lock(computer->peripherals_mutex);
lua_pushboolean(L, computer->peripherals.find(std::string(lua_tostring(L, -1))) != computer->peripherals.end());
lua_pushboolean(L, computer->peripherals.find(tostring(L, -1)) != computer->peripherals.end());
return 1;
}

Expand Down
22 changes: 12 additions & 10 deletions src/peripheral/debugger.cpp
Expand Up @@ -601,23 +601,25 @@ static int debugger_lib_catch(lua_State *L) {
lastCFunction = __func__;
lua_getfield(L, LUA_REGISTRYINDEX, "_debugger");
debugger * dbg = (debugger*)lua_touserdata(L, -1);
if (std::string(lua_tostring(L, 1)) == "error") dbg->breakMask |= DEBUGGER_BREAK_FUNC_ERROR;
else if (std::string(lua_tostring(L, 1)) == "load") dbg->breakMask |= DEBUGGER_BREAK_FUNC_LOAD;
else if (std::string(lua_tostring(L, 1)) == "run") dbg->breakMask |= DEBUGGER_BREAK_FUNC_RUN;
else if (std::string(lua_tostring(L, 1)) == "resume") dbg->breakMask |= DEBUGGER_BREAK_FUNC_RESUME;
else if (std::string(lua_tostring(L, 1)) == "yield") dbg->breakMask |= DEBUGGER_BREAK_FUNC_YIELD;
std::string opt = tostring(L, 1);
if (opt == "error") dbg->breakMask |= DEBUGGER_BREAK_FUNC_ERROR;
else if (opt == "load") dbg->breakMask |= DEBUGGER_BREAK_FUNC_LOAD;
else if (opt == "run") dbg->breakMask |= DEBUGGER_BREAK_FUNC_RUN;
else if (opt == "resume") dbg->breakMask |= DEBUGGER_BREAK_FUNC_RESUME;
else if (opt == "yield") dbg->breakMask |= DEBUGGER_BREAK_FUNC_YIELD;
return 0;
}

static int debugger_lib_uncatch(lua_State *L) {
lastCFunction = __func__;
lua_getfield(L, LUA_REGISTRYINDEX, "_debugger");
debugger * dbg = (debugger*)lua_touserdata(L, -1);
if (std::string(lua_tostring(L, 1)) == "error") dbg->breakMask &= ~DEBUGGER_BREAK_FUNC_ERROR;
else if (std::string(lua_tostring(L, 1)) == "load") dbg->breakMask &= ~DEBUGGER_BREAK_FUNC_LOAD;
else if (std::string(lua_tostring(L, 1)) == "run") dbg->breakMask &= ~DEBUGGER_BREAK_FUNC_RUN;
else if (std::string(lua_tostring(L, 1)) == "resume") dbg->breakMask &= ~DEBUGGER_BREAK_FUNC_RESUME;
else if (std::string(lua_tostring(L, 1)) == "yield") dbg->breakMask &= ~DEBUGGER_BREAK_FUNC_YIELD;
std::string opt = tostring(L, 1);
if (opt == "error") dbg->breakMask &= ~DEBUGGER_BREAK_FUNC_ERROR;
else if (opt == "load") dbg->breakMask &= ~DEBUGGER_BREAK_FUNC_LOAD;
else if (opt == "run") dbg->breakMask &= ~DEBUGGER_BREAK_FUNC_RUN;
else if (opt == "resume") dbg->breakMask &= ~DEBUGGER_BREAK_FUNC_RESUME;
else if (opt == "yield") dbg->breakMask &= ~DEBUGGER_BREAK_FUNC_YIELD;
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion src/peripheral/printer.cpp
Expand Up @@ -268,7 +268,7 @@ int printer::getInkLevel(lua_State *L) {

int printer::setPageTitle(lua_State *L) {
lastCFunction = __func__;
title = std::string(luaL_checkstring(L, 1), lua_rawlen(L, 1));
title = checkstring(L, 1);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion src/terminal/RawTerminal.cpp
Expand Up @@ -724,7 +724,7 @@ static void rawInputLoop() {
lua_getfield(comp->rawFileStack, -1, "readAll");
lua_call(comp->rawFileStack, 0, 1);
if (lua_isnil(comp->rawFileStack, -1)) data = ""; // shouldn't happen
else data = std::string(lua_tostring(comp->rawFileStack, -1), lua_rawlen(comp->rawFileStack, -1));
else data = tostring(comp->rawFileStack, -1);
lua_pop(comp->rawFileStack, 1);
lua_getfield(comp->rawFileStack, -1, "close");
lua_call(comp->rawFileStack, 0, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/termsupport.cpp
Expand Up @@ -419,7 +419,7 @@ static void noDebuggerBreak(lua_State *L, Computer * computer, lua_Debug * ar) {
int status = lua_resume(coro, L, 2);
int narg;
while (status == LUA_YIELD) {
if (lua_isstring(coro, -1)) narg = getNextEvent(coro, std::string(lua_tostring(coro, -1), lua_rawlen(coro, -1)));
if (lua_isstring(coro, -1)) narg = getNextEvent(coro, tostring(coro, -1));
else narg = getNextEvent(coro, "");
status = lua_resume(coro, L, narg);
}
Expand Down

0 comments on commit 17e9b19

Please sign in to comment.