jscript: Return buffer pointer separately from jsstr_t from jsstr_alloc_len.
[wine] / dlls / jscript / object.c
1 /*
2  * Copyright 2008 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 <assert.h>
20
21 #include "jscript.h"
22
23 #include "wine/debug.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
26
27 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
28 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
29 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
30 static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
31 static const WCHAR propertyIsEnumerableW[] =
32     {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
33 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
34
35 static const WCHAR default_valueW[] = {'[','o','b','j','e','c','t',' ','O','b','j','e','c','t',']',0};
36
37 static HRESULT Object_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
38         jsval_t *r)
39 {
40     jsdisp_t *jsdisp;
41     const WCHAR *str;
42
43     static const WCHAR formatW[] = {'[','o','b','j','e','c','t',' ','%','s',']',0};
44
45     static const WCHAR arrayW[] = {'A','r','r','a','y',0};
46     static const WCHAR booleanW[] = {'B','o','o','l','e','a','n',0};
47     static const WCHAR dateW[] = {'D','a','t','e',0};
48     static const WCHAR errorW[] = {'E','r','r','o','r',0};
49     static const WCHAR functionW[] = {'F','u','n','c','t','i','o','n',0};
50     static const WCHAR mathW[] = {'M','a','t','h',0};
51     static const WCHAR numberW[] = {'N','u','m','b','e','r',0};
52     static const WCHAR objectW[] = {'O','b','j','e','c','t',0};
53     static const WCHAR regexpW[] = {'R','e','g','E','x','p',0};
54     static const WCHAR stringW[] = {'S','t','r','i','n','g',0};
55     /* Keep in sync with jsclass_t enum */
56     static const WCHAR *names[] = {NULL, arrayW, booleanW, dateW, errorW,
57         functionW, NULL, mathW, numberW, objectW, regexpW, stringW, objectW, objectW};
58
59     TRACE("\n");
60
61     jsdisp = get_jsdisp(jsthis);
62     if(!jsdisp) {
63         str = objectW;
64     }else if(names[jsdisp->builtin_info->class]) {
65         str = names[jsdisp->builtin_info->class];
66     }else {
67         assert(jsdisp->builtin_info->class != JSCLASS_NONE);
68         FIXME("jdisp->builtin_info->class = %d\n", jsdisp->builtin_info->class);
69         return E_FAIL;
70     }
71
72     if(r) {
73         jsstr_t *ret;
74         WCHAR *ptr;
75
76         ptr = jsstr_alloc_buf(9+strlenW(str), &ret);
77         if(!ptr)
78             return E_OUTOFMEMORY;
79
80         sprintfW(ptr, formatW, str);
81         *r = jsval_string(ret);
82     }
83
84     return S_OK;
85 }
86
87 static HRESULT Object_toLocaleString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
88         jsval_t *r)
89 {
90     TRACE("\n");
91
92     if(!is_jsdisp(jsthis)) {
93         FIXME("Host object this\n");
94         return E_FAIL;
95     }
96
97     return jsdisp_call_name(jsthis->u.jsdisp, toStringW, DISPATCH_METHOD, 0, NULL, r);
98 }
99
100 static HRESULT Object_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
101         jsval_t *r)
102 {
103     TRACE("\n");
104
105     if(r) {
106         IDispatch_AddRef(jsthis->u.disp);
107         *r = jsval_disp(jsthis->u.disp);
108     }
109     return S_OK;
110 }
111
112 static HRESULT Object_hasOwnProperty(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
113         jsval_t *r)
114 {
115     jsstr_t *name;
116     DISPID id;
117     BSTR bstr;
118     HRESULT hres;
119
120     TRACE("\n");
121
122     if(!argc) {
123         if(r)
124             *r = jsval_bool(FALSE);
125         return S_OK;
126     }
127
128     hres = to_string(ctx, argv[0], &name);
129     if(FAILED(hres))
130         return hres;
131
132     if(is_jsdisp(jsthis)) {
133         BOOL result;
134
135         hres = jsdisp_is_own_prop(jsthis->u.jsdisp, name->str, &result);
136         jsstr_release(name);
137         if(FAILED(hres))
138             return hres;
139
140         if(r)
141             *r = jsval_bool(result);
142         return S_OK;
143     }
144
145
146     bstr = SysAllocStringLen(NULL, jsstr_length(name));
147     if(bstr)
148         jsstr_flush(name, bstr);
149     jsstr_release(name);
150     if(!bstr)
151         return E_OUTOFMEMORY;
152
153     if(is_dispex(jsthis))
154         hres = IDispatchEx_GetDispID(jsthis->u.dispex, bstr, make_grfdex(ctx, fdexNameCaseSensitive), &id);
155     else
156         hres = IDispatch_GetIDsOfNames(jsthis->u.disp, &IID_NULL, &bstr, 1, ctx->lcid, &id);
157
158     SysFreeString(bstr);
159     if(r)
160         *r = jsval_bool(SUCCEEDED(hres));
161     return S_OK;
162 }
163
164 static HRESULT Object_propertyIsEnumerable(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
165         jsval_t *r)
166 {
167     jsstr_t *name;
168     BOOL ret;
169     HRESULT hres;
170
171     TRACE("\n");
172
173     if(argc != 1) {
174         FIXME("argc %d not supported\n", argc);
175         return E_NOTIMPL;
176     }
177
178     if(!is_jsdisp(jsthis)) {
179         FIXME("Host object this\n");
180         return E_FAIL;
181     }
182
183     hres = to_string(ctx, argv[0], &name);
184     if(FAILED(hres))
185         return hres;
186
187     hres = jsdisp_is_enumerable(jsthis->u.jsdisp, name->str, &ret);
188     jsstr_release(name);
189     if(FAILED(hres))
190         return hres;
191
192     if(r)
193         *r = jsval_bool(ret);
194     return S_OK;
195 }
196
197 static HRESULT Object_isPrototypeOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
198         jsval_t *r)
199 {
200     FIXME("\n");
201     return E_NOTIMPL;
202 }
203
204 static HRESULT Object_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
205         jsval_t *r)
206 {
207     TRACE("\n");
208
209     switch(flags) {
210     case INVOKE_FUNC:
211         return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
212     case DISPATCH_PROPERTYGET: {
213         jsstr_t *ret = jsstr_alloc(default_valueW);
214         if(!ret)
215             return E_OUTOFMEMORY;
216         *r = jsval_string(ret);
217         break;
218     }
219     default:
220         FIXME("unimplemented flags %x\n", flags);
221         return E_NOTIMPL;
222     }
223
224     return S_OK;
225 }
226
227 static void Object_destructor(jsdisp_t *dispex)
228 {
229     heap_free(dispex);
230 }
231
232 static const builtin_prop_t Object_props[] = {
233     {hasOwnPropertyW,        Object_hasOwnProperty,        PROPF_METHOD|1},
234     {isPrototypeOfW,         Object_isPrototypeOf,         PROPF_METHOD|1},
235     {propertyIsEnumerableW,  Object_propertyIsEnumerable,  PROPF_METHOD|1},
236     {toLocaleStringW,        Object_toLocaleString,        PROPF_METHOD},
237     {toStringW,              Object_toString,              PROPF_METHOD},
238     {valueOfW,               Object_valueOf,               PROPF_METHOD}
239 };
240
241 static const builtin_info_t Object_info = {
242     JSCLASS_OBJECT,
243     {NULL, Object_value, 0},
244     sizeof(Object_props)/sizeof(*Object_props),
245     Object_props,
246     Object_destructor,
247     NULL
248 };
249
250 static const builtin_info_t ObjectInst_info = {
251     JSCLASS_OBJECT,
252     {NULL, Object_value, 0},
253     0, NULL,
254     Object_destructor,
255     NULL
256 };
257
258 static HRESULT ObjectConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
259         jsval_t *r)
260 {
261     HRESULT hres;
262
263     TRACE("\n");
264
265     switch(flags) {
266     case DISPATCH_METHOD:
267         if(argc) {
268             if(!is_undefined(argv[0]) && !is_null(argv[0]) && (!is_object_instance(argv[0]) || get_object(argv[0]))) {
269                 IDispatch *disp;
270
271                 hres = to_object(ctx, argv[0], &disp);
272                 if(FAILED(hres))
273                     return hres;
274
275                 if(r)
276                     *r = jsval_disp(disp);
277                 else
278                     IDispatch_Release(disp);
279                 return S_OK;
280             }
281         }
282         /* fall through */
283     case DISPATCH_CONSTRUCT: {
284         jsdisp_t *obj;
285
286         hres = create_object(ctx, NULL, &obj);
287         if(FAILED(hres))
288             return hres;
289
290         if(r)
291             *r = jsval_obj(obj);
292         else
293             jsdisp_release(obj);
294         break;
295     }
296
297     default:
298         FIXME("unimplemented flags: %x\n", flags);
299         return E_NOTIMPL;
300     }
301
302     return S_OK;
303 }
304
305 HRESULT create_object_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
306 {
307     static const WCHAR ObjectW[] = {'O','b','j','e','c','t',0};
308
309     return create_builtin_constructor(ctx, ObjectConstr_value, ObjectW, NULL, PROPF_CONSTR,
310             object_prototype, ret);
311 }
312
313 HRESULT create_object_prototype(script_ctx_t *ctx, jsdisp_t **ret)
314 {
315     return create_dispex(ctx, &Object_info, NULL, ret);
316 }
317
318 HRESULT create_object(script_ctx_t *ctx, jsdisp_t *constr, jsdisp_t **ret)
319 {
320     jsdisp_t *object;
321     HRESULT hres;
322
323     object = heap_alloc_zero(sizeof(jsdisp_t));
324     if(!object)
325         return E_OUTOFMEMORY;
326
327     hres = init_dispex_from_constr(object, ctx, &ObjectInst_info, constr ? constr : ctx->object_constr);
328     if(FAILED(hres)) {
329         heap_free(object);
330         return hres;
331     }
332
333     *ret = object;
334     return S_OK;
335 }