- Sync inetcomm, inseng, jscript to Wine-1.1.43.
[reactos.git] / reactos / dll / win32 / jscript / string.c
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include "jscript.h"
20
21 #include "wine/debug.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
24
25 typedef struct {
26 DispatchEx dispex;
27
28 WCHAR *str;
29 DWORD length;
30 } StringInstance;
31
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};
66
67 static inline StringInstance *string_from_vdisp(vdisp_t *vdisp)
68 {
69 return (StringInstance*)vdisp->u.jsdisp;
70 }
71
72 static inline StringInstance *string_this(vdisp_t *jsthis)
73 {
74 return is_vclass(jsthis, JSCLASS_STRING) ? string_from_vdisp(jsthis) : NULL;
75 }
76
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)
79 {
80 StringInstance *string;
81 VARIANT this_var;
82 HRESULT hres;
83
84 if((string = string_this(jsthis))) {
85 *str = string->str;
86 *len = string->length;
87 *val_str = NULL;
88 return S_OK;
89 }
90
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;
96
97 *str = *val_str;
98 *len = SysStringLen(*val_str);
99 return S_OK;
100 }
101
102 static HRESULT String_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
103 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
104 {
105 TRACE("%p\n", jsthis);
106
107 switch(flags) {
108 case DISPATCH_PROPERTYGET: {
109 StringInstance *string = string_from_vdisp(jsthis);
110
111 V_VT(retv) = VT_I4;
112 V_I4(retv) = string->length;
113 break;
114 }
115 default:
116 FIXME("unimplemented flags %x\n", flags);
117 return E_NOTIMPL;
118 }
119
120 return S_OK;
121 }
122
123 static HRESULT stringobj_to_string(vdisp_t *jsthis, VARIANT *retv)
124 {
125 StringInstance *string;
126
127 if(!(string = string_this(jsthis))) {
128 WARN("this is not a string object\n");
129 return E_FAIL;
130 }
131
132 if(retv) {
133 BSTR str = SysAllocString(string->str);
134 if(!str)
135 return E_OUTOFMEMORY;
136
137 V_VT(retv) = VT_BSTR;
138 V_BSTR(retv) = str;
139 }
140 return S_OK;
141 }
142
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)
146 {
147 TRACE("\n");
148
149 return stringobj_to_string(jsthis, retv);
150 }
151
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)
155 {
156 TRACE("\n");
157
158 return stringobj_to_string(jsthis, retv);
159 }
160
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)
163 {
164 const WCHAR *str;
165 DWORD length;
166 BSTR val_str = NULL;
167 HRESULT hres;
168
169 static const WCHAR tagfmt[] = {'<','%','s','>','%','s','<','/','%','s','>',0};
170
171 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
172 if(FAILED(hres))
173 return hres;
174
175 if(retv) {
176 BSTR ret = SysAllocStringLen(NULL, length + 2*strlenW(tagname) + 5);
177 if(!ret) {
178 SysFreeString(val_str);
179 return E_OUTOFMEMORY;
180 }
181
182 sprintfW(ret, tagfmt, tagname, str, tagname);
183
184 V_VT(retv) = VT_BSTR;
185 V_BSTR(retv) = ret;
186 }
187
188 SysFreeString(val_str);
189 return S_OK;
190 }
191
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)
195 {
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};
199
200 StringInstance *string;
201 const WCHAR *str;
202 DWORD length;
203 BSTR attr_value, val_str = NULL;
204 HRESULT hres;
205
206 if(!(string = string_this(jsthis))) {
207 VARIANT this;
208
209 V_VT(&this) = VT_DISPATCH;
210 V_DISPATCH(&this) = jsthis->u.disp;
211
212 hres = to_string(ctx, &this, ei, &val_str);
213 if(FAILED(hres))
214 return hres;
215
216 str = val_str;
217 length = SysStringLen(val_str);
218 }
219 else {
220 str = string->str;
221 length = string->length;
222 }
223
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;
229 }
230 }
231 else {
232 attr_value = SysAllocString(undefinedW);
233 if(!attr_value) {
234 SysFreeString(val_str);
235 return E_OUTOFMEMORY;
236 }
237 }
238
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;
246 }
247
248 sprintfW(ret, tagfmtW, tagname, attr, attr_value, str, tagname);
249
250 V_VT(retv) = VT_BSTR;
251 V_BSTR(retv) = ret;
252 }
253
254 SysFreeString(attr_value);
255 SysFreeString(val_str);
256 return S_OK;
257 }
258
259 static HRESULT String_anchor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
260 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
261 {
262 static const WCHAR fontW[] = {'A',0};
263 static const WCHAR colorW[] = {'N','A','M','E',0};
264
265 return do_attribute_tag_format(ctx, jsthis, flags, dp, retv, ei, sp, fontW, colorW);
266 }
267
268 static HRESULT String_big(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
269 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
270 {
271 static const WCHAR bigtagW[] = {'B','I','G',0};
272 return do_attributeless_tag_format(ctx, jsthis, flags, dp, retv, ei, sp, bigtagW);
273 }
274
275 static HRESULT String_blink(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
276 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
277 {
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);
280 }
281
282 static HRESULT String_bold(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
283 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
284 {
285 static const WCHAR boldtagW[] = {'B',0};
286 return do_attributeless_tag_format(ctx, jsthis, flags, dp, retv, ei, sp, boldtagW);
287 }
288
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)
292 {
293 const WCHAR *str;
294 DWORD length;
295 BSTR ret, val_str;
296 INT pos = 0;
297 HRESULT hres;
298
299 TRACE("\n");
300
301 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
302 if(FAILED(hres))
303 return hres;
304
305 if(arg_cnt(dp)) {
306 VARIANT num;
307
308 hres = to_integer(ctx, get_arg(dp, 0), ei, &num);
309 if(FAILED(hres)) {
310 SysFreeString(val_str);
311 return hres;
312 }
313
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;
319 }
320 }
321
322 if(!retv) {
323 SysFreeString(val_str);
324 return S_OK;
325 }
326
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;
334 }
335
336 V_VT(retv) = VT_BSTR;
337 V_BSTR(retv) = ret;
338 return S_OK;
339 }
340
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)
344 {
345 const WCHAR *str;
346 BSTR val_str;
347 DWORD length, idx = 0;
348 HRESULT hres;
349
350 TRACE("\n");
351
352 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
353 if(FAILED(hres))
354 return hres;
355
356 if(arg_cnt(dp) > 0) {
357 VARIANT v;
358
359 hres = to_integer(ctx, get_arg(dp, 0), ei, &v);
360 if(FAILED(hres)) {
361 SysFreeString(val_str);
362 return hres;
363 }
364
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;
369 }
370
371 idx = V_I4(&v);
372 }
373
374 if(retv) {
375 V_VT(retv) = VT_I4;
376 V_I4(retv) = str[idx];
377 }
378
379 SysFreeString(val_str);
380 return S_OK;
381 }
382
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)
386 {
387 BSTR *strs = NULL, ret = NULL;
388 DWORD len = 0, i, l, str_cnt;
389 VARIANT var;
390 WCHAR *ptr;
391 HRESULT hres;
392
393 TRACE("\n");
394
395 str_cnt = arg_cnt(dp)+1;
396 strs = heap_alloc_zero(str_cnt * sizeof(BSTR));
397 if(!strs)
398 return E_OUTOFMEMORY;
399
400 V_VT(&var) = VT_DISPATCH;
401 V_DISPATCH(&var) = jsthis->u.disp;
402
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;
409 }
410 }
411
412 if(SUCCEEDED(hres)) {
413 for(i=0; i < str_cnt; i++)
414 len += SysStringLen(strs[i]);
415
416 ptr = ret = SysAllocStringLen(NULL, len);
417
418 for(i=0; i < str_cnt; i++) {
419 l = SysStringLen(strs[i]);
420 memcpy(ptr, strs[i], l*sizeof(WCHAR));
421 ptr += l;
422 }
423 }
424
425 for(i=0; i < str_cnt; i++)
426 SysFreeString(strs[i]);
427 heap_free(strs);
428
429 if(FAILED(hres))
430 return hres;
431
432 if(retv) {
433 V_VT(retv) = VT_BSTR;
434 V_BSTR(retv) = ret;
435 }else {
436 SysFreeString(ret);
437 }
438 return S_OK;
439 }
440
441 static HRESULT String_fixed(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
442 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
443 {
444 static const WCHAR fixedtagW[] = {'T','T',0};
445 return do_attributeless_tag_format(ctx, jsthis, flags, dp, retv, ei, sp, fixedtagW);
446 }
447
448 static HRESULT String_fontcolor(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
449 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
450 {
451 static const WCHAR fontW[] = {'F','O','N','T',0};
452 static const WCHAR colorW[] = {'C','O','L','O','R',0};
453
454 return do_attribute_tag_format(ctx, jsthis, flags, dp, retv, ei, sp, fontW, colorW);
455 }
456
457 static HRESULT String_fontsize(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
458 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
459 {
460 static const WCHAR fontW[] = {'F','O','N','T',0};
461 static const WCHAR colorW[] = {'S','I','Z','E',0};
462
463 return do_attribute_tag_format(ctx, jsthis, flags, dp, retv, ei, sp, fontW, colorW);
464 }
465
466 static HRESULT String_indexOf(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
467 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
468 {
469 DWORD length, pos = 0;
470 const WCHAR *str;
471 BSTR search_str, val_str;
472 INT ret = -1;
473 HRESULT hres;
474
475 TRACE("\n");
476
477 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
478 if(FAILED(hres))
479 return hres;
480
481 if(!arg_cnt(dp)) {
482 if(retv) {
483 V_VT(retv) = VT_I4;
484 V_I4(retv) = -1;
485 }
486 SysFreeString(val_str);
487 return S_OK;
488 }
489
490 hres = to_string(ctx, get_arg(dp,0), ei, &search_str);
491 if(FAILED(hres)) {
492 SysFreeString(val_str);
493 return hres;
494 }
495
496 if(arg_cnt(dp) >= 2) {
497 VARIANT ival;
498
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;
507 }
508 }
509
510 if(SUCCEEDED(hres)) {
511 const WCHAR *ptr;
512
513 ptr = strstrW(str+pos, search_str);
514 if(ptr)
515 ret = ptr - str;
516 else
517 ret = -1;
518 }
519
520 SysFreeString(search_str);
521 SysFreeString(val_str);
522 if(FAILED(hres))
523 return hres;
524
525 if(retv) {
526 V_VT(retv) = VT_I4;
527 V_I4(retv) = ret;
528 }
529 return S_OK;
530 }
531
532 static HRESULT String_italics(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
533 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
534 {
535 static const WCHAR italicstagW[] = {'I',0};
536 return do_attributeless_tag_format(ctx, jsthis, flags, dp, retv, ei, sp, italicstagW);
537 }
538
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)
542 {
543 BSTR search_str, val_str;
544 DWORD length, pos, search_len;
545 const WCHAR *str;
546 INT ret = -1;
547 HRESULT hres;
548
549 TRACE("\n");
550
551 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
552 if(FAILED(hres))
553 return hres;
554
555 if(!arg_cnt(dp)) {
556 if(retv) {
557 V_VT(retv) = VT_I4;
558 V_I4(retv) = -1;
559 }
560 SysFreeString(val_str);
561 return S_OK;
562 }
563
564 hres = to_string(ctx, get_arg(dp,0), ei, &search_str);
565 if(FAILED(hres)) {
566 SysFreeString(val_str);
567 return hres;
568 }
569
570 search_len = SysStringLen(search_str);
571
572 if(arg_cnt(dp) >= 2) {
573 VARIANT ival;
574
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;
583 }
584 }else {
585 pos = length;
586 }
587
588 if(SUCCEEDED(hres) && length >= search_len) {
589 const WCHAR *ptr;
590
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;
595 }
596 }
597 }
598
599 SysFreeString(search_str);
600 SysFreeString(val_str);
601 if(FAILED(hres))
602 return hres;
603
604 if(retv) {
605 V_VT(retv) = VT_I4;
606 V_I4(retv) = ret;
607 }
608 return S_OK;
609 }
610
611 static HRESULT String_link(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
612 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
613 {
614 static const WCHAR fontW[] = {'A',0};
615 static const WCHAR colorW[] = {'H','R','E','F',0};
616
617 return do_attribute_tag_format(ctx, jsthis, flags, dp, retv, ei, sp, fontW, colorW);
618 }
619
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)
623 {
624 const WCHAR *str;
625 DispatchEx *regexp;
626 VARIANT *arg_var;
627 DWORD length;
628 BSTR val_str = NULL;
629 HRESULT hres = S_OK;
630
631 TRACE("\n");
632
633 if(!arg_cnt(dp)) {
634 if(retv) {
635 V_VT(retv) = VT_NULL;
636 }
637
638 return S_OK;
639 }
640
641 arg_var = get_arg(dp, 0);
642 switch(V_VT(arg_var)) {
643 case VT_DISPATCH:
644 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
645 if(regexp) {
646 if(is_class(regexp, JSCLASS_REGEXP))
647 break;
648 jsdisp_release(regexp);
649 }
650 default: {
651 BSTR match_str;
652
653 hres = to_string(ctx, arg_var, ei, &match_str);
654 if(FAILED(hres))
655 return hres;
656
657 hres = create_regexp(ctx, match_str, SysStringLen(match_str), 0, &regexp);
658 SysFreeString(match_str);
659 if(FAILED(hres))
660 return hres;
661 }
662 }
663
664 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
665 if(SUCCEEDED(hres)) {
666 if(!val_str)
667 val_str = SysAllocStringLen(str, length);
668 if(val_str)
669 hres = regexp_string_match(ctx, regexp, val_str, retv, ei);
670 else
671 hres = E_OUTOFMEMORY;
672 }
673
674 jsdisp_release(regexp);
675 SysFreeString(val_str);
676 return hres;
677 }
678
679 typedef struct {
680 WCHAR *buf;
681 DWORD size;
682 DWORD len;
683 } strbuf_t;
684
685 static HRESULT strbuf_append(strbuf_t *buf, const WCHAR *str, DWORD len)
686 {
687 if(!len)
688 return S_OK;
689
690 if(len + buf->len > buf->size) {
691 WCHAR *new_buf;
692 DWORD new_size;
693
694 new_size = buf->size ? buf->size<<1 : 16;
695 if(new_size < buf->len+len)
696 new_size = buf->len+len;
697 if(buf->buf)
698 new_buf = heap_realloc(buf->buf, new_size*sizeof(WCHAR));
699 else
700 new_buf = heap_alloc(new_size*sizeof(WCHAR));
701 if(!new_buf)
702 return E_OUTOFMEMORY;
703
704 buf->buf = new_buf;
705 buf->size = new_size;
706 }
707
708 memcpy(buf->buf+buf->len, str, len*sizeof(WCHAR));
709 buf->len += len;
710 return S_OK;
711 }
712
713 static HRESULT rep_call(script_ctx_t *ctx, DispatchEx *func, const WCHAR *str, match_result_t *match,
714 match_result_t *parens, DWORD parens_cnt, BSTR *ret, jsexcept_t *ei, IServiceProvider *caller)
715 {
716 DISPPARAMS dp = {NULL, NULL, 0, 0};
717 VARIANTARG *args, *arg;
718 VARIANT var;
719 DWORD i;
720 HRESULT hres = S_OK;
721
722 dp.cArgs = parens_cnt+3;
723 dp.rgvarg = args = heap_alloc_zero(sizeof(VARIANT)*dp.cArgs);
724 if(!args)
725 return E_OUTOFMEMORY;
726
727 arg = get_arg(&dp,0);
728 V_VT(arg) = VT_BSTR;
729 V_BSTR(arg) = SysAllocStringLen(match->str, match->len);
730 if(!V_BSTR(arg))
731 hres = E_OUTOFMEMORY;
732
733 if(SUCCEEDED(hres)) {
734 for(i=0; i < parens_cnt; i++) {
735 arg = get_arg(&dp,i+1);
736 V_VT(arg) = VT_BSTR;
737 V_BSTR(arg) = SysAllocStringLen(parens[i].str, parens[i].len);
738 if(!V_BSTR(arg)) {
739 hres = E_OUTOFMEMORY;
740 break;
741 }
742 }
743 }
744
745 if(SUCCEEDED(hres)) {
746 arg = get_arg(&dp,parens_cnt+1);
747 V_VT(arg) = VT_I4;
748 V_I4(arg) = match->str - str;
749
750 arg = get_arg(&dp,parens_cnt+2);
751 V_VT(arg) = VT_BSTR;
752 V_BSTR(arg) = SysAllocString(str);
753 if(!V_BSTR(arg))
754 hres = E_OUTOFMEMORY;
755 }
756
757 if(SUCCEEDED(hres))
758 hres = jsdisp_call_value(func, DISPATCH_METHOD, &dp, &var, ei, caller);
759
760 for(i=0; i < parens_cnt+3; i++) {
761 if(i != parens_cnt+1)
762 SysFreeString(V_BSTR(get_arg(&dp,i)));
763 }
764 heap_free(args);
765
766 if(FAILED(hres))
767 return hres;
768
769 hres = to_string(ctx, &var, ei, ret);
770 VariantClear(&var);
771 return hres;
772 }
773
774 /* ECMA-262 3rd Edition 15.5.4.11 */
775 static HRESULT String_replace(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
776 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
777 {
778 const WCHAR *str;
779 DWORD parens_cnt = 0, parens_size=0, rep_len=0, length;
780 BSTR rep_str = NULL, match_str = NULL, ret_str, val_str;
781 DispatchEx *rep_func = NULL, *regexp = NULL;
782 match_result_t *parens = NULL, match, **parens_ptr = &parens;
783 strbuf_t ret = {NULL,0,0};
784 DWORD re_flags = 0;
785 VARIANT *arg_var;
786 HRESULT hres = S_OK;
787
788 TRACE("\n");
789
790 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
791 if(FAILED(hres))
792 return hres;
793
794 if(!arg_cnt(dp)) {
795 if(retv) {
796 if(!val_str) {
797 val_str = SysAllocStringLen(str, length);
798 if(!val_str)
799 return E_OUTOFMEMORY;
800 }
801
802 V_VT(retv) = VT_BSTR;
803 V_BSTR(retv) = val_str;
804 }
805 return S_OK;
806 }
807
808 arg_var = get_arg(dp, 0);
809 switch(V_VT(arg_var)) {
810 case VT_DISPATCH:
811 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
812 if(regexp) {
813 if(is_class(regexp, JSCLASS_REGEXP)) {
814 break;
815 }else {
816 jsdisp_release(regexp);
817 regexp = NULL;
818 }
819 }
820
821 default:
822 hres = to_string(ctx, arg_var, ei, &match_str);
823 if(FAILED(hres)) {
824 SysFreeString(val_str);
825 return hres;
826 }
827 }
828
829 if(arg_cnt(dp) >= 2) {
830 arg_var = get_arg(dp,1);
831 switch(V_VT(arg_var)) {
832 case VT_DISPATCH:
833 rep_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
834 if(rep_func) {
835 if(is_class(rep_func, JSCLASS_FUNCTION)) {
836 break;
837 }else {
838 jsdisp_release(rep_func);
839 rep_func = NULL;
840 }
841 }
842
843 default:
844 hres = to_string(ctx, arg_var, ei, &rep_str);
845 if(FAILED(hres))
846 break;
847
848 rep_len = SysStringLen(rep_str);
849 if(!strchrW(rep_str, '$'))
850 parens_ptr = NULL;
851 }
852 }
853
854 if(SUCCEEDED(hres)) {
855 const WCHAR *cp, *ecp;
856
857 cp = ecp = str;
858
859 while(1) {
860 if(regexp) {
861 hres = regexp_match_next(ctx, regexp, re_flags, str, length, &cp, parens_ptr,
862 &parens_size, &parens_cnt, &match);
863 re_flags = REM_CHECK_GLOBAL;
864
865 if(hres == S_FALSE) {
866 hres = S_OK;
867 break;
868 }
869 if(FAILED(hres))
870 break;
871 }else {
872 match.str = strstrW(cp, match_str);
873 if(!match.str)
874 break;
875 match.len = SysStringLen(match_str);
876 cp = match.str+match.len;
877 }
878
879 hres = strbuf_append(&ret, ecp, match.str-ecp);
880 ecp = match.str+match.len;
881 if(FAILED(hres))
882 break;
883
884 if(rep_func) {
885 BSTR cstr;
886
887 hres = rep_call(ctx, rep_func, str, &match, parens, parens_cnt, &cstr, ei, caller);
888 if(FAILED(hres))
889 break;
890
891 hres = strbuf_append(&ret, cstr, SysStringLen(cstr));
892 SysFreeString(cstr);
893 if(FAILED(hres))
894 break;
895 }else if(rep_str && regexp) {
896 const WCHAR *ptr = rep_str, *ptr2;
897
898 while((ptr2 = strchrW(ptr, '$'))) {
899 hres = strbuf_append(&ret, ptr, ptr2-ptr);
900 if(FAILED(hres))
901 break;
902
903 switch(ptr2[1]) {
904 case '$':
905 hres = strbuf_append(&ret, ptr2, 1);
906 ptr = ptr2+2;
907 break;
908 case '&':
909 hres = strbuf_append(&ret, match.str, match.len);
910 ptr = ptr2+2;
911 break;
912 case '`':
913 hres = strbuf_append(&ret, str, match.str-str);
914 ptr = ptr2+2;
915 break;
916 case '\'':
917 hres = strbuf_append(&ret, ecp, (str+length)-ecp);
918 ptr = ptr2+2;
919 break;
920 default: {
921 DWORD idx;
922
923 if(!isdigitW(ptr2[1])) {
924 hres = strbuf_append(&ret, ptr2, 1);
925 ptr = ptr2+1;
926 break;
927 }
928
929 idx = ptr2[1] - '0';
930 if(isdigitW(ptr2[2]) && idx*10 + (ptr2[2]-'0') <= parens_cnt) {
931 idx = idx*10 + (ptr[2]-'0');
932 ptr = ptr2+3;
933 }else if(idx && idx <= parens_cnt) {
934 ptr = ptr2+2;
935 }else {
936 hres = strbuf_append(&ret, ptr2, 1);
937 ptr = ptr2+1;
938 break;
939 }
940
941 hres = strbuf_append(&ret, parens[idx-1].str, parens[idx-1].len);
942 }
943 }
944
945 if(FAILED(hres))
946 break;
947 }
948
949 if(SUCCEEDED(hres))
950 hres = strbuf_append(&ret, ptr, (rep_str+rep_len)-ptr);
951 if(FAILED(hres))
952 break;
953 }else if(rep_str) {
954 hres = strbuf_append(&ret, rep_str, rep_len);
955 if(FAILED(hres))
956 break;
957 }else {
958 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d'};
959
960 hres = strbuf_append(&ret, undefinedW, sizeof(undefinedW)/sizeof(WCHAR));
961 if(FAILED(hres))
962 break;
963 }
964 }
965
966 if(SUCCEEDED(hres))
967 hres = strbuf_append(&ret, ecp, (str+length)-ecp);
968 }
969
970 if(rep_func)
971 jsdisp_release(rep_func);
972 if(regexp)
973 jsdisp_release(regexp);
974 SysFreeString(val_str);
975 SysFreeString(rep_str);
976 SysFreeString(match_str);
977 heap_free(parens);
978
979 if(SUCCEEDED(hres) && retv) {
980 ret_str = SysAllocStringLen(ret.buf, ret.len);
981 if(!ret_str)
982 return E_OUTOFMEMORY;
983
984 V_VT(retv) = VT_BSTR;
985 V_BSTR(retv) = ret_str;
986 TRACE("= %s\n", debugstr_w(ret_str));
987 }
988
989 heap_free(ret.buf);
990 return hres;
991 }
992
993 static HRESULT String_search(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
994 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
995 {
996 DispatchEx *regexp = NULL;
997 const WCHAR *str, *cp;
998 match_result_t match;
999 VARIANT *arg;
1000 DWORD length;
1001 BSTR val_str;
1002 HRESULT hres;
1003
1004 TRACE("\n");
1005
1006 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
1007 if(FAILED(hres))
1008 return hres;
1009
1010 if(!arg_cnt(dp)) {
1011 if(retv)
1012 V_VT(retv) = VT_NULL;
1013 SysFreeString(val_str);
1014 return S_OK;
1015 }
1016
1017 arg = get_arg(dp,0);
1018 if(V_VT(arg) == VT_DISPATCH) {
1019 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
1020 if(regexp) {
1021 if(!is_class(regexp, JSCLASS_REGEXP)) {
1022 jsdisp_release(regexp);
1023 regexp = NULL;
1024 }
1025 }
1026 }
1027
1028 if(!regexp) {
1029 hres = create_regexp_var(ctx, arg, NULL, &regexp);
1030 if(FAILED(hres)) {
1031 SysFreeString(val_str);
1032 return hres;
1033 }
1034 }
1035
1036 cp = str;
1037 hres = regexp_match_next(ctx, regexp, REM_RESET_INDEX, str, length, &cp, NULL, NULL, NULL, &match);
1038 SysFreeString(val_str);
1039 jsdisp_release(regexp);
1040 if(FAILED(hres))
1041 return hres;
1042
1043 if(retv) {
1044 V_VT(retv) = VT_I4;
1045 V_I4(retv) = hres == S_OK ? match.str-str : -1;
1046 }
1047 return S_OK;
1048 }
1049
1050 /* ECMA-262 3rd Edition 15.5.4.13 */
1051 static HRESULT String_slice(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1052 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1053 {
1054 const WCHAR *str;
1055 BSTR val_str;
1056 DWORD length;
1057 INT start=0, end;
1058 VARIANT v;
1059 HRESULT hres;
1060
1061 TRACE("\n");
1062
1063 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
1064 if(FAILED(hres))
1065 return hres;
1066
1067 if(arg_cnt(dp)) {
1068 hres = to_integer(ctx, get_arg(dp,0), ei, &v);
1069 if(FAILED(hres)) {
1070 SysFreeString(val_str);
1071 return hres;
1072 }
1073
1074 if(V_VT(&v) == VT_I4) {
1075 start = V_I4(&v);
1076 if(start < 0) {
1077 start = length + start;
1078 if(start < 0)
1079 start = 0;
1080 }else if(start > length) {
1081 start = length;
1082 }
1083 }else {
1084 start = V_R8(&v) < 0.0 ? 0 : length;
1085 }
1086 }else {
1087 start = 0;
1088 }
1089
1090 if(arg_cnt(dp) >= 2) {
1091 hres = to_integer(ctx, get_arg(dp,1), ei, &v);
1092 if(FAILED(hres)) {
1093 SysFreeString(val_str);
1094 return hres;
1095 }
1096
1097 if(V_VT(&v) == VT_I4) {
1098 end = V_I4(&v);
1099 if(end < 0) {
1100 end = length + end;
1101 if(end < 0)
1102 end = 0;
1103 }else if(end > length) {
1104 end = length;
1105 }
1106 }else {
1107 end = V_R8(&v) < 0.0 ? 0 : length;
1108 }
1109 }else {
1110 end = length;
1111 }
1112
1113 if(end < start)
1114 end = start;
1115
1116 if(retv) {
1117 BSTR retstr = SysAllocStringLen(str+start, end-start);
1118 if(!retstr) {
1119 SysFreeString(val_str);
1120 return E_OUTOFMEMORY;
1121 }
1122
1123 V_VT(retv) = VT_BSTR;
1124 V_BSTR(retv) = retstr;
1125 }
1126
1127 SysFreeString(val_str);
1128 return S_OK;
1129 }
1130
1131 static HRESULT String_small(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1132 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1133 {
1134 static const WCHAR smalltagW[] = {'S','M','A','L','L',0};
1135 return do_attributeless_tag_format(ctx, jsthis, flags, dp, retv, ei, sp, smalltagW);
1136 }
1137
1138 static HRESULT String_split(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1139 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1140 {
1141 match_result_t *match_result = NULL;
1142 DWORD length, match_cnt, i, match_len = 0;
1143 const WCHAR *str, *ptr, *ptr2;
1144 BOOL use_regexp = FALSE;
1145 VARIANT *arg, var;
1146 DispatchEx *array;
1147 BSTR val_str, match_str = NULL;
1148 HRESULT hres;
1149
1150 TRACE("\n");
1151
1152 if(arg_cnt(dp) != 1) {
1153 FIXME("unsupported args\n");
1154 return E_NOTIMPL;
1155 }
1156
1157 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
1158 if(FAILED(hres))
1159 return hres;
1160
1161 arg = get_arg(dp, 0);
1162 switch(V_VT(arg)) {
1163 case VT_DISPATCH: {
1164 DispatchEx *regexp;
1165
1166 regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
1167 if(regexp) {
1168 if(is_class(regexp, JSCLASS_REGEXP)) {
1169 use_regexp = TRUE;
1170 hres = regexp_match(ctx, regexp, str, length, TRUE, &match_result, &match_cnt);
1171 jsdisp_release(regexp);
1172 if(FAILED(hres)) {
1173 SysFreeString(val_str);
1174 return hres;
1175 }
1176 break;
1177 }
1178 jsdisp_release(regexp);
1179 }
1180 }
1181 default:
1182 hres = to_string(ctx, arg, ei, &match_str);
1183 if(FAILED(hres)) {
1184 SysFreeString(val_str);
1185 return hres;
1186 }
1187
1188 match_len = SysStringLen(match_str);
1189 if(!match_len) {
1190 SysFreeString(match_str);
1191 match_str = NULL;
1192 }
1193 }
1194
1195 hres = create_array(ctx, 0, &array);
1196
1197 if(SUCCEEDED(hres)) {
1198 ptr = str;
1199 for(i=0;; i++) {
1200 if(use_regexp) {
1201 if(i == match_cnt)
1202 break;
1203 ptr2 = match_result[i].str;
1204 }else if(match_str) {
1205 ptr2 = strstrW(ptr, match_str);
1206 if(!ptr2)
1207 break;
1208 }else {
1209 if(!*ptr)
1210 break;
1211 ptr2 = ptr+1;
1212 }
1213
1214 V_VT(&var) = VT_BSTR;
1215 V_BSTR(&var) = SysAllocStringLen(ptr, ptr2-ptr);
1216 if(!V_BSTR(&var)) {
1217 hres = E_OUTOFMEMORY;
1218 break;
1219 }
1220
1221 hres = jsdisp_propput_idx(array, i, &var, ei, sp);
1222 SysFreeString(V_BSTR(&var));
1223 if(FAILED(hres))
1224 break;
1225
1226 if(use_regexp)
1227 ptr = match_result[i].str + match_result[i].len;
1228 else if(match_str)
1229 ptr = ptr2 + match_len;
1230 else
1231 ptr++;
1232 }
1233 }
1234
1235 if(SUCCEEDED(hres) && (match_str || use_regexp)) {
1236 DWORD len = (str+length) - ptr;
1237
1238 if(len || match_str) {
1239 V_VT(&var) = VT_BSTR;
1240 V_BSTR(&var) = SysAllocStringLen(ptr, len);
1241
1242 if(V_BSTR(&var)) {
1243 hres = jsdisp_propput_idx(array, i, &var, ei, sp);
1244 SysFreeString(V_BSTR(&var));
1245 }else {
1246 hres = E_OUTOFMEMORY;
1247 }
1248 }
1249 }
1250
1251 SysFreeString(match_str);
1252 SysFreeString(val_str);
1253 heap_free(match_result);
1254
1255 if(SUCCEEDED(hres) && retv) {
1256 V_VT(retv) = VT_DISPATCH;
1257 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(array);
1258 }else {
1259 jsdisp_release(array);
1260 }
1261
1262 return hres;
1263 }
1264
1265 static HRESULT String_strike(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1266 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1267 {
1268 static const WCHAR striketagW[] = {'S','T','R','I','K','E',0};
1269 return do_attributeless_tag_format(ctx, jsthis, flags, dp, retv, ei, sp, striketagW);
1270 }
1271
1272 static HRESULT String_sub(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1273 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1274 {
1275 static const WCHAR subtagW[] = {'S','U','B',0};
1276 return do_attributeless_tag_format(ctx, jsthis, flags, dp, retv, ei, sp, subtagW);
1277 }
1278
1279 /* ECMA-262 3rd Edition 15.5.4.15 */
1280 static HRESULT String_substring(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1281 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1282 {
1283 const WCHAR *str;
1284 BSTR val_str;
1285 INT start=0, end;
1286 DWORD length;
1287 VARIANT v;
1288 HRESULT hres;
1289
1290 TRACE("\n");
1291
1292 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
1293 if(FAILED(hres))
1294 return hres;
1295
1296 if(arg_cnt(dp) >= 1) {
1297 hres = to_integer(ctx, get_arg(dp,0), ei, &v);
1298 if(FAILED(hres)) {
1299 SysFreeString(val_str);
1300 return hres;
1301 }
1302
1303 if(V_VT(&v) == VT_I4) {
1304 start = V_I4(&v);
1305 if(start < 0)
1306 start = 0;
1307 else if(start >= length)
1308 start = length;
1309 }else {
1310 start = V_R8(&v) < 0.0 ? 0 : length;
1311 }
1312 }
1313
1314 if(arg_cnt(dp) >= 2) {
1315 hres = to_integer(ctx, get_arg(dp,1), ei, &v);
1316 if(FAILED(hres)) {
1317 SysFreeString(val_str);
1318 return hres;
1319 }
1320
1321 if(V_VT(&v) == VT_I4) {
1322 end = V_I4(&v);
1323 if(end < 0)
1324 end = 0;
1325 else if(end > length)
1326 end = length;
1327 }else {
1328 end = V_R8(&v) < 0.0 ? 0 : length;
1329 }
1330 }else {
1331 end = length;
1332 }
1333
1334 if(start > end) {
1335 INT tmp = start;
1336 start = end;
1337 end = tmp;
1338 }
1339
1340 if(retv) {
1341 V_VT(retv) = VT_BSTR;
1342 V_BSTR(retv) = SysAllocStringLen(str+start, end-start);
1343 if(!V_BSTR(retv)) {
1344 SysFreeString(val_str);
1345 return E_OUTOFMEMORY;
1346 }
1347 }
1348 SysFreeString(val_str);
1349 return S_OK;
1350 }
1351
1352 /* ECMA-262 3rd Edition B.2.3 */
1353 static HRESULT String_substr(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1354 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1355 {
1356 BSTR val_str;
1357 const WCHAR *str;
1358 INT start=0, len;
1359 DWORD length;
1360 VARIANT v;
1361 HRESULT hres;
1362
1363 TRACE("\n");
1364
1365 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
1366 if(FAILED(hres))
1367 return hres;
1368
1369 if(arg_cnt(dp) >= 1) {
1370 hres = to_integer(ctx, get_arg(dp,0), ei, &v);
1371 if(FAILED(hres)) {
1372 SysFreeString(val_str);
1373 return hres;
1374 }
1375
1376 if(V_VT(&v) == VT_I4) {
1377 start = V_I4(&v);
1378 if(start < 0)
1379 start = 0;
1380 else if(start >= length)
1381 start = length;
1382 }else {
1383 start = V_R8(&v) < 0.0 ? 0 : length;
1384 }
1385 }
1386
1387 if(arg_cnt(dp) >= 2) {
1388 hres = to_integer(ctx, get_arg(dp,1), ei, &v);
1389 if(FAILED(hres)) {
1390 SysFreeString(val_str);
1391 return hres;
1392 }
1393
1394 if(V_VT(&v) == VT_I4) {
1395 len = V_I4(&v);
1396 if(len < 0)
1397 len = 0;
1398 else if(len > length-start)
1399 len = length-start;
1400 }else {
1401 len = V_R8(&v) < 0.0 ? 0 : length-start;
1402 }
1403 }else {
1404 len = length-start;
1405 }
1406
1407 hres = S_OK;
1408 if(retv) {
1409 V_VT(retv) = VT_BSTR;
1410 V_BSTR(retv) = SysAllocStringLen(str+start, len);
1411 if(!V_BSTR(retv))
1412 hres = E_OUTOFMEMORY;
1413 }
1414
1415 SysFreeString(val_str);
1416 return hres;
1417 }
1418
1419 static HRESULT String_sup(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1420 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1421 {
1422 static const WCHAR suptagW[] = {'S','U','P',0};
1423 return do_attributeless_tag_format(ctx, jsthis, flags, dp, retv, ei, sp, suptagW);
1424 }
1425
1426 static HRESULT String_toLowerCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1427 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1428 {
1429 const WCHAR* str;
1430 DWORD length;
1431 BSTR val_str;
1432 HRESULT hres;
1433
1434 TRACE("\n");
1435
1436 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
1437 if(FAILED(hres))
1438 return hres;
1439
1440 if(retv) {
1441 if(!val_str) {
1442 val_str = SysAllocStringLen(str, length);
1443 if(!val_str)
1444 return E_OUTOFMEMORY;
1445 }
1446
1447 strlwrW(val_str);
1448
1449 V_VT(retv) = VT_BSTR;
1450 V_BSTR(retv) = val_str;
1451 }
1452 else SysFreeString(val_str);
1453 return S_OK;
1454 }
1455
1456 static HRESULT String_toUpperCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1457 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1458 {
1459 const WCHAR* str;
1460 DWORD length;
1461 BSTR val_str;
1462 HRESULT hres;
1463
1464 TRACE("\n");
1465
1466 hres = get_string_val(ctx, jsthis, ei, &str, &length, &val_str);
1467 if(FAILED(hres))
1468 return hres;
1469
1470 if(retv) {
1471 if(!val_str) {
1472 val_str = SysAllocStringLen(str, length);
1473 if(!val_str)
1474 return E_OUTOFMEMORY;
1475 }
1476
1477 struprW(val_str);
1478
1479 V_VT(retv) = VT_BSTR;
1480 V_BSTR(retv) = val_str;
1481 }
1482 else SysFreeString(val_str);
1483 return S_OK;
1484 }
1485
1486 static HRESULT String_toLocaleLowerCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1487 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1488 {
1489 FIXME("\n");
1490 return E_NOTIMPL;
1491 }
1492
1493 static HRESULT String_toLocaleUpperCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1494 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1495 {
1496 FIXME("\n");
1497 return E_NOTIMPL;
1498 }
1499
1500 static HRESULT String_localeCompare(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1501 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1502 {
1503 FIXME("\n");
1504 return E_NOTIMPL;
1505 }
1506
1507 static HRESULT String_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1508 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1509 {
1510 StringInstance *This = string_from_vdisp(jsthis);
1511
1512 TRACE("\n");
1513
1514 switch(flags) {
1515 case INVOKE_FUNC:
1516 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
1517 case DISPATCH_PROPERTYGET: {
1518 BSTR str = SysAllocString(This->str);
1519 if(!str)
1520 return E_OUTOFMEMORY;
1521
1522 V_VT(retv) = VT_BSTR;
1523 V_BSTR(retv) = str;
1524 break;
1525 }
1526 default:
1527 FIXME("flags %x\n", flags);
1528 return E_NOTIMPL;
1529 }
1530
1531 return S_OK;
1532 }
1533
1534 static void String_destructor(DispatchEx *dispex)
1535 {
1536 StringInstance *This = (StringInstance*)dispex;
1537
1538 heap_free(This->str);
1539 heap_free(This);
1540 }
1541
1542 static const builtin_prop_t String_props[] = {
1543 {anchorW, String_anchor, PROPF_METHOD|1},
1544 {bigW, String_big, PROPF_METHOD},
1545 {blinkW, String_blink, PROPF_METHOD},
1546 {boldW, String_bold, PROPF_METHOD},
1547 {charAtW, String_charAt, PROPF_METHOD|1},
1548 {charCodeAtW, String_charCodeAt, PROPF_METHOD|1},
1549 {concatW, String_concat, PROPF_METHOD|1},
1550 {fixedW, String_fixed, PROPF_METHOD},
1551 {fontcolorW, String_fontcolor, PROPF_METHOD|1},
1552 {fontsizeW, String_fontsize, PROPF_METHOD|1},
1553 {indexOfW, String_indexOf, PROPF_METHOD|2},
1554 {italicsW, String_italics, PROPF_METHOD},
1555 {lastIndexOfW, String_lastIndexOf, PROPF_METHOD|2},
1556 {lengthW, String_length, 0},
1557 {linkW, String_link, PROPF_METHOD|1},
1558 {localeCompareW, String_localeCompare, PROPF_METHOD|1},
1559 {matchW, String_match, PROPF_METHOD|1},
1560 {replaceW, String_replace, PROPF_METHOD|1},
1561 {searchW, String_search, PROPF_METHOD},
1562 {sliceW, String_slice, PROPF_METHOD},
1563 {smallW, String_small, PROPF_METHOD},
1564 {splitW, String_split, PROPF_METHOD|2},
1565 {strikeW, String_strike, PROPF_METHOD},
1566 {subW, String_sub, PROPF_METHOD},
1567 {substrW, String_substr, PROPF_METHOD|2},
1568 {substringW, String_substring, PROPF_METHOD|2},
1569 {supW, String_sup, PROPF_METHOD},
1570 {toLocaleLowerCaseW, String_toLocaleLowerCase, PROPF_METHOD},
1571 {toLocaleUpperCaseW, String_toLocaleUpperCase, PROPF_METHOD},
1572 {toLowerCaseW, String_toLowerCase, PROPF_METHOD},
1573 {toStringW, String_toString, PROPF_METHOD},
1574 {toUpperCaseW, String_toUpperCase, PROPF_METHOD},
1575 {valueOfW, String_valueOf, PROPF_METHOD}
1576 };
1577
1578 static const builtin_info_t String_info = {
1579 JSCLASS_STRING,
1580 {NULL, String_value, 0},
1581 sizeof(String_props)/sizeof(*String_props),
1582 String_props,
1583 String_destructor,
1584 NULL
1585 };
1586
1587 /* ECMA-262 3rd Edition 15.5.3.2 */
1588 static HRESULT StringConstr_fromCharCode(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
1589 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1590 {
1591 DWORD i, code;
1592 BSTR ret;
1593 HRESULT hres;
1594
1595 ret = SysAllocStringLen(NULL, arg_cnt(dp));
1596 if(!ret)
1597 return E_OUTOFMEMORY;
1598
1599 for(i=0; i<arg_cnt(dp); i++) {
1600 hres = to_uint32(ctx, get_arg(dp, i), ei, &code);
1601 if(FAILED(hres)) {
1602 SysFreeString(ret);
1603 return hres;
1604 }
1605
1606 ret[i] = code;
1607 }
1608
1609 if(retv) {
1610 V_VT(retv) = VT_BSTR;
1611 V_BSTR(retv) = ret;
1612 }
1613 else SysFreeString(ret);
1614
1615 return S_OK;
1616 }
1617
1618 static HRESULT StringConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1619 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1620 {
1621 HRESULT hres;
1622
1623 TRACE("\n");
1624
1625 switch(flags) {
1626 case INVOKE_FUNC: {
1627 BSTR str;
1628
1629 if(arg_cnt(dp)) {
1630 hres = to_string(ctx, get_arg(dp, 0), ei, &str);
1631 if(FAILED(hres))
1632 return hres;
1633 }else {
1634 str = SysAllocStringLen(NULL, 0);
1635 if(!str)
1636 return E_OUTOFMEMORY;
1637 }
1638
1639 V_VT(retv) = VT_BSTR;
1640 V_BSTR(retv) = str;
1641 break;
1642 }
1643 case DISPATCH_CONSTRUCT: {
1644 DispatchEx *ret;
1645
1646 if(arg_cnt(dp)) {
1647 BSTR str;
1648
1649 hres = to_string(ctx, get_arg(dp, 0), ei, &str);
1650 if(FAILED(hres))
1651 return hres;
1652
1653 hres = create_string(ctx, str, SysStringLen(str), &ret);
1654 SysFreeString(str);
1655 }else {
1656 hres = create_string(ctx, NULL, 0, &ret);
1657 }
1658
1659 if(FAILED(hres))
1660 return hres;
1661
1662 V_VT(retv) = VT_DISPATCH;
1663 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
1664 break;
1665 }
1666
1667 default:
1668 FIXME("unimplemented flags: %x\n", flags);
1669 return E_NOTIMPL;
1670 }
1671
1672 return S_OK;
1673 }
1674
1675 static HRESULT string_alloc(script_ctx_t *ctx, DispatchEx *object_prototype, StringInstance **ret)
1676 {
1677 StringInstance *string;
1678 HRESULT hres;
1679
1680 string = heap_alloc_zero(sizeof(StringInstance));
1681 if(!string)
1682 return E_OUTOFMEMORY;
1683
1684 if(object_prototype)
1685 hres = init_dispex(&string->dispex, ctx, &String_info, object_prototype);
1686 else
1687 hres = init_dispex_from_constr(&string->dispex, ctx, &String_info, ctx->string_constr);
1688 if(FAILED(hres)) {
1689 heap_free(string);
1690 return hres;
1691 }
1692
1693 *ret = string;
1694 return S_OK;
1695 }
1696
1697 static const builtin_prop_t StringConstr_props[] = {
1698 {fromCharCodeW, StringConstr_fromCharCode, PROPF_METHOD},
1699 };
1700
1701 static const builtin_info_t StringConstr_info = {
1702 JSCLASS_FUNCTION,
1703 {NULL, Function_value, 0},
1704 sizeof(StringConstr_props)/sizeof(*StringConstr_props),
1705 StringConstr_props,
1706 NULL,
1707 NULL
1708 };
1709
1710 HRESULT create_string_constr(script_ctx_t *ctx, DispatchEx *object_prototype, DispatchEx **ret)
1711 {
1712 StringInstance *string;
1713 HRESULT hres;
1714
1715 static const WCHAR StringW[] = {'S','t','r','i','n','g',0};
1716
1717 hres = string_alloc(ctx, object_prototype, &string);
1718 if(FAILED(hres))
1719 return hres;
1720
1721 hres = create_builtin_function(ctx, StringConstr_value, StringW, &StringConstr_info,
1722 PROPF_CONSTR|1, &string->dispex, ret);
1723
1724 jsdisp_release(&string->dispex);
1725 return hres;
1726 }
1727
1728 HRESULT create_string(script_ctx_t *ctx, const WCHAR *str, DWORD len, DispatchEx **ret)
1729 {
1730 StringInstance *string;
1731 HRESULT hres;
1732
1733 hres = string_alloc(ctx, NULL, &string);
1734 if(FAILED(hres))
1735 return hres;
1736
1737 if(len == -1)
1738 len = strlenW(str);
1739
1740 string->length = len;
1741 string->str = heap_alloc((len+1)*sizeof(WCHAR));
1742 if(!string->str) {
1743 jsdisp_release(&string->dispex);
1744 return E_OUTOFMEMORY;
1745 }
1746
1747 memcpy(string->str, str, len*sizeof(WCHAR));
1748 string->str[len] = 0;
1749
1750 *ret = &string->dispex;
1751 return S_OK;
1752
1753 }