winepulse.drv: add a pulseaudio driver
[wine/hacks.git] / dlls / jscript / function.c
blobdf46f1d996dfe4b2d312216a2859a05d960e909a
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 jsdisp_release(var_disp);
217 if(FAILED(hres))
218 return hres;
220 hres = exec_source(exec_ctx, function->parser, function->source, EXECT_FUNCTION, ei, retv);
221 exec_release(exec_ctx);
223 return hres;
226 static HRESULT invoke_constructor(script_ctx_t *ctx, FunctionInstance *function, DISPPARAMS *dp,
227 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
229 DispatchEx *this_obj;
230 VARIANT var;
231 HRESULT hres;
233 hres = create_object(ctx, &function->dispex, &this_obj);
234 if(FAILED(hres))
235 return hres;
237 hres = invoke_source(ctx, function, (IDispatch*)_IDispatchEx_(this_obj), dp, &var, ei, caller);
238 if(FAILED(hres)) {
239 jsdisp_release(this_obj);
240 return hres;
243 V_VT(retv) = VT_DISPATCH;
244 if(V_VT(&var) == VT_DISPATCH) {
245 jsdisp_release(this_obj);
246 V_DISPATCH(retv) = V_DISPATCH(&var);
247 }else {
248 VariantClear(&var);
249 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(this_obj);
251 return S_OK;
254 static HRESULT invoke_value_proc(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_disp, WORD flags, DISPPARAMS *dp,
255 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
257 vdisp_t vthis;
258 HRESULT hres;
260 if(this_disp)
261 set_disp(&vthis, this_disp);
262 else if(ctx->host_global)
263 set_disp(&vthis, ctx->host_global);
264 else
265 set_jsdisp(&vthis, ctx->global);
267 hres = function->value_proc(ctx, &vthis, flags, dp, retv, ei, caller);
269 vdisp_release(&vthis);
270 return hres;
273 static HRESULT call_function(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *args,
274 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
276 if(function->value_proc)
277 return invoke_value_proc(ctx, function, this_obj, DISPATCH_METHOD, args, retv, ei, caller);
279 return invoke_source(ctx, function, this_obj, args, retv, ei, caller);
282 static HRESULT function_to_string(FunctionInstance *function, BSTR *ret)
284 BSTR str;
286 static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
287 static const WCHAR native_suffixW[] =
288 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
290 if(function->value_proc) {
291 DWORD name_len;
293 name_len = strlenW(function->name);
294 str = SysAllocStringLen(NULL, sizeof(native_prefixW) + name_len*sizeof(WCHAR) + sizeof(native_suffixW));
295 if(!str)
296 return E_OUTOFMEMORY;
298 memcpy(str, native_prefixW, sizeof(native_prefixW));
299 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR), function->name, name_len*sizeof(WCHAR));
300 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR) + name_len, native_suffixW, sizeof(native_suffixW));
301 }else {
302 str = SysAllocStringLen(function->src_str, function->src_len);
303 if(!str)
304 return E_OUTOFMEMORY;
307 *ret = str;
308 return S_OK;
311 static HRESULT Function_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
312 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
314 FunctionInstance *This = function_from_vdisp(jsthis);
316 TRACE("%p %d\n", This, This->length);
318 switch(flags) {
319 case DISPATCH_PROPERTYGET:
320 V_VT(retv) = VT_I4;
321 V_I4(retv) = This->length;
322 break;
323 default:
324 FIXME("unimplemented flags %x\n", flags);
325 return E_NOTIMPL;
328 return S_OK;
331 static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
332 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
334 FunctionInstance *function;
335 BSTR str;
336 HRESULT hres;
338 TRACE("\n");
340 if(!(function = function_this(jsthis)))
341 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
343 hres = function_to_string(function, &str);
344 if(FAILED(hres))
345 return hres;
347 if(retv) {
348 V_VT(retv) = VT_BSTR;
349 V_BSTR(retv) = str;
350 }else {
351 SysFreeString(str);
353 return S_OK;
356 static HRESULT array_to_args(script_ctx_t *ctx, DispatchEx *arg_array, jsexcept_t *ei, IServiceProvider *caller,
357 DISPPARAMS *args)
359 VARIANT var, *argv;
360 DWORD length, i;
361 HRESULT hres;
363 hres = jsdisp_propget_name(arg_array, lengthW, &var, ei, NULL/*FIXME*/);
364 if(FAILED(hres))
365 return hres;
367 hres = to_uint32(ctx, &var, ei, &length);
368 VariantClear(&var);
369 if(FAILED(hres))
370 return hres;
372 argv = heap_alloc(length * sizeof(VARIANT));
373 if(!argv)
374 return E_OUTOFMEMORY;
376 for(i=0; i<length; i++) {
377 hres = jsdisp_get_idx(arg_array, i, argv+i, ei, caller);
378 if(hres == DISP_E_UNKNOWNNAME)
379 V_VT(argv+i) = VT_EMPTY;
380 else if(FAILED(hres)) {
381 while(i--)
382 VariantClear(argv+i);
383 heap_free(argv);
384 return hres;
388 args->cArgs = length;
389 args->rgvarg = argv;
390 return S_OK;
393 static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
394 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
396 FunctionInstance *function;
397 DISPPARAMS args = {NULL,NULL,0,0};
398 DWORD argc, i;
399 IDispatch *this_obj = NULL;
400 HRESULT hres = S_OK;
402 TRACE("\n");
404 if(!(function = function_this(jsthis)))
405 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
407 argc = arg_cnt(dp);
408 if(argc) {
409 VARIANT *v = get_arg(dp,0);
411 if(V_VT(v) != VT_EMPTY && V_VT(v) != VT_NULL) {
412 hres = to_object(ctx, v, &this_obj);
413 if(FAILED(hres))
414 return hres;
418 if(argc >= 2) {
419 DispatchEx *arg_array = NULL;
421 if(V_VT(get_arg(dp,1)) == VT_DISPATCH) {
422 arg_array = iface_to_jsdisp((IUnknown*)V_DISPATCH(get_arg(dp,1)));
423 if(arg_array &&
424 (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
425 jsdisp_release(arg_array);
426 arg_array = NULL;
430 if(arg_array) {
431 hres = array_to_args(ctx, arg_array, ei, caller, &args);
432 jsdisp_release(arg_array);
433 }else {
434 FIXME("throw TypeError\n");
435 hres = E_FAIL;
439 hres = call_function(ctx, function, this_obj, &args, retv, ei, caller);
441 if(this_obj)
442 IDispatch_Release(this_obj);
443 for(i=0; i<args.cArgs; i++)
444 VariantClear(args.rgvarg+i);
445 heap_free(args.rgvarg);
446 return hres;
449 static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
450 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
452 FunctionInstance *function;
453 DISPPARAMS args = {NULL,NULL,0,0};
454 IDispatch *this_obj = NULL;
455 DWORD argc;
456 HRESULT hres;
458 TRACE("\n");
460 if(!(function = function_this(jsthis)))
461 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
463 argc = arg_cnt(dp);
464 if(argc) {
465 VARIANT *v = get_arg(dp,0);
467 if(V_VT(v) != VT_EMPTY && V_VT(v) != VT_NULL) {
468 hres = to_object(ctx, v, &this_obj);
469 if(FAILED(hres))
470 return hres;
473 args.cArgs = argc-1;
476 if(args.cArgs)
477 args.rgvarg = dp->rgvarg + dp->cArgs - args.cArgs-1;
479 hres = call_function(ctx, function, this_obj, &args, retv, ei, caller);
481 if(this_obj)
482 IDispatch_Release(this_obj);
483 return hres;
486 HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
487 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
489 FunctionInstance *function;
491 TRACE("\n");
493 if(!is_vclass(jsthis, JSCLASS_FUNCTION)) {
494 ERR("dispex is not a function\n");
495 return E_FAIL;
498 function = (FunctionInstance*)jsthis->u.jsdisp;
500 switch(flags) {
501 case DISPATCH_METHOD:
502 if(function->value_proc)
503 return invoke_value_proc(ctx, function, get_this(dp), flags, dp, retv, ei, caller);
505 return invoke_source(ctx, function, get_this(dp), dp, retv, ei, caller);
507 case DISPATCH_PROPERTYGET: {
508 HRESULT hres;
509 BSTR str;
511 hres = function_to_string(function, &str);
512 if(FAILED(hres))
513 return hres;
515 V_VT(retv) = VT_BSTR;
516 V_BSTR(retv) = str;
517 break;
520 case DISPATCH_CONSTRUCT:
521 if(function->value_proc)
522 return invoke_value_proc(ctx, function, get_this(dp), flags, dp, retv, ei, caller);
524 return invoke_constructor(ctx, function, dp, retv, ei, caller);
526 default:
527 FIXME("not implemented flags %x\n", flags);
528 return E_NOTIMPL;
531 return S_OK;
534 static void Function_destructor(DispatchEx *dispex)
536 FunctionInstance *This = (FunctionInstance*)dispex;
538 if(This->parser)
539 parser_release(This->parser);
540 if(This->scope_chain)
541 scope_release(This->scope_chain);
542 heap_free(This);
545 static const builtin_prop_t Function_props[] = {
546 {applyW, Function_apply, PROPF_METHOD|2},
547 {callW, Function_call, PROPF_METHOD|1},
548 {lengthW, Function_length, 0},
549 {toStringW, Function_toString, PROPF_METHOD}
552 static const builtin_info_t Function_info = {
553 JSCLASS_FUNCTION,
554 {NULL, Function_value, 0},
555 sizeof(Function_props)/sizeof(*Function_props),
556 Function_props,
557 Function_destructor,
558 NULL
561 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
562 BOOL funcprot, DispatchEx *prototype, FunctionInstance **ret)
564 FunctionInstance *function;
565 HRESULT hres;
567 function = heap_alloc_zero(sizeof(FunctionInstance));
568 if(!function)
569 return E_OUTOFMEMORY;
571 if(funcprot)
572 hres = init_dispex(&function->dispex, ctx, &Function_info, prototype);
573 else if(builtin_info)
574 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
575 else
576 hres = init_dispex_from_constr(&function->dispex, ctx, &Function_info, ctx->function_constr);
577 if(FAILED(hres))
578 return hres;
580 function->flags = flags;
581 function->length = flags & PROPF_ARGMASK;
583 *ret = function;
584 return S_OK;
587 static HRESULT set_prototype(script_ctx_t *ctx, DispatchEx *dispex, DispatchEx *prototype)
589 jsexcept_t jsexcept;
590 VARIANT var;
592 V_VT(&var) = VT_DISPATCH;
593 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(prototype);
594 memset(&jsexcept, 0, sizeof(jsexcept));
596 return jsdisp_propput_name(dispex, prototypeW, &var, &jsexcept, NULL/*FIXME*/);
599 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
600 const builtin_info_t *builtin_info, DWORD flags, DispatchEx *prototype, DispatchEx **ret)
602 FunctionInstance *function;
603 HRESULT hres;
605 hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
606 if(FAILED(hres))
607 return hres;
609 if(builtin_info) {
610 VARIANT var;
612 V_VT(&var) = VT_I4;
613 V_I4(&var) = function->length;
614 hres = jsdisp_propput_const(&function->dispex, lengthW, &var);
617 if(SUCCEEDED(hres))
618 hres = set_prototype(ctx, &function->dispex, prototype);
619 if(FAILED(hres)) {
620 jsdisp_release(&function->dispex);
621 return hres;
624 function->value_proc = value_proc;
625 function->name = name;
627 *ret = &function->dispex;
628 return S_OK;
631 HRESULT create_source_function(parser_ctx_t *ctx, parameter_t *parameters, source_elements_t *source,
632 scope_chain_t *scope_chain, const WCHAR *src_str, DWORD src_len, DispatchEx **ret)
634 FunctionInstance *function;
635 DispatchEx *prototype;
636 parameter_t *iter;
637 DWORD length = 0;
638 HRESULT hres;
640 hres = create_object(ctx->script, NULL, &prototype);
641 if(FAILED(hres))
642 return hres;
644 hres = create_function(ctx->script, NULL, PROPF_CONSTR, FALSE, NULL, &function);
645 if(SUCCEEDED(hres)) {
646 hres = set_prototype(ctx->script, &function->dispex, prototype);
647 if(FAILED(hres))
648 jsdisp_release(&function->dispex);
650 jsdisp_release(prototype);
651 if(FAILED(hres))
652 return hres;
654 function->source = source;
655 function->parameters = parameters;
657 if(scope_chain) {
658 scope_addref(scope_chain);
659 function->scope_chain = scope_chain;
662 parser_addref(ctx);
663 function->parser = ctx;
665 for(iter = parameters; iter; iter = iter->next)
666 length++;
667 function->length = length;
669 function->src_str = src_str;
670 function->src_len = src_len;
672 *ret = &function->dispex;
673 return S_OK;
676 static HRESULT construct_function(script_ctx_t *ctx, DISPPARAMS *dp, jsexcept_t *ei, IDispatch **ret)
678 function_expression_t *expr;
679 WCHAR *str = NULL, *ptr;
680 DWORD argc, len = 0, l;
681 parser_ctx_t *parser;
682 DispatchEx *function;
683 BSTR *params = NULL;
684 int i=0, j=0;
685 HRESULT hres = S_OK;
687 static const WCHAR function_anonymousW[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
688 static const WCHAR function_beginW[] = {')',' ','{','\n'};
689 static const WCHAR function_endW[] = {'\n','}',0};
691 argc = arg_cnt(dp);
692 if(argc) {
693 params = heap_alloc(argc*sizeof(BSTR));
694 if(!params)
695 return E_OUTOFMEMORY;
697 if(argc > 2)
698 len = (argc-2)*2; /* separating commas */
699 for(i=0; i < argc; i++) {
700 hres = to_string(ctx, get_arg(dp,i), ei, params+i);
701 if(FAILED(hres))
702 break;
703 len += SysStringLen(params[i]);
707 if(SUCCEEDED(hres)) {
708 len += (sizeof(function_anonymousW) + sizeof(function_beginW) + sizeof(function_endW)) / sizeof(WCHAR);
709 str = heap_alloc(len*sizeof(WCHAR));
710 if(str) {
711 memcpy(str, function_anonymousW, sizeof(function_anonymousW));
712 ptr = str + sizeof(function_anonymousW)/sizeof(WCHAR);
713 if(argc > 1) {
714 while(1) {
715 l = SysStringLen(params[j]);
716 memcpy(ptr, params[j], l*sizeof(WCHAR));
717 ptr += l;
718 if(++j == argc-1)
719 break;
720 *ptr++ = ',';
721 *ptr++ = ' ';
724 memcpy(ptr, function_beginW, sizeof(function_beginW));
725 ptr += sizeof(function_beginW)/sizeof(WCHAR);
726 if(argc) {
727 l = SysStringLen(params[argc-1]);
728 memcpy(ptr, params[argc-1], l*sizeof(WCHAR));
729 ptr += l;
731 memcpy(ptr, function_endW, sizeof(function_endW));
733 TRACE("%s\n", debugstr_w(str));
734 }else {
735 hres = E_OUTOFMEMORY;
739 while(--i >= 0)
740 SysFreeString(params[i]);
741 heap_free(params);
742 if(FAILED(hres))
743 return hres;
745 hres = script_parse(ctx, str, NULL, &parser);
746 heap_free(str);
747 if(FAILED(hres))
748 return hres;
750 if(!parser->source || !parser->source->functions || parser->source->functions->next || parser->source->variables) {
751 ERR("Invalid parser result!\n");
752 parser_release(parser);
753 return E_UNEXPECTED;
755 expr = parser->source->functions->expr;
757 hres = create_source_function(parser, expr->parameter_list, expr->source_elements, NULL, expr->src_str,
758 expr->src_len, &function);
759 parser_release(parser);
760 if(FAILED(hres))
761 return hres;
763 *ret = (IDispatch*)_IDispatchEx_(function);
764 return S_OK;
767 static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
768 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
770 HRESULT hres;
772 TRACE("\n");
774 switch(flags) {
775 case DISPATCH_CONSTRUCT: {
776 IDispatch *ret;
778 hres = construct_function(ctx, dp, ei, &ret);
779 if(FAILED(hres))
780 return hres;
782 V_VT(retv) = VT_DISPATCH;
783 V_DISPATCH(retv) = ret;
784 break;
786 default:
787 FIXME("unimplemented flags %x\n", flags);
788 return E_NOTIMPL;
791 return S_OK;
794 static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
795 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
797 FIXME("\n");
798 return E_NOTIMPL;
801 HRESULT init_function_constr(script_ctx_t *ctx, DispatchEx *object_prototype)
803 FunctionInstance *prot, *constr;
804 HRESULT hres;
806 static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
808 hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, object_prototype, &prot);
809 if(FAILED(hres))
810 return hres;
812 prot->value_proc = FunctionProt_value;
813 prot->name = prototypeW;
815 hres = create_function(ctx, NULL, PROPF_CONSTR|1, TRUE, &prot->dispex, &constr);
816 if(SUCCEEDED(hres)) {
817 constr->value_proc = FunctionConstr_value;
818 constr->name = FunctionW;
819 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
820 if(FAILED(hres))
821 jsdisp_release(&constr->dispex);
823 jsdisp_release(&prot->dispex);
824 if(FAILED(hres))
825 return hres;
827 ctx->function_constr = &constr->dispex;
828 return S_OK;