jscript: Added Number constructor object implementation.
[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
21 typedef struct _parser_ctx_t {
22     LONG ref;
23
24     const WCHAR *ptr;
25     const WCHAR *begin;
26     const WCHAR *end;
27
28     script_ctx_t *script;
29     source_elements_t *source;
30     BOOL nl;
31     HRESULT hres;
32
33     jsheap_t tmp_heap;
34     jsheap_t heap;
35
36     struct _parser_ctx_t *next;
37 } parser_ctx_t;
38
39 HRESULT script_parse(script_ctx_t*,const WCHAR*,parser_ctx_t**);
40 void parser_release(parser_ctx_t*);
41
42 int parser_lex(void*,parser_ctx_t*);
43
44 static inline void parser_addref(parser_ctx_t *ctx)
45 {
46     ctx->ref++;
47 }
48
49 static inline void *parser_alloc(parser_ctx_t *ctx, DWORD size)
50 {
51     return jsheap_alloc(&ctx->heap, size);
52 }
53
54 static inline void *parser_alloc_tmp(parser_ctx_t *ctx, DWORD size)
55 {
56     return jsheap_alloc(&ctx->tmp_heap, size);
57 }
58
59 typedef struct _scope_chain_t {
60     LONG ref;
61     DispatchEx *obj;
62     struct _scope_chain_t *next;
63 } scope_chain_t;
64
65 HRESULT scope_push(scope_chain_t*,DispatchEx*,scope_chain_t**);
66 void scope_release(scope_chain_t*);
67
68 static inline void scope_addref(scope_chain_t *scope)
69 {
70     scope->ref++;
71 }
72
73 struct _exec_ctx_t {
74     LONG ref;
75
76     parser_ctx_t *parser;
77     scope_chain_t *scope_chain;
78     DispatchEx *var_disp;
79 };
80
81 static inline void exec_addref(exec_ctx_t *ctx)
82 {
83     ctx->ref++;
84 }
85
86 void exec_release(exec_ctx_t*);
87 HRESULT create_exec_ctx(DispatchEx*,scope_chain_t*,exec_ctx_t**);
88 HRESULT exec_source(exec_ctx_t*,parser_ctx_t*,source_elements_t*,jsexcept_t*,VARIANT*);
89
90 typedef struct _statement_t statement_t;
91 typedef struct _expression_t expression_t;
92 typedef struct _parameter_t parameter_t;
93
94 HRESULT create_source_function(parser_ctx_t*,parameter_t*,source_elements_t*,scope_chain_t*,DispatchEx**);
95
96 typedef struct {
97     VARTYPE vt;
98     union {
99         LONG lval;
100         double dval;
101         const WCHAR *wstr;
102         VARIANT_BOOL bval;
103         IDispatch *disp;
104     } u;
105 } literal_t;
106
107 typedef struct _variable_declaration_t {
108     const WCHAR *identifier;
109     expression_t *expr;
110
111     struct _variable_declaration_t *next;
112 } variable_declaration_t;
113
114 typedef struct {
115     enum{
116         RT_NORMAL,
117         RT_RETURN,
118         RT_BREAK,
119         RT_CONTINUE
120     } type;
121     jsexcept_t ei;
122 } return_type_t;
123
124 typedef HRESULT (*statement_eval_t)(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
125
126 struct _statement_t {
127     statement_eval_t eval;
128     statement_t *next;
129 };
130
131 typedef struct {
132     statement_t stat;
133     statement_t *stat_list;
134 } block_statement_t;
135
136 typedef struct {
137     statement_t stat;
138     variable_declaration_t *variable_list;
139 } var_statement_t;
140
141 typedef struct {
142     statement_t stat;
143     expression_t *expr;
144 } expression_statement_t;
145
146 typedef struct {
147     statement_t stat;
148     expression_t *expr;
149     statement_t *if_stat;
150     statement_t *else_stat;
151 } if_statement_t;
152
153 typedef struct {
154     statement_t stat;
155     expression_t *expr;
156     statement_t *statement;
157 } while_statement_t;
158
159 typedef struct {
160     statement_t stat;
161     variable_declaration_t *variable_list;
162     expression_t *begin_expr;
163     expression_t *expr;
164     expression_t *end_expr;
165     statement_t *statement;
166 } for_statement_t;
167
168 typedef struct {
169     statement_t stat;
170     variable_declaration_t *variable;
171     expression_t *expr;
172     expression_t *in_expr;
173     statement_t *statement;
174 } forin_statement_t;
175
176 typedef struct {
177     statement_t stat;
178     const WCHAR *identifier;
179 } branch_statement_t;
180
181 typedef struct {
182     statement_t stat;
183     expression_t *expr;
184     statement_t *statement;
185 } with_statement_t;
186
187 typedef struct {
188     statement_t stat;
189     const WCHAR *identifier;
190     statement_t *statement;
191 } labelled_statement_t;
192
193 typedef struct _case_clausule_t {
194     expression_t *expr;
195     statement_t *stat;
196
197     struct _case_clausule_t *next;
198 } case_clausule_t;
199
200 typedef struct {
201     statement_t stat;
202     expression_t *expr;
203     case_clausule_t *case_list;
204 } switch_statement_t;
205
206 typedef struct {
207     const WCHAR *identifier;
208     statement_t *statement;
209 } catch_block_t;
210
211 typedef struct {
212     statement_t stat;
213     statement_t *try_statement;
214     catch_block_t *catch_block;
215     statement_t *finally_statement;
216 } try_statement_t;
217
218 HRESULT block_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
219 HRESULT var_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
220 HRESULT empty_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
221 HRESULT expression_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
222 HRESULT if_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
223 HRESULT dowhile_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
224 HRESULT while_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
225 HRESULT for_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
226 HRESULT forin_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
227 HRESULT continue_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
228 HRESULT break_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
229 HRESULT return_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
230 HRESULT with_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
231 HRESULT labelled_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
232 HRESULT switch_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
233 HRESULT throw_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
234 HRESULT try_statement_eval(exec_ctx_t*,statement_t*,return_type_t*,VARIANT*);
235
236 typedef struct {
237     enum {
238         EXPRVAL_VARIANT,
239         EXPRVAL_IDREF,
240         EXPRVAL_NAMEREF
241     } type;
242     union {
243         VARIANT var;
244         struct {
245             IDispatch *disp;
246             DISPID id;
247         } idref;
248         struct {
249             IDispatch *disp;
250             BSTR name;
251         } nameref;
252     } u;
253 } exprval_t;
254
255 typedef HRESULT (*expression_eval_t)(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
256
257 struct _expression_t {
258     expression_eval_t eval;
259 };
260
261 struct _parameter_t {
262     const WCHAR *identifier;
263
264     struct _parameter_t *next;
265 };
266
267 typedef struct _function_declaration_t {
268     const WCHAR *identifier;
269     parameter_t *parameter_list;
270     source_elements_t *source_elements;
271
272     struct _function_declaration_t *next;
273 } function_declaration_t;
274
275 struct _source_elements_t {
276     statement_t *statement;
277     statement_t *statement_tail;
278     function_declaration_t *functions;
279     function_declaration_t *functions_tail;
280 };
281
282 typedef struct {
283     expression_t expr;
284     const WCHAR *identifier;
285     parameter_t *parameter_list;
286     source_elements_t *source_elements;
287 } function_expression_t;
288
289 typedef struct {
290     expression_t expr;
291     expression_t *expression1;
292     expression_t *expression2;
293 } binary_expression_t;
294
295 typedef struct {
296     expression_t expr;
297     expression_t *expression;
298 } unary_expression_t;
299
300 typedef struct {
301     expression_t expr;
302     expression_t *expression;
303     expression_t *true_expression;
304     expression_t *false_expression;
305 } conditional_expression_t;
306
307 typedef struct {
308     expression_t expr;
309     expression_t *member_expr;
310     expression_t *expression;
311 } array_expression_t;
312
313 typedef struct {
314     expression_t expr;
315     expression_t *expression;
316     const WCHAR *identifier;
317 } member_expression_t;
318
319 typedef struct _argument_t {
320     expression_t *expr;
321
322     struct _argument_t *next;
323 } argument_t;
324
325 typedef struct {
326     expression_t expr;
327     expression_t *expression;
328     argument_t *argument_list;
329 } call_expression_t;
330
331 typedef struct {
332     expression_t expr;
333     const WCHAR *identifier;
334 } identifier_expression_t;
335
336 typedef struct {
337     expression_t expr;
338     literal_t *literal;
339 } literal_expression_t;
340
341 typedef struct _array_element_t {
342     int elision;
343     expression_t *expr;
344
345     struct _array_element_t *next;
346 } array_element_t;
347
348 typedef struct {
349     expression_t expr;
350     array_element_t *element_list;
351     int length;
352 } array_literal_expression_t;
353
354 typedef struct _prop_val_t {
355     literal_t *name;
356     expression_t *value;
357
358     struct _prop_val_t *next;
359 } prop_val_t;
360
361 typedef struct {
362     expression_t expr;
363     prop_val_t *property_list;
364 } property_value_expression_t;
365
366 typedef enum {
367      EXPR_COMMA,
368      EXPR_OR,
369      EXPR_AND,
370      EXPR_BOR,
371      EXPR_BXOR,
372      EXPR_BAND,
373      EXPR_INSTANCEOF,
374      EXPR_IN,
375      EXPR_ADD,
376      EXPR_SUB,
377      EXPR_MUL,
378      EXPR_DIV,
379      EXPR_MOD,
380      EXPR_DELETE,
381      EXPR_VOID,
382      EXPR_TYPEOF,
383      EXPR_MINUS,
384      EXPR_PLUS,
385      EXPR_POSTINC,
386      EXPR_POSTDEC,
387      EXPR_PREINC,
388      EXPR_PREDEC,
389      EXPR_NEW,
390      EXPR_EQ,
391      EXPR_EQEQ,
392      EXPR_NOTEQ,
393      EXPR_NOTEQEQ,
394      EXPR_LESS,
395      EXPR_LESSEQ,
396      EXPR_GREATER,
397      EXPR_GREATEREQ,
398      EXPR_BITNEG,
399      EXPR_LOGNEG,
400      EXPR_LSHIFT,
401      EXPR_RSHIFT,
402      EXPR_RRSHIFT,
403      EXPR_ASSIGN,
404      EXPR_ASSIGNLSHIFT,
405      EXPR_ASSIGNRSHIFT,
406      EXPR_ASSIGNRRSHIFT,
407      EXPR_ASSIGNADD,
408      EXPR_ASSIGNSUB,
409      EXPR_ASSIGNMUL,
410      EXPR_ASSIGNDIV,
411      EXPR_ASSIGNMOD,
412      EXPR_ASSIGNAND,
413      EXPR_ASSIGNOR,
414      EXPR_ASSIGNXOR
415 } expression_type_t;
416
417 HRESULT function_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
418 HRESULT conditional_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
419 HRESULT array_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
420 HRESULT member_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
421 HRESULT member_new_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
422 HRESULT call_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
423 HRESULT this_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
424 HRESULT identifier_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
425 HRESULT literal_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
426 HRESULT array_literal_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
427 HRESULT property_value_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
428
429 HRESULT comma_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
430 HRESULT logical_or_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
431 HRESULT logical_and_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
432 HRESULT binary_or_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
433 HRESULT binary_xor_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
434 HRESULT binary_and_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
435 HRESULT instanceof_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
436 HRESULT in_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
437 HRESULT add_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
438 HRESULT sub_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
439 HRESULT mul_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
440 HRESULT div_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
441 HRESULT mod_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
442 HRESULT delete_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
443 HRESULT void_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
444 HRESULT typeof_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
445 HRESULT minus_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
446 HRESULT plus_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
447 HRESULT post_increment_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
448 HRESULT post_decrement_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
449 HRESULT pre_increment_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
450 HRESULT pre_decrement_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
451 HRESULT new_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
452 HRESULT equal_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
453 HRESULT equal2_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
454 HRESULT not_equal_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
455 HRESULT not_equal2_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
456 HRESULT less_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
457 HRESULT lesseq_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
458 HRESULT greater_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
459 HRESULT greatereq_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
460 HRESULT binary_negation_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
461 HRESULT logical_negation_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
462 HRESULT left_shift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
463 HRESULT right_shift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
464 HRESULT right2_shift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
465 HRESULT assign_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
466 HRESULT assign_lshift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
467 HRESULT assign_rshift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
468 HRESULT assign_rrshift_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
469 HRESULT assign_add_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
470 HRESULT assign_sub_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
471 HRESULT assign_mul_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
472 HRESULT assign_div_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
473 HRESULT assign_mod_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
474 HRESULT assign_and_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
475 HRESULT assign_or_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
476 HRESULT assign_xor_expression_eval(exec_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);