jscript: Store variable names in function_code_t.
[wine/multimedia.git] / dlls / jscript / engine.h
blobcc58aa887541d9050604f4e382ff62bc8b70dd99
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 typedef struct {
46 const WCHAR *begin;
47 const WCHAR *end;
48 const WCHAR *ptr;
50 script_ctx_t *script;
51 source_elements_t *source;
52 BOOL nl;
53 BOOL is_html;
54 BOOL lexer_error;
55 HRESULT hres;
57 jsheap_t heap;
59 func_stack_t *func_stack;
60 } parser_ctx_t;
62 #define OP_LIST \
63 X(add, 1, 0,0) \
64 X(and, 1, 0,0) \
65 X(array, 1, 0,0) \
66 X(assign, 1, 0,0) \
67 X(assign_call,1, ARG_UINT, 0) \
68 X(bool, 1, ARG_INT, 0) \
69 X(bneg, 1, 0,0) \
70 X(call, 1, ARG_UINT, ARG_UINT) \
71 X(call_member,1, ARG_UINT, ARG_UINT) \
72 X(carray, 1, ARG_UINT, 0) \
73 X(case, 0, ARG_ADDR, 0) \
74 X(cnd_nz, 0, ARG_ADDR, 0) \
75 X(cnd_z, 0, ARG_ADDR, 0) \
76 X(delete, 1, 0,0) \
77 X(delete_ident,1,ARG_BSTR, 0) \
78 X(div, 1, 0,0) \
79 X(double, 1, ARG_DBL, 0) \
80 X(end_finally,1, 0,0) \
81 X(eq, 1, 0,0) \
82 X(eq2, 1, 0,0) \
83 X(forin, 0, ARG_ADDR, 0) \
84 X(func, 1, ARG_UINT, 0) \
85 X(gt, 1, 0,0) \
86 X(gteq, 1, 0,0) \
87 X(ident, 1, ARG_BSTR, 0) \
88 X(identid, 1, ARG_BSTR, ARG_INT) \
89 X(in, 1, 0,0) \
90 X(instanceof, 1, 0,0) \
91 X(int, 1, ARG_INT, 0) \
92 X(jmp, 0, ARG_ADDR, 0) \
93 X(jmp_z, 0, ARG_ADDR, 0) \
94 X(lshift, 1, 0,0) \
95 X(lt, 1, 0,0) \
96 X(lteq, 1, 0,0) \
97 X(member, 1, ARG_BSTR, 0) \
98 X(memberid, 1, ARG_UINT, 0) \
99 X(minus, 1, 0,0) \
100 X(mod, 1, 0,0) \
101 X(mul, 1, 0,0) \
102 X(neg, 1, 0,0) \
103 X(neq, 1, 0,0) \
104 X(neq2, 1, 0,0) \
105 X(new, 1, ARG_INT, 0) \
106 X(new_obj, 1, 0,0) \
107 X(null, 1, 0,0) \
108 X(obj_prop, 1, ARG_BSTR, 0) \
109 X(or, 1, 0,0) \
110 X(pop, 1, 0,0) \
111 X(pop_except, 1, 0,0) \
112 X(pop_scope, 1, 0,0) \
113 X(postinc, 1, ARG_INT, 0) \
114 X(preinc, 1, ARG_INT, 0) \
115 X(push_except,1, ARG_ADDR, ARG_BSTR) \
116 X(push_scope, 1, 0,0) \
117 X(regexp, 1, ARG_STR, ARG_INT) \
118 X(rshift, 1, 0,0) \
119 X(rshift2, 1, 0,0) \
120 X(str, 1, ARG_STR, 0) \
121 X(this, 1, 0,0) \
122 X(throw, 0, 0,0) \
123 X(throw_ref, 0, ARG_UINT, 0) \
124 X(throw_type, 0, ARG_UINT, ARG_STR) \
125 X(tonum, 1, 0,0) \
126 X(typeof, 1, 0,0) \
127 X(typeofid, 1, 0,0) \
128 X(typeofident,1, 0,0) \
129 X(refval, 1, 0,0) \
130 X(ret, 0, 0,0) \
131 X(sub, 1, 0,0) \
132 X(undefined, 1, 0,0) \
133 X(var_set, 1, ARG_BSTR, 0) \
134 X(void, 1, 0,0) \
135 X(xor, 1, 0,0)
137 typedef enum {
138 #define X(x,a,b,c) OP_##x,
139 OP_LIST
140 #undef X
141 OP_LAST
142 } jsop_t;
144 typedef union {
145 BSTR bstr;
146 double *dbl;
147 LONG lng;
148 WCHAR *str;
149 unsigned uint;
150 } instr_arg_t;
152 typedef enum {
153 ARG_NONE = 0,
154 ARG_ADDR,
155 ARG_BSTR,
156 ARG_DBL,
157 ARG_FUNC,
158 ARG_INT,
159 ARG_STR,
160 ARG_UINT
161 } instr_arg_type_t;
163 typedef struct {
164 jsop_t op;
165 instr_arg_t arg1;
166 instr_arg_t arg2;
167 } instr_t;
169 typedef struct _function_code_t {
170 BSTR name;
171 unsigned instr_off;
173 function_expression_t *expr; /* FIXME */
175 const WCHAR *source;
176 unsigned source_len;
178 unsigned func_cnt;
179 struct _function_code_t *funcs;
181 unsigned var_cnt;
182 BSTR *variables;
183 } function_code_t;
185 typedef struct _bytecode_t {
186 LONG ref;
188 instr_t *instrs;
189 jsheap_t heap;
191 function_code_t global_code;
193 WCHAR *source;
195 BSTR *bstr_pool;
196 unsigned bstr_pool_size;
197 unsigned bstr_cnt;
199 parser_ctx_t *parser;
201 struct _bytecode_t *next;
202 } bytecode_t;
204 HRESULT compile_script(script_ctx_t*,const WCHAR*,const WCHAR*,BOOL,BOOL,bytecode_t**) DECLSPEC_HIDDEN;
205 void release_bytecode(bytecode_t*) DECLSPEC_HIDDEN;
207 static inline void bytecode_addref(bytecode_t *code)
209 code->ref++;
212 HRESULT script_parse(script_ctx_t*,const WCHAR*,const WCHAR*,BOOL,parser_ctx_t**) DECLSPEC_HIDDEN;
213 void parser_release(parser_ctx_t*) DECLSPEC_HIDDEN;
215 int parser_lex(void*,parser_ctx_t*) DECLSPEC_HIDDEN;
217 static inline void *parser_alloc(parser_ctx_t *ctx, DWORD size)
219 return jsheap_alloc(&ctx->heap, size);
222 static inline void *parser_alloc_tmp(parser_ctx_t *ctx, DWORD size)
224 return jsheap_alloc(&ctx->script->tmp_heap, size);
227 typedef struct _scope_chain_t {
228 LONG ref;
229 jsdisp_t *obj;
230 struct _scope_chain_t *next;
231 } scope_chain_t;
233 HRESULT scope_push(scope_chain_t*,jsdisp_t*,scope_chain_t**) DECLSPEC_HIDDEN;
234 void scope_release(scope_chain_t*) DECLSPEC_HIDDEN;
236 static inline void scope_addref(scope_chain_t *scope)
238 scope->ref++;
241 typedef struct _except_frame_t except_frame_t;
243 struct _exec_ctx_t {
244 LONG ref;
246 parser_ctx_t *parser;
247 bytecode_t *code;
248 script_ctx_t *script;
249 scope_chain_t *scope_chain;
250 jsdisp_t *var_disp;
251 IDispatch *this_obj;
252 function_code_t *func_code;
253 BOOL is_global;
255 VARIANT *stack;
256 unsigned stack_size;
257 unsigned top;
258 except_frame_t *except_frame;
260 unsigned ip;
261 jsexcept_t *ei;
264 static inline void exec_addref(exec_ctx_t *ctx)
266 ctx->ref++;
269 void exec_release(exec_ctx_t*) DECLSPEC_HIDDEN;
270 HRESULT create_exec_ctx(script_ctx_t*,IDispatch*,jsdisp_t*,scope_chain_t*,BOOL,exec_ctx_t**) DECLSPEC_HIDDEN;
271 HRESULT exec_source(exec_ctx_t*,bytecode_t*,function_code_t*,BOOL,jsexcept_t*,VARIANT*) DECLSPEC_HIDDEN;
273 typedef struct _parameter_t parameter_t;
275 HRESULT create_source_function(script_ctx_t*,bytecode_t*,parameter_t*,function_code_t*,scope_chain_t*,
276 jsdisp_t**) DECLSPEC_HIDDEN;
278 typedef enum {
279 LT_INT,
280 LT_DOUBLE,
281 LT_STRING,
282 LT_BOOL,
283 LT_NULL,
284 LT_REGEXP
285 }literal_type_t;
287 typedef struct {
288 literal_type_t type;
289 union {
290 LONG lval;
291 double dval;
292 const WCHAR *wstr;
293 VARIANT_BOOL bval;
294 struct {
295 const WCHAR *str;
296 DWORD str_len;
297 DWORD flags;
298 } regexp;
299 } u;
300 } literal_t;
302 literal_t *parse_regexp(parser_ctx_t*) DECLSPEC_HIDDEN;
303 literal_t *new_boolean_literal(parser_ctx_t*,VARIANT_BOOL) DECLSPEC_HIDDEN;
305 typedef struct _variable_declaration_t {
306 const WCHAR *identifier;
307 expression_t *expr;
309 struct _variable_declaration_t *next;
310 } variable_declaration_t;
312 typedef enum {
313 STAT_BLOCK,
314 STAT_BREAK,
315 STAT_CONTINUE,
316 STAT_EMPTY,
317 STAT_EXPR,
318 STAT_FOR,
319 STAT_FORIN,
320 STAT_IF,
321 STAT_LABEL,
322 STAT_RETURN,
323 STAT_SWITCH,
324 STAT_THROW,
325 STAT_TRY,
326 STAT_VAR,
327 STAT_WHILE,
328 STAT_WITH
329 } statement_type_t;
331 struct _statement_t {
332 statement_type_t type;
333 statement_t *next;
336 typedef struct {
337 statement_t stat;
338 statement_t *stat_list;
339 } block_statement_t;
341 typedef struct {
342 statement_t stat;
343 variable_declaration_t *variable_list;
344 } var_statement_t;
346 typedef struct {
347 statement_t stat;
348 expression_t *expr;
349 } expression_statement_t;
351 typedef struct {
352 statement_t stat;
353 expression_t *expr;
354 statement_t *if_stat;
355 statement_t *else_stat;
356 } if_statement_t;
358 typedef struct {
359 statement_t stat;
360 BOOL do_while;
361 expression_t *expr;
362 statement_t *statement;
363 } while_statement_t;
365 typedef struct {
366 statement_t stat;
367 variable_declaration_t *variable_list;
368 expression_t *begin_expr;
369 expression_t *expr;
370 expression_t *end_expr;
371 statement_t *statement;
372 } for_statement_t;
374 typedef struct {
375 statement_t stat;
376 variable_declaration_t *variable;
377 expression_t *expr;
378 expression_t *in_expr;
379 statement_t *statement;
380 } forin_statement_t;
382 typedef struct {
383 statement_t stat;
384 const WCHAR *identifier;
385 } branch_statement_t;
387 typedef struct {
388 statement_t stat;
389 expression_t *expr;
390 statement_t *statement;
391 } with_statement_t;
393 typedef struct {
394 statement_t stat;
395 const WCHAR *identifier;
396 statement_t *statement;
397 } labelled_statement_t;
399 typedef struct _case_clausule_t {
400 expression_t *expr;
401 statement_t *stat;
403 struct _case_clausule_t *next;
404 } case_clausule_t;
406 typedef struct {
407 statement_t stat;
408 expression_t *expr;
409 case_clausule_t *case_list;
410 } switch_statement_t;
412 typedef struct {
413 const WCHAR *identifier;
414 statement_t *statement;
415 } catch_block_t;
417 typedef struct {
418 statement_t stat;
419 statement_t *try_statement;
420 catch_block_t *catch_block;
421 statement_t *finally_statement;
422 } try_statement_t;
424 typedef struct {
425 enum {
426 EXPRVAL_VARIANT,
427 EXPRVAL_IDREF,
428 EXPRVAL_INVALID
429 } type;
430 union {
431 VARIANT var;
432 struct {
433 IDispatch *disp;
434 DISPID id;
435 } idref;
436 } u;
437 } exprval_t;
439 typedef enum {
440 EXPR_COMMA,
441 EXPR_OR,
442 EXPR_AND,
443 EXPR_BOR,
444 EXPR_BXOR,
445 EXPR_BAND,
446 EXPR_INSTANCEOF,
447 EXPR_IN,
448 EXPR_ADD,
449 EXPR_SUB,
450 EXPR_MUL,
451 EXPR_DIV,
452 EXPR_MOD,
453 EXPR_DELETE,
454 EXPR_VOID,
455 EXPR_TYPEOF,
456 EXPR_MINUS,
457 EXPR_PLUS,
458 EXPR_POSTINC,
459 EXPR_POSTDEC,
460 EXPR_PREINC,
461 EXPR_PREDEC,
462 EXPR_EQ,
463 EXPR_EQEQ,
464 EXPR_NOTEQ,
465 EXPR_NOTEQEQ,
466 EXPR_LESS,
467 EXPR_LESSEQ,
468 EXPR_GREATER,
469 EXPR_GREATEREQ,
470 EXPR_BITNEG,
471 EXPR_LOGNEG,
472 EXPR_LSHIFT,
473 EXPR_RSHIFT,
474 EXPR_RRSHIFT,
475 EXPR_ASSIGN,
476 EXPR_ASSIGNLSHIFT,
477 EXPR_ASSIGNRSHIFT,
478 EXPR_ASSIGNRRSHIFT,
479 EXPR_ASSIGNADD,
480 EXPR_ASSIGNSUB,
481 EXPR_ASSIGNMUL,
482 EXPR_ASSIGNDIV,
483 EXPR_ASSIGNMOD,
484 EXPR_ASSIGNAND,
485 EXPR_ASSIGNOR,
486 EXPR_ASSIGNXOR,
487 EXPR_COND,
488 EXPR_ARRAY,
489 EXPR_MEMBER,
490 EXPR_NEW,
491 EXPR_CALL,
492 EXPR_THIS,
493 EXPR_FUNC,
494 EXPR_IDENT,
495 EXPR_ARRAYLIT,
496 EXPR_PROPVAL,
497 EXPR_LITERAL
498 } expression_type_t;
500 struct _expression_t {
501 expression_type_t type;
504 struct _parameter_t {
505 const WCHAR *identifier;
507 struct _parameter_t *next;
510 struct _source_elements_t {
511 statement_t *statement;
512 statement_t *statement_tail;
513 function_declaration_t *functions;
514 var_list_t *variables;
517 struct _function_expression_t {
518 expression_t expr;
519 const WCHAR *identifier;
520 parameter_t *parameter_list;
521 source_elements_t *source_elements;
522 const WCHAR *src_str;
523 DWORD src_len;
526 typedef struct {
527 expression_t expr;
528 expression_t *expression1;
529 expression_t *expression2;
530 } binary_expression_t;
532 typedef struct {
533 expression_t expr;
534 expression_t *expression;
535 } unary_expression_t;
537 typedef struct {
538 expression_t expr;
539 expression_t *expression;
540 expression_t *true_expression;
541 expression_t *false_expression;
542 } conditional_expression_t;
544 typedef struct {
545 expression_t expr;
546 expression_t *expression;
547 const WCHAR *identifier;
548 } member_expression_t;
550 typedef struct _argument_t {
551 expression_t *expr;
553 struct _argument_t *next;
554 } argument_t;
556 typedef struct {
557 expression_t expr;
558 expression_t *expression;
559 argument_t *argument_list;
560 } call_expression_t;
562 typedef struct {
563 expression_t expr;
564 const WCHAR *identifier;
565 } identifier_expression_t;
567 typedef struct {
568 expression_t expr;
569 literal_t *literal;
570 } literal_expression_t;
572 typedef struct _array_element_t {
573 int elision;
574 expression_t *expr;
576 struct _array_element_t *next;
577 } array_element_t;
579 typedef struct {
580 expression_t expr;
581 array_element_t *element_list;
582 int length;
583 } array_literal_expression_t;
585 typedef struct _prop_val_t {
586 literal_t *name;
587 expression_t *value;
589 struct _prop_val_t *next;
590 } prop_val_t;
592 typedef struct {
593 expression_t expr;
594 prop_val_t *property_list;
595 } property_value_expression_t;