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