English
Sign in

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) {}

Names

LuauC
cs.chat(to, text)cs.chat(ctx, to, text)
player.healthcs.Player_get_health(ctx, player)
player.health = 50cs.Player_set_health(ctx, player, 50)
player:respawn()cs.Player_respawn(ctx, player)
cs.game.modecs.Game_get_mode(ctx) (one object: no self)
cs.export(...)cs.export_(ctx, ...) (a C++ keyword gets _)

Types

API typeArgumentResult
numberdouble (optional: const double *, NULL absent)double
integerint32_t (optional: const int32_t *)int32_t
booleanbool (optional: const bool *)bool
stringconst char * (optional: NULL absent)const char * (NULL: nil)
vectorcs_vec3 (optional: const cs_vec3 *)cs_vec3
Player, Entity and the other classescs_object, the object's id (0: nil)cs_object
options, records, lists, Players, any, functionscs_valuecs_value

Lifetimes and threads

Luau and modules

A module talks to Luau plugins the same way plugins talk to each other:

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

Tests

tools/plugin_native_test.sh runs a dedicated server with these plugins:

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.