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
23 builtin_invoke_t value_proc
;
26 scope_chain_t
*scope_chain
;
28 function_code_t
*func_code
;
34 FunctionInstance
*function
;
40 static inline FunctionInstance
*function_from_jsdisp(jsdisp_t
*jsdisp
)
42 return CONTAINING_RECORD(jsdisp
, FunctionInstance
, dispex
);
45 static inline FunctionInstance
*function_from_vdisp(vdisp_t
*vdisp
)
47 return function_from_jsdisp(vdisp
->u
.jsdisp
);
50 static inline FunctionInstance
*function_this(vdisp_t
*jsthis
)
52 return is_vclass(jsthis
, JSCLASS_FUNCTION
) ? function_from_vdisp(jsthis
) : NULL
;
55 static inline ArgumentsInstance
*arguments_from_jsdisp(jsdisp_t
*jsdisp
)
57 return CONTAINING_RECORD(jsdisp
, ArgumentsInstance
, jsdisp
);
60 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
62 static const WCHAR lengthW
[] = {'l','e','n','g','t','h',0};
63 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
64 static const WCHAR applyW
[] = {'a','p','p','l','y',0};
65 static const WCHAR callW
[] = {'c','a','l','l',0};
66 static const WCHAR argumentsW
[] = {'a','r','g','u','m','e','n','t','s',0};
68 static HRESULT
Arguments_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
75 static void Arguments_destructor(jsdisp_t
*jsdisp
)
77 ArgumentsInstance
*arguments
= arguments_from_jsdisp(jsdisp
);
79 TRACE("(%p)\n", arguments
);
83 for(i
= 0; i
< arguments
->argc
; i
++)
84 jsval_release(arguments
->buf
[i
]);
85 heap_free(arguments
->buf
);
88 jsdisp_release(&arguments
->function
->dispex
);
92 static unsigned Arguments_idx_length(jsdisp_t
*jsdisp
)
94 ArgumentsInstance
*arguments
= arguments_from_jsdisp(jsdisp
);
95 return arguments
->argc
;
98 static jsval_t
*get_argument_ref(ArgumentsInstance
*arguments
, unsigned idx
)
101 return arguments
->buf
+ idx
;
102 if(arguments
->frame
->base_scope
->frame
|| idx
>= arguments
->frame
->function
->param_cnt
)
103 return arguments
->jsdisp
.ctx
->stack
+ arguments
->frame
->arguments_off
+ idx
;
107 static HRESULT
Arguments_idx_get(jsdisp_t
*jsdisp
, unsigned idx
, jsval_t
*r
)
109 ArgumentsInstance
*arguments
= arguments_from_jsdisp(jsdisp
);
112 TRACE("%p[%u]\n", arguments
, idx
);
114 if((ref
= get_argument_ref(arguments
, idx
)))
115 return jsval_copy(*ref
, r
);
117 /* FIXME: Accessing by name won't work for duplicated argument names */
118 return jsdisp_propget_name(arguments
->frame
->base_scope
->jsobj
, arguments
->function
->func_code
->params
[idx
], r
);
121 static HRESULT
Arguments_idx_put(jsdisp_t
*jsdisp
, unsigned idx
, jsval_t val
)
123 ArgumentsInstance
*arguments
= arguments_from_jsdisp(jsdisp
);
127 TRACE("%p[%u] = %s\n", arguments
, idx
, debugstr_jsval(val
));
129 if((ref
= get_argument_ref(arguments
, idx
))) {
131 hres
= jsval_copy(val
, ©
);
140 /* FIXME: Accessing by name won't work for duplicated argument names */
141 return jsdisp_propput_name(arguments
->frame
->base_scope
->jsobj
, arguments
->function
->func_code
->params
[idx
], val
);
144 static const builtin_info_t Arguments_info
= {
146 {NULL
, Arguments_value
, 0},
148 Arguments_destructor
,
150 Arguments_idx_length
,
155 HRESULT
setup_arguments_object(script_ctx_t
*ctx
, call_frame_t
*frame
)
157 ArgumentsInstance
*args
;
160 static const WCHAR caleeW
[] = {'c','a','l','l','e','e',0};
162 args
= heap_alloc_zero(sizeof(*args
));
164 return E_OUTOFMEMORY
;
166 hres
= init_dispex_from_constr(&args
->jsdisp
, ctx
, &Arguments_info
, ctx
->object_constr
);
172 args
->function
= function_from_jsdisp(jsdisp_addref(frame
->function_instance
));
173 args
->argc
= frame
->argc
;
176 hres
= jsdisp_propput_dontenum(&args
->jsdisp
, lengthW
, jsval_number(args
->argc
));
178 hres
= jsdisp_propput_dontenum(&args
->jsdisp
, caleeW
, jsval_disp(to_disp(&args
->function
->dispex
)));
180 hres
= jsdisp_propput(frame
->base_scope
->jsobj
, argumentsW
, PROPF_DONTDELETE
, jsval_obj(&args
->jsdisp
));
182 jsdisp_release(&args
->jsdisp
);
186 frame
->arguments_obj
= &args
->jsdisp
;
190 void detach_arguments_object(jsdisp_t
*args_disp
)
192 ArgumentsInstance
*arguments
= arguments_from_jsdisp(args_disp
);
193 call_frame_t
*frame
= arguments
->frame
;
194 const BOOL on_stack
= frame
->base_scope
->frame
== frame
;
197 /* Reset arguments value to cut the reference cycle. Note that since all activation contexts have
198 * their own arguments property, it's impossible to use prototype's one during name lookup */
199 jsdisp_propput_name(frame
->base_scope
->jsobj
, argumentsW
, jsval_undefined());
200 arguments
->frame
= NULL
;
202 /* Don't bother coppying arguments if call frame holds the last reference. */
203 if(arguments
->jsdisp
.ref
> 1) {
204 arguments
->buf
= heap_alloc(arguments
->argc
* sizeof(*arguments
->buf
));
208 for(i
= 0; i
< arguments
->argc
; i
++) {
209 if(on_stack
|| i
>= frame
->function
->param_cnt
)
210 hres
= jsval_copy(arguments
->jsdisp
.ctx
->stack
[frame
->arguments_off
+ i
], arguments
->buf
+i
);
212 hres
= jsdisp_propget_name(frame
->base_scope
->jsobj
, frame
->function
->params
[i
], arguments
->buf
+i
);
214 arguments
->buf
[i
] = jsval_undefined();
217 ERR("out of memory\n");
222 jsdisp_release(frame
->arguments_obj
);
225 static HRESULT
invoke_source(script_ctx_t
*ctx
, FunctionInstance
*function
, IDispatch
*this_obj
, unsigned argc
, jsval_t
*argv
,
226 BOOL is_constructor
, BOOL caller_execs_source
, jsval_t
*r
)
229 DWORD exec_flags
= 0;
232 if(ctx
->state
== SCRIPTSTATE_UNINITIALIZED
|| ctx
->state
== SCRIPTSTATE_CLOSED
) {
233 WARN("Script engine state does not allow running code.\n");
237 if(!function
->func_code
) {
238 FIXME("no source\n");
242 hres
= create_dispex(ctx
, NULL
, NULL
, &var_disp
);
246 if(caller_execs_source
)
247 exec_flags
|= EXEC_RETURN_TO_INTERP
;
249 exec_flags
|= EXEC_CONSTRUCTOR
;
250 hres
= exec_source(ctx
, exec_flags
, function
->code
, function
->func_code
, function
->scope_chain
, this_obj
,
251 &function
->dispex
, var_disp
, argc
, argv
, r
);
253 jsdisp_release(var_disp
);
257 static HRESULT
invoke_value_proc(script_ctx_t
*ctx
, FunctionInstance
*function
, IDispatch
*this_disp
, WORD flags
,
258 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
264 set_disp(&vthis
, this_disp
);
265 else if(ctx
->host_global
)
266 set_disp(&vthis
, ctx
->host_global
);
268 set_jsdisp(&vthis
, ctx
->global
);
270 hres
= function
->value_proc(ctx
, &vthis
, flags
, argc
, argv
, r
);
272 vdisp_release(&vthis
);
276 static HRESULT
call_function(script_ctx_t
*ctx
, FunctionInstance
*function
, IDispatch
*this_obj
,
277 unsigned argc
, jsval_t
*argv
, BOOL caller_execs_source
, jsval_t
*r
)
279 if(function
->value_proc
)
280 return invoke_value_proc(ctx
, function
, this_obj
, DISPATCH_METHOD
, argc
, argv
, r
);
282 return invoke_source(ctx
, function
, this_obj
, argc
, argv
, FALSE
, caller_execs_source
, r
);
285 static HRESULT
function_to_string(FunctionInstance
*function
, jsstr_t
**ret
)
289 static const WCHAR native_prefixW
[] = {'\n','f','u','n','c','t','i','o','n',' '};
290 static const WCHAR native_suffixW
[] =
291 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
293 if(function
->value_proc
) {
297 name_len
= strlenW(function
->name
);
298 str
= jsstr_alloc_buf((sizeof(native_prefixW
)+sizeof(native_suffixW
))/sizeof(WCHAR
) + name_len
, &ptr
);
300 return E_OUTOFMEMORY
;
302 memcpy(ptr
, native_prefixW
, sizeof(native_prefixW
));
303 memcpy(ptr
+= sizeof(native_prefixW
)/sizeof(WCHAR
), function
->name
, name_len
*sizeof(WCHAR
));
304 memcpy(ptr
+ name_len
, native_suffixW
, sizeof(native_suffixW
));
306 str
= jsstr_alloc_len(function
->func_code
->source
, function
->func_code
->source_len
);
308 return E_OUTOFMEMORY
;
315 HRESULT
Function_invoke(jsdisp_t
*func_this
, IDispatch
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
317 const BOOL caller_execs_source
= (flags
& DISPATCH_JSCRIPT_CALLEREXECSSOURCE
) != 0;
318 FunctionInstance
*function
;
320 TRACE("func %p this %p\n", func_this
, jsthis
);
322 assert(is_class(func_this
, JSCLASS_FUNCTION
));
323 function
= function_from_jsdisp(func_this
);
325 flags
&= ~DISPATCH_JSCRIPT_INTERNAL_MASK
;
326 if(function
->value_proc
)
327 return invoke_value_proc(function
->dispex
.ctx
, function
, jsthis
, flags
, argc
, argv
, r
);
329 if(flags
== DISPATCH_CONSTRUCT
) {
333 hres
= create_object(function
->dispex
.ctx
, &function
->dispex
, &this_obj
);
337 hres
= invoke_source(function
->dispex
.ctx
, function
, to_disp(this_obj
), argc
, argv
, TRUE
, caller_execs_source
, r
);
338 jsdisp_release(this_obj
);
342 assert(flags
== DISPATCH_METHOD
);
343 return invoke_source(function
->dispex
.ctx
, function
, jsthis
, argc
, argv
, FALSE
, caller_execs_source
, r
);
346 static HRESULT
Function_get_length(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
348 TRACE("%p\n", jsthis
);
350 *r
= jsval_number(function_from_jsdisp(jsthis
)->length
);
354 static HRESULT
Function_set_length(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t value
)
360 static HRESULT
Function_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
363 FunctionInstance
*function
;
369 if(!(function
= function_this(jsthis
)))
370 return throw_type_error(ctx
, JS_E_FUNCTION_EXPECTED
, NULL
);
372 hres
= function_to_string(function
, &str
);
377 *r
= jsval_string(str
);
383 static HRESULT
array_to_args(script_ctx_t
*ctx
, jsdisp_t
*arg_array
, unsigned *argc
, jsval_t
**ret
)
389 hres
= jsdisp_propget_name(arg_array
, lengthW
, &val
);
393 hres
= to_uint32(ctx
, val
, &length
);
398 argv
= heap_alloc(length
* sizeof(*argv
));
400 return E_OUTOFMEMORY
;
402 for(i
=0; i
<length
; i
++) {
403 hres
= jsdisp_get_idx(arg_array
, i
, argv
+i
);
404 if(hres
== DISP_E_UNKNOWNNAME
) {
405 argv
[i
] = jsval_undefined();
406 }else if(FAILED(hres
)) {
408 jsval_release(argv
[i
]);
419 static HRESULT
Function_apply(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
421 FunctionInstance
*function
;
422 jsval_t
*args
= NULL
;
424 IDispatch
*this_obj
= NULL
;
429 if(!(function
= function_this(jsthis
)) && (jsthis
->flags
& VDISP_JSDISP
))
430 return throw_type_error(ctx
, JS_E_FUNCTION_EXPECTED
, NULL
);
433 if(!is_undefined(argv
[0]) && !is_null(argv
[0])) {
434 hres
= to_object(ctx
, argv
[0], &this_obj
);
441 jsdisp_t
*arg_array
= NULL
;
443 if(is_object_instance(argv
[1])) {
444 arg_array
= iface_to_jsdisp(get_object(argv
[1]));
446 (!is_class(arg_array
, JSCLASS_ARRAY
) && !is_class(arg_array
, JSCLASS_ARGUMENTS
) )) {
447 jsdisp_release(arg_array
);
453 hres
= array_to_args(ctx
, arg_array
, &cnt
, &args
);
454 jsdisp_release(arg_array
);
456 FIXME("throw TypeError\n");
461 if(SUCCEEDED(hres
)) {
463 hres
= call_function(ctx
, function
, this_obj
, cnt
, args
, (flags
& DISPATCH_JSCRIPT_CALLEREXECSSOURCE
) != 0, r
);
466 hres
= disp_call_value(ctx
, jsthis
->u
.disp
, this_obj
, DISPATCH_METHOD
, cnt
, args
, &res
);
467 if(SUCCEEDED(hres
)) {
477 IDispatch_Release(this_obj
);
478 for(i
=0; i
< cnt
; i
++)
479 jsval_release(args
[i
]);
484 static HRESULT
Function_call(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
487 FunctionInstance
*function
;
488 IDispatch
*this_obj
= NULL
;
494 if(!(function
= function_this(jsthis
)))
495 return throw_type_error(ctx
, JS_E_FUNCTION_EXPECTED
, NULL
);
498 if(!is_undefined(argv
[0]) && !is_null(argv
[0])) {
499 hres
= to_object(ctx
, argv
[0], &this_obj
);
507 hres
= call_function(ctx
, function
, this_obj
, cnt
, argv
+1, (flags
& DISPATCH_JSCRIPT_CALLEREXECSSOURCE
) != 0, r
);
510 IDispatch_Release(this_obj
);
514 HRESULT
Function_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
517 FunctionInstance
*function
;
521 if(!is_vclass(jsthis
, JSCLASS_FUNCTION
)) {
522 ERR("dispex is not a function\n");
526 function
= function_from_jsdisp(jsthis
->u
.jsdisp
);
528 assert(function
->value_proc
!= NULL
);
529 return invoke_value_proc(ctx
, function
, NULL
, flags
, argc
, argv
, r
);
532 HRESULT
Function_get_value(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
539 hres
= function_to_string(function_from_jsdisp(jsthis
), &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 static void Function_destructor(jsdisp_t
*dispex
)
573 FunctionInstance
*This
= function_from_jsdisp(dispex
);
576 release_bytecode(This
->code
);
577 if(This
->scope_chain
)
578 scope_release(This
->scope_chain
);
582 static const builtin_prop_t Function_props
[] = {
583 {applyW
, Function_apply
, PROPF_METHOD
|2},
584 {argumentsW
, NULL
, 0, Function_get_arguments
, builtin_set_const
},
585 {callW
, Function_call
, PROPF_METHOD
|1},
586 {lengthW
, NULL
, 0, Function_get_length
, Function_set_length
},
587 {toStringW
, Function_toString
, PROPF_METHOD
}
590 static const builtin_info_t Function_info
= {
592 DEFAULT_FUNCTION_VALUE
,
593 sizeof(Function_props
)/sizeof(*Function_props
),
599 static const builtin_prop_t FunctionInst_props
[] = {
600 {argumentsW
, NULL
, 0, Function_get_arguments
, builtin_set_const
},
601 {lengthW
, NULL
, 0, Function_get_length
, Function_set_length
}
604 static const builtin_info_t FunctionInst_info
= {
606 DEFAULT_FUNCTION_VALUE
,
607 sizeof(FunctionInst_props
)/sizeof(*FunctionInst_props
),
613 static HRESULT
create_function(script_ctx_t
*ctx
, const builtin_info_t
*builtin_info
, DWORD flags
,
614 BOOL funcprot
, jsdisp_t
*prototype
, FunctionInstance
**ret
)
616 FunctionInstance
*function
;
619 function
= heap_alloc_zero(sizeof(FunctionInstance
));
621 return E_OUTOFMEMORY
;
624 hres
= init_dispex(&function
->dispex
, ctx
, builtin_info
, prototype
);
625 else if(builtin_info
)
626 hres
= init_dispex_from_constr(&function
->dispex
, ctx
, builtin_info
, ctx
->function_constr
);
628 hres
= init_dispex_from_constr(&function
->dispex
, ctx
, &FunctionInst_info
, ctx
->function_constr
);
634 function
->flags
= flags
;
635 function
->length
= flags
& PROPF_ARGMASK
;
641 static inline HRESULT
set_prototype(script_ctx_t
*ctx
, jsdisp_t
*dispex
, jsdisp_t
*prototype
)
643 return jsdisp_propput_dontenum(dispex
, prototypeW
, jsval_obj(prototype
));
646 HRESULT
create_builtin_function(script_ctx_t
*ctx
, builtin_invoke_t value_proc
, const WCHAR
*name
,
647 const builtin_info_t
*builtin_info
, DWORD flags
, jsdisp_t
*prototype
, jsdisp_t
**ret
)
649 FunctionInstance
*function
;
652 hres
= create_function(ctx
, builtin_info
, flags
, FALSE
, NULL
, &function
);
657 hres
= jsdisp_propput_const(&function
->dispex
, lengthW
, jsval_number(function
->length
));
659 hres
= set_prototype(ctx
, &function
->dispex
, prototype
);
661 jsdisp_release(&function
->dispex
);
665 function
->value_proc
= value_proc
;
666 function
->name
= name
;
668 *ret
= &function
->dispex
;
672 static HRESULT
set_constructor_prop(script_ctx_t
*ctx
, jsdisp_t
*constr
, jsdisp_t
*prot
)
674 static const WCHAR constructorW
[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
676 return jsdisp_propput_dontenum(prot
, constructorW
, jsval_obj(constr
));
679 HRESULT
create_builtin_constructor(script_ctx_t
*ctx
, builtin_invoke_t value_proc
, const WCHAR
*name
,
680 const builtin_info_t
*builtin_info
, DWORD flags
, jsdisp_t
*prototype
, jsdisp_t
**ret
)
685 hres
= create_builtin_function(ctx
, value_proc
, name
, builtin_info
, flags
, prototype
, &constr
);
689 hres
= set_constructor_prop(ctx
, constr
, prototype
);
691 jsdisp_release(constr
);
699 HRESULT
create_source_function(script_ctx_t
*ctx
, bytecode_t
*code
, function_code_t
*func_code
,
700 scope_chain_t
*scope_chain
, jsdisp_t
**ret
)
702 FunctionInstance
*function
;
706 hres
= create_object(ctx
, NULL
, &prototype
);
710 hres
= create_function(ctx
, NULL
, PROPF_CONSTR
, FALSE
, NULL
, &function
);
711 if(SUCCEEDED(hres
)) {
712 hres
= set_prototype(ctx
, &function
->dispex
, prototype
);
714 hres
= set_constructor_prop(ctx
, &function
->dispex
, prototype
);
716 jsdisp_release(&function
->dispex
);
718 jsdisp_release(prototype
);
723 scope_addref(scope_chain
);
724 function
->scope_chain
= scope_chain
;
727 bytecode_addref(code
);
728 function
->code
= code
;
729 function
->func_code
= func_code
;
730 function
->length
= function
->func_code
->param_cnt
;
732 *ret
= &function
->dispex
;
736 static HRESULT
construct_function(script_ctx_t
*ctx
, unsigned argc
, jsval_t
*argv
, IDispatch
**ret
)
738 WCHAR
*str
= NULL
, *ptr
;
739 unsigned len
= 0, i
= 0;
742 jsstr_t
**params
= NULL
;
746 static const WCHAR function_anonymousW
[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
747 static const WCHAR function_beginW
[] = {')',' ','{','\n'};
748 static const WCHAR function_endW
[] = {'\n','}',0};
751 params
= heap_alloc(argc
*sizeof(*params
));
753 return E_OUTOFMEMORY
;
756 len
= (argc
-2)*2; /* separating commas */
757 for(i
=0; i
< argc
; i
++) {
758 hres
= to_string(ctx
, argv
[i
], params
+i
);
761 len
+= jsstr_length(params
[i
]);
765 if(SUCCEEDED(hres
)) {
766 len
+= (sizeof(function_anonymousW
) + sizeof(function_beginW
) + sizeof(function_endW
)) / sizeof(WCHAR
);
767 str
= heap_alloc(len
*sizeof(WCHAR
));
769 memcpy(str
, function_anonymousW
, sizeof(function_anonymousW
));
770 ptr
= str
+ sizeof(function_anonymousW
)/sizeof(WCHAR
);
773 ptr
+= jsstr_flush(params
[j
], ptr
);
780 memcpy(ptr
, function_beginW
, sizeof(function_beginW
));
781 ptr
+= sizeof(function_beginW
)/sizeof(WCHAR
);
783 ptr
+= jsstr_flush(params
[argc
-1], ptr
);
784 memcpy(ptr
, function_endW
, sizeof(function_endW
));
786 TRACE("%s\n", debugstr_w(str
));
788 hres
= E_OUTOFMEMORY
;
793 jsstr_release(params
[--i
]);
798 hres
= compile_script(ctx
, str
, NULL
, NULL
, FALSE
, FALSE
, &code
);
803 if(code
->global_code
.func_cnt
!= 1 || code
->global_code
.var_cnt
!= 1) {
804 ERR("Invalid parser result!\n");
805 release_bytecode(code
);
809 hres
= create_source_function(ctx
, code
, code
->global_code
.funcs
, NULL
, &function
);
810 release_bytecode(code
);
814 *ret
= to_disp(function
);
818 static HRESULT
FunctionConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
826 case DISPATCH_METHOD
:
827 case DISPATCH_CONSTRUCT
: {
830 hres
= construct_function(ctx
, argc
, argv
, &ret
);
834 *r
= jsval_disp(ret
);
838 FIXME("unimplemented flags %x\n", flags
);
845 static HRESULT
FunctionProt_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
852 HRESULT
init_function_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
)
854 FunctionInstance
*prot
, *constr
;
857 static const WCHAR FunctionW
[] = {'F','u','n','c','t','i','o','n',0};
859 hres
= create_function(ctx
, &Function_info
, PROPF_CONSTR
, TRUE
, object_prototype
, &prot
);
863 prot
->value_proc
= FunctionProt_value
;
864 prot
->name
= prototypeW
;
866 hres
= create_function(ctx
, &FunctionInst_info
, PROPF_CONSTR
|1, TRUE
, &prot
->dispex
, &constr
);
867 if(SUCCEEDED(hres
)) {
868 constr
->value_proc
= FunctionConstr_value
;
869 constr
->name
= FunctionW
;
870 hres
= set_prototype(ctx
, &constr
->dispex
, &prot
->dispex
);
872 hres
= set_constructor_prop(ctx
, &constr
->dispex
, &prot
->dispex
);
874 jsdisp_release(&constr
->dispex
);
876 jsdisp_release(&prot
->dispex
);
880 ctx
->function_constr
= &constr
->dispex
;