jscript: Don't expose Function.prototype.arguments in non-html mode.
[wine.git] / dlls / jscript / function.c
bloba0450c57fe4be0673d246e0d35a2e4fb7951ca21
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 call_frame_t *frame;
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 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->detached_vars)
130 return arguments->jsdisp.ctx->stack + arguments->frame->arguments_off + idx;
131 return arguments->frame->base_scope->detached_vars->var + idx;
134 static HRESULT Arguments_idx_get(jsdisp_t *jsdisp, unsigned idx, jsval_t *r)
136 ArgumentsInstance *arguments = arguments_from_jsdisp(jsdisp);
138 TRACE("%p[%u]\n", arguments, idx);
140 return jsval_copy(*get_argument_ref(arguments, idx), r);
143 static HRESULT Arguments_idx_put(jsdisp_t *jsdisp, unsigned idx, jsval_t val)
145 ArgumentsInstance *arguments = arguments_from_jsdisp(jsdisp);
146 jsval_t copy, *ref;
147 HRESULT hres;
149 TRACE("%p[%u] = %s\n", arguments, idx, debugstr_jsval(val));
151 hres = jsval_copy(val, &copy);
152 if(FAILED(hres))
153 return hres;
155 ref = get_argument_ref(arguments, idx);
156 jsval_release(*ref);
157 *ref = copy;
158 return S_OK;
161 static HRESULT Arguments_gc_traverse(struct gc_ctx *gc_ctx, enum gc_traverse_op op, jsdisp_t *jsdisp)
163 ArgumentsInstance *arguments = arguments_from_jsdisp(jsdisp);
164 HRESULT hres;
165 unsigned i;
167 if(arguments->buf) {
168 for(i = 0; i < arguments->argc; i++) {
169 hres = gc_process_linked_val(gc_ctx, op, jsdisp, &arguments->buf[i]);
170 if(FAILED(hres))
171 return hres;
175 return S_OK;
178 static const builtin_info_t Arguments_info = {
179 JSCLASS_ARGUMENTS,
180 Arguments_value,
181 0, NULL,
182 Arguments_destructor,
183 NULL,
184 Arguments_idx_length,
185 Arguments_idx_get,
186 Arguments_idx_put,
187 Arguments_gc_traverse
190 HRESULT setup_arguments_object(script_ctx_t *ctx, call_frame_t *frame)
192 ArgumentsInstance *args;
193 HRESULT hres;
195 args = calloc(1, sizeof(*args));
196 if(!args)
197 return E_OUTOFMEMORY;
199 hres = init_dispex_from_constr(&args->jsdisp, ctx, &Arguments_info, ctx->object_constr);
200 if(FAILED(hres)) {
201 free(args);
202 return hres;
205 args->argc = frame->argc;
206 args->frame = frame;
208 hres = jsdisp_define_data_property(&args->jsdisp, L"length", PROPF_WRITABLE | PROPF_CONFIGURABLE,
209 jsval_number(args->argc));
210 if(SUCCEEDED(hres))
211 hres = jsdisp_define_data_property(&args->jsdisp, L"callee", PROPF_WRITABLE | PROPF_CONFIGURABLE,
212 jsval_obj(frame->function_instance));
213 if(SUCCEEDED(hres))
214 hres = jsdisp_propput(as_jsdisp(frame->base_scope->obj), L"arguments", PROPF_WRITABLE, TRUE, jsval_obj(&args->jsdisp));
215 if(FAILED(hres)) {
216 jsdisp_release(&args->jsdisp);
217 return hres;
220 frame->arguments_obj = &args->jsdisp;
221 return S_OK;
224 void detach_arguments_object(jsdisp_t *args_disp)
226 ArgumentsInstance *arguments = arguments_from_jsdisp(args_disp);
227 call_frame_t *frame = arguments->frame;
228 const BOOL on_stack = frame->base_scope->frame == frame;
229 jsdisp_t *jsobj = as_jsdisp(frame->base_scope->obj);
231 /* Reset arguments value to cut the reference cycle. Note that since all activation contexts have
232 * their own arguments property, it's impossible to use prototype's one during name lookup */
233 jsdisp_propput_name(jsobj, L"arguments", jsval_undefined());
234 arguments->frame = NULL;
236 /* Don't bother coppying arguments if call frame holds the last reference. */
237 if(arguments->jsdisp.ref > 1) {
238 arguments->buf = malloc(arguments->argc * sizeof(*arguments->buf));
239 if(arguments->buf) {
240 const jsval_t *args = on_stack ? arguments->jsdisp.ctx->stack + frame->arguments_off : frame->base_scope->detached_vars->var;
241 int i;
243 for(i = 0; i < arguments->argc ; i++) {
244 if(FAILED(jsval_copy(args[i], &arguments->buf[i])))
245 arguments->buf[i] = jsval_undefined();
247 }else {
248 ERR("out of memory\n");
249 arguments->argc = 0;
253 jsdisp_release(frame->arguments_obj);
256 HRESULT Function_invoke(jsdisp_t *func_this, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
258 FunctionInstance *function;
260 TRACE("func %p this %s\n", func_this, debugstr_jsval(vthis));
262 assert(is_class(func_this, JSCLASS_FUNCTION));
263 function = function_from_jsdisp(func_this);
265 if(function->dispex.ctx->state == SCRIPTSTATE_UNINITIALIZED || function->dispex.ctx->state == SCRIPTSTATE_CLOSED) {
266 WARN("Script engine state does not allow running code.\n");
267 return E_UNEXPECTED;
270 return function->vtbl->call(function->dispex.ctx, function, vthis, flags, argc, argv, r);
273 static HRESULT Function_get_caller(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
275 FunctionInstance *function = function_from_jsdisp(jsthis);
276 call_frame_t *frame;
278 TRACE("%p\n", jsthis);
280 for(frame = ctx->call_ctx; frame; frame = frame->prev_frame) {
281 if(frame->function_instance == &function->dispex) {
282 if(!frame->prev_frame || !frame->prev_frame->function_instance)
283 break;
284 *r = jsval_obj(jsdisp_addref(frame->prev_frame->function_instance));
285 return S_OK;
289 *r = jsval_null();
290 return S_OK;
293 static HRESULT Function_get_length(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
295 TRACE("%p\n", jsthis);
297 *r = jsval_number(function_from_jsdisp(jsthis)->length);
298 return S_OK;
301 static HRESULT Function_toString(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
302 jsval_t *r)
304 FunctionInstance *function;
305 jsstr_t *str;
306 HRESULT hres;
308 TRACE("\n");
310 if(!(function = function_this(vthis)))
311 return JS_E_FUNCTION_EXPECTED;
313 hres = function->vtbl->toString(function, &str);
314 if(FAILED(hres))
315 return hres;
317 if(r)
318 *r = jsval_string(str);
319 else
320 jsstr_release(str);
321 return S_OK;
324 static HRESULT array_to_args(script_ctx_t *ctx, jsdisp_t *arg_array, unsigned *argc, jsval_t **ret)
326 jsval_t *argv, val;
327 UINT32 length, i;
328 HRESULT hres;
330 hres = jsdisp_propget_name(arg_array, L"length", &val);
331 if(FAILED(hres))
332 return hres;
334 hres = to_uint32(ctx, val, &length);
335 jsval_release(val);
336 if(FAILED(hres))
337 return hres;
339 argv = malloc(length * sizeof(*argv));
340 if(!argv)
341 return E_OUTOFMEMORY;
343 for(i=0; i<length; i++) {
344 hres = jsdisp_get_idx(arg_array, i, argv+i);
345 if(hres == DISP_E_UNKNOWNNAME) {
346 argv[i] = jsval_undefined();
347 }else if(FAILED(hres)) {
348 while(i--)
349 jsval_release(argv[i]);
350 free(argv);
351 return hres;
355 *argc = length;
356 *ret = argv;
357 return S_OK;
360 static HRESULT Function_apply(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
362 jsval_t this_val = jsval_undefined();
363 FunctionInstance *function;
364 jsval_t *args = NULL;
365 unsigned i, cnt = 0;
366 HRESULT hres = S_OK;
368 TRACE("\n");
370 if(is_null_disp(vthis))
371 return JS_E_OBJECT_REQUIRED;
372 if(!is_object_instance(vthis) || (!(function = function_this(vthis)) && to_jsdisp(get_object(vthis))))
373 return JS_E_FUNCTION_EXPECTED;
375 if(argc) {
376 if(ctx->version < SCRIPTLANGUAGEVERSION_ES5 && !is_undefined(argv[0]) && !is_null(argv[0])) {
377 IDispatch *this_obj;
378 hres = to_object(ctx, argv[0], &this_obj);
379 if(FAILED(hres))
380 return hres;
381 this_val = jsval_disp(this_obj);
382 }else {
383 hres = jsval_copy(argv[0], &this_val);
384 if(FAILED(hres))
385 return hres;
389 if(argc >= 2) {
390 jsdisp_t *arg_array = NULL;
392 if(is_object_instance(argv[1])) {
393 arg_array = iface_to_jsdisp(get_object(argv[1]));
394 if(arg_array &&
395 (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) {
396 jsdisp_release(arg_array);
397 arg_array = NULL;
401 if(arg_array) {
402 hres = array_to_args(ctx, arg_array, &cnt, &args);
403 jsdisp_release(arg_array);
404 }else {
405 FIXME("throw TypeError\n");
406 hres = E_FAIL;
410 if(SUCCEEDED(hres)) {
411 if(function) {
412 hres = function->vtbl->call(ctx, function, this_val, flags, cnt, args, r);
413 }else {
414 jsval_t res;
415 hres = disp_call_value(ctx, get_object(vthis), this_val, DISPATCH_METHOD, cnt, args, &res);
416 if(SUCCEEDED(hres)) {
417 if(r)
418 *r = res;
419 else
420 jsval_release(res);
425 jsval_release(this_val);
426 for(i=0; i < cnt; i++)
427 jsval_release(args[i]);
428 free(args);
429 return hres;
432 static HRESULT Function_call(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
433 jsval_t *r)
435 jsval_t this_val = jsval_undefined();
436 FunctionInstance *function;
437 unsigned cnt = 0;
438 HRESULT hres;
440 TRACE("\n");
442 if(is_null_disp(vthis))
443 return JS_E_OBJECT_REQUIRED;
444 if(!(function = function_this(vthis)))
445 return JS_E_FUNCTION_EXPECTED;
447 if(argc) {
448 if(ctx->version < SCRIPTLANGUAGEVERSION_ES5 && !is_undefined(argv[0]) && !is_null(argv[0])) {
449 IDispatch *this_obj;
450 hres = to_object(ctx, argv[0], &this_obj);
451 if(FAILED(hres))
452 return hres;
453 this_val = jsval_disp(this_obj);
454 }else {
455 hres = jsval_copy(argv[0], &this_val);
456 if(FAILED(hres))
457 return hres;
459 cnt = argc-1;
462 hres = function->vtbl->call(ctx, function, this_val, flags, cnt, argv + 1, r);
464 jsval_release(this_val);
465 return hres;
468 static HRESULT Function_bind(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
469 jsval_t *r)
471 jsval_t bound_this = jsval_undefined();
472 FunctionInstance *function;
473 jsdisp_t *new_function;
474 HRESULT hres;
476 TRACE("\n");
478 if(!(function = function_this(vthis)))
479 return JS_E_FUNCTION_EXPECTED;
481 if(argc < 1) {
482 argc = 1;
483 }else if(is_null(argv[0])) {
484 bound_this = argv[0];
485 }else if(!is_undefined(argv[0])) {
486 IDispatch *obj;
487 hres = to_object(ctx, argv[0], &obj);
488 if(FAILED(hres))
489 return hres;
490 bound_this = jsval_disp(obj);
493 hres = create_bind_function(ctx, function, bound_this, argc - 1, argv + 1, &new_function);
494 jsval_release(bound_this);
495 if(FAILED(hres))
496 return hres;
498 if(r)
499 *r = jsval_obj(new_function);
500 else
501 jsdisp_release(new_function);
502 return S_OK;
505 HRESULT Function_value(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
506 jsval_t *r)
508 FunctionInstance *function;
510 TRACE("\n");
512 if(!(function = function_this(vthis))) {
513 ERR("dispex is not a function\n");
514 return E_FAIL;
517 return function->vtbl->call(ctx, function, vthis, flags, argc, argv, r);
520 HRESULT Function_get_value(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
522 FunctionInstance *function = function_from_jsdisp(jsthis);
523 jsstr_t *str;
524 HRESULT hres;
526 TRACE("\n");
528 hres = function->vtbl->toString(function, &str);
529 if(FAILED(hres))
530 return hres;
532 *r = jsval_string(str);
533 return S_OK;
536 static HRESULT Function_get_arguments(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
538 FunctionInstance *function = function_from_jsdisp(jsthis);
539 call_frame_t *frame;
540 HRESULT hres;
542 TRACE("\n");
544 for(frame = ctx->call_ctx; frame; frame = frame->prev_frame) {
545 if(frame->function_instance == &function->dispex) {
546 if(!frame->arguments_obj) {
547 hres = setup_arguments_object(ctx, frame);
548 if(FAILED(hres))
549 return hres;
551 *r = jsval_obj(jsdisp_addref(frame->arguments_obj));
552 return S_OK;
556 *r = jsval_null();
557 return S_OK;
560 function_code_t *Function_get_code(jsdisp_t *jsthis)
562 FunctionInstance *function;
564 assert(is_class(jsthis, JSCLASS_FUNCTION));
565 function = function_from_jsdisp(jsthis);
567 return function->vtbl->get_code(function);
570 static void Function_destructor(jsdisp_t *dispex)
572 FunctionInstance *function = function_from_jsdisp(dispex);
573 function->vtbl->destructor(function);
574 free(function);
577 static HRESULT Function_gc_traverse(struct gc_ctx *gc_ctx, enum gc_traverse_op op, jsdisp_t *dispex)
579 FunctionInstance *function = function_from_jsdisp(dispex);
580 return function->vtbl->gc_traverse(gc_ctx, op, function);
583 static const builtin_prop_t Function_props[] = {
584 {L"apply", Function_apply, PROPF_METHOD|2},
585 {L"arguments", NULL, PROPF_HTML, Function_get_arguments},
586 {L"bind", Function_bind, PROPF_METHOD|PROPF_ES5|1},
587 {L"call", Function_call, PROPF_METHOD|1},
588 {L"caller", NULL, PROPF_HTML, Function_get_caller},
589 {L"length", NULL, 0, Function_get_length},
590 {L"toString", Function_toString, PROPF_METHOD}
593 static const builtin_info_t Function_info = {
594 JSCLASS_FUNCTION,
595 Function_value,
596 ARRAY_SIZE(Function_props),
597 Function_props,
598 Function_destructor,
599 NULL,
600 NULL,
601 NULL,
602 NULL,
603 Function_gc_traverse
606 static const builtin_prop_t FunctionInst_props[] = {
607 {L"arguments", NULL, 0, Function_get_arguments},
608 {L"caller", NULL, PROPF_HTML, Function_get_caller},
609 {L"length", NULL, 0, Function_get_length}
612 static const builtin_info_t FunctionInst_info = {
613 JSCLASS_FUNCTION,
614 Function_value,
615 ARRAY_SIZE(FunctionInst_props),
616 FunctionInst_props,
617 Function_destructor,
618 NULL,
619 NULL,
620 NULL,
621 NULL,
622 Function_gc_traverse
625 static HRESULT create_function(script_ctx_t *ctx, const builtin_info_t *builtin_info, const function_vtbl_t *vtbl, size_t size,
626 DWORD flags, BOOL funcprot, jsdisp_t *prototype, void **ret)
628 FunctionInstance *function;
629 HRESULT hres;
631 function = calloc(1, size);
632 if(!function)
633 return E_OUTOFMEMORY;
635 if(funcprot)
636 hres = init_dispex(&function->dispex, ctx, builtin_info, prototype);
637 else if(builtin_info)
638 hres = init_dispex_from_constr(&function->dispex, ctx, builtin_info, ctx->function_constr);
639 else
640 hres = init_dispex_from_constr(&function->dispex, ctx, &FunctionInst_info, ctx->function_constr);
641 if(FAILED(hres)) {
642 free(function);
643 return hres;
646 function->vtbl = vtbl;
647 function->flags = flags;
648 function->length = flags & PROPF_ARGMASK;
650 *ret = function;
651 return S_OK;
654 static HRESULT NativeFunction_call(script_ctx_t *ctx, FunctionInstance *func, jsval_t vthis, unsigned flags,
655 unsigned argc, jsval_t *argv, jsval_t *r)
657 NativeFunction *function = (NativeFunction*)func;
659 if((flags & DISPATCH_CONSTRUCT) && !(function->function.flags & PROPF_CONSTR))
660 return JS_E_INVALID_ACTION;
661 return function->proc(ctx, vthis, flags & ~DISPATCH_JSCRIPT_INTERNAL_MASK, argc, argv, r);
664 static HRESULT NativeFunction_toString(FunctionInstance *func, jsstr_t **ret)
666 NativeFunction *function = (NativeFunction*)func;
667 DWORD name_len;
668 jsstr_t *str;
669 WCHAR *ptr;
671 static const WCHAR native_prefixW[] = L"\nfunction ";
672 static const WCHAR native_suffixW[] = L"() {\n [native code]\n}\n";
674 name_len = function->name ? lstrlenW(function->name) : 0;
675 str = jsstr_alloc_buf(ARRAY_SIZE(native_prefixW) + ARRAY_SIZE(native_suffixW) + name_len - 2, &ptr);
676 if(!str)
677 return E_OUTOFMEMORY;
679 memcpy(ptr, native_prefixW, sizeof(native_prefixW));
680 ptr += ARRAY_SIZE(native_prefixW) - 1;
681 memcpy(ptr, function->name, name_len*sizeof(WCHAR));
682 ptr += name_len;
683 memcpy(ptr, native_suffixW, sizeof(native_suffixW));
685 *ret = str;
686 return S_OK;
689 static function_code_t *NativeFunction_get_code(FunctionInstance *function)
691 return NULL;
694 static void NativeFunction_destructor(FunctionInstance *function)
698 static const function_vtbl_t NativeFunctionVtbl = {
699 NativeFunction_call,
700 NativeFunction_toString,
701 NativeFunction_get_code,
702 NativeFunction_destructor,
703 no_gc_traverse
706 HRESULT create_builtin_function(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
707 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
709 NativeFunction *function;
710 HRESULT hres;
712 if(!ctx->function_constr)
713 return E_UNEXPECTED;
715 hres = create_function(ctx, builtin_info, &NativeFunctionVtbl, sizeof(NativeFunction), flags, FALSE, NULL, (void**)&function);
716 if(FAILED(hres))
717 return hres;
719 if(builtin_info)
720 hres = jsdisp_define_data_property(&function->function.dispex, L"length", 0,
721 jsval_number(function->function.length));
722 if(SUCCEEDED(hres))
723 hres = jsdisp_define_data_property(&function->function.dispex, L"prototype", 0, prototype ? jsval_obj(prototype) : jsval_null());
724 if(FAILED(hres)) {
725 jsdisp_release(&function->function.dispex);
726 return hres;
729 function->proc = value_proc;
730 function->name = name;
732 *ret = &function->function.dispex;
733 return S_OK;
736 static HRESULT set_constructor_prop(script_ctx_t *ctx, jsdisp_t *constr, jsdisp_t *prot)
738 return jsdisp_define_data_property(prot, L"constructor", PROPF_WRITABLE | PROPF_CONFIGURABLE,
739 jsval_obj(constr));
742 HRESULT create_builtin_constructor(script_ctx_t *ctx, builtin_invoke_t value_proc, const WCHAR *name,
743 const builtin_info_t *builtin_info, DWORD flags, jsdisp_t *prototype, jsdisp_t **ret)
745 jsdisp_t *constr;
746 HRESULT hres;
748 hres = create_builtin_function(ctx, value_proc, name, builtin_info, flags, prototype, &constr);
749 if(FAILED(hres))
750 return hres;
752 hres = set_constructor_prop(ctx, constr, prototype);
753 if(FAILED(hres)) {
754 jsdisp_release(constr);
755 return hres;
758 *ret = constr;
759 return S_OK;
763 * Create the actual prototype on demand, since it is a circular ref, which prevents the vast
764 * majority of functions from being released quickly, leading to unnecessary scope detach.
766 static HRESULT InterpretedFunction_get_prototype(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
768 jsdisp_t *prototype;
769 HRESULT hres;
771 hres = create_object(ctx, NULL, &prototype);
772 if(FAILED(hres))
773 return hres;
775 hres = jsdisp_define_data_property(jsthis, L"prototype", PROPF_WRITABLE, jsval_obj(prototype));
776 if(SUCCEEDED(hres))
777 hres = set_constructor_prop(ctx, jsthis, prototype);
778 if(FAILED(hres)) {
779 jsdisp_release(prototype);
780 return hres;
783 *r = jsval_obj(prototype);
784 return S_OK;
787 static HRESULT InterpretedFunction_set_prototype(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t value)
789 return jsdisp_define_data_property(jsthis, L"prototype", PROPF_WRITABLE, value);
792 static const builtin_prop_t InterpretedFunction_props[] = {
793 {L"arguments", NULL, 0, Function_get_arguments},
794 {L"caller", NULL, PROPF_HTML, Function_get_caller},
795 {L"length", NULL, 0, Function_get_length},
796 {L"prototype", NULL, 0, InterpretedFunction_get_prototype, InterpretedFunction_set_prototype}
799 static const builtin_info_t InterpretedFunction_info = {
800 JSCLASS_FUNCTION,
801 Function_value,
802 ARRAY_SIZE(InterpretedFunction_props),
803 InterpretedFunction_props,
804 Function_destructor,
805 NULL,
806 NULL,
807 NULL,
808 NULL,
809 Function_gc_traverse
812 static HRESULT InterpretedFunction_call(script_ctx_t *ctx, FunctionInstance *func, jsval_t vthis, unsigned flags,
813 unsigned argc, jsval_t *argv, jsval_t *r)
815 InterpretedFunction *function = (InterpretedFunction*)func;
816 IDispatch *this_obj = NULL;
817 DWORD exec_flags = 0;
818 jsdisp_t *new_obj;
819 HRESULT hres;
821 TRACE("%p\n", function);
823 if(flags & DISPATCH_CONSTRUCT) {
824 hres = create_object(ctx, &function->function.dispex, &new_obj);
825 if(FAILED(hres))
826 return hres;
827 this_obj = to_disp(new_obj);
828 }else if(is_object_instance(vthis)) {
829 this_obj = get_object(vthis);
830 IDispatch_AddRef(this_obj);
831 }else if(ctx->version >= SCRIPTLANGUAGEVERSION_ES5 && !is_undefined(vthis) && !is_null(vthis)) {
832 hres = to_object(ctx, vthis, &this_obj);
833 if(FAILED(hres))
834 return hres;
837 if(flags & DISPATCH_JSCRIPT_CALLEREXECSSOURCE)
838 exec_flags |= EXEC_RETURN_TO_INTERP;
839 if(flags & DISPATCH_CONSTRUCT)
840 exec_flags |= EXEC_CONSTRUCTOR;
841 hres = exec_source(ctx, exec_flags, function->code, function->func_code, function->scope_chain, this_obj,
842 &function->function.dispex, argc, argv, r);
843 if(this_obj)
844 IDispatch_Release(this_obj);
845 return hres;
848 static HRESULT InterpretedFunction_toString(FunctionInstance *func, jsstr_t **ret)
850 InterpretedFunction *function = (InterpretedFunction*)func;
852 *ret = jsstr_alloc_len(function->func_code->source, function->func_code->source_len);
853 return *ret ? S_OK : E_OUTOFMEMORY;
856 static function_code_t *InterpretedFunction_get_code(FunctionInstance *func)
858 InterpretedFunction *function = (InterpretedFunction*)func;
860 return function->func_code;
863 static void InterpretedFunction_destructor(FunctionInstance *func)
865 InterpretedFunction *function = (InterpretedFunction*)func;
867 release_bytecode(function->code);
868 if(function->scope_chain)
869 scope_release(function->scope_chain);
872 static HRESULT InterpretedFunction_gc_traverse(struct gc_ctx *gc_ctx, enum gc_traverse_op op, FunctionInstance *func)
874 InterpretedFunction *function = (InterpretedFunction*)func;
876 if(!function->scope_chain)
877 return S_OK;
878 return gc_process_linked_obj(gc_ctx, op, &function->function.dispex, &function->scope_chain->dispex,
879 (void**)&function->scope_chain);
882 static const function_vtbl_t InterpretedFunctionVtbl = {
883 InterpretedFunction_call,
884 InterpretedFunction_toString,
885 InterpretedFunction_get_code,
886 InterpretedFunction_destructor,
887 InterpretedFunction_gc_traverse
890 HRESULT create_source_function(script_ctx_t *ctx, bytecode_t *code, function_code_t *func_code,
891 scope_chain_t *scope_chain, jsdisp_t **ret)
893 InterpretedFunction *function;
894 HRESULT hres;
896 hres = create_function(ctx, &InterpretedFunction_info, &InterpretedFunctionVtbl, sizeof(InterpretedFunction),
897 PROPF_CONSTR, FALSE, NULL, (void**)&function);
898 if(FAILED(hres))
899 return hres;
901 if(scope_chain) {
902 scope_addref(scope_chain);
903 function->scope_chain = scope_chain;
906 bytecode_addref(code);
907 function->code = code;
908 function->func_code = func_code;
909 function->function.length = function->func_code->param_cnt;
911 *ret = &function->function.dispex;
912 return S_OK;
915 static HRESULT BindFunction_get_arguments(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
917 return JS_E_INVALID_ACTION;
920 static HRESULT BindFunction_get_caller(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
922 return JS_E_INVALID_ACTION;
925 static const builtin_prop_t BindFunction_props[] = {
926 {L"arguments", NULL, 0, BindFunction_get_arguments},
927 {L"caller", NULL, 0, BindFunction_get_caller},
928 {L"length", NULL, 0, Function_get_length}
931 static const builtin_info_t BindFunction_info = {
932 JSCLASS_FUNCTION,
933 Function_value,
934 ARRAY_SIZE(BindFunction_props),
935 BindFunction_props,
936 Function_destructor,
937 NULL,
938 NULL,
939 NULL,
940 NULL,
941 Function_gc_traverse
944 static HRESULT BindFunction_call(script_ctx_t *ctx, FunctionInstance *func, jsval_t vthis, unsigned flags,
945 unsigned argc, jsval_t *argv, jsval_t *r)
947 BindFunction *function = (BindFunction*)func;
948 jsval_t *call_args = NULL;
949 unsigned call_argc;
950 HRESULT hres;
952 TRACE("%p\n", function);
954 call_argc = function->argc + argc;
955 if(call_argc) {
956 call_args = malloc(call_argc * sizeof(*call_args));
957 if(!call_args)
958 return E_OUTOFMEMORY;
960 if(function->argc)
961 memcpy(call_args, function->args, function->argc * sizeof(*call_args));
962 if(argc)
963 memcpy(call_args + function->argc, argv, argc * sizeof(*call_args));
966 hres = function->target->vtbl->call(ctx, function->target, function->this, flags, call_argc, call_args, r);
968 free(call_args);
969 return hres;
972 static HRESULT BindFunction_toString(FunctionInstance *function, jsstr_t **ret)
974 *ret = jsstr_alloc(L"\nfunction() {\n [native code]\n}\n");
975 return *ret ? S_OK : E_OUTOFMEMORY;
978 static function_code_t *BindFunction_get_code(FunctionInstance *function)
980 return NULL;
983 static void BindFunction_destructor(FunctionInstance *func)
985 BindFunction *function = (BindFunction*)func;
986 unsigned i;
988 TRACE("%p\n", function);
990 for(i = 0; i < function->argc; i++)
991 jsval_release(function->args[i]);
992 if(function->target)
993 jsdisp_release(&function->target->dispex);
994 jsval_release(function->this);
997 static HRESULT BindFunction_gc_traverse(struct gc_ctx *gc_ctx, enum gc_traverse_op op, FunctionInstance *func)
999 BindFunction *function = (BindFunction*)func;
1000 HRESULT hres;
1001 unsigned i;
1003 for(i = 0; i < function->argc; i++) {
1004 hres = gc_process_linked_val(gc_ctx, op, &function->function.dispex, &function->args[i]);
1005 if(FAILED(hres))
1006 return hres;
1009 hres = gc_process_linked_obj(gc_ctx, op, &function->function.dispex, &function->target->dispex, (void**)&function->target);
1010 if(FAILED(hres))
1011 return hres;
1013 return gc_process_linked_val(gc_ctx, op, &function->function.dispex, &function->this);
1016 static const function_vtbl_t BindFunctionVtbl = {
1017 BindFunction_call,
1018 BindFunction_toString,
1019 BindFunction_get_code,
1020 BindFunction_destructor,
1021 BindFunction_gc_traverse
1024 static HRESULT create_bind_function(script_ctx_t *ctx, FunctionInstance *target, jsval_t bound_this, unsigned argc,
1025 jsval_t *argv, jsdisp_t **ret)
1027 BindFunction *function;
1028 HRESULT hres;
1030 hres = create_function(ctx, &BindFunction_info, &BindFunctionVtbl, FIELD_OFFSET(BindFunction, args[argc]), PROPF_METHOD,
1031 FALSE, NULL, (void**)&function);
1032 if(FAILED(hres))
1033 return hres;
1035 jsdisp_addref(&target->dispex);
1036 function->target = target;
1038 hres = jsval_copy(bound_this, &function->this);
1039 if(FAILED(hres)) {
1040 jsdisp_release(&function->function.dispex);
1041 return hres;
1044 for(function->argc = 0; function->argc < argc; function->argc++) {
1045 hres = jsval_copy(argv[function->argc], function->args + function->argc);
1046 if(FAILED(hres)) {
1047 jsdisp_release(&function->function.dispex);
1048 return hres;
1052 function->function.length = target->length > argc ? target->length - argc : 0;
1054 *ret = &function->function.dispex;
1055 return S_OK;
1058 static HRESULT construct_function(script_ctx_t *ctx, unsigned argc, jsval_t *argv, IDispatch **ret)
1060 WCHAR *str = NULL, *ptr;
1061 unsigned len = 0, i = 0;
1062 bytecode_t *code;
1063 jsdisp_t *function;
1064 jsstr_t **params = NULL;
1065 int j = 0;
1066 HRESULT hres = S_OK;
1068 static const WCHAR function_anonymousW[] = L"function anonymous(";
1069 static const WCHAR function_beginW[] = L") {\n";
1070 static const WCHAR function_endW[] = L"\n}";
1072 if(argc) {
1073 params = malloc(argc*sizeof(*params));
1074 if(!params)
1075 return E_OUTOFMEMORY;
1077 if(argc > 2)
1078 len = (argc-2)*2; /* separating commas */
1079 for(i=0; i < argc; i++) {
1080 hres = to_string(ctx, argv[i], params+i);
1081 if(FAILED(hres))
1082 break;
1083 len += jsstr_length(params[i]);
1087 if(SUCCEEDED(hres)) {
1088 len += ARRAY_SIZE(function_anonymousW) + ARRAY_SIZE(function_beginW) + ARRAY_SIZE(function_endW) - 2;
1089 str = malloc(len*sizeof(WCHAR));
1090 if(str) {
1091 memcpy(str, function_anonymousW, sizeof(function_anonymousW));
1092 ptr = str + ARRAY_SIZE(function_anonymousW) - 1;
1093 if(argc > 1) {
1094 while(1) {
1095 ptr += jsstr_flush(params[j], ptr);
1096 if(++j == argc-1)
1097 break;
1098 *ptr++ = ',';
1099 *ptr++ = ' ';
1102 memcpy(ptr, function_beginW, sizeof(function_beginW));
1103 ptr += ARRAY_SIZE(function_beginW) - 1;
1104 if(argc)
1105 ptr += jsstr_flush(params[argc-1], ptr);
1106 memcpy(ptr, function_endW, sizeof(function_endW));
1108 TRACE("%s\n", debugstr_w(str));
1109 }else {
1110 hres = E_OUTOFMEMORY;
1114 while(i)
1115 jsstr_release(params[--i]);
1116 free(params);
1117 if(FAILED(hres))
1118 return hres;
1120 hres = compile_script(ctx, str, 0, 0, NULL, NULL, FALSE, FALSE,
1121 ctx->call_ctx ? ctx->call_ctx->bytecode->named_item : NULL, &code);
1122 free(str);
1123 if(FAILED(hres))
1124 return hres;
1126 if(code->global_code.func_cnt != 1 || code->global_code.var_cnt != 1) {
1127 ERR("Invalid parser result!\n");
1128 release_bytecode(code);
1129 return E_UNEXPECTED;
1132 hres = create_source_function(ctx, code, code->global_code.funcs, NULL, &function);
1133 release_bytecode(code);
1134 if(FAILED(hres))
1135 return hres;
1137 *ret = to_disp(function);
1138 return S_OK;
1141 static HRESULT FunctionConstr_value(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
1142 jsval_t *r)
1144 HRESULT hres;
1146 TRACE("\n");
1148 switch(flags) {
1149 case DISPATCH_METHOD:
1150 case DISPATCH_CONSTRUCT: {
1151 IDispatch *ret;
1153 hres = construct_function(ctx, argc, argv, &ret);
1154 if(FAILED(hres))
1155 return hres;
1157 if(r) *r = jsval_disp(ret);
1158 else IDispatch_Release(ret);
1159 break;
1161 default:
1162 FIXME("unimplemented flags %x\n", flags);
1163 return E_NOTIMPL;
1166 return S_OK;
1169 static HRESULT FunctionProt_value(script_ctx_t *ctx, jsval_t vthis, WORD flags, unsigned argc, jsval_t *argv,
1170 jsval_t *r)
1172 FIXME("\n");
1173 return E_NOTIMPL;
1176 BOOL is_builtin_eval_func(jsdisp_t *jsdisp)
1178 return is_class(jsdisp, JSCLASS_FUNCTION) && function_from_jsdisp(jsdisp)->vtbl == &NativeFunctionVtbl &&
1179 ((NativeFunction*)function_from_jsdisp(jsdisp))->proc == JSGlobal_eval;
1182 HRESULT init_function_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
1184 NativeFunction *prot, *constr;
1185 HRESULT hres;
1187 hres = create_function(ctx, &Function_info, &NativeFunctionVtbl, sizeof(NativeFunction), PROPF_CONSTR,
1188 TRUE, object_prototype, (void**)&prot);
1189 if(FAILED(hres))
1190 return hres;
1192 prot->proc = FunctionProt_value;
1193 prot->name = L"prototype";
1195 hres = create_function(ctx, &FunctionInst_info, &NativeFunctionVtbl, sizeof(NativeFunction), PROPF_CONSTR|1,
1196 TRUE, &prot->function.dispex, (void**)&constr);
1197 if(SUCCEEDED(hres)) {
1198 constr->proc = FunctionConstr_value;
1199 constr->name = L"Function";
1200 hres = jsdisp_define_data_property(&constr->function.dispex, L"prototype", 0, jsval_obj(&prot->function.dispex));
1201 if(SUCCEEDED(hres))
1202 hres = set_constructor_prop(ctx, &constr->function.dispex, &prot->function.dispex);
1203 if(FAILED(hres))
1204 jsdisp_release(&constr->function.dispex);
1206 jsdisp_release(&prot->function.dispex);
1207 if(FAILED(hres))
1208 return hres;
1210 ctx->function_constr = &constr->function.dispex;
1211 return S_OK;