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