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