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
, DISPPARAMS
*dp
,
48 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
50 VBArrayInstance
*vbarray
;
54 vbarray
= vbarray_this(vthis
);
56 return throw_type_error(ctx
, ei
, IDS_NOT_VBARRAY
, NULL
);
59 num_set_val(retv
, SafeArrayGetDim(vbarray
->safearray
));
63 static HRESULT
VBArray_getItem(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
64 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
70 static HRESULT
VBArray_lbound(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
71 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
73 VBArrayInstance
*vbarray
;
79 vbarray
= vbarray_this(vthis
);
81 return throw_type_error(ctx
, ei
, IDS_NOT_VBARRAY
, NULL
);
84 hres
= to_int32(ctx
, get_arg(dp
, 0), ei
, &dim
);
90 hres
= SafeArrayGetLBound(vbarray
->safearray
, dim
, &dim
);
91 if(hres
== DISP_E_BADINDEX
)
92 return throw_range_error(ctx
, ei
, IDS_SUBSCRIPT_OUT_OF_RANGE
, NULL
);
97 num_set_val(retv
, dim
);
101 static HRESULT
VBArray_toArray(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
102 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
108 static HRESULT
VBArray_ubound(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
109 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
111 VBArrayInstance
*vbarray
;
117 vbarray
= vbarray_this(vthis
);
119 return throw_type_error(ctx
, ei
, IDS_NOT_VBARRAY
, NULL
);
122 hres
= to_int32(ctx
, get_arg(dp
, 0), ei
, &dim
);
128 hres
= SafeArrayGetUBound(vbarray
->safearray
, dim
, &dim
);
129 if(hres
== DISP_E_BADINDEX
)
130 return throw_range_error(ctx
, ei
, IDS_SUBSCRIPT_OUT_OF_RANGE
, NULL
);
131 else if(FAILED(hres
))
135 num_set_val(retv
, dim
);
139 static HRESULT
VBArray_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
140 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
146 FIXME("unimplemented flags %x\n", flags
);
153 static void VBArray_destructor(jsdisp_t
*dispex
)
155 VBArrayInstance
*vbarray
= (VBArrayInstance
*)dispex
;
157 SafeArrayDestroy(vbarray
->safearray
);
161 static const builtin_prop_t VBArray_props
[] = {
162 {dimensionsW
, VBArray_dimensions
, PROPF_METHOD
},
163 {getItemW
, VBArray_getItem
, PROPF_METHOD
|1},
164 {lboundW
, VBArray_lbound
, PROPF_METHOD
},
165 {toArrayW
, VBArray_toArray
, PROPF_METHOD
},
166 {uboundW
, VBArray_ubound
, PROPF_METHOD
}
169 static const builtin_info_t VBArray_info
= {
171 {NULL
, VBArray_value
, 0},
172 sizeof(VBArray_props
)/sizeof(*VBArray_props
),
178 static HRESULT
alloc_vbarray(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, VBArrayInstance
**ret
)
180 VBArrayInstance
*vbarray
;
183 vbarray
= heap_alloc_zero(sizeof(VBArrayInstance
));
185 return E_OUTOFMEMORY
;
188 hres
= init_dispex(&vbarray
->dispex
, ctx
, &VBArray_info
, object_prototype
);
190 hres
= init_dispex_from_constr(&vbarray
->dispex
, ctx
, &VBArray_info
, ctx
->vbarray_constr
);
201 static HRESULT
VBArrayConstr_value(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
202 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
205 VBArrayInstance
*vbarray
;
211 case DISPATCH_METHOD
:
212 if(arg_cnt(dp
)<1 || V_VT((arg
= get_arg(dp
, 0)))!=(VT_ARRAY
|VT_VARIANT
))
213 return throw_type_error(ctx
, ei
, IDS_NOT_VBARRAY
, NULL
);
215 VariantCopy(retv
, arg
);
218 case DISPATCH_CONSTRUCT
:
219 if(arg_cnt(dp
)<1 || V_VT((arg
= get_arg(dp
, 0)))!=(VT_ARRAY
|VT_VARIANT
))
220 return throw_type_error(ctx
, ei
, IDS_NOT_VBARRAY
, NULL
);
222 hres
= alloc_vbarray(ctx
, NULL
, &vbarray
);
225 SafeArrayCopy(V_ARRAY(arg
), &vbarray
->safearray
);
227 var_set_jsdisp(retv
, &vbarray
->dispex
);
231 FIXME("unimplemented flags: %x\n", flags
);
238 HRESULT
create_vbarray_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
240 VBArrayInstance
*vbarray
;
243 static const WCHAR VBArrayW
[] = {'V','B','A','r','r','a','y',0};
245 hres
= alloc_vbarray(ctx
, object_prototype
, &vbarray
);
249 hres
= create_builtin_function(ctx
, VBArrayConstr_value
, VBArrayW
, NULL
, PROPF_CONSTR
|1, &vbarray
->dispex
, ret
);
251 jsdisp_release(&vbarray
->dispex
);