winmm: Fix incorrect memory useage (Valgrind w/ MIDI tests).
[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_NULL,
135     LT_REGEXP
136 }literal_type_t;
137
138 typedef struct {
139     literal_type_t type;
140     union {
141         LONG lval;
142         double dval;
143         const WCHAR *wstr;
144         VARIANT_BOOL bval;
145         IDispatch *disp;
146         struct {
147             const WCHAR *str;
148             DWORD str_len;
149             DWORD flags;
150         } regexp;
151     } u;
152 } literal_t;
153
154 literal_t *parse_regexp(parser_ctx_t*);
155
156 typedef struct _variable_declaration_t {
157     const WCHAR *identifier;
158     expression_t *expr;
159
160     struct _variable_declaration_t *next;
161 } variable_declaration_t;
162
163 typedef struct {
164     enum{
165         RT_NORMAL,
166         RT_RETURN,
167         RT_BREAK,
168         RT_CONTINUE
169     } type;
170     jsexcept_t ei;
171 } return_type_t;
172
173 typedef HRESULT (*statement_eval_t)(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
174
175 struct _statement_t {
176     statement_eval_t eval;
177     statement_t *next;
178 };
179
180 typedef struct {
181     statement_t stat;
182     statement_t *stat_list;
183 } block_statement_t;
184
185 typedef struct {
186     statement_t stat;
187     variable_declaration_t *variable_list;
188 } var_statement_t;
189
190 typedef struct {
191     statement_t stat;
192     expression_t *expr;
193 } expression_statement_t;
194
195 typedef struct {
196     statement_t stat;
197     expression_t *expr;
198     statement_t *if_stat;
199     statement_t *else_stat;
200 } if_statement_t;
201
202 typedef struct {
203     statement_t stat;
204     BOOL do_while;
205     expression_t *expr;
206     statement_t *statement;
207 } while_statement_t;
208
209 typedef struct {
210     statement_t stat;
211     variable_declaration_t *variable_list;
212     expression_t *begin_expr;
213     expression_t *expr;
214     expression_t *end_expr;
215     statement_t *statement;
216 } for_statement_t;
217
218 typedef struct {
219     statement_t stat;
220     variable_declaration_t *variable;
221     expression_t *expr;
222     expression_t *in_expr;
223     statement_t *statement;
224 } forin_statement_t;
225
226 typedef struct {
227     statement_t stat;
228     const WCHAR *identifier;
229 } branch_statement_t;
230
231 typedef struct {
232     statement_t stat;
233     expression_t *expr;
234     statement_t *statement;
235 } with_statement_t;
236
237 typedef struct {
238     statement_t stat;
239     const WCHAR *identifier;
240     statement_t *statement;
241 } labelled_statement_t;
242
243 typedef struct _case_clausule_t {
244     expression_t *expr;
245     statement_t *stat;
246
247     struct _case_clausule_t *next;
248 } case_clausule_t;
249
250 typedef struct {
251     statement_t stat;
252     expression_t *expr;
253     case_clausule_t *case_list;
254 } switch_statement_t;
255
256 typedef struct {
257     const WCHAR *identifier;
258     statement_t *statement;
259 } catch_block_t;
260
261 typedef struct {
262     statement_t stat;
263     statement_t *try_statement;
264     catch_block_t *catch_block;
265     statement_t *finally_statement;
266 } try_statement_t;
267
268 HRESULT block_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
269 HRESULT var_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
270 HRESULT empty_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
271 HRESULT expression_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
272 HRESULT if_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
273 HRESULT while_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
274 HRESULT for_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
275 HRESULT forin_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
276 HRESULT continue_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
277 HRESULT break_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
278 HRESULT return_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
279 HRESULT with_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
280 HRESULT labelled_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
281 HRESULT switch_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
282 HRESULT throw_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
283 HRESULT try_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
284
285 typedef struct {
286     enum {
287         EXPRVAL_VARIANT,
288         EXPRVAL_IDREF,
289         EXPRVAL_NAMEREF,
290         EXPRVAL_INVALID
291     } type;
292     union {
293         VARIANT var;
294         struct {
295             IDispatch *disp;
296             DISPID id;
297         } idref;
298         struct {
299             IDispatch *disp;
300             BSTR name;
301         } nameref;
302         BSTR identifier;
303     } u;
304 } exprval_t;
305
306 typedef HRESULT (*expression_eval_t)(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
307
308 struct _expression_t {
309     expression_eval_t eval;
310 };
311
312 struct _parameter_t {
313     const WCHAR *identifier;
314
315     struct _parameter_t *next;
316 };
317
318 struct _source_elements_t {
319     statement_t *statement;
320     statement_t *statement_tail;
321     function_declaration_t *functions;
322     var_list_t *variables;
323 };
324
325 struct _function_expression_t {
326     expression_t expr;
327     const WCHAR *identifier;
328     parameter_t *parameter_list;
329     source_elements_t *source_elements;
330     const WCHAR *src_str;
331     DWORD src_len;
332 };
333
334 typedef struct {
335     expression_t expr;
336     expression_t *expression1;
337     expression_t *expression2;
338 } binary_expression_t;
339
340 typedef struct {
341     expression_t expr;
342     expression_t *expression;
343 } unary_expression_t;
344
345 typedef struct {
346     expression_t expr;
347     expression_t *expression;
348     expression_t *true_expression;
349     expression_t *false_expression;
350 } conditional_expression_t;
351
352 typedef struct {
353     expression_t expr;
354     expression_t *member_expr;
355     expression_t *expression;
356 } array_expression_t;
357
358 typedef struct {
359     expression_t expr;
360     expression_t *expression;
361     const WCHAR *identifier;
362 } member_expression_t;
363
364 typedef struct _argument_t {
365     expression_t *expr;
366
367     struct _argument_t *next;
368 } argument_t;
369
370 typedef struct {
371     expression_t expr;
372     expression_t *expression;
373     argument_t *argument_list;
374 } call_expression_t;
375
376 typedef struct {
377     expression_t expr;
378     const WCHAR *identifier;
379 } identifier_expression_t;
380
381 typedef struct {
382     expression_t expr;
383     literal_t *literal;
384 } literal_expression_t;
385
386 typedef struct _array_element_t {
387     int elision;
388     expression_t *expr;
389
390     struct _array_element_t *next;
391 } array_element_t;
392
393 typedef struct {
394     expression_t expr;
395     array_element_t *element_list;
396     int length;
397 } array_literal_expression_t;
398
399 typedef struct _prop_val_t {
400     literal_t *name;
401     expression_t *value;
402
403     struct _prop_val_t *next;
404 } prop_val_t;
405
406 typedef struct {
407     expression_t expr;
408     prop_val_t *property_list;
409 } property_value_expression_t;
410
411 typedef enum {
412      EXPR_COMMA,
413      EXPR_OR,
414      EXPR_AND,
415      EXPR_BOR,
416      EXPR_BXOR,
417      EXPR_BAND,
418      EXPR_INSTANCEOF,
419      EXPR_IN,
420      EXPR_ADD,
421      EXPR_SUB,
422      EXPR_MUL,
423      EXPR_DIV,
424      EXPR_MOD,
425      EXPR_DELETE,
426      EXPR_VOID,
427      EXPR_TYPEOF,
428      EXPR_MINUS,
429      EXPR_PLUS,
430      EXPR_POSTINC,
431      EXPR_POSTDEC,
432      EXPR_PREINC,
433      EXPR_PREDEC,
434      EXPR_EQ,
435      EXPR_EQEQ,
436      EXPR_NOTEQ,
437      EXPR_NOTEQEQ,
438      EXPR_LESS,
439      EXPR_LESSEQ,
440      EXPR_GREATER,
441      EXPR_GREATEREQ,
442      EXPR_BITNEG,
443      EXPR_LOGNEG,
444      EXPR_LSHIFT,
445      EXPR_RSHIFT,
446      EXPR_RRSHIFT,
447      EXPR_ASSIGN,
448      EXPR_ASSIGNLSHIFT,
449      EXPR_ASSIGNRSHIFT,
450      EXPR_ASSIGNRRSHIFT,
451      EXPR_ASSIGNADD,
452      EXPR_ASSIGNSUB,
453      EXPR_ASSIGNMUL,
454      EXPR_ASSIGNDIV,
455      EXPR_ASSIGNMOD,
456      EXPR_ASSIGNAND,
457      EXPR_ASSIGNOR,
458      EXPR_ASSIGNXOR
459 } expression_type_t;
460
461 HRESULT function_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
462 HRESULT conditional_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
463 HRESULT array_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
464 HRESULT member_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
465 HRESULT new_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
466 HRESULT call_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
467 HRESULT this_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
468 HRESULT identifier_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
469 HRESULT literal_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
470 HRESULT array_literal_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
471 HRESULT property_value_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
472
473 HRESULT comma_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
474 HRESULT logical_or_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
475 HRESULT logical_and_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
476 HRESULT binary_or_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
477 HRESULT binary_xor_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
478 HRESULT binary_and_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
479 HRESULT instanceof_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
480 HRESULT in_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
481 HRESULT add_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
482 HRESULT sub_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
483 HRESULT mul_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
484 HRESULT div_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
485 HRESULT mod_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
486 HRESULT delete_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
487 HRESULT void_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
488 HRESULT typeof_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
489 HRESULT minus_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
490 HRESULT plus_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
491 HRESULT post_increment_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
492 HRESULT post_decrement_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
493 HRESULT pre_increment_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
494 HRESULT pre_decrement_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
495 HRESULT equal_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
496 HRESULT equal2_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
497 HRESULT not_equal_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
498 HRESULT not_equal2_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
499 HRESULT less_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
500 HRESULT lesseq_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
501 HRESULT greater_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
502 HRESULT greatereq_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
503 HRESULT binary_negation_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
504 HRESULT logical_negation_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
505 HRESULT left_shift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
506 HRESULT right_shift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
507 HRESULT right2_shift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
508 HRESULT assign_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
509 HRESULT assign_lshift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
510 HRESULT assign_rshift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
511 HRESULT assign_rrshift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
512 HRESULT assign_add_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
513 HRESULT assign_sub_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
514 HRESULT assign_mul_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
515 HRESULT assign_div_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
516 HRESULT assign_mod_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
517 HRESULT assign_and_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
518 HRESULT assign_or_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
519 HRESULT assign_xor_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);