25 friend class ScriptAllocatorScope;
26 friend class ScriptInstance;
29 using SQPrintFunc = void (
bool error_msg, std::string_view message);
66 static void CompileError(HSQUIRRELVM
vm, std::string_view desc, std::string_view source, SQInteger line, SQInteger column);
73 static void RunError(HSQUIRRELVM
vm, std::string_view error);
80 static void PrintFunc(HSQUIRRELVM
vm, std::string_view s);
97 HSQUIRRELVM
GetVM() {
return this->vm; }
105 bool LoadScript(HSQUIRRELVM
vm,
const std::string &script,
bool in_root =
true);
114 SQRESULT
LoadFile(HSQUIRRELVM
vm,
const std::string &filename, SQBool printerror);
126 void AddMethod(std::string_view method_name, SQFUNCTION proc, std::string_view params = {},
void *userdata =
nullptr,
int size = 0,
bool suspendable =
false);
134 void AddConst(std::string_view var_name, SQInteger value);
142 void AddConst(std::string_view var_name, uint value) { this->
AddConst(var_name, (SQInteger)value); }
150 void AddConst(std::string_view var_name,
int value) { this->
AddConst(var_name, (SQInteger)value); }
166 void AddConst(std::string_view var_name,
bool value);
181 void AddClassBegin(std::string_view class_name, std::string_view parent_class);
205 bool Resume(
int suspend = -1);
217 void InsertResult(
bool result);
218 void InsertResult(
int result);
219 void InsertResult(uint result) { this->InsertResult((
int)result); }
220 void InsertResult(ConvertibleThroughBase
auto result) { this->InsertResult(
static_cast<int>(result.base())); }
230 bool CallMethod(HSQOBJECT instance, std::string_view method_name, HSQOBJECT *ret,
int suspend);
239 bool CallMethod(HSQOBJECT instance, std::string_view method_name,
int suspend) {
return this->
CallMethod(instance, method_name,
nullptr, suspend); }
249 bool CallStringMethod(HSQOBJECT instance, std::string_view method_name, std::string *res,
int suspend);
259 bool CallIntegerMethod(HSQOBJECT instance, std::string_view method_name,
int *res,
int suspend);
269 bool CallBoolMethod(HSQOBJECT instance, std::string_view method_name,
bool *res,
int suspend);
277 bool MethodExists(HSQOBJECT instance, std::string_view method_name);
289 static bool CreateClassInstanceVM(HSQUIRRELVM
vm,
const std::string &class_name,
void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook,
bool prepend_API_name =
false);
298 bool CreateClassInstance(
const std::string &class_name,
void *real_instance, HSQOBJECT *instance);
309 static SQUserPointer
GetRealInstance(HSQUIRRELVM
vm,
int index, std::string_view tag);
319 static void GetInstance(HSQUIRRELVM
vm, HSQOBJECT *ptr,
int pos = 1) { sq_getclass(
vm, pos); sq_getstackobj(
vm, pos, ptr); sq_pop(
vm, 1); }
326 static std::optional<std::string_view>
ObjectToString(HSQOBJECT *ptr) {
return sq_objtostring(ptr); }
340 static bool ObjectToBool(HSQOBJECT *ptr) {
return sq_objtobool(ptr) == 1; }
366 void ThrowError(std::string_view error) { sq_throwerror(this->vm, error); }
437class ScriptAllocatorScope {
441 ScriptAllocatorScope(
const Squirrel *engine)
443 this->old_allocator = _squirrel_allocator;
445 _squirrel_allocator = engine !=
nullptr ? engine->
allocator.get() :
nullptr;
451 _squirrel_allocator = this->old_allocator;
~ScriptAllocatorScope()
Restore the previous allocator.
void AddClassEnd()
Finishes adding a class to the global scope.
bool Resume(int suspend=-1)
Resume a VM when it was suspended via a throw.
static void GetInstance(HSQUIRRELVM vm, HSQOBJECT *ptr, int pos=1)
Get the Squirrel-instance pointer.
static void * GetGlobalPointer(HSQUIRRELVM vm)
Get the pointer as set by SetGlobalPointer.
static SQUserPointer GetRealInstance(HSQUIRRELVM vm, int index, std::string_view tag)
Get the real-instance pointer.
void AddConst(std::string_view var_name, SQInteger value)
Adds a const to the stack.
void * global_pointer
Can be set by who ever initializes Squirrel.
void SetPrintFunction(SQPrintFunc *func)
Set a custom print function, so you can handle outputs from SQ yourself.
bool CallIntegerMethod(HSQOBJECT instance, std::string_view method_name, int *res, int suspend)
Call a method of an instance returning a integer.
void CollectGarbage()
Tell the VM to do a garbage collection run.
HSQUIRRELVM vm
The VirtualMachine instance for squirrel.
void AddMethod(std::string_view method_name, SQFUNCTION proc, std::string_view params={}, void *userdata=nullptr, int size=0, bool suspendable=false)
Adds a function to the stack.
void Reset()
Completely reset the engine; start from scratch.
SQInteger GetOpsTillSuspend()
How many operations can we execute till suspension?
bool LoadScript(const std::string &script)
Load a script.
bool IsSuspended()
Did the squirrel code suspend or return normally.
void ReleaseObject(HSQOBJECT *ptr)
Release a SQ object.
bool CanSuspend()
Are we allowed to suspend the squirrel script at this moment?
std::string_view api_name
Name of the API used for this squirrel.
SQPrintFunc * print_func
Points to either nullptr, or a custom print handler.
void AddConst(std::string_view var_name, const ConvertibleThroughBase auto &value)
Adds a const to the stack.
~Squirrel()
Clean up the Squirrel virtual machine state.
static void DecreaseAllocatedSize(size_t bytes)
Decrease number of bytes allocated in the current script allocator scope.
void AddClassBegin(std::string_view class_name)
Adds a class to the global scope.
void ThrowError(std::string_view error)
Throw a Squirrel error that will be nicely displayed to the user.
static SQInteger _RunError(HSQUIRRELVM vm)
The internal RunError handler.
size_t GetAllocatedMemory() const noexcept
Get number of bytes allocated by this VM.
void AddScopedEnumBegin(std::string_view enum_name)
Adds an enum to the scope.
void SetGlobalPointer(void *ptr)
Sets a pointer in the VM that is reachable from where ever you are in SQ.
static void DecreaseOps(HSQUIRRELVM vm, int amount)
Tell the VM to remove amount ops from the number of ops till suspend.
bool HasScriptCrashed()
Find out if the squirrel script made an error before.
bool CallBoolMethod(HSQOBJECT instance, std::string_view method_name, bool *res, int suspend)
Call a method of an instance returning a boolean.
void CrashOccurred()
Set the script status to crashed.
SQRESULT LoadFile(HSQUIRRELVM vm, const std::string &filename, SQBool printerror)
Load a file to a given VM.
HSQUIRRELVM GetVM()
Get the squirrel VM.
static void CompileError(HSQUIRRELVM vm, std::string_view desc, std::string_view source, SQInteger line, SQInteger column)
The CompileError handler.
static void IncreaseAllocatedSize(size_t bytes)
Increase number of bytes allocated in the current script allocator scope.
static void RunError(HSQUIRRELVM vm, std::string_view error)
The RunError handler.
void Initialize()
Perform all initialization steps to create the engine.
bool crashed
True if the squirrel script made an error.
static void PrintFunc(HSQUIRRELVM vm, std::string_view s)
If a user runs 'print' inside a script, this function gets the params.
static bool ObjectToBool(HSQOBJECT *ptr)
Convert a Squirrel-object to a bool.
int overdrawn_ops
The amount of operations we have overdrawn.
static std::optional< std::string_view > ObjectToString(HSQOBJECT *ptr)
Convert a Squirrel-object to a string.
void AddScopedEnumEnd()
Finishes adding an enum to the scope.
void Uninitialize()
Perform all the cleanups for the engine.
std::string_view GetAPIName()
Get the API name.
bool CallMethod(HSQOBJECT instance, std::string_view method_name, int suspend)
Call a method of an instance returning nothing.
bool CallMethod(HSQOBJECT instance, std::string_view method_name, HSQOBJECT *ret, int suspend)
Call a method of an instance returning a Squirrel object.
void AddConst(std::string_view var_name, uint value)
Adds a const to the stack.
bool MethodExists(HSQOBJECT instance, std::string_view method_name)
Check if a method exists in an instance.
void ResumeError()
Resume the VM with an error so it prints a stack trace.
static bool CreateClassInstanceVM(HSQUIRRELVM vm, const std::string &class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook, bool prepend_API_name=false)
Creates a class instance.
bool CreateClassInstance(const std::string &class_name, void *real_instance, HSQOBJECT *instance)
Create a class instance.
static void ErrorPrintFunc(HSQUIRRELVM vm, std::string_view s)
If an error has to be print, this function is called.
void AddConst(std::string_view var_name, int value)
Adds a const to the stack.
bool CallStringMethod(HSQOBJECT instance, std::string_view method_name, std::string *res, int suspend)
Call a method of an instance returning a string.
static int ObjectToInteger(HSQOBJECT *ptr)
Convert a Squirrel-object to an integer.
std::unique_ptr< ScriptAllocator > allocator
Allocator object used by this script.
A type is considered 'convertible through base()' when it has a 'base()' function that returns someth...
Concept for unifying the convert through 'base()' behaviour of several 'strong' types.
ScriptType
The type of script we're working with, i.e.
@ GS
The script is for Game scripts.