2 * Copyright 2008 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;
23 typedef struct _function_declaration_t {
24 function_expression_t *expr;
26 struct _function_declaration_t *next;
27 } function_declaration_t;
29 typedef struct _var_list_t {
30 const WCHAR *identifier;
32 struct _var_list_t *next;
35 typedef struct _func_stack {
36 function_declaration_t *func_head;
37 function_declaration_t *func_tail;
41 struct _func_stack *next;
46 X(bool, 1, ARG_INT, 0) \
49 X(double, 1, ARG_SBL, 0) \
52 X(ident, 1, ARG_BSTR, 0) \
54 X(int, 1, ARG_INT, 0) \
55 X(jmp, 0, ARG_ADDR, 0) \
56 X(jmp_nz, 0, ARG_ADDR, 0) \
57 X(jmp_z, 0, ARG_ADDR, 0) \
64 X(new, 1, ARG_INT, 0) \
68 X(regexp, 1, ARG_STR, ARG_INT) \
69 X(str, 1, ARG_STR, 0) \
72 X(tree, 1, ARG_EXPR, 0) \
79 #define X(x,a,b,c) OP_##x,
114 unsigned bstr_pool_size;
118 void release_bytecode(bytecode_t*);
120 typedef struct _compiler_ctx_t compiler_ctx_t;
122 void release_compiler(compiler_ctx_t*);
124 typedef struct _parser_ctx_t {
131 script_ctx_t *script;
132 source_elements_t *source;
140 func_stack_t *func_stack;
143 compiler_ctx_t *compiler;
145 struct _parser_ctx_t *next;
148 HRESULT script_parse(script_ctx_t*,const WCHAR*,const WCHAR*,parser_ctx_t**) DECLSPEC_HIDDEN;
149 void parser_release(parser_ctx_t*) DECLSPEC_HIDDEN;
151 int parser_lex(void*,parser_ctx_t*) DECLSPEC_HIDDEN;
153 static inline void parser_addref(parser_ctx_t *ctx)
158 static inline void *parser_alloc(parser_ctx_t *ctx, DWORD size)
160 return jsheap_alloc(&ctx->heap, size);
163 static inline void *parser_alloc_tmp(parser_ctx_t *ctx, DWORD size)
165 return jsheap_alloc(&ctx->script->tmp_heap, size);
168 typedef struct _scope_chain_t {
171 struct _scope_chain_t *next;
174 HRESULT scope_push(scope_chain_t*,jsdisp_t*,scope_chain_t**) DECLSPEC_HIDDEN;
175 void scope_release(scope_chain_t*) DECLSPEC_HIDDEN;
177 static inline void scope_addref(scope_chain_t *scope)
185 parser_ctx_t *parser;
186 scope_chain_t *scope_chain;
199 static inline void exec_addref(exec_ctx_t *ctx)
204 void exec_release(exec_ctx_t*) DECLSPEC_HIDDEN;
205 HRESULT create_exec_ctx(script_ctx_t*,IDispatch*,jsdisp_t*,scope_chain_t*,BOOL,exec_ctx_t**) DECLSPEC_HIDDEN;
206 HRESULT exec_source(exec_ctx_t*,parser_ctx_t*,source_elements_t*,BOOL,jsexcept_t*,VARIANT*) DECLSPEC_HIDDEN;
208 typedef struct _statement_t statement_t;
209 typedef struct _parameter_t parameter_t;
211 HRESULT create_source_function(parser_ctx_t*,parameter_t*,source_elements_t*,scope_chain_t*,
212 const WCHAR*,DWORD,jsdisp_t**) DECLSPEC_HIDDEN;
238 literal_t *parse_regexp(parser_ctx_t*) DECLSPEC_HIDDEN;
239 literal_t *new_boolean_literal(parser_ctx_t*,VARIANT_BOOL) DECLSPEC_HIDDEN;
241 typedef struct _variable_declaration_t {
242 const WCHAR *identifier;
245 struct _variable_declaration_t *next;
246 } variable_declaration_t;
248 typedef struct _return_type_t return_type_t;
250 typedef HRESULT (*statement_eval_t)(script_ctx_t*,statement_t*,return_type_t*,VARIANT*);
252 struct _statement_t {
253 statement_eval_t eval;
259 statement_t *stat_list;
264 variable_declaration_t *variable_list;
270 } expression_statement_t;
275 statement_t *if_stat;
276 statement_t *else_stat;
283 statement_t *statement;
288 variable_declaration_t *variable_list;
289 expression_t *begin_expr;
291 expression_t *end_expr;
292 statement_t *statement;
297 variable_declaration_t *variable;
299 expression_t *in_expr;
300 statement_t *statement;
305 const WCHAR *identifier;
306 } branch_statement_t;
311 statement_t *statement;
316 const WCHAR *identifier;
317 statement_t *statement;
318 } labelled_statement_t;
320 typedef struct _case_clausule_t {
324 struct _case_clausule_t *next;
330 case_clausule_t *case_list;
331 } switch_statement_t;
334 const WCHAR *identifier;
335 statement_t *statement;
340 statement_t *try_statement;
341 catch_block_t *catch_block;
342 statement_t *finally_statement;
345 HRESULT block_statement_eval(script_ctx_t*,statement_t*,return_type_t*,VARIANT*) DECLSPEC_HIDDEN;
346 HRESULT var_statement_eval(script_ctx_t*,statement_t*,return_type_t*,VARIANT*) DECLSPEC_HIDDEN;
347 HRESULT empty_statement_eval(script_ctx_t*,statement_t*,return_type_t*,VARIANT*) DECLSPEC_HIDDEN;
348 HRESULT expression_statement_eval(script_ctx_t*,statement_t*,return_type_t*,VARIANT*) DECLSPEC_HIDDEN;
349 HRESULT if_statement_eval(script_ctx_t*,statement_t*,return_type_t*,VARIANT*) DECLSPEC_HIDDEN;
350 HRESULT while_statement_eval(script_ctx_t*,statement_t*,return_type_t*,VARIANT*) DECLSPEC_HIDDEN;
351 HRESULT for_statement_eval(script_ctx_t*,statement_t*,return_type_t*,VARIANT*) DECLSPEC_HIDDEN;
352 HRESULT forin_statement_eval(script_ctx_t*,statement_t*,return_type_t*,VARIANT*) DECLSPEC_HIDDEN;
353 HRESULT continue_statement_eval(script_ctx_t*,statement_t*,return_type_t*,VARIANT*) DECLSPEC_HIDDEN;
354 HRESULT break_statement_eval(script_ctx_t*,statement_t*,return_type_t*,VARIANT*) DECLSPEC_HIDDEN;
355 HRESULT return_statement_eval(script_ctx_t*,statement_t*,return_type_t*,VARIANT*) DECLSPEC_HIDDEN;
356 HRESULT with_statement_eval(script_ctx_t*,statement_t*,return_type_t*,VARIANT*) DECLSPEC_HIDDEN;
357 HRESULT labelled_statement_eval(script_ctx_t*,statement_t*,return_type_t*,VARIANT*) DECLSPEC_HIDDEN;
358 HRESULT switch_statement_eval(script_ctx_t*,statement_t*,return_type_t*,VARIANT*) DECLSPEC_HIDDEN;
359 HRESULT throw_statement_eval(script_ctx_t*,statement_t*,return_type_t*,VARIANT*) DECLSPEC_HIDDEN;
360 HRESULT try_statement_eval(script_ctx_t*,statement_t*,return_type_t*,VARIANT*) DECLSPEC_HIDDEN;
444 typedef HRESULT (*expression_eval_t)(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*);
446 struct _expression_t {
447 expression_type_t type;
448 expression_eval_t eval;
452 struct _parameter_t {
453 const WCHAR *identifier;
455 struct _parameter_t *next;
458 struct _source_elements_t {
459 statement_t *statement;
460 statement_t *statement_tail;
461 function_declaration_t *functions;
462 var_list_t *variables;
465 struct _function_expression_t {
467 const WCHAR *identifier;
468 parameter_t *parameter_list;
469 source_elements_t *source_elements;
470 const WCHAR *src_str;
476 expression_t *expression1;
477 expression_t *expression2;
478 } binary_expression_t;
482 expression_t *expression;
483 } unary_expression_t;
487 expression_t *expression;
488 expression_t *true_expression;
489 expression_t *false_expression;
490 } conditional_expression_t;
494 expression_t *member_expr;
495 expression_t *expression;
496 } array_expression_t;
500 expression_t *expression;
501 const WCHAR *identifier;
502 } member_expression_t;
504 typedef struct _argument_t {
507 struct _argument_t *next;
512 expression_t *expression;
513 argument_t *argument_list;
518 const WCHAR *identifier;
519 } identifier_expression_t;
524 } literal_expression_t;
526 typedef struct _array_element_t {
530 struct _array_element_t *next;
535 array_element_t *element_list;
537 } array_literal_expression_t;
539 typedef struct _prop_val_t {
543 struct _prop_val_t *next;
548 prop_val_t *property_list;
549 } property_value_expression_t;
551 HRESULT function_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
552 HRESULT array_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
553 HRESULT member_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
554 HRESULT call_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
555 HRESULT identifier_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
556 HRESULT array_literal_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
557 HRESULT property_value_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
559 HRESULT binary_and_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
560 HRESULT instanceof_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
561 HRESULT delete_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
562 HRESULT typeof_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
563 HRESULT post_increment_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
564 HRESULT post_decrement_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
565 HRESULT pre_increment_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
566 HRESULT pre_decrement_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
567 HRESULT less_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
568 HRESULT lesseq_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
569 HRESULT greater_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
570 HRESULT greatereq_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
571 HRESULT left_shift_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
572 HRESULT right_shift_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
573 HRESULT right2_shift_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
574 HRESULT assign_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
575 HRESULT assign_lshift_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
576 HRESULT assign_rshift_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
577 HRESULT assign_rrshift_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
578 HRESULT assign_add_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
579 HRESULT assign_sub_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
580 HRESULT assign_mul_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
581 HRESULT assign_div_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
582 HRESULT assign_mod_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
583 HRESULT assign_and_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
584 HRESULT assign_or_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
585 HRESULT assign_xor_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
587 HRESULT compiled_expression_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,exprval_t*) DECLSPEC_HIDDEN;
589 HRESULT compile_subscript(parser_ctx_t*,expression_t*,unsigned*) DECLSPEC_HIDDEN;