|
#pragma once
|
|
#include <memory>
|
|
#include <string>
|
|
#include <variant>
|
|
#include <deque>
|
|
#include <vector>
|
|
#include <optional>
|
|
|
|
namespace scripting {
|
|
struct null {};
|
|
struct array;
|
|
|
|
using script_value = std::variant<null, int32_t, std::string, array>;
|
|
struct script_variable {
|
|
std::string name;
|
|
};
|
|
|
|
struct code_location {
|
|
std::shared_ptr<const std::string> line_contents;
|
|
int32_t line_number;
|
|
int32_t column_number;
|
|
};
|
|
|
|
struct script_error {
|
|
std::shared_ptr<const code_location> location;
|
|
std::string message;
|
|
};
|
|
|
|
struct array {
|
|
std::deque<script_value> value;
|
|
operator std::deque<script_value>&() {
|
|
return value;
|
|
}
|
|
};
|
|
|
|
using argument = std::variant<script_value, script_variable>;
|
|
|
|
class UserScript;
|
|
|
|
struct function_impl {
|
|
virtual std::optional<script_value> apply(UserScript* self, std::vector<argument>, std::optional<script_error>&) = 0;
|
|
virtual ~function_impl() = default;
|
|
};
|
|
|
|
using function = std::unique_ptr<function_impl>;
|
|
|
|
class UserScript {
|
|
public:
|
|
virtual std::optional<std::reference_wrapper<script_value>> getValue(const std::string& name) = 0;
|
|
virtual bool setValue(const std::string& name, script_value value) = 0;
|
|
virtual void registerFunction(std::string name, function fn) = 0;
|
|
virtual void clear_variables() = 0;
|
|
virtual int32_t op_count() = 0;
|
|
virtual int32_t var_count() = 0;
|
|
virtual void var_clear() = 0;
|
|
virtual std::optional<script_value> varname_by_idx(int32_t idx) = 0;
|
|
virtual std::optional<script_value> var_by_idx(int32_t idx) = 0;
|
|
virtual script_value resolve(const std::string& name) = 0;
|
|
virtual std::variant<script_value, std::vector<script_error>> executeAtOnce(std::string code) = 0;
|
|
virtual std::vector<script_error> prepare(std::string code) = 0;
|
|
virtual std::optional<script_error> stepOnce() = 0;
|
|
virtual ~UserScript() = default;
|
|
};
|
|
|
|
struct UserScriptLibraryParameters {
|
|
bool recursive_arrays = false;
|
|
int32_t array_size_limit = 1024;
|
|
int32_t string_size_limit = 1024;
|
|
int32_t variables_count = 1024;
|
|
};
|
|
|
|
std::unique_ptr<UserScript> prepare_interpreter(const std::string& code);
|
|
std::unique_ptr<UserScript> register_array_lib(std::unique_ptr<UserScript> target, const UserScriptLibraryParameters&);
|
|
std::unique_ptr<UserScript> register_crypto_lib(std::unique_ptr<UserScript> target, const UserScriptLibraryParameters&);
|
|
std::unique_ptr<UserScript> register_utils_lib(std::unique_ptr<UserScript> target, const UserScriptLibraryParameters&);
|
|
}
|