r8399: move the ejs and esp code closer to the directory layout used by the
[jra/samba/.git] / source4 / lib / appweb / ejs / ejsInternal.h
1 /*
2  *      @file   ejsInternal.h
3  *      @brief  Private header for Embedded Javascript (ECMAScript)
4  *      @overview This Embedded Javascript header defines the private Embedded 
5  *              Javascript internal structures.
6  */
7 /********************************* Copyright **********************************/
8 /*
9  *      @copy   default.g
10  *      
11  *      Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved.
12  *      Portions Copyright (c) GoAhead Software, 1995-2000. All Rights Reserved.
13  *      
14  *      This software is distributed under commercial and open source licenses.
15  *      You may use the GPL open source license described below or you may acquire 
16  *      a commercial license from Mbedthis Software. You agree to be fully bound 
17  *      by the terms of either license. Consult the LICENSE.TXT distributed with 
18  *      this software for full details.
19  *      
20  *      This software is open source; you can redistribute it and/or modify it 
21  *      under the terms of the GNU General Public License as published by the 
22  *      Free Software Foundation; either version 2 of the License, or (at your 
23  *      option) any later version. See the GNU General Public License for more 
24  *      details at: http://www.mbedthis.com/downloads/gplLicense.html
25  *      
26  *      This program is distributed WITHOUT ANY WARRANTY; without even the 
27  *      implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
28  *      
29  *      This GPL license does NOT permit incorporating this software into 
30  *      proprietary programs. If you are unable to comply with the GPL, you must
31  *      acquire a commercial license to use this software. Commercial licenses 
32  *      for this software and support services are available from Mbedthis 
33  *      Software at http://www.mbedthis.com 
34  *      
35  *      @end
36  */
37 /********************************* Includes ***********************************/
38
39 #ifndef _h_EJS_INTERNAL
40 #define _h_EJS_INTERNAL 1
41
42 #include                "ejs.h"
43
44 /********************************** Defines ***********************************/
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50 /*
51  *      Constants
52  */
53
54 #if BLD_FEATURE_SQUEEZE
55         #define EJS_PARSE_INCR                  256             /* Growth factor */
56         #define EJS_MAX_RECURSE                 25              /* Sanity for maximum recursion */
57         #define EJS_MAX_ID                              128             /* Maximum ID length */
58         #define EJS_OBJ_HASH_SIZE               13              /* Object hash table size */
59         #define EJS_SMALL_OBJ_HASH_SIZE 11              /* Small object hash size */
60         #define EJS_LIST_INCR                   8               /* Growth increment for lists */
61 #else
62         #define EJS_PARSE_INCR                  1024    /* Growth factor */
63         #define EJS_MAX_RECURSE                 100             /* Sanity for maximum recursion */
64         #define EJS_MAX_ID                              256             /* Maximum ID length */
65         #define EJS_OBJ_HASH_SIZE               29              /* Object hash table size */
66         #define EJS_SMALL_OBJ_HASH_SIZE 11              /* Small object hash size */
67         #define EJS_LIST_INCR                   16              /* Growth increment for lists */
68 #endif
69 #define EJS_TOKEN_STACK                         4               /* Put back token stack */
70
71 /*
72  *      Lexical analyser tokens
73  */
74 #define EJS_TOK_ERR                                     -1              /* Any error */
75 #define EJS_TOK_LPAREN                          1               /* ( */
76 #define EJS_TOK_RPAREN                          2               /* ) */
77 #define EJS_TOK_IF                                      3               /* if */
78 #define EJS_TOK_ELSE                            4               /* else */
79 #define EJS_TOK_LBRACE                          5               /* { */
80 #define EJS_TOK_RBRACE                          6               /* } */
81 #define EJS_TOK_LOGICAL                         7               /* ||, &&, ! */
82 #define EJS_TOK_EXPR                            8               /* +, -, /, % */
83 #define EJS_TOK_SEMI                            9               /* ; */
84 #define EJS_TOK_LITERAL                         10              /* literal string */
85 #define EJS_TOK_FUNCTION_NAME           11              /* functionName */
86 #define EJS_TOK_NEWLINE                         12              /* newline white space */
87 #define EJS_TOK_ID                                      13              /* Identifier */
88 #define EJS_TOK_EOF                                     14              /* End of script */
89 #define EJS_TOK_COMMA                           15              /* Comma */
90 #define EJS_TOK_VAR                                     16              /* var */
91 #define EJS_TOK_ASSIGNMENT                      17              /* = */
92 #define EJS_TOK_FOR                                     18              /* for */
93 #define EJS_TOK_INC_DEC                         19              /* ++, -- */
94 #define EJS_TOK_RETURN                          20              /* return */
95 #define EJS_TOK_PERIOD                          21              /* . */
96 #define EJS_TOK_LBRACKET                        22              /* [ */
97 #define EJS_TOK_RBRACKET                        23              /* ] */
98 #define EJS_TOK_NEW                                     24              /* new */
99 #define EJS_TOK_DELETE                          25              /* delete */
100 #define EJS_TOK_IN                                      26              /* in */
101 #define EJS_TOK_FUNCTION                        27              /* function */
102 #define EJS_TOK_NUMBER                          28              /* Number */
103
104 /*
105  *      Expression operators
106  */
107 #define EJS_EXPR_LESS                           1               /* < */
108 #define EJS_EXPR_LESSEQ                         2               /* <= */
109 #define EJS_EXPR_GREATER                        3               /* > */
110 #define EJS_EXPR_GREATEREQ                      4               /* >= */
111 #define EJS_EXPR_EQ                                     5               /* == */
112 #define EJS_EXPR_NOTEQ                          6               /* != */
113 #define EJS_EXPR_PLUS                           7               /* + */
114 #define EJS_EXPR_MINUS                          8               /* - */
115 #define EJS_EXPR_DIV                            9               /* / */
116 #define EJS_EXPR_MOD                            10              /* % */
117 #define EJS_EXPR_LSHIFT                         11              /* << */
118 #define EJS_EXPR_RSHIFT                         12              /* >> */
119 #define EJS_EXPR_MUL                            13              /* * */
120 #define EJS_EXPR_ASSIGNMENT                     14              /* = */
121 #define EJS_EXPR_INC                            15              /* ++ */
122 #define EJS_EXPR_DEC                            16              /* -- */
123 #define EJS_EXPR_BOOL_COMP                      17              /* ! */
124
125 /*
126  *      Conditional operators
127  */
128 #define EJS_COND_AND                            1               /* && */
129 #define EJS_COND_OR                                     2               /* || */
130 #define EJS_COND_NOT                            3               /* ! */
131
132 /*
133  *      States
134  */
135 #define EJS_STATE_ERR                           -1              /* Error state */
136 #define EJS_STATE_EOF                           1               /* End of file */
137 #define EJS_STATE_COND                          2               /* Parsing a "(conditional)" stmt */
138 #define EJS_STATE_COND_DONE                     3
139 #define EJS_STATE_RELEXP                        4               /* Parsing a relational expr */
140 #define EJS_STATE_RELEXP_DONE           5
141 #define EJS_STATE_EXPR                          6               /* Parsing an expression */
142 #define EJS_STATE_EXPR_DONE                     7
143 #define EJS_STATE_STMT                          8               /* Parsing General statement */
144 #define EJS_STATE_STMT_DONE                     9
145 #define EJS_STATE_STMT_BLOCK_DONE       10              /* End of block "}" */
146 #define EJS_STATE_ARG_LIST                      11              /* Function arg list */
147 #define EJS_STATE_ARG_LIST_DONE         12
148 #define EJS_STATE_DEC_LIST                      16              /* Declaration list */
149 #define EJS_STATE_DEC_LIST_DONE         17
150 #define EJS_STATE_DEC                           18              /* Declaration statement */
151 #define EJS_STATE_DEC_DONE                      19
152 #define EJS_STATE_RET                           20              /* Return statement */
153
154 #define EJS_STATE_BEGIN                         EJS_STATE_STMT
155
156 /*
157  *      General parsing flags.
158  */
159 #define EJS_FLAGS_EXE                           0x1             /* Execute statements */
160 #define EJS_FLAGS_LOCAL                         0x2             /* Get local vars only */
161 #define EJS_FLAGS_GLOBAL                        0x4             /* Get global vars only */
162 #define EJS_FLAGS_CREATE                        0x8             /* Create var */
163 #define EJS_FLAGS_ASSIGNMENT            0x10    /* In assignment stmt */
164 #define EJS_FLAGS_DELETE                        0x20    /* Deleting a variable */
165 #define EJS_FLAGS_FOREACH                       0x40    /* In foreach */
166 #define EJS_FLAGS_NEW                           0x80    /* In a new stmt() */
167 #define EJS_FLAGS_EXIT                          0x100   /* Must exit */
168
169 /*
170  *      Putback token 
171  */
172
173 typedef struct EjsToken {
174         char            *token;                                         /* Token string */
175         int                     id;                                                     /* Token ID */
176 } EjsToken;
177
178 /*
179  *      EJ evaluation block structure
180  */
181 typedef struct ejEval {
182         EjsToken        putBack[EJS_TOKEN_STACK];       /* Put back token stack */
183         int                     putBackIndex;                           /* Top of stack index */
184         MprStr          line;                                           /* Current line */
185         int                     lineLength;                                     /* Current line length */
186         int                     lineNumber;                                     /* Parse line number */
187         int                     lineColumn;                                     /* Column in line */
188         MprStr          script;                                         /* Input script for parsing */
189         char            *scriptServp;                           /* Next token in the script */
190         int                     scriptSize;                                     /* Length of script */
191         MprStr          tokbuf;                                         /* Current token */
192         char            *tokEndp;                                       /* Pointer past end of token */
193         char            *tokServp;                                      /* Pointer to next token char */
194         int                     tokSize;                                        /* Size of token buffer */
195 } EjsInput;
196
197 /*
198  *      Function call structure
199  */
200 typedef struct {
201         MprArray        *args;                                          /* Args for function */
202         MprVar          *fn;                                            /* Function definition */
203         char            *procName;                                      /* Function name */
204 } EjsProc;
205
206 /*
207  *      Per EJS structure
208  */
209 typedef struct ej {
210         EjsHandle       altHandle;                                      /* alternate callback handle */
211         MprVar          *currentObj;                            /* Ptr to current object */
212         MprVar          *currentProperty;                       /* Ptr to current property */
213         EjsId           eid;                                            /* Halloc handle */
214         char            *error;                                         /* Error message */
215         int                     exitStatus;                                     /* Status to exit() */
216         int                     flags;                                          /* Flags */
217         MprArray        *frames;                                        /* List of variable frames */
218         MprVar          *global;                                        /* Global object */
219         EjsInput        *input;                                         /* Input evaluation block */
220         MprVar          *local;                                         /* Local object */
221         EjsHandle       primaryHandle;                          /* primary callback handle */
222         EjsProc         *proc;                                          /* Current function */
223         MprVar          result;                                         /* Variable result */
224         void            *thisPtr;                                       /* C++ ptr for functions */
225         int                     tid;                                            /* Current token id */
226         char            *token;                                         /* Pointer to token string */
227         MprVar          tokenNumber;                            /* Parsed number */
228 } Ejs;
229
230 typedef int             EjsBlock;                                       /* Scope block id */
231
232 /*
233  *      Function callback when using Alternate handles.
234  */
235 typedef int (*EjsAltStringCFunction)(EjsHandle userHandle, EjsHandle altHandle,
236                 int argc, char **argv);
237 typedef int (*EjsAltCFunction)(EjsHandle userHandle, EjsHandle altHandle,
238                 int argc, MprVar **argv);
239
240 /******************************** Prototypes **********************************/
241 /*
242  *      Ejs Lex
243  */
244 extern int              ejsLexOpenScript(Ejs* ep, char *script);
245 extern void     ejsLexCloseScript(Ejs* ep);
246 extern int              ejsInitInputState(EjsInput *ip);
247 extern void     ejsLexSaveInputState(Ejs* ep, EjsInput* state);
248 extern void     ejsLexFreeInputState(Ejs* ep, EjsInput* state);
249 extern void     ejsLexRestoreInputState(Ejs* ep, EjsInput* state);
250 extern int              ejsLexGetToken(Ejs* ep, int state);
251 extern void             ejsLexPutbackToken(Ejs* ep, int tid, char *string);
252
253 /*
254  *      Parsing
255  */
256 extern MprVar   *ejsFindObj(Ejs *ep, int state, const char *property, 
257                                         int flags);
258 extern MprVar   *ejsFindProperty(Ejs *ep, int state, MprVar *obj,
259                                         char *property, int flags);
260 extern int              ejsGetVarCore(Ejs *ep, const char *var, MprVar **obj, 
261                                         MprVar **varValue, int flags);
262 extern int              ejsParse(Ejs *ep, int state, int flags);
263 extern Ejs              *ejsPtr(EjsId eid);
264 extern void     ejsSetExitStatus(int eid, int status);
265 extern void     ejsSetFlags(int orFlags, int andFlags);
266
267 /*
268  *      Create variable scope blocks
269  */
270 extern EjsBlock ejsOpenBlock(EjsId eid);
271 extern int              ejsCloseBlock(EjsId eid, EjsBlock vid);
272 extern int              ejsEvalBlock(EjsId eid, char *script, MprVar *v, char **err);
273 extern int              ejsDefineStandardProperties(MprVar *objVar);
274
275 /*
276  *      Error handling
277  */
278 extern void             ejsError(Ejs *ep, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
279
280 #ifdef __cplusplus
281 }
282 #endif
283 #endif /* _h_EJS_INTERNAL */
284
285 /*
286  * Local variables:
287  * tab-width: 4
288  * c-basic-offset: 4
289  * End:
290  * vim:tw=78
291  * vim600: sw=4 ts=4 fdm=marker
292  * vim<600: sw=4 ts=4
293  */