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