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