14#include "../core/convertible_through_base.hpp"
29 typedef void (SQPrintFunc)(
bool error_msg,
const std::string &message);
58 static void CompileError(HSQUIRRELVM
vm,
const SQChar *desc,
const SQChar *source, SQInteger line, SQInteger column);
63 static void RunError(HSQUIRRELVM
vm,
const SQChar *error);
68 static void PrintFunc(HSQUIRRELVM
vm,
const std::string &s);
90 bool LoadScript(HSQUIRRELVM
vm,
const std::string &script,
bool in_root =
true);
95 SQRESULT
LoadFile(HSQUIRRELVM
vm,
const std::string &filename, SQBool printerror);
101 void AddMethod(
const char *method_name, SQFUNCTION proc, uint nparam = 0,
const char *params =
nullptr,
void *userdata =
nullptr,
int size = 0);
107 void AddConst(
const char *var_name,
int value);
121 void AddConst(
const char *var_name,
bool value);
133 void AddClassBegin(
const char *class_name,
const char *parent_class);
144 bool Resume(
int suspend = -1);
156 void InsertResult(
bool result);
157 void InsertResult(
int result);
158 void InsertResult(uint result) { this->InsertResult((
int)result); }
159 void InsertResult(
ConvertibleThroughBase auto result) { this->InsertResult(
static_cast<int>(result.base())); }
165 bool CallMethod(HSQOBJECT instance,
const char *method_name, HSQOBJECT *ret,
int suspend);
166 bool CallMethod(HSQOBJECT instance,
const char *method_name,
int suspend) {
return this->
CallMethod(instance, method_name,
nullptr, suspend); }
167 bool CallStringMethod(HSQOBJECT instance,
const char *method_name, std::string *res,
int suspend);
168 bool CallIntegerMethod(HSQOBJECT instance,
const char *method_name,
int *res,
int suspend);
169 bool CallBoolMethod(HSQOBJECT instance,
const char *method_name,
bool *res,
int suspend);
174 bool MethodExists(HSQOBJECT instance,
const char *method_name);
186 static bool CreateClassInstanceVM(HSQUIRRELVM
vm,
const std::string &class_name,
void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook,
bool prepend_API_name =
false);
191 bool CreateClassInstance(
const std::string &class_name,
void *real_instance, HSQOBJECT *instance);
198 static SQUserPointer
GetRealInstance(HSQUIRRELVM
vm,
int index,
const char *tag);
205 static bool GetInstance(HSQUIRRELVM
vm, HSQOBJECT *ptr,
int pos = 1) { sq_getclass(
vm, pos); sq_getstackobj(
vm, pos, ptr); sq_pop(
vm, 1);
return true; }
210 static const char *
ObjectToString(HSQOBJECT *ptr) {
return sq_objtostring(ptr); }
220 static bool ObjectToBool(HSQOBJECT *ptr) {
return sq_objtobool(ptr) == 1; }
241 void ThrowError(
const std::string_view error) { sq_throwerror(this->vm, error); }
299 this->old_allocator = _squirrel_allocator;
301 _squirrel_allocator = engine !=
nullptr ? engine->
allocator.get() :
nullptr;
306 _squirrel_allocator = this->old_allocator;
Runtime information about a script like a pointer to the squirrel vm and the current state.
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 * GetGlobalPointer(HSQUIRRELVM vm)
Get the pointer as set by SetGlobalPointer.
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.
void AddConst(const char *var_name, uint value)
Adds a const to the stack.
static void ErrorPrintFunc(HSQUIRRELVM vm, const std::string &s)
If an error has to be print, this function is called.
void CollectGarbage()
Tell the VM to do a garbage collection run.
HSQUIRRELVM vm
The VirtualMachine instance for squirrel.
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 AddMethod(const char *method_name, SQFUNCTION proc, uint nparam=0, const char *params=nullptr, void *userdata=nullptr, int size=0)
Adds a function to the stack.
static void RunError(HSQUIRRELVM vm, const SQChar *error)
The RunError handler.
void ReleaseObject(HSQOBJECT *ptr)
Release a SQ object.
bool CanSuspend()
Are we allowed to suspend the squirrel script at this moment?
SQPrintFunc * print_func
Points to either nullptr, or a custom print handler.
static const char * ObjectToString(HSQOBJECT *ptr)
Convert a Squirrel-object to a string.
void ThrowError(const std::string_view error)
Throw a Squirrel error that will be nicely displayed to the user.
static void CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column)
The CompileError handler.
static SQInteger _RunError(HSQUIRRELVM vm)
The internal RunError handler.
size_t GetAllocatedMemory() const noexcept
Get number of bytes allocated by this VM.
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.
static void PrintFunc(HSQUIRRELVM vm, const std::string &s)
If a user runs 'print' inside a script, this function gets the params.
bool HasScriptCrashed()
Find out if the squirrel script made an error before.
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 bool GetInstance(HSQUIRRELVM vm, HSQOBJECT *ptr, int pos=1)
Get the Squirrel-instance pointer.
void Initialize()
Perform all initialization steps to create the engine.
bool crashed
True if the squirrel script made an error.
static SQUserPointer GetRealInstance(HSQUIRRELVM vm, int index, const char *tag)
Get the real-instance pointer.
static bool ObjectToBool(HSQOBJECT *ptr)
Convert a Squirrel-object to a bool.
bool CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret, int suspend)
Call a method of an instance, in various flavors.
void AddClassBegin(const char *class_name)
Adds a class to the global scope.
int overdrawn_ops
The amount of operations we have overdrawn.
void Uninitialize()
Perform all the cleanups for the engine.
const char * GetAPIName()
Get the API name.
const char * APIName
Name of the API used for this squirrel.
void ResumeError()
Resume the VM with an error so it prints a stack trace.
void AddConst(const char *var_name, int value)
Adds a const to the stack.
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 MethodExists(HSQOBJECT instance, const char *method_name)
Check if a method exists in an instance.
bool CreateClassInstance(const std::string &class_name, void *real_instance, HSQOBJECT *instance)
Exactly the same as CreateClassInstanceVM, only callable without instance of Squirrel.
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...
ScriptType
The type of script we're working with, i.e.
@ AI
The script is for AI scripts.
@ GS
The script is for Game scripts.