jscript: Added String_link 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     static const WCHAR fontW[] = {'A',0};
490     static const WCHAR colorW[] = {'H','R','E','F',0};
491
492     return do_attribute_tag_format(dispex, lcid, flags, dp, retv, ei, sp, fontW, colorW);
493 }
494
495 /* ECMA-262 3rd Edition    15.5.4.10 */
496 static HRESULT String_match(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
497         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
498 {
499     StringInstance *This = (StringInstance*)dispex;
500     match_result_t *match_result;
501     DispatchEx *regexp;
502     DispatchEx *array;
503     VARIANT var, *arg_var;
504     DWORD match_cnt, i;
505     HRESULT hres = S_OK;
506
507     TRACE("\n");
508
509     if(arg_cnt(dp) != 1) {
510         FIXME("unsupported args\n");
511         return E_NOTIMPL;
512     }
513
514     arg_var = get_arg(dp, 0);
515     switch(V_VT(arg_var)) {
516     case VT_DISPATCH:
517         regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
518         if(regexp) {
519             if(regexp->builtin_info->class == JSCLASS_REGEXP)
520                 break;
521             jsdisp_release(regexp);
522         }
523     default: {
524         BSTR match_str;
525
526         hres = to_string(dispex->ctx, arg_var, ei, &match_str);
527         if(FAILED(hres))
528             return hres;
529
530         hres = create_regexp_str(dispex->ctx, match_str, SysStringLen(match_str), NULL, 0, &regexp);
531         SysFreeString(match_str);
532         if(FAILED(hres))
533             return hres;
534     }
535     }
536
537     hres = regexp_match(regexp, This->str, This->length, FALSE, &match_result, &match_cnt);
538     jsdisp_release(regexp);
539     if(FAILED(hres))
540         return hres;
541
542     if(!match_cnt) {
543         TRACE("no match\n");
544
545         if(retv)
546             V_VT(retv) = VT_NULL;
547         return S_OK;
548     }
549
550     hres = create_array(dispex->ctx, match_cnt, &array);
551     if(FAILED(hres))
552         return hres;
553
554     V_VT(&var) = VT_BSTR;
555
556     for(i=0; i < match_cnt; i++) {
557         V_BSTR(&var) = SysAllocStringLen(match_result[i].str, match_result[i].len);
558         if(!V_BSTR(&var)) {
559             hres = E_OUTOFMEMORY;
560             break;
561         }
562
563         hres = jsdisp_propput_idx(array, i, lcid, &var, ei, NULL/*FIXME*/);
564         SysFreeString(V_BSTR(&var));
565         if(FAILED(hres))
566             break;
567     }
568
569     if(SUCCEEDED(hres) && retv) {
570         V_VT(retv) = VT_DISPATCH;
571         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(array);
572     }else {
573         jsdisp_release(array);
574     }
575     return hres;
576 }
577
578 typedef struct {
579     WCHAR *buf;
580     DWORD size;
581     DWORD len;
582 } strbuf_t;
583
584 static HRESULT strbuf_append(strbuf_t *buf, const WCHAR *str, DWORD len)
585 {
586     if(!len)
587         return S_OK;
588
589     if(len + buf->len > buf->size) {
590         WCHAR *new_buf;
591         DWORD new_size;
592
593         new_size = buf->size ? buf->size<<1 : 16;
594         if(new_size < buf->len+len)
595             new_size = buf->len+len;
596         if(buf->buf)
597             new_buf = heap_realloc(buf->buf, new_size*sizeof(WCHAR));
598         else
599             new_buf = heap_alloc(new_size*sizeof(WCHAR));
600         if(!new_buf)
601             return E_OUTOFMEMORY;
602
603         buf->buf = new_buf;
604         buf->size = new_size;
605     }
606
607     memcpy(buf->buf+buf->len, str, len*sizeof(WCHAR));
608     buf->len += len;
609     return S_OK;
610 }
611
612 static HRESULT rep_call(DispatchEx *func, const WCHAR *str, match_result_t *match, match_result_t *parens,
613         DWORD parens_cnt, LCID lcid, BSTR *ret, jsexcept_t *ei, IServiceProvider *caller)
614 {
615     DISPPARAMS dp = {NULL, NULL, 0, 0};
616     VARIANTARG *args, *arg;
617     VARIANT var;
618     DWORD i;
619     HRESULT hres = S_OK;
620
621     dp.cArgs = parens_cnt+3;
622     dp.rgvarg = args = heap_alloc_zero(sizeof(VARIANT)*dp.cArgs);
623     if(!args)
624         return E_OUTOFMEMORY;
625
626     arg = get_arg(&dp,0);
627     V_VT(arg) = VT_BSTR;
628     V_BSTR(arg) = SysAllocStringLen(match->str, match->len);
629     if(!V_BSTR(arg))
630         hres = E_OUTOFMEMORY;
631
632     if(SUCCEEDED(hres)) {
633         for(i=0; i < parens_cnt; i++) {
634             arg = get_arg(&dp,i+1);
635             V_VT(arg) = VT_BSTR;
636             V_BSTR(arg) = SysAllocStringLen(parens[i].str, parens[i].len);
637             if(!V_BSTR(arg)) {
638                hres = E_OUTOFMEMORY;
639                break;
640             }
641         }
642     }
643
644     if(SUCCEEDED(hres)) {
645         arg = get_arg(&dp,parens_cnt+1);
646         V_VT(arg) = VT_I4;
647         V_I4(arg) = match->str - str;
648
649         arg = get_arg(&dp,parens_cnt+2);
650         V_VT(arg) = VT_BSTR;
651         V_BSTR(arg) = SysAllocString(str);
652         if(!V_BSTR(arg))
653             hres = E_OUTOFMEMORY;
654     }
655
656     if(SUCCEEDED(hres))
657         hres = jsdisp_call_value(func, lcid, DISPATCH_METHOD, &dp, &var, ei, caller);
658
659     for(i=0; i < parens_cnt+1; i++) {
660         if(i != parens_cnt+1)
661             SysFreeString(V_BSTR(get_arg(&dp,i)));
662     }
663     heap_free(args);
664
665     if(FAILED(hres))
666         return hres;
667
668     hres = to_string(func->ctx, &var, ei, ret);
669     VariantClear(&var);
670     return hres;
671 }
672
673 /* ECMA-262 3rd Edition    15.5.4.11 */
674 static HRESULT String_replace(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
675         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
676 {
677     DWORD parens_cnt = 0, parens_size=0, rep_len=0, length;
678     BSTR rep_str = NULL, match_str = NULL, ret_str;
679     DispatchEx *rep_func = NULL, *regexp = NULL;
680     match_result_t *parens = NULL, match;
681     const WCHAR *str;
682     strbuf_t ret = {NULL,0,0};
683     BOOL gcheck = FALSE;
684     VARIANT *arg_var;
685     HRESULT hres = S_OK;
686
687     TRACE("\n");
688
689     if(is_class(dispex, JSCLASS_STRING)) {
690         StringInstance *string = (StringInstance*)dispex;
691         str = string->str;
692         length = string->length;
693     }else {
694         FIXME("not String this\n");
695         return E_NOTIMPL;
696     }
697
698     if(!arg_cnt(dp)) {
699         if(retv) {
700             ret_str = SysAllocString(str);
701             if(!ret_str)
702                 return E_OUTOFMEMORY;
703
704             V_VT(retv) = VT_BSTR;
705             V_BSTR(retv) = ret_str;
706         }
707         return S_OK;
708     }
709
710     arg_var = get_arg(dp, 0);
711     switch(V_VT(arg_var)) {
712     case VT_DISPATCH:
713         regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
714         if(regexp) {
715             if(is_class(regexp, JSCLASS_REGEXP)) {
716                 break;
717             }else {
718                 jsdisp_release(regexp);
719                 regexp = NULL;
720             }
721         }
722
723     default:
724         hres = to_string(dispex->ctx, arg_var, ei, &match_str);
725         if(FAILED(hres))
726             return hres;
727     }
728
729     if(arg_cnt(dp) >= 2) {
730         arg_var = get_arg(dp,1);
731         switch(V_VT(arg_var)) {
732         case VT_DISPATCH:
733             rep_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
734             if(rep_func) {
735                 if(is_class(rep_func, JSCLASS_FUNCTION)) {
736                     break;
737                 }else {
738                     jsdisp_release(rep_func);
739                     rep_func = NULL;
740                 }
741             }
742
743         default:
744             hres = to_string(dispex->ctx, arg_var, ei, &rep_str);
745             if(FAILED(hres))
746                 break;
747
748             if(strchrW(rep_str, '$')) {
749                 FIXME("unsupported $ in replace string\n");
750                 hres = E_NOTIMPL;
751             }
752
753             rep_len = SysStringLen(rep_str);
754         }
755     }
756
757     if(SUCCEEDED(hres)) {
758         const WCHAR *cp, *ecp;
759
760         cp = ecp = str;
761
762         while(1) {
763             if(regexp) {
764                 hres = regexp_match_next(regexp, gcheck, str, length, &cp, rep_func ? &parens : NULL,
765                                          &parens_size, &parens_cnt, &match);
766                 gcheck = TRUE;
767
768                 if(hres == S_FALSE) {
769                     hres = S_OK;
770                     break;
771                 }
772                 if(FAILED(hres))
773                     break;
774             }else {
775                 match.str = strstrW(cp, match_str);
776                 if(!match.str)
777                     break;
778                 match.len = SysStringLen(match_str);
779                 cp = match.str+match.len;
780             }
781
782             hres = strbuf_append(&ret, ecp, match.str-ecp);
783             ecp = match.str+match.len;
784             if(FAILED(hres))
785                 break;
786
787             if(rep_func) {
788                 BSTR cstr;
789
790                 hres = rep_call(rep_func, str, &match, parens, parens_cnt, lcid, &cstr, ei, caller);
791                 if(FAILED(hres))
792                     break;
793
794                 hres = strbuf_append(&ret, cstr, SysStringLen(cstr));
795                 SysFreeString(cstr);
796                 if(FAILED(hres))
797                     break;
798             }else if(rep_str) {
799                 hres = strbuf_append(&ret, rep_str, rep_len);
800                 if(FAILED(hres))
801                     break;
802             }else {
803                 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d'};
804
805                 hres = strbuf_append(&ret, undefinedW, sizeof(undefinedW)/sizeof(WCHAR));
806                 if(FAILED(hres))
807                     break;
808             }
809         }
810
811         if(SUCCEEDED(hres))
812             hres = strbuf_append(&ret, ecp, (str+length)-ecp);
813     }
814
815     if(rep_func)
816         jsdisp_release(rep_func);
817     if(regexp)
818         jsdisp_release(regexp);
819     SysFreeString(rep_str);
820     SysFreeString(match_str);
821     heap_free(parens);
822
823     if(SUCCEEDED(hres) && retv) {
824         ret_str = SysAllocStringLen(ret.buf, ret.len);
825         if(!ret_str)
826             return E_OUTOFMEMORY;
827
828         V_VT(retv) = VT_BSTR;
829         V_BSTR(retv) = ret_str;
830         TRACE("= %s\n", debugstr_w(ret_str));
831     }
832
833     heap_free(ret.buf);
834     return hres;
835 }
836
837 static HRESULT String_search(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
838         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
839 {
840     FIXME("\n");
841     return E_NOTIMPL;
842 }
843
844 /* ECMA-262 3rd Edition    15.5.4.13 */
845 static HRESULT String_slice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
846         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
847 {
848     const WCHAR *str;
849     DWORD length;
850     INT start=0, end;
851     VARIANT v;
852     HRESULT hres;
853
854     TRACE("\n");
855
856     if(is_class(dispex, JSCLASS_STRING)) {
857         StringInstance *string = (StringInstance*)dispex;
858
859         str = string->str;
860         length = string->length;
861     }else {
862         FIXME("this is not a string class\n");
863         return E_NOTIMPL;
864     }
865
866     if(arg_cnt(dp)) {
867         hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &v);
868         if(FAILED(hres))
869             return hres;
870
871         if(V_VT(&v) == VT_I4) {
872             start = V_I4(&v);
873             if(start < 0) {
874                 start = length + start;
875                 if(start < 0)
876                     start = 0;
877             }else if(start > length) {
878                 start = length;
879             }
880         }else {
881             start = V_R8(&v) < 0.0 ? 0 : length;
882         }
883     }else {
884         start = 0;
885     }
886
887     if(arg_cnt(dp) >= 2) {
888         hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-2, ei, &v);
889         if(FAILED(hres))
890             return hres;
891
892         if(V_VT(&v) == VT_I4) {
893             end = V_I4(&v);
894             if(end < 0) {
895                 end = length + end;
896                 if(end < 0)
897                     end = 0;
898             }else if(end > length) {
899                 end = length;
900             }
901         }else {
902             end = V_R8(&v) < 0.0 ? 0 : length;
903         }
904     }else {
905         end = length;
906     }
907
908     if(end < start)
909         end = start;
910
911     if(retv) {
912         BSTR retstr = SysAllocStringLen(str+start, end-start);
913         if(!str)
914             return E_OUTOFMEMORY;
915
916         V_VT(retv) = VT_BSTR;
917         V_BSTR(retv) = retstr;
918     }
919     return S_OK;
920 }
921
922 static HRESULT String_small(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
923         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
924 {
925     static const WCHAR smalltagW[] = {'S','M','A','L','L',0};
926     return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, smalltagW);
927 }
928
929 static HRESULT String_split(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
930         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
931 {
932     match_result_t *match_result = NULL;
933     DWORD match_cnt, i, match_len = 0;
934     StringInstance *string;
935     const WCHAR *ptr, *ptr2;
936     VARIANT *arg, var;
937     DispatchEx *array;
938     BSTR match_str = NULL;
939     HRESULT hres;
940
941     TRACE("\n");
942
943     if(!is_class(dispex, JSCLASS_STRING)) {
944         FIXME("not String this\n");
945         return E_NOTIMPL;
946     }
947
948     string = (StringInstance*)dispex;
949
950     if(arg_cnt(dp) != 1) {
951         FIXME("unsupported args\n");
952         return E_NOTIMPL;
953     }
954
955     arg = get_arg(dp, 0);
956     switch(V_VT(arg)) {
957     case VT_DISPATCH: {
958         DispatchEx *regexp;
959
960         regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
961         if(regexp) {
962             if(is_class(regexp, JSCLASS_REGEXP)) {
963                 hres = regexp_match(regexp, string->str, string->length, TRUE, &match_result, &match_cnt);
964                 jsdisp_release(regexp);
965                 if(FAILED(hres))
966                     return hres;
967                 break;
968             }
969             jsdisp_release(regexp);
970         }
971     }
972     default:
973         hres = to_string(dispex->ctx, arg, ei, &match_str);
974         if(FAILED(hres))
975             return hres;
976
977         match_len = SysStringLen(match_str);
978         if(!match_len) {
979             SysFreeString(match_str);
980             match_str = NULL;
981         }
982     }
983
984     hres = create_array(dispex->ctx, 0, &array);
985
986     if(SUCCEEDED(hres)) {
987         ptr = string->str;
988         for(i=0;; i++) {
989             if(match_result) {
990                 if(i == match_cnt)
991                     break;
992                 ptr2 = match_result[i].str;
993             }else if(match_str) {
994                 ptr2 = strstrW(ptr, match_str);
995                 if(!ptr2)
996                     break;
997             }else {
998                 if(!*ptr)
999                     break;
1000                 ptr2 = ptr+1;
1001             }
1002
1003             V_VT(&var) = VT_BSTR;
1004             V_BSTR(&var) = SysAllocStringLen(ptr, ptr2-ptr);
1005             if(!V_BSTR(&var)) {
1006                 hres = E_OUTOFMEMORY;
1007                 break;
1008             }
1009
1010             hres = jsdisp_propput_idx(array, i, lcid, &var, ei, sp);
1011             SysFreeString(V_BSTR(&var));
1012             if(FAILED(hres))
1013                 break;
1014
1015             if(match_result)
1016                 ptr = match_result[i].str + match_result[i].len;
1017             else if(match_str)
1018                 ptr = ptr2 + match_len;
1019             else
1020                 ptr++;
1021         }
1022     }
1023
1024     if(SUCCEEDED(hres) && (match_str || match_result)) {
1025         DWORD len = (string->str+string->length) - ptr;
1026
1027         if(len || match_str) {
1028             V_VT(&var) = VT_BSTR;
1029             V_BSTR(&var) = SysAllocStringLen(ptr, len);
1030
1031             if(V_BSTR(&var)) {
1032                 hres = jsdisp_propput_idx(array, i, lcid, &var, ei, sp);
1033                 SysFreeString(V_BSTR(&var));
1034             }else {
1035                 hres = E_OUTOFMEMORY;
1036             }
1037         }
1038     }
1039
1040     SysFreeString(match_str);
1041     heap_free(match_result);
1042
1043     if(SUCCEEDED(hres) && retv) {
1044         V_VT(retv) = VT_DISPATCH;
1045         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(array);
1046     }else {
1047         jsdisp_release(array);
1048     }
1049
1050     return hres;
1051 }
1052
1053 static HRESULT String_strike(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1054         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1055 {
1056     static const WCHAR striketagW[] = {'S','T','R','I','K','E',0};
1057     return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, striketagW);
1058 }
1059
1060 static HRESULT String_sub(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1061         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1062 {
1063     static const WCHAR subtagW[] = {'S','U','B',0};
1064     return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, subtagW);
1065 }
1066
1067 /* ECMA-262 3rd Edition    15.5.4.15 */
1068 static HRESULT String_substring(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1069         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1070 {
1071     const WCHAR *str;
1072     INT start=0, end;
1073     DWORD length;
1074     VARIANT v;
1075     HRESULT hres;
1076
1077     TRACE("\n");
1078
1079     if(is_class(dispex, JSCLASS_STRING)) {
1080         StringInstance *string = (StringInstance*)dispex;
1081
1082         length = string->length;
1083         str = string->str;
1084     }else {
1085         FIXME("not string this not supported\n");
1086         return E_NOTIMPL;
1087     }
1088
1089     if(arg_cnt(dp) >= 1) {
1090         hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &v);
1091         if(FAILED(hres))
1092             return hres;
1093
1094         if(V_VT(&v) == VT_I4) {
1095             start = V_I4(&v);
1096             if(start < 0)
1097                 start = 0;
1098             else if(start >= length)
1099                 start = length;
1100         }else {
1101             start = V_R8(&v) < 0.0 ? 0 : length;
1102         }
1103     }
1104
1105     if(arg_cnt(dp) >= 2) {
1106         hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-2, ei, &v);
1107         if(FAILED(hres))
1108             return hres;
1109
1110         if(V_VT(&v) == VT_I4) {
1111             end = V_I4(&v);
1112             if(end < 0)
1113                 end = 0;
1114             else if(end > length)
1115                 end = length;
1116         }else {
1117             end = V_R8(&v) < 0.0 ? 0 : length;
1118         }
1119     }else {
1120         end = length;
1121     }
1122
1123     if(start > end) {
1124         INT tmp = start;
1125         start = end;
1126         end = tmp;
1127     }
1128
1129     if(retv) {
1130         V_VT(retv) = VT_BSTR;
1131         V_BSTR(retv) = SysAllocStringLen(str+start, end-start);
1132         if(!V_BSTR(retv))
1133             return E_OUTOFMEMORY;
1134     }
1135     return S_OK;
1136 }
1137
1138 static HRESULT String_substr(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1139         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1140 {
1141     FIXME("\n");
1142     return E_NOTIMPL;
1143 }
1144
1145 static HRESULT String_sup(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1146         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1147 {
1148     static const WCHAR suptagW[] = {'S','U','P',0};
1149     return do_attributeless_tag_format(dispex, lcid, flags, dp, retv, ei, sp, suptagW);
1150 }
1151
1152 static HRESULT String_toLowerCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1153         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1154 {
1155     StringInstance *string;
1156     const WCHAR* str;
1157     DWORD length;
1158     BSTR bstr;
1159
1160     TRACE("\n");
1161
1162     if(is_class(dispex, JSCLASS_STRING)) {
1163         string = (StringInstance*)dispex;
1164
1165         length = string->length;
1166         str = string->str;
1167     }else {
1168         FIXME("not string this not supported\n");
1169         return E_NOTIMPL;
1170     }
1171
1172     if(retv) {
1173         bstr = SysAllocStringLen(str, length);
1174         if (!bstr)
1175             return E_OUTOFMEMORY;
1176
1177         strlwrW(bstr);
1178
1179         V_VT(retv) = VT_BSTR;
1180         V_BSTR(retv) = bstr;
1181     }
1182     return S_OK;
1183 }
1184
1185 static HRESULT String_toUpperCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1186         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1187 {
1188     StringInstance *string;
1189     const WCHAR* str;
1190     DWORD length;
1191     BSTR bstr;
1192
1193     TRACE("\n");
1194
1195     if(is_class(dispex, JSCLASS_STRING)) {
1196         string = (StringInstance*)dispex;
1197
1198         length = string->length;
1199         str = string->str;
1200     }else {
1201         FIXME("not string this not supported\n");
1202         return E_NOTIMPL;
1203     }
1204
1205     if(retv) {
1206         bstr = SysAllocStringLen(str, length);
1207         if (!bstr)
1208             return E_OUTOFMEMORY;
1209
1210         struprW(bstr);
1211
1212         V_VT(retv) = VT_BSTR;
1213         V_BSTR(retv) = bstr;
1214     }
1215     return S_OK;
1216 }
1217
1218 static HRESULT String_toLocaleLowerCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1219         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1220 {
1221     FIXME("\n");
1222     return E_NOTIMPL;
1223 }
1224
1225 static HRESULT String_toLocaleUpperCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1226         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1227 {
1228     FIXME("\n");
1229     return E_NOTIMPL;
1230 }
1231
1232 static HRESULT String_localeCompare(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1233         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1234 {
1235     FIXME("\n");
1236     return E_NOTIMPL;
1237 }
1238
1239 static HRESULT String_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1240         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1241 {
1242     FIXME("\n");
1243     return E_NOTIMPL;
1244 }
1245
1246 static HRESULT String_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1247         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1248 {
1249     FIXME("\n");
1250     return E_NOTIMPL;
1251 }
1252
1253 static HRESULT String_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1254         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1255 {
1256     FIXME("\n");
1257     return E_NOTIMPL;
1258 }
1259
1260 static HRESULT String_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1261         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1262 {
1263     StringInstance *This = (StringInstance*)dispex;
1264
1265     TRACE("\n");
1266
1267     switch(flags) {
1268     case DISPATCH_PROPERTYGET: {
1269         BSTR str = SysAllocString(This->str);
1270         if(!str)
1271             return E_OUTOFMEMORY;
1272
1273         V_VT(retv) = VT_BSTR;
1274         V_BSTR(retv) = str;
1275         break;
1276     }
1277     default:
1278         FIXME("flags %x\n", flags);
1279         return E_NOTIMPL;
1280     }
1281
1282     return S_OK;
1283 }
1284
1285 static void String_destructor(DispatchEx *dispex)
1286 {
1287     StringInstance *This = (StringInstance*)dispex;
1288
1289     heap_free(This->str);
1290     heap_free(This);
1291 }
1292
1293 static const builtin_prop_t String_props[] = {
1294     {anchorW,                String_anchor,                PROPF_METHOD},
1295     {bigW,                   String_big,                   PROPF_METHOD},
1296     {blinkW,                 String_blink,                 PROPF_METHOD},
1297     {boldW,                  String_bold,                  PROPF_METHOD},
1298     {charAtW,                String_charAt,                PROPF_METHOD},
1299     {charCodeAtW,            String_charCodeAt,            PROPF_METHOD},
1300     {concatW,                String_concat,                PROPF_METHOD},
1301     {fixedW,                 String_fixed,                 PROPF_METHOD},
1302     {fontcolorW,             String_fontcolor,             PROPF_METHOD},
1303     {fontsizeW,              String_fontsize,              PROPF_METHOD},
1304     {hasOwnPropertyW,        String_hasOwnProperty,        PROPF_METHOD},
1305     {indexOfW,               String_indexOf,               PROPF_METHOD},
1306     {isPrototypeOfW,         String_isPrototypeOf,         PROPF_METHOD},
1307     {italicsW,               String_italics,               PROPF_METHOD},
1308     {lastIndexOfW,           String_lastIndexOf,           PROPF_METHOD},
1309     {lengthW,                String_length,                0},
1310     {linkW,                  String_link,                  PROPF_METHOD},
1311     {localeCompareW,         String_localeCompare,         PROPF_METHOD},
1312     {matchW,                 String_match,                 PROPF_METHOD},
1313     {propertyIsEnumerableW,  String_propertyIsEnumerable,  PROPF_METHOD},
1314     {replaceW,               String_replace,               PROPF_METHOD},
1315     {searchW,                String_search,                PROPF_METHOD},
1316     {sliceW,                 String_slice,                 PROPF_METHOD},
1317     {smallW,                 String_small,                 PROPF_METHOD},
1318     {splitW,                 String_split,                 PROPF_METHOD},
1319     {strikeW,                String_strike,                PROPF_METHOD},
1320     {subW,                   String_sub,                   PROPF_METHOD},
1321     {substrW,                String_substr,                PROPF_METHOD},
1322     {substringW,             String_substring,             PROPF_METHOD},
1323     {supW,                   String_sup,                   PROPF_METHOD},
1324     {toLocaleLowerCaseW,     String_toLocaleLowerCase,     PROPF_METHOD},
1325     {toLocaleUpperCaseW,     String_toLocaleUpperCase,     PROPF_METHOD},
1326     {toLowerCaseW,           String_toLowerCase,           PROPF_METHOD},
1327     {toStringW,              String_toString,              PROPF_METHOD},
1328     {toUpperCaseW,           String_toUpperCase,           PROPF_METHOD},
1329     {valueOfW,               String_valueOf,               PROPF_METHOD}
1330 };
1331
1332 static const builtin_info_t String_info = {
1333     JSCLASS_STRING,
1334     {NULL, String_value, 0},
1335     sizeof(String_props)/sizeof(*String_props),
1336     String_props,
1337     String_destructor,
1338     NULL
1339 };
1340
1341 static HRESULT StringConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1342         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1343 {
1344     HRESULT hres;
1345
1346     TRACE("\n");
1347
1348     switch(flags) {
1349     case INVOKE_FUNC: {
1350         BSTR str;
1351
1352         if(arg_cnt(dp)) {
1353             hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
1354             if(FAILED(hres))
1355                 return hres;
1356         }else {
1357             str = SysAllocStringLen(NULL, 0);
1358             if(!str)
1359                 return E_OUTOFMEMORY;
1360         }
1361
1362         V_VT(retv) = VT_BSTR;
1363         V_BSTR(retv) = str;
1364         break;
1365     }
1366     case DISPATCH_CONSTRUCT: {
1367         DispatchEx *ret;
1368
1369         if(arg_cnt(dp)) {
1370             BSTR str;
1371
1372             hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
1373             if(FAILED(hres))
1374                 return hres;
1375
1376             hres = create_string(dispex->ctx, str, SysStringLen(str), &ret);
1377             SysFreeString(str);
1378         }else {
1379             hres = create_string(dispex->ctx, NULL, 0, &ret);
1380         }
1381
1382         if(FAILED(hres))
1383             return hres;
1384
1385         V_VT(retv) = VT_DISPATCH;
1386         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
1387         break;
1388     }
1389
1390     default:
1391         FIXME("unimplemented flags: %x\n", flags);
1392         return E_NOTIMPL;
1393     }
1394
1395     return S_OK;
1396 }
1397
1398 static HRESULT string_alloc(script_ctx_t *ctx, BOOL use_constr, StringInstance **ret)
1399 {
1400     StringInstance *string;
1401     HRESULT hres;
1402
1403     string = heap_alloc_zero(sizeof(StringInstance));
1404     if(!string)
1405         return E_OUTOFMEMORY;
1406
1407     if(use_constr)
1408         hres = init_dispex_from_constr(&string->dispex, ctx, &String_info, ctx->string_constr);
1409     else
1410         hres = init_dispex(&string->dispex, ctx, &String_info, NULL);
1411     if(FAILED(hres)) {
1412         heap_free(string);
1413         return hres;
1414     }
1415
1416     *ret = string;
1417     return S_OK;
1418 }
1419
1420 HRESULT create_string_constr(script_ctx_t *ctx, DispatchEx **ret)
1421 {
1422     StringInstance *string;
1423     HRESULT hres;
1424
1425     hres = string_alloc(ctx, FALSE, &string);
1426     if(FAILED(hres))
1427         return hres;
1428
1429     hres = create_builtin_function(ctx, StringConstr_value, NULL, PROPF_CONSTR, &string->dispex, ret);
1430
1431     jsdisp_release(&string->dispex);
1432     return hres;
1433 }
1434
1435 HRESULT create_string(script_ctx_t *ctx, const WCHAR *str, DWORD len, DispatchEx **ret)
1436 {
1437     StringInstance *string;
1438     HRESULT hres;
1439
1440     hres = string_alloc(ctx, TRUE, &string);
1441     if(FAILED(hres))
1442         return hres;
1443
1444     if(len == -1)
1445         len = strlenW(str);
1446
1447     string->length = len;
1448     string->str = heap_alloc((len+1)*sizeof(WCHAR));
1449     if(!string->str) {
1450         jsdisp_release(&string->dispex);
1451         return E_OUTOFMEMORY;
1452     }
1453
1454     memcpy(string->str, str, len*sizeof(WCHAR));
1455     string->str[len] = 0;
1456
1457     *ret = &string->dispex;
1458     return S_OK;
1459
1460 }