jscript: Added while and do..while statement 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 static HRESULT String_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
92         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
93 {
94     FIXME("\n");
95     return E_NOTIMPL;
96 }
97
98 static HRESULT String_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
99         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
100 {
101     FIXME("\n");
102     return E_NOTIMPL;
103 }
104
105 static HRESULT String_anchor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
106         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
107 {
108     FIXME("\n");
109     return E_NOTIMPL;
110 }
111
112 static HRESULT String_big(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
113         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
114 {
115     FIXME("\n");
116     return E_NOTIMPL;
117 }
118
119 static HRESULT String_blink(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
120         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
121 {
122     FIXME("\n");
123     return E_NOTIMPL;
124 }
125
126 static HRESULT String_bold(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
127         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
128 {
129     FIXME("\n");
130     return E_NOTIMPL;
131 }
132
133 static HRESULT String_charAt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
134         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
135 {
136     FIXME("\n");
137     return E_NOTIMPL;
138 }
139
140 static HRESULT String_charCodeAt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
141         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
142 {
143     FIXME("\n");
144     return E_NOTIMPL;
145 }
146
147 static HRESULT String_concat(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
148         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
149 {
150     FIXME("\n");
151     return E_NOTIMPL;
152 }
153
154 static HRESULT String_fixed(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
155         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
156 {
157     FIXME("\n");
158     return E_NOTIMPL;
159 }
160
161 static HRESULT String_fontcolor(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
162         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
163 {
164     FIXME("\n");
165     return E_NOTIMPL;
166 }
167
168 static HRESULT String_fontsize(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
169         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
170 {
171     FIXME("\n");
172     return E_NOTIMPL;
173 }
174
175 static HRESULT String_indexOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
176         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
177 {
178     FIXME("\n");
179     return E_NOTIMPL;
180 }
181
182 static HRESULT String_italics(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
183         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
184 {
185     FIXME("\n");
186     return E_NOTIMPL;
187 }
188
189 static HRESULT String_lastIndexOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
190         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
191 {
192     FIXME("\n");
193     return E_NOTIMPL;
194 }
195
196 static HRESULT String_link(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
197         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
198 {
199     FIXME("\n");
200     return E_NOTIMPL;
201 }
202
203 static HRESULT String_match(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
204         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
205 {
206     StringInstance *This = (StringInstance*)dispex;
207     match_result_t *match_result;
208     DispatchEx *array;
209     VARIANT var, *arg_var;
210     DWORD match_cnt, i;
211     HRESULT hres = S_OK;
212
213     TRACE("\n");
214
215     if(dp->cArgs - dp->cNamedArgs != 1) {
216         FIXME("unsupported args\n");
217         return E_NOTIMPL;
218     }
219
220     arg_var = get_arg(dp, 0);
221     switch(V_VT(arg_var)) {
222     case VT_DISPATCH: {
223         DispatchEx *regexp;
224
225         regexp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg_var));
226         if(regexp) {
227             if(regexp->builtin_info->class == JSCLASS_REGEXP) {
228                 hres = regexp_match(regexp, This->str, This->length, FALSE, &match_result, &match_cnt);
229                 jsdisp_release(regexp);
230                 if(FAILED(hres))
231                     return hres;
232                 break;
233             }
234             jsdisp_release(regexp);
235         }
236     }
237     default:
238         FIXME("implemented only for regexp args\n");
239         return E_NOTIMPL;
240     }
241
242     if(!match_cnt) {
243         TRACE("no match\n");
244
245         if(retv)
246             V_VT(retv) = VT_NULL;
247         return S_OK;
248     }
249
250     hres = create_array(dispex->ctx, match_cnt, &array);
251     if(FAILED(hres))
252         return hres;
253
254     V_VT(&var) = VT_BSTR;
255
256     for(i=0; i < match_cnt; i++) {
257         V_BSTR(&var) = SysAllocStringLen(match_result[i].str, match_result[i].len);
258         if(!V_BSTR(&var)) {
259             hres = E_OUTOFMEMORY;
260             break;
261         }
262
263         hres = jsdisp_propput_idx(array, i, lcid, &var, ei, NULL/*FIXME*/);
264         SysFreeString(V_BSTR(&var));
265         if(FAILED(hres))
266             break;
267     }
268
269     if(FAILED(hres)) {
270         jsdisp_release(array);
271         return hres;
272     }
273
274     V_VT(retv) = VT_DISPATCH;
275     V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(array);
276     return S_OK;
277 }
278
279 static HRESULT String_replace(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
280         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
281 {
282     FIXME("\n");
283     return E_NOTIMPL;
284 }
285
286 static HRESULT String_search(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
287         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
288 {
289     FIXME("\n");
290     return E_NOTIMPL;
291 }
292
293 static HRESULT String_slice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
294         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
295 {
296     FIXME("\n");
297     return E_NOTIMPL;
298 }
299
300 static HRESULT String_small(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
301         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
302 {
303     FIXME("\n");
304     return E_NOTIMPL;
305 }
306
307 static HRESULT String_split(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
308         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
309 {
310     FIXME("\n");
311     return E_NOTIMPL;
312 }
313
314 static HRESULT String_strike(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
315         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
316 {
317     FIXME("\n");
318     return E_NOTIMPL;
319 }
320
321 static HRESULT String_sub(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
322         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
323 {
324     FIXME("\n");
325     return E_NOTIMPL;
326 }
327
328 static HRESULT String_substring(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
329         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
330 {
331     FIXME("\n");
332     return E_NOTIMPL;
333 }
334
335 static HRESULT String_substr(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
336         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
337 {
338     FIXME("\n");
339     return E_NOTIMPL;
340 }
341
342 static HRESULT String_sup(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
343         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
344 {
345     FIXME("\n");
346     return E_NOTIMPL;
347 }
348
349 static HRESULT String_toLowerCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
350         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
351 {
352     FIXME("\n");
353     return E_NOTIMPL;
354 }
355
356 static HRESULT String_toUpperCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
357         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
358 {
359     FIXME("\n");
360     return E_NOTIMPL;
361 }
362
363 static HRESULT String_toLocaleLowerCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
364         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
365 {
366     FIXME("\n");
367     return E_NOTIMPL;
368 }
369
370 static HRESULT String_toLocaleUpperCase(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
371         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
372 {
373     FIXME("\n");
374     return E_NOTIMPL;
375 }
376
377 static HRESULT String_localeCompare(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
378         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
379 {
380     FIXME("\n");
381     return E_NOTIMPL;
382 }
383
384 static HRESULT String_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
385         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
386 {
387     FIXME("\n");
388     return E_NOTIMPL;
389 }
390
391 static HRESULT String_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
392         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
393 {
394     FIXME("\n");
395     return E_NOTIMPL;
396 }
397
398 static HRESULT String_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
399         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
400 {
401     FIXME("\n");
402     return E_NOTIMPL;
403 }
404
405 static HRESULT String_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
406         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
407 {
408     FIXME("\n");
409     return E_NOTIMPL;
410 }
411
412 static void String_destructor(DispatchEx *dispex)
413 {
414     StringInstance *This = (StringInstance*)dispex;
415
416     heap_free(This->str);
417     heap_free(This);
418 }
419
420 static const builtin_prop_t String_props[] = {
421     {anchorW,                String_anchor,                PROPF_METHOD},
422     {bigW,                   String_big,                   PROPF_METHOD},
423     {blinkW,                 String_blink,                 PROPF_METHOD},
424     {boldW,                  String_bold,                  PROPF_METHOD},
425     {charAtW,                String_charAt,                PROPF_METHOD},
426     {charCodeAtW,            String_charCodeAt,            PROPF_METHOD},
427     {concatW,                String_concat,                PROPF_METHOD},
428     {fixedW,                 String_fixed,                 PROPF_METHOD},
429     {fontcolorW,             String_fontcolor,             PROPF_METHOD},
430     {fontsizeW,              String_fontsize,              PROPF_METHOD},
431     {hasOwnPropertyW,        String_hasOwnProperty,        PROPF_METHOD},
432     {indexOfW,               String_indexOf,               PROPF_METHOD},
433     {isPrototypeOfW,         String_isPrototypeOf,         PROPF_METHOD},
434     {italicsW,               String_italics,               PROPF_METHOD},
435     {lastIndexOfW,           String_lastIndexOf,           PROPF_METHOD},
436     {lengthW,                String_length,                0},
437     {linkW,                  String_link,                  PROPF_METHOD},
438     {localeCompareW,         String_localeCompare,         PROPF_METHOD},
439     {matchW,                 String_match,                 PROPF_METHOD},
440     {propertyIsEnumerableW,  String_propertyIsEnumerable,  PROPF_METHOD},
441     {replaceW,               String_replace,               PROPF_METHOD},
442     {searchW,                String_search,                PROPF_METHOD},
443     {sliceW,                 String_slice,                 PROPF_METHOD},
444     {smallW,                 String_small,                 PROPF_METHOD},
445     {splitW,                 String_split,                 PROPF_METHOD},
446     {strikeW,                String_strike,                PROPF_METHOD},
447     {substringW,             String_substring,             PROPF_METHOD},
448     {substrW,                String_substr,                PROPF_METHOD},
449     {subW,                   String_sub,                   PROPF_METHOD},
450     {supW,                   String_sup,                   PROPF_METHOD},
451     {toLocaleLowerCaseW,     String_toLocaleLowerCase,     PROPF_METHOD},
452     {toLocaleUpperCaseW,     String_toLocaleUpperCase,     PROPF_METHOD},
453     {toLowerCaseW,           String_toLowerCase,           PROPF_METHOD},
454     {toStringW,              String_toString,              PROPF_METHOD},
455     {toUpperCaseW,           String_toUpperCase,           PROPF_METHOD},
456     {valueOfW,               String_valueOf,               PROPF_METHOD}
457 };
458
459 static const builtin_info_t String_info = {
460     JSCLASS_STRING,
461     {NULL, String_value, 0},
462     sizeof(String_props)/sizeof(*String_props),
463     String_props,
464     String_destructor,
465     NULL
466 };
467
468 static HRESULT StringConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
469         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
470 {
471     FIXME("\n");
472     return E_NOTIMPL;
473 }
474
475 static HRESULT string_alloc(script_ctx_t *ctx, BOOL use_constr, StringInstance **ret)
476 {
477     StringInstance *string;
478     HRESULT hres;
479
480     string = heap_alloc_zero(sizeof(StringInstance));
481     if(!string)
482         return E_OUTOFMEMORY;
483
484     if(use_constr)
485         hres = init_dispex_from_constr(&string->dispex, ctx, &String_info, ctx->string_constr);
486     else
487         hres = init_dispex(&string->dispex, ctx, &String_info, NULL);
488     if(FAILED(hres)) {
489         heap_free(string);
490         return hres;
491     }
492
493     *ret = string;
494     return S_OK;
495 }
496
497 HRESULT create_string_constr(script_ctx_t *ctx, DispatchEx **ret)
498 {
499     StringInstance *string;
500     HRESULT hres;
501
502     hres = string_alloc(ctx, FALSE, &string);
503     if(FAILED(hres))
504         return hres;
505
506     hres = create_builtin_function(ctx, StringConstr_value, PROPF_CONSTR, &string->dispex, ret);
507
508     jsdisp_release(&string->dispex);
509     return hres;
510 }
511
512 HRESULT create_string(script_ctx_t *ctx, const WCHAR *str, DWORD len, DispatchEx **ret)
513 {
514     StringInstance *string;
515     HRESULT hres;
516
517     hres = string_alloc(ctx, TRUE, &string);
518     if(FAILED(hres))
519         return hres;
520
521     if(len == -1)
522         len = strlenW(str);
523
524     string->length = len;
525     string->str = heap_alloc((len+1)*sizeof(WCHAR));
526     if(!string->str) {
527         jsdisp_release(&string->dispex);
528         return E_OUTOFMEMORY;
529     }
530
531     memcpy(string->str, str, len*sizeof(WCHAR));
532     string->str[len] = 0;
533
534     *ret = &string->dispex;
535     return S_OK;
536
537 }