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