jscript: Use compiler-generated struct for representing function code.
[wine/multimedia.git] / dlls / jscript / engine.h
blob3234ed9a1cba1b270ed617973d9031bc779d06a3
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 unsigned instr_off;
172 function_expression_t *expr; /* FIXME */
173 source_elements_t *source_elements; /* FIXME */
175 unsigned func_cnt;
176 struct _function_code_t *funcs;
177 } function_code_t;
179 typedef struct _bytecode_t {
180 LONG ref;
182 instr_t *instrs;
183 jsheap_t heap;
185 function_code_t global_code;
187 WCHAR *source;
189 BSTR *bstr_pool;
190 unsigned bstr_pool_size;
191 unsigned bstr_cnt;
193 parser_ctx_t *parser;
195 struct _bytecode_t *next;
196 } bytecode_t;
198 HRESULT compile_script(script_ctx_t*,const WCHAR*,const WCHAR*,BOOL,BOOL,bytecode_t**) DECLSPEC_HIDDEN;
199 void release_bytecode(bytecode_t*) DECLSPEC_HIDDEN;
201 static inline void bytecode_addref(bytecode_t *code)
203 code->ref++;
206 HRESULT script_parse(script_ctx_t*,const WCHAR*,const WCHAR*,BOOL,parser_ctx_t**) DECLSPEC_HIDDEN;
207 void parser_release(parser_ctx_t*) DECLSPEC_HIDDEN;
209 int parser_lex(void*,parser_ctx_t*) DECLSPEC_HIDDEN;
211 static inline void *parser_alloc(parser_ctx_t *ctx, DWORD size)
213 return jsheap_alloc(&ctx->heap, size);
216 static inline void *parser_alloc_tmp(parser_ctx_t *ctx, DWORD size)
218 return jsheap_alloc(&ctx->script->tmp_heap, size);
221 typedef struct _scope_chain_t {
222 LONG ref;
223 jsdisp_t *obj;
224 struct _scope_chain_t *next;
225 } scope_chain_t;
227 HRESULT scope_push(scope_chain_t*,jsdisp_t*,scope_chain_t**) DECLSPEC_HIDDEN;
228 void scope_release(scope_chain_t*) DECLSPEC_HIDDEN;
230 static inline void scope_addref(scope_chain_t *scope)
232 scope->ref++;
235 typedef struct _except_frame_t except_frame_t;
237 struct _exec_ctx_t {
238 LONG ref;
240 parser_ctx_t *parser;
241 bytecode_t *code;
242 script_ctx_t *script;
243 scope_chain_t *scope_chain;
244 jsdisp_t *var_disp;
245 IDispatch *this_obj;
246 function_code_t *func_code;
247 BOOL is_global;
249 VARIANT *stack;
250 unsigned stack_size;
251 unsigned top;
252 except_frame_t *except_frame;
254 unsigned ip;
255 jsexcept_t *ei;
258 static inline void exec_addref(exec_ctx_t *ctx)
260 ctx->ref++;
263 void exec_release(exec_ctx_t*) DECLSPEC_HIDDEN;
264 HRESULT create_exec_ctx(script_ctx_t*,IDispatch*,jsdisp_t*,scope_chain_t*,BOOL,exec_ctx_t**) DECLSPEC_HIDDEN;
265 HRESULT exec_source(exec_ctx_t*,bytecode_t*,function_code_t*,BOOL,jsexcept_t*,VARIANT*) DECLSPEC_HIDDEN;
267 typedef struct _parameter_t parameter_t;
269 HRESULT create_source_function(script_ctx_t*,bytecode_t*,parameter_t*,function_code_t*,scope_chain_t*,
270 const WCHAR*,DWORD,jsdisp_t**) DECLSPEC_HIDDEN;
272 typedef enum {
273 LT_INT,
274 LT_DOUBLE,
275 LT_STRING,
276 LT_BOOL,
277 LT_NULL,
278 LT_REGEXP
279 }literal_type_t;
281 typedef struct {
282 literal_type_t type;
283 union {
284 LONG lval;
285 double dval;
286 const WCHAR *wstr;
287 VARIANT_BOOL bval;
288 struct {
289 const WCHAR *str;
290 DWORD str_len;
291 DWORD flags;
292 } regexp;
293 } u;
294 } literal_t;
296 literal_t *parse_regexp(parser_ctx_t*) DECLSPEC_HIDDEN;
297 literal_t *new_boolean_literal(parser_ctx_t*,VARIANT_BOOL) DECLSPEC_HIDDEN;
299 typedef struct _variable_declaration_t {
300 const WCHAR *identifier;
301 expression_t *expr;
303 struct _variable_declaration_t *next;
304 } variable_declaration_t;
306 typedef enum {
307 STAT_BLOCK,
308 STAT_BREAK,
309 STAT_CONTINUE,
310 STAT_EMPTY,
311 STAT_EXPR,
312 STAT_FOR,
313 STAT_FORIN,
314 STAT_IF,
315 STAT_LABEL,
316 STAT_RETURN,
317 STAT_SWITCH,
318 STAT_THROW,
319 STAT_TRY,
320 STAT_VAR,
321 STAT_WHILE,
322 STAT_WITH
323 } statement_type_t;
325 struct _statement_t {
326 statement_type_t type;
327 statement_t *next;
330 typedef struct {
331 statement_t stat;
332 statement_t *stat_list;
333 } block_statement_t;
335 typedef struct {
336 statement_t stat;
337 variable_declaration_t *variable_list;
338 } var_statement_t;
340 typedef struct {
341 statement_t stat;
342 expression_t *expr;
343 } expression_statement_t;
345 typedef struct {
346 statement_t stat;
347 expression_t *expr;
348 statement_t *if_stat;
349 statement_t *else_stat;
350 } if_statement_t;
352 typedef struct {
353 statement_t stat;
354 BOOL do_while;
355 expression_t *expr;
356 statement_t *statement;
357 } while_statement_t;
359 typedef struct {
360 statement_t stat;
361 variable_declaration_t *variable_list;
362 expression_t *begin_expr;
363 expression_t *expr;
364 expression_t *end_expr;
365 statement_t *statement;
366 } for_statement_t;
368 typedef struct {
369 statement_t stat;
370 variable_declaration_t *variable;
371 expression_t *expr;
372 expression_t *in_expr;
373 statement_t *statement;
374 } forin_statement_t;
376 typedef struct {
377 statement_t stat;
378 const WCHAR *identifier;
379 } branch_statement_t;
381 typedef struct {
382 statement_t stat;
383 expression_t *expr;
384 statement_t *statement;
385 } with_statement_t;
387 typedef struct {
388 statement_t stat;
389 const WCHAR *identifier;
390 statement_t *statement;
391 } labelled_statement_t;
393 typedef struct _case_clausule_t {
394 expression_t *expr;
395 statement_t *stat;
397 struct _case_clausule_t *next;
398 } case_clausule_t;
400 typedef struct {
401 statement_t stat;
402 expression_t *expr;
403 case_clausule_t *case_list;
404 } switch_statement_t;
406 typedef struct {
407 const WCHAR *identifier;
408 statement_t *statement;
409 } catch_block_t;
411 typedef struct {
412 statement_t stat;
413 statement_t *try_statement;
414 catch_block_t *catch_block;
415 statement_t *finally_statement;
416 } try_statement_t;
418 typedef struct {
419 enum {
420 EXPRVAL_VARIANT,
421 EXPRVAL_IDREF,
422 EXPRVAL_INVALID
423 } type;
424 union {
425 VARIANT var;
426 struct {
427 IDispatch *disp;
428 DISPID id;
429 } idref;
430 } u;
431 } exprval_t;
433 typedef enum {
434 EXPR_COMMA,
435 EXPR_OR,
436 EXPR_AND,
437 EXPR_BOR,
438 EXPR_BXOR,
439 EXPR_BAND,
440 EXPR_INSTANCEOF,
441 EXPR_IN,
442 EXPR_ADD,
443 EXPR_SUB,
444 EXPR_MUL,
445 EXPR_DIV,
446 EXPR_MOD,
447 EXPR_DELETE,
448 EXPR_VOID,
449 EXPR_TYPEOF,
450 EXPR_MINUS,
451 EXPR_PLUS,
452 EXPR_POSTINC,
453 EXPR_POSTDEC,
454 EXPR_PREINC,
455 EXPR_PREDEC,
456 EXPR_EQ,
457 EXPR_EQEQ,
458 EXPR_NOTEQ,
459 EXPR_NOTEQEQ,
460 EXPR_LESS,
461 EXPR_LESSEQ,
462 EXPR_GREATER,
463 EXPR_GREATEREQ,
464 EXPR_BITNEG,
465 EXPR_LOGNEG,
466 EXPR_LSHIFT,
467 EXPR_RSHIFT,
468 EXPR_RRSHIFT,
469 EXPR_ASSIGN,
470 EXPR_ASSIGNLSHIFT,
471 EXPR_ASSIGNRSHIFT,
472 EXPR_ASSIGNRRSHIFT,
473 EXPR_ASSIGNADD,
474 EXPR_ASSIGNSUB,
475 EXPR_ASSIGNMUL,
476 EXPR_ASSIGNDIV,
477 EXPR_ASSIGNMOD,
478 EXPR_ASSIGNAND,
479 EXPR_ASSIGNOR,
480 EXPR_ASSIGNXOR,
481 EXPR_COND,
482 EXPR_ARRAY,
483 EXPR_MEMBER,
484 EXPR_NEW,
485 EXPR_CALL,
486 EXPR_THIS,
487 EXPR_FUNC,
488 EXPR_IDENT,
489 EXPR_ARRAYLIT,
490 EXPR_PROPVAL,
491 EXPR_LITERAL
492 } expression_type_t;
494 struct _expression_t {
495 expression_type_t type;
498 struct _parameter_t {
499 const WCHAR *identifier;
501 struct _parameter_t *next;
504 struct _source_elements_t {
505 statement_t *statement;
506 statement_t *statement_tail;
507 function_declaration_t *functions;
508 var_list_t *variables;
511 struct _function_expression_t {
512 expression_t expr;
513 const WCHAR *identifier;
514 parameter_t *parameter_list;
515 source_elements_t *source_elements;
516 const WCHAR *src_str;
517 DWORD src_len;
520 typedef struct {
521 expression_t expr;
522 expression_t *expression1;
523 expression_t *expression2;
524 } binary_expression_t;
526 typedef struct {
527 expression_t expr;
528 expression_t *expression;
529 } unary_expression_t;
531 typedef struct {
532 expression_t expr;
533 expression_t *expression;
534 expression_t *true_expression;
535 expression_t *false_expression;
536 } conditional_expression_t;
538 typedef struct {
539 expression_t expr;
540 expression_t *expression;
541 const WCHAR *identifier;
542 } member_expression_t;
544 typedef struct _argument_t {
545 expression_t *expr;
547 struct _argument_t *next;
548 } argument_t;
550 typedef struct {
551 expression_t expr;
552 expression_t *expression;
553 argument_t *argument_list;
554 } call_expression_t;
556 typedef struct {
557 expression_t expr;
558 const WCHAR *identifier;
559 } identifier_expression_t;
561 typedef struct {
562 expression_t expr;
563 literal_t *literal;
564 } literal_expression_t;
566 typedef struct _array_element_t {
567 int elision;
568 expression_t *expr;
570 struct _array_element_t *next;
571 } array_element_t;
573 typedef struct {
574 expression_t expr;
575 array_element_t *element_list;
576 int length;
577 } array_literal_expression_t;
579 typedef struct _prop_val_t {
580 literal_t *name;
581 expression_t *value;
583 struct _prop_val_t *next;
584 } prop_val_t;
586 typedef struct {
587 expression_t expr;
588 prop_val_t *property_list;
589 } property_value_expression_t;