jscript: Make flags parameter of OP_regexp unsigned.
[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     double *dbl;
123     LONG lng;
124     WCHAR *str;
125     unsigned uint;
126 } instr_arg_t;
127
128 typedef enum {
129     ARG_NONE = 0,
130     ARG_ADDR,
131     ARG_BSTR,
132     ARG_DBL,
133     ARG_FUNC,
134     ARG_INT,
135     ARG_STR,
136     ARG_UINT
137 } instr_arg_type_t;
138
139 typedef struct {
140     jsop_t op;
141     instr_arg_t arg1;
142     instr_arg_t arg2;
143 } instr_t;
144
145 typedef struct _function_code_t {
146     BSTR name;
147     unsigned instr_off;
148
149     const WCHAR *source;
150     unsigned source_len;
151
152     unsigned func_cnt;
153     struct _function_code_t *funcs;
154
155     unsigned var_cnt;
156     BSTR *variables;
157
158     unsigned param_cnt;
159     BSTR *params;
160 } function_code_t;
161
162 typedef struct _bytecode_t {
163     LONG ref;
164
165     instr_t *instrs;
166     jsheap_t heap;
167
168     function_code_t global_code;
169
170     WCHAR *source;
171
172     BSTR *bstr_pool;
173     unsigned bstr_pool_size;
174     unsigned bstr_cnt;
175
176     struct _bytecode_t *next;
177 } bytecode_t;
178
179 HRESULT compile_script(script_ctx_t*,const WCHAR*,const WCHAR*,BOOL,BOOL,bytecode_t**) DECLSPEC_HIDDEN;
180 void release_bytecode(bytecode_t*) DECLSPEC_HIDDEN;
181
182 static inline void bytecode_addref(bytecode_t *code)
183 {
184     code->ref++;
185 }
186
187 HRESULT script_parse(script_ctx_t*,const WCHAR*,const WCHAR*,BOOL,parser_ctx_t**) DECLSPEC_HIDDEN;
188 void parser_release(parser_ctx_t*) DECLSPEC_HIDDEN;
189
190 int parser_lex(void*,parser_ctx_t*) DECLSPEC_HIDDEN;
191
192 static inline void *parser_alloc(parser_ctx_t *ctx, DWORD size)
193 {
194     return jsheap_alloc(&ctx->heap, size);
195 }
196
197 static inline void *parser_alloc_tmp(parser_ctx_t *ctx, DWORD size)
198 {
199     return jsheap_alloc(&ctx->script->tmp_heap, size);
200 }
201
202 typedef struct _scope_chain_t {
203     LONG ref;
204     jsdisp_t *obj;
205     struct _scope_chain_t *next;
206 } scope_chain_t;
207
208 HRESULT scope_push(scope_chain_t*,jsdisp_t*,scope_chain_t**) DECLSPEC_HIDDEN;
209 void scope_release(scope_chain_t*) DECLSPEC_HIDDEN;
210
211 static inline void scope_addref(scope_chain_t *scope)
212 {
213     scope->ref++;
214 }
215
216 typedef struct _except_frame_t except_frame_t;
217
218 struct _exec_ctx_t {
219     LONG ref;
220
221     parser_ctx_t *parser;
222     bytecode_t *code;
223     script_ctx_t *script;
224     scope_chain_t *scope_chain;
225     jsdisp_t *var_disp;
226     IDispatch *this_obj;
227     function_code_t *func_code;
228     BOOL is_global;
229
230     VARIANT *stack;
231     unsigned stack_size;
232     unsigned top;
233     except_frame_t *except_frame;
234
235     unsigned ip;
236     jsexcept_t *ei;
237 };
238
239 static inline void exec_addref(exec_ctx_t *ctx)
240 {
241     ctx->ref++;
242 }
243
244 void exec_release(exec_ctx_t*) DECLSPEC_HIDDEN;
245 HRESULT create_exec_ctx(script_ctx_t*,IDispatch*,jsdisp_t*,scope_chain_t*,BOOL,exec_ctx_t**) DECLSPEC_HIDDEN;
246 HRESULT exec_source(exec_ctx_t*,bytecode_t*,function_code_t*,BOOL,jsexcept_t*,VARIANT*) DECLSPEC_HIDDEN;
247 HRESULT create_source_function(script_ctx_t*,bytecode_t*,function_code_t*,scope_chain_t*,jsdisp_t**) DECLSPEC_HIDDEN;
248
249 typedef enum {
250     LT_INT,
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         LONG lval;
262         double dval;
263         const WCHAR *wstr;
264         VARIANT_BOOL bval;
265         struct {
266             const WCHAR *str;
267             DWORD str_len;
268             DWORD flags;
269         } regexp;
270     } u;
271 } literal_t;
272
273 literal_t *parse_regexp(parser_ctx_t*) DECLSPEC_HIDDEN;
274 literal_t *new_boolean_literal(parser_ctx_t*,VARIANT_BOOL) DECLSPEC_HIDDEN;
275
276 typedef struct _variable_declaration_t {
277     const WCHAR *identifier;
278     expression_t *expr;
279
280     struct _variable_declaration_t *next;
281     struct _variable_declaration_t *global_next; /* for compiler */
282 } variable_declaration_t;
283
284 typedef enum {
285     STAT_BLOCK,
286     STAT_BREAK,
287     STAT_CONTINUE,
288     STAT_EMPTY,
289     STAT_EXPR,
290     STAT_FOR,
291     STAT_FORIN,
292     STAT_IF,
293     STAT_LABEL,
294     STAT_RETURN,
295     STAT_SWITCH,
296     STAT_THROW,
297     STAT_TRY,
298     STAT_VAR,
299     STAT_WHILE,
300     STAT_WITH
301 } statement_type_t;
302
303 struct _statement_t {
304     statement_type_t type;
305     statement_t *next;
306 };
307
308 typedef struct {
309     statement_t stat;
310     statement_t *stat_list;
311 } block_statement_t;
312
313 typedef struct {
314     statement_t stat;
315     variable_declaration_t *variable_list;
316 } var_statement_t;
317
318 typedef struct {
319     statement_t stat;
320     expression_t *expr;
321 } expression_statement_t;
322
323 typedef struct {
324     statement_t stat;
325     expression_t *expr;
326     statement_t *if_stat;
327     statement_t *else_stat;
328 } if_statement_t;
329
330 typedef struct {
331     statement_t stat;
332     BOOL do_while;
333     expression_t *expr;
334     statement_t *statement;
335 } while_statement_t;
336
337 typedef struct {
338     statement_t stat;
339     variable_declaration_t *variable_list;
340     expression_t *begin_expr;
341     expression_t *expr;
342     expression_t *end_expr;
343     statement_t *statement;
344 } for_statement_t;
345
346 typedef struct {
347     statement_t stat;
348     variable_declaration_t *variable;
349     expression_t *expr;
350     expression_t *in_expr;
351     statement_t *statement;
352 } forin_statement_t;
353
354 typedef struct {
355     statement_t stat;
356     const WCHAR *identifier;
357 } branch_statement_t;
358
359 typedef struct {
360     statement_t stat;
361     expression_t *expr;
362     statement_t *statement;
363 } with_statement_t;
364
365 typedef struct {
366     statement_t stat;
367     const WCHAR *identifier;
368     statement_t *statement;
369 } labelled_statement_t;
370
371 typedef struct _case_clausule_t {
372     expression_t *expr;
373     statement_t *stat;
374
375     struct _case_clausule_t *next;
376 } case_clausule_t;
377
378 typedef struct {
379     statement_t stat;
380     expression_t *expr;
381     case_clausule_t *case_list;
382 } switch_statement_t;
383
384 typedef struct {
385     const WCHAR *identifier;
386     statement_t *statement;
387 } catch_block_t;
388
389 typedef struct {
390     statement_t stat;
391     statement_t *try_statement;
392     catch_block_t *catch_block;
393     statement_t *finally_statement;
394 } try_statement_t;
395
396 typedef struct {
397     enum {
398         EXPRVAL_VARIANT,
399         EXPRVAL_IDREF,
400         EXPRVAL_INVALID
401     } type;
402     union {
403         VARIANT var;
404         struct {
405             IDispatch *disp;
406             DISPID id;
407         } idref;
408     } u;
409 } exprval_t;
410
411 typedef enum {
412      EXPR_COMMA,
413      EXPR_OR,
414      EXPR_AND,
415      EXPR_BOR,
416      EXPR_BXOR,
417      EXPR_BAND,
418      EXPR_INSTANCEOF,
419      EXPR_IN,
420      EXPR_ADD,
421      EXPR_SUB,
422      EXPR_MUL,
423      EXPR_DIV,
424      EXPR_MOD,
425      EXPR_DELETE,
426      EXPR_VOID,
427      EXPR_TYPEOF,
428      EXPR_MINUS,
429      EXPR_PLUS,
430      EXPR_POSTINC,
431      EXPR_POSTDEC,
432      EXPR_PREINC,
433      EXPR_PREDEC,
434      EXPR_EQ,
435      EXPR_EQEQ,
436      EXPR_NOTEQ,
437      EXPR_NOTEQEQ,
438      EXPR_LESS,
439      EXPR_LESSEQ,
440      EXPR_GREATER,
441      EXPR_GREATEREQ,
442      EXPR_BITNEG,
443      EXPR_LOGNEG,
444      EXPR_LSHIFT,
445      EXPR_RSHIFT,
446      EXPR_RRSHIFT,
447      EXPR_ASSIGN,
448      EXPR_ASSIGNLSHIFT,
449      EXPR_ASSIGNRSHIFT,
450      EXPR_ASSIGNRRSHIFT,
451      EXPR_ASSIGNADD,
452      EXPR_ASSIGNSUB,
453      EXPR_ASSIGNMUL,
454      EXPR_ASSIGNDIV,
455      EXPR_ASSIGNMOD,
456      EXPR_ASSIGNAND,
457      EXPR_ASSIGNOR,
458      EXPR_ASSIGNXOR,
459      EXPR_COND,
460      EXPR_ARRAY,
461      EXPR_MEMBER,
462      EXPR_NEW,
463      EXPR_CALL,
464      EXPR_THIS,
465      EXPR_FUNC,
466      EXPR_IDENT,
467      EXPR_ARRAYLIT,
468      EXPR_PROPVAL,
469      EXPR_LITERAL
470 } expression_type_t;
471
472 struct _expression_t {
473     expression_type_t type;
474 };
475
476 typedef struct _parameter_t {
477     const WCHAR *identifier;
478     struct _parameter_t *next;
479 } parameter_t;
480
481 struct _source_elements_t {
482     statement_t *statement;
483     statement_t *statement_tail;
484 };
485
486 typedef struct _function_expression_t {
487     expression_t expr;
488     const WCHAR *identifier;
489     parameter_t *parameter_list;
490     source_elements_t *source_elements;
491     const WCHAR *src_str;
492     DWORD src_len;
493
494     struct _function_expression_t *next; /* for compiler */
495 } function_expression_t;
496
497 typedef struct {
498     expression_t expr;
499     expression_t *expression1;
500     expression_t *expression2;
501 } binary_expression_t;
502
503 typedef struct {
504     expression_t expr;
505     expression_t *expression;
506 } unary_expression_t;
507
508 typedef struct {
509     expression_t expr;
510     expression_t *expression;
511     expression_t *true_expression;
512     expression_t *false_expression;
513 } conditional_expression_t;
514
515 typedef struct {
516     expression_t expr;
517     expression_t *expression;
518     const WCHAR *identifier;
519 } member_expression_t;
520
521 typedef struct _argument_t {
522     expression_t *expr;
523
524     struct _argument_t *next;
525 } argument_t;
526
527 typedef struct {
528     expression_t expr;
529     expression_t *expression;
530     argument_t *argument_list;
531 } call_expression_t;
532
533 typedef struct {
534     expression_t expr;
535     const WCHAR *identifier;
536 } identifier_expression_t;
537
538 typedef struct {
539     expression_t expr;
540     literal_t *literal;
541 } literal_expression_t;
542
543 typedef struct _array_element_t {
544     int elision;
545     expression_t *expr;
546
547     struct _array_element_t *next;
548 } array_element_t;
549
550 typedef struct {
551     expression_t expr;
552     array_element_t *element_list;
553     int length;
554 } array_literal_expression_t;
555
556 typedef struct _prop_val_t {
557     literal_t *name;
558     expression_t *value;
559
560     struct _prop_val_t *next;
561 } prop_val_t;
562
563 typedef struct {
564     expression_t expr;
565     prop_val_t *property_list;
566 } property_value_expression_t;