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 const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
97 static const WCHAR lengthW
[] = {'l','e','n','g','t','h',0};
98 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
99 static const WCHAR applyW
[] = {'a','p','p','l','y',0};
100 static const WCHAR bindW
[] = {'b','i','n','d',0};
101 static const WCHAR callW
[] = {'c','a','l','l',0};
102 static const WCHAR argumentsW
[] = {'a','r','g','u','m','e','n','t','s',0};
104 static HRESULT
Arguments_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
111 static void Arguments_destructor(jsdisp_t
*jsdisp
)
113 ArgumentsInstance
*arguments
= arguments_from_jsdisp(jsdisp
);
115 TRACE("(%p)\n", arguments
);
119 for(i
= 0; i
< arguments
->argc
; i
++)
120 jsval_release(arguments
->buf
[i
]);
121 heap_free(arguments
->buf
);
124 jsdisp_release(&arguments
->function
->function
.dispex
);
125 heap_free(arguments
);
128 static unsigned Arguments_idx_length(jsdisp_t
*jsdisp
)
130 ArgumentsInstance
*arguments
= arguments_from_jsdisp(jsdisp
);
131 return arguments
->argc
;
134 static jsval_t
*get_argument_ref(ArgumentsInstance
*arguments
, unsigned idx
)
137 return arguments
->buf
+ idx
;
138 if(arguments
->frame
->base_scope
->frame
|| idx
>= arguments
->frame
->function
->param_cnt
)
139 return arguments
->jsdisp
.ctx
->stack
+ arguments
->frame
->arguments_off
+ idx
;
143 static HRESULT
Arguments_idx_get(jsdisp_t
*jsdisp
, unsigned idx
, jsval_t
*r
)
145 ArgumentsInstance
*arguments
= arguments_from_jsdisp(jsdisp
);
148 TRACE("%p[%u]\n", arguments
, idx
);
150 if((ref
= get_argument_ref(arguments
, idx
)))
151 return jsval_copy(*ref
, r
);
153 /* FIXME: Accessing by name won't work for duplicated argument names */
154 return jsdisp_propget_name(arguments
->frame
->base_scope
->jsobj
,
155 arguments
->function
->func_code
->params
[idx
], r
);
158 static HRESULT
Arguments_idx_put(jsdisp_t
*jsdisp
, unsigned idx
, jsval_t val
)
160 ArgumentsInstance
*arguments
= arguments_from_jsdisp(jsdisp
);
164 TRACE("%p[%u] = %s\n", arguments
, idx
, debugstr_jsval(val
));
166 if((ref
= get_argument_ref(arguments
, idx
))) {
168 hres
= jsval_copy(val
, ©
);
177 /* FIXME: Accessing by name won't work for duplicated argument names */
178 return jsdisp_propput_name(arguments
->frame
->base_scope
->jsobj
,
179 arguments
->function
->func_code
->params
[idx
], val
);
182 static const builtin_info_t Arguments_info
= {
184 {NULL
, Arguments_value
, 0},
186 Arguments_destructor
,
188 Arguments_idx_length
,
193 HRESULT
setup_arguments_object(script_ctx_t
*ctx
, call_frame_t
*frame
)
195 ArgumentsInstance
*args
;
198 static const WCHAR caleeW
[] = {'c','a','l','l','e','e',0};
200 args
= heap_alloc_zero(sizeof(*args
));
202 return E_OUTOFMEMORY
;
204 hres
= init_dispex_from_constr(&args
->jsdisp
, ctx
, &Arguments_info
, ctx
->object_constr
);
210 args
->function
= (InterpretedFunction
*)function_from_jsdisp(jsdisp_addref(frame
->function_instance
));
211 args
->argc
= frame
->argc
;
214 hres
= jsdisp_define_data_property(&args
->jsdisp
, lengthW
, PROPF_WRITABLE
| PROPF_CONFIGURABLE
,
215 jsval_number(args
->argc
));
217 hres
= jsdisp_define_data_property(&args
->jsdisp
, caleeW
, PROPF_WRITABLE
| PROPF_CONFIGURABLE
,
218 jsval_obj(&args
->function
->function
.dispex
));
220 hres
= jsdisp_propput(frame
->base_scope
->jsobj
, argumentsW
, PROPF_WRITABLE
, jsval_obj(&args
->jsdisp
));
222 jsdisp_release(&args
->jsdisp
);
226 frame
->arguments_obj
= &args
->jsdisp
;
230 void detach_arguments_object(jsdisp_t
*args_disp
)
232 ArgumentsInstance
*arguments
= arguments_from_jsdisp(args_disp
);
233 call_frame_t
*frame
= arguments
->frame
;
234 const BOOL on_stack
= frame
->base_scope
->frame
== frame
;
237 /* Reset arguments value to cut the reference cycle. Note that since all activation contexts have
238 * their own arguments property, it's impossible to use prototype's one during name lookup */
239 jsdisp_propput_name(frame
->base_scope
->jsobj
, argumentsW
, jsval_undefined());
240 arguments
->frame
= NULL
;
242 /* Don't bother coppying arguments if call frame holds the last reference. */
243 if(arguments
->jsdisp
.ref
> 1) {
244 arguments
->buf
= heap_alloc(arguments
->argc
* sizeof(*arguments
->buf
));
248 for(i
= 0; i
< arguments
->argc
; i
++) {
249 if(on_stack
|| i
>= frame
->function
->param_cnt
)
250 hres
= jsval_copy(arguments
->jsdisp
.ctx
->stack
[frame
->arguments_off
+ i
], arguments
->buf
+i
);
252 hres
= jsdisp_propget_name(frame
->base_scope
->jsobj
, frame
->function
->params
[i
], arguments
->buf
+i
);
254 arguments
->buf
[i
] = jsval_undefined();
257 ERR("out of memory\n");
262 jsdisp_release(frame
->arguments_obj
);
265 HRESULT
Function_invoke(jsdisp_t
*func_this
, IDispatch
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
267 FunctionInstance
*function
;
269 TRACE("func %p this %p\n", func_this
, jsthis
);
271 assert(is_class(func_this
, JSCLASS_FUNCTION
));
272 function
= function_from_jsdisp(func_this
);
274 return function
->vtbl
->call(function
->dispex
.ctx
, function
, jsthis
, flags
, argc
, argv
, r
);
277 static HRESULT
Function_get_length(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
279 TRACE("%p\n", jsthis
);
281 *r
= jsval_number(function_from_jsdisp(jsthis
)->length
);
285 static HRESULT
Function_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
288 FunctionInstance
*function
;
294 if(!(function
= function_this(jsthis
)))
295 return JS_E_FUNCTION_EXPECTED
;
297 hres
= function
->vtbl
->toString(function
, &str
);
302 *r
= jsval_string(str
);
308 static HRESULT
array_to_args(script_ctx_t
*ctx
, jsdisp_t
*arg_array
, unsigned *argc
, jsval_t
**ret
)
314 hres
= jsdisp_propget_name(arg_array
, lengthW
, &val
);
318 hres
= to_uint32(ctx
, val
, &length
);
323 argv
= heap_alloc(length
* sizeof(*argv
));
325 return E_OUTOFMEMORY
;
327 for(i
=0; i
<length
; i
++) {
328 hres
= jsdisp_get_idx(arg_array
, i
, argv
+i
);
329 if(hres
== DISP_E_UNKNOWNNAME
) {
330 argv
[i
] = jsval_undefined();
331 }else if(FAILED(hres
)) {
333 jsval_release(argv
[i
]);
344 static HRESULT
Function_apply(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
346 FunctionInstance
*function
;
347 jsval_t
*args
= NULL
;
349 IDispatch
*this_obj
= NULL
;
354 if(!(function
= function_this(jsthis
)) && (jsthis
->flags
& VDISP_JSDISP
))
355 return JS_E_FUNCTION_EXPECTED
;
358 if(!is_undefined(argv
[0]) && !is_null(argv
[0])) {
359 hres
= to_object(ctx
, argv
[0], &this_obj
);
366 jsdisp_t
*arg_array
= NULL
;
368 if(is_object_instance(argv
[1])) {
369 arg_array
= iface_to_jsdisp(get_object(argv
[1]));
371 (!is_class(arg_array
, JSCLASS_ARRAY
) && !is_class(arg_array
, JSCLASS_ARGUMENTS
) )) {
372 jsdisp_release(arg_array
);
378 hres
= array_to_args(ctx
, arg_array
, &cnt
, &args
);
379 jsdisp_release(arg_array
);
381 FIXME("throw TypeError\n");
386 if(SUCCEEDED(hres
)) {
388 hres
= function
->vtbl
->call(ctx
, function
, this_obj
, flags
, cnt
, args
, r
);
391 hres
= disp_call_value(ctx
, jsthis
->u
.disp
, this_obj
, DISPATCH_METHOD
, cnt
, args
, &res
);
392 if(SUCCEEDED(hres
)) {
402 IDispatch_Release(this_obj
);
403 for(i
=0; i
< cnt
; i
++)
404 jsval_release(args
[i
]);
409 static HRESULT
Function_call(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
412 FunctionInstance
*function
;
413 IDispatch
*this_obj
= NULL
;
419 if(!(function
= function_this(jsthis
)))
420 return JS_E_FUNCTION_EXPECTED
;
423 if(!is_undefined(argv
[0]) && !is_null(argv
[0])) {
424 hres
= to_object(ctx
, argv
[0], &this_obj
);
432 hres
= function
->vtbl
->call(ctx
, function
, this_obj
, flags
, cnt
, argv
+ 1, r
);
435 IDispatch_Release(this_obj
);
439 static HRESULT
Function_bind(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
442 IDispatch
*bound_this
= NULL
;
443 FunctionInstance
*function
;
444 jsdisp_t
*new_function
;
449 if(!(function
= function_this(jsthis
)))
450 return JS_E_FUNCTION_EXPECTED
;
453 FIXME("no this argument\n");
457 if(is_object_instance(argv
[0])) {
458 bound_this
= get_object(argv
[0]);
459 }else if(!is_null(argv
[0])) {
460 FIXME("%s is not an object instance\n", debugstr_jsval(argv
[0]));
464 hres
= create_bind_function(ctx
, function
, bound_this
, argc
- 1, argv
+ 1, &new_function
);
469 *r
= jsval_obj(new_function
);
471 jsdisp_release(new_function
);
475 HRESULT
Function_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
478 FunctionInstance
*function
;
482 if(!is_vclass(jsthis
, JSCLASS_FUNCTION
)) {
483 ERR("dispex is not a function\n");
487 function
= function_from_jsdisp(jsthis
->u
.jsdisp
);
488 return function
->vtbl
->call(ctx
, function
, NULL
, flags
, argc
, argv
, r
);
491 HRESULT
Function_get_value(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
493 FunctionInstance
*function
= function_from_jsdisp(jsthis
);
499 hres
= function
->vtbl
->toString(function
, &str
);
503 *r
= jsval_string(str
);
507 static HRESULT
Function_get_arguments(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
509 FunctionInstance
*function
= function_from_jsdisp(jsthis
);
515 for(frame
= ctx
->call_ctx
; frame
; frame
= frame
->prev_frame
) {
516 if(frame
->function_instance
== &function
->dispex
) {
517 if(!frame
->arguments_obj
) {
518 hres
= setup_arguments_object(ctx
, frame
);
522 *r
= jsval_obj(jsdisp_addref(frame
->arguments_obj
));
531 function_code_t
*Function_get_code(jsdisp_t
*jsthis
)
533 FunctionInstance
*function
;
535 assert(is_class(jsthis
, JSCLASS_FUNCTION
));
536 function
= function_from_jsdisp(jsthis
);
538 return function
->vtbl
->get_code(function
);
541 static void Function_destructor(jsdisp_t
*dispex
)
543 FunctionInstance
*function
= function_from_jsdisp(dispex
);
544 function
->vtbl
->destructor(function
);
548 static const builtin_prop_t Function_props
[] = {
549 {applyW
, Function_apply
, PROPF_METHOD
|2},
550 {argumentsW
, NULL
, 0, Function_get_arguments
},
551 {bindW
, Function_bind
, PROPF_METHOD
|PROPF_ES5
|1},
552 {callW
, Function_call
, PROPF_METHOD
|1},
553 {lengthW
, NULL
, 0, Function_get_length
},
554 {toStringW
, Function_toString
, PROPF_METHOD
}
557 static const builtin_info_t Function_info
= {
559 DEFAULT_FUNCTION_VALUE
,
560 ARRAY_SIZE(Function_props
),
566 static const builtin_prop_t FunctionInst_props
[] = {
567 {argumentsW
, NULL
, 0, Function_get_arguments
},
568 {lengthW
, NULL
, 0, Function_get_length
}
571 static const builtin_info_t FunctionInst_info
= {
573 DEFAULT_FUNCTION_VALUE
,
574 ARRAY_SIZE(FunctionInst_props
),
580 static HRESULT
create_function(script_ctx_t
*ctx
, const builtin_info_t
*builtin_info
, const function_vtbl_t
*vtbl
, size_t size
,
581 DWORD flags
, BOOL funcprot
, jsdisp_t
*prototype
, void **ret
)
583 FunctionInstance
*function
;
586 function
= heap_alloc_zero(size
);
588 return E_OUTOFMEMORY
;
591 hres
= init_dispex(&function
->dispex
, ctx
, builtin_info
, prototype
);
592 else if(builtin_info
)
593 hres
= init_dispex_from_constr(&function
->dispex
, ctx
, builtin_info
, ctx
->function_constr
);
595 hres
= init_dispex_from_constr(&function
->dispex
, ctx
, &FunctionInst_info
, ctx
->function_constr
);
601 function
->vtbl
= vtbl
;
602 function
->flags
= flags
;
603 function
->length
= flags
& PROPF_ARGMASK
;
609 static HRESULT
NativeFunction_call(script_ctx_t
*ctx
, FunctionInstance
*func
, IDispatch
*this_disp
, unsigned flags
,
610 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
612 NativeFunction
*function
= (NativeFunction
*)func
;
617 set_disp(&vthis
, this_disp
);
619 set_disp(&vthis
, lookup_global_host(ctx
));
621 hres
= function
->proc(ctx
, &vthis
, flags
& ~DISPATCH_JSCRIPT_INTERNAL_MASK
, argc
, argv
, r
);
623 vdisp_release(&vthis
);
627 static HRESULT
NativeFunction_toString(FunctionInstance
*func
, jsstr_t
**ret
)
629 NativeFunction
*function
= (NativeFunction
*)func
;
634 static const WCHAR native_prefixW
[] = {'\n','f','u','n','c','t','i','o','n',' '};
635 static const WCHAR native_suffixW
[] =
636 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
638 name_len
= function
->name
? lstrlenW(function
->name
) : 0;
639 str
= jsstr_alloc_buf(ARRAY_SIZE(native_prefixW
) + ARRAY_SIZE(native_suffixW
) + name_len
, &ptr
);
641 return E_OUTOFMEMORY
;
643 memcpy(ptr
, native_prefixW
, sizeof(native_prefixW
));
644 ptr
+= ARRAY_SIZE(native_prefixW
);
645 memcpy(ptr
, function
->name
, name_len
*sizeof(WCHAR
));
647 memcpy(ptr
, native_suffixW
, sizeof(native_suffixW
));
653 static function_code_t
*NativeFunction_get_code(FunctionInstance
*function
)
658 static void NativeFunction_destructor(FunctionInstance
*function
)
662 static const function_vtbl_t NativeFunctionVtbl
= {
664 NativeFunction_toString
,
665 NativeFunction_get_code
,
666 NativeFunction_destructor
669 HRESULT
create_builtin_function(script_ctx_t
*ctx
, builtin_invoke_t value_proc
, const WCHAR
*name
,
670 const builtin_info_t
*builtin_info
, DWORD flags
, jsdisp_t
*prototype
, jsdisp_t
**ret
)
672 NativeFunction
*function
;
675 hres
= create_function(ctx
, builtin_info
, &NativeFunctionVtbl
, sizeof(NativeFunction
), flags
, FALSE
, NULL
, (void**)&function
);
680 hres
= jsdisp_define_data_property(&function
->function
.dispex
, lengthW
, 0,
681 jsval_number(function
->function
.length
));
683 hres
= jsdisp_define_data_property(&function
->function
.dispex
, prototypeW
, 0, jsval_obj(prototype
));
685 jsdisp_release(&function
->function
.dispex
);
689 function
->proc
= value_proc
;
690 function
->name
= name
;
692 *ret
= &function
->function
.dispex
;
696 static HRESULT
set_constructor_prop(script_ctx_t
*ctx
, jsdisp_t
*constr
, jsdisp_t
*prot
)
698 static const WCHAR constructorW
[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
700 return jsdisp_define_data_property(prot
, constructorW
, PROPF_WRITABLE
| PROPF_CONFIGURABLE
,
704 HRESULT
create_builtin_constructor(script_ctx_t
*ctx
, builtin_invoke_t value_proc
, const WCHAR
*name
,
705 const builtin_info_t
*builtin_info
, DWORD flags
, jsdisp_t
*prototype
, jsdisp_t
**ret
)
710 hres
= create_builtin_function(ctx
, value_proc
, name
, builtin_info
, flags
, prototype
, &constr
);
714 hres
= set_constructor_prop(ctx
, constr
, prototype
);
716 jsdisp_release(constr
);
724 static HRESULT
InterpretedFunction_call(script_ctx_t
*ctx
, FunctionInstance
*func
, IDispatch
*this_obj
, unsigned flags
,
725 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
727 InterpretedFunction
*function
= (InterpretedFunction
*)func
;
728 jsdisp_t
*new_obj
= NULL
;
729 DWORD exec_flags
= 0;
732 TRACE("%p\n", function
);
734 if(ctx
->state
== SCRIPTSTATE_UNINITIALIZED
|| ctx
->state
== SCRIPTSTATE_CLOSED
) {
735 WARN("Script engine state does not allow running code.\n");
739 if(flags
& DISPATCH_CONSTRUCT
) {
740 hres
= create_object(ctx
, &function
->function
.dispex
, &new_obj
);
743 this_obj
= to_disp(new_obj
);
746 if(flags
& DISPATCH_JSCRIPT_CALLEREXECSSOURCE
)
747 exec_flags
|= EXEC_RETURN_TO_INTERP
;
748 if(flags
& DISPATCH_CONSTRUCT
)
749 exec_flags
|= EXEC_CONSTRUCTOR
;
750 hres
= exec_source(ctx
, exec_flags
, function
->code
, function
->func_code
, function
->scope_chain
, this_obj
,
751 &function
->function
.dispex
, argc
, argv
, r
);
753 jsdisp_release(new_obj
);
757 static HRESULT
InterpretedFunction_toString(FunctionInstance
*func
, jsstr_t
**ret
)
759 InterpretedFunction
*function
= (InterpretedFunction
*)func
;
761 *ret
= jsstr_alloc_len(function
->func_code
->source
, function
->func_code
->source_len
);
762 return *ret
? S_OK
: E_OUTOFMEMORY
;
765 static function_code_t
*InterpretedFunction_get_code(FunctionInstance
*func
)
767 InterpretedFunction
*function
= (InterpretedFunction
*)func
;
769 return function
->func_code
;
772 static void InterpretedFunction_destructor(FunctionInstance
*func
)
774 InterpretedFunction
*function
= (InterpretedFunction
*)func
;
776 release_bytecode(function
->code
);
777 if(function
->scope_chain
)
778 scope_release(function
->scope_chain
);
781 static const function_vtbl_t InterpretedFunctionVtbl
= {
782 InterpretedFunction_call
,
783 InterpretedFunction_toString
,
784 InterpretedFunction_get_code
,
785 InterpretedFunction_destructor
788 HRESULT
create_source_function(script_ctx_t
*ctx
, bytecode_t
*code
, function_code_t
*func_code
,
789 scope_chain_t
*scope_chain
, jsdisp_t
**ret
)
791 InterpretedFunction
*function
;
795 hres
= create_object(ctx
, NULL
, &prototype
);
799 hres
= create_function(ctx
, NULL
, &InterpretedFunctionVtbl
, sizeof(InterpretedFunction
), PROPF_CONSTR
,
800 FALSE
, NULL
, (void**)&function
);
801 if(SUCCEEDED(hres
)) {
802 hres
= jsdisp_define_data_property(&function
->function
.dispex
, prototypeW
, PROPF_WRITABLE
,
803 jsval_obj(prototype
));
805 hres
= set_constructor_prop(ctx
, &function
->function
.dispex
, prototype
);
807 jsdisp_release(&function
->function
.dispex
);
809 jsdisp_release(prototype
);
814 scope_addref(scope_chain
);
815 function
->scope_chain
= scope_chain
;
818 bytecode_addref(code
);
819 function
->code
= code
;
820 function
->func_code
= func_code
;
821 function
->function
.length
= function
->func_code
->param_cnt
;
823 *ret
= &function
->function
.dispex
;
827 static HRESULT
BindFunction_call(script_ctx_t
*ctx
, FunctionInstance
*func
, IDispatch
*this_obj
, unsigned flags
,
828 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
830 BindFunction
*function
= (BindFunction
*)func
;
831 jsval_t
*call_args
= NULL
;
835 TRACE("%p\n", function
);
837 call_argc
= function
->argc
+ argc
;
839 call_args
= heap_alloc(function
->argc
* sizeof(*function
->args
));
841 return E_OUTOFMEMORY
;
844 memcpy(call_args
, function
->args
, function
->argc
* sizeof(*call_args
));
846 memcpy(call_args
+ function
->argc
, argv
, argc
* sizeof(*call_args
));
849 hres
= function
->target
->vtbl
->call(ctx
, function
->target
, function
->this, flags
, call_argc
, call_args
, r
);
851 heap_free(call_args
);
855 static HRESULT
BindFunction_toString(FunctionInstance
*function
, jsstr_t
**ret
)
857 static const WCHAR native_functionW
[] =
858 {'\n','f','u','n','c','t','i','o','n','(',')',' ','{','\n',
859 ' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n',
862 *ret
= jsstr_alloc(native_functionW
);
863 return *ret
? S_OK
: E_OUTOFMEMORY
;
866 static function_code_t
*BindFunction_get_code(FunctionInstance
*function
)
871 static void BindFunction_destructor(FunctionInstance
*func
)
873 BindFunction
*function
= (BindFunction
*)func
;
876 TRACE("%p\n", function
);
878 for(i
= 0; i
< function
->argc
; i
++)
879 jsval_release(function
->args
[i
]);
880 jsdisp_release(&function
->target
->dispex
);
882 IDispatch_Release(function
->this);
885 static const function_vtbl_t BindFunctionVtbl
= {
887 BindFunction_toString
,
888 BindFunction_get_code
,
889 BindFunction_destructor
892 static HRESULT
create_bind_function(script_ctx_t
*ctx
, FunctionInstance
*target
, IDispatch
*bound_this
, unsigned argc
,
893 jsval_t
*argv
, jsdisp_t
**ret
)
895 BindFunction
*function
;
898 hres
= create_function(ctx
, NULL
, &BindFunctionVtbl
, FIELD_OFFSET(BindFunction
, args
[argc
]), PROPF_METHOD
,
899 FALSE
, NULL
, (void**)&function
);
903 jsdisp_addref(&target
->dispex
);
904 function
->target
= target
;
907 IDispatch_AddRef(function
->this = bound_this
);
909 for(function
->argc
= 0; function
->argc
< argc
; function
->argc
++) {
910 hres
= jsval_copy(argv
[function
->argc
], function
->args
+ function
->argc
);
912 jsdisp_release(&function
->function
.dispex
);
917 function
->function
.length
= target
->length
> argc
? target
->length
- argc
: 0;
919 *ret
= &function
->function
.dispex
;
923 static HRESULT
construct_function(script_ctx_t
*ctx
, unsigned argc
, jsval_t
*argv
, IDispatch
**ret
)
925 WCHAR
*str
= NULL
, *ptr
;
926 unsigned len
= 0, i
= 0;
929 jsstr_t
**params
= NULL
;
933 static const WCHAR function_anonymousW
[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
934 static const WCHAR function_beginW
[] = {')',' ','{','\n'};
935 static const WCHAR function_endW
[] = {'\n','}',0};
938 params
= heap_alloc(argc
*sizeof(*params
));
940 return E_OUTOFMEMORY
;
943 len
= (argc
-2)*2; /* separating commas */
944 for(i
=0; i
< argc
; i
++) {
945 hres
= to_string(ctx
, argv
[i
], params
+i
);
948 len
+= jsstr_length(params
[i
]);
952 if(SUCCEEDED(hres
)) {
953 len
+= ARRAY_SIZE(function_anonymousW
) + ARRAY_SIZE(function_beginW
) + ARRAY_SIZE(function_endW
);
954 str
= heap_alloc(len
*sizeof(WCHAR
));
956 memcpy(str
, function_anonymousW
, sizeof(function_anonymousW
));
957 ptr
= str
+ ARRAY_SIZE(function_anonymousW
);
960 ptr
+= jsstr_flush(params
[j
], ptr
);
967 memcpy(ptr
, function_beginW
, sizeof(function_beginW
));
968 ptr
+= ARRAY_SIZE(function_beginW
);
970 ptr
+= jsstr_flush(params
[argc
-1], ptr
);
971 memcpy(ptr
, function_endW
, sizeof(function_endW
));
973 TRACE("%s\n", debugstr_w(str
));
975 hres
= E_OUTOFMEMORY
;
980 jsstr_release(params
[--i
]);
985 hres
= compile_script(ctx
, str
, 0, 0, NULL
, NULL
, FALSE
, FALSE
,
986 ctx
->call_ctx
? ctx
->call_ctx
->bytecode
->named_item
: NULL
, &code
);
991 if(code
->global_code
.func_cnt
!= 1 || code
->global_code
.var_cnt
!= 1) {
992 ERR("Invalid parser result!\n");
993 release_bytecode(code
);
997 hres
= create_source_function(ctx
, code
, code
->global_code
.funcs
, NULL
, &function
);
998 release_bytecode(code
);
1002 *ret
= to_disp(function
);
1006 static HRESULT
FunctionConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1014 case DISPATCH_METHOD
:
1015 case DISPATCH_CONSTRUCT
: {
1018 hres
= construct_function(ctx
, argc
, argv
, &ret
);
1022 *r
= jsval_disp(ret
);
1026 FIXME("unimplemented flags %x\n", flags
);
1033 static HRESULT
FunctionProt_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1040 HRESULT
init_function_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
)
1042 NativeFunction
*prot
, *constr
;
1045 static const WCHAR FunctionW
[] = {'F','u','n','c','t','i','o','n',0};
1047 hres
= create_function(ctx
, &Function_info
, &NativeFunctionVtbl
, sizeof(NativeFunction
), PROPF_CONSTR
,
1048 TRUE
, object_prototype
, (void**)&prot
);
1052 prot
->proc
= FunctionProt_value
;
1053 prot
->name
= prototypeW
;
1055 hres
= create_function(ctx
, &FunctionInst_info
, &NativeFunctionVtbl
, sizeof(NativeFunction
), PROPF_CONSTR
|1,
1056 TRUE
, &prot
->function
.dispex
, (void**)&constr
);
1057 if(SUCCEEDED(hres
)) {
1058 constr
->proc
= FunctionConstr_value
;
1059 constr
->name
= FunctionW
;
1060 hres
= jsdisp_define_data_property(&constr
->function
.dispex
, prototypeW
, 0, jsval_obj(&prot
->function
.dispex
));
1062 hres
= set_constructor_prop(ctx
, &constr
->function
.dispex
, &prot
->function
.dispex
);
1064 jsdisp_release(&constr
->function
.dispex
);
1066 jsdisp_release(&prot
->function
.dispex
);
1070 ctx
->function_constr
= &constr
->function
.dispex
;