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};
69 static const WCHAR fromCharCodeW[] = {'f','r','o','m','C','h','a','r','C','o','d','e',0};
71 static HRESULT String_length(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
72 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
74 TRACE("%p\n", dispex);
77 case DISPATCH_PROPERTYGET: {
78 StringInstance *jsthis = (StringInstance*)dispex;
81 V_I4(retv) = jsthis->length;
85 FIXME("unimplemented flags %x\n", flags);
92 /* ECMA-262 3rd Edition 15.5.4.2 */
93 static HRESULT String_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
94 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
96 StringInstance *string;
100 if(!is_class(dispex, JSCLASS_STRING)) {
101 WARN("this is not a string object\n");
105 string = (StringInstance*)dispex;
108 BSTR str = SysAllocString(string->str);
110 return E_OUTOFMEMORY;
112 V_VT(retv) = VT_BSTR;
118 /* ECMA-262 3rd Edition 15.5.4.2 */
119 static HRESULT String_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
120 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
124 return String_toString(dispex, lcid, flags, dp, retv, ei, sp);
127 static HRESULT do_attributeless_tag_format(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
128 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp, const WCHAR *tagname)
130 static const WCHAR tagfmt[] = {'<','%','s','>','%','s','<','/','%','s','>',0};
136 if(!is_class(dispex, JSCLASS_STRING)) {
139 V_VT(&this) = VT_DISPATCH;
140 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
142 hres = to_string(dispex->ctx, &this, ei, &val_str);
147 length = SysStringLen(val_str);
150 StringInstance *this = (StringInstance*)dispex;
153 length = this->length;
157 BSTR ret = SysAllocStringLen(NULL, length + 2*strlenW(tagname) + 5);
159 SysFreeString(val_str);
160 return E_OUTOFMEMORY;
163 sprintfW(ret, tagfmt, tagname, str, tagname);
165 V_VT(retv) = VT_BSTR;
169 SysFreeString(val_str);
173 static HRESULT do_attribute_tag_format(DispatchEx *dispex, LCID lcid, WORD flags,
174 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp,
175 const WCHAR *tagname, const WCHAR *attr)
177 static const WCHAR tagfmtW[]
178 = {'<','%','s',' ','%','s','=','\"','%','s','\"','>','%','s','<','/','%','s','>',0};
179 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
183 BSTR attr_value, val_str = NULL;
186 if(!is_class(dispex, JSCLASS_STRING)) {
189 V_VT(&this) = VT_DISPATCH;
190 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
192 hres = to_string(dispex->ctx, &this, ei, &val_str);
197 length = SysStringLen(val_str);
200 StringInstance *this = (StringInstance*)dispex;
203 length = this->length;
207 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &attr_value);
209 SysFreeString(val_str);
214 attr_value = SysAllocString(undefinedW);
216 SysFreeString(val_str);
217 return E_OUTOFMEMORY;
222 BSTR ret = SysAllocStringLen(NULL, length + 2*strlenW(tagname)
223 + strlenW(attr) + SysStringLen(attr_value) + 9);
225 SysFreeString(attr_value);
226 SysFreeString(val_str);
227 return E_OUTOFMEMORY;
230 sprintfW(ret, tagfmtW, tagname, attr, attr_value, str, tagname);
232 V_VT(retv) = VT_BSTR;
236 SysFreeString(attr_value);
237 SysFreeString(val_str);
241 static HRESULT String_anchor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
242 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
244 static const WCHAR fontW[] = {'A',0};
245 static const WCHAR colorW[] = {'N','A','M','E',0};
247 return do_attribute_tag_format(dispex, lcid, flags, dp, retv, ei, sp, fontW, colorW);
250 static HRESULT String_big(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
251 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
253 static const WCHAR bigtagW[] = {'B','I','G',0};
254 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, bigtagW);
257 static HRESULT String_blink(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
258 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
260 static const WCHAR blinktagW[] = {'B','L','I','N','K',0};
261 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, blinktagW);
264 static HRESULT String_bold(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
265 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
267 static const WCHAR boldtagW[] = {'B',0};
268 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, boldtagW);
271 /* ECMA-262 3rd Edition 15.5.4.5 */
272 static HRESULT String_charAt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
273 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
277 BSTR ret, val_str = NULL;
283 if(!is_class(dispex, JSCLASS_STRING)) {
286 V_VT(&this) = VT_DISPATCH;
287 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
289 hres = to_string(dispex->ctx, &this, ei, &val_str);
294 length = SysStringLen(val_str);
297 StringInstance *this = (StringInstance*)dispex;
300 length = this->length;
306 hres = to_integer(dispex->ctx, get_arg(dp, 0), ei, &num);
308 SysFreeString(val_str);
312 if(V_VT(&num) == VT_I4) {
315 WARN("pos = %lf\n", V_R8(&num));
321 SysFreeString(val_str);
325 if(0 <= pos && pos < length)
326 ret = SysAllocStringLen(str+pos, 1);
328 ret = SysAllocStringLen(NULL, 0);
329 SysFreeString(val_str);
331 return E_OUTOFMEMORY;
334 V_VT(retv) = VT_BSTR;
339 /* ECMA-262 3rd Edition 15.5.4.5 */
340 static HRESULT String_charCodeAt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
341 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
345 DWORD length, idx = 0;
350 if(!is_class(dispex, JSCLASS_STRING)) {
353 V_VT(&this) = VT_DISPATCH;
354 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
356 hres = to_string(dispex->ctx, &this, ei, &val_str);
361 length = SysStringLen(val_str);
364 StringInstance *this = (StringInstance*)dispex;
367 length = this->length;
370 if(arg_cnt(dp) > 0) {
373 hres = to_integer(dispex->ctx, get_arg(dp, 0), ei, &v);
375 SysFreeString(val_str);
379 if(V_VT(&v) != VT_I4 || V_I4(&v) < 0 || V_I4(&v) >= length) {
380 if(retv) num_set_nan(&v);
381 SysFreeString(val_str);
390 V_I4(retv) = str[idx];
393 SysFreeString(val_str);
397 /* ECMA-262 3rd Edition 15.5.4.6 */
398 static HRESULT String_concat(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
399 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
401 BSTR *strs = NULL, ret = NULL;
402 DWORD len = 0, i, l, str_cnt;
409 str_cnt = arg_cnt(dp)+1;
410 strs = heap_alloc_zero(str_cnt * sizeof(BSTR));
412 return E_OUTOFMEMORY;
414 V_VT(&var) = VT_DISPATCH;
415 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(dispex);
417 hres = to_string(dispex->ctx, &var, ei, strs);
418 if(SUCCEEDED(hres)) {
419 for(i=0; i < arg_cnt(dp); i++) {
420 hres = to_string(dispex->ctx, get_arg(dp, i), ei, strs+i+1);
426 if(SUCCEEDED(hres)) {
427 for(i=0; i < str_cnt; i++)
428 len += SysStringLen(strs[i]);
430 ptr = ret = SysAllocStringLen(NULL, len);
432 for(i=0; i < str_cnt; i++) {
433 l = SysStringLen(strs[i]);
434 memcpy(ptr, strs[i], l*sizeof(WCHAR));
439 for(i=0; i < str_cnt; i++)
440 SysFreeString(strs[i]);
447 V_VT(retv) = VT_BSTR;
455 static HRESULT String_fixed(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
456 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
458 static const WCHAR fixedtagW[] = {'T','T',0};
459 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, fixedtagW);
462 static HRESULT String_fontcolor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
463 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
465 static const WCHAR fontW[] = {'F','O','N','T',0};
466 static const WCHAR colorW[] = {'C','O','L','O','R',0};
468 return do_attribute_tag_format(dispex, lcid, flags, dp, retv, ei, sp, fontW, colorW);
471 static HRESULT String_fontsize(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
472 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
474 static const WCHAR fontW[] = {'F','O','N','T',0};
475 static const WCHAR colorW[] = {'S','I','Z','E',0};
477 return do_attribute_tag_format(dispex, lcid, flags, dp, retv, ei, sp, fontW, colorW);
480 static HRESULT String_indexOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
481 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
483 DWORD length, pos = 0;
485 BSTR search_str, val_str = NULL;
491 if(!is_class(dispex, JSCLASS_STRING)) {
494 V_VT(&this) = VT_DISPATCH;
495 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
497 hres = to_string(dispex->ctx, &this, ei, &val_str);
502 length = SysStringLen(val_str);
505 StringInstance *this = (StringInstance*)dispex;
508 length = this->length;
516 SysFreeString(val_str);
520 hres = to_string(dispex->ctx, get_arg(dp,0), ei, &search_str);
522 SysFreeString(val_str);
526 if(arg_cnt(dp) >= 2) {
529 hres = to_integer(dispex->ctx, get_arg(dp,1), ei, &ival);
530 if(SUCCEEDED(hres)) {
531 if(V_VT(&ival) == VT_I4)
532 pos = V_VT(&ival) > 0 ? V_I4(&ival) : 0;
534 pos = V_R8(&ival) > 0.0 ? length : 0;
540 if(SUCCEEDED(hres)) {
543 ptr = strstrW(str+pos, search_str);
550 SysFreeString(search_str);
551 SysFreeString(val_str);
562 static HRESULT String_italics(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
563 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
565 static const WCHAR italicstagW[] = {'I',0};
566 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, italicstagW);
569 static HRESULT String_lastIndexOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
570 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
576 static HRESULT String_link(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
577 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
579 static const WCHAR fontW[] = {'A',0};
580 static const WCHAR colorW[] = {'H','R','E','F',0};
582 return do_attribute_tag_format(dispex, lcid, flags, dp, retv, ei, sp, fontW, colorW);
585 /* ECMA-262 3rd Edition 15.5.4.10 */
586 static HRESULT String_match(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
587 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
590 match_result_t *match_result;
593 VARIANT var, *arg_var;
594 DWORD length, match_cnt, i;
602 V_VT(retv) = VT_NULL;
608 arg_var = get_arg(dp, 0);
609 switch(V_VT(arg_var)) {
611 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
613 if(regexp->builtin_info->class == JSCLASS_REGEXP)
615 jsdisp_release(regexp);
620 hres = to_string(dispex->ctx, arg_var, ei, &match_str);
624 hres = create_regexp_str(dispex->ctx, match_str, SysStringLen(match_str), NULL, 0, ®exp);
625 SysFreeString(match_str);
631 if(!is_class(dispex, JSCLASS_STRING)) {
634 V_VT(&this) = VT_DISPATCH;
635 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
637 hres = to_string(dispex->ctx, &this, ei, &val_str);
639 jsdisp_release(regexp);
644 length = SysStringLen(val_str);
647 StringInstance *this = (StringInstance*)dispex;
650 length = this->length;
653 hres = regexp_match(regexp, str, length, FALSE, &match_result, &match_cnt);
654 jsdisp_release(regexp);
656 SysFreeString(val_str);
664 V_VT(retv) = VT_NULL;
666 SysFreeString(val_str);
670 hres = create_array(dispex->ctx, match_cnt, &array);
672 SysFreeString(val_str);
676 V_VT(&var) = VT_BSTR;
678 for(i=0; i < match_cnt; i++) {
679 V_BSTR(&var) = SysAllocStringLen(match_result[i].str, match_result[i].len);
681 hres = E_OUTOFMEMORY;
685 hres = jsdisp_propput_idx(array, i, lcid, &var, ei, NULL/*FIXME*/);
686 SysFreeString(V_BSTR(&var));
691 SysFreeString(val_str);
693 if(SUCCEEDED(hres) && retv) {
694 V_VT(retv) = VT_DISPATCH;
695 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(array);
697 jsdisp_release(array);
708 static HRESULT strbuf_append(strbuf_t *buf, const WCHAR *str, DWORD len)
713 if(len + buf->len > buf->size) {
717 new_size = buf->size ? buf->size<<1 : 16;
718 if(new_size < buf->len+len)
719 new_size = buf->len+len;
721 new_buf = heap_realloc(buf->buf, new_size*sizeof(WCHAR));
723 new_buf = heap_alloc(new_size*sizeof(WCHAR));
725 return E_OUTOFMEMORY;
728 buf->size = new_size;
731 memcpy(buf->buf+buf->len, str, len*sizeof(WCHAR));
736 static HRESULT rep_call(DispatchEx *func, const WCHAR *str, match_result_t *match, match_result_t *parens,
737 DWORD parens_cnt, LCID lcid, BSTR *ret, jsexcept_t *ei, IServiceProvider *caller)
739 DISPPARAMS dp = {NULL, NULL, 0, 0};
740 VARIANTARG *args, *arg;
745 dp.cArgs = parens_cnt+3;
746 dp.rgvarg = args = heap_alloc_zero(sizeof(VARIANT)*dp.cArgs);
748 return E_OUTOFMEMORY;
750 arg = get_arg(&dp,0);
752 V_BSTR(arg) = SysAllocStringLen(match->str, match->len);
754 hres = E_OUTOFMEMORY;
756 if(SUCCEEDED(hres)) {
757 for(i=0; i < parens_cnt; i++) {
758 arg = get_arg(&dp,i+1);
760 V_BSTR(arg) = SysAllocStringLen(parens[i].str, parens[i].len);
762 hres = E_OUTOFMEMORY;
768 if(SUCCEEDED(hres)) {
769 arg = get_arg(&dp,parens_cnt+1);
771 V_I4(arg) = match->str - str;
773 arg = get_arg(&dp,parens_cnt+2);
775 V_BSTR(arg) = SysAllocString(str);
777 hres = E_OUTOFMEMORY;
781 hres = jsdisp_call_value(func, lcid, DISPATCH_METHOD, &dp, &var, ei, caller);
783 for(i=0; i < parens_cnt+1; i++) {
784 if(i != parens_cnt+1)
785 SysFreeString(V_BSTR(get_arg(&dp,i)));
792 hres = to_string(func->ctx, &var, ei, ret);
797 /* ECMA-262 3rd Edition 15.5.4.11 */
798 static HRESULT String_replace(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
799 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
802 DWORD parens_cnt = 0, parens_size=0, rep_len=0, length;
803 BSTR rep_str = NULL, match_str = NULL, ret_str, val_str = NULL;
804 DispatchEx *rep_func = NULL, *regexp = NULL;
805 match_result_t *parens = NULL, match;
806 strbuf_t ret = {NULL,0,0};
813 if(!is_class(dispex, JSCLASS_STRING)) {
816 V_VT(&this) = VT_DISPATCH;
817 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
819 hres = to_string(dispex->ctx, &this, ei, &val_str);
824 length = SysStringLen(val_str);
827 StringInstance *this = (StringInstance*)dispex;
830 length = this->length;
836 val_str = SysAllocStringLen(str, length);
838 return E_OUTOFMEMORY;
841 V_VT(retv) = VT_BSTR;
842 V_BSTR(retv) = val_str;
847 arg_var = get_arg(dp, 0);
848 switch(V_VT(arg_var)) {
850 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
852 if(is_class(regexp, JSCLASS_REGEXP)) {
855 jsdisp_release(regexp);
861 hres = to_string(dispex->ctx, arg_var, ei, &match_str);
863 SysFreeString(val_str);
868 if(arg_cnt(dp) >= 2) {
869 arg_var = get_arg(dp,1);
870 switch(V_VT(arg_var)) {
872 rep_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
874 if(is_class(rep_func, JSCLASS_FUNCTION)) {
877 jsdisp_release(rep_func);
883 hres = to_string(dispex->ctx, arg_var, ei, &rep_str);
887 if(strchrW(rep_str, '$')) {
888 FIXME("unsupported $ in replace string\n");
892 rep_len = SysStringLen(rep_str);
896 if(SUCCEEDED(hres)) {
897 const WCHAR *cp, *ecp;
903 hres = regexp_match_next(regexp, gcheck, str, length, &cp, rep_func ? &parens : NULL,
904 &parens_size, &parens_cnt, &match);
907 if(hres == S_FALSE) {
914 match.str = strstrW(cp, match_str);
917 match.len = SysStringLen(match_str);
918 cp = match.str+match.len;
921 hres = strbuf_append(&ret, ecp, match.str-ecp);
922 ecp = match.str+match.len;
929 hres = rep_call(rep_func, str, &match, parens, parens_cnt, lcid, &cstr, ei, caller);
933 hres = strbuf_append(&ret, cstr, SysStringLen(cstr));
938 hres = strbuf_append(&ret, rep_str, rep_len);
942 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d'};
944 hres = strbuf_append(&ret, undefinedW, sizeof(undefinedW)/sizeof(WCHAR));
951 hres = strbuf_append(&ret, ecp, (str+length)-ecp);
955 jsdisp_release(rep_func);
957 jsdisp_release(regexp);
958 SysFreeString(val_str);
959 SysFreeString(rep_str);
960 SysFreeString(match_str);
963 if(SUCCEEDED(hres) && retv) {
964 ret_str = SysAllocStringLen(ret.buf, ret.len);
966 return E_OUTOFMEMORY;
968 V_VT(retv) = VT_BSTR;
969 V_BSTR(retv) = ret_str;
970 TRACE("= %s\n", debugstr_w(ret_str));
977 static HRESULT String_search(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
978 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
984 /* ECMA-262 3rd Edition 15.5.4.13 */
985 static HRESULT String_slice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
986 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
997 if(!is_class(dispex, JSCLASS_STRING)) {
1000 V_VT(&this) = VT_DISPATCH;
1001 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
1003 hres = to_string(dispex->ctx, &this, ei, &val_str);
1008 length = SysStringLen(val_str);
1011 StringInstance *this = (StringInstance*)dispex;
1014 length = this->length;
1018 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &v);
1020 SysFreeString(val_str);
1024 if(V_VT(&v) == VT_I4) {
1027 start = length + start;
1030 }else if(start > length) {
1034 start = V_R8(&v) < 0.0 ? 0 : length;
1040 if(arg_cnt(dp) >= 2) {
1041 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-2, ei, &v);
1043 SysFreeString(val_str);
1047 if(V_VT(&v) == VT_I4) {
1053 }else if(end > length) {
1057 end = V_R8(&v) < 0.0 ? 0 : length;
1067 BSTR retstr = SysAllocStringLen(str+start, end-start);
1069 SysFreeString(val_str);
1070 return E_OUTOFMEMORY;
1073 V_VT(retv) = VT_BSTR;
1074 V_BSTR(retv) = retstr;
1077 SysFreeString(val_str);
1081 static HRESULT String_small(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1082 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1084 static const WCHAR smalltagW[] = {'S','M','A','L','L',0};
1085 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, smalltagW);
1088 static HRESULT String_split(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1089 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1091 match_result_t *match_result = NULL;
1092 DWORD length, match_cnt, i, match_len = 0;
1093 const WCHAR *str, *ptr, *ptr2;
1096 BSTR val_str = NULL, match_str = NULL;
1101 if(arg_cnt(dp) != 1) {
1102 FIXME("unsupported args\n");
1106 if(!is_class(dispex, JSCLASS_STRING)) {
1109 V_VT(&this) = VT_DISPATCH;
1110 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
1112 hres = to_string(dispex->ctx, &this, ei, &val_str);
1117 length = SysStringLen(val_str);
1120 StringInstance *this = (StringInstance*)dispex;
1123 length = this->length;
1126 arg = get_arg(dp, 0);
1131 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
1133 if(is_class(regexp, JSCLASS_REGEXP)) {
1134 hres = regexp_match(regexp, str, length, TRUE, &match_result, &match_cnt);
1135 jsdisp_release(regexp);
1137 SysFreeString(val_str);
1142 jsdisp_release(regexp);
1146 hres = to_string(dispex->ctx, arg, ei, &match_str);
1148 SysFreeString(val_str);
1152 match_len = SysStringLen(match_str);
1154 SysFreeString(match_str);
1159 hres = create_array(dispex->ctx, 0, &array);
1161 if(SUCCEEDED(hres)) {
1167 ptr2 = match_result[i].str;
1168 }else if(match_str) {
1169 ptr2 = strstrW(ptr, match_str);
1178 V_VT(&var) = VT_BSTR;
1179 V_BSTR(&var) = SysAllocStringLen(ptr, ptr2-ptr);
1181 hres = E_OUTOFMEMORY;
1185 hres = jsdisp_propput_idx(array, i, lcid, &var, ei, sp);
1186 SysFreeString(V_BSTR(&var));
1191 ptr = match_result[i].str + match_result[i].len;
1193 ptr = ptr2 + match_len;
1199 if(SUCCEEDED(hres) && (match_str || match_result)) {
1200 DWORD len = (str+length) - ptr;
1202 if(len || match_str) {
1203 V_VT(&var) = VT_BSTR;
1204 V_BSTR(&var) = SysAllocStringLen(ptr, len);
1207 hres = jsdisp_propput_idx(array, i, lcid, &var, ei, sp);
1208 SysFreeString(V_BSTR(&var));
1210 hres = E_OUTOFMEMORY;
1215 SysFreeString(match_str);
1216 SysFreeString(val_str);
1217 heap_free(match_result);
1219 if(SUCCEEDED(hres) && retv) {
1220 V_VT(retv) = VT_DISPATCH;
1221 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(array);
1223 jsdisp_release(array);
1229 static HRESULT String_strike(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1230 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1232 static const WCHAR striketagW[] = {'S','T','R','I','K','E',0};
1233 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, striketagW);
1236 static HRESULT String_sub(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1237 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1239 static const WCHAR subtagW[] = {'S','U','B',0};
1240 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, subtagW);
1243 /* ECMA-262 3rd Edition 15.5.4.15 */
1244 static HRESULT String_substring(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1245 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1248 BSTR val_str = NULL;
1256 if(!is_class(dispex, JSCLASS_STRING)) {
1259 V_VT(&this) = VT_DISPATCH;
1260 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
1262 hres = to_string(dispex->ctx, &this, ei, &val_str);
1267 length = SysStringLen(val_str);
1270 StringInstance *this = (StringInstance*)dispex;
1273 length = this->length;
1276 if(arg_cnt(dp) >= 1) {
1277 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &v);
1279 SysFreeString(val_str);
1283 if(V_VT(&v) == VT_I4) {
1287 else if(start >= length)
1290 start = V_R8(&v) < 0.0 ? 0 : length;
1294 if(arg_cnt(dp) >= 2) {
1295 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-2, ei, &v);
1297 SysFreeString(val_str);
1301 if(V_VT(&v) == VT_I4) {
1305 else if(end > length)
1308 end = V_R8(&v) < 0.0 ? 0 : length;
1321 V_VT(retv) = VT_BSTR;
1322 V_BSTR(retv) = SysAllocStringLen(str+start, end-start);
1324 SysFreeString(val_str);
1325 return E_OUTOFMEMORY;
1328 SysFreeString(val_str);
1332 static HRESULT String_substr(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1333 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1339 static HRESULT String_sup(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1340 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1342 static const WCHAR suptagW[] = {'S','U','P',0};
1343 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, suptagW);
1346 static HRESULT String_toLowerCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1347 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1351 BSTR val_str = NULL;
1356 if(!is_class(dispex, JSCLASS_STRING)) {
1359 V_VT(&this) = VT_DISPATCH;
1360 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
1362 hres = to_string(dispex->ctx, &this, ei, &val_str);
1367 length = SysStringLen(val_str);
1370 StringInstance *this = (StringInstance*)dispex;
1373 length = this->length;
1378 val_str = SysAllocStringLen(str, length);
1380 return E_OUTOFMEMORY;
1385 V_VT(retv) = VT_BSTR;
1386 V_BSTR(retv) = val_str;
1388 else SysFreeString(val_str);
1392 static HRESULT String_toUpperCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1393 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1397 BSTR val_str = NULL;
1402 if(!is_class(dispex, JSCLASS_STRING)) {
1405 V_VT(&this) = VT_DISPATCH;
1406 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
1408 hres = to_string(dispex->ctx, &this, ei, &val_str);
1413 length = SysStringLen(val_str);
1416 StringInstance *this = (StringInstance*)dispex;
1419 length = this->length;
1424 val_str = SysAllocStringLen(str, length);
1426 return E_OUTOFMEMORY;
1431 V_VT(retv) = VT_BSTR;
1432 V_BSTR(retv) = val_str;
1434 else SysFreeString(val_str);
1438 static HRESULT String_toLocaleLowerCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1439 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1445 static HRESULT String_toLocaleUpperCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1446 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1452 static HRESULT String_localeCompare(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1453 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1459 static HRESULT String_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1460 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1466 static HRESULT String_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1467 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1473 static HRESULT String_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1474 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1480 static HRESULT String_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1481 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1483 StringInstance *This = (StringInstance*)dispex;
1488 case DISPATCH_PROPERTYGET: {
1489 BSTR str = SysAllocString(This->str);
1491 return E_OUTOFMEMORY;
1493 V_VT(retv) = VT_BSTR;
1498 FIXME("flags %x\n", flags);
1505 static void String_destructor(DispatchEx *dispex)
1507 StringInstance *This = (StringInstance*)dispex;
1509 heap_free(This->str);
1513 static const builtin_prop_t String_props[] = {
1514 {anchorW, String_anchor, PROPF_METHOD},
1515 {bigW, String_big, PROPF_METHOD},
1516 {blinkW, String_blink, PROPF_METHOD},
1517 {boldW, String_bold, PROPF_METHOD},
1518 {charAtW, String_charAt, PROPF_METHOD},
1519 {charCodeAtW, String_charCodeAt, PROPF_METHOD},
1520 {concatW, String_concat, PROPF_METHOD},
1521 {fixedW, String_fixed, PROPF_METHOD},
1522 {fontcolorW, String_fontcolor, PROPF_METHOD},
1523 {fontsizeW, String_fontsize, PROPF_METHOD},
1524 {hasOwnPropertyW, String_hasOwnProperty, PROPF_METHOD},
1525 {indexOfW, String_indexOf, PROPF_METHOD},
1526 {isPrototypeOfW, String_isPrototypeOf, PROPF_METHOD},
1527 {italicsW, String_italics, PROPF_METHOD},
1528 {lastIndexOfW, String_lastIndexOf, PROPF_METHOD},
1529 {lengthW, String_length, 0},
1530 {linkW, String_link, PROPF_METHOD},
1531 {localeCompareW, String_localeCompare, PROPF_METHOD},
1532 {matchW, String_match, PROPF_METHOD},
1533 {propertyIsEnumerableW, String_propertyIsEnumerable, PROPF_METHOD},
1534 {replaceW, String_replace, PROPF_METHOD},
1535 {searchW, String_search, PROPF_METHOD},
1536 {sliceW, String_slice, PROPF_METHOD},
1537 {smallW, String_small, PROPF_METHOD},
1538 {splitW, String_split, PROPF_METHOD},
1539 {strikeW, String_strike, PROPF_METHOD},
1540 {subW, String_sub, PROPF_METHOD},
1541 {substrW, String_substr, PROPF_METHOD},
1542 {substringW, String_substring, PROPF_METHOD},
1543 {supW, String_sup, PROPF_METHOD},
1544 {toLocaleLowerCaseW, String_toLocaleLowerCase, PROPF_METHOD},
1545 {toLocaleUpperCaseW, String_toLocaleUpperCase, PROPF_METHOD},
1546 {toLowerCaseW, String_toLowerCase, PROPF_METHOD},
1547 {toStringW, String_toString, PROPF_METHOD},
1548 {toUpperCaseW, String_toUpperCase, PROPF_METHOD},
1549 {valueOfW, String_valueOf, PROPF_METHOD}
1552 static const builtin_info_t String_info = {
1554 {NULL, String_value, 0},
1555 sizeof(String_props)/sizeof(*String_props),
1561 /* ECMA-262 3rd Edition 15.5.3.2 */
1562 static HRESULT StringConstr_fromCharCode(DispatchEx *dispex, LCID lcid, WORD flags,
1563 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1569 ret = SysAllocStringLen(NULL, arg_cnt(dp));
1571 return E_OUTOFMEMORY;
1573 for(i=0; i<arg_cnt(dp); i++) {
1574 hres = to_uint32(dispex->ctx, get_arg(dp, i), ei, &code);
1584 V_VT(retv) = VT_BSTR;
1587 else SysFreeString(ret);
1592 static HRESULT StringConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1593 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1604 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
1608 str = SysAllocStringLen(NULL, 0);
1610 return E_OUTOFMEMORY;
1613 V_VT(retv) = VT_BSTR;
1617 case DISPATCH_CONSTRUCT: {
1623 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
1627 hres = create_string(dispex->ctx, str, SysStringLen(str), &ret);
1630 hres = create_string(dispex->ctx, NULL, 0, &ret);
1636 V_VT(retv) = VT_DISPATCH;
1637 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
1642 FIXME("unimplemented flags: %x\n", flags);
1649 static HRESULT string_alloc(script_ctx_t *ctx, BOOL use_constr, StringInstance **ret)
1651 StringInstance *string;
1654 string = heap_alloc_zero(sizeof(StringInstance));
1656 return E_OUTOFMEMORY;
1659 hres = init_dispex_from_constr(&string->dispex, ctx, &String_info, ctx->string_constr);
1661 hres = init_dispex(&string->dispex, ctx, &String_info, NULL);
1671 static const builtin_prop_t StringConstr_props[] = {
1672 {fromCharCodeW, StringConstr_fromCharCode, PROPF_METHOD},
1675 static const builtin_info_t StringConstr_info = {
1677 {NULL, Function_value, 0},
1678 sizeof(StringConstr_props)/sizeof(*StringConstr_props),
1684 HRESULT create_string_constr(script_ctx_t *ctx, DispatchEx **ret)
1686 StringInstance *string;
1689 hres = string_alloc(ctx, FALSE, &string);
1693 hres = create_builtin_function(ctx, StringConstr_value, &StringConstr_info, PROPF_CONSTR, &string->dispex, ret);
1695 jsdisp_release(&string->dispex);
1699 HRESULT create_string(script_ctx_t *ctx, const WCHAR *str, DWORD len, DispatchEx **ret)
1701 StringInstance *string;
1704 hres = string_alloc(ctx, TRUE, &string);
1711 string->length = len;
1712 string->str = heap_alloc((len+1)*sizeof(WCHAR));
1714 jsdisp_release(&string->dispex);
1715 return E_OUTOFMEMORY;
1718 memcpy(string->str, str, len*sizeof(WCHAR));
1719 string->str[len] = 0;
1721 *ret = &string->dispex;