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
= {
175 {NULL
, Arguments_value
, 0},
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
, 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
= {
548 DEFAULT_FUNCTION_VALUE
,
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
= {
562 DEFAULT_FUNCTION_VALUE
,
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
[] = {'\n','f','u','n','c','t','i','o','n',' '};
624 static const WCHAR native_suffixW
[] =
625 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
627 name_len
= function
->name
? lstrlenW(function
->name
) : 0;
628 str
= jsstr_alloc_buf(ARRAY_SIZE(native_prefixW
) + ARRAY_SIZE(native_suffixW
) + name_len
, &ptr
);
630 return E_OUTOFMEMORY
;
632 memcpy(ptr
, native_prefixW
, sizeof(native_prefixW
));
633 ptr
+= ARRAY_SIZE(native_prefixW
);
634 memcpy(ptr
, function
->name
, name_len
*sizeof(WCHAR
));
636 memcpy(ptr
, native_suffixW
, sizeof(native_suffixW
));
642 static function_code_t
*NativeFunction_get_code(FunctionInstance
*function
)
647 static void NativeFunction_destructor(FunctionInstance
*function
)
651 static const function_vtbl_t NativeFunctionVtbl
= {
653 NativeFunction_toString
,
654 NativeFunction_get_code
,
655 NativeFunction_destructor
658 HRESULT
create_builtin_function(script_ctx_t
*ctx
, builtin_invoke_t value_proc
, const WCHAR
*name
,
659 const builtin_info_t
*builtin_info
, DWORD flags
, jsdisp_t
*prototype
, jsdisp_t
**ret
)
661 NativeFunction
*function
;
664 hres
= create_function(ctx
, builtin_info
, &NativeFunctionVtbl
, sizeof(NativeFunction
), flags
, FALSE
, NULL
, (void**)&function
);
669 hres
= jsdisp_define_data_property(&function
->function
.dispex
, L
"length", 0,
670 jsval_number(function
->function
.length
));
672 hres
= jsdisp_define_data_property(&function
->function
.dispex
, L
"prototype", 0, jsval_obj(prototype
));
674 jsdisp_release(&function
->function
.dispex
);
678 function
->proc
= value_proc
;
679 function
->name
= name
;
681 *ret
= &function
->function
.dispex
;
685 static HRESULT
set_constructor_prop(script_ctx_t
*ctx
, jsdisp_t
*constr
, jsdisp_t
*prot
)
687 return jsdisp_define_data_property(prot
, L
"constructor", PROPF_WRITABLE
| PROPF_CONFIGURABLE
,
691 HRESULT
create_builtin_constructor(script_ctx_t
*ctx
, builtin_invoke_t value_proc
, const WCHAR
*name
,
692 const builtin_info_t
*builtin_info
, DWORD flags
, jsdisp_t
*prototype
, jsdisp_t
**ret
)
697 hres
= create_builtin_function(ctx
, value_proc
, name
, builtin_info
, flags
, prototype
, &constr
);
701 hres
= set_constructor_prop(ctx
, constr
, prototype
);
703 jsdisp_release(constr
);
711 static HRESULT
InterpretedFunction_call(script_ctx_t
*ctx
, FunctionInstance
*func
, IDispatch
*this_obj
, unsigned flags
,
712 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
714 InterpretedFunction
*function
= (InterpretedFunction
*)func
;
715 jsdisp_t
*new_obj
= NULL
;
716 DWORD exec_flags
= 0;
719 TRACE("%p\n", function
);
721 if(ctx
->state
== SCRIPTSTATE_UNINITIALIZED
|| ctx
->state
== SCRIPTSTATE_CLOSED
) {
722 WARN("Script engine state does not allow running code.\n");
726 if(flags
& DISPATCH_CONSTRUCT
) {
727 hres
= create_object(ctx
, &function
->function
.dispex
, &new_obj
);
730 this_obj
= to_disp(new_obj
);
733 if(flags
& DISPATCH_JSCRIPT_CALLEREXECSSOURCE
)
734 exec_flags
|= EXEC_RETURN_TO_INTERP
;
735 if(flags
& DISPATCH_CONSTRUCT
)
736 exec_flags
|= EXEC_CONSTRUCTOR
;
737 hres
= exec_source(ctx
, exec_flags
, function
->code
, function
->func_code
, function
->scope_chain
, this_obj
,
738 &function
->function
.dispex
, argc
, argv
, r
);
740 jsdisp_release(new_obj
);
744 static HRESULT
InterpretedFunction_toString(FunctionInstance
*func
, jsstr_t
**ret
)
746 InterpretedFunction
*function
= (InterpretedFunction
*)func
;
748 *ret
= jsstr_alloc_len(function
->func_code
->source
, function
->func_code
->source_len
);
749 return *ret
? S_OK
: E_OUTOFMEMORY
;
752 static function_code_t
*InterpretedFunction_get_code(FunctionInstance
*func
)
754 InterpretedFunction
*function
= (InterpretedFunction
*)func
;
756 return function
->func_code
;
759 static void InterpretedFunction_destructor(FunctionInstance
*func
)
761 InterpretedFunction
*function
= (InterpretedFunction
*)func
;
763 release_bytecode(function
->code
);
764 if(function
->scope_chain
)
765 scope_release(function
->scope_chain
);
768 static const function_vtbl_t InterpretedFunctionVtbl
= {
769 InterpretedFunction_call
,
770 InterpretedFunction_toString
,
771 InterpretedFunction_get_code
,
772 InterpretedFunction_destructor
775 HRESULT
create_source_function(script_ctx_t
*ctx
, bytecode_t
*code
, function_code_t
*func_code
,
776 scope_chain_t
*scope_chain
, jsdisp_t
**ret
)
778 InterpretedFunction
*function
;
782 hres
= create_object(ctx
, NULL
, &prototype
);
786 hres
= create_function(ctx
, NULL
, &InterpretedFunctionVtbl
, sizeof(InterpretedFunction
), PROPF_CONSTR
,
787 FALSE
, NULL
, (void**)&function
);
788 if(SUCCEEDED(hres
)) {
789 hres
= jsdisp_define_data_property(&function
->function
.dispex
, L
"prototype", PROPF_WRITABLE
,
790 jsval_obj(prototype
));
792 hres
= set_constructor_prop(ctx
, &function
->function
.dispex
, prototype
);
794 jsdisp_release(&function
->function
.dispex
);
796 jsdisp_release(prototype
);
801 scope_addref(scope_chain
);
802 function
->scope_chain
= scope_chain
;
805 bytecode_addref(code
);
806 function
->code
= code
;
807 function
->func_code
= func_code
;
808 function
->function
.length
= function
->func_code
->param_cnt
;
810 *ret
= &function
->function
.dispex
;
814 static HRESULT
BindFunction_call(script_ctx_t
*ctx
, FunctionInstance
*func
, IDispatch
*this_obj
, unsigned flags
,
815 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
817 BindFunction
*function
= (BindFunction
*)func
;
818 jsval_t
*call_args
= NULL
;
822 TRACE("%p\n", function
);
824 call_argc
= function
->argc
+ argc
;
826 call_args
= heap_alloc(call_argc
* sizeof(*call_args
));
828 return E_OUTOFMEMORY
;
831 memcpy(call_args
, function
->args
, function
->argc
* sizeof(*call_args
));
833 memcpy(call_args
+ function
->argc
, argv
, argc
* sizeof(*call_args
));
836 hres
= function
->target
->vtbl
->call(ctx
, function
->target
, function
->this, flags
, call_argc
, call_args
, r
);
838 heap_free(call_args
);
842 static HRESULT
BindFunction_toString(FunctionInstance
*function
, jsstr_t
**ret
)
844 *ret
= jsstr_alloc(L
"\nfunction() {\n [native code]\n}\n");
845 return *ret
? S_OK
: E_OUTOFMEMORY
;
848 static function_code_t
*BindFunction_get_code(FunctionInstance
*function
)
853 static void BindFunction_destructor(FunctionInstance
*func
)
855 BindFunction
*function
= (BindFunction
*)func
;
858 TRACE("%p\n", function
);
860 for(i
= 0; i
< function
->argc
; i
++)
861 jsval_release(function
->args
[i
]);
862 jsdisp_release(&function
->target
->dispex
);
864 IDispatch_Release(function
->this);
867 static const function_vtbl_t BindFunctionVtbl
= {
869 BindFunction_toString
,
870 BindFunction_get_code
,
871 BindFunction_destructor
874 static HRESULT
create_bind_function(script_ctx_t
*ctx
, FunctionInstance
*target
, IDispatch
*bound_this
, unsigned argc
,
875 jsval_t
*argv
, jsdisp_t
**ret
)
877 BindFunction
*function
;
880 hres
= create_function(ctx
, NULL
, &BindFunctionVtbl
, FIELD_OFFSET(BindFunction
, args
[argc
]), PROPF_METHOD
,
881 FALSE
, NULL
, (void**)&function
);
885 jsdisp_addref(&target
->dispex
);
886 function
->target
= target
;
889 IDispatch_AddRef(function
->this = bound_this
);
891 for(function
->argc
= 0; function
->argc
< argc
; function
->argc
++) {
892 hres
= jsval_copy(argv
[function
->argc
], function
->args
+ function
->argc
);
894 jsdisp_release(&function
->function
.dispex
);
899 function
->function
.length
= target
->length
> argc
? target
->length
- argc
: 0;
901 *ret
= &function
->function
.dispex
;
905 static HRESULT
construct_function(script_ctx_t
*ctx
, unsigned argc
, jsval_t
*argv
, IDispatch
**ret
)
907 WCHAR
*str
= NULL
, *ptr
;
908 unsigned len
= 0, i
= 0;
911 jsstr_t
**params
= NULL
;
915 static const WCHAR function_anonymousW
[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
916 static const WCHAR function_beginW
[] = {')',' ','{','\n'};
917 static const WCHAR function_endW
[] = L
"\n}";
920 params
= heap_alloc(argc
*sizeof(*params
));
922 return E_OUTOFMEMORY
;
925 len
= (argc
-2)*2; /* separating commas */
926 for(i
=0; i
< argc
; i
++) {
927 hres
= to_string(ctx
, argv
[i
], params
+i
);
930 len
+= jsstr_length(params
[i
]);
934 if(SUCCEEDED(hres
)) {
935 len
+= ARRAY_SIZE(function_anonymousW
) + ARRAY_SIZE(function_beginW
) + ARRAY_SIZE(function_endW
);
936 str
= heap_alloc(len
*sizeof(WCHAR
));
938 memcpy(str
, function_anonymousW
, sizeof(function_anonymousW
));
939 ptr
= str
+ ARRAY_SIZE(function_anonymousW
);
942 ptr
+= jsstr_flush(params
[j
], ptr
);
949 memcpy(ptr
, function_beginW
, sizeof(function_beginW
));
950 ptr
+= ARRAY_SIZE(function_beginW
);
952 ptr
+= jsstr_flush(params
[argc
-1], ptr
);
953 memcpy(ptr
, function_endW
, sizeof(function_endW
));
955 TRACE("%s\n", debugstr_w(str
));
957 hres
= E_OUTOFMEMORY
;
962 jsstr_release(params
[--i
]);
967 hres
= compile_script(ctx
, str
, 0, 0, NULL
, NULL
, FALSE
, FALSE
,
968 ctx
->call_ctx
? ctx
->call_ctx
->bytecode
->named_item
: NULL
, &code
);
973 if(code
->global_code
.func_cnt
!= 1 || code
->global_code
.var_cnt
!= 1) {
974 ERR("Invalid parser result!\n");
975 release_bytecode(code
);
979 hres
= create_source_function(ctx
, code
, code
->global_code
.funcs
, NULL
, &function
);
980 release_bytecode(code
);
984 *ret
= to_disp(function
);
988 static HRESULT
FunctionConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
996 case DISPATCH_METHOD
:
997 case DISPATCH_CONSTRUCT
: {
1000 hres
= construct_function(ctx
, argc
, argv
, &ret
);
1004 *r
= jsval_disp(ret
);
1008 FIXME("unimplemented flags %x\n", flags
);
1015 static HRESULT
FunctionProt_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1022 HRESULT
init_function_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
)
1024 NativeFunction
*prot
, *constr
;
1027 hres
= create_function(ctx
, &Function_info
, &NativeFunctionVtbl
, sizeof(NativeFunction
), PROPF_CONSTR
,
1028 TRUE
, object_prototype
, (void**)&prot
);
1032 prot
->proc
= FunctionProt_value
;
1033 prot
->name
= L
"prototype";
1035 hres
= create_function(ctx
, &FunctionInst_info
, &NativeFunctionVtbl
, sizeof(NativeFunction
), PROPF_CONSTR
|1,
1036 TRUE
, &prot
->function
.dispex
, (void**)&constr
);
1037 if(SUCCEEDED(hres
)) {
1038 constr
->proc
= FunctionConstr_value
;
1039 constr
->name
= L
"Function";
1040 hres
= jsdisp_define_data_property(&constr
->function
.dispex
, L
"prototype", 0, jsval_obj(&prot
->function
.dispex
));
1042 hres
= set_constructor_prop(ctx
, &constr
->function
.dispex
, &prot
->function
.dispex
);
1044 jsdisp_release(&constr
->function
.dispex
);
1046 jsdisp_release(&prot
->function
.dispex
);
1050 ctx
->function_constr
= &constr
->function
.dispex
;