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