jscript: Don't use detached arguments buffer in html mode.
[wine.git] / dlls / jscript / function.c
blob68f841b38a178409d4cbf83bca12e09401a1734e
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*,jsval_t,unsigned,unsigned,jsval_t*,jsval_t*);
39 HRESULT (*toString)(FunctionInstance*,jsstr_t**);
40 function_code_t* (*get_code)(FunctionInstance*);
41 void (*destructor)(FunctionInstance*);
42 HRESULT (*gc_traverse)(struct gc_ctx*,enum gc_traverse_op,FunctionInstance*);
45 typedef struct {
46 FunctionInstance function;
47 scope_chain_t *scope_chain;
48 bytecode_t *code;
49 function_code_t *func_code;
50 } InterpretedFunction;
52 typedef struct {
53 FunctionInstance function;
54 builtin_invoke_t proc;
55 const WCHAR *name;
56 } NativeFunction;
58 typedef struct {
59 FunctionInstance function;
60 FunctionInstance *target;
61 jsval_t this;
62 unsigned argc;
63 jsval_t args[1];
64 } BindFunction;
66 typedef struct {
67 jsdisp_t jsdisp;
68 jsval_t *buf;
69 scope_chain_t *scope;
70 unsigned argc;
71 } ArgumentsInstance;
73 static HRESULT create_bind_function(script_ctx_t*,FunctionInstance*,jsval_t,unsigned,jsval_t*,jsdisp_t**r);
75 static HRESULT no_gc_traverse(struct gc_ctx *gc_ctx, enum gc_traverse_op op, FunctionInstance *function)
77 return S_OK;
80 static inline FunctionInstance *function_from_jsdisp(jsdisp_t *jsdisp)
82 return CONTAINING_RECORD(jsdisp, FunctionInstance, dispex);
85 static inline FunctionInstance *function_this(jsval_t vthis)
87 jsdisp_t *jsdisp = is_object_instance(vthis) ? to_jsdisp(get_object(vthis)) : NULL;
88 return (jsdisp && is_class(jsdisp, JSCLASS_FUNCTION)) ? function_from_jsdisp(jsdisp) : NULL;
91 static inline ArgumentsInstance *arguments_from_jsdisp(jsdisp_t *jsdisp)
93 return CONTAINING_RECORD(jsdisp, ArgumentsInstance, jsdisp);
96 static HRESULT Arguments_value(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
97 jsval_t *r)
99 FIXME("\n");
100 return E_NOTIMPL;
103 static void Arguments_destructor(jsdisp_t *jsdisp)
105 ArgumentsInstance *arguments = arguments_from_jsdisp(jsdisp);
107 TRACE("(%p)\n", arguments);
109 if(arguments->buf) {
110 unsigned i;
111 for(i = 0; i < arguments->argc; i++)
112 jsval_release(arguments->buf[i]);
113 free(arguments->buf);
116 if(arguments->scope)
117 scope_release(arguments->scope);
119 free(arguments);
122 static unsigned Arguments_idx_length(jsdisp_t *jsdisp)
124 ArgumentsInstance *arguments = arguments_from_jsdisp(jsdisp);
125 return arguments->argc;
128 static jsval_t *get_argument_ref(ArgumentsInstance *arguments, unsigned idx)
130 if(arguments->buf)
131 return arguments->buf + idx;
132 if(!arguments->scope->detached_vars)
133 return arguments->jsdisp.ctx->stack + arguments->scope->frame->arguments_off + idx;
134 return arguments->scope->detached_vars->var + idx;
137 static HRESULT Arguments_idx_get(jsdisp_t *jsdisp, unsigned idx, jsval_t *r)
139 ArgumentsInstance *arguments = arguments_from_jsdisp(jsdisp);
141 TRACE("%p[%u]\n", arguments, idx);
143 return jsval_copy(*get_argument_ref(arguments, idx), r);
146 static HRESULT Arguments_idx_put(jsdisp_t *jsdisp, unsigned idx, jsval_t val)
148 ArgumentsInstance *arguments = arguments_from_jsdisp(jsdisp);
149 jsval_t copy, *ref;
150 HRESULT hres;
152 TRACE("%p[%u] = %s\n", arguments, idx, debugstr_jsval(val));
154 hres = jsval_copy(val, &copy);
155 if(FAILED(hres))
156 return hres;
158 ref = get_argument_ref(arguments, idx);
159 jsval_release(*ref);
160 *ref = copy;
161 return S_OK;
164 static HRESULT Arguments_gc_traverse(struct gc_ctx *gc_ctx, enum gc_traverse_op op, jsdisp_t *jsdisp)
166 ArgumentsInstance *arguments = arguments_from_jsdisp(jsdisp);
167 HRESULT hres;
168 unsigned i;
170 if(arguments->buf) {
171 for(i = 0; i < arguments->argc; i++) {
172 hres = gc_process_linked_val(gc_ctx, op, jsdisp, &arguments->buf[i]);
173 if(FAILED(hres))
174 return hres;
178 if(arguments->scope) {
179 hres = gc_process_linked_obj(gc_ctx, op, jsdisp, &arguments->scope->dispex, (void**)&arguments->scope);
180 if(FAILED(hres))
181 return hres;
184 return S_OK;
187 static const builtin_info_t Arguments_info = {
188 JSCLASS_ARGUMENTS,
189 Arguments_value,
190 0, NULL,
191 Arguments_destructor,
192 NULL,
193 Arguments_idx_length,
194 Arguments_idx_get,
195 Arguments_idx_put,
196 Arguments_gc_traverse
199 HRESULT setup_arguments_object(script_ctx_t *ctx, call_frame_t *frame)
201 ArgumentsInstance *args;
202 HRESULT hres;
204 args = calloc(1, sizeof(*args));
205 if(!args)
206 return E_OUTOFMEMORY;
208 hres = init_dispex_from_constr(&args->jsdisp, ctx, &Arguments_info, ctx->object_constr);
209 if(FAILED(hres)) {
210 free(args);
211 return hres;
214 args->argc = frame->argc;
215 args->scope = scope_addref(frame->base_scope);
217 hres = jsdisp_define_data_property(&args->jsdisp, L"length", PROPF_WRITABLE | PROPF_CONFIGURABLE,
218 jsval_number(args->argc));
219 if(SUCCEEDED(hres))
220 hres = jsdisp_define_data_property(&args->jsdisp, L"callee", PROPF_WRITABLE | PROPF_CONFIGURABLE,
221 jsval_obj(frame->function_instance));
222 if(SUCCEEDED(hres))
223 hres = jsdisp_propput(as_jsdisp(frame->base_scope->obj), L"arguments", PROPF_WRITABLE, TRUE, jsval_obj(&args->jsdisp));
224 if(FAILED(hres)) {
225 jsdisp_release(&args->jsdisp);
226 return hres;
229 frame->arguments_obj = &args->jsdisp;
230 return S_OK;
233 void detach_arguments_object(call_frame_t *frame)
235 ArgumentsInstance *arguments = arguments_from_jsdisp(frame->arguments_obj);
236 scope_chain_t *scope = arguments->scope;
237 const BOOL on_stack = scope->frame == frame;
238 jsdisp_t *jsobj = as_jsdisp(scope->obj);
240 /* Reset arguments value to cut the reference cycle. Note that since all activation contexts have
241 * their own arguments property, it's impossible to use prototype's one during name lookup */
242 jsdisp_propput_name(jsobj, L"arguments", jsval_undefined());
244 /* Don't bother coppying arguments if call frame holds the last reference. */
245 if(arguments->jsdisp.ref > 1 && !arguments->jsdisp.ctx->html_mode) {
246 arguments->buf = malloc(arguments->argc * sizeof(*arguments->buf));
247 if(arguments->buf) {
248 const jsval_t *args = on_stack ? arguments->jsdisp.ctx->stack + frame->arguments_off : scope->detached_vars->var;
249 int i;
251 for(i = 0; i < arguments->argc ; i++) {
252 if(FAILED(jsval_copy(args[i], &arguments->buf[i])))
253 arguments->buf[i] = jsval_undefined();
255 }else {
256 ERR("out of memory\n");
257 arguments->argc = 0;
260 arguments->scope = NULL;
261 scope_release(scope);
264 jsdisp_release(&arguments->jsdisp);
267 HRESULT Function_invoke(jsdisp_t *func_this, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
269 FunctionInstance *function;
271 TRACE("func %p this %s\n", func_this, debugstr_jsval(vthis));
273 assert(is_class(func_this, JSCLASS_FUNCTION));
274 function = function_from_jsdisp(func_this);
276 if(function->dispex.ctx->state == SCRIPTSTATE_UNINITIALIZED || function->dispex.ctx->state == SCRIPTSTATE_CLOSED) {
277 WARN("Script engine state does not allow running code.\n");
278 return E_UNEXPECTED;
281 return function->vtbl->call(function->dispex.ctx, function, vthis, flags, argc, argv, r);
284 static HRESULT Function_get_caller(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
286 FunctionInstance *function = function_from_jsdisp(jsthis);
287 call_frame_t *frame;
289 TRACE("%p\n", jsthis);
291 for(frame = ctx->call_ctx; frame; frame = frame->prev_frame) {
292 if(frame->function_instance == &function->dispex) {
293 if(!frame->prev_frame || !frame->prev_frame->function_instance)
294 break;
295 *r = jsval_obj(jsdisp_addref(frame->prev_frame->function_instance));
296 return S_OK;
300 *r = jsval_null();
301 return S_OK;
304 static HRESULT Function_get_length(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
306 TRACE("%p\n", jsthis);
308 *r = jsval_number(function_from_jsdisp(jsthis)->length);
309 return S_OK;
312 static HRESULT Function_toString(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
313 jsval_t *r)
315 FunctionInstance *function;
316 jsstr_t *str;
317 HRESULT hres;
319 TRACE("\n");
321 if(!(function = function_this(vthis)))
322 return JS_E_FUNCTION_EXPECTED;
324 hres = function->vtbl->toString(function, &str);
325 if(FAILED(hres))
326 return hres;
328 if(r)
329 *r = jsval_string(str);
330 else
331 jsstr_release(str);
332 return S_OK;
335 static HRESULT array_to_args(script_ctx_t *ctx, jsdisp_t *arg_array, unsigned *argc, jsval_t **ret)
337 jsval_t *argv, val;
338 UINT32 length, i;
339 HRESULT hres;
341 hres = jsdisp_propget_name(arg_array, L"length", &val);
342 if(FAILED(hres))
343 return hres;
345 hres = to_uint32(ctx, val, &length);
346 jsval_release(val);
347 if(FAILED(hres))
348 return hres;
350 argv = malloc(length * sizeof(*argv));
351 if(!argv)
352 return E_OUTOFMEMORY;
354 for(i=0; i<length; i++) {
355 hres = jsdisp_get_idx(arg_array, i, argv+i);
356 if(hres == DISP_E_UNKNOWNNAME) {
357 argv[i] = jsval_undefined();
358 }else if(FAILED(hres)) {
359 while(i--)
360 jsval_release(argv[i]);
361 free(argv);
362 return hres;
366 *argc = length;
367 *ret = argv;
368 return S_OK;
371 static HRESULT Function_apply(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
373 jsval_t this_val = jsval_undefined();
374 FunctionInstance *function;
375 jsval_t *args = NULL;
376 unsigned i, cnt = 0;
377 HRESULT hres = S_OK;
379 TRACE("\n");
381 if(is_null_disp(vthis))
382 return JS_E_OBJECT_REQUIRED;
383 if(!is_object_instance(vthis) || (!(function = function_this(vthis)) && to_jsdisp(get_object(vthis))))
384 return JS_E_FUNCTION_EXPECTED;
386 if(argc) {
387 if(ctx->version < SCRIPTLANGUAGEVERSION_ES5 && !is_undefined(argv[0]) && !is_null(argv[0])) {
388 IDispatch *this_obj;
389 hres = to_object(ctx, argv[0], &this_obj);
390 if(FAILED(hres))
391 return hres;
392 this_val = jsval_disp(this_obj);
393 }else {
394 hres = jsval_copy(argv[0], &this_val);
395 if(FAILED(hres))
396 return hres;
400 if(argc >= 2) {
401 jsdisp_t *arg_array = NULL;
403 if(is_object_instance(argv[1])) {
404 arg_array = iface_to_jsdisp(get_object(argv[1]));
405 if(arg_array &&
406 (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
407 jsdisp_release(arg_array);
408 arg_array = NULL;
412 if(arg_array) {
413 hres = array_to_args(ctx, arg_array, &cnt, &args);
414 jsdisp_release(arg_array);
415 }else {
416 FIXME("throw TypeError\n");
417 hres = E_FAIL;
421 if(SUCCEEDED(hres)) {
422 if(function) {
423 hres = function->vtbl->call(ctx, function, this_val, flags, cnt, args, r);
424 }else {
425 jsval_t res;
426 hres = disp_call_value(ctx, get_object(vthis), this_val, DISPATCH_METHOD, cnt, args, &res);
427 if(SUCCEEDED(hres)) {
428 if(r)
429 *r = res;
430 else
431 jsval_release(res);
436 jsval_release(this_val);
437 for(i=0; i < cnt; i++)
438 jsval_release(args[i]);
439 free(args);
440 return hres;
443 static HRESULT Function_call(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
444 jsval_t *r)
446 jsval_t this_val = jsval_undefined();
447 FunctionInstance *function;
448 unsigned cnt = 0;
449 HRESULT hres;
451 TRACE("\n");
453 if(is_null_disp(vthis))
454 return JS_E_OBJECT_REQUIRED;
455 if(!(function = function_this(vthis)))
456 return JS_E_FUNCTION_EXPECTED;
458 if(argc) {
459 if(ctx->version < SCRIPTLANGUAGEVERSION_ES5 && !is_undefined(argv[0]) && !is_null(argv[0])) {
460 IDispatch *this_obj;
461 hres = to_object(ctx, argv[0], &this_obj);
462 if(FAILED(hres))
463 return hres;
464 this_val = jsval_disp(this_obj);
465 }else {
466 hres = jsval_copy(argv[0], &this_val);
467 if(FAILED(hres))
468 return hres;
470 cnt = argc-1;
473 hres = function->vtbl->call(ctx, function, this_val, flags, cnt, argv + 1, r);
475 jsval_release(this_val);
476 return hres;
479 static HRESULT Function_bind(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
480 jsval_t *r)
482 jsval_t bound_this = jsval_undefined();
483 FunctionInstance *function;
484 jsdisp_t *new_function;
485 HRESULT hres;
487 TRACE("\n");
489 if(!(function = function_this(vthis)))
490 return JS_E_FUNCTION_EXPECTED;
492 if(argc < 1) {
493 argc = 1;
494 }else if(is_null(argv[0])) {
495 bound_this = argv[0];
496 }else if(!is_undefined(argv[0])) {
497 IDispatch *obj;
498 hres = to_object(ctx, argv[0], &obj);
499 if(FAILED(hres))
500 return hres;
501 bound_this = jsval_disp(obj);
504 hres = create_bind_function(ctx, function, bound_this, argc - 1, argv + 1, &new_function);
505 jsval_release(bound_this);
506 if(FAILED(hres))
507 return hres;
509 if(r)
510 *r = jsval_obj(new_function);
511 else
512 jsdisp_release(new_function);
513 return S_OK;
516 HRESULT Function_value(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
517 jsval_t *r)
519 FunctionInstance *function;
521 TRACE("\n");
523 if(!(function = function_this(vthis))) {
524 ERR("dispex is not a function\n");
525 return E_FAIL;
528 return function->vtbl->call(ctx, function, vthis, flags, argc, argv, r);
531 HRESULT Function_get_value(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
533 FunctionInstance *function = function_from_jsdisp(jsthis);
534 jsstr_t *str;
535 HRESULT hres;
537 TRACE("\n");
539 hres = function->vtbl->toString(function, &str);
540 if(FAILED(hres))
541 return hres;
543 *r = jsval_string(str);
544 return S_OK;
547 static HRESULT Function_get_arguments(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
549 FunctionInstance *function = function_from_jsdisp(jsthis);
550 call_frame_t *frame;
551 HRESULT hres;
553 TRACE("\n");
555 for(frame = ctx->call_ctx; frame; frame = frame->prev_frame) {
556 if(frame->function_instance == &function->dispex) {
557 if(!frame->arguments_obj) {
558 hres = setup_arguments_object(ctx, frame);
559 if(FAILED(hres))
560 return hres;
562 *r = jsval_obj(jsdisp_addref(frame->arguments_obj));
563 return S_OK;
567 *r = jsval_null();
568 return S_OK;
571 function_code_t *Function_get_code(jsdisp_t *jsthis)
573 FunctionInstance *function;
575 assert(is_class(jsthis, JSCLASS_FUNCTION));
576 function = function_from_jsdisp(jsthis);
578 return function->vtbl->get_code(function);
581 static void Function_destructor(jsdisp_t *dispex)
583 FunctionInstance *function = function_from_jsdisp(dispex);
584 function->vtbl->destructor(function);
585 free(function);
588 static HRESULT Function_gc_traverse(struct gc_ctx *gc_ctx, enum gc_traverse_op op, jsdisp_t *dispex)
590 FunctionInstance *function = function_from_jsdisp(dispex);
591 return function->vtbl->gc_traverse(gc_ctx, op, function);
594 static const builtin_prop_t Function_props[] = {
595 {L"apply", Function_apply, PROPF_METHOD|2},
596 {L"arguments", NULL, PROPF_HTML, Function_get_arguments},
597 {L"bind", Function_bind, PROPF_METHOD|PROPF_ES5|1},
598 {L"call", Function_call, PROPF_METHOD|1},
599 {L"caller", NULL, PROPF_HTML, Function_get_caller},
600 {L"length", NULL, 0, Function_get_length},
601 {L"toString", Function_toString, PROPF_METHOD}
604 static const builtin_info_t Function_info = {
605 JSCLASS_FUNCTION,
606 Function_value,
607 ARRAY_SIZE(Function_props),
608 Function_props,
609 Function_destructor,
610 NULL,
611 NULL,
612 NULL,
613 NULL,
614 Function_gc_traverse
617 static const builtin_prop_t FunctionInst_props[] = {
618 {L"arguments", NULL, 0, Function_get_arguments},
619 {L"caller", NULL, PROPF_HTML, Function_get_caller},
620 {L"length", NULL, 0, Function_get_length}
623 static const builtin_info_t FunctionInst_info = {
624 JSCLASS_FUNCTION,
625 Function_value,
626 ARRAY_SIZE(FunctionInst_props),
627 FunctionInst_props,
628 Function_destructor,
629 NULL,
630 NULL,
631 NULL,
632 NULL,
633 Function_gc_traverse
636 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, const function_vtbl_t *vtbl, size_t size,
637 DWORD flags, BOOL funcprot, jsdisp_t *prototype, void **ret)
639 FunctionInstance *function;
640 HRESULT hres;
642 function = calloc(1, size);
643 if(!function)
644 return E_OUTOFMEMORY;
646 if(funcprot)
647 hres = init_dispex(&function->dispex, ctx, builtin_info, prototype);
648 else if(builtin_info)
649 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
650 else
651 hres = init_dispex_from_constr(&function->dispex, ctx, &FunctionInst_info, ctx->function_constr);
652 if(FAILED(hres)) {
653 free(function);
654 return hres;
657 function->vtbl = vtbl;
658 function->flags = flags;
659 function->length = flags & PROPF_ARGMASK;
661 *ret = function;
662 return S_OK;
665 static HRESULT NativeFunction_call(script_ctx_t *ctx, FunctionInstance *func, jsval_t vthis, unsigned flags,
666 unsigned argc, jsval_t *argv, jsval_t *r)
668 NativeFunction *function = (NativeFunction*)func;
670 if((flags & DISPATCH_CONSTRUCT) && !(function->function.flags & PROPF_CONSTR))
671 return JS_E_INVALID_ACTION;
672 return function->proc(ctx, vthis, flags & ~DISPATCH_JSCRIPT_INTERNAL_MASK, argc, argv, r);
675 static HRESULT NativeFunction_toString(FunctionInstance *func, jsstr_t **ret)
677 NativeFunction *function = (NativeFunction*)func;
678 DWORD name_len;
679 jsstr_t *str;
680 WCHAR *ptr;
682 static const WCHAR native_prefixW[] = L"\nfunction ";
683 static const WCHAR native_suffixW[] = L"() {\n [native code]\n}\n";
685 name_len = function->name ? lstrlenW(function->name) : 0;
686 str = jsstr_alloc_buf(ARRAY_SIZE(native_prefixW) + ARRAY_SIZE(native_suffixW) + name_len - 2, &ptr);
687 if(!str)
688 return E_OUTOFMEMORY;
690 memcpy(ptr, native_prefixW, sizeof(native_prefixW));
691 ptr += ARRAY_SIZE(native_prefixW) - 1;
692 memcpy(ptr, function->name, name_len*sizeof(WCHAR));
693 ptr += name_len;
694 memcpy(ptr, native_suffixW, sizeof(native_suffixW));
696 *ret = str;
697 return S_OK;
700 static function_code_t *NativeFunction_get_code(FunctionInstance *function)
702 return NULL;
705 static void NativeFunction_destructor(FunctionInstance *function)
709 static const function_vtbl_t NativeFunctionVtbl = {
710 NativeFunction_call,
711 NativeFunction_toString,
712 NativeFunction_get_code,
713 NativeFunction_destructor,
714 no_gc_traverse
717 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
718 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
720 NativeFunction *function;
721 HRESULT hres;
723 if(!ctx->function_constr)
724 return E_UNEXPECTED;
726 hres = create_function(ctx, builtin_info, &NativeFunctionVtbl, sizeof(NativeFunction), flags, FALSE, NULL, (void**)&function);
727 if(FAILED(hres))
728 return hres;
730 if(builtin_info)
731 hres = jsdisp_define_data_property(&function->function.dispex, L"length", 0,
732 jsval_number(function->function.length));
733 if(SUCCEEDED(hres))
734 hres = jsdisp_define_data_property(&function->function.dispex, L"prototype", 0, prototype ? jsval_obj(prototype) : jsval_null());
735 if(FAILED(hres)) {
736 jsdisp_release(&function->function.dispex);
737 return hres;
740 function->proc = value_proc;
741 function->name = name;
743 *ret = &function->function.dispex;
744 return S_OK;
747 static HRESULT set_constructor_prop(script_ctx_t *ctx, jsdisp_t *constr, jsdisp_t *prot)
749 return jsdisp_define_data_property(prot, L"constructor", PROPF_WRITABLE | PROPF_CONFIGURABLE,
750 jsval_obj(constr));
753 HRESULT create_builtin_constructor(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
754 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
756 jsdisp_t *constr;
757 HRESULT hres;
759 hres = create_builtin_function(ctx, value_proc, name, builtin_info, flags, prototype, &constr);
760 if(FAILED(hres))
761 return hres;
763 hres = set_constructor_prop(ctx, constr, prototype);
764 if(FAILED(hres)) {
765 jsdisp_release(constr);
766 return hres;
769 *ret = constr;
770 return S_OK;
774 * Create the actual prototype on demand, since it is a circular ref, which prevents the vast
775 * majority of functions from being released quickly, leading to unnecessary scope detach.
777 static HRESULT InterpretedFunction_get_prototype(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
779 jsdisp_t *prototype;
780 HRESULT hres;
782 hres = create_object(ctx, NULL, &prototype);
783 if(FAILED(hres))
784 return hres;
786 hres = jsdisp_define_data_property(jsthis, L"prototype", PROPF_WRITABLE, jsval_obj(prototype));
787 if(SUCCEEDED(hres))
788 hres = set_constructor_prop(ctx, jsthis, prototype);
789 if(FAILED(hres)) {
790 jsdisp_release(prototype);
791 return hres;
794 *r = jsval_obj(prototype);
795 return S_OK;
798 static HRESULT InterpretedFunction_set_prototype(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t value)
800 return jsdisp_define_data_property(jsthis, L"prototype", PROPF_WRITABLE, value);
803 static const builtin_prop_t InterpretedFunction_props[] = {
804 {L"arguments", NULL, 0, Function_get_arguments},
805 {L"caller", NULL, PROPF_HTML, Function_get_caller},
806 {L"length", NULL, 0, Function_get_length},
807 {L"prototype", NULL, 0, InterpretedFunction_get_prototype, InterpretedFunction_set_prototype}
810 static const builtin_info_t InterpretedFunction_info = {
811 JSCLASS_FUNCTION,
812 Function_value,
813 ARRAY_SIZE(InterpretedFunction_props),
814 InterpretedFunction_props,
815 Function_destructor,
816 NULL,
817 NULL,
818 NULL,
819 NULL,
820 Function_gc_traverse
823 static HRESULT InterpretedFunction_call(script_ctx_t *ctx, FunctionInstance *func, jsval_t vthis, unsigned flags,
824 unsigned argc, jsval_t *argv, jsval_t *r)
826 InterpretedFunction *function = (InterpretedFunction*)func;
827 IDispatch *this_obj = NULL;
828 DWORD exec_flags = 0;
829 jsdisp_t *new_obj;
830 HRESULT hres;
832 TRACE("%p\n", function);
834 if(flags & DISPATCH_CONSTRUCT) {
835 hres = create_object(ctx, &function->function.dispex, &new_obj);
836 if(FAILED(hres))
837 return hres;
838 this_obj = to_disp(new_obj);
839 }else if(is_object_instance(vthis)) {
840 this_obj = get_object(vthis);
841 IDispatch_AddRef(this_obj);
842 }else if(ctx->version >= SCRIPTLANGUAGEVERSION_ES5 && !is_undefined(vthis) && !is_null(vthis)) {
843 hres = to_object(ctx, vthis, &this_obj);
844 if(FAILED(hres))
845 return hres;
848 if(flags & DISPATCH_JSCRIPT_CALLEREXECSSOURCE)
849 exec_flags |= EXEC_RETURN_TO_INTERP;
850 if(flags & DISPATCH_CONSTRUCT)
851 exec_flags |= EXEC_CONSTRUCTOR;
852 hres = exec_source(ctx, exec_flags, function->code, function->func_code, function->scope_chain, this_obj,
853 &function->function.dispex, argc, argv, r);
854 if(this_obj)
855 IDispatch_Release(this_obj);
856 return hres;
859 static HRESULT InterpretedFunction_toString(FunctionInstance *func, jsstr_t **ret)
861 InterpretedFunction *function = (InterpretedFunction*)func;
863 *ret = jsstr_alloc_len(function->func_code->source, function->func_code->source_len);
864 return *ret ? S_OK : E_OUTOFMEMORY;
867 static function_code_t *InterpretedFunction_get_code(FunctionInstance *func)
869 InterpretedFunction *function = (InterpretedFunction*)func;
871 return function->func_code;
874 static void InterpretedFunction_destructor(FunctionInstance *func)
876 InterpretedFunction *function = (InterpretedFunction*)func;
878 release_bytecode(function->code);
879 if(function->scope_chain)
880 scope_release(function->scope_chain);
883 static HRESULT InterpretedFunction_gc_traverse(struct gc_ctx *gc_ctx, enum gc_traverse_op op, FunctionInstance *func)
885 InterpretedFunction *function = (InterpretedFunction*)func;
887 if(!function->scope_chain)
888 return S_OK;
889 return gc_process_linked_obj(gc_ctx, op, &function->function.dispex, &function->scope_chain->dispex,
890 (void**)&function->scope_chain);
893 static const function_vtbl_t InterpretedFunctionVtbl = {
894 InterpretedFunction_call,
895 InterpretedFunction_toString,
896 InterpretedFunction_get_code,
897 InterpretedFunction_destructor,
898 InterpretedFunction_gc_traverse
901 HRESULT create_source_function(script_ctx_t *ctx, bytecode_t *code, function_code_t *func_code,
902 scope_chain_t *scope_chain, jsdisp_t **ret)
904 InterpretedFunction *function;
905 HRESULT hres;
907 hres = create_function(ctx, &InterpretedFunction_info, &InterpretedFunctionVtbl, sizeof(InterpretedFunction),
908 PROPF_CONSTR, FALSE, NULL, (void**)&function);
909 if(FAILED(hres))
910 return hres;
912 if(scope_chain) {
913 scope_addref(scope_chain);
914 function->scope_chain = scope_chain;
917 bytecode_addref(code);
918 function->code = code;
919 function->func_code = func_code;
920 function->function.length = function->func_code->param_cnt;
922 *ret = &function->function.dispex;
923 return S_OK;
926 static HRESULT BindFunction_get_arguments(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
928 return JS_E_INVALID_ACTION;
931 static HRESULT BindFunction_get_caller(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
933 return JS_E_INVALID_ACTION;
936 static const builtin_prop_t BindFunction_props[] = {
937 {L"arguments", NULL, 0, BindFunction_get_arguments},
938 {L"caller", NULL, 0, BindFunction_get_caller},
939 {L"length", NULL, 0, Function_get_length}
942 static const builtin_info_t BindFunction_info = {
943 JSCLASS_FUNCTION,
944 Function_value,
945 ARRAY_SIZE(BindFunction_props),
946 BindFunction_props,
947 Function_destructor,
948 NULL,
949 NULL,
950 NULL,
951 NULL,
952 Function_gc_traverse
955 static HRESULT BindFunction_call(script_ctx_t *ctx, FunctionInstance *func, jsval_t vthis, unsigned flags,
956 unsigned argc, jsval_t *argv, jsval_t *r)
958 BindFunction *function = (BindFunction*)func;
959 jsval_t *call_args = NULL;
960 unsigned call_argc;
961 HRESULT hres;
963 TRACE("%p\n", function);
965 call_argc = function->argc + argc;
966 if(call_argc) {
967 call_args = malloc(call_argc * sizeof(*call_args));
968 if(!call_args)
969 return E_OUTOFMEMORY;
971 if(function->argc)
972 memcpy(call_args, function->args, function->argc * sizeof(*call_args));
973 if(argc)
974 memcpy(call_args + function->argc, argv, argc * sizeof(*call_args));
977 hres = function->target->vtbl->call(ctx, function->target, function->this, flags, call_argc, call_args, r);
979 free(call_args);
980 return hres;
983 static HRESULT BindFunction_toString(FunctionInstance *function, jsstr_t **ret)
985 *ret = jsstr_alloc(L"\nfunction() {\n [native code]\n}\n");
986 return *ret ? S_OK : E_OUTOFMEMORY;
989 static function_code_t *BindFunction_get_code(FunctionInstance *function)
991 return NULL;
994 static void BindFunction_destructor(FunctionInstance *func)
996 BindFunction *function = (BindFunction*)func;
997 unsigned i;
999 TRACE("%p\n", function);
1001 for(i = 0; i < function->argc; i++)
1002 jsval_release(function->args[i]);
1003 if(function->target)
1004 jsdisp_release(&function->target->dispex);
1005 jsval_release(function->this);
1008 static HRESULT BindFunction_gc_traverse(struct gc_ctx *gc_ctx, enum gc_traverse_op op, FunctionInstance *func)
1010 BindFunction *function = (BindFunction*)func;
1011 HRESULT hres;
1012 unsigned i;
1014 for(i = 0; i < function->argc; i++) {
1015 hres = gc_process_linked_val(gc_ctx, op, &function->function.dispex, &function->args[i]);
1016 if(FAILED(hres))
1017 return hres;
1020 hres = gc_process_linked_obj(gc_ctx, op, &function->function.dispex, &function->target->dispex, (void**)&function->target);
1021 if(FAILED(hres))
1022 return hres;
1024 return gc_process_linked_val(gc_ctx, op, &function->function.dispex, &function->this);
1027 static const function_vtbl_t BindFunctionVtbl = {
1028 BindFunction_call,
1029 BindFunction_toString,
1030 BindFunction_get_code,
1031 BindFunction_destructor,
1032 BindFunction_gc_traverse
1035 static HRESULT create_bind_function(script_ctx_t *ctx, FunctionInstance *target, jsval_t bound_this, unsigned argc,
1036 jsval_t *argv, jsdisp_t **ret)
1038 BindFunction *function;
1039 HRESULT hres;
1041 hres = create_function(ctx, &BindFunction_info, &BindFunctionVtbl, FIELD_OFFSET(BindFunction, args[argc]), PROPF_METHOD,
1042 FALSE, NULL, (void**)&function);
1043 if(FAILED(hres))
1044 return hres;
1046 jsdisp_addref(&target->dispex);
1047 function->target = target;
1049 hres = jsval_copy(bound_this, &function->this);
1050 if(FAILED(hres)) {
1051 jsdisp_release(&function->function.dispex);
1052 return hres;
1055 for(function->argc = 0; function->argc < argc; function->argc++) {
1056 hres = jsval_copy(argv[function->argc], function->args + function->argc);
1057 if(FAILED(hres)) {
1058 jsdisp_release(&function->function.dispex);
1059 return hres;
1063 function->function.length = target->length > argc ? target->length - argc : 0;
1065 *ret = &function->function.dispex;
1066 return S_OK;
1069 static HRESULT construct_function(script_ctx_t *ctx, unsigned argc, jsval_t *argv, IDispatch **ret)
1071 WCHAR *str = NULL, *ptr;
1072 unsigned len = 0, i = 0;
1073 bytecode_t *code;
1074 jsdisp_t *function;
1075 jsstr_t **params = NULL;
1076 int j = 0;
1077 HRESULT hres = S_OK;
1079 static const WCHAR function_anonymousW[] = L"function anonymous(";
1080 static const WCHAR function_beginW[] = L") {\n";
1081 static const WCHAR function_endW[] = L"\n}";
1083 if(argc) {
1084 params = malloc(argc*sizeof(*params));
1085 if(!params)
1086 return E_OUTOFMEMORY;
1088 if(argc > 2)
1089 len = (argc-2)*2; /* separating commas */
1090 for(i=0; i < argc; i++) {
1091 hres = to_string(ctx, argv[i], params+i);
1092 if(FAILED(hres))
1093 break;
1094 len += jsstr_length(params[i]);
1098 if(SUCCEEDED(hres)) {
1099 len += ARRAY_SIZE(function_anonymousW) + ARRAY_SIZE(function_beginW) + ARRAY_SIZE(function_endW) - 2;
1100 str = malloc(len*sizeof(WCHAR));
1101 if(str) {
1102 memcpy(str, function_anonymousW, sizeof(function_anonymousW));
1103 ptr = str + ARRAY_SIZE(function_anonymousW) - 1;
1104 if(argc > 1) {
1105 while(1) {
1106 ptr += jsstr_flush(params[j], ptr);
1107 if(++j == argc-1)
1108 break;
1109 *ptr++ = ',';
1110 *ptr++ = ' ';
1113 memcpy(ptr, function_beginW, sizeof(function_beginW));
1114 ptr += ARRAY_SIZE(function_beginW) - 1;
1115 if(argc)
1116 ptr += jsstr_flush(params[argc-1], ptr);
1117 memcpy(ptr, function_endW, sizeof(function_endW));
1119 TRACE("%s\n", debugstr_w(str));
1120 }else {
1121 hres = E_OUTOFMEMORY;
1125 while(i)
1126 jsstr_release(params[--i]);
1127 free(params);
1128 if(FAILED(hres))
1129 return hres;
1131 hres = compile_script(ctx, str, 0, 0, NULL, NULL, FALSE, FALSE,
1132 ctx->call_ctx ? ctx->call_ctx->bytecode->named_item : NULL, &code);
1133 free(str);
1134 if(FAILED(hres))
1135 return hres;
1137 if(code->global_code.func_cnt != 1 || code->global_code.var_cnt != 1) {
1138 ERR("Invalid parser result!\n");
1139 release_bytecode(code);
1140 return E_UNEXPECTED;
1143 hres = create_source_function(ctx, code, code->global_code.funcs, NULL, &function);
1144 release_bytecode(code);
1145 if(FAILED(hres))
1146 return hres;
1148 *ret = to_disp(function);
1149 return S_OK;
1152 static HRESULT FunctionConstr_value(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
1153 jsval_t *r)
1155 HRESULT hres;
1157 TRACE("\n");
1159 switch(flags) {
1160 case DISPATCH_METHOD:
1161 case DISPATCH_CONSTRUCT: {
1162 IDispatch *ret;
1164 hres = construct_function(ctx, argc, argv, &ret);
1165 if(FAILED(hres))
1166 return hres;
1168 if(r) *r = jsval_disp(ret);
1169 else IDispatch_Release(ret);
1170 break;
1172 default:
1173 FIXME("unimplemented flags %x\n", flags);
1174 return E_NOTIMPL;
1177 return S_OK;
1180 static HRESULT FunctionProt_value(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
1181 jsval_t *r)
1183 FIXME("\n");
1184 return E_NOTIMPL;
1187 BOOL is_builtin_eval_func(jsdisp_t *jsdisp)
1189 return is_class(jsdisp, JSCLASS_FUNCTION) && function_from_jsdisp(jsdisp)->vtbl == &NativeFunctionVtbl &&
1190 ((NativeFunction*)function_from_jsdisp(jsdisp))->proc == JSGlobal_eval;
1193 HRESULT init_function_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
1195 NativeFunction *prot, *constr;
1196 HRESULT hres;
1198 hres = create_function(ctx, &Function_info, &NativeFunctionVtbl, sizeof(NativeFunction), PROPF_CONSTR,
1199 TRUE, object_prototype, (void**)&prot);
1200 if(FAILED(hres))
1201 return hres;
1203 prot->proc = FunctionProt_value;
1204 prot->name = L"prototype";
1206 hres = create_function(ctx, &FunctionInst_info, &NativeFunctionVtbl, sizeof(NativeFunction), PROPF_CONSTR|1,
1207 TRUE, &prot->function.dispex, (void**)&constr);
1208 if(SUCCEEDED(hres)) {
1209 constr->proc = FunctionConstr_value;
1210 constr->name = L"Function";
1211 hres = jsdisp_define_data_property(&constr->function.dispex, L"prototype", 0, jsval_obj(&prot->function.dispex));
1212 if(SUCCEEDED(hres))
1213 hres = set_constructor_prop(ctx, &constr->function.dispex, &prot->function.dispex);
1214 if(FAILED(hres))
1215 jsdisp_release(&constr->function.dispex);
1217 jsdisp_release(&prot->function.dispex);
1218 if(FAILED(hres))
1219 return hres;
1221 ctx->function_constr = &constr->function.dispex;
1222 return S_OK;