jscript: Added new variable representation and use it for internal function return...
[wine.git] / dlls / jscript / vbarray.c
bloba6e7412fc52ddf3e02b78603ec5c41f5d39b8d9c
1 /*
2 * Copyright 2010 Piotr Caban for CodeWeavers
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.
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.
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
19 #include "jscript.h"
21 #include "wine/debug.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
25 typedef struct {
26 jsdisp_t dispex;
28 SAFEARRAY *safearray;
29 } VBArrayInstance;
31 static const WCHAR dimensionsW[] = {'d','i','m','e','n','s','i','o','n','s',0};
32 static const WCHAR getItemW[] = {'g','e','t','I','t','e','m',0};
33 static const WCHAR lboundW[] = {'l','b','o','u','n','d',0};
34 static const WCHAR toArrayW[] = {'t','o','A','r','r','a','y',0};
35 static const WCHAR uboundW[] = {'u','b','o','u','n','d',0};
37 static inline VBArrayInstance *vbarray_from_vdisp(vdisp_t *vdisp)
39 return (VBArrayInstance*)vdisp->u.jsdisp;
42 static inline VBArrayInstance *vbarray_this(vdisp_t *jsthis)
44 return is_vclass(jsthis, JSCLASS_VBARRAY) ? vbarray_from_vdisp(jsthis) : NULL;
47 static HRESULT VBArray_dimensions(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
48 jsval_t *r, jsexcept_t *ei)
50 VBArrayInstance *vbarray;
52 TRACE("\n");
54 vbarray = vbarray_this(vthis);
55 if(!vbarray)
56 return throw_type_error(ctx, ei, JS_E_VBARRAY_EXPECTED, NULL);
58 if(r)
59 *r = jsval_number(SafeArrayGetDim(vbarray->safearray));
60 return S_OK;
63 static HRESULT VBArray_getItem(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
64 jsval_t *r, jsexcept_t *ei)
66 VBArrayInstance *vbarray;
67 int i, *indexes;
68 VARIANT out;
69 HRESULT hres;
71 TRACE("\n");
73 vbarray = vbarray_this(vthis);
74 if(!vbarray)
75 return throw_type_error(ctx, ei, JS_E_VBARRAY_EXPECTED, NULL);
77 if(argc < SafeArrayGetDim(vbarray->safearray))
78 return throw_range_error(ctx, ei, JS_E_SUBSCRIPT_OUT_OF_RANGE, NULL);
80 indexes = heap_alloc(sizeof(int)*argc);
81 if(!indexes)
82 return E_OUTOFMEMORY;
84 for(i=0; i<argc; i++) {
85 hres = to_int32(ctx, argv+i, ei, indexes+i);
86 if(FAILED(hres)) {
87 heap_free(indexes);
88 return hres;
92 hres = SafeArrayGetElement(vbarray->safearray, indexes, (void*)&out);
93 heap_free(indexes);
94 if(hres == DISP_E_BADINDEX)
95 return throw_range_error(ctx, ei, JS_E_SUBSCRIPT_OUT_OF_RANGE, NULL);
96 else if(FAILED(hres))
97 return hres;
99 if(r)
100 hres = jsval_variant(r, &out);
101 return hres;
104 static HRESULT VBArray_lbound(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
105 jsval_t *r, jsexcept_t *ei)
107 VBArrayInstance *vbarray;
108 int dim;
109 HRESULT hres;
111 TRACE("\n");
113 vbarray = vbarray_this(vthis);
114 if(!vbarray)
115 return throw_type_error(ctx, ei, JS_E_VBARRAY_EXPECTED, NULL);
117 if(argc) {
118 hres = to_int32(ctx, argv, ei, &dim);
119 if(FAILED(hres))
120 return hres;
121 } else
122 dim = 1;
124 hres = SafeArrayGetLBound(vbarray->safearray, dim, &dim);
125 if(hres == DISP_E_BADINDEX)
126 return throw_range_error(ctx, ei, JS_E_SUBSCRIPT_OUT_OF_RANGE, NULL);
127 else if(FAILED(hres))
128 return hres;
130 if(r)
131 *r = jsval_number(dim);
132 return S_OK;
135 static HRESULT VBArray_toArray(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
136 jsval_t *r, jsexcept_t *ei)
138 VBArrayInstance *vbarray;
139 jsdisp_t *array;
140 VARIANT *v;
141 int i, size = 1, ubound, lbound;
142 HRESULT hres;
144 TRACE("\n");
146 vbarray = vbarray_this(vthis);
147 if(!vbarray)
148 return throw_type_error(ctx, ei, JS_E_VBARRAY_EXPECTED, NULL);
150 for(i=1; i<=SafeArrayGetDim(vbarray->safearray); i++) {
151 SafeArrayGetLBound(vbarray->safearray, i, &lbound);
152 SafeArrayGetUBound(vbarray->safearray, i, &ubound);
153 size *= ubound-lbound+1;
156 hres = SafeArrayAccessData(vbarray->safearray, (void**)&v);
157 if(FAILED(hres))
158 return hres;
160 hres = create_array(ctx, 0, &array);
161 if(FAILED(hres)) {
162 SafeArrayUnaccessData(vbarray->safearray);
163 return hres;
166 for(i=0; i<size; i++) {
167 hres = jsdisp_propput_idx(array, i, v, ei);
168 if(FAILED(hres)) {
169 SafeArrayUnaccessData(vbarray->safearray);
170 jsdisp_release(array);
171 return hres;
173 v++;
176 SafeArrayUnaccessData(vbarray->safearray);
178 if(r)
179 *r = jsval_obj(array);
180 return S_OK;
183 static HRESULT VBArray_ubound(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
184 jsval_t *r, jsexcept_t *ei)
186 VBArrayInstance *vbarray;
187 int dim;
188 HRESULT hres;
190 TRACE("\n");
192 vbarray = vbarray_this(vthis);
193 if(!vbarray)
194 return throw_type_error(ctx, ei, JS_E_VBARRAY_EXPECTED, NULL);
196 if(argc) {
197 hres = to_int32(ctx, argv, ei, &dim);
198 if(FAILED(hres))
199 return hres;
200 } else
201 dim = 1;
203 hres = SafeArrayGetUBound(vbarray->safearray, dim, &dim);
204 if(hres == DISP_E_BADINDEX)
205 return throw_range_error(ctx, ei, JS_E_SUBSCRIPT_OUT_OF_RANGE, NULL);
206 else if(FAILED(hres))
207 return hres;
209 if(r)
210 *r = jsval_number(dim);
211 return S_OK;
214 static HRESULT VBArray_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
215 jsval_t *r, jsexcept_t *ei)
217 FIXME("\n");
219 switch(flags) {
220 default:
221 FIXME("unimplemented flags %x\n", flags);
222 return E_NOTIMPL;
225 return S_OK;
228 static void VBArray_destructor(jsdisp_t *dispex)
230 VBArrayInstance *vbarray = (VBArrayInstance*)dispex;
232 SafeArrayDestroy(vbarray->safearray);
233 heap_free(vbarray);
236 static const builtin_prop_t VBArray_props[] = {
237 {dimensionsW, VBArray_dimensions, PROPF_METHOD},
238 {getItemW, VBArray_getItem, PROPF_METHOD|1},
239 {lboundW, VBArray_lbound, PROPF_METHOD},
240 {toArrayW, VBArray_toArray, PROPF_METHOD},
241 {uboundW, VBArray_ubound, PROPF_METHOD}
244 static const builtin_info_t VBArray_info = {
245 JSCLASS_VBARRAY,
246 {NULL, VBArray_value, 0},
247 sizeof(VBArray_props)/sizeof(*VBArray_props),
248 VBArray_props,
249 VBArray_destructor,
250 NULL
253 static HRESULT alloc_vbarray(script_ctx_t *ctx, jsdisp_t *object_prototype, VBArrayInstance **ret)
255 VBArrayInstance *vbarray;
256 HRESULT hres;
258 vbarray = heap_alloc_zero(sizeof(VBArrayInstance));
259 if(!vbarray)
260 return E_OUTOFMEMORY;
262 if(object_prototype)
263 hres = init_dispex(&vbarray->dispex, ctx, &VBArray_info, object_prototype);
264 else
265 hres = init_dispex_from_constr(&vbarray->dispex, ctx, &VBArray_info, ctx->vbarray_constr);
267 if(FAILED(hres)) {
268 heap_free(vbarray);
269 return hres;
272 *ret = vbarray;
273 return S_OK;
276 static HRESULT VBArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
277 jsval_t *r, jsexcept_t *ei)
279 VBArrayInstance *vbarray;
280 HRESULT hres;
282 TRACE("\n");
284 switch(flags) {
285 case DISPATCH_METHOD:
286 if(argc<1 || V_VT(argv) != (VT_ARRAY|VT_VARIANT))
287 return throw_type_error(ctx, ei, JS_E_VBARRAY_EXPECTED, NULL);
289 return variant_to_jsval(argv, r);
291 case DISPATCH_CONSTRUCT:
292 if(argc<1 || V_VT(argv) != (VT_ARRAY|VT_VARIANT))
293 return throw_type_error(ctx, ei, JS_E_VBARRAY_EXPECTED, NULL);
295 hres = alloc_vbarray(ctx, NULL, &vbarray);
296 if(FAILED(hres))
297 return hres;
299 hres = SafeArrayCopy(V_ARRAY(argv), &vbarray->safearray);
300 if(FAILED(hres)) {
301 jsdisp_release(&vbarray->dispex);
302 return hres;
305 *r = jsval_obj(&vbarray->dispex);
306 break;
308 default:
309 FIXME("unimplemented flags: %x\n", flags);
310 return E_NOTIMPL;
313 return S_OK;
316 HRESULT create_vbarray_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
318 VBArrayInstance *vbarray;
319 HRESULT hres;
321 static const WCHAR VBArrayW[] = {'V','B','A','r','r','a','y',0};
323 hres = alloc_vbarray(ctx, object_prototype, &vbarray);
324 if(FAILED(hres))
325 return hres;
327 hres = create_builtin_constructor(ctx, VBArrayConstr_value, VBArrayW, NULL, PROPF_CONSTR|1, &vbarray->dispex, ret);
329 jsdisp_release(&vbarray->dispex);
330 return hres;
333 HRESULT create_vbarray(script_ctx_t *ctx, SAFEARRAY *sa, jsdisp_t **ret)
335 VBArrayInstance *vbarray;
336 HRESULT hres;
338 hres = alloc_vbarray(ctx, NULL, &vbarray);
339 if(FAILED(hres))
340 return hres;
342 hres = SafeArrayCopy(sa, &vbarray->safearray);
343 if(FAILED(hres)) {
344 jsdisp_release(&vbarray->dispex);
345 return hres;
348 *ret = &vbarray->dispex;
349 return S_OK;