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
);
28 #define UINT32_MAX 0xffffffff
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
, jsstr_t
**val
)
82 StringInstance
*string
;
84 if((string
= string_this(jsthis
))) {
85 *val
= jsstr_addref(string
->str
);
89 return to_string(ctx
, jsval_disp(jsthis
->u
.disp
), val
);
92 static HRESULT
String_length(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
95 TRACE("%p\n", jsthis
);
98 case DISPATCH_PROPERTYGET
: {
99 StringInstance
*string
= string_from_vdisp(jsthis
);
101 *r
= jsval_number(jsstr_length(string
->str
));
105 FIXME("unimplemented flags %x\n", flags
);
112 static HRESULT
stringobj_to_string(vdisp_t
*jsthis
, jsval_t
*r
)
114 StringInstance
*string
;
116 if(!(string
= string_this(jsthis
))) {
117 WARN("this is not a string object\n");
122 *r
= jsval_string(jsstr_addref(string
->str
));
126 /* ECMA-262 3rd Edition 15.5.4.2 */
127 static HRESULT
String_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
132 return stringobj_to_string(jsthis
, r
);
135 /* ECMA-262 3rd Edition 15.5.4.2 */
136 static HRESULT
String_valueOf(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
141 return stringobj_to_string(jsthis
, r
);
144 static HRESULT
do_attributeless_tag_format(script_ctx_t
*ctx
, vdisp_t
*jsthis
, jsval_t
*r
, const WCHAR
*tagname
)
149 static const WCHAR tagfmt
[] = {'<','%','s','>','%','s','<','/','%','s','>',0};
151 hres
= get_string_val(ctx
, jsthis
, &str
);
156 jsstr_t
*ret
= jsstr_alloc_buf(jsstr_length(str
) + 2*strlenW(tagname
) + 5);
159 return E_OUTOFMEMORY
;
162 sprintfW(ret
->str
, tagfmt
, tagname
, str
->str
, tagname
);
163 *r
= jsval_string(ret
);
170 static HRESULT
do_attribute_tag_format(script_ctx_t
*ctx
, vdisp_t
*jsthis
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
,
171 const WCHAR
*tagname
, const WCHAR
*attr
)
173 static const WCHAR tagfmtW
[]
174 = {'<','%','s',' ','%','s','=','\"','%','s','\"','>','%','s','<','/','%','s','>',0};
175 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
177 jsstr_t
*str
, *attr_value
= NULL
;
178 const WCHAR
*attr_str
;
182 hres
= get_string_val(ctx
, jsthis
, &str
);
187 hres
= to_string(ctx
, argv
[0], &attr_value
);
192 attr_str
= attr_value
->str
;
193 attr_len
= jsstr_length(attr_value
);
195 attr_str
= undefinedW
;
196 attr_len
= sizeof(undefinedW
)/sizeof(WCHAR
)-1;
202 ret
= jsstr_alloc_buf(2*strlenW(tagname
) + strlenW(attr
) + attr_len
+ jsstr_length(str
) + 9);
204 sprintfW(ret
->str
, tagfmtW
, tagname
, attr
, attr_str
, str
->str
, tagname
);
205 *r
= jsval_string(ret
);
207 hres
= E_OUTOFMEMORY
;
212 jsstr_release(attr_value
);
217 static HRESULT
String_anchor(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
220 static const WCHAR fontW
[] = {'A',0};
221 static const WCHAR colorW
[] = {'N','A','M','E',0};
223 return do_attribute_tag_format(ctx
, jsthis
, argc
, argv
, r
, fontW
, colorW
);
226 static HRESULT
String_big(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
229 static const WCHAR bigtagW
[] = {'B','I','G',0};
230 return do_attributeless_tag_format(ctx
, jsthis
, r
, bigtagW
);
233 static HRESULT
String_blink(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
236 static const WCHAR blinktagW
[] = {'B','L','I','N','K',0};
237 return do_attributeless_tag_format(ctx
, jsthis
, r
, blinktagW
);
240 static HRESULT
String_bold(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
243 static const WCHAR boldtagW
[] = {'B',0};
244 return do_attributeless_tag_format(ctx
, jsthis
, r
, boldtagW
);
247 /* ECMA-262 3rd Edition 15.5.4.5 */
248 static HRESULT
String_charAt(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
257 hres
= get_string_val(ctx
, jsthis
, &str
);
264 hres
= to_integer(ctx
, argv
[0], &d
);
269 pos
= is_int32(d
) ? d
: -1;
277 if(0 <= pos
&& pos
< jsstr_length(str
)) {
278 ret
= jsstr_alloc_len(str
->str
+pos
, 1);
280 return E_OUTOFMEMORY
;
285 *r
= jsval_string(ret
);
289 /* ECMA-262 3rd Edition 15.5.4.5 */
290 static HRESULT
String_charCodeAt(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
299 hres
= get_string_val(ctx
, jsthis
, &str
);
306 hres
= to_integer(ctx
, argv
[0], &d
);
312 if(!is_int32(d
) || d
< 0 || d
>= jsstr_length(str
)) {
315 *r
= jsval_number(NAN
);
323 *r
= jsval_number(str
->str
[idx
]);
329 /* ECMA-262 3rd Edition 15.5.4.6 */
330 static HRESULT
String_concat(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
333 jsstr_t
**strs
, *ret
= NULL
;
334 DWORD len
= 0, i
, l
, str_cnt
;
341 strs
= heap_alloc_zero(str_cnt
* sizeof(*strs
));
343 return E_OUTOFMEMORY
;
345 hres
= to_string(ctx
, jsval_disp(jsthis
->u
.disp
), strs
);
346 if(SUCCEEDED(hres
)) {
347 for(i
=0; i
< argc
; i
++) {
348 hres
= to_string(ctx
, argv
[i
], strs
+i
+1);
354 if(SUCCEEDED(hres
)) {
355 for(i
=0; i
< str_cnt
; i
++) {
356 len
+= jsstr_length(strs
[i
]);
357 if(len
> JSSTR_MAX_LENGTH
) {
358 hres
= E_OUTOFMEMORY
;
363 ret
= jsstr_alloc_buf(len
);
366 for(i
=0; i
< str_cnt
; i
++) {
367 l
= jsstr_length(strs
[i
]);
368 memcpy(ptr
, strs
[i
]->str
, l
*sizeof(WCHAR
));
372 hres
= E_OUTOFMEMORY
;
376 for(i
=0; i
< str_cnt
; i
++)
377 jsstr_release(strs
[i
]);
384 *r
= jsval_string(ret
);
390 static HRESULT
String_fixed(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
393 static const WCHAR fixedtagW
[] = {'T','T',0};
394 return do_attributeless_tag_format(ctx
, jsthis
, r
, fixedtagW
);
397 static HRESULT
String_fontcolor(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
400 static const WCHAR fontW
[] = {'F','O','N','T',0};
401 static const WCHAR colorW
[] = {'C','O','L','O','R',0};
403 return do_attribute_tag_format(ctx
, jsthis
, argc
, argv
, r
, fontW
, colorW
);
406 static HRESULT
String_fontsize(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
409 static const WCHAR fontW
[] = {'F','O','N','T',0};
410 static const WCHAR colorW
[] = {'S','I','Z','E',0};
412 return do_attribute_tag_format(ctx
, jsthis
, argc
, argv
, r
, fontW
, colorW
);
415 static HRESULT
String_indexOf(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
418 jsstr_t
*search_str
, *str
;
425 hres
= get_string_val(ctx
, jsthis
, &str
);
429 length
= jsstr_length(str
);
432 *r
= jsval_number(-1);
437 hres
= to_string(ctx
, argv
[0], &search_str
);
446 hres
= to_integer(ctx
, argv
[1], &d
);
447 if(SUCCEEDED(hres
) && d
> 0.0)
448 pos
= is_int32(d
) ? min(length
, d
) : length
;
451 if(SUCCEEDED(hres
)) {
454 ptr
= strstrW(str
->str
+pos
, search_str
->str
);
456 ret
= ptr
- str
->str
;
461 jsstr_release(search_str
);
467 *r
= jsval_number(ret
);
471 static HRESULT
String_italics(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
474 static const WCHAR italicstagW
[] = {'I',0};
475 return do_attributeless_tag_format(ctx
, jsthis
, r
, italicstagW
);
478 /* ECMA-262 3rd Edition 15.5.4.8 */
479 static HRESULT
String_lastIndexOf(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
482 unsigned pos
= 0, search_len
, length
;
483 jsstr_t
*search_str
, *str
;
489 hres
= get_string_val(ctx
, jsthis
, &str
);
495 *r
= jsval_number(-1);
500 hres
= to_string(ctx
, argv
[0], &search_str
);
506 search_len
= jsstr_length(search_str
);
507 length
= jsstr_length(str
);
512 hres
= to_integer(ctx
, argv
[1], &d
);
513 if(SUCCEEDED(hres
) && d
> 0)
514 pos
= is_int32(d
) ? min(length
, d
) : length
;
519 if(SUCCEEDED(hres
) && length
>= search_len
) {
522 for(ptr
= str
->str
+min(pos
, length
-search_len
); ptr
>= str
->str
; ptr
--) {
523 if(!memcmp(ptr
, search_str
->str
, search_len
*sizeof(WCHAR
))) {
530 jsstr_release(search_str
);
536 *r
= jsval_number(ret
);
540 static HRESULT
String_link(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
543 static const WCHAR fontW
[] = {'A',0};
544 static const WCHAR colorW
[] = {'H','R','E','F',0};
546 return do_attribute_tag_format(ctx
, jsthis
, argc
, argv
, r
, fontW
, colorW
);
549 /* ECMA-262 3rd Edition 15.5.4.10 */
550 static HRESULT
String_match(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
553 jsdisp_t
*regexp
= NULL
;
565 if(is_object_instance(argv
[0])) {
566 regexp
= iface_to_jsdisp((IUnknown
*)get_object(argv
[0]));
567 if(regexp
&& !is_class(regexp
, JSCLASS_REGEXP
)) {
568 jsdisp_release(regexp
);
576 hres
= to_string(ctx
, argv
[0], &match_str
);
580 hres
= create_regexp(ctx
, match_str
, 0, ®exp
);
581 jsstr_release(match_str
);
586 hres
= get_string_val(ctx
, jsthis
, &str
);
588 hres
= regexp_string_match(ctx
, regexp
, str
, r
);
590 jsdisp_release(regexp
);
601 static HRESULT
strbuf_append(strbuf_t
*buf
, const WCHAR
*str
, DWORD len
)
606 if(len
+ buf
->len
> buf
->size
) {
610 new_size
= buf
->size
? buf
->size
<<1 : 16;
611 if(new_size
< buf
->len
+len
)
612 new_size
= buf
->len
+len
;
614 new_buf
= heap_realloc(buf
->buf
, new_size
*sizeof(WCHAR
));
616 new_buf
= heap_alloc(new_size
*sizeof(WCHAR
));
618 return E_OUTOFMEMORY
;
621 buf
->size
= new_size
;
624 memcpy(buf
->buf
+buf
->len
, str
, len
*sizeof(WCHAR
));
629 static HRESULT
rep_call(script_ctx_t
*ctx
, jsdisp_t
*func
, jsstr_t
*str
, match_result_t
*match
,
630 match_result_t
*parens
, DWORD parens_cnt
, jsstr_t
**ret
)
640 argv
= heap_alloc_zero(sizeof(*argv
)*argc
);
642 return E_OUTOFMEMORY
;
644 tmp_str
= jsstr_alloc_len(match
->str
, match
->len
);
646 hres
= E_OUTOFMEMORY
;
647 argv
[0] = jsval_string(tmp_str
);
649 if(SUCCEEDED(hres
)) {
650 for(i
=0; i
< parens_cnt
; i
++) {
651 tmp_str
= jsstr_alloc_len(parens
[i
].str
, parens
[i
].len
);
653 hres
= E_OUTOFMEMORY
;
656 argv
[i
+1] = jsval_string(tmp_str
);
660 if(SUCCEEDED(hres
)) {
661 argv
[parens_cnt
+1] = jsval_number(match
->str
- str
->str
);
662 argv
[parens_cnt
+2] = jsval_string(str
);
666 hres
= jsdisp_call_value(func
, NULL
, DISPATCH_METHOD
, argc
, argv
, &val
);
668 for(i
=0; i
<= parens_cnt
; i
++)
669 jsstr_release(get_string(argv
[i
]));
675 hres
= to_string(ctx
, val
, ret
);
680 /* ECMA-262 3rd Edition 15.5.4.11 */
681 static HRESULT
String_replace(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
684 DWORD parens_cnt
= 0, parens_size
=0, rep_len
=0;
685 jsstr_t
*rep_str
= NULL
, *match_str
= NULL
, *str
;
686 jsdisp_t
*rep_func
= NULL
, *regexp
= NULL
;
687 match_result_t
*parens
= NULL
, match
= {NULL
,0}, **parens_ptr
= &parens
;
688 strbuf_t ret
= {NULL
,0,0};
689 DWORD re_flags
= REM_NO_CTX_UPDATE
;
694 hres
= get_string_val(ctx
, jsthis
, &str
);
700 *r
= jsval_string(str
);
706 if(is_object_instance(argv
[0])) {
707 regexp
= iface_to_jsdisp((IUnknown
*)get_object(argv
[0]));
708 if(regexp
&& !is_class(regexp
, JSCLASS_REGEXP
)) {
709 jsdisp_release(regexp
);
715 hres
= to_string(ctx
, argv
[0], &match_str
);
723 if(is_object_instance(argv
[1])) {
724 rep_func
= iface_to_jsdisp((IUnknown
*)get_object(argv
[1]));
725 if(rep_func
&& !is_class(rep_func
, JSCLASS_FUNCTION
)) {
726 jsdisp_release(rep_func
);
732 hres
= to_string(ctx
, argv
[1], &rep_str
);
733 if(SUCCEEDED(hres
)) {
734 rep_len
= jsstr_length(rep_str
);
735 if(!strchrW(rep_str
->str
, '$'))
741 if(SUCCEEDED(hres
)) {
742 const WCHAR
*cp
, *ecp
;
748 hres
= regexp_match_next(ctx
, regexp
, re_flags
, str
, &cp
, parens_ptr
,
749 &parens_size
, &parens_cnt
, &match
);
750 re_flags
|= REM_CHECK_GLOBAL
;
752 if(hres
== S_FALSE
) {
762 match
.str
= strstrW(cp
, match_str
->str
);
765 match
.len
= jsstr_length(match_str
);
766 cp
= match
.str
+match
.len
;
769 hres
= strbuf_append(&ret
, ecp
, match
.str
-ecp
);
770 ecp
= match
.str
+match
.len
;
777 hres
= rep_call(ctx
, rep_func
, str
, &match
, parens
, parens_cnt
, &cstr
);
781 hres
= strbuf_append(&ret
, cstr
->str
, jsstr_length(cstr
));
785 }else if(rep_str
&& regexp
) {
786 const WCHAR
*ptr
= rep_str
->str
, *ptr2
;
788 while((ptr2
= strchrW(ptr
, '$'))) {
789 hres
= strbuf_append(&ret
, ptr
, ptr2
-ptr
);
795 hres
= strbuf_append(&ret
, ptr2
, 1);
799 hres
= strbuf_append(&ret
, match
.str
, match
.len
);
803 hres
= strbuf_append(&ret
, str
->str
, match
.str
-str
->str
);
807 hres
= strbuf_append(&ret
, ecp
, (str
->str
+jsstr_length(str
))-ecp
);
813 if(!isdigitW(ptr2
[1])) {
814 hres
= strbuf_append(&ret
, ptr2
, 1);
820 if(isdigitW(ptr2
[2]) && idx
*10 + (ptr2
[2]-'0') <= parens_cnt
) {
821 idx
= idx
*10 + (ptr
[2]-'0');
823 }else if(idx
&& idx
<= parens_cnt
) {
826 hres
= strbuf_append(&ret
, ptr2
, 1);
831 hres
= strbuf_append(&ret
, parens
[idx
-1].str
, parens
[idx
-1].len
);
840 hres
= strbuf_append(&ret
, ptr
, (rep_str
->str
+rep_len
)-ptr
);
844 hres
= strbuf_append(&ret
, rep_str
->str
, rep_len
);
848 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d'};
850 hres
= strbuf_append(&ret
, undefinedW
, sizeof(undefinedW
)/sizeof(WCHAR
));
860 hres
= strbuf_append(&ret
, ecp
, str
->str
+jsstr_length(str
)-ecp
);
864 jsdisp_release(rep_func
);
866 jsstr_release(rep_str
);
868 jsstr_release(match_str
);
871 if(SUCCEEDED(hres
) && match
.str
&& regexp
) {
872 jsstr_release(ctx
->last_match
);
873 ctx
->last_match
= jsstr_addref(str
);
874 ctx
->last_match_index
= match
.str
-str
->str
;
875 ctx
->last_match_length
= match
.len
;
879 jsdisp_release(regexp
);
882 if(SUCCEEDED(hres
) && r
) {
885 ret_str
= jsstr_alloc_len(ret
.buf
, ret
.len
);
887 return E_OUTOFMEMORY
;
889 TRACE("= %s\n", debugstr_jsstr(ret_str
));
890 *r
= jsval_string(ret_str
);
897 static HRESULT
String_search(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
900 jsdisp_t
*regexp
= NULL
;
902 match_result_t match
;
908 hres
= get_string_val(ctx
, jsthis
, &str
);
919 if(is_object_instance(argv
[0])) {
920 regexp
= iface_to_jsdisp((IUnknown
*)get_object(argv
[0]));
921 if(regexp
&& !is_class(regexp
, JSCLASS_REGEXP
)) {
922 jsdisp_release(regexp
);
928 hres
= create_regexp_var(ctx
, argv
[0], NULL
, ®exp
);
936 hres
= regexp_match_next(ctx
, regexp
, REM_RESET_INDEX
, str
, &cp
, NULL
, NULL
, NULL
, &match
);
938 jsdisp_release(regexp
);
943 *r
= jsval_number(hres
== S_OK
? match
.str
-str
->str
: -1);
947 /* ECMA-262 3rd Edition 15.5.4.13 */
948 static HRESULT
String_slice(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
951 int start
=0, end
, length
;
958 hres
= get_string_val(ctx
, jsthis
, &str
);
962 length
= jsstr_length(str
);
964 hres
= to_integer(ctx
, argv
[0], &d
);
973 start
= length
+ start
;
976 }else if(start
> length
) {
985 hres
= to_integer(ctx
, argv
[1], &d
);
997 }else if(end
> length
) {
1001 end
= d
< 0.0 ? 0 : length
;
1011 jsstr_t
*retstr
= jsstr_alloc_len(str
->str
+start
, end
-start
);
1014 return E_OUTOFMEMORY
;
1017 *r
= jsval_string(retstr
);
1024 static HRESULT
String_small(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1027 static const WCHAR smalltagW
[] = {'S','M','A','L','L',0};
1028 return do_attributeless_tag_format(ctx
, jsthis
, r
, smalltagW
);
1031 static HRESULT
String_split(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1034 match_result_t match_result
;
1035 DWORD length
, i
, match_len
= 0;
1036 const WCHAR
*ptr
, *ptr2
, *cp
;
1037 unsigned limit
= UINT32_MAX
;
1038 jsdisp_t
*array
, *regexp
= NULL
;
1039 jsstr_t
*str
, *match_str
= NULL
, *tmp_str
;
1044 if(argc
!= 1 && argc
!= 2) {
1045 FIXME("unsupported argc %u\n", argc
);
1049 hres
= get_string_val(ctx
, jsthis
, &str
);
1053 length
= jsstr_length(str
);
1055 if(argc
> 1 && !is_undefined(argv
[1])) {
1056 hres
= to_uint32(ctx
, argv
[1], &limit
);
1063 if(is_object_instance(argv
[0])) {
1064 regexp
= iface_to_jsdisp((IUnknown
*)get_object(argv
[0]));
1066 if(!is_class(regexp
, JSCLASS_REGEXP
)) {
1067 jsdisp_release(regexp
);
1074 hres
= to_string(ctx
, argv
[0], &match_str
);
1080 match_len
= jsstr_length(match_str
);
1082 jsstr_release(match_str
);
1087 hres
= create_array(ctx
, 0, &array
);
1089 if(SUCCEEDED(hres
)) {
1090 ptr
= cp
= str
->str
;
1091 for(i
=0; i
<limit
; i
++) {
1093 hres
= regexp_match_next(ctx
, regexp
, 0, str
, &cp
, NULL
, NULL
, NULL
, &match_result
);
1096 ptr2
= match_result
.str
;
1097 }else if(match_str
) {
1098 ptr2
= strstrW(ptr
, match_str
->str
);
1107 tmp_str
= jsstr_alloc_len(ptr
, ptr2
-ptr
);
1109 hres
= E_OUTOFMEMORY
;
1113 hres
= jsdisp_propput_idx(array
, i
, jsval_string(tmp_str
));
1114 jsstr_release(tmp_str
);
1121 ptr
= ptr2
+ match_len
;
1127 if(SUCCEEDED(hres
) && (match_str
|| regexp
) && i
<limit
) {
1128 DWORD len
= (str
->str
+length
) - ptr
;
1130 if(len
|| match_str
) {
1131 tmp_str
= jsstr_alloc_len(ptr
, len
);
1134 hres
= jsdisp_propput_idx(array
, i
, jsval_string(tmp_str
));
1135 jsstr_release(tmp_str
);
1137 hres
= E_OUTOFMEMORY
;
1143 jsdisp_release(regexp
);
1145 jsstr_release(match_str
);
1148 if(SUCCEEDED(hres
) && r
)
1149 *r
= jsval_obj(array
);
1151 jsdisp_release(array
);
1156 static HRESULT
String_strike(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1159 static const WCHAR striketagW
[] = {'S','T','R','I','K','E',0};
1160 return do_attributeless_tag_format(ctx
, jsthis
, r
, striketagW
);
1163 static HRESULT
String_sub(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1166 static const WCHAR subtagW
[] = {'S','U','B',0};
1167 return do_attributeless_tag_format(ctx
, jsthis
, r
, subtagW
);
1170 /* ECMA-262 3rd Edition 15.5.4.15 */
1171 static HRESULT
String_substring(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1174 INT start
=0, end
, length
;
1181 hres
= get_string_val(ctx
, jsthis
, &str
);
1185 length
= jsstr_length(str
);
1187 hres
= to_integer(ctx
, argv
[0], &d
);
1194 start
= is_int32(d
) ? min(length
, d
) : length
;
1198 hres
= to_integer(ctx
, argv
[1], &d
);
1205 end
= is_int32(d
) ? min(length
, d
) : length
;
1219 jsstr_t
*ret
= jsstr_alloc_len(str
->str
+start
, end
-start
);
1222 return E_OUTOFMEMORY
;
1224 *r
= jsval_string(ret
);
1230 /* ECMA-262 3rd Edition B.2.3 */
1231 static HRESULT
String_substr(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1234 int start
=0, len
, length
;
1241 hres
= get_string_val(ctx
, jsthis
, &str
);
1245 length
= jsstr_length(str
);
1247 hres
= to_integer(ctx
, argv
[0], &d
);
1254 start
= is_int32(d
) ? min(length
, d
) : length
;
1258 hres
= to_integer(ctx
, argv
[1], &d
);
1265 len
= is_int32(d
) ? min(length
-start
, d
) : length
-start
;
1274 jsstr_t
*ret
= jsstr_alloc_len(str
->str
+start
, len
);
1276 *r
= jsval_string(ret
);
1278 hres
= E_OUTOFMEMORY
;
1285 static HRESULT
String_sup(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1288 static const WCHAR suptagW
[] = {'S','U','P',0};
1289 return do_attributeless_tag_format(ctx
, jsthis
, r
, suptagW
);
1292 static HRESULT
String_toLowerCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1300 hres
= get_string_val(ctx
, jsthis
, &str
);
1307 ret
= jsstr_alloc_len(str
->str
, jsstr_length(str
));
1310 return E_OUTOFMEMORY
;
1314 *r
= jsval_string(ret
);
1320 static HRESULT
String_toUpperCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1328 hres
= get_string_val(ctx
, jsthis
, &str
);
1335 ret
= jsstr_alloc_len(str
->str
, jsstr_length(str
));
1338 return E_OUTOFMEMORY
;
1342 *r
= jsval_string(ret
);
1348 static HRESULT
String_toLocaleLowerCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1355 static HRESULT
String_toLocaleUpperCase(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1362 static HRESULT
String_localeCompare(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1369 static HRESULT
String_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1372 StringInstance
*This
= string_from_vdisp(jsthis
);
1378 return throw_type_error(ctx
, JS_E_FUNCTION_EXPECTED
, NULL
);
1379 case DISPATCH_PROPERTYGET
: {
1380 *r
= jsval_string(jsstr_addref(This
->str
));
1384 FIXME("flags %x\n", flags
);
1391 static void String_destructor(jsdisp_t
*dispex
)
1393 StringInstance
*This
= (StringInstance
*)dispex
;
1395 jsstr_release(This
->str
);
1399 static unsigned String_idx_length(jsdisp_t
*jsdisp
)
1401 StringInstance
*string
= (StringInstance
*)jsdisp
;
1404 * NOTE: For invoke version < 2, indexed array is not implemented at all.
1405 * Newer jscript.dll versions implement it on string type, not class,
1406 * which is not how it should work according to spec. IE9 implements it
1407 * properly, but it uses its own JavaScript engine inside MSHTML. We
1408 * implement it here, but in the way IE9 and spec work.
1410 return string
->dispex
.ctx
->version
< 2 ? 0 : jsstr_length(string
->str
);
1413 static HRESULT
String_idx_get(jsdisp_t
*jsdisp
, unsigned idx
, jsval_t
*r
)
1415 StringInstance
*string
= (StringInstance
*)jsdisp
;
1418 TRACE("%p[%u] = %s\n", string
, idx
, debugstr_wn(string
->str
->str
+idx
, 1));
1420 ret
= jsstr_alloc_len(string
->str
->str
+idx
, 1);
1422 return E_OUTOFMEMORY
;
1424 *r
= jsval_string(ret
);
1428 static const builtin_prop_t String_props
[] = {
1429 {anchorW
, String_anchor
, PROPF_METHOD
|1},
1430 {bigW
, String_big
, PROPF_METHOD
},
1431 {blinkW
, String_blink
, PROPF_METHOD
},
1432 {boldW
, String_bold
, PROPF_METHOD
},
1433 {charAtW
, String_charAt
, PROPF_METHOD
|1},
1434 {charCodeAtW
, String_charCodeAt
, PROPF_METHOD
|1},
1435 {concatW
, String_concat
, PROPF_METHOD
|1},
1436 {fixedW
, String_fixed
, PROPF_METHOD
},
1437 {fontcolorW
, String_fontcolor
, PROPF_METHOD
|1},
1438 {fontsizeW
, String_fontsize
, PROPF_METHOD
|1},
1439 {indexOfW
, String_indexOf
, PROPF_METHOD
|2},
1440 {italicsW
, String_italics
, PROPF_METHOD
},
1441 {lastIndexOfW
, String_lastIndexOf
, PROPF_METHOD
|2},
1442 {lengthW
, String_length
, 0},
1443 {linkW
, String_link
, PROPF_METHOD
|1},
1444 {localeCompareW
, String_localeCompare
, PROPF_METHOD
|1},
1445 {matchW
, String_match
, PROPF_METHOD
|1},
1446 {replaceW
, String_replace
, PROPF_METHOD
|1},
1447 {searchW
, String_search
, PROPF_METHOD
},
1448 {sliceW
, String_slice
, PROPF_METHOD
},
1449 {smallW
, String_small
, PROPF_METHOD
},
1450 {splitW
, String_split
, PROPF_METHOD
|2},
1451 {strikeW
, String_strike
, PROPF_METHOD
},
1452 {subW
, String_sub
, PROPF_METHOD
},
1453 {substrW
, String_substr
, PROPF_METHOD
|2},
1454 {substringW
, String_substring
, PROPF_METHOD
|2},
1455 {supW
, String_sup
, PROPF_METHOD
},
1456 {toLocaleLowerCaseW
, String_toLocaleLowerCase
, PROPF_METHOD
},
1457 {toLocaleUpperCaseW
, String_toLocaleUpperCase
, PROPF_METHOD
},
1458 {toLowerCaseW
, String_toLowerCase
, PROPF_METHOD
},
1459 {toStringW
, String_toString
, PROPF_METHOD
},
1460 {toUpperCaseW
, String_toUpperCase
, PROPF_METHOD
},
1461 {valueOfW
, String_valueOf
, PROPF_METHOD
}
1464 static const builtin_info_t String_info
= {
1466 {NULL
, String_value
, 0},
1467 sizeof(String_props
)/sizeof(*String_props
),
1473 static const builtin_prop_t StringInst_props
[] = {
1474 {lengthW
, String_length
, 0}
1477 static const builtin_info_t StringInst_info
= {
1479 {NULL
, String_value
, 0},
1480 sizeof(StringInst_props
)/sizeof(*StringInst_props
),
1488 /* ECMA-262 3rd Edition 15.5.3.2 */
1489 static HRESULT
StringConstr_fromCharCode(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
,
1490 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
1498 ret
= jsstr_alloc_buf(argc
);
1500 return E_OUTOFMEMORY
;
1502 for(i
=0; i
<argc
; i
++) {
1503 hres
= to_uint32(ctx
, argv
[i
], &code
);
1513 *r
= jsval_string(ret
);
1519 static HRESULT
StringConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1531 hres
= to_string(ctx
, argv
[0], &str
);
1535 str
= jsstr_empty();
1538 *r
= jsval_string(str
);
1541 case DISPATCH_CONSTRUCT
: {
1547 hres
= to_string(ctx
, argv
[0], &str
);
1551 hres
= create_string(ctx
, str
, &ret
);
1554 hres
= create_string(ctx
, jsstr_empty(), &ret
);
1560 *r
= jsval_obj(ret
);
1565 FIXME("unimplemented flags: %x\n", flags
);
1572 static HRESULT
string_alloc(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsstr_t
*str
, StringInstance
**ret
)
1574 StringInstance
*string
;
1577 string
= heap_alloc_zero(sizeof(StringInstance
));
1579 return E_OUTOFMEMORY
;
1581 if(object_prototype
)
1582 hres
= init_dispex(&string
->dispex
, ctx
, &String_info
, object_prototype
);
1584 hres
= init_dispex_from_constr(&string
->dispex
, ctx
, &StringInst_info
, ctx
->string_constr
);
1590 string
->str
= jsstr_addref(str
);
1595 static const builtin_prop_t StringConstr_props
[] = {
1596 {fromCharCodeW
, StringConstr_fromCharCode
, PROPF_METHOD
},
1599 static const builtin_info_t StringConstr_info
= {
1601 {NULL
, Function_value
, 0},
1602 sizeof(StringConstr_props
)/sizeof(*StringConstr_props
),
1608 HRESULT
create_string_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
1610 StringInstance
*string
;
1613 static const WCHAR StringW
[] = {'S','t','r','i','n','g',0};
1615 hres
= string_alloc(ctx
, object_prototype
, jsstr_empty(), &string
);
1619 hres
= create_builtin_constructor(ctx
, StringConstr_value
, StringW
, &StringConstr_info
,
1620 PROPF_CONSTR
|1, &string
->dispex
, ret
);
1622 jsdisp_release(&string
->dispex
);
1626 HRESULT
create_string(script_ctx_t
*ctx
, jsstr_t
*str
, jsdisp_t
**ret
)
1628 StringInstance
*string
;
1631 hres
= string_alloc(ctx
, NULL
, str
, &string
);
1635 *ret
= &string
->dispex
;