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
27 static const WCHAR dimensionsW
[] = {'d','i','m','e','n','s','i','o','n','s',0};
28 static const WCHAR getItemW
[] = {'g','e','t','I','t','e','m',0};
29 static const WCHAR lboundW
[] = {'l','b','o','u','n','d',0};
30 static const WCHAR toArrayW
[] = {'t','o','A','r','r','a','y',0};
31 static const WCHAR uboundW
[] = {'u','b','o','u','n','d',0};
33 static inline VBArrayInstance
*vbarray_from_vdisp(vdisp_t
*vdisp
)
35 return (VBArrayInstance
*)vdisp
->u
.jsdisp
;
38 static inline VBArrayInstance
*vbarray_this(vdisp_t
*jsthis
)
40 return is_vclass(jsthis
, JSCLASS_VBARRAY
) ? vbarray_from_vdisp(jsthis
) : NULL
;
43 static HRESULT
VBArray_dimensions(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
46 VBArrayInstance
*vbarray
;
50 vbarray
= vbarray_this(vthis
);
52 return throw_type_error(ctx
, JS_E_VBARRAY_EXPECTED
, NULL
);
55 *r
= jsval_number(SafeArrayGetDim(vbarray
->safearray
));
59 static HRESULT
VBArray_getItem(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
62 VBArrayInstance
*vbarray
;
69 vbarray
= vbarray_this(vthis
);
71 return throw_type_error(ctx
, JS_E_VBARRAY_EXPECTED
, NULL
);
73 if(argc
< SafeArrayGetDim(vbarray
->safearray
))
74 return throw_range_error(ctx
, JS_E_SUBSCRIPT_OUT_OF_RANGE
, NULL
);
76 indexes
= heap_alloc(sizeof(int)*argc
);
80 for(i
=0; i
<argc
; i
++) {
81 hres
= to_int32(ctx
, argv
[i
], indexes
+i
);
88 hres
= SafeArrayGetElement(vbarray
->safearray
, indexes
, (void*)&out
);
90 if(hres
== DISP_E_BADINDEX
)
91 return throw_range_error(ctx
, JS_E_SUBSCRIPT_OUT_OF_RANGE
, NULL
);
96 hres
= variant_to_jsval(&out
, r
);
102 static HRESULT
VBArray_lbound(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
105 VBArrayInstance
*vbarray
;
111 vbarray
= vbarray_this(vthis
);
113 return throw_type_error(ctx
, JS_E_VBARRAY_EXPECTED
, NULL
);
116 hres
= to_int32(ctx
, argv
[0], &dim
);
122 hres
= SafeArrayGetLBound(vbarray
->safearray
, dim
, &dim
);
123 if(hres
== DISP_E_BADINDEX
)
124 return throw_range_error(ctx
, JS_E_SUBSCRIPT_OUT_OF_RANGE
, NULL
);
125 else if(FAILED(hres
))
129 *r
= jsval_number(dim
);
133 static HRESULT
VBArray_toArray(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
136 VBArrayInstance
*vbarray
;
140 int i
, size
= 1, ubound
, lbound
;
145 vbarray
= vbarray_this(vthis
);
147 return throw_type_error(ctx
, JS_E_VBARRAY_EXPECTED
, NULL
);
149 for(i
=1; i
<=SafeArrayGetDim(vbarray
->safearray
); i
++) {
150 SafeArrayGetLBound(vbarray
->safearray
, i
, &lbound
);
151 SafeArrayGetUBound(vbarray
->safearray
, i
, &ubound
);
152 size
*= ubound
-lbound
+1;
155 hres
= SafeArrayAccessData(vbarray
->safearray
, (void**)&v
);
159 hres
= create_array(ctx
, 0, &array
);
161 SafeArrayUnaccessData(vbarray
->safearray
);
165 for(i
=0; i
<size
; i
++) {
166 hres
= variant_to_jsval(v
, &val
);
167 if(SUCCEEDED(hres
)) {
168 hres
= jsdisp_propput_idx(array
, i
, val
);
172 SafeArrayUnaccessData(vbarray
->safearray
);
173 jsdisp_release(array
);
179 SafeArrayUnaccessData(vbarray
->safearray
);
182 *r
= jsval_obj(array
);
184 jsdisp_release(array
);
188 static HRESULT
VBArray_ubound(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
191 VBArrayInstance
*vbarray
;
197 vbarray
= vbarray_this(vthis
);
199 return throw_type_error(ctx
, JS_E_VBARRAY_EXPECTED
, NULL
);
202 hres
= to_int32(ctx
, argv
[0], &dim
);
208 hres
= SafeArrayGetUBound(vbarray
->safearray
, dim
, &dim
);
209 if(hres
== DISP_E_BADINDEX
)
210 return throw_range_error(ctx
, JS_E_SUBSCRIPT_OUT_OF_RANGE
, NULL
);
211 else if(FAILED(hres
))
215 *r
= jsval_number(dim
);
219 static HRESULT
VBArray_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
226 FIXME("unimplemented flags %x\n", flags
);
233 static void VBArray_destructor(jsdisp_t
*dispex
)
235 VBArrayInstance
*vbarray
= (VBArrayInstance
*)dispex
;
237 SafeArrayDestroy(vbarray
->safearray
);
241 static const builtin_prop_t VBArray_props
[] = {
242 {dimensionsW
, VBArray_dimensions
, PROPF_METHOD
},
243 {getItemW
, VBArray_getItem
, PROPF_METHOD
|1},
244 {lboundW
, VBArray_lbound
, PROPF_METHOD
},
245 {toArrayW
, VBArray_toArray
, PROPF_METHOD
},
246 {uboundW
, VBArray_ubound
, PROPF_METHOD
}
249 static const builtin_info_t VBArray_info
= {
251 {NULL
, VBArray_value
, 0},
252 sizeof(VBArray_props
)/sizeof(*VBArray_props
),
258 static HRESULT
alloc_vbarray(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, VBArrayInstance
**ret
)
260 VBArrayInstance
*vbarray
;
263 vbarray
= heap_alloc_zero(sizeof(VBArrayInstance
));
265 return E_OUTOFMEMORY
;
268 hres
= init_dispex(&vbarray
->dispex
, ctx
, &VBArray_info
, object_prototype
);
270 hres
= init_dispex_from_constr(&vbarray
->dispex
, ctx
, &VBArray_info
, ctx
->vbarray_constr
);
281 static HRESULT
VBArrayConstr_value(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
284 VBArrayInstance
*vbarray
;
290 case DISPATCH_METHOD
:
291 if(argc
<1 || !is_variant(argv
[0]) || V_VT(get_variant(argv
[0])) != (VT_ARRAY
|VT_VARIANT
))
292 return throw_type_error(ctx
, JS_E_VBARRAY_EXPECTED
, NULL
);
294 return jsval_copy(argv
[0], r
);
296 case DISPATCH_CONSTRUCT
:
297 if(argc
<1 || !is_variant(argv
[0]) || V_VT(get_variant(argv
[0])) != (VT_ARRAY
|VT_VARIANT
))
298 return throw_type_error(ctx
, JS_E_VBARRAY_EXPECTED
, NULL
);
300 hres
= alloc_vbarray(ctx
, NULL
, &vbarray
);
304 hres
= SafeArrayCopy(V_ARRAY(get_variant(argv
[0])), &vbarray
->safearray
);
306 jsdisp_release(&vbarray
->dispex
);
310 *r
= jsval_obj(&vbarray
->dispex
);
314 FIXME("unimplemented flags: %x\n", flags
);
321 HRESULT
create_vbarray_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
323 VBArrayInstance
*vbarray
;
326 static const WCHAR VBArrayW
[] = {'V','B','A','r','r','a','y',0};
328 hres
= alloc_vbarray(ctx
, object_prototype
, &vbarray
);
332 hres
= create_builtin_constructor(ctx
, VBArrayConstr_value
, VBArrayW
, NULL
, PROPF_CONSTR
|1, &vbarray
->dispex
, ret
);
334 jsdisp_release(&vbarray
->dispex
);
338 HRESULT
create_vbarray(script_ctx_t
*ctx
, SAFEARRAY
*sa
, jsdisp_t
**ret
)
340 VBArrayInstance
*vbarray
;
343 hres
= alloc_vbarray(ctx
, NULL
, &vbarray
);
347 hres
= SafeArrayCopy(sa
, &vbarray
->safearray
);
349 jsdisp_release(&vbarray
->dispex
);
353 *ret
= &vbarray
->dispex
;