|
Aegis2D
Modular C++26 2D game engine for networked games
|
A Lua state for the game's scripts, sandboxed. More...
#include <aegis2d/script/state.hpp>
Public Member Functions | |
| ScriptState () | |
| Opens a sandboxed state. | |
| ScriptState (const ScriptState &)=delete | |
| ScriptState & | operator= (const ScriptState &)=delete |
| ScriptState (ScriptState &&other) noexcept | |
Takes over the state of other, left without one. | |
| ScriptState & | operator= (ScriptState &&other) noexcept |
Takes over the state of other, left without one. | |
| ~ScriptState () | |
| Closes the state. | |
| bool | run (std::string_view code, std::string_view chunk="script") |
| Runs Lua code. | |
| bool | run_file (const std::filesystem::path &file) |
| Runs a Lua file. | |
| bool | call (std::string_view function) |
| Calls a global function without arguments. | |
| bool | call (std::string_view function, double argument) |
| Calls a global function with a number, such as the seconds of a tick. | |
| bool | has (std::string_view name) const |
Tells whether a global is set (not nil). | |
| std::optional< double > | number (std::string_view name) const |
| A global number. | |
| std::optional< std::string > | text (std::string_view name) const |
| A global string. | |
| std::optional< bool > | boolean (std::string_view name) const |
| A global boolean. | |
| void | set_instruction_limit (std::uint32_t instructions) noexcept |
| Sets the instructions a run or a call may take. | |
| std::uint32_t | instruction_limit () const noexcept |
| Instructions a run or a call may take. | |
| void | set_memory_limit (std::size_t bytes) noexcept |
| Sets the memory the state may use while a script runs. | |
| std::size_t | memory_limit () const noexcept |
| Memory the state may use while a script runs. | |
| const std::optional< ScriptError > & | last_error () const noexcept |
| The error of the last run or call that failed. | |
| void | clear_error () noexcept |
| Forgets the last error. | |
Static Public Attributes | |
| static constexpr std::uint32_t | default_instruction_limit = 10'000'000 |
| Instructions a run or a call may take before it is stopped, by default. | |
| static constexpr std::size_t | default_memory_limit = std::size_t{64} * 1024 * 1024 |
| Memory the state may use while a script runs, by default, in bytes. | |
Friends | |
| struct | ScriptAccess |
A Lua state for the game's scripts, sandboxed.
Only the safe standard libraries are open: the base functions, coroutine, math, string, table and utf8. io, os, debug and package (so require) are not, dofile and loadfile are removed, load only reads text, never precompiled chunks, which could crash the virtual machine, finalizers (__gc) are refused and the collector cannot be stopped. print writes to the engine's log. A script that fails, by a syntax error, an error at run time, too many instructions (an endless loop, a costly pattern) or too much memory, is stopped: its error is logged with its chunk and line and kept in last_error(), and the state goes on with what ran before the error. A pcall in the script cannot catch the stop, and its coroutines share its budget. Not thread-safe: one state per thread.
| aegis2d::script::ScriptState::ScriptState | ( | ) |
Opens a sandboxed state.
|
delete |
|
noexcept |
Takes over the state of other, left without one.
| aegis2d::script::ScriptState::~ScriptState | ( | ) |
Closes the state.
| std::optional< bool > aegis2d::script::ScriptState::boolean | ( | std::string_view | name | ) | const |
A global boolean.
| name | Name of the global. |
| bool aegis2d::script::ScriptState::call | ( | std::string_view | function | ) |
Calls a global function without arguments.
| function | Name of the global. |
true if it ran to its end; false if it failed or is not a function. | bool aegis2d::script::ScriptState::call | ( | std::string_view | function, |
| double | argument | ||
| ) |
Calls a global function with a number, such as the seconds of a tick.
| function | Name of the global. |
| argument | Its argument. |
true if it ran to its end; false if it failed or is not a function.
|
noexcept |
Forgets the last error.
| bool aegis2d::script::ScriptState::has | ( | std::string_view | name | ) | const |
Tells whether a global is set (not nil).
| name | Name of the global. |
true if the global has a value.
|
noexcept |
Instructions a run or a call may take.
|
noexcept |
The error of the last run or call that failed.
|
noexcept |
Memory the state may use while a script runs.
| std::optional< double > aegis2d::script::ScriptState::number | ( | std::string_view | name | ) | const |
A global number.
| name | Name of the global. |
|
delete |
|
noexcept |
Takes over the state of other, left without one.
| bool aegis2d::script::ScriptState::run | ( | std::string_view | code, |
| std::string_view | chunk = "script" |
||
| ) |
Runs Lua code.
| code | Source text of the chunk; precompiled chunks are refused. |
| chunk | Name of the chunk in the errors, such as a file name. |
true if the chunk ran to its end, false if it failed (see last_error()). | bool aegis2d::script::ScriptState::run_file | ( | const std::filesystem::path & | file | ) |
Runs a Lua file.
| file | Path of the file; its name is the chunk's in the errors. |
true if the file was read and ran to its end, false otherwise.
|
noexcept |
Sets the instructions a run or a call may take.
Each step of the pattern matcher (string.find, match, gmatch, gsub) counts as one.
| instructions | At least 1; a script that takes more is stopped with an error. |
|
noexcept |
Sets the memory the state may use while a script runs.
| bytes | Memory of the whole state; a script that needs more fails with a memory error. |
| std::optional< std::string > aegis2d::script::ScriptState::text | ( | std::string_view | name | ) | const |
A global string.
| name | Name of the global. |
|
friend |
|
staticconstexpr |
Instructions a run or a call may take before it is stopped, by default.
|
staticconstexpr |
Memory the state may use while a script runs, by default, in bytes.