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
);
34 static const WCHAR lengthW
[] = {'l','e','n','g','t','h',0};
35 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
36 static const WCHAR valueOfW
[] = {'v','a','l','u','e','O','f',0};
37 static const WCHAR anchorW
[] = {'a','n','c','h','o','r',0};
38 static const WCHAR bigW
[] = {'b','i','g',0};
39 static const WCHAR blinkW
[] = {'b','l','i','n','k',0};
40 static const WCHAR boldW
[] = {'b','o','l','d',0};
41 static const WCHAR charAtW
[] = {'c','h','a','r','A','t',0};
42 static const WCHAR charCodeAtW
[] = {'c','h','a','r','C','o','d','e','A','t',0};
43 static const WCHAR concatW
[] = {'c','o','n','c','a','t',0};
44 static const WCHAR fixedW
[] = {'f','i','x','e','d',0};
45 static const WCHAR fontcolorW
[] = {'f','o','n','t','c','o','l','o','r',0};
46 static const WCHAR fontsizeW
[] = {'f','o','n','t','s','i','z','e',0};
47 static const WCHAR indexOfW
[] = {'i','n','d','e','x','O','f',0};
48 static const WCHAR italicsW
[] = {'i','t','a','l','i','c','s',0};
49 static const WCHAR lastIndexOfW
[] = {'l','a','s','t','I','n','d','e','x','O','f',0};
50 static const WCHAR linkW
[] = {'l','i','n','k',0};
51 static const WCHAR matchW
[] = {'m','a','t','c','h',0};
52 static const WCHAR replaceW
[] = {'r','e','p','l','a','c','e',0};
53 static const WCHAR searchW
[] = {'s','e','a','r','c','h',0};
54 static const WCHAR sliceW
[] = {'s','l','i','c','e',0};
55 static const WCHAR smallW
[] = {'s','m','a','l','l',0};
56 static const WCHAR splitW
[] = {'s','p','l','i','t',0};
57 static const WCHAR strikeW
[] = {'s','t','r','i','k','e',0};
58 static const WCHAR subW
[] = {'s','u','b',0};
59 static const WCHAR substringW
[] = {'s','u','b','s','t','r','i','n','g',0};
60 static const WCHAR substrW
[] = {'s','u','b','s','t','r',0};
61 static const WCHAR supW
[] = {'s','u','p',0};
62 static const WCHAR toLowerCaseW
[] = {'t','o','L','o','w','e','r','C','a','s','e',0};
63 static const WCHAR toUpperCaseW
[] = {'t','o','U','p','p','e','r','C','a','s','e',0};
64 static const WCHAR toLocaleLowerCaseW
[] = {'t','o','L','o','c','a','l','e','L','o','w','e','r','C','a','s','e',0};
65 static const WCHAR toLocaleUpperCaseW
[] = {'t','o','L','o','c','a','l','e','U','p','p','e','r','C','a','s','e',0};
66 static const WCHAR localeCompareW
[] = {'l','o','c','a','l','e','C','o','m','p','a','r','e',0};
67 static const WCHAR fromCharCodeW
[] = {'f','r','o','m','C','h','a','r','C','o','d','e',0};
69 static inline StringInstance
*string_from_jsdisp(jsdisp_t
*jsdisp
)
71 return CONTAINING_RECORD(jsdisp
, StringInstance
, dispex
);
74 static inline StringInstance
*string_from_vdisp(vdisp_t
*vdisp
)
76 return string_from_jsdisp(vdisp
->u
.jsdisp
);
79 static inline StringInstance
*string_this(vdisp_t
*jsthis
)
81 return is_vclass(jsthis
, JSCLASS_STRING
) ? string_from_vdisp(jsthis
) : NULL
;
84 static HRESULT
get_string_val(script_ctx_t
*ctx
, vdisp_t
*jsthis
, jsstr_t
**val
)
86 StringInstance
*string
;
88 if((string
= string_this(jsthis
))) {
89 *val
= jsstr_addref(string
->str
);
93 return to_string(ctx
, jsval_disp(jsthis
->u
.disp
), val
);
96 static HRESULT
get_string_flat_val(script_ctx_t
*ctx
, vdisp_t
*jsthis
, jsstr_t
**jsval
, const WCHAR
**val
)
100 hres
= get_string_val(ctx
, jsthis
, jsval
);
104 *val
= jsstr_flatten(*jsval
);
108 jsstr_release(*jsval
);
109 return E_OUTOFMEMORY
;
112 static HRESULT
String_get_length(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
114 StringInstance
*string
= string_from_jsdisp(jsthis
);
116 TRACE("%p\n", jsthis
);
118 *r
= jsval_number(jsstr_length(string
->str
));
122 static HRESULT
String_set_length(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t value
)
124 FIXME("%p\n", jsthis
);
128 static HRESULT
stringobj_to_string(vdisp_t
*jsthis
, jsval_t
*r
)
130 StringInstance
*string
;
132 if(!(string
= string_this(jsthis
))) {
133 WARN("this is not a string object\n");
138 *r
= jsval_string(jsstr_addref(string
->str
));
142 /* ECMA-262 3rd Edition 15.5.4.2 */
143 static HRESULT
String_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
148 return stringobj_to_string(jsthis
, r
);
151 /* ECMA-262 3rd Edition 15.5.4.2 */
152 static HRESULT
String_valueOf(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
157 return stringobj_to_string(jsthis
, r
);
160 static HRESULT
do_attributeless_tag_format(script_ctx_t
*ctx
, vdisp_t
*jsthis
, jsval_t
*r
, const WCHAR
*tagname
)
162 unsigned tagname_len
;
167 hres
= get_string_val(ctx
, jsthis
, &str
);
176 tagname_len
= strlenW(tagname
);
178 ret
= jsstr_alloc_buf(jsstr_length(str
) + 2*tagname_len
+ 5, &ptr
);
181 return E_OUTOFMEMORY
;
185 memcpy(ptr
, tagname
, tagname_len
*sizeof(WCHAR
));
189 ptr
+= jsstr_flush(str
, ptr
);
194 memcpy(ptr
, tagname
, tagname_len
*sizeof(WCHAR
));
198 *r
= jsval_string(ret
);
202 static HRESULT
do_attribute_tag_format(script_ctx_t
*ctx
, vdisp_t
*jsthis
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
,
203 const WCHAR
*tagname
, const WCHAR
*attrname
)
205 jsstr_t
*str
, *attr_value
= NULL
;
208 hres
= get_string_val(ctx
, jsthis
, &str
);
213 hres
= to_string(ctx
, argv
[0], &attr_value
);
219 attr_value
= jsstr_undefined();
223 unsigned attrname_len
= strlenW(attrname
);
224 unsigned tagname_len
= strlenW(tagname
);
228 ret
= jsstr_alloc_buf(2*tagname_len
+ attrname_len
+ jsstr_length(attr_value
) + jsstr_length(str
) + 9, &ptr
);
231 memcpy(ptr
, tagname
, tagname_len
*sizeof(WCHAR
));
234 memcpy(ptr
, attrname
, attrname_len
*sizeof(WCHAR
));
238 ptr
+= jsstr_flush(attr_value
, ptr
);
241 ptr
+= jsstr_flush(str
, ptr
);
245 memcpy(ptr
, tagname
, tagname_len
*sizeof(WCHAR
));
249 *r
= jsval_string(ret
);
251 hres
= E_OUTOFMEMORY
;
255 jsstr_release(attr_value
);
260 static HRESULT
String_anchor(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
263 static const WCHAR fontW
[] = {'A',0};
264 static const WCHAR colorW
[] = {'N','A','M','E',0};
266 return do_attribute_tag_format(ctx
, jsthis
, argc
, argv
, r
, fontW
, colorW
);
269 static HRESULT
String_big(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
272 static const WCHAR bigtagW
[] = {'B','I','G',0};
273 return do_attributeless_tag_format(ctx
, jsthis
, r
, bigtagW
);
276 static HRESULT
String_blink(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
279 static const WCHAR blinktagW
[] = {'B','L','I','N','K',0};
280 return do_attributeless_tag_format(ctx
, jsthis
, r
, blinktagW
);
283 static HRESULT
String_bold(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
286 static const WCHAR boldtagW
[] = {'B',0};
287 return do_attributeless_tag_format(ctx
, jsthis
, r
, boldtagW
);
290 /* ECMA-262 3rd Edition 15.5.4.5 */
291 static HRESULT
String_charAt(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
300 hres
= get_string_val(ctx
, jsthis
, &str
);
307 hres
= to_integer(ctx
, argv
[0], &d
);
312 pos
= is_int32(d
) ? d
: -1;
320 if(0 <= pos
&& pos
< jsstr_length(str
)) {
321 ret
= jsstr_substr(str
, pos
, 1);
323 return E_OUTOFMEMORY
;
328 *r
= jsval_string(ret
);
332 /* ECMA-262 3rd Edition 15.5.4.5 */
333 static HRESULT
String_charCodeAt(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
342 hres
= get_string_val(ctx
, jsthis
, &str
);
349 hres
= to_integer(ctx
, argv
[0], &d
);
355 if(!is_int32(d
) || d
< 0 || d
>= jsstr_length(str
)) {
358 *r
= jsval_number(NAN
);
367 jsstr_extract(str
, idx
, 1, &c
);
368 *r
= jsval_number(c
);
375 /* ECMA-262 3rd Edition 15.5.4.6 */
376 static HRESULT
String_concat(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
379 jsstr_t
*ret
= NULL
, *str
;
384 hres
= get_string_val(ctx
, jsthis
, &str
);
395 hres
= to_string(ctx
, argv
[0], &arg_str
);
401 ret
= jsstr_concat(str
, arg_str
);
404 return E_OUTOFMEMORY
;
408 const unsigned str_cnt
= argc
+1;
413 strs
= heap_alloc_zero(str_cnt
* sizeof(*strs
));
416 return E_OUTOFMEMORY
;
420 for(i
=0; i
< argc
; i
++) {
421 hres
= to_string(ctx
, argv
[i
], strs
+i
+1);
426 if(SUCCEEDED(hres
)) {
427 for(i
=0; i
< str_cnt
; i
++) {
428 len
+= jsstr_length(strs
[i
]);
429 if(len
> JSSTR_MAX_LENGTH
) {
430 hres
= E_OUTOFMEMORY
;
435 if(SUCCEEDED(hres
)) {
436 ret
= jsstr_alloc_buf(len
, &ptr
);
438 for(i
=0; i
< str_cnt
; i
++)
439 ptr
+= jsstr_flush(strs
[i
], ptr
);
441 hres
= E_OUTOFMEMORY
;
447 jsstr_release(strs
[i
]);
455 *r
= jsval_string(ret
);
461 static HRESULT
String_fixed(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
464 static const WCHAR fixedtagW
[] = {'T','T',0};
465 return do_attributeless_tag_format(ctx
, jsthis
, r
, fixedtagW
);
468 static HRESULT
String_fontcolor(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
471 static const WCHAR fontW
[] = {'F','O','N','T',0};
472 static const WCHAR colorW
[] = {'C','O','L','O','R',0};
474 return do_attribute_tag_format(ctx
, jsthis
, argc
, argv
, r
, fontW
, colorW
);
477 static HRESULT
String_fontsize(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
480 static const WCHAR fontW
[] = {'F','O','N','T',0};
481 static const WCHAR colorW
[] = {'S','I','Z','E',0};
483 return do_attribute_tag_format(ctx
, jsthis
, argc
, argv
, r
, fontW
, colorW
);
486 static HRESULT
String_indexOf(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
489 unsigned pos
= 0, search_len
, length
;
490 jsstr_t
*search_jsstr
, *jsstr
;
491 const WCHAR
*search_str
, *str
;
497 hres
= get_string_flat_val(ctx
, jsthis
, &jsstr
, &str
);
503 *r
= jsval_number(-1);
504 jsstr_release(jsstr
);
508 hres
= to_flat_string(ctx
, argv
[0], &search_jsstr
, &search_str
);
510 jsstr_release(jsstr
);
514 search_len
= jsstr_length(search_jsstr
);
515 length
= jsstr_length(jsstr
);
520 hres
= to_integer(ctx
, argv
[1], &d
);
521 if(SUCCEEDED(hres
) && d
> 0.0)
522 pos
= is_int32(d
) ? min(length
, d
) : length
;
525 if(SUCCEEDED(hres
) && length
>= search_len
) {
526 const WCHAR
*end
= str
+length
-search_len
;
529 for(ptr
= str
+pos
; ptr
<= end
; ptr
++) {
530 if(!memcmp(ptr
, search_str
, search_len
*sizeof(WCHAR
))) {
537 jsstr_release(search_jsstr
);
538 jsstr_release(jsstr
);
543 *r
= jsval_number(ret
);
547 static HRESULT
String_italics(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
550 static const WCHAR italicstagW
[] = {'I',0};
551 return do_attributeless_tag_format(ctx
, jsthis
, r
, italicstagW
);
554 /* ECMA-262 3rd Edition 15.5.4.8 */
555 static HRESULT
String_lastIndexOf(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
558 unsigned pos
= 0, search_len
, length
;
559 jsstr_t
*search_jsstr
, *jsstr
;
560 const WCHAR
*search_str
, *str
;
566 hres
= get_string_flat_val(ctx
, jsthis
, &jsstr
, &str
);
572 *r
= jsval_number(-1);
573 jsstr_release(jsstr
);
577 hres
= to_flat_string(ctx
, argv
[0], &search_jsstr
, &search_str
);
579 jsstr_release(jsstr
);
583 search_len
= jsstr_length(search_jsstr
);
584 length
= jsstr_length(jsstr
);
589 hres
= to_integer(ctx
, argv
[1], &d
);
590 if(SUCCEEDED(hres
) && d
> 0)
591 pos
= is_int32(d
) ? min(length
, d
) : length
;
596 if(SUCCEEDED(hres
) && length
>= search_len
) {
599 for(ptr
= str
+min(pos
, length
-search_len
); ptr
>= str
; ptr
--) {
600 if(!memcmp(ptr
, search_str
, search_len
*sizeof(WCHAR
))) {
607 jsstr_release(search_jsstr
);
608 jsstr_release(jsstr
);
613 *r
= jsval_number(ret
);
617 static HRESULT
String_link(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
620 static const WCHAR fontW
[] = {'A',0};
621 static const WCHAR colorW
[] = {'H','R','E','F',0};
623 return do_attribute_tag_format(ctx
, jsthis
, argc
, argv
, r
, fontW
, colorW
);
626 /* ECMA-262 3rd Edition 15.5.4.10 */
627 static HRESULT
String_match(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
630 jsdisp_t
*regexp
= NULL
;
642 if(is_object_instance(argv
[0])) {
643 regexp
= iface_to_jsdisp(get_object(argv
[0]));
644 if(regexp
&& !is_class(regexp
, JSCLASS_REGEXP
)) {
645 jsdisp_release(regexp
);
653 hres
= to_string(ctx
, argv
[0], &match_str
);
657 hres
= create_regexp(ctx
, match_str
, 0, ®exp
);
658 jsstr_release(match_str
);
663 hres
= get_string_val(ctx
, jsthis
, &str
);
665 hres
= regexp_string_match(ctx
, regexp
, str
, r
);
667 jsdisp_release(regexp
);
678 static BOOL
strbuf_ensure_size(strbuf_t
*buf
, unsigned len
)
686 new_size
= buf
->size
? buf
->size
<<1 : 16;
690 new_buf
= heap_realloc(buf
->buf
, new_size
*sizeof(WCHAR
));
692 new_buf
= heap_alloc(new_size
*sizeof(WCHAR
));
697 buf
->size
= new_size
;
701 static HRESULT
strbuf_append(strbuf_t
*buf
, const WCHAR
*str
, DWORD len
)
706 if(!strbuf_ensure_size(buf
, buf
->len
+len
))
707 return E_OUTOFMEMORY
;
709 memcpy(buf
->buf
+buf
->len
, str
, len
*sizeof(WCHAR
));
714 static HRESULT
strbuf_append_jsstr(strbuf_t
*buf
, jsstr_t
*str
)
716 if(!strbuf_ensure_size(buf
, buf
->len
+jsstr_length(str
)))
717 return E_OUTOFMEMORY
;
719 jsstr_flush(str
, buf
->buf
+buf
->len
);
720 buf
->len
+= jsstr_length(str
);
724 static HRESULT
rep_call(script_ctx_t
*ctx
, jsdisp_t
*func
,
725 jsstr_t
*jsstr
, const WCHAR
*str
, match_state_t
*match
, jsstr_t
**ret
)
734 argc
= match
->paren_count
+3;
735 argv
= heap_alloc_zero(sizeof(*argv
)*argc
);
737 return E_OUTOFMEMORY
;
739 tmp_str
= jsstr_alloc_len(match
->cp
-match
->match_len
, match
->match_len
);
741 hres
= E_OUTOFMEMORY
;
742 argv
[0] = jsval_string(tmp_str
);
744 if(SUCCEEDED(hres
)) {
745 for(i
=0; i
< match
->paren_count
; i
++) {
746 if(match
->parens
[i
].index
!= -1)
747 tmp_str
= jsstr_substr(jsstr
, match
->parens
[i
].index
, match
->parens
[i
].length
);
749 tmp_str
= jsstr_empty();
751 hres
= E_OUTOFMEMORY
;
754 argv
[i
+1] = jsval_string(tmp_str
);
758 if(SUCCEEDED(hres
)) {
759 argv
[match
->paren_count
+1] = jsval_number(match
->cp
-str
- match
->match_len
);
760 argv
[match
->paren_count
+2] = jsval_string(jsstr
);
764 hres
= jsdisp_call_value(func
, NULL
, DISPATCH_METHOD
, argc
, argv
, &val
);
766 for(i
=0; i
<= match
->paren_count
; i
++)
767 jsstr_release(get_string(argv
[i
]));
773 hres
= to_string(ctx
, val
, ret
);
778 /* ECMA-262 3rd Edition 15.5.4.11 */
779 static HRESULT
String_replace(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
782 const WCHAR
*str
, *match_str
= NULL
, *rep_str
= NULL
;
783 jsstr_t
*rep_jsstr
, *match_jsstr
, *jsstr
;
784 jsdisp_t
*rep_func
= NULL
, *regexp
= NULL
;
785 match_state_t
*match
= NULL
, last_match
= {0};
786 strbuf_t ret
= {NULL
,0,0};
787 DWORD re_flags
= REM_NO_CTX_UPDATE
|REM_ALLOC_RESULT
;
793 hres
= get_string_flat_val(ctx
, jsthis
, &jsstr
, &str
);
799 *r
= jsval_string(jsstr
);
801 jsstr_release(jsstr
);
805 if(is_object_instance(argv
[0])) {
806 regexp
= iface_to_jsdisp(get_object(argv
[0]));
807 if(regexp
&& !is_class(regexp
, JSCLASS_REGEXP
)) {
808 jsdisp_release(regexp
);
814 hres
= to_flat_string(ctx
, argv
[0], &match_jsstr
, &match_str
);
816 jsstr_release(jsstr
);
822 if(is_object_instance(argv
[1])) {
823 rep_func
= iface_to_jsdisp(get_object(argv
[1]));
824 if(rep_func
&& !is_class(rep_func
, JSCLASS_FUNCTION
)) {
825 jsdisp_release(rep_func
);
831 hres
= to_flat_string(ctx
, argv
[1], &rep_jsstr
, &rep_str
);
833 rep_len
= jsstr_length(rep_jsstr
);
837 if(SUCCEEDED(hres
)) {
838 const WCHAR
*ecp
= str
;
842 hres
= regexp_match_next(ctx
, regexp
, re_flags
, jsstr
, &match
);
843 re_flags
= (re_flags
| REM_CHECK_GLOBAL
) & (~REM_ALLOC_RESULT
);
845 if(hres
== S_FALSE
) {
852 last_match
.cp
= match
->cp
;
853 last_match
.match_len
= match
->match_len
;
855 if(re_flags
& REM_ALLOC_RESULT
) {
856 re_flags
&= ~REM_ALLOC_RESULT
;
861 match
->cp
= strstrW(match
->cp
, match_str
);
864 match
->match_len
= jsstr_length(match_jsstr
);
865 match
->cp
+= match
->match_len
;
868 hres
= strbuf_append(&ret
, ecp
, match
->cp
-ecp
-match
->match_len
);
876 hres
= rep_call(ctx
, rep_func
, jsstr
, str
, match
, &cstr
);
880 hres
= strbuf_append_jsstr(&ret
, cstr
);
884 }else if(rep_str
&& regexp
) {
885 const WCHAR
*ptr
= rep_str
, *ptr2
;
887 while((ptr2
= strchrW(ptr
, '$'))) {
888 hres
= strbuf_append(&ret
, ptr
, ptr2
-ptr
);
894 hres
= strbuf_append(&ret
, ptr2
, 1);
898 hres
= strbuf_append(&ret
, match
->cp
-match
->match_len
, match
->match_len
);
902 hres
= strbuf_append(&ret
, str
, match
->cp
-str
-match
->match_len
);
906 hres
= strbuf_append(&ret
, ecp
, (str
+jsstr_length(jsstr
))-ecp
);
912 if(!isdigitW(ptr2
[1])) {
913 hres
= strbuf_append(&ret
, ptr2
, 1);
919 if(isdigitW(ptr2
[2]) && idx
*10 + (ptr2
[2]-'0') <= match
->paren_count
) {
920 idx
= idx
*10 + (ptr
[2]-'0');
922 }else if(idx
&& idx
<= match
->paren_count
) {
925 hres
= strbuf_append(&ret
, ptr2
, 1);
930 if(match
->parens
[idx
-1].index
!= -1)
931 hres
= strbuf_append(&ret
, str
+match
->parens
[idx
-1].index
,
932 match
->parens
[idx
-1].length
);
941 hres
= strbuf_append(&ret
, ptr
, (rep_str
+rep_len
)-ptr
);
945 hres
= strbuf_append(&ret
, rep_str
, rep_len
);
949 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d'};
951 hres
= strbuf_append(&ret
, undefinedW
, sizeof(undefinedW
)/sizeof(WCHAR
));
958 else if(!match
->match_len
)
963 hres
= strbuf_append(&ret
, ecp
, str
+jsstr_length(jsstr
)-ecp
);
967 jsdisp_release(rep_func
);
969 jsstr_release(rep_jsstr
);
971 jsstr_release(match_jsstr
);
975 if(SUCCEEDED(hres
) && last_match
.cp
&& regexp
) {
976 jsstr_release(ctx
->last_match
);
977 ctx
->last_match
= jsstr_addref(jsstr
);
978 ctx
->last_match_index
= last_match
.cp
-str
-last_match
.match_len
;
979 ctx
->last_match_length
= last_match
.match_len
;
983 jsdisp_release(regexp
);
984 jsstr_release(jsstr
);
986 if(SUCCEEDED(hres
) && r
) {
989 ret_str
= jsstr_alloc_len(ret
.buf
, ret
.len
);
991 return E_OUTOFMEMORY
;
993 TRACE("= %s\n", debugstr_jsstr(ret_str
));
994 *r
= jsval_string(ret_str
);
1001 static HRESULT
String_search(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1004 jsdisp_t
*regexp
= NULL
;
1007 match_state_t match
, *match_ptr
= &match
;
1012 hres
= get_string_flat_val(ctx
, jsthis
, &jsstr
, &str
);
1019 jsstr_release(jsstr
);
1023 if(is_object_instance(argv
[0])) {
1024 regexp
= iface_to_jsdisp(get_object(argv
[0]));
1025 if(regexp
&& !is_class(regexp
, JSCLASS_REGEXP
)) {
1026 jsdisp_release(regexp
);
1032 hres
= create_regexp_var(ctx
, argv
[0], NULL
, ®exp
);
1034 jsstr_release(jsstr
);
1040 hres
= regexp_match_next(ctx
, regexp
, REM_RESET_INDEX
|REM_NO_PARENS
, jsstr
, &match_ptr
);
1041 jsstr_release(jsstr
);
1042 jsdisp_release(regexp
);
1047 *r
= jsval_number(hres
== S_OK
? match
.cp
-match
.match_len
-str
: -1);
1051 /* ECMA-262 3rd Edition 15.5.4.13 */
1052 static HRESULT
String_slice(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1055 int start
=0, end
, length
;
1062 hres
= get_string_val(ctx
, jsthis
, &str
);
1066 length
= jsstr_length(str
);
1068 hres
= to_integer(ctx
, argv
[0], &d
);
1077 start
= length
+ start
;
1080 }else if(start
> length
) {
1089 hres
= to_integer(ctx
, argv
[1], &d
);
1101 }else if(end
> length
) {
1105 end
= d
< 0.0 ? 0 : length
;
1115 jsstr_t
*retstr
= jsstr_substr(str
, start
, end
-start
);
1118 return E_OUTOFMEMORY
;
1121 *r
= jsval_string(retstr
);
1128 static HRESULT
String_small(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1131 static const WCHAR smalltagW
[] = {'S','M','A','L','L',0};
1132 return do_attributeless_tag_format(ctx
, jsthis
, r
, smalltagW
);
1135 static HRESULT
String_split(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1138 match_state_t match_result
, *match_ptr
= &match_result
;
1139 DWORD length
, i
, match_len
= 0;
1140 const WCHAR
*ptr
, *ptr2
, *str
, *match_str
= NULL
;
1141 unsigned limit
= ~0u;
1142 jsdisp_t
*array
, *regexp
= NULL
;
1143 jsstr_t
*jsstr
, *match_jsstr
, *tmp_str
;
1148 if(argc
!= 1 && argc
!= 2) {
1149 FIXME("unsupported argc %u\n", argc
);
1153 hres
= get_string_flat_val(ctx
, jsthis
, &jsstr
, &str
);
1157 length
= jsstr_length(jsstr
);
1159 if(argc
> 1 && !is_undefined(argv
[1])) {
1160 hres
= to_uint32(ctx
, argv
[1], &limit
);
1162 jsstr_release(jsstr
);
1167 if(is_object_instance(argv
[0])) {
1168 regexp
= iface_to_jsdisp(get_object(argv
[0]));
1170 if(!is_class(regexp
, JSCLASS_REGEXP
)) {
1171 jsdisp_release(regexp
);
1178 hres
= to_flat_string(ctx
, argv
[0], &match_jsstr
, &match_str
);
1180 jsstr_release(jsstr
);
1184 match_len
= jsstr_length(match_jsstr
);
1186 jsstr_release(match_jsstr
);
1191 hres
= create_array(ctx
, 0, &array
);
1193 if(SUCCEEDED(hres
)) {
1195 match_result
.cp
= str
;
1196 for(i
=0; i
<limit
; i
++) {
1198 hres
= regexp_match_next(ctx
, regexp
, REM_NO_PARENS
, jsstr
, &match_ptr
);
1201 ptr2
= match_result
.cp
- match_result
.match_len
;
1202 }else if(match_str
) {
1203 ptr2
= strstrW(ptr
, match_str
);
1212 tmp_str
= jsstr_alloc_len(ptr
, ptr2
-ptr
);
1214 hres
= E_OUTOFMEMORY
;
1218 hres
= jsdisp_propput_idx(array
, i
, jsval_string(tmp_str
));
1219 jsstr_release(tmp_str
);
1224 ptr
= match_result
.cp
;
1226 ptr
= ptr2
+ match_len
;
1232 if(SUCCEEDED(hres
) && (match_str
|| regexp
) && i
<limit
) {
1233 DWORD len
= (str
+length
) - ptr
;
1235 if(len
|| match_str
) {
1236 tmp_str
= jsstr_alloc_len(ptr
, len
);
1239 hres
= jsdisp_propput_idx(array
, i
, jsval_string(tmp_str
));
1240 jsstr_release(tmp_str
);
1242 hres
= E_OUTOFMEMORY
;
1248 jsdisp_release(regexp
);
1250 jsstr_release(match_jsstr
);
1251 jsstr_release(jsstr
);
1253 if(SUCCEEDED(hres
) && r
)
1254 *r
= jsval_obj(array
);
1256 jsdisp_release(array
);
1261 static HRESULT
String_strike(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1264 static const WCHAR striketagW
[] = {'S','T','R','I','K','E',0};
1265 return do_attributeless_tag_format(ctx
, jsthis
, r
, striketagW
);
1268 static HRESULT
String_sub(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1271 static const WCHAR subtagW
[] = {'S','U','B',0};
1272 return do_attributeless_tag_format(ctx
, jsthis
, r
, subtagW
);
1275 /* ECMA-262 3rd Edition 15.5.4.15 */
1276 static HRESULT
String_substring(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1279 INT start
=0, end
, length
;
1286 hres
= get_string_val(ctx
, jsthis
, &str
);
1290 length
= jsstr_length(str
);
1292 hres
= to_integer(ctx
, argv
[0], &d
);
1299 start
= is_int32(d
) ? min(length
, d
) : length
;
1303 hres
= to_integer(ctx
, argv
[1], &d
);
1310 end
= is_int32(d
) ? min(length
, d
) : length
;
1324 jsstr_t
*ret
= jsstr_substr(str
, start
, end
-start
);
1326 *r
= jsval_string(ret
);
1328 hres
= E_OUTOFMEMORY
;
1334 /* ECMA-262 3rd Edition B.2.3 */
1335 static HRESULT
String_substr(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1338 int start
=0, len
, length
;
1345 hres
= get_string_val(ctx
, jsthis
, &str
);
1349 length
= jsstr_length(str
);
1351 hres
= to_integer(ctx
, argv
[0], &d
);
1358 start
= is_int32(d
) ? min(length
, d
) : length
;
1362 hres
= to_integer(ctx
, argv
[1], &d
);
1369 len
= is_int32(d
) ? min(length
-start
, d
) : length
-start
;
1378 jsstr_t
*ret
= jsstr_substr(str
, start
, len
);
1380 *r
= jsval_string(ret
);
1382 hres
= E_OUTOFMEMORY
;
1389 static HRESULT
String_sup(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1392 static const WCHAR suptagW
[] = {'S','U','P',0};
1393 return do_attributeless_tag_format(ctx
, jsthis
, r
, suptagW
);
1396 static HRESULT
String_toLowerCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1404 hres
= get_string_val(ctx
, jsthis
, &str
);
1409 unsigned len
= jsstr_length(str
);
1413 ret
= jsstr_alloc_buf(len
, &buf
);
1416 return E_OUTOFMEMORY
;
1419 jsstr_flush(str
, buf
);
1420 for (; len
--; buf
++) *buf
= tolowerW(*buf
);
1422 *r
= jsval_string(ret
);
1428 static HRESULT
String_toUpperCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1436 hres
= get_string_val(ctx
, jsthis
, &str
);
1441 unsigned len
= jsstr_length(str
);
1445 ret
= jsstr_alloc_buf(len
, &buf
);
1448 return E_OUTOFMEMORY
;
1451 jsstr_flush(str
, buf
);
1452 for (; len
--; buf
++) *buf
= toupperW(*buf
);
1454 *r
= jsval_string(ret
);
1460 static HRESULT
String_toLocaleLowerCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1467 static HRESULT
String_toLocaleUpperCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1474 static HRESULT
String_localeCompare(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1481 static HRESULT
String_get_value(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
1483 StringInstance
*This
= string_from_jsdisp(jsthis
);
1487 *r
= jsval_string(jsstr_addref(This
->str
));
1491 static void String_destructor(jsdisp_t
*dispex
)
1493 StringInstance
*This
= string_from_jsdisp(dispex
);
1495 jsstr_release(This
->str
);
1499 static unsigned String_idx_length(jsdisp_t
*jsdisp
)
1501 StringInstance
*string
= string_from_jsdisp(jsdisp
);
1504 * NOTE: For invoke version < 2, indexed array is not implemented at all.
1505 * Newer jscript.dll versions implement it on string type, not class,
1506 * which is not how it should work according to spec. IE9 implements it
1507 * properly, but it uses its own JavaScript engine inside MSHTML. We
1508 * implement it here, but in the way IE9 and spec work.
1510 return string
->dispex
.ctx
->version
< 2 ? 0 : jsstr_length(string
->str
);
1513 static HRESULT
String_idx_get(jsdisp_t
*jsdisp
, unsigned idx
, jsval_t
*r
)
1515 StringInstance
*string
= string_from_jsdisp(jsdisp
);
1518 ret
= jsstr_substr(string
->str
, idx
, 1);
1520 return E_OUTOFMEMORY
;
1522 TRACE("%p[%u] = %s\n", string
, idx
, debugstr_jsstr(ret
));
1524 *r
= jsval_string(ret
);
1528 static const builtin_prop_t String_props
[] = {
1529 {anchorW
, String_anchor
, PROPF_METHOD
|1},
1530 {bigW
, String_big
, PROPF_METHOD
},
1531 {blinkW
, String_blink
, PROPF_METHOD
},
1532 {boldW
, String_bold
, PROPF_METHOD
},
1533 {charAtW
, String_charAt
, PROPF_METHOD
|1},
1534 {charCodeAtW
, String_charCodeAt
, PROPF_METHOD
|1},
1535 {concatW
, String_concat
, PROPF_METHOD
|1},
1536 {fixedW
, String_fixed
, PROPF_METHOD
},
1537 {fontcolorW
, String_fontcolor
, PROPF_METHOD
|1},
1538 {fontsizeW
, String_fontsize
, PROPF_METHOD
|1},
1539 {indexOfW
, String_indexOf
, PROPF_METHOD
|2},
1540 {italicsW
, String_italics
, PROPF_METHOD
},
1541 {lastIndexOfW
, String_lastIndexOf
, PROPF_METHOD
|2},
1542 {lengthW
, NULL
,0, String_get_length
, String_set_length
},
1543 {linkW
, String_link
, PROPF_METHOD
|1},
1544 {localeCompareW
, String_localeCompare
, PROPF_METHOD
|1},
1545 {matchW
, String_match
, PROPF_METHOD
|1},
1546 {replaceW
, String_replace
, PROPF_METHOD
|1},
1547 {searchW
, String_search
, PROPF_METHOD
},
1548 {sliceW
, String_slice
, PROPF_METHOD
},
1549 {smallW
, String_small
, PROPF_METHOD
},
1550 {splitW
, String_split
, PROPF_METHOD
|2},
1551 {strikeW
, String_strike
, PROPF_METHOD
},
1552 {subW
, String_sub
, PROPF_METHOD
},
1553 {substrW
, String_substr
, PROPF_METHOD
|2},
1554 {substringW
, String_substring
, PROPF_METHOD
|2},
1555 {supW
, String_sup
, PROPF_METHOD
},
1556 {toLocaleLowerCaseW
, String_toLocaleLowerCase
, PROPF_METHOD
},
1557 {toLocaleUpperCaseW
, String_toLocaleUpperCase
, PROPF_METHOD
},
1558 {toLowerCaseW
, String_toLowerCase
, PROPF_METHOD
},
1559 {toStringW
, String_toString
, PROPF_METHOD
},
1560 {toUpperCaseW
, String_toUpperCase
, PROPF_METHOD
},
1561 {valueOfW
, String_valueOf
, PROPF_METHOD
}
1564 static const builtin_info_t String_info
= {
1566 {NULL
, NULL
,0, String_get_value
},
1567 sizeof(String_props
)/sizeof(*String_props
),
1573 static const builtin_prop_t StringInst_props
[] = {
1574 {lengthW
, NULL
,0, String_get_length
, String_set_length
}
1577 static const builtin_info_t StringInst_info
= {
1579 {NULL
, NULL
,0, String_get_value
},
1580 sizeof(StringInst_props
)/sizeof(*StringInst_props
),
1588 /* ECMA-262 3rd Edition 15.5.3.2 */
1589 static HRESULT
StringConstr_fromCharCode(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
,
1590 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
1599 ret
= jsstr_alloc_buf(argc
, &ret_str
);
1601 return E_OUTOFMEMORY
;
1603 for(i
=0; i
<argc
; i
++) {
1604 hres
= to_uint32(ctx
, argv
[i
], &code
);
1614 *r
= jsval_string(ret
);
1620 static HRESULT
StringConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1632 hres
= to_string(ctx
, argv
[0], &str
);
1636 str
= jsstr_empty();
1639 *r
= jsval_string(str
);
1642 case DISPATCH_CONSTRUCT
: {
1647 hres
= to_string(ctx
, argv
[0], &str
);
1651 str
= jsstr_empty();
1654 hres
= create_string(ctx
, str
, &ret
);
1655 if (SUCCEEDED(hres
)) *r
= jsval_obj(ret
);
1661 FIXME("unimplemented flags: %x\n", flags
);
1668 static HRESULT
string_alloc(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsstr_t
*str
, StringInstance
**ret
)
1670 StringInstance
*string
;
1673 string
= heap_alloc_zero(sizeof(StringInstance
));
1675 return E_OUTOFMEMORY
;
1677 if(object_prototype
)
1678 hres
= init_dispex(&string
->dispex
, ctx
, &String_info
, object_prototype
);
1680 hres
= init_dispex_from_constr(&string
->dispex
, ctx
, &StringInst_info
, ctx
->string_constr
);
1686 string
->str
= jsstr_addref(str
);
1691 static const builtin_prop_t StringConstr_props
[] = {
1692 {fromCharCodeW
, StringConstr_fromCharCode
, PROPF_METHOD
},
1695 static const builtin_info_t StringConstr_info
= {
1697 DEFAULT_FUNCTION_VALUE
,
1698 sizeof(StringConstr_props
)/sizeof(*StringConstr_props
),
1704 HRESULT
create_string_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
1706 StringInstance
*string
;
1709 static const WCHAR StringW
[] = {'S','t','r','i','n','g',0};
1711 hres
= string_alloc(ctx
, object_prototype
, jsstr_empty(), &string
);
1715 hres
= create_builtin_constructor(ctx
, StringConstr_value
, StringW
, &StringConstr_info
,
1716 PROPF_CONSTR
|1, &string
->dispex
, ret
);
1718 jsdisp_release(&string
->dispex
);
1722 HRESULT
create_string(script_ctx_t
*ctx
, jsstr_t
*str
, jsdisp_t
**ret
)
1724 StringInstance
*string
;
1727 hres
= string_alloc(ctx
, NULL
, str
, &string
);
1731 *ret
= &string
->dispex
;