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 get_string_flat_val(script_ctx_t *ctx, vdisp_t *jsthis, jsstr_t **jsval, const WCHAR **val)
97 hres = get_string_val(ctx, jsthis, jsval);
101 *val = jsstr_flatten(*jsval);
105 jsstr_release(*jsval);
106 return E_OUTOFMEMORY;
109 static HRESULT String_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
112 TRACE("%p\n", jsthis);
115 case DISPATCH_PROPERTYGET: {
116 StringInstance *string = string_from_vdisp(jsthis);
118 *r = jsval_number(jsstr_length(string->str));
122 FIXME("unimplemented flags %x\n", flags);
129 static HRESULT stringobj_to_string(vdisp_t *jsthis, jsval_t *r)
131 StringInstance *string;
133 if(!(string = string_this(jsthis))) {
134 WARN("this is not a string object\n");
139 *r = jsval_string(jsstr_addref(string->str));
143 /* ECMA-262 3rd Edition 15.5.4.2 */
144 static HRESULT String_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
149 return stringobj_to_string(jsthis, r);
152 /* ECMA-262 3rd Edition 15.5.4.2 */
153 static HRESULT String_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
158 return stringobj_to_string(jsthis, r);
161 static HRESULT do_attributeless_tag_format(script_ctx_t *ctx, vdisp_t *jsthis, jsval_t *r, const WCHAR *tagname)
163 unsigned tagname_len;
168 hres = get_string_val(ctx, jsthis, &str);
177 tagname_len = strlenW(tagname);
179 ptr = jsstr_alloc_buf(jsstr_length(str) + 2*tagname_len + 5, &ret);
182 return E_OUTOFMEMORY;
186 memcpy(ptr, tagname, tagname_len*sizeof(WCHAR));
190 ptr += jsstr_flush(str, ptr);
195 memcpy(ptr, tagname, tagname_len*sizeof(WCHAR));
199 *r = jsval_string(ret);
203 static HRESULT do_attribute_tag_format(script_ctx_t *ctx, vdisp_t *jsthis, unsigned argc, jsval_t *argv, jsval_t *r,
204 const WCHAR *tagname, const WCHAR *attrname)
206 jsstr_t *str, *attr_value = NULL;
209 hres = get_string_val(ctx, jsthis, &str);
214 hres = to_string(ctx, argv[0], &attr_value);
220 attr_value = jsstr_undefined();
224 unsigned attrname_len = strlenW(attrname);
225 unsigned tagname_len = strlenW(tagname);
229 ptr = jsstr_alloc_buf(2*tagname_len + attrname_len + jsstr_length(attr_value) + jsstr_length(str) + 9, &ret);
232 memcpy(ptr, tagname, tagname_len*sizeof(WCHAR));
235 memcpy(ptr, attrname, attrname_len*sizeof(WCHAR));
239 ptr += jsstr_flush(attr_value, ptr);
242 ptr += jsstr_flush(str, ptr);
246 memcpy(ptr, tagname, tagname_len*sizeof(WCHAR));
250 *r = jsval_string(ret);
252 hres = E_OUTOFMEMORY;
256 jsstr_release(attr_value);
261 static HRESULT String_anchor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
264 static const WCHAR fontW[] = {'A',0};
265 static const WCHAR colorW[] = {'N','A','M','E',0};
267 return do_attribute_tag_format(ctx, jsthis, argc, argv, r, fontW, colorW);
270 static HRESULT String_big(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
273 static const WCHAR bigtagW[] = {'B','I','G',0};
274 return do_attributeless_tag_format(ctx, jsthis, r, bigtagW);
277 static HRESULT String_blink(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
280 static const WCHAR blinktagW[] = {'B','L','I','N','K',0};
281 return do_attributeless_tag_format(ctx, jsthis, r, blinktagW);
284 static HRESULT String_bold(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
287 static const WCHAR boldtagW[] = {'B',0};
288 return do_attributeless_tag_format(ctx, jsthis, r, boldtagW);
291 /* ECMA-262 3rd Edition 15.5.4.5 */
292 static HRESULT String_charAt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
301 hres = get_string_val(ctx, jsthis, &str);
308 hres = to_integer(ctx, argv[0], &d);
313 pos = is_int32(d) ? d : -1;
321 if(0 <= pos && pos < jsstr_length(str)) {
322 ret = jsstr_substr(str, pos, 1);
324 return E_OUTOFMEMORY;
329 *r = jsval_string(ret);
333 /* ECMA-262 3rd Edition 15.5.4.5 */
334 static HRESULT String_charCodeAt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
343 hres = get_string_val(ctx, jsthis, &str);
350 hres = to_integer(ctx, argv[0], &d);
356 if(!is_int32(d) || d < 0 || d >= jsstr_length(str)) {
359 *r = jsval_number(NAN);
368 jsstr_extract(str, idx, 1, &c);
369 *r = jsval_number(c);
376 /* ECMA-262 3rd Edition 15.5.4.6 */
377 static HRESULT String_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
380 unsigned len = 0, i, str_cnt;
381 jsstr_t **strs, *ret = NULL;
388 strs = heap_alloc_zero(str_cnt * sizeof(*strs));
390 return E_OUTOFMEMORY;
392 hres = to_string(ctx, jsval_disp(jsthis->u.disp), strs);
393 if(SUCCEEDED(hres)) {
394 for(i=0; i < argc; i++) {
395 hres = to_string(ctx, argv[i], strs+i+1);
401 if(SUCCEEDED(hres)) {
402 for(i=0; i < str_cnt; i++) {
403 len += jsstr_length(strs[i]);
404 if(len > JSSTR_MAX_LENGTH) {
405 hres = E_OUTOFMEMORY;
410 ptr = jsstr_alloc_buf(len, &ret);
412 for(i=0; i < str_cnt; i++)
413 ptr += jsstr_flush(strs[i], ptr);
415 hres = E_OUTOFMEMORY;
419 for(i=0; i < str_cnt; i++)
420 jsstr_release(strs[i]);
427 *r = jsval_string(ret);
433 static HRESULT String_fixed(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
436 static const WCHAR fixedtagW[] = {'T','T',0};
437 return do_attributeless_tag_format(ctx, jsthis, r, fixedtagW);
440 static HRESULT String_fontcolor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
443 static const WCHAR fontW[] = {'F','O','N','T',0};
444 static const WCHAR colorW[] = {'C','O','L','O','R',0};
446 return do_attribute_tag_format(ctx, jsthis, argc, argv, r, fontW, colorW);
449 static HRESULT String_fontsize(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
452 static const WCHAR fontW[] = {'F','O','N','T',0};
453 static const WCHAR colorW[] = {'S','I','Z','E',0};
455 return do_attribute_tag_format(ctx, jsthis, argc, argv, r, fontW, colorW);
458 static HRESULT String_indexOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
461 jsstr_t *search_jsstr, *jsstr;
462 const WCHAR *search_str, *str;
469 hres = get_string_flat_val(ctx, jsthis, &jsstr, &str);
473 length = jsstr_length(jsstr);
476 *r = jsval_number(-1);
477 jsstr_release(jsstr);
481 hres = to_flat_string(ctx, argv[0], &search_jsstr, &search_str);
483 jsstr_release(jsstr);
490 hres = to_integer(ctx, argv[1], &d);
491 if(SUCCEEDED(hres) && d > 0.0)
492 pos = is_int32(d) ? min(length, d) : length;
495 if(SUCCEEDED(hres)) {
498 ptr = strstrW(str+pos, search_str);
505 jsstr_release(search_jsstr);
506 jsstr_release(jsstr);
511 *r = jsval_number(ret);
515 static HRESULT String_italics(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
518 static const WCHAR italicstagW[] = {'I',0};
519 return do_attributeless_tag_format(ctx, jsthis, r, italicstagW);
522 /* ECMA-262 3rd Edition 15.5.4.8 */
523 static HRESULT String_lastIndexOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
526 unsigned pos = 0, search_len, length;
527 jsstr_t *search_jsstr, *jsstr;
528 const WCHAR *search_str, *str;
534 hres = get_string_flat_val(ctx, jsthis, &jsstr, &str);
540 *r = jsval_number(-1);
541 jsstr_release(jsstr);
545 hres = to_flat_string(ctx, argv[0], &search_jsstr, &search_str);
547 jsstr_release(jsstr);
551 search_len = jsstr_length(search_jsstr);
552 length = jsstr_length(jsstr);
557 hres = to_integer(ctx, argv[1], &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 jsstr_release(search_jsstr);
576 jsstr_release(jsstr);
581 *r = jsval_number(ret);
585 static HRESULT String_link(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
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, argc, argv, r, 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, unsigned argc, jsval_t *argv,
598 jsdisp_t *regexp = NULL;
610 if(is_object_instance(argv[0])) {
611 regexp = iface_to_jsdisp((IUnknown*)get_object(argv[0]));
612 if(regexp && !is_class(regexp, JSCLASS_REGEXP)) {
613 jsdisp_release(regexp);
621 hres = to_string(ctx, argv[0], &match_str);
625 hres = create_regexp(ctx, match_str, 0, ®exp);
626 jsstr_release(match_str);
631 hres = get_string_val(ctx, jsthis, &str);
633 hres = regexp_string_match(ctx, regexp, str, r);
635 jsdisp_release(regexp);
646 static BOOL strbuf_ensure_size(strbuf_t *buf, unsigned len)
654 new_size = buf->size ? buf->size<<1 : 16;
658 new_buf = heap_realloc(buf->buf, new_size*sizeof(WCHAR));
660 new_buf = heap_alloc(new_size*sizeof(WCHAR));
665 buf->size = new_size;
669 static HRESULT strbuf_append(strbuf_t *buf, const WCHAR *str, DWORD len)
674 if(!strbuf_ensure_size(buf, buf->len+len))
675 return E_OUTOFMEMORY;
677 memcpy(buf->buf+buf->len, str, len*sizeof(WCHAR));
682 static HRESULT strbuf_append_jsstr(strbuf_t *buf, jsstr_t *str)
684 if(!strbuf_ensure_size(buf, buf->len+jsstr_length(str)))
685 return E_OUTOFMEMORY;
687 jsstr_flush(str, buf->buf+buf->len);
688 buf->len += jsstr_length(str);
692 static HRESULT rep_call(script_ctx_t *ctx, jsdisp_t *func,
693 jsstr_t *jsstr, const WCHAR *str, match_state_t *match, jsstr_t **ret)
702 argc = match->paren_count+3;
703 argv = heap_alloc_zero(sizeof(*argv)*argc);
705 return E_OUTOFMEMORY;
707 tmp_str = jsstr_alloc_len(match->cp-match->match_len, match->match_len);
709 hres = E_OUTOFMEMORY;
710 argv[0] = jsval_string(tmp_str);
712 if(SUCCEEDED(hres)) {
713 for(i=0; i < match->paren_count; i++) {
714 if(match->parens[i].index != -1)
715 tmp_str = jsstr_substr(jsstr, match->parens[i].index, match->parens[i].length);
717 tmp_str = jsstr_empty();
719 hres = E_OUTOFMEMORY;
722 argv[i+1] = jsval_string(tmp_str);
726 if(SUCCEEDED(hres)) {
727 argv[match->paren_count+1] = jsval_number(match->cp-str - match->match_len);
728 argv[match->paren_count+2] = jsval_string(jsstr);
732 hres = jsdisp_call_value(func, NULL, DISPATCH_METHOD, argc, argv, &val);
734 for(i=0; i <= match->paren_count; i++)
735 jsstr_release(get_string(argv[i]));
741 hres = to_string(ctx, val, ret);
746 /* ECMA-262 3rd Edition 15.5.4.11 */
747 static HRESULT String_replace(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
750 const WCHAR *str, *match_str = NULL, *rep_str = NULL;
751 jsstr_t *rep_jsstr, *match_jsstr, *jsstr;
752 jsdisp_t *rep_func = NULL, *regexp = NULL;
753 match_state_t *match = NULL, last_match = {0};
754 strbuf_t ret = {NULL,0,0};
755 DWORD re_flags = REM_NO_CTX_UPDATE|REM_ALLOC_RESULT;
761 hres = get_string_flat_val(ctx, jsthis, &jsstr, &str);
767 *r = jsval_string(jsstr);
769 jsstr_release(jsstr);
773 if(is_object_instance(argv[0])) {
774 regexp = iface_to_jsdisp((IUnknown*)get_object(argv[0]));
775 if(regexp && !is_class(regexp, JSCLASS_REGEXP)) {
776 jsdisp_release(regexp);
782 hres = to_flat_string(ctx, argv[0], &match_jsstr, &match_str);
784 jsstr_release(jsstr);
790 if(is_object_instance(argv[1])) {
791 rep_func = iface_to_jsdisp((IUnknown*)get_object(argv[1]));
792 if(rep_func && !is_class(rep_func, JSCLASS_FUNCTION)) {
793 jsdisp_release(rep_func);
799 hres = to_flat_string(ctx, argv[1], &rep_jsstr, &rep_str);
801 rep_len = jsstr_length(rep_jsstr);
805 if(SUCCEEDED(hres)) {
806 const WCHAR *ecp = str;
810 hres = regexp_match_next(ctx, regexp, re_flags, jsstr, &match);
811 re_flags = (re_flags | REM_CHECK_GLOBAL) & (~REM_ALLOC_RESULT);
813 if(hres == S_FALSE) {
820 last_match.cp = match->cp;
821 last_match.match_len = match->match_len;
823 if(re_flags & REM_ALLOC_RESULT) {
824 re_flags &= ~REM_ALLOC_RESULT;
829 match->cp = strstrW(match->cp, match_str);
832 match->match_len = jsstr_length(match_jsstr);
833 match->cp += match->match_len;
836 hres = strbuf_append(&ret, ecp, match->cp-ecp-match->match_len);
844 hres = rep_call(ctx, rep_func, jsstr, str, match, &cstr);
848 hres = strbuf_append_jsstr(&ret, cstr);
852 }else if(rep_str && regexp) {
853 const WCHAR *ptr = rep_str, *ptr2;
855 while((ptr2 = strchrW(ptr, '$'))) {
856 hres = strbuf_append(&ret, ptr, ptr2-ptr);
862 hres = strbuf_append(&ret, ptr2, 1);
866 hres = strbuf_append(&ret, match->cp-match->match_len, match->match_len);
870 hres = strbuf_append(&ret, str, match->cp-str-match->match_len);
874 hres = strbuf_append(&ret, ecp, (str+jsstr_length(jsstr))-ecp);
880 if(!isdigitW(ptr2[1])) {
881 hres = strbuf_append(&ret, ptr2, 1);
887 if(isdigitW(ptr2[2]) && idx*10 + (ptr2[2]-'0') <= match->paren_count) {
888 idx = idx*10 + (ptr[2]-'0');
890 }else if(idx && idx <= match->paren_count) {
893 hres = strbuf_append(&ret, ptr2, 1);
898 if(match->parens[idx-1].index != -1)
899 hres = strbuf_append(&ret, str+match->parens[idx-1].index,
900 match->parens[idx-1].length);
909 hres = strbuf_append(&ret, ptr, (rep_str+rep_len)-ptr);
913 hres = strbuf_append(&ret, rep_str, rep_len);
917 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d'};
919 hres = strbuf_append(&ret, undefinedW, sizeof(undefinedW)/sizeof(WCHAR));
926 else if(!match->match_len)
931 hres = strbuf_append(&ret, ecp, str+jsstr_length(jsstr)-ecp);
935 jsdisp_release(rep_func);
937 jsstr_release(rep_jsstr);
939 jsstr_release(match_jsstr);
943 if(SUCCEEDED(hres) && last_match.cp && regexp) {
944 jsstr_release(ctx->last_match);
945 ctx->last_match = jsstr_addref(jsstr);
946 ctx->last_match_index = last_match.cp-str-last_match.match_len;
947 ctx->last_match_length = last_match.match_len;
951 jsdisp_release(regexp);
952 jsstr_release(jsstr);
954 if(SUCCEEDED(hres) && r) {
957 ret_str = jsstr_alloc_len(ret.buf, ret.len);
959 return E_OUTOFMEMORY;
961 TRACE("= %s\n", debugstr_jsstr(ret_str));
962 *r = jsval_string(ret_str);
969 static HRESULT String_search(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
972 jsdisp_t *regexp = NULL;
975 match_state_t match, *match_ptr = &match;
980 hres = get_string_flat_val(ctx, jsthis, &jsstr, &str);
987 jsstr_release(jsstr);
991 if(is_object_instance(argv[0])) {
992 regexp = iface_to_jsdisp((IUnknown*)get_object(argv[0]));
993 if(regexp && !is_class(regexp, JSCLASS_REGEXP)) {
994 jsdisp_release(regexp);
1000 hres = create_regexp_var(ctx, argv[0], NULL, ®exp);
1002 jsstr_release(jsstr);
1008 hres = regexp_match_next(ctx, regexp, REM_RESET_INDEX|REM_NO_PARENS, jsstr, &match_ptr);
1009 jsstr_release(jsstr);
1010 jsdisp_release(regexp);
1015 *r = jsval_number(hres == S_OK ? match.cp-match.match_len-str : -1);
1019 /* ECMA-262 3rd Edition 15.5.4.13 */
1020 static HRESULT String_slice(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1023 int start=0, end, length;
1030 hres = get_string_val(ctx, jsthis, &str);
1034 length = jsstr_length(str);
1036 hres = to_integer(ctx, argv[0], &d);
1045 start = length + start;
1048 }else if(start > length) {
1057 hres = to_integer(ctx, argv[1], &d);
1069 }else if(end > length) {
1073 end = d < 0.0 ? 0 : length;
1083 jsstr_t *retstr = jsstr_substr(str, start, end-start);
1086 return E_OUTOFMEMORY;
1089 *r = jsval_string(retstr);
1096 static HRESULT String_small(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1099 static const WCHAR smalltagW[] = {'S','M','A','L','L',0};
1100 return do_attributeless_tag_format(ctx, jsthis, r, smalltagW);
1103 static HRESULT String_split(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1106 match_state_t match_result, *match_ptr = &match_result;
1107 DWORD length, i, match_len = 0;
1108 const WCHAR *ptr, *ptr2, *str, *match_str = NULL;
1109 unsigned limit = UINT32_MAX;
1110 jsdisp_t *array, *regexp = NULL;
1111 jsstr_t *jsstr, *match_jsstr, *tmp_str;
1116 if(argc != 1 && argc != 2) {
1117 FIXME("unsupported argc %u\n", argc);
1121 hres = get_string_flat_val(ctx, jsthis, &jsstr, &str);
1125 length = jsstr_length(jsstr);
1127 if(argc > 1 && !is_undefined(argv[1])) {
1128 hres = to_uint32(ctx, argv[1], &limit);
1130 jsstr_release(jsstr);
1135 if(is_object_instance(argv[0])) {
1136 regexp = iface_to_jsdisp((IUnknown*)get_object(argv[0]));
1138 if(!is_class(regexp, JSCLASS_REGEXP)) {
1139 jsdisp_release(regexp);
1146 hres = to_flat_string(ctx, argv[0], &match_jsstr, &match_str);
1148 jsstr_release(jsstr);
1152 match_len = jsstr_length(match_jsstr);
1154 jsstr_release(match_jsstr);
1159 hres = create_array(ctx, 0, &array);
1161 if(SUCCEEDED(hres)) {
1163 match_result.cp = str;
1164 for(i=0; i<limit; i++) {
1166 hres = regexp_match_next(ctx, regexp, REM_NO_PARENS, jsstr, &match_ptr);
1169 ptr2 = match_result.cp - match_result.match_len;
1170 }else if(match_str) {
1171 ptr2 = strstrW(ptr, match_str);
1180 tmp_str = jsstr_alloc_len(ptr, ptr2-ptr);
1182 hres = E_OUTOFMEMORY;
1186 hres = jsdisp_propput_idx(array, i, jsval_string(tmp_str));
1187 jsstr_release(tmp_str);
1192 ptr = match_result.cp;
1194 ptr = ptr2 + match_len;
1200 if(SUCCEEDED(hres) && (match_str || regexp) && i<limit) {
1201 DWORD len = (str+length) - ptr;
1203 if(len || match_str) {
1204 tmp_str = jsstr_alloc_len(ptr, len);
1207 hres = jsdisp_propput_idx(array, i, jsval_string(tmp_str));
1208 jsstr_release(tmp_str);
1210 hres = E_OUTOFMEMORY;
1216 jsdisp_release(regexp);
1218 jsstr_release(match_jsstr);
1219 jsstr_release(jsstr);
1221 if(SUCCEEDED(hres) && r)
1222 *r = jsval_obj(array);
1224 jsdisp_release(array);
1229 static HRESULT String_strike(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1232 static const WCHAR striketagW[] = {'S','T','R','I','K','E',0};
1233 return do_attributeless_tag_format(ctx, jsthis, r, striketagW);
1236 static HRESULT String_sub(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1239 static const WCHAR subtagW[] = {'S','U','B',0};
1240 return do_attributeless_tag_format(ctx, jsthis, r, subtagW);
1243 /* ECMA-262 3rd Edition 15.5.4.15 */
1244 static HRESULT String_substring(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1247 INT start=0, end, length;
1254 hres = get_string_val(ctx, jsthis, &str);
1258 length = jsstr_length(str);
1260 hres = to_integer(ctx, argv[0], &d);
1267 start = is_int32(d) ? min(length, d) : length;
1271 hres = to_integer(ctx, argv[1], &d);
1278 end = is_int32(d) ? min(length, d) : length;
1292 jsstr_t *ret = jsstr_substr(str, start, end-start);
1294 *r = jsval_string(ret);
1296 hres = E_OUTOFMEMORY;
1302 /* ECMA-262 3rd Edition B.2.3 */
1303 static HRESULT String_substr(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1306 int start=0, len, length;
1313 hres = get_string_val(ctx, jsthis, &str);
1317 length = jsstr_length(str);
1319 hres = to_integer(ctx, argv[0], &d);
1326 start = is_int32(d) ? min(length, d) : length;
1330 hres = to_integer(ctx, argv[1], &d);
1337 len = is_int32(d) ? min(length-start, d) : length-start;
1346 jsstr_t *ret = jsstr_substr(str, start, len);
1348 *r = jsval_string(ret);
1350 hres = E_OUTOFMEMORY;
1357 static HRESULT String_sup(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1360 static const WCHAR suptagW[] = {'S','U','P',0};
1361 return do_attributeless_tag_format(ctx, jsthis, r, suptagW);
1364 static HRESULT String_toLowerCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1372 hres = get_string_val(ctx, jsthis, &str);
1380 buf = jsstr_alloc_buf(jsstr_length(str), &ret);
1383 return E_OUTOFMEMORY;
1386 jsstr_flush(str, buf);
1388 *r = jsval_string(ret);
1394 static HRESULT String_toUpperCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1402 hres = get_string_val(ctx, jsthis, &str);
1410 buf = jsstr_alloc_buf(jsstr_length(str), &ret);
1413 return E_OUTOFMEMORY;
1416 jsstr_flush(str, buf);
1418 *r = jsval_string(ret);
1424 static HRESULT String_toLocaleLowerCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1431 static HRESULT String_toLocaleUpperCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1438 static HRESULT String_localeCompare(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1445 static HRESULT String_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1448 StringInstance *This = string_from_vdisp(jsthis);
1454 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
1455 case DISPATCH_PROPERTYGET: {
1456 *r = jsval_string(jsstr_addref(This->str));
1460 FIXME("flags %x\n", flags);
1467 static void String_destructor(jsdisp_t *dispex)
1469 StringInstance *This = (StringInstance*)dispex;
1471 jsstr_release(This->str);
1475 static unsigned String_idx_length(jsdisp_t *jsdisp)
1477 StringInstance *string = (StringInstance*)jsdisp;
1480 * NOTE: For invoke version < 2, indexed array is not implemented at all.
1481 * Newer jscript.dll versions implement it on string type, not class,
1482 * which is not how it should work according to spec. IE9 implements it
1483 * properly, but it uses its own JavaScript engine inside MSHTML. We
1484 * implement it here, but in the way IE9 and spec work.
1486 return string->dispex.ctx->version < 2 ? 0 : jsstr_length(string->str);
1489 static HRESULT String_idx_get(jsdisp_t *jsdisp, unsigned idx, jsval_t *r)
1491 StringInstance *string = (StringInstance*)jsdisp;
1494 ret = jsstr_substr(string->str, idx, 1);
1496 return E_OUTOFMEMORY;
1498 TRACE("%p[%u] = %s\n", string, idx, debugstr_jsstr(ret));
1500 *r = jsval_string(ret);
1504 static const builtin_prop_t String_props[] = {
1505 {anchorW, String_anchor, PROPF_METHOD|1},
1506 {bigW, String_big, PROPF_METHOD},
1507 {blinkW, String_blink, PROPF_METHOD},
1508 {boldW, String_bold, PROPF_METHOD},
1509 {charAtW, String_charAt, PROPF_METHOD|1},
1510 {charCodeAtW, String_charCodeAt, PROPF_METHOD|1},
1511 {concatW, String_concat, PROPF_METHOD|1},
1512 {fixedW, String_fixed, PROPF_METHOD},
1513 {fontcolorW, String_fontcolor, PROPF_METHOD|1},
1514 {fontsizeW, String_fontsize, PROPF_METHOD|1},
1515 {indexOfW, String_indexOf, PROPF_METHOD|2},
1516 {italicsW, String_italics, PROPF_METHOD},
1517 {lastIndexOfW, String_lastIndexOf, PROPF_METHOD|2},
1518 {lengthW, String_length, 0},
1519 {linkW, String_link, PROPF_METHOD|1},
1520 {localeCompareW, String_localeCompare, PROPF_METHOD|1},
1521 {matchW, String_match, PROPF_METHOD|1},
1522 {replaceW, String_replace, PROPF_METHOD|1},
1523 {searchW, String_search, PROPF_METHOD},
1524 {sliceW, String_slice, PROPF_METHOD},
1525 {smallW, String_small, PROPF_METHOD},
1526 {splitW, String_split, PROPF_METHOD|2},
1527 {strikeW, String_strike, PROPF_METHOD},
1528 {subW, String_sub, PROPF_METHOD},
1529 {substrW, String_substr, PROPF_METHOD|2},
1530 {substringW, String_substring, PROPF_METHOD|2},
1531 {supW, String_sup, PROPF_METHOD},
1532 {toLocaleLowerCaseW, String_toLocaleLowerCase, PROPF_METHOD},
1533 {toLocaleUpperCaseW, String_toLocaleUpperCase, PROPF_METHOD},
1534 {toLowerCaseW, String_toLowerCase, PROPF_METHOD},
1535 {toStringW, String_toString, PROPF_METHOD},
1536 {toUpperCaseW, String_toUpperCase, PROPF_METHOD},
1537 {valueOfW, String_valueOf, PROPF_METHOD}
1540 static const builtin_info_t String_info = {
1542 {NULL, String_value, 0},
1543 sizeof(String_props)/sizeof(*String_props),
1549 static const builtin_prop_t StringInst_props[] = {
1550 {lengthW, String_length, 0}
1553 static const builtin_info_t StringInst_info = {
1555 {NULL, String_value, 0},
1556 sizeof(StringInst_props)/sizeof(*StringInst_props),
1564 /* ECMA-262 3rd Edition 15.5.3.2 */
1565 static HRESULT StringConstr_fromCharCode(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
1566 unsigned argc, jsval_t *argv, jsval_t *r)
1575 ret_str = jsstr_alloc_buf(argc, &ret);
1577 return E_OUTOFMEMORY;
1579 for(i=0; i<argc; i++) {
1580 hres = to_uint32(ctx, argv[i], &code);
1590 *r = jsval_string(ret);
1596 static HRESULT StringConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1608 hres = to_string(ctx, argv[0], &str);
1612 str = jsstr_empty();
1615 *r = jsval_string(str);
1618 case DISPATCH_CONSTRUCT: {
1624 hres = to_string(ctx, argv[0], &str);
1628 hres = create_string(ctx, str, &ret);
1631 hres = create_string(ctx, jsstr_empty(), &ret);
1637 *r = jsval_obj(ret);
1642 FIXME("unimplemented flags: %x\n", flags);
1649 static HRESULT string_alloc(script_ctx_t *ctx, jsdisp_t *object_prototype, jsstr_t *str, StringInstance **ret)
1651 StringInstance *string;
1654 string = heap_alloc_zero(sizeof(StringInstance));
1656 return E_OUTOFMEMORY;
1658 if(object_prototype)
1659 hres = init_dispex(&string->dispex, ctx, &String_info, object_prototype);
1661 hres = init_dispex_from_constr(&string->dispex, ctx, &StringInst_info, ctx->string_constr);
1667 string->str = jsstr_addref(str);
1672 static const builtin_prop_t StringConstr_props[] = {
1673 {fromCharCodeW, StringConstr_fromCharCode, PROPF_METHOD},
1676 static const builtin_info_t StringConstr_info = {
1678 {NULL, Function_value, 0},
1679 sizeof(StringConstr_props)/sizeof(*StringConstr_props),
1685 HRESULT create_string_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1687 StringInstance *string;
1690 static const WCHAR StringW[] = {'S','t','r','i','n','g',0};
1692 hres = string_alloc(ctx, object_prototype, jsstr_empty(), &string);
1696 hres = create_builtin_constructor(ctx, StringConstr_value, StringW, &StringConstr_info,
1697 PROPF_CONSTR|1, &string->dispex, ret);
1699 jsdisp_release(&string->dispex);
1703 HRESULT create_string(script_ctx_t *ctx, jsstr_t *str, jsdisp_t **ret)
1705 StringInstance *string;
1708 hres = string_alloc(ctx, NULL, str, &string);
1712 *ret = &string->dispex;