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