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"
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
35 static const WCHAR lengthW
[] = {'l','e','n','g','t','h',0};
36 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
37 static const WCHAR valueOfW
[] = {'v','a','l','u','e','O','f',0};
38 static const WCHAR anchorW
[] = {'a','n','c','h','o','r',0};
39 static const WCHAR bigW
[] = {'b','i','g',0};
40 static const WCHAR blinkW
[] = {'b','l','i','n','k',0};
41 static const WCHAR boldW
[] = {'b','o','l','d',0};
42 static const WCHAR charAtW
[] = {'c','h','a','r','A','t',0};
43 static const WCHAR charCodeAtW
[] = {'c','h','a','r','C','o','d','e','A','t',0};
44 static const WCHAR concatW
[] = {'c','o','n','c','a','t',0};
45 static const WCHAR fixedW
[] = {'f','i','x','e','d',0};
46 static const WCHAR fontcolorW
[] = {'f','o','n','t','c','o','l','o','r',0};
47 static const WCHAR fontsizeW
[] = {'f','o','n','t','s','i','z','e',0};
48 static const WCHAR indexOfW
[] = {'i','n','d','e','x','O','f',0};
49 static const WCHAR italicsW
[] = {'i','t','a','l','i','c','s',0};
50 static const WCHAR lastIndexOfW
[] = {'l','a','s','t','I','n','d','e','x','O','f',0};
51 static const WCHAR linkW
[] = {'l','i','n','k',0};
52 static const WCHAR matchW
[] = {'m','a','t','c','h',0};
53 static const WCHAR replaceW
[] = {'r','e','p','l','a','c','e',0};
54 static const WCHAR searchW
[] = {'s','e','a','r','c','h',0};
55 static const WCHAR sliceW
[] = {'s','l','i','c','e',0};
56 static const WCHAR smallW
[] = {'s','m','a','l','l',0};
57 static const WCHAR splitW
[] = {'s','p','l','i','t',0};
58 static const WCHAR strikeW
[] = {'s','t','r','i','k','e',0};
59 static const WCHAR subW
[] = {'s','u','b',0};
60 static const WCHAR substringW
[] = {'s','u','b','s','t','r','i','n','g',0};
61 static const WCHAR substrW
[] = {'s','u','b','s','t','r',0};
62 static const WCHAR supW
[] = {'s','u','p',0};
63 static const WCHAR toLowerCaseW
[] = {'t','o','L','o','w','e','r','C','a','s','e',0};
64 static const WCHAR toUpperCaseW
[] = {'t','o','U','p','p','e','r','C','a','s','e',0};
65 static const WCHAR toLocaleLowerCaseW
[] = {'t','o','L','o','c','a','l','e','L','o','w','e','r','C','a','s','e',0};
66 static const WCHAR toLocaleUpperCaseW
[] = {'t','o','L','o','c','a','l','e','U','p','p','e','r','C','a','s','e',0};
67 static const WCHAR localeCompareW
[] = {'l','o','c','a','l','e','C','o','m','p','a','r','e',0};
68 static const WCHAR fromCharCodeW
[] = {'f','r','o','m','C','h','a','r','C','o','d','e',0};
70 static inline StringInstance
*string_from_vdisp(vdisp_t
*vdisp
)
72 return (StringInstance
*)vdisp
->u
.jsdisp
;
75 static inline StringInstance
*string_this(vdisp_t
*jsthis
)
77 return is_vclass(jsthis
, JSCLASS_STRING
) ? string_from_vdisp(jsthis
) : NULL
;
80 static HRESULT
get_string_val(script_ctx_t
*ctx
, vdisp_t
*jsthis
, jsexcept_t
*ei
,
81 const WCHAR
**str
, DWORD
*len
, BSTR
*val_str
)
83 StringInstance
*string
;
87 if((string
= string_this(jsthis
))) {
89 *len
= string
->length
;
94 V_VT(&this_var
) = VT_DISPATCH
;
95 V_DISPATCH(&this_var
) = jsthis
->u
.disp
;
96 hres
= to_string(ctx
, &this_var
, ei
, val_str
);
101 *len
= SysStringLen(*val_str
);
105 static HRESULT
String_length(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
106 VARIANT
*retv
, jsexcept_t
*ei
)
108 TRACE("%p\n", jsthis
);
111 case DISPATCH_PROPERTYGET
: {
112 StringInstance
*string
= string_from_vdisp(jsthis
);
114 num_set_int(retv
, string
->length
);
118 FIXME("unimplemented flags %x\n", flags
);
125 static HRESULT
stringobj_to_string(vdisp_t
*jsthis
, VARIANT
*retv
)
127 StringInstance
*string
;
129 if(!(string
= string_this(jsthis
))) {
130 WARN("this is not a string object\n");
135 BSTR str
= SysAllocString(string
->str
);
137 return E_OUTOFMEMORY
;
139 V_VT(retv
) = VT_BSTR
;
145 /* ECMA-262 3rd Edition 15.5.4.2 */
146 static HRESULT
String_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
147 VARIANT
*retv
, jsexcept_t
*ei
)
151 return stringobj_to_string(jsthis
, retv
);
154 /* ECMA-262 3rd Edition 15.5.4.2 */
155 static HRESULT
String_valueOf(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
156 VARIANT
*retv
, jsexcept_t
*ei
)
160 return stringobj_to_string(jsthis
, retv
);
163 static HRESULT
do_attributeless_tag_format(script_ctx_t
*ctx
, vdisp_t
*jsthis
, VARIANT
*retv
,
164 jsexcept_t
*ei
, const WCHAR
*tagname
)
171 static const WCHAR tagfmt
[] = {'<','%','s','>','%','s','<','/','%','s','>',0};
173 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
178 BSTR ret
= SysAllocStringLen(NULL
, length
+ 2*strlenW(tagname
) + 5);
180 SysFreeString(val_str
);
181 return E_OUTOFMEMORY
;
184 sprintfW(ret
, tagfmt
, tagname
, str
, tagname
);
186 V_VT(retv
) = VT_BSTR
;
190 SysFreeString(val_str
);
194 static HRESULT
do_attribute_tag_format(script_ctx_t
*ctx
, vdisp_t
*jsthis
, unsigned argc
, VARIANT
*argv
, VARIANT
*retv
,
195 jsexcept_t
*ei
, const WCHAR
*tagname
, const WCHAR
*attr
)
197 static const WCHAR tagfmtW
[]
198 = {'<','%','s',' ','%','s','=','\"','%','s','\"','>','%','s','<','/','%','s','>',0};
199 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
201 StringInstance
*string
;
204 BSTR attr_value
, val_str
= NULL
;
207 if(!(string
= string_this(jsthis
))) {
210 V_VT(&this) = VT_DISPATCH
;
211 V_DISPATCH(&this) = jsthis
->u
.disp
;
213 hres
= to_string(ctx
, &this, ei
, &val_str
);
218 length
= SysStringLen(val_str
);
222 length
= string
->length
;
226 hres
= to_string(ctx
, argv
, ei
, &attr_value
);
228 SysFreeString(val_str
);
233 attr_value
= SysAllocString(undefinedW
);
235 SysFreeString(val_str
);
236 return E_OUTOFMEMORY
;
241 BSTR ret
= SysAllocStringLen(NULL
, length
+ 2*strlenW(tagname
)
242 + strlenW(attr
) + SysStringLen(attr_value
) + 9);
244 SysFreeString(attr_value
);
245 SysFreeString(val_str
);
246 return E_OUTOFMEMORY
;
249 sprintfW(ret
, tagfmtW
, tagname
, attr
, attr_value
, str
, tagname
);
251 V_VT(retv
) = VT_BSTR
;
255 SysFreeString(attr_value
);
256 SysFreeString(val_str
);
260 static HRESULT
String_anchor(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
261 VARIANT
*retv
, jsexcept_t
*ei
)
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
, retv
, ei
, fontW
, colorW
);
269 static HRESULT
String_big(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
270 VARIANT
*retv
, jsexcept_t
*ei
)
272 static const WCHAR bigtagW
[] = {'B','I','G',0};
273 return do_attributeless_tag_format(ctx
, jsthis
, retv
, ei
, bigtagW
);
276 static HRESULT
String_blink(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
277 VARIANT
*retv
, jsexcept_t
*ei
)
279 static const WCHAR blinktagW
[] = {'B','L','I','N','K',0};
280 return do_attributeless_tag_format(ctx
, jsthis
, retv
, ei
, blinktagW
);
283 static HRESULT
String_bold(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
284 VARIANT
*retv
, jsexcept_t
*ei
)
286 static const WCHAR boldtagW
[] = {'B',0};
287 return do_attributeless_tag_format(ctx
, jsthis
, retv
, ei
, 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
, VARIANT
*argv
,
292 VARIANT
*retv
, jsexcept_t
*ei
)
302 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
309 hres
= to_integer(ctx
, argv
, ei
, &d
);
311 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
, unsigned argc
, VARIANT
*argv
,
338 VARIANT
*retv
, jsexcept_t
*ei
)
342 DWORD length
, idx
= 0;
347 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
354 hres
= to_integer(ctx
, argv
, ei
, &d
);
356 SysFreeString(val_str
);
360 if(!is_int32(d
) || d
< 0 || d
>= length
) {
361 SysFreeString(val_str
);
363 num_set_val(retv
, NAN
);
371 num_set_int(retv
, str
[idx
]);
373 SysFreeString(val_str
);
377 /* ECMA-262 3rd Edition 15.5.4.6 */
378 static HRESULT
String_concat(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
379 VARIANT
*retv
, jsexcept_t
*ei
)
381 BSTR
*strs
= NULL
, ret
= NULL
;
382 DWORD len
= 0, i
, l
, str_cnt
;
390 strs
= heap_alloc_zero(str_cnt
* sizeof(BSTR
));
392 return E_OUTOFMEMORY
;
394 V_VT(&var
) = VT_DISPATCH
;
395 V_DISPATCH(&var
) = jsthis
->u
.disp
;
397 hres
= to_string(ctx
, &var
, ei
, strs
);
398 if(SUCCEEDED(hres
)) {
399 for(i
=0; i
< argc
; i
++) {
400 hres
= to_string(ctx
, argv
+i
, ei
, strs
+i
+1);
406 if(SUCCEEDED(hres
)) {
407 for(i
=0; i
< str_cnt
; i
++)
408 len
+= SysStringLen(strs
[i
]);
410 ptr
= ret
= SysAllocStringLen(NULL
, len
);
412 for(i
=0; i
< str_cnt
; i
++) {
413 l
= SysStringLen(strs
[i
]);
414 memcpy(ptr
, strs
[i
], l
*sizeof(WCHAR
));
419 for(i
=0; i
< str_cnt
; i
++)
420 SysFreeString(strs
[i
]);
427 V_VT(retv
) = VT_BSTR
;
435 static HRESULT
String_fixed(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
436 VARIANT
*retv
, jsexcept_t
*ei
)
438 static const WCHAR fixedtagW
[] = {'T','T',0};
439 return do_attributeless_tag_format(ctx
, jsthis
, retv
, ei
, fixedtagW
);
442 static HRESULT
String_fontcolor(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
443 VARIANT
*retv
, jsexcept_t
*ei
)
445 static const WCHAR fontW
[] = {'F','O','N','T',0};
446 static const WCHAR colorW
[] = {'C','O','L','O','R',0};
448 return do_attribute_tag_format(ctx
, jsthis
, argc
, argv
, retv
, ei
, fontW
, colorW
);
451 static HRESULT
String_fontsize(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
452 VARIANT
*retv
, jsexcept_t
*ei
)
454 static const WCHAR fontW
[] = {'F','O','N','T',0};
455 static const WCHAR colorW
[] = {'S','I','Z','E',0};
457 return do_attribute_tag_format(ctx
, jsthis
, argc
, argv
, retv
, ei
, fontW
, colorW
);
460 static HRESULT
String_indexOf(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
461 VARIANT
*retv
, jsexcept_t
*ei
)
463 DWORD length
, pos
= 0;
465 BSTR search_str
, val_str
;
471 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
477 num_set_int(retv
, -1);
478 SysFreeString(val_str
);
482 hres
= to_string(ctx
, argv
, ei
, &search_str
);
484 SysFreeString(val_str
);
491 hres
= to_integer(ctx
, argv
+1, ei
, &d
);
492 if(SUCCEEDED(hres
) && d
> 0.0)
493 pos
= is_int32(d
) ? min(length
, d
) : length
;
496 if(SUCCEEDED(hres
)) {
499 ptr
= strstrW(str
+pos
, search_str
);
506 SysFreeString(search_str
);
507 SysFreeString(val_str
);
512 num_set_int(retv
, ret
);
516 static HRESULT
String_italics(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
517 VARIANT
*retv
, jsexcept_t
*ei
)
519 static const WCHAR italicstagW
[] = {'I',0};
520 return do_attributeless_tag_format(ctx
, jsthis
, retv
, ei
, italicstagW
);
523 /* ECMA-262 3rd Edition 15.5.4.8 */
524 static HRESULT
String_lastIndexOf(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
525 VARIANT
*retv
, jsexcept_t
*ei
)
527 BSTR search_str
, val_str
;
528 DWORD length
, pos
= 0, search_len
;
535 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
541 num_set_int(retv
, -1);
542 SysFreeString(val_str
);
546 hres
= to_string(ctx
, argv
, ei
, &search_str
);
548 SysFreeString(val_str
);
552 search_len
= SysStringLen(search_str
);
557 hres
= to_integer(ctx
, argv
+1, ei
, &d
);
558 if(SUCCEEDED(hres
) && d
> 0)
559 pos
= is_int32(d
) ? min(length
, d
) : length
;
564 if(SUCCEEDED(hres
) && length
>= search_len
) {
567 for(ptr
= str
+min(pos
, length
-search_len
); ptr
>= str
; ptr
--) {
568 if(!memcmp(ptr
, search_str
, search_len
*sizeof(WCHAR
))) {
575 SysFreeString(search_str
);
576 SysFreeString(val_str
);
581 num_set_int(retv
, ret
);
585 static HRESULT
String_link(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
586 VARIANT
*retv
, jsexcept_t
*ei
)
588 static const WCHAR fontW
[] = {'A',0};
589 static const WCHAR colorW
[] = {'H','R','E','F',0};
591 return do_attribute_tag_format(ctx
, jsthis
, argc
, argv
, retv
, ei
, fontW
, colorW
);
594 /* ECMA-262 3rd Edition 15.5.4.10 */
595 static HRESULT
String_match(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
596 VARIANT
*retv
, jsexcept_t
*ei
)
608 V_VT(retv
) = VT_NULL
;
616 regexp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(argv
));
618 if(is_class(regexp
, JSCLASS_REGEXP
))
620 jsdisp_release(regexp
);
625 hres
= to_string(ctx
, argv
, ei
, &match_str
);
629 hres
= create_regexp(ctx
, match_str
, SysStringLen(match_str
), 0, ®exp
);
630 SysFreeString(match_str
);
636 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
637 if(SUCCEEDED(hres
)) {
639 val_str
= SysAllocStringLen(str
, length
);
641 hres
= regexp_string_match(ctx
, regexp
, val_str
, retv
, ei
);
643 hres
= E_OUTOFMEMORY
;
646 jsdisp_release(regexp
);
647 SysFreeString(val_str
);
657 static HRESULT
strbuf_append(strbuf_t
*buf
, const WCHAR
*str
, DWORD len
)
662 if(len
+ buf
->len
> buf
->size
) {
666 new_size
= buf
->size
? buf
->size
<<1 : 16;
667 if(new_size
< buf
->len
+len
)
668 new_size
= buf
->len
+len
;
670 new_buf
= heap_realloc(buf
->buf
, new_size
*sizeof(WCHAR
));
672 new_buf
= heap_alloc(new_size
*sizeof(WCHAR
));
674 return E_OUTOFMEMORY
;
677 buf
->size
= new_size
;
680 memcpy(buf
->buf
+buf
->len
, str
, len
*sizeof(WCHAR
));
685 static HRESULT
rep_call(script_ctx_t
*ctx
, jsdisp_t
*func
, const WCHAR
*str
, match_result_t
*match
,
686 match_result_t
*parens
, DWORD parens_cnt
, BSTR
*ret
, jsexcept_t
*ei
)
695 argv
= heap_alloc_zero(sizeof(VARIANT
)*argc
);
697 return E_OUTOFMEMORY
;
699 V_VT(argv
) = VT_BSTR
;
700 V_BSTR(argv
) = SysAllocStringLen(match
->str
, match
->len
);
702 hres
= E_OUTOFMEMORY
;
704 if(SUCCEEDED(hres
)) {
705 for(i
=0; i
< parens_cnt
; i
++) {
706 V_VT(argv
+i
+1) = VT_BSTR
;
707 V_BSTR(argv
+i
+1) = SysAllocStringLen(parens
[i
].str
, parens
[i
].len
);
708 if(!V_BSTR(argv
+i
+1)) {
709 hres
= E_OUTOFMEMORY
;
715 if(SUCCEEDED(hres
)) {
716 num_set_int(argv
+parens_cnt
+1, match
->str
- str
);
718 V_VT(argv
+parens_cnt
+2) = VT_BSTR
;
719 V_BSTR(argv
+parens_cnt
+2) = SysAllocString(str
);
720 if(!V_BSTR(argv
+parens_cnt
+2))
721 hres
= E_OUTOFMEMORY
;
725 hres
= jsdisp_call_value(func
, NULL
, DISPATCH_METHOD
, argc
, argv
, &var
, ei
);
727 for(i
=0; i
< parens_cnt
+3; i
++) {
728 if(i
!= parens_cnt
+1)
729 SysFreeString(V_BSTR(argv
+i
));
736 hres
= to_string(ctx
, &var
, ei
, ret
);
741 /* ECMA-262 3rd Edition 15.5.4.11 */
742 static HRESULT
String_replace(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
743 VARIANT
*retv
, jsexcept_t
*ei
)
746 DWORD parens_cnt
= 0, parens_size
=0, rep_len
=0, length
;
747 BSTR rep_str
= NULL
, match_str
= NULL
, ret_str
, val_str
;
748 jsdisp_t
*rep_func
= NULL
, *regexp
= NULL
;
749 match_result_t
*parens
= NULL
, match
= {NULL
,0}, **parens_ptr
= &parens
;
750 strbuf_t ret
= {NULL
,0,0};
751 DWORD re_flags
= REM_NO_CTX_UPDATE
;
756 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
763 val_str
= SysAllocStringLen(str
, length
);
765 return E_OUTOFMEMORY
;
768 V_VT(retv
) = VT_BSTR
;
769 V_BSTR(retv
) = val_str
;
776 regexp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(argv
));
778 if(is_class(regexp
, JSCLASS_REGEXP
)) {
781 jsdisp_release(regexp
);
787 hres
= to_string(ctx
, argv
, ei
, &match_str
);
789 SysFreeString(val_str
);
795 switch(V_VT(argv
+1)) {
797 rep_func
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(argv
+1));
799 if(is_class(rep_func
, JSCLASS_FUNCTION
)) {
802 jsdisp_release(rep_func
);
808 hres
= to_string(ctx
, argv
+1, ei
, &rep_str
);
812 rep_len
= SysStringLen(rep_str
);
813 if(!strchrW(rep_str
, '$'))
818 if(SUCCEEDED(hres
)) {
819 const WCHAR
*cp
, *ecp
;
825 hres
= regexp_match_next(ctx
, regexp
, re_flags
, str
, length
, &cp
, parens_ptr
,
826 &parens_size
, &parens_cnt
, &match
);
827 re_flags
|= REM_CHECK_GLOBAL
;
829 if(hres
== S_FALSE
) {
839 match
.str
= strstrW(cp
, match_str
);
842 match
.len
= SysStringLen(match_str
);
843 cp
= match
.str
+match
.len
;
846 hres
= strbuf_append(&ret
, ecp
, match
.str
-ecp
);
847 ecp
= match
.str
+match
.len
;
854 hres
= rep_call(ctx
, rep_func
, str
, &match
, parens
, parens_cnt
, &cstr
, ei
);
858 hres
= strbuf_append(&ret
, cstr
, SysStringLen(cstr
));
862 }else if(rep_str
&& regexp
) {
863 const WCHAR
*ptr
= rep_str
, *ptr2
;
865 while((ptr2
= strchrW(ptr
, '$'))) {
866 hres
= strbuf_append(&ret
, ptr
, ptr2
-ptr
);
872 hres
= strbuf_append(&ret
, ptr2
, 1);
876 hres
= strbuf_append(&ret
, match
.str
, match
.len
);
880 hres
= strbuf_append(&ret
, str
, match
.str
-str
);
884 hres
= strbuf_append(&ret
, ecp
, (str
+length
)-ecp
);
890 if(!isdigitW(ptr2
[1])) {
891 hres
= strbuf_append(&ret
, ptr2
, 1);
897 if(isdigitW(ptr2
[2]) && idx
*10 + (ptr2
[2]-'0') <= parens_cnt
) {
898 idx
= idx
*10 + (ptr
[2]-'0');
900 }else if(idx
&& idx
<= parens_cnt
) {
903 hres
= strbuf_append(&ret
, ptr2
, 1);
908 hres
= strbuf_append(&ret
, parens
[idx
-1].str
, parens
[idx
-1].len
);
917 hres
= strbuf_append(&ret
, ptr
, (rep_str
+rep_len
)-ptr
);
921 hres
= strbuf_append(&ret
, rep_str
, rep_len
);
925 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d'};
927 hres
= strbuf_append(&ret
, undefinedW
, sizeof(undefinedW
)/sizeof(WCHAR
));
937 hres
= strbuf_append(&ret
, ecp
, (str
+length
)-ecp
);
941 jsdisp_release(rep_func
);
942 SysFreeString(rep_str
);
943 SysFreeString(match_str
);
946 if(SUCCEEDED(hres
) && match
.str
&& regexp
) {
948 val_str
= SysAllocStringLen(str
, length
);
950 SysFreeString(ctx
->last_match
);
951 ctx
->last_match
= val_str
;
953 ctx
->last_match_index
= match
.str
-str
;
954 ctx
->last_match_length
= match
.len
;
956 hres
= E_OUTOFMEMORY
;
961 jsdisp_release(regexp
);
962 SysFreeString(val_str
);
964 if(SUCCEEDED(hres
) && retv
) {
965 ret_str
= SysAllocStringLen(ret
.buf
, ret
.len
);
967 return E_OUTOFMEMORY
;
969 V_VT(retv
) = VT_BSTR
;
970 V_BSTR(retv
) = ret_str
;
971 TRACE("= %s\n", debugstr_w(ret_str
));
978 static HRESULT
String_search(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
979 VARIANT
*retv
, jsexcept_t
*ei
)
981 jsdisp_t
*regexp
= NULL
;
982 const WCHAR
*str
, *cp
;
983 match_result_t match
;
990 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
996 V_VT(retv
) = VT_NULL
;
997 SysFreeString(val_str
);
1001 if(V_VT(argv
) == VT_DISPATCH
) {
1002 regexp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(argv
));
1004 if(!is_class(regexp
, JSCLASS_REGEXP
)) {
1005 jsdisp_release(regexp
);
1012 hres
= create_regexp_var(ctx
, argv
, NULL
, ®exp
);
1014 SysFreeString(val_str
);
1020 hres
= regexp_match_next(ctx
, regexp
, REM_RESET_INDEX
, str
, length
, &cp
, NULL
, NULL
, NULL
, &match
);
1021 SysFreeString(val_str
);
1022 jsdisp_release(regexp
);
1027 num_set_int(retv
, hres
== S_OK
? match
.str
-str
: -1);
1031 /* ECMA-262 3rd Edition 15.5.4.13 */
1032 static HRESULT
String_slice(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1033 VARIANT
*retv
, jsexcept_t
*ei
)
1044 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
1049 hres
= to_integer(ctx
, argv
, ei
, &d
);
1051 SysFreeString(val_str
);
1058 start
= length
+ start
;
1061 }else if(start
> length
) {
1070 hres
= to_integer(ctx
, argv
+1, ei
, &d
);
1072 SysFreeString(val_str
);
1082 }else if(end
> length
) {
1086 end
= d
< 0.0 ? 0 : length
;
1096 BSTR retstr
= SysAllocStringLen(str
+start
, end
-start
);
1098 SysFreeString(val_str
);
1099 return E_OUTOFMEMORY
;
1102 V_VT(retv
) = VT_BSTR
;
1103 V_BSTR(retv
) = retstr
;
1106 SysFreeString(val_str
);
1110 static HRESULT
String_small(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1111 VARIANT
*retv
, jsexcept_t
*ei
)
1113 static const WCHAR smalltagW
[] = {'S','M','A','L','L',0};
1114 return do_attributeless_tag_format(ctx
, jsthis
, retv
, ei
, smalltagW
);
1117 static HRESULT
String_split(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1118 VARIANT
*retv
, jsexcept_t
*ei
)
1120 match_result_t
*match_result
= NULL
;
1121 DWORD length
, match_cnt
, i
, match_len
= 0;
1122 const WCHAR
*str
, *ptr
, *ptr2
;
1123 BOOL use_regexp
= FALSE
;
1126 BSTR val_str
, match_str
= NULL
;
1132 FIXME("unsupported args\n");
1136 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
1140 switch(V_VT(argv
)) {
1144 regexp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(argv
));
1146 if(is_class(regexp
, JSCLASS_REGEXP
)) {
1148 hres
= regexp_match(ctx
, regexp
, str
, length
, TRUE
, &match_result
, &match_cnt
);
1149 jsdisp_release(regexp
);
1151 SysFreeString(val_str
);
1156 jsdisp_release(regexp
);
1160 hres
= to_string(ctx
, argv
, ei
, &match_str
);
1162 SysFreeString(val_str
);
1166 match_len
= SysStringLen(match_str
);
1168 SysFreeString(match_str
);
1173 hres
= create_array(ctx
, 0, &array
);
1175 if(SUCCEEDED(hres
)) {
1181 ptr2
= match_result
[i
].str
;
1182 }else if(match_str
) {
1183 ptr2
= strstrW(ptr
, match_str
);
1192 V_VT(&var
) = VT_BSTR
;
1193 V_BSTR(&var
) = SysAllocStringLen(ptr
, ptr2
-ptr
);
1195 hres
= E_OUTOFMEMORY
;
1199 hres
= jsdisp_propput_idx(array
, i
, &var
, ei
);
1200 SysFreeString(V_BSTR(&var
));
1205 ptr
= match_result
[i
].str
+ match_result
[i
].len
;
1207 ptr
= ptr2
+ match_len
;
1213 if(SUCCEEDED(hres
) && (match_str
|| use_regexp
)) {
1214 DWORD len
= (str
+length
) - ptr
;
1216 if(len
|| match_str
) {
1217 V_VT(&var
) = VT_BSTR
;
1218 V_BSTR(&var
) = SysAllocStringLen(ptr
, len
);
1221 hres
= jsdisp_propput_idx(array
, i
, &var
, ei
);
1222 SysFreeString(V_BSTR(&var
));
1224 hres
= E_OUTOFMEMORY
;
1229 SysFreeString(match_str
);
1230 SysFreeString(val_str
);
1231 heap_free(match_result
);
1233 if(SUCCEEDED(hres
) && retv
)
1234 var_set_jsdisp(retv
, array
);
1236 jsdisp_release(array
);
1241 static HRESULT
String_strike(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1242 VARIANT
*retv
, jsexcept_t
*ei
)
1244 static const WCHAR striketagW
[] = {'S','T','R','I','K','E',0};
1245 return do_attributeless_tag_format(ctx
, jsthis
, retv
, ei
, striketagW
);
1248 static HRESULT
String_sub(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1249 VARIANT
*retv
, jsexcept_t
*ei
)
1251 static const WCHAR subtagW
[] = {'S','U','B',0};
1252 return do_attributeless_tag_format(ctx
, jsthis
, retv
, ei
, subtagW
);
1255 /* ECMA-262 3rd Edition 15.5.4.15 */
1256 static HRESULT
String_substring(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1257 VARIANT
*retv
, jsexcept_t
*ei
)
1268 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
1273 hres
= to_integer(ctx
, argv
, ei
, &d
);
1275 SysFreeString(val_str
);
1280 start
= is_int32(d
) ? min(length
, d
) : length
;
1284 hres
= to_integer(ctx
, argv
+1, ei
, &d
);
1286 SysFreeString(val_str
);
1291 end
= is_int32(d
) ? min(length
, d
) : length
;
1305 V_VT(retv
) = VT_BSTR
;
1306 V_BSTR(retv
) = SysAllocStringLen(str
+start
, end
-start
);
1308 SysFreeString(val_str
);
1309 return E_OUTOFMEMORY
;
1312 SysFreeString(val_str
);
1316 /* ECMA-262 3rd Edition B.2.3 */
1317 static HRESULT
String_substr(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1318 VARIANT
*retv
, jsexcept_t
*ei
)
1329 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
1334 hres
= to_integer(ctx
, argv
, ei
, &d
);
1336 SysFreeString(val_str
);
1341 start
= is_int32(d
) ? min(length
, d
) : length
;
1345 hres
= to_integer(ctx
, argv
+1, ei
, &d
);
1347 SysFreeString(val_str
);
1352 len
= is_int32(d
) ? min(length
-start
, d
) : length
-start
;
1361 V_VT(retv
) = VT_BSTR
;
1362 V_BSTR(retv
) = SysAllocStringLen(str
+start
, len
);
1364 hres
= E_OUTOFMEMORY
;
1367 SysFreeString(val_str
);
1371 static HRESULT
String_sup(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1372 VARIANT
*retv
, jsexcept_t
*ei
)
1374 static const WCHAR suptagW
[] = {'S','U','P',0};
1375 return do_attributeless_tag_format(ctx
, jsthis
, retv
, ei
, suptagW
);
1378 static HRESULT
String_toLowerCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1379 VARIANT
*retv
, jsexcept_t
*ei
)
1388 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
1394 val_str
= SysAllocStringLen(str
, length
);
1396 return E_OUTOFMEMORY
;
1401 V_VT(retv
) = VT_BSTR
;
1402 V_BSTR(retv
) = val_str
;
1404 else SysFreeString(val_str
);
1408 static HRESULT
String_toUpperCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1409 VARIANT
*retv
, jsexcept_t
*ei
)
1418 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
1424 val_str
= SysAllocStringLen(str
, length
);
1426 return E_OUTOFMEMORY
;
1431 V_VT(retv
) = VT_BSTR
;
1432 V_BSTR(retv
) = val_str
;
1434 else SysFreeString(val_str
);
1438 static HRESULT
String_toLocaleLowerCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1439 VARIANT
*retv
, jsexcept_t
*ei
)
1445 static HRESULT
String_toLocaleUpperCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1446 VARIANT
*retv
, jsexcept_t
*ei
)
1452 static HRESULT
String_localeCompare(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1453 VARIANT
*retv
, jsexcept_t
*ei
)
1459 static HRESULT
String_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1460 VARIANT
*retv
, jsexcept_t
*ei
)
1462 StringInstance
*This
= string_from_vdisp(jsthis
);
1468 return throw_type_error(ctx
, ei
, JS_E_FUNCTION_EXPECTED
, NULL
);
1469 case DISPATCH_PROPERTYGET
: {
1470 BSTR str
= SysAllocString(This
->str
);
1472 return E_OUTOFMEMORY
;
1474 V_VT(retv
) = VT_BSTR
;
1479 FIXME("flags %x\n", flags
);
1486 static void String_destructor(jsdisp_t
*dispex
)
1488 StringInstance
*This
= (StringInstance
*)dispex
;
1490 heap_free(This
->str
);
1494 static const builtin_prop_t String_props
[] = {
1495 {anchorW
, String_anchor
, PROPF_METHOD
|1},
1496 {bigW
, String_big
, PROPF_METHOD
},
1497 {blinkW
, String_blink
, PROPF_METHOD
},
1498 {boldW
, String_bold
, PROPF_METHOD
},
1499 {charAtW
, String_charAt
, PROPF_METHOD
|1},
1500 {charCodeAtW
, String_charCodeAt
, PROPF_METHOD
|1},
1501 {concatW
, String_concat
, PROPF_METHOD
|1},
1502 {fixedW
, String_fixed
, PROPF_METHOD
},
1503 {fontcolorW
, String_fontcolor
, PROPF_METHOD
|1},
1504 {fontsizeW
, String_fontsize
, PROPF_METHOD
|1},
1505 {indexOfW
, String_indexOf
, PROPF_METHOD
|2},
1506 {italicsW
, String_italics
, PROPF_METHOD
},
1507 {lastIndexOfW
, String_lastIndexOf
, PROPF_METHOD
|2},
1508 {lengthW
, String_length
, 0},
1509 {linkW
, String_link
, PROPF_METHOD
|1},
1510 {localeCompareW
, String_localeCompare
, PROPF_METHOD
|1},
1511 {matchW
, String_match
, PROPF_METHOD
|1},
1512 {replaceW
, String_replace
, PROPF_METHOD
|1},
1513 {searchW
, String_search
, PROPF_METHOD
},
1514 {sliceW
, String_slice
, PROPF_METHOD
},
1515 {smallW
, String_small
, PROPF_METHOD
},
1516 {splitW
, String_split
, PROPF_METHOD
|2},
1517 {strikeW
, String_strike
, PROPF_METHOD
},
1518 {subW
, String_sub
, PROPF_METHOD
},
1519 {substrW
, String_substr
, PROPF_METHOD
|2},
1520 {substringW
, String_substring
, PROPF_METHOD
|2},
1521 {supW
, String_sup
, PROPF_METHOD
},
1522 {toLocaleLowerCaseW
, String_toLocaleLowerCase
, PROPF_METHOD
},
1523 {toLocaleUpperCaseW
, String_toLocaleUpperCase
, PROPF_METHOD
},
1524 {toLowerCaseW
, String_toLowerCase
, PROPF_METHOD
},
1525 {toStringW
, String_toString
, PROPF_METHOD
},
1526 {toUpperCaseW
, String_toUpperCase
, PROPF_METHOD
},
1527 {valueOfW
, String_valueOf
, PROPF_METHOD
}
1530 static const builtin_info_t String_info
= {
1532 {NULL
, String_value
, 0},
1533 sizeof(String_props
)/sizeof(*String_props
),
1539 /* ECMA-262 3rd Edition 15.5.3.2 */
1540 static HRESULT
StringConstr_fromCharCode(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
,
1541 unsigned argc
, VARIANT
*argv
, VARIANT
*retv
, jsexcept_t
*ei
)
1547 ret
= SysAllocStringLen(NULL
, argc
);
1549 return E_OUTOFMEMORY
;
1551 for(i
=0; i
<argc
; i
++) {
1552 hres
= to_uint32(ctx
, argv
+i
, ei
, &code
);
1562 V_VT(retv
) = VT_BSTR
;
1565 else SysFreeString(ret
);
1570 static HRESULT
StringConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, VARIANT
*argv
,
1571 VARIANT
*retv
, jsexcept_t
*ei
)
1582 hres
= to_string(ctx
, argv
, ei
, &str
);
1586 str
= SysAllocStringLen(NULL
, 0);
1588 return E_OUTOFMEMORY
;
1591 V_VT(retv
) = VT_BSTR
;
1595 case DISPATCH_CONSTRUCT
: {
1601 hres
= to_string(ctx
, argv
, ei
, &str
);
1605 hres
= create_string(ctx
, str
, SysStringLen(str
), &ret
);
1608 hres
= create_string(ctx
, NULL
, 0, &ret
);
1614 var_set_jsdisp(retv
, ret
);
1619 FIXME("unimplemented flags: %x\n", flags
);
1626 static HRESULT
string_alloc(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, StringInstance
**ret
)
1628 StringInstance
*string
;
1631 string
= heap_alloc_zero(sizeof(StringInstance
));
1633 return E_OUTOFMEMORY
;
1635 if(object_prototype
)
1636 hres
= init_dispex(&string
->dispex
, ctx
, &String_info
, object_prototype
);
1638 hres
= init_dispex_from_constr(&string
->dispex
, ctx
, &String_info
, ctx
->string_constr
);
1648 static const builtin_prop_t StringConstr_props
[] = {
1649 {fromCharCodeW
, StringConstr_fromCharCode
, PROPF_METHOD
},
1652 static const builtin_info_t StringConstr_info
= {
1654 {NULL
, Function_value
, 0},
1655 sizeof(StringConstr_props
)/sizeof(*StringConstr_props
),
1661 HRESULT
create_string_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
1663 StringInstance
*string
;
1666 static const WCHAR StringW
[] = {'S','t','r','i','n','g',0};
1668 hres
= string_alloc(ctx
, object_prototype
, &string
);
1672 hres
= create_builtin_function(ctx
, StringConstr_value
, StringW
, &StringConstr_info
,
1673 PROPF_CONSTR
|1, &string
->dispex
, ret
);
1675 jsdisp_release(&string
->dispex
);
1679 HRESULT
create_string(script_ctx_t
*ctx
, const WCHAR
*str
, DWORD len
, jsdisp_t
**ret
)
1681 StringInstance
*string
;
1684 hres
= string_alloc(ctx
, NULL
, &string
);
1691 string
->length
= len
;
1692 string
->str
= heap_alloc((len
+1)*sizeof(WCHAR
));
1694 jsdisp_release(&string
->dispex
);
1695 return E_OUTOFMEMORY
;
1698 memcpy(string
->str
, str
, len
*sizeof(WCHAR
));
1699 string
->str
[len
] = 0;
1701 *ret
= &string
->dispex
;