Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::script::ScriptState Class Reference

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
 

Detailed Description

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.

lua.run_file(core::asset_path("behaviors/drone.lua")); // logged if broken
if (lua.call("on_spawn")) {
...
}
A Lua state for the game's scripts, sandboxed.
Definition state.hpp:52
bool call(std::string_view function)
Calls a global function without arguments.
bool run_file(const std::filesystem::path &file)
Runs a Lua file.
std::filesystem::path asset_path(const std::filesystem::path &relative)
Path of one asset.

Constructor & Destructor Documentation

◆ ScriptState() [1/3]

aegis2d::script::ScriptState::ScriptState ( )

Opens a sandboxed state.

◆ ScriptState() [2/3]

aegis2d::script::ScriptState::ScriptState ( const ScriptState &  )
delete

◆ ScriptState() [3/3]

aegis2d::script::ScriptState::ScriptState ( ScriptState &&  other)
noexcept

Takes over the state of other, left without one.

◆ ~ScriptState()

aegis2d::script::ScriptState::~ScriptState ( )

Closes the state.

Member Function Documentation

◆ boolean()

std::optional< bool > aegis2d::script::ScriptState::boolean ( std::string_view  name) const

A global boolean.

Parameters
nameName of the global.
Returns
Its value, or nothing if it is not a boolean.

◆ call() [1/2]

bool aegis2d::script::ScriptState::call ( std::string_view  function)

Calls a global function without arguments.

Parameters
functionName of the global.
Returns
true if it ran to its end; false if it failed or is not a function.

◆ call() [2/2]

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.

Parameters
functionName of the global.
argumentIts argument.
Returns
true if it ran to its end; false if it failed or is not a function.

◆ clear_error()

void aegis2d::script::ScriptState::clear_error ( )
noexcept

Forgets the last error.

◆ has()

bool aegis2d::script::ScriptState::has ( std::string_view  name) const

Tells whether a global is set (not nil).

Parameters
nameName of the global.
Returns
true if the global has a value.

◆ instruction_limit()

std::uint32_t aegis2d::script::ScriptState::instruction_limit ( ) const
noexcept

Instructions a run or a call may take.

Returns
default_instruction_limit unless set.

◆ last_error()

const std::optional< ScriptError > & aegis2d::script::ScriptState::last_error ( ) const
noexcept

The error of the last run or call that failed.

Returns
Nothing if none failed since the state was opened or clear_error().

◆ memory_limit()

std::size_t aegis2d::script::ScriptState::memory_limit ( ) const
noexcept

Memory the state may use while a script runs.

Returns
default_memory_limit unless set.

◆ number()

std::optional< double > aegis2d::script::ScriptState::number ( std::string_view  name) const

A global number.

Parameters
nameName of the global.
Returns
Its value, or nothing if it is not a number.

◆ operator=() [1/2]

ScriptState & aegis2d::script::ScriptState::operator= ( const ScriptState &  )
delete

◆ operator=() [2/2]

ScriptState & aegis2d::script::ScriptState::operator= ( ScriptState &&  other)
noexcept

Takes over the state of other, left without one.

Returns
This state.

◆ run()

bool aegis2d::script::ScriptState::run ( std::string_view  code,
std::string_view  chunk = "script" 
)

Runs Lua code.

Parameters
codeSource text of the chunk; precompiled chunks are refused.
chunkName of the chunk in the errors, such as a file name.
Returns
true if the chunk ran to its end, false if it failed (see last_error()).

◆ run_file()

bool aegis2d::script::ScriptState::run_file ( const std::filesystem::path &  file)

Runs a Lua file.

Parameters
filePath of the file; its name is the chunk's in the errors.
Returns
true if the file was read and ran to its end, false otherwise.

◆ set_instruction_limit()

void aegis2d::script::ScriptState::set_instruction_limit ( std::uint32_t  instructions)
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.

Parameters
instructionsAt least 1; a script that takes more is stopped with an error.

◆ set_memory_limit()

void aegis2d::script::ScriptState::set_memory_limit ( std::size_t  bytes)
noexcept

Sets the memory the state may use while a script runs.

Parameters
bytesMemory of the whole state; a script that needs more fails with a memory error.

◆ text()

std::optional< std::string > aegis2d::script::ScriptState::text ( std::string_view  name) const

A global string.

Parameters
nameName of the global.
Returns
Its value, or nothing if it is not a string.

Friends And Related Symbol Documentation

◆ ScriptAccess

friend struct ScriptAccess
friend

Member Data Documentation

◆ default_instruction_limit

constexpr std::uint32_t aegis2d::script::ScriptState::default_instruction_limit = 10'000'000
staticconstexpr

Instructions a run or a call may take before it is stopped, by default.

◆ default_memory_limit

constexpr std::size_t aegis2d::script::ScriptState::default_memory_limit = std::size_t{64} * 1024 * 1024
staticconstexpr

Memory the state may use while a script runs, by default, in bytes.


The documentation for this class was generated from the following file: