jscript: Added String_anchor implementation.
[wine] / dlls / 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 hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
66 static const WCHAR propertyIsEnumerableW[] =
67     {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
68 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
69
70 static HRESULT String_length(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
71         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
72 {
73     TRACE("%p\n", dispex);
74
75     switch(flags) {
76     case DISPATCH_PROPERTYGET: {
77         StringInstance *jsthis = (StringInstance*)dispex;
78
79         V_VT(retv) = VT_I4;
80         V_I4(retv) = jsthis->length;
81         break;
82     }
83     default:
84         FIXME("unimplemented flags %x\n", flags);
85         return E_NOTIMPL;
86     }
87
88     return S_OK;
89 }
90
91 /* ECMA-262 3rd Edition    15.5.4.2 */
92 static HRESULT String_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
93         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
94 {
95     StringInstance *string;
96
97     TRACE("\n");
98
99     if(!is_class(dispex, JSCLASS_STRING)) {
100         WARN("this is not a string object\n");
101         return E_FAIL;
102     }
103
104     string = (StringInstance*)dispex;
105
106     if(retv) {
107         BSTR str = SysAllocString(string->str);
108         if(!str)
109             return E_OUTOFMEMORY;
110
111         V_VT(retv) = VT_BSTR;
112         V_BSTR(retv) = str;
113     }
114     return S_OK;
115 }
116
117 /* ECMA-262 3rd Edition    15.5.4.2 */
118 static HRESULT String_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
119         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
120 {
121     TRACE("\n");
122
123     return String_toString(dispex, lcid, flags, dp, retv, ei, sp);
124 }
125
126 static HRESULT do_attributeless_tag_format(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
127         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp, const WCHAR *tagname)
128 {
129     static const WCHAR tagfmt[] = {'<','%','s','>','%','s','<','/','%','s','>',0};
130     StringInstance *string;
131     BSTR ret;
132
133     if(!is_class(dispex, JSCLASS_STRING)) {
134         WARN("this is not a string object\n");
135         return E_NOTIMPL;
136     }
137
138     string = (StringInstance*)dispex;
139
140     if(retv) {
141         ret = SysAllocStringLen(NULL, string->length + 2*strlenW(tagname) + 5);
142         if(!ret)
143             return E_OUTOFMEMORY;
144
145         sprintfW(ret, tagfmt, tagname, string->str, tagname);
146
147         V_VT(retv) = VT_BSTR;
148         V_BSTR(retv) = ret;
149     }
150     return S_OK;
151 }
152
153 static HRESULT do_attribute_tag_format(DispatchEx *dispex, LCID lcid, WORD flags,
154         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp,
155         const WCHAR *tagname, const WCHAR *attr)
156 {
157     static const WCHAR tagfmtW[]
158         = {'<','%','s',' ','%','s','=','\"','%','s','\"','>','%','s','<','/','%','s','>',0};
159     static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
160
161     StringInstance *string;
162     BSTR ret, attr_value;
163     HRESULT hres;
164
165     if(!is_class(dispex, JSCLASS_STRING)) {
166         WARN("this is not a string object\n");
167         return E_NOTIMPL;
168     }
169
170     string = (StringInstance*) dispex;
171
172     if(arg_cnt(dp)) {
173         hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &attr_value);
174         if(FAILED(hres))
175             return hres;
176     }
177     else {
178         attr_value = SysAllocString(undefinedW);
179         if(!attr_value)
180             return E_OUTOFMEMORY;
181     }
182
183     if(retv) {
184         ret = SysAllocStringLen(NULL, string->length + 2*strlenW(tagname)
185                 + strlenW(attr) + SysStringLen(attr_value) + 9);
186         if(!ret) {
187             SysFreeString(attr_value);
188             return E_OUTOFMEMORY;
189         }
190
191         sprintfW(ret, tagfmtW, tagname, attr, attr_value, string->str, tagname);
192
193         V_VT(retv) = VT_BSTR;
194         V_BSTR(retv) = ret;
195     }
196
197     SysFreeString(attr_value);
198     return S_OK;
199 }
200
201 static HRESULT String_anchor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
202         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
203 {
204     static const WCHAR fontW[] = {'A',0};
205     static const WCHAR colorW[] = {'N','A','M','E',0};
206
207     return do_attribute_tag_format(dispex, lcid, flags, dp, retv, ei, sp, fontW, colorW);
208 }
209
210 static HRESULT String_big(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
211         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
212 {
213     static const WCHAR bigtagW[] = {'B','I','G',0};
214     return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, bigtagW);
215 }
216
217 static HRESULT String_blink(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
218         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
219 {
220     static const WCHAR blinktagW[] = {'B','L','I','N','K',0};
221     return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, blinktagW);
222 }
223
224 static HRESULT String_bold(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
225         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
226 {
227     static const WCHAR boldtagW[] = {'B',0};
228     return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, boldtagW);
229 }
230
231 /* ECMA-262 3rd Edition    15.5.4.5 */
232 static HRESULT String_charAt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
233         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
234 {
235     StringInstance *strobj;
236     BSTR str;
237     INT pos = 0;
238     HRESULT hres;
239
240     TRACE("\n");
241
242     if(dispex->builtin_info->class != JSCLASS_STRING) {
243         FIXME("not string this not supported\n");
244         return E_NOTIMPL;
245     }
246
247     strobj = (StringInstance*)dispex;
248
249     if(arg_cnt(dp)) {
250         VARIANT num;
251
252         hres = to_integer(dispex->ctx, get_arg(dp, 0), ei, &num);
253         if(FAILED(hres))
254             return hres;
255
256         if(V_VT(&num) == VT_I4) {
257             pos = V_I4(&num);
258         }else {
259             WARN("pos = %lf\n", V_R8(&num));
260             pos = -1;
261         }
262     }
263
264     if(!retv)
265         return S_OK;
266
267     if(0 <= pos && pos < strobj->length)
268         str = SysAllocStringLen(strobj->str+pos, 1);
269     else
270         str = SysAllocStringLen(NULL, 0);
271     if(!str)
272         return E_OUTOFMEMORY;
273
274     V_VT(retv) = VT_BSTR;
275     V_BSTR(retv) = str;
276     return S_OK;
277 }
278
279 /* ECMA-262 3rd Edition    15.5.4.5 */
280 static HRESULT String_charCodeAt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
281         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
282 {
283     const WCHAR *str;
284     DWORD length, idx = 0;
285     HRESULT hres;
286
287     TRACE("\n");
288
289     if(dispex->builtin_info->class == JSCLASS_STRING) {
290         StringInstance *string = (StringInstance*)dispex;
291
292         str = string->str;
293         length = string->length;
294     }else {
295         FIXME("not string this not supported\n");
296         return E_NOTIMPL;
297     }
298
299     if(arg_cnt(dp) > 0) {
300         VARIANT v;
301
302         hres = to_integer(dispex->ctx, get_arg(dp, 0), ei, &v);
303         if(FAILED(hres))
304             return hres;
305
306         if(V_VT(&v) != VT_I4 || V_I4(&v) < 0 || V_I4(&v) >= length) {
307             if(retv) num_set_nan(&v);
308             return S_OK;
309         }
310
311         idx = V_I4(&v);
312     }
313
314     if(retv) {
315         V_VT(retv) = VT_I4;
316         V_I4(retv) = str[idx];
317     }
318     return S_OK;
319 }
320
321 /* ECMA-262 3rd Edition    15.5.4.6 */
322 static HRESULT String_concat(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
323         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
324 {
325     BSTR *strs = NULL, ret = NULL;
326     DWORD len = 0, i, l, str_cnt;
327     VARIANT var;
328     WCHAR *ptr;
329     HRESULT hres;
330
331     TRACE("\n");
332
333     str_cnt = arg_cnt(dp)+1;
334     strs = heap_alloc_zero(str_cnt * sizeof(BSTR));
335     if(!strs)
336         return E_OUTOFMEMORY;
337
338     V_VT(&var) = VT_DISPATCH;
339     V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(dispex);
340
341     hres = to_string(dispex->ctx, &var, ei, strs);
342     if(SUCCEEDED(hres)) {
343         for(i=0; i < arg_cnt(dp); i++) {
344             hres = to_string(dispex->ctx, get_arg(dp, i), ei, strs+i+1);
345             if(FAILED(hres))
346                 break;
347         }
348     }
349
350     if(SUCCEEDED(hres)) {
351         for(i=0; i < str_cnt; i++)
352             len += SysStringLen(strs[i]);
353
354         ptr = ret = SysAllocStringLen(NULL, len);
355
356         for(i=0; i < str_cnt; i++) {
357             l = SysStringLen(strs[i]);
358             memcpy(ptr, strs[i], l*sizeof(WCHAR));
359             ptr += l;
360         }
361     }
362
363     for(i=0; i < str_cnt; i++)
364         SysFreeString(strs[i]);
365     heap_free(strs);
366
367     if(FAILED(hres))
368         return hres;
369
370     if(retv) {
371         V_VT(retv) = VT_BSTR;
372         V_BSTR(retv) = ret;
373     }else {
374         SysFreeString(ret);
375     }
376     return S_OK;
377 }
378
379 static HRESULT String_fixed(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
380         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
381 {
382     static const WCHAR fixedtagW[] = {'T','T',0};
383     return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, fixedtagW);
384 }
385
386 static HRESULT String_fontcolor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
387         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
388 {
389     static const WCHAR fontW[] = {'F','O','N','T',0};
390     static const WCHAR colorW[] = {'C','O','L','O','R',0};
391
392     return do_attribute_tag_format(dispex, lcid, flags, dp, retv, ei, sp, fontW, colorW);
393 }
394
395 static HRESULT String_fontsize(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
396         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
397 {
398     static const WCHAR fontW[] = {'F','O','N','T',0};
399     static const WCHAR colorW[] = {'S','I','Z','E',0};
400
401     return do_attribute_tag_format(dispex, lcid, flags, dp, retv, ei, sp, fontW, colorW);
402 }
403
404 static HRESULT String_indexOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
405         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
406 {
407     DWORD length, pos = 0;
408     const WCHAR *str;
409     BSTR search_str;
410     INT ret = -1;
411     HRESULT hres;
412
413     TRACE("\n");
414
415     if(is_class(dispex, JSCLASS_STRING)) {
416         StringInstance *string = (StringInstance*)dispex;
417
418         str = string->str;
419         length = string->length;
420     }else {
421         FIXME("not String this\n");
422         return E_NOTIMPL;
423     }
424
425     if(!arg_cnt(dp)) {
426         if(retv) {
427             V_VT(retv) = VT_I4;
428             V_I4(retv) = -1;
429         }
430         return S_OK;
431     }
432
433     hres = to_string(dispex->ctx, get_arg(dp,0), ei, &search_str);
434     if(FAILED(hres))
435         return hres;
436
437     if(arg_cnt(dp) >= 2) {
438         VARIANT ival;
439
440         hres = to_integer(dispex->ctx, get_arg(dp,1), ei, &ival);
441         if(SUCCEEDED(hres)) {
442             if(V_VT(&ival) == VT_I4)
443                 pos = V_VT(&ival) > 0 ? V_I4(&ival) : 0;
444             else
445                 pos = V_R8(&ival) > 0.0 ? length : 0;
446             if(pos > length)
447                 pos = length;
448         }
449     }
450
451     if(SUCCEEDED(hres)) {
452         const WCHAR *ptr;
453
454         ptr = strstrW(str+pos, search_str);
455         if(ptr)
456             ret = ptr - str;
457         else
458             ret = -1;
459     }
460
461     SysFreeString(search_str);
462     if(FAILED(hres))
463         return hres;
464
465     if(retv) {
466         V_VT(retv) = VT_I4;
467         V_I4(retv) = ret;
468     }
469     return S_OK;
470 }
471
472 static HRESULT String_italics(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
473         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
474 {
475     static const WCHAR italicstagW[] = {'I',0};
476     return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, italicstagW);
477 }
478
479 static HRESULT String_lastIndexOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
480         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
481 {
482     FIXME("\n");
483     return E_NOTIMPL;
484 }
485
486 static HRESULT String_link(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
487         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
488 {
489     FIXME("\n");
490     return E_NOTIMPL;
491 }
492
493 /* ECMA-262 3rd Edition    15.5.4.10 */
494 static HRESULT String_match(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
495         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
496 {
497     StringInstance *This = (StringInstance*)dispex;
498     match_result_t *match_result;
499     DispatchEx *regexp;
500     DispatchEx *array;
501     VARIANT var, *arg_var;
502     DWORD match_cnt, i;
503     HRESULT hres = S_OK;
504
505     TRACE("\n");
506
507     if(arg_cnt(dp) != 1) {
508         FIXME("unsupported args\n");
509         return E_NOTIMPL;
510     }
511
512     arg_var = get_arg(dp, 0);
513     switch(V_VT(arg_var)) {
514     case VT_DISPATCH:
515         regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
516         if(regexp) {
517             if(regexp->builtin_info->class == JSCLASS_REGEXP)
518                 break;
519             jsdisp_release(regexp);
520         }
521     default: {
522         BSTR match_str;
523
524         hres = to_string(dispex->ctx, arg_var, ei, &match_str);
525         if(FAILED(hres))
526             return hres;
527
528         hres = create_regexp_str(dispex->ctx, match_str, SysStringLen(match_str), NULL, 0, &regexp);
529         SysFreeString(match_str);
530         if(FAILED(hres))
531             return hres;
532     }
533     }
534
535     hres = regexp_match(regexp, This->str, This->length, FALSE, &match_result, &match_cnt);
536     jsdisp_release(regexp);
537     if(FAILED(hres))
538         return hres;
539
540     if(!match_cnt) {
541         TRACE("no match\n");
542
543         if(retv)
544             V_VT(retv) = VT_NULL;
545         return S_OK;
546     }
547
548     hres = create_array(dispex->ctx, match_cnt, &array);
549     if(FAILED(hres))
550         return hres;
551
552     V_VT(&var) = VT_BSTR;
553
554     for(i=0; i < match_cnt; i++) {
555         V_BSTR(&var) = SysAllocStringLen(match_result[i].str, match_result[i].len);
556         if(!V_BSTR(&var)) {
557             hres = E_OUTOFMEMORY;
558             break;
559         }
560
561         hres = jsdisp_propput_idx(array, i, lcid, &var, ei, NULL/*FIXME*/);
562         SysFreeString(V_BSTR(&var));
563         if(FAILED(hres))
564             break;
565     }
566
567     if(SUCCEEDED(hres) && retv) {
568         V_VT(retv) = VT_DISPATCH;
569         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(array);
570     }else {
571         jsdisp_release(array);
572     }
573     return hres;
574 }
575
576 typedef struct {
577     WCHAR *buf;
578     DWORD size;
579     DWORD len;
580 } strbuf_t;
581
582 static HRESULT strbuf_append(strbuf_t *buf, const WCHAR *str, DWORD len)
583 {
584     if(!len)
585         return S_OK;
586
587     if(len + buf->len > buf->size) {
588         WCHAR *new_buf;
589         DWORD new_size;
590
591         new_size = buf->size ? buf->size<<1 : 16;
592         if(new_size < buf->len+len)
593             new_size = buf->len+len;
594         if(buf->buf)
595             new_buf = heap_realloc(buf->buf, new_size*sizeof(WCHAR));
596         else
597             new_buf = heap_alloc(new_size*sizeof(WCHAR));
598         if(!new_buf)
599             return E_OUTOFMEMORY;
600
601         buf->buf = new_buf;
602         buf->size = new_size;
603     }
604
605     memcpy(buf->buf+buf->len, str, len*sizeof(WCHAR));
606     buf->len += len;
607     return S_OK;
608 }
609
610 static HRESULT rep_call(DispatchEx *func, const WCHAR *str, match_result_t *match, match_result_t *parens,
611         DWORD parens_cnt, LCID lcid, BSTR *ret, jsexcept_t *ei, IServiceProvider *caller)
612 {
613     DISPPARAMS dp = {NULL, NULL, 0, 0};
614     VARIANTARG *args, *arg;
615     VARIANT var;
616     DWORD i;
617     HRESULT hres = S_OK;
618
619     dp.cArgs = parens_cnt+3;
620     dp.rgvarg = args = heap_alloc_zero(sizeof(VARIANT)*dp.cArgs);
621     if(!args)
622         return E_OUTOFMEMORY;
623
624     arg = get_arg(&dp,0);
625     V_VT(arg) = VT_BSTR;
626     V_BSTR(arg) = SysAllocStringLen(match->str, match->len);
627     if(!V_BSTR(arg))
628         hres = E_OUTOFMEMORY;
629
630     if(SUCCEEDED(hres)) {
631         for(i=0; i < parens_cnt; i++) {
632             arg = get_arg(&dp,i+1);
633             V_VT(arg) = VT_BSTR;
634             V_BSTR(arg) = SysAllocStringLen(parens[i].str, parens[i].len);
635             if(!V_BSTR(arg)) {
636                hres = E_OUTOFMEMORY;
637                break;
638             }
639         }
640     }
641
642     if(SUCCEEDED(hres)) {
643         arg = get_arg(&dp,parens_cnt+1);
644         V_VT(arg) = VT_I4;
645         V_I4(arg) = match->str - str;
646
647         arg = get_arg(&dp,parens_cnt+2);
648         V_VT(arg) = VT_BSTR;
649         V_BSTR(arg) = SysAllocString(str);
650         if(!V_BSTR(arg))
651             hres = E_OUTOFMEMORY;
652     }
653
654     if(SUCCEEDED(hres))
655         hres = jsdisp_call_value(func, lcid, DISPATCH_METHOD, &dp, &var, ei, caller);
656
657     for(i=0; i < parens_cnt+1; i++) {
658         if(i != parens_cnt+1)
659             SysFreeString(V_BSTR(get_arg(&dp,i)));
660     }
661     heap_free(args);
662
663     if(FAILED(hres))
664         return hres;
665
666     hres = to_string(func->ctx, &var, ei, ret);
667     VariantClear(&var);
668     return hres;
669 }
670
671 /* ECMA-262 3rd Edition    15.5.4.11 */
672 static HRESULT String_replace(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
673         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
674 {
675     DWORD parens_cnt = 0, parens_size=0, rep_len=0, length;
676     BSTR rep_str = NULL, match_str = NULL, ret_str;
677     DispatchEx *rep_func = NULL, *regexp = NULL;
678     match_result_t *parens = NULL, match;
679     const WCHAR *str;
680     strbuf_t ret = {NULL,0,0};
681     BOOL gcheck = FALSE;
682     VARIANT *arg_var;
683     HRESULT hres = S_OK;
684
685     TRACE("\n");
686
687     if(is_class(dispex, JSCLASS_STRING)) {
688         StringInstance *string = (StringInstance*)dispex;
689         str = string->str;
690         length = string->length;
691     }else {
692         FIXME("not String this\n");
693         return E_NOTIMPL;
694     }
695
696     if(!arg_cnt(dp)) {
697         if(retv) {
698             ret_str = SysAllocString(str);
699             if(!ret_str)
700                 return E_OUTOFMEMORY;
701
702             V_VT(retv) = VT_BSTR;
703             V_BSTR(retv) = ret_str;
704         }
705         return S_OK;
706     }
707
708     arg_var = get_arg(dp, 0);
709     switch(V_VT(arg_var)) {
710     case VT_DISPATCH:
711         regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
712         if(regexp) {
713             if(is_class(regexp, JSCLASS_REGEXP)) {
714                 break;
715             }else {
716                 jsdisp_release(regexp);
717                 regexp = NULL;
718             }
719         }
720
721     default:
722         hres = to_string(dispex->ctx, arg_var, ei, &match_str);
723         if(FAILED(hres))
724             return hres;
725     }
726
727     if(arg_cnt(dp) >= 2) {
728         arg_var = get_arg(dp,1);
729         switch(V_VT(arg_var)) {
730         case VT_DISPATCH:
731             rep_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
732             if(rep_func) {
733                 if(is_class(rep_func, JSCLASS_FUNCTION)) {
734                     break;
735                 }else {
736                     jsdisp_release(rep_func);
737                     rep_func = NULL;
738                 }
739             }
740
741         default:
742             hres = to_string(dispex->ctx, arg_var, ei, &rep_str);
743             if(FAILED(hres))
744                 break;
745
746             if(strchrW(rep_str, '$')) {
747                 FIXME("unsupported $ in replace string\n");
748                 hres = E_NOTIMPL;
749             }
750
751             rep_len = SysStringLen(rep_str);
752         }
753     }
754
755     if(SUCCEEDED(hres)) {
756         const WCHAR *cp, *ecp;
757
758         cp = ecp = str;
759
760         while(1) {
761             if(regexp) {
762                 hres = regexp_match_next(regexp, gcheck, str, length, &cp, rep_func ? &parens : NULL,
763                                          &parens_size, &parens_cnt, &match);
764                 gcheck = TRUE;
765
766                 if(hres == S_FALSE) {
767                     hres = S_OK;
768                     break;
769                 }
770                 if(FAILED(hres))
771                     break;
772             }else {
773                 match.str = strstrW(cp, match_str);
774                 if(!match.str)
775                     break;
776                 match.len = SysStringLen(match_str);
777                 cp = match.str+match.len;
778             }
779
780             hres = strbuf_append(&ret, ecp, match.str-ecp);
781             ecp = match.str+match.len;
782             if(FAILED(hres))
783                 break;
784
785             if(rep_func) {
786                 BSTR cstr;
787
788                 hres = rep_call(rep_func, str, &match, parens, parens_cnt, lcid, &cstr, ei, caller);
789                 if(FAILED(hres))
790                     break;
791
792                 hres = strbuf_append(&ret, cstr, SysStringLen(cstr));
793                 SysFreeString(cstr);
794                 if(FAILED(hres))
795                     break;
796             }else if(rep_str) {
797                 hres = strbuf_append(&ret, rep_str, rep_len);
798                 if(FAILED(hres))
799                     break;
800             }else {
801                 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d'};
802
803                 hres = strbuf_append(&ret, undefinedW, sizeof(undefinedW)/sizeof(WCHAR));
804                 if(FAILED(hres))
805                     break;
806             }
807         }
808
809         if(SUCCEEDED(hres))
810             hres = strbuf_append(&ret, ecp, (str+length)-ecp);
811     }
812
813     if(rep_func)
814         jsdisp_release(rep_func);
815     if(regexp)
816         jsdisp_release(regexp);
817     SysFreeString(rep_str);
818     SysFreeString(match_str);
819     heap_free(parens);
820
821     if(SUCCEEDED(hres) && retv) {
822         ret_str = SysAllocStringLen(ret.buf, ret.len);
823         if(!ret_str)
824             return E_OUTOFMEMORY;
825
826         V_VT(retv) = VT_BSTR;
827         V_BSTR(retv) = ret_str;
828         TRACE("= %s\n", debugstr_w(ret_str));
829     }
830
831     heap_free(ret.buf);
832     return hres;
833 }
834
835 static HRESULT String_search(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
836         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
837 {
838     FIXME("\n");
839     return E_NOTIMPL;
840 }
841
842 /* ECMA-262 3rd Edition    15.5.4.13 */
843 static HRESULT String_slice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
844         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
845 {
846     const WCHAR *str;
847     DWORD length;
848     INT start=0, end;
849     VARIANT v;
850     HRESULT hres;
851
852     TRACE("\n");
853
854     if(is_class(dispex, JSCLASS_STRING)) {
855         StringInstance *string = (StringInstance*)dispex;
856
857         str = string->str;
858         length = string->length;
859     }else {
860         FIXME("this is not a string class\n");
861         return E_NOTIMPL;
862     }
863
864     if(arg_cnt(dp)) {
865         hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &v);
866         if(FAILED(hres))
867             return hres;
868
869         if(V_VT(&v) == VT_I4) {
870             start = V_I4(&v);
871             if(start < 0) {
872                 start = length + start;
873                 if(start < 0)
874                     start = 0;
875             }else if(start > length) {
876                 start = length;
877             }
878         }else {
879             start = V_R8(&v) < 0.0 ? 0 : length;
880         }
881     }else {
882         start = 0;
883     }
884
885     if(arg_cnt(dp) >= 2) {
886         hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-2, ei, &v);
887         if(FAILED(hres))
888             return hres;
889
890         if(V_VT(&v) == VT_I4) {
891             end = V_I4(&v);
892             if(end < 0) {
893                 end = length + end;
894                 if(end < 0)
895                     end = 0;
896             }else if(end > length) {
897                 end = length;
898             }
899         }else {
900             end = V_R8(&v) < 0.0 ? 0 : length;
901         }
902     }else {
903         end = length;
904     }
905
906     if(end < start)
907         end = start;
908
909     if(retv) {
910         BSTR retstr = SysAllocStringLen(str+start, end-start);
911         if(!str)
912             return E_OUTOFMEMORY;
913
914         V_VT(retv) = VT_BSTR;
915         V_BSTR(retv) = retstr;
916     }
917     return S_OK;
918 }
919
920 static HRESULT String_small(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
921         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
922 {
923     static const WCHAR smalltagW[] = {'S','M','A','L','L',0};
924     return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, smalltagW);
925 }
926
927 static HRESULT String_split(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
928         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
929 {
930     match_result_t *match_result = NULL;
931     DWORD match_cnt, i, match_len = 0;
932     StringInstance *string;
933     const WCHAR *ptr, *ptr2;
934     VARIANT *arg, var;
935     DispatchEx *array;
936     BSTR match_str = NULL;
937     HRESULT hres;
938
939     TRACE("\n");
940
941     if(!is_class(dispex, JSCLASS_STRING)) {
942         FIXME("not String this\n");
943         return E_NOTIMPL;
944     }
945
946     string = (StringInstance*)dispex;
947
948     if(arg_cnt(dp) != 1) {
949         FIXME("unsupported args\n");
950         return E_NOTIMPL;
951     }
952
953     arg = get_arg(dp, 0);
954     switch(V_VT(arg)) {
955     case VT_DISPATCH: {
956         DispatchEx *regexp;
957
958         regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
959         if(regexp) {
960             if(is_class(regexp, JSCLASS_REGEXP)) {
961                 hres = regexp_match(regexp, string->str, string->length, TRUE, &match_result, &match_cnt);
962                 jsdisp_release(regexp);
963                 if(FAILED(hres))
964                     return hres;
965                 break;
966             }
967             jsdisp_release(regexp);
968         }
969     }
970     default:
971         hres = to_string(dispex->ctx, arg, ei, &match_str);
972         if(FAILED(hres))
973             return hres;
974
975         match_len = SysStringLen(match_str);
976         if(!match_len) {
977             SysFreeString(match_str);
978             match_str = NULL;
979         }
980     }
981
982     hres = create_array(dispex->ctx, 0, &array);
983
984     if(SUCCEEDED(hres)) {
985         ptr = string->str;
986         for(i=0;; i++) {
987             if(match_result) {
988                 if(i == match_cnt)
989                     break;
990                 ptr2 = match_result[i].str;
991             }else if(match_str) {
992                 ptr2 = strstrW(ptr, match_str);
993                 if(!ptr2)
994                     break;
995             }else {
996                 if(!*ptr)
997                     break;
998                 ptr2 = ptr+1;
999             }
1000
1001             V_VT(&var) = VT_BSTR;
1002             V_BSTR(&var) = SysAllocStringLen(ptr, ptr2-ptr);
1003             if(!V_BSTR(&var)) {
1004                 hres = E_OUTOFMEMORY;
1005                 break;
1006             }
1007
1008             hres = jsdisp_propput_idx(array, i, lcid, &var, ei, sp);
1009             SysFreeString(V_BSTR(&var));
1010             if(FAILED(hres))
1011                 break;
1012
1013             if(match_result)
1014                 ptr = match_result[i].str + match_result[i].len;
1015             else if(match_str)
1016                 ptr = ptr2 + match_len;
1017             else
1018                 ptr++;
1019         }
1020     }
1021
1022     if(SUCCEEDED(hres) && (match_str || match_result)) {
1023         DWORD len = (string->str+string->length) - ptr;
1024
1025         if(len || match_str) {
1026             V_VT(&var) = VT_BSTR;
1027             V_BSTR(&var) = SysAllocStringLen(ptr, len);
1028
1029             if(V_BSTR(&var)) {
1030                 hres = jsdisp_propput_idx(array, i, lcid, &var, ei, sp);
1031                 SysFreeString(V_BSTR(&var));
1032             }else {
1033                 hres = E_OUTOFMEMORY;
1034             }
1035         }
1036     }
1037
1038     SysFreeString(match_str);
1039     heap_free(match_result);
1040
1041     if(SUCCEEDED(hres) && retv) {
1042         V_VT(retv) = VT_DISPATCH;
1043         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(array);
1044     }else {
1045         jsdisp_release(array);
1046     }
1047
1048     return hres;
1049 }
1050
1051 static HRESULT String_strike(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1052         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1053 {
1054     static const WCHAR striketagW[] = {'S','T','R','I','K','E',0};
1055     return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, striketagW);
1056 }
1057
1058 static HRESULT String_sub(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1059         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1060 {
1061     static const WCHAR subtagW[] = {'S','U','B',0};
1062     return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, subtagW);
1063 }
1064
1065 /* ECMA-262 3rd Edition    15.5.4.15 */
1066 static HRESULT String_substring(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1067         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1068 {
1069     const WCHAR *str;
1070     INT start=0, end;
1071     DWORD length;
1072     VARIANT v;
1073     HRESULT hres;
1074
1075     TRACE("\n");
1076
1077     if(is_class(dispex, JSCLASS_STRING)) {
1078         StringInstance *string = (StringInstance*)dispex;
1079
1080         length = string->length;
1081         str = string->str;
1082     }else {
1083         FIXME("not string this not supported\n");
1084         return E_NOTIMPL;
1085     }
1086
1087     if(arg_cnt(dp) >= 1) {
1088         hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &v);
1089         if(FAILED(hres))
1090             return hres;
1091
1092         if(V_VT(&v) == VT_I4) {
1093             start = V_I4(&v);
1094             if(start < 0)
1095                 start = 0;
1096             else if(start >= length)
1097                 start = length;
1098         }else {
1099             start = V_R8(&v) < 0.0 ? 0 : length;
1100         }
1101     }
1102
1103     if(arg_cnt(dp) >= 2) {
1104         hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-2, ei, &v);
1105         if(FAILED(hres))
1106             return hres;
1107
1108         if(V_VT(&v) == VT_I4) {
1109             end = V_I4(&v);
1110             if(end < 0)
1111                 end = 0;
1112             else if(end > length)
1113                 end = length;
1114         }else {
1115             end = V_R8(&v) < 0.0 ? 0 : length;
1116         }
1117     }else {
1118         end = length;
1119     }
1120
1121     if(start > end) {
1122         INT tmp = start;
1123         start = end;
1124         end = tmp;
1125     }
1126
1127     if(retv) {
1128         V_VT(retv) = VT_BSTR;
1129         V_BSTR(retv) = SysAllocStringLen(str+start, end-start);
1130         if(!V_BSTR(retv))
1131             return E_OUTOFMEMORY;
1132     }
1133     return S_OK;
1134 }
1135
1136 static HRESULT String_substr(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1137         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1138 {
1139     FIXME("\n");
1140     return E_NOTIMPL;
1141 }
1142
1143 static HRESULT String_sup(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1144         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1145 {
1146     static const WCHAR suptagW[] = {'S','U','P',0};
1147     return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, suptagW);
1148 }
1149
1150 static HRESULT String_toLowerCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1151         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1152 {
1153     StringInstance *string;
1154     const WCHAR* str;
1155     DWORD length;
1156     BSTR bstr;
1157
1158     TRACE("\n");
1159
1160     if(is_class(dispex, JSCLASS_STRING)) {
1161         string = (StringInstance*)dispex;
1162
1163         length = string->length;
1164         str = string->str;
1165     }else {
1166         FIXME("not string this not supported\n");
1167         return E_NOTIMPL;
1168     }
1169
1170     if(retv) {
1171         bstr = SysAllocStringLen(str, length);
1172         if (!bstr)
1173             return E_OUTOFMEMORY;
1174
1175         strlwrW(bstr);
1176
1177         V_VT(retv) = VT_BSTR;
1178         V_BSTR(retv) = bstr;
1179     }
1180     return S_OK;
1181 }
1182
1183 static HRESULT String_toUpperCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1184         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1185 {
1186     StringInstance *string;
1187     const WCHAR* str;
1188     DWORD length;
1189     BSTR bstr;
1190
1191     TRACE("\n");
1192
1193     if(is_class(dispex, JSCLASS_STRING)) {
1194         string = (StringInstance*)dispex;
1195
1196         length = string->length;
1197         str = string->str;
1198     }else {
1199         FIXME("not string this not supported\n");
1200         return E_NOTIMPL;
1201     }
1202
1203     if(retv) {
1204         bstr = SysAllocStringLen(str, length);
1205         if (!bstr)
1206             return E_OUTOFMEMORY;
1207
1208         struprW(bstr);
1209
1210         V_VT(retv) = VT_BSTR;
1211         V_BSTR(retv) = bstr;
1212     }
1213     return S_OK;
1214 }
1215
1216 static HRESULT String_toLocaleLowerCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1217         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1218 {
1219     FIXME("\n");
1220     return E_NOTIMPL;
1221 }
1222
1223 static HRESULT String_toLocaleUpperCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1224         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1225 {
1226     FIXME("\n");
1227     return E_NOTIMPL;
1228 }
1229
1230 static HRESULT String_localeCompare(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1231         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1232 {
1233     FIXME("\n");
1234     return E_NOTIMPL;
1235 }
1236
1237 static HRESULT String_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1238         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1239 {
1240     FIXME("\n");
1241     return E_NOTIMPL;
1242 }
1243
1244 static HRESULT String_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1245         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1246 {
1247     FIXME("\n");
1248     return E_NOTIMPL;
1249 }
1250
1251 static HRESULT String_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1252         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1253 {
1254     FIXME("\n");
1255     return E_NOTIMPL;
1256 }
1257
1258 static HRESULT String_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1259         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1260 {
1261     StringInstance *This = (StringInstance*)dispex;
1262
1263     TRACE("\n");
1264
1265     switch(flags) {
1266     case DISPATCH_PROPERTYGET: {
1267         BSTR str = SysAllocString(This->str);
1268         if(!str)
1269             return E_OUTOFMEMORY;
1270
1271         V_VT(retv) = VT_BSTR;
1272         V_BSTR(retv) = str;
1273         break;
1274     }
1275     default:
1276         FIXME("flags %x\n", flags);
1277         return E_NOTIMPL;
1278     }
1279
1280     return S_OK;
1281 }
1282
1283 static void String_destructor(DispatchEx *dispex)
1284 {
1285     StringInstance *This = (StringInstance*)dispex;
1286
1287     heap_free(This->str);
1288     heap_free(This);
1289 }
1290
1291 static const builtin_prop_t String_props[] = {
1292     {anchorW,                String_anchor,                PROPF_METHOD},
1293     {bigW,                   String_big,                   PROPF_METHOD},
1294     {blinkW,                 String_blink,                 PROPF_METHOD},
1295     {boldW,                  String_bold,                  PROPF_METHOD},
1296     {charAtW,                String_charAt,                PROPF_METHOD},
1297     {charCodeAtW,            String_charCodeAt,            PROPF_METHOD},
1298     {concatW,                String_concat,                PROPF_METHOD},
1299     {fixedW,                 String_fixed,                 PROPF_METHOD},
1300     {fontcolorW,             String_fontcolor,             PROPF_METHOD},
1301     {fontsizeW,              String_fontsize,              PROPF_METHOD},
1302     {hasOwnPropertyW,        String_hasOwnProperty,        PROPF_METHOD},
1303     {indexOfW,               String_indexOf,               PROPF_METHOD},
1304     {isPrototypeOfW,         String_isPrototypeOf,         PROPF_METHOD},
1305     {italicsW,               String_italics,               PROPF_METHOD},
1306     {lastIndexOfW,           String_lastIndexOf,           PROPF_METHOD},
1307     {lengthW,                String_length,                0},
1308     {linkW,                  String_link,                  PROPF_METHOD},
1309     {localeCompareW,         String_localeCompare,         PROPF_METHOD},
1310     {matchW,                 String_match,                 PROPF_METHOD},
1311     {propertyIsEnumerableW,  String_propertyIsEnumerable,  PROPF_METHOD},
1312     {replaceW,               String_replace,               PROPF_METHOD},
1313     {searchW,                String_search,                PROPF_METHOD},
1314     {sliceW,                 String_slice,                 PROPF_METHOD},
1315     {smallW,                 String_small,                 PROPF_METHOD},
1316     {splitW,                 String_split,                 PROPF_METHOD},
1317     {strikeW,                String_strike,                PROPF_METHOD},
1318     {subW,                   String_sub,                   PROPF_METHOD},
1319     {substrW,                String_substr,                PROPF_METHOD},
1320     {substringW,             String_substring,             PROPF_METHOD},
1321     {supW,                   String_sup,                   PROPF_METHOD},
1322     {toLocaleLowerCaseW,     String_toLocaleLowerCase,     PROPF_METHOD},
1323     {toLocaleUpperCaseW,     String_toLocaleUpperCase,     PROPF_METHOD},
1324     {toLowerCaseW,           String_toLowerCase,           PROPF_METHOD},
1325     {toStringW,              String_toString,              PROPF_METHOD},
1326     {toUpperCaseW,           String_toUpperCase,           PROPF_METHOD},
1327     {valueOfW,               String_valueOf,               PROPF_METHOD}
1328 };
1329
1330 static const builtin_info_t String_info = {
1331     JSCLASS_STRING,
1332     {NULL, String_value, 0},
1333     sizeof(String_props)/sizeof(*String_props),
1334     String_props,
1335     String_destructor,
1336     NULL
1337 };
1338
1339 static HRESULT StringConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1340         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1341 {
1342     HRESULT hres;
1343
1344     TRACE("\n");
1345
1346     switch(flags) {
1347     case INVOKE_FUNC: {
1348         BSTR str;
1349
1350         if(arg_cnt(dp)) {
1351             hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
1352             if(FAILED(hres))
1353                 return hres;
1354         }else {
1355             str = SysAllocStringLen(NULL, 0);
1356             if(!str)
1357                 return E_OUTOFMEMORY;
1358         }
1359
1360         V_VT(retv) = VT_BSTR;
1361         V_BSTR(retv) = str;
1362         break;
1363     }
1364     case DISPATCH_CONSTRUCT: {
1365         DispatchEx *ret;
1366
1367         if(arg_cnt(dp)) {
1368             BSTR str;
1369
1370             hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
1371             if(FAILED(hres))
1372                 return hres;
1373
1374             hres = create_string(dispex->ctx, str, SysStringLen(str), &ret);
1375             SysFreeString(str);
1376         }else {
1377             hres = create_string(dispex->ctx, NULL, 0, &ret);
1378         }
1379
1380         if(FAILED(hres))
1381             return hres;
1382
1383         V_VT(retv) = VT_DISPATCH;
1384         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
1385         break;
1386     }
1387
1388     default:
1389         FIXME("unimplemented flags: %x\n", flags);
1390         return E_NOTIMPL;
1391     }
1392
1393     return S_OK;
1394 }
1395
1396 static HRESULT string_alloc(script_ctx_t *ctx, BOOL use_constr, StringInstance **ret)
1397 {
1398     StringInstance *string;
1399     HRESULT hres;
1400
1401     string = heap_alloc_zero(sizeof(StringInstance));
1402     if(!string)
1403         return E_OUTOFMEMORY;
1404
1405     if(use_constr)
1406         hres = init_dispex_from_constr(&string->dispex, ctx, &String_info, ctx->string_constr);
1407     else
1408         hres = init_dispex(&string->dispex, ctx, &String_info, NULL);
1409     if(FAILED(hres)) {
1410         heap_free(string);
1411         return hres;
1412     }
1413
1414     *ret = string;
1415     return S_OK;
1416 }
1417
1418 HRESULT create_string_constr(script_ctx_t *ctx, DispatchEx **ret)
1419 {
1420     StringInstance *string;
1421     HRESULT hres;
1422
1423     hres = string_alloc(ctx, FALSE, &string);
1424     if(FAILED(hres))
1425         return hres;
1426
1427     hres = create_builtin_function(ctx, StringConstr_value, NULL, PROPF_CONSTR, &string->dispex, ret);
1428
1429     jsdisp_release(&string->dispex);
1430     return hres;
1431 }
1432
1433 HRESULT create_string(script_ctx_t *ctx, const WCHAR *str, DWORD len, DispatchEx **ret)
1434 {
1435     StringInstance *string;
1436     HRESULT hres;
1437
1438     hres = string_alloc(ctx, TRUE, &string);
1439     if(FAILED(hres))
1440         return hres;
1441
1442     if(len == -1)
1443         len = strlenW(str);
1444
1445     string->length = len;
1446     string->str = heap_alloc((len+1)*sizeof(WCHAR));
1447     if(!string->str) {
1448         jsdisp_release(&string->dispex);
1449         return E_OUTOFMEMORY;
1450     }
1451
1452     memcpy(string->str, str, len*sizeof(WCHAR));
1453     string->str[len] = 0;
1454
1455     *ret = &string->dispex;
1456     return S_OK;
1457
1458 }