Commit Graph

11 Commits

Author SHA1 Message Date
Thomas Watson
a550087eee AP_Scripting: generator: dynamically create userdata metatables
The `luaL_testudata` and `luaL_checkudata` functions correctly treat the
absence of a metatable for the type to check as the type not matching,
so there is no need to have the type's metatable in memory until an
object using it exists. Therefore we can defer the metatable's creation
until the first time an object of that type is created.

Saves some 4500 bytes of Lua heap RAM, at least if no such objects are
created. The created metatables are shared between all scripts, so there
is no increase in the total RAM usage if all objects are used. However,
there is some increased risk of heap fragmentation and unexpected
out-of-memory errors on rarely used code paths.

It would be nice to construct the object in the new utility function,
but it's not possible in C++ to take the address of a constructor.
Instead passing in a lambda is ugly, hardly any more efficient, and
screws up constructors which take parameters.

Also optimizes by using the generated userdata creators where possible,
and convincing the compiler to omit unnecessary checks.
2025-12-16 11:13:16 +11:00
Thomas Watson
62e33f847b AP_Scripting: use luaL_checklstring instead of lua_tolstring in helpers
The latter returns a null pointer (and sets the string size to zero) if
the conversion fails, which the following code neglects to check,
potentially leading to null pointer dereferences.

Instead use the former, which throws a helpful type error if it fails
and doesn't expose the code to null pointers.
2025-05-18 11:59:24 +10:00
Andrew Tridgell
4a442d3b20 AP_Scripting: support DroneCAN messaging in lua
allow for sending broadcast messages, subscribing to broadcast
messages and sending requests
2025-05-09 13:08:40 +10:00
Thomas Watson
e66d2a1495 AP_Scripting: remove redundant memsets
Matches current generator behavior.
2024-07-30 09:55:25 +10:00
Thomas Watson
837b17e82e AP_Scripting: more intelligently manage Lua stack
The Lua stack is guaranteed to have at least LUA_MINSTACK (default 20)
slots upon entry to C. Check to see if we might need more than that
minimum and only in that case call the function to check and resize the
stack. In virtually all cases the check can then be optimized away.

Additionally remove the redundant "Out of stack" message. Lua already
says "stack overflow" and a null message is valid.

Saves ~330B.
2024-07-30 09:55:25 +10:00
Andrew Tridgell
a2459c6e61 AP_Scripting: use NEW_NOTHROW for new(std::nothrow) 2024-06-04 09:20:21 +10:00
Peter Barker
d519ab604e AP_Scripting: allow AP_SCRIPTING_ENABLED to come from hwdef files 2023-06-09 16:10:52 +10:00
Iampete1
41e36e3128 AP_Scripting: Parameter helper: allow optional param name in constructior 2022-09-14 13:59:40 +10:00
Iampete1
918b8a1a9c AP_Scripting: add binding and helper to get old params 2022-02-15 10:05:23 +11:00
Iampete1
37db2c5d4d AP_Scripting: Parameter helper: add configured and set defualt 2022-01-23 07:00:56 +11:00
Peter Hall
9dae370356 AP_Scripting: add param accesss helper 2021-01-05 10:55:36 +11:00