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