msvcr110: Add _W_Gettnames implementation.
[wine.git] / dlls / jscript / string.c
blob582a289d39e1d2b3526041199d4b7097c0095da2
1 /*
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
19 #include "config.h"
20 #include "wine/port.h"
22 #include "jscript.h"
23 #include "regexp.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
29 typedef struct {
30 jsdisp_t dispex;
31 jsstr_t *str;
32 } StringInstance;
34 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
35 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
36 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
37 static const WCHAR anchorW[] = {'a','n','c','h','o','r',0};
38 static const WCHAR bigW[] = {'b','i','g',0};
39 static const WCHAR blinkW[] = {'b','l','i','n','k',0};
40 static const WCHAR boldW[] = {'b','o','l','d',0};
41 static const WCHAR charAtW[] = {'c','h','a','r','A','t',0};
42 static const WCHAR charCodeAtW[] = {'c','h','a','r','C','o','d','e','A','t',0};
43 static const WCHAR concatW[] = {'c','o','n','c','a','t',0};
44 static const WCHAR fixedW[] = {'f','i','x','e','d',0};
45 static const WCHAR fontcolorW[] = {'f','o','n','t','c','o','l','o','r',0};
46 static const WCHAR fontsizeW[] = {'f','o','n','t','s','i','z','e',0};
47 static const WCHAR indexOfW[] = {'i','n','d','e','x','O','f',0};
48 static const WCHAR italicsW[] = {'i','t','a','l','i','c','s',0};
49 static const WCHAR lastIndexOfW[] = {'l','a','s','t','I','n','d','e','x','O','f',0};
50 static const WCHAR linkW[] = {'l','i','n','k',0};
51 static const WCHAR matchW[] = {'m','a','t','c','h',0};
52 static const WCHAR replaceW[] = {'r','e','p','l','a','c','e',0};
53 static const WCHAR searchW[] = {'s','e','a','r','c','h',0};
54 static const WCHAR sliceW[] = {'s','l','i','c','e',0};
55 static const WCHAR smallW[] = {'s','m','a','l','l',0};
56 static const WCHAR splitW[] = {'s','p','l','i','t',0};
57 static const WCHAR strikeW[] = {'s','t','r','i','k','e',0};
58 static const WCHAR subW[] = {'s','u','b',0};
59 static const WCHAR substringW[] = {'s','u','b','s','t','r','i','n','g',0};
60 static const WCHAR substrW[] = {'s','u','b','s','t','r',0};
61 static const WCHAR supW[] = {'s','u','p',0};
62 static const WCHAR toLowerCaseW[] = {'t','o','L','o','w','e','r','C','a','s','e',0};
63 static const WCHAR toUpperCaseW[] = {'t','o','U','p','p','e','r','C','a','s','e',0};
64 static const WCHAR toLocaleLowerCaseW[] = {'t','o','L','o','c','a','l','e','L','o','w','e','r','C','a','s','e',0};
65 static const WCHAR toLocaleUpperCaseW[] = {'t','o','L','o','c','a','l','e','U','p','p','e','r','C','a','s','e',0};
66 static const WCHAR localeCompareW[] = {'l','o','c','a','l','e','C','o','m','p','a','r','e',0};
67 static const WCHAR fromCharCodeW[] = {'f','r','o','m','C','h','a','r','C','o','d','e',0};
69 static inline StringInstance *string_from_jsdisp(jsdisp_t *jsdisp)
71 return CONTAINING_RECORD(jsdisp, StringInstance, dispex);
74 static inline StringInstance *string_from_vdisp(vdisp_t *vdisp)
76 return string_from_jsdisp(vdisp->u.jsdisp);
79 static inline StringInstance *string_this(vdisp_t *jsthis)
81 return is_vclass(jsthis, JSCLASS_STRING) ? string_from_vdisp(jsthis) : NULL;
84 static HRESULT get_string_val(script_ctx_t *ctx, vdisp_t *jsthis, jsstr_t **val)
86 StringInstance *string;
88 if((string = string_this(jsthis))) {
89 *val = jsstr_addref(string->str);
90 return S_OK;
93 return to_string(ctx, jsval_disp(jsthis->u.disp), val);
96 static HRESULT get_string_flat_val(script_ctx_t *ctx, vdisp_t *jsthis, jsstr_t **jsval, const WCHAR **val)
98 HRESULT hres;
100 hres = get_string_val(ctx, jsthis, jsval);
101 if(FAILED(hres))
102 return hres;
104 *val = jsstr_flatten(*jsval);
105 if(*val)
106 return S_OK;
108 jsstr_release(*jsval);
109 return E_OUTOFMEMORY;
112 static HRESULT String_get_length(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
114 StringInstance *string = (StringInstance*)jsthis;
116 TRACE("%p\n", jsthis);
118 *r = jsval_number(jsstr_length(string->str));
119 return S_OK;
122 static HRESULT String_set_length(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t value)
124 FIXME("%p\n", jsthis);
125 return E_NOTIMPL;
128 static HRESULT stringobj_to_string(vdisp_t *jsthis, jsval_t *r)
130 StringInstance *string;
132 if(!(string = string_this(jsthis))) {
133 WARN("this is not a string object\n");
134 return E_FAIL;
137 if(r)
138 *r = jsval_string(jsstr_addref(string->str));
139 return S_OK;
142 /* ECMA-262 3rd Edition 15.5.4.2 */
143 static HRESULT String_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
144 jsval_t *r)
146 TRACE("\n");
148 return stringobj_to_string(jsthis, r);
151 /* ECMA-262 3rd Edition 15.5.4.2 */
152 static HRESULT String_valueOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
153 jsval_t *r)
155 TRACE("\n");
157 return stringobj_to_string(jsthis, r);
160 static HRESULT do_attributeless_tag_format(script_ctx_t *ctx, vdisp_t *jsthis, jsval_t *r, const WCHAR *tagname)
162 unsigned tagname_len;
163 jsstr_t *str, *ret;
164 WCHAR *ptr;
165 HRESULT hres;
167 hres = get_string_val(ctx, jsthis, &str);
168 if(FAILED(hres))
169 return hres;
171 if(!r) {
172 jsstr_release(str);
173 return S_OK;
176 tagname_len = strlenW(tagname);
178 ptr = jsstr_alloc_buf(jsstr_length(str) + 2*tagname_len + 5, &ret);
179 if(!ret) {
180 jsstr_release(str);
181 return E_OUTOFMEMORY;
184 *ptr++ = '<';
185 memcpy(ptr, tagname, tagname_len*sizeof(WCHAR));
186 ptr += tagname_len;
187 *ptr++ = '>';
189 ptr += jsstr_flush(str, ptr);
190 jsstr_release(str);
192 *ptr++ = '<';
193 *ptr++ = '/';
194 memcpy(ptr, tagname, tagname_len*sizeof(WCHAR));
195 ptr += tagname_len;
196 *ptr = '>';
198 *r = jsval_string(ret);
199 return S_OK;
202 static HRESULT do_attribute_tag_format(script_ctx_t *ctx, vdisp_t *jsthis, unsigned argc, jsval_t *argv, jsval_t *r,
203 const WCHAR *tagname, const WCHAR *attrname)
205 jsstr_t *str, *attr_value = NULL;
206 HRESULT hres;
208 hres = get_string_val(ctx, jsthis, &str);
209 if(FAILED(hres))
210 return hres;
212 if(argc) {
213 hres = to_string(ctx, argv[0], &attr_value);
214 if(FAILED(hres)) {
215 jsstr_release(str);
216 return hres;
218 }else {
219 attr_value = jsstr_undefined();
222 if(r) {
223 unsigned attrname_len = strlenW(attrname);
224 unsigned tagname_len = strlenW(tagname);
225 jsstr_t *ret;
226 WCHAR *ptr;
228 ptr = jsstr_alloc_buf(2*tagname_len + attrname_len + jsstr_length(attr_value) + jsstr_length(str) + 9, &ret);
229 if(ptr) {
230 *ptr++ = '<';
231 memcpy(ptr, tagname, tagname_len*sizeof(WCHAR));
232 ptr += tagname_len;
233 *ptr++ = ' ';
234 memcpy(ptr, attrname, attrname_len*sizeof(WCHAR));
235 ptr += attrname_len;
236 *ptr++ = '=';
237 *ptr++ = '"';
238 ptr += jsstr_flush(attr_value, ptr);
239 *ptr++ = '"';
240 *ptr++ = '>';
241 ptr += jsstr_flush(str, ptr);
243 *ptr++ = '<';
244 *ptr++ = '/';
245 memcpy(ptr, tagname, tagname_len*sizeof(WCHAR));
246 ptr += tagname_len;
247 *ptr = '>';
249 *r = jsval_string(ret);
250 }else {
251 hres = E_OUTOFMEMORY;
255 jsstr_release(attr_value);
256 jsstr_release(str);
257 return hres;
260 static HRESULT String_anchor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
261 jsval_t *r)
263 static const WCHAR fontW[] = {'A',0};
264 static const WCHAR colorW[] = {'N','A','M','E',0};
266 return do_attribute_tag_format(ctx, jsthis, argc, argv, r, fontW, colorW);
269 static HRESULT String_big(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
270 jsval_t *r)
272 static const WCHAR bigtagW[] = {'B','I','G',0};
273 return do_attributeless_tag_format(ctx, jsthis, r, bigtagW);
276 static HRESULT String_blink(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
277 jsval_t *r)
279 static const WCHAR blinktagW[] = {'B','L','I','N','K',0};
280 return do_attributeless_tag_format(ctx, jsthis, r, blinktagW);
283 static HRESULT String_bold(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
284 jsval_t *r)
286 static const WCHAR boldtagW[] = {'B',0};
287 return do_attributeless_tag_format(ctx, jsthis, r, boldtagW);
290 /* ECMA-262 3rd Edition 15.5.4.5 */
291 static HRESULT String_charAt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
292 jsval_t *r)
294 jsstr_t *str, *ret;
295 INT pos = 0;
296 HRESULT hres;
298 TRACE("\n");
300 hres = get_string_val(ctx, jsthis, &str);
301 if(FAILED(hres))
302 return hres;
304 if(argc) {
305 double d;
307 hres = to_integer(ctx, argv[0], &d);
308 if(FAILED(hres)) {
309 jsstr_release(str);
310 return hres;
312 pos = is_int32(d) ? d : -1;
315 if(!r) {
316 jsstr_release(str);
317 return S_OK;
320 if(0 <= pos && pos < jsstr_length(str)) {
321 ret = jsstr_substr(str, pos, 1);
322 if(!ret)
323 return E_OUTOFMEMORY;
324 }else {
325 ret = jsstr_empty();
328 *r = jsval_string(ret);
329 return S_OK;
332 /* ECMA-262 3rd Edition 15.5.4.5 */
333 static HRESULT String_charCodeAt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
334 jsval_t *r)
336 jsstr_t *str;
337 DWORD idx = 0;
338 HRESULT hres;
340 TRACE("\n");
342 hres = get_string_val(ctx, jsthis, &str);
343 if(FAILED(hres))
344 return hres;
346 if(argc > 0) {
347 double d;
349 hres = to_integer(ctx, argv[0], &d);
350 if(FAILED(hres)) {
351 jsstr_release(str);
352 return hres;
355 if(!is_int32(d) || d < 0 || d >= jsstr_length(str)) {
356 jsstr_release(str);
357 if(r)
358 *r = jsval_number(NAN);
359 return S_OK;
362 idx = d;
365 if(r) {
366 WCHAR c;
367 jsstr_extract(str, idx, 1, &c);
368 *r = jsval_number(c);
371 jsstr_release(str);
372 return S_OK;
375 /* ECMA-262 3rd Edition 15.5.4.6 */
376 static HRESULT String_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
377 jsval_t *r)
379 jsstr_t *ret, *str;
380 HRESULT hres;
382 TRACE("\n");
384 hres = get_string_val(ctx, jsthis, &str);
385 if(FAILED(hres))
386 return hres;
388 switch(argc) {
389 case 0:
390 ret = str;
391 break;
392 case 1: {
393 jsstr_t *arg_str;
395 hres = to_string(ctx, argv[0], &arg_str);
396 if(FAILED(hres)) {
397 jsstr_release(str);
398 return hres;
401 ret = jsstr_concat(str, arg_str);
402 jsstr_release(str);
403 if(!ret)
404 return E_OUTOFMEMORY;
405 break;
407 default: {
408 const unsigned str_cnt = argc+1;
409 unsigned len = 0, i;
410 jsstr_t **strs;
411 WCHAR *ptr;
413 strs = heap_alloc_zero(str_cnt * sizeof(*strs));
414 if(!strs) {
415 jsstr_release(str);
416 return E_OUTOFMEMORY;
419 strs[0] = str;
420 for(i=0; i < argc; i++) {
421 hres = to_string(ctx, argv[i], strs+i+1);
422 if(FAILED(hres))
423 break;
426 if(SUCCEEDED(hres)) {
427 for(i=0; i < str_cnt; i++) {
428 len += jsstr_length(strs[i]);
429 if(len > JSSTR_MAX_LENGTH) {
430 hres = E_OUTOFMEMORY;
431 break;
435 if(SUCCEEDED(hres)) {
436 ptr = jsstr_alloc_buf(len, &ret);
437 if(ptr) {
438 for(i=0; i < str_cnt; i++)
439 ptr += jsstr_flush(strs[i], ptr);
440 }else {
441 hres = E_OUTOFMEMORY;
446 while(i--)
447 jsstr_release(strs[i]);
448 heap_free(strs);
449 if(FAILED(hres))
450 return hres;
454 if(r)
455 *r = jsval_string(ret);
456 else
457 jsstr_release(ret);
458 return S_OK;
461 static HRESULT String_fixed(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
462 jsval_t *r)
464 static const WCHAR fixedtagW[] = {'T','T',0};
465 return do_attributeless_tag_format(ctx, jsthis, r, fixedtagW);
468 static HRESULT String_fontcolor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
469 jsval_t *r)
471 static const WCHAR fontW[] = {'F','O','N','T',0};
472 static const WCHAR colorW[] = {'C','O','L','O','R',0};
474 return do_attribute_tag_format(ctx, jsthis, argc, argv, r, fontW, colorW);
477 static HRESULT String_fontsize(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
478 jsval_t *r)
480 static const WCHAR fontW[] = {'F','O','N','T',0};
481 static const WCHAR colorW[] = {'S','I','Z','E',0};
483 return do_attribute_tag_format(ctx, jsthis, argc, argv, r, fontW, colorW);
486 static HRESULT String_indexOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
487 jsval_t *r)
489 jsstr_t *search_jsstr, *jsstr;
490 const WCHAR *search_str, *str;
491 int length, pos = 0;
492 INT ret = -1;
493 HRESULT hres;
495 TRACE("\n");
497 hres = get_string_flat_val(ctx, jsthis, &jsstr, &str);
498 if(FAILED(hres))
499 return hres;
501 length = jsstr_length(jsstr);
502 if(!argc) {
503 if(r)
504 *r = jsval_number(-1);
505 jsstr_release(jsstr);
506 return S_OK;
509 hres = to_flat_string(ctx, argv[0], &search_jsstr, &search_str);
510 if(FAILED(hres)) {
511 jsstr_release(jsstr);
512 return hres;
515 if(argc >= 2) {
516 double d;
518 hres = to_integer(ctx, argv[1], &d);
519 if(SUCCEEDED(hres) && d > 0.0)
520 pos = is_int32(d) ? min(length, d) : length;
523 if(SUCCEEDED(hres)) {
524 const WCHAR *ptr;
526 ptr = strstrW(str+pos, search_str);
527 if(ptr)
528 ret = ptr - str;
529 else
530 ret = -1;
533 jsstr_release(search_jsstr);
534 jsstr_release(jsstr);
535 if(FAILED(hres))
536 return hres;
538 if(r)
539 *r = jsval_number(ret);
540 return S_OK;
543 static HRESULT String_italics(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
544 jsval_t *r)
546 static const WCHAR italicstagW[] = {'I',0};
547 return do_attributeless_tag_format(ctx, jsthis, r, italicstagW);
550 /* ECMA-262 3rd Edition 15.5.4.8 */
551 static HRESULT String_lastIndexOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
552 jsval_t *r)
554 unsigned pos = 0, search_len, length;
555 jsstr_t *search_jsstr, *jsstr;
556 const WCHAR *search_str, *str;
557 INT ret = -1;
558 HRESULT hres;
560 TRACE("\n");
562 hres = get_string_flat_val(ctx, jsthis, &jsstr, &str);
563 if(FAILED(hres))
564 return hres;
566 if(!argc) {
567 if(r)
568 *r = jsval_number(-1);
569 jsstr_release(jsstr);
570 return S_OK;
573 hres = to_flat_string(ctx, argv[0], &search_jsstr, &search_str);
574 if(FAILED(hres)) {
575 jsstr_release(jsstr);
576 return hres;
579 search_len = jsstr_length(search_jsstr);
580 length = jsstr_length(jsstr);
582 if(argc >= 2) {
583 double d;
585 hres = to_integer(ctx, argv[1], &d);
586 if(SUCCEEDED(hres) && d > 0)
587 pos = is_int32(d) ? min(length, d) : length;
588 }else {
589 pos = length;
592 if(SUCCEEDED(hres) && length >= search_len) {
593 const WCHAR *ptr;
595 for(ptr = str+min(pos, length-search_len); ptr >= str; ptr--) {
596 if(!memcmp(ptr, search_str, search_len*sizeof(WCHAR))) {
597 ret = ptr-str;
598 break;
603 jsstr_release(search_jsstr);
604 jsstr_release(jsstr);
605 if(FAILED(hres))
606 return hres;
608 if(r)
609 *r = jsval_number(ret);
610 return S_OK;
613 static HRESULT String_link(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
614 jsval_t *r)
616 static const WCHAR fontW[] = {'A',0};
617 static const WCHAR colorW[] = {'H','R','E','F',0};
619 return do_attribute_tag_format(ctx, jsthis, argc, argv, r, fontW, colorW);
622 /* ECMA-262 3rd Edition 15.5.4.10 */
623 static HRESULT String_match(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
624 jsval_t *r)
626 jsdisp_t *regexp = NULL;
627 jsstr_t *str;
628 HRESULT hres;
630 TRACE("\n");
632 if(!argc) {
633 if(r)
634 *r = jsval_null();
635 return S_OK;
638 if(is_object_instance(argv[0])) {
639 regexp = iface_to_jsdisp((IUnknown*)get_object(argv[0]));
640 if(regexp && !is_class(regexp, JSCLASS_REGEXP)) {
641 jsdisp_release(regexp);
642 regexp = NULL;
646 if(!regexp) {
647 jsstr_t *match_str;
649 hres = to_string(ctx, argv[0], &match_str);
650 if(FAILED(hres))
651 return hres;
653 hres = create_regexp(ctx, match_str, 0, &regexp);
654 jsstr_release(match_str);
655 if(FAILED(hres))
656 return hres;
659 hres = get_string_val(ctx, jsthis, &str);
660 if(SUCCEEDED(hres))
661 hres = regexp_string_match(ctx, regexp, str, r);
663 jsdisp_release(regexp);
664 jsstr_release(str);
665 return hres;
668 typedef struct {
669 WCHAR *buf;
670 DWORD size;
671 DWORD len;
672 } strbuf_t;
674 static BOOL strbuf_ensure_size(strbuf_t *buf, unsigned len)
676 WCHAR *new_buf;
677 DWORD new_size;
679 if(len <= buf->size)
680 return TRUE;
682 new_size = buf->size ? buf->size<<1 : 16;
683 if(new_size < len)
684 new_size = len;
685 if(buf->buf)
686 new_buf = heap_realloc(buf->buf, new_size*sizeof(WCHAR));
687 else
688 new_buf = heap_alloc(new_size*sizeof(WCHAR));
689 if(!new_buf)
690 return FALSE;
692 buf->buf = new_buf;
693 buf->size = new_size;
694 return TRUE;
697 static HRESULT strbuf_append(strbuf_t *buf, const WCHAR *str, DWORD len)
699 if(!len)
700 return S_OK;
702 if(!strbuf_ensure_size(buf, buf->len+len))
703 return E_OUTOFMEMORY;
705 memcpy(buf->buf+buf->len, str, len*sizeof(WCHAR));
706 buf->len += len;
707 return S_OK;
710 static HRESULT strbuf_append_jsstr(strbuf_t *buf, jsstr_t *str)
712 if(!strbuf_ensure_size(buf, buf->len+jsstr_length(str)))
713 return E_OUTOFMEMORY;
715 jsstr_flush(str, buf->buf+buf->len);
716 buf->len += jsstr_length(str);
717 return S_OK;
720 static HRESULT rep_call(script_ctx_t *ctx, jsdisp_t *func,
721 jsstr_t *jsstr, const WCHAR *str, match_state_t *match, jsstr_t **ret)
723 jsval_t *argv;
724 unsigned argc;
725 jsval_t val;
726 jsstr_t *tmp_str;
727 DWORD i;
728 HRESULT hres = S_OK;
730 argc = match->paren_count+3;
731 argv = heap_alloc_zero(sizeof(*argv)*argc);
732 if(!argv)
733 return E_OUTOFMEMORY;
735 tmp_str = jsstr_alloc_len(match->cp-match->match_len, match->match_len);
736 if(!tmp_str)
737 hres = E_OUTOFMEMORY;
738 argv[0] = jsval_string(tmp_str);
740 if(SUCCEEDED(hres)) {
741 for(i=0; i < match->paren_count; i++) {
742 if(match->parens[i].index != -1)
743 tmp_str = jsstr_substr(jsstr, match->parens[i].index, match->parens[i].length);
744 else
745 tmp_str = jsstr_empty();
746 if(!tmp_str) {
747 hres = E_OUTOFMEMORY;
748 break;
750 argv[i+1] = jsval_string(tmp_str);
754 if(SUCCEEDED(hres)) {
755 argv[match->paren_count+1] = jsval_number(match->cp-str - match->match_len);
756 argv[match->paren_count+2] = jsval_string(jsstr);
759 if(SUCCEEDED(hres))
760 hres = jsdisp_call_value(func, NULL, DISPATCH_METHOD, argc, argv, &val);
762 for(i=0; i <= match->paren_count; i++)
763 jsstr_release(get_string(argv[i]));
764 heap_free(argv);
766 if(FAILED(hres))
767 return hres;
769 hres = to_string(ctx, val, ret);
770 jsval_release(val);
771 return hres;
774 /* ECMA-262 3rd Edition 15.5.4.11 */
775 static HRESULT String_replace(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
776 jsval_t *r)
778 const WCHAR *str, *match_str = NULL, *rep_str = NULL;
779 jsstr_t *rep_jsstr, *match_jsstr, *jsstr;
780 jsdisp_t *rep_func = NULL, *regexp = NULL;
781 match_state_t *match = NULL, last_match = {0};
782 strbuf_t ret = {NULL,0,0};
783 DWORD re_flags = REM_NO_CTX_UPDATE|REM_ALLOC_RESULT;
784 DWORD rep_len=0;
785 HRESULT hres = S_OK;
787 TRACE("\n");
789 hres = get_string_flat_val(ctx, jsthis, &jsstr, &str);
790 if(FAILED(hres))
791 return hres;
793 if(!argc) {
794 if(r)
795 *r = jsval_string(jsstr);
796 else
797 jsstr_release(jsstr);
798 return S_OK;
801 if(is_object_instance(argv[0])) {
802 regexp = iface_to_jsdisp((IUnknown*)get_object(argv[0]));
803 if(regexp && !is_class(regexp, JSCLASS_REGEXP)) {
804 jsdisp_release(regexp);
805 regexp = NULL;
809 if(!regexp) {
810 hres = to_flat_string(ctx, argv[0], &match_jsstr, &match_str);
811 if(FAILED(hres)) {
812 jsstr_release(jsstr);
813 return hres;
817 if(argc >= 2) {
818 if(is_object_instance(argv[1])) {
819 rep_func = iface_to_jsdisp((IUnknown*)get_object(argv[1]));
820 if(rep_func && !is_class(rep_func, JSCLASS_FUNCTION)) {
821 jsdisp_release(rep_func);
822 rep_func = NULL;
826 if(!rep_func) {
827 hres = to_flat_string(ctx, argv[1], &rep_jsstr, &rep_str);
828 if(SUCCEEDED(hres))
829 rep_len = jsstr_length(rep_jsstr);
833 if(SUCCEEDED(hres)) {
834 const WCHAR *ecp = str;
836 while(1) {
837 if(regexp) {
838 hres = regexp_match_next(ctx, regexp, re_flags, jsstr, &match);
839 re_flags = (re_flags | REM_CHECK_GLOBAL) & (~REM_ALLOC_RESULT);
841 if(hres == S_FALSE) {
842 hres = S_OK;
843 break;
845 if(FAILED(hres))
846 break;
848 last_match.cp = match->cp;
849 last_match.match_len = match->match_len;
850 }else {
851 if(re_flags & REM_ALLOC_RESULT) {
852 re_flags &= ~REM_ALLOC_RESULT;
853 match = &last_match;
854 match->cp = str;
857 match->cp = strstrW(match->cp, match_str);
858 if(!match->cp)
859 break;
860 match->match_len = jsstr_length(match_jsstr);
861 match->cp += match->match_len;
864 hres = strbuf_append(&ret, ecp, match->cp-ecp-match->match_len);
865 ecp = match->cp;
866 if(FAILED(hres))
867 break;
869 if(rep_func) {
870 jsstr_t *cstr;
872 hres = rep_call(ctx, rep_func, jsstr, str, match, &cstr);
873 if(FAILED(hres))
874 break;
876 hres = strbuf_append_jsstr(&ret, cstr);
877 jsstr_release(cstr);
878 if(FAILED(hres))
879 break;
880 }else if(rep_str && regexp) {
881 const WCHAR *ptr = rep_str, *ptr2;
883 while((ptr2 = strchrW(ptr, '$'))) {
884 hres = strbuf_append(&ret, ptr, ptr2-ptr);
885 if(FAILED(hres))
886 break;
888 switch(ptr2[1]) {
889 case '$':
890 hres = strbuf_append(&ret, ptr2, 1);
891 ptr = ptr2+2;
892 break;
893 case '&':
894 hres = strbuf_append(&ret, match->cp-match->match_len, match->match_len);
895 ptr = ptr2+2;
896 break;
897 case '`':
898 hres = strbuf_append(&ret, str, match->cp-str-match->match_len);
899 ptr = ptr2+2;
900 break;
901 case '\'':
902 hres = strbuf_append(&ret, ecp, (str+jsstr_length(jsstr))-ecp);
903 ptr = ptr2+2;
904 break;
905 default: {
906 DWORD idx;
908 if(!isdigitW(ptr2[1])) {
909 hres = strbuf_append(&ret, ptr2, 1);
910 ptr = ptr2+1;
911 break;
914 idx = ptr2[1] - '0';
915 if(isdigitW(ptr2[2]) && idx*10 + (ptr2[2]-'0') <= match->paren_count) {
916 idx = idx*10 + (ptr[2]-'0');
917 ptr = ptr2+3;
918 }else if(idx && idx <= match->paren_count) {
919 ptr = ptr2+2;
920 }else {
921 hres = strbuf_append(&ret, ptr2, 1);
922 ptr = ptr2+1;
923 break;
926 if(match->parens[idx-1].index != -1)
927 hres = strbuf_append(&ret, str+match->parens[idx-1].index,
928 match->parens[idx-1].length);
932 if(FAILED(hres))
933 break;
936 if(SUCCEEDED(hres))
937 hres = strbuf_append(&ret, ptr, (rep_str+rep_len)-ptr);
938 if(FAILED(hres))
939 break;
940 }else if(rep_str) {
941 hres = strbuf_append(&ret, rep_str, rep_len);
942 if(FAILED(hres))
943 break;
944 }else {
945 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d'};
947 hres = strbuf_append(&ret, undefinedW, sizeof(undefinedW)/sizeof(WCHAR));
948 if(FAILED(hres))
949 break;
952 if(!regexp)
953 break;
954 else if(!match->match_len)
955 match->cp++;
958 if(SUCCEEDED(hres))
959 hres = strbuf_append(&ret, ecp, str+jsstr_length(jsstr)-ecp);
962 if(rep_func)
963 jsdisp_release(rep_func);
964 if(rep_str)
965 jsstr_release(rep_jsstr);
966 if(match_str)
967 jsstr_release(match_jsstr);
968 if(regexp)
969 heap_free(match);
971 if(SUCCEEDED(hres) && last_match.cp && regexp) {
972 jsstr_release(ctx->last_match);
973 ctx->last_match = jsstr_addref(jsstr);
974 ctx->last_match_index = last_match.cp-str-last_match.match_len;
975 ctx->last_match_length = last_match.match_len;
978 if(regexp)
979 jsdisp_release(regexp);
980 jsstr_release(jsstr);
982 if(SUCCEEDED(hres) && r) {
983 jsstr_t *ret_str;
985 ret_str = jsstr_alloc_len(ret.buf, ret.len);
986 if(!ret_str)
987 return E_OUTOFMEMORY;
989 TRACE("= %s\n", debugstr_jsstr(ret_str));
990 *r = jsval_string(ret_str);
993 heap_free(ret.buf);
994 return hres;
997 static HRESULT String_search(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
998 jsval_t *r)
1000 jsdisp_t *regexp = NULL;
1001 const WCHAR *str;
1002 jsstr_t *jsstr;
1003 match_state_t match, *match_ptr = &match;
1004 HRESULT hres;
1006 TRACE("\n");
1008 hres = get_string_flat_val(ctx, jsthis, &jsstr, &str);
1009 if(FAILED(hres))
1010 return hres;
1012 if(!argc) {
1013 if(r)
1014 *r = jsval_null();
1015 jsstr_release(jsstr);
1016 return S_OK;
1019 if(is_object_instance(argv[0])) {
1020 regexp = iface_to_jsdisp((IUnknown*)get_object(argv[0]));
1021 if(regexp && !is_class(regexp, JSCLASS_REGEXP)) {
1022 jsdisp_release(regexp);
1023 regexp = NULL;
1027 if(!regexp) {
1028 hres = create_regexp_var(ctx, argv[0], NULL, &regexp);
1029 if(FAILED(hres)) {
1030 jsstr_release(jsstr);
1031 return hres;
1035 match.cp = str;
1036 hres = regexp_match_next(ctx, regexp, REM_RESET_INDEX|REM_NO_PARENS, jsstr, &match_ptr);
1037 jsstr_release(jsstr);
1038 jsdisp_release(regexp);
1039 if(FAILED(hres))
1040 return hres;
1042 if(r)
1043 *r = jsval_number(hres == S_OK ? match.cp-match.match_len-str : -1);
1044 return S_OK;
1047 /* ECMA-262 3rd Edition 15.5.4.13 */
1048 static HRESULT String_slice(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1049 jsval_t *r)
1051 int start=0, end, length;
1052 jsstr_t *str;
1053 double d;
1054 HRESULT hres;
1056 TRACE("\n");
1058 hres = get_string_val(ctx, jsthis, &str);
1059 if(FAILED(hres))
1060 return hres;
1062 length = jsstr_length(str);
1063 if(argc) {
1064 hres = to_integer(ctx, argv[0], &d);
1065 if(FAILED(hres)) {
1066 jsstr_release(str);
1067 return hres;
1070 if(is_int32(d)) {
1071 start = d;
1072 if(start < 0) {
1073 start = length + start;
1074 if(start < 0)
1075 start = 0;
1076 }else if(start > length) {
1077 start = length;
1079 }else if(d > 0) {
1080 start = length;
1084 if(argc >= 2) {
1085 hres = to_integer(ctx, argv[1], &d);
1086 if(FAILED(hres)) {
1087 jsstr_release(str);
1088 return hres;
1091 if(is_int32(d)) {
1092 end = d;
1093 if(end < 0) {
1094 end = length + end;
1095 if(end < 0)
1096 end = 0;
1097 }else if(end > length) {
1098 end = length;
1100 }else {
1101 end = d < 0.0 ? 0 : length;
1103 }else {
1104 end = length;
1107 if(end < start)
1108 end = start;
1110 if(r) {
1111 jsstr_t *retstr = jsstr_substr(str, start, end-start);
1112 if(!retstr) {
1113 jsstr_release(str);
1114 return E_OUTOFMEMORY;
1117 *r = jsval_string(retstr);
1120 jsstr_release(str);
1121 return S_OK;
1124 static HRESULT String_small(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1125 jsval_t *r)
1127 static const WCHAR smalltagW[] = {'S','M','A','L','L',0};
1128 return do_attributeless_tag_format(ctx, jsthis, r, smalltagW);
1131 static HRESULT String_split(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1132 jsval_t *r)
1134 match_state_t match_result, *match_ptr = &match_result;
1135 DWORD length, i, match_len = 0;
1136 const WCHAR *ptr, *ptr2, *str, *match_str = NULL;
1137 unsigned limit = ~0u;
1138 jsdisp_t *array, *regexp = NULL;
1139 jsstr_t *jsstr, *match_jsstr, *tmp_str;
1140 HRESULT hres;
1142 TRACE("\n");
1144 if(argc != 1 && argc != 2) {
1145 FIXME("unsupported argc %u\n", argc);
1146 return E_NOTIMPL;
1149 hres = get_string_flat_val(ctx, jsthis, &jsstr, &str);
1150 if(FAILED(hres))
1151 return hres;
1153 length = jsstr_length(jsstr);
1155 if(argc > 1 && !is_undefined(argv[1])) {
1156 hres = to_uint32(ctx, argv[1], &limit);
1157 if(FAILED(hres)) {
1158 jsstr_release(jsstr);
1159 return hres;
1163 if(is_object_instance(argv[0])) {
1164 regexp = iface_to_jsdisp((IUnknown*)get_object(argv[0]));
1165 if(regexp) {
1166 if(!is_class(regexp, JSCLASS_REGEXP)) {
1167 jsdisp_release(regexp);
1168 regexp = NULL;
1173 if(!regexp) {
1174 hres = to_flat_string(ctx, argv[0], &match_jsstr, &match_str);
1175 if(FAILED(hres)) {
1176 jsstr_release(jsstr);
1177 return hres;
1180 match_len = jsstr_length(match_jsstr);
1181 if(!match_len) {
1182 jsstr_release(match_jsstr);
1183 match_str = NULL;
1187 hres = create_array(ctx, 0, &array);
1189 if(SUCCEEDED(hres)) {
1190 ptr = str;
1191 match_result.cp = str;
1192 for(i=0; i<limit; i++) {
1193 if(regexp) {
1194 hres = regexp_match_next(ctx, regexp, REM_NO_PARENS, jsstr, &match_ptr);
1195 if(hres != S_OK)
1196 break;
1197 ptr2 = match_result.cp - match_result.match_len;
1198 }else if(match_str) {
1199 ptr2 = strstrW(ptr, match_str);
1200 if(!ptr2)
1201 break;
1202 }else {
1203 if(!*ptr)
1204 break;
1205 ptr2 = ptr+1;
1208 tmp_str = jsstr_alloc_len(ptr, ptr2-ptr);
1209 if(!tmp_str) {
1210 hres = E_OUTOFMEMORY;
1211 break;
1214 hres = jsdisp_propput_idx(array, i, jsval_string(tmp_str));
1215 jsstr_release(tmp_str);
1216 if(FAILED(hres))
1217 break;
1219 if(regexp)
1220 ptr = match_result.cp;
1221 else if(match_str)
1222 ptr = ptr2 + match_len;
1223 else
1224 ptr++;
1228 if(SUCCEEDED(hres) && (match_str || regexp) && i<limit) {
1229 DWORD len = (str+length) - ptr;
1231 if(len || match_str) {
1232 tmp_str = jsstr_alloc_len(ptr, len);
1234 if(tmp_str) {
1235 hres = jsdisp_propput_idx(array, i, jsval_string(tmp_str));
1236 jsstr_release(tmp_str);
1237 }else {
1238 hres = E_OUTOFMEMORY;
1243 if(regexp)
1244 jsdisp_release(regexp);
1245 if(match_str)
1246 jsstr_release(match_jsstr);
1247 jsstr_release(jsstr);
1249 if(SUCCEEDED(hres) && r)
1250 *r = jsval_obj(array);
1251 else
1252 jsdisp_release(array);
1254 return hres;
1257 static HRESULT String_strike(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1258 jsval_t *r)
1260 static const WCHAR striketagW[] = {'S','T','R','I','K','E',0};
1261 return do_attributeless_tag_format(ctx, jsthis, r, striketagW);
1264 static HRESULT String_sub(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1265 jsval_t *r)
1267 static const WCHAR subtagW[] = {'S','U','B',0};
1268 return do_attributeless_tag_format(ctx, jsthis, r, subtagW);
1271 /* ECMA-262 3rd Edition 15.5.4.15 */
1272 static HRESULT String_substring(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1273 jsval_t *r)
1275 INT start=0, end, length;
1276 jsstr_t *str;
1277 double d;
1278 HRESULT hres;
1280 TRACE("\n");
1282 hres = get_string_val(ctx, jsthis, &str);
1283 if(FAILED(hres))
1284 return hres;
1286 length = jsstr_length(str);
1287 if(argc >= 1) {
1288 hres = to_integer(ctx, argv[0], &d);
1289 if(FAILED(hres)) {
1290 jsstr_release(str);
1291 return hres;
1294 if(d >= 0)
1295 start = is_int32(d) ? min(length, d) : length;
1298 if(argc >= 2) {
1299 hres = to_integer(ctx, argv[1], &d);
1300 if(FAILED(hres)) {
1301 jsstr_release(str);
1302 return hres;
1305 if(d >= 0)
1306 end = is_int32(d) ? min(length, d) : length;
1307 else
1308 end = 0;
1309 }else {
1310 end = length;
1313 if(start > end) {
1314 INT tmp = start;
1315 start = end;
1316 end = tmp;
1319 if(r) {
1320 jsstr_t *ret = jsstr_substr(str, start, end-start);
1321 if(ret)
1322 *r = jsval_string(ret);
1323 else
1324 hres = E_OUTOFMEMORY;
1326 jsstr_release(str);
1327 return hres;
1330 /* ECMA-262 3rd Edition B.2.3 */
1331 static HRESULT String_substr(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1332 jsval_t *r)
1334 int start=0, len, length;
1335 jsstr_t *str;
1336 double d;
1337 HRESULT hres;
1339 TRACE("\n");
1341 hres = get_string_val(ctx, jsthis, &str);
1342 if(FAILED(hres))
1343 return hres;
1345 length = jsstr_length(str);
1346 if(argc >= 1) {
1347 hres = to_integer(ctx, argv[0], &d);
1348 if(FAILED(hres)) {
1349 jsstr_release(str);
1350 return hres;
1353 if(d >= 0)
1354 start = is_int32(d) ? min(length, d) : length;
1357 if(argc >= 2) {
1358 hres = to_integer(ctx, argv[1], &d);
1359 if(FAILED(hres)) {
1360 jsstr_release(str);
1361 return hres;
1364 if(d >= 0.0)
1365 len = is_int32(d) ? min(length-start, d) : length-start;
1366 else
1367 len = 0;
1368 }else {
1369 len = length-start;
1372 hres = S_OK;
1373 if(r) {
1374 jsstr_t *ret = jsstr_substr(str, start, len);
1375 if(ret)
1376 *r = jsval_string(ret);
1377 else
1378 hres = E_OUTOFMEMORY;
1381 jsstr_release(str);
1382 return hres;
1385 static HRESULT String_sup(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1386 jsval_t *r)
1388 static const WCHAR suptagW[] = {'S','U','P',0};
1389 return do_attributeless_tag_format(ctx, jsthis, r, suptagW);
1392 static HRESULT String_toLowerCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1393 jsval_t *r)
1395 jsstr_t *str;
1396 HRESULT hres;
1398 TRACE("\n");
1400 hres = get_string_val(ctx, jsthis, &str);
1401 if(FAILED(hres))
1402 return hres;
1404 if(r) {
1405 jsstr_t *ret;
1406 WCHAR *buf;
1408 buf = jsstr_alloc_buf(jsstr_length(str), &ret);
1409 if(!buf) {
1410 jsstr_release(str);
1411 return E_OUTOFMEMORY;
1414 jsstr_flush(str, buf);
1415 strlwrW(buf);
1416 *r = jsval_string(ret);
1418 jsstr_release(str);
1419 return S_OK;
1422 static HRESULT String_toUpperCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1423 jsval_t *r)
1425 jsstr_t *str;
1426 HRESULT hres;
1428 TRACE("\n");
1430 hres = get_string_val(ctx, jsthis, &str);
1431 if(FAILED(hres))
1432 return hres;
1434 if(r) {
1435 jsstr_t *ret;
1436 WCHAR *buf;
1438 buf = jsstr_alloc_buf(jsstr_length(str), &ret);
1439 if(!buf) {
1440 jsstr_release(str);
1441 return E_OUTOFMEMORY;
1444 jsstr_flush(str, buf);
1445 struprW(buf);
1446 *r = jsval_string(ret);
1448 jsstr_release(str);
1449 return S_OK;
1452 static HRESULT String_toLocaleLowerCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1453 jsval_t *r)
1455 FIXME("\n");
1456 return E_NOTIMPL;
1459 static HRESULT String_toLocaleUpperCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1460 jsval_t *r)
1462 FIXME("\n");
1463 return E_NOTIMPL;
1466 static HRESULT String_localeCompare(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1467 jsval_t *r)
1469 FIXME("\n");
1470 return E_NOTIMPL;
1473 static HRESULT String_get_value(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
1475 StringInstance *This = (StringInstance*)jsthis;
1477 TRACE("\n");
1479 *r = jsval_string(jsstr_addref(This->str));
1480 return S_OK;
1483 static void String_destructor(jsdisp_t *dispex)
1485 StringInstance *This = (StringInstance*)dispex;
1487 jsstr_release(This->str);
1488 heap_free(This);
1491 static unsigned String_idx_length(jsdisp_t *jsdisp)
1493 StringInstance *string = (StringInstance*)jsdisp;
1496 * NOTE: For invoke version < 2, indexed array is not implemented at all.
1497 * Newer jscript.dll versions implement it on string type, not class,
1498 * which is not how it should work according to spec. IE9 implements it
1499 * properly, but it uses its own JavaScript engine inside MSHTML. We
1500 * implement it here, but in the way IE9 and spec work.
1502 return string->dispex.ctx->version < 2 ? 0 : jsstr_length(string->str);
1505 static HRESULT String_idx_get(jsdisp_t *jsdisp, unsigned idx, jsval_t *r)
1507 StringInstance *string = (StringInstance*)jsdisp;
1508 jsstr_t *ret;
1510 ret = jsstr_substr(string->str, idx, 1);
1511 if(!ret)
1512 return E_OUTOFMEMORY;
1514 TRACE("%p[%u] = %s\n", string, idx, debugstr_jsstr(ret));
1516 *r = jsval_string(ret);
1517 return S_OK;
1520 static const builtin_prop_t String_props[] = {
1521 {anchorW, String_anchor, PROPF_METHOD|1},
1522 {bigW, String_big, PROPF_METHOD},
1523 {blinkW, String_blink, PROPF_METHOD},
1524 {boldW, String_bold, PROPF_METHOD},
1525 {charAtW, String_charAt, PROPF_METHOD|1},
1526 {charCodeAtW, String_charCodeAt, PROPF_METHOD|1},
1527 {concatW, String_concat, PROPF_METHOD|1},
1528 {fixedW, String_fixed, PROPF_METHOD},
1529 {fontcolorW, String_fontcolor, PROPF_METHOD|1},
1530 {fontsizeW, String_fontsize, PROPF_METHOD|1},
1531 {indexOfW, String_indexOf, PROPF_METHOD|2},
1532 {italicsW, String_italics, PROPF_METHOD},
1533 {lastIndexOfW, String_lastIndexOf, PROPF_METHOD|2},
1534 {lengthW, NULL,0, String_get_length, String_set_length},
1535 {linkW, String_link, PROPF_METHOD|1},
1536 {localeCompareW, String_localeCompare, PROPF_METHOD|1},
1537 {matchW, String_match, PROPF_METHOD|1},
1538 {replaceW, String_replace, PROPF_METHOD|1},
1539 {searchW, String_search, PROPF_METHOD},
1540 {sliceW, String_slice, PROPF_METHOD},
1541 {smallW, String_small, PROPF_METHOD},
1542 {splitW, String_split, PROPF_METHOD|2},
1543 {strikeW, String_strike, PROPF_METHOD},
1544 {subW, String_sub, PROPF_METHOD},
1545 {substrW, String_substr, PROPF_METHOD|2},
1546 {substringW, String_substring, PROPF_METHOD|2},
1547 {supW, String_sup, PROPF_METHOD},
1548 {toLocaleLowerCaseW, String_toLocaleLowerCase, PROPF_METHOD},
1549 {toLocaleUpperCaseW, String_toLocaleUpperCase, PROPF_METHOD},
1550 {toLowerCaseW, String_toLowerCase, PROPF_METHOD},
1551 {toStringW, String_toString, PROPF_METHOD},
1552 {toUpperCaseW, String_toUpperCase, PROPF_METHOD},
1553 {valueOfW, String_valueOf, PROPF_METHOD}
1556 static const builtin_info_t String_info = {
1557 JSCLASS_STRING,
1558 {NULL, NULL,0, String_get_value},
1559 sizeof(String_props)/sizeof(*String_props),
1560 String_props,
1561 String_destructor,
1562 NULL
1565 static const builtin_prop_t StringInst_props[] = {
1566 {lengthW, NULL,0, String_get_length, String_set_length}
1569 static const builtin_info_t StringInst_info = {
1570 JSCLASS_STRING,
1571 {NULL, NULL,0, String_get_value},
1572 sizeof(StringInst_props)/sizeof(*StringInst_props),
1573 StringInst_props,
1574 String_destructor,
1575 NULL,
1576 String_idx_length,
1577 String_idx_get
1580 /* ECMA-262 3rd Edition 15.5.3.2 */
1581 static HRESULT StringConstr_fromCharCode(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
1582 unsigned argc, jsval_t *argv, jsval_t *r)
1584 WCHAR *ret_str;
1585 DWORD i, code;
1586 jsstr_t *ret;
1587 HRESULT hres;
1589 TRACE("\n");
1591 ret_str = jsstr_alloc_buf(argc, &ret);
1592 if(!ret_str)
1593 return E_OUTOFMEMORY;
1595 for(i=0; i<argc; i++) {
1596 hres = to_uint32(ctx, argv[i], &code);
1597 if(FAILED(hres)) {
1598 jsstr_release(ret);
1599 return hres;
1602 ret_str[i] = code;
1605 if(r)
1606 *r = jsval_string(ret);
1607 else
1608 jsstr_release(ret);
1609 return S_OK;
1612 static HRESULT StringConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1613 jsval_t *r)
1615 HRESULT hres;
1617 TRACE("\n");
1619 switch(flags) {
1620 case INVOKE_FUNC: {
1621 jsstr_t *str;
1623 if(argc) {
1624 hres = to_string(ctx, argv[0], &str);
1625 if(FAILED(hres))
1626 return hres;
1627 }else {
1628 str = jsstr_empty();
1631 *r = jsval_string(str);
1632 break;
1634 case DISPATCH_CONSTRUCT: {
1635 jsstr_t *str;
1636 jsdisp_t *ret;
1638 if(argc) {
1639 hres = to_string(ctx, argv[0], &str);
1640 if(FAILED(hres))
1641 return hres;
1642 }else {
1643 str = jsstr_empty();
1646 hres = create_string(ctx, str, &ret);
1647 if (SUCCEEDED(hres)) *r = jsval_obj(ret);
1648 jsstr_release(str);
1649 return hres;
1652 default:
1653 FIXME("unimplemented flags: %x\n", flags);
1654 return E_NOTIMPL;
1657 return S_OK;
1660 static HRESULT string_alloc(script_ctx_t *ctx, jsdisp_t *object_prototype, jsstr_t *str, StringInstance **ret)
1662 StringInstance *string;
1663 HRESULT hres;
1665 string = heap_alloc_zero(sizeof(StringInstance));
1666 if(!string)
1667 return E_OUTOFMEMORY;
1669 if(object_prototype)
1670 hres = init_dispex(&string->dispex, ctx, &String_info, object_prototype);
1671 else
1672 hres = init_dispex_from_constr(&string->dispex, ctx, &StringInst_info, ctx->string_constr);
1673 if(FAILED(hres)) {
1674 heap_free(string);
1675 return hres;
1678 string->str = jsstr_addref(str);
1679 *ret = string;
1680 return S_OK;
1683 static const builtin_prop_t StringConstr_props[] = {
1684 {fromCharCodeW, StringConstr_fromCharCode, PROPF_METHOD},
1687 static const builtin_info_t StringConstr_info = {
1688 JSCLASS_FUNCTION,
1689 DEFAULT_FUNCTION_VALUE,
1690 sizeof(StringConstr_props)/sizeof(*StringConstr_props),
1691 StringConstr_props,
1692 NULL,
1693 NULL
1696 HRESULT create_string_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1698 StringInstance *string;
1699 HRESULT hres;
1701 static const WCHAR StringW[] = {'S','t','r','i','n','g',0};
1703 hres = string_alloc(ctx, object_prototype, jsstr_empty(), &string);
1704 if(FAILED(hres))
1705 return hres;
1707 hres = create_builtin_constructor(ctx, StringConstr_value, StringW, &StringConstr_info,
1708 PROPF_CONSTR|1, &string->dispex, ret);
1710 jsdisp_release(&string->dispex);
1711 return hres;
1714 HRESULT create_string(script_ctx_t *ctx, jsstr_t *str, jsdisp_t **ret)
1716 StringInstance *string;
1717 HRESULT hres;
1719 hres = string_alloc(ctx, NULL, str, &string);
1720 if(FAILED(hres))
1721 return hres;
1723 *ret = &string->dispex;
1724 return S_OK;