2 * Copyright 2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/debug.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
31 static const WCHAR 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};
50 const WCHAR default_separatorW[] = {',',0};
52 static HRESULT Array_length(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
53 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
55 ArrayInstance *This = (ArrayInstance*)dispex;
57 TRACE("%p %d\n", This, This->length);
60 case DISPATCH_PROPERTYGET:
62 V_I4(retv) = This->length;
65 FIXME("unimplemented flags %x\n", flags);
72 static HRESULT Array_concat(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
73 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
79 static HRESULT array_join(DispatchEx *array, LCID lcid, DWORD length, const WCHAR *sep, VARIANT *retv,
80 jsexcept_t *ei, IServiceProvider *caller)
82 BSTR *str_tab, ret = NULL;
85 HRESULT hres = E_FAIL;
90 V_BSTR(retv) = SysAllocStringLen(NULL, 0);
97 str_tab = heap_alloc_zero(length * sizeof(BSTR));
101 for(i=0; i < length; i++) {
102 hres = jsdisp_propget_idx(array, i, lcid, &var, ei, caller);
106 if(V_VT(&var) != VT_EMPTY && V_VT(&var) != VT_NULL)
107 hres = to_string(array->ctx, &var, ei, str_tab+i);
113 if(SUCCEEDED(hres)) {
114 DWORD seplen = 0, len = 0;
117 seplen = strlenW(sep);
120 len = SysStringLen(str_tab[0]);
121 for(i=1; i < length; i++)
122 len += seplen + SysStringLen(str_tab[i]);
124 ret = SysAllocStringLen(NULL, len);
129 tmplen = SysStringLen(str_tab[0]);
130 memcpy(ret, str_tab[0], tmplen*sizeof(WCHAR));
134 for(i=1; i < length; i++) {
136 memcpy(ptr, sep, seplen*sizeof(WCHAR));
141 tmplen = SysStringLen(str_tab[i]);
142 memcpy(ptr, str_tab[i], tmplen*sizeof(WCHAR));
148 hres = E_OUTOFMEMORY;
152 for(i=0; i < length; i++)
153 SysFreeString(str_tab[i]);
158 TRACE("= %s\n", debugstr_w(ret));
162 ret = SysAllocStringLen(NULL, 0);
164 return E_OUTOFMEMORY;
167 V_VT(retv) = VT_BSTR;
176 /* ECMA-262 3rd Edition 15.4.4.5 */
177 static HRESULT Array_join(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
178 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
185 if(is_class(dispex, JSCLASS_ARRAY)) {
186 length = ((ArrayInstance*)dispex)->length;
188 FIXME("dispid is not Array\n");
195 hres = to_string(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &sep);
199 hres = array_join(dispex, lcid, length, sep, retv, ei, caller);
203 hres = array_join(dispex, lcid, length, default_separatorW, retv, ei, caller);
209 static HRESULT Array_pop(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
210 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
216 /* ECMA-262 3rd Edition 15.4.4.7 */
217 static HRESULT Array_push(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
218 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
226 if(dispex->builtin_info->class == JSCLASS_ARRAY) {
227 length = ((ArrayInstance*)dispex)->length;
229 FIXME("not Array this\n");
233 n = dp->cArgs - dp->cNamedArgs;
234 for(i=0; i < n; i++) {
235 hres = jsdisp_propput_idx(dispex, length+i, lcid, get_arg(dp, i), ei, sp);
242 V_I4(retv) = length+n;
247 static HRESULT Array_reverse(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
248 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
254 static HRESULT Array_shift(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
255 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
261 static HRESULT Array_slice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
262 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
268 static HRESULT Array_sort(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
269 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
275 static HRESULT Array_splice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
276 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
282 /* ECMA-262 3rd Edition 15.4.4.2 */
283 static HRESULT Array_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
284 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
288 if(!is_class(dispex, JSCLASS_ARRAY)) {
289 WARN("not Array object\n");
293 return array_join(dispex, lcid, ((ArrayInstance*)dispex)->length, default_separatorW, retv, ei, sp);
296 static HRESULT Array_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
297 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
303 static HRESULT Array_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
304 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
310 static HRESULT Array_unshift(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
311 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
317 static HRESULT Array_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
318 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
324 static HRESULT Array_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
325 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
331 static HRESULT Array_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
332 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
338 static HRESULT Array_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
339 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
345 static void Array_destructor(DispatchEx *dispex)
350 static void Array_on_put(DispatchEx *dispex, const WCHAR *name)
352 ArrayInstance *array = (ArrayInstance*)dispex;
353 const WCHAR *ptr = name;
359 while(*ptr && isdigitW(*ptr)) {
360 id = id*10 + (*ptr-'0');
367 if(id >= array->length)
368 array->length = id+1;
371 static const builtin_prop_t Array_props[] = {
372 {concatW, Array_concat, PROPF_METHOD},
373 {hasOwnPropertyW, Array_hasOwnProperty, PROPF_METHOD},
374 {isPrototypeOfW, Array_isPrototypeOf, PROPF_METHOD},
375 {joinW, Array_join, PROPF_METHOD},
376 {lengthW, Array_length, 0},
377 {popW, Array_pop, PROPF_METHOD},
378 {propertyIsEnumerableW, Array_propertyIsEnumerable, PROPF_METHOD},
379 {pushW, Array_push, PROPF_METHOD},
380 {reverseW, Array_reverse, PROPF_METHOD},
381 {shiftW, Array_shift, PROPF_METHOD},
382 {sliceW, Array_slice, PROPF_METHOD},
383 {sortW, Array_sort, PROPF_METHOD},
384 {spliceW, Array_splice, PROPF_METHOD},
385 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
386 {toStringW, Array_toString, PROPF_METHOD},
387 {unshiftW, Array_unshift, PROPF_METHOD},
388 {valueOfW, Array_valueOf, PROPF_METHOD}
391 static const builtin_info_t Array_info = {
393 {NULL, Array_value, 0},
394 sizeof(Array_props)/sizeof(*Array_props),
400 static HRESULT ArrayConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
401 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
411 case DISPATCH_CONSTRUCT: {
412 if(arg_cnt(dp) == 1 && V_VT((arg_var = get_arg(dp, 0))) == VT_I4) {
413 if(V_I4(arg_var) < 0) {
414 FIXME("throw RangeError\n");
418 hres = create_array(dispex->ctx, V_I4(arg_var), &obj);
422 V_VT(retv) = VT_DISPATCH;
423 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
427 hres = create_array(dispex->ctx, arg_cnt(dp), &obj);
431 for(i=0; i < arg_cnt(dp); i++) {
432 hres = jsdisp_propput_idx(obj, i, lcid, get_arg(dp, i), ei, caller);
441 V_VT(retv) = VT_DISPATCH;
442 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
446 FIXME("unimplemented flags: %x\n", flags);
453 static HRESULT alloc_array(script_ctx_t *ctx, BOOL use_constr, ArrayInstance **ret)
455 ArrayInstance *array = heap_alloc_zero(sizeof(ArrayInstance));
459 hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->array_constr);
461 hres = init_dispex(&array->dispex, ctx, &Array_info, NULL);
472 HRESULT create_array_constr(script_ctx_t *ctx, DispatchEx **ret)
474 ArrayInstance *array;
477 hres = alloc_array(ctx, FALSE, &array);
481 hres = create_builtin_function(ctx, ArrayConstr_value, PROPF_CONSTR, &array->dispex, ret);
483 IDispatchEx_Release(_IDispatchEx_(&array->dispex));
487 HRESULT create_array(script_ctx_t *ctx, DWORD length, DispatchEx **ret)
489 ArrayInstance *array;
492 hres = alloc_array(ctx, TRUE, &array);
496 array->length = length;
498 *ret = &array->dispex;