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