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
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
28 typedef struct _function_vtbl_t function_vtbl_t
;
32 const function_vtbl_t
*vtbl
;
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
*);
46 FunctionInstance function
;
47 scope_chain_t
*scope_chain
;
49 function_code_t
*func_code
;
50 } InterpretedFunction
;
53 FunctionInstance function
;
54 builtin_invoke_t proc
;
59 FunctionInstance function
;
60 FunctionInstance
*target
;
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
)
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
,
103 static void Arguments_destructor(jsdisp_t
*jsdisp
)
105 ArgumentsInstance
*arguments
= arguments_from_jsdisp(jsdisp
);
107 TRACE("(%p)\n", arguments
);
111 for(i
= 0; i
< arguments
->argc
; i
++)
112 jsval_release(arguments
->buf
[i
]);
113 free(arguments
->buf
);
117 scope_release(arguments
->scope
);
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
)
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
);
152 TRACE("%p[%u] = %s\n", arguments
, idx
, debugstr_jsval(val
));
154 hres
= jsval_copy(val
, ©
);
158 ref
= get_argument_ref(arguments
, idx
);
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
);
171 for(i
= 0; i
< arguments
->argc
; i
++) {
172 hres
= gc_process_linked_val(gc_ctx
, op
, jsdisp
, &arguments
->buf
[i
]);
178 if(arguments
->scope
) {
179 hres
= gc_process_linked_obj(gc_ctx
, op
, jsdisp
, &arguments
->scope
->dispex
, (void**)&arguments
->scope
);
187 static const builtin_info_t Arguments_info
= {
191 Arguments_destructor
,
193 Arguments_idx_length
,
196 Arguments_gc_traverse
199 HRESULT
setup_arguments_object(script_ctx_t
*ctx
, call_frame_t
*frame
)
201 ArgumentsInstance
*args
;
204 args
= calloc(1, sizeof(*args
));
206 return E_OUTOFMEMORY
;
208 hres
= init_dispex_from_constr(&args
->jsdisp
, ctx
, &Arguments_info
, ctx
->object_constr
);
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
));
220 hres
= jsdisp_define_data_property(&args
->jsdisp
, L
"callee", PROPF_WRITABLE
| PROPF_CONFIGURABLE
,
221 jsval_obj(frame
->function_instance
));
223 hres
= jsdisp_propput(as_jsdisp(frame
->base_scope
->obj
), L
"arguments", PROPF_WRITABLE
, TRUE
, jsval_obj(&args
->jsdisp
));
225 jsdisp_release(&args
->jsdisp
);
229 frame
->arguments_obj
= &args
->jsdisp
;
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
));
248 const jsval_t
*args
= on_stack
? arguments
->jsdisp
.ctx
->stack
+ frame
->arguments_off
: scope
->detached_vars
->var
;
251 for(i
= 0; i
< arguments
->argc
; i
++) {
252 if(FAILED(jsval_copy(args
[i
], &arguments
->buf
[i
])))
253 arguments
->buf
[i
] = jsval_undefined();
256 ERR("out of memory\n");
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");
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
);
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
)
295 *r
= jsval_obj(jsdisp_addref(frame
->prev_frame
->function_instance
));
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
);
312 static HRESULT
Function_toString(script_ctx_t
*ctx
, jsval_t vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
315 FunctionInstance
*function
;
321 if(!(function
= function_this(vthis
)))
322 return JS_E_FUNCTION_EXPECTED
;
324 hres
= function
->vtbl
->toString(function
, &str
);
329 *r
= jsval_string(str
);
335 static HRESULT
array_to_args(script_ctx_t
*ctx
, jsdisp_t
*arg_array
, unsigned *argc
, jsval_t
**ret
)
341 hres
= jsdisp_propget_name(arg_array
, L
"length", &val
);
345 hres
= to_uint32(ctx
, val
, &length
);
350 argv
= malloc(length
* sizeof(*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
)) {
360 jsval_release(argv
[i
]);
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
;
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
;
387 if(ctx
->version
< SCRIPTLANGUAGEVERSION_ES5
&& !is_undefined(argv
[0]) && !is_null(argv
[0])) {
389 hres
= to_object(ctx
, argv
[0], &this_obj
);
392 this_val
= jsval_disp(this_obj
);
394 hres
= jsval_copy(argv
[0], &this_val
);
401 jsdisp_t
*arg_array
= NULL
;
403 if(is_object_instance(argv
[1])) {
404 arg_array
= iface_to_jsdisp(get_object(argv
[1]));
406 (!is_class(arg_array
, JSCLASS_ARRAY
) && !is_class(arg_array
, JSCLASS_ARGUMENTS
) )) {
407 jsdisp_release(arg_array
);
413 hres
= array_to_args(ctx
, arg_array
, &cnt
, &args
);
414 jsdisp_release(arg_array
);
416 FIXME("throw TypeError\n");
421 if(SUCCEEDED(hres
)) {
423 hres
= function
->vtbl
->call(ctx
, function
, this_val
, flags
, cnt
, args
, r
);
426 hres
= disp_call_value(ctx
, get_object(vthis
), this_val
, DISPATCH_METHOD
, cnt
, args
, &res
);
427 if(SUCCEEDED(hres
)) {
436 jsval_release(this_val
);
437 for(i
=0; i
< cnt
; i
++)
438 jsval_release(args
[i
]);
443 static HRESULT
Function_call(script_ctx_t
*ctx
, jsval_t vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
446 jsval_t this_val
= jsval_undefined();
447 FunctionInstance
*function
;
453 if(is_null_disp(vthis
))
454 return JS_E_OBJECT_REQUIRED
;
455 if(!(function
= function_this(vthis
)))
456 return JS_E_FUNCTION_EXPECTED
;
459 if(ctx
->version
< SCRIPTLANGUAGEVERSION_ES5
&& !is_undefined(argv
[0]) && !is_null(argv
[0])) {
461 hres
= to_object(ctx
, argv
[0], &this_obj
);
464 this_val
= jsval_disp(this_obj
);
466 hres
= jsval_copy(argv
[0], &this_val
);
473 hres
= function
->vtbl
->call(ctx
, function
, this_val
, flags
, cnt
, argv
+ 1, r
);
475 jsval_release(this_val
);
479 static HRESULT
Function_bind(script_ctx_t
*ctx
, jsval_t vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
482 jsval_t bound_this
= jsval_undefined();
483 FunctionInstance
*function
;
484 jsdisp_t
*new_function
;
489 if(!(function
= function_this(vthis
)))
490 return JS_E_FUNCTION_EXPECTED
;
494 }else if(is_null(argv
[0])) {
495 bound_this
= argv
[0];
496 }else if(!is_undefined(argv
[0])) {
498 hres
= to_object(ctx
, argv
[0], &obj
);
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
);
510 *r
= jsval_obj(new_function
);
512 jsdisp_release(new_function
);
516 HRESULT
Function_value(script_ctx_t
*ctx
, jsval_t vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
519 FunctionInstance
*function
;
523 if(!(function
= function_this(vthis
))) {
524 ERR("dispex is not a function\n");
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
);
539 hres
= function
->vtbl
->toString(function
, &str
);
543 *r
= jsval_string(str
);
547 static HRESULT
Function_get_arguments(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
549 FunctionInstance
*function
= function_from_jsdisp(jsthis
);
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
);
562 *r
= jsval_obj(jsdisp_addref(frame
->arguments_obj
));
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
);
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
= {
607 ARRAY_SIZE(Function_props
),
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
= {
626 ARRAY_SIZE(FunctionInst_props
),
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
;
642 function
= calloc(1, size
);
644 return E_OUTOFMEMORY
;
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
);
651 hres
= init_dispex_from_constr(&function
->dispex
, ctx
, &FunctionInst_info
, ctx
->function_constr
);
657 function
->vtbl
= vtbl
;
658 function
->flags
= flags
;
659 function
->length
= flags
& PROPF_ARGMASK
;
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
;
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
);
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
));
694 memcpy(ptr
, native_suffixW
, sizeof(native_suffixW
));
700 static function_code_t
*NativeFunction_get_code(FunctionInstance
*function
)
705 static void NativeFunction_destructor(FunctionInstance
*function
)
709 static const function_vtbl_t NativeFunctionVtbl
= {
711 NativeFunction_toString
,
712 NativeFunction_get_code
,
713 NativeFunction_destructor
,
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
;
723 if(!ctx
->function_constr
)
726 hres
= create_function(ctx
, builtin_info
, &NativeFunctionVtbl
, sizeof(NativeFunction
), flags
, FALSE
, NULL
, (void**)&function
);
731 hres
= jsdisp_define_data_property(&function
->function
.dispex
, L
"length", 0,
732 jsval_number(function
->function
.length
));
734 hres
= jsdisp_define_data_property(&function
->function
.dispex
, L
"prototype", 0, prototype
? jsval_obj(prototype
) : jsval_null());
736 jsdisp_release(&function
->function
.dispex
);
740 function
->proc
= value_proc
;
741 function
->name
= name
;
743 *ret
= &function
->function
.dispex
;
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
,
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
)
759 hres
= create_builtin_function(ctx
, value_proc
, name
, builtin_info
, flags
, prototype
, &constr
);
763 hres
= set_constructor_prop(ctx
, constr
, prototype
);
765 jsdisp_release(constr
);
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
)
782 hres
= create_object(ctx
, NULL
, &prototype
);
786 hres
= jsdisp_define_data_property(jsthis
, L
"prototype", PROPF_WRITABLE
, jsval_obj(prototype
));
788 hres
= set_constructor_prop(ctx
, jsthis
, prototype
);
790 jsdisp_release(prototype
);
794 *r
= jsval_obj(prototype
);
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
= {
813 ARRAY_SIZE(InterpretedFunction_props
),
814 InterpretedFunction_props
,
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;
832 TRACE("%p\n", function
);
834 if(flags
& DISPATCH_CONSTRUCT
) {
835 hres
= create_object(ctx
, &function
->function
.dispex
, &new_obj
);
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
);
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
);
855 IDispatch_Release(this_obj
);
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
)
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
;
907 hres
= create_function(ctx
, &InterpretedFunction_info
, &InterpretedFunctionVtbl
, sizeof(InterpretedFunction
),
908 PROPF_CONSTR
, FALSE
, NULL
, (void**)&function
);
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
;
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
= {
945 ARRAY_SIZE(BindFunction_props
),
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
;
963 TRACE("%p\n", function
);
965 call_argc
= function
->argc
+ argc
;
967 call_args
= malloc(call_argc
* sizeof(*call_args
));
969 return E_OUTOFMEMORY
;
972 memcpy(call_args
, function
->args
, function
->argc
* sizeof(*call_args
));
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
);
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
)
994 static void BindFunction_destructor(FunctionInstance
*func
)
996 BindFunction
*function
= (BindFunction
*)func
;
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
;
1014 for(i
= 0; i
< function
->argc
; i
++) {
1015 hres
= gc_process_linked_val(gc_ctx
, op
, &function
->function
.dispex
, &function
->args
[i
]);
1020 hres
= gc_process_linked_obj(gc_ctx
, op
, &function
->function
.dispex
, &function
->target
->dispex
, (void**)&function
->target
);
1024 return gc_process_linked_val(gc_ctx
, op
, &function
->function
.dispex
, &function
->this);
1027 static const function_vtbl_t BindFunctionVtbl
= {
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
;
1041 hres
= create_function(ctx
, &BindFunction_info
, &BindFunctionVtbl
, FIELD_OFFSET(BindFunction
, args
[argc
]), PROPF_METHOD
,
1042 FALSE
, NULL
, (void**)&function
);
1046 jsdisp_addref(&target
->dispex
);
1047 function
->target
= target
;
1049 hres
= jsval_copy(bound_this
, &function
->this);
1051 jsdisp_release(&function
->function
.dispex
);
1055 for(function
->argc
= 0; function
->argc
< argc
; function
->argc
++) {
1056 hres
= jsval_copy(argv
[function
->argc
], function
->args
+ function
->argc
);
1058 jsdisp_release(&function
->function
.dispex
);
1063 function
->function
.length
= target
->length
> argc
? target
->length
- argc
: 0;
1065 *ret
= &function
->function
.dispex
;
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;
1075 jsstr_t
**params
= NULL
;
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}";
1084 params
= malloc(argc
*sizeof(*params
));
1086 return E_OUTOFMEMORY
;
1089 len
= (argc
-2)*2; /* separating commas */
1090 for(i
=0; i
< argc
; i
++) {
1091 hres
= to_string(ctx
, argv
[i
], params
+i
);
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
));
1102 memcpy(str
, function_anonymousW
, sizeof(function_anonymousW
));
1103 ptr
= str
+ ARRAY_SIZE(function_anonymousW
) - 1;
1106 ptr
+= jsstr_flush(params
[j
], ptr
);
1113 memcpy(ptr
, function_beginW
, sizeof(function_beginW
));
1114 ptr
+= ARRAY_SIZE(function_beginW
) - 1;
1116 ptr
+= jsstr_flush(params
[argc
-1], ptr
);
1117 memcpy(ptr
, function_endW
, sizeof(function_endW
));
1119 TRACE("%s\n", debugstr_w(str
));
1121 hres
= E_OUTOFMEMORY
;
1126 jsstr_release(params
[--i
]);
1131 hres
= compile_script(ctx
, str
, 0, 0, NULL
, NULL
, FALSE
, FALSE
,
1132 ctx
->call_ctx
? ctx
->call_ctx
->bytecode
->named_item
: NULL
, &code
);
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
);
1148 *ret
= to_disp(function
);
1152 static HRESULT
FunctionConstr_value(script_ctx_t
*ctx
, jsval_t vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1160 case DISPATCH_METHOD
:
1161 case DISPATCH_CONSTRUCT
: {
1164 hres
= construct_function(ctx
, argc
, argv
, &ret
);
1168 if(r
) *r
= jsval_disp(ret
);
1169 else IDispatch_Release(ret
);
1173 FIXME("unimplemented flags %x\n", flags
);
1180 static HRESULT
FunctionProt_value(script_ctx_t
*ctx
, jsval_t vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
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
;
1198 hres
= create_function(ctx
, &Function_info
, &NativeFunctionVtbl
, sizeof(NativeFunction
), PROPF_CONSTR
,
1199 TRUE
, object_prototype
, (void**)&prot
);
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
));
1213 hres
= set_constructor_prop(ctx
, &constr
->function
.dispex
, &prot
->function
.dispex
);
1215 jsdisp_release(&constr
->function
.dispex
);
1217 jsdisp_release(&prot
->function
.dispex
);
1221 ctx
->function_constr
= &constr
->function
.dispex
;