@@ -56,6 +56,7 @@ extern const luaL_Reg transformlib[];
|
||||
|
||||
// Lua Overrides
|
||||
extern int l_print(lua::State* L);
|
||||
extern int l_crc32(lua::State* L);
|
||||
|
||||
namespace lua {
|
||||
inline uint check_argc(lua::State* L, int a) {
|
||||
|
||||
@@ -84,6 +84,7 @@ static void create_libs(State* L, StateType stateType) {
|
||||
}
|
||||
|
||||
addfunc(L, "print", lua::wrap<l_print>);
|
||||
addfunc(L, "_crc32", lua::wrap<l_crc32>);
|
||||
}
|
||||
|
||||
void lua::init_state(State* L, StateType stateType) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <iostream>
|
||||
#include <zlib.h>
|
||||
|
||||
#include "libs/api_lua.hpp"
|
||||
|
||||
@@ -25,3 +26,27 @@ int l_print(lua::State* L) {
|
||||
*output_stream << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_crc32(lua::State* L) {
|
||||
auto value = lua::tointeger(L, 2);
|
||||
if (lua::isstring(L, 1)) {
|
||||
auto string = lua::tolstring(L, 1);
|
||||
return lua::pushinteger(
|
||||
L,
|
||||
crc32(
|
||||
value,
|
||||
reinterpret_cast<const ubyte*>(string.data()),
|
||||
string.length()
|
||||
)
|
||||
);
|
||||
} else if (auto bytearray = lua::touserdata<lua::LuaBytearray>(L, 1)) {
|
||||
auto& bytes = bytearray->data();
|
||||
return lua::pushinteger(L, crc32(value, bytes.data(), bytes.size()));
|
||||
} else if (lua::istable(L, 1)) {
|
||||
std::vector<ubyte> bytes;
|
||||
lua::read_bytes_from_table(L, 1, bytes);
|
||||
return lua::pushinteger(L, crc32(value, bytes.data(), bytes.size()));
|
||||
} else {
|
||||
throw std::runtime_error("invalid argument 1");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user