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);
205 * \brief Get/Set an attribute's value
207 tSpiderValue *(*GetSetAttribute)(tSpiderObject *This, int AttibuteID, tSpiderValue *NewValue);
210 * \brief Method Definitions (linked list)
212 tSpiderFunction *Methods;
215 * \brief Number of attributes
219 //! Attribute definitions
221 const char *Name; //!< Attribute Name
222 int Type; //!< Datatype
223 char bReadOnly; //!< Allow writes to the attribute?
224 char bMethod; //!< IO Goes via GetSetAttribute function
229 * \brief Object Instance
233 tSpiderObjectDef *Type; //!< Object Type
234 int ReferenceCount; //!< Number of references
235 void *OpaqueData; //!< Pointer to the end of the \a Attributes array
236 tSpiderValue *Attributes[]; //!< Attribute Array
240 * \brief Represents a function avaliable to a script
242 struct sSpiderFunction
245 * \brief Next function in list
247 struct sSpiderFunction *Next;
250 * \brief Function name
254 * \brief Function handler
256 tSpiderValue *(*Handler)(tSpiderScript *Script, int nParams, tSpiderValue **Parameters);
259 * \brief What type is returned
264 * \brief Argument types
266 * Zero or -1 terminated array of \a eSpiderScript_DataTypes.
267 * If the final entry is zero, the function has a fixed number of
268 * parameters, if the final entry is -1, the function has a variable
269 * number of arguments.
271 int ArgTypes[]; // Zero (or -1) terminated array of parameter types
277 * \brief Parse a file into a script
278 * \param Variant Variant structure
279 * \param Filename File to parse
280 * \return Script suitable for execution
282 extern tSpiderScript *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filename);
284 * \brief Execute a function from a script
285 * \param Script Script to run
286 * \param Function Name of function to run ("" for the 'main')
287 * \return Return value
289 extern tSpiderValue *SpiderScript_ExecuteFunction(tSpiderScript *Script,
290 const char *Function, const char *DefaultNamespaces[],
291 int NArguments, tSpiderValue **Arguments,
295 * \brief Execute an object method
297 extern tSpiderValue *SpiderScript_ExecuteMethod(tSpiderScript *Script,
298 tSpiderObject *Object, const char *MethodName,
299 int NArguments, tSpiderValue **Arguments
302 * \brief Creates an object instance
304 extern tSpiderValue *SpiderScript_CreateObject(tSpiderScript *Script,
305 const char *ClassName, const char *DefaultNamespaces[],
306 int NArguments, tSpiderValue **Arguments
310 * \brief Convert a script to bytecode and save to a file
312 extern int SpiderScript_SaveBytecode(tSpiderScript *Script, const char *DestFile);
314 * \brief Save the AST of a script to a file
316 extern int SpiderScript_SaveAST(tSpiderScript *Script, const char *Filename);
319 * \brief Free a script
320 * \param Script Script structure to free
322 extern void SpiderScript_Free(tSpiderScript *Script);
324 extern tSpiderObject *SpiderScript_AllocateObject(tSpiderObjectDef *Class, int ExtraBytes);
327 * \name tSpiderValue Manipulation functions
330 extern void SpiderScript_DereferenceValue(tSpiderValue *Object);
331 extern void SpiderScript_ReferenceValue(tSpiderValue *Object);
332 extern tSpiderValue *SpiderScript_CreateInteger(uint64_t Value);
333 extern tSpiderValue *SpiderScript_CreateReal(double Value);
334 extern tSpiderValue *SpiderScript_CreateString(int Length, const char *Data);
335 extern tSpiderValue *SpiderScript_CreateArray(int InnerType, int ItemCount);
336 extern tSpiderValue *SpiderScript_StringConcat(const tSpiderValue *Str1, const tSpiderValue *Str2);
337 extern tSpiderValue *SpiderScript_CastValueTo(int Type, tSpiderValue *Source);
338 extern int SpiderScript_IsValueTrue(tSpiderValue *Value);
339 extern void SpiderScript_FreeValue(tSpiderValue *Value);
340 extern char *SpiderScript_DumpValue(tSpiderValue *Value);
342 extern tSpiderValue *SpiderScript_DoOp(tSpiderValue *Left, enum eSpiderValueOps Op, int bCanCast, tSpiderValue *Right);