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