shell32: Remove some superfluous LPARAM/WPARAM casts.
[wine/hacks.git] / dlls / jscript / function.c
blob4f4a87f960b103199e321fa645aadea7a4ca3426
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
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.
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.
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
19 #include "jscript.h"
20 #include "engine.h"
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
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;
40 static inline FunctionInstance *function_from_vdisp(vdisp_t *vdisp)
42 return (FunctionInstance*)vdisp->u.jsdisp;
45 static inline FunctionInstance *function_this(vdisp_t *jsthis)
47 return is_vclass(jsthis, JSCLASS_FUNCTION) ? function_from_vdisp(jsthis) : NULL;
50 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
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};
57 static IDispatch *get_this(DISPPARAMS *dp)
59 DWORD i;
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);
66 WARN("This is not VT_DISPATCH\n");
67 return NULL;
71 TRACE("no this passed\n");
72 return NULL;
75 static HRESULT init_parameters(DispatchEx *var_disp, FunctionInstance *function, DISPPARAMS *dp,
76 jsexcept_t *ei, IServiceProvider *caller)
78 parameter_t *param;
79 VARIANT var_empty;
80 DWORD cargs, i=0;
81 HRESULT hres;
83 V_VT(&var_empty) = VT_EMPTY;
84 cargs = arg_cnt(dp);
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;
92 i++;
95 return S_OK;
98 static HRESULT Arguments_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
99 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
101 FIXME("\n");
102 return E_NOTIMPL;
105 static const builtin_info_t Arguments_info = {
106 JSCLASS_ARGUMENTS,
107 {NULL, Arguments_value, 0},
108 0, NULL,
109 NULL,
110 NULL
113 static HRESULT create_arguments(script_ctx_t *ctx, IDispatch *calee, DISPPARAMS *dp,
114 jsexcept_t *ei, IServiceProvider *caller, DispatchEx **ret)
116 DispatchEx *args;
117 VARIANT var;
118 DWORD i;
119 HRESULT hres;
121 static const WCHAR caleeW[] = {'c','a','l','l','e','e',0};
123 args = heap_alloc_zero(sizeof(DispatchEx));
124 if(!args)
125 return E_OUTOFMEMORY;
127 hres = init_dispex_from_constr(args, ctx, &Arguments_info, ctx->object_constr);
128 if(FAILED(hres)) {
129 heap_free(args);
130 return hres;
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;
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);
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);
151 if(FAILED(hres)) {
152 jsdisp_release(args);
153 return hres;
156 *ret = args;
157 return S_OK;
160 static HRESULT create_var_disp(script_ctx_t *ctx, FunctionInstance *function, DISPPARAMS *dp, jsexcept_t *ei,
161 IServiceProvider *caller, DispatchEx **ret)
163 DispatchEx *var_disp, *arg_disp;
164 HRESULT hres;
166 static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
168 hres = create_dispex(ctx, NULL, NULL, &var_disp);
169 if(FAILED(hres))
170 return hres;
172 hres = create_arguments(ctx, (IDispatch*)_IDispatchEx_(&function->dispex),
173 dp, ei, caller, &arg_disp);
174 if(SUCCEEDED(hres)) {
175 VARIANT var;
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);
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;
190 *ret = var_disp;
191 return S_OK;
194 static HRESULT invoke_source(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *dp,
195 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
197 DispatchEx *var_disp;
198 exec_ctx_t *exec_ctx;
199 scope_chain_t *scope;
200 HRESULT hres;
202 if(!function->source) {
203 FIXME("no source\n");
204 return E_FAIL;
207 hres = create_var_disp(ctx, function, dp, ei, caller, &var_disp);
208 if(FAILED(hres))
209 return hres;
211 hres = scope_push(function->scope_chain, var_disp, &scope);
212 if(SUCCEEDED(hres)) {
213 hres = create_exec_ctx(ctx, this_obj, var_disp, scope, &exec_ctx);
214 scope_release(scope);
216 if(FAILED(hres))
217 return hres;
219 hres = exec_source(exec_ctx, function->parser, function->source, EXECT_FUNCTION, ei, retv);
220 exec_release(exec_ctx);
222 return hres;
225 static HRESULT invoke_constructor(script_ctx_t *ctx, FunctionInstance *function, DISPPARAMS *dp,
226 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
228 DispatchEx *this_obj;
229 VARIANT var;
230 HRESULT hres;
232 hres = create_object(ctx, &function->dispex, &this_obj);
233 if(FAILED(hres))
234 return hres;
236 hres = invoke_source(ctx, function, (IDispatch*)_IDispatchEx_(this_obj), dp, &var, ei, caller);
237 if(FAILED(hres)) {
238 jsdisp_release(this_obj);
239 return hres;
242 V_VT(retv) = VT_DISPATCH;
243 if(V_VT(&var) == VT_DISPATCH) {
244 jsdisp_release(this_obj);
245 V_DISPATCH(retv) = V_DISPATCH(&var);
246 }else {
247 VariantClear(&var);
248 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(this_obj);
250 return S_OK;
253 static HRESULT invoke_value_proc(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_disp, WORD flags, DISPPARAMS *dp,
254 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
256 vdisp_t vthis;
257 HRESULT hres;
259 if(this_disp)
260 set_disp(&vthis, this_disp);
261 else if(ctx->host_global)
262 set_disp(&vthis, ctx->host_global);
263 else
264 set_jsdisp(&vthis, ctx->global);
266 hres = function->value_proc(ctx, &vthis, flags, dp, retv, ei, caller);
268 vdisp_release(&vthis);
269 return hres;
272 static HRESULT call_function(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *args,
273 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
275 if(function->value_proc)
276 return invoke_value_proc(ctx, function, this_obj, DISPATCH_METHOD, args, retv, ei, caller);
278 return invoke_source(ctx, function, this_obj, args, retv, ei, caller);
281 static HRESULT function_to_string(FunctionInstance *function, BSTR *ret)
283 BSTR str;
285 static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
286 static const WCHAR native_suffixW[] =
287 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
289 if(function->value_proc) {
290 DWORD name_len;
292 name_len = strlenW(function->name);
293 str = SysAllocStringLen(NULL, sizeof(native_prefixW) + name_len*sizeof(WCHAR) + sizeof(native_suffixW));
294 if(!str)
295 return E_OUTOFMEMORY;
297 memcpy(str, native_prefixW, sizeof(native_prefixW));
298 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR), function->name, name_len*sizeof(WCHAR));
299 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR) + name_len, native_suffixW, sizeof(native_suffixW));
300 }else {
301 str = SysAllocStringLen(function->src_str, function->src_len);
302 if(!str)
303 return E_OUTOFMEMORY;
306 *ret = str;
307 return S_OK;
310 static HRESULT Function_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
311 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
313 FunctionInstance *This = function_from_vdisp(jsthis);
315 TRACE("%p %d\n", This, This->length);
317 switch(flags) {
318 case DISPATCH_PROPERTYGET:
319 V_VT(retv) = VT_I4;
320 V_I4(retv) = This->length;
321 break;
322 default:
323 FIXME("unimplemented flags %x\n", flags);
324 return E_NOTIMPL;
327 return S_OK;
330 static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
331 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
333 FunctionInstance *function;
334 BSTR str;
335 HRESULT hres;
337 TRACE("\n");
339 if(!(function = function_this(jsthis)))
340 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
342 hres = function_to_string(function, &str);
343 if(FAILED(hres))
344 return hres;
346 if(retv) {
347 V_VT(retv) = VT_BSTR;
348 V_BSTR(retv) = str;
349 }else {
350 SysFreeString(str);
352 return S_OK;
355 static HRESULT array_to_args(script_ctx_t *ctx, DispatchEx *arg_array, jsexcept_t *ei, IServiceProvider *caller,
356 DISPPARAMS *args)
358 VARIANT var, *argv;
359 DWORD length, i;
360 HRESULT hres;
362 hres = jsdisp_propget_name(arg_array, lengthW, &var, ei, NULL/*FIXME*/);
363 if(FAILED(hres))
364 return hres;
366 hres = to_uint32(ctx, &var, ei, &length);
367 VariantClear(&var);
368 if(FAILED(hres))
369 return hres;
371 argv = heap_alloc(length * sizeof(VARIANT));
372 if(!argv)
373 return E_OUTOFMEMORY;
375 for(i=0; i<length; i++) {
376 hres = jsdisp_propget_idx(arg_array, i, argv+i, ei, caller);
377 if(FAILED(hres)) {
378 while(i--)
379 VariantClear(argv+i);
380 heap_free(argv);
381 return hres;
385 args->cArgs = length;
386 args->rgvarg = argv;
387 return S_OK;
390 static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
391 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
393 FunctionInstance *function;
394 DISPPARAMS args = {NULL,NULL,0,0};
395 DWORD argc, i;
396 IDispatch *this_obj = NULL;
397 HRESULT hres = S_OK;
399 TRACE("\n");
401 if(!(function = function_this(jsthis)))
402 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
404 argc = arg_cnt(dp);
405 if(argc) {
406 VARIANT *v = get_arg(dp,0);
408 if(V_VT(v) != VT_EMPTY && V_VT(v) != VT_NULL) {
409 hres = to_object(ctx, v, &this_obj);
410 if(FAILED(hres))
411 return hres;
415 if(argc >= 2) {
416 DispatchEx *arg_array = NULL;
418 if(V_VT(get_arg(dp,1)) == VT_DISPATCH) {
419 arg_array = iface_to_jsdisp((IUnknown*)V_DISPATCH(get_arg(dp,1)));
420 if(arg_array &&
421 (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
422 jsdisp_release(arg_array);
423 arg_array = NULL;
427 if(arg_array) {
428 hres = array_to_args(ctx, arg_array, ei, caller, &args);
429 jsdisp_release(arg_array);
430 }else {
431 FIXME("throw TypeError\n");
432 hres = E_FAIL;
436 hres = call_function(ctx, function, this_obj, &args, retv, ei, caller);
438 if(this_obj)
439 IDispatch_Release(this_obj);
440 for(i=0; i<args.cArgs; i++)
441 VariantClear(args.rgvarg+i);
442 heap_free(args.rgvarg);
443 return hres;
446 static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
447 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
449 FunctionInstance *function;
450 DISPPARAMS args = {NULL,NULL,0,0};
451 IDispatch *this_obj = NULL;
452 DWORD argc;
453 HRESULT hres;
455 TRACE("\n");
457 if(!(function = function_this(jsthis)))
458 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
460 argc = arg_cnt(dp);
461 if(argc) {
462 VARIANT *v = get_arg(dp,0);
464 if(V_VT(v) != VT_EMPTY && V_VT(v) != VT_NULL) {
465 hres = to_object(ctx, v, &this_obj);
466 if(FAILED(hres))
467 return hres;
470 args.cArgs = argc-1;
473 if(args.cArgs)
474 args.rgvarg = dp->rgvarg + dp->cArgs - args.cArgs-1;
476 hres = call_function(ctx, function, this_obj, &args, retv, ei, caller);
478 if(this_obj)
479 IDispatch_Release(this_obj);
480 return hres;
483 HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
484 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
486 FunctionInstance *function;
488 TRACE("\n");
490 if(!is_vclass(jsthis, JSCLASS_FUNCTION)) {
491 ERR("dispex is not a function\n");
492 return E_FAIL;
495 function = (FunctionInstance*)jsthis->u.jsdisp;
497 switch(flags) {
498 case DISPATCH_METHOD:
499 if(function->value_proc)
500 return invoke_value_proc(ctx, function, get_this(dp), flags, dp, retv, ei, caller);
502 return invoke_source(ctx, function, get_this(dp), dp, retv, ei, caller);
504 case DISPATCH_PROPERTYGET: {
505 HRESULT hres;
506 BSTR str;
508 hres = function_to_string(function, &str);
509 if(FAILED(hres))
510 return hres;
512 V_VT(retv) = VT_BSTR;
513 V_BSTR(retv) = str;
514 break;
517 case DISPATCH_CONSTRUCT:
518 if(function->value_proc)
519 return invoke_value_proc(ctx, function, get_this(dp), flags, dp, retv, ei, caller);
521 return invoke_constructor(ctx, function, dp, retv, ei, caller);
523 default:
524 FIXME("not implemented flags %x\n", flags);
525 return E_NOTIMPL;
528 return S_OK;
531 static void Function_destructor(DispatchEx *dispex)
533 FunctionInstance *This = (FunctionInstance*)dispex;
535 if(This->parser)
536 parser_release(This->parser);
537 if(This->scope_chain)
538 scope_release(This->scope_chain);
539 heap_free(This);
542 static const builtin_prop_t Function_props[] = {
543 {applyW, Function_apply, PROPF_METHOD|2},
544 {callW, Function_call, PROPF_METHOD|1},
545 {lengthW, Function_length, 0},
546 {toStringW, Function_toString, PROPF_METHOD}
549 static const builtin_info_t Function_info = {
550 JSCLASS_FUNCTION,
551 {NULL, Function_value, 0},
552 sizeof(Function_props)/sizeof(*Function_props),
553 Function_props,
554 Function_destructor,
555 NULL
558 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
559 BOOL funcprot, DispatchEx *prototype, FunctionInstance **ret)
561 FunctionInstance *function;
562 HRESULT hres;
564 function = heap_alloc_zero(sizeof(FunctionInstance));
565 if(!function)
566 return E_OUTOFMEMORY;
568 if(funcprot)
569 hres = init_dispex(&function->dispex, ctx, &Function_info, prototype);
570 else if(builtin_info)
571 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
572 else
573 hres = init_dispex_from_constr(&function->dispex, ctx, &Function_info, ctx->function_constr);
574 if(FAILED(hres))
575 return hres;
577 function->flags = flags;
578 function->length = flags & PROPF_ARGMASK;
580 *ret = function;
581 return S_OK;
584 static HRESULT set_prototype(script_ctx_t *ctx, DispatchEx *dispex, DispatchEx *prototype)
586 jsexcept_t jsexcept;
587 VARIANT var;
589 V_VT(&var) = VT_DISPATCH;
590 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(prototype);
591 memset(&jsexcept, 0, sizeof(jsexcept));
593 return jsdisp_propput_name(dispex, prototypeW, &var, &jsexcept, NULL/*FIXME*/);
596 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
597 const builtin_info_t *builtin_info, DWORD flags, DispatchEx *prototype, DispatchEx **ret)
599 FunctionInstance *function;
600 HRESULT hres;
602 hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
603 if(FAILED(hres))
604 return hres;
606 hres = set_prototype(ctx, &function->dispex, prototype);
607 if(FAILED(hres)) {
608 jsdisp_release(&function->dispex);
609 return hres;
612 function->value_proc = value_proc;
613 function->name = name;
615 *ret = &function->dispex;
616 return S_OK;
619 HRESULT create_source_function(parser_ctx_t *ctx, parameter_t *parameters, source_elements_t *source,
620 scope_chain_t *scope_chain, const WCHAR *src_str, DWORD src_len, DispatchEx **ret)
622 FunctionInstance *function;
623 DispatchEx *prototype;
624 parameter_t *iter;
625 DWORD length = 0;
626 HRESULT hres;
628 hres = create_object(ctx->script, NULL, &prototype);
629 if(FAILED(hres))
630 return hres;
632 hres = create_function(ctx->script, NULL, PROPF_CONSTR, FALSE, NULL, &function);
633 if(SUCCEEDED(hres)) {
634 hres = set_prototype(ctx->script, &function->dispex, prototype);
635 if(FAILED(hres))
636 jsdisp_release(&function->dispex);
638 jsdisp_release(prototype);
639 if(FAILED(hres))
640 return hres;
642 function->source = source;
643 function->parameters = parameters;
645 if(scope_chain) {
646 scope_addref(scope_chain);
647 function->scope_chain = scope_chain;
650 parser_addref(ctx);
651 function->parser = ctx;
653 for(iter = parameters; iter; iter = iter->next)
654 length++;
655 function->length = length;
657 function->src_str = src_str;
658 function->src_len = src_len;
660 *ret = &function->dispex;
661 return S_OK;
664 static HRESULT construct_function(script_ctx_t *ctx, DISPPARAMS *dp, jsexcept_t *ei, IDispatch **ret)
666 function_expression_t *expr;
667 WCHAR *str = NULL, *ptr;
668 DWORD argc, len = 0, l;
669 parser_ctx_t *parser;
670 DispatchEx *function;
671 BSTR *params = NULL;
672 int i=0, j=0;
673 HRESULT hres = S_OK;
675 static const WCHAR function_anonymousW[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
676 static const WCHAR function_beginW[] = {')',' ','{','\n'};
677 static const WCHAR function_endW[] = {'\n','}',0};
679 argc = arg_cnt(dp);
680 if(argc) {
681 params = heap_alloc(argc*sizeof(BSTR));
682 if(!params)
683 return E_OUTOFMEMORY;
685 if(argc > 2)
686 len = (argc-2)*2; /* separating commas */
687 for(i=0; i < argc; i++) {
688 hres = to_string(ctx, get_arg(dp,i), ei, params+i);
689 if(FAILED(hres))
690 break;
691 len += SysStringLen(params[i]);
695 if(SUCCEEDED(hres)) {
696 len += (sizeof(function_anonymousW) + sizeof(function_beginW) + sizeof(function_endW)) / sizeof(WCHAR);
697 str = heap_alloc(len*sizeof(WCHAR));
698 if(str) {
699 memcpy(str, function_anonymousW, sizeof(function_anonymousW));
700 ptr = str + sizeof(function_anonymousW)/sizeof(WCHAR);
701 if(argc > 1) {
702 while(1) {
703 l = SysStringLen(params[j]);
704 memcpy(ptr, params[j], l*sizeof(WCHAR));
705 ptr += l;
706 if(++j == argc-1)
707 break;
708 *ptr++ = ',';
709 *ptr++ = ' ';
712 memcpy(ptr, function_beginW, sizeof(function_beginW));
713 ptr += sizeof(function_beginW)/sizeof(WCHAR);
714 if(argc) {
715 l = SysStringLen(params[argc-1]);
716 memcpy(ptr, params[argc-1], l*sizeof(WCHAR));
717 ptr += l;
719 memcpy(ptr, function_endW, sizeof(function_endW));
721 TRACE("%s\n", debugstr_w(str));
722 }else {
723 hres = E_OUTOFMEMORY;
727 while(--i >= 0)
728 SysFreeString(params[i]);
729 heap_free(params);
730 if(FAILED(hres))
731 return hres;
733 hres = script_parse(ctx, str, NULL, &parser);
734 heap_free(str);
735 if(FAILED(hres))
736 return hres;
738 if(!parser->source || !parser->source->functions || parser->source->functions->next || parser->source->variables) {
739 ERR("Invalid parser result!\n");
740 parser_release(parser);
741 return E_UNEXPECTED;
743 expr = parser->source->functions->expr;
745 hres = create_source_function(parser, expr->parameter_list, expr->source_elements, NULL, expr->src_str,
746 expr->src_len, &function);
747 parser_release(parser);
748 if(FAILED(hres))
749 return hres;
751 *ret = (IDispatch*)_IDispatchEx_(function);
752 return S_OK;
755 static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
756 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
758 HRESULT hres;
760 TRACE("\n");
762 switch(flags) {
763 case DISPATCH_CONSTRUCT: {
764 IDispatch *ret;
766 hres = construct_function(ctx, dp, ei, &ret);
767 if(FAILED(hres))
768 return hres;
770 V_VT(retv) = VT_DISPATCH;
771 V_DISPATCH(retv) = ret;
772 break;
774 default:
775 FIXME("unimplemented flags %x\n", flags);
776 return E_NOTIMPL;
779 return S_OK;
782 static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
783 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
785 FIXME("\n");
786 return E_NOTIMPL;
789 HRESULT init_function_constr(script_ctx_t *ctx, DispatchEx *object_prototype)
791 FunctionInstance *prot, *constr;
792 HRESULT hres;
794 static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
796 hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, object_prototype, &prot);
797 if(FAILED(hres))
798 return hres;
800 prot->value_proc = FunctionProt_value;
801 prot->name = prototypeW;
803 hres = create_function(ctx, NULL, PROPF_CONSTR|1, TRUE, &prot->dispex, &constr);
804 if(SUCCEEDED(hres)) {
805 constr->value_proc = FunctionConstr_value;
806 constr->name = FunctionW;
807 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
808 if(FAILED(hres))
809 jsdisp_release(&constr->dispex);
811 jsdisp_release(&prot->dispex);
812 if(FAILED(hres))
813 return hres;
815 ctx->function_constr = &constr->dispex;
816 return S_OK;