jscript: Store source code range in function_code_t.
[wine/multimedia.git] / dlls / jscript / engine.h
bloba2be6ee62ce6a412dcc9dc6b279c6f22b2bf1637
1 /*
2 * Copyright 2008,2011 Jacek Caban for CodeWeavers
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.
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.
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
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;
24 typedef struct _function_declaration_t {
25 function_expression_t *expr;
27 struct _function_declaration_t *next;
28 } function_declaration_t;
30 typedef struct _var_list_t {
31 const WCHAR *identifier;
33 struct _var_list_t *next;
34 } var_list_t;
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;
42 struct _func_stack *next;
43 } func_stack_t;
45 typedef struct {
46 const WCHAR *begin;
47 const WCHAR *end;
48 const WCHAR *ptr;
50 script_ctx_t *script;
51 source_elements_t *source;
52 BOOL nl;
53 BOOL is_html;
54 BOOL lexer_error;
55 HRESULT hres;
57 jsheap_t heap;
59 func_stack_t *func_stack;
60 } parser_ctx_t;
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_UINT, 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)
137 typedef enum {
138 #define X(x,a,b,c) OP_##x,
139 OP_LIST
140 #undef X
141 OP_LAST
142 } jsop_t;
144 typedef union {
145 BSTR bstr;
146 double *dbl;
147 LONG lng;
148 WCHAR *str;
149 unsigned uint;
150 } instr_arg_t;
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;
163 typedef struct {
164 jsop_t op;
165 instr_arg_t arg1;
166 instr_arg_t arg2;
167 } instr_t;
169 typedef struct _function_code_t {
170 BSTR name;
171 unsigned instr_off;
173 function_expression_t *expr; /* FIXME */
174 source_elements_t *source_elements; /* FIXME */
176 const WCHAR *source;
177 unsigned source_len;
179 unsigned func_cnt;
180 struct _function_code_t *funcs;
181 } function_code_t;
183 typedef struct _bytecode_t {
184 LONG ref;
186 instr_t *instrs;
187 jsheap_t heap;
189 function_code_t global_code;
191 WCHAR *source;
193 BSTR *bstr_pool;
194 unsigned bstr_pool_size;
195 unsigned bstr_cnt;
197 parser_ctx_t *parser;
199 struct _bytecode_t *next;
200 } bytecode_t;
202 HRESULT compile_script(script_ctx_t*,const WCHAR*,const WCHAR*,BOOL,BOOL,bytecode_t**) DECLSPEC_HIDDEN;
203 void release_bytecode(bytecode_t*) DECLSPEC_HIDDEN;
205 static inline void bytecode_addref(bytecode_t *code)
207 code->ref++;
210 HRESULT script_parse(script_ctx_t*,const WCHAR*,const WCHAR*,BOOL,parser_ctx_t**) DECLSPEC_HIDDEN;
211 void parser_release(parser_ctx_t*) DECLSPEC_HIDDEN;
213 int parser_lex(void*,parser_ctx_t*) DECLSPEC_HIDDEN;
215 static inline void *parser_alloc(parser_ctx_t *ctx, DWORD size)
217 return jsheap_alloc(&ctx->heap, size);
220 static inline void *parser_alloc_tmp(parser_ctx_t *ctx, DWORD size)
222 return jsheap_alloc(&ctx->script->tmp_heap, size);
225 typedef struct _scope_chain_t {
226 LONG ref;
227 jsdisp_t *obj;
228 struct _scope_chain_t *next;
229 } scope_chain_t;
231 HRESULT scope_push(scope_chain_t*,jsdisp_t*,scope_chain_t**) DECLSPEC_HIDDEN;
232 void scope_release(scope_chain_t*) DECLSPEC_HIDDEN;
234 static inline void scope_addref(scope_chain_t *scope)
236 scope->ref++;
239 typedef struct _except_frame_t except_frame_t;
241 struct _exec_ctx_t {
242 LONG ref;
244 parser_ctx_t *parser;
245 bytecode_t *code;
246 script_ctx_t *script;
247 scope_chain_t *scope_chain;
248 jsdisp_t *var_disp;
249 IDispatch *this_obj;
250 function_code_t *func_code;
251 BOOL is_global;
253 VARIANT *stack;
254 unsigned stack_size;
255 unsigned top;
256 except_frame_t *except_frame;
258 unsigned ip;
259 jsexcept_t *ei;
262 static inline void exec_addref(exec_ctx_t *ctx)
264 ctx->ref++;
267 void exec_release(exec_ctx_t*) DECLSPEC_HIDDEN;
268 HRESULT create_exec_ctx(script_ctx_t*,IDispatch*,jsdisp_t*,scope_chain_t*,BOOL,exec_ctx_t**) DECLSPEC_HIDDEN;
269 HRESULT exec_source(exec_ctx_t*,bytecode_t*,function_code_t*,BOOL,jsexcept_t*,VARIANT*) DECLSPEC_HIDDEN;
271 typedef struct _parameter_t parameter_t;
273 HRESULT create_source_function(script_ctx_t*,bytecode_t*,parameter_t*,function_code_t*,scope_chain_t*,
274 jsdisp_t**) DECLSPEC_HIDDEN;
276 typedef enum {
277 LT_INT,
278 LT_DOUBLE,
279 LT_STRING,
280 LT_BOOL,
281 LT_NULL,
282 LT_REGEXP
283 }literal_type_t;
285 typedef struct {
286 literal_type_t type;
287 union {
288 LONG lval;
289 double dval;
290 const WCHAR *wstr;
291 VARIANT_BOOL bval;
292 struct {
293 const WCHAR *str;
294 DWORD str_len;
295 DWORD flags;
296 } regexp;
297 } u;
298 } literal_t;
300 literal_t *parse_regexp(parser_ctx_t*) DECLSPEC_HIDDEN;
301 literal_t *new_boolean_literal(parser_ctx_t*,VARIANT_BOOL) DECLSPEC_HIDDEN;
303 typedef struct _variable_declaration_t {
304 const WCHAR *identifier;
305 expression_t *expr;
307 struct _variable_declaration_t *next;
308 } variable_declaration_t;
310 typedef enum {
311 STAT_BLOCK,
312 STAT_BREAK,
313 STAT_CONTINUE,
314 STAT_EMPTY,
315 STAT_EXPR,
316 STAT_FOR,
317 STAT_FORIN,
318 STAT_IF,
319 STAT_LABEL,
320 STAT_RETURN,
321 STAT_SWITCH,
322 STAT_THROW,
323 STAT_TRY,
324 STAT_VAR,
325 STAT_WHILE,
326 STAT_WITH
327 } statement_type_t;
329 struct _statement_t {
330 statement_type_t type;
331 statement_t *next;
334 typedef struct {
335 statement_t stat;
336 statement_t *stat_list;
337 } block_statement_t;
339 typedef struct {
340 statement_t stat;
341 variable_declaration_t *variable_list;
342 } var_statement_t;
344 typedef struct {
345 statement_t stat;
346 expression_t *expr;
347 } expression_statement_t;
349 typedef struct {
350 statement_t stat;
351 expression_t *expr;
352 statement_t *if_stat;
353 statement_t *else_stat;
354 } if_statement_t;
356 typedef struct {
357 statement_t stat;
358 BOOL do_while;
359 expression_t *expr;
360 statement_t *statement;
361 } while_statement_t;
363 typedef struct {
364 statement_t stat;
365 variable_declaration_t *variable_list;
366 expression_t *begin_expr;
367 expression_t *expr;
368 expression_t *end_expr;
369 statement_t *statement;
370 } for_statement_t;
372 typedef struct {
373 statement_t stat;
374 variable_declaration_t *variable;
375 expression_t *expr;
376 expression_t *in_expr;
377 statement_t *statement;
378 } forin_statement_t;
380 typedef struct {
381 statement_t stat;
382 const WCHAR *identifier;
383 } branch_statement_t;
385 typedef struct {
386 statement_t stat;
387 expression_t *expr;
388 statement_t *statement;
389 } with_statement_t;
391 typedef struct {
392 statement_t stat;
393 const WCHAR *identifier;
394 statement_t *statement;
395 } labelled_statement_t;
397 typedef struct _case_clausule_t {
398 expression_t *expr;
399 statement_t *stat;
401 struct _case_clausule_t *next;
402 } case_clausule_t;
404 typedef struct {
405 statement_t stat;
406 expression_t *expr;
407 case_clausule_t *case_list;
408 } switch_statement_t;
410 typedef struct {
411 const WCHAR *identifier;
412 statement_t *statement;
413 } catch_block_t;
415 typedef struct {
416 statement_t stat;
417 statement_t *try_statement;
418 catch_block_t *catch_block;
419 statement_t *finally_statement;
420 } try_statement_t;
422 typedef struct {
423 enum {
424 EXPRVAL_VARIANT,
425 EXPRVAL_IDREF,
426 EXPRVAL_INVALID
427 } type;
428 union {
429 VARIANT var;
430 struct {
431 IDispatch *disp;
432 DISPID id;
433 } idref;
434 } u;
435 } exprval_t;
437 typedef enum {
438 EXPR_COMMA,
439 EXPR_OR,
440 EXPR_AND,
441 EXPR_BOR,
442 EXPR_BXOR,
443 EXPR_BAND,
444 EXPR_INSTANCEOF,
445 EXPR_IN,
446 EXPR_ADD,
447 EXPR_SUB,
448 EXPR_MUL,
449 EXPR_DIV,
450 EXPR_MOD,
451 EXPR_DELETE,
452 EXPR_VOID,
453 EXPR_TYPEOF,
454 EXPR_MINUS,
455 EXPR_PLUS,
456 EXPR_POSTINC,
457 EXPR_POSTDEC,
458 EXPR_PREINC,
459 EXPR_PREDEC,
460 EXPR_EQ,
461 EXPR_EQEQ,
462 EXPR_NOTEQ,
463 EXPR_NOTEQEQ,
464 EXPR_LESS,
465 EXPR_LESSEQ,
466 EXPR_GREATER,
467 EXPR_GREATEREQ,
468 EXPR_BITNEG,
469 EXPR_LOGNEG,
470 EXPR_LSHIFT,
471 EXPR_RSHIFT,
472 EXPR_RRSHIFT,
473 EXPR_ASSIGN,
474 EXPR_ASSIGNLSHIFT,
475 EXPR_ASSIGNRSHIFT,
476 EXPR_ASSIGNRRSHIFT,
477 EXPR_ASSIGNADD,
478 EXPR_ASSIGNSUB,
479 EXPR_ASSIGNMUL,
480 EXPR_ASSIGNDIV,
481 EXPR_ASSIGNMOD,
482 EXPR_ASSIGNAND,
483 EXPR_ASSIGNOR,
484 EXPR_ASSIGNXOR,
485 EXPR_COND,
486 EXPR_ARRAY,
487 EXPR_MEMBER,
488 EXPR_NEW,
489 EXPR_CALL,
490 EXPR_THIS,
491 EXPR_FUNC,
492 EXPR_IDENT,
493 EXPR_ARRAYLIT,
494 EXPR_PROPVAL,
495 EXPR_LITERAL
496 } expression_type_t;
498 struct _expression_t {
499 expression_type_t type;
502 struct _parameter_t {
503 const WCHAR *identifier;
505 struct _parameter_t *next;
508 struct _source_elements_t {
509 statement_t *statement;
510 statement_t *statement_tail;
511 function_declaration_t *functions;
512 var_list_t *variables;
515 struct _function_expression_t {
516 expression_t expr;
517 const WCHAR *identifier;
518 parameter_t *parameter_list;
519 source_elements_t *source_elements;
520 const WCHAR *src_str;
521 DWORD src_len;
524 typedef struct {
525 expression_t expr;
526 expression_t *expression1;
527 expression_t *expression2;
528 } binary_expression_t;
530 typedef struct {
531 expression_t expr;
532 expression_t *expression;
533 } unary_expression_t;
535 typedef struct {
536 expression_t expr;
537 expression_t *expression;
538 expression_t *true_expression;
539 expression_t *false_expression;
540 } conditional_expression_t;
542 typedef struct {
543 expression_t expr;
544 expression_t *expression;
545 const WCHAR *identifier;
546 } member_expression_t;
548 typedef struct _argument_t {
549 expression_t *expr;
551 struct _argument_t *next;
552 } argument_t;
554 typedef struct {
555 expression_t expr;
556 expression_t *expression;
557 argument_t *argument_list;
558 } call_expression_t;
560 typedef struct {
561 expression_t expr;
562 const WCHAR *identifier;
563 } identifier_expression_t;
565 typedef struct {
566 expression_t expr;
567 literal_t *literal;
568 } literal_expression_t;
570 typedef struct _array_element_t {
571 int elision;
572 expression_t *expr;
574 struct _array_element_t *next;
575 } array_element_t;
577 typedef struct {
578 expression_t expr;
579 array_element_t *element_list;
580 int length;
581 } array_literal_expression_t;
583 typedef struct _prop_val_t {
584 literal_t *name;
585 expression_t *value;
587 struct _prop_val_t *next;
588 } prop_val_t;
590 typedef struct {
591 expression_t expr;
592 prop_val_t *property_list;
593 } property_value_expression_t;