vbscript.h 11.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright 2011 Jacek Caban for CodeWeavers
 *
 * 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
#include <stdarg.h>
20
#include <stdint.h>
21 22 23 24 25 26

#define COBJMACROS

#include "windef.h"
#include "winbase.h"
#include "ole2.h"
27
#include "dispex.h"
28
#include "activscp.h"
29
#include "activdbg.h"
30

31
#include "vbscript_classes.h"
32
#include "vbscript_defs.h"
33

34
#include "wine/heap.h"
35 36
#include "wine/list.h"

37 38 39 40 41
typedef struct {
    void **blocks;
    DWORD block_cnt;
    DWORD last_block;
    DWORD offset;
42
    BOOL mark;
43
    struct list custom_blocks;
44
} heap_pool_t;
45

46 47
void heap_pool_init(heap_pool_t*) DECLSPEC_HIDDEN;
void *heap_pool_alloc(heap_pool_t*,size_t) __WINE_ALLOC_SIZE(2) DECLSPEC_HIDDEN;
48 49
void *heap_pool_grow(heap_pool_t*,void*,DWORD,DWORD) DECLSPEC_HIDDEN;
void heap_pool_clear(heap_pool_t*) DECLSPEC_HIDDEN;
50
void heap_pool_free(heap_pool_t*) DECLSPEC_HIDDEN;
51
heap_pool_t *heap_pool_mark(heap_pool_t*) DECLSPEC_HIDDEN;
52

53 54
typedef struct _function_t function_t;
typedef struct _vbscode_t vbscode_t;
55
typedef struct _script_ctx_t script_ctx_t;
56
typedef struct _vbdisp_t vbdisp_t;
57

58 59 60 61 62 63 64
typedef enum {
    VBDISP_CALLGET,
    VBDISP_LET,
    VBDISP_SET,
    VBDISP_ANY
} vbdisp_invoke_type_t;

65 66 67 68 69
typedef struct {
    unsigned dim_cnt;
    SAFEARRAYBOUND *bounds;
} array_desc_t;

70 71
typedef struct {
    BOOL is_public;
72
    BOOL is_array;
73 74 75
    const WCHAR *name;
} vbdisp_prop_desc_t;

76 77 78
typedef struct {
    const WCHAR *name;
    BOOL is_public;
79
    BOOL is_array;
80 81 82
    function_t *entries[VBDISP_ANY];
} vbdisp_funcprop_desc_t;

83 84 85
typedef struct _class_desc_t {
    const WCHAR *name;
    script_ctx_t *ctx;
86

87
    unsigned class_initialize_id;
88
    unsigned class_terminate_id;
89 90
    unsigned func_cnt;
    vbdisp_funcprop_desc_t *funcs;
91

92 93
    unsigned prop_cnt;
    vbdisp_prop_desc_t *props;
94

95 96 97
    unsigned array_cnt;
    array_desc_t *array_descs;

98
    function_t *value_func;
99

100 101 102
    struct _class_desc_t *next;
} class_desc_t;

103
struct _vbdisp_t {
104 105 106
    IDispatchEx IDispatchEx_iface;

    LONG ref;
107
    BOOL terminator_ran;
108
    struct list entry;
109 110

    const class_desc_t *desc;
111
    SAFEARRAY **arrays;
112
    VARIANT props[1];
113
};
114

115 116 117 118 119 120 121 122
typedef struct _dynamic_var_t {
    struct _dynamic_var_t *next;
    VARIANT v;
    const WCHAR *name;
    BOOL is_const;
    SAFEARRAY *array;
} dynamic_var_t;

123 124 125
typedef struct {
    IDispatchEx IDispatchEx_iface;
    LONG ref;
126

127 128 129 130 131 132 133 134 135 136
    dynamic_var_t **global_vars;
    size_t global_vars_cnt;
    size_t global_vars_size;

    function_t **global_funcs;
    size_t global_funcs_cnt;
    size_t global_funcs_size;

    class_desc_t *classes;

137
    script_ctx_t *ctx;
138
    heap_pool_t heap;
139 140
} ScriptDisp;

141 142 143 144 145 146 147 148 149
typedef struct _builtin_prop_t builtin_prop_t;

typedef struct {
    IDispatch IDispatch_iface;
    LONG ref;
    size_t member_cnt;
    const builtin_prop_t *members;
    script_ctx_t *ctx;
} BuiltinDisp;
150

151
typedef struct named_item_t {
152
    ScriptDisp *script_obj;
153
    IDispatch *disp;
154
    unsigned ref;
155 156 157 158 159 160
    DWORD flags;
    LPWSTR name;

    struct list entry;
} named_item_t;

161 162 163 164
HRESULT create_vbdisp(const class_desc_t*,vbdisp_t**) DECLSPEC_HIDDEN;
HRESULT disp_get_id(IDispatch*,BSTR,vbdisp_invoke_type_t,BOOL,DISPID*) DECLSPEC_HIDDEN;
HRESULT vbdisp_get_id(vbdisp_t*,BSTR,vbdisp_invoke_type_t,BOOL,DISPID*) DECLSPEC_HIDDEN;
HRESULT disp_call(script_ctx_t*,IDispatch*,DISPID,DISPPARAMS*,VARIANT*) DECLSPEC_HIDDEN;
165
HRESULT disp_propput(script_ctx_t*,IDispatch*,DISPID,WORD,DISPPARAMS*) DECLSPEC_HIDDEN;
166
HRESULT get_disp_value(script_ctx_t*,IDispatch*,VARIANT*) DECLSPEC_HIDDEN;
167
void collect_objects(script_ctx_t*) DECLSPEC_HIDDEN;
168
HRESULT create_script_disp(script_ctx_t*,ScriptDisp**) DECLSPEC_HIDDEN;
169

170 171
HRESULT to_int(VARIANT*,int*) DECLSPEC_HIDDEN;

172 173 174 175 176
static inline unsigned arg_cnt(const DISPPARAMS *dp)
{
    return dp->cArgs - dp->cNamedArgs;
}

177 178 179 180 181
static inline VARIANT *get_arg(DISPPARAMS *dp, DWORD i)
{
    return dp->rgvarg + dp->cArgs-i-1;
}

182
struct _script_ctx_t {
183 184
    IActiveScriptSite *site;
    LCID lcid;
185

186 187 188
    IInternetHostSecurityManager *secmgr;
    DWORD safeopt;

189
    ScriptDisp *script_obj;
190

191 192
    BuiltinDisp *global_obj;
    BuiltinDisp *err_obj;
193

194
    EXCEPINFO ei;
195 196
    vbscode_t *error_loc_code;
    unsigned error_loc_offset;
197

198
    struct list objects;
199
    struct list code_list;
200
    struct list named_items;
201
};
202

203 204
HRESULT init_global(script_ctx_t*) DECLSPEC_HIDDEN;
HRESULT init_err(script_ctx_t*) DECLSPEC_HIDDEN;
205

206 207
IUnknown *create_ax_site(script_ctx_t*) DECLSPEC_HIDDEN;

208 209
typedef enum {
    ARG_NONE = 0,
210
    ARG_STR,
211 212
    ARG_BSTR,
    ARG_INT,
213
    ARG_UINT,
214
    ARG_ADDR,
215
    ARG_DOUBLE
216 217 218
} instr_arg_type_t;

#define OP_LIST                                   \
219
    X(add,            1, 0,           0)          \
220
    X(and,            1, 0,           0)          \
221 222
    X(assign_ident,   1, ARG_BSTR,    ARG_UINT)   \
    X(assign_member,  1, ARG_BSTR,    ARG_UINT)   \
223
    X(bool,           1, ARG_INT,     0)          \
224
    X(catch,          1, ARG_ADDR,    ARG_UINT)   \
225
    X(case,           0, ARG_ADDR,    0)          \
226
    X(concat,         1, 0,           0)          \
227
    X(const,          1, ARG_BSTR,    0)          \
228
    X(deref,          1, 0,           0)          \
229
    X(dim,            1, ARG_BSTR,    ARG_UINT)   \
230
    X(div,            1, 0,           0)          \
231
    X(double,         1, ARG_DOUBLE,  0)          \
232
    X(empty,          1, 0,           0)          \
233
    X(enumnext,       0, ARG_ADDR,    ARG_BSTR)   \
234
    X(equal,          1, 0,           0)          \
235
    X(hres,           1, ARG_UINT,    0)          \
236
    X(errmode,        1, ARG_INT,     0)          \
237
    X(eqv,            1, 0,           0)          \
238
    X(exp,            1, 0,           0)          \
239 240
    X(gt,             1, 0,           0)          \
    X(gteq,           1, 0,           0)          \
241
    X(icall,          1, ARG_BSTR,    ARG_UINT)   \
242
    X(icallv,         1, ARG_BSTR,    ARG_UINT)   \
243
    X(idiv,           1, 0,           0)          \
244
    X(imp,            1, 0,           0)          \
245
    X(incc,           1, ARG_BSTR,    0)          \
246
    X(int,            1, ARG_INT,     0)          \
247
    X(is,             1, 0,           0)          \
248 249
    X(jmp,            0, ARG_ADDR,    0)          \
    X(jmp_false,      0, ARG_ADDR,    0)          \
250
    X(jmp_true,       0, ARG_ADDR,    0)          \
251 252
    X(lt,             1, 0,           0)          \
    X(lteq,           1, 0,           0)          \
253 254
    X(mcall,          1, ARG_BSTR,    ARG_UINT)   \
    X(mcallv,         1, ARG_BSTR,    ARG_UINT)   \
255
    X(me,             1, 0,           0)          \
256
    X(mod,            1, 0,           0)          \
257
    X(mul,            1, 0,           0)          \
258
    X(neg,            1, 0,           0)          \
259
    X(nequal,         1, 0,           0)          \
260
    X(new,            1, ARG_STR,     0)          \
261
    X(newenum,        1, 0,           0)          \
262
    X(not,            1, 0,           0)          \
263
    X(nothing,        1, 0,           0)          \
264
    X(null,           1, 0,           0)          \
265
    X(or,             1, 0,           0)          \
266
    X(pop,            1, ARG_UINT,    0)          \
267
    X(redim,          1, ARG_BSTR,    ARG_UINT)   \
268
    X(ret,            0, 0,           0)          \
269
    X(retval,         1, 0,           0)          \
270 271
    X(set_ident,      1, ARG_BSTR,    ARG_UINT)   \
    X(set_member,     1, ARG_BSTR,    ARG_UINT)   \
272
    X(stack,          1, ARG_UINT,    0)          \
273
    X(step,           0, ARG_ADDR,    ARG_BSTR)   \
274
    X(stop,           1, 0,           0)          \
275
    X(string,         1, ARG_STR,     0)          \
276
    X(sub,            1, 0,           0)          \
277
    X(val,            1, 0,           0)          \
278 279
    X(vcall,          1, ARG_UINT,    0)          \
    X(vcallv,         1, ARG_UINT,    0)          \
280
    X(xor,            1, 0,           0)
281 282

typedef enum {
283
#define X(x,n,a,b) OP_##x,
284 285 286 287 288
OP_LIST
#undef X
    OP_LAST
} vbsop_t;

289 290
typedef union {
    const WCHAR *str;
291
    BSTR bstr;
292 293
    unsigned uint;
    LONG lng;
294
    double *dbl;
295 296
} instr_arg_t;

297 298
typedef struct {
    vbsop_t op;
299
    unsigned loc;
300 301
    instr_arg_t arg1;
    instr_arg_t arg2;
302 303
} instr_t;

304 305 306 307 308
typedef struct {
    const WCHAR *name;
    BOOL by_ref;
} arg_desc_t;

309 310
typedef enum {
    FUNC_GLOBAL,
311
    FUNC_FUNCTION,
312 313 314
    FUNC_SUB,
    FUNC_PROPGET,
    FUNC_PROPLET,
315 316
    FUNC_PROPSET,
    FUNC_DEFGET
317 318
} function_type_t;

319 320 321 322
typedef struct {
    const WCHAR *name;
} var_desc_t;

323
struct _function_t {
324 325
    function_type_t type;
    const WCHAR *name;
326
    BOOL is_public;
327 328
    arg_desc_t *args;
    unsigned arg_cnt;
329 330
    var_desc_t *vars;
    unsigned var_cnt;
331 332
    array_desc_t *array_descs;
    unsigned array_cnt;
333 334
    unsigned code_off;
    vbscode_t *code_ctx;
335
    function_t *next;
336 337 338 339
};

struct _vbscode_t {
    instr_t *instrs;
340
    unsigned ref;
341

342 343 344 345
    WCHAR *source;
    DWORD_PTR cookie;
    unsigned start_line;

346 347
    BOOL option_explicit;

348
    BOOL pending_exec;
349
    BOOL is_persistent;
350
    function_t main_code;
351
    named_item_t *named_item;
352

353 354 355
    BSTR *bstr_pool;
    unsigned bstr_pool_size;
    unsigned bstr_cnt;
356
    heap_pool_t heap;
357

358
    function_t *funcs;
359
    class_desc_t *classes;
360 361
    class_desc_t *last_class;

362 363 364
    struct list entry;
};

365 366 367 368 369
static inline void grab_vbscode(vbscode_t *code)
{
    code->ref++;
}

370
void release_vbscode(vbscode_t*) DECLSPEC_HIDDEN;
371 372
HRESULT compile_script(script_ctx_t*,const WCHAR*,const WCHAR*,const WCHAR*,DWORD_PTR,unsigned,DWORD,vbscode_t**) DECLSPEC_HIDDEN;
HRESULT compile_procedure(script_ctx_t*,const WCHAR*,const WCHAR*,const WCHAR*,DWORD_PTR,unsigned,DWORD,class_desc_t**) DECLSPEC_HIDDEN;
373
HRESULT exec_script(script_ctx_t*,BOOL,function_t*,vbdisp_t*,DISPPARAMS*,VARIANT*) DECLSPEC_HIDDEN;
374
void release_dynamic_var(dynamic_var_t*) DECLSPEC_HIDDEN;
375
named_item_t *lookup_named_item(script_ctx_t*,const WCHAR*,unsigned) DECLSPEC_HIDDEN;
376
void release_named_item(named_item_t*) DECLSPEC_HIDDEN;
377
void clear_ei(EXCEPINFO*) DECLSPEC_HIDDEN;
378
HRESULT report_script_error(script_ctx_t*,const vbscode_t*,unsigned) DECLSPEC_HIDDEN;
379
void detach_global_objects(script_ctx_t*) DECLSPEC_HIDDEN;
380
HRESULT get_builtin_id(BuiltinDisp*,const WCHAR*,DISPID*) DECLSPEC_HIDDEN;
381

382
void release_regexp_typelib(void) DECLSPEC_HIDDEN;
383
HRESULT get_dispatch_typeinfo(ITypeInfo**) DECLSPEC_HIDDEN;
384

385 386 387 388 389
static inline BOOL is_int32(double d)
{
    return INT32_MIN <= d && d <= INT32_MAX && (double)(int)d == d;
}

390 391 392 393 394
static inline BOOL is_digit(WCHAR c)
{
    return '0' <= c && c <= '9';
}

395
HRESULT create_regexp(IDispatch**) DECLSPEC_HIDDEN;
396
BSTR string_replace(BSTR,BSTR,BSTR,int,int) DECLSPEC_HIDDEN;
397

398 399
HRESULT map_hres(HRESULT) DECLSPEC_HIDDEN;

400 401
HRESULT create_safearray_iter(SAFEARRAY *sa, IEnumVARIANT **ev) DECLSPEC_HIDDEN;

402 403 404
#define FACILITY_VBS 0xa
#define MAKE_VBSERROR(code) MAKE_HRESULT(SEVERITY_ERROR, FACILITY_VBS, code)

405
HRESULT WINAPI VBScriptFactory_CreateInstance(IClassFactory*,IUnknown*,REFIID,void**) DECLSPEC_HIDDEN;
406
HRESULT WINAPI VBScriptRegExpFactory_CreateInstance(IClassFactory*,IUnknown*,REFIID,void**) DECLSPEC_HIDDEN;
407

408 409 410
BSTR get_vbscript_string(int) DECLSPEC_HIDDEN;
BSTR get_vbscript_error_string(HRESULT) DECLSPEC_HIDDEN;

411 412 413 414 415 416 417
static inline LPWSTR heap_strdupW(LPCWSTR str)
{
    LPWSTR ret = NULL;

    if(str) {
        DWORD size;

418
        size = (lstrlenW(str)+1)*sizeof(WCHAR);
419 420 421 422 423 424 425
        ret = heap_alloc(size);
        if(ret)
            memcpy(ret, str, size);
    }

    return ret;
}
426 427

#define VBSCRIPT_BUILD_VERSION 16978
428
#define VBSCRIPT_MAJOR_VERSION 5
429
#define VBSCRIPT_MINOR_VERSION 8