немножко раскидал функции по отдельным файлам && add helloWorld example

This commit is contained in:
2025-04-05 20:29:18 +05:00
parent 5dc9f2ca31
commit 96eb2c55d3
13 changed files with 410 additions and 267 deletions

View File

@@ -2,20 +2,17 @@
syscalls
*/
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ksys.h>
#include "syscalls.h"
#include "scancodes.h"
#include "ARP_entry.h"
#include "systemColors.h"
#include "socket.h"
/*
@@ -42,53 +39,7 @@ static ksys_pos_t syscalls_screenSizeCache = { 0 };
*/
inline void syscalls_ReturnIntegerOrNil(LUA_INTEGER value, lua_State* L)
{
if (value == -1)
{
lua_pushnil(L);
}
else
{
lua_pushinteger(L, value);
}
}
inline void syscalls_ReturnIntegerValueOrNil(LUA_INTEGER cond, LUA_INTEGER value, lua_State* L)
{
if (cond == -1)
{
lua_pushnil(L);
}
else
{
lua_pushinteger(L, value);
}
}
inline void syscalls_ReturnTrueOrNil(LUA_INTEGER value, lua_State* L)
{
if (value == -1)
{
lua_pushnil(L);
}
else
{
lua_pushboolean(L, true);
}
}
inline void syscalls_ReturnStringOrNil(LUA_INTEGER cond, const char* value, lua_State* L)
{
if (cond == -1)
{
lua_pushnil(L);
}
else
{
lua_pushstring(L, value);
}
}
@@ -287,6 +238,23 @@ static int syscalls_drawPixel(lua_State* L)
return 0;
}
static int syscalls_WindowMsg(lua_State* L)
{
int event = luaL_checkinteger(L, 1);
int code = luaL_checkinteger(L, 2);
int ret;
asm_inline(
"int $0x40"
:"=a"(ret)
: "a"(72), "b"(1), "c"(event), "d"(code)
);
syscalls_ReturnTrueOrNil(ret, L);
return 1;
}
static int syscalls_threadInfo(lua_State* L)
{
ksys_thread_t t;
@@ -684,7 +652,7 @@ static int syscalls_getSystemColors(lua_State* L)
static int syscalls_SetSystemColors(lua_State* L)
{
ksys_colors_table_t* t = (ksys_colors_table_t*)lua_touserdata(L, 1);
ksys_colors_table_t* t = (ksys_colors_table_t*)luaL_checkudata(L, 1, syscalls_SystemColors_metatable_name);
asm_inline(
"int $0x40" ::"a"(48), "b"(2), "c"(t), "d"(40));
@@ -1637,17 +1605,25 @@ static int syscalls_ReadARPEntries(lua_State* L)
return 1;
}
/**
* Return ARP entry
*/
static int syscalls_ReadARPEntry(lua_State* L)
{
uint32_t eax;
uint8_t device = luaL_checkinteger(L, 1);
uint32_t entryNum = luaL_checkinteger(L, 2);
struct ARP_entry* buffer = syscalls_pushARPEntry(L);
struct ARP_entry buffer;
asm_inline(
"int $0x40"
: "=a"(eax)
: "a"(76), "b"((ARP << 24) | (device << 8) | 3), "c"(entryNum), "D"(buffer));
: "a"(76), "b"((ARP << 24) | (device << 8) | 3), "c"(entryNum), "D"(&buffer));
if (eax == -1)
lua_pushnil(L);
else
memcpy(syscalls_pushARPEntry(L), &buffer, sizeof(struct ARP_entry));
return 1;
}
@@ -1657,7 +1633,7 @@ static int syscalls_AddARPEntry(lua_State* L)
uint32_t eax;
uint8_t device = luaL_checkinteger(L, 1);
uint32_t entryNum = luaL_checkinteger(L, 2);
struct ARP_entry* buffer = (struct ARP_entry*)lua_touserdata(L, 3);
struct ARP_entry* buffer = (struct ARP_entry*)luaL_checkudata(L, 3, syscalls_ARPEntry_metatable_name);
asm_inline(
"int $0x40"
@@ -1715,211 +1691,39 @@ static int syscalls_ReadARPConflicts(lua_State* L)
return 1;
}
/* Сокеты */
static int syscalls_OpenSocket(lua_State* L)
static int syscalls_DebugPuts(lua_State* L)
{
int32_t socketNum;
uint32_t errorCode;
_ksys_debug_puts(luaL_checkstring(L, 1));
uint32_t family = luaL_checkinteger(L, 1);
uint32_t type = luaL_checkinteger(L, 2);
uint32_t protocol = luaL_checkinteger(L, 3);
return 0;
}
static int syscalls_DebugPutc(lua_State* L)
{
_ksys_debug_putc(*luaL_checkstring(L, 1));
return 0;
}
struct DebugMessageArea
{
int Size;
int Used;
char data[];
};
static int syscalls_SetMessageArea(lua_State* L)
{
struct DebugMessageArea* p = luaL_checkinteger(L, 1);
asm_inline(
"int $0x40"
:"=a"(socketNum), "=b"(errorCode)
: "a"(77), "b"(0), "c"(family), "d"(type), "S"(protocol)
:: "a"(69), "b"(0)
);
if (socketNum == -1)
{
lua_pushnil(L); // Push socketNum
lua_pushnumber(L, socketNum); // Push error Code
}
else
{
lua_pushnumber(L, socketNum); // Push socketNum
lua_pushnil(L); // Push error code
}
return 2;
return 0;
}
static int syscalls_CloseSocket(lua_State* L)
{
uint32_t eax;
uint32_t errorCode;
uint32_t socketNum = luaL_checkinteger(L, 1);
asm_inline(
"int $0x40"
: "=a"(eax), "=b"(errorCode)
: "a"(77), "b"(1), "c"(socketNum));
syscalls_ReturnIntegerValueOrNil(eax, errorCode, L);
return 1;
}
static int syscalls_Bind(lua_State* L)
{
uint32_t eax;
uint32_t errorCode;
uint32_t socketNum = luaL_checkinteger(L, 1);
uint32_t sockaddr = luaL_checkinteger(L, 2);
uint32_t sockaddrLen = luaL_checkinteger(L, 3);
asm_inline(
"int $0x40"
: "=a"(eax), "=b"(errorCode)
: "a"(77), "b"(2), "c"(socketNum), "d"(sockaddr), "S"(sockaddrLen));
syscalls_ReturnIntegerValueOrNil(eax, errorCode, L);
return 1;
}
static int syscalls_Listen(lua_State* L)
{
uint32_t eax;
uint32_t errorCode;
uint32_t socketNum = luaL_checkinteger(L, 1);
uint32_t backlog = luaL_checkinteger(L, 2);
asm_inline(
"int $0x40"
: "=a"(eax), "=b"(errorCode)
: "a"(77), "b"(3), "c"(socketNum), "d"(backlog));
syscalls_ReturnIntegerValueOrNil(eax, errorCode, L);
return 1;
}
static int syscalls_Connect(lua_State* L)
{
uint32_t eax;
uint32_t errorCode;
uint32_t socketNum = luaL_checkinteger(L, 1);
uint32_t sockaddr = luaL_checkinteger(L, 2);
uint32_t sockaddrLen = luaL_checkinteger(L, 3);
asm_inline(
"int $0x40"
: "=a"(eax), "=b"(errorCode)
: "a"(77), "b"(4), "c"(socketNum), "d"(sockaddr), "S"(sockaddrLen));
syscalls_ReturnIntegerValueOrNil(eax, errorCode, L);
return 1;
}
static int syscalls_Accept(lua_State* L)
{
uint32_t eax;
uint32_t errorCode;
uint32_t socketNum = luaL_checkinteger(L, 1);
uint32_t sockaddr = luaL_checkinteger(L, 2);
uint32_t sockaddrLen = luaL_checkinteger(L, 3);
asm_inline(
"int $0x40"
: "=a"(eax), "=b"(errorCode)
: "a"(77), "b"(5), "c"(socketNum), "d"(sockaddr), "S"(sockaddrLen));
syscalls_ReturnIntegerValueOrNil(eax, errorCode, L);
return 1;
}
static int syscalls_Send(lua_State* L)
{
uint32_t eax;
uint32_t errorCode;
uint32_t socketNum = luaL_checkinteger(L, 1);
uint32_t buffer = luaL_checkinteger(L, 2);
uint32_t bufferLen = luaL_checkinteger(L, 3);
uint32_t flags = luaL_checkinteger(L, 4);
asm_inline(
"int $0x40"
: "=a"(eax), "=b"(errorCode)
: "a"(77), "b"(6), "c"(socketNum), "d"(buffer), "S"(bufferLen), "D"(flags));
syscalls_ReturnIntegerValueOrNil(eax, errorCode, L);
return 1;
}
static int syscalls_Receive(lua_State* L)
{
uint32_t eax;
uint32_t errorCode;
uint32_t socketNum = luaL_checkinteger(L, 1);
uint32_t buffer = luaL_checkinteger(L, 2);
uint32_t bufferLen = luaL_checkinteger(L, 3);
uint32_t flags = luaL_checkinteger(L, 4);
asm_inline(
"int $0x40"
: "=a"(eax), "=b"(errorCode)
: "a"(77), "b"(7), "c"(socketNum), "d"(buffer), "S"(bufferLen), "D"(flags));
syscalls_ReturnIntegerValueOrNil(eax, errorCode, L);
return 1;
}
static int syscalls_GetSocketOptions(lua_State* L)
{
uint32_t eax;
uint32_t errorCode;
uint32_t socketNum = luaL_checkinteger(L, 1);
uint32_t optstruct = luaL_checkinteger(L, 2);
asm_inline(
"int $0x40"
: "=a"(eax), "=b"(errorCode)
: "a"(77), "b"(8), "c"(socketNum), "d"(optstruct));
syscalls_ReturnIntegerValueOrNil(eax, errorCode, L);
return 1;
}
static int syscalls_GetPairSocket(lua_State* L)
{
int32_t firstSocketNum;
uint32_t secondSocketNum;
asm_inline(
"int $0x40"
: "=a"(firstSocketNum), "=b"(secondSocketNum)
: "a"(77), "b"(9));
if (firstSocketNum == -1)
{
lua_pushnil(L);
lua_pushinteger(L, secondSocketNum);
}
else
{
lua_pushinteger(L, firstSocketNum);
lua_pushinteger(L, secondSocketNum);
}
return 2;
}
/*
** functions for 'syscalls' library
*/
@@ -1969,7 +1773,7 @@ static const luaL_Reg syscallsLib[] = {
{"ReadPoint", syscalls_ReadPoint},
/* keyboard funcs */
{"SetKeyInputMode", syscalls_setKeyInputMode},
{"GetKeyInputMouse", syscalls_getKeyInputMode},
{"GetKeyInputMode", syscalls_getKeyInputMode},
{"getKey", syscalls_getKey},
{"getControlKeyState", syscalls_getControlKeyState},
{"SetHotkey", syscalls_SetHotkey},
@@ -2052,7 +1856,11 @@ static const luaL_Reg syscallsLib[] = {
{"Receive", syscalls_Receive},
{"GetSocketOptions", syscalls_GetSocketOptions},
{"GetPairSocket", syscalls_GetPairSocket},
{NULL, NULL} };
/* Debug */
{"DebugPuts", syscalls_DebugPuts},
{ "DebugPutc", syscalls_DebugPutc },
{NULL, NULL}
};
static inline void syscalls_push_events(lua_State* L)
{