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 do_attributeless_tag_format(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
127 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp, const WCHAR *tagname)
129 static const WCHAR tagfmt[] = {'<','%','s','>','%','s','<','/','%','s','>',0};
135 if(!is_class(dispex, JSCLASS_STRING)) {
138 V_VT(&this) = VT_DISPATCH;
139 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
141 hres = to_string(dispex->ctx, &this, ei, &val_str);
146 length = SysStringLen(val_str);
149 StringInstance *this = (StringInstance*)dispex;
152 length = this->length;
156 BSTR ret = SysAllocStringLen(NULL, length + 2*strlenW(tagname) + 5);
158 SysFreeString(val_str);
159 return E_OUTOFMEMORY;
162 sprintfW(ret, tagfmt, tagname, str, tagname);
164 V_VT(retv) = VT_BSTR;
168 SysFreeString(val_str);
172 static HRESULT do_attribute_tag_format(DispatchEx *dispex, LCID lcid, WORD flags,
173 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp,
174 const WCHAR *tagname, const WCHAR *attr)
176 static const WCHAR tagfmtW[]
177 = {'<','%','s',' ','%','s','=','\"','%','s','\"','>','%','s','<','/','%','s','>',0};
178 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
182 BSTR attr_value, val_str = NULL;
185 if(!is_class(dispex, JSCLASS_STRING)) {
188 V_VT(&this) = VT_DISPATCH;
189 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
191 hres = to_string(dispex->ctx, &this, ei, &val_str);
196 length = SysStringLen(val_str);
199 StringInstance *this = (StringInstance*)dispex;
202 length = this->length;
206 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &attr_value);
208 SysFreeString(val_str);
213 attr_value = SysAllocString(undefinedW);
215 SysFreeString(val_str);
216 return E_OUTOFMEMORY;
221 BSTR ret = SysAllocStringLen(NULL, length + 2*strlenW(tagname)
222 + strlenW(attr) + SysStringLen(attr_value) + 9);
224 SysFreeString(attr_value);
225 SysFreeString(val_str);
226 return E_OUTOFMEMORY;
229 sprintfW(ret, tagfmtW, tagname, attr, attr_value, str, tagname);
231 V_VT(retv) = VT_BSTR;
235 SysFreeString(attr_value);
236 SysFreeString(val_str);
240 static HRESULT String_anchor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
241 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
243 static const WCHAR fontW[] = {'A',0};
244 static const WCHAR colorW[] = {'N','A','M','E',0};
246 return do_attribute_tag_format(dispex, lcid, flags, dp, retv, ei, sp, fontW, colorW);
249 static HRESULT String_big(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
250 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
252 static const WCHAR bigtagW[] = {'B','I','G',0};
253 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, bigtagW);
256 static HRESULT String_blink(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
257 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
259 static const WCHAR blinktagW[] = {'B','L','I','N','K',0};
260 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, blinktagW);
263 static HRESULT String_bold(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
264 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
266 static const WCHAR boldtagW[] = {'B',0};
267 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, boldtagW);
270 /* ECMA-262 3rd Edition 15.5.4.5 */
271 static HRESULT String_charAt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
272 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
276 BSTR ret, val_str = NULL;
282 if(!is_class(dispex, JSCLASS_STRING)) {
285 V_VT(&this) = VT_DISPATCH;
286 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
288 hres = to_string(dispex->ctx, &this, ei, &val_str);
293 length = SysStringLen(val_str);
296 StringInstance *this = (StringInstance*)dispex;
299 length = this->length;
305 hres = to_integer(dispex->ctx, get_arg(dp, 0), ei, &num);
307 SysFreeString(val_str);
311 if(V_VT(&num) == VT_I4) {
314 WARN("pos = %lf\n", V_R8(&num));
320 SysFreeString(val_str);
324 if(0 <= pos && pos < length)
325 ret = SysAllocStringLen(str+pos, 1);
327 ret = SysAllocStringLen(NULL, 0);
328 SysFreeString(val_str);
330 return E_OUTOFMEMORY;
333 V_VT(retv) = VT_BSTR;
338 /* ECMA-262 3rd Edition 15.5.4.5 */
339 static HRESULT String_charCodeAt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
340 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
344 DWORD length, idx = 0;
349 if(!is_class(dispex, JSCLASS_STRING)) {
352 V_VT(&this) = VT_DISPATCH;
353 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
355 hres = to_string(dispex->ctx, &this, ei, &val_str);
360 length = SysStringLen(val_str);
363 StringInstance *this = (StringInstance*)dispex;
366 length = this->length;
369 if(arg_cnt(dp) > 0) {
372 hres = to_integer(dispex->ctx, get_arg(dp, 0), ei, &v);
374 SysFreeString(val_str);
378 if(V_VT(&v) != VT_I4 || V_I4(&v) < 0 || V_I4(&v) >= length) {
379 if(retv) num_set_nan(&v);
380 SysFreeString(val_str);
389 V_I4(retv) = str[idx];
392 SysFreeString(val_str);
396 /* ECMA-262 3rd Edition 15.5.4.6 */
397 static HRESULT String_concat(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
398 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
400 BSTR *strs = NULL, ret = NULL;
401 DWORD len = 0, i, l, str_cnt;
408 str_cnt = arg_cnt(dp)+1;
409 strs = heap_alloc_zero(str_cnt * sizeof(BSTR));
411 return E_OUTOFMEMORY;
413 V_VT(&var) = VT_DISPATCH;
414 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(dispex);
416 hres = to_string(dispex->ctx, &var, ei, strs);
417 if(SUCCEEDED(hres)) {
418 for(i=0; i < arg_cnt(dp); i++) {
419 hres = to_string(dispex->ctx, get_arg(dp, i), ei, strs+i+1);
425 if(SUCCEEDED(hres)) {
426 for(i=0; i < str_cnt; i++)
427 len += SysStringLen(strs[i]);
429 ptr = ret = SysAllocStringLen(NULL, len);
431 for(i=0; i < str_cnt; i++) {
432 l = SysStringLen(strs[i]);
433 memcpy(ptr, strs[i], l*sizeof(WCHAR));
438 for(i=0; i < str_cnt; i++)
439 SysFreeString(strs[i]);
446 V_VT(retv) = VT_BSTR;
454 static HRESULT String_fixed(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
455 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
457 static const WCHAR fixedtagW[] = {'T','T',0};
458 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, fixedtagW);
461 static HRESULT String_fontcolor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
462 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
464 static const WCHAR fontW[] = {'F','O','N','T',0};
465 static const WCHAR colorW[] = {'C','O','L','O','R',0};
467 return do_attribute_tag_format(dispex, lcid, flags, dp, retv, ei, sp, fontW, colorW);
470 static HRESULT String_fontsize(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
471 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
473 static const WCHAR fontW[] = {'F','O','N','T',0};
474 static const WCHAR colorW[] = {'S','I','Z','E',0};
476 return do_attribute_tag_format(dispex, lcid, flags, dp, retv, ei, sp, fontW, colorW);
479 static HRESULT String_indexOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
480 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
482 DWORD length, pos = 0;
484 BSTR search_str, val_str = NULL;
490 if(!is_class(dispex, JSCLASS_STRING)) {
493 V_VT(&this) = VT_DISPATCH;
494 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
496 hres = to_string(dispex->ctx, &this, ei, &val_str);
501 length = SysStringLen(val_str);
504 StringInstance *this = (StringInstance*)dispex;
507 length = this->length;
515 SysFreeString(val_str);
519 hres = to_string(dispex->ctx, get_arg(dp,0), ei, &search_str);
521 SysFreeString(val_str);
525 if(arg_cnt(dp) >= 2) {
528 hres = to_integer(dispex->ctx, get_arg(dp,1), ei, &ival);
529 if(SUCCEEDED(hres)) {
530 if(V_VT(&ival) == VT_I4)
531 pos = V_VT(&ival) > 0 ? V_I4(&ival) : 0;
533 pos = V_R8(&ival) > 0.0 ? length : 0;
539 if(SUCCEEDED(hres)) {
542 ptr = strstrW(str+pos, search_str);
549 SysFreeString(search_str);
550 SysFreeString(val_str);
561 static HRESULT String_italics(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
562 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
564 static const WCHAR italicstagW[] = {'I',0};
565 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, italicstagW);
568 static HRESULT String_lastIndexOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
569 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
575 static HRESULT String_link(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
576 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
578 static const WCHAR fontW[] = {'A',0};
579 static const WCHAR colorW[] = {'H','R','E','F',0};
581 return do_attribute_tag_format(dispex, lcid, flags, dp, retv, ei, sp, fontW, colorW);
584 /* ECMA-262 3rd Edition 15.5.4.10 */
585 static HRESULT String_match(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
586 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
589 match_result_t *match_result;
592 VARIANT var, *arg_var;
593 DWORD length, match_cnt, i;
599 if(arg_cnt(dp) != 1) {
600 FIXME("unsupported args\n");
604 arg_var = get_arg(dp, 0);
605 switch(V_VT(arg_var)) {
607 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
609 if(regexp->builtin_info->class == JSCLASS_REGEXP)
611 jsdisp_release(regexp);
616 hres = to_string(dispex->ctx, arg_var, ei, &match_str);
620 hres = create_regexp_str(dispex->ctx, match_str, SysStringLen(match_str), NULL, 0, ®exp);
621 SysFreeString(match_str);
627 if(!is_class(dispex, JSCLASS_STRING)) {
630 V_VT(&this) = VT_DISPATCH;
631 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
633 hres = to_string(dispex->ctx, &this, ei, &val_str);
635 jsdisp_release(regexp);
640 length = SysStringLen(val_str);
643 StringInstance *this = (StringInstance*)dispex;
646 length = this->length;
649 hres = regexp_match(regexp, str, length, FALSE, &match_result, &match_cnt);
650 jsdisp_release(regexp);
652 SysFreeString(val_str);
660 V_VT(retv) = VT_NULL;
662 SysFreeString(val_str);
666 hres = create_array(dispex->ctx, match_cnt, &array);
668 SysFreeString(val_str);
672 V_VT(&var) = VT_BSTR;
674 for(i=0; i < match_cnt; i++) {
675 V_BSTR(&var) = SysAllocStringLen(match_result[i].str, match_result[i].len);
677 hres = E_OUTOFMEMORY;
681 hres = jsdisp_propput_idx(array, i, lcid, &var, ei, NULL/*FIXME*/);
682 SysFreeString(V_BSTR(&var));
687 SysFreeString(val_str);
689 if(SUCCEEDED(hres) && retv) {
690 V_VT(retv) = VT_DISPATCH;
691 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(array);
693 jsdisp_release(array);
704 static HRESULT strbuf_append(strbuf_t *buf, const WCHAR *str, DWORD len)
709 if(len + buf->len > buf->size) {
713 new_size = buf->size ? buf->size<<1 : 16;
714 if(new_size < buf->len+len)
715 new_size = buf->len+len;
717 new_buf = heap_realloc(buf->buf, new_size*sizeof(WCHAR));
719 new_buf = heap_alloc(new_size*sizeof(WCHAR));
721 return E_OUTOFMEMORY;
724 buf->size = new_size;
727 memcpy(buf->buf+buf->len, str, len*sizeof(WCHAR));
732 static HRESULT rep_call(DispatchEx *func, const WCHAR *str, match_result_t *match, match_result_t *parens,
733 DWORD parens_cnt, LCID lcid, BSTR *ret, jsexcept_t *ei, IServiceProvider *caller)
735 DISPPARAMS dp = {NULL, NULL, 0, 0};
736 VARIANTARG *args, *arg;
741 dp.cArgs = parens_cnt+3;
742 dp.rgvarg = args = heap_alloc_zero(sizeof(VARIANT)*dp.cArgs);
744 return E_OUTOFMEMORY;
746 arg = get_arg(&dp,0);
748 V_BSTR(arg) = SysAllocStringLen(match->str, match->len);
750 hres = E_OUTOFMEMORY;
752 if(SUCCEEDED(hres)) {
753 for(i=0; i < parens_cnt; i++) {
754 arg = get_arg(&dp,i+1);
756 V_BSTR(arg) = SysAllocStringLen(parens[i].str, parens[i].len);
758 hres = E_OUTOFMEMORY;
764 if(SUCCEEDED(hres)) {
765 arg = get_arg(&dp,parens_cnt+1);
767 V_I4(arg) = match->str - str;
769 arg = get_arg(&dp,parens_cnt+2);
771 V_BSTR(arg) = SysAllocString(str);
773 hres = E_OUTOFMEMORY;
777 hres = jsdisp_call_value(func, lcid, DISPATCH_METHOD, &dp, &var, ei, caller);
779 for(i=0; i < parens_cnt+1; i++) {
780 if(i != parens_cnt+1)
781 SysFreeString(V_BSTR(get_arg(&dp,i)));
788 hres = to_string(func->ctx, &var, ei, ret);
793 /* ECMA-262 3rd Edition 15.5.4.11 */
794 static HRESULT String_replace(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
795 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
798 DWORD parens_cnt = 0, parens_size=0, rep_len=0, length;
799 BSTR rep_str = NULL, match_str = NULL, ret_str, val_str = NULL;
800 DispatchEx *rep_func = NULL, *regexp = NULL;
801 match_result_t *parens = NULL, match;
802 strbuf_t ret = {NULL,0,0};
809 if(!is_class(dispex, JSCLASS_STRING)) {
812 V_VT(&this) = VT_DISPATCH;
813 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
815 hres = to_string(dispex->ctx, &this, ei, &val_str);
820 length = SysStringLen(val_str);
823 StringInstance *this = (StringInstance*)dispex;
826 length = this->length;
832 val_str = SysAllocStringLen(str, length);
834 return E_OUTOFMEMORY;
837 V_VT(retv) = VT_BSTR;
838 V_BSTR(retv) = val_str;
843 arg_var = get_arg(dp, 0);
844 switch(V_VT(arg_var)) {
846 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
848 if(is_class(regexp, JSCLASS_REGEXP)) {
851 jsdisp_release(regexp);
857 hres = to_string(dispex->ctx, arg_var, ei, &match_str);
859 SysFreeString(val_str);
864 if(arg_cnt(dp) >= 2) {
865 arg_var = get_arg(dp,1);
866 switch(V_VT(arg_var)) {
868 rep_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
870 if(is_class(rep_func, JSCLASS_FUNCTION)) {
873 jsdisp_release(rep_func);
879 hres = to_string(dispex->ctx, arg_var, ei, &rep_str);
883 if(strchrW(rep_str, '$')) {
884 FIXME("unsupported $ in replace string\n");
888 rep_len = SysStringLen(rep_str);
892 if(SUCCEEDED(hres)) {
893 const WCHAR *cp, *ecp;
899 hres = regexp_match_next(regexp, gcheck, str, length, &cp, rep_func ? &parens : NULL,
900 &parens_size, &parens_cnt, &match);
903 if(hres == S_FALSE) {
910 match.str = strstrW(cp, match_str);
913 match.len = SysStringLen(match_str);
914 cp = match.str+match.len;
917 hres = strbuf_append(&ret, ecp, match.str-ecp);
918 ecp = match.str+match.len;
925 hres = rep_call(rep_func, str, &match, parens, parens_cnt, lcid, &cstr, ei, caller);
929 hres = strbuf_append(&ret, cstr, SysStringLen(cstr));
934 hres = strbuf_append(&ret, rep_str, rep_len);
938 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d'};
940 hres = strbuf_append(&ret, undefinedW, sizeof(undefinedW)/sizeof(WCHAR));
947 hres = strbuf_append(&ret, ecp, (str+length)-ecp);
951 jsdisp_release(rep_func);
953 jsdisp_release(regexp);
954 SysFreeString(val_str);
955 SysFreeString(rep_str);
956 SysFreeString(match_str);
959 if(SUCCEEDED(hres) && retv) {
960 ret_str = SysAllocStringLen(ret.buf, ret.len);
962 return E_OUTOFMEMORY;
964 V_VT(retv) = VT_BSTR;
965 V_BSTR(retv) = ret_str;
966 TRACE("= %s\n", debugstr_w(ret_str));
973 static HRESULT String_search(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
974 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
980 /* ECMA-262 3rd Edition 15.5.4.13 */
981 static HRESULT String_slice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
982 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
993 if(!is_class(dispex, JSCLASS_STRING)) {
996 V_VT(&this) = VT_DISPATCH;
997 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
999 hres = to_string(dispex->ctx, &this, ei, &val_str);
1004 length = SysStringLen(val_str);
1007 StringInstance *this = (StringInstance*)dispex;
1010 length = this->length;
1014 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &v);
1016 SysFreeString(val_str);
1020 if(V_VT(&v) == VT_I4) {
1023 start = length + start;
1026 }else if(start > length) {
1030 start = V_R8(&v) < 0.0 ? 0 : length;
1036 if(arg_cnt(dp) >= 2) {
1037 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-2, ei, &v);
1039 SysFreeString(val_str);
1043 if(V_VT(&v) == VT_I4) {
1049 }else if(end > length) {
1053 end = V_R8(&v) < 0.0 ? 0 : length;
1063 BSTR retstr = SysAllocStringLen(str+start, end-start);
1065 SysFreeString(val_str);
1066 return E_OUTOFMEMORY;
1069 V_VT(retv) = VT_BSTR;
1070 V_BSTR(retv) = retstr;
1073 SysFreeString(val_str);
1077 static HRESULT String_small(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1078 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1080 static const WCHAR smalltagW[] = {'S','M','A','L','L',0};
1081 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, smalltagW);
1084 static HRESULT String_split(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1085 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1087 match_result_t *match_result = NULL;
1088 DWORD length, match_cnt, i, match_len = 0;
1089 const WCHAR *str, *ptr, *ptr2;
1092 BSTR val_str = NULL, match_str = NULL;
1097 if(arg_cnt(dp) != 1) {
1098 FIXME("unsupported args\n");
1102 if(!is_class(dispex, JSCLASS_STRING)) {
1105 V_VT(&this) = VT_DISPATCH;
1106 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
1108 hres = to_string(dispex->ctx, &this, ei, &val_str);
1113 length = SysStringLen(val_str);
1116 StringInstance *this = (StringInstance*)dispex;
1119 length = this->length;
1122 arg = get_arg(dp, 0);
1127 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
1129 if(is_class(regexp, JSCLASS_REGEXP)) {
1130 hres = regexp_match(regexp, str, length, TRUE, &match_result, &match_cnt);
1131 jsdisp_release(regexp);
1133 SysFreeString(val_str);
1138 jsdisp_release(regexp);
1142 hres = to_string(dispex->ctx, arg, ei, &match_str);
1144 SysFreeString(val_str);
1148 match_len = SysStringLen(match_str);
1150 SysFreeString(match_str);
1155 hres = create_array(dispex->ctx, 0, &array);
1157 if(SUCCEEDED(hres)) {
1163 ptr2 = match_result[i].str;
1164 }else if(match_str) {
1165 ptr2 = strstrW(ptr, match_str);
1174 V_VT(&var) = VT_BSTR;
1175 V_BSTR(&var) = SysAllocStringLen(ptr, ptr2-ptr);
1177 hres = E_OUTOFMEMORY;
1181 hres = jsdisp_propput_idx(array, i, lcid, &var, ei, sp);
1182 SysFreeString(V_BSTR(&var));
1187 ptr = match_result[i].str + match_result[i].len;
1189 ptr = ptr2 + match_len;
1195 if(SUCCEEDED(hres) && (match_str || match_result)) {
1196 DWORD len = (str+length) - ptr;
1198 if(len || match_str) {
1199 V_VT(&var) = VT_BSTR;
1200 V_BSTR(&var) = SysAllocStringLen(ptr, len);
1203 hres = jsdisp_propput_idx(array, i, lcid, &var, ei, sp);
1204 SysFreeString(V_BSTR(&var));
1206 hres = E_OUTOFMEMORY;
1211 SysFreeString(match_str);
1212 SysFreeString(val_str);
1213 heap_free(match_result);
1215 if(SUCCEEDED(hres) && retv) {
1216 V_VT(retv) = VT_DISPATCH;
1217 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(array);
1219 jsdisp_release(array);
1225 static HRESULT String_strike(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1226 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1228 static const WCHAR striketagW[] = {'S','T','R','I','K','E',0};
1229 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, striketagW);
1232 static HRESULT String_sub(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1233 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1235 static const WCHAR subtagW[] = {'S','U','B',0};
1236 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, subtagW);
1239 /* ECMA-262 3rd Edition 15.5.4.15 */
1240 static HRESULT String_substring(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1241 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1244 BSTR val_str = NULL;
1252 if(!is_class(dispex, JSCLASS_STRING)) {
1255 V_VT(&this) = VT_DISPATCH;
1256 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
1258 hres = to_string(dispex->ctx, &this, ei, &val_str);
1263 length = SysStringLen(val_str);
1266 StringInstance *this = (StringInstance*)dispex;
1269 length = this->length;
1272 if(arg_cnt(dp) >= 1) {
1273 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &v);
1275 SysFreeString(val_str);
1279 if(V_VT(&v) == VT_I4) {
1283 else if(start >= length)
1286 start = V_R8(&v) < 0.0 ? 0 : length;
1290 if(arg_cnt(dp) >= 2) {
1291 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-2, ei, &v);
1293 SysFreeString(val_str);
1297 if(V_VT(&v) == VT_I4) {
1301 else if(end > length)
1304 end = V_R8(&v) < 0.0 ? 0 : length;
1317 V_VT(retv) = VT_BSTR;
1318 V_BSTR(retv) = SysAllocStringLen(str+start, end-start);
1320 SysFreeString(val_str);
1321 return E_OUTOFMEMORY;
1324 SysFreeString(val_str);
1328 static HRESULT String_substr(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1329 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1335 static HRESULT String_sup(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1336 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1338 static const WCHAR suptagW[] = {'S','U','P',0};
1339 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, suptagW);
1342 static HRESULT String_toLowerCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1343 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1347 BSTR val_str = NULL;
1352 if(!is_class(dispex, JSCLASS_STRING)) {
1355 V_VT(&this) = VT_DISPATCH;
1356 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
1358 hres = to_string(dispex->ctx, &this, ei, &val_str);
1363 length = SysStringLen(val_str);
1366 StringInstance *this = (StringInstance*)dispex;
1369 length = this->length;
1374 val_str = SysAllocStringLen(str, length);
1376 return E_OUTOFMEMORY;
1381 V_VT(retv) = VT_BSTR;
1382 V_BSTR(retv) = val_str;
1384 else SysFreeString(val_str);
1388 static HRESULT String_toUpperCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1389 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1393 BSTR val_str = NULL;
1398 if(!is_class(dispex, JSCLASS_STRING)) {
1401 V_VT(&this) = VT_DISPATCH;
1402 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
1404 hres = to_string(dispex->ctx, &this, ei, &val_str);
1409 length = SysStringLen(val_str);
1412 StringInstance *this = (StringInstance*)dispex;
1415 length = this->length;
1420 val_str = SysAllocStringLen(str, length);
1422 return E_OUTOFMEMORY;
1427 V_VT(retv) = VT_BSTR;
1428 V_BSTR(retv) = val_str;
1430 else SysFreeString(val_str);
1434 static HRESULT String_toLocaleLowerCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1435 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1441 static HRESULT String_toLocaleUpperCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1442 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1448 static HRESULT String_localeCompare(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1449 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1455 static HRESULT String_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1456 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1462 static HRESULT String_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1463 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1469 static HRESULT String_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1470 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1476 static HRESULT String_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1477 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1479 StringInstance *This = (StringInstance*)dispex;
1484 case DISPATCH_PROPERTYGET: {
1485 BSTR str = SysAllocString(This->str);
1487 return E_OUTOFMEMORY;
1489 V_VT(retv) = VT_BSTR;
1494 FIXME("flags %x\n", flags);
1501 static void String_destructor(DispatchEx *dispex)
1503 StringInstance *This = (StringInstance*)dispex;
1505 heap_free(This->str);
1509 static const builtin_prop_t String_props[] = {
1510 {anchorW, String_anchor, PROPF_METHOD},
1511 {bigW, String_big, PROPF_METHOD},
1512 {blinkW, String_blink, PROPF_METHOD},
1513 {boldW, String_bold, PROPF_METHOD},
1514 {charAtW, String_charAt, PROPF_METHOD},
1515 {charCodeAtW, String_charCodeAt, PROPF_METHOD},
1516 {concatW, String_concat, PROPF_METHOD},
1517 {fixedW, String_fixed, PROPF_METHOD},
1518 {fontcolorW, String_fontcolor, PROPF_METHOD},
1519 {fontsizeW, String_fontsize, PROPF_METHOD},
1520 {hasOwnPropertyW, String_hasOwnProperty, PROPF_METHOD},
1521 {indexOfW, String_indexOf, PROPF_METHOD},
1522 {isPrototypeOfW, String_isPrototypeOf, PROPF_METHOD},
1523 {italicsW, String_italics, PROPF_METHOD},
1524 {lastIndexOfW, String_lastIndexOf, PROPF_METHOD},
1525 {lengthW, String_length, 0},
1526 {linkW, String_link, PROPF_METHOD},
1527 {localeCompareW, String_localeCompare, PROPF_METHOD},
1528 {matchW, String_match, PROPF_METHOD},
1529 {propertyIsEnumerableW, String_propertyIsEnumerable, PROPF_METHOD},
1530 {replaceW, String_replace, PROPF_METHOD},
1531 {searchW, String_search, PROPF_METHOD},
1532 {sliceW, String_slice, PROPF_METHOD},
1533 {smallW, String_small, PROPF_METHOD},
1534 {splitW, String_split, PROPF_METHOD},
1535 {strikeW, String_strike, PROPF_METHOD},
1536 {subW, String_sub, PROPF_METHOD},
1537 {substrW, String_substr, PROPF_METHOD},
1538 {substringW, String_substring, PROPF_METHOD},
1539 {supW, String_sup, PROPF_METHOD},
1540 {toLocaleLowerCaseW, String_toLocaleLowerCase, PROPF_METHOD},
1541 {toLocaleUpperCaseW, String_toLocaleUpperCase, PROPF_METHOD},
1542 {toLowerCaseW, String_toLowerCase, PROPF_METHOD},
1543 {toStringW, String_toString, PROPF_METHOD},
1544 {toUpperCaseW, String_toUpperCase, PROPF_METHOD},
1545 {valueOfW, String_valueOf, PROPF_METHOD}
1548 static const builtin_info_t String_info = {
1550 {NULL, String_value, 0},
1551 sizeof(String_props)/sizeof(*String_props),
1557 static HRESULT StringConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1558 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1569 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
1573 str = SysAllocStringLen(NULL, 0);
1575 return E_OUTOFMEMORY;
1578 V_VT(retv) = VT_BSTR;
1582 case DISPATCH_CONSTRUCT: {
1588 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
1592 hres = create_string(dispex->ctx, str, SysStringLen(str), &ret);
1595 hres = create_string(dispex->ctx, NULL, 0, &ret);
1601 V_VT(retv) = VT_DISPATCH;
1602 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
1607 FIXME("unimplemented flags: %x\n", flags);
1614 static HRESULT string_alloc(script_ctx_t *ctx, BOOL use_constr, StringInstance **ret)
1616 StringInstance *string;
1619 string = heap_alloc_zero(sizeof(StringInstance));
1621 return E_OUTOFMEMORY;
1624 hres = init_dispex_from_constr(&string->dispex, ctx, &String_info, ctx->string_constr);
1626 hres = init_dispex(&string->dispex, ctx, &String_info, NULL);
1636 HRESULT create_string_constr(script_ctx_t *ctx, DispatchEx **ret)
1638 StringInstance *string;
1641 hres = string_alloc(ctx, FALSE, &string);
1645 hres = create_builtin_function(ctx, StringConstr_value, NULL, PROPF_CONSTR, &string->dispex, ret);
1647 jsdisp_release(&string->dispex);
1651 HRESULT create_string(script_ctx_t *ctx, const WCHAR *str, DWORD len, DispatchEx **ret)
1653 StringInstance *string;
1656 hres = string_alloc(ctx, TRUE, &string);
1663 string->length = len;
1664 string->str = heap_alloc((len+1)*sizeof(WCHAR));
1666 jsdisp_release(&string->dispex);
1667 return E_OUTOFMEMORY;
1670 memcpy(string->str, str, len*sizeof(WCHAR));
1671 string->str[len] = 0;
1673 *ret = &string->dispex;