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);
32 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
33 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
34 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
35 static const WCHAR anchorW[] = {'a','n','c','h','o','r',0};
36 static const WCHAR bigW[] = {'b','i','g',0};
37 static const WCHAR blinkW[] = {'b','l','i','n','k',0};
38 static const WCHAR boldW[] = {'b','o','l','d',0};
39 static const WCHAR charAtW[] = {'c','h','a','r','A','t',0};
40 static const WCHAR charCodeAtW[] = {'c','h','a','r','C','o','d','e','A','t',0};
41 static const WCHAR concatW[] = {'c','o','n','c','a','t',0};
42 static const WCHAR fixedW[] = {'f','i','x','e','d',0};
43 static const WCHAR fontcolorW[] = {'f','o','n','t','c','o','l','o','r',0};
44 static const WCHAR fontsizeW[] = {'f','o','n','t','s','i','z','e',0};
45 static const WCHAR indexOfW[] = {'i','n','d','e','x','O','f',0};
46 static const WCHAR italicsW[] = {'i','t','a','l','i','c','s',0};
47 static const WCHAR lastIndexOfW[] = {'l','a','s','t','I','n','d','e','x','O','f',0};
48 static const WCHAR linkW[] = {'l','i','n','k',0};
49 static const WCHAR matchW[] = {'m','a','t','c','h',0};
50 static const WCHAR replaceW[] = {'r','e','p','l','a','c','e',0};
51 static const WCHAR searchW[] = {'s','e','a','r','c','h',0};
52 static const WCHAR sliceW[] = {'s','l','i','c','e',0};
53 static const WCHAR smallW[] = {'s','m','a','l','l',0};
54 static const WCHAR splitW[] = {'s','p','l','i','t',0};
55 static const WCHAR strikeW[] = {'s','t','r','i','k','e',0};
56 static const WCHAR subW[] = {'s','u','b',0};
57 static const WCHAR substringW[] = {'s','u','b','s','t','r','i','n','g',0};
58 static const WCHAR substrW[] = {'s','u','b','s','t','r',0};
59 static const WCHAR supW[] = {'s','u','p',0};
60 static const WCHAR toLowerCaseW[] = {'t','o','L','o','w','e','r','C','a','s','e',0};
61 static const WCHAR toUpperCaseW[] = {'t','o','U','p','p','e','r','C','a','s','e',0};
62 static const WCHAR toLocaleLowerCaseW[] = {'t','o','L','o','c','a','l','e','L','o','w','e','r','C','a','s','e',0};
63 static const WCHAR toLocaleUpperCaseW[] = {'t','o','L','o','c','a','l','e','U','p','p','e','r','C','a','s','e',0};
64 static const WCHAR localeCompareW[] = {'l','o','c','a','l','e','C','o','m','p','a','r','e',0};
65 static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
66 static const WCHAR propertyIsEnumerableW[] =
67 {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
68 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
70 static HRESULT String_length(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
71 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
73 TRACE("%p\n", dispex);
76 case DISPATCH_PROPERTYGET: {
77 StringInstance *jsthis = (StringInstance*)dispex;
80 V_I4(retv) = jsthis->length;
84 FIXME("unimplemented flags %x\n", flags);
91 /* ECMA-262 3rd Edition 15.5.4.2 */
92 static HRESULT String_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
93 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
95 StringInstance *string;
99 if(!is_class(dispex, JSCLASS_STRING)) {
100 WARN("this is not a string object\n");
104 string = (StringInstance*)dispex;
107 BSTR str = SysAllocString(string->str);
109 return E_OUTOFMEMORY;
111 V_VT(retv) = VT_BSTR;
117 /* ECMA-262 3rd Edition 15.5.4.2 */
118 static HRESULT String_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
119 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
123 return String_toString(dispex, lcid, flags, dp, retv, ei, sp);
126 static HRESULT String_anchor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
127 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
133 static HRESULT String_big(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
134 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
140 static HRESULT String_blink(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
141 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
147 static HRESULT String_bold(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
148 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
154 /* ECMA-262 3rd Edition 15.5.4.5 */
155 static HRESULT String_charAt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
156 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
158 StringInstance *strobj;
165 if(dispex->builtin_info->class != JSCLASS_STRING) {
166 FIXME("not string this not supported\n");
170 strobj = (StringInstance*)dispex;
175 hres = to_integer(dispex->ctx, get_arg(dp, 0), ei, &num);
179 if(V_VT(&num) == VT_I4) {
182 WARN("pos = %lf\n", V_R8(&num));
190 if(0 <= pos && pos < strobj->length)
191 str = SysAllocStringLen(strobj->str+pos, 1);
193 str = SysAllocStringLen(NULL, 0);
195 return E_OUTOFMEMORY;
197 V_VT(retv) = VT_BSTR;
202 /* ECMA-262 3rd Edition 15.5.4.5 */
203 static HRESULT String_charCodeAt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
204 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
207 DWORD length, idx = 0;
212 if(dispex->builtin_info->class == JSCLASS_STRING) {
213 StringInstance *string = (StringInstance*)dispex;
216 length = string->length;
218 FIXME("not string this not supported\n");
222 if(arg_cnt(dp) > 0) {
225 hres = to_integer(dispex->ctx, get_arg(dp, 0), ei, &v);
229 if(V_VT(&v) != VT_I4 || V_I4(&v) < 0 || V_I4(&v) >= length) {
239 V_I4(retv) = str[idx];
244 /* ECMA-262 3rd Edition 15.5.4.6 */
245 static HRESULT String_concat(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
246 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
248 BSTR *strs = NULL, ret = NULL;
249 DWORD len = 0, i, l, str_cnt;
256 str_cnt = arg_cnt(dp)+1;
257 strs = heap_alloc_zero(str_cnt * sizeof(BSTR));
259 return E_OUTOFMEMORY;
261 V_VT(&var) = VT_DISPATCH;
262 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(dispex);
264 hres = to_string(dispex->ctx, &var, ei, strs);
265 if(SUCCEEDED(hres)) {
266 for(i=0; i < arg_cnt(dp); i++) {
267 hres = to_string(dispex->ctx, get_arg(dp, i), ei, strs+i+1);
273 if(SUCCEEDED(hres)) {
274 for(i=0; i < str_cnt; i++)
275 len += SysStringLen(strs[i]);
277 ptr = ret = SysAllocStringLen(NULL, len);
279 for(i=0; i < str_cnt; i++) {
280 l = SysStringLen(strs[i]);
281 memcpy(ptr, strs[i], l*sizeof(WCHAR));
286 for(i=0; i < str_cnt; i++)
287 SysFreeString(strs[i]);
294 V_VT(retv) = VT_BSTR;
302 static HRESULT String_fixed(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
303 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
309 static HRESULT String_fontcolor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
310 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
316 static HRESULT String_fontsize(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
317 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
323 static HRESULT String_indexOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
324 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
330 static HRESULT String_italics(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
331 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
337 static HRESULT String_lastIndexOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
338 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
344 static HRESULT String_link(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
345 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
351 /* ECMA-262 3rd Edition 15.5.4.10 */
352 static HRESULT String_match(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
353 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
355 StringInstance *This = (StringInstance*)dispex;
356 match_result_t *match_result;
358 VARIANT var, *arg_var;
364 if(dp->cArgs - dp->cNamedArgs != 1) {
365 FIXME("unsupported args\n");
369 arg_var = get_arg(dp, 0);
370 switch(V_VT(arg_var)) {
374 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
376 if(regexp->builtin_info->class == JSCLASS_REGEXP) {
377 hres = regexp_match(regexp, This->str, This->length, FALSE, &match_result, &match_cnt);
378 jsdisp_release(regexp);
383 jsdisp_release(regexp);
387 FIXME("implemented only for regexp args\n");
395 V_VT(retv) = VT_NULL;
399 hres = create_array(dispex->ctx, match_cnt, &array);
403 V_VT(&var) = VT_BSTR;
405 for(i=0; i < match_cnt; i++) {
406 V_BSTR(&var) = SysAllocStringLen(match_result[i].str, match_result[i].len);
408 hres = E_OUTOFMEMORY;
412 hres = jsdisp_propput_idx(array, i, lcid, &var, ei, NULL/*FIXME*/);
413 SysFreeString(V_BSTR(&var));
419 jsdisp_release(array);
423 V_VT(retv) = VT_DISPATCH;
424 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(array);
428 static HRESULT String_replace(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
429 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
435 static HRESULT String_search(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
436 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
442 /* ECMA-262 3rd Edition 15.5.4.13 */
443 static HRESULT String_slice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
444 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
454 if(is_class(dispex, JSCLASS_STRING)) {
455 StringInstance *string = (StringInstance*)dispex;
458 length = string->length;
460 FIXME("this is not a string class");
465 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &v);
469 if(V_VT(&v) == VT_I4) {
472 start = length + start;
475 }else if(start > length) {
479 start = V_R8(&v) < 0.0 ? 0 : length;
485 if(arg_cnt(dp) >= 2) {
486 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-2, ei, &v);
490 if(V_VT(&v) == VT_I4) {
496 }else if(end > length) {
500 end = V_R8(&v) < 0.0 ? 0 : length;
510 BSTR retstr = SysAllocStringLen(str+start, end-start);
512 return E_OUTOFMEMORY;
514 V_VT(retv) = VT_BSTR;
515 V_BSTR(retv) = retstr;
520 static HRESULT String_small(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
521 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
527 static HRESULT String_split(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
528 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
534 static HRESULT String_strike(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
535 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
541 static HRESULT String_sub(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
542 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
548 /* ECMA-262 3rd Edition 15.5.4.15 */
549 static HRESULT String_substring(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
550 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
560 if(is_class(dispex, JSCLASS_STRING)) {
561 StringInstance *string = (StringInstance*)dispex;
563 length = string->length;
566 FIXME("not string this not supported\n");
570 if(arg_cnt(dp) >= 1) {
571 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &v);
575 if(V_VT(&v) == VT_I4) {
579 else if(start >= length)
582 start = V_R8(&v) < 0.0 ? 0 : length;
586 if(arg_cnt(dp) >= 2) {
587 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-2, ei, &v);
591 if(V_VT(&v) == VT_I4) {
595 else if(end > length)
598 end = V_R8(&v) < 0.0 ? 0 : length;
611 V_VT(retv) = VT_BSTR;
612 V_BSTR(retv) = SysAllocStringLen(str+start, end-start);
614 return E_OUTOFMEMORY;
619 static HRESULT String_substr(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
620 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
626 static HRESULT String_sup(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
627 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
633 static HRESULT String_toLowerCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
634 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
640 static HRESULT String_toUpperCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
641 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
647 static HRESULT String_toLocaleLowerCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
648 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
654 static HRESULT String_toLocaleUpperCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
655 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
661 static HRESULT String_localeCompare(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
662 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
668 static HRESULT String_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
669 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
675 static HRESULT String_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
676 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
682 static HRESULT String_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
683 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
689 static HRESULT String_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
690 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
692 StringInstance *This = (StringInstance*)dispex;
697 case DISPATCH_PROPERTYGET: {
698 BSTR str = SysAllocString(This->str);
700 return E_OUTOFMEMORY;
702 V_VT(retv) = VT_BSTR;
707 FIXME("flags %x\n", flags);
714 static void String_destructor(DispatchEx *dispex)
716 StringInstance *This = (StringInstance*)dispex;
718 heap_free(This->str);
722 static const builtin_prop_t String_props[] = {
723 {anchorW, String_anchor, PROPF_METHOD},
724 {bigW, String_big, PROPF_METHOD},
725 {blinkW, String_blink, PROPF_METHOD},
726 {boldW, String_bold, PROPF_METHOD},
727 {charAtW, String_charAt, PROPF_METHOD},
728 {charCodeAtW, String_charCodeAt, PROPF_METHOD},
729 {concatW, String_concat, PROPF_METHOD},
730 {fixedW, String_fixed, PROPF_METHOD},
731 {fontcolorW, String_fontcolor, PROPF_METHOD},
732 {fontsizeW, String_fontsize, PROPF_METHOD},
733 {hasOwnPropertyW, String_hasOwnProperty, PROPF_METHOD},
734 {indexOfW, String_indexOf, PROPF_METHOD},
735 {isPrototypeOfW, String_isPrototypeOf, PROPF_METHOD},
736 {italicsW, String_italics, PROPF_METHOD},
737 {lastIndexOfW, String_lastIndexOf, PROPF_METHOD},
738 {lengthW, String_length, 0},
739 {linkW, String_link, PROPF_METHOD},
740 {localeCompareW, String_localeCompare, PROPF_METHOD},
741 {matchW, String_match, PROPF_METHOD},
742 {propertyIsEnumerableW, String_propertyIsEnumerable, PROPF_METHOD},
743 {replaceW, String_replace, PROPF_METHOD},
744 {searchW, String_search, PROPF_METHOD},
745 {sliceW, String_slice, PROPF_METHOD},
746 {smallW, String_small, PROPF_METHOD},
747 {splitW, String_split, PROPF_METHOD},
748 {strikeW, String_strike, PROPF_METHOD},
749 {substringW, String_substring, PROPF_METHOD},
750 {substrW, String_substr, PROPF_METHOD},
751 {subW, String_sub, PROPF_METHOD},
752 {supW, String_sup, PROPF_METHOD},
753 {toLocaleLowerCaseW, String_toLocaleLowerCase, PROPF_METHOD},
754 {toLocaleUpperCaseW, String_toLocaleUpperCase, PROPF_METHOD},
755 {toLowerCaseW, String_toLowerCase, PROPF_METHOD},
756 {toStringW, String_toString, PROPF_METHOD},
757 {toUpperCaseW, String_toUpperCase, PROPF_METHOD},
758 {valueOfW, String_valueOf, PROPF_METHOD}
761 static const builtin_info_t String_info = {
763 {NULL, String_value, 0},
764 sizeof(String_props)/sizeof(*String_props),
770 static HRESULT StringConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
771 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
782 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
786 str = SysAllocStringLen(NULL, 0);
788 return E_OUTOFMEMORY;
791 V_VT(retv) = VT_BSTR;
795 case DISPATCH_CONSTRUCT: {
801 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
805 hres = create_string(dispex->ctx, str, SysStringLen(str), &ret);
808 hres = create_string(dispex->ctx, NULL, 0, &ret);
814 V_VT(retv) = VT_DISPATCH;
815 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
820 FIXME("unimplemented flags: %x\n", flags);
827 static HRESULT string_alloc(script_ctx_t *ctx, BOOL use_constr, StringInstance **ret)
829 StringInstance *string;
832 string = heap_alloc_zero(sizeof(StringInstance));
834 return E_OUTOFMEMORY;
837 hres = init_dispex_from_constr(&string->dispex, ctx, &String_info, ctx->string_constr);
839 hres = init_dispex(&string->dispex, ctx, &String_info, NULL);
849 HRESULT create_string_constr(script_ctx_t *ctx, DispatchEx **ret)
851 StringInstance *string;
854 hres = string_alloc(ctx, FALSE, &string);
858 hres = create_builtin_function(ctx, StringConstr_value, PROPF_CONSTR, &string->dispex, ret);
860 jsdisp_release(&string->dispex);
864 HRESULT create_string(script_ctx_t *ctx, const WCHAR *str, DWORD len, DispatchEx **ret)
866 StringInstance *string;
869 hres = string_alloc(ctx, TRUE, &string);
876 string->length = len;
877 string->str = heap_alloc((len+1)*sizeof(WCHAR));
879 jsdisp_release(&string->dispex);
880 return E_OUTOFMEMORY;
883 memcpy(string->str, str, len*sizeof(WCHAR));
884 string->str[len] = 0;
886 *ret = &string->dispex;