4 #ifndef _SPIDERSCRIPT_H_
5 #define _SPIDERSCRIPT_H_
9 #define ERRPTR ((void*)((intptr_t)0-1))
12 * \brief Opaque script handle
14 typedef struct sSpiderScript tSpiderScript;
16 typedef struct sSpiderVariant tSpiderVariant;
17 typedef struct sSpiderNamespace tSpiderNamespace;
18 typedef struct sSpiderFunction tSpiderFunction;
19 typedef struct sSpiderValue tSpiderValue;
20 typedef struct sSpiderObjectDef tSpiderObjectDef;
21 typedef struct sSpiderObject tSpiderObject;
25 * \brief SpiderScript Variable Datatypes
26 * \todo Expand the descriptions
28 enum eSpiderScript_DataTypes
31 * \brief Undefined data
32 * \note Default type of an undefined dynamic variable
36 * \brief Dynamically typed variable
37 * \note Used to dentote a non-fixed type for function parameters
41 * \brief Opaque Data Pointer
43 * Opaque data types are used for resource handles or for system buffers.
47 * \brief Object reference
49 * A reference to a SpiderScript class instance. Can be accessed
50 * using the -> operator.
54 * \brief Array data type (invalid when using static typing)
58 * \brief Integer datatype
63 SS_DATATYPE_REAL, //!< Real Number (double)
64 SS_DATATYPE_STRING, //!< String
68 #define SS_MAKEARRAY(_type) ((_type) + 0x10000)
69 #define SS_DOWNARRAY(_type) ((_type) - 0x10000)
70 #define SS_GETARRAYDEPTH(_type) ((_type) >> 16)
89 SS_VALUEOP_SHIFTRIGHT,
94 * \brief Namespace definition
96 struct sSpiderNamespace
98 tSpiderNamespace *Next;
100 tSpiderNamespace *FirstChild;
102 tSpiderFunction *Functions;
104 tSpiderObjectDef *Classes;
106 int NConstants; //!< Number of constants
107 tSpiderValue *Constants; //!< Number of constants
113 * \brief Variant of SpiderScript
115 struct sSpiderVariant
117 const char *Name; // Just for debug
119 int bDyamicTyped; //!< Use dynamic typing
120 int bImplicitCasts; //!< Allow implicit casts (casts to lefthand side)
122 tSpiderFunction *Functions; //!< Functions (Linked List)
124 int NConstants; //!< Number of constants
125 tSpiderValue *Constants; //!< Number of constants
127 tSpiderNamespace RootNamespace;
131 * \brief SpiderScript data object
135 enum eSpiderScript_DataTypes Type; //!< Variable type
136 int ReferenceCount; //!< Reference count
139 int64_t Integer; //!< Integer data
140 double Real; //!< Real Number data
145 int Length; //!< Length
146 char Data[]; //!< Actual string (\a Length bytes)
149 * \brief Variable data
152 int Length; //!< Length of the array
153 tSpiderValue *Items[]; //!< Array elements (\a Length long)
160 void *Data; //!< Data (can be anywhere)
161 void (*Destroy)(void *Data); //!< Called on GC
165 * \brief Object Instance
167 tSpiderObject *Object;
172 * \brief Object Definition
174 * Internal representation of an arbitary object.
176 struct sSpiderObjectDef
180 struct sSpiderObjectDef *Next; //!< Internal linked list
182 * \brief Object type name
184 const char * const Name;
186 * \brief Construct an instance of the object
187 * \param NArgs Number of arguments
188 * \param Args Argument array
189 * \return Pointer to an object instance (which must be fully valid)
190 * \retval NULL Invalid parameter (usually, actually just a NULL value)
191 * \retval ERRPTR Invalid parameter count
193 tSpiderObject *(*Constructor)(int NArgs, tSpiderValue **Args);
196 * \brief Clean up and destroy the object
197 * \param This Object instace
198 * \note The object pointer (\a This) should be invalidated and freed
201 void (*Destructor)(tSpiderObject *This);
203 tSpiderFunction *Methods; //!< Method Definitions (linked list)
205 int NAttributes; //!< Number of attributes
207 //! Attribute definitions
209 const char *Name; //!< Attribute Name
210 int bReadOnly; //!< Allow writes to the attribute?
215 * \brief Object Instance
219 tSpiderObjectDef *Type; //!< Object Type
220 int ReferenceCount; //!< Number of references
221 void *OpaqueData; //!< Pointer to the end of the \a Attributes array
222 tSpiderValue *Attributes[]; //!< Attribute Array
226 * \brief Represents a function avaliable to a script
228 struct sSpiderFunction
231 * \brief Next function in list
233 struct sSpiderFunction *Next;
236 * \brief Function name
240 * \brief Function handler
242 tSpiderValue *(*Handler)(tSpiderScript *Script, int nParams, tSpiderValue **Parameters);
245 * \brief What type is returned
250 * \brief Argument types
252 * Zero or -1 terminated array of \a eSpiderScript_DataTypes.
253 * If the final entry is zero, the function has a fixed number of
254 * parameters, if the final entry is -1, the function has a variable
255 * number of arguments.
257 int ArgTypes[]; // Zero (or -1) terminated array of parameter types
263 * \brief Parse a file into a script
264 * \param Variant Variant structure
265 * \param Filename File to parse
266 * \return Script suitable for execution
268 extern tSpiderScript *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filename);
270 * \brief Execute a function from a script
271 * \param Script Script to run
272 * \param Function Name of function to run ("" for the 'main')
273 * \return Return value
275 extern tSpiderValue *SpiderScript_ExecuteFunction(tSpiderScript *Script,
276 const char *Function, const char *DefaultNamespaces[],
277 int NArguments, tSpiderValue **Arguments,
281 * \brief Execute an object method
283 extern tSpiderValue *SpiderScript_ExecuteMethod(tSpiderScript *Script,
284 tSpiderObject *Object, const char *MethodName,
285 int NArguments, tSpiderValue **Arguments
288 * \brief Creates an object instance
290 extern tSpiderValue *SpiderScript_CreateObject(tSpiderScript *Script,
291 const char *ClassName, const char *DefaultNamespaces[],
292 int NArguments, tSpiderValue **Arguments
296 * \brief Convert a script to bytecode and save to a file
298 extern int SpiderScript_SaveBytecode(tSpiderScript *Script, const char *DestFile);
300 * \brief Save the AST of a script to a file
302 extern int SpiderScript_SaveAST(tSpiderScript *Script, const char *Filename);
305 * \brief Free a script
306 * \param Script Script structure to free
308 extern void SpiderScript_Free(tSpiderScript *Script);
310 extern tSpiderObject *SpiderScript_AllocateObject(tSpiderObjectDef *Class, int ExtraBytes);
313 * \name tSpiderValue Manipulation functions
316 extern void SpiderScript_DereferenceValue(tSpiderValue *Object);
317 extern void SpiderScript_ReferenceValue(tSpiderValue *Object);
318 extern tSpiderValue *SpiderScript_CreateInteger(uint64_t Value);
319 extern tSpiderValue *SpiderScript_CreateReal(double Value);
320 extern tSpiderValue *SpiderScript_CreateString(int Length, const char *Data);
321 extern tSpiderValue *SpiderScript_StringConcat(const tSpiderValue *Str1, const tSpiderValue *Str2);
322 extern tSpiderValue *SpiderScript_CastValueTo(int Type, tSpiderValue *Source);
323 extern int SpiderScript_IsValueTrue(tSpiderValue *Value);
324 extern void SpiderScript_FreeValue(tSpiderValue *Value);
325 extern char *SpiderScript_DumpValue(tSpiderValue *Value);
327 extern tSpiderValue *SpiderScript_DoOp(tSpiderValue *Left, enum eSpiderValueOps Op, int bCanCast, tSpiderValue *Right);