-
-
Notifications
You must be signed in to change notification settings - Fork 1
4. Advanced Custom Functions
AsynchronousAI edited this page Sep 3, 2025
·
4 revisions
The get_args() function exists which returns a tuple of a maximum of 7 arguments (integers only, but some data will be pointers), you can return arguments using push_args().
module.functions["sum"] = function()
local a, b = module.util.get_args()
push_args(a+b)
endFloating point numbers can be used as arguments.
module.functions["floating_sum"] = function()
local a, b = module.util.get_f_args() -- notice the _f_, it will return normal luau numbers.
push_f_args(a+b)
endStrings are simply a integer that represents a pointer in module.memory <buffer> which holds the needed string.
module.functions["print_upper"] = function()
local pointer = module.util.get_args()
local str = module.util.read_string(pointer)
print(str:upper())
endGetting strings back requires using malloc to write the string to a new location in memory.
const char * upperString(const char *);
int printf(const char *, ...);
int main() {
const char *orig = "Hello, World!";
const char *dest = upperString(orig);
printf("%s: %s", orig, dest);
return 0;
}local module = require("./module")
module.functions["upperString"] = function()
local readPointer = module.util.get_args()
local str = module.util.read_string(readPointer)
-- allocate new string
local newString = str:upper().."\0" -- append null terminator, so reader knows where to stop
local writePointer = module.util.malloc(#newString+1) -- find out where to place it
buffer.writestring(module.memory, writePointer, newString) -- write to memory
module.util.push_args(writePointer) -- return where we wrote to
end
module()Much more utilities are provided for more advanced functions. Some of the built in functions will now be provided as examples.
["printf"] = function()
local args = {get_args()}
local fmt = args[1]
table.remove(args, 1)
print(format_string(read_string(fmt), args))
end,-
read_stringis a function that is exposed to convert a string inside the memory buffer to a Luau string. It works by taking in a start pointer (int) and reading until it finds a null terminator (\0). -
format_stringis a function that is meant to act likestring.formatbut have support for:- formatting pointers into
%s(withread_string) - converting ints into floats for
%f
- formatting pointers into
["memcpy"] = function()
local dest, src, count = get_args()
buffer.copy(memory, dest, memory, src, count)
end,This is a simple wrapper for buffer.copy.