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
= (StringInstance
*)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 ptr
= jsstr_alloc_buf(jsstr_length(str
) + 2*tagname_len
+ 5, &ret
);
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 ptr
= jsstr_alloc_buf(2*tagname_len
+ attrname_len
+ jsstr_length(attr_value
) + jsstr_length(str
) + 9, &ret
);
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
,
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 ptr
= jsstr_alloc_buf(len
, &ret
);
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 jsstr_t
*search_jsstr
, *jsstr
;
490 const WCHAR
*search_str
, *str
;
497 hres
= get_string_flat_val(ctx
, jsthis
, &jsstr
, &str
);
501 length
= jsstr_length(jsstr
);
504 *r
= jsval_number(-1);
505 jsstr_release(jsstr
);
509 hres
= to_flat_string(ctx
, argv
[0], &search_jsstr
, &search_str
);
511 jsstr_release(jsstr
);
518 hres
= to_integer(ctx
, argv
[1], &d
);
519 if(SUCCEEDED(hres
) && d
> 0.0)
520 pos
= is_int32(d
) ? min(length
, d
) : length
;
523 if(SUCCEEDED(hres
)) {
526 ptr
= strstrW(str
+pos
, search_str
);
533 jsstr_release(search_jsstr
);
534 jsstr_release(jsstr
);
539 *r
= jsval_number(ret
);
543 static HRESULT
String_italics(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
546 static const WCHAR italicstagW
[] = {'I',0};
547 return do_attributeless_tag_format(ctx
, jsthis
, r
, italicstagW
);
550 /* ECMA-262 3rd Edition 15.5.4.8 */
551 static HRESULT
String_lastIndexOf(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
554 unsigned pos
= 0, search_len
, length
;
555 jsstr_t
*search_jsstr
, *jsstr
;
556 const WCHAR
*search_str
, *str
;
562 hres
= get_string_flat_val(ctx
, jsthis
, &jsstr
, &str
);
568 *r
= jsval_number(-1);
569 jsstr_release(jsstr
);
573 hres
= to_flat_string(ctx
, argv
[0], &search_jsstr
, &search_str
);
575 jsstr_release(jsstr
);
579 search_len
= jsstr_length(search_jsstr
);
580 length
= jsstr_length(jsstr
);
585 hres
= to_integer(ctx
, argv
[1], &d
);
586 if(SUCCEEDED(hres
) && d
> 0)
587 pos
= is_int32(d
) ? min(length
, d
) : length
;
592 if(SUCCEEDED(hres
) && length
>= search_len
) {
595 for(ptr
= str
+min(pos
, length
-search_len
); ptr
>= str
; ptr
--) {
596 if(!memcmp(ptr
, search_str
, search_len
*sizeof(WCHAR
))) {
603 jsstr_release(search_jsstr
);
604 jsstr_release(jsstr
);
609 *r
= jsval_number(ret
);
613 static HRESULT
String_link(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
616 static const WCHAR fontW
[] = {'A',0};
617 static const WCHAR colorW
[] = {'H','R','E','F',0};
619 return do_attribute_tag_format(ctx
, jsthis
, argc
, argv
, r
, fontW
, colorW
);
622 /* ECMA-262 3rd Edition 15.5.4.10 */
623 static HRESULT
String_match(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
626 jsdisp_t
*regexp
= NULL
;
638 if(is_object_instance(argv
[0])) {
639 regexp
= iface_to_jsdisp((IUnknown
*)get_object(argv
[0]));
640 if(regexp
&& !is_class(regexp
, JSCLASS_REGEXP
)) {
641 jsdisp_release(regexp
);
649 hres
= to_string(ctx
, argv
[0], &match_str
);
653 hres
= create_regexp(ctx
, match_str
, 0, ®exp
);
654 jsstr_release(match_str
);
659 hres
= get_string_val(ctx
, jsthis
, &str
);
661 hres
= regexp_string_match(ctx
, regexp
, str
, r
);
663 jsdisp_release(regexp
);
674 static BOOL
strbuf_ensure_size(strbuf_t
*buf
, unsigned len
)
682 new_size
= buf
->size
? buf
->size
<<1 : 16;
686 new_buf
= heap_realloc(buf
->buf
, new_size
*sizeof(WCHAR
));
688 new_buf
= heap_alloc(new_size
*sizeof(WCHAR
));
693 buf
->size
= new_size
;
697 static HRESULT
strbuf_append(strbuf_t
*buf
, const WCHAR
*str
, DWORD len
)
702 if(!strbuf_ensure_size(buf
, buf
->len
+len
))
703 return E_OUTOFMEMORY
;
705 memcpy(buf
->buf
+buf
->len
, str
, len
*sizeof(WCHAR
));
710 static HRESULT
strbuf_append_jsstr(strbuf_t
*buf
, jsstr_t
*str
)
712 if(!strbuf_ensure_size(buf
, buf
->len
+jsstr_length(str
)))
713 return E_OUTOFMEMORY
;
715 jsstr_flush(str
, buf
->buf
+buf
->len
);
716 buf
->len
+= jsstr_length(str
);
720 static HRESULT
rep_call(script_ctx_t
*ctx
, jsdisp_t
*func
,
721 jsstr_t
*jsstr
, const WCHAR
*str
, match_state_t
*match
, jsstr_t
**ret
)
730 argc
= match
->paren_count
+3;
731 argv
= heap_alloc_zero(sizeof(*argv
)*argc
);
733 return E_OUTOFMEMORY
;
735 tmp_str
= jsstr_alloc_len(match
->cp
-match
->match_len
, match
->match_len
);
737 hres
= E_OUTOFMEMORY
;
738 argv
[0] = jsval_string(tmp_str
);
740 if(SUCCEEDED(hres
)) {
741 for(i
=0; i
< match
->paren_count
; i
++) {
742 if(match
->parens
[i
].index
!= -1)
743 tmp_str
= jsstr_substr(jsstr
, match
->parens
[i
].index
, match
->parens
[i
].length
);
745 tmp_str
= jsstr_empty();
747 hres
= E_OUTOFMEMORY
;
750 argv
[i
+1] = jsval_string(tmp_str
);
754 if(SUCCEEDED(hres
)) {
755 argv
[match
->paren_count
+1] = jsval_number(match
->cp
-str
- match
->match_len
);
756 argv
[match
->paren_count
+2] = jsval_string(jsstr
);
760 hres
= jsdisp_call_value(func
, NULL
, DISPATCH_METHOD
, argc
, argv
, &val
);
762 for(i
=0; i
<= match
->paren_count
; i
++)
763 jsstr_release(get_string(argv
[i
]));
769 hres
= to_string(ctx
, val
, ret
);
774 /* ECMA-262 3rd Edition 15.5.4.11 */
775 static HRESULT
String_replace(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
778 const WCHAR
*str
, *match_str
= NULL
, *rep_str
= NULL
;
779 jsstr_t
*rep_jsstr
, *match_jsstr
, *jsstr
;
780 jsdisp_t
*rep_func
= NULL
, *regexp
= NULL
;
781 match_state_t
*match
= NULL
, last_match
= {0};
782 strbuf_t ret
= {NULL
,0,0};
783 DWORD re_flags
= REM_NO_CTX_UPDATE
|REM_ALLOC_RESULT
;
789 hres
= get_string_flat_val(ctx
, jsthis
, &jsstr
, &str
);
795 *r
= jsval_string(jsstr
);
797 jsstr_release(jsstr
);
801 if(is_object_instance(argv
[0])) {
802 regexp
= iface_to_jsdisp((IUnknown
*)get_object(argv
[0]));
803 if(regexp
&& !is_class(regexp
, JSCLASS_REGEXP
)) {
804 jsdisp_release(regexp
);
810 hres
= to_flat_string(ctx
, argv
[0], &match_jsstr
, &match_str
);
812 jsstr_release(jsstr
);
818 if(is_object_instance(argv
[1])) {
819 rep_func
= iface_to_jsdisp((IUnknown
*)get_object(argv
[1]));
820 if(rep_func
&& !is_class(rep_func
, JSCLASS_FUNCTION
)) {
821 jsdisp_release(rep_func
);
827 hres
= to_flat_string(ctx
, argv
[1], &rep_jsstr
, &rep_str
);
829 rep_len
= jsstr_length(rep_jsstr
);
833 if(SUCCEEDED(hres
)) {
834 const WCHAR
*ecp
= str
;
838 hres
= regexp_match_next(ctx
, regexp
, re_flags
, jsstr
, &match
);
839 re_flags
= (re_flags
| REM_CHECK_GLOBAL
) & (~REM_ALLOC_RESULT
);
841 if(hres
== S_FALSE
) {
848 last_match
.cp
= match
->cp
;
849 last_match
.match_len
= match
->match_len
;
851 if(re_flags
& REM_ALLOC_RESULT
) {
852 re_flags
&= ~REM_ALLOC_RESULT
;
857 match
->cp
= strstrW(match
->cp
, match_str
);
860 match
->match_len
= jsstr_length(match_jsstr
);
861 match
->cp
+= match
->match_len
;
864 hres
= strbuf_append(&ret
, ecp
, match
->cp
-ecp
-match
->match_len
);
872 hres
= rep_call(ctx
, rep_func
, jsstr
, str
, match
, &cstr
);
876 hres
= strbuf_append_jsstr(&ret
, cstr
);
880 }else if(rep_str
&& regexp
) {
881 const WCHAR
*ptr
= rep_str
, *ptr2
;
883 while((ptr2
= strchrW(ptr
, '$'))) {
884 hres
= strbuf_append(&ret
, ptr
, ptr2
-ptr
);
890 hres
= strbuf_append(&ret
, ptr2
, 1);
894 hres
= strbuf_append(&ret
, match
->cp
-match
->match_len
, match
->match_len
);
898 hres
= strbuf_append(&ret
, str
, match
->cp
-str
-match
->match_len
);
902 hres
= strbuf_append(&ret
, ecp
, (str
+jsstr_length(jsstr
))-ecp
);
908 if(!isdigitW(ptr2
[1])) {
909 hres
= strbuf_append(&ret
, ptr2
, 1);
915 if(isdigitW(ptr2
[2]) && idx
*10 + (ptr2
[2]-'0') <= match
->paren_count
) {
916 idx
= idx
*10 + (ptr
[2]-'0');
918 }else if(idx
&& idx
<= match
->paren_count
) {
921 hres
= strbuf_append(&ret
, ptr2
, 1);
926 if(match
->parens
[idx
-1].index
!= -1)
927 hres
= strbuf_append(&ret
, str
+match
->parens
[idx
-1].index
,
928 match
->parens
[idx
-1].length
);
937 hres
= strbuf_append(&ret
, ptr
, (rep_str
+rep_len
)-ptr
);
941 hres
= strbuf_append(&ret
, rep_str
, rep_len
);
945 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d'};
947 hres
= strbuf_append(&ret
, undefinedW
, sizeof(undefinedW
)/sizeof(WCHAR
));
954 else if(!match
->match_len
)
959 hres
= strbuf_append(&ret
, ecp
, str
+jsstr_length(jsstr
)-ecp
);
963 jsdisp_release(rep_func
);
965 jsstr_release(rep_jsstr
);
967 jsstr_release(match_jsstr
);
971 if(SUCCEEDED(hres
) && last_match
.cp
&& regexp
) {
972 jsstr_release(ctx
->last_match
);
973 ctx
->last_match
= jsstr_addref(jsstr
);
974 ctx
->last_match_index
= last_match
.cp
-str
-last_match
.match_len
;
975 ctx
->last_match_length
= last_match
.match_len
;
979 jsdisp_release(regexp
);
980 jsstr_release(jsstr
);
982 if(SUCCEEDED(hres
) && r
) {
985 ret_str
= jsstr_alloc_len(ret
.buf
, ret
.len
);
987 return E_OUTOFMEMORY
;
989 TRACE("= %s\n", debugstr_jsstr(ret_str
));
990 *r
= jsval_string(ret_str
);
997 static HRESULT
String_search(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1000 jsdisp_t
*regexp
= NULL
;
1003 match_state_t match
, *match_ptr
= &match
;
1008 hres
= get_string_flat_val(ctx
, jsthis
, &jsstr
, &str
);
1015 jsstr_release(jsstr
);
1019 if(is_object_instance(argv
[0])) {
1020 regexp
= iface_to_jsdisp((IUnknown
*)get_object(argv
[0]));
1021 if(regexp
&& !is_class(regexp
, JSCLASS_REGEXP
)) {
1022 jsdisp_release(regexp
);
1028 hres
= create_regexp_var(ctx
, argv
[0], NULL
, ®exp
);
1030 jsstr_release(jsstr
);
1036 hres
= regexp_match_next(ctx
, regexp
, REM_RESET_INDEX
|REM_NO_PARENS
, jsstr
, &match_ptr
);
1037 jsstr_release(jsstr
);
1038 jsdisp_release(regexp
);
1043 *r
= jsval_number(hres
== S_OK
? match
.cp
-match
.match_len
-str
: -1);
1047 /* ECMA-262 3rd Edition 15.5.4.13 */
1048 static HRESULT
String_slice(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1051 int start
=0, end
, length
;
1058 hres
= get_string_val(ctx
, jsthis
, &str
);
1062 length
= jsstr_length(str
);
1064 hres
= to_integer(ctx
, argv
[0], &d
);
1073 start
= length
+ start
;
1076 }else if(start
> length
) {
1085 hres
= to_integer(ctx
, argv
[1], &d
);
1097 }else if(end
> length
) {
1101 end
= d
< 0.0 ? 0 : length
;
1111 jsstr_t
*retstr
= jsstr_substr(str
, start
, end
-start
);
1114 return E_OUTOFMEMORY
;
1117 *r
= jsval_string(retstr
);
1124 static HRESULT
String_small(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1127 static const WCHAR smalltagW
[] = {'S','M','A','L','L',0};
1128 return do_attributeless_tag_format(ctx
, jsthis
, r
, smalltagW
);
1131 static HRESULT
String_split(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1134 match_state_t match_result
, *match_ptr
= &match_result
;
1135 DWORD length
, i
, match_len
= 0;
1136 const WCHAR
*ptr
, *ptr2
, *str
, *match_str
= NULL
;
1137 unsigned limit
= ~0u;
1138 jsdisp_t
*array
, *regexp
= NULL
;
1139 jsstr_t
*jsstr
, *match_jsstr
, *tmp_str
;
1144 if(argc
!= 1 && argc
!= 2) {
1145 FIXME("unsupported argc %u\n", argc
);
1149 hres
= get_string_flat_val(ctx
, jsthis
, &jsstr
, &str
);
1153 length
= jsstr_length(jsstr
);
1155 if(argc
> 1 && !is_undefined(argv
[1])) {
1156 hres
= to_uint32(ctx
, argv
[1], &limit
);
1158 jsstr_release(jsstr
);
1163 if(is_object_instance(argv
[0])) {
1164 regexp
= iface_to_jsdisp((IUnknown
*)get_object(argv
[0]));
1166 if(!is_class(regexp
, JSCLASS_REGEXP
)) {
1167 jsdisp_release(regexp
);
1174 hres
= to_flat_string(ctx
, argv
[0], &match_jsstr
, &match_str
);
1176 jsstr_release(jsstr
);
1180 match_len
= jsstr_length(match_jsstr
);
1182 jsstr_release(match_jsstr
);
1187 hres
= create_array(ctx
, 0, &array
);
1189 if(SUCCEEDED(hres
)) {
1191 match_result
.cp
= str
;
1192 for(i
=0; i
<limit
; i
++) {
1194 hres
= regexp_match_next(ctx
, regexp
, REM_NO_PARENS
, jsstr
, &match_ptr
);
1197 ptr2
= match_result
.cp
- match_result
.match_len
;
1198 }else if(match_str
) {
1199 ptr2
= strstrW(ptr
, match_str
);
1208 tmp_str
= jsstr_alloc_len(ptr
, ptr2
-ptr
);
1210 hres
= E_OUTOFMEMORY
;
1214 hres
= jsdisp_propput_idx(array
, i
, jsval_string(tmp_str
));
1215 jsstr_release(tmp_str
);
1220 ptr
= match_result
.cp
;
1222 ptr
= ptr2
+ match_len
;
1228 if(SUCCEEDED(hres
) && (match_str
|| regexp
) && i
<limit
) {
1229 DWORD len
= (str
+length
) - ptr
;
1231 if(len
|| match_str
) {
1232 tmp_str
= jsstr_alloc_len(ptr
, len
);
1235 hres
= jsdisp_propput_idx(array
, i
, jsval_string(tmp_str
));
1236 jsstr_release(tmp_str
);
1238 hres
= E_OUTOFMEMORY
;
1244 jsdisp_release(regexp
);
1246 jsstr_release(match_jsstr
);
1247 jsstr_release(jsstr
);
1249 if(SUCCEEDED(hres
) && r
)
1250 *r
= jsval_obj(array
);
1252 jsdisp_release(array
);
1257 static HRESULT
String_strike(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1260 static const WCHAR striketagW
[] = {'S','T','R','I','K','E',0};
1261 return do_attributeless_tag_format(ctx
, jsthis
, r
, striketagW
);
1264 static HRESULT
String_sub(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1267 static const WCHAR subtagW
[] = {'S','U','B',0};
1268 return do_attributeless_tag_format(ctx
, jsthis
, r
, subtagW
);
1271 /* ECMA-262 3rd Edition 15.5.4.15 */
1272 static HRESULT
String_substring(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1275 INT start
=0, end
, length
;
1282 hres
= get_string_val(ctx
, jsthis
, &str
);
1286 length
= jsstr_length(str
);
1288 hres
= to_integer(ctx
, argv
[0], &d
);
1295 start
= is_int32(d
) ? min(length
, d
) : length
;
1299 hres
= to_integer(ctx
, argv
[1], &d
);
1306 end
= is_int32(d
) ? min(length
, d
) : length
;
1320 jsstr_t
*ret
= jsstr_substr(str
, start
, end
-start
);
1322 *r
= jsval_string(ret
);
1324 hres
= E_OUTOFMEMORY
;
1330 /* ECMA-262 3rd Edition B.2.3 */
1331 static HRESULT
String_substr(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1334 int start
=0, len
, length
;
1341 hres
= get_string_val(ctx
, jsthis
, &str
);
1345 length
= jsstr_length(str
);
1347 hres
= to_integer(ctx
, argv
[0], &d
);
1354 start
= is_int32(d
) ? min(length
, d
) : length
;
1358 hres
= to_integer(ctx
, argv
[1], &d
);
1365 len
= is_int32(d
) ? min(length
-start
, d
) : length
-start
;
1374 jsstr_t
*ret
= jsstr_substr(str
, start
, len
);
1376 *r
= jsval_string(ret
);
1378 hres
= E_OUTOFMEMORY
;
1385 static HRESULT
String_sup(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1388 static const WCHAR suptagW
[] = {'S','U','P',0};
1389 return do_attributeless_tag_format(ctx
, jsthis
, r
, suptagW
);
1392 static HRESULT
String_toLowerCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1400 hres
= get_string_val(ctx
, jsthis
, &str
);
1408 buf
= jsstr_alloc_buf(jsstr_length(str
), &ret
);
1411 return E_OUTOFMEMORY
;
1414 jsstr_flush(str
, buf
);
1416 *r
= jsval_string(ret
);
1422 static HRESULT
String_toUpperCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1430 hres
= get_string_val(ctx
, jsthis
, &str
);
1438 buf
= jsstr_alloc_buf(jsstr_length(str
), &ret
);
1441 return E_OUTOFMEMORY
;
1444 jsstr_flush(str
, buf
);
1446 *r
= jsval_string(ret
);
1452 static HRESULT
String_toLocaleLowerCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1459 static HRESULT
String_toLocaleUpperCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1466 static HRESULT
String_localeCompare(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1473 static HRESULT
String_get_value(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
1475 StringInstance
*This
= (StringInstance
*)jsthis
;
1479 *r
= jsval_string(jsstr_addref(This
->str
));
1483 static void String_destructor(jsdisp_t
*dispex
)
1485 StringInstance
*This
= (StringInstance
*)dispex
;
1487 jsstr_release(This
->str
);
1491 static unsigned String_idx_length(jsdisp_t
*jsdisp
)
1493 StringInstance
*string
= (StringInstance
*)jsdisp
;
1496 * NOTE: For invoke version < 2, indexed array is not implemented at all.
1497 * Newer jscript.dll versions implement it on string type, not class,
1498 * which is not how it should work according to spec. IE9 implements it
1499 * properly, but it uses its own JavaScript engine inside MSHTML. We
1500 * implement it here, but in the way IE9 and spec work.
1502 return string
->dispex
.ctx
->version
< 2 ? 0 : jsstr_length(string
->str
);
1505 static HRESULT
String_idx_get(jsdisp_t
*jsdisp
, unsigned idx
, jsval_t
*r
)
1507 StringInstance
*string
= (StringInstance
*)jsdisp
;
1510 ret
= jsstr_substr(string
->str
, idx
, 1);
1512 return E_OUTOFMEMORY
;
1514 TRACE("%p[%u] = %s\n", string
, idx
, debugstr_jsstr(ret
));
1516 *r
= jsval_string(ret
);
1520 static const builtin_prop_t String_props
[] = {
1521 {anchorW
, String_anchor
, PROPF_METHOD
|1},
1522 {bigW
, String_big
, PROPF_METHOD
},
1523 {blinkW
, String_blink
, PROPF_METHOD
},
1524 {boldW
, String_bold
, PROPF_METHOD
},
1525 {charAtW
, String_charAt
, PROPF_METHOD
|1},
1526 {charCodeAtW
, String_charCodeAt
, PROPF_METHOD
|1},
1527 {concatW
, String_concat
, PROPF_METHOD
|1},
1528 {fixedW
, String_fixed
, PROPF_METHOD
},
1529 {fontcolorW
, String_fontcolor
, PROPF_METHOD
|1},
1530 {fontsizeW
, String_fontsize
, PROPF_METHOD
|1},
1531 {indexOfW
, String_indexOf
, PROPF_METHOD
|2},
1532 {italicsW
, String_italics
, PROPF_METHOD
},
1533 {lastIndexOfW
, String_lastIndexOf
, PROPF_METHOD
|2},
1534 {lengthW
, NULL
,0, String_get_length
, String_set_length
},
1535 {linkW
, String_link
, PROPF_METHOD
|1},
1536 {localeCompareW
, String_localeCompare
, PROPF_METHOD
|1},
1537 {matchW
, String_match
, PROPF_METHOD
|1},
1538 {replaceW
, String_replace
, PROPF_METHOD
|1},
1539 {searchW
, String_search
, PROPF_METHOD
},
1540 {sliceW
, String_slice
, PROPF_METHOD
},
1541 {smallW
, String_small
, PROPF_METHOD
},
1542 {splitW
, String_split
, PROPF_METHOD
|2},
1543 {strikeW
, String_strike
, PROPF_METHOD
},
1544 {subW
, String_sub
, PROPF_METHOD
},
1545 {substrW
, String_substr
, PROPF_METHOD
|2},
1546 {substringW
, String_substring
, PROPF_METHOD
|2},
1547 {supW
, String_sup
, PROPF_METHOD
},
1548 {toLocaleLowerCaseW
, String_toLocaleLowerCase
, PROPF_METHOD
},
1549 {toLocaleUpperCaseW
, String_toLocaleUpperCase
, PROPF_METHOD
},
1550 {toLowerCaseW
, String_toLowerCase
, PROPF_METHOD
},
1551 {toStringW
, String_toString
, PROPF_METHOD
},
1552 {toUpperCaseW
, String_toUpperCase
, PROPF_METHOD
},
1553 {valueOfW
, String_valueOf
, PROPF_METHOD
}
1556 static const builtin_info_t String_info
= {
1558 {NULL
, NULL
,0, String_get_value
},
1559 sizeof(String_props
)/sizeof(*String_props
),
1565 static const builtin_prop_t StringInst_props
[] = {
1566 {lengthW
, NULL
,0, String_get_length
, String_set_length
}
1569 static const builtin_info_t StringInst_info
= {
1571 {NULL
, NULL
,0, String_get_value
},
1572 sizeof(StringInst_props
)/sizeof(*StringInst_props
),
1580 /* ECMA-262 3rd Edition 15.5.3.2 */
1581 static HRESULT
StringConstr_fromCharCode(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
,
1582 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
1591 ret_str
= jsstr_alloc_buf(argc
, &ret
);
1593 return E_OUTOFMEMORY
;
1595 for(i
=0; i
<argc
; i
++) {
1596 hres
= to_uint32(ctx
, argv
[i
], &code
);
1606 *r
= jsval_string(ret
);
1612 static HRESULT
StringConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1624 hres
= to_string(ctx
, argv
[0], &str
);
1628 str
= jsstr_empty();
1631 *r
= jsval_string(str
);
1634 case DISPATCH_CONSTRUCT
: {
1639 hres
= to_string(ctx
, argv
[0], &str
);
1643 str
= jsstr_empty();
1646 hres
= create_string(ctx
, str
, &ret
);
1647 if (SUCCEEDED(hres
)) *r
= jsval_obj(ret
);
1653 FIXME("unimplemented flags: %x\n", flags
);
1660 static HRESULT
string_alloc(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsstr_t
*str
, StringInstance
**ret
)
1662 StringInstance
*string
;
1665 string
= heap_alloc_zero(sizeof(StringInstance
));
1667 return E_OUTOFMEMORY
;
1669 if(object_prototype
)
1670 hres
= init_dispex(&string
->dispex
, ctx
, &String_info
, object_prototype
);
1672 hres
= init_dispex_from_constr(&string
->dispex
, ctx
, &StringInst_info
, ctx
->string_constr
);
1678 string
->str
= jsstr_addref(str
);
1683 static const builtin_prop_t StringConstr_props
[] = {
1684 {fromCharCodeW
, StringConstr_fromCharCode
, PROPF_METHOD
},
1687 static const builtin_info_t StringConstr_info
= {
1689 DEFAULT_FUNCTION_VALUE
,
1690 sizeof(StringConstr_props
)/sizeof(*StringConstr_props
),
1696 HRESULT
create_string_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
1698 StringInstance
*string
;
1701 static const WCHAR StringW
[] = {'S','t','r','i','n','g',0};
1703 hres
= string_alloc(ctx
, object_prototype
, jsstr_empty(), &string
);
1707 hres
= create_builtin_constructor(ctx
, StringConstr_value
, StringW
, &StringConstr_info
,
1708 PROPF_CONSTR
|1, &string
->dispex
, ret
);
1710 jsdisp_release(&string
->dispex
);
1714 HRESULT
create_string(script_ctx_t
*ctx
, jsstr_t
*str
, jsdisp_t
**ret
)
1716 StringInstance
*string
;
1719 hres
= string_alloc(ctx
, NULL
, str
, &string
);
1723 *ret
= &string
->dispex
;