Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yeshan333/calling-c-func-in-lua-demo
For comparing the difference of the C API luaL_tolstring of two lua versions
https://github.com/yeshan333/calling-c-func-in-lua-demo
c lua lua-c makefile xmake
Last synced: 9 days ago
JSON representation
For comparing the difference of the C API luaL_tolstring of two lua versions
- Host: GitHub
- URL: https://github.com/yeshan333/calling-c-func-in-lua-demo
- Owner: yeshan333
- Created: 2024-07-14T15:45:08.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-07-30T09:35:02.000Z (6 months ago)
- Last Synced: 2025-01-19T15:11:34.698Z (16 days ago)
- Topics: c, lua, lua-c, makefile, xmake
- Language: C
- Homepage:
- Size: 775 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 在 lua 脚本中调用 C 函数
[English](README_en.md)
> 用于对比两个 lua 版本 C API luaL_tolstring 的表现
## 快速开始
```shell
# 安装两个 lua 版本
make install_lua# 编译自定义 C 库
make compile_cmp1_so
# 测试版本 1,lua C API luaL_tolstring 的表现
make test_cmp1
# 测试版本 2,lua C API luaL_tolstring 的表现
make compile_cmp2_so
make test_cmp2
```使用 xmake 编译
```shell
# 指定 lua 版本进行编译 mylib.c
xmake f --lua_version=5.4.6 -v
xmake -v# 测试版本 1 lua 5.4.7,lua C API luaL_tolstring 的表现
make test_cmp1
# 测试版本 2 lua 5.4.6,lua C API luaL_tolstring 的表现
make test_cmp2
```---
```c
static int my_printer(lua_State *L)
{
size_t len;
const int arg1 = luaL_checkinteger(L, 1);
printf("arg1: %d\n", arg1);
const uint8_t* s = (const uint8_t*)luaL_tolstring(L, 2, &len);
printf("my printer's output: %s, length: %zu\n", s, len);
// lua_pushstring(L, s);
return 0;
}
```Compare Result (test my_printer func):
```shell
# lua 5.4.7
echo "Testing lua version: lua-5.4.7"
Testing lua version: lua-5.4.7
/github/calling-c-func-in-lua-demo/lua-5.4.7/install/bin/lua test_printer.lua
arg1: 1
my printer's output: Oh my god, length: 9
arg1: 2
my printer's output: Oh my god, length: 9
arg1: 3
my printer's output: Oh my god, length: 9
``````shell
echo "Testing lua version: lua-5.4.6"
Testing lua version: lua-5.4.6
/github/calling-c-func-in-lua-demo/lua-5.4.6/install/bin/lua test_printer.lua
arg1: 1
my printer's output: Oh my god, length: 9
arg1: 2
my printer's output: Oh my god, length: 9
arg1: 3
my printer's output: Oh my god, length: 9
```## 参考
https://www.nosuchfield.com/2019/05/17/Call-C-function-in-Lua/
http://www.troubleshooters.com/codecorn/lua/lua_lua_calls_c.htm