jscript: Use script LCID in *disp_propget* functions.
[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, IDispatch *calee, 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     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, lcid, 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, lcid, &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, lcid, &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(FunctionInstance *function, LCID lcid, 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(function->dispex.ctx, NULL, NULL, &var_disp);
159     if(FAILED(hres))
160         return hres;
161
162     hres = create_arguments(function->dispex.ctx, (IDispatch*)_IDispatchEx_(&function->dispex),
163             lcid, 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, lcid, &var, ei, caller);
170         jsdisp_release(arg_disp);
171     }
172
173     if(SUCCEEDED(hres))
174         hres = init_parameters(var_disp, function, lcid, 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(FunctionInstance *function, IDispatch *this_obj, LCID lcid, 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(function, lcid, 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(FunctionInstance *function, LCID lcid, 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_(function->dispex.ctx->script_disp);
222
223     return invoke_source(function, this_obj, lcid, dp, retv, ei, caller);
224 }
225
226 static HRESULT invoke_constructor(FunctionInstance *function, LCID lcid, DISPPARAMS *dp,
227         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
228 {
229     DispatchEx *this_obj;
230     HRESULT hres;
231
232     hres = create_object(function->dispex.ctx, &function->dispex, &this_obj);
233     if(FAILED(hres))
234         return hres;
235
236     hres = invoke_source(function, (IDispatch*)_IDispatchEx_(this_obj), lcid, 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(FunctionInstance *function, LCID lcid, 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(this_obj ? this_obj : function->dispex.ctx->script_disp, lcid,
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(FunctionInstance *function, IDispatch *this_obj, LCID lcid, 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(jsthis ? jsthis : function->dispex.ctx->script_disp, lcid,
281                 DISPATCH_METHOD, args, retv, ei, caller);
282
283         if(jsthis)
284             jsdisp_release(jsthis);
285     }else {
286         hres = invoke_source(function,
287                 this_obj ? this_obj : (IDispatch*)_IDispatchEx_(function->dispex.ctx->script_disp),
288                 lcid, args, retv, ei, caller);
289     }
290
291     return hres;
292 }
293
294 static HRESULT function_to_string(FunctionInstance *function, BSTR *ret)
295 {
296     BSTR str;
297
298     static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
299     static const WCHAR native_suffixW[] =
300         {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
301
302     if(function->value_proc) {
303         DWORD name_len;
304
305         name_len = strlenW(function->name);
306         str = SysAllocStringLen(NULL, sizeof(native_prefixW) + name_len*sizeof(WCHAR) + sizeof(native_suffixW));
307         if(!str)
308             return E_OUTOFMEMORY;
309
310         memcpy(str, native_prefixW, sizeof(native_prefixW));
311         memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR), function->name, name_len*sizeof(WCHAR));
312         memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR) + name_len, native_suffixW, sizeof(native_suffixW));
313     }else {
314         str = SysAllocStringLen(function->src_str, function->src_len);
315         if(!str)
316             return E_OUTOFMEMORY;
317     }
318
319     *ret = str;
320     return S_OK;
321 }
322
323 static HRESULT Function_length(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
324         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
325 {
326     FunctionInstance *This = (FunctionInstance*)dispex;
327
328     TRACE("%p %d\n", This, This->length);
329
330     switch(flags) {
331     case DISPATCH_PROPERTYGET:
332         V_VT(retv) = VT_I4;
333         V_I4(retv) = This->length;
334         break;
335     default:
336         FIXME("unimplemented flags %x\n", flags);
337         return E_NOTIMPL;
338     }
339
340     return S_OK;
341 }
342
343 static HRESULT Function_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
344         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
345 {
346     FunctionInstance *function;
347     BSTR str;
348     HRESULT hres;
349
350     TRACE("\n");
351
352     if(!is_class(dispex, JSCLASS_FUNCTION))
353         return throw_type_error(dispex->ctx, ei, IDS_NOT_FUNC, NULL);
354
355     function = (FunctionInstance*)dispex;
356
357     hres = function_to_string(function, &str);
358     if(FAILED(hres))
359         return hres;
360
361     if(retv) {
362         V_VT(retv) = VT_BSTR;
363         V_BSTR(retv) = str;
364     }else {
365         SysFreeString(str);
366     }
367     return S_OK;
368 }
369
370 static HRESULT array_to_args(DispatchEx *arg_array, LCID lcid, jsexcept_t *ei, IServiceProvider *caller,
371         DISPPARAMS *args)
372 {
373     VARIANT var, *argv;
374     DWORD length, i;
375     HRESULT hres;
376
377     hres = jsdisp_propget_name(arg_array, lengthW, &var, ei, NULL/*FIXME*/);
378     if(FAILED(hres))
379         return hres;
380
381     hres = to_uint32(arg_array->ctx, &var, ei, &length);
382     VariantClear(&var);
383     if(FAILED(hres))
384         return hres;
385
386     argv = heap_alloc(length * sizeof(VARIANT));
387     if(!argv)
388         return E_OUTOFMEMORY;
389
390     for(i=0; i<length; i++) {
391         hres = jsdisp_propget_idx(arg_array, i, argv+i, ei, caller);
392         if(FAILED(hres)) {
393             while(i--)
394                 VariantClear(argv+i);
395             heap_free(argv);
396             return hres;
397         }
398     }
399
400     args->cArgs = length;
401     args->rgvarg = argv;
402     return S_OK;
403 }
404
405 static HRESULT Function_apply(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
406         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
407 {
408     FunctionInstance *function;
409     DISPPARAMS args = {NULL,NULL,0,0};
410     DWORD argc, i;
411     IDispatch *this_obj = NULL;
412     HRESULT hres = S_OK;
413
414     TRACE("\n");
415
416     if(!is_class(dispex, JSCLASS_FUNCTION)) {
417         FIXME("dispex is not a function\n");
418         return E_FAIL;
419     }
420
421     function = (FunctionInstance*)dispex;
422     argc = arg_cnt(dp);
423
424     if(argc) {
425         hres = to_object(dispex->ctx, get_arg(dp,0), &this_obj);
426         if(FAILED(hres))
427             return hres;
428     }
429
430     if(argc >= 2) {
431         DispatchEx *arg_array = NULL;
432
433         if(V_VT(get_arg(dp,1)) == VT_DISPATCH) {
434             arg_array = iface_to_jsdisp((IUnknown*)V_DISPATCH(get_arg(dp,1)));
435             if(arg_array && (
436                 !is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
437                 jsdisp_release(arg_array);
438                 arg_array = NULL;
439             }
440         }
441
442         if(arg_array) {
443             hres = array_to_args(arg_array, lcid, ei, caller, &args);
444             jsdisp_release(arg_array);
445         }else {
446             FIXME("throw TypeError\n");
447             hres = E_FAIL;
448         }
449     }
450
451     hres = call_function(function, this_obj, lcid, &args, retv, ei, caller);
452
453     if(this_obj)
454         IDispatch_Release(this_obj);
455     for(i=0; i<args.cArgs; i++)
456         VariantClear(args.rgvarg+i);
457     heap_free(args.rgvarg);
458     return hres;
459 }
460
461 static HRESULT Function_call(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
462         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
463 {
464     FunctionInstance *function;
465     DISPPARAMS args = {NULL,NULL,0,0};
466     IDispatch *this_obj = NULL;
467     DWORD argc;
468     HRESULT hres;
469
470     TRACE("\n");
471
472     if(!is_class(dispex, JSCLASS_FUNCTION)) {
473         FIXME("dispex is not a function\n");
474         return E_FAIL;
475     }
476
477     function = (FunctionInstance*)dispex;
478     argc = arg_cnt(dp);
479
480     if(argc) {
481         hres = to_object(dispex->ctx, get_arg(dp,0), &this_obj);
482         if(FAILED(hres))
483             return hres;
484         args.cArgs = argc-1;
485     }
486
487     if(args.cArgs)
488         args.rgvarg = dp->rgvarg + dp->cArgs - args.cArgs-1;
489
490     hres = call_function(function, this_obj, lcid, &args, retv, ei, caller);
491
492     if(this_obj)
493         IDispatch_Release(this_obj);
494     return hres;
495 }
496
497 HRESULT Function_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
498         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
499 {
500     FunctionInstance *function;
501
502     TRACE("\n");
503
504     if(dispex->builtin_info->class != JSCLASS_FUNCTION) {
505         ERR("dispex is not a function\n");
506         return E_FAIL;
507     }
508
509     function = (FunctionInstance*)dispex;
510
511     switch(flags) {
512     case DISPATCH_METHOD:
513         if(function->value_proc)
514             return invoke_value_proc(function, lcid, flags, dp, retv, ei, caller);
515
516         return invoke_function(function, lcid, dp, retv, ei, caller);
517
518     case DISPATCH_PROPERTYGET: {
519         HRESULT hres;
520         BSTR str;
521
522         hres = function_to_string(function, &str);
523         if(FAILED(hres))
524             return hres;
525
526         V_VT(retv) = VT_BSTR;
527         V_BSTR(retv) = str;
528         break;
529     }
530
531     case DISPATCH_CONSTRUCT:
532         if(function->value_proc)
533             return invoke_value_proc(function, lcid, flags, dp, retv, ei, caller);
534
535         return invoke_constructor(function, lcid, dp, retv, ei, caller);
536
537     default:
538         FIXME("not implemented flags %x\n", flags);
539         return E_NOTIMPL;
540     }
541
542     return S_OK;
543 }
544
545 static void Function_destructor(DispatchEx *dispex)
546 {
547     FunctionInstance *This = (FunctionInstance*)dispex;
548
549     if(This->parser)
550         parser_release(This->parser);
551     if(This->scope_chain)
552         scope_release(This->scope_chain);
553     heap_free(This);
554 }
555
556 static const builtin_prop_t Function_props[] = {
557     {applyW,                 Function_apply,                 PROPF_METHOD|2},
558     {callW,                  Function_call,                  PROPF_METHOD|1},
559     {lengthW,                Function_length,                0},
560     {toStringW,              Function_toString,              PROPF_METHOD}
561 };
562
563 static const builtin_info_t Function_info = {
564     JSCLASS_FUNCTION,
565     {NULL, Function_value, 0},
566     sizeof(Function_props)/sizeof(*Function_props),
567     Function_props,
568     Function_destructor,
569     NULL
570 };
571
572 static HRESULT FunctionConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
573         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
574 {
575     FIXME("\n");
576     return E_NOTIMPL;
577 }
578
579 static HRESULT FunctionProt_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
580         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
581 {
582     FIXME("\n");
583     return E_NOTIMPL;
584 }
585
586 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
587         BOOL funcprot, DispatchEx *prototype, FunctionInstance **ret)
588 {
589     FunctionInstance *function;
590     HRESULT hres;
591
592     function = heap_alloc_zero(sizeof(FunctionInstance));
593     if(!function)
594         return E_OUTOFMEMORY;
595
596     if(funcprot)
597         hres = init_dispex(&function->dispex, ctx, &Function_info, prototype);
598     else if(builtin_info)
599         hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
600     else
601         hres = init_dispex_from_constr(&function->dispex, ctx, &Function_info, ctx->function_constr);
602     if(FAILED(hres))
603         return hres;
604
605     function->flags = flags;
606     function->length = flags & PROPF_ARGMASK;
607
608     *ret = function;
609     return S_OK;
610 }
611
612 static HRESULT set_prototype(script_ctx_t *ctx, DispatchEx *dispex, DispatchEx *prototype)
613 {
614     jsexcept_t jsexcept;
615     VARIANT var;
616
617     V_VT(&var) = VT_DISPATCH;
618     V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(prototype);
619     memset(&jsexcept, 0, sizeof(jsexcept));
620
621     return jsdisp_propput_name(dispex, prototypeW, ctx->lcid, &var, &jsexcept, NULL/*FIXME*/);
622 }
623
624 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
625         const builtin_info_t *builtin_info, DWORD flags, DispatchEx *prototype, DispatchEx **ret)
626 {
627     FunctionInstance *function;
628     HRESULT hres;
629
630     hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
631     if(FAILED(hres))
632         return hres;
633
634     hres = set_prototype(ctx, &function->dispex, prototype);
635     if(FAILED(hres)) {
636         jsdisp_release(&function->dispex);
637         return hres;
638     }
639
640     function->value_proc = value_proc;
641     function->name = name;
642
643     *ret = &function->dispex;
644     return S_OK;
645 }
646
647 HRESULT create_source_function(parser_ctx_t *ctx, parameter_t *parameters, source_elements_t *source,
648         scope_chain_t *scope_chain, const WCHAR *src_str, DWORD src_len, DispatchEx **ret)
649 {
650     FunctionInstance *function;
651     DispatchEx *prototype;
652     parameter_t *iter;
653     DWORD length = 0;
654     HRESULT hres;
655
656     hres = create_object(ctx->script, NULL, &prototype);
657     if(FAILED(hres))
658         return hres;
659
660     hres = create_function(ctx->script, NULL, PROPF_CONSTR, FALSE, NULL, &function);
661     if(SUCCEEDED(hres)) {
662         hres = set_prototype(ctx->script, &function->dispex, prototype);
663         if(FAILED(hres))
664             jsdisp_release(&function->dispex);
665     }
666     jsdisp_release(prototype);
667     if(FAILED(hres))
668         return hres;
669
670     function->source = source;
671     function->parameters = parameters;
672
673     if(scope_chain) {
674         scope_addref(scope_chain);
675         function->scope_chain = scope_chain;
676     }
677
678     parser_addref(ctx);
679     function->parser = ctx;
680
681     for(iter = parameters; iter; iter = iter->next)
682         length++;
683     function->length = length;
684
685     function->src_str = src_str;
686     function->src_len = src_len;
687
688     *ret = &function->dispex;
689     return S_OK;
690 }
691
692 HRESULT init_function_constr(script_ctx_t *ctx, DispatchEx *object_prototype)
693 {
694     FunctionInstance *prot, *constr;
695     HRESULT hres;
696
697     static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
698
699     hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, object_prototype, &prot);
700     if(FAILED(hres))
701         return hres;
702
703     prot->value_proc = FunctionProt_value;
704     prot->name = prototypeW;
705
706     hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, &prot->dispex, &constr);
707     if(SUCCEEDED(hres)) {
708         constr->value_proc = FunctionConstr_value;
709         constr->name = FunctionW;
710         hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
711         if(FAILED(hres))
712             jsdisp_release(&constr->dispex);
713     }
714     jsdisp_release(&prot->dispex);
715     if(FAILED(hres))
716         return hres;
717
718     ctx->function_constr = &constr->dispex;
719     return S_OK;
720 }