msxml3/tests: Check the correct return value.
[wine/multimedia.git] / dlls / jscript / function.c
blob31d3be3caaddf429c9df26e9f9f62f3986356911
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 jsdisp_t dispex;
28 builtin_invoke_t value_proc;
29 const WCHAR *name;
30 DWORD flags;
31 scope_chain_t *scope_chain;
32 bytecode_t *code;
33 function_code_t *func_code;
34 DWORD length;
35 jsdisp_t *arguments;
36 } FunctionInstance;
38 static inline FunctionInstance *function_from_vdisp(vdisp_t *vdisp)
40 return (FunctionInstance*)vdisp->u.jsdisp;
43 static inline FunctionInstance *function_this(vdisp_t *jsthis)
45 return is_vclass(jsthis, JSCLASS_FUNCTION) ? function_from_vdisp(jsthis) : NULL;
48 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
50 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
51 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
52 static const WCHAR applyW[] = {'a','p','p','l','y',0};
53 static const WCHAR callW[] = {'c','a','l','l',0};
54 static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
56 static IDispatch *get_this(DISPPARAMS *dp)
58 DWORD i;
60 for(i=0; i < dp->cNamedArgs; i++) {
61 if(dp->rgdispidNamedArgs[i] == DISPID_THIS) {
62 if(V_VT(dp->rgvarg+i) == VT_DISPATCH)
63 return V_DISPATCH(dp->rgvarg+i);
65 WARN("This is not VT_DISPATCH\n");
66 return NULL;
70 TRACE("no this passed\n");
71 return NULL;
74 static HRESULT init_parameters(jsdisp_t *var_disp, FunctionInstance *function, DISPPARAMS *dp,
75 jsexcept_t *ei)
77 VARIANT var_empty;
78 DWORD cargs, i=0;
79 HRESULT hres;
81 V_VT(&var_empty) = VT_EMPTY;
82 cargs = arg_cnt(dp);
84 for(i=0; i < function->func_code->param_cnt; i++) {
85 hres = jsdisp_propput_name(var_disp, function->func_code->params[i],
86 i < cargs ? get_arg(dp,i) : &var_empty, ei);
87 if(FAILED(hres))
88 return hres;
91 return S_OK;
94 static HRESULT Arguments_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
95 VARIANT *retv, jsexcept_t *ei)
97 FIXME("\n");
98 return E_NOTIMPL;
101 static const builtin_info_t Arguments_info = {
102 JSCLASS_ARGUMENTS,
103 {NULL, Arguments_value, 0},
104 0, NULL,
105 NULL,
106 NULL
109 static HRESULT create_arguments(script_ctx_t *ctx, IDispatch *calee, DISPPARAMS *dp,
110 jsexcept_t *ei, jsdisp_t **ret)
112 jsdisp_t *args;
113 VARIANT var;
114 DWORD i;
115 HRESULT hres;
117 static const WCHAR caleeW[] = {'c','a','l','l','e','e',0};
119 args = heap_alloc_zero(sizeof(jsdisp_t));
120 if(!args)
121 return E_OUTOFMEMORY;
123 hres = init_dispex_from_constr(args, ctx, &Arguments_info, ctx->object_constr);
124 if(FAILED(hres)) {
125 heap_free(args);
126 return hres;
129 for(i=0; i < arg_cnt(dp); i++) {
130 hres = jsdisp_propput_idx(args, i, get_arg(dp,i), ei);
131 if(FAILED(hres))
132 break;
135 if(SUCCEEDED(hres)) {
136 V_VT(&var) = VT_I4;
137 V_I4(&var) = arg_cnt(dp);
138 hres = jsdisp_propput_name(args, lengthW, &var, ei);
140 if(SUCCEEDED(hres)) {
141 V_VT(&var) = VT_DISPATCH;
142 V_DISPATCH(&var) = calee;
143 hres = jsdisp_propput_name(args, caleeW, &var, ei);
147 if(FAILED(hres)) {
148 jsdisp_release(args);
149 return hres;
152 *ret = args;
153 return S_OK;
156 static HRESULT create_var_disp(script_ctx_t *ctx, FunctionInstance *function, jsdisp_t *arg_disp,
157 DISPPARAMS *dp, jsexcept_t *ei, jsdisp_t **ret)
159 jsdisp_t *var_disp;
160 VARIANT var;
161 HRESULT hres;
163 hres = create_dispex(ctx, NULL, NULL, &var_disp);
164 if(FAILED(hres))
165 return hres;
167 var_set_jsdisp(&var, arg_disp);
168 hres = jsdisp_propput_name(var_disp, argumentsW, &var, ei);
169 if(SUCCEEDED(hres))
170 hres = init_parameters(var_disp, function, dp, ei);
171 if(FAILED(hres)) {
172 jsdisp_release(var_disp);
173 return hres;
176 *ret = var_disp;
177 return S_OK;
180 static HRESULT invoke_source(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *dp,
181 VARIANT *retv, jsexcept_t *ei)
183 jsdisp_t *var_disp, *arg_disp;
184 exec_ctx_t *exec_ctx;
185 scope_chain_t *scope;
186 HRESULT hres;
188 if(!function->func_code) {
189 FIXME("no source\n");
190 return E_FAIL;
193 hres = create_arguments(ctx, to_disp(&function->dispex), dp, ei, &arg_disp);
194 if(FAILED(hres))
195 return hres;
197 hres = create_var_disp(ctx, function, arg_disp, dp, ei, &var_disp);
198 if(FAILED(hres)) {
199 jsdisp_release(arg_disp);
200 return hres;
203 hres = scope_push(function->scope_chain, var_disp, &scope);
204 if(SUCCEEDED(hres)) {
205 hres = create_exec_ctx(ctx, this_obj, var_disp, scope, FALSE, &exec_ctx);
206 scope_release(scope);
208 jsdisp_release(var_disp);
209 if(SUCCEEDED(hres)) {
210 jsdisp_t *prev_args;
212 prev_args = function->arguments;
213 function->arguments = arg_disp;
214 hres = exec_source(exec_ctx, function->code, function->func_code, FALSE, ei, retv);
215 function->arguments = prev_args;
217 jsdisp_release(arg_disp);
218 exec_release(exec_ctx);
221 return hres;
224 static HRESULT invoke_constructor(script_ctx_t *ctx, FunctionInstance *function, DISPPARAMS *dp,
225 VARIANT *retv, jsexcept_t *ei)
227 jsdisp_t *this_obj;
228 VARIANT var;
229 HRESULT hres;
231 hres = create_object(ctx, &function->dispex, &this_obj);
232 if(FAILED(hres))
233 return hres;
235 hres = invoke_source(ctx, function, to_disp(this_obj), dp, &var, ei);
236 if(FAILED(hres)) {
237 jsdisp_release(this_obj);
238 return hres;
241 if(V_VT(&var) == VT_DISPATCH) {
242 jsdisp_release(this_obj);
243 V_VT(retv) = VT_DISPATCH;
244 V_DISPATCH(retv) = V_DISPATCH(&var);
245 }else {
246 VariantClear(&var);
247 var_set_jsdisp(retv, this_obj);
249 return S_OK;
252 static HRESULT invoke_value_proc(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_disp, WORD flags, DISPPARAMS *dp,
253 VARIANT *retv, jsexcept_t *ei)
255 vdisp_t vthis;
256 HRESULT hres;
258 if(this_disp)
259 set_disp(&vthis, this_disp);
260 else if(ctx->host_global)
261 set_disp(&vthis, ctx->host_global);
262 else
263 set_jsdisp(&vthis, ctx->global);
265 hres = function->value_proc(ctx, &vthis, flags, dp, retv, ei);
267 vdisp_release(&vthis);
268 return hres;
271 static HRESULT call_function(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *args,
272 VARIANT *retv, jsexcept_t *ei)
274 if(function->value_proc)
275 return invoke_value_proc(ctx, function, this_obj, DISPATCH_METHOD, args, retv, ei);
277 return invoke_source(ctx, function, this_obj, args, retv, ei);
280 static HRESULT function_to_string(FunctionInstance *function, BSTR *ret)
282 BSTR str;
284 static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
285 static const WCHAR native_suffixW[] =
286 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
288 if(function->value_proc) {
289 DWORD name_len;
291 name_len = strlenW(function->name);
292 str = SysAllocStringLen(NULL, sizeof(native_prefixW) + name_len*sizeof(WCHAR) + sizeof(native_suffixW));
293 if(!str)
294 return E_OUTOFMEMORY;
296 memcpy(str, native_prefixW, sizeof(native_prefixW));
297 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR), function->name, name_len*sizeof(WCHAR));
298 memcpy(str + sizeof(native_prefixW)/sizeof(WCHAR) + name_len, native_suffixW, sizeof(native_suffixW));
299 }else {
300 str = SysAllocStringLen(function->func_code->source, function->func_code->source_len);
301 if(!str)
302 return E_OUTOFMEMORY;
305 *ret = str;
306 return S_OK;
309 static HRESULT Function_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
310 VARIANT *retv, jsexcept_t *ei)
312 FunctionInstance *This = function_from_vdisp(jsthis);
314 TRACE("%p %d\n", This, This->length);
316 switch(flags) {
317 case DISPATCH_PROPERTYGET:
318 V_VT(retv) = VT_I4;
319 V_I4(retv) = This->length;
320 break;
321 default:
322 FIXME("unimplemented flags %x\n", flags);
323 return E_NOTIMPL;
326 return S_OK;
329 static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
330 VARIANT *retv, jsexcept_t *ei)
332 FunctionInstance *function;
333 BSTR str;
334 HRESULT hres;
336 TRACE("\n");
338 if(!(function = function_this(jsthis)))
339 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
341 hres = function_to_string(function, &str);
342 if(FAILED(hres))
343 return hres;
345 if(retv) {
346 V_VT(retv) = VT_BSTR;
347 V_BSTR(retv) = str;
348 }else {
349 SysFreeString(str);
351 return S_OK;
354 static HRESULT array_to_args(script_ctx_t *ctx, jsdisp_t *arg_array, jsexcept_t *ei, DISPPARAMS *args)
356 VARIANT var, *argv;
357 DWORD length, i;
358 HRESULT hres;
360 hres = jsdisp_propget_name(arg_array, lengthW, &var, ei);
361 if(FAILED(hres))
362 return hres;
364 hres = to_uint32(ctx, &var, ei, &length);
365 VariantClear(&var);
366 if(FAILED(hres))
367 return hres;
369 argv = heap_alloc(length * sizeof(VARIANT));
370 if(!argv)
371 return E_OUTOFMEMORY;
373 for(i=0; i<length; i++) {
374 hres = jsdisp_get_idx(arg_array, i, argv+i, ei);
375 if(hres == DISP_E_UNKNOWNNAME)
376 V_VT(argv+i) = VT_EMPTY;
377 else 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)
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, JS_E_FUNCTION_EXPECTED, 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 jsdisp_t *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, &args);
429 jsdisp_release(arg_array);
430 }else {
431 FIXME("throw TypeError\n");
432 hres = E_FAIL;
436 if(SUCCEEDED(hres))
437 hres = call_function(ctx, function, this_obj, &args, retv, ei);
439 if(this_obj)
440 IDispatch_Release(this_obj);
441 for(i=0; i<args.cArgs; i++)
442 VariantClear(args.rgvarg+i);
443 heap_free(args.rgvarg);
444 return hres;
447 static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
448 VARIANT *retv, jsexcept_t *ei)
450 FunctionInstance *function;
451 DISPPARAMS args = {NULL,NULL,0,0};
452 IDispatch *this_obj = NULL;
453 DWORD argc;
454 HRESULT hres;
456 TRACE("\n");
458 if(!(function = function_this(jsthis)))
459 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
461 argc = arg_cnt(dp);
462 if(argc) {
463 VARIANT *v = get_arg(dp,0);
465 if(V_VT(v) != VT_EMPTY && V_VT(v) != VT_NULL) {
466 hres = to_object(ctx, v, &this_obj);
467 if(FAILED(hres))
468 return hres;
471 args.cArgs = argc-1;
474 if(args.cArgs)
475 args.rgvarg = dp->rgvarg + dp->cArgs - args.cArgs-1;
477 hres = call_function(ctx, function, this_obj, &args, retv, ei);
479 if(this_obj)
480 IDispatch_Release(this_obj);
481 return hres;
484 HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
485 VARIANT *retv, jsexcept_t *ei)
487 FunctionInstance *function;
489 TRACE("\n");
491 if(!is_vclass(jsthis, JSCLASS_FUNCTION)) {
492 ERR("dispex is not a function\n");
493 return E_FAIL;
496 function = (FunctionInstance*)jsthis->u.jsdisp;
498 switch(flags) {
499 case DISPATCH_METHOD:
500 if(function->value_proc)
501 return invoke_value_proc(ctx, function, get_this(dp), flags, dp, retv, ei);
503 return invoke_source(ctx, function, get_this(dp), dp, retv, ei);
505 case DISPATCH_PROPERTYGET: {
506 HRESULT hres;
507 BSTR str;
509 hres = function_to_string(function, &str);
510 if(FAILED(hres))
511 return hres;
513 V_VT(retv) = VT_BSTR;
514 V_BSTR(retv) = str;
515 break;
518 case DISPATCH_CONSTRUCT:
519 if(function->value_proc)
520 return invoke_value_proc(ctx, function, get_this(dp), flags, dp, retv, ei);
522 return invoke_constructor(ctx, function, dp, retv, ei);
524 default:
525 FIXME("not implemented flags %x\n", flags);
526 return E_NOTIMPL;
529 return S_OK;
532 static HRESULT Function_arguments(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
533 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
535 FunctionInstance *function = (FunctionInstance*)jsthis->u.jsdisp;
536 HRESULT hres = S_OK;
538 TRACE("\n");
540 switch(flags) {
541 case DISPATCH_PROPERTYGET: {
542 if(function->arguments) {
543 jsdisp_addref(function->arguments);
544 var_set_jsdisp(retv, function->arguments);
545 }else {
546 V_VT(retv) = VT_NULL;
548 break;
550 case DISPATCH_PROPERTYPUT:
551 break;
552 default:
553 FIXME("unimplemented flags %x\n", flags);
554 hres = E_NOTIMPL;
557 return hres;
560 static void Function_destructor(jsdisp_t *dispex)
562 FunctionInstance *This = (FunctionInstance*)dispex;
564 if(This->code)
565 release_bytecode(This->code);
566 if(This->scope_chain)
567 scope_release(This->scope_chain);
568 heap_free(This);
571 static const builtin_prop_t Function_props[] = {
572 {applyW, Function_apply, PROPF_METHOD|2},
573 {argumentsW, Function_arguments, 0},
574 {callW, Function_call, PROPF_METHOD|1},
575 {lengthW, Function_length, 0},
576 {toStringW, Function_toString, PROPF_METHOD}
579 static const builtin_info_t Function_info = {
580 JSCLASS_FUNCTION,
581 {NULL, Function_value, 0},
582 sizeof(Function_props)/sizeof(*Function_props),
583 Function_props,
584 Function_destructor,
585 NULL
588 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
589 BOOL funcprot, jsdisp_t *prototype, FunctionInstance **ret)
591 FunctionInstance *function;
592 HRESULT hres;
594 function = heap_alloc_zero(sizeof(FunctionInstance));
595 if(!function)
596 return E_OUTOFMEMORY;
598 if(funcprot)
599 hres = init_dispex(&function->dispex, ctx, &Function_info, prototype);
600 else if(builtin_info)
601 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
602 else
603 hres = init_dispex_from_constr(&function->dispex, ctx, &Function_info, ctx->function_constr);
604 if(FAILED(hres))
605 return hres;
607 function->flags = flags;
608 function->length = flags & PROPF_ARGMASK;
610 *ret = function;
611 return S_OK;
614 static HRESULT set_prototype(script_ctx_t *ctx, jsdisp_t *dispex, jsdisp_t *prototype)
616 jsexcept_t jsexcept;
617 VARIANT var;
619 var_set_jsdisp(&var, prototype);
620 memset(&jsexcept, 0, sizeof(jsexcept));
622 return jsdisp_propput_name(dispex, prototypeW, &var, &jsexcept);
625 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
626 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
628 FunctionInstance *function;
629 HRESULT hres;
631 hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
632 if(FAILED(hres))
633 return hres;
635 if(builtin_info) {
636 VARIANT var;
638 V_VT(&var) = VT_I4;
639 V_I4(&var) = function->length;
640 hres = jsdisp_propput_const(&function->dispex, lengthW, &var);
643 if(SUCCEEDED(hres))
644 hres = set_prototype(ctx, &function->dispex, prototype);
645 if(FAILED(hres)) {
646 jsdisp_release(&function->dispex);
647 return hres;
650 function->value_proc = value_proc;
651 function->name = name;
653 *ret = &function->dispex;
654 return S_OK;
657 HRESULT create_source_function(script_ctx_t *ctx, bytecode_t *code, function_code_t *func_code,
658 scope_chain_t *scope_chain, jsdisp_t **ret)
660 FunctionInstance *function;
661 jsdisp_t *prototype;
662 HRESULT hres;
664 hres = create_object(ctx, NULL, &prototype);
665 if(FAILED(hres))
666 return hres;
668 hres = create_function(ctx, NULL, PROPF_CONSTR, FALSE, NULL, &function);
669 if(SUCCEEDED(hres)) {
670 hres = set_prototype(ctx, &function->dispex, prototype);
671 if(FAILED(hres))
672 jsdisp_release(&function->dispex);
674 jsdisp_release(prototype);
675 if(FAILED(hres))
676 return hres;
678 if(scope_chain) {
679 scope_addref(scope_chain);
680 function->scope_chain = scope_chain;
683 bytecode_addref(code);
684 function->code = code;
685 function->func_code = func_code;
686 function->length = function->func_code->param_cnt;
688 *ret = &function->dispex;
689 return S_OK;
692 static HRESULT construct_function(script_ctx_t *ctx, DISPPARAMS *dp, jsexcept_t *ei, IDispatch **ret)
694 WCHAR *str = NULL, *ptr;
695 DWORD argc, len = 0, l;
696 bytecode_t *code;
697 jsdisp_t *function;
698 BSTR *params = NULL;
699 int i=0, j=0;
700 HRESULT hres = S_OK;
702 static const WCHAR function_anonymousW[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
703 static const WCHAR function_beginW[] = {')',' ','{','\n'};
704 static const WCHAR function_endW[] = {'\n','}',0};
706 argc = arg_cnt(dp);
707 if(argc) {
708 params = heap_alloc(argc*sizeof(BSTR));
709 if(!params)
710 return E_OUTOFMEMORY;
712 if(argc > 2)
713 len = (argc-2)*2; /* separating commas */
714 for(i=0; i < argc; i++) {
715 hres = to_string(ctx, get_arg(dp,i), ei, params+i);
716 if(FAILED(hres))
717 break;
718 len += SysStringLen(params[i]);
722 if(SUCCEEDED(hres)) {
723 len += (sizeof(function_anonymousW) + sizeof(function_beginW) + sizeof(function_endW)) / sizeof(WCHAR);
724 str = heap_alloc(len*sizeof(WCHAR));
725 if(str) {
726 memcpy(str, function_anonymousW, sizeof(function_anonymousW));
727 ptr = str + sizeof(function_anonymousW)/sizeof(WCHAR);
728 if(argc > 1) {
729 while(1) {
730 l = SysStringLen(params[j]);
731 memcpy(ptr, params[j], l*sizeof(WCHAR));
732 ptr += l;
733 if(++j == argc-1)
734 break;
735 *ptr++ = ',';
736 *ptr++ = ' ';
739 memcpy(ptr, function_beginW, sizeof(function_beginW));
740 ptr += sizeof(function_beginW)/sizeof(WCHAR);
741 if(argc) {
742 l = SysStringLen(params[argc-1]);
743 memcpy(ptr, params[argc-1], l*sizeof(WCHAR));
744 ptr += l;
746 memcpy(ptr, function_endW, sizeof(function_endW));
748 TRACE("%s\n", debugstr_w(str));
749 }else {
750 hres = E_OUTOFMEMORY;
754 while(--i >= 0)
755 SysFreeString(params[i]);
756 heap_free(params);
757 if(FAILED(hres))
758 return hres;
760 hres = compile_script(ctx, str, NULL, FALSE, FALSE, &code);
761 heap_free(str);
762 if(FAILED(hres))
763 return hres;
765 if(code->global_code.func_cnt != 1 || code->global_code.var_cnt) {
766 ERR("Invalid parser result!\n");
767 release_bytecode(code);
768 return E_UNEXPECTED;
771 hres = create_source_function(ctx, code, code->global_code.funcs, NULL, &function);
772 release_bytecode(code);
773 if(FAILED(hres))
774 return hres;
776 *ret = to_disp(function);
777 return S_OK;
780 static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
781 VARIANT *retv, jsexcept_t *ei)
783 HRESULT hres;
785 TRACE("\n");
787 switch(flags) {
788 case DISPATCH_CONSTRUCT: {
789 IDispatch *ret;
791 hres = construct_function(ctx, dp, ei, &ret);
792 if(FAILED(hres))
793 return hres;
795 V_VT(retv) = VT_DISPATCH;
796 V_DISPATCH(retv) = ret;
797 break;
799 default:
800 FIXME("unimplemented flags %x\n", flags);
801 return E_NOTIMPL;
804 return S_OK;
807 static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
808 VARIANT *retv, jsexcept_t *ei)
810 FIXME("\n");
811 return E_NOTIMPL;
814 HRESULT init_function_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
816 FunctionInstance *prot, *constr;
817 HRESULT hres;
819 static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
821 hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, object_prototype, &prot);
822 if(FAILED(hres))
823 return hres;
825 prot->value_proc = FunctionProt_value;
826 prot->name = prototypeW;
828 hres = create_function(ctx, NULL, PROPF_CONSTR|1, TRUE, &prot->dispex, &constr);
829 if(SUCCEEDED(hres)) {
830 constr->value_proc = FunctionConstr_value;
831 constr->name = FunctionW;
832 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
833 if(FAILED(hres))
834 jsdisp_release(&constr->dispex);
836 jsdisp_release(&prot->dispex);
837 if(FAILED(hres))
838 return hres;
840 ctx->function_constr = &constr->dispex;
841 return S_OK;