xref: /reactos/dll/win32/jscript/array.c (revision 3e2d6582)
1c2c66affSColin Finck /*
2c2c66affSColin Finck  * Copyright 2008 Jacek Caban for CodeWeavers
3c2c66affSColin Finck  *
4c2c66affSColin Finck  * This library is free software; you can redistribute it and/or
5c2c66affSColin Finck  * modify it under the terms of the GNU Lesser General Public
6c2c66affSColin Finck  * License as published by the Free Software Foundation; either
7c2c66affSColin Finck  * version 2.1 of the License, or (at your option) any later version.
8c2c66affSColin Finck  *
9c2c66affSColin Finck  * This library is distributed in the hope that it will be useful,
10c2c66affSColin Finck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11c2c66affSColin Finck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12c2c66affSColin Finck  * Lesser General Public License for more details.
13c2c66affSColin Finck  *
14c2c66affSColin Finck  * You should have received a copy of the GNU Lesser General Public
15c2c66affSColin Finck  * License along with this library; if not, write to the Free Software
16c2c66affSColin Finck  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17c2c66affSColin Finck  */
18c2c66affSColin Finck 
19*3e2d6582SAmine Khaldi #ifdef __REACTOS__
20*3e2d6582SAmine Khaldi #include <wine/config.h>
21*3e2d6582SAmine Khaldi #include <wine/port.h>
22*3e2d6582SAmine Khaldi #endif
238dba275bSAmine Khaldi 
248dba275bSAmine Khaldi #include <math.h>
258dba275bSAmine Khaldi #include <assert.h>
268dba275bSAmine Khaldi 
27c2c66affSColin Finck #include "jscript.h"
28c2c66affSColin Finck 
298dba275bSAmine Khaldi #include "wine/debug.h"
308dba275bSAmine Khaldi 
318dba275bSAmine Khaldi WINE_DEFAULT_DEBUG_CHANNEL(jscript);
328dba275bSAmine Khaldi 
33c2c66affSColin Finck typedef struct {
34c2c66affSColin Finck     jsdisp_t dispex;
35c2c66affSColin Finck 
36c2c66affSColin Finck     DWORD length;
37c2c66affSColin Finck } ArrayInstance;
38c2c66affSColin Finck 
39c2c66affSColin Finck static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
40c2c66affSColin Finck static const WCHAR concatW[] = {'c','o','n','c','a','t',0};
41660f7b90SAmine Khaldi static const WCHAR forEachW[] = {'f','o','r','E','a','c','h',0};
42c2c66affSColin Finck static const WCHAR joinW[] = {'j','o','i','n',0};
43c2c66affSColin Finck static const WCHAR popW[] = {'p','o','p',0};
44c2c66affSColin Finck static const WCHAR pushW[] = {'p','u','s','h',0};
45c2c66affSColin Finck static const WCHAR reverseW[] = {'r','e','v','e','r','s','e',0};
46c2c66affSColin Finck static const WCHAR shiftW[] = {'s','h','i','f','t',0};
47c2c66affSColin Finck static const WCHAR sliceW[] = {'s','l','i','c','e',0};
48c2c66affSColin Finck static const WCHAR sortW[] = {'s','o','r','t',0};
49c2c66affSColin Finck static const WCHAR spliceW[] = {'s','p','l','i','c','e',0};
50c2c66affSColin Finck static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
51c2c66affSColin Finck static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
52c2c66affSColin Finck static const WCHAR unshiftW[] = {'u','n','s','h','i','f','t',0};
538dba275bSAmine Khaldi static const WCHAR indexOfW[] = {'i','n','d','e','x','O','f',0};
54*3e2d6582SAmine Khaldi static const WCHAR mapW[] = {'m','a','p',0};
55c2c66affSColin Finck 
56c2c66affSColin Finck static const WCHAR default_separatorW[] = {',',0};
57c2c66affSColin Finck 
array_from_jsdisp(jsdisp_t * jsdisp)58c2c66affSColin Finck static inline ArrayInstance *array_from_jsdisp(jsdisp_t *jsdisp)
59c2c66affSColin Finck {
60c2c66affSColin Finck     return CONTAINING_RECORD(jsdisp, ArrayInstance, dispex);
61c2c66affSColin Finck }
62c2c66affSColin Finck 
array_from_vdisp(vdisp_t * vdisp)63c2c66affSColin Finck static inline ArrayInstance *array_from_vdisp(vdisp_t *vdisp)
64c2c66affSColin Finck {
65c2c66affSColin Finck     return array_from_jsdisp(vdisp->u.jsdisp);
66c2c66affSColin Finck }
67c2c66affSColin Finck 
array_this(vdisp_t * jsthis)68c2c66affSColin Finck static inline ArrayInstance *array_this(vdisp_t *jsthis)
69c2c66affSColin Finck {
70c2c66affSColin Finck     return is_vclass(jsthis, JSCLASS_ARRAY) ? array_from_vdisp(jsthis) : NULL;
71c2c66affSColin Finck }
72c2c66affSColin Finck 
array_get_length(jsdisp_t * array)73c2c66affSColin Finck unsigned array_get_length(jsdisp_t *array)
74c2c66affSColin Finck {
75c2c66affSColin Finck     assert(is_class(array, JSCLASS_ARRAY));
76c2c66affSColin Finck     return array_from_jsdisp(array)->length;
77c2c66affSColin Finck }
78c2c66affSColin Finck 
get_length(script_ctx_t * ctx,vdisp_t * vdisp,jsdisp_t ** jsthis,DWORD * ret)79c2c66affSColin Finck static HRESULT get_length(script_ctx_t *ctx, vdisp_t *vdisp, jsdisp_t **jsthis, DWORD *ret)
80c2c66affSColin Finck {
81c2c66affSColin Finck     ArrayInstance *array;
82c2c66affSColin Finck     jsval_t val;
83c2c66affSColin Finck     HRESULT hres;
84c2c66affSColin Finck 
85c2c66affSColin Finck     array = array_this(vdisp);
86c2c66affSColin Finck     if(array) {
87c2c66affSColin Finck         *jsthis = &array->dispex;
88c2c66affSColin Finck         *ret = array->length;
89c2c66affSColin Finck         return S_OK;
90c2c66affSColin Finck     }
91c2c66affSColin Finck 
92c2c66affSColin Finck     if(!is_jsdisp(vdisp))
93c2c66affSColin Finck         return throw_type_error(ctx, JS_E_JSCRIPT_EXPECTED, NULL);
94c2c66affSColin Finck 
95c2c66affSColin Finck     hres = jsdisp_propget_name(vdisp->u.jsdisp, lengthW, &val);
96c2c66affSColin Finck     if(FAILED(hres))
97c2c66affSColin Finck         return hres;
98c2c66affSColin Finck 
99c2c66affSColin Finck     hres = to_uint32(ctx, val, ret);
100c2c66affSColin Finck     jsval_release(val);
101c2c66affSColin Finck     if(FAILED(hres))
102c2c66affSColin Finck         return hres;
103c2c66affSColin Finck 
104c2c66affSColin Finck     *jsthis = vdisp->u.jsdisp;
105c2c66affSColin Finck     return S_OK;
106c2c66affSColin Finck }
107c2c66affSColin Finck 
set_length(jsdisp_t * obj,DWORD length)108c2c66affSColin Finck static HRESULT set_length(jsdisp_t *obj, DWORD length)
109c2c66affSColin Finck {
110c2c66affSColin Finck     if(is_class(obj, JSCLASS_ARRAY)) {
111c2c66affSColin Finck         array_from_jsdisp(obj)->length = length;
112c2c66affSColin Finck         return S_OK;
113c2c66affSColin Finck     }
114c2c66affSColin Finck 
115c2c66affSColin Finck     return jsdisp_propput_name(obj, lengthW, jsval_number(length));
116c2c66affSColin Finck }
117c2c66affSColin Finck 
idx_to_str(DWORD idx,WCHAR * ptr)118c2c66affSColin Finck static WCHAR *idx_to_str(DWORD idx, WCHAR *ptr)
119c2c66affSColin Finck {
120c2c66affSColin Finck     if(!idx) {
121c2c66affSColin Finck         *ptr = '0';
122c2c66affSColin Finck         return ptr;
123c2c66affSColin Finck     }
124c2c66affSColin Finck 
125c2c66affSColin Finck     while(idx) {
126c2c66affSColin Finck         *ptr-- = '0' + (idx%10);
127c2c66affSColin Finck         idx /= 10;
128c2c66affSColin Finck     }
129c2c66affSColin Finck 
130c2c66affSColin Finck     return ptr+1;
131c2c66affSColin Finck }
132c2c66affSColin Finck 
Array_get_length(script_ctx_t * ctx,jsdisp_t * jsthis,jsval_t * r)133c2c66affSColin Finck static HRESULT Array_get_length(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
134c2c66affSColin Finck {
135c2c66affSColin Finck     TRACE("%p\n", jsthis);
136c2c66affSColin Finck 
137c2c66affSColin Finck     *r = jsval_number(array_from_jsdisp(jsthis)->length);
138c2c66affSColin Finck     return S_OK;
139c2c66affSColin Finck }
140c2c66affSColin Finck 
Array_set_length(script_ctx_t * ctx,jsdisp_t * jsthis,jsval_t value)141c2c66affSColin Finck static HRESULT Array_set_length(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t value)
142c2c66affSColin Finck {
143c2c66affSColin Finck     ArrayInstance *This = array_from_jsdisp(jsthis);
144c2c66affSColin Finck     DOUBLE len = -1;
145c2c66affSColin Finck     DWORD i;
146c2c66affSColin Finck     HRESULT hres;
147c2c66affSColin Finck 
148c2c66affSColin Finck     TRACE("%p %d\n", This, This->length);
149c2c66affSColin Finck 
150c2c66affSColin Finck     hres = to_number(ctx, value, &len);
151c2c66affSColin Finck     if(FAILED(hres))
152c2c66affSColin Finck         return hres;
153c2c66affSColin Finck 
154c2c66affSColin Finck     len = floor(len);
155c2c66affSColin Finck     if(len!=(DWORD)len)
156c2c66affSColin Finck         return throw_range_error(ctx, JS_E_INVALID_LENGTH, NULL);
157c2c66affSColin Finck 
158c2c66affSColin Finck     for(i=len; i < This->length; i++) {
159c2c66affSColin Finck         hres = jsdisp_delete_idx(&This->dispex, i);
160c2c66affSColin Finck         if(FAILED(hres))
161c2c66affSColin Finck             return hres;
162c2c66affSColin Finck     }
163c2c66affSColin Finck 
164c2c66affSColin Finck     This->length = len;
165c2c66affSColin Finck     return S_OK;
166c2c66affSColin Finck }
167c2c66affSColin Finck 
concat_array(jsdisp_t * array,ArrayInstance * obj,DWORD * len)168c2c66affSColin Finck static HRESULT concat_array(jsdisp_t *array, ArrayInstance *obj, DWORD *len)
169c2c66affSColin Finck {
170c2c66affSColin Finck     jsval_t val;
171c2c66affSColin Finck     DWORD i;
172c2c66affSColin Finck     HRESULT hres;
173c2c66affSColin Finck 
174c2c66affSColin Finck     for(i=0; i < obj->length; i++) {
175c2c66affSColin Finck         hres = jsdisp_get_idx(&obj->dispex, i, &val);
176c2c66affSColin Finck         if(hres == DISP_E_UNKNOWNNAME)
177c2c66affSColin Finck             continue;
178c2c66affSColin Finck         if(FAILED(hres))
179c2c66affSColin Finck             return hres;
180c2c66affSColin Finck 
181c2c66affSColin Finck         hres = jsdisp_propput_idx(array, *len+i, val);
182c2c66affSColin Finck         jsval_release(val);
183c2c66affSColin Finck         if(FAILED(hres))
184c2c66affSColin Finck             return hres;
185c2c66affSColin Finck     }
186c2c66affSColin Finck 
187c2c66affSColin Finck     *len += obj->length;
188c2c66affSColin Finck     return S_OK;
189c2c66affSColin Finck }
190c2c66affSColin Finck 
concat_obj(jsdisp_t * array,IDispatch * obj,DWORD * len)191c2c66affSColin Finck static HRESULT concat_obj(jsdisp_t *array, IDispatch *obj, DWORD *len)
192c2c66affSColin Finck {
193c2c66affSColin Finck     jsdisp_t *jsobj;
194c2c66affSColin Finck     HRESULT hres;
195c2c66affSColin Finck 
196c2c66affSColin Finck     jsobj = iface_to_jsdisp(obj);
197c2c66affSColin Finck     if(jsobj) {
198c2c66affSColin Finck         if(is_class(jsobj, JSCLASS_ARRAY)) {
199c2c66affSColin Finck             hres = concat_array(array, array_from_jsdisp(jsobj), len);
200c2c66affSColin Finck             jsdisp_release(jsobj);
201c2c66affSColin Finck             return hres;
202c2c66affSColin Finck         }
203c2c66affSColin Finck         jsdisp_release(jsobj);
204c2c66affSColin Finck     }
205c2c66affSColin Finck 
206c2c66affSColin Finck     return jsdisp_propput_idx(array, (*len)++, jsval_disp(obj));
207c2c66affSColin Finck }
208c2c66affSColin Finck 
Array_concat(script_ctx_t * ctx,vdisp_t * jsthis,WORD flags,unsigned argc,jsval_t * argv,jsval_t * r)209c2c66affSColin Finck static HRESULT Array_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
210c2c66affSColin Finck         jsval_t *r)
211c2c66affSColin Finck {
212c2c66affSColin Finck     jsdisp_t *ret;
213c2c66affSColin Finck     DWORD len = 0;
214c2c66affSColin Finck     HRESULT hres;
215c2c66affSColin Finck 
216c2c66affSColin Finck     TRACE("\n");
217c2c66affSColin Finck 
218c2c66affSColin Finck     hres = create_array(ctx, 0, &ret);
219c2c66affSColin Finck     if(FAILED(hres))
220c2c66affSColin Finck         return hres;
221c2c66affSColin Finck 
222c2c66affSColin Finck     hres = concat_obj(ret, jsthis->u.disp, &len);
223c2c66affSColin Finck     if(SUCCEEDED(hres)) {
224c2c66affSColin Finck         DWORD i;
225c2c66affSColin Finck 
226c2c66affSColin Finck         for(i=0; i < argc; i++) {
227c2c66affSColin Finck             if(is_object_instance(argv[i]))
228c2c66affSColin Finck                 hres = concat_obj(ret, get_object(argv[i]), &len);
229c2c66affSColin Finck             else
230c2c66affSColin Finck                 hres = jsdisp_propput_idx(ret, len++, argv[i]);
231c2c66affSColin Finck             if(FAILED(hres))
232c2c66affSColin Finck                 break;
233c2c66affSColin Finck         }
234c2c66affSColin Finck     }
235c2c66affSColin Finck 
236c2c66affSColin Finck     if(FAILED(hres))
237c2c66affSColin Finck         return hres;
238c2c66affSColin Finck 
239c2c66affSColin Finck     if(r)
240c2c66affSColin Finck         *r = jsval_obj(ret);
241c2c66affSColin Finck     else
242c2c66affSColin Finck         jsdisp_release(ret);
243c2c66affSColin Finck     return S_OK;
244c2c66affSColin Finck }
245c2c66affSColin Finck 
array_join(script_ctx_t * ctx,jsdisp_t * array,DWORD length,const WCHAR * sep,unsigned seplen,jsval_t * r)246c2c66affSColin Finck static HRESULT array_join(script_ctx_t *ctx, jsdisp_t *array, DWORD length, const WCHAR *sep,
247c2c66affSColin Finck         unsigned seplen, jsval_t *r)
248c2c66affSColin Finck {
249c2c66affSColin Finck     jsstr_t **str_tab, *ret = NULL;
250c2c66affSColin Finck     jsval_t val;
251c2c66affSColin Finck     DWORD i;
252c2c66affSColin Finck     HRESULT hres = E_FAIL;
253c2c66affSColin Finck 
254c2c66affSColin Finck     if(!length) {
255c2c66affSColin Finck         if(r)
256c2c66affSColin Finck             *r = jsval_string(jsstr_empty());
257c2c66affSColin Finck         return S_OK;
258c2c66affSColin Finck     }
259c2c66affSColin Finck 
260c2c66affSColin Finck     str_tab = heap_alloc_zero(length * sizeof(*str_tab));
261c2c66affSColin Finck     if(!str_tab)
262c2c66affSColin Finck         return E_OUTOFMEMORY;
263c2c66affSColin Finck 
264c2c66affSColin Finck     for(i=0; i < length; i++) {
265c2c66affSColin Finck         hres = jsdisp_get_idx(array, i, &val);
266c2c66affSColin Finck         if(hres == DISP_E_UNKNOWNNAME) {
267c2c66affSColin Finck             hres = S_OK;
268c2c66affSColin Finck             continue;
269c2c66affSColin Finck         } else if(FAILED(hres))
270c2c66affSColin Finck             break;
271c2c66affSColin Finck 
272c2c66affSColin Finck         if(!is_undefined(val) && !is_null(val)) {
273c2c66affSColin Finck             hres = to_string(ctx, val, str_tab+i);
274c2c66affSColin Finck             jsval_release(val);
275c2c66affSColin Finck             if(FAILED(hres))
276c2c66affSColin Finck                 break;
277c2c66affSColin Finck         }
278c2c66affSColin Finck     }
279c2c66affSColin Finck 
280c2c66affSColin Finck     if(SUCCEEDED(hres)) {
281c2c66affSColin Finck         DWORD len = 0;
282c2c66affSColin Finck 
283c2c66affSColin Finck         if(str_tab[0])
284c2c66affSColin Finck             len = jsstr_length(str_tab[0]);
285c2c66affSColin Finck         for(i=1; i < length; i++) {
286c2c66affSColin Finck             len += seplen;
287c2c66affSColin Finck             if(str_tab[i])
288c2c66affSColin Finck                 len += jsstr_length(str_tab[i]);
289c2c66affSColin Finck             if(len > JSSTR_MAX_LENGTH) {
290c2c66affSColin Finck                 hres = E_OUTOFMEMORY;
291c2c66affSColin Finck                 break;
292c2c66affSColin Finck             }
293c2c66affSColin Finck         }
294c2c66affSColin Finck 
295c2c66affSColin Finck         if(SUCCEEDED(hres)) {
296c2c66affSColin Finck             WCHAR *ptr = NULL;
297c2c66affSColin Finck 
298c2c66affSColin Finck             ret = jsstr_alloc_buf(len, &ptr);
299c2c66affSColin Finck             if(ret) {
300c2c66affSColin Finck                 if(str_tab[0])
301c2c66affSColin Finck                     ptr += jsstr_flush(str_tab[0], ptr);
302c2c66affSColin Finck 
303c2c66affSColin Finck                 for(i=1; i < length; i++) {
304c2c66affSColin Finck                     if(seplen) {
305c2c66affSColin Finck                         memcpy(ptr, sep, seplen*sizeof(WCHAR));
306c2c66affSColin Finck                         ptr += seplen;
307c2c66affSColin Finck                     }
308c2c66affSColin Finck 
309c2c66affSColin Finck                     if(str_tab[i])
310c2c66affSColin Finck                         ptr += jsstr_flush(str_tab[i], ptr);
311c2c66affSColin Finck                 }
312c2c66affSColin Finck             }else {
313c2c66affSColin Finck                 hres = E_OUTOFMEMORY;
314c2c66affSColin Finck             }
315c2c66affSColin Finck         }
316c2c66affSColin Finck     }
317c2c66affSColin Finck 
318c2c66affSColin Finck     for(i=0; i < length; i++) {
319c2c66affSColin Finck         if(str_tab[i])
320c2c66affSColin Finck             jsstr_release(str_tab[i]);
321c2c66affSColin Finck     }
322c2c66affSColin Finck     heap_free(str_tab);
323c2c66affSColin Finck     if(FAILED(hres))
324c2c66affSColin Finck         return hres;
325c2c66affSColin Finck 
326c2c66affSColin Finck     TRACE("= %s\n", debugstr_jsstr(ret));
327c2c66affSColin Finck 
328c2c66affSColin Finck     if(r)
329c2c66affSColin Finck         *r = jsval_string(ret);
330c2c66affSColin Finck     else
331c2c66affSColin Finck         jsstr_release(ret);
332c2c66affSColin Finck     return S_OK;
333c2c66affSColin Finck }
334c2c66affSColin Finck 
335c2c66affSColin Finck /* ECMA-262 3rd Edition    15.4.4.5 */
Array_join(script_ctx_t * ctx,vdisp_t * vthis,WORD flags,unsigned argc,jsval_t * argv,jsval_t * r)336c2c66affSColin Finck static HRESULT Array_join(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
337c2c66affSColin Finck         jsval_t *r)
338c2c66affSColin Finck {
339c2c66affSColin Finck     jsdisp_t *jsthis;
340c2c66affSColin Finck     DWORD length;
341c2c66affSColin Finck     HRESULT hres;
342c2c66affSColin Finck 
343c2c66affSColin Finck     TRACE("\n");
344c2c66affSColin Finck 
345c2c66affSColin Finck     hres = get_length(ctx, vthis, &jsthis, &length);
346c2c66affSColin Finck     if(FAILED(hres))
347c2c66affSColin Finck         return hres;
348c2c66affSColin Finck 
349c2c66affSColin Finck     if(argc) {
350c2c66affSColin Finck         const WCHAR *sep;
351c2c66affSColin Finck         jsstr_t *sep_str;
352c2c66affSColin Finck 
353c2c66affSColin Finck         hres = to_flat_string(ctx, argv[0], &sep_str, &sep);
354c2c66affSColin Finck         if(FAILED(hres))
355c2c66affSColin Finck             return hres;
356c2c66affSColin Finck 
357c2c66affSColin Finck         hres = array_join(ctx, jsthis, length, sep, jsstr_length(sep_str), r);
358c2c66affSColin Finck 
359c2c66affSColin Finck         jsstr_release(sep_str);
360c2c66affSColin Finck     }else {
361*3e2d6582SAmine Khaldi         hres = array_join(ctx, jsthis, length, default_separatorW, lstrlenW(default_separatorW), r);
362c2c66affSColin Finck     }
363c2c66affSColin Finck 
364c2c66affSColin Finck     return hres;
365c2c66affSColin Finck }
366c2c66affSColin Finck 
Array_pop(script_ctx_t * ctx,vdisp_t * vthis,WORD flags,unsigned argc,jsval_t * argv,jsval_t * r)367c2c66affSColin Finck static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
368c2c66affSColin Finck         jsval_t *r)
369c2c66affSColin Finck {
370c2c66affSColin Finck     jsdisp_t *jsthis;
371c2c66affSColin Finck     jsval_t val;
372c2c66affSColin Finck     DWORD length;
373c2c66affSColin Finck     HRESULT hres;
374c2c66affSColin Finck 
375c2c66affSColin Finck     TRACE("\n");
376c2c66affSColin Finck 
377c2c66affSColin Finck     hres = get_length(ctx, vthis, &jsthis, &length);
378c2c66affSColin Finck     if(FAILED(hres))
379c2c66affSColin Finck         return hres;
380c2c66affSColin Finck 
381c2c66affSColin Finck     if(!length) {
382c2c66affSColin Finck         hres = set_length(jsthis, 0);
383c2c66affSColin Finck         if(FAILED(hres))
384c2c66affSColin Finck             return hres;
385c2c66affSColin Finck 
386c2c66affSColin Finck         if(r)
387c2c66affSColin Finck             *r = jsval_undefined();
388c2c66affSColin Finck         return S_OK;
389c2c66affSColin Finck     }
390c2c66affSColin Finck 
391c2c66affSColin Finck     length--;
392c2c66affSColin Finck     hres = jsdisp_get_idx(jsthis, length, &val);
393c2c66affSColin Finck     if(SUCCEEDED(hres))
394c2c66affSColin Finck         hres = jsdisp_delete_idx(jsthis, length);
3958dba275bSAmine Khaldi     else if(hres == DISP_E_UNKNOWNNAME) {
396c2c66affSColin Finck         val = jsval_undefined();
3978dba275bSAmine Khaldi         hres = S_OK;
3988dba275bSAmine Khaldi     }else
399c2c66affSColin Finck         return hres;
400c2c66affSColin Finck 
401c2c66affSColin Finck     if(SUCCEEDED(hres))
402c2c66affSColin Finck         hres = set_length(jsthis, length);
403c2c66affSColin Finck 
404c2c66affSColin Finck     if(FAILED(hres)) {
405c2c66affSColin Finck         jsval_release(val);
406c2c66affSColin Finck         return hres;
407c2c66affSColin Finck     }
408c2c66affSColin Finck 
409c2c66affSColin Finck     if(r)
410c2c66affSColin Finck         *r = val;
411c2c66affSColin Finck     else
412c2c66affSColin Finck         jsval_release(val);
413c2c66affSColin Finck     return hres;
414c2c66affSColin Finck }
415c2c66affSColin Finck 
416c2c66affSColin Finck /* ECMA-262 3rd Edition    15.4.4.7 */
Array_push(script_ctx_t * ctx,vdisp_t * vthis,WORD flags,unsigned argc,jsval_t * argv,jsval_t * r)417c2c66affSColin Finck static HRESULT Array_push(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
418c2c66affSColin Finck         jsval_t *r)
419c2c66affSColin Finck {
420c2c66affSColin Finck     jsdisp_t *jsthis;
421c2c66affSColin Finck     DWORD length = 0;
422c2c66affSColin Finck     unsigned i;
423c2c66affSColin Finck     HRESULT hres;
424c2c66affSColin Finck 
425c2c66affSColin Finck     TRACE("\n");
426c2c66affSColin Finck 
427c2c66affSColin Finck     hres = get_length(ctx, vthis, &jsthis, &length);
428c2c66affSColin Finck     if(FAILED(hres))
429c2c66affSColin Finck         return hres;
430c2c66affSColin Finck 
431c2c66affSColin Finck     for(i=0; i < argc; i++) {
432c2c66affSColin Finck         hres = jsdisp_propput_idx(jsthis, length+i, argv[i]);
433c2c66affSColin Finck         if(FAILED(hres))
434c2c66affSColin Finck             return hres;
435c2c66affSColin Finck     }
436c2c66affSColin Finck 
437c2c66affSColin Finck     hres = set_length(jsthis, length+argc);
438c2c66affSColin Finck     if(FAILED(hres))
439c2c66affSColin Finck         return hres;
440c2c66affSColin Finck 
441c2c66affSColin Finck     if(r)
442c2c66affSColin Finck         *r = jsval_number(length+argc);
443c2c66affSColin Finck     return S_OK;
444c2c66affSColin Finck }
445c2c66affSColin Finck 
Array_reverse(script_ctx_t * ctx,vdisp_t * vthis,WORD flags,unsigned argc,jsval_t * argv,jsval_t * r)446c2c66affSColin Finck static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
447c2c66affSColin Finck         jsval_t *r)
448c2c66affSColin Finck {
449c2c66affSColin Finck     jsdisp_t *jsthis;
450c2c66affSColin Finck     DWORD length, k, l;
451c2c66affSColin Finck     jsval_t v1, v2;
452c2c66affSColin Finck     HRESULT hres1, hres2;
453c2c66affSColin Finck 
454c2c66affSColin Finck     TRACE("\n");
455c2c66affSColin Finck 
456c2c66affSColin Finck     hres1 = get_length(ctx, vthis, &jsthis, &length);
457c2c66affSColin Finck     if(FAILED(hres1))
458c2c66affSColin Finck         return hres1;
459c2c66affSColin Finck 
460c2c66affSColin Finck     for(k=0; k<length/2; k++) {
461c2c66affSColin Finck         l = length-k-1;
462c2c66affSColin Finck 
463c2c66affSColin Finck         hres1 = jsdisp_get_idx(jsthis, k, &v1);
464c2c66affSColin Finck         if(FAILED(hres1) && hres1!=DISP_E_UNKNOWNNAME)
465c2c66affSColin Finck             return hres1;
466c2c66affSColin Finck 
467c2c66affSColin Finck         hres2 = jsdisp_get_idx(jsthis, l, &v2);
468c2c66affSColin Finck         if(FAILED(hres2) && hres2!=DISP_E_UNKNOWNNAME) {
469c2c66affSColin Finck             jsval_release(v1);
470c2c66affSColin Finck             return hres2;
471c2c66affSColin Finck         }
472c2c66affSColin Finck 
473c2c66affSColin Finck         if(hres1 == DISP_E_UNKNOWNNAME)
474c2c66affSColin Finck             hres1 = jsdisp_delete_idx(jsthis, l);
475c2c66affSColin Finck         else
476c2c66affSColin Finck             hres1 = jsdisp_propput_idx(jsthis, l, v1);
477c2c66affSColin Finck 
478c2c66affSColin Finck         if(FAILED(hres1)) {
479c2c66affSColin Finck             jsval_release(v1);
480c2c66affSColin Finck             jsval_release(v2);
481c2c66affSColin Finck             return hres1;
482c2c66affSColin Finck         }
483c2c66affSColin Finck 
484c2c66affSColin Finck         if(hres2 == DISP_E_UNKNOWNNAME)
485c2c66affSColin Finck             hres2 = jsdisp_delete_idx(jsthis, k);
486c2c66affSColin Finck         else
487c2c66affSColin Finck             hres2 = jsdisp_propput_idx(jsthis, k, v2);
488c2c66affSColin Finck 
489c2c66affSColin Finck         if(FAILED(hres2)) {
490c2c66affSColin Finck             jsval_release(v2);
491c2c66affSColin Finck             return hres2;
492c2c66affSColin Finck         }
493c2c66affSColin Finck     }
494c2c66affSColin Finck 
495c2c66affSColin Finck     if(r)
496c2c66affSColin Finck         *r = jsval_obj(jsdisp_addref(jsthis));
497c2c66affSColin Finck     return S_OK;
498c2c66affSColin Finck }
499c2c66affSColin Finck 
500c2c66affSColin Finck /* ECMA-262 3rd Edition    15.4.4.9 */
Array_shift(script_ctx_t * ctx,vdisp_t * vthis,WORD flags,unsigned argc,jsval_t * argv,jsval_t * r)501c2c66affSColin Finck static HRESULT Array_shift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
502c2c66affSColin Finck         jsval_t *r)
503c2c66affSColin Finck {
504c2c66affSColin Finck     jsdisp_t *jsthis;
505c2c66affSColin Finck     DWORD length = 0, i;
506c2c66affSColin Finck     jsval_t v, ret;
507c2c66affSColin Finck     HRESULT hres;
508c2c66affSColin Finck 
509c2c66affSColin Finck     TRACE("\n");
510c2c66affSColin Finck 
511c2c66affSColin Finck     hres = get_length(ctx, vthis, &jsthis, &length);
512c2c66affSColin Finck     if(FAILED(hres))
513c2c66affSColin Finck         return hres;
514c2c66affSColin Finck 
515c2c66affSColin Finck     if(!length) {
516c2c66affSColin Finck         hres = set_length(jsthis, 0);
517c2c66affSColin Finck         if(FAILED(hres))
518c2c66affSColin Finck             return hres;
519c2c66affSColin Finck 
520c2c66affSColin Finck         if(r)
521c2c66affSColin Finck             *r = jsval_undefined();
522c2c66affSColin Finck         return S_OK;
523c2c66affSColin Finck     }
524c2c66affSColin Finck 
525c2c66affSColin Finck     hres = jsdisp_get_idx(jsthis, 0, &ret);
526c2c66affSColin Finck     if(hres == DISP_E_UNKNOWNNAME) {
527c2c66affSColin Finck         ret = jsval_undefined();
528c2c66affSColin Finck         hres = S_OK;
529c2c66affSColin Finck     }
530c2c66affSColin Finck 
531c2c66affSColin Finck     for(i=1; SUCCEEDED(hres) && i<length; i++) {
532c2c66affSColin Finck         hres = jsdisp_get_idx(jsthis, i, &v);
533c2c66affSColin Finck         if(hres == DISP_E_UNKNOWNNAME)
534c2c66affSColin Finck             hres = jsdisp_delete_idx(jsthis, i-1);
535c2c66affSColin Finck         else if(SUCCEEDED(hres))
536c2c66affSColin Finck             hres = jsdisp_propput_idx(jsthis, i-1, v);
537c2c66affSColin Finck     }
538c2c66affSColin Finck 
539c2c66affSColin Finck     if(SUCCEEDED(hres)) {
540c2c66affSColin Finck         hres = jsdisp_delete_idx(jsthis, length-1);
541c2c66affSColin Finck         if(SUCCEEDED(hres))
542c2c66affSColin Finck             hres = set_length(jsthis, length-1);
543c2c66affSColin Finck     }
544c2c66affSColin Finck 
545c2c66affSColin Finck     if(FAILED(hres))
546c2c66affSColin Finck         return hres;
547c2c66affSColin Finck 
548c2c66affSColin Finck     if(r)
549c2c66affSColin Finck         *r = ret;
550c2c66affSColin Finck     else
551c2c66affSColin Finck         jsval_release(ret);
552c2c66affSColin Finck     return hres;
553c2c66affSColin Finck }
554c2c66affSColin Finck 
555c2c66affSColin Finck /* ECMA-262 3rd Edition    15.4.4.10 */
Array_slice(script_ctx_t * ctx,vdisp_t * vthis,WORD flags,unsigned argc,jsval_t * argv,jsval_t * r)556c2c66affSColin Finck static HRESULT Array_slice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
557c2c66affSColin Finck {
558c2c66affSColin Finck     jsdisp_t *arr, *jsthis;
559c2c66affSColin Finck     DOUBLE range;
560c2c66affSColin Finck     DWORD length, start, end, idx;
561c2c66affSColin Finck     HRESULT hres;
562c2c66affSColin Finck 
563c2c66affSColin Finck     TRACE("\n");
564c2c66affSColin Finck 
565c2c66affSColin Finck     hres = get_length(ctx, vthis, &jsthis, &length);
566c2c66affSColin Finck     if(FAILED(hres))
567c2c66affSColin Finck         return hres;
568c2c66affSColin Finck 
569c2c66affSColin Finck     if(argc) {
570c2c66affSColin Finck         hres = to_number(ctx, argv[0], &range);
571c2c66affSColin Finck         if(FAILED(hres))
572c2c66affSColin Finck             return hres;
573c2c66affSColin Finck 
574c2c66affSColin Finck         range = floor(range);
575c2c66affSColin Finck         if(-range>length || isnan(range)) start = 0;
576c2c66affSColin Finck         else if(range < 0) start = range+length;
577c2c66affSColin Finck         else if(range <= length) start = range;
578c2c66affSColin Finck         else start = length;
579c2c66affSColin Finck     }
580c2c66affSColin Finck     else start = 0;
581c2c66affSColin Finck 
582c2c66affSColin Finck     if(argc > 1) {
583c2c66affSColin Finck         hres = to_number(ctx, argv[1], &range);
584c2c66affSColin Finck         if(FAILED(hres))
585c2c66affSColin Finck             return hres;
586c2c66affSColin Finck 
587c2c66affSColin Finck         range = floor(range);
588c2c66affSColin Finck         if(-range>length) end = 0;
589c2c66affSColin Finck         else if(range < 0) end = range+length;
590c2c66affSColin Finck         else if(range <= length) end = range;
591c2c66affSColin Finck         else end = length;
592c2c66affSColin Finck     }
593c2c66affSColin Finck     else end = length;
594c2c66affSColin Finck 
595c2c66affSColin Finck     hres = create_array(ctx, (end>start)?end-start:0, &arr);
596c2c66affSColin Finck     if(FAILED(hres))
597c2c66affSColin Finck         return hres;
598c2c66affSColin Finck 
599c2c66affSColin Finck     for(idx=start; idx<end; idx++) {
600c2c66affSColin Finck         jsval_t v;
601c2c66affSColin Finck 
602c2c66affSColin Finck         hres = jsdisp_get_idx(jsthis, idx, &v);
603c2c66affSColin Finck         if(hres == DISP_E_UNKNOWNNAME)
604c2c66affSColin Finck             continue;
605c2c66affSColin Finck 
606c2c66affSColin Finck         if(SUCCEEDED(hres)) {
607c2c66affSColin Finck             hres = jsdisp_propput_idx(arr, idx-start, v);
608c2c66affSColin Finck             jsval_release(v);
609c2c66affSColin Finck         }
610c2c66affSColin Finck 
611c2c66affSColin Finck         if(FAILED(hres)) {
612c2c66affSColin Finck             jsdisp_release(arr);
613c2c66affSColin Finck             return hres;
614c2c66affSColin Finck         }
615c2c66affSColin Finck     }
616c2c66affSColin Finck 
617c2c66affSColin Finck     if(r)
618c2c66affSColin Finck         *r = jsval_obj(arr);
619c2c66affSColin Finck     else
620c2c66affSColin Finck         jsdisp_release(arr);
621c2c66affSColin Finck 
622c2c66affSColin Finck     return S_OK;
623c2c66affSColin Finck }
624c2c66affSColin Finck 
sort_cmp(script_ctx_t * ctx,jsdisp_t * cmp_func,jsval_t v1,jsval_t v2,INT * cmp)625c2c66affSColin Finck static HRESULT sort_cmp(script_ctx_t *ctx, jsdisp_t *cmp_func, jsval_t v1, jsval_t v2, INT *cmp)
626c2c66affSColin Finck {
627c2c66affSColin Finck     HRESULT hres;
628c2c66affSColin Finck 
629c2c66affSColin Finck     if(cmp_func) {
630c2c66affSColin Finck         jsval_t args[2];
631c2c66affSColin Finck         jsval_t res;
632c2c66affSColin Finck         double n;
633c2c66affSColin Finck 
634c2c66affSColin Finck         args[0] = v1;
635c2c66affSColin Finck         args[1] = v2;
636c2c66affSColin Finck 
637c2c66affSColin Finck         hres = jsdisp_call_value(cmp_func, NULL, DISPATCH_METHOD, 2, args, &res);
638c2c66affSColin Finck         if(FAILED(hres))
639c2c66affSColin Finck             return hres;
640c2c66affSColin Finck 
641c2c66affSColin Finck         hres = to_number(ctx, res, &n);
642c2c66affSColin Finck         jsval_release(res);
643c2c66affSColin Finck         if(FAILED(hres))
644c2c66affSColin Finck             return hres;
645c2c66affSColin Finck 
646c2c66affSColin Finck         if(n == 0)
647c2c66affSColin Finck             *cmp = 0;
648c2c66affSColin Finck         *cmp = n > 0.0 ? 1 : -1;
649c2c66affSColin Finck     }else if(is_undefined(v1)) {
650c2c66affSColin Finck         *cmp = is_undefined(v2) ? 0 : 1;
651c2c66affSColin Finck     }else if(is_undefined(v2)) {
652c2c66affSColin Finck         *cmp = -1;
653c2c66affSColin Finck     }else if(is_number(v1) && is_number(v2)) {
654c2c66affSColin Finck         double d = get_number(v1)-get_number(v2);
655c2c66affSColin Finck         if(d > 0.0)
656c2c66affSColin Finck             *cmp = 1;
657c2c66affSColin Finck         else
658c2c66affSColin Finck             *cmp = d < -0.0 ? -1 : 0;
659c2c66affSColin Finck     }else {
660c2c66affSColin Finck         jsstr_t *x, *y;
661c2c66affSColin Finck 
662c2c66affSColin Finck         hres = to_string(ctx, v1, &x);
663c2c66affSColin Finck         if(FAILED(hres))
664c2c66affSColin Finck             return hres;
665c2c66affSColin Finck 
666c2c66affSColin Finck         hres = to_string(ctx, v2, &y);
667c2c66affSColin Finck         if(SUCCEEDED(hres)) {
668c2c66affSColin Finck             *cmp = jsstr_cmp(x, y);
669c2c66affSColin Finck             jsstr_release(y);
670c2c66affSColin Finck         }
671c2c66affSColin Finck         jsstr_release(x);
672c2c66affSColin Finck         if(FAILED(hres))
673c2c66affSColin Finck             return hres;
674c2c66affSColin Finck     }
675c2c66affSColin Finck 
676c2c66affSColin Finck     return S_OK;
677c2c66affSColin Finck }
678c2c66affSColin Finck 
679c2c66affSColin Finck /* ECMA-262 3rd Edition    15.4.4.11 */
Array_sort(script_ctx_t * ctx,vdisp_t * vthis,WORD flags,unsigned argc,jsval_t * argv,jsval_t * r)680c2c66affSColin Finck static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
681c2c66affSColin Finck         jsval_t *r)
682c2c66affSColin Finck {
683c2c66affSColin Finck     jsdisp_t *jsthis, *cmp_func = NULL;
684c2c66affSColin Finck     jsval_t *vtab, **sorttab = NULL;
685c2c66affSColin Finck     DWORD length;
686c2c66affSColin Finck     DWORD i;
687c2c66affSColin Finck     HRESULT hres = S_OK;
688c2c66affSColin Finck 
689c2c66affSColin Finck     TRACE("\n");
690c2c66affSColin Finck 
691c2c66affSColin Finck     hres = get_length(ctx, vthis, &jsthis, &length);
692c2c66affSColin Finck     if(FAILED(hres))
693c2c66affSColin Finck         return hres;
694c2c66affSColin Finck 
695c2c66affSColin Finck     if(argc > 1) {
696c2c66affSColin Finck         WARN("invalid arg_cnt %d\n", argc);
697c2c66affSColin Finck         return E_FAIL;
698c2c66affSColin Finck     }
699c2c66affSColin Finck 
700c2c66affSColin Finck     if(argc == 1) {
701c2c66affSColin Finck         if(!is_object_instance(argv[0])) {
702c2c66affSColin Finck             WARN("arg is not dispatch\n");
703c2c66affSColin Finck             return E_FAIL;
704c2c66affSColin Finck         }
705c2c66affSColin Finck 
706c2c66affSColin Finck         cmp_func = iface_to_jsdisp(get_object(argv[0]));
707c2c66affSColin Finck         if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
708c2c66affSColin Finck             WARN("cmp_func is not a function\n");
709c2c66affSColin Finck             if(cmp_func)
710c2c66affSColin Finck                 jsdisp_release(cmp_func);
711c2c66affSColin Finck             return E_FAIL;
712c2c66affSColin Finck         }
713c2c66affSColin Finck     }
714c2c66affSColin Finck 
715c2c66affSColin Finck     if(!length) {
716c2c66affSColin Finck         if(cmp_func)
717c2c66affSColin Finck             jsdisp_release(cmp_func);
718c2c66affSColin Finck         if(r)
719c2c66affSColin Finck             *r = jsval_obj(jsdisp_addref(jsthis));
720c2c66affSColin Finck         return S_OK;
721c2c66affSColin Finck     }
722c2c66affSColin Finck 
723c2c66affSColin Finck     vtab = heap_alloc_zero(length * sizeof(*vtab));
724c2c66affSColin Finck     if(vtab) {
725c2c66affSColin Finck         for(i=0; i<length; i++) {
726c2c66affSColin Finck             hres = jsdisp_get_idx(jsthis, i, vtab+i);
727c2c66affSColin Finck             if(hres == DISP_E_UNKNOWNNAME) {
728c2c66affSColin Finck                 vtab[i] = jsval_undefined();
729c2c66affSColin Finck                 hres = S_OK;
730c2c66affSColin Finck             } else if(FAILED(hres)) {
731c2c66affSColin Finck                 WARN("Could not get elem %d: %08x\n", i, hres);
732c2c66affSColin Finck                 break;
733c2c66affSColin Finck             }
734c2c66affSColin Finck         }
735c2c66affSColin Finck     }else {
736c2c66affSColin Finck         hres = E_OUTOFMEMORY;
737c2c66affSColin Finck     }
738c2c66affSColin Finck 
739c2c66affSColin Finck     if(SUCCEEDED(hres)) {
740c2c66affSColin Finck         sorttab = heap_alloc(length*2*sizeof(*sorttab));
741c2c66affSColin Finck         if(!sorttab)
742c2c66affSColin Finck             hres = E_OUTOFMEMORY;
743c2c66affSColin Finck     }
744c2c66affSColin Finck 
745c2c66affSColin Finck     /* merge-sort */
746c2c66affSColin Finck     if(SUCCEEDED(hres)) {
747c2c66affSColin Finck         jsval_t *tmpv, **tmpbuf;
748c2c66affSColin Finck         INT cmp;
749c2c66affSColin Finck 
750c2c66affSColin Finck         tmpbuf = sorttab + length;
751c2c66affSColin Finck         for(i=0; i < length; i++)
752c2c66affSColin Finck             sorttab[i] = vtab+i;
753c2c66affSColin Finck 
754c2c66affSColin Finck         for(i=0; i < length/2; i++) {
755c2c66affSColin Finck             hres = sort_cmp(ctx, cmp_func, *sorttab[2*i+1], *sorttab[2*i], &cmp);
756c2c66affSColin Finck             if(FAILED(hres))
757c2c66affSColin Finck                 break;
758c2c66affSColin Finck 
759c2c66affSColin Finck             if(cmp < 0) {
760c2c66affSColin Finck                 tmpv = sorttab[2*i];
761c2c66affSColin Finck                 sorttab[2*i] = sorttab[2*i+1];
762c2c66affSColin Finck                 sorttab[2*i+1] = tmpv;
763c2c66affSColin Finck             }
764c2c66affSColin Finck         }
765c2c66affSColin Finck 
766c2c66affSColin Finck         if(SUCCEEDED(hres)) {
767c2c66affSColin Finck             DWORD k, a, b, bend;
768c2c66affSColin Finck 
769c2c66affSColin Finck             for(k=2; k < length; k *= 2) {
770c2c66affSColin Finck                 for(i=0; i+k < length; i += 2*k) {
771c2c66affSColin Finck                     a = b = 0;
772c2c66affSColin Finck                     if(i+2*k <= length)
773c2c66affSColin Finck                         bend = k;
774c2c66affSColin Finck                     else
775c2c66affSColin Finck                         bend = length - (i+k);
776c2c66affSColin Finck 
777c2c66affSColin Finck                     memcpy(tmpbuf, sorttab+i, k*sizeof(jsval_t*));
778c2c66affSColin Finck 
779c2c66affSColin Finck                     while(a < k && b < bend) {
780c2c66affSColin Finck                         hres = sort_cmp(ctx, cmp_func, *tmpbuf[a], *sorttab[i+k+b], &cmp);
781c2c66affSColin Finck                         if(FAILED(hres))
782c2c66affSColin Finck                             break;
783c2c66affSColin Finck 
784c2c66affSColin Finck                         if(cmp < 0) {
785c2c66affSColin Finck                             sorttab[i+a+b] = tmpbuf[a];
786c2c66affSColin Finck                             a++;
787c2c66affSColin Finck                         }else {
788c2c66affSColin Finck                             sorttab[i+a+b] = sorttab[i+k+b];
789c2c66affSColin Finck                             b++;
790c2c66affSColin Finck                         }
791c2c66affSColin Finck                     }
792c2c66affSColin Finck 
793c2c66affSColin Finck                     if(FAILED(hres))
794c2c66affSColin Finck                         break;
795c2c66affSColin Finck 
796c2c66affSColin Finck                     if(a < k)
797c2c66affSColin Finck                         memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(jsval_t*));
798c2c66affSColin Finck                 }
799c2c66affSColin Finck 
800c2c66affSColin Finck                 if(FAILED(hres))
801c2c66affSColin Finck                     break;
802c2c66affSColin Finck             }
803c2c66affSColin Finck         }
804c2c66affSColin Finck 
805c2c66affSColin Finck         for(i=0; SUCCEEDED(hres) && i < length; i++)
806c2c66affSColin Finck             hres = jsdisp_propput_idx(jsthis, i, *sorttab[i]);
807c2c66affSColin Finck     }
808c2c66affSColin Finck 
809c2c66affSColin Finck     if(vtab) {
810c2c66affSColin Finck         for(i=0; i < length; i++)
811c2c66affSColin Finck             jsval_release(vtab[i]);
812c2c66affSColin Finck         heap_free(vtab);
813c2c66affSColin Finck     }
814c2c66affSColin Finck     heap_free(sorttab);
815c2c66affSColin Finck     if(cmp_func)
816c2c66affSColin Finck         jsdisp_release(cmp_func);
817c2c66affSColin Finck 
818c2c66affSColin Finck     if(FAILED(hres))
819c2c66affSColin Finck         return hres;
820c2c66affSColin Finck 
821c2c66affSColin Finck     if(r)
822c2c66affSColin Finck         *r = jsval_obj(jsdisp_addref(jsthis));
823c2c66affSColin Finck     return S_OK;
824c2c66affSColin Finck }
825c2c66affSColin Finck 
826c2c66affSColin Finck /* ECMA-262 3rd Edition    15.4.4.12 */
Array_splice(script_ctx_t * ctx,vdisp_t * vthis,WORD flags,unsigned argc,jsval_t * argv,jsval_t * r)827c2c66affSColin Finck static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
828c2c66affSColin Finck         jsval_t *r)
829c2c66affSColin Finck {
830c2c66affSColin Finck     DWORD length, start=0, delete_cnt=0, i, add_args = 0;
831c2c66affSColin Finck     jsdisp_t *ret_array = NULL, *jsthis;
832c2c66affSColin Finck     jsval_t val;
833c2c66affSColin Finck     double d;
834c2c66affSColin Finck     int n;
835c2c66affSColin Finck     HRESULT hres = S_OK;
836c2c66affSColin Finck 
837c2c66affSColin Finck     TRACE("\n");
838c2c66affSColin Finck 
839c2c66affSColin Finck     hres = get_length(ctx, vthis, &jsthis, &length);
840c2c66affSColin Finck     if(FAILED(hres))
841c2c66affSColin Finck         return hres;
842c2c66affSColin Finck 
843c2c66affSColin Finck     if(argc) {
844c2c66affSColin Finck         hres = to_integer(ctx, argv[0], &d);
845c2c66affSColin Finck         if(FAILED(hres))
846c2c66affSColin Finck             return hres;
847c2c66affSColin Finck 
848c2c66affSColin Finck         if(is_int32(d)) {
849c2c66affSColin Finck             if((n = d) >= 0)
850c2c66affSColin Finck                 start = min(n, length);
851c2c66affSColin Finck             else
852c2c66affSColin Finck                 start = -n > length ? 0 : length + n;
853c2c66affSColin Finck         }else {
854c2c66affSColin Finck             start = d < 0.0 ? 0 : length;
855c2c66affSColin Finck         }
856c2c66affSColin Finck     }
857c2c66affSColin Finck 
858c2c66affSColin Finck     if(argc >= 2) {
859c2c66affSColin Finck         hres = to_integer(ctx, argv[1], &d);
860c2c66affSColin Finck         if(FAILED(hres))
861c2c66affSColin Finck             return hres;
862c2c66affSColin Finck 
863c2c66affSColin Finck         if(is_int32(d)) {
864c2c66affSColin Finck             if((n = d) > 0)
865c2c66affSColin Finck                 delete_cnt = min(n, length-start);
866c2c66affSColin Finck         }else if(d > 0.0) {
867c2c66affSColin Finck             delete_cnt = length-start;
868c2c66affSColin Finck         }
869c2c66affSColin Finck 
870c2c66affSColin Finck         add_args = argc-2;
871c2c66affSColin Finck     }
872c2c66affSColin Finck 
873c2c66affSColin Finck     if(r) {
874c2c66affSColin Finck         hres = create_array(ctx, 0, &ret_array);
875c2c66affSColin Finck         if(FAILED(hres))
876c2c66affSColin Finck             return hres;
877c2c66affSColin Finck 
878c2c66affSColin Finck         for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
879c2c66affSColin Finck             hres = jsdisp_get_idx(jsthis, start+i, &val);
880c2c66affSColin Finck             if(hres == DISP_E_UNKNOWNNAME) {
881c2c66affSColin Finck                 hres = S_OK;
882c2c66affSColin Finck             }else if(SUCCEEDED(hres)) {
883c2c66affSColin Finck                 hres = jsdisp_propput_idx(ret_array, i, val);
884c2c66affSColin Finck                 jsval_release(val);
885c2c66affSColin Finck             }
886c2c66affSColin Finck         }
887c2c66affSColin Finck 
888c2c66affSColin Finck         if(SUCCEEDED(hres))
889c2c66affSColin Finck             hres = jsdisp_propput_name(ret_array, lengthW, jsval_number(delete_cnt));
890c2c66affSColin Finck     }
891c2c66affSColin Finck 
892c2c66affSColin Finck     if(add_args < delete_cnt) {
893c2c66affSColin Finck         for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
894c2c66affSColin Finck             hres = jsdisp_get_idx(jsthis, i+delete_cnt, &val);
895c2c66affSColin Finck             if(hres == DISP_E_UNKNOWNNAME) {
896c2c66affSColin Finck                 hres = jsdisp_delete_idx(jsthis, i+add_args);
897c2c66affSColin Finck             }else if(SUCCEEDED(hres)) {
898c2c66affSColin Finck                 hres = jsdisp_propput_idx(jsthis, i+add_args, val);
899c2c66affSColin Finck                 jsval_release(val);
900c2c66affSColin Finck             }
901c2c66affSColin Finck         }
902c2c66affSColin Finck 
903c2c66affSColin Finck         for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
904c2c66affSColin Finck             hres = jsdisp_delete_idx(jsthis, i-1);
905c2c66affSColin Finck     }else if(add_args > delete_cnt) {
906c2c66affSColin Finck         for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
907c2c66affSColin Finck             hres = jsdisp_get_idx(jsthis, i+delete_cnt-1, &val);
908c2c66affSColin Finck             if(hres == DISP_E_UNKNOWNNAME) {
909c2c66affSColin Finck                 hres = jsdisp_delete_idx(jsthis, i+add_args-1);
910c2c66affSColin Finck             }else if(SUCCEEDED(hres)) {
911c2c66affSColin Finck                 hres = jsdisp_propput_idx(jsthis, i+add_args-1, val);
912c2c66affSColin Finck                 jsval_release(val);
913c2c66affSColin Finck             }
914c2c66affSColin Finck         }
915c2c66affSColin Finck     }
916c2c66affSColin Finck 
917c2c66affSColin Finck     for(i=0; SUCCEEDED(hres) && i < add_args; i++)
918c2c66affSColin Finck         hres = jsdisp_propput_idx(jsthis, start+i, argv[i+2]);
919c2c66affSColin Finck 
920c2c66affSColin Finck     if(SUCCEEDED(hres))
921c2c66affSColin Finck         hres = jsdisp_propput_name(jsthis, lengthW, jsval_number(length-delete_cnt+add_args));
922c2c66affSColin Finck 
923c2c66affSColin Finck     if(FAILED(hres)) {
924c2c66affSColin Finck         if(ret_array)
925c2c66affSColin Finck             jsdisp_release(ret_array);
926c2c66affSColin Finck         return hres;
927c2c66affSColin Finck     }
928c2c66affSColin Finck 
929c2c66affSColin Finck     if(r)
930c2c66affSColin Finck         *r = jsval_obj(ret_array);
931c2c66affSColin Finck     return S_OK;
932c2c66affSColin Finck }
933c2c66affSColin Finck 
934c2c66affSColin Finck /* ECMA-262 3rd Edition    15.4.4.2 */
Array_toString(script_ctx_t * ctx,vdisp_t * jsthis,WORD flags,unsigned argc,jsval_t * argv,jsval_t * r)935c2c66affSColin Finck static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
936c2c66affSColin Finck         jsval_t *r)
937c2c66affSColin Finck {
938c2c66affSColin Finck     ArrayInstance *array;
939c2c66affSColin Finck 
940c2c66affSColin Finck     TRACE("\n");
941c2c66affSColin Finck 
942c2c66affSColin Finck     array = array_this(jsthis);
943c2c66affSColin Finck     if(!array)
944c2c66affSColin Finck         return throw_type_error(ctx, JS_E_ARRAY_EXPECTED, NULL);
945c2c66affSColin Finck 
946c2c66affSColin Finck     return array_join(ctx, &array->dispex, array->length, default_separatorW,
947*3e2d6582SAmine Khaldi                       lstrlenW(default_separatorW), r);
948c2c66affSColin Finck }
949c2c66affSColin Finck 
Array_toLocaleString(script_ctx_t * ctx,vdisp_t * vthis,WORD flags,unsigned argc,jsval_t * argv,jsval_t * r)950c2c66affSColin Finck static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
951c2c66affSColin Finck         jsval_t *r)
952c2c66affSColin Finck {
953c2c66affSColin Finck     FIXME("\n");
954c2c66affSColin Finck     return E_NOTIMPL;
955c2c66affSColin Finck }
956c2c66affSColin Finck 
Array_forEach(script_ctx_t * ctx,vdisp_t * vthis,WORD flags,unsigned argc,jsval_t * argv,jsval_t * r)957660f7b90SAmine Khaldi static HRESULT Array_forEach(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
958660f7b90SAmine Khaldi         jsval_t *r)
959660f7b90SAmine Khaldi {
960660f7b90SAmine Khaldi     jsval_t value, args[3], res;
961660f7b90SAmine Khaldi     jsdisp_t *jsthis;
962660f7b90SAmine Khaldi     unsigned length, i;
963660f7b90SAmine Khaldi     HRESULT hres;
964660f7b90SAmine Khaldi 
965660f7b90SAmine Khaldi     TRACE("\n");
966660f7b90SAmine Khaldi 
967660f7b90SAmine Khaldi     hres = get_length(ctx, vthis, &jsthis, &length);
968660f7b90SAmine Khaldi     if(FAILED(hres))
969660f7b90SAmine Khaldi         return hres;
970660f7b90SAmine Khaldi 
971*3e2d6582SAmine Khaldi     /* Fixme check IsCallable */
972*3e2d6582SAmine Khaldi     if(!argc || !is_object_instance(argv[0]) || !get_object(argv[0])) {
973*3e2d6582SAmine Khaldi         FIXME("Invalid arg %s\n", debugstr_jsval(argc ? argv[0] : jsval_undefined()));
974*3e2d6582SAmine Khaldi         return E_INVALIDARG;
975*3e2d6582SAmine Khaldi     }
976*3e2d6582SAmine Khaldi 
977*3e2d6582SAmine Khaldi     if(argc > 1 && !is_undefined(argv[1])) {
978*3e2d6582SAmine Khaldi         FIXME("Unsupported context this %s\n", debugstr_jsval(argv[1]));
979*3e2d6582SAmine Khaldi         return E_NOTIMPL;
980*3e2d6582SAmine Khaldi     }
981*3e2d6582SAmine Khaldi 
982660f7b90SAmine Khaldi     for(i = 0; i < length; i++) {
983660f7b90SAmine Khaldi         hres = jsdisp_get_idx(jsthis, i, &value);
984660f7b90SAmine Khaldi         if(hres == DISP_E_UNKNOWNNAME)
985660f7b90SAmine Khaldi             continue;
986660f7b90SAmine Khaldi         if(FAILED(hres))
987660f7b90SAmine Khaldi             return hres;
988660f7b90SAmine Khaldi 
989660f7b90SAmine Khaldi         args[0] = value;
990660f7b90SAmine Khaldi         args[1] = jsval_number(i);
991660f7b90SAmine Khaldi         args[2] = jsval_obj(jsthis);
992660f7b90SAmine Khaldi         hres = disp_call_value(ctx, get_object(argv[0]), NULL, DISPATCH_METHOD, ARRAY_SIZE(args), args, &res);
993660f7b90SAmine Khaldi         jsval_release(value);
994660f7b90SAmine Khaldi         if(FAILED(hres))
995660f7b90SAmine Khaldi             return hres;
996660f7b90SAmine Khaldi         jsval_release(res);
997660f7b90SAmine Khaldi     }
998660f7b90SAmine Khaldi 
999660f7b90SAmine Khaldi     if(r) *r = jsval_undefined();
1000660f7b90SAmine Khaldi     return S_OK;
1001660f7b90SAmine Khaldi }
1002660f7b90SAmine Khaldi 
Array_indexOf(script_ctx_t * ctx,vdisp_t * vthis,WORD flags,unsigned argc,jsval_t * argv,jsval_t * r)10038dba275bSAmine Khaldi static HRESULT Array_indexOf(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
10048dba275bSAmine Khaldi         jsval_t *r)
10058dba275bSAmine Khaldi {
10068dba275bSAmine Khaldi     jsdisp_t *jsthis;
10078dba275bSAmine Khaldi     unsigned length, i, from = 0;
10088dba275bSAmine Khaldi     jsval_t search, value;
10098dba275bSAmine Khaldi     BOOL eq;
10108dba275bSAmine Khaldi     HRESULT hres;
10118dba275bSAmine Khaldi 
10128dba275bSAmine Khaldi     TRACE("\n");
10138dba275bSAmine Khaldi 
10148dba275bSAmine Khaldi     hres = get_length(ctx, vthis, &jsthis, &length);
10158dba275bSAmine Khaldi     if(FAILED(hres))
10168dba275bSAmine Khaldi         return hres;
10178dba275bSAmine Khaldi     if(!length) {
10188dba275bSAmine Khaldi         if(r) *r = jsval_number(-1);
10198dba275bSAmine Khaldi         return S_OK;
10208dba275bSAmine Khaldi     }
10218dba275bSAmine Khaldi 
10228dba275bSAmine Khaldi     search = argc ? argv[0] : jsval_undefined();
10238dba275bSAmine Khaldi 
10248dba275bSAmine Khaldi     if(argc > 1) {
10258dba275bSAmine Khaldi         double from_arg;
10268dba275bSAmine Khaldi 
10278dba275bSAmine Khaldi         hres = to_integer(ctx, argv[1], &from_arg);
10288dba275bSAmine Khaldi         if(FAILED(hres))
10298dba275bSAmine Khaldi             return hres;
10308dba275bSAmine Khaldi 
10318dba275bSAmine Khaldi         if(from_arg >= 0)
10328dba275bSAmine Khaldi             from = min(from_arg, length);
10338dba275bSAmine Khaldi         else
10348dba275bSAmine Khaldi             from = max(from_arg + length, 0);
10358dba275bSAmine Khaldi     }
10368dba275bSAmine Khaldi 
10378dba275bSAmine Khaldi     for(i = from; i < length; i++) {
10388dba275bSAmine Khaldi         hres = jsdisp_get_idx(jsthis, i, &value);
10398dba275bSAmine Khaldi         if(hres == DISP_E_UNKNOWNNAME)
10408dba275bSAmine Khaldi             continue;
10418dba275bSAmine Khaldi         if(FAILED(hres))
10428dba275bSAmine Khaldi             return hres;
10438dba275bSAmine Khaldi 
10448dba275bSAmine Khaldi         hres = jsval_strict_equal(value, search, &eq);
10458dba275bSAmine Khaldi         jsval_release(value);
10468dba275bSAmine Khaldi         if(FAILED(hres))
10478dba275bSAmine Khaldi             return hres;
10488dba275bSAmine Khaldi         if(eq) {
10498dba275bSAmine Khaldi             if(r) *r = jsval_number(i);
10508dba275bSAmine Khaldi             return S_OK;
10518dba275bSAmine Khaldi         }
10528dba275bSAmine Khaldi     }
10538dba275bSAmine Khaldi 
10548dba275bSAmine Khaldi     if(r) *r = jsval_number(-1);
10558dba275bSAmine Khaldi     return S_OK;
10568dba275bSAmine Khaldi }
10578dba275bSAmine Khaldi 
Array_map(script_ctx_t * ctx,vdisp_t * vthis,WORD flags,unsigned argc,jsval_t * argv,jsval_t * r)1058*3e2d6582SAmine Khaldi static HRESULT Array_map(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
1059*3e2d6582SAmine Khaldi {
1060*3e2d6582SAmine Khaldi     IDispatch *context_this = NULL, *callback;
1061*3e2d6582SAmine Khaldi     jsval_t callback_args[3], mapped_value;
1062*3e2d6582SAmine Khaldi     jsdisp_t *jsthis, *array;
1063*3e2d6582SAmine Khaldi     DWORD length, k;
1064*3e2d6582SAmine Khaldi     HRESULT hres;
1065*3e2d6582SAmine Khaldi 
1066*3e2d6582SAmine Khaldi     TRACE("\n");
1067*3e2d6582SAmine Khaldi 
1068*3e2d6582SAmine Khaldi     hres = get_length(ctx, vthis, &jsthis, &length);
1069*3e2d6582SAmine Khaldi     if(FAILED(hres)) {
1070*3e2d6582SAmine Khaldi         FIXME("Could not get length\n");
1071*3e2d6582SAmine Khaldi         return hres;
1072*3e2d6582SAmine Khaldi     }
1073*3e2d6582SAmine Khaldi 
1074*3e2d6582SAmine Khaldi     /* Fixme check IsCallable */
1075*3e2d6582SAmine Khaldi     if(!argc || !is_object_instance(argv[0]) || !get_object(argv[0])) {
1076*3e2d6582SAmine Khaldi         FIXME("Invalid arg %s\n", debugstr_jsval(argc ? argv[0] : jsval_undefined()));
1077*3e2d6582SAmine Khaldi         return E_INVALIDARG;
1078*3e2d6582SAmine Khaldi     }
1079*3e2d6582SAmine Khaldi     callback = get_object(argv[0]);
1080*3e2d6582SAmine Khaldi 
1081*3e2d6582SAmine Khaldi     if(argc > 1) {
1082*3e2d6582SAmine Khaldi         if(is_object_instance(argv[1]) && get_object(argv[1])) {
1083*3e2d6582SAmine Khaldi             context_this = get_object(argv[1]);
1084*3e2d6582SAmine Khaldi         }else if(!is_undefined(argv[1])) {
1085*3e2d6582SAmine Khaldi             FIXME("Unsupported context this %s\n", debugstr_jsval(argv[1]));
1086*3e2d6582SAmine Khaldi             return E_NOTIMPL;
1087*3e2d6582SAmine Khaldi         }
1088*3e2d6582SAmine Khaldi     }
1089*3e2d6582SAmine Khaldi 
1090*3e2d6582SAmine Khaldi     hres = create_array(ctx, length, &array);
1091*3e2d6582SAmine Khaldi     if(FAILED(hres))
1092*3e2d6582SAmine Khaldi         return hres;
1093*3e2d6582SAmine Khaldi 
1094*3e2d6582SAmine Khaldi     for(k = 0; k < length; k++) {
1095*3e2d6582SAmine Khaldi         hres = jsdisp_get_idx(jsthis, k, &callback_args[0]);
1096*3e2d6582SAmine Khaldi         if(hres == DISP_E_UNKNOWNNAME)
1097*3e2d6582SAmine Khaldi             continue;
1098*3e2d6582SAmine Khaldi         if(FAILED(hres))
1099*3e2d6582SAmine Khaldi             break;
1100*3e2d6582SAmine Khaldi 
1101*3e2d6582SAmine Khaldi         callback_args[1] = jsval_number(k);
1102*3e2d6582SAmine Khaldi         callback_args[2] = jsval_obj(jsthis);
1103*3e2d6582SAmine Khaldi         hres = disp_call_value(ctx, callback, context_this, DISPATCH_METHOD, 3, callback_args, &mapped_value);
1104*3e2d6582SAmine Khaldi         jsval_release(callback_args[0]);
1105*3e2d6582SAmine Khaldi         if(FAILED(hres))
1106*3e2d6582SAmine Khaldi             break;
1107*3e2d6582SAmine Khaldi 
1108*3e2d6582SAmine Khaldi         hres = jsdisp_propput_idx(array, k, mapped_value);
1109*3e2d6582SAmine Khaldi         if(FAILED(hres))
1110*3e2d6582SAmine Khaldi             break;
1111*3e2d6582SAmine Khaldi     }
1112*3e2d6582SAmine Khaldi 
1113*3e2d6582SAmine Khaldi     if(SUCCEEDED(hres) && r)
1114*3e2d6582SAmine Khaldi         *r = jsval_obj(array);
1115*3e2d6582SAmine Khaldi     else
1116*3e2d6582SAmine Khaldi         jsdisp_release(array);
1117*3e2d6582SAmine Khaldi     return hres;
1118*3e2d6582SAmine Khaldi }
1119*3e2d6582SAmine Khaldi 
1120c2c66affSColin Finck /* ECMA-262 3rd Edition    15.4.4.13 */
Array_unshift(script_ctx_t * ctx,vdisp_t * vthis,WORD flags,unsigned argc,jsval_t * argv,jsval_t * r)1121c2c66affSColin Finck static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
1122c2c66affSColin Finck         jsval_t *r)
1123c2c66affSColin Finck {
1124c2c66affSColin Finck     jsdisp_t *jsthis;
1125c2c66affSColin Finck     WCHAR buf[14], *buf_end, *str;
1126c2c66affSColin Finck     DWORD i, length;
1127c2c66affSColin Finck     jsval_t val;
1128c2c66affSColin Finck     DISPID id;
1129c2c66affSColin Finck     HRESULT hres;
1130c2c66affSColin Finck 
1131c2c66affSColin Finck     TRACE("\n");
1132c2c66affSColin Finck 
1133c2c66affSColin Finck     hres = get_length(ctx, vthis, &jsthis, &length);
1134c2c66affSColin Finck     if(FAILED(hres))
1135c2c66affSColin Finck         return hres;
1136c2c66affSColin Finck 
1137c2c66affSColin Finck     if(argc) {
1138660f7b90SAmine Khaldi         buf_end = buf + ARRAY_SIZE(buf)-1;
1139c2c66affSColin Finck         *buf_end-- = 0;
1140c2c66affSColin Finck         i = length;
1141c2c66affSColin Finck 
1142c2c66affSColin Finck         while(i--) {
1143c2c66affSColin Finck             str = idx_to_str(i, buf_end);
1144c2c66affSColin Finck 
1145c2c66affSColin Finck             hres = jsdisp_get_id(jsthis, str, 0, &id);
1146c2c66affSColin Finck             if(SUCCEEDED(hres)) {
1147c2c66affSColin Finck                 hres = jsdisp_propget(jsthis, id, &val);
1148c2c66affSColin Finck                 if(FAILED(hres))
1149c2c66affSColin Finck                     return hres;
1150c2c66affSColin Finck 
1151c2c66affSColin Finck                 hres = jsdisp_propput_idx(jsthis, i+argc, val);
1152c2c66affSColin Finck                 jsval_release(val);
1153c2c66affSColin Finck             }else if(hres == DISP_E_UNKNOWNNAME) {
1154c2c66affSColin Finck                 hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
1155c2c66affSColin Finck             }
1156c2c66affSColin Finck         }
1157c2c66affSColin Finck 
1158c2c66affSColin Finck         if(FAILED(hres))
1159c2c66affSColin Finck             return hres;
1160c2c66affSColin Finck     }
1161c2c66affSColin Finck 
1162c2c66affSColin Finck     for(i=0; i<argc; i++) {
1163c2c66affSColin Finck         hres = jsdisp_propput_idx(jsthis, i, argv[i]);
1164c2c66affSColin Finck         if(FAILED(hres))
1165c2c66affSColin Finck             return hres;
1166c2c66affSColin Finck     }
1167c2c66affSColin Finck 
1168c2c66affSColin Finck     if(argc) {
1169c2c66affSColin Finck         length += argc;
1170c2c66affSColin Finck         hres = set_length(jsthis, length);
1171c2c66affSColin Finck         if(FAILED(hres))
1172c2c66affSColin Finck             return hres;
1173c2c66affSColin Finck     }
1174c2c66affSColin Finck 
1175c2c66affSColin Finck     if(r)
1176c2c66affSColin Finck         *r = ctx->version < 2 ? jsval_undefined() : jsval_number(length);
1177c2c66affSColin Finck     return S_OK;
1178c2c66affSColin Finck }
1179c2c66affSColin Finck 
Array_get_value(script_ctx_t * ctx,jsdisp_t * jsthis,jsval_t * r)1180c2c66affSColin Finck static HRESULT Array_get_value(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
1181c2c66affSColin Finck {
1182c2c66affSColin Finck     ArrayInstance *array = array_from_jsdisp(jsthis);
1183c2c66affSColin Finck 
1184c2c66affSColin Finck     TRACE("\n");
1185c2c66affSColin Finck 
1186c2c66affSColin Finck     return array_join(ctx, &array->dispex, array->length, default_separatorW,
1187*3e2d6582SAmine Khaldi                       lstrlenW(default_separatorW), r);
1188c2c66affSColin Finck }
1189c2c66affSColin Finck 
Array_destructor(jsdisp_t * dispex)1190c2c66affSColin Finck static void Array_destructor(jsdisp_t *dispex)
1191c2c66affSColin Finck {
1192c2c66affSColin Finck     heap_free(dispex);
1193c2c66affSColin Finck }
1194c2c66affSColin Finck 
Array_on_put(jsdisp_t * dispex,const WCHAR * name)1195c2c66affSColin Finck static void Array_on_put(jsdisp_t *dispex, const WCHAR *name)
1196c2c66affSColin Finck {
1197c2c66affSColin Finck     ArrayInstance *array = array_from_jsdisp(dispex);
1198c2c66affSColin Finck     const WCHAR *ptr = name;
1199c2c66affSColin Finck     DWORD id = 0;
1200c2c66affSColin Finck 
1201*3e2d6582SAmine Khaldi     if(!iswdigit(*ptr))
1202c2c66affSColin Finck         return;
1203c2c66affSColin Finck 
1204*3e2d6582SAmine Khaldi     while(*ptr && iswdigit(*ptr)) {
1205c2c66affSColin Finck         id = id*10 + (*ptr-'0');
1206c2c66affSColin Finck         ptr++;
1207c2c66affSColin Finck     }
1208c2c66affSColin Finck 
1209c2c66affSColin Finck     if(*ptr)
1210c2c66affSColin Finck         return;
1211c2c66affSColin Finck 
1212c2c66affSColin Finck     if(id >= array->length)
1213c2c66affSColin Finck         array->length = id+1;
1214c2c66affSColin Finck }
1215c2c66affSColin Finck 
1216c2c66affSColin Finck static const builtin_prop_t Array_props[] = {
1217c2c66affSColin Finck     {concatW,                Array_concat,               PROPF_METHOD|1},
1218660f7b90SAmine Khaldi     {forEachW,               Array_forEach,              PROPF_METHOD|PROPF_ES5|1},
1219660f7b90SAmine Khaldi     {indexOfW,               Array_indexOf,              PROPF_METHOD|PROPF_ES5|1},
1220c2c66affSColin Finck     {joinW,                  Array_join,                 PROPF_METHOD|1},
1221c2c66affSColin Finck     {lengthW,                NULL,0,                     Array_get_length, Array_set_length},
1222*3e2d6582SAmine Khaldi     {mapW,                   Array_map,                  PROPF_METHOD|PROPF_ES5|1},
1223c2c66affSColin Finck     {popW,                   Array_pop,                  PROPF_METHOD},
1224c2c66affSColin Finck     {pushW,                  Array_push,                 PROPF_METHOD|1},
1225c2c66affSColin Finck     {reverseW,               Array_reverse,              PROPF_METHOD},
1226c2c66affSColin Finck     {shiftW,                 Array_shift,                PROPF_METHOD},
1227c2c66affSColin Finck     {sliceW,                 Array_slice,                PROPF_METHOD|2},
1228c2c66affSColin Finck     {sortW,                  Array_sort,                 PROPF_METHOD|1},
1229c2c66affSColin Finck     {spliceW,                Array_splice,               PROPF_METHOD|2},
1230c2c66affSColin Finck     {toLocaleStringW,        Array_toLocaleString,       PROPF_METHOD},
1231c2c66affSColin Finck     {toStringW,              Array_toString,             PROPF_METHOD},
1232c2c66affSColin Finck     {unshiftW,               Array_unshift,              PROPF_METHOD|1},
1233c2c66affSColin Finck };
1234c2c66affSColin Finck 
1235c2c66affSColin Finck static const builtin_info_t Array_info = {
1236c2c66affSColin Finck     JSCLASS_ARRAY,
1237c2c66affSColin Finck     {NULL, NULL,0, Array_get_value},
1238660f7b90SAmine Khaldi     ARRAY_SIZE(Array_props),
1239c2c66affSColin Finck     Array_props,
1240c2c66affSColin Finck     Array_destructor,
1241c2c66affSColin Finck     Array_on_put
1242c2c66affSColin Finck };
1243c2c66affSColin Finck 
1244c2c66affSColin Finck static const builtin_prop_t ArrayInst_props[] = {
1245c2c66affSColin Finck     {lengthW,                NULL,0,                     Array_get_length, Array_set_length}
1246c2c66affSColin Finck };
1247c2c66affSColin Finck 
1248c2c66affSColin Finck static const builtin_info_t ArrayInst_info = {
1249c2c66affSColin Finck     JSCLASS_ARRAY,
1250c2c66affSColin Finck     {NULL, NULL,0, Array_get_value},
1251660f7b90SAmine Khaldi     ARRAY_SIZE(ArrayInst_props),
1252c2c66affSColin Finck     ArrayInst_props,
1253c2c66affSColin Finck     Array_destructor,
1254c2c66affSColin Finck     Array_on_put
1255c2c66affSColin Finck };
1256c2c66affSColin Finck 
12578dba275bSAmine Khaldi /* ECMA-262 5.1 Edition    15.4.3.2 */
ArrayConstr_isArray(script_ctx_t * ctx,vdisp_t * vthis,WORD flags,unsigned argc,jsval_t * argv,jsval_t * r)12588dba275bSAmine Khaldi static HRESULT ArrayConstr_isArray(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
12598dba275bSAmine Khaldi {
12608dba275bSAmine Khaldi     jsdisp_t *obj;
12618dba275bSAmine Khaldi 
12628dba275bSAmine Khaldi     TRACE("\n");
12638dba275bSAmine Khaldi 
12648dba275bSAmine Khaldi     if(!argc || !is_object_instance(argv[0])) {
12658dba275bSAmine Khaldi         if(r) *r = jsval_bool(FALSE);
12668dba275bSAmine Khaldi         return S_OK;
12678dba275bSAmine Khaldi     }
12688dba275bSAmine Khaldi 
12698dba275bSAmine Khaldi     obj = iface_to_jsdisp(get_object(argv[0]));
12708dba275bSAmine Khaldi     if(r) *r = jsval_bool(obj && is_class(obj, JSCLASS_ARRAY));
12718dba275bSAmine Khaldi     if(obj) jsdisp_release(obj);
12728dba275bSAmine Khaldi     return S_OK;
12738dba275bSAmine Khaldi }
12748dba275bSAmine Khaldi 
ArrayConstr_value(script_ctx_t * ctx,vdisp_t * vthis,WORD flags,unsigned argc,jsval_t * argv,jsval_t * r)1275c2c66affSColin Finck static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
1276c2c66affSColin Finck         jsval_t *r)
1277c2c66affSColin Finck {
1278c2c66affSColin Finck     jsdisp_t *obj;
1279c2c66affSColin Finck     DWORD i;
1280c2c66affSColin Finck     HRESULT hres;
1281c2c66affSColin Finck 
1282c2c66affSColin Finck     TRACE("\n");
1283c2c66affSColin Finck 
1284c2c66affSColin Finck     switch(flags) {
1285c2c66affSColin Finck     case DISPATCH_METHOD:
1286c2c66affSColin Finck     case DISPATCH_CONSTRUCT: {
1287c2c66affSColin Finck         if(argc == 1 && is_number(argv[0])) {
1288c2c66affSColin Finck             double n = get_number(argv[0]);
1289c2c66affSColin Finck 
1290c2c66affSColin Finck             if(n < 0 || !is_int32(n))
1291c2c66affSColin Finck                 return throw_range_error(ctx, JS_E_INVALID_LENGTH, NULL);
1292c2c66affSColin Finck 
1293c2c66affSColin Finck             hres = create_array(ctx, n, &obj);
1294c2c66affSColin Finck             if(FAILED(hres))
1295c2c66affSColin Finck                 return hres;
1296c2c66affSColin Finck 
1297c2c66affSColin Finck             *r = jsval_obj(obj);
1298c2c66affSColin Finck             return S_OK;
1299c2c66affSColin Finck         }
1300c2c66affSColin Finck 
1301c2c66affSColin Finck         hres = create_array(ctx, argc, &obj);
1302c2c66affSColin Finck         if(FAILED(hres))
1303c2c66affSColin Finck             return hres;
1304c2c66affSColin Finck 
1305c2c66affSColin Finck         for(i=0; i < argc; i++) {
1306c2c66affSColin Finck             hres = jsdisp_propput_idx(obj, i, argv[i]);
1307c2c66affSColin Finck             if(FAILED(hres))
1308c2c66affSColin Finck                 break;
1309c2c66affSColin Finck         }
1310c2c66affSColin Finck         if(FAILED(hres)) {
1311c2c66affSColin Finck             jsdisp_release(obj);
1312c2c66affSColin Finck             return hres;
1313c2c66affSColin Finck         }
1314c2c66affSColin Finck 
1315c2c66affSColin Finck         *r = jsval_obj(obj);
1316c2c66affSColin Finck         break;
1317c2c66affSColin Finck     }
1318c2c66affSColin Finck     default:
1319c2c66affSColin Finck         FIXME("unimplemented flags: %x\n", flags);
1320c2c66affSColin Finck         return E_NOTIMPL;
1321c2c66affSColin Finck     }
1322c2c66affSColin Finck 
1323c2c66affSColin Finck     return S_OK;
1324c2c66affSColin Finck }
1325c2c66affSColin Finck 
alloc_array(script_ctx_t * ctx,jsdisp_t * object_prototype,ArrayInstance ** ret)1326c2c66affSColin Finck static HRESULT alloc_array(script_ctx_t *ctx, jsdisp_t *object_prototype, ArrayInstance **ret)
1327c2c66affSColin Finck {
1328c2c66affSColin Finck     ArrayInstance *array;
1329c2c66affSColin Finck     HRESULT hres;
1330c2c66affSColin Finck 
1331c2c66affSColin Finck     array = heap_alloc_zero(sizeof(ArrayInstance));
1332c2c66affSColin Finck     if(!array)
1333c2c66affSColin Finck         return E_OUTOFMEMORY;
1334c2c66affSColin Finck 
1335c2c66affSColin Finck     if(object_prototype)
1336c2c66affSColin Finck         hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1337c2c66affSColin Finck     else
1338c2c66affSColin Finck         hres = init_dispex_from_constr(&array->dispex, ctx, &ArrayInst_info, ctx->array_constr);
1339c2c66affSColin Finck 
1340c2c66affSColin Finck     if(FAILED(hres)) {
1341c2c66affSColin Finck         heap_free(array);
1342c2c66affSColin Finck         return hres;
1343c2c66affSColin Finck     }
1344c2c66affSColin Finck 
1345c2c66affSColin Finck     *ret = array;
1346c2c66affSColin Finck     return S_OK;
1347c2c66affSColin Finck }
1348c2c66affSColin Finck 
13498dba275bSAmine Khaldi static const WCHAR isArrayW[] = {'i','s','A','r','r','a','y',0};
13508dba275bSAmine Khaldi 
13518dba275bSAmine Khaldi static const builtin_prop_t ArrayConstr_props[] = {
13528dba275bSAmine Khaldi     {isArrayW,    ArrayConstr_isArray,    PROPF_ES5|PROPF_METHOD|1}
13538dba275bSAmine Khaldi };
13548dba275bSAmine Khaldi 
13558dba275bSAmine Khaldi static const builtin_info_t ArrayConstr_info = {
13568dba275bSAmine Khaldi     JSCLASS_FUNCTION,
13578dba275bSAmine Khaldi     DEFAULT_FUNCTION_VALUE,
1358660f7b90SAmine Khaldi     ARRAY_SIZE(ArrayConstr_props),
13598dba275bSAmine Khaldi     ArrayConstr_props,
13608dba275bSAmine Khaldi     NULL,
13618dba275bSAmine Khaldi     NULL
13628dba275bSAmine Khaldi };
13638dba275bSAmine Khaldi 
create_array_constr(script_ctx_t * ctx,jsdisp_t * object_prototype,jsdisp_t ** ret)1364c2c66affSColin Finck HRESULT create_array_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1365c2c66affSColin Finck {
1366c2c66affSColin Finck     ArrayInstance *array;
1367c2c66affSColin Finck     HRESULT hres;
1368c2c66affSColin Finck 
1369c2c66affSColin Finck     static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1370c2c66affSColin Finck 
1371c2c66affSColin Finck     hres = alloc_array(ctx, object_prototype, &array);
1372c2c66affSColin Finck     if(FAILED(hres))
1373c2c66affSColin Finck         return hres;
1374c2c66affSColin Finck 
13758dba275bSAmine Khaldi     hres = create_builtin_constructor(ctx, ArrayConstr_value, ArrayW, &ArrayConstr_info, PROPF_CONSTR|1, &array->dispex, ret);
1376c2c66affSColin Finck 
1377c2c66affSColin Finck     jsdisp_release(&array->dispex);
1378c2c66affSColin Finck     return hres;
1379c2c66affSColin Finck }
1380c2c66affSColin Finck 
create_array(script_ctx_t * ctx,DWORD length,jsdisp_t ** ret)1381c2c66affSColin Finck HRESULT create_array(script_ctx_t *ctx, DWORD length, jsdisp_t **ret)
1382c2c66affSColin Finck {
1383c2c66affSColin Finck     ArrayInstance *array;
1384c2c66affSColin Finck     HRESULT hres;
1385c2c66affSColin Finck 
1386c2c66affSColin Finck     hres = alloc_array(ctx, NULL, &array);
1387c2c66affSColin Finck     if(FAILED(hres))
1388c2c66affSColin Finck         return hres;
1389c2c66affSColin Finck 
1390c2c66affSColin Finck     array->length = length;
1391c2c66affSColin Finck 
1392c2c66affSColin Finck     *ret = &array->dispex;
1393c2c66affSColin Finck     return S_OK;
1394c2c66affSColin Finck }
1395