jscript: Update German translation.
[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 } 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
57 static IDispatch *get_this(DISPPARAMS *dp)
58 {
59     DWORD i;
60
61     for(i=0; i < dp->cNamedArgs; i++) {
62         if(dp->rgdispidNamedArgs[i] == DISPID_THIS) {
63             if(V_VT(dp->rgvarg+i) == VT_DISPATCH)
64                 return V_DISPATCH(dp->rgvarg+i);
65
66             WARN("This is not VT_DISPATCH\n");
67             return NULL;
68         }
69     }
70
71     TRACE("no this passed\n");
72     return NULL;
73 }
74
75 static HRESULT init_parameters(DispatchEx *var_disp, FunctionInstance *function, DISPPARAMS *dp,
76         jsexcept_t *ei, IServiceProvider *caller)
77 {
78     parameter_t *param;
79     VARIANT var_empty;
80     DWORD cargs, i=0;
81     HRESULT hres;
82
83     V_VT(&var_empty) = VT_EMPTY;
84     cargs = arg_cnt(dp);
85
86     for(param = function->parameters; param; param = param->next) {
87         hres = jsdisp_propput_name(var_disp, param->identifier,
88                 i < cargs ? get_arg(dp,i) : &var_empty, ei, caller);
89         if(FAILED(hres))
90             return hres;
91
92         i++;
93     }
94
95     return S_OK;
96 }
97
98 static HRESULT Arguments_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
99         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
100 {
101     FIXME("\n");
102     return E_NOTIMPL;
103 }
104
105 static const builtin_info_t Arguments_info = {
106     JSCLASS_ARGUMENTS,
107     {NULL, Arguments_value, 0},
108     0, NULL,
109     NULL,
110     NULL
111 };
112
113 static HRESULT create_arguments(script_ctx_t *ctx, IDispatch *calee, DISPPARAMS *dp,
114         jsexcept_t *ei, IServiceProvider *caller, DispatchEx **ret)
115 {
116     DispatchEx *args;
117     VARIANT var;
118     DWORD i;
119     HRESULT hres;
120
121     static const WCHAR caleeW[] = {'c','a','l','l','e','e',0};
122
123     args = heap_alloc_zero(sizeof(DispatchEx));
124     if(!args)
125         return E_OUTOFMEMORY;
126
127     hres = init_dispex_from_constr(args, ctx, &Arguments_info, ctx->object_constr);
128     if(FAILED(hres)) {
129         heap_free(args);
130         return hres;
131     }
132
133     for(i=0; i < arg_cnt(dp); i++) {
134         hres = jsdisp_propput_idx(args, i, get_arg(dp,i), ei, caller);
135         if(FAILED(hres))
136             break;
137     }
138
139     if(SUCCEEDED(hres)) {
140         V_VT(&var) = VT_I4;
141         V_I4(&var) = arg_cnt(dp);
142         hres = jsdisp_propput_name(args, lengthW, &var, ei, caller);
143
144         if(SUCCEEDED(hres)) {
145             V_VT(&var) = VT_DISPATCH;
146             V_DISPATCH(&var) = calee;
147             hres = jsdisp_propput_name(args, caleeW, &var, ei, caller);
148         }
149     }
150
151     if(FAILED(hres)) {
152         jsdisp_release(args);
153         return hres;
154     }
155
156     *ret = args;
157     return S_OK;
158 }
159
160 static HRESULT create_var_disp(script_ctx_t *ctx, FunctionInstance *function, DISPPARAMS *dp, jsexcept_t *ei,
161                                IServiceProvider *caller, DispatchEx **ret)
162 {
163     DispatchEx *var_disp, *arg_disp;
164     HRESULT hres;
165
166     static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
167
168     hres = create_dispex(ctx, NULL, NULL, &var_disp);
169     if(FAILED(hres))
170         return hres;
171
172     hres = create_arguments(ctx, (IDispatch*)_IDispatchEx_(&function->dispex),
173             dp, ei, caller, &arg_disp);
174     if(SUCCEEDED(hres)) {
175         VARIANT var;
176
177         V_VT(&var) = VT_DISPATCH;
178         V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(arg_disp);
179         hres = jsdisp_propput_name(var_disp, argumentsW, &var, ei, caller);
180         jsdisp_release(arg_disp);
181     }
182
183     if(SUCCEEDED(hres))
184         hres = init_parameters(var_disp, function, dp, ei, caller);
185     if(FAILED(hres)) {
186         jsdisp_release(var_disp);
187         return hres;
188     }
189
190     *ret = var_disp;
191     return S_OK;
192 }
193
194 static HRESULT invoke_source(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *dp,
195         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
196 {
197     DispatchEx *var_disp;
198     exec_ctx_t *exec_ctx;
199     scope_chain_t *scope;
200     HRESULT hres;
201
202     if(!function->source) {
203         FIXME("no source\n");
204         return E_FAIL;
205     }
206
207     hres = create_var_disp(ctx, function, dp, ei, caller, &var_disp);
208     if(FAILED(hres))
209         return hres;
210
211     hres = scope_push(function->scope_chain, var_disp, &scope);
212     if(SUCCEEDED(hres)) {
213         hres = create_exec_ctx(this_obj, var_disp, scope, &exec_ctx);
214         scope_release(scope);
215     }
216     if(FAILED(hres))
217         return hres;
218
219     hres = exec_source(exec_ctx, function->parser, function->source, ei, retv);
220     exec_release(exec_ctx);
221
222     return hres;
223 }
224
225 static HRESULT invoke_function(script_ctx_t *ctx, FunctionInstance *function, DISPPARAMS *dp,
226         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
227 {
228     IDispatch *this_obj;
229
230     if(!(this_obj = get_this(dp)))
231         this_obj = (IDispatch*)_IDispatchEx_(ctx->script_disp);
232
233     return invoke_source(ctx, function, this_obj, dp, retv, ei, caller);
234 }
235
236 static HRESULT invoke_constructor(script_ctx_t *ctx, FunctionInstance *function, DISPPARAMS *dp,
237         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
238 {
239     DispatchEx *this_obj;
240     HRESULT hres;
241
242     hres = create_object(ctx, &function->dispex, &this_obj);
243     if(FAILED(hres))
244         return hres;
245
246     hres = invoke_source(ctx, function, (IDispatch*)_IDispatchEx_(this_obj), dp, retv, ei, caller);
247     if(FAILED(hres)) {
248         jsdisp_release(this_obj);
249         return hres;
250     }
251
252     V_VT(retv) = VT_DISPATCH;
253     V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(this_obj);
254     return S_OK;
255 }
256
257 static HRESULT invoke_value_proc(script_ctx_t *ctx, FunctionInstance *function, WORD flags, DISPPARAMS *dp,
258         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
259 {
260     IDispatch *this_disp;
261     vdisp_t vthis;
262     HRESULT hres;
263
264     this_disp = get_this(dp);
265     if(this_disp)
266         set_disp(&vthis, this_disp);
267     else
268         set_jsdisp(&vthis, ctx->script_disp);
269
270     hres = function->value_proc(ctx, &vthis, flags, dp, retv, ei, caller);
271
272     vdisp_release(&vthis);
273     return hres;
274 }
275
276 static HRESULT call_function(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *args,
277         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
278 {
279     HRESULT hres;
280
281     if(function->value_proc) {
282         vdisp_t vthis;
283
284         if(this_obj)
285             set_disp(&vthis, this_obj);
286         else
287             set_jsdisp(&vthis, ctx->script_disp);
288
289         hres = function->value_proc(ctx, &vthis, DISPATCH_METHOD, args, retv, ei, caller);
290         vdisp_release(&vthis);
291     }else {
292         hres = invoke_source(ctx, function, this_obj ? this_obj : (IDispatch*)_IDispatchEx_(ctx->script_disp),
293                 args, retv, ei, caller);
294     }
295
296     return hres;
297 }
298
299 static HRESULT function_to_string(FunctionInstance *function, BSTR *ret)
300 {
301     BSTR str;
302
303     static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
304     static const WCHAR native_suffixW[] =
305         {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
306
307     if(function->value_proc) {
308         DWORD name_len;
309
310         name_len = strlenW(function->name);
311         str = SysAllocStringLen(NULL, sizeof(native_prefixW) + name_len*sizeof(WCHAR) + sizeof(native_suffixW));
312         if(!str)
313             return E_OUTOFMEMORY;
314
315         memcpy(str, native_prefixW, sizeof(native_prefixW));
316         memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR), function->name, name_len*sizeof(WCHAR));
317         memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR) + name_len, native_suffixW, sizeof(native_suffixW));
318     }else {
319         str = SysAllocStringLen(function->src_str, function->src_len);
320         if(!str)
321             return E_OUTOFMEMORY;
322     }
323
324     *ret = str;
325     return S_OK;
326 }
327
328 static HRESULT Function_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
329         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
330 {
331     FunctionInstance *This = function_from_vdisp(jsthis);
332
333     TRACE("%p %d\n", This, This->length);
334
335     switch(flags) {
336     case DISPATCH_PROPERTYGET:
337         V_VT(retv) = VT_I4;
338         V_I4(retv) = This->length;
339         break;
340     default:
341         FIXME("unimplemented flags %x\n", flags);
342         return E_NOTIMPL;
343     }
344
345     return S_OK;
346 }
347
348 static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
349         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
350 {
351     FunctionInstance *function;
352     BSTR str;
353     HRESULT hres;
354
355     TRACE("\n");
356
357     if(!(function = function_this(jsthis)))
358         return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
359
360     hres = function_to_string(function, &str);
361     if(FAILED(hres))
362         return hres;
363
364     if(retv) {
365         V_VT(retv) = VT_BSTR;
366         V_BSTR(retv) = str;
367     }else {
368         SysFreeString(str);
369     }
370     return S_OK;
371 }
372
373 static HRESULT array_to_args(script_ctx_t *ctx, DispatchEx *arg_array, jsexcept_t *ei, IServiceProvider *caller,
374         DISPPARAMS *args)
375 {
376     VARIANT var, *argv;
377     DWORD length, i;
378     HRESULT hres;
379
380     hres = jsdisp_propget_name(arg_array, lengthW, &var, ei, NULL/*FIXME*/);
381     if(FAILED(hres))
382         return hres;
383
384     hres = to_uint32(ctx, &var, ei, &length);
385     VariantClear(&var);
386     if(FAILED(hres))
387         return hres;
388
389     argv = heap_alloc(length * sizeof(VARIANT));
390     if(!argv)
391         return E_OUTOFMEMORY;
392
393     for(i=0; i<length; i++) {
394         hres = jsdisp_propget_idx(arg_array, i, argv+i, ei, caller);
395         if(FAILED(hres)) {
396             while(i--)
397                 VariantClear(argv+i);
398             heap_free(argv);
399             return hres;
400         }
401     }
402
403     args->cArgs = length;
404     args->rgvarg = argv;
405     return S_OK;
406 }
407
408 static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
409         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
410 {
411     FunctionInstance *function;
412     DISPPARAMS args = {NULL,NULL,0,0};
413     DWORD argc, i;
414     IDispatch *this_obj = NULL;
415     HRESULT hres = S_OK;
416
417     TRACE("\n");
418
419     if(!(function = function_this(jsthis)))
420         return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
421
422     argc = arg_cnt(dp);
423     if(argc) {
424         hres = to_object(ctx, get_arg(dp,0), &this_obj);
425         if(FAILED(hres))
426             return hres;
427     }
428
429     if(argc >= 2) {
430         DispatchEx *arg_array = NULL;
431
432         if(V_VT(get_arg(dp,1)) == VT_DISPATCH) {
433             arg_array = iface_to_jsdisp((IUnknown*)V_DISPATCH(get_arg(dp,1)));
434             if(arg_array && (
435                 !is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
436                 jsdisp_release(arg_array);
437                 arg_array = NULL;
438             }
439         }
440
441         if(arg_array) {
442             hres = array_to_args(ctx, arg_array, ei, caller, &args);
443             jsdisp_release(arg_array);
444         }else {
445             FIXME("throw TypeError\n");
446             hres = E_FAIL;
447         }
448     }
449
450     hres = call_function(ctx, function, this_obj, &args, retv, ei, caller);
451
452     if(this_obj)
453         IDispatch_Release(this_obj);
454     for(i=0; i<args.cArgs; i++)
455         VariantClear(args.rgvarg+i);
456     heap_free(args.rgvarg);
457     return hres;
458 }
459
460 static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
461         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
462 {
463     FunctionInstance *function;
464     DISPPARAMS args = {NULL,NULL,0,0};
465     IDispatch *this_obj = NULL;
466     DWORD argc;
467     HRESULT hres;
468
469     TRACE("\n");
470
471     if(!(function = function_this(jsthis)))
472         return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
473
474     argc = arg_cnt(dp);
475     if(argc) {
476         hres = to_object(ctx, get_arg(dp,0), &this_obj);
477         if(FAILED(hres))
478             return hres;
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, flags, dp, retv, ei, caller);
510
511         return invoke_function(ctx, function, 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, 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 void Function_destructor(DispatchEx *dispex)
541 {
542     FunctionInstance *This = (FunctionInstance*)dispex;
543
544     if(This->parser)
545         parser_release(This->parser);
546     if(This->scope_chain)
547         scope_release(This->scope_chain);
548     heap_free(This);
549 }
550
551 static const builtin_prop_t Function_props[] = {
552     {applyW,                 Function_apply,                 PROPF_METHOD|2},
553     {callW,                  Function_call,                  PROPF_METHOD|1},
554     {lengthW,                Function_length,                0},
555     {toStringW,              Function_toString,              PROPF_METHOD}
556 };
557
558 static const builtin_info_t Function_info = {
559     JSCLASS_FUNCTION,
560     {NULL, Function_value, 0},
561     sizeof(Function_props)/sizeof(*Function_props),
562     Function_props,
563     Function_destructor,
564     NULL
565 };
566
567 static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
568         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
569 {
570     FIXME("\n");
571     return E_NOTIMPL;
572 }
573
574 static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
575         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
576 {
577     FIXME("\n");
578     return E_NOTIMPL;
579 }
580
581 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
582         BOOL funcprot, DispatchEx *prototype, FunctionInstance **ret)
583 {
584     FunctionInstance *function;
585     HRESULT hres;
586
587     function = heap_alloc_zero(sizeof(FunctionInstance));
588     if(!function)
589         return E_OUTOFMEMORY;
590
591     if(funcprot)
592         hres = init_dispex(&function->dispex, ctx, &Function_info, prototype);
593     else if(builtin_info)
594         hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
595     else
596         hres = init_dispex_from_constr(&function->dispex, ctx, &Function_info, ctx->function_constr);
597     if(FAILED(hres))
598         return hres;
599
600     function->flags = flags;
601     function->length = flags & PROPF_ARGMASK;
602
603     *ret = function;
604     return S_OK;
605 }
606
607 static HRESULT set_prototype(script_ctx_t *ctx, DispatchEx *dispex, DispatchEx *prototype)
608 {
609     jsexcept_t jsexcept;
610     VARIANT var;
611
612     V_VT(&var) = VT_DISPATCH;
613     V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(prototype);
614     memset(&jsexcept, 0, sizeof(jsexcept));
615
616     return jsdisp_propput_name(dispex, prototypeW, &var, &jsexcept, NULL/*FIXME*/);
617 }
618
619 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
620         const builtin_info_t *builtin_info, DWORD flags, DispatchEx *prototype, DispatchEx **ret)
621 {
622     FunctionInstance *function;
623     HRESULT hres;
624
625     hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
626     if(FAILED(hres))
627         return hres;
628
629     hres = set_prototype(ctx, &function->dispex, prototype);
630     if(FAILED(hres)) {
631         jsdisp_release(&function->dispex);
632         return hres;
633     }
634
635     function->value_proc = value_proc;
636     function->name = name;
637
638     *ret = &function->dispex;
639     return S_OK;
640 }
641
642 HRESULT create_source_function(parser_ctx_t *ctx, parameter_t *parameters, source_elements_t *source,
643         scope_chain_t *scope_chain, const WCHAR *src_str, DWORD src_len, DispatchEx **ret)
644 {
645     FunctionInstance *function;
646     DispatchEx *prototype;
647     parameter_t *iter;
648     DWORD length = 0;
649     HRESULT hres;
650
651     hres = create_object(ctx->script, NULL, &prototype);
652     if(FAILED(hres))
653         return hres;
654
655     hres = create_function(ctx->script, NULL, PROPF_CONSTR, FALSE, NULL, &function);
656     if(SUCCEEDED(hres)) {
657         hres = set_prototype(ctx->script, &function->dispex, prototype);
658         if(FAILED(hres))
659             jsdisp_release(&function->dispex);
660     }
661     jsdisp_release(prototype);
662     if(FAILED(hres))
663         return hres;
664
665     function->source = source;
666     function->parameters = parameters;
667
668     if(scope_chain) {
669         scope_addref(scope_chain);
670         function->scope_chain = scope_chain;
671     }
672
673     parser_addref(ctx);
674     function->parser = ctx;
675
676     for(iter = parameters; iter; iter = iter->next)
677         length++;
678     function->length = length;
679
680     function->src_str = src_str;
681     function->src_len = src_len;
682
683     *ret = &function->dispex;
684     return S_OK;
685 }
686
687 HRESULT init_function_constr(script_ctx_t *ctx, DispatchEx *object_prototype)
688 {
689     FunctionInstance *prot, *constr;
690     HRESULT hres;
691
692     static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
693
694     hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, object_prototype, &prot);
695     if(FAILED(hres))
696         return hres;
697
698     prot->value_proc = FunctionProt_value;
699     prot->name = prototypeW;
700
701     hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, &prot->dispex, &constr);
702     if(SUCCEEDED(hres)) {
703         constr->value_proc = FunctionConstr_value;
704         constr->name = FunctionW;
705         hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
706         if(FAILED(hres))
707             jsdisp_release(&constr->dispex);
708     }
709     jsdisp_release(&prot->dispex);
710     if(FAILED(hres))
711         return hres;
712
713     ctx->function_constr = &constr->dispex;
714     return S_OK;
715 }