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