jscript: Moved instr_off from statement_t to source_elements_t.
[wine] / dlls / jscript / engine.h
1 /*
2  * Copyright 2008 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 _function_expression_t function_expression_t;
21 typedef struct _expression_t expression_t;
22 typedef struct _statement_t statement_t;
23
24 typedef struct _function_declaration_t {
25     function_expression_t *expr;
26
27     struct _function_declaration_t *next;
28 } function_declaration_t;
29
30 typedef struct _var_list_t {
31     const WCHAR *identifier;
32
33     struct _var_list_t *next;
34 } var_list_t;
35
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;
41
42     struct _func_stack *next;
43 } func_stack_t;
44
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(label,      0, 0,0)                  \
77     X(lshift,     1, 0,0)                  \
78     X(lt,         1, 0,0)                  \
79     X(lteq,       1, 0,0)                  \
80     X(member,     1, ARG_BSTR,   0)        \
81     X(memberid,   1, ARG_UINT,   0)        \
82     X(minus,      1, 0,0)                  \
83     X(mod,        1, 0,0)                  \
84     X(mul,        1, 0,0)                  \
85     X(neg,        1, 0,0)                  \
86     X(neq,        1, 0,0)                  \
87     X(neq2,       1, 0,0)                  \
88     X(new,        1, ARG_INT,    0)        \
89     X(new_obj,    1, 0,0)                  \
90     X(null,       1, 0,0)                  \
91     X(obj_prop,   1, ARG_BSTR,   0)        \
92     X(or,         1, 0,0)                  \
93     X(pop,        1, 0,0)                  \
94     X(pop_except, 1, 0,0)                  \
95     X(pop_scope,  1, 0,0)                  \
96     X(postinc,    1, ARG_INT,    0)        \
97     X(preinc,     1, ARG_INT,    0)        \
98     X(push_except,1, ARG_ADDR,   ARG_BSTR) \
99     X(push_scope, 1, 0,0)                  \
100     X(regexp,     1, ARG_STR,    ARG_INT)  \
101     X(rshift,     1, 0,0)                  \
102     X(rshift2,    1, 0,0)                  \
103     X(str,        1, ARG_STR,    0)        \
104     X(this,       1, 0,0)                  \
105     X(throw,      0, 0,0)                  \
106     X(throw_ref,  0, ARG_UINT,   0)        \
107     X(throw_type, 0, ARG_UINT,   ARG_STR)  \
108     X(tonum,      1, 0,0)                  \
109     X(tree,       1, ARG_STAT,   0)        \
110     X(typeof,     1, 0,0)                  \
111     X(typeofid,   1, 0,0)                  \
112     X(typeofident,1, 0,0)                  \
113     X(refval,     1, 0,0)                  \
114     X(ret,        0, 0,0)                  \
115     X(sub,        1, 0,0)                  \
116     X(undefined,  1, 0,0)                  \
117     X(var_set,    1, ARG_BSTR,   0)        \
118     X(void,       1, 0,0)                  \
119     X(xor,        1, 0,0)
120
121 typedef enum {
122 #define X(x,a,b,c) OP_##x,
123 OP_LIST
124 #undef X
125     OP_LAST
126 } jsop_t;
127
128 typedef union {
129     statement_t *stat;
130     BSTR bstr;
131     double *dbl;
132     LONG lng;
133     WCHAR *str;
134     unsigned uint;
135     function_expression_t *func; /* FIXME */
136 } instr_arg_t;
137
138 typedef enum {
139     ARG_NONE = 0,
140     ARG_ADDR,
141     ARG_BSTR,
142     ARG_DBL,
143     ARG_STAT,
144     ARG_FUNC,
145     ARG_INT,
146     ARG_STR,
147     ARG_UINT
148 } instr_arg_type_t;
149
150 typedef struct {
151     jsop_t op;
152     instr_arg_t arg1;
153     instr_arg_t arg2;
154 } instr_t;
155
156 typedef struct {
157     instr_t *instrs;
158     jsheap_t heap;
159
160     BSTR *bstr_pool;
161     unsigned bstr_pool_size;
162     unsigned bstr_cnt;
163 } bytecode_t;
164
165 void release_bytecode(bytecode_t*);
166
167 typedef struct _compiler_ctx_t compiler_ctx_t;
168
169 void release_compiler(compiler_ctx_t*);
170
171 typedef struct _parser_ctx_t {
172     LONG ref;
173
174     WCHAR *begin;
175     const WCHAR *end;
176     const WCHAR *ptr;
177
178     script_ctx_t *script;
179     source_elements_t *source;
180     BOOL nl;
181     BOOL is_html;
182     BOOL lexer_error;
183     HRESULT hres;
184
185     jsheap_t heap;
186
187     func_stack_t *func_stack;
188
189     bytecode_t *code;
190     compiler_ctx_t *compiler;
191
192     struct _parser_ctx_t *next;
193 } parser_ctx_t;
194
195 HRESULT script_parse(script_ctx_t*,const WCHAR*,const WCHAR*,parser_ctx_t**) DECLSPEC_HIDDEN;
196 void parser_release(parser_ctx_t*) DECLSPEC_HIDDEN;
197
198 int parser_lex(void*,parser_ctx_t*) DECLSPEC_HIDDEN;
199
200 static inline void parser_addref(parser_ctx_t *ctx)
201 {
202     ctx->ref++;
203 }
204
205 static inline void *parser_alloc(parser_ctx_t *ctx, DWORD size)
206 {
207     return jsheap_alloc(&ctx->heap, size);
208 }
209
210 static inline void *parser_alloc_tmp(parser_ctx_t *ctx, DWORD size)
211 {
212     return jsheap_alloc(&ctx->script->tmp_heap, size);
213 }
214
215 typedef struct _scope_chain_t {
216     LONG ref;
217     jsdisp_t *obj;
218     struct _scope_chain_t *next;
219 } scope_chain_t;
220
221 HRESULT scope_push(scope_chain_t*,jsdisp_t*,scope_chain_t**) DECLSPEC_HIDDEN;
222 void scope_release(scope_chain_t*) DECLSPEC_HIDDEN;
223
224 static inline void scope_addref(scope_chain_t *scope)
225 {
226     scope->ref++;
227 }
228
229 typedef struct _return_type_t return_type_t;
230 typedef struct _except_frame_t except_frame_t;
231
232 struct _exec_ctx_t {
233     LONG ref;
234
235     parser_ctx_t *parser;
236     scope_chain_t *scope_chain;
237     jsdisp_t *var_disp;
238     IDispatch *this_obj;
239     BOOL is_global;
240
241     VARIANT *stack;
242     unsigned stack_size;
243     unsigned top;
244     except_frame_t *except_frame;
245
246     unsigned ip;
247     jsexcept_t *ei;
248     return_type_t *rt; /* FIXME */
249 };
250
251 static inline void exec_addref(exec_ctx_t *ctx)
252 {
253     ctx->ref++;
254 }
255
256 void exec_release(exec_ctx_t*) DECLSPEC_HIDDEN;
257 HRESULT create_exec_ctx(script_ctx_t*,IDispatch*,jsdisp_t*,scope_chain_t*,BOOL,exec_ctx_t**) DECLSPEC_HIDDEN;
258 HRESULT exec_source(exec_ctx_t*,parser_ctx_t*,source_elements_t*,BOOL,jsexcept_t*,VARIANT*) DECLSPEC_HIDDEN;
259
260 typedef struct _parameter_t parameter_t;
261
262 HRESULT create_source_function(parser_ctx_t*,parameter_t*,source_elements_t*,scope_chain_t*,
263         const WCHAR*,DWORD,jsdisp_t**) DECLSPEC_HIDDEN;
264
265 typedef enum {
266     LT_INT,
267     LT_DOUBLE,
268     LT_STRING,
269     LT_BOOL,
270     LT_NULL,
271     LT_REGEXP
272 }literal_type_t;
273
274 typedef struct {
275     literal_type_t type;
276     union {
277         LONG lval;
278         double dval;
279         const WCHAR *wstr;
280         VARIANT_BOOL bval;
281         struct {
282             const WCHAR *str;
283             DWORD str_len;
284             DWORD flags;
285         } regexp;
286     } u;
287 } literal_t;
288
289 literal_t *parse_regexp(parser_ctx_t*) DECLSPEC_HIDDEN;
290 literal_t *new_boolean_literal(parser_ctx_t*,VARIANT_BOOL) DECLSPEC_HIDDEN;
291
292 typedef struct _variable_declaration_t {
293     const WCHAR *identifier;
294     expression_t *expr;
295
296     struct _variable_declaration_t *next;
297 } variable_declaration_t;
298
299 typedef HRESULT (*statement_eval_t)(script_ctx_t*,statement_t*,return_type_t*,VARIANT*);
300
301 typedef enum {
302     STAT_BLOCK,
303     STAT_BREAK,
304     STAT_CONTINUE,
305     STAT_EMPTY,
306     STAT_EXPR,
307     STAT_FOR,
308     STAT_FORIN,
309     STAT_IF,
310     STAT_LABEL,
311     STAT_RETURN,
312     STAT_SWITCH,
313     STAT_THROW,
314     STAT_TRY,
315     STAT_VAR,
316     STAT_WHILE,
317     STAT_WITH
318 } statement_type_t;
319
320 struct _statement_t {
321     statement_type_t type;
322     statement_eval_t eval;
323     statement_t *next;
324 };
325
326 typedef struct {
327     statement_t stat;
328     statement_t *stat_list;
329 } block_statement_t;
330
331 typedef struct {
332     statement_t stat;
333     variable_declaration_t *variable_list;
334 } var_statement_t;
335
336 typedef struct {
337     statement_t stat;
338     expression_t *expr;
339 } expression_statement_t;
340
341 typedef struct {
342     statement_t stat;
343     expression_t *expr;
344     statement_t *if_stat;
345     statement_t *else_stat;
346 } if_statement_t;
347
348 typedef struct {
349     statement_t stat;
350     BOOL do_while;
351     expression_t *expr;
352     statement_t *statement;
353 } while_statement_t;
354
355 typedef struct {
356     statement_t stat;
357     variable_declaration_t *variable_list;
358     expression_t *begin_expr;
359     expression_t *expr;
360     expression_t *end_expr;
361     statement_t *statement;
362 } for_statement_t;
363
364 typedef struct {
365     statement_t stat;
366     variable_declaration_t *variable;
367     expression_t *expr;
368     expression_t *in_expr;
369     statement_t *statement;
370 } forin_statement_t;
371
372 typedef struct {
373     statement_t stat;
374     const WCHAR *identifier;
375 } branch_statement_t;
376
377 typedef struct {
378     statement_t stat;
379     expression_t *expr;
380     statement_t *statement;
381 } with_statement_t;
382
383 typedef struct {
384     statement_t stat;
385     const WCHAR *identifier;
386     statement_t *statement;
387 } labelled_statement_t;
388
389 typedef struct _case_clausule_t {
390     expression_t *expr;
391     statement_t *stat;
392
393     struct _case_clausule_t *next;
394 } case_clausule_t;
395
396 typedef struct {
397     statement_t stat;
398     expression_t *expr;
399     case_clausule_t *case_list;
400 } switch_statement_t;
401
402 typedef struct {
403     const WCHAR *identifier;
404     statement_t *statement;
405 } catch_block_t;
406
407 typedef struct {
408     statement_t stat;
409     statement_t *try_statement;
410     catch_block_t *catch_block;
411     statement_t *finally_statement;
412 } try_statement_t;
413
414 HRESULT continue_statement_eval(script_ctx_t*,statement_t*,return_type_t*,VARIANT*) DECLSPEC_HIDDEN;
415 HRESULT break_statement_eval(script_ctx_t*,statement_t*,return_type_t*,VARIANT*) DECLSPEC_HIDDEN;
416 HRESULT return_statement_eval(script_ctx_t*,statement_t*,return_type_t*,VARIANT*) DECLSPEC_HIDDEN;
417
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;
432
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;
493
494 struct _expression_t {
495     expression_type_t type;
496     unsigned instr_off;
497 };
498
499 struct _parameter_t {
500     const WCHAR *identifier;
501
502     struct _parameter_t *next;
503 };
504
505 struct _source_elements_t {
506     statement_t *statement;
507     statement_t *statement_tail;
508     function_declaration_t *functions;
509     var_list_t *variables;
510     unsigned instr_off;
511 };
512
513 struct _function_expression_t {
514     expression_t expr;
515     const WCHAR *identifier;
516     parameter_t *parameter_list;
517     source_elements_t *source_elements;
518     const WCHAR *src_str;
519     DWORD src_len;
520 };
521
522 typedef struct {
523     expression_t expr;
524     expression_t *expression1;
525     expression_t *expression2;
526 } binary_expression_t;
527
528 typedef struct {
529     expression_t expr;
530     expression_t *expression;
531 } unary_expression_t;
532
533 typedef struct {
534     expression_t expr;
535     expression_t *expression;
536     expression_t *true_expression;
537     expression_t *false_expression;
538 } conditional_expression_t;
539
540 typedef struct {
541     expression_t expr;
542     expression_t *expression;
543     const WCHAR *identifier;
544 } member_expression_t;
545
546 typedef struct _argument_t {
547     expression_t *expr;
548
549     struct _argument_t *next;
550 } argument_t;
551
552 typedef struct {
553     expression_t expr;
554     expression_t *expression;
555     argument_t *argument_list;
556 } call_expression_t;
557
558 typedef struct {
559     expression_t expr;
560     const WCHAR *identifier;
561 } identifier_expression_t;
562
563 typedef struct {
564     expression_t expr;
565     literal_t *literal;
566 } literal_expression_t;
567
568 typedef struct _array_element_t {
569     int elision;
570     expression_t *expr;
571
572     struct _array_element_t *next;
573 } array_element_t;
574
575 typedef struct {
576     expression_t expr;
577     array_element_t *element_list;
578     int length;
579 } array_literal_expression_t;
580
581 typedef struct _prop_val_t {
582     literal_t *name;
583     expression_t *value;
584
585     struct _prop_val_t *next;
586 } prop_val_t;
587
588 typedef struct {
589     expression_t expr;
590     prop_val_t *property_list;
591 } property_value_expression_t;
592
593 HRESULT compile_subscript(parser_ctx_t*,expression_t*,unsigned*) DECLSPEC_HIDDEN;
594 HRESULT compile_subscript_stat(parser_ctx_t*,statement_t*,unsigned*) DECLSPEC_HIDDEN;