### Example Function Definitions Source: https://lua.org/manual/5.1/manual.html Provides example function definitions to illustrate parameter handling and variadic arguments. ```lua function f(a, b) end ``` ```lua function g(a, b, ...) end ``` ```lua function r() return 1,2,3 end ``` -------------------------------- ### C Path Template Example Source: https://lua.org/manual/5.1/manual.html This example shows a typical C path configuration for `require`. It lists potential locations and file extensions for C libraries, using '.' for the current directory and '?' as a placeholder for the module name. ```lua "./?.so;./?.dll;/usr/local/?/init.so" ``` -------------------------------- ### Lua Path Template Example Source: https://lua.org/manual/5.1/manual.html This example demonstrates the format of a Lua path, which is a sequence of templates separated by semicolons. Each template can contain an interrogation mark '?' which is replaced by the module name. ```lua "./?.lua;./?.lc;/usr/local/?/init.lua" ``` -------------------------------- ### Equivalent Table Initialization Source: https://lua.org/manual/5.1/manual.html Shows the expanded form of the table constructor example, demonstrating sequential assignment. ```lua do local t = {} t[f(1)] = g t[1] = "x" -- 1st exp t[2] = "y" -- 2nd exp t.x = 1 -- t["x"] = 1 t[3] = f(x) -- 3rd exp t[30] = 23 t[4] = 45 -- 4th exp a = t end ``` -------------------------------- ### Lua Assignment Example Source: https://lua.org/manual/5.1/manual.html Demonstrates how multiple assignments work, including value adjustment and the order of evaluation. ```lua i = 3 i, a[i] = i+1, 20 ``` -------------------------------- ### Lua Table Constructor Example Source: https://lua.org/manual/5.1/manual.html Illustrates how a complex table constructor is equivalent to a sequence of table assignments. ```lua a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 } ``` -------------------------------- ### Vararg Function Call Examples Source: https://lua.org/manual/5.1/manual.html Illustrates how arguments are mapped to parameters and the vararg expression for different function calls. ```lua CALL PARAMETERS f(3) a=3, b=nil f(3, 4) a=3, b=4 f(3, 4, 5) a=3, b=4 f(r(), 10) a=1, b=10 f(r()) a=1, b=2 g(3) a=3, b=nil, ... --> (nothing) g(3, 4) a=3, b=4, ... --> (nothing) g(3, 4, 5, 8) a=3, b=4, ... --> 5 8 g(5, r()) a=5, b=1, ... --> 2 3 ``` -------------------------------- ### Lua Numerical Constant Examples Source: https://lua.org/manual/5.1/manual.html Illustrates valid numerical constants in Lua, including decimal, exponential, and hexadecimal formats. ```lua 3 3.0 3.1416 314.16e-2 0.31416E1 0xff 0x56 ``` -------------------------------- ### Get Lua Function Information Source: https://lua.org/manual/5.1/manual.html Retrieves information about a specific function or function invocation. Use 'what' to specify the desired fields. For function information, push the function onto the stack and start 'what' with '>'. ```c int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar); ``` ```c lua_Debug ar; lua_getfield(L, LUA_GLOBALSINDEX, "f"); /* get global 'f' */ lua_getinfo(L, ">S", &ar); printf("%d\n", ar.linedefined); ``` -------------------------------- ### Coroutine Example in Lua Source: https://lua.org/manual/5.1/manual.html Demonstrates the creation and execution of coroutines using `coroutine.create`, `coroutine.resume`, and `coroutine.yield`. Shows how values are passed between the main thread and the coroutine body, and how errors are handled. ```lua function foo (a) print("foo", a) return coroutine.yield(2*a) end co = coroutine.create(function (a,b) print("co-body", a, b) local r = foo(a+1) print("co-body", r) local r, s = coroutine.yield(a+b, a-b) print("co-body", r, s) return b, "end" end) print("main", coroutine.resume(co, 1, 10)) print("main", coroutine.resume(co, "r")) print("main", coroutine.resume(co, "x", "y")) print("main", coroutine.resume(co, "x", "y")) ``` -------------------------------- ### Lua String Literal Examples Source: https://lua.org/manual/5.1/manual.html Demonstrates various ways to define string literals in Lua, including escape sequences and long bracket notation. ```lua a = 'alo\n123"' a = "alo\n123\"" a = '\97lo\10\04923"' a = [[alo 123"]] a = [==[ alo 123"]==] ``` -------------------------------- ### Operating System Facilities Source: https://lua.org/manual/5.1/manual.html Functions for interacting with the operating system, such as getting CPU time. ```APIDOC ## `os.clock ()` ### Description Returns an approximation of the amount in seconds of CPU time used by the program. ### Method `os.clock` ### Response Example ```json { "cpu_time_seconds": 1.234 } ``` ``` -------------------------------- ### Making Lua Scripts Executable Source: https://lua.org/manual/5.1/manual.html Provides examples of the shebang line for making Lua scripts executable on Unix-like systems. Using '#!/usr/bin/env lua' is a more portable solution. ```bash #!/usr/local/bin/lua ``` ```bash #!/usr/bin/env lua ``` -------------------------------- ### Lua Argument Table Example Source: https://lua.org/manual/5.1/manual.html Illustrates how command-line arguments are populated into the global 'arg' table when using the Lua interpreter. Arguments before the script name are stored at negative indices. ```lua arg = { [-2] = "lua", [-1] = "-la", [0] = "b.lua", [1] = "t1", [2] = "t2" } ``` -------------------------------- ### String formatting with substitutions Source: https://lua.org/manual/5.1/errata.html Example of using `string.gsub` to perform string substitutions, replacing patterns with values from a table. The pattern `$%w+` captures sequences of word characters. ```lua x = string.gsub("$name-$%version.tar.gz", "%$(%w+)", t) ``` -------------------------------- ### Lua Logical Operator Examples Source: https://lua.org/manual/5.1/manual.html Demonstrates the behavior of 'and' and 'or' operators, including short-circuit evaluation. Both 'false' and 'nil' are treated as false. ```lua 10 or 20 --> 10 10 or error() --> 10 nil or "a" --> "a" nil and 10 --> nil false and error() --> false false and nil --> false false or nil --> nil 10 and 20 --> 20 ``` -------------------------------- ### Load File as Lua Chunk with luaL_loadfile Source: https://lua.org/manual/5.1/manual.html Loads a file as a Lua chunk. Uses `lua_load` internally. Handles standard input if `filename` is NULL. Ignores the first line if it starts with '#'. ```c int luaL_loadfile (lua_State *L, const char *filename); ``` -------------------------------- ### Get Local Variable Information Source: https://lua.org/manual/5.1/manual.html Retrieves the name and value of a local variable at a specific index for a given stack level. Returns nil if the index is out of range. Variable names starting with '(' are internal. ```lua debug.getlocal(level, local) ``` -------------------------------- ### Get Lua Type Name Source: https://lua.org/manual/5.1/manual.html Use `lua_typename` to get the string name of a Lua type, given its integer code. The type code must be one returned by `lua_type`. ```c const char *lua_typename (lua_State *L, int tp); ``` -------------------------------- ### luaL_buffinit Source: https://lua.org/manual/5.1/manual.html Initializes a string buffer. ```APIDOC ## luaL_buffinit ### Description Initializes a buffer `B`. This function does not allocate any space; the buffer must be declared as a variable (see `luaL_Buffer`). ### Method N/A (C function) ### Endpoint N/A (C function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```c luaL_Buffer b; luaL_buffinit(L, &b); ``` ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### Get Function Information Source: https://lua.org/manual/5.1/manual.html Retrieves information about a function or a stack level. Use 'f' to include the function itself and 'L' for valid line numbers. The default is to get all available info except line numbers. ```lua debug.getinfo(1, "n").name ``` ```lua debug.getinfo(print) ``` -------------------------------- ### Get Object Length in Lua Source: https://lua.org/manual/5.1/manual.html Use `lua_objlen` to get the length of a value at a given stack index. For strings, it's the length; for tables, it's the result of the `#` operator; for userdata, it's the memory block size; otherwise, it's 0. ```c size_t lua_objlen (lua_State *L, int index); ``` -------------------------------- ### Open All Standard Lua Libraries Source: https://lua.org/manual/5.1/manual.html Opens all standard Lua libraries, making them available to the Lua state. This is the recommended way to include the standard library. ```c luaL_openlibs(L); ``` -------------------------------- ### Load and Run a String in Lua Source: https://lua.org/manual/5.1/manual.html Use this idiom to load and immediately run a Lua string. Ensure the string is valid Lua code; otherwise, `assert` will raise an error. ```lua assert(loadstring(s))() ``` -------------------------------- ### Object Length Source: https://lua.org/manual/5.1/manual.html Function to get the length of a Lua object. ```APIDOC ## `lua_objlen` ### Description Returns the "length" of the value at the given acceptable index: for strings, this is the string length; for tables, this is the result of the length operator ('`#`'); for userdata, this is the size of the block of memory allocated for the userdata; for other values, it is 0. ### Method `size_t lua_objlen (lua_State *L, int index); ` ### Parameters - **L** (`lua_State *`) - The Lua state. - **index** (`int`) - The index of the value on the stack. ### Request Example ```c size_t lua_objlen (lua_State *L, int index); ``` ### Response - **`size_t`**: The length of the value. ``` -------------------------------- ### Get Metatable Source: https://lua.org/manual/5.1/manual.html Returns the metatable associated with an object, or nil if none exists. ```lua debug.getmetatable(object) ``` -------------------------------- ### Prepare Buffer for String Addition Source: https://lua.org/manual/5.1/manual.html Prepares a buffer of LUAL_BUFFERSIZE for copying a string to be added to a luaL_Buffer. Must be followed by luaL_addsize. ```c char *luaL_prepbuffer (luaL_Buffer *B); ``` -------------------------------- ### Get CPU Time Source: https://lua.org/manual/5.1/manual.html Returns an approximation of the CPU time used by the program in seconds. ```lua os.clock () ``` -------------------------------- ### Get Lua Version Source: https://lua.org/manual/5.1/manual.html This global variable holds the current Lua version string. ```lua _VERSION ``` -------------------------------- ### dofile Source: https://lua.org/manual/5.1/manual.html Opens and executes a Lua chunk from a file. ```APIDOC ## `dofile ([filename])` ### Description `dofile` is similar to `loadfile`, but it also calls the resulting function in the Lua state. It opens the named file, compiles the contents as a Lua chunk, and then runs the chunk. `dofile` returns any values returned by the chunk. If the file is not found or cannot be opened, `dofile` raises an error. If called without arguments, `dofile` executes the contents of `stdin`. ### Parameters - **filename** (string, optional): The path to the Lua file to execute. ### Returns - The values returned by the executed Lua chunk. ### Errors - Raises an error if the file cannot be opened or compiled. ### Example ```lua -- Assuming 'my_script.lua' exists and contains 'print("Hello from dofile!")' dofile("my_script.lua") -- Output: Hello from dofile! -- Execute code from standard input -- dofile() ``` ``` -------------------------------- ### Raw Get Value in Lua Source: https://lua.org/manual/5.1/manual.html Retrieves the value of `table[index]` without invoking metamethods. ```lua rawget(table, index) ``` -------------------------------- ### luaL_dofile Source: https://lua.org/manual/5.1/manual.html Loads and runs a Lua file. ```APIDOC ## luaL_dofile ### Description Loads and runs the given file. It is defined as the following macro: `(luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0))`. It returns 0 if there are no errors or 1 in case of errors. ### Method N/A (C function) ### Endpoint N/A (C function) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) Returns 0 on success, 1 on error. #### Response Example N/A ``` -------------------------------- ### Get the Metatable of an Object Source: https://lua.org/manual/5.1/manual.html Retrieves the metatable associated with an object. Returns nil if the object has no metatable. ```lua getmetatable (object) ``` -------------------------------- ### Open a File Source: https://lua.org/manual/5.1/manual.html Opens a file with the specified mode ('r', 'w', 'a', 'r+', 'w+', 'a+'). A 'b' can be appended for binary mode. Returns a file handle or nil on error. ```lua io.open ("file.txt", "r") ``` ```lua io.open ("file.txt", "wb") ``` -------------------------------- ### Open Standard Lua Libraries with luaL_openlibs Source: https://lua.org/manual/5.1/manual.html Loads all standard Lua libraries into the provided Lua state. ```c void luaL_openlibs (lua_State *L); ``` -------------------------------- ### Load and Run File with luaL_dofile Source: https://lua.org/manual/5.1/manual.html Executes a Lua file. This is a macro that combines `luaL_loadfile` and `lua_pcall`. Returns 0 on success, 1 on error. ```c int luaL_dofile (lua_State *L, const char *filename); ``` ```c (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0)) ``` -------------------------------- ### luaL_dostring Source: https://lua.org/manual/5.1/manual.html Loads and runs a Lua string. ```APIDOC ## luaL_dostring ### Description Loads and runs the given string. It is defined as the following macro: `(luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0))`. It returns 0 if there are no errors or 1 in case of errors. ### Method N/A (C function) ### Endpoint N/A (C function) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) Returns 0 on success, 1 on error. #### Response Example N/A ``` -------------------------------- ### Executing Lua Strings with Options Source: https://lua.org/manual/5.1/manual.html Demonstrates executing multiple Lua strings sequentially using the -e option. The interpreter processes these options in order before running the specified script. ```bash $ lua -e'a=1' -e 'print(a)' script.lua ``` -------------------------------- ### Get Registry Table Source: https://lua.org/manual/5.1/manual.html Returns the global registry table, which holds Lua's internal state. ```lua debug.getregistry() ``` -------------------------------- ### Get Metatable by Name with luaL_getmetatable Source: https://lua.org/manual/5.1/manual.html Pushes the metatable associated with a given name from the registry onto the stack. ```c void luaL_getmetatable (lua_State *L, const char *tname); ``` -------------------------------- ### debug.setupvalue Source: https://lua.org/manual/5.1/manual.html Assigns a value to an upvalue of a function. ```APIDOC ## `debug.setupvalue (func, up, value)` ### Description This function assigns the value `value` to the upvalue with index `up` of the function `func`. The function returns **nil** if there is no upvalue with the given index. Otherwise, it returns the name of the upvalue. ``` -------------------------------- ### Check and Get String Argument Source: https://lua.org/manual/5.1/manual.html Verifies if the argument at `narg` is a string and returns it. Uses `lua_tolstring` internally. ```c const char *luaL_checkstring (lua_State *L, int narg); ``` -------------------------------- ### luaL_prepbuffer Source: https://lua.org/manual/5.1/manual.html Prepares a buffer for string concatenation by returning a pointer to a writable space. ```APIDOC ## luaL_prepbuffer ### Description Returns an address to a space of size `LUAL_BUFFERSIZE` where you can copy a string to be added to buffer `B` (see `luaL_Buffer`). After copying the string into this space you must call `luaL_addsize` with the size of the string to actually add it to the buffer. ### Method C Function ### Endpoint N/A (C API Function) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (char *) - Returns a pointer to a character array (buffer space) of size `LUAL_BUFFERSIZE`. #### Response Example N/A ``` -------------------------------- ### Get String Length with string.len Source: https://lua.org/manual/5.1/manual.html Calculates the length of a string. It correctly handles strings containing null characters. ```lua string.len("") "" ``` ```lua string.len("a\000bc\000") "a\000bc\000" ``` -------------------------------- ### Get Type Name of a Value Source: https://lua.org/manual/5.1/manual.html Returns the string name of the type of the Lua value at the specified stack index. ```c const char *luaL_typename (lua_State *L, int index); ``` -------------------------------- ### Lua Syntactic Sugar for Function Definitions Source: https://lua.org/manual/5.1/manual.html Illustrates the syntactic sugar available for defining functions and local functions in Lua. ```lua stat ::= **function** funcname funcbody ``` ```lua stat ::= **local** **function** Name funcbody ``` ```lua funcname ::= Name {`**.**´ Name} [`**:**´ Name] ``` -------------------------------- ### Check and Get Number Argument Source: https://lua.org/manual/5.1/manual.html Verifies if the argument at `narg` is a number and returns it as a `lua_Number`. Raises an error if the type is incorrect. ```c lua_Number luaL_checknumber (lua_State *L, int narg); ``` -------------------------------- ### Create New Metatable with luaL_newmetatable Source: https://lua.org/manual/5.1/manual.html Creates a new table for userdata metatables and registers it under `tname`. Pushes the metatable onto the stack. Returns 1 if a new table was created, 0 otherwise. ```c int luaL_newmetatable (lua_State *L, const char *tname); ``` -------------------------------- ### Check and Get Integer Argument Source: https://lua.org/manual/5.1/manual.html Verifies if the argument at `narg` is a number and returns it as an `int`. Raises an error if the type is incorrect. ```c int luaL_checkint (lua_State *L, int narg); ``` -------------------------------- ### Create and Return a Reference Source: https://lua.org/manual/5.1/manual.html Creates a reference to an object on the stack within a given table and returns a unique integer key for it. Pops the object from the stack. Returns LUA_REFNIL for nil objects. ```c int luaL_ref (lua_State *L, int t); ``` -------------------------------- ### Get Upvalue Information Source: https://lua.org/manual/5.1/manual.html Retrieves the name and value of an upvalue for a given function and upvalue index. Returns nil if the index is invalid. ```lua debug.getupvalue(func, up) ``` -------------------------------- ### _G Source: https://lua.org/manual/5.1/manual.html Represents the global environment table in Lua. ```APIDOC ## `_G` ### Description `_G` is a global variable that holds the global environment table. All global variables are stored as fields in this table. By default, `_G._G = _G`, meaning the global environment table refers to itself. ### Usage Modifying `_G` can affect global variables and the behavior of functions that rely on the global environment. It is generally not recommended to modify `_G` directly unless you have a specific reason, such as implementing custom global environments or using `setfenv` (which is deprecated in favor of `_ENV`). ### Example ```lua -- Accessing a global variable through _G print(_G.print) -- Assigning a new global variable _G.my_global = 100 print(my_global) -- Output: 100 ``` ``` -------------------------------- ### Check and Get Long Integer Argument Source: https://lua.org/manual/5.1/manual.html Verifies if the argument at `narg` is a number and returns it as a `long`. Raises an error if the type is incorrect. ```c long luaL_checklong (lua_State *L, int narg); ``` -------------------------------- ### Check and Get Lua Integer Argument Source: https://lua.org/manual/5.1/manual.html Verifies if the argument at `narg` is a number and returns it as a `lua_Integer`. Raises an error if the type is incorrect. ```c lua_Integer luaL_checkinteger (lua_State *L, int narg); ``` -------------------------------- ### Get Lua Hook Mask Source: https://lua.org/manual/5.1/manual.html Retrieves the current hook mask of the Lua state. This indicates which events trigger debug hooks. ```c int lua_gethookmask (lua_State *L); ``` -------------------------------- ### Get Current Hook Function Source: https://lua.org/manual/5.1/manual.html Retrieves the currently set hook function for the Lua state. Hooks are used for debugging and profiling. ```c lua_Hook lua_gethook (lua_State *L); ``` -------------------------------- ### Create New Lua State with luaL_newstate Source: https://lua.org/manual/5.1/manual.html Initializes a new Lua state using `realloc` as the allocator and sets a default panic function. ```c lua_State *luaL_newstate (void); ``` -------------------------------- ### lua_createtable Source: https://lua.org/manual/5.1/manual.html Creates a new empty table and pushes it onto the Lua stack. It allows for pre-allocation of space for array and non-array elements, which can improve performance if the table size is known in advance. ```APIDOC ## lua_createtable ### Description Creates a new empty table and pushes it onto the stack. The new table has space pre-allocated for `narr` array elements and `nrec` non-array elements. This pre-allocation is useful when you know exactly how many elements the table will have. Otherwise you can use the function `lua_newtable`. ### Method `void lua_createtable (lua_State *L, int narr, int nrec); ` ### Stack [-0, +1, _m_] ### Parameters - **L** (*lua_State*) - The Lua state. - **narr** (*int*) - The number of elements to pre-allocate for the array part. - **nrec** (*int*) - The number of elements to pre-allocate for the non-array part. ``` -------------------------------- ### Get environment table in Lua Source: https://lua.org/manual/5.1/errata.html The `getfenv` function retrieves the environment table of a given function or the current function if no argument is provided. ```lua getfenv ([f]) ``` -------------------------------- ### luaL_newstate Source: https://lua.org/manual/5.1/manual.html Creates a new Lua state. ```APIDOC ## luaL_newstate ### Description Creates a new Lua state. It calls `lua_newstate` with an allocator based on the standard C `realloc` function and then sets a panic function (see `lua_atpanic`) that prints an error message to the standard error output in case of fatal errors. Returns the new state, or `NULL` if there is a memory allocation error. ### Method N/A (C function) ### Endpoint N/A (C function) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) Returns a pointer to the new Lua state. #### Response Example N/A ``` -------------------------------- ### Get Metatable Field with luaL_getmetafield Source: https://lua.org/manual/5.1/manual.html Retrieves a field from an object's metatable. Returns 0 and pushes nothing if the metatable or field is absent. ```c int luaL_getmetafield (lua_State *L, int obj, const char *e); ``` -------------------------------- ### lua_setupvalue Source: https://lua.org/manual/5.1/manual.html Sets the value of a closure's upvalue. ```APIDOC ## `lua_setupvalue` ### Description Sets the value of a closure's upvalue. The new value is taken from the top of the stack. ### Method `const char *lua_setupvalue (lua_State *L, int funcindex, int n); ` ### Parameters - **L** (`lua_State *`) - The Lua state. - **funcindex** (`int`) - The index of the closure in the stack. - **n** (`int`) - The index of the upvalue. ### Returns - The name of the upvalue if successful. - `NULL` if the index is invalid, and the stack is not modified. ``` -------------------------------- ### debug.getregistry () Source: https://lua.org/manual/5.1/manual.html Returns the registry table. ```APIDOC ## debug.getregistry () ### Description Returns the registry table (see §3.5). ### Method Call ### Endpoint N/A (Internal function) ``` -------------------------------- ### Register Lua Library Functions Source: https://lua.org/manual/5.1/manual.html Opens a Lua library by registering a list of C functions. It can register functions into the top table or create/update a global library. ```c void luaL_register (lua_State *L, const char *libname, const luaL_Reg *l); ``` -------------------------------- ### Get Optional String Argument Source: https://lua.org/manual/5.1/manual.html Retrieves a string argument by its index. Returns a default string if the argument is nil or absent. Raises an error for other types. ```c const char *luaL_optstring (lua_State *L, int narg, const char *d); ``` -------------------------------- ### Get Optional Number Argument Source: https://lua.org/manual/5.1/manual.html Retrieves a number argument by its index. Returns a default value if the argument is nil or absent. Raises an error for other types. ```c lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number d); ``` -------------------------------- ### Set File Buffering Mode Source: https://lua.org/manual/5.1/manual.html Configures the buffering for an output file. 'no' for no buffering, 'full' for full buffering, and 'line' for line buffering. An optional size can be specified. ```lua file:setvbuf ("no") ``` ```lua file:setvbuf ("full", 1024) ``` ```lua file:setvbuf ("line") ``` -------------------------------- ### Get Optional Integer Argument Source: https://lua.org/manual/5.1/manual.html Retrieves an integer argument by its index. Returns a default value if the argument is nil or absent. Raises an error for other types. ```c lua_Integer luaL_optinteger (lua_State *L, int narg, lua_Integer d); ``` -------------------------------- ### lua_pushvfstring Source: https://lua.org/manual/5.1/manual.html Equivalent to `lua_pushfstring`, but accepts a `va_list` for arguments. ```APIDOC ## lua_pushvfstring ### Description Equivalent to `lua_pushfstring`, except that it receives a `va_list` instead of a variable number of arguments. ### Method `const char *lua_pushvfstring (lua_State *L, const char *fmt, va_list argp); ` ### Parameters #### Path Parameters - **L** (`lua_State *`) - Required - The Lua state. - **fmt** (`const char *`) - Required - The format string. - **argp** (`va_list`) - Required - The variable arguments list. ### Response Example ```json { "formatted_string": "example string" } ``` ``` -------------------------------- ### Create a Temporary File Source: https://lua.org/manual/5.1/manual.html Returns a file handle for a temporary file opened in update mode. The file is automatically removed when the program ends. ```lua io.tmpfile () ``` -------------------------------- ### Get Optional Integer Argument with luaL_optint Source: https://lua.org/manual/5.1/manual.html Retrieves an integer argument, returning a default value if the argument is absent or nil. Raises an error for other types. ```c int luaL_optint (lua_State *L, int narg, int d); ``` -------------------------------- ### Get Current Hook Count Source: https://lua.org/manual/5.1/manual.html Retrieves the current hook count, which determines how often the hook function is called (e.g., every N instructions). ```c int lua_gethookcount (lua_State *L); ``` -------------------------------- ### Load and Run String with luaL_dostring Source: https://lua.org/manual/5.1/manual.html Executes a Lua string. This is a macro that combines `luaL_loadstring` and `lua_pcall`. Returns 0 on success, 1 on error. ```c int luaL_dostring (lua_State *L, const char *str); ``` ```c (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0)) ``` -------------------------------- ### Lua Pattern Matching Source: https://lua.org/manual/5.1/manual.html Explains the syntax and components of Lua's pattern matching, including character classes, repetition modifiers, captures, and anchoring. ```APIDOC ## Pattern Item A _pattern item_ can be: * a single character class, which matches any single character in the class; * a single character class followed by '`*`', which matches 0 or more repetitions of characters in the class. These repetition items will always match the longest possible sequence; * a single character class followed by '`+`', which matches 1 or more repetitions of characters in the class. These repetition items will always match the longest possible sequence; * a single character class followed by '`-`', which also matches 0 or more repetitions of characters in the class. Unlike '`*`', these repetition items will always match the _shortest_ possible sequence; * a single character class followed by '`?`', which matches 0 or 1 occurrence of a character in the class; * `%_n_`, for _n_ between 1 and 9; such item matches a substring equal to the _n_ -th captured string. * `%b_xy_`, where _x_ and _y_ are two distinct characters; such item matches strings that start with _x_, end with _y_, and where the _x_ and _y_ are _balanced_. ## Pattern A _pattern_ is a sequence of pattern items. A '`^`' at the beginning of a pattern anchors the match at the beginning of the subject string. A '`$`' at the end of a pattern anchors the match at the end of the subject string. At other positions, '`^`' and '`$`' have no special meaning and represent themselves. ## Captures A pattern can contain sub-patterns enclosed in parentheses; they describe _captures_. When a match succeeds, the substrings of the subject string that match captures are stored (_captured_) for future use. Captures are numbered according to their left parentheses. For instance, in the pattern `"(a*(.)%w(%s*))"`, the part of the string matching `"a*(.)%w(%s*)"` is stored as the first capture (and therefore has number 1); the character matching "." is captured with number 2, and the part matching "`%s*`" has number 3. As a special case, the empty capture `()` captures the current string position (a number). For instance, if we apply the pattern `"()aa()"` on the string `"flaaap"`, there will be two captures: 3 and 5. A pattern cannot contain embedded zeros. Use `%z` instead. ``` -------------------------------- ### Get Lua Value Type Source: https://lua.org/manual/5.1/manual.html Use `lua_type` to determine the data type of a value at a specific stack index. Returns `LUA_TNONE` for invalid indices. ```c int lua_type (lua_State *L, int index); ``` -------------------------------- ### Lua Method Call Syntax Source: https://lua.org/manual/5.1/manual.html Illustrates the syntax for calling methods in Lua. This is syntactic sugar for accessing a field and then calling it as a function with the object as the first argument. ```lua functioncall ::= prefixexp ":" Name args ``` -------------------------------- ### Get Optional Long Argument Source: https://lua.org/manual/5.1/manual.html Retrieves a long integer argument by its index. Returns a default value if the argument is nil or absent. Raises an error for other types. ```c long luaL_optlong (lua_State *L, int narg, long d); ``` -------------------------------- ### Table Creation Source: https://lua.org/manual/5.1/manual.html Functions for creating new tables in Lua. ```APIDOC ## `lua_newtable` ### Description Creates a new empty table and pushes it onto the stack. It is equivalent to `lua_createtable(L, 0, 0)`. ### Method `void lua_newtable (lua_State *L);` ### Parameters None ### Request Example ```c void lua_newtable (lua_State *L); ``` ### Response None ``` -------------------------------- ### Lua Raw Table Get Function Source: https://lua.org/manual/5.1/manual.html Performs a raw table access without invoking metamethods. Use when direct access to table elements is required. ```c void lua_rawget (lua_State *L, int index); ```