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
, IServiceProvider
*sp
)
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
, IServiceProvider
*sp
)
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
, IServiceProvider
*sp
)
158 return stringobj_to_string(jsthis
, retv
);
161 static HRESULT
do_attributeless_tag_format(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
162 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
, 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
, WORD flags
,
193 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
,
194 const WCHAR
*tagname
, const WCHAR
*attr
)
196 static const WCHAR tagfmtW
[]
197 = {'<','%','s',' ','%','s','=','\"','%','s','\"','>','%','s','<','/','%','s','>',0};
198 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
200 StringInstance
*string
;
203 BSTR attr_value
, val_str
= NULL
;
206 if(!(string
= string_this(jsthis
))) {
209 V_VT(&this) = VT_DISPATCH
;
210 V_DISPATCH(&this) = jsthis
->u
.disp
;
212 hres
= to_string(ctx
, &this, ei
, &val_str
);
217 length
= SysStringLen(val_str
);
221 length
= string
->length
;
225 hres
= to_string(ctx
, get_arg(dp
, 0), ei
, &attr_value
);
227 SysFreeString(val_str
);
232 attr_value
= SysAllocString(undefinedW
);
234 SysFreeString(val_str
);
235 return E_OUTOFMEMORY
;
240 BSTR ret
= SysAllocStringLen(NULL
, length
+ 2*strlenW(tagname
)
241 + strlenW(attr
) + SysStringLen(attr_value
) + 9);
243 SysFreeString(attr_value
);
244 SysFreeString(val_str
);
245 return E_OUTOFMEMORY
;
248 sprintfW(ret
, tagfmtW
, tagname
, attr
, attr_value
, str
, tagname
);
250 V_VT(retv
) = VT_BSTR
;
254 SysFreeString(attr_value
);
255 SysFreeString(val_str
);
259 static HRESULT
String_anchor(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
260 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
262 static const WCHAR fontW
[] = {'A',0};
263 static const WCHAR colorW
[] = {'N','A','M','E',0};
265 return do_attribute_tag_format(ctx
, jsthis
, flags
, dp
, retv
, ei
, sp
, fontW
, colorW
);
268 static HRESULT
String_big(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
269 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
271 static const WCHAR bigtagW
[] = {'B','I','G',0};
272 return do_attributeless_tag_format(ctx
, jsthis
, flags
, dp
, retv
, ei
, sp
, bigtagW
);
275 static HRESULT
String_blink(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
276 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
278 static const WCHAR blinktagW
[] = {'B','L','I','N','K',0};
279 return do_attributeless_tag_format(ctx
, jsthis
, flags
, dp
, retv
, ei
, sp
, blinktagW
);
282 static HRESULT
String_bold(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
283 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
285 static const WCHAR boldtagW
[] = {'B',0};
286 return do_attributeless_tag_format(ctx
, jsthis
, flags
, dp
, retv
, ei
, sp
, boldtagW
);
289 /* ECMA-262 3rd Edition 15.5.4.5 */
290 static HRESULT
String_charAt(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
291 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
301 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 if(V_VT(&num
) == VT_I4
) {
317 WARN("pos = %lf\n", V_R8(&num
));
323 SysFreeString(val_str
);
327 if(0 <= pos
&& pos
< length
)
328 ret
= SysAllocStringLen(str
+pos
, 1);
330 ret
= SysAllocStringLen(NULL
, 0);
331 SysFreeString(val_str
);
333 return E_OUTOFMEMORY
;
336 V_VT(retv
) = VT_BSTR
;
341 /* ECMA-262 3rd Edition 15.5.4.5 */
342 static HRESULT
String_charCodeAt(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
343 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
347 DWORD length
, idx
= 0;
352 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
356 if(arg_cnt(dp
) > 0) {
359 hres
= to_integer(ctx
, get_arg(dp
, 0), ei
, &v
);
361 SysFreeString(val_str
);
365 if(V_VT(&v
) != VT_I4
|| V_I4(&v
) < 0 || V_I4(&v
) >= length
) {
366 if(retv
) num_set_nan(&v
);
367 SysFreeString(val_str
);
376 V_I4(retv
) = str
[idx
];
379 SysFreeString(val_str
);
383 /* ECMA-262 3rd Edition 15.5.4.6 */
384 static HRESULT
String_concat(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
385 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
387 BSTR
*strs
= NULL
, ret
= NULL
;
388 DWORD len
= 0, i
, l
, str_cnt
;
395 str_cnt
= arg_cnt(dp
)+1;
396 strs
= heap_alloc_zero(str_cnt
* sizeof(BSTR
));
398 return E_OUTOFMEMORY
;
400 V_VT(&var
) = VT_DISPATCH
;
401 V_DISPATCH(&var
) = jsthis
->u
.disp
;
403 hres
= to_string(ctx
, &var
, ei
, strs
);
404 if(SUCCEEDED(hres
)) {
405 for(i
=0; i
< arg_cnt(dp
); i
++) {
406 hres
= to_string(ctx
, get_arg(dp
, i
), ei
, strs
+i
+1);
412 if(SUCCEEDED(hres
)) {
413 for(i
=0; i
< str_cnt
; i
++)
414 len
+= SysStringLen(strs
[i
]);
416 ptr
= ret
= SysAllocStringLen(NULL
, len
);
418 for(i
=0; i
< str_cnt
; i
++) {
419 l
= SysStringLen(strs
[i
]);
420 memcpy(ptr
, strs
[i
], l
*sizeof(WCHAR
));
425 for(i
=0; i
< str_cnt
; i
++)
426 SysFreeString(strs
[i
]);
433 V_VT(retv
) = VT_BSTR
;
441 static HRESULT
String_fixed(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
442 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
444 static const WCHAR fixedtagW
[] = {'T','T',0};
445 return do_attributeless_tag_format(ctx
, jsthis
, flags
, dp
, retv
, ei
, sp
, fixedtagW
);
448 static HRESULT
String_fontcolor(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
449 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
451 static const WCHAR fontW
[] = {'F','O','N','T',0};
452 static const WCHAR colorW
[] = {'C','O','L','O','R',0};
454 return do_attribute_tag_format(ctx
, jsthis
, flags
, dp
, retv
, ei
, sp
, fontW
, colorW
);
457 static HRESULT
String_fontsize(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
458 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
460 static const WCHAR fontW
[] = {'F','O','N','T',0};
461 static const WCHAR colorW
[] = {'S','I','Z','E',0};
463 return do_attribute_tag_format(ctx
, jsthis
, flags
, dp
, retv
, ei
, sp
, fontW
, colorW
);
466 static HRESULT
String_indexOf(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
467 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
469 DWORD length
, pos
= 0;
471 BSTR search_str
, val_str
;
477 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
486 SysFreeString(val_str
);
490 hres
= to_string(ctx
, get_arg(dp
,0), ei
, &search_str
);
492 SysFreeString(val_str
);
496 if(arg_cnt(dp
) >= 2) {
499 hres
= to_integer(ctx
, get_arg(dp
,1), ei
, &ival
);
500 if(SUCCEEDED(hres
)) {
501 if(V_VT(&ival
) == VT_I4
)
502 pos
= V_VT(&ival
) > 0 ? V_I4(&ival
) : 0;
504 pos
= V_R8(&ival
) > 0.0 ? length
: 0;
510 if(SUCCEEDED(hres
)) {
513 ptr
= strstrW(str
+pos
, search_str
);
520 SysFreeString(search_str
);
521 SysFreeString(val_str
);
532 static HRESULT
String_italics(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
533 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
535 static const WCHAR italicstagW
[] = {'I',0};
536 return do_attributeless_tag_format(ctx
, jsthis
, flags
, dp
, retv
, ei
, sp
, italicstagW
);
539 /* ECMA-262 3rd Edition 15.5.4.8 */
540 static HRESULT
String_lastIndexOf(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
541 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
543 BSTR search_str
, val_str
;
544 DWORD length
, pos
, search_len
;
551 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
560 SysFreeString(val_str
);
564 hres
= to_string(ctx
, get_arg(dp
,0), ei
, &search_str
);
566 SysFreeString(val_str
);
570 search_len
= SysStringLen(search_str
);
572 if(arg_cnt(dp
) >= 2) {
575 hres
= to_integer(ctx
, get_arg(dp
,1), ei
, &ival
);
576 if(SUCCEEDED(hres
)) {
577 if(V_VT(&ival
) == VT_I4
)
578 pos
= V_VT(&ival
) > 0 ? V_I4(&ival
) : 0;
580 pos
= V_R8(&ival
) > 0.0 ? length
: 0;
588 if(SUCCEEDED(hres
) && length
>= search_len
) {
591 for(ptr
= str
+min(pos
, length
-search_len
); ptr
>= str
; ptr
--) {
592 if(!memcmp(ptr
, search_str
, search_len
*sizeof(WCHAR
))) {
599 SysFreeString(search_str
);
600 SysFreeString(val_str
);
611 static HRESULT
String_link(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
612 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
614 static const WCHAR fontW
[] = {'A',0};
615 static const WCHAR colorW
[] = {'H','R','E','F',0};
617 return do_attribute_tag_format(ctx
, jsthis
, flags
, dp
, retv
, ei
, sp
, fontW
, colorW
);
620 /* ECMA-262 3rd Edition 15.5.4.10 */
621 static HRESULT
String_match(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
622 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
635 V_VT(retv
) = VT_NULL
;
641 arg_var
= get_arg(dp
, 0);
642 switch(V_VT(arg_var
)) {
644 regexp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(arg_var
));
646 if(is_class(regexp
, JSCLASS_REGEXP
))
648 jsdisp_release(regexp
);
653 hres
= to_string(ctx
, arg_var
, ei
, &match_str
);
657 hres
= create_regexp(ctx
, match_str
, SysStringLen(match_str
), 0, ®exp
);
658 SysFreeString(match_str
);
664 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
665 if(SUCCEEDED(hres
)) {
667 val_str
= SysAllocStringLen(str
, length
);
669 hres
= regexp_string_match(ctx
, regexp
, val_str
, retv
, ei
);
671 hres
= E_OUTOFMEMORY
;
674 jsdisp_release(regexp
);
675 SysFreeString(val_str
);
685 static HRESULT
strbuf_append(strbuf_t
*buf
, const WCHAR
*str
, DWORD len
)
690 if(len
+ buf
->len
> buf
->size
) {
694 new_size
= buf
->size
? buf
->size
<<1 : 16;
695 if(new_size
< buf
->len
+len
)
696 new_size
= buf
->len
+len
;
698 new_buf
= heap_realloc(buf
->buf
, new_size
*sizeof(WCHAR
));
700 new_buf
= heap_alloc(new_size
*sizeof(WCHAR
));
702 return E_OUTOFMEMORY
;
705 buf
->size
= new_size
;
708 memcpy(buf
->buf
+buf
->len
, str
, len
*sizeof(WCHAR
));
713 static HRESULT
rep_call(script_ctx_t
*ctx
, jsdisp_t
*func
, const WCHAR
*str
, match_result_t
*match
,
714 match_result_t
*parens
, DWORD parens_cnt
, BSTR
*ret
, jsexcept_t
*ei
, IServiceProvider
*caller
)
716 DISPPARAMS dp
= {NULL
, NULL
, 0, 0};
717 VARIANTARG
*args
, *arg
;
722 dp
.cArgs
= parens_cnt
+3;
723 dp
.rgvarg
= args
= heap_alloc_zero(sizeof(VARIANT
)*dp
.cArgs
);
725 return E_OUTOFMEMORY
;
727 arg
= get_arg(&dp
,0);
729 V_BSTR(arg
) = SysAllocStringLen(match
->str
, match
->len
);
731 hres
= E_OUTOFMEMORY
;
733 if(SUCCEEDED(hres
)) {
734 for(i
=0; i
< parens_cnt
; i
++) {
735 arg
= get_arg(&dp
,i
+1);
737 V_BSTR(arg
) = SysAllocStringLen(parens
[i
].str
, parens
[i
].len
);
739 hres
= E_OUTOFMEMORY
;
745 if(SUCCEEDED(hres
)) {
746 arg
= get_arg(&dp
,parens_cnt
+1);
748 V_I4(arg
) = match
->str
- str
;
750 arg
= get_arg(&dp
,parens_cnt
+2);
752 V_BSTR(arg
) = SysAllocString(str
);
754 hres
= E_OUTOFMEMORY
;
758 hres
= jsdisp_call_value(func
, DISPATCH_METHOD
, &dp
, &var
, ei
, caller
);
760 for(i
=0; i
< parens_cnt
+3; i
++) {
761 if(i
!= parens_cnt
+1)
762 SysFreeString(V_BSTR(get_arg(&dp
,i
)));
769 hres
= to_string(ctx
, &var
, ei
, ret
);
774 /* ECMA-262 3rd Edition 15.5.4.11 */
775 static HRESULT
String_replace(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
776 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
779 DWORD parens_cnt
= 0, parens_size
=0, rep_len
=0, length
;
780 BSTR rep_str
= NULL
, match_str
= NULL
, ret_str
, val_str
;
781 jsdisp_t
*rep_func
= NULL
, *regexp
= NULL
;
782 match_result_t
*parens
= NULL
, match
= {NULL
,0}, **parens_ptr
= &parens
;
783 strbuf_t ret
= {NULL
,0,0};
784 DWORD re_flags
= REM_NO_CTX_UPDATE
;
790 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
797 val_str
= SysAllocStringLen(str
, length
);
799 return E_OUTOFMEMORY
;
802 V_VT(retv
) = VT_BSTR
;
803 V_BSTR(retv
) = val_str
;
808 arg_var
= get_arg(dp
, 0);
809 switch(V_VT(arg_var
)) {
811 regexp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(arg_var
));
813 if(is_class(regexp
, JSCLASS_REGEXP
)) {
816 jsdisp_release(regexp
);
822 hres
= to_string(ctx
, arg_var
, ei
, &match_str
);
824 SysFreeString(val_str
);
829 if(arg_cnt(dp
) >= 2) {
830 arg_var
= get_arg(dp
,1);
831 switch(V_VT(arg_var
)) {
833 rep_func
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(arg_var
));
835 if(is_class(rep_func
, JSCLASS_FUNCTION
)) {
838 jsdisp_release(rep_func
);
844 hres
= to_string(ctx
, arg_var
, ei
, &rep_str
);
848 rep_len
= SysStringLen(rep_str
);
849 if(!strchrW(rep_str
, '$'))
854 if(SUCCEEDED(hres
)) {
855 const WCHAR
*cp
, *ecp
;
861 hres
= regexp_match_next(ctx
, regexp
, re_flags
, str
, length
, &cp
, parens_ptr
,
862 &parens_size
, &parens_cnt
, &match
);
863 re_flags
|= REM_CHECK_GLOBAL
;
865 if(hres
== S_FALSE
) {
872 match
.str
= strstrW(cp
, match_str
);
875 match
.len
= SysStringLen(match_str
);
876 cp
= match
.str
+match
.len
;
879 hres
= strbuf_append(&ret
, ecp
, match
.str
-ecp
);
880 ecp
= match
.str
+match
.len
;
887 hres
= rep_call(ctx
, rep_func
, str
, &match
, parens
, parens_cnt
, &cstr
, ei
, caller
);
891 hres
= strbuf_append(&ret
, cstr
, SysStringLen(cstr
));
895 }else if(rep_str
&& regexp
) {
896 const WCHAR
*ptr
= rep_str
, *ptr2
;
898 while((ptr2
= strchrW(ptr
, '$'))) {
899 hres
= strbuf_append(&ret
, ptr
, ptr2
-ptr
);
905 hres
= strbuf_append(&ret
, ptr2
, 1);
909 hres
= strbuf_append(&ret
, match
.str
, match
.len
);
913 hres
= strbuf_append(&ret
, str
, match
.str
-str
);
917 hres
= strbuf_append(&ret
, ecp
, (str
+length
)-ecp
);
923 if(!isdigitW(ptr2
[1])) {
924 hres
= strbuf_append(&ret
, ptr2
, 1);
930 if(isdigitW(ptr2
[2]) && idx
*10 + (ptr2
[2]-'0') <= parens_cnt
) {
931 idx
= idx
*10 + (ptr
[2]-'0');
933 }else if(idx
&& idx
<= parens_cnt
) {
936 hres
= strbuf_append(&ret
, ptr2
, 1);
941 hres
= strbuf_append(&ret
, parens
[idx
-1].str
, parens
[idx
-1].len
);
950 hres
= strbuf_append(&ret
, ptr
, (rep_str
+rep_len
)-ptr
);
954 hres
= strbuf_append(&ret
, rep_str
, rep_len
);
958 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d'};
960 hres
= strbuf_append(&ret
, undefinedW
, sizeof(undefinedW
)/sizeof(WCHAR
));
967 hres
= strbuf_append(&ret
, ecp
, (str
+length
)-ecp
);
971 jsdisp_release(rep_func
);
972 SysFreeString(rep_str
);
973 SysFreeString(match_str
);
976 if(SUCCEEDED(hres
) && match
.str
&& regexp
) {
978 val_str
= SysAllocStringLen(str
, length
);
980 SysFreeString(ctx
->last_match
);
981 ctx
->last_match
= val_str
;
983 ctx
->last_match_index
= match
.str
-str
;
984 ctx
->last_match_length
= match
.len
;
986 hres
= E_OUTOFMEMORY
;
991 jsdisp_release(regexp
);
992 SysFreeString(val_str
);
994 if(SUCCEEDED(hres
) && retv
) {
995 ret_str
= SysAllocStringLen(ret
.buf
, ret
.len
);
997 return E_OUTOFMEMORY
;
999 V_VT(retv
) = VT_BSTR
;
1000 V_BSTR(retv
) = ret_str
;
1001 TRACE("= %s\n", debugstr_w(ret_str
));
1008 static HRESULT
String_search(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1009 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
1011 jsdisp_t
*regexp
= NULL
;
1012 const WCHAR
*str
, *cp
;
1013 match_result_t match
;
1021 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
1027 V_VT(retv
) = VT_NULL
;
1028 SysFreeString(val_str
);
1032 arg
= get_arg(dp
,0);
1033 if(V_VT(arg
) == VT_DISPATCH
) {
1034 regexp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(arg
));
1036 if(!is_class(regexp
, JSCLASS_REGEXP
)) {
1037 jsdisp_release(regexp
);
1044 hres
= create_regexp_var(ctx
, arg
, NULL
, ®exp
);
1046 SysFreeString(val_str
);
1052 hres
= regexp_match_next(ctx
, regexp
, REM_RESET_INDEX
, str
, length
, &cp
, NULL
, NULL
, NULL
, &match
);
1053 SysFreeString(val_str
);
1054 jsdisp_release(regexp
);
1060 V_I4(retv
) = hres
== S_OK
? match
.str
-str
: -1;
1065 /* ECMA-262 3rd Edition 15.5.4.13 */
1066 static HRESULT
String_slice(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1067 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
1078 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
1083 hres
= to_integer(ctx
, get_arg(dp
,0), ei
, &v
);
1085 SysFreeString(val_str
);
1089 if(V_VT(&v
) == VT_I4
) {
1092 start
= length
+ start
;
1095 }else if(start
> length
) {
1099 start
= V_R8(&v
) < 0.0 ? 0 : length
;
1105 if(arg_cnt(dp
) >= 2) {
1106 hres
= to_integer(ctx
, get_arg(dp
,1), ei
, &v
);
1108 SysFreeString(val_str
);
1112 if(V_VT(&v
) == VT_I4
) {
1118 }else if(end
> length
) {
1122 end
= V_R8(&v
) < 0.0 ? 0 : length
;
1132 BSTR retstr
= SysAllocStringLen(str
+start
, end
-start
);
1134 SysFreeString(val_str
);
1135 return E_OUTOFMEMORY
;
1138 V_VT(retv
) = VT_BSTR
;
1139 V_BSTR(retv
) = retstr
;
1142 SysFreeString(val_str
);
1146 static HRESULT
String_small(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1147 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
1149 static const WCHAR smalltagW
[] = {'S','M','A','L','L',0};
1150 return do_attributeless_tag_format(ctx
, jsthis
, flags
, dp
, retv
, ei
, sp
, smalltagW
);
1153 static HRESULT
String_split(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1154 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
1156 match_result_t
*match_result
= NULL
;
1157 DWORD length
, match_cnt
, i
, match_len
= 0;
1158 const WCHAR
*str
, *ptr
, *ptr2
;
1159 BOOL use_regexp
= FALSE
;
1162 BSTR val_str
, match_str
= NULL
;
1167 if(arg_cnt(dp
) != 1) {
1168 FIXME("unsupported args\n");
1172 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
1176 arg
= get_arg(dp
, 0);
1181 regexp
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(arg
));
1183 if(is_class(regexp
, JSCLASS_REGEXP
)) {
1185 hres
= regexp_match(ctx
, regexp
, str
, length
, TRUE
, &match_result
, &match_cnt
);
1186 jsdisp_release(regexp
);
1188 SysFreeString(val_str
);
1193 jsdisp_release(regexp
);
1197 hres
= to_string(ctx
, arg
, ei
, &match_str
);
1199 SysFreeString(val_str
);
1203 match_len
= SysStringLen(match_str
);
1205 SysFreeString(match_str
);
1210 hres
= create_array(ctx
, 0, &array
);
1212 if(SUCCEEDED(hres
)) {
1218 ptr2
= match_result
[i
].str
;
1219 }else if(match_str
) {
1220 ptr2
= strstrW(ptr
, match_str
);
1229 V_VT(&var
) = VT_BSTR
;
1230 V_BSTR(&var
) = SysAllocStringLen(ptr
, ptr2
-ptr
);
1232 hres
= E_OUTOFMEMORY
;
1236 hres
= jsdisp_propput_idx(array
, i
, &var
, ei
, sp
);
1237 SysFreeString(V_BSTR(&var
));
1242 ptr
= match_result
[i
].str
+ match_result
[i
].len
;
1244 ptr
= ptr2
+ match_len
;
1250 if(SUCCEEDED(hres
) && (match_str
|| use_regexp
)) {
1251 DWORD len
= (str
+length
) - ptr
;
1253 if(len
|| match_str
) {
1254 V_VT(&var
) = VT_BSTR
;
1255 V_BSTR(&var
) = SysAllocStringLen(ptr
, len
);
1258 hres
= jsdisp_propput_idx(array
, i
, &var
, ei
, sp
);
1259 SysFreeString(V_BSTR(&var
));
1261 hres
= E_OUTOFMEMORY
;
1266 SysFreeString(match_str
);
1267 SysFreeString(val_str
);
1268 heap_free(match_result
);
1270 if(SUCCEEDED(hres
) && retv
)
1271 var_set_jsdisp(retv
, array
);
1273 jsdisp_release(array
);
1278 static HRESULT
String_strike(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1279 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
1281 static const WCHAR striketagW
[] = {'S','T','R','I','K','E',0};
1282 return do_attributeless_tag_format(ctx
, jsthis
, flags
, dp
, retv
, ei
, sp
, striketagW
);
1285 static HRESULT
String_sub(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1286 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
1288 static const WCHAR subtagW
[] = {'S','U','B',0};
1289 return do_attributeless_tag_format(ctx
, jsthis
, flags
, dp
, retv
, ei
, sp
, subtagW
);
1292 /* ECMA-262 3rd Edition 15.5.4.15 */
1293 static HRESULT
String_substring(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1294 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
1305 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
1309 if(arg_cnt(dp
) >= 1) {
1310 hres
= to_integer(ctx
, get_arg(dp
,0), ei
, &v
);
1312 SysFreeString(val_str
);
1316 if(V_VT(&v
) == VT_I4
) {
1320 else if(start
>= length
)
1323 start
= V_R8(&v
) < 0.0 ? 0 : length
;
1327 if(arg_cnt(dp
) >= 2) {
1328 hres
= to_integer(ctx
, get_arg(dp
,1), ei
, &v
);
1330 SysFreeString(val_str
);
1334 if(V_VT(&v
) == VT_I4
) {
1338 else if(end
> length
)
1341 end
= V_R8(&v
) < 0.0 ? 0 : length
;
1354 V_VT(retv
) = VT_BSTR
;
1355 V_BSTR(retv
) = SysAllocStringLen(str
+start
, end
-start
);
1357 SysFreeString(val_str
);
1358 return E_OUTOFMEMORY
;
1361 SysFreeString(val_str
);
1365 /* ECMA-262 3rd Edition B.2.3 */
1366 static HRESULT
String_substr(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1367 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
1378 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
1382 if(arg_cnt(dp
) >= 1) {
1383 hres
= to_integer(ctx
, get_arg(dp
,0), ei
, &v
);
1385 SysFreeString(val_str
);
1389 if(V_VT(&v
) == VT_I4
) {
1393 else if(start
>= length
)
1396 start
= V_R8(&v
) < 0.0 ? 0 : length
;
1400 if(arg_cnt(dp
) >= 2) {
1401 hres
= to_integer(ctx
, get_arg(dp
,1), ei
, &v
);
1403 SysFreeString(val_str
);
1407 if(V_VT(&v
) == VT_I4
) {
1411 else if(len
> length
-start
)
1414 len
= V_R8(&v
) < 0.0 ? 0 : length
-start
;
1422 V_VT(retv
) = VT_BSTR
;
1423 V_BSTR(retv
) = SysAllocStringLen(str
+start
, len
);
1425 hres
= E_OUTOFMEMORY
;
1428 SysFreeString(val_str
);
1432 static HRESULT
String_sup(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1433 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
1435 static const WCHAR suptagW
[] = {'S','U','P',0};
1436 return do_attributeless_tag_format(ctx
, jsthis
, flags
, dp
, retv
, ei
, sp
, suptagW
);
1439 static HRESULT
String_toLowerCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1440 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
1449 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
1455 val_str
= SysAllocStringLen(str
, length
);
1457 return E_OUTOFMEMORY
;
1462 V_VT(retv
) = VT_BSTR
;
1463 V_BSTR(retv
) = val_str
;
1465 else SysFreeString(val_str
);
1469 static HRESULT
String_toUpperCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1470 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
1479 hres
= get_string_val(ctx
, jsthis
, ei
, &str
, &length
, &val_str
);
1485 val_str
= SysAllocStringLen(str
, length
);
1487 return E_OUTOFMEMORY
;
1492 V_VT(retv
) = VT_BSTR
;
1493 V_BSTR(retv
) = val_str
;
1495 else SysFreeString(val_str
);
1499 static HRESULT
String_toLocaleLowerCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1500 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
1506 static HRESULT
String_toLocaleUpperCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1507 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
1513 static HRESULT
String_localeCompare(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1514 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
1520 static HRESULT
String_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1521 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
1523 StringInstance
*This
= string_from_vdisp(jsthis
);
1529 return throw_type_error(ctx
, ei
, JS_E_FUNCTION_EXPECTED
, NULL
);
1530 case DISPATCH_PROPERTYGET
: {
1531 BSTR str
= SysAllocString(This
->str
);
1533 return E_OUTOFMEMORY
;
1535 V_VT(retv
) = VT_BSTR
;
1540 FIXME("flags %x\n", flags
);
1547 static void String_destructor(jsdisp_t
*dispex
)
1549 StringInstance
*This
= (StringInstance
*)dispex
;
1551 heap_free(This
->str
);
1555 static const builtin_prop_t String_props
[] = {
1556 {anchorW
, String_anchor
, PROPF_METHOD
|1},
1557 {bigW
, String_big
, PROPF_METHOD
},
1558 {blinkW
, String_blink
, PROPF_METHOD
},
1559 {boldW
, String_bold
, PROPF_METHOD
},
1560 {charAtW
, String_charAt
, PROPF_METHOD
|1},
1561 {charCodeAtW
, String_charCodeAt
, PROPF_METHOD
|1},
1562 {concatW
, String_concat
, PROPF_METHOD
|1},
1563 {fixedW
, String_fixed
, PROPF_METHOD
},
1564 {fontcolorW
, String_fontcolor
, PROPF_METHOD
|1},
1565 {fontsizeW
, String_fontsize
, PROPF_METHOD
|1},
1566 {indexOfW
, String_indexOf
, PROPF_METHOD
|2},
1567 {italicsW
, String_italics
, PROPF_METHOD
},
1568 {lastIndexOfW
, String_lastIndexOf
, PROPF_METHOD
|2},
1569 {lengthW
, String_length
, 0},
1570 {linkW
, String_link
, PROPF_METHOD
|1},
1571 {localeCompareW
, String_localeCompare
, PROPF_METHOD
|1},
1572 {matchW
, String_match
, PROPF_METHOD
|1},
1573 {replaceW
, String_replace
, PROPF_METHOD
|1},
1574 {searchW
, String_search
, PROPF_METHOD
},
1575 {sliceW
, String_slice
, PROPF_METHOD
},
1576 {smallW
, String_small
, PROPF_METHOD
},
1577 {splitW
, String_split
, PROPF_METHOD
|2},
1578 {strikeW
, String_strike
, PROPF_METHOD
},
1579 {subW
, String_sub
, PROPF_METHOD
},
1580 {substrW
, String_substr
, PROPF_METHOD
|2},
1581 {substringW
, String_substring
, PROPF_METHOD
|2},
1582 {supW
, String_sup
, PROPF_METHOD
},
1583 {toLocaleLowerCaseW
, String_toLocaleLowerCase
, PROPF_METHOD
},
1584 {toLocaleUpperCaseW
, String_toLocaleUpperCase
, PROPF_METHOD
},
1585 {toLowerCaseW
, String_toLowerCase
, PROPF_METHOD
},
1586 {toStringW
, String_toString
, PROPF_METHOD
},
1587 {toUpperCaseW
, String_toUpperCase
, PROPF_METHOD
},
1588 {valueOfW
, String_valueOf
, PROPF_METHOD
}
1591 static const builtin_info_t String_info
= {
1593 {NULL
, String_value
, 0},
1594 sizeof(String_props
)/sizeof(*String_props
),
1600 /* ECMA-262 3rd Edition 15.5.3.2 */
1601 static HRESULT
StringConstr_fromCharCode(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
,
1602 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
1608 ret
= SysAllocStringLen(NULL
, arg_cnt(dp
));
1610 return E_OUTOFMEMORY
;
1612 for(i
=0; i
<arg_cnt(dp
); i
++) {
1613 hres
= to_uint32(ctx
, get_arg(dp
, i
), ei
, &code
);
1623 V_VT(retv
) = VT_BSTR
;
1626 else SysFreeString(ret
);
1631 static HRESULT
StringConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1632 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
1643 hres
= to_string(ctx
, get_arg(dp
, 0), ei
, &str
);
1647 str
= SysAllocStringLen(NULL
, 0);
1649 return E_OUTOFMEMORY
;
1652 V_VT(retv
) = VT_BSTR
;
1656 case DISPATCH_CONSTRUCT
: {
1662 hres
= to_string(ctx
, get_arg(dp
, 0), ei
, &str
);
1666 hres
= create_string(ctx
, str
, SysStringLen(str
), &ret
);
1669 hres
= create_string(ctx
, NULL
, 0, &ret
);
1675 var_set_jsdisp(retv
, ret
);
1680 FIXME("unimplemented flags: %x\n", flags
);
1687 static HRESULT
string_alloc(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, StringInstance
**ret
)
1689 StringInstance
*string
;
1692 string
= heap_alloc_zero(sizeof(StringInstance
));
1694 return E_OUTOFMEMORY
;
1696 if(object_prototype
)
1697 hres
= init_dispex(&string
->dispex
, ctx
, &String_info
, object_prototype
);
1699 hres
= init_dispex_from_constr(&string
->dispex
, ctx
, &String_info
, ctx
->string_constr
);
1709 static const builtin_prop_t StringConstr_props
[] = {
1710 {fromCharCodeW
, StringConstr_fromCharCode
, PROPF_METHOD
},
1713 static const builtin_info_t StringConstr_info
= {
1715 {NULL
, Function_value
, 0},
1716 sizeof(StringConstr_props
)/sizeof(*StringConstr_props
),
1722 HRESULT
create_string_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
1724 StringInstance
*string
;
1727 static const WCHAR StringW
[] = {'S','t','r','i','n','g',0};
1729 hres
= string_alloc(ctx
, object_prototype
, &string
);
1733 hres
= create_builtin_function(ctx
, StringConstr_value
, StringW
, &StringConstr_info
,
1734 PROPF_CONSTR
|1, &string
->dispex
, ret
);
1736 jsdisp_release(&string
->dispex
);
1740 HRESULT
create_string(script_ctx_t
*ctx
, const WCHAR
*str
, DWORD len
, jsdisp_t
**ret
)
1742 StringInstance
*string
;
1745 hres
= string_alloc(ctx
, NULL
, &string
);
1752 string
->length
= len
;
1753 string
->str
= heap_alloc((len
+1)*sizeof(WCHAR
));
1755 jsdisp_release(&string
->dispex
);
1756 return E_OUTOFMEMORY
;
1759 memcpy(string
->str
, str
, len
*sizeof(WCHAR
));
1760 string
->str
[len
] = 0;
1762 *ret
= &string
->dispex
;