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
20 #include "wine/port.h"
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
35 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
36 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
37 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
38 static const WCHAR anchorW[] = {'a','n','c','h','o','r',0};
39 static const WCHAR bigW[] = {'b','i','g',0};
40 static const WCHAR blinkW[] = {'b','l','i','n','k',0};
41 static const WCHAR boldW[] = {'b','o','l','d',0};
42 static const WCHAR charAtW[] = {'c','h','a','r','A','t',0};
43 static const WCHAR charCodeAtW[] = {'c','h','a','r','C','o','d','e','A','t',0};
44 static const WCHAR concatW[] = {'c','o','n','c','a','t',0};
45 static const WCHAR fixedW[] = {'f','i','x','e','d',0};
46 static const WCHAR fontcolorW[] = {'f','o','n','t','c','o','l','o','r',0};
47 static const WCHAR fontsizeW[] = {'f','o','n','t','s','i','z','e',0};
48 static const WCHAR indexOfW[] = {'i','n','d','e','x','O','f',0};
49 static const WCHAR italicsW[] = {'i','t','a','l','i','c','s',0};
50 static const WCHAR lastIndexOfW[] = {'l','a','s','t','I','n','d','e','x','O','f',0};
51 static const WCHAR linkW[] = {'l','i','n','k',0};
52 static const WCHAR matchW[] = {'m','a','t','c','h',0};
53 static const WCHAR replaceW[] = {'r','e','p','l','a','c','e',0};
54 static const WCHAR searchW[] = {'s','e','a','r','c','h',0};
55 static const WCHAR sliceW[] = {'s','l','i','c','e',0};
56 static const WCHAR smallW[] = {'s','m','a','l','l',0};
57 static const WCHAR splitW[] = {'s','p','l','i','t',0};
58 static const WCHAR strikeW[] = {'s','t','r','i','k','e',0};
59 static const WCHAR subW[] = {'s','u','b',0};
60 static const WCHAR substringW[] = {'s','u','b','s','t','r','i','n','g',0};
61 static const WCHAR substrW[] = {'s','u','b','s','t','r',0};
62 static const WCHAR supW[] = {'s','u','p',0};
63 static const WCHAR toLowerCaseW[] = {'t','o','L','o','w','e','r','C','a','s','e',0};
64 static const WCHAR toUpperCaseW[] = {'t','o','U','p','p','e','r','C','a','s','e',0};
65 static const WCHAR toLocaleLowerCaseW[] = {'t','o','L','o','c','a','l','e','L','o','w','e','r','C','a','s','e',0};
66 static const WCHAR toLocaleUpperCaseW[] = {'t','o','L','o','c','a','l','e','U','p','p','e','r','C','a','s','e',0};
67 static const WCHAR localeCompareW[] = {'l','o','c','a','l','e','C','o','m','p','a','r','e',0};
68 static const WCHAR fromCharCodeW[] = {'f','r','o','m','C','h','a','r','C','o','d','e',0};
70 static inline StringInstance *string_from_vdisp(vdisp_t *vdisp)
72 return (StringInstance*)vdisp->u.jsdisp;
75 static inline StringInstance *string_this(vdisp_t *jsthis)
77 return is_vclass(jsthis, JSCLASS_STRING) ? string_from_vdisp(jsthis) : NULL;
80 static HRESULT get_string_val(script_ctx_t *ctx, vdisp_t *jsthis, jsexcept_t *ei,
81 const WCHAR **str, DWORD *len, BSTR *val_str)
83 StringInstance *string;
87 if((string = string_this(jsthis))) {
89 *len = string->length;
94 V_VT(&this_var) = VT_DISPATCH;
95 V_DISPATCH(&this_var) = jsthis->u.disp;
96 hres = to_string(ctx, &this_var, ei, val_str);
101 *len = SysStringLen(*val_str);
105 static HRESULT String_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
106 VARIANT *retv, jsexcept_t *ei)
108 TRACE("%p\n", jsthis);
111 case DISPATCH_PROPERTYGET: {
112 StringInstance *string = string_from_vdisp(jsthis);
114 num_set_int(retv, string->length);
118 FIXME("unimplemented flags %x\n", flags);
125 static HRESULT stringobj_to_string(vdisp_t *jsthis, VARIANT *retv)
127 StringInstance *string;
129 if(!(string = string_this(jsthis))) {
130 WARN("this is not a string object\n");
135 BSTR str = SysAllocString(string->str);
137 return E_OUTOFMEMORY;
139 V_VT(retv) = VT_BSTR;
145 /* ECMA-262 3rd Edition 15.5.4.2 */
146 static HRESULT String_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
147 VARIANT *retv, jsexcept_t *ei)
151 return stringobj_to_string(jsthis, retv);
154 /* ECMA-262 3rd Edition 15.5.4.2 */
155 static HRESULT String_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
156 VARIANT *retv, jsexcept_t *ei)
160 return stringobj_to_string(jsthis, retv);
163 static HRESULT do_attributeless_tag_format(script_ctx_t *ctx, vdisp_t *jsthis, VARIANT *retv,
164 jsexcept_t *ei, const WCHAR *tagname)
171 static const WCHAR tagfmt[] = {'<','%','s','>','%','s','<','/','%','s','>',0};
173 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
178 BSTR ret = SysAllocStringLen(NULL, length + 2*strlenW(tagname) + 5);
180 SysFreeString(val_str);
181 return E_OUTOFMEMORY;
184 sprintfW(ret, tagfmt, tagname, str, tagname);
186 V_VT(retv) = VT_BSTR;
190 SysFreeString(val_str);
194 static HRESULT do_attribute_tag_format(script_ctx_t *ctx, vdisp_t *jsthis, DISPPARAMS *dp, VARIANT *retv,
195 jsexcept_t *ei, const WCHAR *tagname, const WCHAR *attr)
197 static const WCHAR tagfmtW[]
198 = {'<','%','s',' ','%','s','=','\"','%','s','\"','>','%','s','<','/','%','s','>',0};
199 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
201 StringInstance *string;
204 BSTR attr_value, val_str = NULL;
207 if(!(string = string_this(jsthis))) {
210 V_VT(&this) = VT_DISPATCH;
211 V_DISPATCH(&this) = jsthis->u.disp;
213 hres = to_string(ctx, &this, ei, &val_str);
218 length = SysStringLen(val_str);
222 length = string->length;
226 hres = to_string(ctx, get_arg(dp, 0), ei, &attr_value);
228 SysFreeString(val_str);
233 attr_value = SysAllocString(undefinedW);
235 SysFreeString(val_str);
236 return E_OUTOFMEMORY;
241 BSTR ret = SysAllocStringLen(NULL, length + 2*strlenW(tagname)
242 + strlenW(attr) + SysStringLen(attr_value) + 9);
244 SysFreeString(attr_value);
245 SysFreeString(val_str);
246 return E_OUTOFMEMORY;
249 sprintfW(ret, tagfmtW, tagname, attr, attr_value, str, tagname);
251 V_VT(retv) = VT_BSTR;
255 SysFreeString(attr_value);
256 SysFreeString(val_str);
260 static HRESULT String_anchor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
261 VARIANT *retv, jsexcept_t *ei)
263 static const WCHAR fontW[] = {'A',0};
264 static const WCHAR colorW[] = {'N','A','M','E',0};
266 return do_attribute_tag_format(ctx, jsthis, dp, retv, ei, fontW, colorW);
269 static HRESULT String_big(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
270 VARIANT *retv, jsexcept_t *ei)
272 static const WCHAR bigtagW[] = {'B','I','G',0};
273 return do_attributeless_tag_format(ctx, jsthis, retv, ei, bigtagW);
276 static HRESULT String_blink(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
277 VARIANT *retv, jsexcept_t *ei)
279 static const WCHAR blinktagW[] = {'B','L','I','N','K',0};
280 return do_attributeless_tag_format(ctx, jsthis, retv, ei, blinktagW);
283 static HRESULT String_bold(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
284 VARIANT *retv, jsexcept_t *ei)
286 static const WCHAR boldtagW[] = {'B',0};
287 return do_attributeless_tag_format(ctx, jsthis, retv, ei, boldtagW);
290 /* ECMA-262 3rd Edition 15.5.4.5 */
291 static HRESULT String_charAt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
292 VARIANT *retv, jsexcept_t *ei)
302 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
309 hres = to_integer(ctx, get_arg(dp, 0), ei, &d);
311 SysFreeString(val_str);
314 pos = is_int32(d) ? d : -1;
318 SysFreeString(val_str);
322 if(0 <= pos && pos < length)
323 ret = SysAllocStringLen(str+pos, 1);
325 ret = SysAllocStringLen(NULL, 0);
326 SysFreeString(val_str);
328 return E_OUTOFMEMORY;
331 V_VT(retv) = VT_BSTR;
336 /* ECMA-262 3rd Edition 15.5.4.5 */
337 static HRESULT String_charCodeAt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
338 VARIANT *retv, jsexcept_t *ei)
342 DWORD length, idx = 0;
347 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
351 if(arg_cnt(dp) > 0) {
354 hres = to_integer(ctx, get_arg(dp, 0), ei, &d);
356 SysFreeString(val_str);
360 if(!is_int32(d) || d < 0 || d >= length) {
361 SysFreeString(val_str);
363 num_set_val(retv, NAN);
371 num_set_int(retv, str[idx]);
373 SysFreeString(val_str);
377 /* ECMA-262 3rd Edition 15.5.4.6 */
378 static HRESULT String_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
379 VARIANT *retv, jsexcept_t *ei)
381 BSTR *strs = NULL, ret = NULL;
382 DWORD len = 0, i, l, str_cnt;
389 str_cnt = arg_cnt(dp)+1;
390 strs = heap_alloc_zero(str_cnt * sizeof(BSTR));
392 return E_OUTOFMEMORY;
394 V_VT(&var) = VT_DISPATCH;
395 V_DISPATCH(&var) = jsthis->u.disp;
397 hres = to_string(ctx, &var, ei, strs);
398 if(SUCCEEDED(hres)) {
399 for(i=0; i < arg_cnt(dp); i++) {
400 hres = to_string(ctx, get_arg(dp, i), ei, strs+i+1);
406 if(SUCCEEDED(hres)) {
407 for(i=0; i < str_cnt; i++)
408 len += SysStringLen(strs[i]);
410 ptr = ret = SysAllocStringLen(NULL, len);
412 for(i=0; i < str_cnt; i++) {
413 l = SysStringLen(strs[i]);
414 memcpy(ptr, strs[i], l*sizeof(WCHAR));
419 for(i=0; i < str_cnt; i++)
420 SysFreeString(strs[i]);
427 V_VT(retv) = VT_BSTR;
435 static HRESULT String_fixed(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
436 VARIANT *retv, jsexcept_t *ei)
438 static const WCHAR fixedtagW[] = {'T','T',0};
439 return do_attributeless_tag_format(ctx, jsthis, retv, ei, fixedtagW);
442 static HRESULT String_fontcolor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
443 VARIANT *retv, jsexcept_t *ei)
445 static const WCHAR fontW[] = {'F','O','N','T',0};
446 static const WCHAR colorW[] = {'C','O','L','O','R',0};
448 return do_attribute_tag_format(ctx, jsthis, dp, retv, ei, fontW, colorW);
451 static HRESULT String_fontsize(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
452 VARIANT *retv, jsexcept_t *ei)
454 static const WCHAR fontW[] = {'F','O','N','T',0};
455 static const WCHAR colorW[] = {'S','I','Z','E',0};
457 return do_attribute_tag_format(ctx, jsthis, dp, retv, ei, fontW, colorW);
460 static HRESULT String_indexOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
461 VARIANT *retv, jsexcept_t *ei)
463 DWORD length, pos = 0;
465 BSTR search_str, val_str;
471 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
477 num_set_int(retv, -1);
478 SysFreeString(val_str);
482 hres = to_string(ctx, get_arg(dp,0), ei, &search_str);
484 SysFreeString(val_str);
488 if(arg_cnt(dp) >= 2) {
491 hres = to_integer(ctx, get_arg(dp,1), ei, &d);
492 if(SUCCEEDED(hres) && d > 0.0)
493 pos = is_int32(d) ? min(length, d) : length;
496 if(SUCCEEDED(hres)) {
499 ptr = strstrW(str+pos, search_str);
506 SysFreeString(search_str);
507 SysFreeString(val_str);
512 num_set_int(retv, ret);
516 static HRESULT String_italics(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
517 VARIANT *retv, jsexcept_t *ei)
519 static const WCHAR italicstagW[] = {'I',0};
520 return do_attributeless_tag_format(ctx, jsthis, retv, ei, italicstagW);
523 /* ECMA-262 3rd Edition 15.5.4.8 */
524 static HRESULT String_lastIndexOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
525 VARIANT *retv, jsexcept_t *ei)
527 BSTR search_str, val_str;
528 DWORD length, pos = 0, search_len;
535 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
541 num_set_int(retv, -1);
542 SysFreeString(val_str);
546 hres = to_string(ctx, get_arg(dp,0), ei, &search_str);
548 SysFreeString(val_str);
552 search_len = SysStringLen(search_str);
554 if(arg_cnt(dp) >= 2) {
557 hres = to_integer(ctx, get_arg(dp,1), ei, &d);
558 if(SUCCEEDED(hres) && d > 0)
559 pos = is_int32(d) ? min(length, d) : length;
564 if(SUCCEEDED(hres) && length >= search_len) {
567 for(ptr = str+min(pos, length-search_len); ptr >= str; ptr--) {
568 if(!memcmp(ptr, search_str, search_len*sizeof(WCHAR))) {
575 SysFreeString(search_str);
576 SysFreeString(val_str);
581 num_set_int(retv, ret);
585 static HRESULT String_link(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
586 VARIANT *retv, jsexcept_t *ei)
588 static const WCHAR fontW[] = {'A',0};
589 static const WCHAR colorW[] = {'H','R','E','F',0};
591 return do_attribute_tag_format(ctx, jsthis, dp, retv, ei, fontW, colorW);
594 /* ECMA-262 3rd Edition 15.5.4.10 */
595 static HRESULT String_match(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
596 VARIANT *retv, jsexcept_t *ei)
609 V_VT(retv) = VT_NULL;
615 arg_var = get_arg(dp, 0);
616 switch(V_VT(arg_var)) {
618 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
620 if(is_class(regexp, JSCLASS_REGEXP))
622 jsdisp_release(regexp);
627 hres = to_string(ctx, arg_var, ei, &match_str);
631 hres = create_regexp(ctx, match_str, SysStringLen(match_str), 0, ®exp);
632 SysFreeString(match_str);
638 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
639 if(SUCCEEDED(hres)) {
641 val_str = SysAllocStringLen(str, length);
643 hres = regexp_string_match(ctx, regexp, val_str, retv, ei);
645 hres = E_OUTOFMEMORY;
648 jsdisp_release(regexp);
649 SysFreeString(val_str);
659 static HRESULT strbuf_append(strbuf_t *buf, const WCHAR *str, DWORD len)
664 if(len + buf->len > buf->size) {
668 new_size = buf->size ? buf->size<<1 : 16;
669 if(new_size < buf->len+len)
670 new_size = buf->len+len;
672 new_buf = heap_realloc(buf->buf, new_size*sizeof(WCHAR));
674 new_buf = heap_alloc(new_size*sizeof(WCHAR));
676 return E_OUTOFMEMORY;
679 buf->size = new_size;
682 memcpy(buf->buf+buf->len, str, len*sizeof(WCHAR));
687 static HRESULT rep_call(script_ctx_t *ctx, jsdisp_t *func, const WCHAR *str, match_result_t *match,
688 match_result_t *parens, DWORD parens_cnt, BSTR *ret, jsexcept_t *ei)
690 DISPPARAMS dp = {NULL, NULL, 0, 0};
691 VARIANTARG *args, *arg;
696 dp.cArgs = parens_cnt+3;
697 dp.rgvarg = args = heap_alloc_zero(sizeof(VARIANT)*dp.cArgs);
699 return E_OUTOFMEMORY;
701 arg = get_arg(&dp,0);
703 V_BSTR(arg) = SysAllocStringLen(match->str, match->len);
705 hres = E_OUTOFMEMORY;
707 if(SUCCEEDED(hres)) {
708 for(i=0; i < parens_cnt; i++) {
709 arg = get_arg(&dp,i+1);
711 V_BSTR(arg) = SysAllocStringLen(parens[i].str, parens[i].len);
713 hres = E_OUTOFMEMORY;
719 if(SUCCEEDED(hres)) {
720 arg = get_arg(&dp,parens_cnt+1);
721 num_set_int(arg, match->str - str);
723 arg = get_arg(&dp,parens_cnt+2);
725 V_BSTR(arg) = SysAllocString(str);
727 hres = E_OUTOFMEMORY;
731 hres = jsdisp_call_value(func, NULL, DISPATCH_METHOD, &dp, &var, ei);
733 for(i=0; i < parens_cnt+3; i++) {
734 if(i != parens_cnt+1)
735 SysFreeString(V_BSTR(get_arg(&dp,i)));
742 hres = to_string(ctx, &var, ei, ret);
747 /* ECMA-262 3rd Edition 15.5.4.11 */
748 static HRESULT String_replace(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
749 VARIANT *retv, jsexcept_t *ei)
752 DWORD parens_cnt = 0, parens_size=0, rep_len=0, length;
753 BSTR rep_str = NULL, match_str = NULL, ret_str, val_str;
754 jsdisp_t *rep_func = NULL, *regexp = NULL;
755 match_result_t *parens = NULL, match = {NULL,0}, **parens_ptr = &parens;
756 strbuf_t ret = {NULL,0,0};
757 DWORD re_flags = REM_NO_CTX_UPDATE;
763 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
770 val_str = SysAllocStringLen(str, length);
772 return E_OUTOFMEMORY;
775 V_VT(retv) = VT_BSTR;
776 V_BSTR(retv) = val_str;
781 arg_var = get_arg(dp, 0);
782 switch(V_VT(arg_var)) {
784 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
786 if(is_class(regexp, JSCLASS_REGEXP)) {
789 jsdisp_release(regexp);
795 hres = to_string(ctx, arg_var, ei, &match_str);
797 SysFreeString(val_str);
802 if(arg_cnt(dp) >= 2) {
803 arg_var = get_arg(dp,1);
804 switch(V_VT(arg_var)) {
806 rep_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
808 if(is_class(rep_func, JSCLASS_FUNCTION)) {
811 jsdisp_release(rep_func);
817 hres = to_string(ctx, arg_var, ei, &rep_str);
821 rep_len = SysStringLen(rep_str);
822 if(!strchrW(rep_str, '$'))
827 if(SUCCEEDED(hres)) {
828 const WCHAR *cp, *ecp;
834 hres = regexp_match_next(ctx, regexp, re_flags, str, length, &cp, parens_ptr,
835 &parens_size, &parens_cnt, &match);
836 re_flags |= REM_CHECK_GLOBAL;
838 if(hres == S_FALSE) {
848 match.str = strstrW(cp, match_str);
851 match.len = SysStringLen(match_str);
852 cp = match.str+match.len;
855 hres = strbuf_append(&ret, ecp, match.str-ecp);
856 ecp = match.str+match.len;
863 hres = rep_call(ctx, rep_func, str, &match, parens, parens_cnt, &cstr, ei);
867 hres = strbuf_append(&ret, cstr, SysStringLen(cstr));
871 }else if(rep_str && regexp) {
872 const WCHAR *ptr = rep_str, *ptr2;
874 while((ptr2 = strchrW(ptr, '$'))) {
875 hres = strbuf_append(&ret, ptr, ptr2-ptr);
881 hres = strbuf_append(&ret, ptr2, 1);
885 hres = strbuf_append(&ret, match.str, match.len);
889 hres = strbuf_append(&ret, str, match.str-str);
893 hres = strbuf_append(&ret, ecp, (str+length)-ecp);
899 if(!isdigitW(ptr2[1])) {
900 hres = strbuf_append(&ret, ptr2, 1);
906 if(isdigitW(ptr2[2]) && idx*10 + (ptr2[2]-'0') <= parens_cnt) {
907 idx = idx*10 + (ptr[2]-'0');
909 }else if(idx && idx <= parens_cnt) {
912 hres = strbuf_append(&ret, ptr2, 1);
917 hres = strbuf_append(&ret, parens[idx-1].str, parens[idx-1].len);
926 hres = strbuf_append(&ret, ptr, (rep_str+rep_len)-ptr);
930 hres = strbuf_append(&ret, rep_str, rep_len);
934 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d'};
936 hres = strbuf_append(&ret, undefinedW, sizeof(undefinedW)/sizeof(WCHAR));
946 hres = strbuf_append(&ret, ecp, (str+length)-ecp);
950 jsdisp_release(rep_func);
951 SysFreeString(rep_str);
952 SysFreeString(match_str);
955 if(SUCCEEDED(hres) && match.str && regexp) {
957 val_str = SysAllocStringLen(str, length);
959 SysFreeString(ctx->last_match);
960 ctx->last_match = val_str;
962 ctx->last_match_index = match.str-str;
963 ctx->last_match_length = match.len;
965 hres = E_OUTOFMEMORY;
970 jsdisp_release(regexp);
971 SysFreeString(val_str);
973 if(SUCCEEDED(hres) && retv) {
974 ret_str = SysAllocStringLen(ret.buf, ret.len);
976 return E_OUTOFMEMORY;
978 V_VT(retv) = VT_BSTR;
979 V_BSTR(retv) = ret_str;
980 TRACE("= %s\n", debugstr_w(ret_str));
987 static HRESULT String_search(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
988 VARIANT *retv, jsexcept_t *ei)
990 jsdisp_t *regexp = NULL;
991 const WCHAR *str, *cp;
992 match_result_t match;
1000 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
1006 V_VT(retv) = VT_NULL;
1007 SysFreeString(val_str);
1011 arg = get_arg(dp,0);
1012 if(V_VT(arg) == VT_DISPATCH) {
1013 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
1015 if(!is_class(regexp, JSCLASS_REGEXP)) {
1016 jsdisp_release(regexp);
1023 hres = create_regexp_var(ctx, arg, NULL, ®exp);
1025 SysFreeString(val_str);
1031 hres = regexp_match_next(ctx, regexp, REM_RESET_INDEX, str, length, &cp, NULL, NULL, NULL, &match);
1032 SysFreeString(val_str);
1033 jsdisp_release(regexp);
1038 num_set_int(retv, hres == S_OK ? match.str-str : -1);
1042 /* ECMA-262 3rd Edition 15.5.4.13 */
1043 static HRESULT String_slice(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1044 VARIANT *retv, jsexcept_t *ei)
1055 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
1060 hres = to_integer(ctx, get_arg(dp,0), ei, &d);
1062 SysFreeString(val_str);
1069 start = length + start;
1072 }else if(start > length) {
1080 if(arg_cnt(dp) >= 2) {
1081 hres = to_integer(ctx, get_arg(dp,1), ei, &d);
1083 SysFreeString(val_str);
1093 }else if(end > length) {
1097 end = d < 0.0 ? 0 : length;
1107 BSTR retstr = SysAllocStringLen(str+start, end-start);
1109 SysFreeString(val_str);
1110 return E_OUTOFMEMORY;
1113 V_VT(retv) = VT_BSTR;
1114 V_BSTR(retv) = retstr;
1117 SysFreeString(val_str);
1121 static HRESULT String_small(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1122 VARIANT *retv, jsexcept_t *ei)
1124 static const WCHAR smalltagW[] = {'S','M','A','L','L',0};
1125 return do_attributeless_tag_format(ctx, jsthis, retv, ei, smalltagW);
1128 static HRESULT String_split(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1129 VARIANT *retv, jsexcept_t *ei)
1131 match_result_t *match_result = NULL;
1132 DWORD length, match_cnt, i, match_len = 0;
1133 const WCHAR *str, *ptr, *ptr2;
1134 BOOL use_regexp = FALSE;
1137 BSTR val_str, match_str = NULL;
1142 if(arg_cnt(dp) != 1) {
1143 FIXME("unsupported args\n");
1147 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
1151 arg = get_arg(dp, 0);
1156 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
1158 if(is_class(regexp, JSCLASS_REGEXP)) {
1160 hres = regexp_match(ctx, regexp, str, length, TRUE, &match_result, &match_cnt);
1161 jsdisp_release(regexp);
1163 SysFreeString(val_str);
1168 jsdisp_release(regexp);
1172 hres = to_string(ctx, arg, ei, &match_str);
1174 SysFreeString(val_str);
1178 match_len = SysStringLen(match_str);
1180 SysFreeString(match_str);
1185 hres = create_array(ctx, 0, &array);
1187 if(SUCCEEDED(hres)) {
1193 ptr2 = match_result[i].str;
1194 }else if(match_str) {
1195 ptr2 = strstrW(ptr, match_str);
1204 V_VT(&var) = VT_BSTR;
1205 V_BSTR(&var) = SysAllocStringLen(ptr, ptr2-ptr);
1207 hres = E_OUTOFMEMORY;
1211 hres = jsdisp_propput_idx(array, i, &var, ei);
1212 SysFreeString(V_BSTR(&var));
1217 ptr = match_result[i].str + match_result[i].len;
1219 ptr = ptr2 + match_len;
1225 if(SUCCEEDED(hres) && (match_str || use_regexp)) {
1226 DWORD len = (str+length) - ptr;
1228 if(len || match_str) {
1229 V_VT(&var) = VT_BSTR;
1230 V_BSTR(&var) = SysAllocStringLen(ptr, len);
1233 hres = jsdisp_propput_idx(array, i, &var, ei);
1234 SysFreeString(V_BSTR(&var));
1236 hres = E_OUTOFMEMORY;
1241 SysFreeString(match_str);
1242 SysFreeString(val_str);
1243 heap_free(match_result);
1245 if(SUCCEEDED(hres) && retv)
1246 var_set_jsdisp(retv, array);
1248 jsdisp_release(array);
1253 static HRESULT String_strike(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1254 VARIANT *retv, jsexcept_t *ei)
1256 static const WCHAR striketagW[] = {'S','T','R','I','K','E',0};
1257 return do_attributeless_tag_format(ctx, jsthis, retv, ei, striketagW);
1260 static HRESULT String_sub(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1261 VARIANT *retv, jsexcept_t *ei)
1263 static const WCHAR subtagW[] = {'S','U','B',0};
1264 return do_attributeless_tag_format(ctx, jsthis, retv, ei, subtagW);
1267 /* ECMA-262 3rd Edition 15.5.4.15 */
1268 static HRESULT String_substring(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1269 VARIANT *retv, jsexcept_t *ei)
1280 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
1284 if(arg_cnt(dp) >= 1) {
1285 hres = to_integer(ctx, get_arg(dp,0), ei, &d);
1287 SysFreeString(val_str);
1292 start = is_int32(d) ? min(length, d) : length;
1295 if(arg_cnt(dp) >= 2) {
1296 hres = to_integer(ctx, get_arg(dp,1), ei, &d);
1298 SysFreeString(val_str);
1303 end = is_int32(d) ? min(length, d) : length;
1317 V_VT(retv) = VT_BSTR;
1318 V_BSTR(retv) = SysAllocStringLen(str+start, end-start);
1320 SysFreeString(val_str);
1321 return E_OUTOFMEMORY;
1324 SysFreeString(val_str);
1328 /* ECMA-262 3rd Edition B.2.3 */
1329 static HRESULT String_substr(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1330 VARIANT *retv, jsexcept_t *ei)
1341 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
1345 if(arg_cnt(dp) >= 1) {
1346 hres = to_integer(ctx, get_arg(dp,0), ei, &d);
1348 SysFreeString(val_str);
1353 start = is_int32(d) ? min(length, d) : length;
1356 if(arg_cnt(dp) >= 2) {
1357 hres = to_integer(ctx, get_arg(dp,1), ei, &d);
1359 SysFreeString(val_str);
1364 len = is_int32(d) ? min(length-start, d) : length-start;
1373 V_VT(retv) = VT_BSTR;
1374 V_BSTR(retv) = SysAllocStringLen(str+start, len);
1376 hres = E_OUTOFMEMORY;
1379 SysFreeString(val_str);
1383 static HRESULT String_sup(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1384 VARIANT *retv, jsexcept_t *ei)
1386 static const WCHAR suptagW[] = {'S','U','P',0};
1387 return do_attributeless_tag_format(ctx, jsthis, retv, ei, suptagW);
1390 static HRESULT String_toLowerCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1391 VARIANT *retv, jsexcept_t *ei)
1400 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
1406 val_str = SysAllocStringLen(str, length);
1408 return E_OUTOFMEMORY;
1413 V_VT(retv) = VT_BSTR;
1414 V_BSTR(retv) = val_str;
1416 else SysFreeString(val_str);
1420 static HRESULT String_toUpperCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1421 VARIANT *retv, jsexcept_t *ei)
1430 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
1436 val_str = SysAllocStringLen(str, length);
1438 return E_OUTOFMEMORY;
1443 V_VT(retv) = VT_BSTR;
1444 V_BSTR(retv) = val_str;
1446 else SysFreeString(val_str);
1450 static HRESULT String_toLocaleLowerCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1451 VARIANT *retv, jsexcept_t *ei)
1457 static HRESULT String_toLocaleUpperCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1458 VARIANT *retv, jsexcept_t *ei)
1464 static HRESULT String_localeCompare(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1465 VARIANT *retv, jsexcept_t *ei)
1471 static HRESULT String_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1472 VARIANT *retv, jsexcept_t *ei)
1474 StringInstance *This = string_from_vdisp(jsthis);
1480 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
1481 case DISPATCH_PROPERTYGET: {
1482 BSTR str = SysAllocString(This->str);
1484 return E_OUTOFMEMORY;
1486 V_VT(retv) = VT_BSTR;
1491 FIXME("flags %x\n", flags);
1498 static void String_destructor(jsdisp_t *dispex)
1500 StringInstance *This = (StringInstance*)dispex;
1502 heap_free(This->str);
1506 static const builtin_prop_t String_props[] = {
1507 {anchorW, String_anchor, PROPF_METHOD|1},
1508 {bigW, String_big, PROPF_METHOD},
1509 {blinkW, String_blink, PROPF_METHOD},
1510 {boldW, String_bold, PROPF_METHOD},
1511 {charAtW, String_charAt, PROPF_METHOD|1},
1512 {charCodeAtW, String_charCodeAt, PROPF_METHOD|1},
1513 {concatW, String_concat, PROPF_METHOD|1},
1514 {fixedW, String_fixed, PROPF_METHOD},
1515 {fontcolorW, String_fontcolor, PROPF_METHOD|1},
1516 {fontsizeW, String_fontsize, PROPF_METHOD|1},
1517 {indexOfW, String_indexOf, PROPF_METHOD|2},
1518 {italicsW, String_italics, PROPF_METHOD},
1519 {lastIndexOfW, String_lastIndexOf, PROPF_METHOD|2},
1520 {lengthW, String_length, 0},
1521 {linkW, String_link, PROPF_METHOD|1},
1522 {localeCompareW, String_localeCompare, PROPF_METHOD|1},
1523 {matchW, String_match, PROPF_METHOD|1},
1524 {replaceW, String_replace, PROPF_METHOD|1},
1525 {searchW, String_search, PROPF_METHOD},
1526 {sliceW, String_slice, PROPF_METHOD},
1527 {smallW, String_small, PROPF_METHOD},
1528 {splitW, String_split, PROPF_METHOD|2},
1529 {strikeW, String_strike, PROPF_METHOD},
1530 {subW, String_sub, PROPF_METHOD},
1531 {substrW, String_substr, PROPF_METHOD|2},
1532 {substringW, String_substring, PROPF_METHOD|2},
1533 {supW, String_sup, PROPF_METHOD},
1534 {toLocaleLowerCaseW, String_toLocaleLowerCase, PROPF_METHOD},
1535 {toLocaleUpperCaseW, String_toLocaleUpperCase, PROPF_METHOD},
1536 {toLowerCaseW, String_toLowerCase, PROPF_METHOD},
1537 {toStringW, String_toString, PROPF_METHOD},
1538 {toUpperCaseW, String_toUpperCase, PROPF_METHOD},
1539 {valueOfW, String_valueOf, PROPF_METHOD}
1542 static const builtin_info_t String_info = {
1544 {NULL, String_value, 0},
1545 sizeof(String_props)/sizeof(*String_props),
1551 /* ECMA-262 3rd Edition 15.5.3.2 */
1552 static HRESULT StringConstr_fromCharCode(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
1553 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
1559 ret = SysAllocStringLen(NULL, arg_cnt(dp));
1561 return E_OUTOFMEMORY;
1563 for(i=0; i<arg_cnt(dp); i++) {
1564 hres = to_uint32(ctx, get_arg(dp, i), ei, &code);
1574 V_VT(retv) = VT_BSTR;
1577 else SysFreeString(ret);
1582 static HRESULT StringConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1583 VARIANT *retv, jsexcept_t *ei)
1594 hres = to_string(ctx, get_arg(dp, 0), ei, &str);
1598 str = SysAllocStringLen(NULL, 0);
1600 return E_OUTOFMEMORY;
1603 V_VT(retv) = VT_BSTR;
1607 case DISPATCH_CONSTRUCT: {
1613 hres = to_string(ctx, get_arg(dp, 0), ei, &str);
1617 hres = create_string(ctx, str, SysStringLen(str), &ret);
1620 hres = create_string(ctx, NULL, 0, &ret);
1626 var_set_jsdisp(retv, ret);
1631 FIXME("unimplemented flags: %x\n", flags);
1638 static HRESULT string_alloc(script_ctx_t *ctx, jsdisp_t *object_prototype, StringInstance **ret)
1640 StringInstance *string;
1643 string = heap_alloc_zero(sizeof(StringInstance));
1645 return E_OUTOFMEMORY;
1647 if(object_prototype)
1648 hres = init_dispex(&string->dispex, ctx, &String_info, object_prototype);
1650 hres = init_dispex_from_constr(&string->dispex, ctx, &String_info, ctx->string_constr);
1660 static const builtin_prop_t StringConstr_props[] = {
1661 {fromCharCodeW, StringConstr_fromCharCode, PROPF_METHOD},
1664 static const builtin_info_t StringConstr_info = {
1666 {NULL, Function_value, 0},
1667 sizeof(StringConstr_props)/sizeof(*StringConstr_props),
1673 HRESULT create_string_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1675 StringInstance *string;
1678 static const WCHAR StringW[] = {'S','t','r','i','n','g',0};
1680 hres = string_alloc(ctx, object_prototype, &string);
1684 hres = create_builtin_function(ctx, StringConstr_value, StringW, &StringConstr_info,
1685 PROPF_CONSTR|1, &string->dispex, ret);
1687 jsdisp_release(&string->dispex);
1691 HRESULT create_string(script_ctx_t *ctx, const WCHAR *str, DWORD len, jsdisp_t **ret)
1693 StringInstance *string;
1696 hres = string_alloc(ctx, NULL, &string);
1703 string->length = len;
1704 string->str = heap_alloc((len+1)*sizeof(WCHAR));
1706 jsdisp_release(&string->dispex);
1707 return E_OUTOFMEMORY;
1710 memcpy(string->str, str, len*sizeof(WCHAR));
1711 string->str[len] = 0;
1713 *ret = &string->dispex;