Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.11.0 #1002

Draft
wants to merge 42 commits into
base: main
Choose a base branch
from
Draft

0.11.0 #1002

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
9a7f8b8
Bump version
Sainan Nov 2, 2024
ef88b3b
Add table.invert
Sainan Nov 2, 2024
78487f3
Socket: Add basic capability to be a UDP client
Sainan Nov 2, 2024
061033e
Add socket.isudp
Sainan Nov 2, 2024
379f47e
Error if socket.starttls is used on UDP sockets
Sainan Nov 2, 2024
5eb6129
Add socket.udpserver
Sainan Nov 3, 2024
4103907
Rely on 4over6 as UDP client/sender on UNIX
Sainan Nov 3, 2024
47230b1
Handle DNS resolution for UDP in socket.connect
Sainan Nov 4, 2024
b515ba8
Fix incorrect usage of luaL_checkoption
Sainan Nov 5, 2024
1c66aed
Return 'parent, name' if no option given to io.part
Sainan Nov 5, 2024
0b08024
Add "unused" warning
Sainan Nov 5, 2024
7dcfecf
Enable 'unused' warning by default
Sainan Nov 10, 2024
7cc2787
Handle 'for as' value being unused
Sainan Nov 10, 2024
da4b851
Update Soup
Sainan Nov 12, 2024
e0a7061
Allow 'port' argument to socket.listen, socket.bind, & socket.udpserv…
Sainan Nov 12, 2024
72e5081
Revert "Deprecate 'export'"
Sainan Nov 12, 2024
8083b88
Make exported variables constants
Sainan Nov 12, 2024
b1636c2
Freeze exports table
Sainan Nov 12, 2024
a95f3fe
Assert that export table is frozen
Sainan Nov 12, 2024
4ad07f6
Add buffer library (new, append, tostring)
Sainan Nov 13, 2024
59cc2b0
Respect PLUTO_MEMORY_LIMIT for buffer library
Sainan Nov 13, 2024
9e29bc1
Add configurations to disable warnings by default
Sainan Nov 13, 2024
ecff609
Add simple preprocessor with substitution directive '$insteadof'
Sainan Nov 13, 2024
cd82537
Use line numbers to delimit preprocessor substitutions
Sainan Nov 13, 2024
8a4af4e
Add function-like aliases
Sainan Nov 13, 2024
8c8578c
Add preprocessor test
Sainan Nov 13, 2024
1bae9d7
tablib: Add deduplicate/dedup & deduplicated/deduped
Sainan Nov 14, 2024
6c693ca
Update Pluto.vcxproj.filters
Sainan Nov 18, 2024
6ab5d6a
Optimize equality check of compile-time constants
Sainan Nov 18, 2024
ff831d6
Don't implicitly convert bools to strings in function arguments
Sainan Nov 20, 2024
087b8b2
Update Soup
Sainan Nov 19, 2024
7d15e89
Default string.split's needle to ""
Sainan Dec 22, 2024
e9d31f6
Add tests for preprocessor
Sainan Dec 25, 2024
dd089c9
Allow aliases to be empty
Sainan Dec 25, 2024
51252a5
Add $assert
Sainan Jan 23, 2025
05cc60f
Add table.values
Sainan Jan 26, 2025
08da954
Allow nil as a default argument but raise bad-practice warning
Sainan Jan 29, 2025
fe7e9b2
Add optional 'binary' parameter to crypto.md5
Sainan Feb 8, 2025
3692072
Add ffi.alloc, ffi.write, & ffi.read
Sainan Feb 8, 2025
21664e2
Add crypto.crc32c
Sainan Feb 8, 2025
bb9c73e
FFI: Push pointers as lightuserdata instead of integer
Sainan Feb 10, 2025
afab401
Don't allow pipe expression after string literal for consistency
Sainan Feb 18, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 37 additions & 94 deletions Pluto.vcxproj

Large diffs are not rendered by default.

70 changes: 46 additions & 24 deletions Pluto.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@
<Filter>vendor\Soup\soup</Filter>
</ClCompile>
<ClCompile Include="src\lhttplib.cpp" />
<ClCompile Include="src\vendor\Soup\Intrin\aes_helper.cpp">
<Filter>vendor\Soup\Intrin</Filter>
</ClCompile>
<ClCompile Include="src\vendor\Soup\soup\adler32.cpp">
<Filter>vendor\Soup\soup</Filter>
</ClCompile>
Expand Down Expand Up @@ -265,9 +262,6 @@
<ClCompile Include="src\vendor\Soup\soup\DefaultRngInterface.cpp">
<Filter>vendor\Soup\soup</Filter>
</ClCompile>
<ClCompile Include="src\vendor\Soup\Intrin\hardware_rng.cpp">
<Filter>vendor\Soup\Intrin</Filter>
</ClCompile>
<ClCompile Include="src\vendor\Soup\soup\sha384.cpp">
<Filter>vendor\Soup\soup</Filter>
</ClCompile>
Expand All @@ -283,18 +277,9 @@
<ClCompile Include="src\vendor\Soup\soup\Server.cpp">
<Filter>vendor\Soup\soup</Filter>
</ClCompile>
<ClCompile Include="src\vendor\Soup\Intrin\crc32_intrin.cpp">
<Filter>vendor\Soup\Intrin</Filter>
</ClCompile>
<ClCompile Include="src\vendor\Soup\soup\alloc.cpp">
<Filter>vendor\Soup\soup</Filter>
</ClCompile>
<ClCompile Include="src\vendor\Soup\Intrin\sha1_transform.cpp">
<Filter>vendor\Soup\Intrin</Filter>
</ClCompile>
<ClCompile Include="src\vendor\Soup\Intrin\sha256_transform.cpp">
<Filter>vendor\Soup\Intrin</Filter>
</ClCompile>
<ClCompile Include="src\vendor\Soup\soup\os.cpp">
<Filter>vendor\Soup\soup</Filter>
</ClCompile>
Expand Down Expand Up @@ -349,6 +334,19 @@
<ClCompile Include="src\vendor\Soup\soup\QrCode.cpp">
<Filter>vendor\Soup\soup</Filter>
</ClCompile>
<ClCompile Include="src\vendor\Soup\soup\memAllocator.cpp">
<Filter>vendor\Soup\soup</Filter>
</ClCompile>
<ClCompile Include="src\vendor\Soup\soup\ResolveIpAddrTask.cpp">
<Filter>vendor\Soup\soup</Filter>
</ClCompile>
<ClCompile Include="src\lbufferlib.cpp" />
<ClCompile Include="src\vendor\Soup\soup\memAllocator.cpp">
<Filter>vendor\Soup\soup</Filter>
</ClCompile>
<ClCompile Include="src\vendor\Soup\soup\SocketAddr.cpp">
<Filter>vendor\Soup\soup</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\ltm.h" />
Expand Down Expand Up @@ -459,9 +457,6 @@
<ClInclude Include="src\vendor\Soup\soup\string.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\StringRefReader.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\TreeNode.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
Expand Down Expand Up @@ -561,9 +556,6 @@
<ClInclude Include="src\vendor\Soup\soup\deflate.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\DelayedCtor.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\deleter.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
Expand Down Expand Up @@ -1074,6 +1066,39 @@
<ClInclude Include="src\vendor\Soup\soup\BCanvas.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\ResolveIpAddrTask.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\memAllocator.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\memCAllocator.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\utility.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\MemoryRefReader.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\aes_intrin.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\crc32_intrin.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\sha256_intrin.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\sha1_intrin.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\MemoryRefReader.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
<ClInclude Include="src\vendor\Soup\soup\utility.hpp">
<Filter>vendor\Soup\soup</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="vendor">
Expand All @@ -1085,8 +1110,5 @@
<Filter Include="vendor\Soup\soup">
<UniqueIdentifier>{713ac335-27c4-4677-9da1-08c37c023a77}</UniqueIdentifier>
</Filter>
<Filter Include="vendor\Soup\Intrin">
<UniqueIdentifier>{f0adda43-f311-40e5-b4ec-284f248bad46}</UniqueIdentifier>
</Filter>
</ItemGroup>
</Project>
8 changes: 3 additions & 5 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ SYSLIBS=

MYCFLAGS=
MYLDFLAGS=
MYLIBS=-Lvendor/Soup/soup -lsoup -Lvendor/Soup/Intrin -lsoupintrin
MYLIBS=-Lvendor/Soup/soup -lsoup
MYOBJS=

# Special flags for compiler modules; -Os reduces code size.
Expand All @@ -40,7 +40,7 @@ PLATS= guess aix bsd freebsd generic linux linux-readline macosx posix solaris
LUA_A= libplutostatic.a
LUA_SO= libpluto.so
CORE_O= lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o lundump.o lvm.o lzio.o
LIB_O= lauxlib.o lbaselib.o lcorolib.o ldblib.o liolib.o lmathlib.o loadlib.o loslib.o lstrlib.o lcryptolib.o ltablib.o lutf8lib.o lassertlib.o lvector3lib.o lbase32.o lbase64.o ljson.o lurllib.o linit.o lstarlib.o lcatlib.o lhttplib.o lschedulerlib.o lsocketlib.o lbigint.o lxml.o lregex.o lffi.o lcanvas.o
LIB_O= lauxlib.o lbaselib.o lcorolib.o ldblib.o liolib.o lmathlib.o loadlib.o loslib.o lstrlib.o lcryptolib.o ltablib.o lutf8lib.o lassertlib.o lvector3lib.o lbase32.o lbase64.o ljson.o lurllib.o linit.o lstarlib.o lcatlib.o lhttplib.o lschedulerlib.o lsocketlib.o lbigint.o lxml.o lregex.o lffi.o lcanvas.o lbufferlib.o
BASE_O= $(CORE_O) $(LIB_O) $(MYOBJS)

LUA_T= pluto
Expand All @@ -63,9 +63,8 @@ o: $(ALL_O)
a: $(ALL_A)

$(LUA_A): $(BASE_O)
cd vendor/Soup/Intrin && $(MAKE) && cd ../..
cd vendor/Soup/soup && $(MAKE) && cd ../..
$(AR) $@ $(BASE_O) vendor/Soup/Intrin/*.o vendor/Soup/soup/*.o
$(AR) $@ $(BASE_O) vendor/Soup/soup/*.o
$(RANLIB) $@

$(LUA_SO): $(BASE_O) $(LUA_A)
Expand All @@ -81,7 +80,6 @@ test:
./$(LUA_T) -v

clean:
cd vendor/Soup/Intrin && $(MAKE) clean && cd ../..
cd vendor/Soup/soup && $(MAKE) clean && cd ../..
$(RM) $(ALL_T) $(ALL_O)

Expand Down
2 changes: 1 addition & 1 deletion src/lbaselib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ static int luaB_dofile (lua_State *L) {
}


static int luaB_assert (lua_State *L) {
int luaB_assert (lua_State *L) {
if (l_likely(lua_toboolean(L, 1))) /* condition is true? */
return lua_gettop(L); /* return all arguments */
else { /* error */
Expand Down
126 changes: 126 additions & 0 deletions src/lbufferlib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#define LUA_LIB
#include "lualib.h"

#include <cstdlib> // malloc, realloc, free
#include <memory> // destroy_at
#include <new> // bad_alloc

#include "vendor/Soup/soup/Buffer.hpp"

#include "ldo.h"
#include "lmem.h"

#ifdef PLUTO_MEMORY_LIMIT
struct PlutoSingleBlockAllocator : public soup::memAllocator
{
lua_State* L;
size_t size = 0;

PlutoSingleBlockAllocator()
: memAllocator(&allocateImpl, &reallocateImpl, &deallocateImpl)
{
}

static void* allocateImpl(memAllocator* inst, size_t size) /* SOUP_EXCAL */
{
printf("allocate %zu\n", size);
void* ptr = luaM_realloc_(static_cast<PlutoSingleBlockAllocator*>(inst)->L, nullptr, 0, size);
SOUP_IF_LIKELY (ptr)
{
static_cast<PlutoSingleBlockAllocator*>(inst)->size = size;
return ptr;
}
throw std::bad_alloc{};
}

static void* reallocateImpl(memAllocator* inst, void* addr, size_t new_size) /* SOUP_EXCAL */
{
printf("resize %zu to %zu\n", static_cast<PlutoSingleBlockAllocator*>(inst)->size, new_size);
addr = luaM_realloc_(static_cast<PlutoSingleBlockAllocator*>(inst)->L, addr, static_cast<PlutoSingleBlockAllocator*>(inst)->size, new_size);
SOUP_IF_LIKELY (addr)
{
static_cast<PlutoSingleBlockAllocator*>(inst)->size = new_size;
return addr;
}
throw std::bad_alloc{};
}

static void deallocateImpl(memAllocator* inst, void* addr) noexcept
{
luaM_free_(static_cast<PlutoSingleBlockAllocator*>(inst)->L, addr, static_cast<PlutoSingleBlockAllocator*>(inst)->size);
}
};
#endif

struct PlutoBuffer
{
#ifdef PLUTO_MEMORY_LIMIT
PlutoSingleBlockAllocator allocator;
#endif
soup::Buffer buffer;

PlutoBuffer()
#ifdef PLUTO_MEMORY_LIMIT
: buffer(allocator)
#endif
{
}
};

[[nodiscard]] static PlutoBuffer* checkbuffer (lua_State *L, int i) {
const auto buf = (PlutoBuffer*)luaL_checkudata(L, i, "pluto:buffer");
#ifdef PLUTO_MEMORY_LIMIT
buf->allocator.L = L;
#endif
return buf;
}

static int buffer_new (lua_State *L) {
new (lua_newuserdata(L, sizeof(PlutoBuffer))) PlutoBuffer{};
if (luaL_newmetatable(L, "pluto:buffer")) {
lua_pushliteral(L, "__index");
luaL_loadbuffer(L, "return require\"pluto:buffer\"", 28, 0);
lua_call(L, 0, 1);
lua_settable(L, -3);
lua_pushliteral(L, "__gc");
lua_pushcfunction(L, [](lua_State *L) {
std::destroy_at<>(checkbuffer(L, 1));
return 0;
});
lua_settable(L, -3);
}
lua_setmetatable(L, -2);
return 1;
}

static int buffer_append (lua_State *L) {
size_t size;
const char* data = luaL_checklstring(L, 2, &size);
try {
checkbuffer(L, 1)->buffer.append(data, size);
}
catch (std::bad_alloc&) {
luaD_throw(L, LUA_ERRMEM);
}
return 0;
}

static int buffer_tostring (lua_State *L) {
soup::Buffer& buf = checkbuffer(L, 1)->buffer;
try {
lua_pushlstring(L, (const char*)buf.data(), buf.size());
}
catch (std::bad_alloc&) {
luaD_throw(L, LUA_ERRMEM);
}
return 1;
}

static const luaL_Reg funcs_buffer[] = {
{"new", buffer_new},
{"append", buffer_append},
{"tostring", buffer_tostring},
{nullptr, nullptr}
};

PLUTO_NEWLIB(buffer)
19 changes: 17 additions & 2 deletions src/lcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1728,6 +1728,22 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
lua_assert(e1->k == VK || e1->k == VKINT || e1->k == VKFLT);
swapexps(e1, e2);
}
if (vkisconst(e1->k) && vkisconst(e2->k) && !hasjumps(e1) && !hasjumps(e2)
&& (e1->k != VKINT || e2->k != VKFLT) && (e1->k != VKFLT || e2->k != VKINT) /* don't optimize comparisons between VKINT and VKFLT */
) {
bool eq = false;
if (e1->k == e2->k) {
switch (e1->k) {
default: eq = true; break;
case VKFLT: eq = (e1->u.nval == e2->u.nval); break;
case VKINT: eq = (e1->u.ival == e2->u.ival); break;
case VKSTR: eq = (e1->u.strval == e2->u.strval); break;
case VCONST: eq = (e1->u.info == e2->u.info); break;
}
}
e1->k = ((eq ^ (opr != OPR_EQ)) ? VTRUE : VFALSE);
return;
}
r1 = luaK_exp2anyreg(fs, e1); /* 1st expression must be in register */
if (isSCnumber(e2, &im, &isfloat)) {
op = OP_EQI;
Expand Down Expand Up @@ -1816,9 +1832,8 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
break;
}
case OPR_EQ: case OPR_NE: {
if (!tonumeral(v, NULL))
if (!vkisconst(v->k) || hasjumps(v))
exp2RK(fs, v);
/* else keep numeral, which may be an immediate operand */
break;
}
case OPR_LT: case OPR_LE:
Expand Down
29 changes: 24 additions & 5 deletions src/lcryptolib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "vendor/Soup/soup/adler32.hpp"
#include "vendor/Soup/soup/aes.hpp"
#include "vendor/Soup/soup/crc32.hpp"
#include "vendor/Soup/soup/crc32c.hpp"
#include "vendor/Soup/soup/deflate.hpp"
#include "vendor/Soup/soup/HardwareRng.hpp"
#include "vendor/Soup/soup/ripemd160.hpp"
Expand Down Expand Up @@ -218,13 +219,20 @@ static int sdbm(lua_State *L)
static int md5(lua_State *L)
{
size_t len;
unsigned char buffer[16];
const auto str = luaL_checklstring(L, 1, &len);
const bool binary = lua_istrue(L, 2);

unsigned char buffer[16];
md5_fn((unsigned char*)str, (int)len, buffer);

char hexbuff[32];
soup::string::bin2hexAt(hexbuff, (const char*)buffer, sizeof(buffer), soup::string::charset_hex_lower);
lua_pushlstring(L, hexbuff, sizeof(hexbuff));

if (binary) {
lua_pushlstring(L, (const char*)buffer, sizeof(buffer));
}
else {
char hexbuff[32];
soup::string::bin2hexAt(hexbuff, (const char*)buffer, sizeof(buffer), soup::string::charset_hex_lower);
lua_pushlstring(L, hexbuff, sizeof(hexbuff));
}
return 1;
}

Expand All @@ -249,6 +257,16 @@ static int crc32(lua_State *L)
}


static int crc32c(lua_State *L)
{
size_t len;
const auto text = luaL_checklstring(L, 1, &len);
const auto hash = soup::crc32c::hash((const uint8_t*)text, len, (uint32_t)luaL_optinteger(L, 2, 0));
lua_pushinteger(L, hash);
return 1;
}


// The hashing function used inside Lua. Honorary addition.
// Basically a slightly different DJB2.
static int lua(lua_State *L)
Expand Down Expand Up @@ -751,6 +769,7 @@ static const luaL_Reg funcs_crypto[] = {
{"sha512", l_hashwithdigest<soup::sha512>},
{"lua", lua},
{"crc32", crc32},
{"crc32c", crc32c},
{"lookup3", lookup3},
{"md5", md5},
{"sdbm", sdbm},
Expand Down
Loading
Loading