jscript: Added Array constructor implementation.
[wine] / dlls / jscript / array.c
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "jscript.h"
20
21 #include "wine/debug.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
24
25 typedef struct {
26     DispatchEx dispex;
27
28     DWORD length;
29 } ArrayInstance;
30
31 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
32 static const WCHAR concatW[] = {'c','o','n','c','a','t',0};
33 static const WCHAR joinW[] = {'j','o','i','n',0};
34 static const WCHAR popW[] = {'p','o','p',0};
35 static const WCHAR pushW[] = {'p','u','s','h',0};
36 static const WCHAR reverseW[] = {'r','e','v','e','r','s','e',0};
37 static const WCHAR shiftW[] = {'s','h','i','f','t',0};
38 static const WCHAR sliceW[] = {'s','l','i','c','e',0};
39 static const WCHAR sortW[] = {'s','o','r','t',0};
40 static const WCHAR spliceW[] = {'s','p','l','i','c','e',0};
41 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
42 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
43 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
44 static const WCHAR unshiftW[] = {'u','n','s','h','i','f','t',0};
45 static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
46 static const WCHAR propertyIsEnumerableW[] =
47     {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
48 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
49
50
51 static HRESULT Array_length(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
52         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
53 {
54     FIXME("\n");
55     return E_NOTIMPL;
56 }
57
58 static HRESULT Array_concat(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
59         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
60 {
61     FIXME("\n");
62     return E_NOTIMPL;
63 }
64
65 static HRESULT Array_join(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
66         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
67 {
68     FIXME("\n");
69     return E_NOTIMPL;
70 }
71
72 static HRESULT Array_pop(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
73         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
74 {
75     FIXME("\n");
76     return E_NOTIMPL;
77 }
78
79 static HRESULT Array_push(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
80         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
81 {
82     FIXME("\n");
83     return E_NOTIMPL;
84 }
85
86 static HRESULT Array_reverse(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
87         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
88 {
89     FIXME("\n");
90     return E_NOTIMPL;
91 }
92
93 static HRESULT Array_shift(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
94         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
95 {
96     FIXME("\n");
97     return E_NOTIMPL;
98 }
99
100 static HRESULT Array_slice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
101         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
102 {
103     FIXME("\n");
104     return E_NOTIMPL;
105 }
106
107 static HRESULT Array_sort(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
108         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
109 {
110     FIXME("\n");
111     return E_NOTIMPL;
112 }
113
114 static HRESULT Array_splice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
115         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
116 {
117     FIXME("\n");
118     return E_NOTIMPL;
119 }
120
121 static HRESULT Array_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
122         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
123 {
124     FIXME("\n");
125     return E_NOTIMPL;
126 }
127
128 static HRESULT Array_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
129         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
130 {
131     FIXME("\n");
132     return E_NOTIMPL;
133 }
134
135 static HRESULT Array_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
136         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
137 {
138     FIXME("\n");
139     return E_NOTIMPL;
140 }
141
142 static HRESULT Array_unshift(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
143         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
144 {
145     FIXME("\n");
146     return E_NOTIMPL;
147 }
148
149 static HRESULT Array_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
150         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
151 {
152     FIXME("\n");
153     return E_NOTIMPL;
154 }
155
156 static HRESULT Array_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
157         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
158 {
159     FIXME("\n");
160     return E_NOTIMPL;
161 }
162
163 static HRESULT Array_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
164         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
165 {
166     FIXME("\n");
167     return E_NOTIMPL;
168 }
169
170 static HRESULT Array_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
171         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
172 {
173     FIXME("\n");
174     return E_NOTIMPL;
175 }
176
177 static void Array_destructor(DispatchEx *dispex)
178 {
179     heap_free(dispex);
180 }
181
182 static void Array_on_put(DispatchEx *dispex, const WCHAR *name)
183 {
184     FIXME("\n");
185 }
186
187 static const builtin_prop_t Array_props[] = {
188     {concatW,                Array_concat,               PROPF_METHOD},
189     {hasOwnPropertyW,        Array_hasOwnProperty,       PROPF_METHOD},
190     {isPrototypeOfW,         Array_isPrototypeOf,        PROPF_METHOD},
191     {joinW,                  Array_join,                 PROPF_METHOD},
192     {lengthW,                Array_length,               0},
193     {popW,                   Array_pop,                  PROPF_METHOD},
194     {propertyIsEnumerableW,  Array_propertyIsEnumerable, PROPF_METHOD},
195     {pushW,                  Array_push,                 PROPF_METHOD},
196     {reverseW,               Array_reverse,              PROPF_METHOD},
197     {shiftW,                 Array_shift,                PROPF_METHOD},
198     {sliceW,                 Array_slice,                PROPF_METHOD},
199     {sortW,                  Array_sort,                 PROPF_METHOD},
200     {spliceW,                Array_splice,               PROPF_METHOD},
201     {toLocaleStringW,        Array_toLocaleString,       PROPF_METHOD},
202     {toStringW,              Array_toString,             PROPF_METHOD},
203     {unshiftW,               Array_unshift,              PROPF_METHOD},
204     {valueOfW,               Array_valueOf,              PROPF_METHOD}
205 };
206
207 static const builtin_info_t Array_info = {
208     JSCLASS_ARRAY,
209     {NULL, Array_value, 0},
210     sizeof(Array_props)/sizeof(*Array_props),
211     Array_props,
212     Array_destructor,
213     Array_on_put
214 };
215
216 static HRESULT ArrayConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
217         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
218 {
219     DispatchEx *obj;
220     VARIANT *arg_var;
221     DWORD i;
222     HRESULT hres;
223
224     TRACE("\n");
225
226     switch(flags) {
227     case DISPATCH_CONSTRUCT: {
228         if(arg_cnt(dp) == 1 && V_VT((arg_var = get_arg(dp, 0))) == VT_I4) {
229             if(V_I4(arg_var) < 0) {
230                 FIXME("throw RangeError\n");
231                 return E_FAIL;
232             }
233
234             hres = create_array(dispex->ctx, V_I4(arg_var), &obj);
235             if(FAILED(hres))
236                 return hres;
237
238             V_VT(retv) = VT_DISPATCH;
239             V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
240             return S_OK;
241         }
242
243         hres = create_array(dispex->ctx, arg_cnt(dp), &obj);
244         if(FAILED(hres))
245             return hres;
246
247         for(i=0; i < arg_cnt(dp); i++) {
248             hres = jsdisp_propput_idx(obj, i, lcid, get_arg(dp, i), ei, caller);
249             if(FAILED(hres))
250                 break;
251         }
252         if(FAILED(hres)) {
253             jsdisp_release(obj);
254             return hres;
255         }
256
257         V_VT(retv) = VT_DISPATCH;
258         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
259         break;
260     }
261     default:
262         FIXME("unimplemented flags: %x\n", flags);
263         return E_NOTIMPL;
264     }
265
266     return S_OK;
267 }
268
269 static HRESULT alloc_array(script_ctx_t *ctx, BOOL use_constr, ArrayInstance **ret)
270 {
271     ArrayInstance *array = heap_alloc_zero(sizeof(ArrayInstance));
272     HRESULT hres;
273
274     if(use_constr)
275         hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->array_constr);
276     else
277         hres = init_dispex(&array->dispex, ctx, &Array_info, NULL);
278
279     if(FAILED(hres)) {
280         heap_free(array);
281         return hres;
282     }
283
284     *ret = array;
285     return S_OK;
286 }
287
288 HRESULT create_array_constr(script_ctx_t *ctx, DispatchEx **ret)
289 {
290     ArrayInstance *array;
291     HRESULT hres;
292
293     hres = alloc_array(ctx, FALSE, &array);
294     if(FAILED(hres))
295         return hres;
296
297     hres = create_builtin_function(ctx, ArrayConstr_value, PROPF_CONSTR, &array->dispex, ret);
298
299     IDispatchEx_Release(_IDispatchEx_(&array->dispex));
300     return hres;
301 }
302
303 HRESULT create_array(script_ctx_t *ctx, DWORD length, DispatchEx **ret)
304 {
305     ArrayInstance *array;
306     HRESULT hres;
307
308     hres = alloc_array(ctx, TRUE, &array);
309     if(FAILED(hres))
310         return hres;
311
312     array->length = length;
313
314     *ret = &array->dispex;
315     return S_OK;
316 }