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