[JSCRIPT] Sync with Wine Staging 1.7.47. CORE-9924
[reactos.git] / reactos / dll / win32 / 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 #pragma once
20
21 #define OP_LIST \
22 X(add, 1, 0,0) \
23 X(and, 1, 0,0) \
24 X(array, 1, 0,0) \
25 X(assign, 1, 0,0) \
26 X(assign_call,1, ARG_UINT, 0) \
27 X(bool, 1, ARG_INT, 0) \
28 X(bneg, 1, 0,0) \
29 X(call, 1, ARG_UINT, ARG_UINT) \
30 X(call_member,1, ARG_UINT, ARG_UINT) \
31 X(carray, 1, ARG_UINT, 0) \
32 X(case, 0, ARG_ADDR, 0) \
33 X(cnd_nz, 0, ARG_ADDR, 0) \
34 X(cnd_z, 0, ARG_ADDR, 0) \
35 X(delete, 1, 0,0) \
36 X(delete_ident,1,ARG_BSTR, 0) \
37 X(div, 1, 0,0) \
38 X(double, 1, ARG_DBL, 0) \
39 X(end_finally,1, 0,0) \
40 X(eq, 1, 0,0) \
41 X(eq2, 1, 0,0) \
42 X(forin, 0, ARG_ADDR, 0) \
43 X(func, 1, ARG_UINT, 0) \
44 X(gt, 1, 0,0) \
45 X(gteq, 1, 0,0) \
46 X(ident, 1, ARG_BSTR, 0) \
47 X(identid, 1, ARG_BSTR, ARG_INT) \
48 X(in, 1, 0,0) \
49 X(instanceof, 1, 0,0) \
50 X(int, 1, ARG_INT, 0) \
51 X(jmp, 0, ARG_ADDR, 0) \
52 X(jmp_z, 0, ARG_ADDR, 0) \
53 X(lshift, 1, 0,0) \
54 X(lt, 1, 0,0) \
55 X(lteq, 1, 0,0) \
56 X(member, 1, ARG_BSTR, 0) \
57 X(memberid, 1, ARG_UINT, 0) \
58 X(minus, 1, 0,0) \
59 X(mod, 1, 0,0) \
60 X(mul, 1, 0,0) \
61 X(neg, 1, 0,0) \
62 X(neq, 1, 0,0) \
63 X(neq2, 1, 0,0) \
64 X(new, 1, ARG_UINT, 0) \
65 X(new_obj, 1, 0,0) \
66 X(null, 1, 0,0) \
67 X(obj_prop, 1, ARG_BSTR, 0) \
68 X(or, 1, 0,0) \
69 X(pop, 1, ARG_UINT, 0) \
70 X(pop_except, 1, 0,0) \
71 X(pop_scope, 1, 0,0) \
72 X(postinc, 1, ARG_INT, 0) \
73 X(preinc, 1, ARG_INT, 0) \
74 X(push_except,1, ARG_ADDR, ARG_BSTR) \
75 X(push_scope, 1, 0,0) \
76 X(regexp, 1, ARG_STR, ARG_UINT) \
77 X(rshift, 1, 0,0) \
78 X(rshift2, 1, 0,0) \
79 X(str, 1, ARG_STR, 0) \
80 X(this, 1, 0,0) \
81 X(throw, 0, 0,0) \
82 X(throw_ref, 0, ARG_UINT, 0) \
83 X(throw_type, 0, ARG_UINT, ARG_STR) \
84 X(tonum, 1, 0,0) \
85 X(typeof, 1, 0,0) \
86 X(typeofid, 1, 0,0) \
87 X(typeofident,1, 0,0) \
88 X(refval, 1, 0,0) \
89 X(ret, 0, 0,0) \
90 X(setret, 1, 0,0) \
91 X(sub, 1, 0,0) \
92 X(undefined, 1, 0,0) \
93 X(var_set, 1, ARG_BSTR, 0) \
94 X(void, 1, 0,0) \
95 X(xor, 1, 0,0)
96
97 typedef enum {
98 #define X(x,a,b,c) OP_##x,
99 OP_LIST
100 #undef X
101 OP_LAST
102 } jsop_t;
103
104 typedef union {
105 BSTR bstr;
106 LONG lng;
107 jsstr_t *str;
108 unsigned uint;
109 } instr_arg_t;
110
111 typedef enum {
112 ARG_NONE = 0,
113 ARG_ADDR,
114 ARG_BSTR,
115 ARG_DBL,
116 ARG_FUNC,
117 ARG_INT,
118 ARG_STR,
119 ARG_UINT
120 } instr_arg_type_t;
121
122 typedef struct {
123 jsop_t op;
124 union {
125 instr_arg_t arg[2];
126 double dbl;
127 } u;
128 } instr_t;
129
130 typedef struct _function_code_t {
131 BSTR name;
132 BSTR event_target;
133 unsigned instr_off;
134
135 const WCHAR *source;
136 unsigned source_len;
137
138 unsigned func_cnt;
139 struct _function_code_t *funcs;
140
141 unsigned var_cnt;
142 BSTR *variables;
143
144 unsigned param_cnt;
145 BSTR *params;
146 } function_code_t;
147
148 typedef struct _bytecode_t {
149 LONG ref;
150
151 instr_t *instrs;
152 heap_pool_t heap;
153
154 function_code_t global_code;
155
156 WCHAR *source;
157
158 BSTR *bstr_pool;
159 unsigned bstr_pool_size;
160 unsigned bstr_cnt;
161
162 jsstr_t **str_pool;
163 unsigned str_pool_size;
164 unsigned str_cnt;
165
166 struct _bytecode_t *next;
167 } bytecode_t;
168
169 HRESULT compile_script(script_ctx_t*,const WCHAR*,const WCHAR*,const WCHAR*,BOOL,BOOL,bytecode_t**) DECLSPEC_HIDDEN;
170 void release_bytecode(bytecode_t*) DECLSPEC_HIDDEN;
171
172 static inline void bytecode_addref(bytecode_t *code)
173 {
174 code->ref++;
175 }
176
177 typedef struct _scope_chain_t {
178 LONG ref;
179 jsdisp_t *jsobj;
180 IDispatch *obj;
181 struct _scope_chain_t *next;
182 } scope_chain_t;
183
184 HRESULT scope_push(scope_chain_t*,jsdisp_t*,IDispatch*,scope_chain_t**) DECLSPEC_HIDDEN;
185 void scope_release(scope_chain_t*) DECLSPEC_HIDDEN;
186
187 static inline void scope_addref(scope_chain_t *scope)
188 {
189 scope->ref++;
190 }
191
192 typedef struct _except_frame_t except_frame_t;
193 struct _parser_ctx_t;
194
195 struct _exec_ctx_t {
196 LONG ref;
197
198 struct _parser_ctx_t *parser;
199 bytecode_t *code;
200 script_ctx_t *script;
201 scope_chain_t *scope_chain;
202 jsdisp_t *var_disp;
203 IDispatch *this_obj;
204 function_code_t *func_code;
205 BOOL is_global;
206
207 jsval_t *stack;
208 unsigned stack_size;
209 unsigned top;
210 except_frame_t *except_frame;
211 jsval_t ret;
212
213 unsigned ip;
214 };
215
216 static inline void exec_addref(exec_ctx_t *ctx)
217 {
218 ctx->ref++;
219 }
220
221 void exec_release(exec_ctx_t*) DECLSPEC_HIDDEN;
222 HRESULT create_exec_ctx(script_ctx_t*,IDispatch*,jsdisp_t*,scope_chain_t*,BOOL,exec_ctx_t**) DECLSPEC_HIDDEN;
223 HRESULT exec_source(exec_ctx_t*,bytecode_t*,function_code_t*,BOOL,jsval_t*) DECLSPEC_HIDDEN;
224 HRESULT create_source_function(script_ctx_t*,bytecode_t*,function_code_t*,scope_chain_t*,jsdisp_t**) DECLSPEC_HIDDEN;