minilua namespace

Public API of the MiniLua library.

This namespaces defines the public api of the MiniLua library.

For more information see the main page.

Namespaces

namespace details
Users of the library should ignore this namespace. It is only usable internally.
namespace math
Math functions in the lua standard library.
namespace mt
contains the metatable events.

Classes

class BadArgumentError
Exception indicating a bad argument of a function call.
template<typename Fn, typename ReverseLeft, typename ReverseRight>
struct BinaryNumericFunctionHelper
Helper for creating a reversible numeric binary function.
struct BinaryOrigin
Origin for a Value that was created in a binary operation (or some functions with two arguments).
struct Bool
A lua boolean value.
class CallContext
Argument for lua Functions.
class CallResult
Result of calling a lua Function.
struct CommonSCInfo
Common information for source changes.
class Env
class Environment
The environment/configuration for the Interpreter.
struct EvalResult
Result of calling Interpreter::evaluate.
struct ExternalOrigin
Can be used for externally produced values but is mainly a placeholder.
class Function
A lua function value.
class Interpreter
An interpreter instance is used to parse and evaluate lua source code.
struct InterpreterConfig
Configuration for the Interpreter.
class InterpreterException
Exception thrown by the interpreter.
template<typename _Tp, typename dummy = void>
struct is_printable
Can be used in std::enable_if to check for printable types.
struct LiteralOrigin
Origin for a Value that was created from a literal in code.
struct Location
A location in source code.
class MemoryAllocator
A memory allocator for the Tables.
struct MultipleArgsOrigin
struct Nil
A lua nil value.
struct NoOrigin
Default origin for Values.
class Number
class Origin
The origin of a value.
template<class... Ts>
struct overloaded
Overloading trick to make functions take multiple overloaded lambdas.
template<typename T>
class owning_ptr
Heap allocated owned value.
struct ParseResult
Result of calling Interpreter::parse.
struct Range
A range (sometimes called span) in source code.
struct SourceChange
A source change for a single location.
struct SourceChangeAlternative
Multiple source changes where only one can be applied (i.e. alternatives).
struct SourceChangeCombination
Multiple source changes that all need to be applied together (i.e. in combination).
class SourceChangeTree
Wrapper for the source change tree.
struct String
A lua string value.
class Table
A lua table value.
template<typename Fn, typename Reverse>
struct UnaryNumericFunctionHelper
Helper for creating reversible numeric unary function.
struct UnaryOrigin
Origin for a Value that was created in a unary operation (or some functions with one argument).
class Vallist
A Vallist can contain an arbitrary amount of Values.
class Value
Represents a value in lua.

Typedefs

using LocalEnv = std::unordered_map<std::string, std::shared_ptr<Value>>

Functions

template<typename Fn>
auto unary_num_reverse(Fn fn) -> auto
Helper to create the reverse function for UnaryOrigin or UnaryNumericFunctionHelper.
template<typename FnLeft, typename FnRight>
auto binary_num_reverse(FnLeft fn_left, FnRight fn_right, std::string origin = "") -> auto
Helper to create the reverse function for BinaryOrigin or BinaryNumericFunctionHelper.
template<typename... Ts>
UnaryNumericFunctionHelper(Ts...) -> UnaryNumericFunctionHelper< Ts... >
template<class... Ts>
BinaryNumericFunctionHelper(Ts...) -> BinaryNumericFunctionHelper< Ts... >
auto parse_number_literal(const std::string& str) -> Value -> auto
Parse a string into a lua value number.
auto parse_string_literal(const std::string& str) -> Value -> auto
Parse and escape a string into a lua value string.
auto simplify(const std::optional<SourceChangeTree>& tree) -> std::optional< SourceChangeTree > -> auto
See SourceChangeTree::simplify.
auto combine_source_changes(const std::optional<SourceChangeTree>& lhs, const std::optional<SourceChangeTree>& rhs) -> std::optional< SourceChangeTree > -> auto
Combines two source changes using a SourceChangeCombination if necessary.
template<typename T, typename... Args>
auto make_owning(Args... args) -> owning_ptr< T > -> auto
auto to_string_with_base(int number, int base) -> std::string -> auto
template<typename Fn>
auto with_call_stack(Fn f, const std::string& function_name, const StackItem& item) -> auto
Execute the given function and correctly re-throw exceptions using the given function name and stack item.
auto create_string_table(MemoryAllocator* allocator) -> Table -> auto
auto create_math_table(MemoryAllocator* allocator) -> Table -> auto
auto create_table_table(MemoryAllocator* allocator) -> Table -> auto
auto force(const CallContext& ctx) -> CallResult -> auto
Tries to force the first argument to take on the value of the second argument.
auto to_string(const CallContext& ctx) -> CallResult -> auto
Converts the value to a string.
auto pcall(const CallContext& ctx) -> CallResult -> auto
Calls the given function with the given arguments and catches errors.
auto select(const CallContext& ctx) -> Vallist -> auto
auto discard_origin(const CallContext& ctx) -> Vallist -> auto
void debug_print(const CallContext& ctx)
auto get_metatable(const CallContext& ctx) -> Value -> auto
auto set_metatable(const CallContext& ctx) -> Value -> auto
auto rawget(const CallContext& ctx) -> Value -> auto
auto rawset(const CallContext& ctx) -> Value -> auto
static auto check_and_get_trim(const std::string& s) -> size_t -> auto
auto operator==(const Table& a, const Table& b) noexcept -> bool -> auto
auto operator!=(const Table& a, const Table& b) noexcept -> bool -> auto

Variables

MemoryAllocator GLOBAL_ALLOCATOR
The global memory allocator.

Typedef documentation

using minilua::LocalEnv = std::unordered_map<std::string, std::shared_ptr<Value>>

Type used for the local environment.

This is different from the global environment because it is handled differently when capturing it in functions.

This is also not a Table because we need to copy it when capturing local variables for a function. Using a Table would cause wrong behaviour in these cases. E.g. variables declared after the capturing function should not be accessible in the function.

shared_ptr is used for the value because we need to be able to assign to the variable.

Usage: When a new block is started (e.g. the body of a for loop) the local environment shoule be copied so it can be extended with new local variables without changing the old environment.

TODO potential memory and performance improvements by nesting multiple local environments (lower memory footprint, faster creation, slower lookup)

TODO maybe replace the shared_ptr<Value> by something else we also use in the interpreter

Function documentation

template<typename Fn>
auto minilua::unary_num_reverse(Fn fn)

Helper to create the reverse function for UnaryOrigin or UnaryNumericFunctionHelper.

You only need to supply a function that accepts one Number (the new value for the result) and returns Number (the new value for the parameter).

See also: binary_num_reverse.

template<typename FnLeft, typename FnRight>
auto minilua::binary_num_reverse(FnLeft fn_left, FnRight fn_right, std::string origin = "")

Helper to create the reverse function for BinaryOrigin or BinaryNumericFunctionHelper.

You only need to supply a function that accepts two Numbers (the new value and old lhs/rhs value) and returns Number (the new value for the lhs/rhs parameter).

See also: unary_num_reverse.

template<typename... Ts>
minilua::UnaryNumericFunctionHelper(Ts...) -> UnaryNumericFunctionHelper< Ts... >

deduction guide

template<class... Ts>
minilua::BinaryNumericFunctionHelper(Ts...) -> BinaryNumericFunctionHelper< Ts... >

deduction guide

template<typename T, typename... Args>
auto minilua::make_owning(Args... args) -> owning_ptr< T >

@breif Helper function to create owning_ptr.

Similar to std::make_unique, etc.

auto minilua::to_string_with_base(int number, int base) -> std::string

@breif Convert int to string in the given base.

Uses character 0-9 and A-Z. So base has to be between 2 and 36.

auto minilua::create_string_table(MemoryAllocator* allocator) -> Table

Returns a table with all string functions

auto minilua::create_math_table(MemoryAllocator* allocator) -> Table

Returns a table with all the math functions.

auto minilua::create_table_table(MemoryAllocator* allocator) -> Table

Returns a table with all the table functions.

auto minilua::force(const CallContext& ctx) -> CallResult

Tries to force the first argument to take on the value of the second argument.

The same as calling arg1.force(arg2) on the Values in C++ where arg1 and arg2 are the first and second arguments.

auto minilua::to_string(const CallContext& ctx) -> CallResult

Converts the value to a string.

Tables and functions will not return a meaningful string. They will only be represented as their address.

Will respect the metamethod __tostring.

auto minilua::pcall(const CallContext& ctx) -> CallResult

Calls the given function with the given arguments and catches errors.

Calls the given function (first argument) with the given arguments (rest of the arguments) and catches any errors.

If the function raise an error this function returns two values: false and the error message. If the function does not raise an error this function will return true and all return values of the called functions.

auto minilua::select(const CallContext& ctx) -> Vallist

If index is a number, returns all arguments after the index-th argument (including index-th argument).

If index is a negative number, the index counting starts at the end (so -1 is the index of the last argument). If index is the string "#", select returns the total amount of extra arguments it received. If index is positive and bigger than the size select just returns an empty Vallist. If index in an invalid index (0 or negative bigger than size) an index out of range error is thrown if index is an invalid value, an error is thrown that an invalid index was entered.

auto minilua::discard_origin(const CallContext& ctx) -> Vallist

Remove the origin of all passed in values and return them.

void minilua::debug_print(const CallContext& ctx)

Debug print the values (this will also print tables but not functions).

auto minilua::get_metatable(const CallContext& ctx) -> Value

Returns the metatable of the first parameter or nil if there is none.

auto minilua::set_metatable(const CallContext& ctx) -> Value

Sets the metatable (second parameter) for the table (first parameter) and return the table.

Will error if the first value is not a table or second value is not a table or nil.

auto minilua::rawget(const CallContext& ctx) -> Value

Gets the real value of table[index], without invoking any metamethod.

First param is the table, second param is the index.

Will error if the table is not a Table.

auto minilua::rawset(const CallContext& ctx) -> Value

Sets the real value of table[index] to value, without invoking any metamethod.

First param is the table, second param is the index and third param is the value.

Will error if the table is not a Table or if the index is invalid.

This function returns table.

static auto minilua::check_and_get_trim(const std::string& s) -> size_t

Helper function to check if the string literal "delimiters" are correct and get their length.

Delimiters are either " or ‘’on both sides of the string or[=[on the start and]=]on the end. But the number of=has to match (can also be no=`).

auto minilua::operator==(const Table& a, const Table& b) noexcept -> bool

Does not compare the content of two tables, only if the table actually represent the same table.

auto minilua::operator!=(const Table& a, const Table& b) noexcept -> bool

Does not compare the content of two tables, only if the table actually represent the same table.

Variable documentation

MemoryAllocator minilua::GLOBAL_ALLOCATOR

The global memory allocator.

This is meant only for use for values outside of the interpreter and outside of Functions.

This will get freed when the program terminates. You can also manually free it but you need to be absolutely certain that none of the values allocated with it are not in use anymore.