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_jsdisp(jsdisp_t
*jsdisp
)
35 return CONTAINING_RECORD(jsdisp
, VBArrayInstance
, dispex
);
38 static inline VBArrayInstance
*vbarray_from_vdisp(vdisp_t
*vdisp
)
40 return vbarray_from_jsdisp(vdisp
->u
.jsdisp
);
43 static inline VBArrayInstance
*vbarray_this(vdisp_t
*jsthis
)
45 return is_vclass(jsthis
, JSCLASS_VBARRAY
) ? vbarray_from_vdisp(jsthis
) : NULL
;
48 static HRESULT
VBArray_dimensions(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
51 VBArrayInstance
*vbarray
;
55 vbarray
= vbarray_this(vthis
);
57 return throw_type_error(ctx
, JS_E_VBARRAY_EXPECTED
, NULL
);
60 *r
= jsval_number(SafeArrayGetDim(vbarray
->safearray
));
64 static HRESULT
VBArray_getItem(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
67 VBArrayInstance
*vbarray
;
74 vbarray
= vbarray_this(vthis
);
76 return throw_type_error(ctx
, JS_E_VBARRAY_EXPECTED
, NULL
);
78 if(argc
< SafeArrayGetDim(vbarray
->safearray
))
79 return throw_range_error(ctx
, JS_E_SUBSCRIPT_OUT_OF_RANGE
, NULL
);
81 indexes
= heap_alloc(sizeof(int)*argc
);
85 for(i
=0; i
<argc
; i
++) {
86 hres
= to_int32(ctx
, argv
[i
], indexes
+i
);
93 hres
= SafeArrayGetElement(vbarray
->safearray
, indexes
, (void*)&out
);
95 if(hres
== DISP_E_BADINDEX
)
96 return throw_range_error(ctx
, JS_E_SUBSCRIPT_OUT_OF_RANGE
, NULL
);
101 hres
= variant_to_jsval(&out
, r
);
107 static HRESULT
VBArray_lbound(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
110 VBArrayInstance
*vbarray
;
116 vbarray
= vbarray_this(vthis
);
118 return throw_type_error(ctx
, JS_E_VBARRAY_EXPECTED
, NULL
);
121 hres
= to_int32(ctx
, argv
[0], &dim
);
127 hres
= SafeArrayGetLBound(vbarray
->safearray
, dim
, &dim
);
128 if(hres
== DISP_E_BADINDEX
)
129 return throw_range_error(ctx
, JS_E_SUBSCRIPT_OUT_OF_RANGE
, NULL
);
130 else if(FAILED(hres
))
134 *r
= jsval_number(dim
);
138 static HRESULT
VBArray_toArray(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
141 VBArrayInstance
*vbarray
;
145 int i
, size
= 1, ubound
, lbound
;
150 vbarray
= vbarray_this(vthis
);
152 return throw_type_error(ctx
, JS_E_VBARRAY_EXPECTED
, NULL
);
154 for(i
=1; i
<=SafeArrayGetDim(vbarray
->safearray
); i
++) {
155 SafeArrayGetLBound(vbarray
->safearray
, i
, &lbound
);
156 SafeArrayGetUBound(vbarray
->safearray
, i
, &ubound
);
157 size
*= ubound
-lbound
+1;
160 hres
= SafeArrayAccessData(vbarray
->safearray
, (void**)&v
);
164 hres
= create_array(ctx
, 0, &array
);
166 SafeArrayUnaccessData(vbarray
->safearray
);
170 for(i
=0; i
<size
; i
++) {
171 hres
= variant_to_jsval(v
, &val
);
172 if(SUCCEEDED(hres
)) {
173 hres
= jsdisp_propput_idx(array
, i
, val
);
177 SafeArrayUnaccessData(vbarray
->safearray
);
178 jsdisp_release(array
);
184 SafeArrayUnaccessData(vbarray
->safearray
);
187 *r
= jsval_obj(array
);
189 jsdisp_release(array
);
193 static HRESULT
VBArray_ubound(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
196 VBArrayInstance
*vbarray
;
202 vbarray
= vbarray_this(vthis
);
204 return throw_type_error(ctx
, JS_E_VBARRAY_EXPECTED
, NULL
);
207 hres
= to_int32(ctx
, argv
[0], &dim
);
213 hres
= SafeArrayGetUBound(vbarray
->safearray
, dim
, &dim
);
214 if(hres
== DISP_E_BADINDEX
)
215 return throw_range_error(ctx
, JS_E_SUBSCRIPT_OUT_OF_RANGE
, NULL
);
216 else if(FAILED(hres
))
220 *r
= jsval_number(dim
);
224 static HRESULT
VBArray_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
231 FIXME("unimplemented flags %x\n", flags
);
238 static void VBArray_destructor(jsdisp_t
*dispex
)
240 VBArrayInstance
*vbarray
= vbarray_from_jsdisp(dispex
);
242 SafeArrayDestroy(vbarray
->safearray
);
246 static const builtin_prop_t VBArray_props
[] = {
247 {dimensionsW
, VBArray_dimensions
, PROPF_METHOD
},
248 {getItemW
, VBArray_getItem
, PROPF_METHOD
|1},
249 {lboundW
, VBArray_lbound
, PROPF_METHOD
},
250 {toArrayW
, VBArray_toArray
, PROPF_METHOD
},
251 {uboundW
, VBArray_ubound
, PROPF_METHOD
}
254 static const builtin_info_t VBArray_info
= {
256 {NULL
, VBArray_value
, 0},
257 sizeof(VBArray_props
)/sizeof(*VBArray_props
),
263 static HRESULT
alloc_vbarray(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, VBArrayInstance
**ret
)
265 VBArrayInstance
*vbarray
;
268 vbarray
= heap_alloc_zero(sizeof(VBArrayInstance
));
270 return E_OUTOFMEMORY
;
273 hres
= init_dispex(&vbarray
->dispex
, ctx
, &VBArray_info
, object_prototype
);
275 hres
= init_dispex_from_constr(&vbarray
->dispex
, ctx
, &VBArray_info
, ctx
->vbarray_constr
);
286 static HRESULT
VBArrayConstr_value(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
289 VBArrayInstance
*vbarray
;
295 case DISPATCH_METHOD
:
296 if(argc
<1 || !is_variant(argv
[0]) || V_VT(get_variant(argv
[0])) != (VT_ARRAY
|VT_VARIANT
))
297 return throw_type_error(ctx
, JS_E_VBARRAY_EXPECTED
, NULL
);
299 return jsval_copy(argv
[0], r
);
301 case DISPATCH_CONSTRUCT
:
302 if(argc
<1 || !is_variant(argv
[0]) || V_VT(get_variant(argv
[0])) != (VT_ARRAY
|VT_VARIANT
))
303 return throw_type_error(ctx
, JS_E_VBARRAY_EXPECTED
, NULL
);
305 hres
= alloc_vbarray(ctx
, NULL
, &vbarray
);
309 hres
= SafeArrayCopy(V_ARRAY(get_variant(argv
[0])), &vbarray
->safearray
);
311 jsdisp_release(&vbarray
->dispex
);
315 *r
= jsval_obj(&vbarray
->dispex
);
319 FIXME("unimplemented flags: %x\n", flags
);
326 HRESULT
create_vbarray_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
328 VBArrayInstance
*vbarray
;
331 static const WCHAR VBArrayW
[] = {'V','B','A','r','r','a','y',0};
333 hres
= alloc_vbarray(ctx
, object_prototype
, &vbarray
);
337 hres
= create_builtin_constructor(ctx
, VBArrayConstr_value
, VBArrayW
, NULL
, PROPF_CONSTR
|1, &vbarray
->dispex
, ret
);
339 jsdisp_release(&vbarray
->dispex
);
343 HRESULT
create_vbarray(script_ctx_t
*ctx
, SAFEARRAY
*sa
, jsdisp_t
**ret
)
345 VBArrayInstance
*vbarray
;
348 hres
= alloc_vbarray(ctx
, NULL
, &vbarray
);
352 hres
= SafeArrayCopy(sa
, &vbarray
->safearray
);
354 jsdisp_release(&vbarray
->dispex
);
358 *ret
= &vbarray
->dispex
;