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 sSpiderFunction tSpiderFunction;
18 typedef struct sSpiderValue tSpiderValue;
19 typedef struct sSpiderObjectDef tSpiderObjectDef;
20 typedef struct sSpiderObject tSpiderObject;
24 * \brief SpiderScript Variable Datatypes
25 * \todo Expand the descriptions
27 enum eSpiderScript_DataTypes
30 * \brief Undefined data
31 * \note Default type of an undefined dynamic variable
35 * \brief Dynamically typed variable
36 * \note Used to dentote a non-fixed type for function parameters
40 * \brief Opaque Data Pointer
42 * Opaque data types are used for resource handles or for system buffers.
46 * \brief Object reference
48 * A reference to a SpiderScript class instance. Can be accessed
49 * using the -> operator.
53 * \brief Array data type
57 * \brief Integer datatype
62 SS_DATATYPE_REAL, //!< Real Number (double)
63 SS_DATATYPE_STRING, //!< String
68 * \brief Variant of SpiderScript
72 const char *Name; // Just for debug
74 int bDyamicTyped; //!< Use static typing
76 tSpiderFunction *Functions; //!< Functions (Linked List)
78 int NConstants; //!< Number of constants
79 tSpiderValue *Constants; //!< Number of constants
83 * \brief SpiderScript data object
87 enum eSpiderScript_DataTypes Type; //!< Variable type
88 int ReferenceCount; //!< Reference count
91 uint64_t Integer; //!< Integer data
92 double Real; //!< Real Number data
97 int Length; //!< Length
98 char Data[]; //!< Actual string (\a Length bytes)
101 * \brief Variable data
104 int Length; //!< Length of the array
105 tSpiderValue *Items[]; //!< Array elements (\a Length long)
112 void *Data; //!< Data (can be anywhere)
113 void (*Destroy)(void *Data); //!< Called on GC
117 * \brief Object Instance
119 tSpiderObject *Object;
124 * \brief Object Definition
126 * Internal representation of an arbitary object.
128 struct sSpiderObjectDef
132 struct sSpiderObjectDef *Next; //!< Internal linked list
134 * \brief Object type name
136 const char* const Name;
138 * \brief Construct an instance of the object
139 * \param NArgs Number of arguments
140 * \param Args Argument count
141 * \return Pointer to an object instance (which must be fully valid)
142 * \retval NULL Invalid parameter (usually, actually just a NULL value)
143 * \retval ERRPTR Invalid parameter count
145 tSpiderObject *(*Constructor)(int NArgs, tSpiderValue *Args);
148 * \brief Clean up and destroy the object
149 * \param This Object instace
150 * \note The object pointer (\a This) should be invalidated and freed
153 void (*Destructor)(tSpiderObject *This);
155 int NAttributes; //!< Number of attributes
157 //! Attribute definitions
159 const char *Name; //!< Attribute Name
160 int bReadOnly; //!< Allow writes to the attribute?
164 int NMethods; //!< Number of methods
165 tSpiderFunction *Methods; //!< Method Definitions
169 * \brief Object Instance
173 tSpiderObjectDef *Type; //!< Object Type
174 int NReferences; //!< Number of references
175 void *OpaqueData; //!< Pointer to the end of the \a Attributes array
176 tSpiderValue *Attributes[]; //!< Attribute Array
180 * \brief Represents a function avaliable to a script
182 struct sSpiderFunction
185 * \brief Next function in list
187 struct sSpiderFunction *Next;
190 * \brief Function name
194 * \brief Function handler
196 tSpiderValue *(*Handler)(tSpiderScript *Script, int nParams, tSpiderValue **Parameters);
198 * \brief Argument types
200 * Zero or -1 terminated array of \a eSpiderScript_DataTypes.
201 * If the final entry is zero, the function has a fixed number of
202 * parameters, if the final entry is -1, the function has a variable
203 * number of arguments.
205 int ArgTypes[]; // Zero (or -1) terminated array of parameter types
211 * \brief Parse a file into a script
212 * \param Variant Variant structure
213 * \param Filename File to parse
214 * \return Script suitable for execution
216 extern tSpiderScript *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filename);
218 * \brief Execute a method from a script
219 * \param Script Script to run
220 * \param Function Name of function to run ("" for the 'main')
221 * \return Return value
223 extern tSpiderValue *SpiderScript_ExecuteMethod(tSpiderScript *Script,
224 const char *Function,
225 int NArguments, tSpiderValue **Arguments
229 * \brief Free a script
230 * \param Script Script structure to free
232 extern void SpiderScript_Free(tSpiderScript *Script);
235 * \name tSpiderValue Manipulation functions
238 extern tSpiderValue *SpiderScript_CreateInteger(uint64_t Value);
239 extern tSpiderValue *SpiderScript_CreateReal(double Value);
240 extern tSpiderValue *SpiderScript_CreateString(int Length, const char *Data);
241 extern tSpiderValue *SpiderScript_CastValueTo(int Type, tSpiderValue *Source);
242 extern int SpiderScript_IsValueTrue(tSpiderValue *Value);
243 extern char *SpiderScript_DumpValue(tSpiderValue *Value);