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)
343 DWORD length, idx = 0;
348 if(dispex->builtin_info->class == JSCLASS_STRING) {
349 StringInstance *string = (StringInstance*)dispex;
352 length = string->length;
354 FIXME("not string this not supported\n");
358 if(arg_cnt(dp) > 0) {
361 hres = to_integer(dispex->ctx, get_arg(dp, 0), ei, &v);
365 if(V_VT(&v) != VT_I4 || V_I4(&v) < 0 || V_I4(&v) >= length) {
366 if(retv) num_set_nan(&v);
375 V_I4(retv) = str[idx];
380 /* ECMA-262 3rd Edition 15.5.4.6 */
381 static HRESULT String_concat(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
382 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
384 BSTR *strs = NULL, ret = NULL;
385 DWORD len = 0, i, l, str_cnt;
392 str_cnt = arg_cnt(dp)+1;
393 strs = heap_alloc_zero(str_cnt * sizeof(BSTR));
395 return E_OUTOFMEMORY;
397 V_VT(&var) = VT_DISPATCH;
398 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(dispex);
400 hres = to_string(dispex->ctx, &var, ei, strs);
401 if(SUCCEEDED(hres)) {
402 for(i=0; i < arg_cnt(dp); i++) {
403 hres = to_string(dispex->ctx, get_arg(dp, i), ei, strs+i+1);
409 if(SUCCEEDED(hres)) {
410 for(i=0; i < str_cnt; i++)
411 len += SysStringLen(strs[i]);
413 ptr = ret = SysAllocStringLen(NULL, len);
415 for(i=0; i < str_cnt; i++) {
416 l = SysStringLen(strs[i]);
417 memcpy(ptr, strs[i], l*sizeof(WCHAR));
422 for(i=0; i < str_cnt; i++)
423 SysFreeString(strs[i]);
430 V_VT(retv) = VT_BSTR;
438 static HRESULT String_fixed(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
439 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
441 static const WCHAR fixedtagW[] = {'T','T',0};
442 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, fixedtagW);
445 static HRESULT String_fontcolor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
446 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
448 static const WCHAR fontW[] = {'F','O','N','T',0};
449 static const WCHAR colorW[] = {'C','O','L','O','R',0};
451 return do_attribute_tag_format(dispex, lcid, flags, dp, retv, ei, sp, fontW, colorW);
454 static HRESULT String_fontsize(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
455 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
457 static const WCHAR fontW[] = {'F','O','N','T',0};
458 static const WCHAR colorW[] = {'S','I','Z','E',0};
460 return do_attribute_tag_format(dispex, lcid, flags, dp, retv, ei, sp, fontW, colorW);
463 static HRESULT String_indexOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
464 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
466 DWORD length, pos = 0;
474 if(is_class(dispex, JSCLASS_STRING)) {
475 StringInstance *string = (StringInstance*)dispex;
478 length = string->length;
480 FIXME("not String this\n");
492 hres = to_string(dispex->ctx, get_arg(dp,0), ei, &search_str);
496 if(arg_cnt(dp) >= 2) {
499 hres = to_integer(dispex->ctx, get_arg(dp,1), ei, &ival);
500 if(SUCCEEDED(hres)) {
501 if(V_VT(&ival) == VT_I4)
502 pos = V_VT(&ival) > 0 ? V_I4(&ival) : 0;
504 pos = V_R8(&ival) > 0.0 ? length : 0;
510 if(SUCCEEDED(hres)) {
513 ptr = strstrW(str+pos, search_str);
520 SysFreeString(search_str);
531 static HRESULT String_italics(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
532 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
534 static const WCHAR italicstagW[] = {'I',0};
535 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, italicstagW);
538 static HRESULT String_lastIndexOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
539 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
545 static HRESULT String_link(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
546 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
548 static const WCHAR fontW[] = {'A',0};
549 static const WCHAR colorW[] = {'H','R','E','F',0};
551 return do_attribute_tag_format(dispex, lcid, flags, dp, retv, ei, sp, fontW, colorW);
554 /* ECMA-262 3rd Edition 15.5.4.10 */
555 static HRESULT String_match(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
556 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
558 StringInstance *This = (StringInstance*)dispex;
559 match_result_t *match_result;
562 VARIANT var, *arg_var;
568 if(arg_cnt(dp) != 1) {
569 FIXME("unsupported args\n");
573 arg_var = get_arg(dp, 0);
574 switch(V_VT(arg_var)) {
576 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
578 if(regexp->builtin_info->class == JSCLASS_REGEXP)
580 jsdisp_release(regexp);
585 hres = to_string(dispex->ctx, arg_var, ei, &match_str);
589 hres = create_regexp_str(dispex->ctx, match_str, SysStringLen(match_str), NULL, 0, ®exp);
590 SysFreeString(match_str);
596 hres = regexp_match(regexp, This->str, This->length, FALSE, &match_result, &match_cnt);
597 jsdisp_release(regexp);
605 V_VT(retv) = VT_NULL;
609 hres = create_array(dispex->ctx, match_cnt, &array);
613 V_VT(&var) = VT_BSTR;
615 for(i=0; i < match_cnt; i++) {
616 V_BSTR(&var) = SysAllocStringLen(match_result[i].str, match_result[i].len);
618 hres = E_OUTOFMEMORY;
622 hres = jsdisp_propput_idx(array, i, lcid, &var, ei, NULL/*FIXME*/);
623 SysFreeString(V_BSTR(&var));
628 if(SUCCEEDED(hres) && retv) {
629 V_VT(retv) = VT_DISPATCH;
630 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(array);
632 jsdisp_release(array);
643 static HRESULT strbuf_append(strbuf_t *buf, const WCHAR *str, DWORD len)
648 if(len + buf->len > buf->size) {
652 new_size = buf->size ? buf->size<<1 : 16;
653 if(new_size < buf->len+len)
654 new_size = buf->len+len;
656 new_buf = heap_realloc(buf->buf, new_size*sizeof(WCHAR));
658 new_buf = heap_alloc(new_size*sizeof(WCHAR));
660 return E_OUTOFMEMORY;
663 buf->size = new_size;
666 memcpy(buf->buf+buf->len, str, len*sizeof(WCHAR));
671 static HRESULT rep_call(DispatchEx *func, const WCHAR *str, match_result_t *match, match_result_t *parens,
672 DWORD parens_cnt, LCID lcid, BSTR *ret, jsexcept_t *ei, IServiceProvider *caller)
674 DISPPARAMS dp = {NULL, NULL, 0, 0};
675 VARIANTARG *args, *arg;
680 dp.cArgs = parens_cnt+3;
681 dp.rgvarg = args = heap_alloc_zero(sizeof(VARIANT)*dp.cArgs);
683 return E_OUTOFMEMORY;
685 arg = get_arg(&dp,0);
687 V_BSTR(arg) = SysAllocStringLen(match->str, match->len);
689 hres = E_OUTOFMEMORY;
691 if(SUCCEEDED(hres)) {
692 for(i=0; i < parens_cnt; i++) {
693 arg = get_arg(&dp,i+1);
695 V_BSTR(arg) = SysAllocStringLen(parens[i].str, parens[i].len);
697 hres = E_OUTOFMEMORY;
703 if(SUCCEEDED(hres)) {
704 arg = get_arg(&dp,parens_cnt+1);
706 V_I4(arg) = match->str - str;
708 arg = get_arg(&dp,parens_cnt+2);
710 V_BSTR(arg) = SysAllocString(str);
712 hres = E_OUTOFMEMORY;
716 hres = jsdisp_call_value(func, lcid, DISPATCH_METHOD, &dp, &var, ei, caller);
718 for(i=0; i < parens_cnt+1; i++) {
719 if(i != parens_cnt+1)
720 SysFreeString(V_BSTR(get_arg(&dp,i)));
727 hres = to_string(func->ctx, &var, ei, ret);
732 /* ECMA-262 3rd Edition 15.5.4.11 */
733 static HRESULT String_replace(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
734 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
736 DWORD parens_cnt = 0, parens_size=0, rep_len=0, length;
737 BSTR rep_str = NULL, match_str = NULL, ret_str;
738 DispatchEx *rep_func = NULL, *regexp = NULL;
739 match_result_t *parens = NULL, match;
741 strbuf_t ret = {NULL,0,0};
748 if(is_class(dispex, JSCLASS_STRING)) {
749 StringInstance *string = (StringInstance*)dispex;
751 length = string->length;
753 FIXME("not String this\n");
759 ret_str = SysAllocString(str);
761 return E_OUTOFMEMORY;
763 V_VT(retv) = VT_BSTR;
764 V_BSTR(retv) = ret_str;
769 arg_var = get_arg(dp, 0);
770 switch(V_VT(arg_var)) {
772 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
774 if(is_class(regexp, JSCLASS_REGEXP)) {
777 jsdisp_release(regexp);
783 hres = to_string(dispex->ctx, arg_var, ei, &match_str);
788 if(arg_cnt(dp) >= 2) {
789 arg_var = get_arg(dp,1);
790 switch(V_VT(arg_var)) {
792 rep_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
794 if(is_class(rep_func, JSCLASS_FUNCTION)) {
797 jsdisp_release(rep_func);
803 hres = to_string(dispex->ctx, arg_var, ei, &rep_str);
807 if(strchrW(rep_str, '$')) {
808 FIXME("unsupported $ in replace string\n");
812 rep_len = SysStringLen(rep_str);
816 if(SUCCEEDED(hres)) {
817 const WCHAR *cp, *ecp;
823 hres = regexp_match_next(regexp, gcheck, str, length, &cp, rep_func ? &parens : NULL,
824 &parens_size, &parens_cnt, &match);
827 if(hres == S_FALSE) {
834 match.str = strstrW(cp, match_str);
837 match.len = SysStringLen(match_str);
838 cp = match.str+match.len;
841 hres = strbuf_append(&ret, ecp, match.str-ecp);
842 ecp = match.str+match.len;
849 hres = rep_call(rep_func, str, &match, parens, parens_cnt, lcid, &cstr, ei, caller);
853 hres = strbuf_append(&ret, cstr, SysStringLen(cstr));
858 hres = strbuf_append(&ret, rep_str, rep_len);
862 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d'};
864 hres = strbuf_append(&ret, undefinedW, sizeof(undefinedW)/sizeof(WCHAR));
871 hres = strbuf_append(&ret, ecp, (str+length)-ecp);
875 jsdisp_release(rep_func);
877 jsdisp_release(regexp);
878 SysFreeString(rep_str);
879 SysFreeString(match_str);
882 if(SUCCEEDED(hres) && retv) {
883 ret_str = SysAllocStringLen(ret.buf, ret.len);
885 return E_OUTOFMEMORY;
887 V_VT(retv) = VT_BSTR;
888 V_BSTR(retv) = ret_str;
889 TRACE("= %s\n", debugstr_w(ret_str));
896 static HRESULT String_search(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
897 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
903 /* ECMA-262 3rd Edition 15.5.4.13 */
904 static HRESULT String_slice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
905 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
915 if(is_class(dispex, JSCLASS_STRING)) {
916 StringInstance *string = (StringInstance*)dispex;
919 length = string->length;
921 FIXME("this is not a string class\n");
926 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &v);
930 if(V_VT(&v) == VT_I4) {
933 start = length + start;
936 }else if(start > length) {
940 start = V_R8(&v) < 0.0 ? 0 : length;
946 if(arg_cnt(dp) >= 2) {
947 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-2, ei, &v);
951 if(V_VT(&v) == VT_I4) {
957 }else if(end > length) {
961 end = V_R8(&v) < 0.0 ? 0 : length;
971 BSTR retstr = SysAllocStringLen(str+start, end-start);
973 return E_OUTOFMEMORY;
975 V_VT(retv) = VT_BSTR;
976 V_BSTR(retv) = retstr;
981 static HRESULT String_small(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
982 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
984 static const WCHAR smalltagW[] = {'S','M','A','L','L',0};
985 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, smalltagW);
988 static HRESULT String_split(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
989 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
991 match_result_t *match_result = NULL;
992 DWORD match_cnt, i, match_len = 0;
993 StringInstance *string;
994 const WCHAR *ptr, *ptr2;
997 BSTR match_str = NULL;
1002 if(!is_class(dispex, JSCLASS_STRING)) {
1003 FIXME("not String this\n");
1007 string = (StringInstance*)dispex;
1009 if(arg_cnt(dp) != 1) {
1010 FIXME("unsupported args\n");
1014 arg = get_arg(dp, 0);
1019 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
1021 if(is_class(regexp, JSCLASS_REGEXP)) {
1022 hres = regexp_match(regexp, string->str, string->length, TRUE, &match_result, &match_cnt);
1023 jsdisp_release(regexp);
1028 jsdisp_release(regexp);
1032 hres = to_string(dispex->ctx, arg, ei, &match_str);
1036 match_len = SysStringLen(match_str);
1038 SysFreeString(match_str);
1043 hres = create_array(dispex->ctx, 0, &array);
1045 if(SUCCEEDED(hres)) {
1051 ptr2 = match_result[i].str;
1052 }else if(match_str) {
1053 ptr2 = strstrW(ptr, match_str);
1062 V_VT(&var) = VT_BSTR;
1063 V_BSTR(&var) = SysAllocStringLen(ptr, ptr2-ptr);
1065 hres = E_OUTOFMEMORY;
1069 hres = jsdisp_propput_idx(array, i, lcid, &var, ei, sp);
1070 SysFreeString(V_BSTR(&var));
1075 ptr = match_result[i].str + match_result[i].len;
1077 ptr = ptr2 + match_len;
1083 if(SUCCEEDED(hres) && (match_str || match_result)) {
1084 DWORD len = (string->str+string->length) - ptr;
1086 if(len || match_str) {
1087 V_VT(&var) = VT_BSTR;
1088 V_BSTR(&var) = SysAllocStringLen(ptr, len);
1091 hres = jsdisp_propput_idx(array, i, lcid, &var, ei, sp);
1092 SysFreeString(V_BSTR(&var));
1094 hres = E_OUTOFMEMORY;
1099 SysFreeString(match_str);
1100 heap_free(match_result);
1102 if(SUCCEEDED(hres) && retv) {
1103 V_VT(retv) = VT_DISPATCH;
1104 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(array);
1106 jsdisp_release(array);
1112 static HRESULT String_strike(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1113 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1115 static const WCHAR striketagW[] = {'S','T','R','I','K','E',0};
1116 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, striketagW);
1119 static HRESULT String_sub(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1120 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1122 static const WCHAR subtagW[] = {'S','U','B',0};
1123 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, subtagW);
1126 /* ECMA-262 3rd Edition 15.5.4.15 */
1127 static HRESULT String_substring(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1128 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1138 if(is_class(dispex, JSCLASS_STRING)) {
1139 StringInstance *string = (StringInstance*)dispex;
1141 length = string->length;
1144 FIXME("not string this not supported\n");
1148 if(arg_cnt(dp) >= 1) {
1149 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &v);
1153 if(V_VT(&v) == VT_I4) {
1157 else if(start >= length)
1160 start = V_R8(&v) < 0.0 ? 0 : length;
1164 if(arg_cnt(dp) >= 2) {
1165 hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-2, ei, &v);
1169 if(V_VT(&v) == VT_I4) {
1173 else if(end > length)
1176 end = V_R8(&v) < 0.0 ? 0 : length;
1189 V_VT(retv) = VT_BSTR;
1190 V_BSTR(retv) = SysAllocStringLen(str+start, end-start);
1192 return E_OUTOFMEMORY;
1197 static HRESULT String_substr(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1198 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1204 static HRESULT String_sup(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1205 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1207 static const WCHAR suptagW[] = {'S','U','P',0};
1208 return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, suptagW);
1211 static HRESULT String_toLowerCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1212 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1214 StringInstance *string;
1221 if(is_class(dispex, JSCLASS_STRING)) {
1222 string = (StringInstance*)dispex;
1224 length = string->length;
1227 FIXME("not string this not supported\n");
1232 bstr = SysAllocStringLen(str, length);
1234 return E_OUTOFMEMORY;
1238 V_VT(retv) = VT_BSTR;
1239 V_BSTR(retv) = bstr;
1244 static HRESULT String_toUpperCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1245 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1247 StringInstance *string;
1254 if(is_class(dispex, JSCLASS_STRING)) {
1255 string = (StringInstance*)dispex;
1257 length = string->length;
1260 FIXME("not string this not supported\n");
1265 bstr = SysAllocStringLen(str, length);
1267 return E_OUTOFMEMORY;
1271 V_VT(retv) = VT_BSTR;
1272 V_BSTR(retv) = bstr;
1277 static HRESULT String_toLocaleLowerCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1278 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1284 static HRESULT String_toLocaleUpperCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1285 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1291 static HRESULT String_localeCompare(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1292 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1298 static HRESULT String_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1299 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1305 static HRESULT String_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1306 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1312 static HRESULT String_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1313 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1319 static HRESULT String_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1320 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1322 StringInstance *This = (StringInstance*)dispex;
1327 case DISPATCH_PROPERTYGET: {
1328 BSTR str = SysAllocString(This->str);
1330 return E_OUTOFMEMORY;
1332 V_VT(retv) = VT_BSTR;
1337 FIXME("flags %x\n", flags);
1344 static void String_destructor(DispatchEx *dispex)
1346 StringInstance *This = (StringInstance*)dispex;
1348 heap_free(This->str);
1352 static const builtin_prop_t String_props[] = {
1353 {anchorW, String_anchor, PROPF_METHOD},
1354 {bigW, String_big, PROPF_METHOD},
1355 {blinkW, String_blink, PROPF_METHOD},
1356 {boldW, String_bold, PROPF_METHOD},
1357 {charAtW, String_charAt, PROPF_METHOD},
1358 {charCodeAtW, String_charCodeAt, PROPF_METHOD},
1359 {concatW, String_concat, PROPF_METHOD},
1360 {fixedW, String_fixed, PROPF_METHOD},
1361 {fontcolorW, String_fontcolor, PROPF_METHOD},
1362 {fontsizeW, String_fontsize, PROPF_METHOD},
1363 {hasOwnPropertyW, String_hasOwnProperty, PROPF_METHOD},
1364 {indexOfW, String_indexOf, PROPF_METHOD},
1365 {isPrototypeOfW, String_isPrototypeOf, PROPF_METHOD},
1366 {italicsW, String_italics, PROPF_METHOD},
1367 {lastIndexOfW, String_lastIndexOf, PROPF_METHOD},
1368 {lengthW, String_length, 0},
1369 {linkW, String_link, PROPF_METHOD},
1370 {localeCompareW, String_localeCompare, PROPF_METHOD},
1371 {matchW, String_match, PROPF_METHOD},
1372 {propertyIsEnumerableW, String_propertyIsEnumerable, PROPF_METHOD},
1373 {replaceW, String_replace, PROPF_METHOD},
1374 {searchW, String_search, PROPF_METHOD},
1375 {sliceW, String_slice, PROPF_METHOD},
1376 {smallW, String_small, PROPF_METHOD},
1377 {splitW, String_split, PROPF_METHOD},
1378 {strikeW, String_strike, PROPF_METHOD},
1379 {subW, String_sub, PROPF_METHOD},
1380 {substrW, String_substr, PROPF_METHOD},
1381 {substringW, String_substring, PROPF_METHOD},
1382 {supW, String_sup, PROPF_METHOD},
1383 {toLocaleLowerCaseW, String_toLocaleLowerCase, PROPF_METHOD},
1384 {toLocaleUpperCaseW, String_toLocaleUpperCase, PROPF_METHOD},
1385 {toLowerCaseW, String_toLowerCase, PROPF_METHOD},
1386 {toStringW, String_toString, PROPF_METHOD},
1387 {toUpperCaseW, String_toUpperCase, PROPF_METHOD},
1388 {valueOfW, String_valueOf, PROPF_METHOD}
1391 static const builtin_info_t String_info = {
1393 {NULL, String_value, 0},
1394 sizeof(String_props)/sizeof(*String_props),
1400 static HRESULT StringConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1401 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1412 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
1416 str = SysAllocStringLen(NULL, 0);
1418 return E_OUTOFMEMORY;
1421 V_VT(retv) = VT_BSTR;
1425 case DISPATCH_CONSTRUCT: {
1431 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
1435 hres = create_string(dispex->ctx, str, SysStringLen(str), &ret);
1438 hres = create_string(dispex->ctx, NULL, 0, &ret);
1444 V_VT(retv) = VT_DISPATCH;
1445 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
1450 FIXME("unimplemented flags: %x\n", flags);
1457 static HRESULT string_alloc(script_ctx_t *ctx, BOOL use_constr, StringInstance **ret)
1459 StringInstance *string;
1462 string = heap_alloc_zero(sizeof(StringInstance));
1464 return E_OUTOFMEMORY;
1467 hres = init_dispex_from_constr(&string->dispex, ctx, &String_info, ctx->string_constr);
1469 hres = init_dispex(&string->dispex, ctx, &String_info, NULL);
1479 HRESULT create_string_constr(script_ctx_t *ctx, DispatchEx **ret)
1481 StringInstance *string;
1484 hres = string_alloc(ctx, FALSE, &string);
1488 hres = create_builtin_function(ctx, StringConstr_value, NULL, PROPF_CONSTR, &string->dispex, ret);
1490 jsdisp_release(&string->dispex);
1494 HRESULT create_string(script_ctx_t *ctx, const WCHAR *str, DWORD len, DispatchEx **ret)
1496 StringInstance *string;
1499 hres = string_alloc(ctx, TRUE, &string);
1506 string->length = len;
1507 string->str = heap_alloc((len+1)*sizeof(WCHAR));
1509 jsdisp_release(&string->dispex);
1510 return E_OUTOFMEMORY;
1513 memcpy(string->str, str, len*sizeof(WCHAR));
1514 string->str[len] = 0;
1516 *ret = &string->dispex;