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