d3d11/tests: Add some tests for ID3DUserDefinedAnnotation.
[wine.git] / dlls / jscript / function.c
blob3eef1aa2dd643568c7278f76f0eb71e357eefe45
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 _function_vtbl_t function_vtbl_t;
30 typedef struct {
31 jsdisp_t dispex;
32 const function_vtbl_t *vtbl;
33 DWORD flags;
34 DWORD length;
35 } FunctionInstance;
37 struct _function_vtbl_t {
38 HRESULT (*call)(script_ctx_t*,FunctionInstance*,IDispatch*,unsigned,unsigned,jsval_t*,jsval_t*);
39 HRESULT (*toString)(FunctionInstance*,jsstr_t**);
40 function_code_t* (*get_code)(FunctionInstance*);
41 void (*destructor)(FunctionInstance*);
44 typedef struct {
45 FunctionInstance function;
46 scope_chain_t *scope_chain;
47 bytecode_t *code;
48 function_code_t *func_code;
49 } InterpretedFunction;
51 typedef struct {
52 FunctionInstance function;
53 builtin_invoke_t proc;
54 const WCHAR *name;
55 } NativeFunction;
57 typedef struct {
58 FunctionInstance function;
59 FunctionInstance *target;
60 IDispatch *this;
61 unsigned argc;
62 jsval_t args[1];
63 } BindFunction;
65 typedef struct {
66 jsdisp_t jsdisp;
67 InterpretedFunction *function;
68 jsval_t *buf;
69 call_frame_t *frame;
70 unsigned argc;
71 } ArgumentsInstance;
73 static HRESULT create_bind_function(script_ctx_t*,FunctionInstance*,IDispatch*,unsigned,jsval_t*,jsdisp_t**r);
75 static inline FunctionInstance *function_from_jsdisp(jsdisp_t *jsdisp)
77 return CONTAINING_RECORD(jsdisp, FunctionInstance, dispex);
80 static inline FunctionInstance *function_from_vdisp(vdisp_t *vdisp)
82 return function_from_jsdisp(vdisp->u.jsdisp);
85 static inline FunctionInstance *function_this(vdisp_t *jsthis)
87 return is_vclass(jsthis, JSCLASS_FUNCTION) ? function_from_vdisp(jsthis) : NULL;
90 static inline ArgumentsInstance *arguments_from_jsdisp(jsdisp_t *jsdisp)
92 return CONTAINING_RECORD(jsdisp, ArgumentsInstance, jsdisp);
95 static HRESULT Arguments_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
96 jsval_t *r)
98 FIXME("\n");
99 return E_NOTIMPL;
102 static void Arguments_destructor(jsdisp_t *jsdisp)
104 ArgumentsInstance *arguments = arguments_from_jsdisp(jsdisp);
106 TRACE("(%p)\n", arguments);
108 if(arguments->buf) {
109 unsigned i;
110 for(i = 0; i < arguments->argc; i++)
111 jsval_release(arguments->buf[i]);
112 heap_free(arguments->buf);
115 jsdisp_release(&arguments->function->function.dispex);
116 heap_free(arguments);
119 static unsigned Arguments_idx_length(jsdisp_t *jsdisp)
121 ArgumentsInstance *arguments = arguments_from_jsdisp(jsdisp);
122 return arguments->argc;
125 static jsval_t *get_argument_ref(ArgumentsInstance *arguments, unsigned idx)
127 if(arguments->buf)
128 return arguments->buf + idx;
129 if(arguments->frame->base_scope->frame || idx >= arguments->frame->function->param_cnt)
130 return arguments->jsdisp.ctx->stack + arguments->frame->arguments_off + idx;
131 return NULL;
134 static HRESULT Arguments_idx_get(jsdisp_t *jsdisp, unsigned idx, jsval_t *r)
136 ArgumentsInstance *arguments = arguments_from_jsdisp(jsdisp);
137 jsval_t *ref;
139 TRACE("%p[%u]\n", arguments, idx);
141 if((ref = get_argument_ref(arguments, idx)))
142 return jsval_copy(*ref, r);
144 /* FIXME: Accessing by name won't work for duplicated argument names */
145 return jsdisp_propget_name(arguments->frame->base_scope->jsobj,
146 arguments->function->func_code->params[idx], r);
149 static HRESULT Arguments_idx_put(jsdisp_t *jsdisp, unsigned idx, jsval_t val)
151 ArgumentsInstance *arguments = arguments_from_jsdisp(jsdisp);
152 jsval_t *ref;
153 HRESULT hres;
155 TRACE("%p[%u] = %s\n", arguments, idx, debugstr_jsval(val));
157 if((ref = get_argument_ref(arguments, idx))) {
158 jsval_t copy;
159 hres = jsval_copy(val, &copy);
160 if(FAILED(hres))
161 return hres;
163 jsval_release(*ref);
164 *ref = copy;
165 return S_OK;
168 /* FIXME: Accessing by name won't work for duplicated argument names */
169 return jsdisp_propput_name(arguments->frame->base_scope->jsobj,
170 arguments->function->func_code->params[idx], val);
173 static const builtin_info_t Arguments_info = {
174 JSCLASS_ARGUMENTS,
175 Arguments_value,
176 0, NULL,
177 Arguments_destructor,
178 NULL,
179 Arguments_idx_length,
180 Arguments_idx_get,
181 Arguments_idx_put
184 HRESULT setup_arguments_object(script_ctx_t *ctx, call_frame_t *frame)
186 ArgumentsInstance *args;
187 HRESULT hres;
189 args = heap_alloc_zero(sizeof(*args));
190 if(!args)
191 return E_OUTOFMEMORY;
193 hres = init_dispex_from_constr(&args->jsdisp, ctx, &Arguments_info, ctx->object_constr);
194 if(FAILED(hres)) {
195 heap_free(args);
196 return hres;
199 args->function = (InterpretedFunction*)function_from_jsdisp(jsdisp_addref(frame->function_instance));
200 args->argc = frame->argc;
201 args->frame = frame;
203 hres = jsdisp_define_data_property(&args->jsdisp, L"length", PROPF_WRITABLE | PROPF_CONFIGURABLE,
204 jsval_number(args->argc));
205 if(SUCCEEDED(hres))
206 hres = jsdisp_define_data_property(&args->jsdisp, L"callee", PROPF_WRITABLE | PROPF_CONFIGURABLE,
207 jsval_obj(&args->function->function.dispex));
208 if(SUCCEEDED(hres))
209 hres = jsdisp_propput(frame->base_scope->jsobj, L"arguments", PROPF_WRITABLE, TRUE, jsval_obj(&args->jsdisp));
210 if(FAILED(hres)) {
211 jsdisp_release(&args->jsdisp);
212 return hres;
215 frame->arguments_obj = &args->jsdisp;
216 return S_OK;
219 void detach_arguments_object(jsdisp_t *args_disp)
221 ArgumentsInstance *arguments = arguments_from_jsdisp(args_disp);
222 call_frame_t *frame = arguments->frame;
223 const BOOL on_stack = frame->base_scope->frame == frame;
224 HRESULT hres;
226 /* Reset arguments value to cut the reference cycle. Note that since all activation contexts have
227 * their own arguments property, it's impossible to use prototype's one during name lookup */
228 jsdisp_propput_name(frame->base_scope->jsobj, L"arguments", jsval_undefined());
229 arguments->frame = NULL;
231 /* Don't bother coppying arguments if call frame holds the last reference. */
232 if(arguments->jsdisp.ref > 1) {
233 arguments->buf = heap_alloc(arguments->argc * sizeof(*arguments->buf));
234 if(arguments->buf) {
235 int i;
237 for(i = 0; i < arguments->argc ; i++) {
238 if(on_stack || i >= frame->function->param_cnt)
239 hres = jsval_copy(arguments->jsdisp.ctx->stack[frame->arguments_off + i], arguments->buf+i);
240 else
241 hres = jsdisp_propget_name(frame->base_scope->jsobj, frame->function->params[i], arguments->buf+i);
242 if(FAILED(hres))
243 arguments->buf[i] = jsval_undefined();
245 }else {
246 ERR("out of memory\n");
247 arguments->argc = 0;
251 jsdisp_release(frame->arguments_obj);
254 HRESULT Function_invoke(jsdisp_t *func_this, IDispatch *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
256 FunctionInstance *function;
258 TRACE("func %p this %p\n", func_this, jsthis);
260 assert(is_class(func_this, JSCLASS_FUNCTION));
261 function = function_from_jsdisp(func_this);
263 return function->vtbl->call(function->dispex.ctx, function, jsthis, flags, argc, argv, r);
266 static HRESULT Function_get_length(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
268 TRACE("%p\n", jsthis);
270 *r = jsval_number(function_from_jsdisp(jsthis)->length);
271 return S_OK;
274 static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
275 jsval_t *r)
277 FunctionInstance *function;
278 jsstr_t *str;
279 HRESULT hres;
281 TRACE("\n");
283 if(!(function = function_this(jsthis)))
284 return JS_E_FUNCTION_EXPECTED;
286 hres = function->vtbl->toString(function, &str);
287 if(FAILED(hres))
288 return hres;
290 if(r)
291 *r = jsval_string(str);
292 else
293 jsstr_release(str);
294 return S_OK;
297 static HRESULT array_to_args(script_ctx_t *ctx, jsdisp_t *arg_array, unsigned *argc, jsval_t **ret)
299 jsval_t *argv, val;
300 DWORD length, i;
301 HRESULT hres;
303 hres = jsdisp_propget_name(arg_array, L"length", &val);
304 if(FAILED(hres))
305 return hres;
307 hres = to_uint32(ctx, val, &length);
308 jsval_release(val);
309 if(FAILED(hres))
310 return hres;
312 argv = heap_alloc(length * sizeof(*argv));
313 if(!argv)
314 return E_OUTOFMEMORY;
316 for(i=0; i<length; i++) {
317 hres = jsdisp_get_idx(arg_array, i, argv+i);
318 if(hres == DISP_E_UNKNOWNNAME) {
319 argv[i] = jsval_undefined();
320 }else if(FAILED(hres)) {
321 while(i--)
322 jsval_release(argv[i]);
323 heap_free(argv);
324 return hres;
328 *argc = length;
329 *ret = argv;
330 return S_OK;
333 static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
335 FunctionInstance *function;
336 jsval_t *args = NULL;
337 unsigned i, cnt = 0;
338 IDispatch *this_obj = NULL;
339 HRESULT hres = S_OK;
341 TRACE("\n");
343 if(!(function = function_this(jsthis)) && (jsthis->flags & VDISP_JSDISP))
344 return JS_E_FUNCTION_EXPECTED;
346 if(argc) {
347 if(!is_undefined(argv[0]) && !is_null(argv[0])) {
348 hres = to_object(ctx, argv[0], &this_obj);
349 if(FAILED(hres))
350 return hres;
354 if(argc >= 2) {
355 jsdisp_t *arg_array = NULL;
357 if(is_object_instance(argv[1])) {
358 arg_array = iface_to_jsdisp(get_object(argv[1]));
359 if(arg_array &&
360 (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
361 jsdisp_release(arg_array);
362 arg_array = NULL;
366 if(arg_array) {
367 hres = array_to_args(ctx, arg_array, &cnt, &args);
368 jsdisp_release(arg_array);
369 }else {
370 FIXME("throw TypeError\n");
371 hres = E_FAIL;
375 if(SUCCEEDED(hres)) {
376 if(function) {
377 hres = function->vtbl->call(ctx, function, this_obj, flags, cnt, args, r);
378 }else {
379 jsval_t res;
380 hres = disp_call_value(ctx, jsthis->u.disp, this_obj, DISPATCH_METHOD, cnt, args, &res);
381 if(SUCCEEDED(hres)) {
382 if(r)
383 *r = res;
384 else
385 jsval_release(res);
390 if(this_obj)
391 IDispatch_Release(this_obj);
392 for(i=0; i < cnt; i++)
393 jsval_release(args[i]);
394 heap_free(args);
395 return hres;
398 static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
399 jsval_t *r)
401 FunctionInstance *function;
402 IDispatch *this_obj = NULL;
403 unsigned cnt = 0;
404 HRESULT hres;
406 TRACE("\n");
408 if(!(function = function_this(jsthis)))
409 return JS_E_FUNCTION_EXPECTED;
411 if(argc) {
412 if(!is_undefined(argv[0]) && !is_null(argv[0])) {
413 hres = to_object(ctx, argv[0], &this_obj);
414 if(FAILED(hres))
415 return hres;
418 cnt = argc-1;
421 hres = function->vtbl->call(ctx, function, this_obj, flags, cnt, argv + 1, r);
423 if(this_obj)
424 IDispatch_Release(this_obj);
425 return hres;
428 static HRESULT Function_bind(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
429 jsval_t *r)
431 IDispatch *bound_this = NULL;
432 FunctionInstance *function;
433 jsdisp_t *new_function;
434 HRESULT hres;
436 TRACE("\n");
438 if(!(function = function_this(jsthis)))
439 return JS_E_FUNCTION_EXPECTED;
441 if(argc < 1) {
442 FIXME("no this argument\n");
443 return E_NOTIMPL;
446 if(is_object_instance(argv[0])) {
447 bound_this = get_object(argv[0]);
448 }else if(!is_null(argv[0])) {
449 FIXME("%s is not an object instance\n", debugstr_jsval(argv[0]));
450 return E_NOTIMPL;
453 hres = create_bind_function(ctx, function, bound_this, argc - 1, argv + 1, &new_function);
454 if(FAILED(hres))
455 return hres;
457 if(r)
458 *r = jsval_obj(new_function);
459 else
460 jsdisp_release(new_function);
461 return S_OK;
464 HRESULT Function_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
465 jsval_t *r)
467 FunctionInstance *function;
469 TRACE("\n");
471 if(!is_vclass(jsthis, JSCLASS_FUNCTION)) {
472 ERR("dispex is not a function\n");
473 return E_FAIL;
476 function = function_from_jsdisp(jsthis->u.jsdisp);
477 return function->vtbl->call(ctx, function, NULL, flags, argc, argv, r);
480 HRESULT Function_get_value(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
482 FunctionInstance *function = function_from_jsdisp(jsthis);
483 jsstr_t *str;
484 HRESULT hres;
486 TRACE("\n");
488 hres = function->vtbl->toString(function, &str);
489 if(FAILED(hres))
490 return hres;
492 *r = jsval_string(str);
493 return S_OK;
496 static HRESULT Function_get_arguments(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
498 FunctionInstance *function = function_from_jsdisp(jsthis);
499 call_frame_t *frame;
500 HRESULT hres;
502 TRACE("\n");
504 for(frame = ctx->call_ctx; frame; frame = frame->prev_frame) {
505 if(frame->function_instance == &function->dispex) {
506 if(!frame->arguments_obj) {
507 hres = setup_arguments_object(ctx, frame);
508 if(FAILED(hres))
509 return hres;
511 *r = jsval_obj(jsdisp_addref(frame->arguments_obj));
512 return S_OK;
516 *r = jsval_null();
517 return S_OK;
520 function_code_t *Function_get_code(jsdisp_t *jsthis)
522 FunctionInstance *function;
524 assert(is_class(jsthis, JSCLASS_FUNCTION));
525 function = function_from_jsdisp(jsthis);
527 return function->vtbl->get_code(function);
530 static void Function_destructor(jsdisp_t *dispex)
532 FunctionInstance *function = function_from_jsdisp(dispex);
533 function->vtbl->destructor(function);
534 heap_free(function);
537 static const builtin_prop_t Function_props[] = {
538 {L"apply", Function_apply, PROPF_METHOD|2},
539 {L"arguments", NULL, 0, Function_get_arguments},
540 {L"bind", Function_bind, PROPF_METHOD|PROPF_ES5|1},
541 {L"call", Function_call, PROPF_METHOD|1},
542 {L"length", NULL, 0, Function_get_length},
543 {L"toString", Function_toString, PROPF_METHOD}
546 static const builtin_info_t Function_info = {
547 JSCLASS_FUNCTION,
548 Function_value,
549 ARRAY_SIZE(Function_props),
550 Function_props,
551 Function_destructor,
552 NULL
555 static const builtin_prop_t FunctionInst_props[] = {
556 {L"arguments", NULL, 0, Function_get_arguments},
557 {L"length", NULL, 0, Function_get_length}
560 static const builtin_info_t FunctionInst_info = {
561 JSCLASS_FUNCTION,
562 Function_value,
563 ARRAY_SIZE(FunctionInst_props),
564 FunctionInst_props,
565 Function_destructor,
566 NULL
569 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, const function_vtbl_t *vtbl, size_t size,
570 DWORD flags, BOOL funcprot, jsdisp_t *prototype, void **ret)
572 FunctionInstance *function;
573 HRESULT hres;
575 function = heap_alloc_zero(size);
576 if(!function)
577 return E_OUTOFMEMORY;
579 if(funcprot)
580 hres = init_dispex(&function->dispex, ctx, builtin_info, prototype);
581 else if(builtin_info)
582 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
583 else
584 hres = init_dispex_from_constr(&function->dispex, ctx, &FunctionInst_info, ctx->function_constr);
585 if(FAILED(hres)) {
586 heap_free(function);
587 return hres;
590 function->vtbl = vtbl;
591 function->flags = flags;
592 function->length = flags & PROPF_ARGMASK;
594 *ret = function;
595 return S_OK;
598 static HRESULT NativeFunction_call(script_ctx_t *ctx, FunctionInstance *func, IDispatch *this_disp, unsigned flags,
599 unsigned argc, jsval_t *argv, jsval_t *r)
601 NativeFunction *function = (NativeFunction*)func;
602 vdisp_t vthis;
603 HRESULT hres;
605 if(this_disp)
606 set_disp(&vthis, this_disp);
607 else
608 set_disp(&vthis, lookup_global_host(ctx));
610 hres = function->proc(ctx, &vthis, flags & ~DISPATCH_JSCRIPT_INTERNAL_MASK, argc, argv, r);
612 vdisp_release(&vthis);
613 return hres;
616 static HRESULT NativeFunction_toString(FunctionInstance *func, jsstr_t **ret)
618 NativeFunction *function = (NativeFunction*)func;
619 DWORD name_len;
620 jsstr_t *str;
621 WCHAR *ptr;
623 static const WCHAR native_prefixW[] = L"\nfunction ";
624 static const WCHAR native_suffixW[] = L"() {\n [native code]\n}\n";
626 name_len = function->name ? lstrlenW(function->name) : 0;
627 str = jsstr_alloc_buf(ARRAY_SIZE(native_prefixW) + ARRAY_SIZE(native_suffixW) + name_len - 2, &ptr);
628 if(!str)
629 return E_OUTOFMEMORY;
631 memcpy(ptr, native_prefixW, sizeof(native_prefixW));
632 ptr += ARRAY_SIZE(native_prefixW) - 1;
633 memcpy(ptr, function->name, name_len*sizeof(WCHAR));
634 ptr += name_len;
635 memcpy(ptr, native_suffixW, sizeof(native_suffixW));
637 *ret = str;
638 return S_OK;
641 static function_code_t *NativeFunction_get_code(FunctionInstance *function)
643 return NULL;
646 static void NativeFunction_destructor(FunctionInstance *function)
650 static const function_vtbl_t NativeFunctionVtbl = {
651 NativeFunction_call,
652 NativeFunction_toString,
653 NativeFunction_get_code,
654 NativeFunction_destructor
657 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
658 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
660 NativeFunction *function;
661 HRESULT hres;
663 hres = create_function(ctx, builtin_info, &NativeFunctionVtbl, sizeof(NativeFunction), flags, FALSE, NULL, (void**)&function);
664 if(FAILED(hres))
665 return hres;
667 if(builtin_info)
668 hres = jsdisp_define_data_property(&function->function.dispex, L"length", 0,
669 jsval_number(function->function.length));
670 if(SUCCEEDED(hres))
671 hres = jsdisp_define_data_property(&function->function.dispex, L"prototype", 0, jsval_obj(prototype));
672 if(FAILED(hres)) {
673 jsdisp_release(&function->function.dispex);
674 return hres;
677 function->proc = value_proc;
678 function->name = name;
680 *ret = &function->function.dispex;
681 return S_OK;
684 static HRESULT set_constructor_prop(script_ctx_t *ctx, jsdisp_t *constr, jsdisp_t *prot)
686 return jsdisp_define_data_property(prot, L"constructor", PROPF_WRITABLE | PROPF_CONFIGURABLE,
687 jsval_obj(constr));
690 HRESULT create_builtin_constructor(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
691 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
693 jsdisp_t *constr;
694 HRESULT hres;
696 hres = create_builtin_function(ctx, value_proc, name, builtin_info, flags, prototype, &constr);
697 if(FAILED(hres))
698 return hres;
700 hres = set_constructor_prop(ctx, constr, prototype);
701 if(FAILED(hres)) {
702 jsdisp_release(constr);
703 return hres;
706 *ret = constr;
707 return S_OK;
710 static HRESULT InterpretedFunction_call(script_ctx_t *ctx, FunctionInstance *func, IDispatch *this_obj, unsigned flags,
711 unsigned argc, jsval_t *argv, jsval_t *r)
713 InterpretedFunction *function = (InterpretedFunction*)func;
714 jsdisp_t *new_obj = NULL;
715 DWORD exec_flags = 0;
716 HRESULT hres;
718 TRACE("%p\n", function);
720 if(ctx->state == SCRIPTSTATE_UNINITIALIZED || ctx->state == SCRIPTSTATE_CLOSED) {
721 WARN("Script engine state does not allow running code.\n");
722 return E_UNEXPECTED;
725 if(flags & DISPATCH_CONSTRUCT) {
726 hres = create_object(ctx, &function->function.dispex, &new_obj);
727 if(FAILED(hres))
728 return hres;
729 this_obj = to_disp(new_obj);
732 if(flags & DISPATCH_JSCRIPT_CALLEREXECSSOURCE)
733 exec_flags |= EXEC_RETURN_TO_INTERP;
734 if(flags & DISPATCH_CONSTRUCT)
735 exec_flags |= EXEC_CONSTRUCTOR;
736 hres = exec_source(ctx, exec_flags, function->code, function->func_code, function->scope_chain, this_obj,
737 &function->function.dispex, argc, argv, r);
738 if(new_obj)
739 jsdisp_release(new_obj);
740 return hres;
743 static HRESULT InterpretedFunction_toString(FunctionInstance *func, jsstr_t **ret)
745 InterpretedFunction *function = (InterpretedFunction*)func;
747 *ret = jsstr_alloc_len(function->func_code->source, function->func_code->source_len);
748 return *ret ? S_OK : E_OUTOFMEMORY;
751 static function_code_t *InterpretedFunction_get_code(FunctionInstance *func)
753 InterpretedFunction *function = (InterpretedFunction*)func;
755 return function->func_code;
758 static void InterpretedFunction_destructor(FunctionInstance *func)
760 InterpretedFunction *function = (InterpretedFunction*)func;
762 release_bytecode(function->code);
763 if(function->scope_chain)
764 scope_release(function->scope_chain);
767 static const function_vtbl_t InterpretedFunctionVtbl = {
768 InterpretedFunction_call,
769 InterpretedFunction_toString,
770 InterpretedFunction_get_code,
771 InterpretedFunction_destructor
774 HRESULT create_source_function(script_ctx_t *ctx, bytecode_t *code, function_code_t *func_code,
775 scope_chain_t *scope_chain, jsdisp_t **ret)
777 InterpretedFunction *function;
778 jsdisp_t *prototype;
779 HRESULT hres;
781 hres = create_object(ctx, NULL, &prototype);
782 if(FAILED(hres))
783 return hres;
785 hres = create_function(ctx, NULL, &InterpretedFunctionVtbl, sizeof(InterpretedFunction), PROPF_CONSTR,
786 FALSE, NULL, (void**)&function);
787 if(SUCCEEDED(hres)) {
788 hres = jsdisp_define_data_property(&function->function.dispex, L"prototype", PROPF_WRITABLE,
789 jsval_obj(prototype));
790 if(SUCCEEDED(hres))
791 hres = set_constructor_prop(ctx, &function->function.dispex, prototype);
792 if(FAILED(hres))
793 jsdisp_release(&function->function.dispex);
795 jsdisp_release(prototype);
796 if(FAILED(hres))
797 return hres;
799 if(scope_chain) {
800 scope_addref(scope_chain);
801 function->scope_chain = scope_chain;
804 bytecode_addref(code);
805 function->code = code;
806 function->func_code = func_code;
807 function->function.length = function->func_code->param_cnt;
809 *ret = &function->function.dispex;
810 return S_OK;
813 static HRESULT BindFunction_call(script_ctx_t *ctx, FunctionInstance *func, IDispatch *this_obj, unsigned flags,
814 unsigned argc, jsval_t *argv, jsval_t *r)
816 BindFunction *function = (BindFunction*)func;
817 jsval_t *call_args = NULL;
818 unsigned call_argc;
819 HRESULT hres;
821 TRACE("%p\n", function);
823 call_argc = function->argc + argc;
824 if(call_argc) {
825 call_args = heap_alloc(call_argc * sizeof(*call_args));
826 if(!call_args)
827 return E_OUTOFMEMORY;
829 if(function->argc)
830 memcpy(call_args, function->args, function->argc * sizeof(*call_args));
831 if(argc)
832 memcpy(call_args + function->argc, argv, argc * sizeof(*call_args));
835 hres = function->target->vtbl->call(ctx, function->target, function->this, flags, call_argc, call_args, r);
837 heap_free(call_args);
838 return hres;
841 static HRESULT BindFunction_toString(FunctionInstance *function, jsstr_t **ret)
843 *ret = jsstr_alloc(L"\nfunction() {\n [native code]\n}\n");
844 return *ret ? S_OK : E_OUTOFMEMORY;
847 static function_code_t *BindFunction_get_code(FunctionInstance *function)
849 return NULL;
852 static void BindFunction_destructor(FunctionInstance *func)
854 BindFunction *function = (BindFunction*)func;
855 unsigned i;
857 TRACE("%p\n", function);
859 for(i = 0; i < function->argc; i++)
860 jsval_release(function->args[i]);
861 jsdisp_release(&function->target->dispex);
862 if(function->this)
863 IDispatch_Release(function->this);
866 static const function_vtbl_t BindFunctionVtbl = {
867 BindFunction_call,
868 BindFunction_toString,
869 BindFunction_get_code,
870 BindFunction_destructor
873 static HRESULT create_bind_function(script_ctx_t *ctx, FunctionInstance *target, IDispatch *bound_this, unsigned argc,
874 jsval_t *argv, jsdisp_t **ret)
876 BindFunction *function;
877 HRESULT hres;
879 hres = create_function(ctx, NULL, &BindFunctionVtbl, FIELD_OFFSET(BindFunction, args[argc]), PROPF_METHOD,
880 FALSE, NULL, (void**)&function);
881 if(FAILED(hres))
882 return hres;
884 jsdisp_addref(&target->dispex);
885 function->target = target;
887 if(bound_this)
888 IDispatch_AddRef(function->this = bound_this);
890 for(function->argc = 0; function->argc < argc; function->argc++) {
891 hres = jsval_copy(argv[function->argc], function->args + function->argc);
892 if(FAILED(hres)) {
893 jsdisp_release(&function->function.dispex);
894 return hres;
898 function->function.length = target->length > argc ? target->length - argc : 0;
900 *ret = &function->function.dispex;
901 return S_OK;
904 static HRESULT construct_function(script_ctx_t *ctx, unsigned argc, jsval_t *argv, IDispatch **ret)
906 WCHAR *str = NULL, *ptr;
907 unsigned len = 0, i = 0;
908 bytecode_t *code;
909 jsdisp_t *function;
910 jsstr_t **params = NULL;
911 int j = 0;
912 HRESULT hres = S_OK;
914 static const WCHAR function_anonymousW[] = L"function anonymous(";
915 static const WCHAR function_beginW[] = L") {\n";
916 static const WCHAR function_endW[] = L"\n}";
918 if(argc) {
919 params = heap_alloc(argc*sizeof(*params));
920 if(!params)
921 return E_OUTOFMEMORY;
923 if(argc > 2)
924 len = (argc-2)*2; /* separating commas */
925 for(i=0; i < argc; i++) {
926 hres = to_string(ctx, argv[i], params+i);
927 if(FAILED(hres))
928 break;
929 len += jsstr_length(params[i]);
933 if(SUCCEEDED(hres)) {
934 len += ARRAY_SIZE(function_anonymousW) + ARRAY_SIZE(function_beginW) + ARRAY_SIZE(function_endW) - 2;
935 str = heap_alloc(len*sizeof(WCHAR));
936 if(str) {
937 memcpy(str, function_anonymousW, sizeof(function_anonymousW));
938 ptr = str + ARRAY_SIZE(function_anonymousW) - 1;
939 if(argc > 1) {
940 while(1) {
941 ptr += jsstr_flush(params[j], ptr);
942 if(++j == argc-1)
943 break;
944 *ptr++ = ',';
945 *ptr++ = ' ';
948 memcpy(ptr, function_beginW, sizeof(function_beginW));
949 ptr += ARRAY_SIZE(function_beginW) - 1;
950 if(argc)
951 ptr += jsstr_flush(params[argc-1], ptr);
952 memcpy(ptr, function_endW, sizeof(function_endW));
954 TRACE("%s\n", debugstr_w(str));
955 }else {
956 hres = E_OUTOFMEMORY;
960 while(i)
961 jsstr_release(params[--i]);
962 heap_free(params);
963 if(FAILED(hres))
964 return hres;
966 hres = compile_script(ctx, str, 0, 0, NULL, NULL, FALSE, FALSE,
967 ctx->call_ctx ? ctx->call_ctx->bytecode->named_item : NULL, &code);
968 heap_free(str);
969 if(FAILED(hres))
970 return hres;
972 if(code->global_code.func_cnt != 1 || code->global_code.var_cnt != 1) {
973 ERR("Invalid parser result!\n");
974 release_bytecode(code);
975 return E_UNEXPECTED;
978 hres = create_source_function(ctx, code, code->global_code.funcs, NULL, &function);
979 release_bytecode(code);
980 if(FAILED(hres))
981 return hres;
983 *ret = to_disp(function);
984 return S_OK;
987 static HRESULT FunctionConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
988 jsval_t *r)
990 HRESULT hres;
992 TRACE("\n");
994 switch(flags) {
995 case DISPATCH_METHOD:
996 case DISPATCH_CONSTRUCT: {
997 IDispatch *ret;
999 hres = construct_function(ctx, argc, argv, &ret);
1000 if(FAILED(hres))
1001 return hres;
1003 *r = jsval_disp(ret);
1004 break;
1006 default:
1007 FIXME("unimplemented flags %x\n", flags);
1008 return E_NOTIMPL;
1011 return S_OK;
1014 static HRESULT FunctionProt_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1015 jsval_t *r)
1017 FIXME("\n");
1018 return E_NOTIMPL;
1021 HRESULT init_function_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
1023 NativeFunction *prot, *constr;
1024 HRESULT hres;
1026 hres = create_function(ctx, &Function_info, &NativeFunctionVtbl, sizeof(NativeFunction), PROPF_CONSTR,
1027 TRUE, object_prototype, (void**)&prot);
1028 if(FAILED(hres))
1029 return hres;
1031 prot->proc = FunctionProt_value;
1032 prot->name = L"prototype";
1034 hres = create_function(ctx, &FunctionInst_info, &NativeFunctionVtbl, sizeof(NativeFunction), PROPF_CONSTR|1,
1035 TRUE, &prot->function.dispex, (void**)&constr);
1036 if(SUCCEEDED(hres)) {
1037 constr->proc = FunctionConstr_value;
1038 constr->name = L"Function";
1039 hres = jsdisp_define_data_property(&constr->function.dispex, L"prototype", 0, jsval_obj(&prot->function.dispex));
1040 if(SUCCEEDED(hres))
1041 hres = set_constructor_prop(ctx, &constr->function.dispex, &prot->function.dispex);
1042 if(FAILED(hres))
1043 jsdisp_release(&constr->function.dispex);
1045 jsdisp_release(&prot->function.dispex);
1046 if(FAILED(hres))
1047 return hres;
1049 ctx->function_constr = &constr->function.dispex;
1050 return S_OK;