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 inline VBArrayInstance
*vbarray_from_jsdisp(jsdisp_t
*jsdisp
)
33 return CONTAINING_RECORD(jsdisp
, VBArrayInstance
, dispex
);
36 static inline VBArrayInstance
*vbarray_from_vdisp(vdisp_t
*vdisp
)
38 return vbarray_from_jsdisp(vdisp
->u
.jsdisp
);
41 static inline VBArrayInstance
*vbarray_this(vdisp_t
*jsthis
)
43 return is_vclass(jsthis
, JSCLASS_VBARRAY
) ? vbarray_from_vdisp(jsthis
) : NULL
;
46 static HRESULT
VBArray_dimensions(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
49 VBArrayInstance
*vbarray
;
53 vbarray
= vbarray_this(vthis
);
55 return JS_E_VBARRAY_EXPECTED
;
58 *r
= jsval_number(SafeArrayGetDim(vbarray
->safearray
));
62 static HRESULT
VBArray_getItem(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
65 VBArrayInstance
*vbarray
;
72 vbarray
= vbarray_this(vthis
);
74 return JS_E_VBARRAY_EXPECTED
;
76 if(argc
< SafeArrayGetDim(vbarray
->safearray
))
77 return JS_E_SUBSCRIPT_OUT_OF_RANGE
;
79 indexes
= heap_alloc(sizeof(int)*argc
);
83 for(i
=0; i
<argc
; i
++) {
84 hres
= to_int32(ctx
, argv
[i
], indexes
+i
);
91 hres
= SafeArrayGetElement(vbarray
->safearray
, indexes
, (void*)&out
);
93 if(hres
== DISP_E_BADINDEX
)
94 return JS_E_SUBSCRIPT_OUT_OF_RANGE
;
99 hres
= variant_to_jsval(&out
, r
);
105 static HRESULT
VBArray_lbound(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
108 VBArrayInstance
*vbarray
;
114 vbarray
= vbarray_this(vthis
);
116 return JS_E_VBARRAY_EXPECTED
;
119 hres
= to_int32(ctx
, argv
[0], &dim
);
125 hres
= SafeArrayGetLBound(vbarray
->safearray
, dim
, &dim
);
126 if(hres
== DISP_E_BADINDEX
)
127 return JS_E_SUBSCRIPT_OUT_OF_RANGE
;
128 else if(FAILED(hres
))
132 *r
= jsval_number(dim
);
136 static HRESULT
VBArray_toArray(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
139 VBArrayInstance
*vbarray
;
143 int i
, size
= 1, ubound
, lbound
;
148 vbarray
= vbarray_this(vthis
);
150 return JS_E_VBARRAY_EXPECTED
;
152 for(i
=1; i
<=SafeArrayGetDim(vbarray
->safearray
); i
++) {
153 SafeArrayGetLBound(vbarray
->safearray
, i
, &lbound
);
154 SafeArrayGetUBound(vbarray
->safearray
, i
, &ubound
);
155 size
*= ubound
-lbound
+1;
158 hres
= SafeArrayAccessData(vbarray
->safearray
, (void**)&v
);
162 hres
= create_array(ctx
, 0, &array
);
164 SafeArrayUnaccessData(vbarray
->safearray
);
168 for(i
=0; i
<size
; i
++) {
169 hres
= variant_to_jsval(v
, &val
);
170 if(SUCCEEDED(hres
)) {
171 hres
= jsdisp_propput_idx(array
, i
, val
);
175 SafeArrayUnaccessData(vbarray
->safearray
);
176 jsdisp_release(array
);
182 SafeArrayUnaccessData(vbarray
->safearray
);
185 *r
= jsval_obj(array
);
187 jsdisp_release(array
);
191 static HRESULT
VBArray_ubound(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
194 VBArrayInstance
*vbarray
;
200 vbarray
= vbarray_this(vthis
);
202 return JS_E_VBARRAY_EXPECTED
;
205 hres
= to_int32(ctx
, argv
[0], &dim
);
211 hres
= SafeArrayGetUBound(vbarray
->safearray
, dim
, &dim
);
212 if(hres
== DISP_E_BADINDEX
)
213 return JS_E_SUBSCRIPT_OUT_OF_RANGE
;
214 else if(FAILED(hres
))
218 *r
= jsval_number(dim
);
222 static HRESULT
VBArray_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
229 FIXME("unimplemented flags %x\n", flags
);
236 static void VBArray_destructor(jsdisp_t
*dispex
)
238 VBArrayInstance
*vbarray
= vbarray_from_jsdisp(dispex
);
240 SafeArrayDestroy(vbarray
->safearray
);
244 static const builtin_prop_t VBArray_props
[] = {
245 {L
"dimensions", VBArray_dimensions
, PROPF_METHOD
},
246 {L
"getItem", VBArray_getItem
, PROPF_METHOD
|1},
247 {L
"lbound", VBArray_lbound
, PROPF_METHOD
},
248 {L
"toArray", VBArray_toArray
, PROPF_METHOD
},
249 {L
"ubound", VBArray_ubound
, PROPF_METHOD
}
252 static const builtin_info_t VBArray_info
= {
254 {NULL
, VBArray_value
, 0},
255 ARRAY_SIZE(VBArray_props
),
261 static HRESULT
alloc_vbarray(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, VBArrayInstance
**ret
)
263 VBArrayInstance
*vbarray
;
266 vbarray
= heap_alloc_zero(sizeof(VBArrayInstance
));
268 return E_OUTOFMEMORY
;
271 hres
= init_dispex(&vbarray
->dispex
, ctx
, &VBArray_info
, object_prototype
);
273 hres
= init_dispex_from_constr(&vbarray
->dispex
, ctx
, &VBArray_info
, ctx
->vbarray_constr
);
284 static HRESULT
VBArrayConstr_value(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
287 VBArrayInstance
*vbarray
;
293 case DISPATCH_METHOD
:
294 if(argc
<1 || !is_variant(argv
[0]) || V_VT(get_variant(argv
[0])) != (VT_ARRAY
|VT_VARIANT
))
295 return JS_E_VBARRAY_EXPECTED
;
297 return jsval_copy(argv
[0], r
);
299 case DISPATCH_CONSTRUCT
:
300 if(argc
<1 || !is_variant(argv
[0]) || V_VT(get_variant(argv
[0])) != (VT_ARRAY
|VT_VARIANT
))
301 return JS_E_VBARRAY_EXPECTED
;
303 hres
= alloc_vbarray(ctx
, NULL
, &vbarray
);
307 hres
= SafeArrayCopy(V_ARRAY(get_variant(argv
[0])), &vbarray
->safearray
);
309 jsdisp_release(&vbarray
->dispex
);
313 *r
= jsval_obj(&vbarray
->dispex
);
317 FIXME("unimplemented flags: %x\n", flags
);
324 HRESULT
create_vbarray_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
326 VBArrayInstance
*vbarray
;
329 hres
= alloc_vbarray(ctx
, object_prototype
, &vbarray
);
333 hres
= create_builtin_constructor(ctx
, VBArrayConstr_value
, L
"VBArray", NULL
, PROPF_CONSTR
|1, &vbarray
->dispex
, ret
);
335 jsdisp_release(&vbarray
->dispex
);
339 HRESULT
create_vbarray(script_ctx_t
*ctx
, SAFEARRAY
*sa
, jsdisp_t
**ret
)
341 VBArrayInstance
*vbarray
;
344 hres
= alloc_vbarray(ctx
, NULL
, &vbarray
);
348 hres
= SafeArrayCopy(sa
, &vbarray
->safearray
);
350 jsdisp_release(&vbarray
->dispex
);
354 *ret
= &vbarray
->dispex
;