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)
588 StringInstance *This = (StringInstance*)dispex;
589 match_result_t *match_result;
592 VARIANT var, *arg_var;
598 if(arg_cnt(dp) != 1) {
599 FIXME("unsupported args\n");
603 arg_var = get_arg(dp, 0);
604 switch(V_VT(arg_var)) {
606 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
608 if(regexp->builtin_info->class == JSCLASS_REGEXP)
610 jsdisp_release(regexp);
615 hres = to_string(dispex->ctx, arg_var, ei, &match_str);
619 hres = create_regexp_str(dispex->ctx, match_str, SysStringLen(match_str), NULL, 0, ®exp);
620 SysFreeString(match_str);
626 hres = regexp_match(regexp, This->str, This->length, FALSE, &match_result, &match_cnt);
627 jsdisp_release(regexp);
635 V_VT(retv) = VT_NULL;
639 hres = create_array(dispex->ctx, match_cnt, &array);
643 V_VT(&var) = VT_BSTR;
645 for(i=0; i < match_cnt; i++) {
646 V_BSTR(&var) = SysAllocStringLen(match_result[i].str, match_result[i].len);
648 hres = E_OUTOFMEMORY;
652 hres = jsdisp_propput_idx(array, i, lcid, &var, ei, NULL/*FIXME*/);
653 SysFreeString(V_BSTR(&var));
658 if(SUCCEEDED(hres) && retv) {
659 V_VT(retv) = VT_DISPATCH;
660 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(array);
662 jsdisp_release(array);
673 static HRESULT strbuf_append(strbuf_t *buf, const WCHAR *str, DWORD len)
678 if(len + buf->len > buf->size) {
682 new_size = buf->size ? buf->size<<1 : 16;
683 if(new_size < buf->len+len)
684 new_size = buf->len+len;
686 new_buf = heap_realloc(buf->buf, new_size*sizeof(WCHAR));
688 new_buf = heap_alloc(new_size*sizeof(WCHAR));
690 return E_OUTOFMEMORY;
693 buf->size = new_size;
696 memcpy(buf->buf+buf->len, str, len*sizeof(WCHAR));
701 static HRESULT rep_call(DispatchEx *func, const WCHAR *str, match_result_t *match, match_result_t *parens,
702 DWORD parens_cnt, LCID lcid, BSTR *ret, jsexcept_t *ei, IServiceProvider *caller)
704 DISPPARAMS dp = {NULL, NULL, 0, 0};
705 VARIANTARG *args, *arg;
710 dp.cArgs = parens_cnt+3;
711 dp.rgvarg = args = heap_alloc_zero(sizeof(VARIANT)*dp.cArgs);
713 return E_OUTOFMEMORY;
715 arg = get_arg(&dp,0);
717 V_BSTR(arg) = SysAllocStringLen(match->str, match->len);
719 hres = E_OUTOFMEMORY;
721 if(SUCCEEDED(hres)) {
722 for(i=0; i < parens_cnt; i++) {
723 arg = get_arg(&dp,i+1);
725 V_BSTR(arg) = SysAllocStringLen(parens[i].str, parens[i].len);
727 hres = E_OUTOFMEMORY;
733 if(SUCCEEDED(hres)) {
734 arg = get_arg(&dp,parens_cnt+1);
736 V_I4(arg) = match->str - str;
738 arg = get_arg(&dp,parens_cnt+2);
740 V_BSTR(arg) = SysAllocString(str);
742 hres = E_OUTOFMEMORY;
746 hres = jsdisp_call_value(func, lcid, DISPATCH_METHOD, &dp, &var, ei, caller);
748 for(i=0; i < parens_cnt+1; i++) {
749 if(i != parens_cnt+1)
750 SysFreeString(V_BSTR(get_arg(&dp,i)));
757 hres = to_string(func->ctx, &var, ei, ret);
762 /* ECMA-262 3rd Edition 15.5.4.11 */
763 static HRESULT String_replace(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
764 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
766 DWORD parens_cnt = 0, parens_size=0, rep_len=0, length;
767 BSTR rep_str = NULL, match_str = NULL, ret_str;
768 DispatchEx *rep_func = NULL, *regexp = NULL;
769 match_result_t *parens = NULL, match;
771 strbuf_t ret = {NULL,0,0};
778 if(is_class(dispex, JSCLASS_STRING)) {
779 StringInstance *string = (StringInstance*)dispex;
781 length = string->length;
783 FIXME("not String this\n");
789 ret_str = SysAllocString(str);
791 return E_OUTOFMEMORY;
793 V_VT(retv) = VT_BSTR;
794 V_BSTR(retv) = ret_str;
799 arg_var = get_arg(dp, 0);
800 switch(V_VT(arg_var)) {
802 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
804 if(is_class(regexp, JSCLASS_REGEXP)) {
807 jsdisp_release(regexp);
813 hres = to_string(dispex->ctx, arg_var, ei, &match_str);
818 if(arg_cnt(dp) >= 2) {
819 arg_var = get_arg(dp,1);
820 switch(V_VT(arg_var)) {
822 rep_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
824 if(is_class(rep_func, JSCLASS_FUNCTION)) {
827 jsdisp_release(rep_func);
833 hres = to_string(dispex->ctx, arg_var, ei, &rep_str);
837 if(strchrW(rep_str, '$')) {
838 FIXME("unsupported $ in replace string\n");
842 rep_len = SysStringLen(rep_str);
846 if(SUCCEEDED(hres)) {
847 const WCHAR *cp, *ecp;
853 hres = regexp_match_next(regexp, gcheck, str, length, &cp, rep_func ? &parens : NULL,
854 &parens_size, &parens_cnt, &match);
857 if(hres == S_FALSE) {
864 match.str = strstrW(cp, match_str);
867 match.len = SysStringLen(match_str);
868 cp = match.str+match.len;
871 hres = strbuf_append(&ret, ecp, match.str-ecp);
872 ecp = match.str+match.len;
879 hres = rep_call(rep_func, str, &match, parens, parens_cnt, lcid, &cstr, ei, caller);
883 hres = strbuf_append(&ret, cstr, SysStringLen(cstr));
888 hres = strbuf_append(&ret, rep_str, rep_len);
892 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d'};
894 hres = strbuf_append(&ret, undefinedW, sizeof(undefinedW)/sizeof(WCHAR));
901 hres = strbuf_append(&ret, ecp, (str+length)-ecp);
905 jsdisp_release(rep_func);
907 jsdisp_release(regexp);
908 SysFreeString(rep_str);
909 SysFreeString(match_str);
912 if(SUCCEEDED(hres) && retv) {
913 ret_str = SysAllocStringLen(ret.buf, ret.len);
915 return E_OUTOFMEMORY;
917 V_VT(retv) = VT_BSTR;
918 V_BSTR(retv) = ret_str;
919 TRACE("= %s\n", debugstr_w(ret_str));
926 static HRESULT String_search(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
927 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
933 /* ECMA-262 3rd Edition 15.5.4.13 */
934 static HRESULT String_slice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
935 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
945 if(is_class(dispex, JSCLASS_STRING)) {
946 StringInstance *string = (StringInstance*)dispex;
949 length = string->length;
951 FIXME("this is not a string class\n");
956 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &v);
960 if(V_VT(&v) == VT_I4) {
963 start = length + start;
966 }else if(start > length) {
970 start = V_R8(&v) < 0.0 ? 0 : length;
976 if(arg_cnt(dp) >= 2) {
977 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-2, ei, &v);
981 if(V_VT(&v) == VT_I4) {
987 }else if(end > length) {
991 end = V_R8(&v) < 0.0 ? 0 : length;
1001 BSTR retstr = SysAllocStringLen(str+start, end-start);
1003 return E_OUTOFMEMORY;
1005 V_VT(retv) = VT_BSTR;
1006 V_BSTR(retv) = retstr;
1011 static HRESULT String_small(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1012 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1014 static const WCHAR smalltagW[] = {'S','M','A','L','L',0};
1015 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, smalltagW);
1018 static HRESULT String_split(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1019 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1021 match_result_t *match_result = NULL;
1022 DWORD match_cnt, i, match_len = 0;
1023 StringInstance *string;
1024 const WCHAR *ptr, *ptr2;
1027 BSTR match_str = NULL;
1032 if(!is_class(dispex, JSCLASS_STRING)) {
1033 FIXME("not String this\n");
1037 string = (StringInstance*)dispex;
1039 if(arg_cnt(dp) != 1) {
1040 FIXME("unsupported args\n");
1044 arg = get_arg(dp, 0);
1049 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
1051 if(is_class(regexp, JSCLASS_REGEXP)) {
1052 hres = regexp_match(regexp, string->str, string->length, TRUE, &match_result, &match_cnt);
1053 jsdisp_release(regexp);
1058 jsdisp_release(regexp);
1062 hres = to_string(dispex->ctx, arg, ei, &match_str);
1066 match_len = SysStringLen(match_str);
1068 SysFreeString(match_str);
1073 hres = create_array(dispex->ctx, 0, &array);
1075 if(SUCCEEDED(hres)) {
1081 ptr2 = match_result[i].str;
1082 }else if(match_str) {
1083 ptr2 = strstrW(ptr, match_str);
1092 V_VT(&var) = VT_BSTR;
1093 V_BSTR(&var) = SysAllocStringLen(ptr, ptr2-ptr);
1095 hres = E_OUTOFMEMORY;
1099 hres = jsdisp_propput_idx(array, i, lcid, &var, ei, sp);
1100 SysFreeString(V_BSTR(&var));
1105 ptr = match_result[i].str + match_result[i].len;
1107 ptr = ptr2 + match_len;
1113 if(SUCCEEDED(hres) && (match_str || match_result)) {
1114 DWORD len = (string->str+string->length) - ptr;
1116 if(len || match_str) {
1117 V_VT(&var) = VT_BSTR;
1118 V_BSTR(&var) = SysAllocStringLen(ptr, len);
1121 hres = jsdisp_propput_idx(array, i, lcid, &var, ei, sp);
1122 SysFreeString(V_BSTR(&var));
1124 hres = E_OUTOFMEMORY;
1129 SysFreeString(match_str);
1130 heap_free(match_result);
1132 if(SUCCEEDED(hres) && retv) {
1133 V_VT(retv) = VT_DISPATCH;
1134 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(array);
1136 jsdisp_release(array);
1142 static HRESULT String_strike(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1143 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1145 static const WCHAR striketagW[] = {'S','T','R','I','K','E',0};
1146 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, striketagW);
1149 static HRESULT String_sub(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1150 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1152 static const WCHAR subtagW[] = {'S','U','B',0};
1153 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, subtagW);
1156 /* ECMA-262 3rd Edition 15.5.4.15 */
1157 static HRESULT String_substring(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1158 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1168 if(is_class(dispex, JSCLASS_STRING)) {
1169 StringInstance *string = (StringInstance*)dispex;
1171 length = string->length;
1174 FIXME("not string this not supported\n");
1178 if(arg_cnt(dp) >= 1) {
1179 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &v);
1183 if(V_VT(&v) == VT_I4) {
1187 else if(start >= length)
1190 start = V_R8(&v) < 0.0 ? 0 : length;
1194 if(arg_cnt(dp) >= 2) {
1195 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-2, ei, &v);
1199 if(V_VT(&v) == VT_I4) {
1203 else if(end > length)
1206 end = V_R8(&v) < 0.0 ? 0 : length;
1219 V_VT(retv) = VT_BSTR;
1220 V_BSTR(retv) = SysAllocStringLen(str+start, end-start);
1222 return E_OUTOFMEMORY;
1227 static HRESULT String_substr(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1228 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1234 static HRESULT String_sup(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1235 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1237 static const WCHAR suptagW[] = {'S','U','P',0};
1238 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, suptagW);
1241 static HRESULT String_toLowerCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1242 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1244 StringInstance *string;
1251 if(is_class(dispex, JSCLASS_STRING)) {
1252 string = (StringInstance*)dispex;
1254 length = string->length;
1257 FIXME("not string this not supported\n");
1262 bstr = SysAllocStringLen(str, length);
1264 return E_OUTOFMEMORY;
1268 V_VT(retv) = VT_BSTR;
1269 V_BSTR(retv) = bstr;
1274 static HRESULT String_toUpperCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1275 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1277 StringInstance *string;
1284 if(is_class(dispex, JSCLASS_STRING)) {
1285 string = (StringInstance*)dispex;
1287 length = string->length;
1290 FIXME("not string this not supported\n");
1295 bstr = SysAllocStringLen(str, length);
1297 return E_OUTOFMEMORY;
1301 V_VT(retv) = VT_BSTR;
1302 V_BSTR(retv) = bstr;
1307 static HRESULT String_toLocaleLowerCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1308 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1314 static HRESULT String_toLocaleUpperCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1315 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1321 static HRESULT String_localeCompare(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1322 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1328 static HRESULT String_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1329 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1335 static HRESULT String_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1336 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1342 static HRESULT String_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1343 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1349 static HRESULT String_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1350 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1352 StringInstance *This = (StringInstance*)dispex;
1357 case DISPATCH_PROPERTYGET: {
1358 BSTR str = SysAllocString(This->str);
1360 return E_OUTOFMEMORY;
1362 V_VT(retv) = VT_BSTR;
1367 FIXME("flags %x\n", flags);
1374 static void String_destructor(DispatchEx *dispex)
1376 StringInstance *This = (StringInstance*)dispex;
1378 heap_free(This->str);
1382 static const builtin_prop_t String_props[] = {
1383 {anchorW, String_anchor, PROPF_METHOD},
1384 {bigW, String_big, PROPF_METHOD},
1385 {blinkW, String_blink, PROPF_METHOD},
1386 {boldW, String_bold, PROPF_METHOD},
1387 {charAtW, String_charAt, PROPF_METHOD},
1388 {charCodeAtW, String_charCodeAt, PROPF_METHOD},
1389 {concatW, String_concat, PROPF_METHOD},
1390 {fixedW, String_fixed, PROPF_METHOD},
1391 {fontcolorW, String_fontcolor, PROPF_METHOD},
1392 {fontsizeW, String_fontsize, PROPF_METHOD},
1393 {hasOwnPropertyW, String_hasOwnProperty, PROPF_METHOD},
1394 {indexOfW, String_indexOf, PROPF_METHOD},
1395 {isPrototypeOfW, String_isPrototypeOf, PROPF_METHOD},
1396 {italicsW, String_italics, PROPF_METHOD},
1397 {lastIndexOfW, String_lastIndexOf, PROPF_METHOD},
1398 {lengthW, String_length, 0},
1399 {linkW, String_link, PROPF_METHOD},
1400 {localeCompareW, String_localeCompare, PROPF_METHOD},
1401 {matchW, String_match, PROPF_METHOD},
1402 {propertyIsEnumerableW, String_propertyIsEnumerable, PROPF_METHOD},
1403 {replaceW, String_replace, PROPF_METHOD},
1404 {searchW, String_search, PROPF_METHOD},
1405 {sliceW, String_slice, PROPF_METHOD},
1406 {smallW, String_small, PROPF_METHOD},
1407 {splitW, String_split, PROPF_METHOD},
1408 {strikeW, String_strike, PROPF_METHOD},
1409 {subW, String_sub, PROPF_METHOD},
1410 {substrW, String_substr, PROPF_METHOD},
1411 {substringW, String_substring, PROPF_METHOD},
1412 {supW, String_sup, PROPF_METHOD},
1413 {toLocaleLowerCaseW, String_toLocaleLowerCase, PROPF_METHOD},
1414 {toLocaleUpperCaseW, String_toLocaleUpperCase, PROPF_METHOD},
1415 {toLowerCaseW, String_toLowerCase, PROPF_METHOD},
1416 {toStringW, String_toString, PROPF_METHOD},
1417 {toUpperCaseW, String_toUpperCase, PROPF_METHOD},
1418 {valueOfW, String_valueOf, PROPF_METHOD}
1421 static const builtin_info_t String_info = {
1423 {NULL, String_value, 0},
1424 sizeof(String_props)/sizeof(*String_props),
1430 static HRESULT StringConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1431 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1442 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
1446 str = SysAllocStringLen(NULL, 0);
1448 return E_OUTOFMEMORY;
1451 V_VT(retv) = VT_BSTR;
1455 case DISPATCH_CONSTRUCT: {
1461 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
1465 hres = create_string(dispex->ctx, str, SysStringLen(str), &ret);
1468 hres = create_string(dispex->ctx, NULL, 0, &ret);
1474 V_VT(retv) = VT_DISPATCH;
1475 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
1480 FIXME("unimplemented flags: %x\n", flags);
1487 static HRESULT string_alloc(script_ctx_t *ctx, BOOL use_constr, StringInstance **ret)
1489 StringInstance *string;
1492 string = heap_alloc_zero(sizeof(StringInstance));
1494 return E_OUTOFMEMORY;
1497 hres = init_dispex_from_constr(&string->dispex, ctx, &String_info, ctx->string_constr);
1499 hres = init_dispex(&string->dispex, ctx, &String_info, NULL);
1509 HRESULT create_string_constr(script_ctx_t *ctx, DispatchEx **ret)
1511 StringInstance *string;
1514 hres = string_alloc(ctx, FALSE, &string);
1518 hres = create_builtin_function(ctx, StringConstr_value, NULL, PROPF_CONSTR, &string->dispex, ret);
1520 jsdisp_release(&string->dispex);
1524 HRESULT create_string(script_ctx_t *ctx, const WCHAR *str, DWORD len, DispatchEx **ret)
1526 StringInstance *string;
1529 hres = string_alloc(ctx, TRUE, &string);
1536 string->length = len;
1537 string->str = heap_alloc((len+1)*sizeof(WCHAR));
1539 jsdisp_release(&string->dispex);
1540 return E_OUTOFMEMORY;
1543 memcpy(string->str, str, len*sizeof(WCHAR));
1544 string->str[len] = 0;
1546 *ret = &string->dispex;