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