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 fromCharCodeW[] = {'f','r','o','m','C','h','a','r','C','o','d','e',0};
67 static HRESULT String_length(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
68 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
70 TRACE("%p\n", dispex);
73 case DISPATCH_PROPERTYGET: {
74 StringInstance *jsthis = (StringInstance*)dispex;
77 V_I4(retv) = jsthis->length;
81 FIXME("unimplemented flags %x\n", flags);
88 /* ECMA-262 3rd Edition 15.5.4.2 */
89 static HRESULT String_toString(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
90 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
92 StringInstance *string;
96 if(!is_class(dispex, JSCLASS_STRING)) {
97 WARN("this is not a string object\n");
101 string = (StringInstance*)dispex;
104 BSTR str = SysAllocString(string->str);
106 return E_OUTOFMEMORY;
108 V_VT(retv) = VT_BSTR;
114 /* ECMA-262 3rd Edition 15.5.4.2 */
115 static HRESULT String_valueOf(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
116 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
120 return String_toString(ctx, dispex, flags, dp, retv, ei, sp);
123 static HRESULT do_attributeless_tag_format(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
124 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp, const WCHAR *tagname)
126 static const WCHAR tagfmt[] = {'<','%','s','>','%','s','<','/','%','s','>',0};
132 if(!is_class(dispex, JSCLASS_STRING)) {
135 V_VT(&this) = VT_DISPATCH;
136 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
138 hres = to_string(dispex->ctx, &this, ei, &val_str);
143 length = SysStringLen(val_str);
146 StringInstance *this = (StringInstance*)dispex;
149 length = this->length;
153 BSTR ret = SysAllocStringLen(NULL, length + 2*strlenW(tagname) + 5);
155 SysFreeString(val_str);
156 return E_OUTOFMEMORY;
159 sprintfW(ret, tagfmt, tagname, str, tagname);
161 V_VT(retv) = VT_BSTR;
165 SysFreeString(val_str);
169 static HRESULT do_attribute_tag_format(DispatchEx *dispex, WORD flags,
170 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp,
171 const WCHAR *tagname, const WCHAR *attr)
173 static const WCHAR tagfmtW[]
174 = {'<','%','s',' ','%','s','=','\"','%','s','\"','>','%','s','<','/','%','s','>',0};
175 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
179 BSTR attr_value, val_str = NULL;
182 if(!is_class(dispex, JSCLASS_STRING)) {
185 V_VT(&this) = VT_DISPATCH;
186 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
188 hres = to_string(dispex->ctx, &this, ei, &val_str);
193 length = SysStringLen(val_str);
196 StringInstance *this = (StringInstance*)dispex;
199 length = this->length;
203 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &attr_value);
205 SysFreeString(val_str);
210 attr_value = SysAllocString(undefinedW);
212 SysFreeString(val_str);
213 return E_OUTOFMEMORY;
218 BSTR ret = SysAllocStringLen(NULL, length + 2*strlenW(tagname)
219 + strlenW(attr) + SysStringLen(attr_value) + 9);
221 SysFreeString(attr_value);
222 SysFreeString(val_str);
223 return E_OUTOFMEMORY;
226 sprintfW(ret, tagfmtW, tagname, attr, attr_value, str, tagname);
228 V_VT(retv) = VT_BSTR;
232 SysFreeString(attr_value);
233 SysFreeString(val_str);
237 static HRESULT String_anchor(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
238 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
240 static const WCHAR fontW[] = {'A',0};
241 static const WCHAR colorW[] = {'N','A','M','E',0};
243 return do_attribute_tag_format(dispex, flags, dp, retv, ei, sp, fontW, colorW);
246 static HRESULT String_big(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
247 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
249 static const WCHAR bigtagW[] = {'B','I','G',0};
250 return do_attributeless_tag_format(dispex, flags, dp, retv, ei, sp, bigtagW);
253 static HRESULT String_blink(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
254 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
256 static const WCHAR blinktagW[] = {'B','L','I','N','K',0};
257 return do_attributeless_tag_format(dispex, flags, dp, retv, ei, sp, blinktagW);
260 static HRESULT String_bold(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
261 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
263 static const WCHAR boldtagW[] = {'B',0};
264 return do_attributeless_tag_format(dispex, flags, dp, retv, ei, sp, boldtagW);
267 /* ECMA-262 3rd Edition 15.5.4.5 */
268 static HRESULT String_charAt(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
269 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
273 BSTR ret, val_str = NULL;
279 if(!is_class(dispex, JSCLASS_STRING)) {
282 V_VT(&this) = VT_DISPATCH;
283 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
285 hres = to_string(dispex->ctx, &this, ei, &val_str);
290 length = SysStringLen(val_str);
293 StringInstance *this = (StringInstance*)dispex;
296 length = this->length;
302 hres = to_integer(dispex->ctx, get_arg(dp, 0), ei, &num);
304 SysFreeString(val_str);
308 if(V_VT(&num) == VT_I4) {
311 WARN("pos = %lf\n", V_R8(&num));
317 SysFreeString(val_str);
321 if(0 <= pos && pos < length)
322 ret = SysAllocStringLen(str+pos, 1);
324 ret = SysAllocStringLen(NULL, 0);
325 SysFreeString(val_str);
327 return E_OUTOFMEMORY;
330 V_VT(retv) = VT_BSTR;
335 /* ECMA-262 3rd Edition 15.5.4.5 */
336 static HRESULT String_charCodeAt(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
337 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
341 DWORD length, idx = 0;
346 if(!is_class(dispex, JSCLASS_STRING)) {
349 V_VT(&this) = VT_DISPATCH;
350 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
352 hres = to_string(dispex->ctx, &this, ei, &val_str);
357 length = SysStringLen(val_str);
360 StringInstance *this = (StringInstance*)dispex;
363 length = this->length;
366 if(arg_cnt(dp) > 0) {
369 hres = to_integer(dispex->ctx, get_arg(dp, 0), ei, &v);
371 SysFreeString(val_str);
375 if(V_VT(&v) != VT_I4 || V_I4(&v) < 0 || V_I4(&v) >= length) {
376 if(retv) num_set_nan(&v);
377 SysFreeString(val_str);
386 V_I4(retv) = str[idx];
389 SysFreeString(val_str);
393 /* ECMA-262 3rd Edition 15.5.4.6 */
394 static HRESULT String_concat(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
395 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
397 BSTR *strs = NULL, ret = NULL;
398 DWORD len = 0, i, l, str_cnt;
405 str_cnt = arg_cnt(dp)+1;
406 strs = heap_alloc_zero(str_cnt * sizeof(BSTR));
408 return E_OUTOFMEMORY;
410 V_VT(&var) = VT_DISPATCH;
411 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(dispex);
413 hres = to_string(dispex->ctx, &var, ei, strs);
414 if(SUCCEEDED(hres)) {
415 for(i=0; i < arg_cnt(dp); i++) {
416 hres = to_string(dispex->ctx, get_arg(dp, i), ei, strs+i+1);
422 if(SUCCEEDED(hres)) {
423 for(i=0; i < str_cnt; i++)
424 len += SysStringLen(strs[i]);
426 ptr = ret = SysAllocStringLen(NULL, len);
428 for(i=0; i < str_cnt; i++) {
429 l = SysStringLen(strs[i]);
430 memcpy(ptr, strs[i], l*sizeof(WCHAR));
435 for(i=0; i < str_cnt; i++)
436 SysFreeString(strs[i]);
443 V_VT(retv) = VT_BSTR;
451 static HRESULT String_fixed(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
452 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
454 static const WCHAR fixedtagW[] = {'T','T',0};
455 return do_attributeless_tag_format(dispex, flags, dp, retv, ei, sp, fixedtagW);
458 static HRESULT String_fontcolor(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
459 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
461 static const WCHAR fontW[] = {'F','O','N','T',0};
462 static const WCHAR colorW[] = {'C','O','L','O','R',0};
464 return do_attribute_tag_format(dispex, flags, dp, retv, ei, sp, fontW, colorW);
467 static HRESULT String_fontsize(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
468 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
470 static const WCHAR fontW[] = {'F','O','N','T',0};
471 static const WCHAR colorW[] = {'S','I','Z','E',0};
473 return do_attribute_tag_format(dispex, flags, dp, retv, ei, sp, fontW, colorW);
476 static HRESULT String_indexOf(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
477 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
479 DWORD length, pos = 0;
481 BSTR search_str, val_str = NULL;
487 if(!is_class(dispex, JSCLASS_STRING)) {
490 V_VT(&this) = VT_DISPATCH;
491 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
493 hres = to_string(dispex->ctx, &this, ei, &val_str);
498 length = SysStringLen(val_str);
501 StringInstance *this = (StringInstance*)dispex;
504 length = this->length;
512 SysFreeString(val_str);
516 hres = to_string(dispex->ctx, get_arg(dp,0), ei, &search_str);
518 SysFreeString(val_str);
522 if(arg_cnt(dp) >= 2) {
525 hres = to_integer(dispex->ctx, get_arg(dp,1), ei, &ival);
526 if(SUCCEEDED(hres)) {
527 if(V_VT(&ival) == VT_I4)
528 pos = V_VT(&ival) > 0 ? V_I4(&ival) : 0;
530 pos = V_R8(&ival) > 0.0 ? length : 0;
536 if(SUCCEEDED(hres)) {
539 ptr = strstrW(str+pos, search_str);
546 SysFreeString(search_str);
547 SysFreeString(val_str);
558 static HRESULT String_italics(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
559 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
561 static const WCHAR italicstagW[] = {'I',0};
562 return do_attributeless_tag_format(dispex, flags, dp, retv, ei, sp, italicstagW);
565 /* ECMA-262 3rd Edition 15.5.4.8 */
566 static HRESULT String_lastIndexOf(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
567 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
569 BSTR search_str, val_str = NULL;
570 DWORD length, pos, search_len;
577 if(is_class(dispex, JSCLASS_STRING)) {
578 StringInstance *this = (StringInstance*)dispex;
581 length = this->length;
585 V_VT(&this) = VT_DISPATCH;
586 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
588 hres = to_string(dispex->ctx, &this, ei, &val_str);
593 length = SysStringLen(val_str);
601 SysFreeString(val_str);
605 hres = to_string(dispex->ctx, get_arg(dp,0), ei, &search_str);
607 SysFreeString(val_str);
611 search_len = SysStringLen(search_str);
613 if(arg_cnt(dp) >= 2) {
616 hres = to_integer(dispex->ctx, get_arg(dp,1), ei, &ival);
617 if(SUCCEEDED(hres)) {
618 if(V_VT(&ival) == VT_I4)
619 pos = V_VT(&ival) > 0 ? V_I4(&ival) : 0;
621 pos = V_R8(&ival) > 0.0 ? length : 0;
629 if(SUCCEEDED(hres) && length >= search_len) {
632 for(ptr = str+min(pos, length-search_len); ptr >= str; ptr--) {
633 if(!memcmp(ptr, search_str, search_len*sizeof(WCHAR))) {
640 SysFreeString(search_str);
641 SysFreeString(val_str);
652 static HRESULT String_link(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
653 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
655 static const WCHAR fontW[] = {'A',0};
656 static const WCHAR colorW[] = {'H','R','E','F',0};
658 return do_attribute_tag_format(dispex, flags, dp, retv, ei, sp, fontW, colorW);
661 /* ECMA-262 3rd Edition 15.5.4.10 */
662 static HRESULT String_match(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
663 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
666 match_result_t *match_result;
669 VARIANT var, *arg_var;
670 DWORD length, match_cnt, i;
678 V_VT(retv) = VT_NULL;
684 arg_var = get_arg(dp, 0);
685 switch(V_VT(arg_var)) {
687 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
689 if(regexp->builtin_info->class == JSCLASS_REGEXP)
691 jsdisp_release(regexp);
696 hres = to_string(dispex->ctx, arg_var, ei, &match_str);
700 hres = create_regexp_str(dispex->ctx, match_str, SysStringLen(match_str), NULL, 0, ®exp);
701 SysFreeString(match_str);
707 if(!is_class(dispex, JSCLASS_STRING)) {
710 V_VT(&this) = VT_DISPATCH;
711 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
713 hres = to_string(dispex->ctx, &this, ei, &val_str);
715 jsdisp_release(regexp);
720 length = SysStringLen(val_str);
723 StringInstance *this = (StringInstance*)dispex;
726 length = this->length;
729 hres = regexp_match(regexp, str, length, FALSE, &match_result, &match_cnt);
730 jsdisp_release(regexp);
732 SysFreeString(val_str);
740 V_VT(retv) = VT_NULL;
742 SysFreeString(val_str);
746 hres = create_array(dispex->ctx, match_cnt, &array);
748 SysFreeString(val_str);
752 V_VT(&var) = VT_BSTR;
754 for(i=0; i < match_cnt; i++) {
755 V_BSTR(&var) = SysAllocStringLen(match_result[i].str, match_result[i].len);
757 hres = E_OUTOFMEMORY;
761 hres = jsdisp_propput_idx(array, i, &var, ei, NULL/*FIXME*/);
762 SysFreeString(V_BSTR(&var));
767 SysFreeString(val_str);
769 if(SUCCEEDED(hres) && retv) {
770 V_VT(retv) = VT_DISPATCH;
771 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(array);
773 jsdisp_release(array);
784 static HRESULT strbuf_append(strbuf_t *buf, const WCHAR *str, DWORD len)
789 if(len + buf->len > buf->size) {
793 new_size = buf->size ? buf->size<<1 : 16;
794 if(new_size < buf->len+len)
795 new_size = buf->len+len;
797 new_buf = heap_realloc(buf->buf, new_size*sizeof(WCHAR));
799 new_buf = heap_alloc(new_size*sizeof(WCHAR));
801 return E_OUTOFMEMORY;
804 buf->size = new_size;
807 memcpy(buf->buf+buf->len, str, len*sizeof(WCHAR));
812 static HRESULT rep_call(DispatchEx *func, const WCHAR *str, match_result_t *match, match_result_t *parens,
813 DWORD parens_cnt, BSTR *ret, jsexcept_t *ei, IServiceProvider *caller)
815 DISPPARAMS dp = {NULL, NULL, 0, 0};
816 VARIANTARG *args, *arg;
821 dp.cArgs = parens_cnt+3;
822 dp.rgvarg = args = heap_alloc_zero(sizeof(VARIANT)*dp.cArgs);
824 return E_OUTOFMEMORY;
826 arg = get_arg(&dp,0);
828 V_BSTR(arg) = SysAllocStringLen(match->str, match->len);
830 hres = E_OUTOFMEMORY;
832 if(SUCCEEDED(hres)) {
833 for(i=0; i < parens_cnt; i++) {
834 arg = get_arg(&dp,i+1);
836 V_BSTR(arg) = SysAllocStringLen(parens[i].str, parens[i].len);
838 hres = E_OUTOFMEMORY;
844 if(SUCCEEDED(hres)) {
845 arg = get_arg(&dp,parens_cnt+1);
847 V_I4(arg) = match->str - str;
849 arg = get_arg(&dp,parens_cnt+2);
851 V_BSTR(arg) = SysAllocString(str);
853 hres = E_OUTOFMEMORY;
857 hres = jsdisp_call_value(func, DISPATCH_METHOD, &dp, &var, ei, caller);
859 for(i=0; i < parens_cnt+1; i++) {
860 if(i != parens_cnt+1)
861 SysFreeString(V_BSTR(get_arg(&dp,i)));
868 hres = to_string(func->ctx, &var, ei, ret);
873 /* ECMA-262 3rd Edition 15.5.4.11 */
874 static HRESULT String_replace(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
875 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
878 DWORD parens_cnt = 0, parens_size=0, rep_len=0, length;
879 BSTR rep_str = NULL, match_str = NULL, ret_str, val_str = NULL;
880 DispatchEx *rep_func = NULL, *regexp = NULL;
881 match_result_t *parens = NULL, match, **parens_ptr = &parens;
882 strbuf_t ret = {NULL,0,0};
889 if(!is_class(dispex, JSCLASS_STRING)) {
892 V_VT(&this) = VT_DISPATCH;
893 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
895 hres = to_string(dispex->ctx, &this, ei, &val_str);
900 length = SysStringLen(val_str);
903 StringInstance *this = (StringInstance*)dispex;
906 length = this->length;
912 val_str = SysAllocStringLen(str, length);
914 return E_OUTOFMEMORY;
917 V_VT(retv) = VT_BSTR;
918 V_BSTR(retv) = val_str;
923 arg_var = get_arg(dp, 0);
924 switch(V_VT(arg_var)) {
926 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
928 if(is_class(regexp, JSCLASS_REGEXP)) {
931 jsdisp_release(regexp);
937 hres = to_string(dispex->ctx, arg_var, ei, &match_str);
939 SysFreeString(val_str);
944 if(arg_cnt(dp) >= 2) {
945 arg_var = get_arg(dp,1);
946 switch(V_VT(arg_var)) {
948 rep_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
950 if(is_class(rep_func, JSCLASS_FUNCTION)) {
953 jsdisp_release(rep_func);
959 hres = to_string(dispex->ctx, arg_var, ei, &rep_str);
963 rep_len = SysStringLen(rep_str);
964 if(!strchrW(rep_str, '$'))
969 if(SUCCEEDED(hres)) {
970 const WCHAR *cp, *ecp;
976 hres = regexp_match_next(regexp, gcheck, str, length, &cp, parens_ptr,
977 &parens_size, &parens_cnt, &match);
980 if(hres == S_FALSE) {
987 match.str = strstrW(cp, match_str);
990 match.len = SysStringLen(match_str);
991 cp = match.str+match.len;
994 hres = strbuf_append(&ret, ecp, match.str-ecp);
995 ecp = match.str+match.len;
1002 hres = rep_call(rep_func, str, &match, parens, parens_cnt, &cstr, ei, caller);
1006 hres = strbuf_append(&ret, cstr, SysStringLen(cstr));
1007 SysFreeString(cstr);
1010 }else if(rep_str && regexp) {
1011 const WCHAR *ptr = rep_str, *ptr2;
1013 while((ptr2 = strchrW(ptr, '$'))) {
1014 hres = strbuf_append(&ret, ptr, ptr2-ptr);
1020 hres = strbuf_append(&ret, ptr2, 1);
1024 hres = strbuf_append(&ret, match.str, match.len);
1028 hres = strbuf_append(&ret, str, match.str-str);
1032 hres = strbuf_append(&ret, ecp, (str+length)-ecp);
1038 if(!isdigitW(ptr2[1])) {
1039 hres = strbuf_append(&ret, ptr2, 1);
1044 idx = ptr2[1] - '0';
1045 if(isdigitW(ptr[3]) && idx*10 + (ptr[2]-'0') <= parens_cnt) {
1046 idx = idx*10 + (ptr[2]-'0');
1048 }else if(idx && idx <= parens_cnt) {
1051 hres = strbuf_append(&ret, ptr2, 1);
1056 hres = strbuf_append(&ret, parens[idx-1].str, parens[idx-1].len);
1065 hres = strbuf_append(&ret, ptr, (rep_str+rep_len)-ptr);
1069 hres = strbuf_append(&ret, rep_str, rep_len);
1073 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d'};
1075 hres = strbuf_append(&ret, undefinedW, sizeof(undefinedW)/sizeof(WCHAR));
1082 hres = strbuf_append(&ret, ecp, (str+length)-ecp);
1086 jsdisp_release(rep_func);
1088 jsdisp_release(regexp);
1089 SysFreeString(val_str);
1090 SysFreeString(rep_str);
1091 SysFreeString(match_str);
1094 if(SUCCEEDED(hres) && retv) {
1095 ret_str = SysAllocStringLen(ret.buf, ret.len);
1097 return E_OUTOFMEMORY;
1099 V_VT(retv) = VT_BSTR;
1100 V_BSTR(retv) = ret_str;
1101 TRACE("= %s\n", debugstr_w(ret_str));
1108 static HRESULT String_search(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
1109 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1115 /* ECMA-262 3rd Edition 15.5.4.13 */
1116 static HRESULT String_slice(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
1117 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1120 BSTR val_str = NULL;
1128 if(!is_class(dispex, JSCLASS_STRING)) {
1131 V_VT(&this) = VT_DISPATCH;
1132 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
1134 hres = to_string(dispex->ctx, &this, ei, &val_str);
1139 length = SysStringLen(val_str);
1142 StringInstance *this = (StringInstance*)dispex;
1145 length = this->length;
1149 hres = to_integer(dispex->ctx, get_arg(dp,0), ei, &v);
1151 SysFreeString(val_str);
1155 if(V_VT(&v) == VT_I4) {
1158 start = length + start;
1161 }else if(start > length) {
1165 start = V_R8(&v) < 0.0 ? 0 : length;
1171 if(arg_cnt(dp) >= 2) {
1172 hres = to_integer(dispex->ctx, get_arg(dp,1), ei, &v);
1174 SysFreeString(val_str);
1178 if(V_VT(&v) == VT_I4) {
1184 }else if(end > length) {
1188 end = V_R8(&v) < 0.0 ? 0 : length;
1198 BSTR retstr = SysAllocStringLen(str+start, end-start);
1200 SysFreeString(val_str);
1201 return E_OUTOFMEMORY;
1204 V_VT(retv) = VT_BSTR;
1205 V_BSTR(retv) = retstr;
1208 SysFreeString(val_str);
1212 static HRESULT String_small(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
1213 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1215 static const WCHAR smalltagW[] = {'S','M','A','L','L',0};
1216 return do_attributeless_tag_format(dispex, flags, dp, retv, ei, sp, smalltagW);
1219 static HRESULT String_split(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
1220 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1222 match_result_t *match_result = NULL;
1223 DWORD length, match_cnt, i, match_len = 0;
1224 const WCHAR *str, *ptr, *ptr2;
1227 BSTR val_str = NULL, match_str = NULL;
1232 if(arg_cnt(dp) != 1) {
1233 FIXME("unsupported args\n");
1237 if(!is_class(dispex, JSCLASS_STRING)) {
1240 V_VT(&this) = VT_DISPATCH;
1241 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
1243 hres = to_string(dispex->ctx, &this, ei, &val_str);
1248 length = SysStringLen(val_str);
1251 StringInstance *this = (StringInstance*)dispex;
1254 length = this->length;
1257 arg = get_arg(dp, 0);
1262 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
1264 if(is_class(regexp, JSCLASS_REGEXP)) {
1265 hres = regexp_match(regexp, str, length, TRUE, &match_result, &match_cnt);
1266 jsdisp_release(regexp);
1268 SysFreeString(val_str);
1273 jsdisp_release(regexp);
1277 hres = to_string(dispex->ctx, arg, ei, &match_str);
1279 SysFreeString(val_str);
1283 match_len = SysStringLen(match_str);
1285 SysFreeString(match_str);
1290 hres = create_array(dispex->ctx, 0, &array);
1292 if(SUCCEEDED(hres)) {
1298 ptr2 = match_result[i].str;
1299 }else if(match_str) {
1300 ptr2 = strstrW(ptr, match_str);
1309 V_VT(&var) = VT_BSTR;
1310 V_BSTR(&var) = SysAllocStringLen(ptr, ptr2-ptr);
1312 hres = E_OUTOFMEMORY;
1316 hres = jsdisp_propput_idx(array, i, &var, ei, sp);
1317 SysFreeString(V_BSTR(&var));
1322 ptr = match_result[i].str + match_result[i].len;
1324 ptr = ptr2 + match_len;
1330 if(SUCCEEDED(hres) && (match_str || match_result)) {
1331 DWORD len = (str+length) - ptr;
1333 if(len || match_str) {
1334 V_VT(&var) = VT_BSTR;
1335 V_BSTR(&var) = SysAllocStringLen(ptr, len);
1338 hres = jsdisp_propput_idx(array, i, &var, ei, sp);
1339 SysFreeString(V_BSTR(&var));
1341 hres = E_OUTOFMEMORY;
1346 SysFreeString(match_str);
1347 SysFreeString(val_str);
1348 heap_free(match_result);
1350 if(SUCCEEDED(hres) && retv) {
1351 V_VT(retv) = VT_DISPATCH;
1352 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(array);
1354 jsdisp_release(array);
1360 static HRESULT String_strike(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
1361 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1363 static const WCHAR striketagW[] = {'S','T','R','I','K','E',0};
1364 return do_attributeless_tag_format(dispex, flags, dp, retv, ei, sp, striketagW);
1367 static HRESULT String_sub(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
1368 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1370 static const WCHAR subtagW[] = {'S','U','B',0};
1371 return do_attributeless_tag_format(dispex, flags, dp, retv, ei, sp, subtagW);
1374 /* ECMA-262 3rd Edition 15.5.4.15 */
1375 static HRESULT String_substring(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
1376 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1379 BSTR val_str = NULL;
1387 if(!is_class(dispex, JSCLASS_STRING)) {
1390 V_VT(&this) = VT_DISPATCH;
1391 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
1393 hres = to_string(dispex->ctx, &this, ei, &val_str);
1398 length = SysStringLen(val_str);
1401 StringInstance *this = (StringInstance*)dispex;
1404 length = this->length;
1407 if(arg_cnt(dp) >= 1) {
1408 hres = to_integer(dispex->ctx, get_arg(dp,0), ei, &v);
1410 SysFreeString(val_str);
1414 if(V_VT(&v) == VT_I4) {
1418 else if(start >= length)
1421 start = V_R8(&v) < 0.0 ? 0 : length;
1425 if(arg_cnt(dp) >= 2) {
1426 hres = to_integer(dispex->ctx, get_arg(dp,1), ei, &v);
1428 SysFreeString(val_str);
1432 if(V_VT(&v) == VT_I4) {
1436 else if(end > length)
1439 end = V_R8(&v) < 0.0 ? 0 : length;
1452 V_VT(retv) = VT_BSTR;
1453 V_BSTR(retv) = SysAllocStringLen(str+start, end-start);
1455 SysFreeString(val_str);
1456 return E_OUTOFMEMORY;
1459 SysFreeString(val_str);
1463 /* ECMA-262 3rd Edition B.2.3 */
1464 static HRESULT String_substr(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
1465 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1467 BSTR val_str = NULL;
1476 if(is_class(dispex, JSCLASS_STRING)) {
1477 StringInstance *this = (StringInstance*)dispex;
1480 length = this->length;
1484 V_VT(&this) = VT_DISPATCH;
1485 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
1486 hres = to_string(dispex->ctx, &this, ei, &val_str);
1491 length = SysStringLen(val_str);
1494 if(arg_cnt(dp) >= 1) {
1495 hres = to_integer(dispex->ctx, get_arg(dp,0), ei, &v);
1497 SysFreeString(val_str);
1501 if(V_VT(&v) == VT_I4) {
1505 else if(start >= length)
1508 start = V_R8(&v) < 0.0 ? 0 : length;
1512 if(arg_cnt(dp) >= 2) {
1513 hres = to_integer(dispex->ctx, get_arg(dp,1), ei, &v);
1515 SysFreeString(val_str);
1519 if(V_VT(&v) == VT_I4) {
1523 else if(len > length-start)
1526 len = V_R8(&v) < 0.0 ? 0 : length-start;
1534 V_VT(retv) = VT_BSTR;
1535 V_BSTR(retv) = SysAllocStringLen(str+start, len);
1537 hres = E_OUTOFMEMORY;
1540 SysFreeString(val_str);
1544 static HRESULT String_sup(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
1545 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1547 static const WCHAR suptagW[] = {'S','U','P',0};
1548 return do_attributeless_tag_format(dispex, flags, dp, retv, ei, sp, suptagW);
1551 static HRESULT String_toLowerCase(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
1552 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1556 BSTR val_str = NULL;
1561 if(!is_class(dispex, JSCLASS_STRING)) {
1564 V_VT(&this) = VT_DISPATCH;
1565 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
1567 hres = to_string(dispex->ctx, &this, ei, &val_str);
1572 length = SysStringLen(val_str);
1575 StringInstance *this = (StringInstance*)dispex;
1578 length = this->length;
1583 val_str = SysAllocStringLen(str, length);
1585 return E_OUTOFMEMORY;
1590 V_VT(retv) = VT_BSTR;
1591 V_BSTR(retv) = val_str;
1593 else SysFreeString(val_str);
1597 static HRESULT String_toUpperCase(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
1598 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1602 BSTR val_str = NULL;
1607 if(!is_class(dispex, JSCLASS_STRING)) {
1610 V_VT(&this) = VT_DISPATCH;
1611 V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
1613 hres = to_string(dispex->ctx, &this, ei, &val_str);
1618 length = SysStringLen(val_str);
1621 StringInstance *this = (StringInstance*)dispex;
1624 length = this->length;
1629 val_str = SysAllocStringLen(str, length);
1631 return E_OUTOFMEMORY;
1636 V_VT(retv) = VT_BSTR;
1637 V_BSTR(retv) = val_str;
1639 else SysFreeString(val_str);
1643 static HRESULT String_toLocaleLowerCase(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
1644 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1650 static HRESULT String_toLocaleUpperCase(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
1651 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1657 static HRESULT String_localeCompare(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
1658 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1664 static HRESULT String_value(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
1665 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1667 StringInstance *This = (StringInstance*)dispex;
1673 return throw_type_error(dispex->ctx, ei, IDS_NOT_FUNC, NULL);
1674 case DISPATCH_PROPERTYGET: {
1675 BSTR str = SysAllocString(This->str);
1677 return E_OUTOFMEMORY;
1679 V_VT(retv) = VT_BSTR;
1684 FIXME("flags %x\n", flags);
1691 static void String_destructor(DispatchEx *dispex)
1693 StringInstance *This = (StringInstance*)dispex;
1695 heap_free(This->str);
1699 static const builtin_prop_t String_props[] = {
1700 {anchorW, String_anchor, PROPF_METHOD|1},
1701 {bigW, String_big, PROPF_METHOD},
1702 {blinkW, String_blink, PROPF_METHOD},
1703 {boldW, String_bold, PROPF_METHOD},
1704 {charAtW, String_charAt, PROPF_METHOD|1},
1705 {charCodeAtW, String_charCodeAt, PROPF_METHOD|1},
1706 {concatW, String_concat, PROPF_METHOD|1},
1707 {fixedW, String_fixed, PROPF_METHOD},
1708 {fontcolorW, String_fontcolor, PROPF_METHOD|1},
1709 {fontsizeW, String_fontsize, PROPF_METHOD|1},
1710 {indexOfW, String_indexOf, PROPF_METHOD|2},
1711 {italicsW, String_italics, PROPF_METHOD},
1712 {lastIndexOfW, String_lastIndexOf, PROPF_METHOD|2},
1713 {lengthW, String_length, 0},
1714 {linkW, String_link, PROPF_METHOD|1},
1715 {localeCompareW, String_localeCompare, PROPF_METHOD|1},
1716 {matchW, String_match, PROPF_METHOD|1},
1717 {replaceW, String_replace, PROPF_METHOD|1},
1718 {searchW, String_search, PROPF_METHOD},
1719 {sliceW, String_slice, PROPF_METHOD},
1720 {smallW, String_small, PROPF_METHOD},
1721 {splitW, String_split, PROPF_METHOD|2},
1722 {strikeW, String_strike, PROPF_METHOD},
1723 {subW, String_sub, PROPF_METHOD},
1724 {substrW, String_substr, PROPF_METHOD|2},
1725 {substringW, String_substring, PROPF_METHOD|2},
1726 {supW, String_sup, PROPF_METHOD},
1727 {toLocaleLowerCaseW, String_toLocaleLowerCase, PROPF_METHOD},
1728 {toLocaleUpperCaseW, String_toLocaleUpperCase, PROPF_METHOD},
1729 {toLowerCaseW, String_toLowerCase, PROPF_METHOD},
1730 {toStringW, String_toString, PROPF_METHOD},
1731 {toUpperCaseW, String_toUpperCase, PROPF_METHOD},
1732 {valueOfW, String_valueOf, PROPF_METHOD}
1735 static const builtin_info_t String_info = {
1737 {NULL, String_value, 0},
1738 sizeof(String_props)/sizeof(*String_props),
1744 /* ECMA-262 3rd Edition 15.5.3.2 */
1745 static HRESULT StringConstr_fromCharCode(script_ctx_t *ctx, DispatchEx *dispex, WORD flags,
1746 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1752 ret = SysAllocStringLen(NULL, arg_cnt(dp));
1754 return E_OUTOFMEMORY;
1756 for(i=0; i<arg_cnt(dp); i++) {
1757 hres = to_uint32(dispex->ctx, get_arg(dp, i), ei, &code);
1767 V_VT(retv) = VT_BSTR;
1770 else SysFreeString(ret);
1775 static HRESULT StringConstr_value(script_ctx_t *ctx, DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
1776 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1787 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
1791 str = SysAllocStringLen(NULL, 0);
1793 return E_OUTOFMEMORY;
1796 V_VT(retv) = VT_BSTR;
1800 case DISPATCH_CONSTRUCT: {
1806 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
1810 hres = create_string(dispex->ctx, str, SysStringLen(str), &ret);
1813 hres = create_string(dispex->ctx, NULL, 0, &ret);
1819 V_VT(retv) = VT_DISPATCH;
1820 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
1825 FIXME("unimplemented flags: %x\n", flags);
1832 static HRESULT string_alloc(script_ctx_t *ctx, DispatchEx *object_prototype, StringInstance **ret)
1834 StringInstance *string;
1837 string = heap_alloc_zero(sizeof(StringInstance));
1839 return E_OUTOFMEMORY;
1841 if(object_prototype)
1842 hres = init_dispex(&string->dispex, ctx, &String_info, object_prototype);
1844 hres = init_dispex_from_constr(&string->dispex, ctx, &String_info, ctx->string_constr);
1854 static const builtin_prop_t StringConstr_props[] = {
1855 {fromCharCodeW, StringConstr_fromCharCode, PROPF_METHOD},
1858 static const builtin_info_t StringConstr_info = {
1860 {NULL, Function_value, 0},
1861 sizeof(StringConstr_props)/sizeof(*StringConstr_props),
1867 HRESULT create_string_constr(script_ctx_t *ctx, DispatchEx *object_prototype, DispatchEx **ret)
1869 StringInstance *string;
1872 static const WCHAR StringW[] = {'S','t','r','i','n','g',0};
1874 hres = string_alloc(ctx, object_prototype, &string);
1878 hres = create_builtin_function(ctx, StringConstr_value, StringW, &StringConstr_info,
1879 PROPF_CONSTR, &string->dispex, ret);
1881 jsdisp_release(&string->dispex);
1885 HRESULT create_string(script_ctx_t *ctx, const WCHAR *str, DWORD len, DispatchEx **ret)
1887 StringInstance *string;
1890 hres = string_alloc(ctx, NULL, &string);
1897 string->length = len;
1898 string->str = heap_alloc((len+1)*sizeof(WCHAR));
1900 jsdisp_release(&string->dispex);
1901 return E_OUTOFMEMORY;
1904 memcpy(string->str, str, len*sizeof(WCHAR));
1905 string->str[len] = 0;
1907 *ret = &string->dispex;