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