Created
October 29, 2022 13:58
-
-
Save samyeyo/e6dcad7974a75d0e05dbc00609a0b4dd to your computer and use it in GitHub Desktop.
Here is an example of a C binary module that uses the LuaRT extensions for the Lua C API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//--- include the luart.h for the LuaRT C API | |
//--- compilation : gcc -shared -Ipath\to\LuaRT\src\include calc.c -Lpath\to\LuaRT\src -llua54 -o calc.dll | |
//--- howto use : luart usecalc.lua | |
#include <luart.h> | |
//-- number formats supported | |
static const char *base_format[] = { "0x%X", "%d" }; | |
//-- current format for the result of the calc.add() function | |
static int base_index = 0; | |
//------------------------------------ calc.get_base() function (property getter) | |
LUA_PROPERTY_GET(calc, base) | |
{ | |
//--- pushes the string corresponding to the current base value and returns it | |
lua_pushstring(L, base_index ? "dec" : "hex"); | |
return 1; | |
} | |
//------------------------------------ calc.set_base() function (property setter) | |
LUA_PROPERTY_SET(calc, base) | |
{ | |
//--- The value at the first stack index is the value to set the property with | |
const char *format = luaL_checkstring(L, 1); | |
//--- Compare the provided string and set the global base variable accordingly... | |
if (strcmp(format, "hex") == 0) | |
base_index = 0; | |
else | |
if (strcmp(format, "dec") == 0) | |
base_index = 1; | |
else | |
//--- ...or throw an error if the format is not valid | |
luaL_error(L, "invalid format"); | |
//--- A setter function don't return any value | |
return 0; | |
} | |
//------------------------------------ calc.add() function | |
LUA_METHOD(calc, add) | |
{ | |
char buffer[12] = {0}; | |
//--- You access function arguments like any other Lua C function (index 1 => first argument, ...) | |
lua_Integer a,b; | |
//--- Here we are checking and getting integers arguments | |
a = luaL_checkinteger(L, 1); | |
b = luaL_checkinteger(L, 2); | |
//--- pushes a formated string for the result of a+b, using the current base_index format... | |
snprintf(buffer, 12, base_format[base_index], a+b); | |
lua_pushstring(L, buffer); | |
//--- ...and returns it | |
return 1; | |
} | |
//--------------- The finalizer function called when the module is garbage collected | |
int calc_finalize(lua_State *L) | |
{ | |
puts("calc module has been finalized"); | |
//--- finalizer functions returns nothing | |
return 0; | |
} | |
//--- luaL_Reg array for module properties, postfixed by "_properties" | |
static const luaL_Reg calc_properties[] = { | |
{"get_base", calc_getbase}, //--- module "base" property getter | |
{"set_base", calc_setbase}, //--- module "base" property setter | |
{NULL, NULL} | |
}; | |
//--- luaL_Reg array for module functions, postfixed by "lib" | |
static const luaL_Reg calclib[] = { | |
{"add", calc_add}, //--- module "add" function | |
{NULL, NULL} | |
}; | |
//----- "calc" module registration function | |
int __declspec(dllexport) luaopen_calc(lua_State *L) | |
{ | |
//--- lua_regmodulefinalize() registers the specified module with a finalizer function | |
//--- and pushes the module on the stack | |
lua_regmodulefinalize(L, calc); | |
//--- returns one value (the just pushed calc module) | |
return 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- use the calc.dll binary module | |
-- require for the calc module (will load calc.dll) | |
local calc = require "calc" | |
-- Get the calc.base module property value (calls the C function calc_getbase) | |
print("calc.base = "..calc.base) | |
-- Call the calc.add() module function (calls the C function calc_add) | |
print("calc.add(254, 1) = "..calc.add(254, 1)) | |
-- Set the calc.base module property (calls the C function calc_setbase) | |
calc.base = "dec" | |
-- Check that the calc.base property has been changed | |
print("calc.base = "..calc.base) | |
print("calc.add(254, 1) = "..calc.add(254, 1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment