Native modules
A native module is a server-only library (.so on Linux, .dll on Windows) that a plugin carries. It calls the same API as Luau plugins through a C ABI, data/plugin_sdk/native/cs_plugin.h, which tools/gen_plugin_api.py generates from plugins/api.toml. Use one for what Luau cannot do: a client library for an outside service (a database server, for example), heavy computation, or existing C/C++ code. Everything else belongs in Luau.
A module runs without the sandbox, so the server's admin has to allow it by name. It never reaches players: clients load no native code.
A plugin with a module
plugins/hello/
plugin.toml
lib/hello.so (lib/hello.dll on Windows)
main.luau (optional with a module)
name = "hello"
version = "1.0.0"
api = 1
native = "lib/hello" # in the plugin's folder, without the extension
The admin allows it in plugins/natives.toml (in the server's user folder):
allow = ["hello"]
Without that line the plugin is refused, and the server says why. The module loads before the plugin's main.luau, which is optional when native is set. plugin info hello names the module.
The module
#include "cs_plugin.h"
static cs_functions cs;
static cs_value greet(cs_ctx *ctx, const cs_value *args, int count, void *ud) {
cs_object player = cs.to_object(ctx, cs.get(ctx, args[0], "player"));
cs.chat(ctx, cs.object(ctx, "Player", player), "Welcome!");
return 0;
}
CS_EXPORT int cs_module_load(cs_ctx *ctx, const cs_api *api) {
if (api->abi != CS_ABI_VERSION || cs_load(api, &cs) != 0)
return 1; /* built for another server version */
cs.on(ctx, "player_join", cs.function(ctx, greet, NULL), 0);
return 0;
}
CS_EXPORT void cs_module_unload(cs_ctx *ctx) {}
-
Exports:
-
cs_module_loadreturns 0 when the module is ready. Any other value refuses the plugin, andlast_erroror the module's own message says why. -
cs_module_unloadis optional. It runs while the API still works, and it must stop and join the module's threads.
-
-
Finding the functions:
cs_loadlooks every function up by name and returns how many the server lacks (an older server leaves those as NULL). A module built against a newer header can still run if it checks the ones it uses. -
Building: the samples'
CMakeLists.txtwork on their own, on Linux and with the MinGW toolchain (native/cmake/mingw-w64.cmake). Build with hidden visibility:CS_EXPORTmarks the two exports.
Names
| Luau | C |
|---|---|
cs.chat(to, text) | cs.chat(ctx, to, text) |
player.health | cs.Player_get_health(ctx, player) |
player.health = 50 | cs.Player_set_health(ctx, player, 50) |
player:respawn() | cs.Player_respawn(ctx, player) |
cs.game.mode | cs.Game_get_mode(ctx) (one object: no self) |
cs.export(...) | cs.export_(ctx, ...) (a C++ keyword gets _) |
-
If a property's accessor would have the same name as a method, the accessor gets
_property(for example,Entity.sizeandEntity:set_size()giveEntity_set_size_property). -
Constants are enums:
CS_BUTTON_JUMP,CS_FLAG_ON_GROUND... -
Functions written by hand in Luau (
cs.import,cs.emit) are reached throughcall_api.
Types
| API type | Argument | Result |
|---|---|---|
number | double (optional: const double *, NULL absent) | double |
integer | int32_t (optional: const int32_t *) | int32_t |
boolean | bool (optional: const bool *) | bool |
string | const char * (optional: NULL absent) | const char * (NULL: nil) |
vector | cs_vec3 (optional: const cs_vec3 *) | cs_vec3 |
Player, Entity and the other classes | cs_object, the object's id (0: nil) | cs_object |
options, records, lists, Players, any, functions | cs_value | cs_value |
-
A
cs_objectis the id the API uses: a Player's is its slot, an Entity's is its index + 1.cs.object(ctx, "Player", id)makes a value of it for an argument that takes a value (such asPlayers), andto_objectreads one back. -
A
cs_valueis a value in the module's own Luau VM (0 is nil). Make values withnumber,string,boolean,vector,object,tableandfunction. Read them withto_number,to_string... and withget,get_indexandlength, which work on tables, API objects and event objects. -
An option table is a
tablefilled withset_string,set_number... An unknown key fails, as in Luau.
Lifetimes and threads
-
Entry points: the module's code runs inside entry points. These are
cs_module_load,cs_module_unload, a function made withfunctionwhile it runs (a handler, a command, an export), and a task given topost_to_tick. -
What the API gives back lives until the entry point returns. That covers values and strings.
keepa value to hold it longer, andreleaseit when done. Copy any string you need later. -
Game thread only: the API works only on the game's thread, inside an entry point. A module's own threads (I/O, a client library) hand their results over with
post_to_tick(ctx, task, ud), the one function safe from any thread. The task runs at the next server frame. If the module unloads first, the task is dropped. -
Errors: a failed call returns 0, NULL or nil and sets
last_error(ctx). Every call clears it first. Inside a function value,fail(ctx, message)makes the Luau caller get an error once the function returns.
Luau and modules
A module talks to Luau plugins the same way plugins talk to each other:
-
cs.export_(ctx, "name", cs.function(ctx, fn, ud))offers a function. Luau plugins call it withcs.import("plugin", "name"). -
call_api(ctx, "import", ...)reaches a Luau plugin's export. -
call_api(ctx, "emit", ...)raises the module's own events, andcs.on(ctx, "other:event", ...)listens to others.
Values are copied between the VMs, and functions never cross. That is why asynchronous results come back as events (see the sql sample's client.luau).
Samples
-
**
samples/native/hello** (C99): a command, a greeting when a player joins, and an export. -
**
samples/native/sql** (C++17): MySQL/MariaDB and PostgreSQL for Luau plugins.-
Each connection runs on its own thread, and results come back as the
sql:resultevent.client.luauturns them back into callbacks:require("@sql/client")from a plugin that depends onsql. - It is built with libpq and/or the MariaDB client library. Both are LGPL and linked dynamically, so the sample is not shipped with the game.
-
Each connection runs on its own thread, and results come back as the
Tests
tools/plugin_native_test.sh runs a dedicated server with these plugins:
-
**
tests/native:** a module that checks each kind of argument and result, properties, methods, errors, kept values, exports both ways, events and a task posted from a thread. -
**
tests/native_user:** its Luau user. -
**The
hellosample.** -
**
tests/native_denied:** a module thatnatives.tomldoes not allow.
When mariadbd is installed, the script also runs the sql sample and tests/native_sql against a temporary MariaDB server with its own socket and no network.