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