kernel32: Fix the target buffer size.
[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     ArrayInstance *This = (ArrayInstance*)dispex;
55
56     TRACE("%p %d\n", This, This->length);
57
58     switch(flags) {
59     case DISPATCH_PROPERTYGET:
60         V_VT(retv) = VT_I4;
61         V_I4(retv) = This->length;
62         break;
63     default:
64         FIXME("unimplemented flags %x\n", flags);
65         return E_NOTIMPL;
66     }
67
68     return S_OK;
69 }
70
71 static HRESULT Array_concat(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
72         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
73 {
74     FIXME("\n");
75     return E_NOTIMPL;
76 }
77
78 static HRESULT Array_join(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
79         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
80 {
81     FIXME("\n");
82     return E_NOTIMPL;
83 }
84
85 static HRESULT Array_pop(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
86         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
87 {
88     FIXME("\n");
89     return E_NOTIMPL;
90 }
91
92 static HRESULT Array_push(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
93         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
94 {
95     FIXME("\n");
96     return E_NOTIMPL;
97 }
98
99 static HRESULT Array_reverse(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
100         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
101 {
102     FIXME("\n");
103     return E_NOTIMPL;
104 }
105
106 static HRESULT Array_shift(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
107         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
108 {
109     FIXME("\n");
110     return E_NOTIMPL;
111 }
112
113 static HRESULT Array_slice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
114         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
115 {
116     FIXME("\n");
117     return E_NOTIMPL;
118 }
119
120 static HRESULT Array_sort(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
121         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
122 {
123     FIXME("\n");
124     return E_NOTIMPL;
125 }
126
127 static HRESULT Array_splice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
128         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
129 {
130     FIXME("\n");
131     return E_NOTIMPL;
132 }
133
134 static HRESULT Array_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
135         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
136 {
137     FIXME("\n");
138     return E_NOTIMPL;
139 }
140
141 static HRESULT Array_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
142         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
143 {
144     FIXME("\n");
145     return E_NOTIMPL;
146 }
147
148 static HRESULT Array_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
149         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
150 {
151     FIXME("\n");
152     return E_NOTIMPL;
153 }
154
155 static HRESULT Array_unshift(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
156         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
157 {
158     FIXME("\n");
159     return E_NOTIMPL;
160 }
161
162 static HRESULT Array_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
163         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
164 {
165     FIXME("\n");
166     return E_NOTIMPL;
167 }
168
169 static HRESULT Array_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
170         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
171 {
172     FIXME("\n");
173     return E_NOTIMPL;
174 }
175
176 static HRESULT Array_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
177         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
178 {
179     FIXME("\n");
180     return E_NOTIMPL;
181 }
182
183 static HRESULT Array_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
184         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
185 {
186     FIXME("\n");
187     return E_NOTIMPL;
188 }
189
190 static void Array_destructor(DispatchEx *dispex)
191 {
192     heap_free(dispex);
193 }
194
195 static void Array_on_put(DispatchEx *dispex, const WCHAR *name)
196 {
197     ArrayInstance *array = (ArrayInstance*)dispex;
198     const WCHAR *ptr = name;
199     DWORD id = 0;
200
201     if(!isdigitW(*ptr))
202         return;
203
204     while(*ptr && isdigitW(*ptr)) {
205         id = id*10 + (*ptr-'0');
206         ptr++;
207     }
208
209     if(*ptr)
210         return;
211
212     if(id >= array->length)
213         array->length = id+1;
214 }
215
216 static const builtin_prop_t Array_props[] = {
217     {concatW,                Array_concat,               PROPF_METHOD},
218     {hasOwnPropertyW,        Array_hasOwnProperty,       PROPF_METHOD},
219     {isPrototypeOfW,         Array_isPrototypeOf,        PROPF_METHOD},
220     {joinW,                  Array_join,                 PROPF_METHOD},
221     {lengthW,                Array_length,               0},
222     {popW,                   Array_pop,                  PROPF_METHOD},
223     {propertyIsEnumerableW,  Array_propertyIsEnumerable, PROPF_METHOD},
224     {pushW,                  Array_push,                 PROPF_METHOD},
225     {reverseW,               Array_reverse,              PROPF_METHOD},
226     {shiftW,                 Array_shift,                PROPF_METHOD},
227     {sliceW,                 Array_slice,                PROPF_METHOD},
228     {sortW,                  Array_sort,                 PROPF_METHOD},
229     {spliceW,                Array_splice,               PROPF_METHOD},
230     {toLocaleStringW,        Array_toLocaleString,       PROPF_METHOD},
231     {toStringW,              Array_toString,             PROPF_METHOD},
232     {unshiftW,               Array_unshift,              PROPF_METHOD},
233     {valueOfW,               Array_valueOf,              PROPF_METHOD}
234 };
235
236 static const builtin_info_t Array_info = {
237     JSCLASS_ARRAY,
238     {NULL, Array_value, 0},
239     sizeof(Array_props)/sizeof(*Array_props),
240     Array_props,
241     Array_destructor,
242     Array_on_put
243 };
244
245 static HRESULT ArrayConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
246         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
247 {
248     DispatchEx *obj;
249     VARIANT *arg_var;
250     DWORD i;
251     HRESULT hres;
252
253     TRACE("\n");
254
255     switch(flags) {
256     case DISPATCH_CONSTRUCT: {
257         if(arg_cnt(dp) == 1 && V_VT((arg_var = get_arg(dp, 0))) == VT_I4) {
258             if(V_I4(arg_var) < 0) {
259                 FIXME("throw RangeError\n");
260                 return E_FAIL;
261             }
262
263             hres = create_array(dispex->ctx, V_I4(arg_var), &obj);
264             if(FAILED(hres))
265                 return hres;
266
267             V_VT(retv) = VT_DISPATCH;
268             V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
269             return S_OK;
270         }
271
272         hres = create_array(dispex->ctx, arg_cnt(dp), &obj);
273         if(FAILED(hres))
274             return hres;
275
276         for(i=0; i < arg_cnt(dp); i++) {
277             hres = jsdisp_propput_idx(obj, i, lcid, get_arg(dp, i), ei, caller);
278             if(FAILED(hres))
279                 break;
280         }
281         if(FAILED(hres)) {
282             jsdisp_release(obj);
283             return hres;
284         }
285
286         V_VT(retv) = VT_DISPATCH;
287         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
288         break;
289     }
290     default:
291         FIXME("unimplemented flags: %x\n", flags);
292         return E_NOTIMPL;
293     }
294
295     return S_OK;
296 }
297
298 static HRESULT alloc_array(script_ctx_t *ctx, BOOL use_constr, ArrayInstance **ret)
299 {
300     ArrayInstance *array = heap_alloc_zero(sizeof(ArrayInstance));
301     HRESULT hres;
302
303     if(use_constr)
304         hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->array_constr);
305     else
306         hres = init_dispex(&array->dispex, ctx, &Array_info, NULL);
307
308     if(FAILED(hres)) {
309         heap_free(array);
310         return hres;
311     }
312
313     *ret = array;
314     return S_OK;
315 }
316
317 HRESULT create_array_constr(script_ctx_t *ctx, DispatchEx **ret)
318 {
319     ArrayInstance *array;
320     HRESULT hres;
321
322     hres = alloc_array(ctx, FALSE, &array);
323     if(FAILED(hres))
324         return hres;
325
326     hres = create_builtin_function(ctx, ArrayConstr_value, PROPF_CONSTR, &array->dispex, ret);
327
328     IDispatchEx_Release(_IDispatchEx_(&array->dispex));
329     return hres;
330 }
331
332 HRESULT create_array(script_ctx_t *ctx, DWORD length, DispatchEx **ret)
333 {
334     ArrayInstance *array;
335     HRESULT hres;
336
337     hres = alloc_array(ctx, TRUE, &array);
338     if(FAILED(hres))
339         return hres;
340
341     array->length = length;
342
343     *ret = &array->dispex;
344     return S_OK;
345 }