add 'network' permission

This commit is contained in:
MihailRis
2025-12-08 19:21:50 +03:00
committed by ShiftyX1
parent 56d808e3e2
commit dd40780334
10 changed files with 67 additions and 27 deletions
+5 -1
View File
@@ -15,7 +15,11 @@ using namespace scripting;
static int l_start_debug_instance(lua::State* L) {
int port = lua::tointeger(L, 1);
if (port == 0) {
port = engine->getNetwork().findFreePort();
auto network = engine->getNetwork();
if (network == nullptr) {
throw std::runtime_error("project has no network permission");
}
port = network->findFreePort();
if (port == -1) {
throw std::runtime_error("could not find free port");
}
+14 -3
View File
@@ -133,7 +133,7 @@ static int l_post(lua::State* L, network::Network& network) {
auto headers = read_headers(L, 3);
int currentRequestId = request_id++;
engine->getNetwork().post(
network.post(
url,
string,
[currentRequestId](std::vector<char> bytes) {
@@ -240,7 +240,7 @@ static int l_recv(lua::State* L, network::Network& network) {
u64id_t id = lua::tointeger(L, 1);
int length = lua::tointeger(L, 2);
auto connection = engine->getNetwork().getConnection(id, false);
auto connection = network.getConnection(id, false);
if (connection == nullptr || connection->getTransportType() != network::TransportType::TCP) {
return 0;
@@ -519,11 +519,21 @@ static int l_pull_events(lua::State* L, network::Network& network) {
return 1;
}
int l_is_available(lua::State* L) {
return engine->getNetwork() != nullptr;
}
template <int(*func)(lua::State*, network::Network&)>
int wrap(lua_State* L) {
int result = 0;
try {
result = func(L, engine->getNetwork());
auto network = engine->getNetwork();
if (network == nullptr) {
throw std::runtime_error(
"network subsystem is not available in the project"
);
}
result = func(L, *network);
}
// transform exception with description into lua_error
catch (std::exception& e) {
@@ -543,6 +553,7 @@ const luaL_Reg networklib[] = {
{"get_total_upload", wrap<l_get_total_upload>},
{"get_total_download", wrap<l_get_total_download>},
{"find_free_port", wrap<l_find_free_port>},
{"is_available", lua::wrap<l_is_available>},
{"__pull_events", wrap<l_pull_events>},
{"__open_tcp", wrap<l_open_tcp>},
{"__open_udp", wrap<l_open_udp>},