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