wininet: Store request object instead of handle in error dialog.
[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 is_html;
32     BOOL lexer_error;
33     HRESULT hres;
34
35     jsheap_t heap;
36 } parser_ctx_t;
37
38 #define OP_LIST                            \
39     X(add,        1, 0,0)                  \
40     X(and,        1, 0,0)                  \
41     X(array,      1, 0,0)                  \
42     X(assign,     1, 0,0)                  \
43     X(assign_call,1, ARG_UINT,   0)       \
44     X(bool,       1, ARG_INT,    0)        \
45     X(bneg,       1, 0,0)                  \
46     X(call,       1, ARG_UINT,   ARG_UINT) \
47     X(call_member,1, ARG_UINT,   ARG_UINT) \
48     X(carray,     1, ARG_UINT,   0)        \
49     X(case,       0, ARG_ADDR,   0)        \
50     X(cnd_nz,     0, ARG_ADDR,   0)        \
51     X(cnd_z,      0, ARG_ADDR,   0)        \
52     X(delete,     1, 0,0)                  \
53     X(delete_ident,1,ARG_BSTR,   0)        \
54     X(div,        1, 0,0)                  \
55     X(double,     1, ARG_DBL,    0)        \
56     X(end_finally,1, 0,0)                  \
57     X(eq,         1, 0,0)                  \
58     X(eq2,        1, 0,0)                  \
59     X(forin,      0, ARG_ADDR,   0)        \
60     X(func,       1, ARG_UINT,   0)        \
61     X(gt,         1, 0,0)                  \
62     X(gteq,       1, 0,0)                  \
63     X(ident,      1, ARG_BSTR,   0)        \
64     X(identid,    1, ARG_BSTR,   ARG_INT)  \
65     X(in,         1, 0,0)                  \
66     X(instanceof, 1, 0,0)                  \
67     X(int,        1, ARG_INT,    0)        \
68     X(jmp,        0, ARG_ADDR,   0)        \
69     X(jmp_z,      0, ARG_ADDR,   0)        \
70     X(lshift,     1, 0,0)                  \
71     X(lt,         1, 0,0)                  \
72     X(lteq,       1, 0,0)                  \
73     X(member,     1, ARG_BSTR,   0)        \
74     X(memberid,   1, ARG_UINT,   0)        \
75     X(minus,      1, 0,0)                  \
76     X(mod,        1, 0,0)                  \
77     X(mul,        1, 0,0)                  \
78     X(neg,        1, 0,0)                  \
79     X(neq,        1, 0,0)                  \
80     X(neq2,       1, 0,0)                  \
81     X(new,        1, ARG_UINT,   0)        \
82     X(new_obj,    1, 0,0)                  \
83     X(null,       1, 0,0)                  \
84     X(obj_prop,   1, ARG_BSTR,   0)        \
85     X(or,         1, 0,0)                  \
86     X(pop,        1, 0,0)                  \
87     X(pop_except, 1, 0,0)                  \
88     X(pop_scope,  1, 0,0)                  \
89     X(postinc,    1, ARG_INT,    0)        \
90     X(preinc,     1, ARG_INT,    0)        \
91     X(push_except,1, ARG_ADDR,   ARG_BSTR) \
92     X(push_scope, 1, 0,0)                  \
93     X(regexp,     1, ARG_STR,    ARG_UINT) \
94     X(rshift,     1, 0,0)                  \
95     X(rshift2,    1, 0,0)                  \
96     X(str,        1, ARG_STR,    0)        \
97     X(this,       1, 0,0)                  \
98     X(throw,      0, 0,0)                  \
99     X(throw_ref,  0, ARG_UINT,   0)        \
100     X(throw_type, 0, ARG_UINT,   ARG_STR)  \
101     X(tonum,      1, 0,0)                  \
102     X(typeof,     1, 0,0)                  \
103     X(typeofid,   1, 0,0)                  \
104     X(typeofident,1, 0,0)                  \
105     X(refval,     1, 0,0)                  \
106     X(ret,        0, 0,0)                  \
107     X(sub,        1, 0,0)                  \
108     X(undefined,  1, 0,0)                  \
109     X(var_set,    1, ARG_BSTR,   0)        \
110     X(void,       1, 0,0)                  \
111     X(xor,        1, 0,0)
112
113 typedef enum {
114 #define X(x,a,b,c) OP_##x,
115 OP_LIST
116 #undef X
117     OP_LAST
118 } jsop_t;
119
120 typedef union {
121     BSTR bstr;
122     LONG lng;
123     WCHAR *str;
124     unsigned uint;
125 } instr_arg_t;
126
127 typedef enum {
128     ARG_NONE = 0,
129     ARG_ADDR,
130     ARG_BSTR,
131     ARG_DBL,
132     ARG_FUNC,
133     ARG_INT,
134     ARG_STR,
135     ARG_UINT
136 } instr_arg_type_t;
137
138 typedef struct {
139     jsop_t op;
140     union {
141         instr_arg_t arg[2];
142         double dbl;
143     } u;
144 } instr_t;
145
146 typedef struct _function_code_t {
147     BSTR name;
148     unsigned instr_off;
149
150     const WCHAR *source;
151     unsigned source_len;
152
153     unsigned func_cnt;
154     struct _function_code_t *funcs;
155
156     unsigned var_cnt;
157     BSTR *variables;
158
159     unsigned param_cnt;
160     BSTR *params;
161 } function_code_t;
162
163 typedef struct _bytecode_t {
164     LONG ref;
165
166     instr_t *instrs;
167     jsheap_t heap;
168
169     function_code_t global_code;
170
171     WCHAR *source;
172
173     BSTR *bstr_pool;
174     unsigned bstr_pool_size;
175     unsigned bstr_cnt;
176
177     struct _bytecode_t *next;
178 } bytecode_t;
179
180 HRESULT compile_script(script_ctx_t*,const WCHAR*,const WCHAR*,BOOL,BOOL,bytecode_t**) DECLSPEC_HIDDEN;
181 void release_bytecode(bytecode_t*) DECLSPEC_HIDDEN;
182
183 static inline void bytecode_addref(bytecode_t *code)
184 {
185     code->ref++;
186 }
187
188 HRESULT script_parse(script_ctx_t*,const WCHAR*,const WCHAR*,BOOL,parser_ctx_t**) DECLSPEC_HIDDEN;
189 void parser_release(parser_ctx_t*) DECLSPEC_HIDDEN;
190
191 int parser_lex(void*,parser_ctx_t*) DECLSPEC_HIDDEN;
192
193 static inline void *parser_alloc(parser_ctx_t *ctx, DWORD size)
194 {
195     return jsheap_alloc(&ctx->heap, size);
196 }
197
198 static inline void *parser_alloc_tmp(parser_ctx_t *ctx, DWORD size)
199 {
200     return jsheap_alloc(&ctx->script->tmp_heap, size);
201 }
202
203 typedef struct _scope_chain_t {
204     LONG ref;
205     jsdisp_t *obj;
206     struct _scope_chain_t *next;
207 } scope_chain_t;
208
209 HRESULT scope_push(scope_chain_t*,jsdisp_t*,scope_chain_t**) DECLSPEC_HIDDEN;
210 void scope_release(scope_chain_t*) DECLSPEC_HIDDEN;
211
212 static inline void scope_addref(scope_chain_t *scope)
213 {
214     scope->ref++;
215 }
216
217 typedef struct _except_frame_t except_frame_t;
218
219 struct _exec_ctx_t {
220     LONG ref;
221
222     parser_ctx_t *parser;
223     bytecode_t *code;
224     script_ctx_t *script;
225     scope_chain_t *scope_chain;
226     jsdisp_t *var_disp;
227     IDispatch *this_obj;
228     function_code_t *func_code;
229     BOOL is_global;
230
231     VARIANT *stack;
232     unsigned stack_size;
233     unsigned top;
234     except_frame_t *except_frame;
235
236     unsigned ip;
237     jsexcept_t *ei;
238 };
239
240 static inline void exec_addref(exec_ctx_t *ctx)
241 {
242     ctx->ref++;
243 }
244
245 void exec_release(exec_ctx_t*) DECLSPEC_HIDDEN;
246 HRESULT create_exec_ctx(script_ctx_t*,IDispatch*,jsdisp_t*,scope_chain_t*,BOOL,exec_ctx_t**) DECLSPEC_HIDDEN;
247 HRESULT exec_source(exec_ctx_t*,bytecode_t*,function_code_t*,BOOL,jsexcept_t*,VARIANT*) DECLSPEC_HIDDEN;
248 HRESULT create_source_function(script_ctx_t*,bytecode_t*,function_code_t*,scope_chain_t*,jsdisp_t**) DECLSPEC_HIDDEN;
249
250 typedef enum {
251     LT_INT,
252     LT_DOUBLE,
253     LT_STRING,
254     LT_BOOL,
255     LT_NULL,
256     LT_REGEXP
257 }literal_type_t;
258
259 typedef struct {
260     literal_type_t type;
261     union {
262         LONG lval;
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_VARIANT,
400         EXPRVAL_IDREF,
401         EXPRVAL_INVALID
402     } type;
403     union {
404         VARIANT var;
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;