ddraw: Use wined3d_get_adapter_display_mode() in d3d_device7_EnumTextureFormats().
[wine/multimedia.git] / dlls / jscript / function.c
blob5cde865725bc5fa04d7b7cb70d5dfed4f0c3a693
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 <assert.h>
21 #include "jscript.h"
22 #include "engine.h"
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
28 typedef struct {
29 jsdisp_t dispex;
30 builtin_invoke_t value_proc;
31 const WCHAR *name;
32 DWORD flags;
33 scope_chain_t *scope_chain;
34 bytecode_t *code;
35 function_code_t *func_code;
36 DWORD length;
37 jsdisp_t *arguments;
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};
56 static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
58 static IDispatch *get_this(DISPPARAMS *dp)
60 DWORD i;
62 for(i=0; i < dp->cNamedArgs; i++) {
63 if(dp->rgdispidNamedArgs[i] == DISPID_THIS) {
64 if(V_VT(dp->rgvarg+i) == VT_DISPATCH)
65 return V_DISPATCH(dp->rgvarg+i);
67 WARN("This is not VT_DISPATCH\n");
68 return NULL;
72 TRACE("no this passed\n");
73 return NULL;
76 static HRESULT init_parameters(jsdisp_t *var_disp, FunctionInstance *function, DISPPARAMS *dp,
77 jsexcept_t *ei)
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(i=0; i < function->func_code->param_cnt; i++) {
87 hres = jsdisp_propput_name(var_disp, function->func_code->params[i],
88 i < cargs ? get_arg(dp,i) : &var_empty, ei);
89 if(FAILED(hres))
90 return hres;
93 return S_OK;
96 static HRESULT Arguments_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
97 VARIANT *retv, jsexcept_t *ei)
99 FIXME("\n");
100 return E_NOTIMPL;
103 static const builtin_info_t Arguments_info = {
104 JSCLASS_ARGUMENTS,
105 {NULL, Arguments_value, 0},
106 0, NULL,
107 NULL,
108 NULL
111 static HRESULT create_arguments(script_ctx_t *ctx, IDispatch *calee, DISPPARAMS *dp,
112 jsexcept_t *ei, jsdisp_t **ret)
114 jsdisp_t *args;
115 VARIANT var;
116 DWORD i;
117 HRESULT hres;
119 static const WCHAR caleeW[] = {'c','a','l','l','e','e',0};
121 args = heap_alloc_zero(sizeof(jsdisp_t));
122 if(!args)
123 return E_OUTOFMEMORY;
125 hres = init_dispex_from_constr(args, ctx, &Arguments_info, ctx->object_constr);
126 if(FAILED(hres)) {
127 heap_free(args);
128 return hres;
131 for(i=0; i < arg_cnt(dp); i++) {
132 hres = jsdisp_propput_idx(args, i, get_arg(dp,i), ei);
133 if(FAILED(hres))
134 break;
137 if(SUCCEEDED(hres)) {
138 num_set_int(&var, arg_cnt(dp));
139 hres = jsdisp_propput_name(args, lengthW, &var, ei);
141 if(SUCCEEDED(hres)) {
142 V_VT(&var) = VT_DISPATCH;
143 V_DISPATCH(&var) = calee;
144 hres = jsdisp_propput_name(args, caleeW, &var, ei);
148 if(FAILED(hres)) {
149 jsdisp_release(args);
150 return hres;
153 *ret = args;
154 return S_OK;
157 static HRESULT create_var_disp(script_ctx_t *ctx, FunctionInstance *function, jsdisp_t *arg_disp,
158 DISPPARAMS *dp, jsexcept_t *ei, jsdisp_t **ret)
160 jsdisp_t *var_disp;
161 VARIANT var;
162 HRESULT hres;
164 hres = create_dispex(ctx, NULL, NULL, &var_disp);
165 if(FAILED(hres))
166 return hres;
168 var_set_jsdisp(&var, arg_disp);
169 hres = jsdisp_propput_name(var_disp, argumentsW, &var, ei);
170 if(SUCCEEDED(hres))
171 hres = init_parameters(var_disp, function, dp, ei);
172 if(FAILED(hres)) {
173 jsdisp_release(var_disp);
174 return hres;
177 *ret = var_disp;
178 return S_OK;
181 static HRESULT invoke_source(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, DISPPARAMS *dp,
182 VARIANT *retv, jsexcept_t *ei)
184 jsdisp_t *var_disp, *arg_disp;
185 exec_ctx_t *exec_ctx;
186 scope_chain_t *scope;
187 HRESULT hres;
189 if(!function->func_code) {
190 FIXME("no source\n");
191 return E_FAIL;
194 hres = create_arguments(ctx, to_disp(&function->dispex), dp, ei, &arg_disp);
195 if(FAILED(hres))
196 return hres;
198 hres = create_var_disp(ctx, function, arg_disp, dp, ei, &var_disp);
199 if(FAILED(hres)) {
200 jsdisp_release(arg_disp);
201 return hres;
204 hres = scope_push(function->scope_chain, var_disp, &scope);
205 if(SUCCEEDED(hres)) {
206 hres = create_exec_ctx(ctx, this_obj, var_disp, scope, FALSE, &exec_ctx);
207 scope_release(scope);
209 jsdisp_release(var_disp);
210 if(SUCCEEDED(hres)) {
211 jsdisp_t *prev_args;
213 prev_args = function->arguments;
214 function->arguments = arg_disp;
215 hres = exec_source(exec_ctx, function->code, function->func_code, FALSE, ei, retv);
216 function->arguments = prev_args;
218 jsdisp_release(arg_disp);
219 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)
228 jsdisp_t *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, to_disp(this_obj), dp, &var, ei);
237 if(FAILED(hres)) {
238 jsdisp_release(this_obj);
239 return hres;
242 if(V_VT(&var) == VT_DISPATCH) {
243 jsdisp_release(this_obj);
244 V_VT(retv) = VT_DISPATCH;
245 V_DISPATCH(retv) = V_DISPATCH(&var);
246 }else {
247 VariantClear(&var);
248 var_set_jsdisp(retv, 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)
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);
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)
275 if(function->value_proc)
276 return invoke_value_proc(ctx, function, this_obj, DISPATCH_METHOD, args, retv, ei);
278 return invoke_source(ctx, function, this_obj, args, retv, ei);
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->func_code->source, function->func_code->source_len);
302 if(!str)
303 return E_OUTOFMEMORY;
306 *ret = str;
307 return S_OK;
310 HRESULT Function_invoke(jsdisp_t *func_this, IDispatch *jsthis, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
312 FunctionInstance *function;
314 TRACE("func %p this %p\n", func_this, jsthis);
316 assert(is_class(func_this, JSCLASS_FUNCTION));
317 function = (FunctionInstance*)func_this;
319 if(function->value_proc)
320 return invoke_value_proc(function->dispex.ctx, function, jsthis, flags, dp, retv, ei);
322 if(flags == DISPATCH_CONSTRUCT)
323 return invoke_constructor(function->dispex.ctx, function, dp, retv, ei);
325 assert(flags == DISPATCH_METHOD);
326 return invoke_source(function->dispex.ctx, function, jsthis, dp, retv, ei);
329 static HRESULT Function_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
330 VARIANT *retv, jsexcept_t *ei)
332 FunctionInstance *This = function_from_vdisp(jsthis);
334 TRACE("%p %d\n", This, This->length);
336 switch(flags) {
337 case DISPATCH_PROPERTYGET:
338 num_set_int(retv, This->length);
339 break;
340 default:
341 FIXME("unimplemented flags %x\n", flags);
342 return E_NOTIMPL;
345 return S_OK;
348 static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
349 VARIANT *retv, jsexcept_t *ei)
351 FunctionInstance *function;
352 BSTR str;
353 HRESULT hres;
355 TRACE("\n");
357 if(!(function = function_this(jsthis)))
358 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
360 hres = function_to_string(function, &str);
361 if(FAILED(hres))
362 return hres;
364 if(retv) {
365 V_VT(retv) = VT_BSTR;
366 V_BSTR(retv) = str;
367 }else {
368 SysFreeString(str);
370 return S_OK;
373 static HRESULT array_to_args(script_ctx_t *ctx, jsdisp_t *arg_array, jsexcept_t *ei, DISPPARAMS *args)
375 VARIANT var, *argv;
376 DWORD length, i;
377 HRESULT hres;
379 hres = jsdisp_propget_name(arg_array, lengthW, &var, ei);
380 if(FAILED(hres))
381 return hres;
383 hres = to_uint32(ctx, &var, ei, &length);
384 VariantClear(&var);
385 if(FAILED(hres))
386 return hres;
388 argv = heap_alloc(length * sizeof(VARIANT));
389 if(!argv)
390 return E_OUTOFMEMORY;
392 for(i=0; i<length; i++) {
393 hres = jsdisp_get_idx(arg_array, i, argv+i, ei);
394 if(hres == DISP_E_UNKNOWNNAME)
395 V_VT(argv+i) = VT_EMPTY;
396 else if(FAILED(hres)) {
397 while(i--)
398 VariantClear(argv+i);
399 heap_free(argv);
400 return hres;
404 args->cArgs = length;
405 args->rgvarg = argv;
406 return S_OK;
409 static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
410 VARIANT *retv, jsexcept_t *ei)
412 FunctionInstance *function;
413 DISPPARAMS args = {NULL,NULL,0,0};
414 DWORD argc, i;
415 IDispatch *this_obj = NULL;
416 HRESULT hres = S_OK;
418 TRACE("\n");
420 if(!(function = function_this(jsthis)))
421 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
423 argc = arg_cnt(dp);
424 if(argc) {
425 VARIANT *v = get_arg(dp,0);
427 if(V_VT(v) != VT_EMPTY && V_VT(v) != VT_NULL) {
428 hres = to_object(ctx, v, &this_obj);
429 if(FAILED(hres))
430 return hres;
434 if(argc >= 2) {
435 jsdisp_t *arg_array = NULL;
437 if(V_VT(get_arg(dp,1)) == VT_DISPATCH) {
438 arg_array = iface_to_jsdisp((IUnknown*)V_DISPATCH(get_arg(dp,1)));
439 if(arg_array &&
440 (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
441 jsdisp_release(arg_array);
442 arg_array = NULL;
446 if(arg_array) {
447 hres = array_to_args(ctx, arg_array, ei, &args);
448 jsdisp_release(arg_array);
449 }else {
450 FIXME("throw TypeError\n");
451 hres = E_FAIL;
455 if(SUCCEEDED(hres))
456 hres = call_function(ctx, function, this_obj, &args, retv, ei);
458 if(this_obj)
459 IDispatch_Release(this_obj);
460 for(i=0; i<args.cArgs; i++)
461 VariantClear(args.rgvarg+i);
462 heap_free(args.rgvarg);
463 return hres;
466 static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
467 VARIANT *retv, jsexcept_t *ei)
469 FunctionInstance *function;
470 DISPPARAMS args = {NULL,NULL,0,0};
471 IDispatch *this_obj = NULL;
472 DWORD argc;
473 HRESULT hres;
475 TRACE("\n");
477 if(!(function = function_this(jsthis)))
478 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
480 argc = arg_cnt(dp);
481 if(argc) {
482 VARIANT *v = get_arg(dp,0);
484 if(V_VT(v) != VT_EMPTY && V_VT(v) != VT_NULL) {
485 hres = to_object(ctx, v, &this_obj);
486 if(FAILED(hres))
487 return hres;
490 args.cArgs = argc-1;
493 if(args.cArgs)
494 args.rgvarg = dp->rgvarg + dp->cArgs - args.cArgs-1;
496 hres = call_function(ctx, function, this_obj, &args, retv, ei);
498 if(this_obj)
499 IDispatch_Release(this_obj);
500 return hres;
503 HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
504 VARIANT *retv, jsexcept_t *ei)
506 FunctionInstance *function;
508 TRACE("\n");
510 if(!is_vclass(jsthis, JSCLASS_FUNCTION)) {
511 ERR("dispex is not a function\n");
512 return E_FAIL;
515 function = (FunctionInstance*)jsthis->u.jsdisp;
517 switch(flags) {
518 case DISPATCH_METHOD:
519 if(function->value_proc)
520 return invoke_value_proc(ctx, function, get_this(dp), flags, dp, retv, ei);
522 return invoke_source(ctx, function, get_this(dp), dp, retv, ei);
524 case DISPATCH_PROPERTYGET: {
525 HRESULT hres;
526 BSTR str;
528 hres = function_to_string(function, &str);
529 if(FAILED(hres))
530 return hres;
532 V_VT(retv) = VT_BSTR;
533 V_BSTR(retv) = str;
534 break;
537 case DISPATCH_CONSTRUCT:
538 if(function->value_proc)
539 return invoke_value_proc(ctx, function, get_this(dp), flags, dp, retv, ei);
541 return invoke_constructor(ctx, function, dp, retv, ei);
543 default:
544 FIXME("not implemented flags %x\n", flags);
545 return E_NOTIMPL;
548 return S_OK;
551 static HRESULT Function_arguments(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
552 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
554 FunctionInstance *function = (FunctionInstance*)jsthis->u.jsdisp;
555 HRESULT hres = S_OK;
557 TRACE("\n");
559 switch(flags) {
560 case DISPATCH_PROPERTYGET: {
561 if(function->arguments) {
562 jsdisp_addref(function->arguments);
563 var_set_jsdisp(retv, function->arguments);
564 }else {
565 V_VT(retv) = VT_NULL;
567 break;
569 case DISPATCH_PROPERTYPUT:
570 break;
571 default:
572 FIXME("unimplemented flags %x\n", flags);
573 hres = E_NOTIMPL;
576 return hres;
579 static void Function_destructor(jsdisp_t *dispex)
581 FunctionInstance *This = (FunctionInstance*)dispex;
583 if(This->code)
584 release_bytecode(This->code);
585 if(This->scope_chain)
586 scope_release(This->scope_chain);
587 heap_free(This);
590 static const builtin_prop_t Function_props[] = {
591 {applyW, Function_apply, PROPF_METHOD|2},
592 {argumentsW, Function_arguments, 0},
593 {callW, Function_call, PROPF_METHOD|1},
594 {lengthW, Function_length, 0},
595 {toStringW, Function_toString, PROPF_METHOD}
598 static const builtin_info_t Function_info = {
599 JSCLASS_FUNCTION,
600 {NULL, Function_value, 0},
601 sizeof(Function_props)/sizeof(*Function_props),
602 Function_props,
603 Function_destructor,
604 NULL
607 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
608 BOOL funcprot, jsdisp_t *prototype, FunctionInstance **ret)
610 FunctionInstance *function;
611 HRESULT hres;
613 function = heap_alloc_zero(sizeof(FunctionInstance));
614 if(!function)
615 return E_OUTOFMEMORY;
617 if(funcprot)
618 hres = init_dispex(&function->dispex, ctx, &Function_info, prototype);
619 else if(builtin_info)
620 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
621 else
622 hres = init_dispex_from_constr(&function->dispex, ctx, &Function_info, ctx->function_constr);
623 if(FAILED(hres))
624 return hres;
626 function->flags = flags;
627 function->length = flags & PROPF_ARGMASK;
629 *ret = function;
630 return S_OK;
633 static HRESULT set_prototype(script_ctx_t *ctx, jsdisp_t *dispex, jsdisp_t *prototype)
635 jsexcept_t jsexcept;
636 VARIANT var;
638 var_set_jsdisp(&var, prototype);
639 memset(&jsexcept, 0, sizeof(jsexcept));
641 return jsdisp_propput_name(dispex, prototypeW, &var, &jsexcept);
644 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
645 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
647 FunctionInstance *function;
648 HRESULT hres;
650 hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
651 if(FAILED(hres))
652 return hres;
654 if(builtin_info) {
655 VARIANT var;
657 num_set_int(&var, function->length);
658 hres = jsdisp_propput_const(&function->dispex, lengthW, &var);
661 if(SUCCEEDED(hres))
662 hres = set_prototype(ctx, &function->dispex, prototype);
663 if(FAILED(hres)) {
664 jsdisp_release(&function->dispex);
665 return hres;
668 function->value_proc = value_proc;
669 function->name = name;
671 *ret = &function->dispex;
672 return S_OK;
675 HRESULT create_source_function(script_ctx_t *ctx, bytecode_t *code, function_code_t *func_code,
676 scope_chain_t *scope_chain, jsdisp_t **ret)
678 FunctionInstance *function;
679 jsdisp_t *prototype;
680 HRESULT hres;
682 hres = create_object(ctx, NULL, &prototype);
683 if(FAILED(hres))
684 return hres;
686 hres = create_function(ctx, NULL, PROPF_CONSTR, FALSE, NULL, &function);
687 if(SUCCEEDED(hres)) {
688 hres = set_prototype(ctx, &function->dispex, prototype);
689 if(FAILED(hres))
690 jsdisp_release(&function->dispex);
692 jsdisp_release(prototype);
693 if(FAILED(hres))
694 return hres;
696 if(scope_chain) {
697 scope_addref(scope_chain);
698 function->scope_chain = scope_chain;
701 bytecode_addref(code);
702 function->code = code;
703 function->func_code = func_code;
704 function->length = function->func_code->param_cnt;
706 *ret = &function->dispex;
707 return S_OK;
710 static HRESULT construct_function(script_ctx_t *ctx, DISPPARAMS *dp, jsexcept_t *ei, IDispatch **ret)
712 WCHAR *str = NULL, *ptr;
713 DWORD argc, len = 0, l;
714 bytecode_t *code;
715 jsdisp_t *function;
716 BSTR *params = NULL;
717 int i=0, j=0;
718 HRESULT hres = S_OK;
720 static const WCHAR function_anonymousW[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
721 static const WCHAR function_beginW[] = {')',' ','{','\n'};
722 static const WCHAR function_endW[] = {'\n','}',0};
724 argc = arg_cnt(dp);
725 if(argc) {
726 params = heap_alloc(argc*sizeof(BSTR));
727 if(!params)
728 return E_OUTOFMEMORY;
730 if(argc > 2)
731 len = (argc-2)*2; /* separating commas */
732 for(i=0; i < argc; i++) {
733 hres = to_string(ctx, get_arg(dp,i), ei, params+i);
734 if(FAILED(hres))
735 break;
736 len += SysStringLen(params[i]);
740 if(SUCCEEDED(hres)) {
741 len += (sizeof(function_anonymousW) + sizeof(function_beginW) + sizeof(function_endW)) / sizeof(WCHAR);
742 str = heap_alloc(len*sizeof(WCHAR));
743 if(str) {
744 memcpy(str, function_anonymousW, sizeof(function_anonymousW));
745 ptr = str + sizeof(function_anonymousW)/sizeof(WCHAR);
746 if(argc > 1) {
747 while(1) {
748 l = SysStringLen(params[j]);
749 memcpy(ptr, params[j], l*sizeof(WCHAR));
750 ptr += l;
751 if(++j == argc-1)
752 break;
753 *ptr++ = ',';
754 *ptr++ = ' ';
757 memcpy(ptr, function_beginW, sizeof(function_beginW));
758 ptr += sizeof(function_beginW)/sizeof(WCHAR);
759 if(argc) {
760 l = SysStringLen(params[argc-1]);
761 memcpy(ptr, params[argc-1], l*sizeof(WCHAR));
762 ptr += l;
764 memcpy(ptr, function_endW, sizeof(function_endW));
766 TRACE("%s\n", debugstr_w(str));
767 }else {
768 hres = E_OUTOFMEMORY;
772 while(--i >= 0)
773 SysFreeString(params[i]);
774 heap_free(params);
775 if(FAILED(hres))
776 return hres;
778 hres = compile_script(ctx, str, NULL, FALSE, FALSE, &code);
779 heap_free(str);
780 if(FAILED(hres))
781 return hres;
783 if(code->global_code.func_cnt != 1 || code->global_code.var_cnt) {
784 ERR("Invalid parser result!\n");
785 release_bytecode(code);
786 return E_UNEXPECTED;
789 hres = create_source_function(ctx, code, code->global_code.funcs, NULL, &function);
790 release_bytecode(code);
791 if(FAILED(hres))
792 return hres;
794 *ret = to_disp(function);
795 return S_OK;
798 static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
799 VARIANT *retv, jsexcept_t *ei)
801 HRESULT hres;
803 TRACE("\n");
805 switch(flags) {
806 case DISPATCH_CONSTRUCT: {
807 IDispatch *ret;
809 hres = construct_function(ctx, dp, ei, &ret);
810 if(FAILED(hres))
811 return hres;
813 V_VT(retv) = VT_DISPATCH;
814 V_DISPATCH(retv) = ret;
815 break;
817 default:
818 FIXME("unimplemented flags %x\n", flags);
819 return E_NOTIMPL;
822 return S_OK;
825 static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
826 VARIANT *retv, jsexcept_t *ei)
828 FIXME("\n");
829 return E_NOTIMPL;
832 HRESULT init_function_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
834 FunctionInstance *prot, *constr;
835 HRESULT hres;
837 static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
839 hres = create_function(ctx, NULL, PROPF_CONSTR, TRUE, object_prototype, &prot);
840 if(FAILED(hres))
841 return hres;
843 prot->value_proc = FunctionProt_value;
844 prot->name = prototypeW;
846 hres = create_function(ctx, NULL, PROPF_CONSTR|1, TRUE, &prot->dispex, &constr);
847 if(SUCCEEDED(hres)) {
848 constr->value_proc = FunctionConstr_value;
849 constr->name = FunctionW;
850 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
851 if(FAILED(hres))
852 jsdisp_release(&constr->dispex);
854 jsdisp_release(&prot->dispex);
855 if(FAILED(hres))
856 return hres;
858 ctx->function_constr = &constr->dispex;
859 return S_OK;