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