2 * Copyright 2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/debug.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
32 static const WCHAR lengthW
[] = {'l','e','n','g','t','h',0};
33 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
34 static const WCHAR valueOfW
[] = {'v','a','l','u','e','O','f',0};
35 static const WCHAR anchorW
[] = {'a','n','c','h','o','r',0};
36 static const WCHAR bigW
[] = {'b','i','g',0};
37 static const WCHAR blinkW
[] = {'b','l','i','n','k',0};
38 static const WCHAR boldW
[] = {'b','o','l','d',0};
39 static const WCHAR charAtW
[] = {'c','h','a','r','A','t',0};
40 static const WCHAR charCodeAtW
[] = {'c','h','a','r','C','o','d','e','A','t',0};
41 static const WCHAR concatW
[] = {'c','o','n','c','a','t',0};
42 static const WCHAR fixedW
[] = {'f','i','x','e','d',0};
43 static const WCHAR fontcolorW
[] = {'f','o','n','t','c','o','l','o','r',0};
44 static const WCHAR fontsizeW
[] = {'f','o','n','t','s','i','z','e',0};
45 static const WCHAR indexOfW
[] = {'i','n','d','e','x','O','f',0};
46 static const WCHAR italicsW
[] = {'i','t','a','l','i','c','s',0};
47 static const WCHAR lastIndexOfW
[] = {'l','a','s','t','I','n','d','e','x','O','f',0};
48 static const WCHAR linkW
[] = {'l','i','n','k',0};
49 static const WCHAR matchW
[] = {'m','a','t','c','h',0};
50 static const WCHAR replaceW
[] = {'r','e','p','l','a','c','e',0};
51 static const WCHAR searchW
[] = {'s','e','a','r','c','h',0};
52 static const WCHAR sliceW
[] = {'s','l','i','c','e',0};
53 static const WCHAR smallW
[] = {'s','m','a','l','l',0};
54 static const WCHAR splitW
[] = {'s','p','l','i','t',0};
55 static const WCHAR strikeW
[] = {'s','t','r','i','k','e',0};
56 static const WCHAR subW
[] = {'s','u','b',0};
57 static const WCHAR substringW
[] = {'s','u','b','s','t','r','i','n','g',0};
58 static const WCHAR substrW
[] = {'s','u','b','s','t','r',0};
59 static const WCHAR supW
[] = {'s','u','p',0};
60 static const WCHAR toLowerCaseW
[] = {'t','o','L','o','w','e','r','C','a','s','e',0};
61 static const WCHAR toUpperCaseW
[] = {'t','o','U','p','p','e','r','C','a','s','e',0};
62 static const WCHAR toLocaleLowerCaseW
[] = {'t','o','L','o','c','a','l','e','L','o','w','e','r','C','a','s','e',0};
63 static const WCHAR toLocaleUpperCaseW
[] = {'t','o','L','o','c','a','l','e','U','p','p','e','r','C','a','s','e',0};
64 static const WCHAR localeCompareW
[] = {'l','o','c','a','l','e','C','o','m','p','a','r','e',0};
65 static const WCHAR fromCharCodeW
[] = {'f','r','o','m','C','h','a','r','C','o','d','e',0};
67 static inline StringInstance
*string_from_vdisp(vdisp_t
*vdisp
)
69 return (StringInstance
*)vdisp
->u
.jsdisp
;
72 static inline StringInstance
*string_this(vdisp_t
*jsthis
)
74 return is_vclass(jsthis
, JSCLASS_STRING
) ? string_from_vdisp(jsthis
) : NULL
;
77 static HRESULT
get_string_val(script_ctx_t
*ctx
, vdisp_t
*jsthis
, jsexcept_t
*ei
,
78 const WCHAR
**str
, DWORD
*len
, BSTR
*val_str
)
80 StringInstance
*string
;
84 if((string
= string_this(jsthis
))) {
86 *len
= string
->length
;
91 V_VT(&this_var
) = VT_DISPATCH
;
92 V_DISPATCH(&this_var
) = jsthis
->u
.disp
;
93 hres
= to_string(ctx
, &this_var
, ei
, val_str
);
98 *len
= SysStringLen(*val_str
);
102 static HRESULT
String_length(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
103 VARIANT
*retv
, jsexcept_t
*ei
)
105 TRACE("%p\n", jsthis
);
108 case DISPATCH_PROPERTYGET
: {
109 StringInstance
*string
= string_from_vdisp(jsthis
);
112 V_I4(retv
) = string
->length
;
116 FIXME("unimplemented flags %x\n", flags
);
123 static HRESULT
stringobj_to_string(vdisp_t
*jsthis
, VARIANT
*retv
)
125 StringInstance
*string
;
127 if(!(string
= string_this(jsthis
))) {
128 WARN("this is not a string object\n");
133 BSTR str
= SysAllocString(string
->str
);
135 return E_OUTOFMEMORY
;
137 V_VT(retv
) = VT_BSTR
;
143 /* ECMA-262 3rd Edition 15.5.4.2 */
144 static HRESULT
String_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
145 VARIANT
*retv
, jsexcept_t
*ei
)
149 return stringobj_to_string(jsthis
, retv
);
152 /* ECMA-262 3rd Edition 15.5.4.2 */
153 static HRESULT
String_valueOf(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
154 VARIANT
*retv
, jsexcept_t
*ei
)
158 return stringobj_to_string(jsthis
, retv
);
161 static HRESULT
do_attributeless_tag_format(script_ctx_t
*ctx
, vdisp_t
*jsthis
, VARIANT
*retv
,
162 jsexcept_t
*ei
, const WCHAR
*tagname
)
169 static const WCHAR tagfmt
[] = {'<','%','s','>','%','s','<','/','%','s','>',0};
171 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
176 BSTR ret
= SysAllocStringLen(NULL
, length
+ 2*strlenW(tagname
) + 5);
178 SysFreeString(val_str
);
179 return E_OUTOFMEMORY
;
182 sprintfW(ret
, tagfmt
, tagname
, str
, tagname
);
184 V_VT(retv
) = VT_BSTR
;
188 SysFreeString(val_str
);
192 static HRESULT
do_attribute_tag_format(script_ctx_t
*ctx
, vdisp_t
*jsthis
, DISPPARAMS
*dp
, VARIANT
*retv
,
193 jsexcept_t
*ei
, const WCHAR
*tagname
, const WCHAR
*attr
)
195 static const WCHAR tagfmtW
[]
196 = {'<','%','s',' ','%','s','=','\"','%','s','\"','>','%','s','<','/','%','s','>',0};
197 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
199 StringInstance
*string
;
202 BSTR attr_value
, val_str
= NULL
;
205 if(!(string
= string_this(jsthis
))) {
208 V_VT(&this) = VT_DISPATCH
;
209 V_DISPATCH(&this) = jsthis
->u
.disp
;
211 hres
= to_string(ctx
, &this, ei
, &val_str
);
216 length
= SysStringLen(val_str
);
220 length
= string
->length
;
224 hres
= to_string(ctx
, get_arg(dp
, 0), ei
, &attr_value
);
226 SysFreeString(val_str
);
231 attr_value
= SysAllocString(undefinedW
);
233 SysFreeString(val_str
);
234 return E_OUTOFMEMORY
;
239 BSTR ret
= SysAllocStringLen(NULL
, length
+ 2*strlenW(tagname
)
240 + strlenW(attr
) + SysStringLen(attr_value
) + 9);
242 SysFreeString(attr_value
);
243 SysFreeString(val_str
);
244 return E_OUTOFMEMORY
;
247 sprintfW(ret
, tagfmtW
, tagname
, attr
, attr_value
, str
, tagname
);
249 V_VT(retv
) = VT_BSTR
;
253 SysFreeString(attr_value
);
254 SysFreeString(val_str
);
258 static HRESULT
String_anchor(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
259 VARIANT
*retv
, jsexcept_t
*ei
)
261 static const WCHAR fontW
[] = {'A',0};
262 static const WCHAR colorW
[] = {'N','A','M','E',0};
264 return do_attribute_tag_format(ctx
, jsthis
, dp
, retv
, ei
, fontW
, colorW
);
267 static HRESULT
String_big(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
268 VARIANT
*retv
, jsexcept_t
*ei
)
270 static const WCHAR bigtagW
[] = {'B','I','G',0};
271 return do_attributeless_tag_format(ctx
, jsthis
, retv
, ei
, bigtagW
);
274 static HRESULT
String_blink(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
275 VARIANT
*retv
, jsexcept_t
*ei
)
277 static const WCHAR blinktagW
[] = {'B','L','I','N','K',0};
278 return do_attributeless_tag_format(ctx
, jsthis
, retv
, ei
, blinktagW
);
281 static HRESULT
String_bold(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
282 VARIANT
*retv
, jsexcept_t
*ei
)
284 static const WCHAR boldtagW
[] = {'B',0};
285 return do_attributeless_tag_format(ctx
, jsthis
, retv
, ei
, boldtagW
);
288 /* ECMA-262 3rd Edition 15.5.4.5 */
289 static HRESULT
String_charAt(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
290 VARIANT
*retv
, jsexcept_t
*ei
)
300 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
308 hres
= to_integer(ctx
, get_arg(dp
, 0), ei
, &num
);
310 SysFreeString(val_str
);
314 pos
= is_int32(d
) ? d
: -1;
318 SysFreeString(val_str
);
322 if(0 <= pos
&& pos
< length
)
323 ret
= SysAllocStringLen(str
+pos
, 1);
325 ret
= SysAllocStringLen(NULL
, 0);
326 SysFreeString(val_str
);
328 return E_OUTOFMEMORY
;
331 V_VT(retv
) = VT_BSTR
;
336 /* ECMA-262 3rd Edition 15.5.4.5 */
337 static HRESULT
String_charCodeAt(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
338 VARIANT
*retv
, jsexcept_t
*ei
)
342 DWORD length
, idx
= 0;
347 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
351 if(arg_cnt(dp
) > 0) {
355 hres
= to_integer(ctx
, get_arg(dp
, 0), ei
, &v
);
357 SysFreeString(val_str
);
363 if(!is_int32(d
) || d
< 0 || d
>= length
) {
364 SysFreeString(val_str
);
375 V_I4(retv
) = str
[idx
];
378 SysFreeString(val_str
);
382 /* ECMA-262 3rd Edition 15.5.4.6 */
383 static HRESULT
String_concat(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
384 VARIANT
*retv
, jsexcept_t
*ei
)
386 BSTR
*strs
= NULL
, ret
= NULL
;
387 DWORD len
= 0, i
, l
, str_cnt
;
394 str_cnt
= arg_cnt(dp
)+1;
395 strs
= heap_alloc_zero(str_cnt
* sizeof(BSTR
));
397 return E_OUTOFMEMORY
;
399 V_VT(&var
) = VT_DISPATCH
;
400 V_DISPATCH(&var
) = jsthis
->u
.disp
;
402 hres
= to_string(ctx
, &var
, ei
, strs
);
403 if(SUCCEEDED(hres
)) {
404 for(i
=0; i
< arg_cnt(dp
); i
++) {
405 hres
= to_string(ctx
, get_arg(dp
, i
), ei
, strs
+i
+1);
411 if(SUCCEEDED(hres
)) {
412 for(i
=0; i
< str_cnt
; i
++)
413 len
+= SysStringLen(strs
[i
]);
415 ptr
= ret
= SysAllocStringLen(NULL
, len
);
417 for(i
=0; i
< str_cnt
; i
++) {
418 l
= SysStringLen(strs
[i
]);
419 memcpy(ptr
, strs
[i
], l
*sizeof(WCHAR
));
424 for(i
=0; i
< str_cnt
; i
++)
425 SysFreeString(strs
[i
]);
432 V_VT(retv
) = VT_BSTR
;
440 static HRESULT
String_fixed(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
441 VARIANT
*retv
, jsexcept_t
*ei
)
443 static const WCHAR fixedtagW
[] = {'T','T',0};
444 return do_attributeless_tag_format(ctx
, jsthis
, retv
, ei
, fixedtagW
);
447 static HRESULT
String_fontcolor(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
448 VARIANT
*retv
, jsexcept_t
*ei
)
450 static const WCHAR fontW
[] = {'F','O','N','T',0};
451 static const WCHAR colorW
[] = {'C','O','L','O','R',0};
453 return do_attribute_tag_format(ctx
, jsthis
, dp
, retv
, ei
, fontW
, colorW
);
456 static HRESULT
String_fontsize(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
457 VARIANT
*retv
, jsexcept_t
*ei
)
459 static const WCHAR fontW
[] = {'F','O','N','T',0};
460 static const WCHAR colorW
[] = {'S','I','Z','E',0};
462 return do_attribute_tag_format(ctx
, jsthis
, dp
, retv
, ei
, fontW
, colorW
);
465 static HRESULT
String_indexOf(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
466 VARIANT
*retv
, jsexcept_t
*ei
)
468 DWORD length
, pos
= 0;
470 BSTR search_str
, val_str
;
476 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
485 SysFreeString(val_str
);
489 hres
= to_string(ctx
, get_arg(dp
,0), ei
, &search_str
);
491 SysFreeString(val_str
);
495 if(arg_cnt(dp
) >= 2) {
499 hres
= to_integer(ctx
, get_arg(dp
,1), ei
, &ival
);
500 if(SUCCEEDED(hres
)) {
503 pos
= is_int32(d
) ? min((int)d
, length
) : length
;
507 if(SUCCEEDED(hres
)) {
510 ptr
= strstrW(str
+pos
, search_str
);
517 SysFreeString(search_str
);
518 SysFreeString(val_str
);
529 static HRESULT
String_italics(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
530 VARIANT
*retv
, jsexcept_t
*ei
)
532 static const WCHAR italicstagW
[] = {'I',0};
533 return do_attributeless_tag_format(ctx
, jsthis
, retv
, ei
, italicstagW
);
536 /* ECMA-262 3rd Edition 15.5.4.8 */
537 static HRESULT
String_lastIndexOf(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
538 VARIANT
*retv
, jsexcept_t
*ei
)
540 BSTR search_str
, val_str
;
541 DWORD length
, pos
= 0, search_len
;
548 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
557 SysFreeString(val_str
);
561 hres
= to_string(ctx
, get_arg(dp
,0), ei
, &search_str
);
563 SysFreeString(val_str
);
567 search_len
= SysStringLen(search_str
);
569 if(arg_cnt(dp
) >= 2) {
573 hres
= to_integer(ctx
, get_arg(dp
,1), ei
, &ival
);
574 if(SUCCEEDED(hres
)) {
577 pos
= is_int32(d
) ? min((int)d
, length
) : length
;
583 if(SUCCEEDED(hres
) && length
>= search_len
) {
586 for(ptr
= str
+min(pos
, length
-search_len
); ptr
>= str
; ptr
--) {
587 if(!memcmp(ptr
, search_str
, search_len
*sizeof(WCHAR
))) {
594 SysFreeString(search_str
);
595 SysFreeString(val_str
);
606 static HRESULT
String_link(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
607 VARIANT
*retv
, jsexcept_t
*ei
)
609 static const WCHAR fontW
[] = {'A',0};
610 static const WCHAR colorW
[] = {'H','R','E','F',0};
612 return do_attribute_tag_format(ctx
, jsthis
, dp
, retv
, ei
, fontW
, colorW
);
615 /* ECMA-262 3rd Edition 15.5.4.10 */
616 static HRESULT
String_match(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
617 VARIANT
*retv
, jsexcept_t
*ei
)
630 V_VT(retv
) = VT_NULL
;
636 arg_var
= get_arg(dp
, 0);
637 switch(V_VT(arg_var
)) {
639 regexp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(arg_var
));
641 if(is_class(regexp
, JSCLASS_REGEXP
))
643 jsdisp_release(regexp
);
648 hres
= to_string(ctx
, arg_var
, ei
, &match_str
);
652 hres
= create_regexp(ctx
, match_str
, SysStringLen(match_str
), 0, ®exp
);
653 SysFreeString(match_str
);
659 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
660 if(SUCCEEDED(hres
)) {
662 val_str
= SysAllocStringLen(str
, length
);
664 hres
= regexp_string_match(ctx
, regexp
, val_str
, retv
, ei
);
666 hres
= E_OUTOFMEMORY
;
669 jsdisp_release(regexp
);
670 SysFreeString(val_str
);
680 static HRESULT
strbuf_append(strbuf_t
*buf
, const WCHAR
*str
, DWORD len
)
685 if(len
+ buf
->len
> buf
->size
) {
689 new_size
= buf
->size
? buf
->size
<<1 : 16;
690 if(new_size
< buf
->len
+len
)
691 new_size
= buf
->len
+len
;
693 new_buf
= heap_realloc(buf
->buf
, new_size
*sizeof(WCHAR
));
695 new_buf
= heap_alloc(new_size
*sizeof(WCHAR
));
697 return E_OUTOFMEMORY
;
700 buf
->size
= new_size
;
703 memcpy(buf
->buf
+buf
->len
, str
, len
*sizeof(WCHAR
));
708 static HRESULT
rep_call(script_ctx_t
*ctx
, jsdisp_t
*func
, const WCHAR
*str
, match_result_t
*match
,
709 match_result_t
*parens
, DWORD parens_cnt
, BSTR
*ret
, jsexcept_t
*ei
)
711 DISPPARAMS dp
= {NULL
, NULL
, 0, 0};
712 VARIANTARG
*args
, *arg
;
717 dp
.cArgs
= parens_cnt
+3;
718 dp
.rgvarg
= args
= heap_alloc_zero(sizeof(VARIANT
)*dp
.cArgs
);
720 return E_OUTOFMEMORY
;
722 arg
= get_arg(&dp
,0);
724 V_BSTR(arg
) = SysAllocStringLen(match
->str
, match
->len
);
726 hres
= E_OUTOFMEMORY
;
728 if(SUCCEEDED(hres
)) {
729 for(i
=0; i
< parens_cnt
; i
++) {
730 arg
= get_arg(&dp
,i
+1);
732 V_BSTR(arg
) = SysAllocStringLen(parens
[i
].str
, parens
[i
].len
);
734 hres
= E_OUTOFMEMORY
;
740 if(SUCCEEDED(hres
)) {
741 arg
= get_arg(&dp
,parens_cnt
+1);
743 V_I4(arg
) = match
->str
- str
;
745 arg
= get_arg(&dp
,parens_cnt
+2);
747 V_BSTR(arg
) = SysAllocString(str
);
749 hres
= E_OUTOFMEMORY
;
753 hres
= jsdisp_call_value(func
, DISPATCH_METHOD
, &dp
, &var
, ei
);
755 for(i
=0; i
< parens_cnt
+3; i
++) {
756 if(i
!= parens_cnt
+1)
757 SysFreeString(V_BSTR(get_arg(&dp
,i
)));
764 hres
= to_string(ctx
, &var
, ei
, ret
);
769 /* ECMA-262 3rd Edition 15.5.4.11 */
770 static HRESULT
String_replace(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
771 VARIANT
*retv
, jsexcept_t
*ei
)
774 DWORD parens_cnt
= 0, parens_size
=0, rep_len
=0, length
;
775 BSTR rep_str
= NULL
, match_str
= NULL
, ret_str
, val_str
;
776 jsdisp_t
*rep_func
= NULL
, *regexp
= NULL
;
777 match_result_t
*parens
= NULL
, match
= {NULL
,0}, **parens_ptr
= &parens
;
778 strbuf_t ret
= {NULL
,0,0};
779 DWORD re_flags
= REM_NO_CTX_UPDATE
;
785 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
792 val_str
= SysAllocStringLen(str
, length
);
794 return E_OUTOFMEMORY
;
797 V_VT(retv
) = VT_BSTR
;
798 V_BSTR(retv
) = val_str
;
803 arg_var
= get_arg(dp
, 0);
804 switch(V_VT(arg_var
)) {
806 regexp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(arg_var
));
808 if(is_class(regexp
, JSCLASS_REGEXP
)) {
811 jsdisp_release(regexp
);
817 hres
= to_string(ctx
, arg_var
, ei
, &match_str
);
819 SysFreeString(val_str
);
824 if(arg_cnt(dp
) >= 2) {
825 arg_var
= get_arg(dp
,1);
826 switch(V_VT(arg_var
)) {
828 rep_func
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(arg_var
));
830 if(is_class(rep_func
, JSCLASS_FUNCTION
)) {
833 jsdisp_release(rep_func
);
839 hres
= to_string(ctx
, arg_var
, ei
, &rep_str
);
843 rep_len
= SysStringLen(rep_str
);
844 if(!strchrW(rep_str
, '$'))
849 if(SUCCEEDED(hres
)) {
850 const WCHAR
*cp
, *ecp
;
856 hres
= regexp_match_next(ctx
, regexp
, re_flags
, str
, length
, &cp
, parens_ptr
,
857 &parens_size
, &parens_cnt
, &match
);
858 re_flags
|= REM_CHECK_GLOBAL
;
860 if(hres
== S_FALSE
) {
870 match
.str
= strstrW(cp
, match_str
);
873 match
.len
= SysStringLen(match_str
);
874 cp
= match
.str
+match
.len
;
877 hres
= strbuf_append(&ret
, ecp
, match
.str
-ecp
);
878 ecp
= match
.str
+match
.len
;
885 hres
= rep_call(ctx
, rep_func
, str
, &match
, parens
, parens_cnt
, &cstr
, ei
);
889 hres
= strbuf_append(&ret
, cstr
, SysStringLen(cstr
));
893 }else if(rep_str
&& regexp
) {
894 const WCHAR
*ptr
= rep_str
, *ptr2
;
896 while((ptr2
= strchrW(ptr
, '$'))) {
897 hres
= strbuf_append(&ret
, ptr
, ptr2
-ptr
);
903 hres
= strbuf_append(&ret
, ptr2
, 1);
907 hres
= strbuf_append(&ret
, match
.str
, match
.len
);
911 hres
= strbuf_append(&ret
, str
, match
.str
-str
);
915 hres
= strbuf_append(&ret
, ecp
, (str
+length
)-ecp
);
921 if(!isdigitW(ptr2
[1])) {
922 hres
= strbuf_append(&ret
, ptr2
, 1);
928 if(isdigitW(ptr2
[2]) && idx
*10 + (ptr2
[2]-'0') <= parens_cnt
) {
929 idx
= idx
*10 + (ptr
[2]-'0');
931 }else if(idx
&& idx
<= parens_cnt
) {
934 hres
= strbuf_append(&ret
, ptr2
, 1);
939 hres
= strbuf_append(&ret
, parens
[idx
-1].str
, parens
[idx
-1].len
);
948 hres
= strbuf_append(&ret
, ptr
, (rep_str
+rep_len
)-ptr
);
952 hres
= strbuf_append(&ret
, rep_str
, rep_len
);
956 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d'};
958 hres
= strbuf_append(&ret
, undefinedW
, sizeof(undefinedW
)/sizeof(WCHAR
));
968 hres
= strbuf_append(&ret
, ecp
, (str
+length
)-ecp
);
972 jsdisp_release(rep_func
);
973 SysFreeString(rep_str
);
974 SysFreeString(match_str
);
977 if(SUCCEEDED(hres
) && match
.str
&& regexp
) {
979 val_str
= SysAllocStringLen(str
, length
);
981 SysFreeString(ctx
->last_match
);
982 ctx
->last_match
= val_str
;
984 ctx
->last_match_index
= match
.str
-str
;
985 ctx
->last_match_length
= match
.len
;
987 hres
= E_OUTOFMEMORY
;
992 jsdisp_release(regexp
);
993 SysFreeString(val_str
);
995 if(SUCCEEDED(hres
) && retv
) {
996 ret_str
= SysAllocStringLen(ret
.buf
, ret
.len
);
998 return E_OUTOFMEMORY
;
1000 V_VT(retv
) = VT_BSTR
;
1001 V_BSTR(retv
) = ret_str
;
1002 TRACE("= %s\n", debugstr_w(ret_str
));
1009 static HRESULT
String_search(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1010 VARIANT
*retv
, jsexcept_t
*ei
)
1012 jsdisp_t
*regexp
= NULL
;
1013 const WCHAR
*str
, *cp
;
1014 match_result_t match
;
1022 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
1028 V_VT(retv
) = VT_NULL
;
1029 SysFreeString(val_str
);
1033 arg
= get_arg(dp
,0);
1034 if(V_VT(arg
) == VT_DISPATCH
) {
1035 regexp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(arg
));
1037 if(!is_class(regexp
, JSCLASS_REGEXP
)) {
1038 jsdisp_release(regexp
);
1045 hres
= create_regexp_var(ctx
, arg
, NULL
, ®exp
);
1047 SysFreeString(val_str
);
1053 hres
= regexp_match_next(ctx
, regexp
, REM_RESET_INDEX
, str
, length
, &cp
, NULL
, NULL
, NULL
, &match
);
1054 SysFreeString(val_str
);
1055 jsdisp_release(regexp
);
1061 V_I4(retv
) = hres
== S_OK
? match
.str
-str
: -1;
1066 /* ECMA-262 3rd Edition 15.5.4.13 */
1067 static HRESULT
String_slice(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1068 VARIANT
*retv
, jsexcept_t
*ei
)
1080 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
1085 hres
= to_integer(ctx
, get_arg(dp
,0), ei
, &v
);
1087 SysFreeString(val_str
);
1096 start
= length
+ start
;
1099 }else if(start
> length
) {
1107 if(arg_cnt(dp
) >= 2) {
1108 hres
= to_integer(ctx
, get_arg(dp
,1), ei
, &v
);
1110 SysFreeString(val_str
);
1122 }else if(end
> length
) {
1126 end
= d
< 0.0 ? 0 : length
;
1136 BSTR retstr
= SysAllocStringLen(str
+start
, end
-start
);
1138 SysFreeString(val_str
);
1139 return E_OUTOFMEMORY
;
1142 V_VT(retv
) = VT_BSTR
;
1143 V_BSTR(retv
) = retstr
;
1146 SysFreeString(val_str
);
1150 static HRESULT
String_small(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1151 VARIANT
*retv
, jsexcept_t
*ei
)
1153 static const WCHAR smalltagW
[] = {'S','M','A','L','L',0};
1154 return do_attributeless_tag_format(ctx
, jsthis
, retv
, ei
, smalltagW
);
1157 static HRESULT
String_split(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1158 VARIANT
*retv
, jsexcept_t
*ei
)
1160 match_result_t
*match_result
= NULL
;
1161 DWORD length
, match_cnt
, i
, match_len
= 0;
1162 const WCHAR
*str
, *ptr
, *ptr2
;
1163 BOOL use_regexp
= FALSE
;
1166 BSTR val_str
, match_str
= NULL
;
1171 if(arg_cnt(dp
) != 1) {
1172 FIXME("unsupported args\n");
1176 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
1180 arg
= get_arg(dp
, 0);
1185 regexp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(arg
));
1187 if(is_class(regexp
, JSCLASS_REGEXP
)) {
1189 hres
= regexp_match(ctx
, regexp
, str
, length
, TRUE
, &match_result
, &match_cnt
);
1190 jsdisp_release(regexp
);
1192 SysFreeString(val_str
);
1197 jsdisp_release(regexp
);
1201 hres
= to_string(ctx
, arg
, ei
, &match_str
);
1203 SysFreeString(val_str
);
1207 match_len
= SysStringLen(match_str
);
1209 SysFreeString(match_str
);
1214 hres
= create_array(ctx
, 0, &array
);
1216 if(SUCCEEDED(hres
)) {
1222 ptr2
= match_result
[i
].str
;
1223 }else if(match_str
) {
1224 ptr2
= strstrW(ptr
, match_str
);
1233 V_VT(&var
) = VT_BSTR
;
1234 V_BSTR(&var
) = SysAllocStringLen(ptr
, ptr2
-ptr
);
1236 hres
= E_OUTOFMEMORY
;
1240 hres
= jsdisp_propput_idx(array
, i
, &var
, ei
);
1241 SysFreeString(V_BSTR(&var
));
1246 ptr
= match_result
[i
].str
+ match_result
[i
].len
;
1248 ptr
= ptr2
+ match_len
;
1254 if(SUCCEEDED(hres
) && (match_str
|| use_regexp
)) {
1255 DWORD len
= (str
+length
) - ptr
;
1257 if(len
|| match_str
) {
1258 V_VT(&var
) = VT_BSTR
;
1259 V_BSTR(&var
) = SysAllocStringLen(ptr
, len
);
1262 hres
= jsdisp_propput_idx(array
, i
, &var
, ei
);
1263 SysFreeString(V_BSTR(&var
));
1265 hres
= E_OUTOFMEMORY
;
1270 SysFreeString(match_str
);
1271 SysFreeString(val_str
);
1272 heap_free(match_result
);
1274 if(SUCCEEDED(hres
) && retv
)
1275 var_set_jsdisp(retv
, array
);
1277 jsdisp_release(array
);
1282 static HRESULT
String_strike(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1283 VARIANT
*retv
, jsexcept_t
*ei
)
1285 static const WCHAR striketagW
[] = {'S','T','R','I','K','E',0};
1286 return do_attributeless_tag_format(ctx
, jsthis
, retv
, ei
, striketagW
);
1289 static HRESULT
String_sub(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1290 VARIANT
*retv
, jsexcept_t
*ei
)
1292 static const WCHAR subtagW
[] = {'S','U','B',0};
1293 return do_attributeless_tag_format(ctx
, jsthis
, retv
, ei
, subtagW
);
1296 /* ECMA-262 3rd Edition 15.5.4.15 */
1297 static HRESULT
String_substring(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1298 VARIANT
*retv
, jsexcept_t
*ei
)
1309 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
1313 if(arg_cnt(dp
) >= 1) {
1314 hres
= to_integer(ctx
, get_arg(dp
,0), ei
, &v
);
1316 SysFreeString(val_str
);
1320 if(V_VT(&v
) == VT_I4
) {
1324 else if(start
>= length
)
1327 start
= V_R8(&v
) < 0.0 ? 0 : length
;
1331 if(arg_cnt(dp
) >= 2) {
1332 hres
= to_integer(ctx
, get_arg(dp
,1), ei
, &v
);
1334 SysFreeString(val_str
);
1338 if(V_VT(&v
) == VT_I4
) {
1342 else if(end
> length
)
1345 end
= V_R8(&v
) < 0.0 ? 0 : length
;
1358 V_VT(retv
) = VT_BSTR
;
1359 V_BSTR(retv
) = SysAllocStringLen(str
+start
, end
-start
);
1361 SysFreeString(val_str
);
1362 return E_OUTOFMEMORY
;
1365 SysFreeString(val_str
);
1369 /* ECMA-262 3rd Edition B.2.3 */
1370 static HRESULT
String_substr(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1371 VARIANT
*retv
, jsexcept_t
*ei
)
1382 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
1386 if(arg_cnt(dp
) >= 1) {
1387 hres
= to_integer(ctx
, get_arg(dp
,0), ei
, &v
);
1389 SysFreeString(val_str
);
1393 if(V_VT(&v
) == VT_I4
) {
1397 else if(start
>= length
)
1400 start
= V_R8(&v
) < 0.0 ? 0 : length
;
1404 if(arg_cnt(dp
) >= 2) {
1405 hres
= to_integer(ctx
, get_arg(dp
,1), ei
, &v
);
1407 SysFreeString(val_str
);
1411 if(V_VT(&v
) == VT_I4
) {
1415 else if(len
> length
-start
)
1418 len
= V_R8(&v
) < 0.0 ? 0 : length
-start
;
1426 V_VT(retv
) = VT_BSTR
;
1427 V_BSTR(retv
) = SysAllocStringLen(str
+start
, len
);
1429 hres
= E_OUTOFMEMORY
;
1432 SysFreeString(val_str
);
1436 static HRESULT
String_sup(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1437 VARIANT
*retv
, jsexcept_t
*ei
)
1439 static const WCHAR suptagW
[] = {'S','U','P',0};
1440 return do_attributeless_tag_format(ctx
, jsthis
, retv
, ei
, suptagW
);
1443 static HRESULT
String_toLowerCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1444 VARIANT
*retv
, jsexcept_t
*ei
)
1453 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
1459 val_str
= SysAllocStringLen(str
, length
);
1461 return E_OUTOFMEMORY
;
1466 V_VT(retv
) = VT_BSTR
;
1467 V_BSTR(retv
) = val_str
;
1469 else SysFreeString(val_str
);
1473 static HRESULT
String_toUpperCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1474 VARIANT
*retv
, jsexcept_t
*ei
)
1483 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
1489 val_str
= SysAllocStringLen(str
, length
);
1491 return E_OUTOFMEMORY
;
1496 V_VT(retv
) = VT_BSTR
;
1497 V_BSTR(retv
) = val_str
;
1499 else SysFreeString(val_str
);
1503 static HRESULT
String_toLocaleLowerCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1504 VARIANT
*retv
, jsexcept_t
*ei
)
1510 static HRESULT
String_toLocaleUpperCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1511 VARIANT
*retv
, jsexcept_t
*ei
)
1517 static HRESULT
String_localeCompare(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1518 VARIANT
*retv
, jsexcept_t
*ei
)
1524 static HRESULT
String_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1525 VARIANT
*retv
, jsexcept_t
*ei
)
1527 StringInstance
*This
= string_from_vdisp(jsthis
);
1533 return throw_type_error(ctx
, ei
, JS_E_FUNCTION_EXPECTED
, NULL
);
1534 case DISPATCH_PROPERTYGET
: {
1535 BSTR str
= SysAllocString(This
->str
);
1537 return E_OUTOFMEMORY
;
1539 V_VT(retv
) = VT_BSTR
;
1544 FIXME("flags %x\n", flags
);
1551 static void String_destructor(jsdisp_t
*dispex
)
1553 StringInstance
*This
= (StringInstance
*)dispex
;
1555 heap_free(This
->str
);
1559 static const builtin_prop_t String_props
[] = {
1560 {anchorW
, String_anchor
, PROPF_METHOD
|1},
1561 {bigW
, String_big
, PROPF_METHOD
},
1562 {blinkW
, String_blink
, PROPF_METHOD
},
1563 {boldW
, String_bold
, PROPF_METHOD
},
1564 {charAtW
, String_charAt
, PROPF_METHOD
|1},
1565 {charCodeAtW
, String_charCodeAt
, PROPF_METHOD
|1},
1566 {concatW
, String_concat
, PROPF_METHOD
|1},
1567 {fixedW
, String_fixed
, PROPF_METHOD
},
1568 {fontcolorW
, String_fontcolor
, PROPF_METHOD
|1},
1569 {fontsizeW
, String_fontsize
, PROPF_METHOD
|1},
1570 {indexOfW
, String_indexOf
, PROPF_METHOD
|2},
1571 {italicsW
, String_italics
, PROPF_METHOD
},
1572 {lastIndexOfW
, String_lastIndexOf
, PROPF_METHOD
|2},
1573 {lengthW
, String_length
, 0},
1574 {linkW
, String_link
, PROPF_METHOD
|1},
1575 {localeCompareW
, String_localeCompare
, PROPF_METHOD
|1},
1576 {matchW
, String_match
, PROPF_METHOD
|1},
1577 {replaceW
, String_replace
, PROPF_METHOD
|1},
1578 {searchW
, String_search
, PROPF_METHOD
},
1579 {sliceW
, String_slice
, PROPF_METHOD
},
1580 {smallW
, String_small
, PROPF_METHOD
},
1581 {splitW
, String_split
, PROPF_METHOD
|2},
1582 {strikeW
, String_strike
, PROPF_METHOD
},
1583 {subW
, String_sub
, PROPF_METHOD
},
1584 {substrW
, String_substr
, PROPF_METHOD
|2},
1585 {substringW
, String_substring
, PROPF_METHOD
|2},
1586 {supW
, String_sup
, PROPF_METHOD
},
1587 {toLocaleLowerCaseW
, String_toLocaleLowerCase
, PROPF_METHOD
},
1588 {toLocaleUpperCaseW
, String_toLocaleUpperCase
, PROPF_METHOD
},
1589 {toLowerCaseW
, String_toLowerCase
, PROPF_METHOD
},
1590 {toStringW
, String_toString
, PROPF_METHOD
},
1591 {toUpperCaseW
, String_toUpperCase
, PROPF_METHOD
},
1592 {valueOfW
, String_valueOf
, PROPF_METHOD
}
1595 static const builtin_info_t String_info
= {
1597 {NULL
, String_value
, 0},
1598 sizeof(String_props
)/sizeof(*String_props
),
1604 /* ECMA-262 3rd Edition 15.5.3.2 */
1605 static HRESULT
StringConstr_fromCharCode(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
,
1606 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
)
1612 ret
= SysAllocStringLen(NULL
, arg_cnt(dp
));
1614 return E_OUTOFMEMORY
;
1616 for(i
=0; i
<arg_cnt(dp
); i
++) {
1617 hres
= to_uint32(ctx
, get_arg(dp
, i
), ei
, &code
);
1627 V_VT(retv
) = VT_BSTR
;
1630 else SysFreeString(ret
);
1635 static HRESULT
StringConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1636 VARIANT
*retv
, jsexcept_t
*ei
)
1647 hres
= to_string(ctx
, get_arg(dp
, 0), ei
, &str
);
1651 str
= SysAllocStringLen(NULL
, 0);
1653 return E_OUTOFMEMORY
;
1656 V_VT(retv
) = VT_BSTR
;
1660 case DISPATCH_CONSTRUCT
: {
1666 hres
= to_string(ctx
, get_arg(dp
, 0), ei
, &str
);
1670 hres
= create_string(ctx
, str
, SysStringLen(str
), &ret
);
1673 hres
= create_string(ctx
, NULL
, 0, &ret
);
1679 var_set_jsdisp(retv
, ret
);
1684 FIXME("unimplemented flags: %x\n", flags
);
1691 static HRESULT
string_alloc(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, StringInstance
**ret
)
1693 StringInstance
*string
;
1696 string
= heap_alloc_zero(sizeof(StringInstance
));
1698 return E_OUTOFMEMORY
;
1700 if(object_prototype
)
1701 hres
= init_dispex(&string
->dispex
, ctx
, &String_info
, object_prototype
);
1703 hres
= init_dispex_from_constr(&string
->dispex
, ctx
, &String_info
, ctx
->string_constr
);
1713 static const builtin_prop_t StringConstr_props
[] = {
1714 {fromCharCodeW
, StringConstr_fromCharCode
, PROPF_METHOD
},
1717 static const builtin_info_t StringConstr_info
= {
1719 {NULL
, Function_value
, 0},
1720 sizeof(StringConstr_props
)/sizeof(*StringConstr_props
),
1726 HRESULT
create_string_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
1728 StringInstance
*string
;
1731 static const WCHAR StringW
[] = {'S','t','r','i','n','g',0};
1733 hres
= string_alloc(ctx
, object_prototype
, &string
);
1737 hres
= create_builtin_function(ctx
, StringConstr_value
, StringW
, &StringConstr_info
,
1738 PROPF_CONSTR
|1, &string
->dispex
, ret
);
1740 jsdisp_release(&string
->dispex
);
1744 HRESULT
create_string(script_ctx_t
*ctx
, const WCHAR
*str
, DWORD len
, jsdisp_t
**ret
)
1746 StringInstance
*string
;
1749 hres
= string_alloc(ctx
, NULL
, &string
);
1756 string
->length
= len
;
1757 string
->str
= heap_alloc((len
+1)*sizeof(WCHAR
));
1759 jsdisp_release(&string
->dispex
);
1760 return E_OUTOFMEMORY
;
1763 memcpy(string
->str
, str
, len
*sizeof(WCHAR
));
1764 string
->str
[len
] = 0;
1766 *ret
= &string
->dispex
;