From 683210d02fc6b478d675db893d5dee7c360f2c77 Mon Sep 17 00:00:00 2001 From: Ziyao Date: Thu, 29 Feb 2024 21:37:54 +0800 Subject: [PATCH] add lua binding (#53) --- README.md | 29 +++++++++++++++++++++++++++++ lua/ruapu-0.1-1.rockspec | 19 +++++++++++++++++++ lua/ruapu-binding.c | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 lua/ruapu-0.1-1.rockspec create mode 100644 lua/ruapu-binding.c diff --git a/README.md b/README.md index f5f8e02..5af2854 100644 --- a/README.md +++ b/README.md @@ -154,6 +154,35 @@ fn main() { +### ruapu with Lua + + + + + +
+ +Compile ruapu library + +```shell +# from source code +cd lua +# lua binding has been tested on Lua 5.2~5.4 +luarocks make +``` + + +Use ruapu in Lua + +```Lua +ruapu = require "ruapu"; +print(ruapu.supports("mmx")); +for _, ext in ipairs(ruapu.rua()) do + print(ext); +end +``` +
+
Github-hosted runner result (Linux) diff --git a/lua/ruapu-0.1-1.rockspec b/lua/ruapu-0.1-1.rockspec new file mode 100644 index 0000000..59e5afd --- /dev/null +++ b/lua/ruapu-0.1-1.rockspec @@ -0,0 +1,19 @@ +package = "ruapu" +version = "0.1-1" +source = { + url = "https://github.com/nihui/ruapu.git" +} +description = { + summary = "Detect CPU ISA features with single-file", + homepage = "https://github.com/nihui/ruapu", + license = "MIT" +} +dependencies = { + "lua >= 5.2, <= 5.4" +} +build = { + type = "builtin", + modules = { + ruapu = "ruapu-binding.c" + } +} diff --git a/lua/ruapu-binding.c b/lua/ruapu-binding.c new file mode 100644 index 0000000..c6d0a7b --- /dev/null +++ b/lua/ruapu-binding.c @@ -0,0 +1,36 @@ +#include +#include + +#define RUAPU_IMPLEMENTATION +#include"../ruapu.h" + +static int supports(lua_State *l) +{ + luaL_checktype(l, 1, LUA_TSTRING); + lua_pushboolean(l, ruapu_supports(luaL_checkstring(l, 1))); + return 1; +} + +static int rua(lua_State *l) +{ + const char * const *s = ruapu_rua(); + lua_newtable(l); + for (int i = 0; s[i]; i++) { + lua_pushstring(l, s[i]); + lua_rawseti(l, -2, i + 1); + } + return 1; +} + +static const struct luaL_Reg modRuapu[] = { + { "supports", supports }, + { "rua", rua }, + { NULL, NULL } +}; + +int luaopen_ruapu(lua_State *l) +{ + ruapu_init(); + luaL_newlib(l, modRuapu); + return 1; +}