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 String_anchor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
154 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
160 static HRESULT String_big(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
161 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
163 static const WCHAR bigtagW[] = {'B','I','G',0};
164 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, bigtagW);
167 static HRESULT String_blink(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
168 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
170 static const WCHAR blinktagW[] = {'B','L','I','N','K',0};
171 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, blinktagW);
174 static HRESULT String_bold(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
175 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
177 static const WCHAR boldtagW[] = {'B',0};
178 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, boldtagW);
181 /* ECMA-262 3rd Edition 15.5.4.5 */
182 static HRESULT String_charAt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
183 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
185 StringInstance *strobj;
192 if(dispex->builtin_info->class != JSCLASS_STRING) {
193 FIXME("not string this not supported\n");
197 strobj = (StringInstance*)dispex;
202 hres = to_integer(dispex->ctx, get_arg(dp, 0), ei, &num);
206 if(V_VT(&num) == VT_I4) {
209 WARN("pos = %lf\n", V_R8(&num));
217 if(0 <= pos && pos < strobj->length)
218 str = SysAllocStringLen(strobj->str+pos, 1);
220 str = SysAllocStringLen(NULL, 0);
222 return E_OUTOFMEMORY;
224 V_VT(retv) = VT_BSTR;
229 /* ECMA-262 3rd Edition 15.5.4.5 */
230 static HRESULT String_charCodeAt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
231 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
234 DWORD length, idx = 0;
239 if(dispex->builtin_info->class == JSCLASS_STRING) {
240 StringInstance *string = (StringInstance*)dispex;
243 length = string->length;
245 FIXME("not string this not supported\n");
249 if(arg_cnt(dp) > 0) {
252 hres = to_integer(dispex->ctx, get_arg(dp, 0), ei, &v);
256 if(V_VT(&v) != VT_I4 || V_I4(&v) < 0 || V_I4(&v) >= length) {
266 V_I4(retv) = str[idx];
271 /* ECMA-262 3rd Edition 15.5.4.6 */
272 static HRESULT String_concat(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
273 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
275 BSTR *strs = NULL, ret = NULL;
276 DWORD len = 0, i, l, str_cnt;
283 str_cnt = arg_cnt(dp)+1;
284 strs = heap_alloc_zero(str_cnt * sizeof(BSTR));
286 return E_OUTOFMEMORY;
288 V_VT(&var) = VT_DISPATCH;
289 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(dispex);
291 hres = to_string(dispex->ctx, &var, ei, strs);
292 if(SUCCEEDED(hres)) {
293 for(i=0; i < arg_cnt(dp); i++) {
294 hres = to_string(dispex->ctx, get_arg(dp, i), ei, strs+i+1);
300 if(SUCCEEDED(hres)) {
301 for(i=0; i < str_cnt; i++)
302 len += SysStringLen(strs[i]);
304 ptr = ret = SysAllocStringLen(NULL, len);
306 for(i=0; i < str_cnt; i++) {
307 l = SysStringLen(strs[i]);
308 memcpy(ptr, strs[i], l*sizeof(WCHAR));
313 for(i=0; i < str_cnt; i++)
314 SysFreeString(strs[i]);
321 V_VT(retv) = VT_BSTR;
329 static HRESULT String_fixed(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
330 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
332 static const WCHAR fixedtagW[] = {'T','T',0};
333 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, fixedtagW);
336 static HRESULT String_fontcolor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
337 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
343 static HRESULT String_fontsize(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
344 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
350 static HRESULT String_indexOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
351 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
353 DWORD length, pos = 0;
361 if(is_class(dispex, JSCLASS_STRING)) {
362 StringInstance *string = (StringInstance*)dispex;
365 length = string->length;
367 FIXME("not String this\n");
379 hres = to_string(dispex->ctx, get_arg(dp,0), ei, &search_str);
383 if(arg_cnt(dp) >= 2) {
386 hres = to_integer(dispex->ctx, get_arg(dp,1), ei, &ival);
387 if(SUCCEEDED(hres)) {
388 if(V_VT(&ival) == VT_I4)
389 pos = V_VT(&ival) > 0 ? V_I4(&ival) : 0;
391 pos = V_R8(&ival) > 0.0 ? length : 0;
397 if(SUCCEEDED(hres)) {
400 ptr = strstrW(str+pos, search_str);
407 SysFreeString(search_str);
418 static HRESULT String_italics(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
419 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
421 static const WCHAR italicstagW[] = {'I',0};
422 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, italicstagW);
425 static HRESULT String_lastIndexOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
426 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
432 static HRESULT String_link(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
433 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
439 /* ECMA-262 3rd Edition 15.5.4.10 */
440 static HRESULT String_match(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
441 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
443 StringInstance *This = (StringInstance*)dispex;
444 match_result_t *match_result;
447 VARIANT var, *arg_var;
453 if(arg_cnt(dp) != 1) {
454 FIXME("unsupported args\n");
458 arg_var = get_arg(dp, 0);
459 switch(V_VT(arg_var)) {
461 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
463 if(regexp->builtin_info->class == JSCLASS_REGEXP)
465 jsdisp_release(regexp);
470 hres = to_string(dispex->ctx, arg_var, ei, &match_str);
474 hres = create_regexp_str(dispex->ctx, match_str, SysStringLen(match_str), NULL, 0, ®exp);
475 SysFreeString(match_str);
481 hres = regexp_match(regexp, This->str, This->length, FALSE, &match_result, &match_cnt);
482 jsdisp_release(regexp);
490 V_VT(retv) = VT_NULL;
494 hres = create_array(dispex->ctx, match_cnt, &array);
498 V_VT(&var) = VT_BSTR;
500 for(i=0; i < match_cnt; i++) {
501 V_BSTR(&var) = SysAllocStringLen(match_result[i].str, match_result[i].len);
503 hres = E_OUTOFMEMORY;
507 hres = jsdisp_propput_idx(array, i, lcid, &var, ei, NULL/*FIXME*/);
508 SysFreeString(V_BSTR(&var));
513 if(SUCCEEDED(hres) && retv) {
514 V_VT(retv) = VT_DISPATCH;
515 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(array);
517 jsdisp_release(array);
528 static HRESULT strbuf_append(strbuf_t *buf, const WCHAR *str, DWORD len)
533 if(len + buf->len > buf->size) {
537 new_size = buf->size ? buf->size<<1 : 16;
538 if(new_size < buf->len+len)
539 new_size = buf->len+len;
541 new_buf = heap_realloc(buf->buf, new_size*sizeof(WCHAR));
543 new_buf = heap_alloc(new_size*sizeof(WCHAR));
545 return E_OUTOFMEMORY;
548 buf->size = new_size;
551 memcpy(buf->buf+buf->len, str, len*sizeof(WCHAR));
556 static HRESULT rep_call(DispatchEx *func, const WCHAR *str, match_result_t *match, match_result_t *parens,
557 DWORD parens_cnt, LCID lcid, BSTR *ret, jsexcept_t *ei, IServiceProvider *caller)
559 DISPPARAMS dp = {NULL, NULL, 0, 0};
560 VARIANTARG *args, *arg;
565 dp.cArgs = parens_cnt+3;
566 dp.rgvarg = args = heap_alloc_zero(sizeof(VARIANT)*dp.cArgs);
568 return E_OUTOFMEMORY;
570 arg = get_arg(&dp,0);
572 V_BSTR(arg) = SysAllocStringLen(match->str, match->len);
574 hres = E_OUTOFMEMORY;
576 if(SUCCEEDED(hres)) {
577 for(i=0; i < parens_cnt; i++) {
578 arg = get_arg(&dp,i+1);
580 V_BSTR(arg) = SysAllocStringLen(parens[i].str, parens[i].len);
582 hres = E_OUTOFMEMORY;
588 if(SUCCEEDED(hres)) {
589 arg = get_arg(&dp,parens_cnt+1);
591 V_I4(arg) = match->str - str;
593 arg = get_arg(&dp,parens_cnt+2);
595 V_BSTR(arg) = SysAllocString(str);
597 hres = E_OUTOFMEMORY;
601 hres = jsdisp_call_value(func, lcid, DISPATCH_METHOD, &dp, &var, ei, caller);
603 for(i=0; i < parens_cnt+1; i++) {
604 if(i != parens_cnt+1)
605 SysFreeString(V_BSTR(get_arg(&dp,i)));
612 hres = to_string(func->ctx, &var, ei, ret);
617 /* ECMA-262 3rd Edition 15.5.4.11 */
618 static HRESULT String_replace(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
619 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
621 DWORD parens_cnt = 0, parens_size=0, rep_len=0, length;
622 BSTR rep_str = NULL, match_str = NULL, ret_str;
623 DispatchEx *rep_func = NULL, *regexp = NULL;
624 match_result_t *parens = NULL, match;
626 strbuf_t ret = {NULL,0,0};
633 if(is_class(dispex, JSCLASS_STRING)) {
634 StringInstance *string = (StringInstance*)dispex;
636 length = string->length;
638 FIXME("not String this\n");
644 ret_str = SysAllocString(str);
646 return E_OUTOFMEMORY;
648 V_VT(retv) = VT_BSTR;
649 V_BSTR(retv) = ret_str;
654 arg_var = get_arg(dp, 0);
655 switch(V_VT(arg_var)) {
657 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
659 if(is_class(regexp, JSCLASS_REGEXP)) {
662 jsdisp_release(regexp);
668 hres = to_string(dispex->ctx, arg_var, ei, &match_str);
673 if(arg_cnt(dp) >= 2) {
674 arg_var = get_arg(dp,1);
675 switch(V_VT(arg_var)) {
677 rep_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
679 if(is_class(rep_func, JSCLASS_FUNCTION)) {
682 jsdisp_release(rep_func);
688 hres = to_string(dispex->ctx, arg_var, ei, &rep_str);
692 if(strchrW(rep_str, '$')) {
693 FIXME("unsupported $ in replace string\n");
697 rep_len = SysStringLen(rep_str);
701 if(SUCCEEDED(hres)) {
702 const WCHAR *cp, *ecp;
708 hres = regexp_match_next(regexp, gcheck, str, length, &cp, rep_func ? &parens : NULL,
709 &parens_size, &parens_cnt, &match);
712 if(hres == S_FALSE) {
719 match.str = strstrW(cp, match_str);
722 match.len = SysStringLen(match_str);
723 cp = match.str+match.len;
726 hres = strbuf_append(&ret, ecp, match.str-ecp);
727 ecp = match.str+match.len;
734 hres = rep_call(rep_func, str, &match, parens, parens_cnt, lcid, &cstr, ei, caller);
738 hres = strbuf_append(&ret, cstr, SysStringLen(cstr));
743 hres = strbuf_append(&ret, rep_str, rep_len);
747 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d'};
749 hres = strbuf_append(&ret, undefinedW, sizeof(undefinedW)/sizeof(WCHAR));
756 hres = strbuf_append(&ret, ecp, (str+length)-ecp);
760 jsdisp_release(rep_func);
762 jsdisp_release(regexp);
763 SysFreeString(rep_str);
764 SysFreeString(match_str);
767 if(SUCCEEDED(hres) && retv) {
768 ret_str = SysAllocStringLen(ret.buf, ret.len);
770 return E_OUTOFMEMORY;
772 V_VT(retv) = VT_BSTR;
773 V_BSTR(retv) = ret_str;
774 TRACE("= %s\n", debugstr_w(ret_str));
781 static HRESULT String_search(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
782 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
788 /* ECMA-262 3rd Edition 15.5.4.13 */
789 static HRESULT String_slice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
790 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
800 if(is_class(dispex, JSCLASS_STRING)) {
801 StringInstance *string = (StringInstance*)dispex;
804 length = string->length;
806 FIXME("this is not a string class\n");
811 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &v);
815 if(V_VT(&v) == VT_I4) {
818 start = length + start;
821 }else if(start > length) {
825 start = V_R8(&v) < 0.0 ? 0 : length;
831 if(arg_cnt(dp) >= 2) {
832 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-2, ei, &v);
836 if(V_VT(&v) == VT_I4) {
842 }else if(end > length) {
846 end = V_R8(&v) < 0.0 ? 0 : length;
856 BSTR retstr = SysAllocStringLen(str+start, end-start);
858 return E_OUTOFMEMORY;
860 V_VT(retv) = VT_BSTR;
861 V_BSTR(retv) = retstr;
866 static HRESULT String_small(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
867 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
869 static const WCHAR smalltagW[] = {'S','M','A','L','L',0};
870 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, smalltagW);
873 static HRESULT String_split(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
874 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
876 match_result_t *match_result = NULL;
877 DWORD match_cnt, i, match_len = 0;
878 StringInstance *string;
879 const WCHAR *ptr, *ptr2;
882 BSTR match_str = NULL;
887 if(!is_class(dispex, JSCLASS_STRING)) {
888 FIXME("not String this\n");
892 string = (StringInstance*)dispex;
894 if(arg_cnt(dp) != 1) {
895 FIXME("unsupported args\n");
899 arg = get_arg(dp, 0);
904 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
906 if(is_class(regexp, JSCLASS_REGEXP)) {
907 hres = regexp_match(regexp, string->str, string->length, TRUE, &match_result, &match_cnt);
908 jsdisp_release(regexp);
913 jsdisp_release(regexp);
917 hres = to_string(dispex->ctx, arg, ei, &match_str);
921 match_len = SysStringLen(match_str);
923 SysFreeString(match_str);
928 hres = create_array(dispex->ctx, 0, &array);
930 if(SUCCEEDED(hres)) {
936 ptr2 = match_result[i].str;
937 }else if(match_str) {
938 ptr2 = strstrW(ptr, match_str);
947 V_VT(&var) = VT_BSTR;
948 V_BSTR(&var) = SysAllocStringLen(ptr, ptr2-ptr);
950 hres = E_OUTOFMEMORY;
954 hres = jsdisp_propput_idx(array, i, lcid, &var, ei, sp);
955 SysFreeString(V_BSTR(&var));
960 ptr = match_result[i].str + match_result[i].len;
962 ptr = ptr2 + match_len;
968 if(SUCCEEDED(hres) && (match_str || match_result)) {
969 DWORD len = (string->str+string->length) - ptr;
971 if(len || match_str) {
972 V_VT(&var) = VT_BSTR;
973 V_BSTR(&var) = SysAllocStringLen(ptr, len);
976 hres = jsdisp_propput_idx(array, i, lcid, &var, ei, sp);
977 SysFreeString(V_BSTR(&var));
979 hres = E_OUTOFMEMORY;
984 SysFreeString(match_str);
985 heap_free(match_result);
987 if(SUCCEEDED(hres) && retv) {
988 V_VT(retv) = VT_DISPATCH;
989 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(array);
991 jsdisp_release(array);
997 static HRESULT String_strike(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
998 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1000 static const WCHAR striketagW[] = {'S','T','R','I','K','E',0};
1001 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, striketagW);
1004 static HRESULT String_sub(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1005 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1007 static const WCHAR subtagW[] = {'S','U','B',0};
1008 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, subtagW);
1011 /* ECMA-262 3rd Edition 15.5.4.15 */
1012 static HRESULT String_substring(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1013 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1023 if(is_class(dispex, JSCLASS_STRING)) {
1024 StringInstance *string = (StringInstance*)dispex;
1026 length = string->length;
1029 FIXME("not string this not supported\n");
1033 if(arg_cnt(dp) >= 1) {
1034 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &v);
1038 if(V_VT(&v) == VT_I4) {
1042 else if(start >= length)
1045 start = V_R8(&v) < 0.0 ? 0 : length;
1049 if(arg_cnt(dp) >= 2) {
1050 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-2, ei, &v);
1054 if(V_VT(&v) == VT_I4) {
1058 else if(end > length)
1061 end = V_R8(&v) < 0.0 ? 0 : length;
1074 V_VT(retv) = VT_BSTR;
1075 V_BSTR(retv) = SysAllocStringLen(str+start, end-start);
1077 return E_OUTOFMEMORY;
1082 static HRESULT String_substr(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1083 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1089 static HRESULT String_sup(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1090 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1092 static const WCHAR suptagW[] = {'S','U','P',0};
1093 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, suptagW);
1096 static HRESULT String_toLowerCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1097 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1099 StringInstance *string;
1106 if(is_class(dispex, JSCLASS_STRING)) {
1107 string = (StringInstance*)dispex;
1109 length = string->length;
1112 FIXME("not string this not supported\n");
1117 bstr = SysAllocStringLen(str, length);
1119 return E_OUTOFMEMORY;
1123 V_VT(retv) = VT_BSTR;
1124 V_BSTR(retv) = bstr;
1129 static HRESULT String_toUpperCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1130 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1132 StringInstance *string;
1139 if(is_class(dispex, JSCLASS_STRING)) {
1140 string = (StringInstance*)dispex;
1142 length = string->length;
1145 FIXME("not string this not supported\n");
1150 bstr = SysAllocStringLen(str, length);
1152 return E_OUTOFMEMORY;
1156 V_VT(retv) = VT_BSTR;
1157 V_BSTR(retv) = bstr;
1162 static HRESULT String_toLocaleLowerCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1163 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1169 static HRESULT String_toLocaleUpperCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1170 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1176 static HRESULT String_localeCompare(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1177 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1183 static HRESULT String_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1184 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1190 static HRESULT String_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1191 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1197 static HRESULT String_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1198 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1204 static HRESULT String_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1205 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1207 StringInstance *This = (StringInstance*)dispex;
1212 case DISPATCH_PROPERTYGET: {
1213 BSTR str = SysAllocString(This->str);
1215 return E_OUTOFMEMORY;
1217 V_VT(retv) = VT_BSTR;
1222 FIXME("flags %x\n", flags);
1229 static void String_destructor(DispatchEx *dispex)
1231 StringInstance *This = (StringInstance*)dispex;
1233 heap_free(This->str);
1237 static const builtin_prop_t String_props[] = {
1238 {anchorW, String_anchor, PROPF_METHOD},
1239 {bigW, String_big, PROPF_METHOD},
1240 {blinkW, String_blink, PROPF_METHOD},
1241 {boldW, String_bold, PROPF_METHOD},
1242 {charAtW, String_charAt, PROPF_METHOD},
1243 {charCodeAtW, String_charCodeAt, PROPF_METHOD},
1244 {concatW, String_concat, PROPF_METHOD},
1245 {fixedW, String_fixed, PROPF_METHOD},
1246 {fontcolorW, String_fontcolor, PROPF_METHOD},
1247 {fontsizeW, String_fontsize, PROPF_METHOD},
1248 {hasOwnPropertyW, String_hasOwnProperty, PROPF_METHOD},
1249 {indexOfW, String_indexOf, PROPF_METHOD},
1250 {isPrototypeOfW, String_isPrototypeOf, PROPF_METHOD},
1251 {italicsW, String_italics, PROPF_METHOD},
1252 {lastIndexOfW, String_lastIndexOf, PROPF_METHOD},
1253 {lengthW, String_length, 0},
1254 {linkW, String_link, PROPF_METHOD},
1255 {localeCompareW, String_localeCompare, PROPF_METHOD},
1256 {matchW, String_match, PROPF_METHOD},
1257 {propertyIsEnumerableW, String_propertyIsEnumerable, PROPF_METHOD},
1258 {replaceW, String_replace, PROPF_METHOD},
1259 {searchW, String_search, PROPF_METHOD},
1260 {sliceW, String_slice, PROPF_METHOD},
1261 {smallW, String_small, PROPF_METHOD},
1262 {splitW, String_split, PROPF_METHOD},
1263 {strikeW, String_strike, PROPF_METHOD},
1264 {subW, String_sub, PROPF_METHOD},
1265 {substrW, String_substr, PROPF_METHOD},
1266 {substringW, String_substring, PROPF_METHOD},
1267 {supW, String_sup, PROPF_METHOD},
1268 {toLocaleLowerCaseW, String_toLocaleLowerCase, PROPF_METHOD},
1269 {toLocaleUpperCaseW, String_toLocaleUpperCase, PROPF_METHOD},
1270 {toLowerCaseW, String_toLowerCase, PROPF_METHOD},
1271 {toStringW, String_toString, PROPF_METHOD},
1272 {toUpperCaseW, String_toUpperCase, PROPF_METHOD},
1273 {valueOfW, String_valueOf, PROPF_METHOD}
1276 static const builtin_info_t String_info = {
1278 {NULL, String_value, 0},
1279 sizeof(String_props)/sizeof(*String_props),
1285 static HRESULT StringConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1286 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1297 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
1301 str = SysAllocStringLen(NULL, 0);
1303 return E_OUTOFMEMORY;
1306 V_VT(retv) = VT_BSTR;
1310 case DISPATCH_CONSTRUCT: {
1316 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
1320 hres = create_string(dispex->ctx, str, SysStringLen(str), &ret);
1323 hres = create_string(dispex->ctx, NULL, 0, &ret);
1329 V_VT(retv) = VT_DISPATCH;
1330 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
1335 FIXME("unimplemented flags: %x\n", flags);
1342 static HRESULT string_alloc(script_ctx_t *ctx, BOOL use_constr, StringInstance **ret)
1344 StringInstance *string;
1347 string = heap_alloc_zero(sizeof(StringInstance));
1349 return E_OUTOFMEMORY;
1352 hres = init_dispex_from_constr(&string->dispex, ctx, &String_info, ctx->string_constr);
1354 hres = init_dispex(&string->dispex, ctx, &String_info, NULL);
1364 HRESULT create_string_constr(script_ctx_t *ctx, DispatchEx **ret)
1366 StringInstance *string;
1369 hres = string_alloc(ctx, FALSE, &string);
1373 hres = create_builtin_function(ctx, StringConstr_value, PROPF_CONSTR, &string->dispex, ret);
1375 jsdisp_release(&string->dispex);
1379 HRESULT create_string(script_ctx_t *ctx, const WCHAR *str, DWORD len, DispatchEx **ret)
1381 StringInstance *string;
1384 hres = string_alloc(ctx, TRUE, &string);
1391 string->length = len;
1392 string->str = heap_alloc((len+1)*sizeof(WCHAR));
1394 jsdisp_release(&string->dispex);
1395 return E_OUTOFMEMORY;
1398 memcpy(string->str, str, len*sizeof(WCHAR));
1399 string->str[len] = 0;
1401 *ret = &string->dispex;