minilua::CallContext class

Argument for lua Functions.

Contains information for use in the implementation of native (and lua) functions.

Contains the arguments and the environment.

Constructors, destructors, conversion operators

CallContext(Environment* env)
Create a CallContext from the Environment.
CallContext(const CallContext&)
Copy constructor.
CallContext(CallContext&&)
Move constructor.

Public functions

auto operator=(const CallContext&) -> CallContext & -> auto
Copy assignment operator.
auto operator=(CallContext&&) -> CallContext & -> auto
Move assignment operator.
auto make_new(Vallist, std::optional<Range> location = std::nullopt) const -> CallContext -> auto
Create a new call context with new arguments.
template<typename... Args>
auto make_new(Args... args, std::optional<Range> location = std::nullopt) const -> CallContext -> auto
Create a new call context with new arguments.
auto make_table() const -> Table -> auto
Create a new table with the same allocator as the environment.
auto call_location() const -> std::optional< Range > -> auto
The location of the call.
auto environment() const -> Environment & -> auto
Returns a reference to the environment.
auto get(const std::string& name) const -> Value -> auto
Returns the value of a variable accessible from the function.
auto arguments() const -> const Vallist & -> auto
Returns the arguments given to this function.
template<typename... Ts>
auto expect_argument(size_t index) const -> const Value & -> auto
Helper to check required arguments.
auto unary_numeric_arg_helper() const -> std::tuple< Number, UnaryOrigin > -> auto
Convenience method for writing unary numeric functions.
auto binary_numeric_args_helper() const -> std::tuple< Number, Number, BinaryOrigin > -> auto
Convenience method for writing unary numeric functions.

Function documentation

minilua::CallContext::CallContext(Environment* env)

Create a CallContext from the Environment.

auto minilua::CallContext::make_new(Vallist, std::optional<Range> location = std::nullopt) const -> CallContext

Create a new call context with new arguments.

It reuses all other information.

template<typename... Args>
auto minilua::CallContext::make_new(Args... args, std::optional<Range> location = std::nullopt) const -> CallContext

Create a new call context with new arguments.

It reuses all other information.

auto minilua::CallContext::call_location() const -> std::optional< Range >

The location of the call.

Can be std::nullopt if the CallContext was not created by a lua function call.

auto minilua::CallContext::environment() const -> Environment &

Returns a reference to the environment.

For Functions created in C++ you can only access the global environment.

For functions created in lua you can access the global environment and local variables that were in scope when creating the function.

auto minilua::CallContext::get(const std::string& name) const -> Value

Returns the value of a variable accessible from the function.

Returns Nil if the variable is not accessible or does not exist.

template<typename... Ts>
auto minilua::CallContext::expect_argument(size_t index) const -> const Value &

Helper to check required arguments.

Usage:

Value table = ctx.expect_argument<Table>(0);
Value nil_or_table = ctx.expect_argument<Nil, Table>(1);
Value string_or_number = ctx.expect_argument<String, Number>(2);

If the type does not match or the value is not present this method will throw a lua appropriate exception that looks something like this:

bad argument #1 (table expected, got no value)

auto minilua::CallContext::unary_numeric_arg_helper() const -> std::tuple< Number, UnaryOrigin >

Convenience method for writing unary numeric functions.

Convenience method for writing functions with one numeric argument that should track the origin (e.g. pow).

Usage:

auto [arg, origin] = ctx.unary_numeric_arg_helper();
origin.reverse = unary_num_reverse(...);
// ...

See also: CallContext::binary_numeric_args_helper.

auto minilua::CallContext::binary_numeric_args_helper() const -> std::tuple< Number, Number, BinaryOrigin >

Convenience method for writing unary numeric functions.

Convenience method for writing functions with two numeric arguments that should track the origin (e.g. sqrt).

Usage:

auto [arg1, arg2, origin] = ctx.binary_numeric_arg_helper();
origin.reverse = binary_num_reverse(...);
// ...

See also: CallContext::unary_numeric_arg_helper.