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"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
29 #define UINT32_MAX 0xffffffff
36 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
37 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
38 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
39 static const WCHAR anchorW[] = {'a','n','c','h','o','r',0};
40 static const WCHAR bigW[] = {'b','i','g',0};
41 static const WCHAR blinkW[] = {'b','l','i','n','k',0};
42 static const WCHAR boldW[] = {'b','o','l','d',0};
43 static const WCHAR charAtW[] = {'c','h','a','r','A','t',0};
44 static const WCHAR charCodeAtW[] = {'c','h','a','r','C','o','d','e','A','t',0};
45 static const WCHAR concatW[] = {'c','o','n','c','a','t',0};
46 static const WCHAR fixedW[] = {'f','i','x','e','d',0};
47 static const WCHAR fontcolorW[] = {'f','o','n','t','c','o','l','o','r',0};
48 static const WCHAR fontsizeW[] = {'f','o','n','t','s','i','z','e',0};
49 static const WCHAR indexOfW[] = {'i','n','d','e','x','O','f',0};
50 static const WCHAR italicsW[] = {'i','t','a','l','i','c','s',0};
51 static const WCHAR lastIndexOfW[] = {'l','a','s','t','I','n','d','e','x','O','f',0};
52 static const WCHAR linkW[] = {'l','i','n','k',0};
53 static const WCHAR matchW[] = {'m','a','t','c','h',0};
54 static const WCHAR replaceW[] = {'r','e','p','l','a','c','e',0};
55 static const WCHAR searchW[] = {'s','e','a','r','c','h',0};
56 static const WCHAR sliceW[] = {'s','l','i','c','e',0};
57 static const WCHAR smallW[] = {'s','m','a','l','l',0};
58 static const WCHAR splitW[] = {'s','p','l','i','t',0};
59 static const WCHAR strikeW[] = {'s','t','r','i','k','e',0};
60 static const WCHAR subW[] = {'s','u','b',0};
61 static const WCHAR substringW[] = {'s','u','b','s','t','r','i','n','g',0};
62 static const WCHAR substrW[] = {'s','u','b','s','t','r',0};
63 static const WCHAR supW[] = {'s','u','p',0};
64 static const WCHAR toLowerCaseW[] = {'t','o','L','o','w','e','r','C','a','s','e',0};
65 static const WCHAR toUpperCaseW[] = {'t','o','U','p','p','e','r','C','a','s','e',0};
66 static const WCHAR toLocaleLowerCaseW[] = {'t','o','L','o','c','a','l','e','L','o','w','e','r','C','a','s','e',0};
67 static const WCHAR toLocaleUpperCaseW[] = {'t','o','L','o','c','a','l','e','U','p','p','e','r','C','a','s','e',0};
68 static const WCHAR localeCompareW[] = {'l','o','c','a','l','e','C','o','m','p','a','r','e',0};
69 static const WCHAR fromCharCodeW[] = {'f','r','o','m','C','h','a','r','C','o','d','e',0};
71 static inline StringInstance *string_from_vdisp(vdisp_t *vdisp)
73 return (StringInstance*)vdisp->u.jsdisp;
76 static inline StringInstance *string_this(vdisp_t *jsthis)
78 return is_vclass(jsthis, JSCLASS_STRING) ? string_from_vdisp(jsthis) : NULL;
81 static HRESULT get_string_val(script_ctx_t *ctx, vdisp_t *jsthis, jsstr_t **val)
83 StringInstance *string;
85 if((string = string_this(jsthis))) {
86 *val = jsstr_addref(string->str);
90 return to_string(ctx, jsval_disp(jsthis->u.disp), val);
93 static HRESULT String_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
96 TRACE("%p\n", jsthis);
99 case DISPATCH_PROPERTYGET: {
100 StringInstance *string = string_from_vdisp(jsthis);
102 *r = jsval_number(jsstr_length(string->str));
106 FIXME("unimplemented flags %x\n", flags);
113 static HRESULT stringobj_to_string(vdisp_t *jsthis, jsval_t *r)
115 StringInstance *string;
117 if(!(string = string_this(jsthis))) {
118 WARN("this is not a string object\n");
123 *r = jsval_string(jsstr_addref(string->str));
127 /* ECMA-262 3rd Edition 15.5.4.2 */
128 static HRESULT String_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
133 return stringobj_to_string(jsthis, r);
136 /* ECMA-262 3rd Edition 15.5.4.2 */
137 static HRESULT String_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
142 return stringobj_to_string(jsthis, r);
145 static HRESULT do_attributeless_tag_format(script_ctx_t *ctx, vdisp_t *jsthis, jsval_t *r, const WCHAR *tagname)
147 unsigned tagname_len;
152 hres = get_string_val(ctx, jsthis, &str);
161 tagname_len = strlenW(tagname);
163 ptr = jsstr_alloc_buf(jsstr_length(str) + 2*tagname_len + 5, &ret);
166 return E_OUTOFMEMORY;
170 memcpy(ptr, tagname, tagname_len*sizeof(WCHAR));
174 ptr += jsstr_flush(str, ptr);
179 memcpy(ptr, tagname, tagname_len*sizeof(WCHAR));
183 *r = jsval_string(ret);
187 static HRESULT do_attribute_tag_format(script_ctx_t *ctx, vdisp_t *jsthis, unsigned argc, jsval_t *argv, jsval_t *r,
188 const WCHAR *tagname, const WCHAR *attrname)
190 jsstr_t *str, *attr_value = NULL;
193 hres = get_string_val(ctx, jsthis, &str);
198 hres = to_string(ctx, argv[0], &attr_value);
204 attr_value = jsstr_undefined();
208 unsigned attrname_len = strlenW(attrname);
209 unsigned tagname_len = strlenW(tagname);
213 ptr = jsstr_alloc_buf(2*tagname_len + attrname_len + jsstr_length(attr_value) + jsstr_length(str) + 9, &ret);
216 memcpy(ptr, tagname, tagname_len*sizeof(WCHAR));
219 memcpy(ptr, attrname, attrname_len*sizeof(WCHAR));
223 ptr += jsstr_flush(attr_value, ptr);
226 ptr += jsstr_flush(str, ptr);
230 memcpy(ptr, tagname, tagname_len*sizeof(WCHAR));
234 *r = jsval_string(ret);
236 hres = E_OUTOFMEMORY;
240 jsstr_release(attr_value);
245 static HRESULT String_anchor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
248 static const WCHAR fontW[] = {'A',0};
249 static const WCHAR colorW[] = {'N','A','M','E',0};
251 return do_attribute_tag_format(ctx, jsthis, argc, argv, r, fontW, colorW);
254 static HRESULT String_big(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
257 static const WCHAR bigtagW[] = {'B','I','G',0};
258 return do_attributeless_tag_format(ctx, jsthis, r, bigtagW);
261 static HRESULT String_blink(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
264 static const WCHAR blinktagW[] = {'B','L','I','N','K',0};
265 return do_attributeless_tag_format(ctx, jsthis, r, blinktagW);
268 static HRESULT String_bold(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
271 static const WCHAR boldtagW[] = {'B',0};
272 return do_attributeless_tag_format(ctx, jsthis, r, boldtagW);
275 /* ECMA-262 3rd Edition 15.5.4.5 */
276 static HRESULT String_charAt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
285 hres = get_string_val(ctx, jsthis, &str);
292 hres = to_integer(ctx, argv[0], &d);
297 pos = is_int32(d) ? d : -1;
305 if(0 <= pos && pos < jsstr_length(str)) {
306 ret = jsstr_substr(str, pos, 1);
308 return E_OUTOFMEMORY;
313 *r = jsval_string(ret);
317 /* ECMA-262 3rd Edition 15.5.4.5 */
318 static HRESULT String_charCodeAt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
327 hres = get_string_val(ctx, jsthis, &str);
334 hres = to_integer(ctx, argv[0], &d);
340 if(!is_int32(d) || d < 0 || d >= jsstr_length(str)) {
343 *r = jsval_number(NAN);
351 *r = jsval_number(str->str[idx]);
357 /* ECMA-262 3rd Edition 15.5.4.6 */
358 static HRESULT String_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
361 unsigned len = 0, i, str_cnt;
362 jsstr_t **strs, *ret = NULL;
369 strs = heap_alloc_zero(str_cnt * sizeof(*strs));
371 return E_OUTOFMEMORY;
373 hres = to_string(ctx, jsval_disp(jsthis->u.disp), strs);
374 if(SUCCEEDED(hres)) {
375 for(i=0; i < argc; i++) {
376 hres = to_string(ctx, argv[i], strs+i+1);
382 if(SUCCEEDED(hres)) {
383 for(i=0; i < str_cnt; i++) {
384 len += jsstr_length(strs[i]);
385 if(len > JSSTR_MAX_LENGTH) {
386 hres = E_OUTOFMEMORY;
391 ptr = jsstr_alloc_buf(len, &ret);
393 for(i=0; i < str_cnt; i++)
394 ptr += jsstr_flush(strs[i], ptr);
396 hres = E_OUTOFMEMORY;
400 for(i=0; i < str_cnt; i++)
401 jsstr_release(strs[i]);
408 *r = jsval_string(ret);
414 static HRESULT String_fixed(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
417 static const WCHAR fixedtagW[] = {'T','T',0};
418 return do_attributeless_tag_format(ctx, jsthis, r, fixedtagW);
421 static HRESULT String_fontcolor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
424 static const WCHAR fontW[] = {'F','O','N','T',0};
425 static const WCHAR colorW[] = {'C','O','L','O','R',0};
427 return do_attribute_tag_format(ctx, jsthis, argc, argv, r, fontW, colorW);
430 static HRESULT String_fontsize(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
433 static const WCHAR fontW[] = {'F','O','N','T',0};
434 static const WCHAR colorW[] = {'S','I','Z','E',0};
436 return do_attribute_tag_format(ctx, jsthis, argc, argv, r, fontW, colorW);
439 static HRESULT String_indexOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
442 jsstr_t *search_str, *str;
449 hres = get_string_val(ctx, jsthis, &str);
453 length = jsstr_length(str);
456 *r = jsval_number(-1);
461 hres = to_string(ctx, argv[0], &search_str);
470 hres = to_integer(ctx, argv[1], &d);
471 if(SUCCEEDED(hres) && d > 0.0)
472 pos = is_int32(d) ? min(length, d) : length;
475 if(SUCCEEDED(hres)) {
478 ptr = strstrW(str->str+pos, search_str->str);
480 ret = ptr - str->str;
485 jsstr_release(search_str);
491 *r = jsval_number(ret);
495 static HRESULT String_italics(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
498 static const WCHAR italicstagW[] = {'I',0};
499 return do_attributeless_tag_format(ctx, jsthis, r, italicstagW);
502 /* ECMA-262 3rd Edition 15.5.4.8 */
503 static HRESULT String_lastIndexOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
506 unsigned pos = 0, search_len, length;
507 jsstr_t *search_str, *str;
513 hres = get_string_val(ctx, jsthis, &str);
519 *r = jsval_number(-1);
524 hres = to_string(ctx, argv[0], &search_str);
530 search_len = jsstr_length(search_str);
531 length = jsstr_length(str);
536 hres = to_integer(ctx, argv[1], &d);
537 if(SUCCEEDED(hres) && d > 0)
538 pos = is_int32(d) ? min(length, d) : length;
543 if(SUCCEEDED(hres) && length >= search_len) {
546 for(ptr = str->str+min(pos, length-search_len); ptr >= str->str; ptr--) {
547 if(!memcmp(ptr, search_str->str, search_len*sizeof(WCHAR))) {
554 jsstr_release(search_str);
560 *r = jsval_number(ret);
564 static HRESULT String_link(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
567 static const WCHAR fontW[] = {'A',0};
568 static const WCHAR colorW[] = {'H','R','E','F',0};
570 return do_attribute_tag_format(ctx, jsthis, argc, argv, r, fontW, colorW);
573 /* ECMA-262 3rd Edition 15.5.4.10 */
574 static HRESULT String_match(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
577 jsdisp_t *regexp = NULL;
589 if(is_object_instance(argv[0])) {
590 regexp = iface_to_jsdisp((IUnknown*)get_object(argv[0]));
591 if(regexp && !is_class(regexp, JSCLASS_REGEXP)) {
592 jsdisp_release(regexp);
600 hres = to_string(ctx, argv[0], &match_str);
604 hres = create_regexp(ctx, match_str, 0, ®exp);
605 jsstr_release(match_str);
610 hres = get_string_val(ctx, jsthis, &str);
612 hres = regexp_string_match(ctx, regexp, str, r);
614 jsdisp_release(regexp);
625 static HRESULT strbuf_append(strbuf_t *buf, const WCHAR *str, DWORD len)
630 if(len + buf->len > buf->size) {
634 new_size = buf->size ? buf->size<<1 : 16;
635 if(new_size < buf->len+len)
636 new_size = buf->len+len;
638 new_buf = heap_realloc(buf->buf, new_size*sizeof(WCHAR));
640 new_buf = heap_alloc(new_size*sizeof(WCHAR));
642 return E_OUTOFMEMORY;
645 buf->size = new_size;
648 memcpy(buf->buf+buf->len, str, len*sizeof(WCHAR));
653 static HRESULT rep_call(script_ctx_t *ctx, jsdisp_t *func,
654 jsstr_t *str, match_state_t *match, jsstr_t **ret)
663 argc = match->paren_count+3;
664 argv = heap_alloc_zero(sizeof(*argv)*argc);
666 return E_OUTOFMEMORY;
668 tmp_str = jsstr_alloc_len(match->cp-match->match_len, match->match_len);
670 hres = E_OUTOFMEMORY;
671 argv[0] = jsval_string(tmp_str);
673 if(SUCCEEDED(hres)) {
674 for(i=0; i < match->paren_count; i++) {
675 if(match->parens[i].index != -1)
676 tmp_str = jsstr_substr(str, match->parens[i].index, match->parens[i].length);
678 tmp_str = jsstr_empty();
680 hres = E_OUTOFMEMORY;
683 argv[i+1] = jsval_string(tmp_str);
687 if(SUCCEEDED(hres)) {
688 argv[match->paren_count+1] = jsval_number(match->cp-str->str - match->match_len);
689 argv[match->paren_count+2] = jsval_string(str);
693 hres = jsdisp_call_value(func, NULL, DISPATCH_METHOD, argc, argv, &val);
695 for(i=0; i <= match->paren_count; i++)
696 jsstr_release(get_string(argv[i]));
702 hres = to_string(ctx, val, ret);
707 /* ECMA-262 3rd Edition 15.5.4.11 */
708 static HRESULT String_replace(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
712 jsstr_t *rep_str = NULL, *match_str = NULL, *str;
713 jsdisp_t *rep_func = NULL, *regexp = NULL;
714 match_state_t *match = NULL, last_match = {0};
715 strbuf_t ret = {NULL,0,0};
716 DWORD re_flags = REM_NO_CTX_UPDATE|REM_ALLOC_RESULT;
721 hres = get_string_val(ctx, jsthis, &str);
727 *r = jsval_string(str);
733 if(is_object_instance(argv[0])) {
734 regexp = iface_to_jsdisp((IUnknown*)get_object(argv[0]));
735 if(regexp && !is_class(regexp, JSCLASS_REGEXP)) {
736 jsdisp_release(regexp);
742 hres = to_string(ctx, argv[0], &match_str);
750 if(is_object_instance(argv[1])) {
751 rep_func = iface_to_jsdisp((IUnknown*)get_object(argv[1]));
752 if(rep_func && !is_class(rep_func, JSCLASS_FUNCTION)) {
753 jsdisp_release(rep_func);
759 hres = to_string(ctx, argv[1], &rep_str);
761 rep_len = jsstr_length(rep_str);
765 if(SUCCEEDED(hres)) {
766 const WCHAR *ecp = str->str;
770 hres = regexp_match_next(ctx, regexp, re_flags, str, &match);
771 re_flags = (re_flags | REM_CHECK_GLOBAL) & (~REM_ALLOC_RESULT);
773 if(hres == S_FALSE) {
780 last_match.cp = match->cp;
781 last_match.match_len = match->match_len;
783 if(re_flags & REM_ALLOC_RESULT) {
784 re_flags &= ~REM_ALLOC_RESULT;
786 match->cp = str->str;
789 match->cp = strstrW(match->cp, match_str->str);
792 match->match_len = jsstr_length(match_str);
793 match->cp += match->match_len;
796 hres = strbuf_append(&ret, ecp, match->cp-ecp-match->match_len);
804 hres = rep_call(ctx, rep_func, str, match, &cstr);
808 hres = strbuf_append(&ret, cstr->str, jsstr_length(cstr));
812 }else if(rep_str && regexp) {
813 const WCHAR *ptr = rep_str->str, *ptr2;
815 while((ptr2 = strchrW(ptr, '$'))) {
816 hres = strbuf_append(&ret, ptr, ptr2-ptr);
822 hres = strbuf_append(&ret, ptr2, 1);
826 hres = strbuf_append(&ret, match->cp-match->match_len, match->match_len);
830 hres = strbuf_append(&ret, str->str, match->cp-str->str-match->match_len);
834 hres = strbuf_append(&ret, ecp, (str->str+jsstr_length(str))-ecp);
840 if(!isdigitW(ptr2[1])) {
841 hres = strbuf_append(&ret, ptr2, 1);
847 if(isdigitW(ptr2[2]) && idx*10 + (ptr2[2]-'0') <= match->paren_count) {
848 idx = idx*10 + (ptr[2]-'0');
850 }else if(idx && idx <= match->paren_count) {
853 hres = strbuf_append(&ret, ptr2, 1);
858 if(match->parens[idx-1].index != -1)
859 hres = strbuf_append(&ret, str->str+match->parens[idx-1].index,
860 match->parens[idx-1].length);
869 hres = strbuf_append(&ret, ptr, (rep_str->str+rep_len)-ptr);
873 hres = strbuf_append(&ret, rep_str->str, rep_len);
877 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d'};
879 hres = strbuf_append(&ret, undefinedW, sizeof(undefinedW)/sizeof(WCHAR));
886 else if(!match->match_len)
891 hres = strbuf_append(&ret, ecp, str->str+jsstr_length(str)-ecp);
895 jsdisp_release(rep_func);
897 jsstr_release(rep_str);
899 jsstr_release(match_str);
903 if(SUCCEEDED(hres) && last_match.cp && regexp) {
904 jsstr_release(ctx->last_match);
905 ctx->last_match = jsstr_addref(str);
906 ctx->last_match_index = last_match.cp-str->str-last_match.match_len;
907 ctx->last_match_length = last_match.match_len;
911 jsdisp_release(regexp);
914 if(SUCCEEDED(hres) && r) {
917 ret_str = jsstr_alloc_len(ret.buf, ret.len);
919 return E_OUTOFMEMORY;
921 TRACE("= %s\n", debugstr_jsstr(ret_str));
922 *r = jsval_string(ret_str);
929 static HRESULT String_search(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
932 jsdisp_t *regexp = NULL;
934 match_state_t match, *match_ptr = &match;
939 hres = get_string_val(ctx, jsthis, &str);
950 if(is_object_instance(argv[0])) {
951 regexp = iface_to_jsdisp((IUnknown*)get_object(argv[0]));
952 if(regexp && !is_class(regexp, JSCLASS_REGEXP)) {
953 jsdisp_release(regexp);
959 hres = create_regexp_var(ctx, argv[0], NULL, ®exp);
967 hres = regexp_match_next(ctx, regexp, REM_RESET_INDEX|REM_NO_PARENS, str, &match_ptr);
969 jsdisp_release(regexp);
974 *r = jsval_number(hres == S_OK ? match.cp-match.match_len-str->str : -1);
978 /* ECMA-262 3rd Edition 15.5.4.13 */
979 static HRESULT String_slice(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
982 int start=0, end, length;
989 hres = get_string_val(ctx, jsthis, &str);
993 length = jsstr_length(str);
995 hres = to_integer(ctx, argv[0], &d);
1004 start = length + start;
1007 }else if(start > length) {
1016 hres = to_integer(ctx, argv[1], &d);
1028 }else if(end > length) {
1032 end = d < 0.0 ? 0 : length;
1042 jsstr_t *retstr = jsstr_substr(str, start, end-start);
1045 return E_OUTOFMEMORY;
1048 *r = jsval_string(retstr);
1055 static HRESULT String_small(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1058 static const WCHAR smalltagW[] = {'S','M','A','L','L',0};
1059 return do_attributeless_tag_format(ctx, jsthis, r, smalltagW);
1062 static HRESULT String_split(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1065 match_state_t match_result, *match_ptr = &match_result;
1066 DWORD length, i, match_len = 0;
1067 const WCHAR *ptr, *ptr2;
1068 unsigned limit = UINT32_MAX;
1069 jsdisp_t *array, *regexp = NULL;
1070 jsstr_t *str, *match_str = NULL, *tmp_str;
1075 if(argc != 1 && argc != 2) {
1076 FIXME("unsupported argc %u\n", argc);
1080 hres = get_string_val(ctx, jsthis, &str);
1084 length = jsstr_length(str);
1086 if(argc > 1 && !is_undefined(argv[1])) {
1087 hres = to_uint32(ctx, argv[1], &limit);
1094 if(is_object_instance(argv[0])) {
1095 regexp = iface_to_jsdisp((IUnknown*)get_object(argv[0]));
1097 if(!is_class(regexp, JSCLASS_REGEXP)) {
1098 jsdisp_release(regexp);
1105 hres = to_string(ctx, argv[0], &match_str);
1111 match_len = jsstr_length(match_str);
1113 jsstr_release(match_str);
1118 hres = create_array(ctx, 0, &array);
1120 if(SUCCEEDED(hres)) {
1122 match_result.cp = str->str;
1123 for(i=0; i<limit; i++) {
1125 hres = regexp_match_next(ctx, regexp, REM_NO_PARENS, str, &match_ptr);
1128 ptr2 = match_result.cp - match_result.match_len;
1129 }else if(match_str) {
1130 ptr2 = strstrW(ptr, match_str->str);
1139 tmp_str = jsstr_alloc_len(ptr, ptr2-ptr);
1141 hres = E_OUTOFMEMORY;
1145 hres = jsdisp_propput_idx(array, i, jsval_string(tmp_str));
1146 jsstr_release(tmp_str);
1151 ptr = match_result.cp;
1153 ptr = ptr2 + match_len;
1159 if(SUCCEEDED(hres) && (match_str || regexp) && i<limit) {
1160 DWORD len = (str->str+length) - ptr;
1162 if(len || match_str) {
1163 tmp_str = jsstr_alloc_len(ptr, len);
1166 hres = jsdisp_propput_idx(array, i, jsval_string(tmp_str));
1167 jsstr_release(tmp_str);
1169 hres = E_OUTOFMEMORY;
1175 jsdisp_release(regexp);
1177 jsstr_release(match_str);
1180 if(SUCCEEDED(hres) && r)
1181 *r = jsval_obj(array);
1183 jsdisp_release(array);
1188 static HRESULT String_strike(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1191 static const WCHAR striketagW[] = {'S','T','R','I','K','E',0};
1192 return do_attributeless_tag_format(ctx, jsthis, r, striketagW);
1195 static HRESULT String_sub(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1198 static const WCHAR subtagW[] = {'S','U','B',0};
1199 return do_attributeless_tag_format(ctx, jsthis, r, subtagW);
1202 /* ECMA-262 3rd Edition 15.5.4.15 */
1203 static HRESULT String_substring(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1206 INT start=0, end, length;
1213 hres = get_string_val(ctx, jsthis, &str);
1217 length = jsstr_length(str);
1219 hres = to_integer(ctx, argv[0], &d);
1226 start = is_int32(d) ? min(length, d) : length;
1230 hres = to_integer(ctx, argv[1], &d);
1237 end = is_int32(d) ? min(length, d) : length;
1251 jsstr_t *ret = jsstr_substr(str, start, end-start);
1253 *r = jsval_string(ret);
1255 hres = E_OUTOFMEMORY;
1261 /* ECMA-262 3rd Edition B.2.3 */
1262 static HRESULT String_substr(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1265 int start=0, len, length;
1272 hres = get_string_val(ctx, jsthis, &str);
1276 length = jsstr_length(str);
1278 hres = to_integer(ctx, argv[0], &d);
1285 start = is_int32(d) ? min(length, d) : length;
1289 hres = to_integer(ctx, argv[1], &d);
1296 len = is_int32(d) ? min(length-start, d) : length-start;
1305 jsstr_t *ret = jsstr_substr(str, start, len);
1307 *r = jsval_string(ret);
1309 hres = E_OUTOFMEMORY;
1316 static HRESULT String_sup(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1319 static const WCHAR suptagW[] = {'S','U','P',0};
1320 return do_attributeless_tag_format(ctx, jsthis, r, suptagW);
1323 static HRESULT String_toLowerCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1331 hres = get_string_val(ctx, jsthis, &str);
1339 buf = jsstr_alloc_buf(jsstr_length(str), &ret);
1342 return E_OUTOFMEMORY;
1345 jsstr_flush(str, buf);
1347 *r = jsval_string(ret);
1353 static HRESULT String_toUpperCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1361 hres = get_string_val(ctx, jsthis, &str);
1369 buf = jsstr_alloc_buf(jsstr_length(str), &ret);
1372 return E_OUTOFMEMORY;
1375 jsstr_flush(str, buf);
1377 *r = jsval_string(ret);
1383 static HRESULT String_toLocaleLowerCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1390 static HRESULT String_toLocaleUpperCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1397 static HRESULT String_localeCompare(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1404 static HRESULT String_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1407 StringInstance *This = string_from_vdisp(jsthis);
1413 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
1414 case DISPATCH_PROPERTYGET: {
1415 *r = jsval_string(jsstr_addref(This->str));
1419 FIXME("flags %x\n", flags);
1426 static void String_destructor(jsdisp_t *dispex)
1428 StringInstance *This = (StringInstance*)dispex;
1430 jsstr_release(This->str);
1434 static unsigned String_idx_length(jsdisp_t *jsdisp)
1436 StringInstance *string = (StringInstance*)jsdisp;
1439 * NOTE: For invoke version < 2, indexed array is not implemented at all.
1440 * Newer jscript.dll versions implement it on string type, not class,
1441 * which is not how it should work according to spec. IE9 implements it
1442 * properly, but it uses its own JavaScript engine inside MSHTML. We
1443 * implement it here, but in the way IE9 and spec work.
1445 return string->dispex.ctx->version < 2 ? 0 : jsstr_length(string->str);
1448 static HRESULT String_idx_get(jsdisp_t *jsdisp, unsigned idx, jsval_t *r)
1450 StringInstance *string = (StringInstance*)jsdisp;
1453 ret = jsstr_substr(string->str, idx, 1);
1455 return E_OUTOFMEMORY;
1457 TRACE("%p[%u] = %s\n", string, idx, debugstr_jsstr(ret));
1459 *r = jsval_string(ret);
1463 static const builtin_prop_t String_props[] = {
1464 {anchorW, String_anchor, PROPF_METHOD|1},
1465 {bigW, String_big, PROPF_METHOD},
1466 {blinkW, String_blink, PROPF_METHOD},
1467 {boldW, String_bold, PROPF_METHOD},
1468 {charAtW, String_charAt, PROPF_METHOD|1},
1469 {charCodeAtW, String_charCodeAt, PROPF_METHOD|1},
1470 {concatW, String_concat, PROPF_METHOD|1},
1471 {fixedW, String_fixed, PROPF_METHOD},
1472 {fontcolorW, String_fontcolor, PROPF_METHOD|1},
1473 {fontsizeW, String_fontsize, PROPF_METHOD|1},
1474 {indexOfW, String_indexOf, PROPF_METHOD|2},
1475 {italicsW, String_italics, PROPF_METHOD},
1476 {lastIndexOfW, String_lastIndexOf, PROPF_METHOD|2},
1477 {lengthW, String_length, 0},
1478 {linkW, String_link, PROPF_METHOD|1},
1479 {localeCompareW, String_localeCompare, PROPF_METHOD|1},
1480 {matchW, String_match, PROPF_METHOD|1},
1481 {replaceW, String_replace, PROPF_METHOD|1},
1482 {searchW, String_search, PROPF_METHOD},
1483 {sliceW, String_slice, PROPF_METHOD},
1484 {smallW, String_small, PROPF_METHOD},
1485 {splitW, String_split, PROPF_METHOD|2},
1486 {strikeW, String_strike, PROPF_METHOD},
1487 {subW, String_sub, PROPF_METHOD},
1488 {substrW, String_substr, PROPF_METHOD|2},
1489 {substringW, String_substring, PROPF_METHOD|2},
1490 {supW, String_sup, PROPF_METHOD},
1491 {toLocaleLowerCaseW, String_toLocaleLowerCase, PROPF_METHOD},
1492 {toLocaleUpperCaseW, String_toLocaleUpperCase, PROPF_METHOD},
1493 {toLowerCaseW, String_toLowerCase, PROPF_METHOD},
1494 {toStringW, String_toString, PROPF_METHOD},
1495 {toUpperCaseW, String_toUpperCase, PROPF_METHOD},
1496 {valueOfW, String_valueOf, PROPF_METHOD}
1499 static const builtin_info_t String_info = {
1501 {NULL, String_value, 0},
1502 sizeof(String_props)/sizeof(*String_props),
1508 static const builtin_prop_t StringInst_props[] = {
1509 {lengthW, String_length, 0}
1512 static const builtin_info_t StringInst_info = {
1514 {NULL, String_value, 0},
1515 sizeof(StringInst_props)/sizeof(*StringInst_props),
1523 /* ECMA-262 3rd Edition 15.5.3.2 */
1524 static HRESULT StringConstr_fromCharCode(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
1525 unsigned argc, jsval_t *argv, jsval_t *r)
1534 ret_str = jsstr_alloc_buf(argc, &ret);
1536 return E_OUTOFMEMORY;
1538 for(i=0; i<argc; i++) {
1539 hres = to_uint32(ctx, argv[i], &code);
1549 *r = jsval_string(ret);
1555 static HRESULT StringConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1567 hres = to_string(ctx, argv[0], &str);
1571 str = jsstr_empty();
1574 *r = jsval_string(str);
1577 case DISPATCH_CONSTRUCT: {
1583 hres = to_string(ctx, argv[0], &str);
1587 hres = create_string(ctx, str, &ret);
1590 hres = create_string(ctx, jsstr_empty(), &ret);
1596 *r = jsval_obj(ret);
1601 FIXME("unimplemented flags: %x\n", flags);
1608 static HRESULT string_alloc(script_ctx_t *ctx, jsdisp_t *object_prototype, jsstr_t *str, StringInstance **ret)
1610 StringInstance *string;
1613 string = heap_alloc_zero(sizeof(StringInstance));
1615 return E_OUTOFMEMORY;
1617 if(object_prototype)
1618 hres = init_dispex(&string->dispex, ctx, &String_info, object_prototype);
1620 hres = init_dispex_from_constr(&string->dispex, ctx, &StringInst_info, ctx->string_constr);
1626 string->str = jsstr_addref(str);
1631 static const builtin_prop_t StringConstr_props[] = {
1632 {fromCharCodeW, StringConstr_fromCharCode, PROPF_METHOD},
1635 static const builtin_info_t StringConstr_info = {
1637 {NULL, Function_value, 0},
1638 sizeof(StringConstr_props)/sizeof(*StringConstr_props),
1644 HRESULT create_string_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1646 StringInstance *string;
1649 static const WCHAR StringW[] = {'S','t','r','i','n','g',0};
1651 hres = string_alloc(ctx, object_prototype, jsstr_empty(), &string);
1655 hres = create_builtin_constructor(ctx, StringConstr_value, StringW, &StringConstr_info,
1656 PROPF_CONSTR|1, &string->dispex, ret);
1658 jsdisp_release(&string->dispex);
1662 HRESULT create_string(script_ctx_t *ctx, jsstr_t *str, jsdisp_t **ret)
1664 StringInstance *string;
1667 hres = string_alloc(ctx, NULL, str, &string);
1671 *ret = &string->dispex;