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

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