wined3d: Use the texture dimension helpers in surface_is_full_rect().
[wine.git] / dlls / jscript / function.c
blob1e8d563cafd64bb272ea16178f82f81c5bcb77cb
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 } FunctionInstance;
39 typedef struct {
40 jsdisp_t jsdisp;
41 FunctionInstance *function;
42 jsdisp_t *var_obj;
43 } ArgumentsInstance;
45 static inline FunctionInstance *function_from_jsdisp(jsdisp_t *jsdisp)
47 return CONTAINING_RECORD(jsdisp, FunctionInstance, dispex);
50 static inline FunctionInstance *function_from_vdisp(vdisp_t *vdisp)
52 return function_from_jsdisp(vdisp->u.jsdisp);
55 static inline FunctionInstance *function_this(vdisp_t *jsthis)
57 return is_vclass(jsthis, JSCLASS_FUNCTION) ? function_from_vdisp(jsthis) : NULL;
60 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
62 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
63 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
64 static const WCHAR applyW[] = {'a','p','p','l','y',0};
65 static const WCHAR callW[] = {'c','a','l','l',0};
66 static const WCHAR argumentsW[] = {'a','r','g','u','m','e','n','t','s',0};
68 static HRESULT init_parameters(jsdisp_t *var_disp, FunctionInstance *function, unsigned argc, jsval_t *argv)
70 DWORD i=0;
71 HRESULT hres;
73 for(i=0; i < function->func_code->param_cnt; i++) {
74 hres = jsdisp_propput_name(var_disp, function->func_code->params[i],
75 i < argc ? argv[i] : jsval_undefined());
76 if(FAILED(hres))
77 return hres;
80 return S_OK;
83 static HRESULT Arguments_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
84 jsval_t *r)
86 FIXME("\n");
87 return E_NOTIMPL;
90 static void Arguments_destructor(jsdisp_t *jsdisp)
92 ArgumentsInstance *arguments = (ArgumentsInstance*)jsdisp;
94 jsdisp_release(&arguments->function->dispex);
95 jsdisp_release(arguments->var_obj);
96 heap_free(arguments);
99 static unsigned Arguments_idx_length(jsdisp_t *jsdisp)
101 ArgumentsInstance *arguments = (ArgumentsInstance*)jsdisp;
102 return arguments->function->length;
105 static HRESULT Arguments_idx_get(jsdisp_t *jsdisp, unsigned idx, jsval_t *res)
107 ArgumentsInstance *arguments = (ArgumentsInstance*)jsdisp;
109 TRACE("%p[%u]\n", arguments, idx);
111 /* FIXME: Accessing by name won't work for duplicated argument names */
112 return jsdisp_propget_name(arguments->var_obj, arguments->function->func_code->params[idx], res);
115 static HRESULT Arguments_idx_put(jsdisp_t *jsdisp, unsigned idx, jsval_t val)
117 ArgumentsInstance *arguments = (ArgumentsInstance*)jsdisp;
119 TRACE("%p[%u] = %s\n", arguments, idx, debugstr_jsval(val));
121 /* FIXME: Accessing by name won't work for duplicated argument names */
122 return jsdisp_propput_name(arguments->var_obj, arguments->function->func_code->params[idx], val);
125 static const builtin_info_t Arguments_info = {
126 JSCLASS_ARGUMENTS,
127 {NULL, Arguments_value, 0},
128 0, NULL,
129 Arguments_destructor,
130 NULL,
131 Arguments_idx_length,
132 Arguments_idx_get,
133 Arguments_idx_put
136 static HRESULT create_arguments(script_ctx_t *ctx, FunctionInstance *calee, jsdisp_t *var_obj,
137 unsigned argc, jsval_t *argv, jsdisp_t **ret)
139 ArgumentsInstance *args;
140 unsigned i;
141 HRESULT hres;
143 static const WCHAR caleeW[] = {'c','a','l','l','e','e',0};
145 args = heap_alloc_zero(sizeof(*args));
146 if(!args)
147 return E_OUTOFMEMORY;
149 hres = init_dispex_from_constr(&args->jsdisp, ctx, &Arguments_info, ctx->object_constr);
150 if(FAILED(hres)) {
151 heap_free(args);
152 return hres;
155 jsdisp_addref(&calee->dispex);
156 args->function = calee;
157 args->var_obj = jsdisp_addref(var_obj);
159 /* Store unnamed arguments directly in arguments object */
160 for(i = calee->length; i < argc; i++) {
161 WCHAR buf[12];
163 static const WCHAR formatW[] = {'%','d',0};
165 sprintfW(buf, formatW, i);
166 hres = jsdisp_propput_dontenum(&args->jsdisp, buf, argv[i]);
167 if(FAILED(hres))
168 break;
171 if(SUCCEEDED(hres)) {
172 hres = jsdisp_propput_dontenum(&args->jsdisp, lengthW, jsval_number(argc));
173 if(SUCCEEDED(hres))
174 hres = jsdisp_propput_dontenum(&args->jsdisp, caleeW, jsval_disp(to_disp(&calee->dispex)));
176 if(FAILED(hres)) {
177 jsdisp_release(&args->jsdisp);
178 return hres;
181 *ret = &args->jsdisp;
182 return S_OK;
185 static HRESULT create_var_disp(script_ctx_t *ctx, FunctionInstance *function, unsigned argc, jsval_t *argv, jsdisp_t **ret)
187 jsdisp_t *var_disp;
188 HRESULT hres;
190 hres = create_dispex(ctx, NULL, NULL, &var_disp);
191 if(FAILED(hres))
192 return hres;
194 hres = init_parameters(var_disp, function, argc, argv);
195 if(FAILED(hres)) {
196 jsdisp_release(var_disp);
197 return hres;
200 *ret = var_disp;
201 return S_OK;
204 static HRESULT invoke_source(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj, unsigned argc, jsval_t *argv,
205 BOOL is_constructor, BOOL caller_execs_source, jsval_t *r)
207 jsdisp_t *var_disp, *arg_disp;
208 scope_chain_t *scope;
209 HRESULT hres;
211 if(ctx->state == SCRIPTSTATE_UNINITIALIZED || ctx->state == SCRIPTSTATE_CLOSED) {
212 WARN("Script engine state does not allow running code.\n");
213 return E_UNEXPECTED;
216 if(!function->func_code) {
217 FIXME("no source\n");
218 return E_FAIL;
221 hres = create_var_disp(ctx, function, argc, argv, &var_disp);
222 if(FAILED(hres))
223 return hres;
225 hres = create_arguments(ctx, function, var_disp, argc, argv, &arg_disp);
226 if(FAILED(hres)) {
227 jsdisp_release(var_disp);
228 return hres;
231 hres = jsdisp_propput(var_disp, argumentsW, PROPF_DONTDELETE, jsval_obj(arg_disp));
232 if(FAILED(hres)) {
233 jsdisp_release(arg_disp);
234 jsdisp_release(var_disp);
235 return hres;
238 hres = scope_push(function->scope_chain, var_disp, to_disp(var_disp), &scope);
239 if(SUCCEEDED(hres)) {
240 DWORD exec_flags = 0;
242 if(caller_execs_source)
243 exec_flags |= EXEC_RETURN_TO_INTERP;
244 if(is_constructor)
245 exec_flags |= EXEC_CONSTRUCTOR;
246 hres = exec_source(ctx, exec_flags, function->code, function->func_code, scope, this_obj,
247 &function->dispex, var_disp, arg_disp, r);
249 scope_release(scope);
252 jsdisp_release(arg_disp);
253 jsdisp_release(var_disp);
254 return hres;
257 static HRESULT invoke_value_proc(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_disp, WORD flags,
258 unsigned argc, jsval_t *argv, jsval_t *r)
260 vdisp_t vthis;
261 HRESULT hres;
263 if(this_disp)
264 set_disp(&vthis, this_disp);
265 else if(ctx->host_global)
266 set_disp(&vthis, ctx->host_global);
267 else
268 set_jsdisp(&vthis, ctx->global);
270 hres = function->value_proc(ctx, &vthis, flags, argc, argv, r);
272 vdisp_release(&vthis);
273 return hres;
276 static HRESULT call_function(script_ctx_t *ctx, FunctionInstance *function, IDispatch *this_obj,
277 unsigned argc, jsval_t *argv, BOOL caller_execs_source, jsval_t *r)
279 if(function->value_proc)
280 return invoke_value_proc(ctx, function, this_obj, DISPATCH_METHOD, argc, argv, r);
282 return invoke_source(ctx, function, this_obj, argc, argv, FALSE, caller_execs_source, r);
285 static HRESULT function_to_string(FunctionInstance *function, jsstr_t **ret)
287 jsstr_t *str;
289 static const WCHAR native_prefixW[] = {'\n','f','u','n','c','t','i','o','n',' '};
290 static const WCHAR native_suffixW[] =
291 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
293 if(function->value_proc) {
294 DWORD name_len;
295 WCHAR *ptr;
297 name_len = strlenW(function->name);
298 ptr = jsstr_alloc_buf((sizeof(native_prefixW)+sizeof(native_suffixW))/sizeof(WCHAR) + name_len, &str);
299 if(!ptr)
300 return E_OUTOFMEMORY;
302 memcpy(ptr, native_prefixW, sizeof(native_prefixW));
303 memcpy(ptr += sizeof(native_prefixW)/sizeof(WCHAR), function->name, name_len*sizeof(WCHAR));
304 memcpy(ptr + name_len, native_suffixW, sizeof(native_suffixW));
305 }else {
306 str = jsstr_alloc_len(function->func_code->source, function->func_code->source_len);
307 if(!str)
308 return E_OUTOFMEMORY;
311 *ret = str;
312 return S_OK;
315 HRESULT Function_invoke(jsdisp_t *func_this, IDispatch *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
317 const BOOL caller_execs_source = (flags & DISPATCH_JSCRIPT_CALLEREXECSSOURCE) != 0;
318 FunctionInstance *function;
320 TRACE("func %p this %p\n", func_this, jsthis);
322 assert(is_class(func_this, JSCLASS_FUNCTION));
323 function = (FunctionInstance*)func_this;
325 flags &= ~DISPATCH_JSCRIPT_INTERNAL_MASK;
326 if(function->value_proc)
327 return invoke_value_proc(function->dispex.ctx, function, jsthis, flags, argc, argv, r);
329 if(flags == DISPATCH_CONSTRUCT) {
330 jsdisp_t *this_obj;
331 HRESULT hres;
333 hres = create_object(function->dispex.ctx, &function->dispex, &this_obj);
334 if(FAILED(hres))
335 return hres;
337 hres = invoke_source(function->dispex.ctx, function, to_disp(this_obj), argc, argv, TRUE, caller_execs_source, r);
338 jsdisp_release(this_obj);
339 return hres;
342 assert(flags == DISPATCH_METHOD);
343 return invoke_source(function->dispex.ctx, function, jsthis, argc, argv, FALSE, caller_execs_source, r);
346 static HRESULT Function_get_length(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
348 TRACE("%p\n", jsthis);
350 *r = jsval_number(function_from_jsdisp(jsthis)->length);
351 return S_OK;
354 static HRESULT Function_set_length(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t value)
356 FIXME("\n");
357 return E_NOTIMPL;
360 static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
361 jsval_t *r)
363 FunctionInstance *function;
364 jsstr_t *str;
365 HRESULT hres;
367 TRACE("\n");
369 if(!(function = function_this(jsthis)))
370 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
372 hres = function_to_string(function, &str);
373 if(FAILED(hres))
374 return hres;
376 if(r)
377 *r = jsval_string(str);
378 else
379 jsstr_release(str);
380 return S_OK;
383 static HRESULT array_to_args(script_ctx_t *ctx, jsdisp_t *arg_array, unsigned *argc, jsval_t **ret)
385 jsval_t *argv, val;
386 DWORD length, i;
387 HRESULT hres;
389 hres = jsdisp_propget_name(arg_array, lengthW, &val);
390 if(FAILED(hres))
391 return hres;
393 hres = to_uint32(ctx, val, &length);
394 jsval_release(val);
395 if(FAILED(hres))
396 return hres;
398 argv = heap_alloc(length * sizeof(*argv));
399 if(!argv)
400 return E_OUTOFMEMORY;
402 for(i=0; i<length; i++) {
403 hres = jsdisp_get_idx(arg_array, i, argv+i);
404 if(hres == DISP_E_UNKNOWNNAME) {
405 argv[i] = jsval_undefined();
406 }else if(FAILED(hres)) {
407 while(i--)
408 jsval_release(argv[i]);
409 heap_free(argv);
410 return hres;
414 *argc = length;
415 *ret = argv;
416 return S_OK;
419 static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
421 FunctionInstance *function;
422 jsval_t *args = NULL;
423 unsigned i, cnt = 0;
424 IDispatch *this_obj = NULL;
425 HRESULT hres = S_OK;
427 TRACE("\n");
429 if(!(function = function_this(jsthis)))
430 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
432 if(argc) {
433 if(!is_undefined(argv[0]) && !is_null(argv[0])) {
434 hres = to_object(ctx, argv[0], &this_obj);
435 if(FAILED(hres))
436 return hres;
440 if(argc >= 2) {
441 jsdisp_t *arg_array = NULL;
443 if(is_object_instance(argv[1])) {
444 arg_array = iface_to_jsdisp((IUnknown*)get_object(argv[1]));
445 if(arg_array &&
446 (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
447 jsdisp_release(arg_array);
448 arg_array = NULL;
452 if(arg_array) {
453 hres = array_to_args(ctx, arg_array, &cnt, &args);
454 jsdisp_release(arg_array);
455 }else {
456 FIXME("throw TypeError\n");
457 hres = E_FAIL;
461 if(SUCCEEDED(hres))
462 hres = call_function(ctx, function, this_obj, cnt, args, (flags & DISPATCH_JSCRIPT_CALLEREXECSSOURCE) != 0, r);
464 if(this_obj)
465 IDispatch_Release(this_obj);
466 for(i=0; i < cnt; i++)
467 jsval_release(args[i]);
468 heap_free(args);
469 return hres;
472 static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
473 jsval_t *r)
475 FunctionInstance *function;
476 IDispatch *this_obj = NULL;
477 unsigned cnt = 0;
478 HRESULT hres;
480 TRACE("\n");
482 if(!(function = function_this(jsthis)))
483 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
485 if(argc) {
486 if(!is_undefined(argv[0]) && !is_null(argv[0])) {
487 hres = to_object(ctx, argv[0], &this_obj);
488 if(FAILED(hres))
489 return hres;
492 cnt = argc-1;
495 hres = call_function(ctx, function, this_obj, cnt, argv+1, (flags & DISPATCH_JSCRIPT_CALLEREXECSSOURCE) != 0, r);
497 if(this_obj)
498 IDispatch_Release(this_obj);
499 return hres;
502 HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
503 jsval_t *r)
505 FunctionInstance *function;
507 TRACE("\n");
509 if(!is_vclass(jsthis, JSCLASS_FUNCTION)) {
510 ERR("dispex is not a function\n");
511 return E_FAIL;
514 function = (FunctionInstance*)jsthis->u.jsdisp;
516 assert(function->value_proc != NULL);
517 return invoke_value_proc(ctx, function, NULL, flags, argc, argv, r);
520 HRESULT Function_get_value(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
522 jsstr_t *str;
523 HRESULT hres;
525 TRACE("\n");
527 hres = function_to_string(function_from_jsdisp(jsthis), &str);
528 if(FAILED(hres))
529 return hres;
531 *r = jsval_string(str);
532 return S_OK;
535 static HRESULT Function_get_arguments(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
537 FunctionInstance *function = function_from_jsdisp(jsthis);
538 call_frame_t *frame;
540 TRACE("\n");
542 for(frame = ctx->call_ctx; frame; frame = frame->prev_frame) {
543 if(frame->function_instance == &function->dispex) {
544 *r = jsval_obj(jsdisp_addref(frame->arguments_obj));
545 return S_OK;
549 *r = jsval_null();
550 return S_OK;
553 static void Function_destructor(jsdisp_t *dispex)
555 FunctionInstance *This = (FunctionInstance*)dispex;
557 if(This->code)
558 release_bytecode(This->code);
559 if(This->scope_chain)
560 scope_release(This->scope_chain);
561 heap_free(This);
564 static const builtin_prop_t Function_props[] = {
565 {applyW, Function_apply, PROPF_METHOD|2},
566 {argumentsW, NULL, 0, Function_get_arguments, builtin_set_const},
567 {callW, Function_call, PROPF_METHOD|1},
568 {lengthW, NULL, 0, Function_get_length, Function_set_length},
569 {toStringW, Function_toString, PROPF_METHOD}
572 static const builtin_info_t Function_info = {
573 JSCLASS_FUNCTION,
574 DEFAULT_FUNCTION_VALUE,
575 sizeof(Function_props)/sizeof(*Function_props),
576 Function_props,
577 Function_destructor,
578 NULL
581 static const builtin_prop_t FunctionInst_props[] = {
582 {argumentsW, NULL, 0, Function_get_arguments, builtin_set_const},
583 {lengthW, NULL, 0, Function_get_length, Function_set_length}
586 static const builtin_info_t FunctionInst_info = {
587 JSCLASS_FUNCTION,
588 DEFAULT_FUNCTION_VALUE,
589 sizeof(FunctionInst_props)/sizeof(*FunctionInst_props),
590 FunctionInst_props,
591 Function_destructor,
592 NULL
595 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, DWORD flags,
596 BOOL funcprot, jsdisp_t *prototype, FunctionInstance **ret)
598 FunctionInstance *function;
599 HRESULT hres;
601 function = heap_alloc_zero(sizeof(FunctionInstance));
602 if(!function)
603 return E_OUTOFMEMORY;
605 if(funcprot)
606 hres = init_dispex(&function->dispex, ctx, builtin_info, prototype);
607 else if(builtin_info)
608 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
609 else
610 hres = init_dispex_from_constr(&function->dispex, ctx, &FunctionInst_info, ctx->function_constr);
611 if(FAILED(hres)) {
612 heap_free(function);
613 return hres;
616 function->flags = flags;
617 function->length = flags & PROPF_ARGMASK;
619 *ret = function;
620 return S_OK;
623 static inline HRESULT set_prototype(script_ctx_t *ctx, jsdisp_t *dispex, jsdisp_t *prototype)
625 return jsdisp_propput_dontenum(dispex, prototypeW, jsval_obj(prototype));
628 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
629 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
631 FunctionInstance *function;
632 HRESULT hres;
634 hres = create_function(ctx, builtin_info, flags, FALSE, NULL, &function);
635 if(FAILED(hres))
636 return hres;
638 if(builtin_info)
639 hres = jsdisp_propput_const(&function->dispex, lengthW, jsval_number(function->length));
640 if(SUCCEEDED(hres))
641 hres = set_prototype(ctx, &function->dispex, prototype);
642 if(FAILED(hres)) {
643 jsdisp_release(&function->dispex);
644 return hres;
647 function->value_proc = value_proc;
648 function->name = name;
650 *ret = &function->dispex;
651 return S_OK;
654 static HRESULT set_constructor_prop(script_ctx_t *ctx, jsdisp_t *constr, jsdisp_t *prot)
656 static const WCHAR constructorW[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
658 return jsdisp_propput_dontenum(prot, constructorW, jsval_obj(constr));
661 HRESULT create_builtin_constructor(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
662 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
664 jsdisp_t *constr;
665 HRESULT hres;
667 hres = create_builtin_function(ctx, value_proc, name, builtin_info, flags, prototype, &constr);
668 if(FAILED(hres))
669 return hres;
671 hres = set_constructor_prop(ctx, constr, prototype);
672 if(FAILED(hres)) {
673 jsdisp_release(constr);
674 return hres;
677 *ret = constr;
678 return S_OK;
681 HRESULT create_source_function(script_ctx_t *ctx, bytecode_t *code, function_code_t *func_code,
682 scope_chain_t *scope_chain, jsdisp_t **ret)
684 FunctionInstance *function;
685 jsdisp_t *prototype;
686 HRESULT hres;
688 hres = create_object(ctx, NULL, &prototype);
689 if(FAILED(hres))
690 return hres;
692 hres = create_function(ctx, NULL, PROPF_CONSTR, FALSE, NULL, &function);
693 if(SUCCEEDED(hres)) {
694 hres = set_prototype(ctx, &function->dispex, prototype);
695 if(SUCCEEDED(hres))
696 hres = set_constructor_prop(ctx, &function->dispex, prototype);
697 if(FAILED(hres))
698 jsdisp_release(&function->dispex);
700 jsdisp_release(prototype);
701 if(FAILED(hres))
702 return hres;
704 if(scope_chain) {
705 scope_addref(scope_chain);
706 function->scope_chain = scope_chain;
709 bytecode_addref(code);
710 function->code = code;
711 function->func_code = func_code;
712 function->length = function->func_code->param_cnt;
714 *ret = &function->dispex;
715 return S_OK;
718 static HRESULT construct_function(script_ctx_t *ctx, unsigned argc, jsval_t *argv, IDispatch **ret)
720 WCHAR *str = NULL, *ptr;
721 unsigned len = 0, i = 0;
722 bytecode_t *code;
723 jsdisp_t *function;
724 jsstr_t **params = NULL;
725 int j = 0;
726 HRESULT hres = S_OK;
728 static const WCHAR function_anonymousW[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
729 static const WCHAR function_beginW[] = {')',' ','{','\n'};
730 static const WCHAR function_endW[] = {'\n','}',0};
732 if(argc) {
733 params = heap_alloc(argc*sizeof(*params));
734 if(!params)
735 return E_OUTOFMEMORY;
737 if(argc > 2)
738 len = (argc-2)*2; /* separating commas */
739 for(i=0; i < argc; i++) {
740 hres = to_string(ctx, argv[i], params+i);
741 if(FAILED(hres))
742 break;
743 len += jsstr_length(params[i]);
747 if(SUCCEEDED(hres)) {
748 len += (sizeof(function_anonymousW) + sizeof(function_beginW) + sizeof(function_endW)) / sizeof(WCHAR);
749 str = heap_alloc(len*sizeof(WCHAR));
750 if(str) {
751 memcpy(str, function_anonymousW, sizeof(function_anonymousW));
752 ptr = str + sizeof(function_anonymousW)/sizeof(WCHAR);
753 if(argc > 1) {
754 while(1) {
755 ptr += jsstr_flush(params[j], ptr);
756 if(++j == argc-1)
757 break;
758 *ptr++ = ',';
759 *ptr++ = ' ';
762 memcpy(ptr, function_beginW, sizeof(function_beginW));
763 ptr += sizeof(function_beginW)/sizeof(WCHAR);
764 if(argc)
765 ptr += jsstr_flush(params[argc-1], ptr);
766 memcpy(ptr, function_endW, sizeof(function_endW));
768 TRACE("%s\n", debugstr_w(str));
769 }else {
770 hres = E_OUTOFMEMORY;
774 while(i)
775 jsstr_release(params[--i]);
776 heap_free(params);
777 if(FAILED(hres))
778 return hres;
780 hres = compile_script(ctx, str, NULL, NULL, FALSE, FALSE, &code);
781 heap_free(str);
782 if(FAILED(hres))
783 return hres;
785 if(code->global_code.func_cnt != 1 || code->global_code.var_cnt) {
786 ERR("Invalid parser result!\n");
787 release_bytecode(code);
788 return E_UNEXPECTED;
791 hres = create_source_function(ctx, code, code->global_code.funcs, NULL, &function);
792 release_bytecode(code);
793 if(FAILED(hres))
794 return hres;
796 *ret = to_disp(function);
797 return S_OK;
800 static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
801 jsval_t *r)
803 HRESULT hres;
805 TRACE("\n");
807 switch(flags) {
808 case DISPATCH_METHOD:
809 case DISPATCH_CONSTRUCT: {
810 IDispatch *ret;
812 hres = construct_function(ctx, argc, argv, &ret);
813 if(FAILED(hres))
814 return hres;
816 *r = jsval_disp(ret);
817 break;
819 default:
820 FIXME("unimplemented flags %x\n", flags);
821 return E_NOTIMPL;
824 return S_OK;
827 static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
828 jsval_t *r)
830 FIXME("\n");
831 return E_NOTIMPL;
834 HRESULT init_function_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
836 FunctionInstance *prot, *constr;
837 HRESULT hres;
839 static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
841 hres = create_function(ctx, &Function_info, PROPF_CONSTR, TRUE, object_prototype, &prot);
842 if(FAILED(hres))
843 return hres;
845 prot->value_proc = FunctionProt_value;
846 prot->name = prototypeW;
848 hres = create_function(ctx, &FunctionInst_info, PROPF_CONSTR|1, TRUE, &prot->dispex, &constr);
849 if(SUCCEEDED(hres)) {
850 constr->value_proc = FunctionConstr_value;
851 constr->name = FunctionW;
852 hres = set_prototype(ctx, &constr->dispex, &prot->dispex);
853 if(SUCCEEDED(hres))
854 hres = set_constructor_prop(ctx, &constr->dispex, &prot->dispex);
855 if(FAILED(hres))
856 jsdisp_release(&constr->dispex);
858 jsdisp_release(&prot->dispex);
859 if(FAILED(hres))
860 return hres;
862 ctx->function_constr = &constr->dispex;
863 return S_OK;