#pragma once #include #include #include #include #include #include namespace scripting { struct null {}; struct array; using script_value = std::variant; struct script_variable { std::string name; }; struct code_location { std::shared_ptr line_contents; int32_t line_number; int32_t column_number; }; struct script_error { std::shared_ptr location; std::string message; }; struct array { std::deque value; operator std::deque&() { return value; } }; using argument = std::variant; class UserScript; struct function_impl { virtual std::optional apply(UserScript* self, std::vector, std::optional&) = 0; virtual ~function_impl() = default; }; using function = std::unique_ptr; class UserScript { public: virtual std::optional> 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 varname_by_idx(int32_t idx) = 0; virtual std::optional var_by_idx(int32_t idx) = 0; virtual script_value resolve(const std::string& name) = 0; virtual std::variant> executeAtOnce(std::string code) = 0; virtual std::vector prepare(std::string code) = 0; virtual std::optional 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 prepare_interpreter(const std::string& code); std::unique_ptr register_array_lib(std::unique_ptr target, const UserScriptLibraryParameters&); std::unique_ptr register_crypto_lib(std::unique_ptr target, const UserScriptLibraryParameters&); std::unique_ptr register_utils_lib(std::unique_ptr target, const UserScriptLibraryParameters&); }