ntdll/tests: Add tests for opening files with a root directory handle.
[wine] / dlls / jscript / engine.h
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 typedef struct _source_elements_t source_elements_t;
20 typedef struct _function_expression_t function_expression_t;
21
22 typedef struct _function_declaration_t {
23     function_expression_t *expr;
24
25     struct _function_declaration_t *next;
26 } function_declaration_t;
27
28 typedef struct _var_list_t {
29     const WCHAR *identifier;
30
31     struct _var_list_t *next;
32 } var_list_t;
33
34 typedef struct _func_stack {
35     function_declaration_t *func_head;
36     function_declaration_t *func_tail;
37     var_list_t *var_head;
38     var_list_t *var_tail;
39
40     struct _func_stack *next;
41 } func_stack_t;
42
43 typedef struct _parser_ctx_t {
44     LONG ref;
45
46     WCHAR *begin;
47     const WCHAR *end;
48     const WCHAR *ptr;
49
50     script_ctx_t *script;
51     source_elements_t *source;
52     BOOL nl;
53     BOOL is_html;
54     BOOL lexer_error;
55     HRESULT hres;
56
57     jsheap_t heap;
58
59     func_stack_t *func_stack;
60
61     struct _parser_ctx_t *next;
62 } parser_ctx_t;
63
64 HRESULT script_parse(script_ctx_t*,const WCHAR*,const WCHAR*,parser_ctx_t**);
65 void parser_release(parser_ctx_t*);
66
67 int parser_lex(void*,parser_ctx_t*);
68
69 static inline void parser_addref(parser_ctx_t *ctx)
70 {
71     ctx->ref++;
72 }
73
74 static inline void *parser_alloc(parser_ctx_t *ctx, DWORD size)
75 {
76     return jsheap_alloc(&ctx->heap, size);
77 }
78
79 static inline void *parser_alloc_tmp(parser_ctx_t *ctx, DWORD size)
80 {
81     return jsheap_alloc(&ctx->script->tmp_heap, size);
82 }
83
84 typedef struct _scope_chain_t {
85     LONG ref;
86     DispatchEx *obj;
87     struct _scope_chain_t *next;
88 } scope_chain_t;
89
90 HRESULT scope_push(scope_chain_t*,DispatchEx*,scope_chain_t**);
91 void scope_release(scope_chain_t*);
92
93 static inline void scope_addref(scope_chain_t *scope)
94 {
95     scope->ref++;
96 }
97
98 struct _exec_ctx_t {
99     LONG ref;
100
101     parser_ctx_t *parser;
102     scope_chain_t *scope_chain;
103     DispatchEx *var_disp;
104     IDispatch *this_obj;
105 };
106
107 static inline void exec_addref(exec_ctx_t *ctx)
108 {
109     ctx->ref++;
110 }
111
112 typedef enum {
113     EXECT_PROGRAM,
114     EXECT_FUNCTION,
115     EXECT_EVAL
116 } exec_type_t;
117
118 void exec_release(exec_ctx_t*);
119 HRESULT create_exec_ctx(script_ctx_t*,IDispatch*,DispatchEx*,scope_chain_t*,exec_ctx_t**);
120 HRESULT exec_source(exec_ctx_t*,parser_ctx_t*,source_elements_t*,exec_type_t,jsexcept_t*,VARIANT*);
121
122 typedef struct _statement_t statement_t;
123 typedef struct _expression_t expression_t;
124 typedef struct _parameter_t parameter_t;
125
126 HRESULT create_source_function(parser_ctx_t*,parameter_t*,source_elements_t*,scope_chain_t*,
127         const WCHAR*,DWORD,DispatchEx**);
128
129 typedef enum {
130     LT_INT,
131     LT_DOUBLE,
132     LT_STRING,
133     LT_BOOL,
134     LT_UNDEFINED,
135     LT_NULL,
136     LT_REGEXP
137 }literal_type_t;
138
139 typedef struct {
140     literal_type_t type;
141     union {
142         LONG lval;
143         double dval;
144         const WCHAR *wstr;
145         VARIANT_BOOL bval;
146         IDispatch *disp;
147         struct {
148             const WCHAR *str;
149             DWORD str_len;
150             DWORD flags;
151         } regexp;
152     } u;
153 } literal_t;
154
155 literal_t *parse_regexp(parser_ctx_t*);
156
157 typedef struct _variable_declaration_t {
158     const WCHAR *identifier;
159     expression_t *expr;
160
161     struct _variable_declaration_t *next;
162 } variable_declaration_t;
163
164 typedef struct {
165     enum{
166         RT_NORMAL,
167         RT_RETURN,
168         RT_BREAK,
169         RT_CONTINUE
170     } type;
171     jsexcept_t ei;
172 } return_type_t;
173
174 typedef HRESULT (*statement_eval_t)(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
175
176 struct _statement_t {
177     statement_eval_t eval;
178     statement_t *next;
179 };
180
181 typedef struct {
182     statement_t stat;
183     statement_t *stat_list;
184 } block_statement_t;
185
186 typedef struct {
187     statement_t stat;
188     variable_declaration_t *variable_list;
189 } var_statement_t;
190
191 typedef struct {
192     statement_t stat;
193     expression_t *expr;
194 } expression_statement_t;
195
196 typedef struct {
197     statement_t stat;
198     expression_t *expr;
199     statement_t *if_stat;
200     statement_t *else_stat;
201 } if_statement_t;
202
203 typedef struct {
204     statement_t stat;
205     BOOL do_while;
206     expression_t *expr;
207     statement_t *statement;
208 } while_statement_t;
209
210 typedef struct {
211     statement_t stat;
212     variable_declaration_t *variable_list;
213     expression_t *begin_expr;
214     expression_t *expr;
215     expression_t *end_expr;
216     statement_t *statement;
217 } for_statement_t;
218
219 typedef struct {
220     statement_t stat;
221     variable_declaration_t *variable;
222     expression_t *expr;
223     expression_t *in_expr;
224     statement_t *statement;
225 } forin_statement_t;
226
227 typedef struct {
228     statement_t stat;
229     const WCHAR *identifier;
230 } branch_statement_t;
231
232 typedef struct {
233     statement_t stat;
234     expression_t *expr;
235     statement_t *statement;
236 } with_statement_t;
237
238 typedef struct {
239     statement_t stat;
240     const WCHAR *identifier;
241     statement_t *statement;
242 } labelled_statement_t;
243
244 typedef struct _case_clausule_t {
245     expression_t *expr;
246     statement_t *stat;
247
248     struct _case_clausule_t *next;
249 } case_clausule_t;
250
251 typedef struct {
252     statement_t stat;
253     expression_t *expr;
254     case_clausule_t *case_list;
255 } switch_statement_t;
256
257 typedef struct {
258     const WCHAR *identifier;
259     statement_t *statement;
260 } catch_block_t;
261
262 typedef struct {
263     statement_t stat;
264     statement_t *try_statement;
265     catch_block_t *catch_block;
266     statement_t *finally_statement;
267 } try_statement_t;
268
269 HRESULT block_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
270 HRESULT var_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
271 HRESULT empty_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
272 HRESULT expression_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
273 HRESULT if_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
274 HRESULT while_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
275 HRESULT for_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
276 HRESULT forin_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
277 HRESULT continue_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
278 HRESULT break_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
279 HRESULT return_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
280 HRESULT with_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
281 HRESULT labelled_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
282 HRESULT switch_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
283 HRESULT throw_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
284 HRESULT try_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
285
286 typedef struct {
287     enum {
288         EXPRVAL_VARIANT,
289         EXPRVAL_IDREF,
290         EXPRVAL_NAMEREF,
291         EXPRVAL_INVALID
292     } type;
293     union {
294         VARIANT var;
295         struct {
296             IDispatch *disp;
297             DISPID id;
298         } idref;
299         struct {
300             IDispatch *disp;
301             BSTR name;
302         } nameref;
303         BSTR identifier;
304     } u;
305 } exprval_t;
306
307 typedef HRESULT (*expression_eval_t)(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
308
309 struct _expression_t {
310     expression_eval_t eval;
311 };
312
313 struct _parameter_t {
314     const WCHAR *identifier;
315
316     struct _parameter_t *next;
317 };
318
319 struct _source_elements_t {
320     statement_t *statement;
321     statement_t *statement_tail;
322     function_declaration_t *functions;
323     var_list_t *variables;
324 };
325
326 struct _function_expression_t {
327     expression_t expr;
328     const WCHAR *identifier;
329     parameter_t *parameter_list;
330     source_elements_t *source_elements;
331     const WCHAR *src_str;
332     DWORD src_len;
333 };
334
335 typedef struct {
336     expression_t expr;
337     expression_t *expression1;
338     expression_t *expression2;
339 } binary_expression_t;
340
341 typedef struct {
342     expression_t expr;
343     expression_t *expression;
344 } unary_expression_t;
345
346 typedef struct {
347     expression_t expr;
348     expression_t *expression;
349     expression_t *true_expression;
350     expression_t *false_expression;
351 } conditional_expression_t;
352
353 typedef struct {
354     expression_t expr;
355     expression_t *member_expr;
356     expression_t *expression;
357 } array_expression_t;
358
359 typedef struct {
360     expression_t expr;
361     expression_t *expression;
362     const WCHAR *identifier;
363 } member_expression_t;
364
365 typedef struct _argument_t {
366     expression_t *expr;
367
368     struct _argument_t *next;
369 } argument_t;
370
371 typedef struct {
372     expression_t expr;
373     expression_t *expression;
374     argument_t *argument_list;
375 } call_expression_t;
376
377 typedef struct {
378     expression_t expr;
379     const WCHAR *identifier;
380 } identifier_expression_t;
381
382 typedef struct {
383     expression_t expr;
384     literal_t *literal;
385 } literal_expression_t;
386
387 typedef struct _array_element_t {
388     int elision;
389     expression_t *expr;
390
391     struct _array_element_t *next;
392 } array_element_t;
393
394 typedef struct {
395     expression_t expr;
396     array_element_t *element_list;
397     int length;
398 } array_literal_expression_t;
399
400 typedef struct _prop_val_t {
401     literal_t *name;
402     expression_t *value;
403
404     struct _prop_val_t *next;
405 } prop_val_t;
406
407 typedef struct {
408     expression_t expr;
409     prop_val_t *property_list;
410 } property_value_expression_t;
411
412 typedef enum {
413      EXPR_COMMA,
414      EXPR_OR,
415      EXPR_AND,
416      EXPR_BOR,
417      EXPR_BXOR,
418      EXPR_BAND,
419      EXPR_INSTANCEOF,
420      EXPR_IN,
421      EXPR_ADD,
422      EXPR_SUB,
423      EXPR_MUL,
424      EXPR_DIV,
425      EXPR_MOD,
426      EXPR_DELETE,
427      EXPR_VOID,
428      EXPR_TYPEOF,
429      EXPR_MINUS,
430      EXPR_PLUS,
431      EXPR_POSTINC,
432      EXPR_POSTDEC,
433      EXPR_PREINC,
434      EXPR_PREDEC,
435      EXPR_EQ,
436      EXPR_EQEQ,
437      EXPR_NOTEQ,
438      EXPR_NOTEQEQ,
439      EXPR_LESS,
440      EXPR_LESSEQ,
441      EXPR_GREATER,
442      EXPR_GREATEREQ,
443      EXPR_BITNEG,
444      EXPR_LOGNEG,
445      EXPR_LSHIFT,
446      EXPR_RSHIFT,
447      EXPR_RRSHIFT,
448      EXPR_ASSIGN,
449      EXPR_ASSIGNLSHIFT,
450      EXPR_ASSIGNRSHIFT,
451      EXPR_ASSIGNRRSHIFT,
452      EXPR_ASSIGNADD,
453      EXPR_ASSIGNSUB,
454      EXPR_ASSIGNMUL,
455      EXPR_ASSIGNDIV,
456      EXPR_ASSIGNMOD,
457      EXPR_ASSIGNAND,
458      EXPR_ASSIGNOR,
459      EXPR_ASSIGNXOR
460 } expression_type_t;
461
462 HRESULT function_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
463 HRESULT conditional_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
464 HRESULT array_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
465 HRESULT member_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
466 HRESULT new_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
467 HRESULT call_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
468 HRESULT this_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
469 HRESULT identifier_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
470 HRESULT literal_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
471 HRESULT array_literal_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
472 HRESULT property_value_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
473
474 HRESULT comma_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
475 HRESULT logical_or_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
476 HRESULT logical_and_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
477 HRESULT binary_or_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
478 HRESULT binary_xor_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
479 HRESULT binary_and_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
480 HRESULT instanceof_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
481 HRESULT in_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
482 HRESULT add_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
483 HRESULT sub_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
484 HRESULT mul_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
485 HRESULT div_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
486 HRESULT mod_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
487 HRESULT delete_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
488 HRESULT void_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
489 HRESULT typeof_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
490 HRESULT minus_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
491 HRESULT plus_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
492 HRESULT post_increment_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
493 HRESULT post_decrement_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
494 HRESULT pre_increment_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
495 HRESULT pre_decrement_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
496 HRESULT equal_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
497 HRESULT equal2_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
498 HRESULT not_equal_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
499 HRESULT not_equal2_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
500 HRESULT less_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
501 HRESULT lesseq_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
502 HRESULT greater_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
503 HRESULT greatereq_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
504 HRESULT binary_negation_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
505 HRESULT logical_negation_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
506 HRESULT left_shift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
507 HRESULT right_shift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
508 HRESULT right2_shift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
509 HRESULT assign_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
510 HRESULT assign_lshift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
511 HRESULT assign_rshift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
512 HRESULT assign_rrshift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
513 HRESULT assign_add_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
514 HRESULT assign_sub_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
515 HRESULT assign_mul_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
516 HRESULT assign_div_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
517 HRESULT assign_mod_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
518 HRESULT assign_and_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
519 HRESULT assign_or_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
520 HRESULT assign_xor_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);