engine.h 8.11 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(carray_set, 1, ARG_UINT,   0)        \
31
    X(case,       0, ARG_ADDR,   0)        \
32 33
    X(cnd_nz,     0, ARG_ADDR,   0)        \
    X(cnd_z,      0, ARG_ADDR,   0)        \
34
    X(delete,     1, 0,0)                  \
35
    X(delete_ident,1,ARG_BSTR,   0)        \
36
    X(div,        1, 0,0)                  \
37
    X(double,     1, ARG_DBL,    0)        \
38
    X(end_finally,0, 0,0)                  \
39
    X(enter_catch,1, ARG_BSTR,   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(local,      1, ARG_INT,    0)        \
54
    X(local_ref,  1, ARG_INT,    ARG_UINT) \
55
    X(lshift,     1, 0,0)                  \
56
    X(lt,         1, 0,0)                  \
57
    X(lteq,       1, 0,0)                  \
58
    X(member,     1, ARG_BSTR,   0)        \
59
    X(memberid,   1, ARG_UINT,   0)        \
60
    X(minus,      1, 0,0)                  \
61
    X(mod,        1, 0,0)                  \
62
    X(mul,        1, 0,0)                  \
63
    X(neg,        1, 0,0)                  \
64
    X(neq,        1, 0,0)                  \
65
    X(neq2,       1, 0,0)                  \
66
    X(new,        1, ARG_UINT,   0)        \
67
    X(new_obj,    1, 0,0)                  \
68
    X(null,       1, 0,0)                  \
69
    X(obj_prop,   1, ARG_STR,    ARG_UINT) \
70
    X(or,         1, 0,0)                  \
71
    X(pop,        1, ARG_UINT,   0)        \
72
    X(pop_except, 0, ARG_ADDR,   0)        \
73
    X(pop_scope,  1, 0,0)                  \
74
    X(postinc,    1, ARG_INT,    0)        \
75
    X(preinc,     1, ARG_INT,    0)        \
76
    X(push_acc,   1, 0,0)                  \
77
    X(push_except,1, ARG_ADDR,   ARG_UINT) \
78
    X(push_scope, 1, 0,0)                  \
79
    X(regexp,     1, ARG_STR,    ARG_UINT) \
80
    X(rshift,     1, 0,0)                  \
81
    X(rshift2,    1, 0,0)                  \
82
    X(str,        1, ARG_STR,    0)        \
83
    X(this,       1, 0,0)                  \
84
    X(throw,      0, 0,0)                  \
85
    X(throw_ref,  0, ARG_UINT,   0)        \
86
    X(throw_type, 0, ARG_UINT,   ARG_STR)  \
87
    X(tonum,      1, 0,0)                  \
88 89 90
    X(typeof,     1, 0,0)                  \
    X(typeofid,   1, 0,0)                  \
    X(typeofident,1, 0,0)                  \
91
    X(refval,     1, 0,0)                  \
92
    X(ret,        0, ARG_UINT,   0)        \
93
    X(setret,     1, 0,0)                  \
94
    X(sub,        1, 0,0)                  \
95
    X(undefined,  1, 0,0)                  \
96 97
    X(void,       1, 0,0)                  \
    X(xor,        1, 0,0)
98 99

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

106 107
typedef struct _bytecode_t bytecode_t;

108
typedef union {
109
    BSTR bstr;
110
    LONG lng;
111
    jsstr_t *str;
112
    unsigned uint;
113 114 115 116
} instr_arg_t;

typedef enum {
    ARG_NONE = 0,
117
    ARG_ADDR,
118
    ARG_BSTR,
119
    ARG_DBL,
120
    ARG_FUNC,
121
    ARG_INT,
122 123
    ARG_STR,
    ARG_UINT
124 125 126 127
} instr_arg_type_t;

typedef struct {
    jsop_t op;
128
    unsigned loc;
129 130 131 132
    union {
        instr_arg_t arg[2];
        double dbl;
    } u;
133 134
} instr_t;

135 136 137 138 139 140
typedef enum {
    PROPERTY_DEFINITION_VALUE,
    PROPERTY_DEFINITION_GETTER,
    PROPERTY_DEFINITION_SETTER
} property_definition_type_t;

141 142 143 144 145
typedef struct {
    BSTR name;
    int ref;
} local_ref_t;

146
typedef struct _function_code_t {
147
    BSTR name;
148
    int local_ref;
149
    BSTR event_target;
150 151
    unsigned instr_off;

152 153 154
    const WCHAR *source;
    unsigned source_len;

155 156
    unsigned func_cnt;
    struct _function_code_t *funcs;
157 158

    unsigned var_cnt;
159 160 161 162
    struct {
        BSTR name;
        int func_id; /* -1 if not a function */
    } *variables;
163 164 165

    unsigned param_cnt;
    BSTR *params;
166 167 168

    unsigned locals_cnt;
    local_ref_t *locals;
169 170

    bytecode_t *bytecode;
171 172
} function_code_t;

173 174
local_ref_t *lookup_local(const function_code_t*,const WCHAR*) DECLSPEC_HIDDEN;

175
struct _bytecode_t {
176
    LONG ref;
177
    BOOL is_persistent;
178

179
    instr_t *instrs;
180
    heap_pool_t heap;
181

182 183
    function_code_t global_code;

184
    WCHAR *source;
185 186
    UINT64 source_context;
    unsigned start_line;
187

188 189 190
    BSTR *bstr_pool;
    unsigned bstr_pool_size;
    unsigned bstr_cnt;
191

192 193 194 195
    jsstr_t **str_pool;
    unsigned str_pool_size;
    unsigned str_cnt;

196
    struct list entry;
197
};
198

199
HRESULT compile_script(script_ctx_t*,const WCHAR*,UINT64,unsigned,const WCHAR*,const WCHAR*,BOOL,BOOL,bytecode_t**) DECLSPEC_HIDDEN;
200
void release_bytecode(bytecode_t*) DECLSPEC_HIDDEN;
201

202
static inline bytecode_t *bytecode_addref(bytecode_t *code)
203 204
{
    code->ref++;
205
    return code;
206
}
Jacek Caban's avatar
Jacek Caban committed
207

208 209
typedef struct _scope_chain_t {
    LONG ref;
210 211
    jsdisp_t *jsobj;
    IDispatch *obj;
212
    struct _call_frame_t *frame;
213 214 215
    struct _scope_chain_t *next;
} scope_chain_t;

216
void scope_release(scope_chain_t*) DECLSPEC_HIDDEN;
217

218
static inline scope_chain_t *scope_addref(scope_chain_t *scope)
219 220
{
    scope->ref++;
221
    return scope;
222 223
}

224
struct _jsexcept_t {
225 226
    HRESULT error;

227
    BOOL valid_value;
228
    jsval_t value;
229

230
    jsstr_t *source;
231
    jsstr_t *message;
232
    jsstr_t *line;
233 234 235 236

    bytecode_t *code;
    unsigned loc;

237
    BOOL enter_notified;
238 239 240 241 242 243
    jsexcept_t *prev;
};

void enter_script(script_ctx_t*,jsexcept_t*) DECLSPEC_HIDDEN;
HRESULT leave_script(script_ctx_t*,HRESULT) DECLSPEC_HIDDEN;
void reset_ei(jsexcept_t*) DECLSPEC_HIDDEN;
244
void set_error_location(jsexcept_t*,bytecode_t*,unsigned,unsigned,jsstr_t*) DECLSPEC_HIDDEN;
245

246
typedef struct _except_frame_t except_frame_t;
247
struct _parser_ctx_t;
248

249
typedef struct _call_frame_t {
250
    unsigned ip;
251
    except_frame_t *except_frame;
252
    unsigned stack_base;
253
    scope_chain_t *scope;
254
    scope_chain_t *base_scope;
255

256 257
    jsval_t ret;

258
    IDispatch *this_obj;
259
    jsdisp_t *function_instance;
260
    jsdisp_t *variable_obj;
261
    jsdisp_t *arguments_obj;
262
    DWORD flags;
263

264
    unsigned argc;
265 266
    unsigned pop_locals;
    unsigned arguments_off;
267 268
    unsigned variables_off;
    unsigned pop_variables;
269

270
    bytecode_t *bytecode;
271
    function_code_t *function;
272

273 274 275
    struct _call_frame_t *prev_frame;
} call_frame_t;

276
#define EXEC_GLOBAL            0x0001
277
#define EXEC_CONSTRUCTOR       0x0002
278
#define EXEC_RETURN_TO_INTERP  0x0004
279
#define EXEC_EVAL              0x0008
280

281
HRESULT exec_source(script_ctx_t*,DWORD,bytecode_t*,function_code_t*,scope_chain_t*,IDispatch*,
282
        jsdisp_t*,jsdisp_t*,unsigned,jsval_t*,jsval_t*) DECLSPEC_HIDDEN;
283

284
HRESULT create_source_function(script_ctx_t*,bytecode_t*,function_code_t*,scope_chain_t*,jsdisp_t**) DECLSPEC_HIDDEN;
285
HRESULT setup_arguments_object(script_ctx_t*,call_frame_t*) DECLSPEC_HIDDEN;
286
void detach_arguments_object(jsdisp_t*) DECLSPEC_HIDDEN;