Just cleaning up some things that may be needed in the tree
[tpg/acess2.git] / Usermode / include / spiderscript.h
1 /*
2  * 
3  */
4 #ifndef _SPIDERSCRIPT_H_
5 #define _SPIDERSCRIPT_H_
6
7 #include <stdint.h>
8
9 #define ERRPTR  ((void*)((intptr_t)0-1))
10
11 /**
12  * \brief Opaque script handle
13  */
14 typedef struct sSpiderScript    tSpiderScript;
15
16 /**
17  * \brief Variant type
18  */
19 typedef struct sSpiderVariant   tSpiderVariant;
20 typedef struct sSpiderFunction  tSpiderFunction;
21 typedef struct sSpiderValue     tSpiderValue;
22 typedef struct sSpiderObjectDef tSpiderObjectDef;
23 typedef struct sSpiderObject    tSpiderObject;
24
25
26 /**
27  * \brief SpiderScript Variable Datatypes
28  */
29 enum eSpiderScript_DataTypes
30 {
31         SS_DATATYPE_UNDEF,      //!< Undefined
32         SS_DATATYPE_NULL,       //!< NULL (Probably will never be used)
33         SS_DATATYPE_DYNAMIC,    //!< Dynamically typed variable (will this be used?)
34         SS_DATATYPE_OBJECT,     //!< Opaque object reference
35         SS_DATATYPE_ARRAY,      //!< Array
36         SS_DATATYPE_INTEGER,    //!< Integer (64-bits)
37         SS_DATATYPE_REAL,       //!< Real Number (double)
38         SS_DATATYPE_STRING,     //!< String
39         NUM_SS_DATATYPES
40 };
41
42 /**
43  * \brief Variant of SpiderScript
44  */
45 struct sSpiderVariant
46 {
47         const char      *Name;  // Just for debug
48         
49          int    bDyamicTyped;
50         
51          int    NFunctions;     //!< Number of functions
52         tSpiderFunction *Functions;     //!< Functions
53         
54          int    NConstants;     //!< Number of constants
55         tSpiderValue    *Constants;     //!< Number of constants
56 };
57
58 /**
59  * \brief SpiderScript data object
60  */
61 struct sSpiderValue
62 {
63          int    Type;   //!< Variable type
64          int    ReferenceCount; //!< Reference count
65         
66         union {
67                 uint64_t        Integer;        //!< Integer data
68                 double  Real;   //!< Real Number data
69                 /**
70                  * \brief String data
71                  */
72                 struct {
73                          int    Length; //!< Length
74                         char    Data[]; //!< Actual string (\a Length bytes)
75                 }       String;
76                 /**
77                  * \brief Variable data
78                  */
79                 struct {
80                          int    Length; //!< Length of the array
81                         tSpiderValue    *Items[];       //!< Array elements (\a Length long)
82                 }       Array;
83                 
84                 tSpiderObject   *Object;
85         };
86 };
87
88 /**
89  * \brief Object Definition
90  * 
91  * Internal representation of an arbitary object.
92  */
93 struct sSpiderObjectDef
94 {
95         /**
96          * \brief Construct an instance of the object
97          * \param NArgs Number of arguments
98          * \param Args  Argument count
99          * \return Pointer to an object instance (which must be fully valid)
100          * \retval NULL Invalid parameter (usually, actually just a NULL value)
101          * \retval ERRPTR       Invalid parameter count
102          */
103         tSpiderObject   *(*Constructor)(int NArgs, tSpiderValue *Args);
104         
105         /**
106          * \brief Clean up and destroy the object
107          * \param This  Object instace
108          * \note The object pointer (\a This) should be invalidated and freed
109          *       by this function.
110          */
111         void    (*Destructor)(tSpiderObject *This);
112         
113          int    NAttributes;    //!< Number of attributes
114         
115         //! Attribute definitions
116         struct {
117                 const char      *Name;  //!< Attribute Name
118                  int    bReadOnly;      //!< Allow writes to the attribute?
119         }       *AttributeDefs;
120         
121         
122          int    NMethods;       //!< Number of methods
123         tSpiderFunction *Methods;       //!< Method Definitions
124 };
125
126 /**
127  * \brief Object Instance
128  */
129 struct sSpiderObject
130 {
131         tSpiderObjectDef        *Type;  //!< Object Type
132          int    NReferences;    //!< Number of references
133         void    *OpaqueData;    //!< Pointer to the end of the \a Attributes array
134         tSpiderValue    *Attributes[];  //!< Attribute Array
135 };
136
137 /**
138  * \brief Represents a function avaliable to a script
139  */
140 struct sSpiderFunction
141 {
142         /**
143          * \brief Function name
144          */
145         const char      *Name;
146         /**
147          * \brief Function handler
148          */
149         tSpiderValue    *(*Handler)(tSpiderScript *Script, int nParams, tSpiderValue **Parameters);
150         /**
151          * \brief Argument types
152          * 
153          * Zero or -1 terminated array of \a eSpiderScript_DataTypes.
154          * If the final entry is zero, the function has a fixed number of
155          * parameters, if the final entry is -1, the function has a variable
156          * number of arguments.
157          */
158          int    *ArgTypes;      // Zero (or -1) terminated array of parameter types
159 };
160
161
162 // === FUNCTIONS ===
163 /**
164  * \brief Parse a file into a script
165  * \param Variant       Variant structure
166  * \param Filename      File to parse
167  * \return Script suitable for execution
168  */
169 extern tSpiderScript    *SpiderScript_ParseFile(tSpiderVariant *Variant, const char *Filename);
170 /**
171  * \brief Execute a method from a script
172  * \param Script        Script to run
173  * \param Function      Name of function to run ("" for the 'main')
174  * \return Return value
175  */
176 extern tSpiderValue     *SpiderScript_ExecuteMethod(tSpiderScript *Script,
177         const char *Function,
178         int NArguments, tSpiderValue **Arguments
179         );
180
181 /**
182  * \brief Free a script
183  */
184 extern void     SpiderScript_Free(tSpiderScript *Script);
185
186 #endif

UCC git Repository :: git.ucc.asn.au