Merge pull request 'Bounded Lua evaluator with transactional host results (#17)' (#33) from issue/17-bounded-lua into main
This commit was merged in pull request #33.
This commit is contained in:
commit
706383e13f
6 files changed
+552
No files matched your search
@@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Script/Types.hpp>
|
||||
|
||||
#include <expected>
|
||||
#include <functional>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Script
|
||||
{
|
||||
|
||||
/// Host function: receives integer arguments, returns an integer or an error
|
||||
/// message. It must not commit externally visible state itself; the evaluator
|
||||
/// records each call in `Result::host_calls` so the caller can commit after a
|
||||
/// successful evaluation.
|
||||
using HostFunction = std::function<std::expected<Integer, std::string>(
|
||||
std::span<const Integer>)>;
|
||||
|
||||
struct Host {
|
||||
std::string name;
|
||||
HostFunction function;
|
||||
};
|
||||
|
||||
/// Bounded Lua text evaluator.
|
||||
///
|
||||
/// Each `evaluate` call creates a fresh, isolated Lua state, applies the
|
||||
/// allocator, instruction and host-call limits, exposes the integer-keyed
|
||||
/// inputs as `input[key]`, registers the host functions as globals, and runs
|
||||
/// the chunk with a protected call. Only integer return values are accepted
|
||||
/// and collected into an owned result. On failure the state is discarded, so
|
||||
/// inputs and the caller's host state are untouched; a later valid evaluation
|
||||
/// starts clean.
|
||||
class LuaEvaluator
|
||||
{
|
||||
public:
|
||||
|
||||
explicit LuaEvaluator(Limits limits = Limits{},
|
||||
std::vector<Host> hosts = {})
|
||||
: limits_(limits)
|
||||
, hosts_(std::move(hosts))
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] const Limits& limits() const noexcept { return limits_; }
|
||||
|
||||
[[nodiscard]] std::expected<Result, Error>
|
||||
evaluate(std::string_view source,
|
||||
std::span<const Input> inputs = {}) const;
|
||||
|
||||
private:
|
||||
|
||||
Limits limits_;
|
||||
std::vector<Host> hosts_;
|
||||
};
|
||||
|
||||
} // namespace Void::Script
|
||||
@@ -0,0 +1,72 @@
|
||||
#pragma once
|
||||
|
||||
#include <compare>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Script
|
||||
{
|
||||
|
||||
using Integer = std::int64_t;
|
||||
|
||||
enum class ErrorKind : std::uint8_t {
|
||||
compile = 0,
|
||||
runtime,
|
||||
allocator_limit,
|
||||
instruction_limit,
|
||||
host_call_limit,
|
||||
result_limit,
|
||||
host_error,
|
||||
invalid_input,
|
||||
};
|
||||
|
||||
struct Error {
|
||||
ErrorKind kind{ErrorKind::runtime};
|
||||
std::string message;
|
||||
|
||||
[[nodiscard]] bool operator==(const Error&) const = default;
|
||||
};
|
||||
|
||||
struct Limits {
|
||||
/// Total bytes the VM may allocate (including the state and libraries).
|
||||
std::size_t max_allocator_bytes{1ULL * 1024ULL * 1024ULL};
|
||||
/// VM instructions before evaluation is aborted.
|
||||
std::size_t max_instructions{100'000};
|
||||
/// Host function invocations before evaluation is aborted.
|
||||
std::size_t max_host_calls{1'000};
|
||||
/// Maximum number of integer results collected.
|
||||
std::size_t max_result_integers{1'024};
|
||||
/// Maximum number of integer-keyed inputs.
|
||||
std::size_t max_inputs{256};
|
||||
};
|
||||
|
||||
/// Integer-keyed input exposed to the script as `input[key]`.
|
||||
struct Input {
|
||||
Integer key{0};
|
||||
Integer value{0};
|
||||
|
||||
[[nodiscard]] bool operator==(const Input&) const = default;
|
||||
};
|
||||
|
||||
/// One host function call observed during a successful evaluation. The caller
|
||||
/// may commit these effects after `evaluate` returns; the evaluator itself
|
||||
/// never commits host state.
|
||||
struct HostInvocation {
|
||||
std::string host;
|
||||
std::vector<Integer> arguments;
|
||||
Integer result{0};
|
||||
|
||||
[[nodiscard]] bool operator==(const HostInvocation&) const = default;
|
||||
};
|
||||
|
||||
/// Owned result of a successful evaluation.
|
||||
struct Result {
|
||||
/// Integer values returned by the chunk, in order.
|
||||
std::vector<Integer> integers;
|
||||
/// Host calls made by the script, in order.
|
||||
std::vector<HostInvocation> host_calls;
|
||||
};
|
||||
|
||||
} // namespace Void::Script
|
||||
Reference in new issue
Block a user