|
1 /* $Id$ */ |
|
2 |
|
3 /** @file squirrel.hpp defines the Squirrel class */ |
|
4 |
|
5 #ifndef SQUIRREL_HPP |
|
6 #define SQUIRREL_HPP |
|
7 |
|
8 class Squirrel { |
|
9 private: |
|
10 HSQUIRRELVM vm; ///< The VirtualMachine instnace for squirrel |
|
11 |
|
12 /** |
|
13 * The internal RunError handler. It looks up the real error and calls RunError with it. |
|
14 */ |
|
15 static SQInteger _RunError(HSQUIRRELVM vm); |
|
16 |
|
17 protected: |
|
18 /** |
|
19 * The CompileError handler. |
|
20 */ |
|
21 static void CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column); |
|
22 |
|
23 /** |
|
24 * The RunError handler. |
|
25 */ |
|
26 static void RunError(HSQUIRRELVM vm, const char *error); |
|
27 |
|
28 /** |
|
29 * If a user runs 'print' inside a script, this function gets the params. |
|
30 */ |
|
31 static void PrintFunc(HSQUIRRELVM vm, const SQChar *s, ...); |
|
32 |
|
33 public: |
|
34 Squirrel(); |
|
35 ~Squirrel(); |
|
36 |
|
37 /** |
|
38 * Load a script. |
|
39 * @param script The full script-name to load. |
|
40 * @return False if loading failed. |
|
41 */ |
|
42 bool LoadScript(const char *script); |
|
43 |
|
44 /** |
|
45 * Adds a function to the stack. Depending on the current state this means |
|
46 * either a method or a global function. |
|
47 */ |
|
48 void AddMethod(const char *method_name, SQFUNCTION proc, uint nparam, const char *params); |
|
49 void AddMethod(const char *method_name, SQFUNCTION proc); |
|
50 void AddMethod(const char *method_name, SQFUNCTION proc, void *userdata, int size); |
|
51 |
|
52 /** |
|
53 * Adds a class to the global scope. Make sure to call AddClassEnd when you |
|
54 * are done adding methods. |
|
55 */ |
|
56 void AddClassBegin(const char *class_name); |
|
57 |
|
58 /** |
|
59 * Finishes adding a class to the global scope. If this isn't called, no |
|
60 * class is really created. |
|
61 */ |
|
62 void AddClassEnd(); |
|
63 |
|
64 /** |
|
65 * Call a method of an instance, in various flavors. |
|
66 */ |
|
67 void CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret); |
|
68 void CallMethod(HSQOBJECT instance, const char *method_name) { this->CallMethod(instance, method_name, NULL); } |
|
69 const char *CallStringMethod(HSQOBJECT instance, const char *method_name) { HSQOBJECT ret; this->CallMethod(instance, method_name, &ret); return ObjectToString(&ret); } |
|
70 int CallIntegerMethod(HSQOBJECT instance, const char *method_name) { HSQOBJECT ret; this->CallMethod(instance, method_name, &ret); return ObjectToInteger(&ret); } |
|
71 |
|
72 /** |
|
73 * Check if a method exists in an instance. |
|
74 */ |
|
75 bool MethodExists(HSQOBJECT instance, const char *method_name); |
|
76 |
|
77 /** |
|
78 * Creates a class instance. |
|
79 * @param class_name The name of the class of which we create an instance. |
|
80 * @param real_instance The instance to the real class, if it represents a real class. |
|
81 * @param instance Returning value with the pointer to the instance. |
|
82 * @return False if creating failed. |
|
83 */ |
|
84 bool CreateClassInstance(const char *class_name, void *real_instance, HSQOBJECT *instance); |
|
85 |
|
86 /** |
|
87 * Get the real-instance pointer. |
|
88 * @note This will only work just after a function-call from within Squirrel |
|
89 * to your C++ function. |
|
90 */ |
|
91 static bool GetRealInstance(HSQUIRRELVM vm, SQUserPointer *ptr) { return SQ_SUCCEEDED(sq_getinstanceup(vm, 1, ptr, 0)); } |
|
92 |
|
93 /** |
|
94 * Get the Squirrel-instance pointer. |
|
95 * @note This will only work just after a function-call from within Squirrel |
|
96 * to your C++ function. |
|
97 */ |
|
98 static bool GetInstance(HSQUIRRELVM vm, HSQOBJECT *ptr) { sq_getclass(vm, 1); sq_getstackobj(vm, 1, ptr); return true; } |
|
99 |
|
100 /** |
|
101 * Convert a Squirrel-object to a string. |
|
102 */ |
|
103 static const char *ObjectToString(HSQOBJECT *ptr) { return sq_objtostring(ptr); } |
|
104 |
|
105 /** |
|
106 * Convert a Squirrel-object to an integer. |
|
107 */ |
|
108 static int ObjectToInteger(HSQOBJECT *ptr) { return sq_objtointeger(ptr); } |
|
109 |
|
110 /** |
|
111 * Sets a pointer in the VM that is reasable from where ever you are, what |
|
112 * ever your state is. Useful to keep track of the main instance. |
|
113 */ |
|
114 void SetGlobalPointer(void *ptr) { sq_setforeignptr(this->vm, ptr); } |
|
115 |
|
116 /** |
|
117 * Get the pointer as set by SetGlobalPointer. |
|
118 */ |
|
119 static void *GetGlobalPointer(HSQUIRRELVM vm) { return sq_getforeignptr(vm); } |
|
120 |
|
121 /** |
|
122 * Throw a Squirrel error that will be nicely displayed to the user. |
|
123 */ |
|
124 void ThrowError(const char *error) { sq_throwerror(this->vm, error); } |
|
125 }; |
|
126 |
|
127 #endif /* SQUIRREL_HPP */ |