feat add lua
All checks were successful
linux-mips64-gcc / linux-gcc-mips64el (Debug) (push) Successful in 1m53s
linux-x64-gcc / linux-gcc (Debug) (push) Successful in 1m46s
linux-mips64-gcc / linux-gcc-mips64el (Release) (push) Successful in 2m13s
linux-x64-gcc / linux-gcc (Release) (push) Successful in 1m49s

This commit is contained in:
tqcq 2024-03-31 19:44:27 +08:00
parent 17619b443b
commit 3c43c4ff58
5 changed files with 29335 additions and 13 deletions

View File

@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.10)
project(
minilua
VERSION 5.4.6
LANGUAGES C CXX)
add_library(minilua STATIC src/minilua.c)
target_include_directories(minilua PUBLIC src)

View File

@ -0,0 +1,2 @@
#define LUA_IMPL
#include "minilua.h"

29240
3party/minilua/src/minilua.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -24,6 +24,7 @@ target_include_directories(benchmark_main PUBLIC src/)
add_library(sled STATIC "")
add_subdirectory(3party/minilua EXCLUDE_FROM_ALL)
add_subdirectory(3party/gperftools EXCLUDE_FROM_ALL)
add_subdirectory(3party/asyncplusplus EXCLUDE_FROM_ALL)
# add_subdirectory(3party/cppuprofile EXCLUDE_FROM_ALL)
@ -88,19 +89,18 @@ target_sources(
src/sled/uri.cc)
# set(BUILD_RTTR_DYNAMIC OFF) set(BUILD_UNIT_TESTS OFF)
# set(BUILD_WITH_STATIC_RUNTIME_LIBS ON) set(BUILD_WITH_DOCUMENTATION OFF)
# add_subdirectory(3party/rttr EXCLUDE_FROM_ALL)
include(CheckCCompilerFlag)
check_c_compiler_flag("-Wl,--whole-archive" SUPPORT_COMPILE_WHOLE_ARCHIVE)
if(SUPPORT_COMPILE_WHOLE_ARCHIVE)
set(WHOLE_ARCHIVE_WRAPPER_START "-Wl,--whole-archive")
set(WHOLE_ARCHIVE_WRAPPER_END "-Wl,--no-whole-archive")
endif()
# add_subdirectory(3party/rttr EXCLUDE_FROM_ALL) include(CheckCCompilerFlag)
# check_c_compiler_flag("-Wl,--whole-archive" SUPPORT_COMPILE_WHOLE_ARCHIVE)
# if(SUPPORT_COMPILE_WHOLE_ARCHIVE) set(WHOLE_ARCHIVE_WRAPPER_START
# "-Wl,--whole-archive") set(WHOLE_ARCHIVE_WRAPPER_END "-Wl,--no-whole-archive")
# endif()
target_link_libraries(
sled
PUBLIC dl rpc_core fmt marl Async++
# protobuf::libprotobuf ${WHOLE_ARCHIVE_WRAPPER_START}
# tcmalloc_and_profiler_static
PUBLIC rpc_core fmt marl Async++ minilua
PRIVATE dl
# protobuf::libprotobuf ${WHOLE_ARCHIVE_WRAPPER_START}
# tcmalloc_and_profiler_static
# ${WHOLE_ARCHIVE_WRAPPER_END}
)
@ -148,7 +148,7 @@ function(sled_add_test)
sled/testing/test.h)
endif()
target_include_directories(${SLED_TEST_NAME} PRIVATE ${SLED_TEST_INC_DIRS})
target_link_libraries(${SLED_TEST_NAME} PRIVATE ${SLED_TEST_LIBS})
target_link_libraries(${SLED_TEST_NAME} PRIVATE ${SLED_TEST_LIBS} sled)
add_test(NAME ${SLED_TEST_NAME} COMMAND ${SLED_TEST_NAME})
endfunction()
@ -178,11 +178,11 @@ if(SLED_BUILD_TESTS)
src/sled/rx_test.cc
src/sled/uri_test.cc
LIBS
sled
test_main)
sled_add_test(NAME sled_symbolize_test SRCS
src/sled/debugging/symbolize_test.cc LIBS sled)
src/sled/debugging/symbolize_test.cc)
sled_add_test(NAME sled_lua_test SRCS tests/lua_test.cc LIBS test_main)
endif(SLED_BUILD_TESTS)
if(SLED_BUILD_FUZZ)

73
tests/lua_test.cc Normal file
View File

@ -0,0 +1,73 @@
#include <minilua.h>
#include <sled/random.h>
TEST_SUITE("lua")
{
TEST_CASE("Base Test")
{
lua_State *state = luaL_newstate();
CHECK(state != nullptr);
luaL_openlibs(state);
REQUIRE_EQ(lua_gettop(state), 0);
SUBCASE("add")
{
REQUIRE_EQ(LUA_OK, luaL_loadstring(state, "function add(a, b) return a+b end"));
// load script
REQUIRE_EQ(lua_pcall(state, 0, 0, 0), LUA_OK);
// push function
sled::Random rand(-1);
for (int i = 0; i < 100; i++) {
int a = rand.Rand(0, 100);
int b = rand.Rand(0, 100);
CHECK_EQ(LUA_TFUNCTION, lua_getglobal(state, "add"));
CHECK(lua_isfunction(state, -1));
REQUIRE_EQ(lua_gettop(state), 1);
lua_pushnumber(state, a);
lua_pushnumber(state, b);
CHECK_EQ(lua_gettop(state), 3);
CHECK_MESSAGE(lua_pcall(state, 2, 1, 0) == LUA_OK, "call add(a,b) error: ", lua_tostring(state, -1));
CHECK(lua_isnumber(state, -1));
CHECK_EQ(lua_tonumber(state, -1), a + b);
lua_pop(state, 1);
CHECK_EQ(lua_gettop(state), 0);
}
}
SUBCASE("dump all function")
{
const char *dump_func
= " local seen={}\n"
"\n"
" function dump(t,i)\n"
" seen[t]=true\n"
" local s={}\n"
" local n=0\n"
" for k in pairs(t) do\n"
" n=n+1\n"
" s[n]=k\n"
" end\n"
" table.sort(s)\n"
" for k,v in ipairs(s) do\n"
" -- print(i,v)\n"
" v=t[v]\n"
" if type(v)==\"table\" and not seen[v] then\n"
" dump(v,i..\"\\t\")\n"
" end\n"
" end\n"
" end\n"
"\n"
"dump(_G,\"\")";
REQUIRE_EQ(LUA_OK, luaL_loadstring(state, dump_func));
CHECK_EQ(lua_gettop(state), 1);
CHECK_EQ(LUA_TFUNCTION, lua_type(state, -1));
REQUIRE_EQ(LUA_OK, lua_pcall(state, 0, 0, 0));
}
lua_close(state);
}
}