vbscript: Store call identifier as BSTR.
[wine] / dlls / vbscript / vbscript.h
1 /*
2  * Copyright 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 #include <stdarg.h>
20
21 #define COBJMACROS
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "ole2.h"
26 #include "dispex.h"
27 #include "activscp.h"
28
29 #include "vbscript_classes.h"
30
31 #include "wine/list.h"
32 #include "wine/unicode.h"
33
34 typedef struct _function_t function_t;
35 typedef struct _vbscode_t vbscode_t;
36
37 typedef struct named_item_t {
38     IDispatch *disp;
39     DWORD flags;
40     LPWSTR name;
41
42     struct list entry;
43 } named_item_t;
44
45 typedef struct {
46     IDispatchEx IDispatchEx_iface;
47
48     LONG ref;
49 } vbdisp_t;
50
51 typedef struct {
52     IActiveScriptSite *site;
53     LCID lcid;
54
55     IDispatch *host_global;
56
57     vbdisp_t *script_obj;
58
59     struct list code_list;
60     struct list named_items;
61 } script_ctx_t;
62
63 HRESULT init_global(script_ctx_t*);
64
65 typedef enum {
66     ARG_NONE = 0,
67     ARG_STR,
68     ARG_BSTR
69 } instr_arg_type_t;
70
71 #define OP_LIST                                   \
72     X(icallv,         1, ARG_BSTR,    0)          \
73     X(ret,            0, 0,           0)
74
75 typedef enum {
76 #define X(x,n,a,b) OP_##x,
77 OP_LIST
78 #undef X
79     OP_LAST
80 } vbsop_t;
81
82 typedef union {
83     const WCHAR *str;
84     BSTR bstr;
85 } instr_arg_t;
86
87 typedef struct {
88     vbsop_t op;
89     instr_arg_t arg1;
90     instr_arg_t arg2;
91 } instr_t;
92
93 struct _function_t {
94     unsigned code_off;
95     vbscode_t *code_ctx;
96 };
97
98 struct _vbscode_t {
99     instr_t *instrs;
100     WCHAR *source;
101
102     BOOL global_executed;
103     function_t global_code;
104
105     BSTR *bstr_pool;
106     unsigned bstr_pool_size;
107     unsigned bstr_cnt;
108
109     struct list entry;
110 };
111
112 void release_vbscode(vbscode_t*) DECLSPEC_HIDDEN;
113 HRESULT compile_script(script_ctx_t*,const WCHAR*,vbscode_t**) DECLSPEC_HIDDEN;
114 HRESULT exec_script(script_ctx_t*,function_t*) DECLSPEC_HIDDEN;
115
116 HRESULT WINAPI VBScriptFactory_CreateInstance(IClassFactory*,IUnknown*,REFIID,void**);
117
118 static inline void *heap_alloc(size_t len)
119 {
120     return HeapAlloc(GetProcessHeap(), 0, len);
121 }
122
123 static inline void *heap_alloc_zero(size_t len)
124 {
125     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
126 }
127
128 static inline void *heap_realloc(void *mem, size_t len)
129 {
130     return HeapReAlloc(GetProcessHeap(), 0, mem, len);
131 }
132
133 static inline BOOL heap_free(void *mem)
134 {
135     return HeapFree(GetProcessHeap(), 0, mem);
136 }
137
138 static inline LPWSTR heap_strdupW(LPCWSTR str)
139 {
140     LPWSTR ret = NULL;
141
142     if(str) {
143         DWORD size;
144
145         size = (strlenW(str)+1)*sizeof(WCHAR);
146         ret = heap_alloc(size);
147         if(ret)
148             memcpy(ret, str, size);
149     }
150
151     return ret;
152 }