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