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