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