include: Specify calling convention explicitly in idl files where needed.
[wine/hramrach.git] / dlls / jscript / function.c
blob9f5fb3b6fc69c9339024b6d1dded65f75df1b1c2
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 DispatchEx *arguments;
39 } FunctionInstance;
41 static inline FunctionInstance *function_from_vdisp(vdisp_t *vdisp)
43 return (FunctionInstance*)vdisp->u.jsdisp;
46 static inline FunctionInstance *function_this(vdisp_t *jsthis)
48 return is_vclass(jsthis, JSCLASS_FUNCTION) ? function_from_vdisp(jsthis) : NULL;
51 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
53 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
54 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
55 static const WCHAR applyW[] = {'a','p','p','l','y',0};
56 static const WCHAR callW[] = {'c','a','l','l',0};
57 static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
59 static IDispatch *get_this(DISPPARAMS *dp)
61 DWORD i;
63 for(i=0; i < dp->cNamedArgs; i++) {
64 if(dp->rgdispidNamedArgs[i] == DISPID_THIS) {
65 if(V_VT(dp->rgvarg+i) == VT_DISPATCH)
66 return V_DISPATCH(dp->rgvarg+i);
68 WARN("This is not VT_DISPATCH\n");
69 return NULL;
73 TRACE("no this passed\n");
74 return NULL;
77 static HRESULT init_parameters(DispatchEx *var_disp, FunctionInstance *function, DISPPARAMS *dp,
78 jsexcept_t *ei, IServiceProvider *caller)
80 parameter_t *param;
81 VARIANT var_empty;
82 DWORD cargs, i=0;
83 HRESULT hres;
85 V_VT(&var_empty) = VT_EMPTY;
86 cargs = arg_cnt(dp);
88 for(param = function->parameters; param; param = param->next) {
89 hres = jsdisp_propput_name(var_disp, param->identifier,
90 i < cargs ? get_arg(dp,i) : &var_empty, ei, caller);
91 if(FAILED(hres))
92 return hres;
94 i++;
97 return S_OK;
100 static HRESULT Arguments_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
101 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
103 FIXME("\n");
104 return E_NOTIMPL;
107 static const builtin_info_t Arguments_info = {
108 JSCLASS_ARGUMENTS,
109 {NULL, Arguments_value, 0},
110 0, NULL,
111 NULL,
112 NULL
115 static HRESULT create_arguments(script_ctx_t *ctx, IDispatch *calee, DISPPARAMS *dp,
116 jsexcept_t *ei, IServiceProvider *caller, DispatchEx **ret)
118 DispatchEx *args;
119 VARIANT var;
120 DWORD i;
121 HRESULT hres;
123 static const WCHAR caleeW[] = {'c','a','l','l','e','e',0};
125 args = heap_alloc_zero(sizeof(DispatchEx));
126 if(!args)
127 return E_OUTOFMEMORY;
129 hres = init_dispex_from_constr(args, ctx, &Arguments_info, ctx->object_constr);
130 if(FAILED(hres)) {
131 heap_free(args);
132 return hres;
135 for(i=0; i < arg_cnt(dp); i++) {
136 hres = jsdisp_propput_idx(args, i, get_arg(dp,i), ei, caller);
137 if(FAILED(hres))
138 break;
141 if(SUCCEEDED(hres)) {
142 V_VT(&var) = VT_I4;
143 V_I4(&var) = arg_cnt(dp);
144 hres = jsdisp_propput_name(args, lengthW, &var, ei, caller);
146 if(SUCCEEDED(hres)) {
147 V_VT(&var) = VT_DISPATCH;
148 V_DISPATCH(&var) = calee;
149 hres = jsdisp_propput_name(args, caleeW, &var, ei, caller);
153 if(FAILED(hres)) {
154 jsdisp_release(args);
155 return hres;
158 *ret = args;
159 return S_OK;
162 static HRESULT create_var_disp(script_ctx_t *ctx, FunctionInstance *function, DispatchEx *arg_disp,
163 DISPPARAMS *dp, jsexcept_t *ei, IServiceProvider *caller, DispatchEx **ret)
165 DispatchEx *var_disp;
166 VARIANT var;
167 HRESULT hres;
169 hres = create_dispex(ctx, NULL, NULL, &var_disp);
170 if(FAILED(hres))
171 return hres;
173 V_VT(&var) = VT_DISPATCH;
174 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(arg_disp);
175 hres = jsdisp_propput_name(var_disp, argumentsW, &var, ei, caller);
176 if(SUCCEEDED(hres))
177 hres = init_parameters(var_disp, function, dp, ei, caller);
178 if(FAILED(hres)) {
179 jsdisp_release(var_disp);
180 return hres;
183 *ret = var_disp;
184 return S_OK;
187 static HRESULT invoke_source(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *dp,
188 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
190 DispatchEx *var_disp, *arg_disp;
191 exec_ctx_t *exec_ctx;
192 scope_chain_t *scope;
193 HRESULT hres;
195 if(!function->source) {
196 FIXME("no source\n");
197 return E_FAIL;
200 hres = create_arguments(ctx, (IDispatch*)_IDispatchEx_(&function->dispex),
201 dp, ei, caller, &arg_disp);
202 if(FAILED(hres))
203 return hres;
205 hres = create_var_disp(ctx, function, arg_disp, dp, ei, caller, &var_disp);
206 if(FAILED(hres)) {
207 jsdisp_release(arg_disp);
208 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 jsdisp_release(var_disp);
217 if(SUCCEEDED(hres)) {
218 DispatchEx *prev_args;
220 prev_args = function->arguments;
221 function->arguments = arg_disp;
222 hres = exec_source(exec_ctx, function->parser, function->source, EXECT_FUNCTION, ei, retv);
223 function->arguments = prev_args;
225 jsdisp_release(arg_disp);
226 exec_release(exec_ctx);
229 return hres;
232 static HRESULT invoke_constructor(script_ctx_t *ctx, FunctionInstance *function, DISPPARAMS *dp,
233 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
235 DispatchEx *this_obj;
236 VARIANT var;
237 HRESULT hres;
239 hres = create_object(ctx, &function->dispex, &this_obj);
240 if(FAILED(hres))
241 return hres;
243 hres = invoke_source(ctx, function, (IDispatch*)_IDispatchEx_(this_obj), dp, &var, ei, caller);
244 if(FAILED(hres)) {
245 jsdisp_release(this_obj);
246 return hres;
249 V_VT(retv) = VT_DISPATCH;
250 if(V_VT(&var) == VT_DISPATCH) {
251 jsdisp_release(this_obj);
252 V_DISPATCH(retv) = V_DISPATCH(&var);
253 }else {
254 VariantClear(&var);
255 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(this_obj);
257 return S_OK;
260 static HRESULT invoke_value_proc(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_disp, WORD flags, DISPPARAMS *dp,
261 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
263 vdisp_t vthis;
264 HRESULT hres;
266 if(this_disp)
267 set_disp(&vthis, this_disp);
268 else if(ctx->host_global)
269 set_disp(&vthis, ctx->host_global);
270 else
271 set_jsdisp(&vthis, ctx->global);
273 hres = function->value_proc(ctx, &vthis, flags, dp, retv, ei, caller);
275 vdisp_release(&vthis);
276 return hres;
279 static HRESULT call_function(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *args,
280 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
282 if(function->value_proc)
283 return invoke_value_proc(ctx, function, this_obj, DISPATCH_METHOD, args, retv, ei, caller);
285 return invoke_source(ctx, function, this_obj, args, retv, ei, caller);
288 static HRESULT function_to_string(FunctionInstance *function, BSTR *ret)
290 BSTR str;
292 static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
293 static const WCHAR native_suffixW[] =
294 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
296 if(function->value_proc) {
297 DWORD name_len;
299 name_len = strlenW(function->name);
300 str = SysAllocStringLen(NULL, sizeof(native_prefixW) + name_len*sizeof(WCHAR) + sizeof(native_suffixW));
301 if(!str)
302 return E_OUTOFMEMORY;
304 memcpy(str, native_prefixW, sizeof(native_prefixW));
305 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR), function->name, name_len*sizeof(WCHAR));
306 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR) + name_len, native_suffixW, sizeof(native_suffixW));
307 }else {
308 str = SysAllocStringLen(function->src_str, function->src_len);
309 if(!str)
310 return E_OUTOFMEMORY;
313 *ret = str;
314 return S_OK;
317 static HRESULT Function_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
318 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
320 FunctionInstance *This = function_from_vdisp(jsthis);
322 TRACE("%p %d\n", This, This->length);
324 switch(flags) {
325 case DISPATCH_PROPERTYGET:
326 V_VT(retv) = VT_I4;
327 V_I4(retv) = This->length;
328 break;
329 default:
330 FIXME("unimplemented flags %x\n", flags);
331 return E_NOTIMPL;
334 return S_OK;
337 static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
338 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
340 FunctionInstance *function;
341 BSTR str;
342 HRESULT hres;
344 TRACE("\n");
346 if(!(function = function_this(jsthis)))
347 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
349 hres = function_to_string(function, &str);
350 if(FAILED(hres))
351 return hres;
353 if(retv) {
354 V_VT(retv) = VT_BSTR;
355 V_BSTR(retv) = str;
356 }else {
357 SysFreeString(str);
359 return S_OK;
362 static HRESULT array_to_args(script_ctx_t *ctx, DispatchEx *arg_array, jsexcept_t *ei, IServiceProvider *caller,
363 DISPPARAMS *args)
365 VARIANT var, *argv;
366 DWORD length, i;
367 HRESULT hres;
369 hres = jsdisp_propget_name(arg_array, lengthW, &var, ei, NULL/*FIXME*/);
370 if(FAILED(hres))
371 return hres;
373 hres = to_uint32(ctx, &var, ei, &length);
374 VariantClear(&var);
375 if(FAILED(hres))
376 return hres;
378 argv = heap_alloc(length * sizeof(VARIANT));
379 if(!argv)
380 return E_OUTOFMEMORY;
382 for(i=0; i<length; i++) {
383 hres = jsdisp_get_idx(arg_array, i, argv+i, ei, caller);
384 if(hres == DISP_E_UNKNOWNNAME)
385 V_VT(argv+i) = VT_EMPTY;
386 else if(FAILED(hres)) {
387 while(i--)
388 VariantClear(argv+i);
389 heap_free(argv);
390 return hres;
394 args->cArgs = length;
395 args->rgvarg = argv;
396 return S_OK;
399 static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
400 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
402 FunctionInstance *function;
403 DISPPARAMS args = {NULL,NULL,0,0};
404 DWORD argc, i;
405 IDispatch *this_obj = NULL;
406 HRESULT hres = S_OK;
408 TRACE("\n");
410 if(!(function = function_this(jsthis)))
411 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
413 argc = arg_cnt(dp);
414 if(argc) {
415 VARIANT *v = get_arg(dp,0);
417 if(V_VT(v) != VT_EMPTY && V_VT(v) != VT_NULL) {
418 hres = to_object(ctx, v, &this_obj);
419 if(FAILED(hres))
420 return hres;
424 if(argc >= 2) {
425 DispatchEx *arg_array = NULL;
427 if(V_VT(get_arg(dp,1)) == VT_DISPATCH) {
428 arg_array = iface_to_jsdisp((IUnknown*)V_DISPATCH(get_arg(dp,1)));
429 if(arg_array &&
430 (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
431 jsdisp_release(arg_array);
432 arg_array = NULL;
436 if(arg_array) {
437 hres = array_to_args(ctx, arg_array, ei, caller, &args);
438 jsdisp_release(arg_array);
439 }else {
440 FIXME("throw TypeError\n");
441 hres = E_FAIL;
445 hres = call_function(ctx, function, this_obj, &args, retv, ei, caller);
447 if(this_obj)
448 IDispatch_Release(this_obj);
449 for(i=0; i<args.cArgs; i++)
450 VariantClear(args.rgvarg+i);
451 heap_free(args.rgvarg);
452 return hres;
455 static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
456 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
458 FunctionInstance *function;
459 DISPPARAMS args = {NULL,NULL,0,0};
460 IDispatch *this_obj = NULL;
461 DWORD argc;
462 HRESULT hres;
464 TRACE("\n");
466 if(!(function = function_this(jsthis)))
467 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
469 argc = arg_cnt(dp);
470 if(argc) {
471 VARIANT *v = get_arg(dp,0);
473 if(V_VT(v) != VT_EMPTY && V_VT(v) != VT_NULL) {
474 hres = to_object(ctx, v, &this_obj);
475 if(FAILED(hres))
476 return hres;
479 args.cArgs = argc-1;
482 if(args.cArgs)
483 args.rgvarg = dp->rgvarg + dp->cArgs - args.cArgs-1;
485 hres = call_function(ctx, function, this_obj, &args, retv, ei, caller);
487 if(this_obj)
488 IDispatch_Release(this_obj);
489 return hres;
492 HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
493 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
495 FunctionInstance *function;
497 TRACE("\n");
499 if(!is_vclass(jsthis, JSCLASS_FUNCTION)) {
500 ERR("dispex is not a function\n");
501 return E_FAIL;
504 function = (FunctionInstance*)jsthis->u.jsdisp;
506 switch(flags) {
507 case DISPATCH_METHOD:
508 if(function->value_proc)
509 return invoke_value_proc(ctx, function, get_this(dp), flags, dp, retv, ei, caller);
511 return invoke_source(ctx, function, get_this(dp), dp, retv, ei, caller);
513 case DISPATCH_PROPERTYGET: {
514 HRESULT hres;
515 BSTR str;
517 hres = function_to_string(function, &str);
518 if(FAILED(hres))
519 return hres;
521 V_VT(retv) = VT_BSTR;
522 V_BSTR(retv) = str;
523 break;
526 case DISPATCH_CONSTRUCT:
527 if(function->value_proc)
528 return invoke_value_proc(ctx, function, get_this(dp), flags, dp, retv, ei, caller);
530 return invoke_constructor(ctx, function, dp, retv, ei, caller);
532 default:
533 FIXME("not implemented flags %x\n", flags);
534 return E_NOTIMPL;
537 return S_OK;
540 static HRESULT Function_arguments(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
541 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
543 FunctionInstance *function = (FunctionInstance*)jsthis->u.jsdisp;
544 HRESULT hres = S_OK;
546 TRACE("\n");
548 switch(flags) {
549 case DISPATCH_PROPERTYGET: {
550 if(function->arguments) {
551 IDispatchEx_AddRef(_IDispatchEx_(function->arguments));
552 V_VT(retv) = VT_DISPATCH;
553 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(function->arguments);
554 }else {
555 V_VT(retv) = VT_NULL;
557 break;
559 case DISPATCH_PROPERTYPUT:
560 break;
561 default:
562 FIXME("unimplemented flags %x\n", flags);
563 hres = E_NOTIMPL;
566 return hres;
569 static void Function_destructor(DispatchEx *dispex)
571 FunctionInstance *This = (FunctionInstance*)dispex;
573 if(This->parser)
574 parser_release(This->parser);
575 if(This->scope_chain)
576 scope_release(This->scope_chain);
577 heap_free(This);
580 static const builtin_prop_t Function_props[] = {
581 {applyW, Function_apply, PROPF_METHOD|2},
582 {argumentsW, Function_arguments, 0},
583 {callW, Function_call, PROPF_METHOD|1},
584 {lengthW, Function_length, 0},
585 {toStringW, Function_toString, PROPF_METHOD}
588 static const builtin_info_t Function_info = {
589 JSCLASS_FUNCTION,
590 {NULL, Function_value, 0},
591 sizeof(Function_props)/sizeof(*Function_props),
592 Function_props,
593 Function_destructor,
594 NULL
597 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
598 BOOL funcprot, DispatchEx *prototype, FunctionInstance **ret)
600 FunctionInstance *function;
601 HRESULT hres;
603 function = heap_alloc_zero(sizeof(FunctionInstance));
604 if(!function)
605 return E_OUTOFMEMORY;
607 if(funcprot)
608 hres = init_dispex(&function->dispex, ctx, &Function_info, prototype);
609 else if(builtin_info)
610 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
611 else
612 hres = init_dispex_from_constr(&function->dispex, ctx, &Function_info, ctx->function_constr);
613 if(FAILED(hres))
614 return hres;
616 function->flags = flags;
617 function->length = flags & PROPF_ARGMASK;
619 *ret = function;
620 return S_OK;
623 static HRESULT set_prototype(script_ctx_t *ctx, DispatchEx *dispex, DispatchEx *prototype)
625 jsexcept_t jsexcept;
626 VARIANT var;
628 V_VT(&var) = VT_DISPATCH;
629 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(prototype);
630 memset(&jsexcept, 0, sizeof(jsexcept));
632 return jsdisp_propput_name(dispex, prototypeW, &var, &jsexcept, NULL/*FIXME*/);
635 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
636 const builtin_info_t *builtin_info, DWORD flags, DispatchEx *prototype, DispatchEx **ret)
638 FunctionInstance *function;
639 HRESULT hres;
641 hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
642 if(FAILED(hres))
643 return hres;
645 if(builtin_info) {
646 VARIANT var;
648 V_VT(&var) = VT_I4;
649 V_I4(&var) = function->length;
650 hres = jsdisp_propput_const(&function->dispex, lengthW, &var);
653 if(SUCCEEDED(hres))
654 hres = set_prototype(ctx, &function->dispex, prototype);
655 if(FAILED(hres)) {
656 jsdisp_release(&function->dispex);
657 return hres;
660 function->value_proc = value_proc;
661 function->name = name;
663 *ret = &function->dispex;
664 return S_OK;
667 HRESULT create_source_function(parser_ctx_t *ctx, parameter_t *parameters, source_elements_t *source,
668 scope_chain_t *scope_chain, const WCHAR *src_str, DWORD src_len, DispatchEx **ret)
670 FunctionInstance *function;
671 DispatchEx *prototype;
672 parameter_t *iter;
673 DWORD length = 0;
674 HRESULT hres;
676 hres = create_object(ctx->script, NULL, &prototype);
677 if(FAILED(hres))
678 return hres;
680 hres = create_function(ctx->script, NULL, PROPF_CONSTR, FALSE, NULL, &function);
681 if(SUCCEEDED(hres)) {
682 hres = set_prototype(ctx->script, &function->dispex, prototype);
683 if(FAILED(hres))
684 jsdisp_release(&function->dispex);
686 jsdisp_release(prototype);
687 if(FAILED(hres))
688 return hres;
690 function->source = source;
691 function->parameters = parameters;
693 if(scope_chain) {
694 scope_addref(scope_chain);
695 function->scope_chain = scope_chain;
698 parser_addref(ctx);
699 function->parser = ctx;
701 for(iter = parameters; iter; iter = iter->next)
702 length++;
703 function->length = length;
705 function->src_str = src_str;
706 function->src_len = src_len;
708 *ret = &function->dispex;
709 return S_OK;
712 static HRESULT construct_function(script_ctx_t *ctx, DISPPARAMS *dp, jsexcept_t *ei, IDispatch **ret)
714 function_expression_t *expr;
715 WCHAR *str = NULL, *ptr;
716 DWORD argc, len = 0, l;
717 parser_ctx_t *parser;
718 DispatchEx *function;
719 BSTR *params = NULL;
720 int i=0, j=0;
721 HRESULT hres = S_OK;
723 static const WCHAR function_anonymousW[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
724 static const WCHAR function_beginW[] = {')',' ','{','\n'};
725 static const WCHAR function_endW[] = {'\n','}',0};
727 argc = arg_cnt(dp);
728 if(argc) {
729 params = heap_alloc(argc*sizeof(BSTR));
730 if(!params)
731 return E_OUTOFMEMORY;
733 if(argc > 2)
734 len = (argc-2)*2; /* separating commas */
735 for(i=0; i < argc; i++) {
736 hres = to_string(ctx, get_arg(dp,i), ei, params+i);
737 if(FAILED(hres))
738 break;
739 len += SysStringLen(params[i]);
743 if(SUCCEEDED(hres)) {
744 len += (sizeof(function_anonymousW) + sizeof(function_beginW) + sizeof(function_endW)) / sizeof(WCHAR);
745 str = heap_alloc(len*sizeof(WCHAR));
746 if(str) {
747 memcpy(str, function_anonymousW, sizeof(function_anonymousW));
748 ptr = str + sizeof(function_anonymousW)/sizeof(WCHAR);
749 if(argc > 1) {
750 while(1) {
751 l = SysStringLen(params[j]);
752 memcpy(ptr, params[j], l*sizeof(WCHAR));
753 ptr += l;
754 if(++j == argc-1)
755 break;
756 *ptr++ = ',';
757 *ptr++ = ' ';
760 memcpy(ptr, function_beginW, sizeof(function_beginW));
761 ptr += sizeof(function_beginW)/sizeof(WCHAR);
762 if(argc) {
763 l = SysStringLen(params[argc-1]);
764 memcpy(ptr, params[argc-1], l*sizeof(WCHAR));
765 ptr += l;
767 memcpy(ptr, function_endW, sizeof(function_endW));
769 TRACE("%s\n", debugstr_w(str));
770 }else {
771 hres = E_OUTOFMEMORY;
775 while(--i >= 0)
776 SysFreeString(params[i]);
777 heap_free(params);
778 if(FAILED(hres))
779 return hres;
781 hres = script_parse(ctx, str, NULL, &parser);
782 heap_free(str);
783 if(FAILED(hres))
784 return hres;
786 if(!parser->source || !parser->source->functions || parser->source->functions->next || parser->source->variables) {
787 ERR("Invalid parser result!\n");
788 parser_release(parser);
789 return E_UNEXPECTED;
791 expr = parser->source->functions->expr;
793 hres = create_source_function(parser, expr->parameter_list, expr->source_elements, NULL, expr->src_str,
794 expr->src_len, &function);
795 parser_release(parser);
796 if(FAILED(hres))
797 return hres;
799 *ret = (IDispatch*)_IDispatchEx_(function);
800 return S_OK;
803 static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
804 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
806 HRESULT hres;
808 TRACE("\n");
810 switch(flags) {
811 case DISPATCH_CONSTRUCT: {
812 IDispatch *ret;
814 hres = construct_function(ctx, dp, ei, &ret);
815 if(FAILED(hres))
816 return hres;
818 V_VT(retv) = VT_DISPATCH;
819 V_DISPATCH(retv) = ret;
820 break;
822 default:
823 FIXME("unimplemented flags %x\n", flags);
824 return E_NOTIMPL;
827 return S_OK;
830 static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
831 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
833 FIXME("\n");
834 return E_NOTIMPL;
837 HRESULT init_function_constr(script_ctx_t *ctx, DispatchEx *object_prototype)
839 FunctionInstance *prot, *constr;
840 HRESULT hres;
842 static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
844 hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, object_prototype, &prot);
845 if(FAILED(hres))
846 return hres;
848 prot->value_proc = FunctionProt_value;
849 prot->name = prototypeW;
851 hres = create_function(ctx, NULL, PROPF_CONSTR|1, TRUE, &prot->dispex, &constr);
852 if(SUCCEEDED(hres)) {
853 constr->value_proc = FunctionConstr_value;
854 constr->name = FunctionW;
855 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
856 if(FAILED(hres))
857 jsdisp_release(&constr->dispex);
859 jsdisp_release(&prot->dispex);
860 if(FAILED(hres))
861 return hres;
863 ctx->function_constr = &constr->dispex;
864 return S_OK;