msvcp110: Add tr2_sys__File_size_wchar implementation and test.
[wine/multimedia.git] / dlls / jscript / function.c
blob0ac6c618d271690121ff8dda99b3fff7578750ca
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 typedef struct {
41 jsdisp_t jsdisp;
42 FunctionInstance *function;
43 jsdisp_t *var_obj;
44 } ArgumentsInstance;
46 static inline FunctionInstance *function_from_jsdisp(jsdisp_t *jsdisp)
48 return CONTAINING_RECORD(jsdisp, FunctionInstance, dispex);
51 static inline FunctionInstance *function_from_vdisp(vdisp_t *vdisp)
53 return function_from_jsdisp(vdisp->u.jsdisp);
56 static inline FunctionInstance *function_this(vdisp_t *jsthis)
58 return is_vclass(jsthis, JSCLASS_FUNCTION) ? function_from_vdisp(jsthis) : NULL;
61 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
63 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
64 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
65 static const WCHAR applyW[] = {'a','p','p','l','y',0};
66 static const WCHAR callW[] = {'c','a','l','l',0};
67 static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
69 static HRESULT init_parameters(jsdisp_t *var_disp, FunctionInstance *function, unsigned argc, jsval_t *argv)
71 DWORD i=0;
72 HRESULT hres;
74 for(i=0; i < function->func_code->param_cnt; i++) {
75 hres = jsdisp_propput_name(var_disp, function->func_code->params[i],
76 i < argc ? argv[i] : jsval_undefined());
77 if(FAILED(hres))
78 return hres;
81 return S_OK;
84 static HRESULT Arguments_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
85 jsval_t *r)
87 FIXME("\n");
88 return E_NOTIMPL;
91 static void Arguments_destructor(jsdisp_t *jsdisp)
93 ArgumentsInstance *arguments = (ArgumentsInstance*)jsdisp;
95 jsdisp_release(&arguments->function->dispex);
96 jsdisp_release(arguments->var_obj);
97 heap_free(arguments);
100 static unsigned Arguments_idx_length(jsdisp_t *jsdisp)
102 ArgumentsInstance *arguments = (ArgumentsInstance*)jsdisp;
103 return arguments->function->length;
106 static HRESULT Arguments_idx_get(jsdisp_t *jsdisp, unsigned idx, jsval_t *res)
108 ArgumentsInstance *arguments = (ArgumentsInstance*)jsdisp;
110 TRACE("%p[%u]\n", arguments, idx);
112 /* FIXME: Accessing by name won't work for duplicated argument names */
113 return jsdisp_propget_name(arguments->var_obj, arguments->function->func_code->params[idx], res);
116 static HRESULT Arguments_idx_put(jsdisp_t *jsdisp, unsigned idx, jsval_t val)
118 ArgumentsInstance *arguments = (ArgumentsInstance*)jsdisp;
120 TRACE("%p[%u] = %s\n", arguments, idx, debugstr_jsval(val));
122 /* FIXME: Accessing by name won't work for duplicated argument names */
123 return jsdisp_propput_name(arguments->var_obj, arguments->function->func_code->params[idx], val);
126 static const builtin_info_t Arguments_info = {
127 JSCLASS_ARGUMENTS,
128 {NULL, Arguments_value, 0},
129 0, NULL,
130 Arguments_destructor,
131 NULL,
132 Arguments_idx_length,
133 Arguments_idx_get,
134 Arguments_idx_put
137 static HRESULT create_arguments(script_ctx_t *ctx, FunctionInstance *calee, jsdisp_t *var_obj,
138 unsigned argc, jsval_t *argv, jsdisp_t **ret)
140 ArgumentsInstance *args;
141 unsigned i;
142 HRESULT hres;
144 static const WCHAR caleeW[] = {'c','a','l','l','e','e',0};
146 args = heap_alloc_zero(sizeof(*args));
147 if(!args)
148 return E_OUTOFMEMORY;
150 hres = init_dispex_from_constr(&args->jsdisp, ctx, &Arguments_info, ctx->object_constr);
151 if(FAILED(hres)) {
152 heap_free(args);
153 return hres;
156 jsdisp_addref(&calee->dispex);
157 args->function = calee;
158 args->var_obj = jsdisp_addref(var_obj);
160 /* Store unnamed arguments directly in arguments object */
161 for(i = calee->length; i < argc; i++) {
162 WCHAR buf[12];
164 static const WCHAR formatW[] = {'%','d',0};
166 sprintfW(buf, formatW, i);
167 hres = jsdisp_propput_dontenum(&args->jsdisp, buf, argv[i]);
168 if(FAILED(hres))
169 break;
172 if(SUCCEEDED(hres)) {
173 hres = jsdisp_propput_dontenum(&args->jsdisp, lengthW, jsval_number(argc));
174 if(SUCCEEDED(hres))
175 hres = jsdisp_propput_dontenum(&args->jsdisp, caleeW, jsval_disp(to_disp(&calee->dispex)));
177 if(FAILED(hres)) {
178 jsdisp_release(&args->jsdisp);
179 return hres;
182 *ret = &args->jsdisp;
183 return S_OK;
186 static HRESULT create_var_disp(script_ctx_t *ctx, FunctionInstance *function, unsigned argc, jsval_t *argv, jsdisp_t **ret)
188 jsdisp_t *var_disp;
189 HRESULT hres;
191 hres = create_dispex(ctx, NULL, NULL, &var_disp);
192 if(FAILED(hres))
193 return hres;
195 hres = init_parameters(var_disp, function, argc, argv);
196 if(FAILED(hres)) {
197 jsdisp_release(var_disp);
198 return hres;
201 *ret = var_disp;
202 return S_OK;
205 static HRESULT invoke_source(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, unsigned argc, jsval_t *argv,
206 jsval_t *r)
208 jsdisp_t *var_disp, *arg_disp;
209 exec_ctx_t *exec_ctx;
210 scope_chain_t *scope;
211 HRESULT hres;
213 if(ctx->state == SCRIPTSTATE_UNINITIALIZED || ctx->state == SCRIPTSTATE_CLOSED) {
214 WARN("Script engine state does not allow running code.\n");
215 return E_UNEXPECTED;
218 if(!function->func_code) {
219 FIXME("no source\n");
220 return E_FAIL;
223 hres = create_var_disp(ctx, function, argc, argv, &var_disp);
224 if(FAILED(hres))
225 return hres;
227 hres = create_arguments(ctx, function, var_disp, argc, argv, &arg_disp);
228 if(FAILED(hres)) {
229 jsdisp_release(var_disp);
230 return hres;
233 hres = jsdisp_propput(var_disp, argumentsW, PROPF_DONTDELETE, jsval_obj(arg_disp));
234 if(FAILED(hres)) {
235 jsdisp_release(arg_disp);
236 jsdisp_release(var_disp);
237 return hres;
240 hres = scope_push(function->scope_chain, var_disp, to_disp(var_disp), &scope);
241 if(SUCCEEDED(hres)) {
242 hres = create_exec_ctx(ctx, this_obj, var_disp, scope, FALSE, &exec_ctx);
243 scope_release(scope);
245 if(SUCCEEDED(hres)) {
246 jsdisp_t *prev_args;
248 prev_args = function->arguments;
249 function->arguments = arg_disp;
250 hres = exec_source(exec_ctx, function->code, function->func_code, FALSE, r);
251 function->arguments = prev_args;
253 exec_release(exec_ctx);
257 /* Reset arguments value to cut the reference cycle. Note that since all activation contexts have
258 * their own arguments property, it's impossible to use prototype's one during name lookup */
259 jsdisp_propput_name(var_disp, argumentsW, jsval_undefined());
261 jsdisp_release(arg_disp);
262 jsdisp_release(var_disp);
263 return hres;
266 static HRESULT invoke_constructor(script_ctx_t *ctx, FunctionInstance *function, unsigned argc, jsval_t *argv,
267 jsval_t *r)
269 jsdisp_t *this_obj;
270 jsval_t var;
271 HRESULT hres;
273 hres = create_object(ctx, &function->dispex, &this_obj);
274 if(FAILED(hres))
275 return hres;
277 hres = invoke_source(ctx, function, to_disp(this_obj), argc, argv, &var);
278 if(FAILED(hres)) {
279 jsdisp_release(this_obj);
280 return hres;
283 if(is_object_instance(var)) {
284 jsdisp_release(this_obj);
285 *r = var;
286 }else {
287 jsval_release(var);
288 *r = jsval_obj(this_obj);
290 return S_OK;
293 static HRESULT invoke_value_proc(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_disp, WORD flags,
294 unsigned argc, jsval_t *argv, jsval_t *r)
296 vdisp_t vthis;
297 HRESULT hres;
299 if(this_disp)
300 set_disp(&vthis, this_disp);
301 else if(ctx->host_global)
302 set_disp(&vthis, ctx->host_global);
303 else
304 set_jsdisp(&vthis, ctx->global);
306 hres = function->value_proc(ctx, &vthis, flags, argc, argv, r);
308 vdisp_release(&vthis);
309 return hres;
312 static HRESULT call_function(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj,
313 unsigned argc, jsval_t *argv, jsval_t *r)
315 if(function->value_proc)
316 return invoke_value_proc(ctx, function, this_obj, DISPATCH_METHOD, argc, argv, r);
318 return invoke_source(ctx, function, this_obj, argc, argv, r);
321 static HRESULT function_to_string(FunctionInstance *function, jsstr_t **ret)
323 jsstr_t *str;
325 static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
326 static const WCHAR native_suffixW[] =
327 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
329 if(function->value_proc) {
330 DWORD name_len;
331 WCHAR *ptr;
333 name_len = strlenW(function->name);
334 ptr = jsstr_alloc_buf((sizeof(native_prefixW)+sizeof(native_suffixW))/sizeof(WCHAR) + name_len, &str);
335 if(!ptr)
336 return E_OUTOFMEMORY;
338 memcpy(ptr, native_prefixW, sizeof(native_prefixW));
339 memcpy(ptr += sizeof(native_prefixW)/sizeof(WCHAR), function->name, name_len*sizeof(WCHAR));
340 memcpy(ptr + name_len, native_suffixW, sizeof(native_suffixW));
341 }else {
342 str = jsstr_alloc_len(function->func_code->source, function->func_code->source_len);
343 if(!str)
344 return E_OUTOFMEMORY;
347 *ret = str;
348 return S_OK;
351 HRESULT Function_invoke(jsdisp_t *func_this, IDispatch *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
353 FunctionInstance *function;
355 TRACE("func %p this %p\n", func_this, jsthis);
357 assert(is_class(func_this, JSCLASS_FUNCTION));
358 function = (FunctionInstance*)func_this;
360 if(function->value_proc)
361 return invoke_value_proc(function->dispex.ctx, function, jsthis, flags, argc, argv, r);
363 if(flags == DISPATCH_CONSTRUCT)
364 return invoke_constructor(function->dispex.ctx, function, argc, argv, r);
366 assert(flags == DISPATCH_METHOD);
367 return invoke_source(function->dispex.ctx, function, jsthis, argc, argv, r);
370 static HRESULT Function_get_length(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
372 TRACE("%p\n", jsthis);
374 *r = jsval_number(function_from_jsdisp(jsthis)->length);
375 return S_OK;
378 static HRESULT Function_set_length(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t value)
380 FIXME("\n");
381 return E_NOTIMPL;
384 static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
385 jsval_t *r)
387 FunctionInstance *function;
388 jsstr_t *str;
389 HRESULT hres;
391 TRACE("\n");
393 if(!(function = function_this(jsthis)))
394 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
396 hres = function_to_string(function, &str);
397 if(FAILED(hres))
398 return hres;
400 if(r)
401 *r = jsval_string(str);
402 else
403 jsstr_release(str);
404 return S_OK;
407 static HRESULT array_to_args(script_ctx_t *ctx, jsdisp_t *arg_array, unsigned *argc, jsval_t **ret)
409 jsval_t *argv, val;
410 DWORD length, i;
411 HRESULT hres;
413 hres = jsdisp_propget_name(arg_array, lengthW, &val);
414 if(FAILED(hres))
415 return hres;
417 hres = to_uint32(ctx, val, &length);
418 jsval_release(val);
419 if(FAILED(hres))
420 return hres;
422 argv = heap_alloc(length * sizeof(*argv));
423 if(!argv)
424 return E_OUTOFMEMORY;
426 for(i=0; i<length; i++) {
427 hres = jsdisp_get_idx(arg_array, i, argv+i);
428 if(hres == DISP_E_UNKNOWNNAME) {
429 argv[i] = jsval_undefined();
430 }else if(FAILED(hres)) {
431 while(i--)
432 jsval_release(argv[i]);
433 heap_free(argv);
434 return hres;
438 *argc = length;
439 *ret = argv;
440 return S_OK;
443 static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
445 FunctionInstance *function;
446 jsval_t *args = NULL;
447 unsigned i, cnt = 0;
448 IDispatch *this_obj = NULL;
449 HRESULT hres = S_OK;
451 TRACE("\n");
453 if(!(function = function_this(jsthis)))
454 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
456 if(argc) {
457 if(!is_undefined(argv[0]) && !is_null(argv[0])) {
458 hres = to_object(ctx, argv[0], &this_obj);
459 if(FAILED(hres))
460 return hres;
464 if(argc >= 2) {
465 jsdisp_t *arg_array = NULL;
467 if(is_object_instance(argv[1])) {
468 arg_array = iface_to_jsdisp((IUnknown*)get_object(argv[1]));
469 if(arg_array &&
470 (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
471 jsdisp_release(arg_array);
472 arg_array = NULL;
476 if(arg_array) {
477 hres = array_to_args(ctx, arg_array, &cnt, &args);
478 jsdisp_release(arg_array);
479 }else {
480 FIXME("throw TypeError\n");
481 hres = E_FAIL;
485 if(SUCCEEDED(hres))
486 hres = call_function(ctx, function, this_obj, cnt, args, r);
488 if(this_obj)
489 IDispatch_Release(this_obj);
490 for(i=0; i < cnt; i++)
491 jsval_release(args[i]);
492 heap_free(args);
493 return hres;
496 static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
497 jsval_t *r)
499 FunctionInstance *function;
500 IDispatch *this_obj = NULL;
501 unsigned cnt = 0;
502 HRESULT hres;
504 TRACE("\n");
506 if(!(function = function_this(jsthis)))
507 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
509 if(argc) {
510 if(!is_undefined(argv[0]) && !is_null(argv[0])) {
511 hres = to_object(ctx, argv[0], &this_obj);
512 if(FAILED(hres))
513 return hres;
516 cnt = argc-1;
519 hres = call_function(ctx, function, this_obj, cnt, argv+1, r);
521 if(this_obj)
522 IDispatch_Release(this_obj);
523 return hres;
526 HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
527 jsval_t *r)
529 FunctionInstance *function;
531 TRACE("\n");
533 if(!is_vclass(jsthis, JSCLASS_FUNCTION)) {
534 ERR("dispex is not a function\n");
535 return E_FAIL;
538 function = (FunctionInstance*)jsthis->u.jsdisp;
540 assert(function->value_proc != NULL);
541 return invoke_value_proc(ctx, function, NULL, flags, argc, argv, r);
544 HRESULT Function_get_value(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
546 jsstr_t *str;
547 HRESULT hres;
549 TRACE("\n");
551 hres = function_to_string(function_from_jsdisp(jsthis), &str);
552 if(FAILED(hres))
553 return hres;
555 *r = jsval_string(str);
556 return S_OK;
559 static HRESULT Function_get_arguments(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
561 FunctionInstance *function = function_from_jsdisp(jsthis);
563 TRACE("\n");
565 *r = function->arguments ? jsval_obj(jsdisp_addref(function->arguments)) : jsval_null();
566 return S_OK;
569 static void Function_destructor(jsdisp_t *dispex)
571 FunctionInstance *This = (FunctionInstance*)dispex;
573 if(This->code)
574 release_bytecode(This->code);
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, NULL, 0, Function_get_arguments, builtin_set_const},
583 {callW, Function_call, PROPF_METHOD|1},
584 {lengthW, NULL, 0, Function_get_length, Function_set_length},
585 {toStringW, Function_toString, PROPF_METHOD}
588 static const builtin_info_t Function_info = {
589 JSCLASS_FUNCTION,
590 DEFAULT_FUNCTION_VALUE,
591 sizeof(Function_props)/sizeof(*Function_props),
592 Function_props,
593 Function_destructor,
594 NULL
597 static const builtin_prop_t FunctionInst_props[] = {
598 {argumentsW, NULL, 0, Function_get_arguments, builtin_set_const},
599 {lengthW, NULL, 0, Function_get_length, Function_set_length}
602 static const builtin_info_t FunctionInst_info = {
603 JSCLASS_FUNCTION,
604 DEFAULT_FUNCTION_VALUE,
605 sizeof(FunctionInst_props)/sizeof(*FunctionInst_props),
606 FunctionInst_props,
607 Function_destructor,
608 NULL
611 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
612 BOOL funcprot, jsdisp_t *prototype, FunctionInstance **ret)
614 FunctionInstance *function;
615 HRESULT hres;
617 function = heap_alloc_zero(sizeof(FunctionInstance));
618 if(!function)
619 return E_OUTOFMEMORY;
621 if(funcprot)
622 hres = init_dispex(&function->dispex, ctx, builtin_info, prototype);
623 else if(builtin_info)
624 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
625 else
626 hres = init_dispex_from_constr(&function->dispex, ctx, &FunctionInst_info, ctx->function_constr);
627 if(FAILED(hres)) {
628 heap_free(function);
629 return hres;
632 function->flags = flags;
633 function->length = flags & PROPF_ARGMASK;
635 *ret = function;
636 return S_OK;
639 static inline HRESULT set_prototype(script_ctx_t *ctx, jsdisp_t *dispex, jsdisp_t *prototype)
641 return jsdisp_propput_dontenum(dispex, prototypeW, jsval_obj(prototype));
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 hres = jsdisp_propput_const(&function->dispex, lengthW, jsval_number(function->length));
656 if(SUCCEEDED(hres))
657 hres = set_prototype(ctx, &function->dispex, prototype);
658 if(FAILED(hres)) {
659 jsdisp_release(&function->dispex);
660 return hres;
663 function->value_proc = value_proc;
664 function->name = name;
666 *ret = &function->dispex;
667 return S_OK;
670 static HRESULT set_constructor_prop(script_ctx_t *ctx, jsdisp_t *constr, jsdisp_t *prot)
672 static const WCHAR constructorW[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
674 return jsdisp_propput_dontenum(prot, constructorW, jsval_obj(constr));
677 HRESULT create_builtin_constructor(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
678 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
680 jsdisp_t *constr;
681 HRESULT hres;
683 hres = create_builtin_function(ctx, value_proc, name, builtin_info, flags, prototype, &constr);
684 if(FAILED(hres))
685 return hres;
687 hres = set_constructor_prop(ctx, constr, prototype);
688 if(FAILED(hres)) {
689 jsdisp_release(constr);
690 return hres;
693 *ret = constr;
694 return S_OK;
697 HRESULT create_source_function(script_ctx_t *ctx, bytecode_t *code, function_code_t *func_code,
698 scope_chain_t *scope_chain, jsdisp_t **ret)
700 FunctionInstance *function;
701 jsdisp_t *prototype;
702 HRESULT hres;
704 hres = create_object(ctx, NULL, &prototype);
705 if(FAILED(hres))
706 return hres;
708 hres = create_function(ctx, NULL, PROPF_CONSTR, FALSE, NULL, &function);
709 if(SUCCEEDED(hres)) {
710 hres = set_prototype(ctx, &function->dispex, prototype);
711 if(SUCCEEDED(hres))
712 hres = set_constructor_prop(ctx, &function->dispex, prototype);
713 if(FAILED(hres))
714 jsdisp_release(&function->dispex);
716 jsdisp_release(prototype);
717 if(FAILED(hres))
718 return hres;
720 if(scope_chain) {
721 scope_addref(scope_chain);
722 function->scope_chain = scope_chain;
725 bytecode_addref(code);
726 function->code = code;
727 function->func_code = func_code;
728 function->length = function->func_code->param_cnt;
730 *ret = &function->dispex;
731 return S_OK;
734 static HRESULT construct_function(script_ctx_t *ctx, unsigned argc, jsval_t *argv, IDispatch **ret)
736 WCHAR *str = NULL, *ptr;
737 unsigned len = 0, i = 0;
738 bytecode_t *code;
739 jsdisp_t *function;
740 jsstr_t **params = NULL;
741 int j = 0;
742 HRESULT hres = S_OK;
744 static const WCHAR function_anonymousW[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
745 static const WCHAR function_beginW[] = {')',' ','{','\n'};
746 static const WCHAR function_endW[] = {'\n','}',0};
748 if(argc) {
749 params = heap_alloc(argc*sizeof(*params));
750 if(!params)
751 return E_OUTOFMEMORY;
753 if(argc > 2)
754 len = (argc-2)*2; /* separating commas */
755 for(i=0; i < argc; i++) {
756 hres = to_string(ctx, argv[i], params+i);
757 if(FAILED(hres))
758 break;
759 len += jsstr_length(params[i]);
763 if(SUCCEEDED(hres)) {
764 len += (sizeof(function_anonymousW) + sizeof(function_beginW) + sizeof(function_endW)) / sizeof(WCHAR);
765 str = heap_alloc(len*sizeof(WCHAR));
766 if(str) {
767 memcpy(str, function_anonymousW, sizeof(function_anonymousW));
768 ptr = str + sizeof(function_anonymousW)/sizeof(WCHAR);
769 if(argc > 1) {
770 while(1) {
771 ptr += jsstr_flush(params[j], ptr);
772 if(++j == argc-1)
773 break;
774 *ptr++ = ',';
775 *ptr++ = ' ';
778 memcpy(ptr, function_beginW, sizeof(function_beginW));
779 ptr += sizeof(function_beginW)/sizeof(WCHAR);
780 if(argc)
781 ptr += jsstr_flush(params[argc-1], ptr);
782 memcpy(ptr, function_endW, sizeof(function_endW));
784 TRACE("%s\n", debugstr_w(str));
785 }else {
786 hres = E_OUTOFMEMORY;
790 while(i)
791 jsstr_release(params[--i]);
792 heap_free(params);
793 if(FAILED(hres))
794 return hres;
796 hres = compile_script(ctx, str, NULL, NULL, FALSE, FALSE, &code);
797 heap_free(str);
798 if(FAILED(hres))
799 return hres;
801 if(code->global_code.func_cnt != 1 || code->global_code.var_cnt) {
802 ERR("Invalid parser result!\n");
803 release_bytecode(code);
804 return E_UNEXPECTED;
807 hres = create_source_function(ctx, code, code->global_code.funcs, NULL, &function);
808 release_bytecode(code);
809 if(FAILED(hres))
810 return hres;
812 *ret = to_disp(function);
813 return S_OK;
816 static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
817 jsval_t *r)
819 HRESULT hres;
821 TRACE("\n");
823 switch(flags) {
824 case DISPATCH_CONSTRUCT: {
825 IDispatch *ret;
827 hres = construct_function(ctx, argc, argv, &ret);
828 if(FAILED(hres))
829 return hres;
831 *r = jsval_disp(ret);
832 break;
834 default:
835 FIXME("unimplemented flags %x\n", flags);
836 return E_NOTIMPL;
839 return S_OK;
842 static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
843 jsval_t *r)
845 FIXME("\n");
846 return E_NOTIMPL;
849 HRESULT init_function_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
851 FunctionInstance *prot, *constr;
852 HRESULT hres;
854 static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
856 hres = create_function(ctx, &Function_info, PROPF_CONSTR, TRUE, object_prototype, &prot);
857 if(FAILED(hres))
858 return hres;
860 prot->value_proc = FunctionProt_value;
861 prot->name = prototypeW;
863 hres = create_function(ctx, &FunctionInst_info, PROPF_CONSTR|1, TRUE, &prot->dispex, &constr);
864 if(SUCCEEDED(hres)) {
865 constr->value_proc = FunctionConstr_value;
866 constr->name = FunctionW;
867 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
868 if(SUCCEEDED(hres))
869 hres = set_constructor_prop(ctx, &constr->dispex, &prot->dispex);
870 if(FAILED(hres))
871 jsdisp_release(&constr->dispex);
873 jsdisp_release(&prot->dispex);
874 if(FAILED(hres))
875 return hres;
877 ctx->function_constr = &constr->dispex;
878 return S_OK;