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
58 * \brief Integer datatype
63 SS_DATATYPE_REAL, //!< Real Number (double)
64 SS_DATATYPE_STRING, //!< String
69 * \brief Namespace definition
71 struct sSpiderNamespace
73 tSpiderNamespace *Next;
75 tSpiderNamespace *FirstChild;
77 tSpiderFunction *Functions;
79 tSpiderObjectDef *Classes;
81 int NConstants; //!< Number of constants
82 tSpiderValue *Constants; //!< Number of constants
88 * \brief Variant of SpiderScript
92 const char *Name; // Just for debug
94 int bDyamicTyped; //!< Use dynamic typing
95 int bImplicitCasts; //!< Allow implicit casts (casts to lefthand side)
97 tSpiderFunction *Functions; //!< Functions (Linked List)
99 int NConstants; //!< Number of constants
100 tSpiderValue *Constants; //!< Number of constants
102 tSpiderNamespace RootNamespace;
106 * \brief SpiderScript data object
110 enum eSpiderScript_DataTypes Type; //!< Variable type
111 int ReferenceCount; //!< Reference count
114 int64_t Integer; //!< Integer data
115 double Real; //!< Real Number data
120 int Length; //!< Length
121 char Data[]; //!< Actual string (\a Length bytes)
124 * \brief Variable data
127 int Length; //!< Length of the array
128 tSpiderValue *Items[]; //!< Array elements (\a Length long)
135 void *Data; //!< Data (can be anywhere)
136 void (*Destroy)(void *Data); //!< Called on GC
140 * \brief Object Instance
142 tSpiderObject *Object;
147 * \brief Object Definition
149 * Internal representation of an arbitary object.
151 struct sSpiderObjectDef
155 struct sSpiderObjectDef *Next; //!< Internal linked list
157 * \brief Object type name
159 const char * const Name;
161 * \brief Construct an instance of the object
162 * \param NArgs Number of arguments
163 * \param Args Argument array
164 * \return Pointer to an object instance (which must be fully valid)
165 * \retval NULL Invalid parameter (usually, actually just a NULL value)
166 * \retval ERRPTR Invalid parameter count
168 tSpiderObject *(*Constructor)(int NArgs, tSpiderValue **Args);
171 * \brief Clean up and destroy the object
172 * \param This Object instace
173 * \note The object pointer (\a This) should be invalidated and freed
176 void (*Destructor)(tSpiderObject *This);
178 tSpiderFunction *Methods; //!< Method Definitions (linked list)
180 int NAttributes; //!< Number of attributes
182 //! Attribute definitions
184 const char *Name; //!< Attribute Name
185 int bReadOnly; //!< Allow writes to the attribute?
190 * \brief Object Instance
194 tSpiderObjectDef *Type; //!< Object Type
195 int ReferenceCount; //!< Number of references
196 void *OpaqueData; //!< Pointer to the end of the \a Attributes array
197 tSpiderValue *Attributes[]; //!< Attribute Array
201 * \brief Represents a function avaliable to a script
203 struct sSpiderFunction
206 * \brief Next function in list
208 struct sSpiderFunction *Next;
211 * \brief Function name
215 * \brief Function handler
217 tSpiderValue *(*Handler)(tSpiderScript *Script, int nParams, tSpiderValue **Parameters);
219 * \brief Argument types
221 * Zero or -1 terminated array of \a eSpiderScript_DataTypes.
222 * If the final entry is zero, the function has a fixed number of
223 * parameters, if the final entry is -1, the function has a variable
224 * number of arguments.
226 int ArgTypes[]; // Zero (or -1) terminated array of parameter types
232 * \brief Parse a file into a script
233 * \param Variant Variant structure
234 * \param Filename File to parse
235 * \return Script suitable for execution
237 extern tSpiderScript *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filename);
239 * \brief Execute a function from a script
240 * \param Script Script to run
241 * \param Function Name of function to run ("" for the 'main')
242 * \return Return value
244 extern tSpiderValue *SpiderScript_ExecuteFunction(tSpiderScript *Script,
245 tSpiderNamespace *Namespace, const char *Function,
246 int NArguments, tSpiderValue **Arguments
250 * \brief Free a script
251 * \param Script Script structure to free
253 extern void SpiderScript_Free(tSpiderScript *Script);
255 extern tSpiderObject *SpiderScript_AllocateObject(tSpiderObjectDef *Class, int ExtraBytes);
258 * \name tSpiderValue Manipulation functions
261 extern tSpiderValue *SpiderScript_CreateInteger(uint64_t Value);
262 extern tSpiderValue *SpiderScript_CreateReal(double Value);
263 extern tSpiderValue *SpiderScript_CreateString(int Length, const char *Data);
264 extern tSpiderValue *SpiderScript_CastValueTo(int Type, tSpiderValue *Source);
265 extern int SpiderScript_IsValueTrue(tSpiderValue *Value);
266 extern void SpiderScript_FreeValue(tSpiderValue *Value);
267 extern char *SpiderScript_DumpValue(tSpiderValue *Value);