engine.h 7.34 KB
Newer Older
Jacek Caban's avatar
Jacek Caban committed
1
/*
2
 * Copyright 2008,2011 Jacek Caban for CodeWeavers
Jacek Caban's avatar
Jacek Caban committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

19 20
#define OP_LIST                            \
    X(add,        1, 0,0)                  \
21
    X(and,        1, 0,0)                  \
22
    X(array,      1, 0,0)                  \
23
    X(assign,     1, 0,0)                  \
24
    X(assign_call,1, ARG_UINT,   0)       \
25 26
    X(bool,       1, ARG_INT,    0)        \
    X(bneg,       1, 0,0)                  \
27
    X(call,       1, ARG_UINT,   ARG_UINT) \
28
    X(call_member,1, ARG_UINT,   ARG_UINT) \
29
    X(carray,     1, ARG_UINT,   0)        \
30
    X(case,       0, ARG_ADDR,   0)        \
31 32
    X(cnd_nz,     0, ARG_ADDR,   0)        \
    X(cnd_z,      0, ARG_ADDR,   0)        \
33
    X(delete,     1, 0,0)                  \
34
    X(delete_ident,1,ARG_BSTR,   0)        \
35
    X(div,        1, 0,0)                  \
36 37
    X(double,     1, ARG_DBL,    0)        \
    X(end_finally,1, 0,0)                  \
38
    X(eq,         1, 0,0)                  \
39
    X(eq2,        1, 0,0)                  \
40
    X(forin,      0, ARG_ADDR,   0)        \
41
    X(func,       1, ARG_UINT,   0)        \
42
    X(gt,         1, 0,0)                  \
43
    X(gteq,       1, 0,0)                  \
44
    X(ident,      1, ARG_BSTR,   0)        \
45
    X(identid,    1, ARG_BSTR,   ARG_INT)  \
46
    X(in,         1, 0,0)                  \
47
    X(instanceof, 1, 0,0)                  \
48
    X(int,        1, ARG_INT,    0)        \
49
    X(jmp,        0, ARG_ADDR,   0)        \
50
    X(jmp_z,      0, ARG_ADDR,   0)        \
51
    X(local,      1, ARG_INT,    0)        \
52
    X(local_ref,  1, ARG_INT,    ARG_UINT) \
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_ret,   1, 0,0)                  \
76
    X(push_scope, 1, 0,0)                  \
77
    X(regexp,     1, ARG_STR,    ARG_UINT) \
78
    X(rshift,     1, 0,0)                  \
79
    X(rshift2,    1, 0,0)                  \
80
    X(str,        1, ARG_STR,    0)        \
81
    X(this,       1, 0,0)                  \
82
    X(throw,      0, 0,0)                  \
83
    X(throw_ref,  0, ARG_UINT,   0)        \
84
    X(throw_type, 0, ARG_UINT,   ARG_STR)  \
85
    X(tonum,      1, 0,0)                  \
86 87 88
    X(typeof,     1, 0,0)                  \
    X(typeofid,   1, 0,0)                  \
    X(typeofident,1, 0,0)                  \
89
    X(refval,     1, 0,0)                  \
90
    X(ret,        0, 0,0)                  \
91
    X(setret,     1, 0,0)                  \
92
    X(sub,        1, 0,0)                  \
93
    X(undefined,  1, 0,0)                  \
94 95
    X(void,       1, 0,0)                  \
    X(xor,        1, 0,0)
96 97

typedef enum {
98
#define X(x,a,b,c) OP_##x,
99 100 101 102 103 104
OP_LIST
#undef X
    OP_LAST
} jsop_t;

typedef union {
105
    BSTR bstr;
106
    LONG lng;
107
    jsstr_t *str;
108
    unsigned uint;
109 110 111 112
} instr_arg_t;

typedef enum {
    ARG_NONE = 0,
113
    ARG_ADDR,
114
    ARG_BSTR,
115
    ARG_DBL,
116
    ARG_FUNC,
117
    ARG_INT,
118 119
    ARG_STR,
    ARG_UINT
120 121 122 123
} instr_arg_type_t;

typedef struct {
    jsop_t op;
124 125 126 127
    union {
        instr_arg_t arg[2];
        double dbl;
    } u;
128 129
} instr_t;

130 131 132 133 134
typedef struct {
    BSTR name;
    int ref;
} local_ref_t;

135
typedef struct _function_code_t {
136
    BSTR name;
137
    int local_ref;
138
    BSTR event_target;
139 140
    unsigned instr_off;

141 142 143
    const WCHAR *source;
    unsigned source_len;

144 145
    unsigned func_cnt;
    struct _function_code_t *funcs;
146 147

    unsigned var_cnt;
148 149 150 151
    struct {
        BSTR name;
        int func_id; /* -1 if not a function */
    } *variables;
152 153 154

    unsigned param_cnt;
    BSTR *params;
155 156 157

    unsigned locals_cnt;
    local_ref_t *locals;
158 159
} function_code_t;

160 161
local_ref_t *lookup_local(const function_code_t*,const WCHAR*) DECLSPEC_HIDDEN;

162 163 164
typedef struct _bytecode_t {
    LONG ref;

165
    instr_t *instrs;
166
    heap_pool_t heap;
167

168 169
    function_code_t global_code;

170 171
    WCHAR *source;

172 173 174
    BSTR *bstr_pool;
    unsigned bstr_pool_size;
    unsigned bstr_cnt;
175

176 177 178 179
    jsstr_t **str_pool;
    unsigned str_pool_size;
    unsigned str_cnt;

180 181
    struct _bytecode_t *next;
} bytecode_t;
182

183
HRESULT compile_script(script_ctx_t*,const WCHAR*,const WCHAR*,const WCHAR*,BOOL,BOOL,bytecode_t**) DECLSPEC_HIDDEN;
184
void release_bytecode(bytecode_t*) DECLSPEC_HIDDEN;
185

186
static inline bytecode_t *bytecode_addref(bytecode_t *code)
187 188
{
    code->ref++;
189
    return code;
190
}
Jacek Caban's avatar
Jacek Caban committed
191

192 193
typedef struct _scope_chain_t {
    LONG ref;
194 195
    jsdisp_t *jsobj;
    IDispatch *obj;
196
    struct _call_frame_t *frame;
197 198 199
    struct _scope_chain_t *next;
} scope_chain_t;

200
HRESULT scope_push(scope_chain_t*,jsdisp_t*,IDispatch*,scope_chain_t**) DECLSPEC_HIDDEN;
201
void scope_release(scope_chain_t*) DECLSPEC_HIDDEN;
202

203
static inline scope_chain_t *scope_addref(scope_chain_t *scope)
204 205
{
    scope->ref++;
206
    return scope;
207 208
}

209
typedef struct _except_frame_t except_frame_t;
210
struct _parser_ctx_t;
211

212
typedef struct _call_frame_t {
213
    unsigned ip;
214
    except_frame_t *except_frame;
215
    unsigned stack_base;
216
    scope_chain_t *scope;
217
    scope_chain_t *base_scope;
218

219 220
    jsval_t ret;

221
    IDispatch *this_obj;
222
    jsdisp_t *function_instance;
223
    jsdisp_t *variable_obj;
224
    jsdisp_t *arguments_obj;
225
    DWORD flags;
226

227
    unsigned argc;
228 229
    unsigned pop_locals;
    unsigned arguments_off;
230 231
    unsigned variables_off;
    unsigned pop_variables;
232

233
    bytecode_t *bytecode;
234
    function_code_t *function;
235

236 237 238
    struct _call_frame_t *prev_frame;
} call_frame_t;

239
#define EXEC_GLOBAL            0x0001
240
#define EXEC_CONSTRUCTOR       0x0002
241
#define EXEC_RETURN_TO_INTERP  0x0004
242
#define EXEC_EVAL              0x0008
243

244
HRESULT exec_source(script_ctx_t*,DWORD,bytecode_t*,function_code_t*,scope_chain_t*,IDispatch*,
245
        jsdisp_t*,jsdisp_t*,unsigned,jsval_t*,jsval_t*) DECLSPEC_HIDDEN;
246

247
HRESULT create_source_function(script_ctx_t*,bytecode_t*,function_code_t*,scope_chain_t*,jsdisp_t**) DECLSPEC_HIDDEN;
248
HRESULT setup_arguments_object(script_ctx_t*,call_frame_t*) DECLSPEC_HIDDEN;
249
void detach_arguments_object(jsdisp_t*) DECLSPEC_HIDDEN;