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