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