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
*,IDispatch
*,unsigned,unsigned,jsval_t
*,jsval_t
*);
39 HRESULT (*toString
)(FunctionInstance
*,jsstr_t
**);
40 function_code_t
* (*get_code
)(FunctionInstance
*);
41 void (*destructor
)(FunctionInstance
*);
45 FunctionInstance function
;
46 scope_chain_t
*scope_chain
;
48 function_code_t
*func_code
;
49 } InterpretedFunction
;
52 FunctionInstance function
;
53 builtin_invoke_t proc
;
58 FunctionInstance function
;
59 FunctionInstance
*target
;
67 InterpretedFunction
*function
;
73 static HRESULT
create_bind_function(script_ctx_t
*,FunctionInstance
*,IDispatch
*,unsigned,jsval_t
*,jsdisp_t
**r
);
75 static inline FunctionInstance
*function_from_jsdisp(jsdisp_t
*jsdisp
)
77 return CONTAINING_RECORD(jsdisp
, FunctionInstance
, dispex
);
80 static inline FunctionInstance
*function_from_vdisp(vdisp_t
*vdisp
)
82 return function_from_jsdisp(vdisp
->u
.jsdisp
);
85 static inline FunctionInstance
*function_this(vdisp_t
*jsthis
)
87 return is_vclass(jsthis
, JSCLASS_FUNCTION
) ? function_from_vdisp(jsthis
) : NULL
;
90 static inline ArgumentsInstance
*arguments_from_jsdisp(jsdisp_t
*jsdisp
)
92 return CONTAINING_RECORD(jsdisp
, ArgumentsInstance
, jsdisp
);
95 static HRESULT
Arguments_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
102 static void Arguments_destructor(jsdisp_t
*jsdisp
)
104 ArgumentsInstance
*arguments
= arguments_from_jsdisp(jsdisp
);
106 TRACE("(%p)\n", arguments
);
110 for(i
= 0; i
< arguments
->argc
; i
++)
111 jsval_release(arguments
->buf
[i
]);
112 heap_free(arguments
->buf
);
115 jsdisp_release(&arguments
->function
->function
.dispex
);
116 heap_free(arguments
);
119 static unsigned Arguments_idx_length(jsdisp_t
*jsdisp
)
121 ArgumentsInstance
*arguments
= arguments_from_jsdisp(jsdisp
);
122 return arguments
->argc
;
125 static jsval_t
*get_argument_ref(ArgumentsInstance
*arguments
, unsigned idx
)
128 return arguments
->buf
+ idx
;
129 if(arguments
->frame
->base_scope
->frame
|| idx
>= arguments
->frame
->function
->param_cnt
)
130 return arguments
->jsdisp
.ctx
->stack
+ arguments
->frame
->arguments_off
+ idx
;
134 static HRESULT
Arguments_idx_get(jsdisp_t
*jsdisp
, unsigned idx
, jsval_t
*r
)
136 ArgumentsInstance
*arguments
= arguments_from_jsdisp(jsdisp
);
139 TRACE("%p[%u]\n", arguments
, idx
);
141 if((ref
= get_argument_ref(arguments
, idx
)))
142 return jsval_copy(*ref
, r
);
144 /* FIXME: Accessing by name won't work for duplicated argument names */
145 return jsdisp_propget_name(arguments
->frame
->base_scope
->jsobj
,
146 arguments
->function
->func_code
->params
[idx
], r
);
149 static HRESULT
Arguments_idx_put(jsdisp_t
*jsdisp
, unsigned idx
, jsval_t val
)
151 ArgumentsInstance
*arguments
= arguments_from_jsdisp(jsdisp
);
155 TRACE("%p[%u] = %s\n", arguments
, idx
, debugstr_jsval(val
));
157 if((ref
= get_argument_ref(arguments
, idx
))) {
159 hres
= jsval_copy(val
, ©
);
168 /* FIXME: Accessing by name won't work for duplicated argument names */
169 return jsdisp_propput_name(arguments
->frame
->base_scope
->jsobj
,
170 arguments
->function
->func_code
->params
[idx
], val
);
173 static const builtin_info_t Arguments_info
= {
177 Arguments_destructor
,
179 Arguments_idx_length
,
184 HRESULT
setup_arguments_object(script_ctx_t
*ctx
, call_frame_t
*frame
)
186 ArgumentsInstance
*args
;
189 args
= heap_alloc_zero(sizeof(*args
));
191 return E_OUTOFMEMORY
;
193 hres
= init_dispex_from_constr(&args
->jsdisp
, ctx
, &Arguments_info
, ctx
->object_constr
);
199 args
->function
= (InterpretedFunction
*)function_from_jsdisp(jsdisp_addref(frame
->function_instance
));
200 args
->argc
= frame
->argc
;
203 hres
= jsdisp_define_data_property(&args
->jsdisp
, L
"length", PROPF_WRITABLE
| PROPF_CONFIGURABLE
,
204 jsval_number(args
->argc
));
206 hres
= jsdisp_define_data_property(&args
->jsdisp
, L
"callee", PROPF_WRITABLE
| PROPF_CONFIGURABLE
,
207 jsval_obj(&args
->function
->function
.dispex
));
209 hres
= jsdisp_propput(frame
->base_scope
->jsobj
, L
"arguments", PROPF_WRITABLE
, TRUE
, jsval_obj(&args
->jsdisp
));
211 jsdisp_release(&args
->jsdisp
);
215 frame
->arguments_obj
= &args
->jsdisp
;
219 void detach_arguments_object(jsdisp_t
*args_disp
)
221 ArgumentsInstance
*arguments
= arguments_from_jsdisp(args_disp
);
222 call_frame_t
*frame
= arguments
->frame
;
223 const BOOL on_stack
= frame
->base_scope
->frame
== frame
;
226 /* Reset arguments value to cut the reference cycle. Note that since all activation contexts have
227 * their own arguments property, it's impossible to use prototype's one during name lookup */
228 jsdisp_propput_name(frame
->base_scope
->jsobj
, L
"arguments", jsval_undefined());
229 arguments
->frame
= NULL
;
231 /* Don't bother coppying arguments if call frame holds the last reference. */
232 if(arguments
->jsdisp
.ref
> 1) {
233 arguments
->buf
= heap_alloc(arguments
->argc
* sizeof(*arguments
->buf
));
237 for(i
= 0; i
< arguments
->argc
; i
++) {
238 if(on_stack
|| i
>= frame
->function
->param_cnt
)
239 hres
= jsval_copy(arguments
->jsdisp
.ctx
->stack
[frame
->arguments_off
+ i
], arguments
->buf
+i
);
241 hres
= jsdisp_propget_name(frame
->base_scope
->jsobj
, frame
->function
->params
[i
], arguments
->buf
+i
);
243 arguments
->buf
[i
] = jsval_undefined();
246 ERR("out of memory\n");
251 jsdisp_release(frame
->arguments_obj
);
254 HRESULT
Function_invoke(jsdisp_t
*func_this
, IDispatch
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
256 FunctionInstance
*function
;
258 TRACE("func %p this %p\n", func_this
, jsthis
);
260 assert(is_class(func_this
, JSCLASS_FUNCTION
));
261 function
= function_from_jsdisp(func_this
);
263 return function
->vtbl
->call(function
->dispex
.ctx
, function
, jsthis
, flags
, argc
, argv
, r
);
266 static HRESULT
Function_get_length(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
268 TRACE("%p\n", jsthis
);
270 *r
= jsval_number(function_from_jsdisp(jsthis
)->length
);
274 static HRESULT
Function_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
277 FunctionInstance
*function
;
283 if(!(function
= function_this(jsthis
)))
284 return JS_E_FUNCTION_EXPECTED
;
286 hres
= function
->vtbl
->toString(function
, &str
);
291 *r
= jsval_string(str
);
297 static HRESULT
array_to_args(script_ctx_t
*ctx
, jsdisp_t
*arg_array
, unsigned *argc
, jsval_t
**ret
)
303 hres
= jsdisp_propget_name(arg_array
, L
"length", &val
);
307 hres
= to_uint32(ctx
, val
, &length
);
312 argv
= heap_alloc(length
* sizeof(*argv
));
314 return E_OUTOFMEMORY
;
316 for(i
=0; i
<length
; i
++) {
317 hres
= jsdisp_get_idx(arg_array
, i
, argv
+i
);
318 if(hres
== DISP_E_UNKNOWNNAME
) {
319 argv
[i
] = jsval_undefined();
320 }else if(FAILED(hres
)) {
322 jsval_release(argv
[i
]);
333 static HRESULT
Function_apply(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
335 FunctionInstance
*function
;
336 jsval_t
*args
= NULL
;
338 IDispatch
*this_obj
= NULL
;
343 if(!(function
= function_this(jsthis
)) && (jsthis
->flags
& VDISP_JSDISP
))
344 return JS_E_FUNCTION_EXPECTED
;
347 if(!is_undefined(argv
[0]) && !is_null(argv
[0])) {
348 hres
= to_object(ctx
, argv
[0], &this_obj
);
355 jsdisp_t
*arg_array
= NULL
;
357 if(is_object_instance(argv
[1])) {
358 arg_array
= iface_to_jsdisp(get_object(argv
[1]));
360 (!is_class(arg_array
, JSCLASS_ARRAY
) && !is_class(arg_array
, JSCLASS_ARGUMENTS
) )) {
361 jsdisp_release(arg_array
);
367 hres
= array_to_args(ctx
, arg_array
, &cnt
, &args
);
368 jsdisp_release(arg_array
);
370 FIXME("throw TypeError\n");
375 if(SUCCEEDED(hres
)) {
377 hres
= function
->vtbl
->call(ctx
, function
, this_obj
, flags
, cnt
, args
, r
);
380 hres
= disp_call_value(ctx
, jsthis
->u
.disp
, this_obj
, DISPATCH_METHOD
, cnt
, args
, &res
);
381 if(SUCCEEDED(hres
)) {
391 IDispatch_Release(this_obj
);
392 for(i
=0; i
< cnt
; i
++)
393 jsval_release(args
[i
]);
398 static HRESULT
Function_call(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
401 FunctionInstance
*function
;
402 IDispatch
*this_obj
= NULL
;
408 if(!(function
= function_this(jsthis
)))
409 return JS_E_FUNCTION_EXPECTED
;
412 if(!is_undefined(argv
[0]) && !is_null(argv
[0])) {
413 hres
= to_object(ctx
, argv
[0], &this_obj
);
421 hres
= function
->vtbl
->call(ctx
, function
, this_obj
, flags
, cnt
, argv
+ 1, r
);
424 IDispatch_Release(this_obj
);
428 static HRESULT
Function_bind(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
431 IDispatch
*bound_this
= NULL
;
432 FunctionInstance
*function
;
433 jsdisp_t
*new_function
;
438 if(!(function
= function_this(jsthis
)))
439 return JS_E_FUNCTION_EXPECTED
;
442 FIXME("no this argument\n");
446 if(is_object_instance(argv
[0])) {
447 bound_this
= get_object(argv
[0]);
448 }else if(!is_null(argv
[0])) {
449 FIXME("%s is not an object instance\n", debugstr_jsval(argv
[0]));
453 hres
= create_bind_function(ctx
, function
, bound_this
, argc
- 1, argv
+ 1, &new_function
);
458 *r
= jsval_obj(new_function
);
460 jsdisp_release(new_function
);
464 HRESULT
Function_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
467 FunctionInstance
*function
;
471 if(!is_vclass(jsthis
, JSCLASS_FUNCTION
)) {
472 ERR("dispex is not a function\n");
476 function
= function_from_jsdisp(jsthis
->u
.jsdisp
);
477 return function
->vtbl
->call(ctx
, function
, NULL
, flags
, argc
, argv
, r
);
480 HRESULT
Function_get_value(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
482 FunctionInstance
*function
= function_from_jsdisp(jsthis
);
488 hres
= function
->vtbl
->toString(function
, &str
);
492 *r
= jsval_string(str
);
496 static HRESULT
Function_get_arguments(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
498 FunctionInstance
*function
= function_from_jsdisp(jsthis
);
504 for(frame
= ctx
->call_ctx
; frame
; frame
= frame
->prev_frame
) {
505 if(frame
->function_instance
== &function
->dispex
) {
506 if(!frame
->arguments_obj
) {
507 hres
= setup_arguments_object(ctx
, frame
);
511 *r
= jsval_obj(jsdisp_addref(frame
->arguments_obj
));
520 function_code_t
*Function_get_code(jsdisp_t
*jsthis
)
522 FunctionInstance
*function
;
524 assert(is_class(jsthis
, JSCLASS_FUNCTION
));
525 function
= function_from_jsdisp(jsthis
);
527 return function
->vtbl
->get_code(function
);
530 static void Function_destructor(jsdisp_t
*dispex
)
532 FunctionInstance
*function
= function_from_jsdisp(dispex
);
533 function
->vtbl
->destructor(function
);
537 static const builtin_prop_t Function_props
[] = {
538 {L
"apply", Function_apply
, PROPF_METHOD
|2},
539 {L
"arguments", NULL
, 0, Function_get_arguments
},
540 {L
"bind", Function_bind
, PROPF_METHOD
|PROPF_ES5
|1},
541 {L
"call", Function_call
, PROPF_METHOD
|1},
542 {L
"length", NULL
, 0, Function_get_length
},
543 {L
"toString", Function_toString
, PROPF_METHOD
}
546 static const builtin_info_t Function_info
= {
549 ARRAY_SIZE(Function_props
),
555 static const builtin_prop_t FunctionInst_props
[] = {
556 {L
"arguments", NULL
, 0, Function_get_arguments
},
557 {L
"length", NULL
, 0, Function_get_length
}
560 static const builtin_info_t FunctionInst_info
= {
563 ARRAY_SIZE(FunctionInst_props
),
569 static HRESULT
create_function(script_ctx_t
*ctx
, const builtin_info_t
*builtin_info
, const function_vtbl_t
*vtbl
, size_t size
,
570 DWORD flags
, BOOL funcprot
, jsdisp_t
*prototype
, void **ret
)
572 FunctionInstance
*function
;
575 function
= heap_alloc_zero(size
);
577 return E_OUTOFMEMORY
;
580 hres
= init_dispex(&function
->dispex
, ctx
, builtin_info
, prototype
);
581 else if(builtin_info
)
582 hres
= init_dispex_from_constr(&function
->dispex
, ctx
, builtin_info
, ctx
->function_constr
);
584 hres
= init_dispex_from_constr(&function
->dispex
, ctx
, &FunctionInst_info
, ctx
->function_constr
);
590 function
->vtbl
= vtbl
;
591 function
->flags
= flags
;
592 function
->length
= flags
& PROPF_ARGMASK
;
598 static HRESULT
NativeFunction_call(script_ctx_t
*ctx
, FunctionInstance
*func
, IDispatch
*this_disp
, unsigned flags
,
599 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
601 NativeFunction
*function
= (NativeFunction
*)func
;
606 set_disp(&vthis
, this_disp
);
608 set_disp(&vthis
, lookup_global_host(ctx
));
610 hres
= function
->proc(ctx
, &vthis
, flags
& ~DISPATCH_JSCRIPT_INTERNAL_MASK
, argc
, argv
, r
);
612 vdisp_release(&vthis
);
616 static HRESULT
NativeFunction_toString(FunctionInstance
*func
, jsstr_t
**ret
)
618 NativeFunction
*function
= (NativeFunction
*)func
;
623 static const WCHAR native_prefixW
[] = L
"\nfunction ";
624 static const WCHAR native_suffixW
[] = L
"() {\n [native code]\n}\n";
626 name_len
= function
->name
? lstrlenW(function
->name
) : 0;
627 str
= jsstr_alloc_buf(ARRAY_SIZE(native_prefixW
) + ARRAY_SIZE(native_suffixW
) + name_len
- 2, &ptr
);
629 return E_OUTOFMEMORY
;
631 memcpy(ptr
, native_prefixW
, sizeof(native_prefixW
));
632 ptr
+= ARRAY_SIZE(native_prefixW
) - 1;
633 memcpy(ptr
, function
->name
, name_len
*sizeof(WCHAR
));
635 memcpy(ptr
, native_suffixW
, sizeof(native_suffixW
));
641 static function_code_t
*NativeFunction_get_code(FunctionInstance
*function
)
646 static void NativeFunction_destructor(FunctionInstance
*function
)
650 static const function_vtbl_t NativeFunctionVtbl
= {
652 NativeFunction_toString
,
653 NativeFunction_get_code
,
654 NativeFunction_destructor
657 HRESULT
create_builtin_function(script_ctx_t
*ctx
, builtin_invoke_t value_proc
, const WCHAR
*name
,
658 const builtin_info_t
*builtin_info
, DWORD flags
, jsdisp_t
*prototype
, jsdisp_t
**ret
)
660 NativeFunction
*function
;
663 hres
= create_function(ctx
, builtin_info
, &NativeFunctionVtbl
, sizeof(NativeFunction
), flags
, FALSE
, NULL
, (void**)&function
);
668 hres
= jsdisp_define_data_property(&function
->function
.dispex
, L
"length", 0,
669 jsval_number(function
->function
.length
));
671 hres
= jsdisp_define_data_property(&function
->function
.dispex
, L
"prototype", 0, jsval_obj(prototype
));
673 jsdisp_release(&function
->function
.dispex
);
677 function
->proc
= value_proc
;
678 function
->name
= name
;
680 *ret
= &function
->function
.dispex
;
684 static HRESULT
set_constructor_prop(script_ctx_t
*ctx
, jsdisp_t
*constr
, jsdisp_t
*prot
)
686 return jsdisp_define_data_property(prot
, L
"constructor", PROPF_WRITABLE
| PROPF_CONFIGURABLE
,
690 HRESULT
create_builtin_constructor(script_ctx_t
*ctx
, builtin_invoke_t value_proc
, const WCHAR
*name
,
691 const builtin_info_t
*builtin_info
, DWORD flags
, jsdisp_t
*prototype
, jsdisp_t
**ret
)
696 hres
= create_builtin_function(ctx
, value_proc
, name
, builtin_info
, flags
, prototype
, &constr
);
700 hres
= set_constructor_prop(ctx
, constr
, prototype
);
702 jsdisp_release(constr
);
710 static HRESULT
InterpretedFunction_call(script_ctx_t
*ctx
, FunctionInstance
*func
, IDispatch
*this_obj
, unsigned flags
,
711 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
713 InterpretedFunction
*function
= (InterpretedFunction
*)func
;
714 jsdisp_t
*new_obj
= NULL
;
715 DWORD exec_flags
= 0;
718 TRACE("%p\n", function
);
720 if(ctx
->state
== SCRIPTSTATE_UNINITIALIZED
|| ctx
->state
== SCRIPTSTATE_CLOSED
) {
721 WARN("Script engine state does not allow running code.\n");
725 if(flags
& DISPATCH_CONSTRUCT
) {
726 hres
= create_object(ctx
, &function
->function
.dispex
, &new_obj
);
729 this_obj
= to_disp(new_obj
);
732 if(flags
& DISPATCH_JSCRIPT_CALLEREXECSSOURCE
)
733 exec_flags
|= EXEC_RETURN_TO_INTERP
;
734 if(flags
& DISPATCH_CONSTRUCT
)
735 exec_flags
|= EXEC_CONSTRUCTOR
;
736 hres
= exec_source(ctx
, exec_flags
, function
->code
, function
->func_code
, function
->scope_chain
, this_obj
,
737 &function
->function
.dispex
, argc
, argv
, r
);
739 jsdisp_release(new_obj
);
743 static HRESULT
InterpretedFunction_toString(FunctionInstance
*func
, jsstr_t
**ret
)
745 InterpretedFunction
*function
= (InterpretedFunction
*)func
;
747 *ret
= jsstr_alloc_len(function
->func_code
->source
, function
->func_code
->source_len
);
748 return *ret
? S_OK
: E_OUTOFMEMORY
;
751 static function_code_t
*InterpretedFunction_get_code(FunctionInstance
*func
)
753 InterpretedFunction
*function
= (InterpretedFunction
*)func
;
755 return function
->func_code
;
758 static void InterpretedFunction_destructor(FunctionInstance
*func
)
760 InterpretedFunction
*function
= (InterpretedFunction
*)func
;
762 release_bytecode(function
->code
);
763 if(function
->scope_chain
)
764 scope_release(function
->scope_chain
);
767 static const function_vtbl_t InterpretedFunctionVtbl
= {
768 InterpretedFunction_call
,
769 InterpretedFunction_toString
,
770 InterpretedFunction_get_code
,
771 InterpretedFunction_destructor
774 HRESULT
create_source_function(script_ctx_t
*ctx
, bytecode_t
*code
, function_code_t
*func_code
,
775 scope_chain_t
*scope_chain
, jsdisp_t
**ret
)
777 InterpretedFunction
*function
;
781 hres
= create_object(ctx
, NULL
, &prototype
);
785 hres
= create_function(ctx
, NULL
, &InterpretedFunctionVtbl
, sizeof(InterpretedFunction
), PROPF_CONSTR
,
786 FALSE
, NULL
, (void**)&function
);
787 if(SUCCEEDED(hres
)) {
788 hres
= jsdisp_define_data_property(&function
->function
.dispex
, L
"prototype", PROPF_WRITABLE
,
789 jsval_obj(prototype
));
791 hres
= set_constructor_prop(ctx
, &function
->function
.dispex
, prototype
);
793 jsdisp_release(&function
->function
.dispex
);
795 jsdisp_release(prototype
);
800 scope_addref(scope_chain
);
801 function
->scope_chain
= scope_chain
;
804 bytecode_addref(code
);
805 function
->code
= code
;
806 function
->func_code
= func_code
;
807 function
->function
.length
= function
->func_code
->param_cnt
;
809 *ret
= &function
->function
.dispex
;
813 static HRESULT
BindFunction_call(script_ctx_t
*ctx
, FunctionInstance
*func
, IDispatch
*this_obj
, unsigned flags
,
814 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
816 BindFunction
*function
= (BindFunction
*)func
;
817 jsval_t
*call_args
= NULL
;
821 TRACE("%p\n", function
);
823 call_argc
= function
->argc
+ argc
;
825 call_args
= heap_alloc(call_argc
* sizeof(*call_args
));
827 return E_OUTOFMEMORY
;
830 memcpy(call_args
, function
->args
, function
->argc
* sizeof(*call_args
));
832 memcpy(call_args
+ function
->argc
, argv
, argc
* sizeof(*call_args
));
835 hres
= function
->target
->vtbl
->call(ctx
, function
->target
, function
->this, flags
, call_argc
, call_args
, r
);
837 heap_free(call_args
);
841 static HRESULT
BindFunction_toString(FunctionInstance
*function
, jsstr_t
**ret
)
843 *ret
= jsstr_alloc(L
"\nfunction() {\n [native code]\n}\n");
844 return *ret
? S_OK
: E_OUTOFMEMORY
;
847 static function_code_t
*BindFunction_get_code(FunctionInstance
*function
)
852 static void BindFunction_destructor(FunctionInstance
*func
)
854 BindFunction
*function
= (BindFunction
*)func
;
857 TRACE("%p\n", function
);
859 for(i
= 0; i
< function
->argc
; i
++)
860 jsval_release(function
->args
[i
]);
861 jsdisp_release(&function
->target
->dispex
);
863 IDispatch_Release(function
->this);
866 static const function_vtbl_t BindFunctionVtbl
= {
868 BindFunction_toString
,
869 BindFunction_get_code
,
870 BindFunction_destructor
873 static HRESULT
create_bind_function(script_ctx_t
*ctx
, FunctionInstance
*target
, IDispatch
*bound_this
, unsigned argc
,
874 jsval_t
*argv
, jsdisp_t
**ret
)
876 BindFunction
*function
;
879 hres
= create_function(ctx
, NULL
, &BindFunctionVtbl
, FIELD_OFFSET(BindFunction
, args
[argc
]), PROPF_METHOD
,
880 FALSE
, NULL
, (void**)&function
);
884 jsdisp_addref(&target
->dispex
);
885 function
->target
= target
;
888 IDispatch_AddRef(function
->this = bound_this
);
890 for(function
->argc
= 0; function
->argc
< argc
; function
->argc
++) {
891 hres
= jsval_copy(argv
[function
->argc
], function
->args
+ function
->argc
);
893 jsdisp_release(&function
->function
.dispex
);
898 function
->function
.length
= target
->length
> argc
? target
->length
- argc
: 0;
900 *ret
= &function
->function
.dispex
;
904 static HRESULT
construct_function(script_ctx_t
*ctx
, unsigned argc
, jsval_t
*argv
, IDispatch
**ret
)
906 WCHAR
*str
= NULL
, *ptr
;
907 unsigned len
= 0, i
= 0;
910 jsstr_t
**params
= NULL
;
914 static const WCHAR function_anonymousW
[] = L
"function anonymous(";
915 static const WCHAR function_beginW
[] = L
") {\n";
916 static const WCHAR function_endW
[] = L
"\n}";
919 params
= heap_alloc(argc
*sizeof(*params
));
921 return E_OUTOFMEMORY
;
924 len
= (argc
-2)*2; /* separating commas */
925 for(i
=0; i
< argc
; i
++) {
926 hres
= to_string(ctx
, argv
[i
], params
+i
);
929 len
+= jsstr_length(params
[i
]);
933 if(SUCCEEDED(hres
)) {
934 len
+= ARRAY_SIZE(function_anonymousW
) + ARRAY_SIZE(function_beginW
) + ARRAY_SIZE(function_endW
) - 2;
935 str
= heap_alloc(len
*sizeof(WCHAR
));
937 memcpy(str
, function_anonymousW
, sizeof(function_anonymousW
));
938 ptr
= str
+ ARRAY_SIZE(function_anonymousW
) - 1;
941 ptr
+= jsstr_flush(params
[j
], ptr
);
948 memcpy(ptr
, function_beginW
, sizeof(function_beginW
));
949 ptr
+= ARRAY_SIZE(function_beginW
) - 1;
951 ptr
+= jsstr_flush(params
[argc
-1], ptr
);
952 memcpy(ptr
, function_endW
, sizeof(function_endW
));
954 TRACE("%s\n", debugstr_w(str
));
956 hres
= E_OUTOFMEMORY
;
961 jsstr_release(params
[--i
]);
966 hres
= compile_script(ctx
, str
, 0, 0, NULL
, NULL
, FALSE
, FALSE
,
967 ctx
->call_ctx
? ctx
->call_ctx
->bytecode
->named_item
: NULL
, &code
);
972 if(code
->global_code
.func_cnt
!= 1 || code
->global_code
.var_cnt
!= 1) {
973 ERR("Invalid parser result!\n");
974 release_bytecode(code
);
978 hres
= create_source_function(ctx
, code
, code
->global_code
.funcs
, NULL
, &function
);
979 release_bytecode(code
);
983 *ret
= to_disp(function
);
987 static HRESULT
FunctionConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
995 case DISPATCH_METHOD
:
996 case DISPATCH_CONSTRUCT
: {
999 hres
= construct_function(ctx
, argc
, argv
, &ret
);
1003 *r
= jsval_disp(ret
);
1007 FIXME("unimplemented flags %x\n", flags
);
1014 static HRESULT
FunctionProt_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1021 HRESULT
init_function_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
)
1023 NativeFunction
*prot
, *constr
;
1026 hres
= create_function(ctx
, &Function_info
, &NativeFunctionVtbl
, sizeof(NativeFunction
), PROPF_CONSTR
,
1027 TRUE
, object_prototype
, (void**)&prot
);
1031 prot
->proc
= FunctionProt_value
;
1032 prot
->name
= L
"prototype";
1034 hres
= create_function(ctx
, &FunctionInst_info
, &NativeFunctionVtbl
, sizeof(NativeFunction
), PROPF_CONSTR
|1,
1035 TRUE
, &prot
->function
.dispex
, (void**)&constr
);
1036 if(SUCCEEDED(hres
)) {
1037 constr
->proc
= FunctionConstr_value
;
1038 constr
->name
= L
"Function";
1039 hres
= jsdisp_define_data_property(&constr
->function
.dispex
, L
"prototype", 0, jsval_obj(&prot
->function
.dispex
));
1041 hres
= set_constructor_prop(ctx
, &constr
->function
.dispex
, &prot
->function
.dispex
);
1043 jsdisp_release(&constr
->function
.dispex
);
1045 jsdisp_release(&prot
->function
.dispex
);
1049 ctx
->function_constr
= &constr
->function
.dispex
;