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};
130 StringInstance *string;
133 if(!is_class(dispex, JSCLASS_STRING)) {
134 WARN("this is not a string object\n");
138 string = (StringInstance*)dispex;
141 ret = SysAllocStringLen(NULL, string->length + 2*strlenW(tagname) + 5);
143 return E_OUTOFMEMORY;
145 sprintfW(ret, tagfmt, tagname, string->str, tagname);
147 V_VT(retv) = VT_BSTR;
153 static HRESULT do_attribute_tag_format(DispatchEx *dispex, LCID lcid, WORD flags,
154 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp,
155 const WCHAR *tagname, const WCHAR *attr)
157 static const WCHAR tagfmtW[]
158 = {'<','%','s',' ','%','s','=','\"','%','s','\"','>','%','s','<','/','%','s','>',0};
159 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
161 StringInstance *string;
162 BSTR ret, attr_value;
165 if(!is_class(dispex, JSCLASS_STRING)) {
166 WARN("this is not a string object\n");
170 string = (StringInstance*) dispex;
173 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &attr_value);
178 attr_value = SysAllocString(undefinedW);
180 return E_OUTOFMEMORY;
184 ret = SysAllocStringLen(NULL, string->length + 2*strlenW(tagname)
185 + strlenW(attr) + SysStringLen(attr_value) + 9);
187 SysFreeString(attr_value);
188 return E_OUTOFMEMORY;
191 sprintfW(ret, tagfmtW, tagname, attr, attr_value, string->str, tagname);
193 V_VT(retv) = VT_BSTR;
197 SysFreeString(attr_value);
201 static HRESULT String_anchor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
202 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
208 static HRESULT String_big(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
209 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
211 static const WCHAR bigtagW[] = {'B','I','G',0};
212 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, bigtagW);
215 static HRESULT String_blink(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
216 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
218 static const WCHAR blinktagW[] = {'B','L','I','N','K',0};
219 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, blinktagW);
222 static HRESULT String_bold(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
223 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
225 static const WCHAR boldtagW[] = {'B',0};
226 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, boldtagW);
229 /* ECMA-262 3rd Edition 15.5.4.5 */
230 static HRESULT String_charAt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
231 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
233 StringInstance *strobj;
240 if(dispex->builtin_info->class != JSCLASS_STRING) {
241 FIXME("not string this not supported\n");
245 strobj = (StringInstance*)dispex;
250 hres = to_integer(dispex->ctx, get_arg(dp, 0), ei, &num);
254 if(V_VT(&num) == VT_I4) {
257 WARN("pos = %lf\n", V_R8(&num));
265 if(0 <= pos && pos < strobj->length)
266 str = SysAllocStringLen(strobj->str+pos, 1);
268 str = SysAllocStringLen(NULL, 0);
270 return E_OUTOFMEMORY;
272 V_VT(retv) = VT_BSTR;
277 /* ECMA-262 3rd Edition 15.5.4.5 */
278 static HRESULT String_charCodeAt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
279 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
282 DWORD length, idx = 0;
287 if(dispex->builtin_info->class == JSCLASS_STRING) {
288 StringInstance *string = (StringInstance*)dispex;
291 length = string->length;
293 FIXME("not string this not supported\n");
297 if(arg_cnt(dp) > 0) {
300 hres = to_integer(dispex->ctx, get_arg(dp, 0), ei, &v);
304 if(V_VT(&v) != VT_I4 || V_I4(&v) < 0 || V_I4(&v) >= length) {
305 if(retv) num_set_nan(&v);
314 V_I4(retv) = str[idx];
319 /* ECMA-262 3rd Edition 15.5.4.6 */
320 static HRESULT String_concat(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
321 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
323 BSTR *strs = NULL, ret = NULL;
324 DWORD len = 0, i, l, str_cnt;
331 str_cnt = arg_cnt(dp)+1;
332 strs = heap_alloc_zero(str_cnt * sizeof(BSTR));
334 return E_OUTOFMEMORY;
336 V_VT(&var) = VT_DISPATCH;
337 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(dispex);
339 hres = to_string(dispex->ctx, &var, ei, strs);
340 if(SUCCEEDED(hres)) {
341 for(i=0; i < arg_cnt(dp); i++) {
342 hres = to_string(dispex->ctx, get_arg(dp, i), ei, strs+i+1);
348 if(SUCCEEDED(hres)) {
349 for(i=0; i < str_cnt; i++)
350 len += SysStringLen(strs[i]);
352 ptr = ret = SysAllocStringLen(NULL, len);
354 for(i=0; i < str_cnt; i++) {
355 l = SysStringLen(strs[i]);
356 memcpy(ptr, strs[i], l*sizeof(WCHAR));
361 for(i=0; i < str_cnt; i++)
362 SysFreeString(strs[i]);
369 V_VT(retv) = VT_BSTR;
377 static HRESULT String_fixed(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
378 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
380 static const WCHAR fixedtagW[] = {'T','T',0};
381 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, fixedtagW);
384 static HRESULT String_fontcolor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
385 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
387 static const WCHAR fontW[] = {'F','O','N','T',0};
388 static const WCHAR colorW[] = {'C','O','L','O','R',0};
390 return do_attribute_tag_format(dispex, lcid, flags, dp, retv, ei, sp, fontW, colorW);
393 static HRESULT String_fontsize(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
394 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
400 static HRESULT String_indexOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
401 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
403 DWORD length, pos = 0;
411 if(is_class(dispex, JSCLASS_STRING)) {
412 StringInstance *string = (StringInstance*)dispex;
415 length = string->length;
417 FIXME("not String this\n");
429 hres = to_string(dispex->ctx, get_arg(dp,0), ei, &search_str);
433 if(arg_cnt(dp) >= 2) {
436 hres = to_integer(dispex->ctx, get_arg(dp,1), ei, &ival);
437 if(SUCCEEDED(hres)) {
438 if(V_VT(&ival) == VT_I4)
439 pos = V_VT(&ival) > 0 ? V_I4(&ival) : 0;
441 pos = V_R8(&ival) > 0.0 ? length : 0;
447 if(SUCCEEDED(hres)) {
450 ptr = strstrW(str+pos, search_str);
457 SysFreeString(search_str);
468 static HRESULT String_italics(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
469 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
471 static const WCHAR italicstagW[] = {'I',0};
472 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, italicstagW);
475 static HRESULT String_lastIndexOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
476 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
482 static HRESULT String_link(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
483 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
489 /* ECMA-262 3rd Edition 15.5.4.10 */
490 static HRESULT String_match(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
491 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
493 StringInstance *This = (StringInstance*)dispex;
494 match_result_t *match_result;
497 VARIANT var, *arg_var;
503 if(arg_cnt(dp) != 1) {
504 FIXME("unsupported args\n");
508 arg_var = get_arg(dp, 0);
509 switch(V_VT(arg_var)) {
511 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
513 if(regexp->builtin_info->class == JSCLASS_REGEXP)
515 jsdisp_release(regexp);
520 hres = to_string(dispex->ctx, arg_var, ei, &match_str);
524 hres = create_regexp_str(dispex->ctx, match_str, SysStringLen(match_str), NULL, 0, ®exp);
525 SysFreeString(match_str);
531 hres = regexp_match(regexp, This->str, This->length, FALSE, &match_result, &match_cnt);
532 jsdisp_release(regexp);
540 V_VT(retv) = VT_NULL;
544 hres = create_array(dispex->ctx, match_cnt, &array);
548 V_VT(&var) = VT_BSTR;
550 for(i=0; i < match_cnt; i++) {
551 V_BSTR(&var) = SysAllocStringLen(match_result[i].str, match_result[i].len);
553 hres = E_OUTOFMEMORY;
557 hres = jsdisp_propput_idx(array, i, lcid, &var, ei, NULL/*FIXME*/);
558 SysFreeString(V_BSTR(&var));
563 if(SUCCEEDED(hres) && retv) {
564 V_VT(retv) = VT_DISPATCH;
565 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(array);
567 jsdisp_release(array);
578 static HRESULT strbuf_append(strbuf_t *buf, const WCHAR *str, DWORD len)
583 if(len + buf->len > buf->size) {
587 new_size = buf->size ? buf->size<<1 : 16;
588 if(new_size < buf->len+len)
589 new_size = buf->len+len;
591 new_buf = heap_realloc(buf->buf, new_size*sizeof(WCHAR));
593 new_buf = heap_alloc(new_size*sizeof(WCHAR));
595 return E_OUTOFMEMORY;
598 buf->size = new_size;
601 memcpy(buf->buf+buf->len, str, len*sizeof(WCHAR));
606 static HRESULT rep_call(DispatchEx *func, const WCHAR *str, match_result_t *match, match_result_t *parens,
607 DWORD parens_cnt, LCID lcid, BSTR *ret, jsexcept_t *ei, IServiceProvider *caller)
609 DISPPARAMS dp = {NULL, NULL, 0, 0};
610 VARIANTARG *args, *arg;
615 dp.cArgs = parens_cnt+3;
616 dp.rgvarg = args = heap_alloc_zero(sizeof(VARIANT)*dp.cArgs);
618 return E_OUTOFMEMORY;
620 arg = get_arg(&dp,0);
622 V_BSTR(arg) = SysAllocStringLen(match->str, match->len);
624 hres = E_OUTOFMEMORY;
626 if(SUCCEEDED(hres)) {
627 for(i=0; i < parens_cnt; i++) {
628 arg = get_arg(&dp,i+1);
630 V_BSTR(arg) = SysAllocStringLen(parens[i].str, parens[i].len);
632 hres = E_OUTOFMEMORY;
638 if(SUCCEEDED(hres)) {
639 arg = get_arg(&dp,parens_cnt+1);
641 V_I4(arg) = match->str - str;
643 arg = get_arg(&dp,parens_cnt+2);
645 V_BSTR(arg) = SysAllocString(str);
647 hres = E_OUTOFMEMORY;
651 hres = jsdisp_call_value(func, lcid, DISPATCH_METHOD, &dp, &var, ei, caller);
653 for(i=0; i < parens_cnt+1; i++) {
654 if(i != parens_cnt+1)
655 SysFreeString(V_BSTR(get_arg(&dp,i)));
662 hres = to_string(func->ctx, &var, ei, ret);
667 /* ECMA-262 3rd Edition 15.5.4.11 */
668 static HRESULT String_replace(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
669 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
671 DWORD parens_cnt = 0, parens_size=0, rep_len=0, length;
672 BSTR rep_str = NULL, match_str = NULL, ret_str;
673 DispatchEx *rep_func = NULL, *regexp = NULL;
674 match_result_t *parens = NULL, match;
676 strbuf_t ret = {NULL,0,0};
683 if(is_class(dispex, JSCLASS_STRING)) {
684 StringInstance *string = (StringInstance*)dispex;
686 length = string->length;
688 FIXME("not String this\n");
694 ret_str = SysAllocString(str);
696 return E_OUTOFMEMORY;
698 V_VT(retv) = VT_BSTR;
699 V_BSTR(retv) = ret_str;
704 arg_var = get_arg(dp, 0);
705 switch(V_VT(arg_var)) {
707 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
709 if(is_class(regexp, JSCLASS_REGEXP)) {
712 jsdisp_release(regexp);
718 hres = to_string(dispex->ctx, arg_var, ei, &match_str);
723 if(arg_cnt(dp) >= 2) {
724 arg_var = get_arg(dp,1);
725 switch(V_VT(arg_var)) {
727 rep_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
729 if(is_class(rep_func, JSCLASS_FUNCTION)) {
732 jsdisp_release(rep_func);
738 hres = to_string(dispex->ctx, arg_var, ei, &rep_str);
742 if(strchrW(rep_str, '$')) {
743 FIXME("unsupported $ in replace string\n");
747 rep_len = SysStringLen(rep_str);
751 if(SUCCEEDED(hres)) {
752 const WCHAR *cp, *ecp;
758 hres = regexp_match_next(regexp, gcheck, str, length, &cp, rep_func ? &parens : NULL,
759 &parens_size, &parens_cnt, &match);
762 if(hres == S_FALSE) {
769 match.str = strstrW(cp, match_str);
772 match.len = SysStringLen(match_str);
773 cp = match.str+match.len;
776 hres = strbuf_append(&ret, ecp, match.str-ecp);
777 ecp = match.str+match.len;
784 hres = rep_call(rep_func, str, &match, parens, parens_cnt, lcid, &cstr, ei, caller);
788 hres = strbuf_append(&ret, cstr, SysStringLen(cstr));
793 hres = strbuf_append(&ret, rep_str, rep_len);
797 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d'};
799 hres = strbuf_append(&ret, undefinedW, sizeof(undefinedW)/sizeof(WCHAR));
806 hres = strbuf_append(&ret, ecp, (str+length)-ecp);
810 jsdisp_release(rep_func);
812 jsdisp_release(regexp);
813 SysFreeString(rep_str);
814 SysFreeString(match_str);
817 if(SUCCEEDED(hres) && retv) {
818 ret_str = SysAllocStringLen(ret.buf, ret.len);
820 return E_OUTOFMEMORY;
822 V_VT(retv) = VT_BSTR;
823 V_BSTR(retv) = ret_str;
824 TRACE("= %s\n", debugstr_w(ret_str));
831 static HRESULT String_search(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
832 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
838 /* ECMA-262 3rd Edition 15.5.4.13 */
839 static HRESULT String_slice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
840 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
850 if(is_class(dispex, JSCLASS_STRING)) {
851 StringInstance *string = (StringInstance*)dispex;
854 length = string->length;
856 FIXME("this is not a string class\n");
861 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &v);
865 if(V_VT(&v) == VT_I4) {
868 start = length + start;
871 }else if(start > length) {
875 start = V_R8(&v) < 0.0 ? 0 : length;
881 if(arg_cnt(dp) >= 2) {
882 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-2, ei, &v);
886 if(V_VT(&v) == VT_I4) {
892 }else if(end > length) {
896 end = V_R8(&v) < 0.0 ? 0 : length;
906 BSTR retstr = SysAllocStringLen(str+start, end-start);
908 return E_OUTOFMEMORY;
910 V_VT(retv) = VT_BSTR;
911 V_BSTR(retv) = retstr;
916 static HRESULT String_small(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
917 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
919 static const WCHAR smalltagW[] = {'S','M','A','L','L',0};
920 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, smalltagW);
923 static HRESULT String_split(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
924 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
926 match_result_t *match_result = NULL;
927 DWORD match_cnt, i, match_len = 0;
928 StringInstance *string;
929 const WCHAR *ptr, *ptr2;
932 BSTR match_str = NULL;
937 if(!is_class(dispex, JSCLASS_STRING)) {
938 FIXME("not String this\n");
942 string = (StringInstance*)dispex;
944 if(arg_cnt(dp) != 1) {
945 FIXME("unsupported args\n");
949 arg = get_arg(dp, 0);
954 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
956 if(is_class(regexp, JSCLASS_REGEXP)) {
957 hres = regexp_match(regexp, string->str, string->length, TRUE, &match_result, &match_cnt);
958 jsdisp_release(regexp);
963 jsdisp_release(regexp);
967 hres = to_string(dispex->ctx, arg, ei, &match_str);
971 match_len = SysStringLen(match_str);
973 SysFreeString(match_str);
978 hres = create_array(dispex->ctx, 0, &array);
980 if(SUCCEEDED(hres)) {
986 ptr2 = match_result[i].str;
987 }else if(match_str) {
988 ptr2 = strstrW(ptr, match_str);
997 V_VT(&var) = VT_BSTR;
998 V_BSTR(&var) = SysAllocStringLen(ptr, ptr2-ptr);
1000 hres = E_OUTOFMEMORY;
1004 hres = jsdisp_propput_idx(array, i, lcid, &var, ei, sp);
1005 SysFreeString(V_BSTR(&var));
1010 ptr = match_result[i].str + match_result[i].len;
1012 ptr = ptr2 + match_len;
1018 if(SUCCEEDED(hres) && (match_str || match_result)) {
1019 DWORD len = (string->str+string->length) - ptr;
1021 if(len || match_str) {
1022 V_VT(&var) = VT_BSTR;
1023 V_BSTR(&var) = SysAllocStringLen(ptr, len);
1026 hres = jsdisp_propput_idx(array, i, lcid, &var, ei, sp);
1027 SysFreeString(V_BSTR(&var));
1029 hres = E_OUTOFMEMORY;
1034 SysFreeString(match_str);
1035 heap_free(match_result);
1037 if(SUCCEEDED(hres) && retv) {
1038 V_VT(retv) = VT_DISPATCH;
1039 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(array);
1041 jsdisp_release(array);
1047 static HRESULT String_strike(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1048 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1050 static const WCHAR striketagW[] = {'S','T','R','I','K','E',0};
1051 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, striketagW);
1054 static HRESULT String_sub(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1055 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1057 static const WCHAR subtagW[] = {'S','U','B',0};
1058 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, subtagW);
1061 /* ECMA-262 3rd Edition 15.5.4.15 */
1062 static HRESULT String_substring(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1063 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1073 if(is_class(dispex, JSCLASS_STRING)) {
1074 StringInstance *string = (StringInstance*)dispex;
1076 length = string->length;
1079 FIXME("not string this not supported\n");
1083 if(arg_cnt(dp) >= 1) {
1084 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &v);
1088 if(V_VT(&v) == VT_I4) {
1092 else if(start >= length)
1095 start = V_R8(&v) < 0.0 ? 0 : length;
1099 if(arg_cnt(dp) >= 2) {
1100 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-2, ei, &v);
1104 if(V_VT(&v) == VT_I4) {
1108 else if(end > length)
1111 end = V_R8(&v) < 0.0 ? 0 : length;
1124 V_VT(retv) = VT_BSTR;
1125 V_BSTR(retv) = SysAllocStringLen(str+start, end-start);
1127 return E_OUTOFMEMORY;
1132 static HRESULT String_substr(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1133 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1139 static HRESULT String_sup(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1140 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1142 static const WCHAR suptagW[] = {'S','U','P',0};
1143 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, suptagW);
1146 static HRESULT String_toLowerCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1147 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1149 StringInstance *string;
1156 if(is_class(dispex, JSCLASS_STRING)) {
1157 string = (StringInstance*)dispex;
1159 length = string->length;
1162 FIXME("not string this not supported\n");
1167 bstr = SysAllocStringLen(str, length);
1169 return E_OUTOFMEMORY;
1173 V_VT(retv) = VT_BSTR;
1174 V_BSTR(retv) = bstr;
1179 static HRESULT String_toUpperCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1180 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1182 StringInstance *string;
1189 if(is_class(dispex, JSCLASS_STRING)) {
1190 string = (StringInstance*)dispex;
1192 length = string->length;
1195 FIXME("not string this not supported\n");
1200 bstr = SysAllocStringLen(str, length);
1202 return E_OUTOFMEMORY;
1206 V_VT(retv) = VT_BSTR;
1207 V_BSTR(retv) = bstr;
1212 static HRESULT String_toLocaleLowerCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1213 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1219 static HRESULT String_toLocaleUpperCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1220 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1226 static HRESULT String_localeCompare(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1227 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1233 static HRESULT String_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1234 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1240 static HRESULT String_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1241 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1247 static HRESULT String_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1248 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1254 static HRESULT String_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1255 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1257 StringInstance *This = (StringInstance*)dispex;
1262 case DISPATCH_PROPERTYGET: {
1263 BSTR str = SysAllocString(This->str);
1265 return E_OUTOFMEMORY;
1267 V_VT(retv) = VT_BSTR;
1272 FIXME("flags %x\n", flags);
1279 static void String_destructor(DispatchEx *dispex)
1281 StringInstance *This = (StringInstance*)dispex;
1283 heap_free(This->str);
1287 static const builtin_prop_t String_props[] = {
1288 {anchorW, String_anchor, PROPF_METHOD},
1289 {bigW, String_big, PROPF_METHOD},
1290 {blinkW, String_blink, PROPF_METHOD},
1291 {boldW, String_bold, PROPF_METHOD},
1292 {charAtW, String_charAt, PROPF_METHOD},
1293 {charCodeAtW, String_charCodeAt, PROPF_METHOD},
1294 {concatW, String_concat, PROPF_METHOD},
1295 {fixedW, String_fixed, PROPF_METHOD},
1296 {fontcolorW, String_fontcolor, PROPF_METHOD},
1297 {fontsizeW, String_fontsize, PROPF_METHOD},
1298 {hasOwnPropertyW, String_hasOwnProperty, PROPF_METHOD},
1299 {indexOfW, String_indexOf, PROPF_METHOD},
1300 {isPrototypeOfW, String_isPrototypeOf, PROPF_METHOD},
1301 {italicsW, String_italics, PROPF_METHOD},
1302 {lastIndexOfW, String_lastIndexOf, PROPF_METHOD},
1303 {lengthW, String_length, 0},
1304 {linkW, String_link, PROPF_METHOD},
1305 {localeCompareW, String_localeCompare, PROPF_METHOD},
1306 {matchW, String_match, PROPF_METHOD},
1307 {propertyIsEnumerableW, String_propertyIsEnumerable, PROPF_METHOD},
1308 {replaceW, String_replace, PROPF_METHOD},
1309 {searchW, String_search, PROPF_METHOD},
1310 {sliceW, String_slice, PROPF_METHOD},
1311 {smallW, String_small, PROPF_METHOD},
1312 {splitW, String_split, PROPF_METHOD},
1313 {strikeW, String_strike, PROPF_METHOD},
1314 {subW, String_sub, PROPF_METHOD},
1315 {substrW, String_substr, PROPF_METHOD},
1316 {substringW, String_substring, PROPF_METHOD},
1317 {supW, String_sup, PROPF_METHOD},
1318 {toLocaleLowerCaseW, String_toLocaleLowerCase, PROPF_METHOD},
1319 {toLocaleUpperCaseW, String_toLocaleUpperCase, PROPF_METHOD},
1320 {toLowerCaseW, String_toLowerCase, PROPF_METHOD},
1321 {toStringW, String_toString, PROPF_METHOD},
1322 {toUpperCaseW, String_toUpperCase, PROPF_METHOD},
1323 {valueOfW, String_valueOf, PROPF_METHOD}
1326 static const builtin_info_t String_info = {
1328 {NULL, String_value, 0},
1329 sizeof(String_props)/sizeof(*String_props),
1335 static HRESULT StringConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1336 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1347 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
1351 str = SysAllocStringLen(NULL, 0);
1353 return E_OUTOFMEMORY;
1356 V_VT(retv) = VT_BSTR;
1360 case DISPATCH_CONSTRUCT: {
1366 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
1370 hres = create_string(dispex->ctx, str, SysStringLen(str), &ret);
1373 hres = create_string(dispex->ctx, NULL, 0, &ret);
1379 V_VT(retv) = VT_DISPATCH;
1380 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
1385 FIXME("unimplemented flags: %x\n", flags);
1392 static HRESULT string_alloc(script_ctx_t *ctx, BOOL use_constr, StringInstance **ret)
1394 StringInstance *string;
1397 string = heap_alloc_zero(sizeof(StringInstance));
1399 return E_OUTOFMEMORY;
1402 hres = init_dispex_from_constr(&string->dispex, ctx, &String_info, ctx->string_constr);
1404 hres = init_dispex(&string->dispex, ctx, &String_info, NULL);
1414 HRESULT create_string_constr(script_ctx_t *ctx, DispatchEx **ret)
1416 StringInstance *string;
1419 hres = string_alloc(ctx, FALSE, &string);
1423 hres = create_builtin_function(ctx, StringConstr_value, NULL, PROPF_CONSTR, &string->dispex, ret);
1425 jsdisp_release(&string->dispex);
1429 HRESULT create_string(script_ctx_t *ctx, const WCHAR *str, DWORD len, DispatchEx **ret)
1431 StringInstance *string;
1434 hres = string_alloc(ctx, TRUE, &string);
1441 string->length = len;
1442 string->str = heap_alloc((len+1)*sizeof(WCHAR));
1444 jsdisp_release(&string->dispex);
1445 return E_OUTOFMEMORY;
1448 memcpy(string->str, str, len*sizeof(WCHAR));
1449 string->str[len] = 0;
1451 *ret = &string->dispex;