kernel32/tests: Init pointers in common way, add some win_skip() while skipping.
[wine/hramrach.git] / dlls / jscript / string.c
blob95ad7737edb3a1031d196ac68c3e48ff5c69a00f
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 "jscript.h"
21 #include "wine/debug.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
25 typedef struct {
26 DispatchEx dispex;
28 WCHAR *str;
29 DWORD length;
30 } StringInstance;
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;
81 VARIANT this_var;
82 HRESULT hres;
84 if((string = string_this(jsthis))) {
85 *str = string->str;
86 *len = string->length;
87 *val_str = NULL;
88 return S_OK;
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);
94 if(FAILED(hres))
95 return hres;
97 *str = *val_str;
98 *len = SysStringLen(*val_str);
99 return S_OK;
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);
107 switch(flags) {
108 case DISPATCH_PROPERTYGET: {
109 StringInstance *string = string_from_vdisp(jsthis);
111 V_VT(retv) = VT_I4;
112 V_I4(retv) = string->length;
113 break;
115 default:
116 FIXME("unimplemented flags %x\n", flags);
117 return E_NOTIMPL;
120 return S_OK;
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");
129 return E_FAIL;
132 if(retv) {
133 BSTR str = SysAllocString(string->str);
134 if(!str)
135 return E_OUTOFMEMORY;
137 V_VT(retv) = VT_BSTR;
138 V_BSTR(retv) = str;
140 return S_OK;
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)
147 TRACE("\n");
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)
156 TRACE("\n");
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)
164 const WCHAR *str;
165 DWORD length;
166 BSTR val_str = NULL;
167 HRESULT hres;
169 static const WCHAR tagfmt[] = {'<','%','s','>','%','s','<','/','%','s','>',0};
171 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
172 if(FAILED(hres))
173 return hres;
175 if(retv) {
176 BSTR ret = SysAllocStringLen(NULL, length + 2*strlenW(tagname) + 5);
177 if(!ret) {
178 SysFreeString(val_str);
179 return E_OUTOFMEMORY;
182 sprintfW(ret, tagfmt, tagname, str, tagname);
184 V_VT(retv) = VT_BSTR;
185 V_BSTR(retv) = ret;
188 SysFreeString(val_str);
189 return S_OK;
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;
201 const WCHAR *str;
202 DWORD length;
203 BSTR attr_value, val_str = NULL;
204 HRESULT hres;
206 if(!(string = string_this(jsthis))) {
207 VARIANT this;
209 V_VT(&this) = VT_DISPATCH;
210 V_DISPATCH(&this) = jsthis->u.disp;
212 hres = to_string(ctx, &this, ei, &val_str);
213 if(FAILED(hres))
214 return hres;
216 str = val_str;
217 length = SysStringLen(val_str);
219 else {
220 str = string->str;
221 length = string->length;
224 if(arg_cnt(dp)) {
225 hres = to_string(ctx, get_arg(dp, 0), ei, &attr_value);
226 if(FAILED(hres)) {
227 SysFreeString(val_str);
228 return hres;
231 else {
232 attr_value = SysAllocString(undefinedW);
233 if(!attr_value) {
234 SysFreeString(val_str);
235 return E_OUTOFMEMORY;
239 if(retv) {
240 BSTR ret = SysAllocStringLen(NULL, length + 2*strlenW(tagname)
241 + strlenW(attr) + SysStringLen(attr_value) + 9);
242 if(!ret) {
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;
251 V_BSTR(retv) = ret;
254 SysFreeString(attr_value);
255 SysFreeString(val_str);
256 return S_OK;
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)
293 const WCHAR *str;
294 DWORD length;
295 BSTR ret, val_str;
296 INT pos = 0;
297 HRESULT hres;
299 TRACE("\n");
301 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
302 if(FAILED(hres))
303 return hres;
305 if(arg_cnt(dp)) {
306 VARIANT num;
308 hres = to_integer(ctx, get_arg(dp, 0), ei, &num);
309 if(FAILED(hres)) {
310 SysFreeString(val_str);
311 return hres;
314 if(V_VT(&num) == VT_I4) {
315 pos = V_I4(&num);
316 }else {
317 WARN("pos = %lf\n", V_R8(&num));
318 pos = -1;
322 if(!retv) {
323 SysFreeString(val_str);
324 return S_OK;
327 if(0 <= pos && pos < length)
328 ret = SysAllocStringLen(str+pos, 1);
329 else
330 ret = SysAllocStringLen(NULL, 0);
331 SysFreeString(val_str);
332 if(!ret) {
333 return E_OUTOFMEMORY;
336 V_VT(retv) = VT_BSTR;
337 V_BSTR(retv) = ret;
338 return S_OK;
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)
345 const WCHAR *str;
346 BSTR val_str;
347 DWORD length, idx = 0;
348 HRESULT hres;
350 TRACE("\n");
352 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
353 if(FAILED(hres))
354 return hres;
356 if(arg_cnt(dp) > 0) {
357 VARIANT v;
359 hres = to_integer(ctx, get_arg(dp, 0), ei, &v);
360 if(FAILED(hres)) {
361 SysFreeString(val_str);
362 return hres;
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);
368 return S_OK;
371 idx = V_I4(&v);
374 if(retv) {
375 V_VT(retv) = VT_I4;
376 V_I4(retv) = str[idx];
379 SysFreeString(val_str);
380 return S_OK;
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;
389 VARIANT var;
390 WCHAR *ptr;
391 HRESULT hres;
393 TRACE("\n");
395 str_cnt = arg_cnt(dp)+1;
396 strs = heap_alloc_zero(str_cnt * sizeof(BSTR));
397 if(!strs)
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);
407 if(FAILED(hres))
408 break;
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));
421 ptr += l;
425 for(i=0; i < str_cnt; i++)
426 SysFreeString(strs[i]);
427 heap_free(strs);
429 if(FAILED(hres))
430 return hres;
432 if(retv) {
433 V_VT(retv) = VT_BSTR;
434 V_BSTR(retv) = ret;
435 }else {
436 SysFreeString(ret);
438 return S_OK;
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;
470 const WCHAR *str;
471 BSTR search_str, val_str;
472 INT ret = -1;
473 HRESULT hres;
475 TRACE("\n");
477 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
478 if(FAILED(hres))
479 return hres;
481 if(!arg_cnt(dp)) {
482 if(retv) {
483 V_VT(retv) = VT_I4;
484 V_I4(retv) = -1;
486 SysFreeString(val_str);
487 return S_OK;
490 hres = to_string(ctx, get_arg(dp,0), ei, &search_str);
491 if(FAILED(hres)) {
492 SysFreeString(val_str);
493 return hres;
496 if(arg_cnt(dp) >= 2) {
497 VARIANT ival;
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;
503 else
504 pos = V_R8(&ival) > 0.0 ? length : 0;
505 if(pos > length)
506 pos = length;
510 if(SUCCEEDED(hres)) {
511 const WCHAR *ptr;
513 ptr = strstrW(str+pos, search_str);
514 if(ptr)
515 ret = ptr - str;
516 else
517 ret = -1;
520 SysFreeString(search_str);
521 SysFreeString(val_str);
522 if(FAILED(hres))
523 return hres;
525 if(retv) {
526 V_VT(retv) = VT_I4;
527 V_I4(retv) = ret;
529 return S_OK;
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;
545 const WCHAR *str;
546 INT ret = -1;
547 HRESULT hres;
549 TRACE("\n");
551 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
552 if(FAILED(hres))
553 return hres;
555 if(!arg_cnt(dp)) {
556 if(retv) {
557 V_VT(retv) = VT_I4;
558 V_I4(retv) = -1;
560 SysFreeString(val_str);
561 return S_OK;
564 hres = to_string(ctx, get_arg(dp,0), ei, &search_str);
565 if(FAILED(hres)) {
566 SysFreeString(val_str);
567 return hres;
570 search_len = SysStringLen(search_str);
572 if(arg_cnt(dp) >= 2) {
573 VARIANT ival;
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;
579 else
580 pos = V_R8(&ival) > 0.0 ? length : 0;
581 if(pos > length)
582 pos = length;
584 }else {
585 pos = length;
588 if(SUCCEEDED(hres) && length >= search_len) {
589 const WCHAR *ptr;
591 for(ptr = str+min(pos, length-search_len); ptr >= str; ptr--) {
592 if(!memcmp(ptr, search_str, search_len*sizeof(WCHAR))) {
593 ret = ptr-str;
594 break;
599 SysFreeString(search_str);
600 SysFreeString(val_str);
601 if(FAILED(hres))
602 return hres;
604 if(retv) {
605 V_VT(retv) = VT_I4;
606 V_I4(retv) = ret;
608 return S_OK;
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)
624 const WCHAR *str;
625 match_result_t *match_result;
626 DispatchEx *regexp;
627 DispatchEx *array;
628 VARIANT var, *arg_var;
629 DWORD length, match_cnt, i;
630 BSTR val_str = NULL;
631 HRESULT hres = S_OK;
633 TRACE("\n");
635 if(!arg_cnt(dp)) {
636 if(retv) {
637 V_VT(retv) = VT_NULL;
640 return S_OK;
643 arg_var = get_arg(dp, 0);
644 switch(V_VT(arg_var)) {
645 case VT_DISPATCH:
646 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
647 if(regexp) {
648 if(regexp->builtin_info->class == JSCLASS_REGEXP)
649 break;
650 jsdisp_release(regexp);
652 default: {
653 BSTR match_str;
655 hres = to_string(ctx, arg_var, ei, &match_str);
656 if(FAILED(hres))
657 return hres;
659 hres = create_regexp(ctx, match_str, SysStringLen(match_str), 0, &regexp);
660 SysFreeString(match_str);
661 if(FAILED(hres))
662 return hres;
666 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
667 if(SUCCEEDED(hres))
668 hres = regexp_match(ctx, regexp, str, length, FALSE, &match_result, &match_cnt);
669 jsdisp_release(regexp);
670 if(FAILED(hres)) {
671 SysFreeString(val_str);
672 return hres;
675 if(!match_cnt) {
676 TRACE("no match\n");
678 if(retv)
679 V_VT(retv) = VT_NULL;
681 SysFreeString(val_str);
682 return S_OK;
685 hres = create_array(ctx, match_cnt, &array);
686 if(FAILED(hres)) {
687 SysFreeString(val_str);
688 return hres;
691 V_VT(&var) = VT_BSTR;
693 for(i=0; i < match_cnt; i++) {
694 V_BSTR(&var) = SysAllocStringLen(match_result[i].str, match_result[i].len);
695 if(!V_BSTR(&var)) {
696 hres = E_OUTOFMEMORY;
697 break;
700 hres = jsdisp_propput_idx(array, i, &var, ei, NULL/*FIXME*/);
701 SysFreeString(V_BSTR(&var));
702 if(FAILED(hres))
703 break;
706 SysFreeString(val_str);
708 if(SUCCEEDED(hres) && retv) {
709 V_VT(retv) = VT_DISPATCH;
710 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(array);
711 }else {
712 jsdisp_release(array);
714 return hres;
717 typedef struct {
718 WCHAR *buf;
719 DWORD size;
720 DWORD len;
721 } strbuf_t;
723 static HRESULT strbuf_append(strbuf_t *buf, const WCHAR *str, DWORD len)
725 if(!len)
726 return S_OK;
728 if(len + buf->len > buf->size) {
729 WCHAR *new_buf;
730 DWORD new_size;
732 new_size = buf->size ? buf->size<<1 : 16;
733 if(new_size < buf->len+len)
734 new_size = buf->len+len;
735 if(buf->buf)
736 new_buf = heap_realloc(buf->buf, new_size*sizeof(WCHAR));
737 else
738 new_buf = heap_alloc(new_size*sizeof(WCHAR));
739 if(!new_buf)
740 return E_OUTOFMEMORY;
742 buf->buf = new_buf;
743 buf->size = new_size;
746 memcpy(buf->buf+buf->len, str, len*sizeof(WCHAR));
747 buf->len += len;
748 return S_OK;
751 static HRESULT rep_call(script_ctx_t *ctx, DispatchEx *func, const WCHAR *str, match_result_t *match,
752 match_result_t *parens, DWORD parens_cnt, BSTR *ret, jsexcept_t *ei, IServiceProvider *caller)
754 DISPPARAMS dp = {NULL, NULL, 0, 0};
755 VARIANTARG *args, *arg;
756 VARIANT var;
757 DWORD i;
758 HRESULT hres = S_OK;
760 dp.cArgs = parens_cnt+3;
761 dp.rgvarg = args = heap_alloc_zero(sizeof(VARIANT)*dp.cArgs);
762 if(!args)
763 return E_OUTOFMEMORY;
765 arg = get_arg(&dp,0);
766 V_VT(arg) = VT_BSTR;
767 V_BSTR(arg) = SysAllocStringLen(match->str, match->len);
768 if(!V_BSTR(arg))
769 hres = E_OUTOFMEMORY;
771 if(SUCCEEDED(hres)) {
772 for(i=0; i < parens_cnt; i++) {
773 arg = get_arg(&dp,i+1);
774 V_VT(arg) = VT_BSTR;
775 V_BSTR(arg) = SysAllocStringLen(parens[i].str, parens[i].len);
776 if(!V_BSTR(arg)) {
777 hres = E_OUTOFMEMORY;
778 break;
783 if(SUCCEEDED(hres)) {
784 arg = get_arg(&dp,parens_cnt+1);
785 V_VT(arg) = VT_I4;
786 V_I4(arg) = match->str - str;
788 arg = get_arg(&dp,parens_cnt+2);
789 V_VT(arg) = VT_BSTR;
790 V_BSTR(arg) = SysAllocString(str);
791 if(!V_BSTR(arg))
792 hres = E_OUTOFMEMORY;
795 if(SUCCEEDED(hres))
796 hres = jsdisp_call_value(func, DISPATCH_METHOD, &dp, &var, ei, caller);
798 for(i=0; i < parens_cnt+1; i++) {
799 if(i != parens_cnt+1)
800 SysFreeString(V_BSTR(get_arg(&dp,i)));
802 heap_free(args);
804 if(FAILED(hres))
805 return hres;
807 hres = to_string(ctx, &var, ei, ret);
808 VariantClear(&var);
809 return hres;
812 /* ECMA-262 3rd Edition 15.5.4.11 */
813 static HRESULT String_replace(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
814 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
816 const WCHAR *str;
817 DWORD parens_cnt = 0, parens_size=0, rep_len=0, length;
818 BSTR rep_str = NULL, match_str = NULL, ret_str, val_str;
819 DispatchEx *rep_func = NULL, *regexp = NULL;
820 match_result_t *parens = NULL, match, **parens_ptr = &parens;
821 strbuf_t ret = {NULL,0,0};
822 BOOL gcheck = FALSE;
823 VARIANT *arg_var;
824 HRESULT hres = S_OK;
826 TRACE("\n");
828 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
829 if(FAILED(hres))
830 return hres;
832 if(!arg_cnt(dp)) {
833 if(retv) {
834 if(!val_str) {
835 val_str = SysAllocStringLen(str, length);
836 if(!val_str)
837 return E_OUTOFMEMORY;
840 V_VT(retv) = VT_BSTR;
841 V_BSTR(retv) = val_str;
843 return S_OK;
846 arg_var = get_arg(dp, 0);
847 switch(V_VT(arg_var)) {
848 case VT_DISPATCH:
849 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
850 if(regexp) {
851 if(is_class(regexp, JSCLASS_REGEXP)) {
852 break;
853 }else {
854 jsdisp_release(regexp);
855 regexp = NULL;
859 default:
860 hres = to_string(ctx, arg_var, ei, &match_str);
861 if(FAILED(hres)) {
862 SysFreeString(val_str);
863 return hres;
867 if(arg_cnt(dp) >= 2) {
868 arg_var = get_arg(dp,1);
869 switch(V_VT(arg_var)) {
870 case VT_DISPATCH:
871 rep_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
872 if(rep_func) {
873 if(is_class(rep_func, JSCLASS_FUNCTION)) {
874 break;
875 }else {
876 jsdisp_release(rep_func);
877 rep_func = NULL;
881 default:
882 hres = to_string(ctx, arg_var, ei, &rep_str);
883 if(FAILED(hres))
884 break;
886 rep_len = SysStringLen(rep_str);
887 if(!strchrW(rep_str, '$'))
888 parens_ptr = NULL;
892 if(SUCCEEDED(hres)) {
893 const WCHAR *cp, *ecp;
895 cp = ecp = str;
897 while(1) {
898 if(regexp) {
899 hres = regexp_match_next(ctx, regexp, gcheck, str, length, &cp, parens_ptr,
900 &parens_size, &parens_cnt, &match);
901 gcheck = TRUE;
903 if(hres == S_FALSE) {
904 hres = S_OK;
905 break;
907 if(FAILED(hres))
908 break;
909 }else {
910 match.str = strstrW(cp, match_str);
911 if(!match.str)
912 break;
913 match.len = SysStringLen(match_str);
914 cp = match.str+match.len;
917 hres = strbuf_append(&ret, ecp, match.str-ecp);
918 ecp = match.str+match.len;
919 if(FAILED(hres))
920 break;
922 if(rep_func) {
923 BSTR cstr;
925 hres = rep_call(ctx, rep_func, str, &match, parens, parens_cnt, &cstr, ei, caller);
926 if(FAILED(hres))
927 break;
929 hres = strbuf_append(&ret, cstr, SysStringLen(cstr));
930 SysFreeString(cstr);
931 if(FAILED(hres))
932 break;
933 }else if(rep_str && regexp) {
934 const WCHAR *ptr = rep_str, *ptr2;
936 while((ptr2 = strchrW(ptr, '$'))) {
937 hres = strbuf_append(&ret, ptr, ptr2-ptr);
938 if(FAILED(hres))
939 break;
941 switch(ptr2[1]) {
942 case '$':
943 hres = strbuf_append(&ret, ptr2, 1);
944 ptr = ptr2+2;
945 break;
946 case '&':
947 hres = strbuf_append(&ret, match.str, match.len);
948 ptr = ptr2+2;
949 break;
950 case '`':
951 hres = strbuf_append(&ret, str, match.str-str);
952 ptr = ptr2+2;
953 break;
954 case '\'':
955 hres = strbuf_append(&ret, ecp, (str+length)-ecp);
956 ptr = ptr2+2;
957 break;
958 default: {
959 DWORD idx;
961 if(!isdigitW(ptr2[1])) {
962 hres = strbuf_append(&ret, ptr2, 1);
963 ptr = ptr2+1;
964 break;
967 idx = ptr2[1] - '0';
968 if(isdigitW(ptr2[2]) && idx*10 + (ptr2[2]-'0') <= parens_cnt) {
969 idx = idx*10 + (ptr[2]-'0');
970 ptr = ptr2+3;
971 }else if(idx && idx <= parens_cnt) {
972 ptr = ptr2+2;
973 }else {
974 hres = strbuf_append(&ret, ptr2, 1);
975 ptr = ptr2+1;
976 break;
979 hres = strbuf_append(&ret, parens[idx-1].str, parens[idx-1].len);
983 if(FAILED(hres))
984 break;
987 if(SUCCEEDED(hres))
988 hres = strbuf_append(&ret, ptr, (rep_str+rep_len)-ptr);
989 if(FAILED(hres))
990 break;
991 }else if(rep_str) {
992 hres = strbuf_append(&ret, rep_str, rep_len);
993 if(FAILED(hres))
994 break;
995 }else {
996 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d'};
998 hres = strbuf_append(&ret, undefinedW, sizeof(undefinedW)/sizeof(WCHAR));
999 if(FAILED(hres))
1000 break;
1004 if(SUCCEEDED(hres))
1005 hres = strbuf_append(&ret, ecp, (str+length)-ecp);
1008 if(rep_func)
1009 jsdisp_release(rep_func);
1010 if(regexp)
1011 jsdisp_release(regexp);
1012 SysFreeString(val_str);
1013 SysFreeString(rep_str);
1014 SysFreeString(match_str);
1015 heap_free(parens);
1017 if(SUCCEEDED(hres) && retv) {
1018 ret_str = SysAllocStringLen(ret.buf, ret.len);
1019 if(!ret_str)
1020 return E_OUTOFMEMORY;
1022 V_VT(retv) = VT_BSTR;
1023 V_BSTR(retv) = ret_str;
1024 TRACE("= %s\n", debugstr_w(ret_str));
1027 heap_free(ret.buf);
1028 return hres;
1031 static HRESULT String_search(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1032 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1034 FIXME("\n");
1035 return E_NOTIMPL;
1038 /* ECMA-262 3rd Edition 15.5.4.13 */
1039 static HRESULT String_slice(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1040 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1042 const WCHAR *str;
1043 BSTR val_str;
1044 DWORD length;
1045 INT start=0, end;
1046 VARIANT v;
1047 HRESULT hres;
1049 TRACE("\n");
1051 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
1052 if(FAILED(hres))
1053 return hres;
1055 if(arg_cnt(dp)) {
1056 hres = to_integer(ctx, get_arg(dp,0), ei, &v);
1057 if(FAILED(hres)) {
1058 SysFreeString(val_str);
1059 return hres;
1062 if(V_VT(&v) == VT_I4) {
1063 start = V_I4(&v);
1064 if(start < 0) {
1065 start = length + start;
1066 if(start < 0)
1067 start = 0;
1068 }else if(start > length) {
1069 start = length;
1071 }else {
1072 start = V_R8(&v) < 0.0 ? 0 : length;
1074 }else {
1075 start = 0;
1078 if(arg_cnt(dp) >= 2) {
1079 hres = to_integer(ctx, get_arg(dp,1), ei, &v);
1080 if(FAILED(hres)) {
1081 SysFreeString(val_str);
1082 return hres;
1085 if(V_VT(&v) == VT_I4) {
1086 end = V_I4(&v);
1087 if(end < 0) {
1088 end = length + end;
1089 if(end < 0)
1090 end = 0;
1091 }else if(end > length) {
1092 end = length;
1094 }else {
1095 end = V_R8(&v) < 0.0 ? 0 : length;
1097 }else {
1098 end = length;
1101 if(end < start)
1102 end = start;
1104 if(retv) {
1105 BSTR retstr = SysAllocStringLen(str+start, end-start);
1106 if(!retstr) {
1107 SysFreeString(val_str);
1108 return E_OUTOFMEMORY;
1111 V_VT(retv) = VT_BSTR;
1112 V_BSTR(retv) = retstr;
1115 SysFreeString(val_str);
1116 return S_OK;
1119 static HRESULT String_small(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1120 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1122 static const WCHAR smalltagW[] = {'S','M','A','L','L',0};
1123 return do_attributeless_tag_format(ctx, jsthis, flags, dp, retv, ei, sp, smalltagW);
1126 static HRESULT String_split(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1127 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1129 match_result_t *match_result = NULL;
1130 DWORD length, match_cnt, i, match_len = 0;
1131 const WCHAR *str, *ptr, *ptr2;
1132 BOOL use_regexp = FALSE;
1133 VARIANT *arg, var;
1134 DispatchEx *array;
1135 BSTR val_str, match_str = NULL;
1136 HRESULT hres;
1138 TRACE("\n");
1140 if(arg_cnt(dp) != 1) {
1141 FIXME("unsupported args\n");
1142 return E_NOTIMPL;
1145 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
1146 if(FAILED(hres))
1147 return hres;
1149 arg = get_arg(dp, 0);
1150 switch(V_VT(arg)) {
1151 case VT_DISPATCH: {
1152 DispatchEx *regexp;
1154 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
1155 if(regexp) {
1156 if(is_class(regexp, JSCLASS_REGEXP)) {
1157 use_regexp = TRUE;
1158 hres = regexp_match(ctx, regexp, str, length, TRUE, &match_result, &match_cnt);
1159 jsdisp_release(regexp);
1160 if(FAILED(hres)) {
1161 SysFreeString(val_str);
1162 return hres;
1164 break;
1166 jsdisp_release(regexp);
1169 default:
1170 hres = to_string(ctx, arg, ei, &match_str);
1171 if(FAILED(hres)) {
1172 SysFreeString(val_str);
1173 return hres;
1176 match_len = SysStringLen(match_str);
1177 if(!match_len) {
1178 SysFreeString(match_str);
1179 match_str = NULL;
1183 hres = create_array(ctx, 0, &array);
1185 if(SUCCEEDED(hres)) {
1186 ptr = str;
1187 for(i=0;; i++) {
1188 if(use_regexp) {
1189 if(i == match_cnt)
1190 break;
1191 ptr2 = match_result[i].str;
1192 }else if(match_str) {
1193 ptr2 = strstrW(ptr, match_str);
1194 if(!ptr2)
1195 break;
1196 }else {
1197 if(!*ptr)
1198 break;
1199 ptr2 = ptr+1;
1202 V_VT(&var) = VT_BSTR;
1203 V_BSTR(&var) = SysAllocStringLen(ptr, ptr2-ptr);
1204 if(!V_BSTR(&var)) {
1205 hres = E_OUTOFMEMORY;
1206 break;
1209 hres = jsdisp_propput_idx(array, i, &var, ei, sp);
1210 SysFreeString(V_BSTR(&var));
1211 if(FAILED(hres))
1212 break;
1214 if(use_regexp)
1215 ptr = match_result[i].str + match_result[i].len;
1216 else if(match_str)
1217 ptr = ptr2 + match_len;
1218 else
1219 ptr++;
1223 if(SUCCEEDED(hres) && (match_str || use_regexp)) {
1224 DWORD len = (str+length) - ptr;
1226 if(len || match_str) {
1227 V_VT(&var) = VT_BSTR;
1228 V_BSTR(&var) = SysAllocStringLen(ptr, len);
1230 if(V_BSTR(&var)) {
1231 hres = jsdisp_propput_idx(array, i, &var, ei, sp);
1232 SysFreeString(V_BSTR(&var));
1233 }else {
1234 hres = E_OUTOFMEMORY;
1239 SysFreeString(match_str);
1240 SysFreeString(val_str);
1241 heap_free(match_result);
1243 if(SUCCEEDED(hres) && retv) {
1244 V_VT(retv) = VT_DISPATCH;
1245 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(array);
1246 }else {
1247 jsdisp_release(array);
1250 return hres;
1253 static HRESULT String_strike(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1254 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1256 static const WCHAR striketagW[] = {'S','T','R','I','K','E',0};
1257 return do_attributeless_tag_format(ctx, jsthis, flags, dp, retv, ei, sp, striketagW);
1260 static HRESULT String_sub(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1261 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1263 static const WCHAR subtagW[] = {'S','U','B',0};
1264 return do_attributeless_tag_format(ctx, jsthis, flags, dp, retv, ei, sp, subtagW);
1267 /* ECMA-262 3rd Edition 15.5.4.15 */
1268 static HRESULT String_substring(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1269 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1271 const WCHAR *str;
1272 BSTR val_str;
1273 INT start=0, end;
1274 DWORD length;
1275 VARIANT v;
1276 HRESULT hres;
1278 TRACE("\n");
1280 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
1281 if(FAILED(hres))
1282 return hres;
1284 if(arg_cnt(dp) >= 1) {
1285 hres = to_integer(ctx, get_arg(dp,0), ei, &v);
1286 if(FAILED(hres)) {
1287 SysFreeString(val_str);
1288 return hres;
1291 if(V_VT(&v) == VT_I4) {
1292 start = V_I4(&v);
1293 if(start < 0)
1294 start = 0;
1295 else if(start >= length)
1296 start = length;
1297 }else {
1298 start = V_R8(&v) < 0.0 ? 0 : length;
1302 if(arg_cnt(dp) >= 2) {
1303 hres = to_integer(ctx, get_arg(dp,1), ei, &v);
1304 if(FAILED(hres)) {
1305 SysFreeString(val_str);
1306 return hres;
1309 if(V_VT(&v) == VT_I4) {
1310 end = V_I4(&v);
1311 if(end < 0)
1312 end = 0;
1313 else if(end > length)
1314 end = length;
1315 }else {
1316 end = V_R8(&v) < 0.0 ? 0 : length;
1318 }else {
1319 end = length;
1322 if(start > end) {
1323 INT tmp = start;
1324 start = end;
1325 end = tmp;
1328 if(retv) {
1329 V_VT(retv) = VT_BSTR;
1330 V_BSTR(retv) = SysAllocStringLen(str+start, end-start);
1331 if(!V_BSTR(retv)) {
1332 SysFreeString(val_str);
1333 return E_OUTOFMEMORY;
1336 SysFreeString(val_str);
1337 return S_OK;
1340 /* ECMA-262 3rd Edition B.2.3 */
1341 static HRESULT String_substr(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1342 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1344 BSTR val_str;
1345 const WCHAR *str;
1346 INT start=0, len;
1347 DWORD length;
1348 VARIANT v;
1349 HRESULT hres;
1351 TRACE("\n");
1353 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
1354 if(FAILED(hres))
1355 return hres;
1357 if(arg_cnt(dp) >= 1) {
1358 hres = to_integer(ctx, get_arg(dp,0), ei, &v);
1359 if(FAILED(hres)) {
1360 SysFreeString(val_str);
1361 return hres;
1364 if(V_VT(&v) == VT_I4) {
1365 start = V_I4(&v);
1366 if(start < 0)
1367 start = 0;
1368 else if(start >= length)
1369 start = length;
1370 }else {
1371 start = V_R8(&v) < 0.0 ? 0 : length;
1375 if(arg_cnt(dp) >= 2) {
1376 hres = to_integer(ctx, get_arg(dp,1), ei, &v);
1377 if(FAILED(hres)) {
1378 SysFreeString(val_str);
1379 return hres;
1382 if(V_VT(&v) == VT_I4) {
1383 len = V_I4(&v);
1384 if(len < 0)
1385 len = 0;
1386 else if(len > length-start)
1387 len = length-start;
1388 }else {
1389 len = V_R8(&v) < 0.0 ? 0 : length-start;
1391 }else {
1392 len = length-start;
1395 hres = S_OK;
1396 if(retv) {
1397 V_VT(retv) = VT_BSTR;
1398 V_BSTR(retv) = SysAllocStringLen(str+start, len);
1399 if(!V_BSTR(retv))
1400 hres = E_OUTOFMEMORY;
1403 SysFreeString(val_str);
1404 return hres;
1407 static HRESULT String_sup(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1408 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1410 static const WCHAR suptagW[] = {'S','U','P',0};
1411 return do_attributeless_tag_format(ctx, jsthis, flags, dp, retv, ei, sp, suptagW);
1414 static HRESULT String_toLowerCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1415 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1417 const WCHAR* str;
1418 DWORD length;
1419 BSTR val_str;
1420 HRESULT hres;
1422 TRACE("\n");
1424 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
1425 if(FAILED(hres))
1426 return hres;
1428 if(retv) {
1429 if(!val_str) {
1430 val_str = SysAllocStringLen(str, length);
1431 if(!val_str)
1432 return E_OUTOFMEMORY;
1435 strlwrW(val_str);
1437 V_VT(retv) = VT_BSTR;
1438 V_BSTR(retv) = val_str;
1440 else SysFreeString(val_str);
1441 return S_OK;
1444 static HRESULT String_toUpperCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1445 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1447 const WCHAR* str;
1448 DWORD length;
1449 BSTR val_str;
1450 HRESULT hres;
1452 TRACE("\n");
1454 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
1455 if(FAILED(hres))
1456 return hres;
1458 if(retv) {
1459 if(!val_str) {
1460 val_str = SysAllocStringLen(str, length);
1461 if(!val_str)
1462 return E_OUTOFMEMORY;
1465 struprW(val_str);
1467 V_VT(retv) = VT_BSTR;
1468 V_BSTR(retv) = val_str;
1470 else SysFreeString(val_str);
1471 return S_OK;
1474 static HRESULT String_toLocaleLowerCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1475 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1477 FIXME("\n");
1478 return E_NOTIMPL;
1481 static HRESULT String_toLocaleUpperCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1482 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1484 FIXME("\n");
1485 return E_NOTIMPL;
1488 static HRESULT String_localeCompare(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1489 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1491 FIXME("\n");
1492 return E_NOTIMPL;
1495 static HRESULT String_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1496 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1498 StringInstance *This = string_from_vdisp(jsthis);
1500 TRACE("\n");
1502 switch(flags) {
1503 case INVOKE_FUNC:
1504 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
1505 case DISPATCH_PROPERTYGET: {
1506 BSTR str = SysAllocString(This->str);
1507 if(!str)
1508 return E_OUTOFMEMORY;
1510 V_VT(retv) = VT_BSTR;
1511 V_BSTR(retv) = str;
1512 break;
1514 default:
1515 FIXME("flags %x\n", flags);
1516 return E_NOTIMPL;
1519 return S_OK;
1522 static void String_destructor(DispatchEx *dispex)
1524 StringInstance *This = (StringInstance*)dispex;
1526 heap_free(This->str);
1527 heap_free(This);
1530 static const builtin_prop_t String_props[] = {
1531 {anchorW, String_anchor, PROPF_METHOD|1},
1532 {bigW, String_big, PROPF_METHOD},
1533 {blinkW, String_blink, PROPF_METHOD},
1534 {boldW, String_bold, PROPF_METHOD},
1535 {charAtW, String_charAt, PROPF_METHOD|1},
1536 {charCodeAtW, String_charCodeAt, PROPF_METHOD|1},
1537 {concatW, String_concat, PROPF_METHOD|1},
1538 {fixedW, String_fixed, PROPF_METHOD},
1539 {fontcolorW, String_fontcolor, PROPF_METHOD|1},
1540 {fontsizeW, String_fontsize, PROPF_METHOD|1},
1541 {indexOfW, String_indexOf, PROPF_METHOD|2},
1542 {italicsW, String_italics, PROPF_METHOD},
1543 {lastIndexOfW, String_lastIndexOf, PROPF_METHOD|2},
1544 {lengthW, String_length, 0},
1545 {linkW, String_link, PROPF_METHOD|1},
1546 {localeCompareW, String_localeCompare, PROPF_METHOD|1},
1547 {matchW, String_match, PROPF_METHOD|1},
1548 {replaceW, String_replace, PROPF_METHOD|1},
1549 {searchW, String_search, PROPF_METHOD},
1550 {sliceW, String_slice, PROPF_METHOD},
1551 {smallW, String_small, PROPF_METHOD},
1552 {splitW, String_split, PROPF_METHOD|2},
1553 {strikeW, String_strike, PROPF_METHOD},
1554 {subW, String_sub, PROPF_METHOD},
1555 {substrW, String_substr, PROPF_METHOD|2},
1556 {substringW, String_substring, PROPF_METHOD|2},
1557 {supW, String_sup, PROPF_METHOD},
1558 {toLocaleLowerCaseW, String_toLocaleLowerCase, PROPF_METHOD},
1559 {toLocaleUpperCaseW, String_toLocaleUpperCase, PROPF_METHOD},
1560 {toLowerCaseW, String_toLowerCase, PROPF_METHOD},
1561 {toStringW, String_toString, PROPF_METHOD},
1562 {toUpperCaseW, String_toUpperCase, PROPF_METHOD},
1563 {valueOfW, String_valueOf, PROPF_METHOD}
1566 static const builtin_info_t String_info = {
1567 JSCLASS_STRING,
1568 {NULL, String_value, 0},
1569 sizeof(String_props)/sizeof(*String_props),
1570 String_props,
1571 String_destructor,
1572 NULL
1575 /* ECMA-262 3rd Edition 15.5.3.2 */
1576 static HRESULT StringConstr_fromCharCode(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
1577 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1579 DWORD i, code;
1580 BSTR ret;
1581 HRESULT hres;
1583 ret = SysAllocStringLen(NULL, arg_cnt(dp));
1584 if(!ret)
1585 return E_OUTOFMEMORY;
1587 for(i=0; i<arg_cnt(dp); i++) {
1588 hres = to_uint32(ctx, get_arg(dp, i), ei, &code);
1589 if(FAILED(hres)) {
1590 SysFreeString(ret);
1591 return hres;
1594 ret[i] = code;
1597 if(retv) {
1598 V_VT(retv) = VT_BSTR;
1599 V_BSTR(retv) = ret;
1601 else SysFreeString(ret);
1603 return S_OK;
1606 static HRESULT StringConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1607 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1609 HRESULT hres;
1611 TRACE("\n");
1613 switch(flags) {
1614 case INVOKE_FUNC: {
1615 BSTR str;
1617 if(arg_cnt(dp)) {
1618 hres = to_string(ctx, get_arg(dp, 0), ei, &str);
1619 if(FAILED(hres))
1620 return hres;
1621 }else {
1622 str = SysAllocStringLen(NULL, 0);
1623 if(!str)
1624 return E_OUTOFMEMORY;
1627 V_VT(retv) = VT_BSTR;
1628 V_BSTR(retv) = str;
1629 break;
1631 case DISPATCH_CONSTRUCT: {
1632 DispatchEx *ret;
1634 if(arg_cnt(dp)) {
1635 BSTR str;
1637 hres = to_string(ctx, get_arg(dp, 0), ei, &str);
1638 if(FAILED(hres))
1639 return hres;
1641 hres = create_string(ctx, str, SysStringLen(str), &ret);
1642 SysFreeString(str);
1643 }else {
1644 hres = create_string(ctx, NULL, 0, &ret);
1647 if(FAILED(hres))
1648 return hres;
1650 V_VT(retv) = VT_DISPATCH;
1651 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
1652 break;
1655 default:
1656 FIXME("unimplemented flags: %x\n", flags);
1657 return E_NOTIMPL;
1660 return S_OK;
1663 static HRESULT string_alloc(script_ctx_t *ctx, DispatchEx *object_prototype, StringInstance **ret)
1665 StringInstance *string;
1666 HRESULT hres;
1668 string = heap_alloc_zero(sizeof(StringInstance));
1669 if(!string)
1670 return E_OUTOFMEMORY;
1672 if(object_prototype)
1673 hres = init_dispex(&string->dispex, ctx, &String_info, object_prototype);
1674 else
1675 hres = init_dispex_from_constr(&string->dispex, ctx, &String_info, ctx->string_constr);
1676 if(FAILED(hres)) {
1677 heap_free(string);
1678 return hres;
1681 *ret = string;
1682 return S_OK;
1685 static const builtin_prop_t StringConstr_props[] = {
1686 {fromCharCodeW, StringConstr_fromCharCode, PROPF_METHOD},
1689 static const builtin_info_t StringConstr_info = {
1690 JSCLASS_FUNCTION,
1691 {NULL, Function_value, 0},
1692 sizeof(StringConstr_props)/sizeof(*StringConstr_props),
1693 StringConstr_props,
1694 NULL,
1695 NULL
1698 HRESULT create_string_constr(script_ctx_t *ctx, DispatchEx *object_prototype, DispatchEx **ret)
1700 StringInstance *string;
1701 HRESULT hres;
1703 static const WCHAR StringW[] = {'S','t','r','i','n','g',0};
1705 hres = string_alloc(ctx, object_prototype, &string);
1706 if(FAILED(hres))
1707 return hres;
1709 hres = create_builtin_function(ctx, StringConstr_value, StringW, &StringConstr_info,
1710 PROPF_CONSTR|1, &string->dispex, ret);
1712 jsdisp_release(&string->dispex);
1713 return hres;
1716 HRESULT create_string(script_ctx_t *ctx, const WCHAR *str, DWORD len, DispatchEx **ret)
1718 StringInstance *string;
1719 HRESULT hres;
1721 hres = string_alloc(ctx, NULL, &string);
1722 if(FAILED(hres))
1723 return hres;
1725 if(len == -1)
1726 len = strlenW(str);
1728 string->length = len;
1729 string->str = heap_alloc((len+1)*sizeof(WCHAR));
1730 if(!string->str) {
1731 jsdisp_release(&string->dispex);
1732 return E_OUTOFMEMORY;
1735 memcpy(string->str, str, len*sizeof(WCHAR));
1736 string->str[len] = 0;
1738 *ret = &string->dispex;
1739 return S_OK;