From 5da725fa900da3f865a5c973ba5b89558cb609fa Mon Sep 17 00:00:00 2001 From: MCJack123 Date: Thu, 7 Dec 2023 13:56:08 -0500 Subject: [PATCH] Bytecode now disables in standards mode; fixed CI --- CraftOS-PC 2.vcxproj | 4 +++ craftos2-lua | 2 +- examples/ccemux.cpp | 10 +++--- examples/plugin_base.cpp | 4 +-- src/Computer.cpp | 2 ++ src/apis/config.cpp | 4 +++ src/apis/handles/http_handle.cpp | 14 ++------ src/apis/handles/http_handle.hpp | 8 +++++ src/apis/http.cpp | 27 +++++++------- src/util.hpp | 60 +++++++++++--------------------- 10 files changed, 62 insertions(+), 73 deletions(-) diff --git a/CraftOS-PC 2.vcxproj b/CraftOS-PC 2.vcxproj index cf0ec4f6..0a1164e8 100644 --- a/CraftOS-PC 2.vcxproj +++ b/CraftOS-PC 2.vcxproj @@ -367,6 +367,7 @@ true stdc11 26812;4005 + true false @@ -420,6 +421,7 @@ stdc11 26812;4005 stdcpp17 + true false @@ -471,6 +473,7 @@ stdc11 26812;4005 stdcpp17 + true false @@ -522,6 +525,7 @@ stdc17 26812;4005 stdcpp17 + true false diff --git a/craftos2-lua b/craftos2-lua index 4189d662..237efa5a 160000 --- a/craftos2-lua +++ b/craftos2-lua @@ -1 +1 @@ -Subproject commit 4189d6622fdd5f352ca11736461582408dcc8b51 +Subproject commit 237efa5acc4f6c1142da90751cc0cf8dff0ed9a6 diff --git a/examples/ccemux.cpp b/examples/ccemux.cpp index 698c1e33..8e6347e3 100644 --- a/examples/ccemux.cpp +++ b/examples/ccemux.cpp @@ -145,11 +145,11 @@ static int ccemux_getVersion(lua_State *L) { static int ccemux_openEmu(lua_State *L) { Computer * comp = get_comp(L); - int id = 0; + int id = luaL_optinteger(L, 1, -1); if (lua_isnumber(L, 1)) id = (int)lua_tointeger(L, 1); - else if (!lua_isnoneornil(L, 1)) luaL_typerror(L, 1, "number"); - else { + if (id < 0) { std::lock_guard lock(comp->peripherals_mutex); + id = 0; while (functions->getComputerById(id) != NULL) id++; } if (functions->attachPeripheral(comp, "computer_" + std::to_string(id), "computer", NULL, "") == NULL) lua_pushnil(L); @@ -233,7 +233,7 @@ static int ccemux_detach(lua_State *L) { return 0; } -static struct luaL_reg M[] = { +static struct luaL_Reg M[] = { {"getVersion", ccemux_getVersion}, {"openEmu", ccemux_openEmu}, {"closeEmu", ccemux_closeEmu}, @@ -252,7 +252,7 @@ static PluginInfo info("ccemux", 3); extern "C" { DLLEXPORT int luaopen_ccemux(lua_State *L) { - luaL_register(L, lua_tostring(L, 1), M); + luaL_newlib(L, M); functions->addVirtualMount(get_comp(L), emuROM, "/rom"); return 1; } diff --git a/examples/plugin_base.cpp b/examples/plugin_base.cpp index 7786e863..27863dda 100644 --- a/examples/plugin_base.cpp +++ b/examples/plugin_base.cpp @@ -15,7 +15,7 @@ extern "C" { // add your functions here... -static luaL_reg M[] = { +static luaL_Reg M[] = { // add functions here as {name, function}... {NULL, NULL} }; @@ -26,7 +26,7 @@ static PluginInfo info("myplugin"); extern "C" { // replace "myplugin" with the plugin name DLLEXPORT int luaopen_myplugin(lua_State *L) { - luaL_register(L, "myplugin", M); + luaL_newlib(L, M); return 1; } diff --git a/src/Computer.cpp b/src/Computer.cpp index 3e05ad16..3a14da19 100644 --- a/src/Computer.cpp +++ b/src/Computer.cpp @@ -479,6 +479,8 @@ void runComputer(Computer * self, const path_t& bios_name, const std::string& bi // Override the default loader to allow yielding from `load` lua_pushcfunction(L, yieldable_load); lua_setglobal(L, "load"); + // Disable bytecode + lua_setdisableflags(L, LUA_DISABLE_BYTECODE); } // Load any plugins available diff --git a/src/apis/config.cpp b/src/apis/config.cpp index 5b3e7cc5..3f198329 100644 --- a/src/apis/config.cpp +++ b/src/apis/config.cpp @@ -198,6 +198,10 @@ static int config_set(lua_State *L) { setConfigSetting(monitorsUseMouseEvents, boolean); setConfigSettingI(defaultWidth); setConfigSettingI(defaultHeight); + else if (strcmp(name, "standardsMode") == 0) { + config.standardsMode = lua_toboolean(L, 2); + lua_setdisableflags(L, config.standardsMode ? LUA_DISABLE_BYTECODE : 0); + } setConfigSetting(standardsMode, boolean); setConfigSetting(useHardwareRenderer, boolean); else if (strcmp(name, "preferredHardwareDriver") == 0) diff --git a/src/apis/handles/http_handle.cpp b/src/apis/handles/http_handle.cpp index 4ef2a2d2..86723d96 100644 --- a/src/apis/handles/http_handle.cpp +++ b/src/apis/handles/http_handle.cpp @@ -14,21 +14,11 @@ #include #include #include -#include "http_handle.hpp" #include "../../util.hpp" +#include "http_handle.hpp" using namespace Poco::Net; -struct http_handle_t { - std::string url; - HTTPClientSession * session; - HTTPResponse * handle; - std::istream * stream; - bool isBinary; - std::string failureReason; - http_handle_t(std::istream * s) : stream(s) {} -}; - struct http_res { std::string body; HTTPServerResponse * res; @@ -82,7 +72,7 @@ int http_handle_readLine(lua_State *L) { if (retval.empty() && handle->stream->eof()) return 0; if (lua_toboolean(L, 1)) retval += '\n'; else if (!retval.empty() && retval[retval.size()-1] == '\r') retval = retval.substr(0, retval.size()-1); - const std::string out = handle->isBinary ? retval : makeASCIISafe(retval.c_str(), retval.size()); + const std::string out = retval; lua_pushlstring(L, out.c_str(), out.length()); return 1; } diff --git a/src/apis/handles/http_handle.hpp b/src/apis/handles/http_handle.hpp index cd74bf54..26817704 100644 --- a/src/apis/handles/http_handle.hpp +++ b/src/apis/handles/http_handle.hpp @@ -13,6 +13,14 @@ extern "C" { #include } +struct http_handle_t { + std::string url; + std::string failureReason; + Poco::Net::HTTPClientSession * session; + Poco::Net::HTTPResponse * handle; + std::istream * stream; + http_handle_t(std::istream * s) : stream(s) {} +}; extern int http_handle_free(lua_State *L); extern int http_handle_close(lua_State *L); extern int http_handle_readAll(lua_State *L); diff --git a/src/apis/http.cpp b/src/apis/http.cpp index ba77d9f7..75957fe5 100644 --- a/src/apis/http.cpp +++ b/src/apis/http.cpp @@ -32,10 +32,10 @@ #include #include #include -#include "handles/http_handle.hpp" #include "../platform.hpp" #include "../runtime.hpp" #include "../util.hpp" +#include "handles/http_handle.hpp" #ifdef __ANDROID__ extern "C" {extern int Android_JNI_SetupThread(void);} @@ -58,15 +58,6 @@ struct http_param_t { double timeout; }; -struct http_handle_t { - std::string url; - HTTPClientSession * session; - HTTPResponse * handle; - std::istream * stream; - std::string failureReason; - http_handle_t(std::istream * s): stream(s) {} -}; - struct http_check_t { std::string url; std::string status; @@ -254,12 +245,20 @@ static void downloadThread(void* arg) { else if (uri.getScheme() == "http") { session = new HTTPClientSession(uri.getHost(), uri.getPort()); } else if (uri.getScheme() == "https") { - Context::Ptr context = new Context(Context::CLIENT_USE, "", Context::VERIFY_RELAXED, 9, true, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); - addSystemCertificates(context); + try { + Context::Ptr context = new Context(Context::CLIENT_USE, "", Context::VERIFY_RELAXED, 9, true, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); + addSystemCertificates(context); #if POCO_VERSION >= 0x010A0000 - context->disableProtocols(Context::PROTO_TLSV1_3); // Some sites break under TLS 1.3 - disable it to maintain compatibility until fixed (pocoproject/poco#3395) + context->disableProtocols(Context::PROTO_TLSV1_3); // Some sites break under TLS 1.3 - disable it to maintain compatibility until fixed (pocoproject/poco#3395) #endif - session = new HTTPSClientSession(uri.getHost(), uri.getPort(), context); + session = new HTTPSClientSession(uri.getHost(), uri.getPort(), context); + } catch (Poco::Exception &e) { + http_handle_t * err = new http_handle_t(NULL); + err->url = param->url; + err->failureReason = e.message(); + queueEvent(param->comp, http_failure, err); + goto downloadThread_finish; + } } else status = "Invalid protocol '" + uri.getScheme() + "'"; } if (!status.empty()) { diff --git a/src/util.hpp b/src/util.hpp index aa103f69..1914148e 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -139,46 +139,28 @@ class Value { }; // For get_comp -typedef union { - void *gc; - void *p; - lua_Number n; - int b; -} lua_Value; - -typedef struct lua_TValue { - lua_Value value; int tt; -} TValue; - struct lua_State { - void *next; - unsigned char tt; - unsigned char marked; - unsigned char status; - void* top; /* first free slot in the stack */ - void* base; /* base of current function */ - void *l_G; - void *ci; /* call info for current function */ - void* ctx; /* `savedpc' of current function, or context */ - void* stack_last; /* last free slot in the stack */ - void* stack; /* stack base */ - void *end_ci; /* points after end of ci array*/ - void *base_ci; /* array of CallInfo's */ - int stacksize; - int size_ci; /* size of array `base_ci' */ - unsigned short nCcalls; /* number of nested C calls */ - unsigned short baseCcalls; /* nested C calls when resuming coroutine */ - unsigned char hookmask; - unsigned char allowhook; - int basehookcount; - int hookcount; - lua_Hook hook; - TValue l_gt; /* table of globals */ - TValue env; /* temporary place for environments */ - void *openupval; /* list of open upvalues in this stack */ - void *gclist; - struct lua_longjmp *errorJmp; /* current error recover point */ - ptrdiff_t errfunc; /* current error handling function (stack index) */ + void *next; uint8_t tt; uint8_t marked; + uint8_t status; + void* top; /* first free slot in the stack */ + void* l_G; + void *ci; /* call info for current function */ + const int *oldpc; /* last pc traced */ + void* stack_last; /* last free slot in the stack */ + void* stack; /* stack base */ + int stacksize; + unsigned short nny; /* number of non-yieldable calls in stack */ + unsigned short nCcalls; /* number of nested C calls */ + uint8_t hookmask; + uint8_t allowhook; + int basehookcount; + int hookcount; + lua_Hook hook; + void *openupval; /* list of open upvalues in this stack */ + void *gclist; + struct lua_longjmp *errorJmp; /* current error recover point */ + ptrdiff_t errfunc; /* current error handling function (stack index) */ + void* base_ci; /* CallInfo for first level (C calling Lua) */ }; inline int log2i(int num) {