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
21 #include "wine/debug.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
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 VARIANT *retv, jsexcept_t *ei)
50 VBArrayInstance *vbarray;
54 vbarray = vbarray_this(vthis);
56 return throw_type_error(ctx, ei, JS_E_VBARRAY_EXPECTED, NULL);
59 num_set_int(retv, SafeArrayGetDim(vbarray->safearray));
63 static HRESULT VBArray_getItem(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
64 VARIANT *retv, jsexcept_t *ei)
66 VBArrayInstance *vbarray;
73 vbarray = vbarray_this(vthis);
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);
84 for(i=0; i<argc; i++) {
85 hres = to_int32(ctx, argv+i, ei, indexes+i);
92 hres = SafeArrayGetElement(vbarray->safearray, indexes, (void*)&out);
94 if(hres == DISP_E_BADINDEX)
95 return throw_range_error(ctx, ei, JS_E_SUBSCRIPT_OUT_OF_RANGE, NULL);
100 hres = VariantCopy(retv, &out);
105 static HRESULT VBArray_lbound(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
106 VARIANT *retv, jsexcept_t *ei)
108 VBArrayInstance *vbarray;
114 vbarray = vbarray_this(vthis);
116 return throw_type_error(ctx, ei, JS_E_VBARRAY_EXPECTED, NULL);
119 hres = to_int32(ctx, argv, ei, &dim);
125 hres = SafeArrayGetLBound(vbarray->safearray, dim, &dim);
126 if(hres == DISP_E_BADINDEX)
127 return throw_range_error(ctx, ei, JS_E_SUBSCRIPT_OUT_OF_RANGE, NULL);
128 else if(FAILED(hres))
132 num_set_int(retv, dim);
136 static HRESULT VBArray_toArray(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
137 VARIANT *retv, jsexcept_t *ei)
139 VBArrayInstance *vbarray;
142 int i, size = 1, ubound, lbound;
147 vbarray = vbarray_this(vthis);
149 return throw_type_error(ctx, ei, JS_E_VBARRAY_EXPECTED, NULL);
151 for(i=1; i<=SafeArrayGetDim(vbarray->safearray); i++) {
152 SafeArrayGetLBound(vbarray->safearray, i, &lbound);
153 SafeArrayGetUBound(vbarray->safearray, i, &ubound);
154 size *= ubound-lbound+1;
157 hres = SafeArrayAccessData(vbarray->safearray, (void**)&v);
161 hres = create_array(ctx, 0, &array);
163 SafeArrayUnaccessData(vbarray->safearray);
167 for(i=0; i<size; i++) {
168 hres = jsdisp_propput_idx(array, i, v, ei);
170 SafeArrayUnaccessData(vbarray->safearray);
171 jsdisp_release(array);
177 SafeArrayUnaccessData(vbarray->safearray);
180 var_set_jsdisp(retv, array);
184 static HRESULT VBArray_ubound(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
185 VARIANT *retv, jsexcept_t *ei)
187 VBArrayInstance *vbarray;
193 vbarray = vbarray_this(vthis);
195 return throw_type_error(ctx, ei, JS_E_VBARRAY_EXPECTED, NULL);
198 hres = to_int32(ctx, argv, ei, &dim);
204 hres = SafeArrayGetUBound(vbarray->safearray, dim, &dim);
205 if(hres == DISP_E_BADINDEX)
206 return throw_range_error(ctx, ei, JS_E_SUBSCRIPT_OUT_OF_RANGE, NULL);
207 else if(FAILED(hres))
211 num_set_int(retv, dim);
215 static HRESULT VBArray_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
216 VARIANT *retv, jsexcept_t *ei)
222 FIXME("unimplemented flags %x\n", flags);
229 static void VBArray_destructor(jsdisp_t *dispex)
231 VBArrayInstance *vbarray = (VBArrayInstance*)dispex;
233 SafeArrayDestroy(vbarray->safearray);
237 static const builtin_prop_t VBArray_props[] = {
238 {dimensionsW, VBArray_dimensions, PROPF_METHOD},
239 {getItemW, VBArray_getItem, PROPF_METHOD|1},
240 {lboundW, VBArray_lbound, PROPF_METHOD},
241 {toArrayW, VBArray_toArray, PROPF_METHOD},
242 {uboundW, VBArray_ubound, PROPF_METHOD}
245 static const builtin_info_t VBArray_info = {
247 {NULL, VBArray_value, 0},
248 sizeof(VBArray_props)/sizeof(*VBArray_props),
254 static HRESULT alloc_vbarray(script_ctx_t *ctx, jsdisp_t *object_prototype, VBArrayInstance **ret)
256 VBArrayInstance *vbarray;
259 vbarray = heap_alloc_zero(sizeof(VBArrayInstance));
261 return E_OUTOFMEMORY;
264 hres = init_dispex(&vbarray->dispex, ctx, &VBArray_info, object_prototype);
266 hres = init_dispex_from_constr(&vbarray->dispex, ctx, &VBArray_info, ctx->vbarray_constr);
277 static HRESULT VBArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
278 VARIANT *retv, jsexcept_t *ei)
280 VBArrayInstance *vbarray;
286 case DISPATCH_METHOD:
287 if(argc<1 || V_VT(argv) != (VT_ARRAY|VT_VARIANT))
288 return throw_type_error(ctx, ei, JS_E_VBARRAY_EXPECTED, NULL);
290 hres = VariantCopy(retv, argv);
293 case DISPATCH_CONSTRUCT:
294 if(argc<1 || V_VT(argv) != (VT_ARRAY|VT_VARIANT))
295 return throw_type_error(ctx, ei, JS_E_VBARRAY_EXPECTED, NULL);
297 hres = alloc_vbarray(ctx, NULL, &vbarray);
301 hres = SafeArrayCopy(V_ARRAY(argv), &vbarray->safearray);
303 jsdisp_release(&vbarray->dispex);
307 var_set_jsdisp(retv, &vbarray->dispex);
311 FIXME("unimplemented flags: %x\n", flags);
318 HRESULT create_vbarray_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
320 VBArrayInstance *vbarray;
323 static const WCHAR VBArrayW[] = {'V','B','A','r','r','a','y',0};
325 hres = alloc_vbarray(ctx, object_prototype, &vbarray);
329 hres = create_builtin_constructor(ctx, VBArrayConstr_value, VBArrayW, NULL, PROPF_CONSTR|1, &vbarray->dispex, ret);
331 jsdisp_release(&vbarray->dispex);
335 HRESULT create_vbarray(script_ctx_t *ctx, SAFEARRAY *sa, jsdisp_t **ret)
337 VBArrayInstance *vbarray;
340 hres = alloc_vbarray(ctx, NULL, &vbarray);
344 hres = SafeArrayCopy(sa, &vbarray->safearray);
346 jsdisp_release(&vbarray->dispex);
350 *ret = &vbarray->dispex;