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