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