jscript: Fixed some leaks (coverity).
[wine] / dlls / jscript / function.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 <assert.h>
20
21 #include "jscript.h"
22 #include "engine.h"
23
24 #include "wine/debug.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
27
28 typedef struct {
29     jsdisp_t dispex;
30     builtin_invoke_t value_proc;
31     const WCHAR *name;
32     DWORD flags;
33     scope_chain_t *scope_chain;
34     bytecode_t *code;
35     function_code_t *func_code;
36     DWORD length;
37     jsdisp_t *arguments;
38 } FunctionInstance;
39
40 static inline FunctionInstance *function_from_vdisp(vdisp_t *vdisp)
41 {
42     return (FunctionInstance*)vdisp->u.jsdisp;
43 }
44
45 static inline FunctionInstance *function_this(vdisp_t *jsthis)
46 {
47     return is_vclass(jsthis, JSCLASS_FUNCTION) ? function_from_vdisp(jsthis) : NULL;
48 }
49
50 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
51
52 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
53 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
54 static const WCHAR applyW[] = {'a','p','p','l','y',0};
55 static const WCHAR callW[] = {'c','a','l','l',0};
56 static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
57
58 static HRESULT init_parameters(jsdisp_t *var_disp, FunctionInstance *function, unsigned argc, jsval_t *argv)
59 {
60     DWORD i=0;
61     HRESULT hres;
62
63     for(i=0; i < function->func_code->param_cnt; i++) {
64         hres = jsdisp_propput_name(var_disp, function->func_code->params[i],
65                 i < argc ? argv[i] : jsval_undefined());
66         if(FAILED(hres))
67             return hres;
68     }
69
70     return S_OK;
71 }
72
73 static HRESULT Arguments_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
74         jsval_t *r)
75 {
76     FIXME("\n");
77     return E_NOTIMPL;
78 }
79
80 static const builtin_info_t Arguments_info = {
81     JSCLASS_ARGUMENTS,
82     {NULL, Arguments_value, 0},
83     0, NULL,
84     NULL,
85     NULL
86 };
87
88 static HRESULT create_arguments(script_ctx_t *ctx, IDispatch *calee, unsigned argc, jsval_t *argv, jsdisp_t **ret)
89 {
90     jsdisp_t *args;
91     DWORD i;
92     HRESULT hres;
93
94     static const WCHAR caleeW[] = {'c','a','l','l','e','e',0};
95
96     args = heap_alloc_zero(sizeof(jsdisp_t));
97     if(!args)
98         return E_OUTOFMEMORY;
99
100     hres = init_dispex_from_constr(args, ctx, &Arguments_info, ctx->object_constr);
101     if(FAILED(hres)) {
102         heap_free(args);
103         return hres;
104     }
105
106     for(i=0; i < argc; i++) {
107         hres = jsdisp_propput_idx(args, i, argv[i]);
108         if(FAILED(hres))
109             break;
110     }
111
112     if(SUCCEEDED(hres)) {
113         hres = jsdisp_propput_name(args, lengthW, jsval_number(argc));
114
115         if(SUCCEEDED(hres))
116             hres = jsdisp_propput_name(args, caleeW, jsval_disp(calee));
117     }
118
119     if(FAILED(hres)) {
120         jsdisp_release(args);
121         return hres;
122     }
123
124     *ret = args;
125     return S_OK;
126 }
127
128 static HRESULT create_var_disp(script_ctx_t *ctx, FunctionInstance *function, jsdisp_t *arg_disp,
129         unsigned argc, jsval_t *argv, jsdisp_t **ret)
130 {
131     jsdisp_t *var_disp;
132     HRESULT hres;
133
134     hres = create_dispex(ctx, NULL, NULL, &var_disp);
135     if(FAILED(hres))
136         return hres;
137
138     hres = jsdisp_propput_name(var_disp, argumentsW, jsval_obj(arg_disp));
139     if(SUCCEEDED(hres))
140         hres = init_parameters(var_disp, function, argc, argv);
141     if(FAILED(hres)) {
142         jsdisp_release(var_disp);
143         return hres;
144     }
145
146     *ret = var_disp;
147     return S_OK;
148 }
149
150 static HRESULT invoke_source(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, unsigned argc, jsval_t *argv,
151         jsval_t *r)
152 {
153     jsdisp_t *var_disp, *arg_disp;
154     exec_ctx_t *exec_ctx;
155     scope_chain_t *scope;
156     HRESULT hres;
157
158     if(!function->func_code) {
159         FIXME("no source\n");
160         return E_FAIL;
161     }
162
163     hres = create_arguments(ctx, to_disp(&function->dispex), argc, argv, &arg_disp);
164     if(FAILED(hres))
165         return hres;
166
167     hres = create_var_disp(ctx, function, arg_disp, argc, argv, &var_disp);
168     if(FAILED(hres)) {
169         jsdisp_release(arg_disp);
170         return hres;
171     }
172
173     hres = scope_push(function->scope_chain, var_disp, to_disp(var_disp), &scope);
174     if(SUCCEEDED(hres)) {
175         hres = create_exec_ctx(ctx, this_obj, var_disp, scope, FALSE, &exec_ctx);
176         scope_release(scope);
177     }
178     jsdisp_release(var_disp);
179     if(SUCCEEDED(hres)) {
180         jsdisp_t *prev_args;
181
182         prev_args = function->arguments;
183         function->arguments = arg_disp;
184         hres = exec_source(exec_ctx, function->code, function->func_code, FALSE, r);
185         function->arguments = prev_args;
186     }
187
188     jsdisp_release(arg_disp);
189     exec_release(exec_ctx);
190     return hres;
191 }
192
193 static HRESULT invoke_constructor(script_ctx_t *ctx, FunctionInstance *function, unsigned argc, jsval_t *argv,
194         jsval_t *r)
195 {
196     jsdisp_t *this_obj;
197     jsval_t var;
198     HRESULT hres;
199
200     hres = create_object(ctx, &function->dispex, &this_obj);
201     if(FAILED(hres))
202         return hres;
203
204     hres = invoke_source(ctx, function, to_disp(this_obj), argc, argv, &var);
205     if(FAILED(hres)) {
206         jsdisp_release(this_obj);
207         return hres;
208     }
209
210     if(is_object_instance(var)) {
211         jsdisp_release(this_obj);
212         *r = var;
213     }else {
214         jsval_release(var);
215         *r = jsval_obj(this_obj);
216     }
217     return S_OK;
218 }
219
220 static HRESULT invoke_value_proc(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_disp, WORD flags,
221         unsigned argc, jsval_t *argv, jsval_t *r)
222 {
223     vdisp_t vthis;
224     HRESULT hres;
225
226     if(this_disp)
227         set_disp(&vthis, this_disp);
228     else if(ctx->host_global)
229         set_disp(&vthis, ctx->host_global);
230     else
231         set_jsdisp(&vthis, ctx->global);
232
233     hres = function->value_proc(ctx, &vthis, flags, argc, argv, r);
234
235     vdisp_release(&vthis);
236     return hres;
237 }
238
239 static HRESULT call_function(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj,
240         unsigned argc, jsval_t *argv, jsval_t *r)
241 {
242     if(function->value_proc)
243         return invoke_value_proc(ctx, function, this_obj, DISPATCH_METHOD, argc, argv, r);
244
245     return invoke_source(ctx, function, this_obj, argc, argv, r);
246 }
247
248 static HRESULT function_to_string(FunctionInstance *function, jsstr_t **ret)
249 {
250     jsstr_t *str;
251
252     static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
253     static const WCHAR native_suffixW[] =
254         {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
255
256     if(function->value_proc) {
257         DWORD name_len;
258
259         name_len = strlenW(function->name);
260         str = jsstr_alloc_buf((sizeof(native_prefixW)+sizeof(native_suffixW))/sizeof(WCHAR) + name_len);
261         if(!str)
262             return E_OUTOFMEMORY;
263
264         memcpy(str->str, native_prefixW, sizeof(native_prefixW));
265         memcpy(str->str + sizeof(native_prefixW)/sizeof(WCHAR), function->name, name_len*sizeof(WCHAR));
266         memcpy(str->str + sizeof(native_prefixW)/sizeof(WCHAR) + name_len, native_suffixW, sizeof(native_suffixW));
267     }else {
268         str = jsstr_alloc_len(function->func_code->source, function->func_code->source_len);
269         if(!str)
270             return E_OUTOFMEMORY;
271     }
272
273     *ret = str;
274     return S_OK;
275 }
276
277 HRESULT Function_invoke(jsdisp_t *func_this, IDispatch *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
278 {
279     FunctionInstance *function;
280
281     TRACE("func %p this %p\n", func_this, jsthis);
282
283     assert(is_class(func_this, JSCLASS_FUNCTION));
284     function = (FunctionInstance*)func_this;
285
286     if(function->value_proc)
287         return invoke_value_proc(function->dispex.ctx, function, jsthis, flags, argc, argv, r);
288
289     if(flags == DISPATCH_CONSTRUCT)
290         return invoke_constructor(function->dispex.ctx, function, argc, argv, r);
291
292     assert(flags == DISPATCH_METHOD);
293     return invoke_source(function->dispex.ctx, function, jsthis, argc, argv, r);
294 }
295
296 static HRESULT Function_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
297         jsval_t *r)
298 {
299     FunctionInstance *This = function_from_vdisp(jsthis);
300
301     TRACE("%p %d\n", This, This->length);
302
303     switch(flags) {
304     case DISPATCH_PROPERTYGET:
305         *r = jsval_number(This->length);
306         break;
307     default:
308         FIXME("unimplemented flags %x\n", flags);
309         return E_NOTIMPL;
310     }
311
312     return S_OK;
313 }
314
315 static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
316         jsval_t *r)
317 {
318     FunctionInstance *function;
319     jsstr_t *str;
320     HRESULT hres;
321
322     TRACE("\n");
323
324     if(!(function = function_this(jsthis)))
325         return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
326
327     hres = function_to_string(function, &str);
328     if(FAILED(hres))
329         return hres;
330
331     if(r)
332         *r = jsval_string(str);
333     else
334         jsstr_release(str);
335     return S_OK;
336 }
337
338 static HRESULT array_to_args(script_ctx_t *ctx, jsdisp_t *arg_array, unsigned *argc, jsval_t **ret)
339 {
340     jsval_t *argv, val;
341     DWORD length, i;
342     HRESULT hres;
343
344     hres = jsdisp_propget_name(arg_array, lengthW, &val);
345     if(FAILED(hres))
346         return hres;
347
348     hres = to_uint32(ctx, val, &length);
349     jsval_release(val);
350     if(FAILED(hres))
351         return hres;
352
353     argv = heap_alloc(length * sizeof(*argv));
354     if(!argv)
355         return E_OUTOFMEMORY;
356
357     for(i=0; i<length; i++) {
358         hres = jsdisp_get_idx(arg_array, i, argv+i);
359         if(hres == DISP_E_UNKNOWNNAME) {
360             argv[i] = jsval_undefined();
361         }else if(FAILED(hres)) {
362             while(i--)
363                 jsval_release(argv[i]);
364             heap_free(argv);
365             return hres;
366         }
367     }
368
369     *argc = length;
370     *ret = argv;
371     return S_OK;
372 }
373
374 static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
375 {
376     FunctionInstance *function;
377     jsval_t *args = NULL;
378     unsigned i, cnt = 0;
379     IDispatch *this_obj = NULL;
380     HRESULT hres = S_OK;
381
382     TRACE("\n");
383
384     if(!(function = function_this(jsthis)))
385         return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
386
387     if(argc) {
388         if(!is_undefined(argv[0]) && !is_null(argv[0])) {
389             hres = to_object(ctx, argv[0], &this_obj);
390             if(FAILED(hres))
391                 return hres;
392         }
393     }
394
395     if(argc >= 2) {
396         jsdisp_t *arg_array = NULL;
397
398         if(is_object_instance(argv[1])) {
399             arg_array = iface_to_jsdisp((IUnknown*)get_object(argv[1]));
400             if(arg_array &&
401                (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
402                 jsdisp_release(arg_array);
403                 arg_array = NULL;
404             }
405         }
406
407         if(arg_array) {
408             hres = array_to_args(ctx, arg_array, &cnt, &args);
409             jsdisp_release(arg_array);
410         }else {
411             FIXME("throw TypeError\n");
412             hres = E_FAIL;
413         }
414     }
415
416     if(SUCCEEDED(hres))
417         hres = call_function(ctx, function, this_obj, cnt, args, r);
418
419     if(this_obj)
420         IDispatch_Release(this_obj);
421     for(i=0; i < cnt; i++)
422         jsval_release(args[i]);
423     heap_free(args);
424     return hres;
425 }
426
427 static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
428         jsval_t *r)
429 {
430     FunctionInstance *function;
431     IDispatch *this_obj = NULL;
432     unsigned cnt = 0;
433     HRESULT hres;
434
435     TRACE("\n");
436
437     if(!(function = function_this(jsthis)))
438         return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
439
440     if(argc) {
441         if(!is_undefined(argv[0]) && !is_null(argv[0])) {
442             hres = to_object(ctx, argv[0], &this_obj);
443             if(FAILED(hres))
444                 return hres;
445         }
446
447         cnt = argc-1;
448     }
449
450     hres = call_function(ctx, function, this_obj, cnt, argv+1, r);
451
452     if(this_obj)
453         IDispatch_Release(this_obj);
454     return hres;
455 }
456
457 HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
458         jsval_t *r)
459 {
460     FunctionInstance *function;
461
462     TRACE("\n");
463
464     if(!is_vclass(jsthis, JSCLASS_FUNCTION)) {
465         ERR("dispex is not a function\n");
466         return E_FAIL;
467     }
468
469     function = (FunctionInstance*)jsthis->u.jsdisp;
470
471     switch(flags) {
472     case DISPATCH_METHOD:
473         assert(function->value_proc != NULL);
474         return invoke_value_proc(ctx, function, NULL, flags, argc, argv, r);
475
476     case DISPATCH_PROPERTYGET: {
477         HRESULT hres;
478         jsstr_t *str;
479
480         hres = function_to_string(function, &str);
481         if(FAILED(hres))
482             return hres;
483
484         *r = jsval_string(str);
485         break;
486     }
487
488     case DISPATCH_CONSTRUCT:
489         assert(function->value_proc != NULL);
490         return invoke_value_proc(ctx, function, NULL, flags, argc, argv, r);
491
492     default:
493         FIXME("not implemented flags %x\n", flags);
494         return E_NOTIMPL;
495     }
496
497     return S_OK;
498 }
499
500 static HRESULT Function_arguments(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
501         unsigned argc, jsval_t *argv, jsval_t *r)
502 {
503     FunctionInstance *function = (FunctionInstance*)jsthis->u.jsdisp;
504     HRESULT hres = S_OK;
505
506     TRACE("\n");
507
508     switch(flags) {
509     case DISPATCH_PROPERTYGET: {
510         *r = function->arguments ? jsval_obj(jsdisp_addref(function->arguments)) : jsval_null();
511         break;
512     }
513     case DISPATCH_PROPERTYPUT:
514         break;
515     default:
516         FIXME("unimplemented flags %x\n", flags);
517         hres = E_NOTIMPL;
518     }
519
520     return hres;
521 }
522
523 static void Function_destructor(jsdisp_t *dispex)
524 {
525     FunctionInstance *This = (FunctionInstance*)dispex;
526
527     if(This->code)
528         release_bytecode(This->code);
529     if(This->scope_chain)
530         scope_release(This->scope_chain);
531     heap_free(This);
532 }
533
534 static const builtin_prop_t Function_props[] = {
535     {applyW,                 Function_apply,                 PROPF_METHOD|2},
536     {argumentsW,             Function_arguments,             0},
537     {callW,                  Function_call,                  PROPF_METHOD|1},
538     {lengthW,                Function_length,                0},
539     {toStringW,              Function_toString,              PROPF_METHOD}
540 };
541
542 static const builtin_info_t Function_info = {
543     JSCLASS_FUNCTION,
544     {NULL, Function_value, 0},
545     sizeof(Function_props)/sizeof(*Function_props),
546     Function_props,
547     Function_destructor,
548     NULL
549 };
550
551 static const builtin_prop_t FunctionInst_props[] = {
552     {argumentsW,             Function_arguments,             0},
553     {lengthW,                Function_length,                0}
554 };
555
556 static const builtin_info_t FunctionInst_info = {
557     JSCLASS_FUNCTION,
558     {NULL, Function_value, 0},
559     sizeof(FunctionInst_props)/sizeof(*FunctionInst_props),
560     FunctionInst_props,
561     Function_destructor,
562     NULL
563 };
564
565 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
566         BOOL funcprot, jsdisp_t *prototype, FunctionInstance **ret)
567 {
568     FunctionInstance *function;
569     HRESULT hres;
570
571     function = heap_alloc_zero(sizeof(FunctionInstance));
572     if(!function)
573         return E_OUTOFMEMORY;
574
575     if(funcprot)
576         hres = init_dispex(&function->dispex, ctx, builtin_info, prototype);
577     else if(builtin_info)
578         hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
579     else
580         hres = init_dispex_from_constr(&function->dispex, ctx, &FunctionInst_info, ctx->function_constr);
581     if(FAILED(hres)) {
582         heap_free(function);
583         return hres;
584     }
585
586     function->flags = flags;
587     function->length = flags & PROPF_ARGMASK;
588
589     *ret = function;
590     return S_OK;
591 }
592
593 static inline HRESULT set_prototype(script_ctx_t *ctx, jsdisp_t *dispex, jsdisp_t *prototype)
594 {
595     return jsdisp_propput_name(dispex, prototypeW, jsval_obj(prototype));
596 }
597
598 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
599         const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
600 {
601     FunctionInstance *function;
602     HRESULT hres;
603
604     hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
605     if(FAILED(hres))
606         return hres;
607
608     if(builtin_info)
609         hres = jsdisp_propput_const(&function->dispex, lengthW, jsval_number(function->length));
610     if(SUCCEEDED(hres))
611         hres = set_prototype(ctx, &function->dispex, prototype);
612     if(FAILED(hres)) {
613         jsdisp_release(&function->dispex);
614         return hres;
615     }
616
617     function->value_proc = value_proc;
618     function->name = name;
619
620     *ret = &function->dispex;
621     return S_OK;
622 }
623
624 static HRESULT set_constructor_prop(script_ctx_t *ctx, jsdisp_t *constr, jsdisp_t *prot)
625 {
626     static const WCHAR constructorW[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
627
628     return jsdisp_propput_dontenum(prot, constructorW, jsval_obj(constr));
629 }
630
631 HRESULT create_builtin_constructor(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
632         const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
633 {
634     jsdisp_t *constr;
635     HRESULT hres;
636
637     hres = create_builtin_function(ctx, value_proc, name, builtin_info, flags, prototype, &constr);
638     if(FAILED(hres))
639         return hres;
640
641     hres = set_constructor_prop(ctx, constr, prototype);
642     if(FAILED(hres)) {
643         jsdisp_release(constr);
644         return hres;
645     }
646
647     *ret = constr;
648     return S_OK;
649 }
650
651 HRESULT create_source_function(script_ctx_t *ctx, bytecode_t *code, function_code_t *func_code,
652         scope_chain_t *scope_chain, jsdisp_t **ret)
653 {
654     FunctionInstance *function;
655     jsdisp_t *prototype;
656     HRESULT hres;
657
658     hres = create_object(ctx, NULL, &prototype);
659     if(FAILED(hres))
660         return hres;
661
662     hres = create_function(ctx, NULL, PROPF_CONSTR, FALSE, NULL, &function);
663     if(SUCCEEDED(hres)) {
664         hres = set_prototype(ctx, &function->dispex, prototype);
665         if(SUCCEEDED(hres))
666             hres = set_constructor_prop(ctx, &function->dispex, prototype);
667         if(FAILED(hres))
668             jsdisp_release(&function->dispex);
669     }
670     jsdisp_release(prototype);
671     if(FAILED(hres))
672         return hres;
673
674     if(scope_chain) {
675         scope_addref(scope_chain);
676         function->scope_chain = scope_chain;
677     }
678
679     bytecode_addref(code);
680     function->code = code;
681     function->func_code = func_code;
682     function->length = function->func_code->param_cnt;
683
684     *ret = &function->dispex;
685     return S_OK;
686 }
687
688 static HRESULT construct_function(script_ctx_t *ctx, unsigned argc, jsval_t *argv, IDispatch **ret)
689 {
690     WCHAR *str = NULL, *ptr;
691     DWORD len = 0, l;
692     bytecode_t *code;
693     jsdisp_t *function;
694     jsstr_t **params = NULL;
695     int i=0, j=0;
696     HRESULT hres = S_OK;
697
698     static const WCHAR function_anonymousW[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
699     static const WCHAR function_beginW[] = {')',' ','{','\n'};
700     static const WCHAR function_endW[] = {'\n','}',0};
701
702     if(argc) {
703         params = heap_alloc(argc*sizeof(*params));
704         if(!params)
705             return E_OUTOFMEMORY;
706
707         if(argc > 2)
708             len = (argc-2)*2; /* separating commas */
709         for(i=0; i < argc; i++) {
710             hres = to_string(ctx, argv[i], params+i);
711             if(FAILED(hres))
712                 break;
713             len += jsstr_length(params[i]);
714         }
715     }
716
717     if(SUCCEEDED(hres)) {
718         len += (sizeof(function_anonymousW) + sizeof(function_beginW) + sizeof(function_endW)) / sizeof(WCHAR);
719         str = heap_alloc(len*sizeof(WCHAR));
720         if(str) {
721             memcpy(str, function_anonymousW, sizeof(function_anonymousW));
722             ptr = str + sizeof(function_anonymousW)/sizeof(WCHAR);
723             if(argc > 1) {
724                 while(1) {
725                     l = jsstr_length(params[j]);
726                     memcpy(ptr, params[j]->str, l*sizeof(WCHAR));
727                     ptr += l;
728                     if(++j == argc-1)
729                         break;
730                     *ptr++ = ',';
731                     *ptr++ = ' ';
732                 }
733             }
734             memcpy(ptr, function_beginW, sizeof(function_beginW));
735             ptr += sizeof(function_beginW)/sizeof(WCHAR);
736             if(argc) {
737                 l = jsstr_length(params[argc-1]);
738                 memcpy(ptr, params[argc-1]->str, l*sizeof(WCHAR));
739                 ptr += l;
740             }
741             memcpy(ptr, function_endW, sizeof(function_endW));
742
743             TRACE("%s\n", debugstr_w(str));
744         }else {
745             hres = E_OUTOFMEMORY;
746         }
747     }
748
749     while(--i >= 0)
750         jsstr_release(params[i]);
751     heap_free(params);
752     if(FAILED(hres))
753         return hres;
754
755     hres = compile_script(ctx, str, NULL, NULL, FALSE, FALSE, &code);
756     heap_free(str);
757     if(FAILED(hres))
758         return hres;
759
760     if(code->global_code.func_cnt != 1 || code->global_code.var_cnt) {
761         ERR("Invalid parser result!\n");
762         release_bytecode(code);
763         return E_UNEXPECTED;
764     }
765
766     hres = create_source_function(ctx, code, code->global_code.funcs, NULL, &function);
767     release_bytecode(code);
768     if(FAILED(hres))
769         return hres;
770
771     *ret = to_disp(function);
772     return S_OK;
773 }
774
775 static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
776         jsval_t *r)
777 {
778     HRESULT hres;
779
780     TRACE("\n");
781
782     switch(flags) {
783     case DISPATCH_CONSTRUCT: {
784         IDispatch *ret;
785
786         hres = construct_function(ctx, argc, argv, &ret);
787         if(FAILED(hres))
788             return hres;
789
790         *r = jsval_disp(ret);
791         break;
792     }
793     default:
794         FIXME("unimplemented flags %x\n", flags);
795         return E_NOTIMPL;
796     }
797
798     return S_OK;
799 }
800
801 static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
802         jsval_t *r)
803 {
804     FIXME("\n");
805     return E_NOTIMPL;
806 }
807
808 HRESULT init_function_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
809 {
810     FunctionInstance *prot, *constr;
811     HRESULT hres;
812
813     static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
814
815     hres = create_function(ctx, &Function_info, PROPF_CONSTR, TRUE, object_prototype, &prot);
816     if(FAILED(hres))
817         return hres;
818
819     prot->value_proc = FunctionProt_value;
820     prot->name = prototypeW;
821
822     hres = create_function(ctx, &FunctionInst_info, PROPF_CONSTR|1, TRUE, &prot->dispex, &constr);
823     if(SUCCEEDED(hres)) {
824         constr->value_proc = FunctionConstr_value;
825         constr->name = FunctionW;
826         hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
827         if(SUCCEEDED(hres))
828             hres = set_constructor_prop(ctx, &constr->dispex, &prot->dispex);
829         if(FAILED(hres))
830             jsdisp_release(&constr->dispex);
831     }
832     jsdisp_release(&prot->dispex);
833     if(FAILED(hres))
834         return hres;
835
836     ctx->function_constr = &constr->dispex;
837     return S_OK;
838 }