jscript: Don't store compiler_ctx_t in parser_ctx_t.
[wine/multimedia.git] / dlls / jscript / engine.h
blob1c4f49a353e7ad0d5c288f91549b3dd1009aaeb5
1 /*
2 * Copyright 2008,2011 Jacek Caban for CodeWeavers
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.
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.
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
19 typedef struct _source_elements_t source_elements_t;
20 typedef struct _function_expression_t function_expression_t;
21 typedef struct _expression_t expression_t;
22 typedef struct _statement_t statement_t;
24 typedef struct _function_declaration_t {
25 function_expression_t *expr;
27 struct _function_declaration_t *next;
28 } function_declaration_t;
30 typedef struct _var_list_t {
31 const WCHAR *identifier;
33 struct _var_list_t *next;
34 } var_list_t;
36 typedef struct _func_stack {
37 function_declaration_t *func_head;
38 function_declaration_t *func_tail;
39 var_list_t *var_head;
40 var_list_t *var_tail;
42 struct _func_stack *next;
43 } func_stack_t;
45 #define OP_LIST \
46 X(add, 1, 0,0) \
47 X(and, 1, 0,0) \
48 X(array, 1, 0,0) \
49 X(assign, 1, 0,0) \
50 X(bool, 1, ARG_INT, 0) \
51 X(bneg, 1, 0,0) \
52 X(call, 1, ARG_UINT, ARG_UINT) \
53 X(call_member,1, ARG_UINT, ARG_UINT) \
54 X(carray, 1, ARG_UINT, 0) \
55 X(case, 0, ARG_ADDR, 0) \
56 X(cnd_nz, 0, ARG_ADDR, 0) \
57 X(cnd_z, 0, ARG_ADDR, 0) \
58 X(delete, 1, 0,0) \
59 X(delete_ident,1,ARG_BSTR, 0) \
60 X(div, 1, 0,0) \
61 X(double, 1, ARG_DBL, 0) \
62 X(end_finally,1, 0,0) \
63 X(eq, 1, 0,0) \
64 X(eq2, 1, 0,0) \
65 X(forin, 0, ARG_ADDR, 0) \
66 X(func, 1, ARG_FUNC, 0) \
67 X(gt, 1, 0,0) \
68 X(gteq, 1, 0,0) \
69 X(ident, 1, ARG_BSTR, 0) \
70 X(identid, 1, ARG_BSTR, ARG_INT) \
71 X(in, 1, 0,0) \
72 X(instanceof, 1, 0,0) \
73 X(int, 1, ARG_INT, 0) \
74 X(jmp, 0, ARG_ADDR, 0) \
75 X(jmp_z, 0, ARG_ADDR, 0) \
76 X(lshift, 1, 0,0) \
77 X(lt, 1, 0,0) \
78 X(lteq, 1, 0,0) \
79 X(member, 1, ARG_BSTR, 0) \
80 X(memberid, 1, ARG_UINT, 0) \
81 X(minus, 1, 0,0) \
82 X(mod, 1, 0,0) \
83 X(mul, 1, 0,0) \
84 X(neg, 1, 0,0) \
85 X(neq, 1, 0,0) \
86 X(neq2, 1, 0,0) \
87 X(new, 1, ARG_INT, 0) \
88 X(new_obj, 1, 0,0) \
89 X(null, 1, 0,0) \
90 X(obj_prop, 1, ARG_BSTR, 0) \
91 X(or, 1, 0,0) \
92 X(pop, 1, 0,0) \
93 X(pop_except, 1, 0,0) \
94 X(pop_scope, 1, 0,0) \
95 X(postinc, 1, ARG_INT, 0) \
96 X(preinc, 1, ARG_INT, 0) \
97 X(push_except,1, ARG_ADDR, ARG_BSTR) \
98 X(push_scope, 1, 0,0) \
99 X(regexp, 1, ARG_STR, ARG_INT) \
100 X(rshift, 1, 0,0) \
101 X(rshift2, 1, 0,0) \
102 X(str, 1, ARG_STR, 0) \
103 X(this, 1, 0,0) \
104 X(throw, 0, 0,0) \
105 X(throw_ref, 0, ARG_UINT, 0) \
106 X(throw_type, 0, ARG_UINT, ARG_STR) \
107 X(tonum, 1, 0,0) \
108 X(typeof, 1, 0,0) \
109 X(typeofid, 1, 0,0) \
110 X(typeofident,1, 0,0) \
111 X(refval, 1, 0,0) \
112 X(ret, 0, 0,0) \
113 X(sub, 1, 0,0) \
114 X(undefined, 1, 0,0) \
115 X(var_set, 1, ARG_BSTR, 0) \
116 X(void, 1, 0,0) \
117 X(xor, 1, 0,0)
119 typedef enum {
120 #define X(x,a,b,c) OP_##x,
121 OP_LIST
122 #undef X
123 OP_LAST
124 } jsop_t;
126 typedef union {
127 BSTR bstr;
128 double *dbl;
129 LONG lng;
130 WCHAR *str;
131 unsigned uint;
132 function_expression_t *func; /* FIXME */
133 } instr_arg_t;
135 typedef enum {
136 ARG_NONE = 0,
137 ARG_ADDR,
138 ARG_BSTR,
139 ARG_DBL,
140 ARG_FUNC,
141 ARG_INT,
142 ARG_STR,
143 ARG_UINT
144 } instr_arg_type_t;
146 typedef struct {
147 jsop_t op;
148 instr_arg_t arg1;
149 instr_arg_t arg2;
150 } instr_t;
152 typedef struct {
153 instr_t *instrs;
154 jsheap_t heap;
156 BSTR *bstr_pool;
157 unsigned bstr_pool_size;
158 unsigned bstr_cnt;
159 } bytecode_t;
161 void release_bytecode(bytecode_t*);
163 typedef struct _parser_ctx_t {
164 LONG ref;
166 WCHAR *begin;
167 const WCHAR *end;
168 const WCHAR *ptr;
170 script_ctx_t *script;
171 source_elements_t *source;
172 BOOL nl;
173 BOOL is_html;
174 BOOL lexer_error;
175 HRESULT hres;
177 jsheap_t heap;
179 func_stack_t *func_stack;
181 bytecode_t *code;
183 struct _parser_ctx_t *next;
184 } parser_ctx_t;
186 HRESULT script_parse(script_ctx_t*,const WCHAR*,const WCHAR*,BOOL,parser_ctx_t**) DECLSPEC_HIDDEN;
187 void parser_release(parser_ctx_t*) DECLSPEC_HIDDEN;
189 int parser_lex(void*,parser_ctx_t*) DECLSPEC_HIDDEN;
191 static inline void parser_addref(parser_ctx_t *ctx)
193 ctx->ref++;
196 static inline void *parser_alloc(parser_ctx_t *ctx, DWORD size)
198 return jsheap_alloc(&ctx->heap, size);
201 static inline void *parser_alloc_tmp(parser_ctx_t *ctx, DWORD size)
203 return jsheap_alloc(&ctx->script->tmp_heap, size);
206 typedef struct _scope_chain_t {
207 LONG ref;
208 jsdisp_t *obj;
209 struct _scope_chain_t *next;
210 } scope_chain_t;
212 HRESULT scope_push(scope_chain_t*,jsdisp_t*,scope_chain_t**) DECLSPEC_HIDDEN;
213 void scope_release(scope_chain_t*) DECLSPEC_HIDDEN;
215 static inline void scope_addref(scope_chain_t *scope)
217 scope->ref++;
220 typedef struct _except_frame_t except_frame_t;
222 struct _exec_ctx_t {
223 LONG ref;
225 parser_ctx_t *parser;
226 bytecode_t *code;
227 scope_chain_t *scope_chain;
228 jsdisp_t *var_disp;
229 IDispatch *this_obj;
230 BOOL is_global;
232 VARIANT *stack;
233 unsigned stack_size;
234 unsigned top;
235 except_frame_t *except_frame;
237 unsigned ip;
238 jsexcept_t *ei;
241 static inline void exec_addref(exec_ctx_t *ctx)
243 ctx->ref++;
246 void exec_release(exec_ctx_t*) DECLSPEC_HIDDEN;
247 HRESULT create_exec_ctx(script_ctx_t*,IDispatch*,jsdisp_t*,scope_chain_t*,BOOL,exec_ctx_t**) DECLSPEC_HIDDEN;
248 HRESULT exec_source(exec_ctx_t*,parser_ctx_t*,source_elements_t*,BOOL,jsexcept_t*,VARIANT*) DECLSPEC_HIDDEN;
250 typedef struct _parameter_t parameter_t;
252 HRESULT create_source_function(parser_ctx_t*,parameter_t*,source_elements_t*,scope_chain_t*,
253 const WCHAR*,DWORD,jsdisp_t**) DECLSPEC_HIDDEN;
255 typedef enum {
256 LT_INT,
257 LT_DOUBLE,
258 LT_STRING,
259 LT_BOOL,
260 LT_NULL,
261 LT_REGEXP
262 }literal_type_t;
264 typedef struct {
265 literal_type_t type;
266 union {
267 LONG lval;
268 double dval;
269 const WCHAR *wstr;
270 VARIANT_BOOL bval;
271 struct {
272 const WCHAR *str;
273 DWORD str_len;
274 DWORD flags;
275 } regexp;
276 } u;
277 } literal_t;
279 literal_t *parse_regexp(parser_ctx_t*) DECLSPEC_HIDDEN;
280 literal_t *new_boolean_literal(parser_ctx_t*,VARIANT_BOOL) DECLSPEC_HIDDEN;
282 typedef struct _variable_declaration_t {
283 const WCHAR *identifier;
284 expression_t *expr;
286 struct _variable_declaration_t *next;
287 } variable_declaration_t;
289 typedef enum {
290 STAT_BLOCK,
291 STAT_BREAK,
292 STAT_CONTINUE,
293 STAT_EMPTY,
294 STAT_EXPR,
295 STAT_FOR,
296 STAT_FORIN,
297 STAT_IF,
298 STAT_LABEL,
299 STAT_RETURN,
300 STAT_SWITCH,
301 STAT_THROW,
302 STAT_TRY,
303 STAT_VAR,
304 STAT_WHILE,
305 STAT_WITH
306 } statement_type_t;
308 struct _statement_t {
309 statement_type_t type;
310 statement_t *next;
313 typedef struct {
314 statement_t stat;
315 statement_t *stat_list;
316 } block_statement_t;
318 typedef struct {
319 statement_t stat;
320 variable_declaration_t *variable_list;
321 } var_statement_t;
323 typedef struct {
324 statement_t stat;
325 expression_t *expr;
326 } expression_statement_t;
328 typedef struct {
329 statement_t stat;
330 expression_t *expr;
331 statement_t *if_stat;
332 statement_t *else_stat;
333 } if_statement_t;
335 typedef struct {
336 statement_t stat;
337 BOOL do_while;
338 expression_t *expr;
339 statement_t *statement;
340 } while_statement_t;
342 typedef struct {
343 statement_t stat;
344 variable_declaration_t *variable_list;
345 expression_t *begin_expr;
346 expression_t *expr;
347 expression_t *end_expr;
348 statement_t *statement;
349 } for_statement_t;
351 typedef struct {
352 statement_t stat;
353 variable_declaration_t *variable;
354 expression_t *expr;
355 expression_t *in_expr;
356 statement_t *statement;
357 } forin_statement_t;
359 typedef struct {
360 statement_t stat;
361 const WCHAR *identifier;
362 } branch_statement_t;
364 typedef struct {
365 statement_t stat;
366 expression_t *expr;
367 statement_t *statement;
368 } with_statement_t;
370 typedef struct {
371 statement_t stat;
372 const WCHAR *identifier;
373 statement_t *statement;
374 } labelled_statement_t;
376 typedef struct _case_clausule_t {
377 expression_t *expr;
378 statement_t *stat;
380 struct _case_clausule_t *next;
381 } case_clausule_t;
383 typedef struct {
384 statement_t stat;
385 expression_t *expr;
386 case_clausule_t *case_list;
387 } switch_statement_t;
389 typedef struct {
390 const WCHAR *identifier;
391 statement_t *statement;
392 } catch_block_t;
394 typedef struct {
395 statement_t stat;
396 statement_t *try_statement;
397 catch_block_t *catch_block;
398 statement_t *finally_statement;
399 } try_statement_t;
401 typedef struct {
402 enum {
403 EXPRVAL_VARIANT,
404 EXPRVAL_IDREF,
405 EXPRVAL_INVALID
406 } type;
407 union {
408 VARIANT var;
409 struct {
410 IDispatch *disp;
411 DISPID id;
412 } idref;
413 } u;
414 } exprval_t;
416 typedef enum {
417 EXPR_COMMA,
418 EXPR_OR,
419 EXPR_AND,
420 EXPR_BOR,
421 EXPR_BXOR,
422 EXPR_BAND,
423 EXPR_INSTANCEOF,
424 EXPR_IN,
425 EXPR_ADD,
426 EXPR_SUB,
427 EXPR_MUL,
428 EXPR_DIV,
429 EXPR_MOD,
430 EXPR_DELETE,
431 EXPR_VOID,
432 EXPR_TYPEOF,
433 EXPR_MINUS,
434 EXPR_PLUS,
435 EXPR_POSTINC,
436 EXPR_POSTDEC,
437 EXPR_PREINC,
438 EXPR_PREDEC,
439 EXPR_EQ,
440 EXPR_EQEQ,
441 EXPR_NOTEQ,
442 EXPR_NOTEQEQ,
443 EXPR_LESS,
444 EXPR_LESSEQ,
445 EXPR_GREATER,
446 EXPR_GREATEREQ,
447 EXPR_BITNEG,
448 EXPR_LOGNEG,
449 EXPR_LSHIFT,
450 EXPR_RSHIFT,
451 EXPR_RRSHIFT,
452 EXPR_ASSIGN,
453 EXPR_ASSIGNLSHIFT,
454 EXPR_ASSIGNRSHIFT,
455 EXPR_ASSIGNRRSHIFT,
456 EXPR_ASSIGNADD,
457 EXPR_ASSIGNSUB,
458 EXPR_ASSIGNMUL,
459 EXPR_ASSIGNDIV,
460 EXPR_ASSIGNMOD,
461 EXPR_ASSIGNAND,
462 EXPR_ASSIGNOR,
463 EXPR_ASSIGNXOR,
464 EXPR_COND,
465 EXPR_ARRAY,
466 EXPR_MEMBER,
467 EXPR_NEW,
468 EXPR_CALL,
469 EXPR_THIS,
470 EXPR_FUNC,
471 EXPR_IDENT,
472 EXPR_ARRAYLIT,
473 EXPR_PROPVAL,
474 EXPR_LITERAL
475 } expression_type_t;
477 struct _expression_t {
478 expression_type_t type;
481 struct _parameter_t {
482 const WCHAR *identifier;
484 struct _parameter_t *next;
487 struct _source_elements_t {
488 statement_t *statement;
489 statement_t *statement_tail;
490 function_declaration_t *functions;
491 var_list_t *variables;
492 unsigned instr_off;
495 struct _function_expression_t {
496 expression_t expr;
497 const WCHAR *identifier;
498 parameter_t *parameter_list;
499 source_elements_t *source_elements;
500 const WCHAR *src_str;
501 DWORD src_len;
504 typedef struct {
505 expression_t expr;
506 expression_t *expression1;
507 expression_t *expression2;
508 } binary_expression_t;
510 typedef struct {
511 expression_t expr;
512 expression_t *expression;
513 } unary_expression_t;
515 typedef struct {
516 expression_t expr;
517 expression_t *expression;
518 expression_t *true_expression;
519 expression_t *false_expression;
520 } conditional_expression_t;
522 typedef struct {
523 expression_t expr;
524 expression_t *expression;
525 const WCHAR *identifier;
526 } member_expression_t;
528 typedef struct _argument_t {
529 expression_t *expr;
531 struct _argument_t *next;
532 } argument_t;
534 typedef struct {
535 expression_t expr;
536 expression_t *expression;
537 argument_t *argument_list;
538 } call_expression_t;
540 typedef struct {
541 expression_t expr;
542 const WCHAR *identifier;
543 } identifier_expression_t;
545 typedef struct {
546 expression_t expr;
547 literal_t *literal;
548 } literal_expression_t;
550 typedef struct _array_element_t {
551 int elision;
552 expression_t *expr;
554 struct _array_element_t *next;
555 } array_element_t;
557 typedef struct {
558 expression_t expr;
559 array_element_t *element_list;
560 int length;
561 } array_literal_expression_t;
563 typedef struct _prop_val_t {
564 literal_t *name;
565 expression_t *value;
567 struct _prop_val_t *next;
568 } prop_val_t;
570 typedef struct {
571 expression_t expr;
572 prop_val_t *property_list;
573 } property_value_expression_t;
575 HRESULT compile_script(script_ctx_t*,const WCHAR*,const WCHAR*,BOOL,parser_ctx_t**) DECLSPEC_HIDDEN;