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
);
30 builtin_invoke_t value_proc
;
33 scope_chain_t
*scope_chain
;
35 function_code_t
*func_code
;
41 FunctionInstance
*function
;
47 static inline FunctionInstance
*function_from_jsdisp(jsdisp_t
*jsdisp
)
49 return CONTAINING_RECORD(jsdisp
, FunctionInstance
, dispex
);
52 static inline FunctionInstance
*function_from_vdisp(vdisp_t
*vdisp
)
54 return function_from_jsdisp(vdisp
->u
.jsdisp
);
57 static inline FunctionInstance
*function_this(vdisp_t
*jsthis
)
59 return is_vclass(jsthis
, JSCLASS_FUNCTION
) ? function_from_vdisp(jsthis
) : NULL
;
62 static inline ArgumentsInstance
*arguments_from_jsdisp(jsdisp_t
*jsdisp
)
64 return CONTAINING_RECORD(jsdisp
, ArgumentsInstance
, jsdisp
);
67 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
69 static const WCHAR lengthW
[] = {'l','e','n','g','t','h',0};
70 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
71 static const WCHAR applyW
[] = {'a','p','p','l','y',0};
72 static const WCHAR callW
[] = {'c','a','l','l',0};
73 static const WCHAR argumentsW
[] = {'a','r','g','u','m','e','n','t','s',0};
75 static HRESULT
Arguments_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
82 static void Arguments_destructor(jsdisp_t
*jsdisp
)
84 ArgumentsInstance
*arguments
= arguments_from_jsdisp(jsdisp
);
86 TRACE("(%p)\n", arguments
);
90 for(i
= 0; i
< arguments
->argc
; i
++)
91 jsval_release(arguments
->buf
[i
]);
92 heap_free(arguments
->buf
);
95 jsdisp_release(&arguments
->function
->dispex
);
99 static unsigned Arguments_idx_length(jsdisp_t
*jsdisp
)
101 ArgumentsInstance
*arguments
= arguments_from_jsdisp(jsdisp
);
102 return arguments
->argc
;
105 static jsval_t
*get_argument_ref(ArgumentsInstance
*arguments
, unsigned idx
)
108 return arguments
->buf
+ idx
;
109 if(arguments
->frame
->base_scope
->frame
|| idx
>= arguments
->frame
->function
->param_cnt
)
110 return arguments
->jsdisp
.ctx
->stack
+ arguments
->frame
->arguments_off
+ idx
;
114 static HRESULT
Arguments_idx_get(jsdisp_t
*jsdisp
, unsigned idx
, jsval_t
*r
)
116 ArgumentsInstance
*arguments
= arguments_from_jsdisp(jsdisp
);
119 TRACE("%p[%u]\n", arguments
, idx
);
121 if((ref
= get_argument_ref(arguments
, idx
)))
122 return jsval_copy(*ref
, r
);
124 /* FIXME: Accessing by name won't work for duplicated argument names */
125 return jsdisp_propget_name(arguments
->frame
->base_scope
->jsobj
, arguments
->function
->func_code
->params
[idx
], r
);
128 static HRESULT
Arguments_idx_put(jsdisp_t
*jsdisp
, unsigned idx
, jsval_t val
)
130 ArgumentsInstance
*arguments
= arguments_from_jsdisp(jsdisp
);
134 TRACE("%p[%u] = %s\n", arguments
, idx
, debugstr_jsval(val
));
136 if((ref
= get_argument_ref(arguments
, idx
))) {
138 hres
= jsval_copy(val
, ©
);
147 /* FIXME: Accessing by name won't work for duplicated argument names */
148 return jsdisp_propput_name(arguments
->frame
->base_scope
->jsobj
, arguments
->function
->func_code
->params
[idx
], val
);
151 static const builtin_info_t Arguments_info
= {
153 {NULL
, Arguments_value
, 0},
155 Arguments_destructor
,
157 Arguments_idx_length
,
162 HRESULT
setup_arguments_object(script_ctx_t
*ctx
, call_frame_t
*frame
)
164 ArgumentsInstance
*args
;
167 static const WCHAR caleeW
[] = {'c','a','l','l','e','e',0};
169 args
= heap_alloc_zero(sizeof(*args
));
171 return E_OUTOFMEMORY
;
173 hres
= init_dispex_from_constr(&args
->jsdisp
, ctx
, &Arguments_info
, ctx
->object_constr
);
179 args
->function
= function_from_jsdisp(jsdisp_addref(frame
->function_instance
));
180 args
->argc
= frame
->argc
;
183 hres
= jsdisp_define_data_property(&args
->jsdisp
, lengthW
, PROPF_WRITABLE
| PROPF_CONFIGURABLE
,
184 jsval_number(args
->argc
));
186 hres
= jsdisp_define_data_property(&args
->jsdisp
, caleeW
, PROPF_WRITABLE
| PROPF_CONFIGURABLE
,
187 jsval_obj(&args
->function
->dispex
));
189 hres
= jsdisp_propput(frame
->base_scope
->jsobj
, argumentsW
, PROPF_WRITABLE
, jsval_obj(&args
->jsdisp
));
191 jsdisp_release(&args
->jsdisp
);
195 frame
->arguments_obj
= &args
->jsdisp
;
199 void detach_arguments_object(jsdisp_t
*args_disp
)
201 ArgumentsInstance
*arguments
= arguments_from_jsdisp(args_disp
);
202 call_frame_t
*frame
= arguments
->frame
;
203 const BOOL on_stack
= frame
->base_scope
->frame
== frame
;
206 /* Reset arguments value to cut the reference cycle. Note that since all activation contexts have
207 * their own arguments property, it's impossible to use prototype's one during name lookup */
208 jsdisp_propput_name(frame
->base_scope
->jsobj
, argumentsW
, jsval_undefined());
209 arguments
->frame
= NULL
;
211 /* Don't bother coppying arguments if call frame holds the last reference. */
212 if(arguments
->jsdisp
.ref
> 1) {
213 arguments
->buf
= heap_alloc(arguments
->argc
* sizeof(*arguments
->buf
));
217 for(i
= 0; i
< arguments
->argc
; i
++) {
218 if(on_stack
|| i
>= frame
->function
->param_cnt
)
219 hres
= jsval_copy(arguments
->jsdisp
.ctx
->stack
[frame
->arguments_off
+ i
], arguments
->buf
+i
);
221 hres
= jsdisp_propget_name(frame
->base_scope
->jsobj
, frame
->function
->params
[i
], arguments
->buf
+i
);
223 arguments
->buf
[i
] = jsval_undefined();
226 ERR("out of memory\n");
231 jsdisp_release(frame
->arguments_obj
);
234 static HRESULT
invoke_source(script_ctx_t
*ctx
, FunctionInstance
*function
, IDispatch
*this_obj
, unsigned argc
, jsval_t
*argv
,
235 BOOL is_constructor
, BOOL caller_execs_source
, jsval_t
*r
)
238 DWORD exec_flags
= 0;
241 if(ctx
->state
== SCRIPTSTATE_UNINITIALIZED
|| ctx
->state
== SCRIPTSTATE_CLOSED
) {
242 WARN("Script engine state does not allow running code.\n");
246 if(!function
->func_code
) {
247 FIXME("no source\n");
251 hres
= create_dispex(ctx
, NULL
, NULL
, &var_disp
);
255 if(caller_execs_source
)
256 exec_flags
|= EXEC_RETURN_TO_INTERP
;
258 exec_flags
|= EXEC_CONSTRUCTOR
;
259 hres
= exec_source(ctx
, exec_flags
, function
->code
, function
->func_code
, function
->scope_chain
, this_obj
,
260 &function
->dispex
, var_disp
, argc
, argv
, r
);
262 jsdisp_release(var_disp
);
266 static HRESULT
invoke_value_proc(script_ctx_t
*ctx
, FunctionInstance
*function
, IDispatch
*this_disp
, WORD flags
,
267 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
273 set_disp(&vthis
, this_disp
);
274 else if(ctx
->host_global
)
275 set_disp(&vthis
, ctx
->host_global
);
277 set_jsdisp(&vthis
, ctx
->global
);
279 hres
= function
->value_proc(ctx
, &vthis
, flags
, argc
, argv
, r
);
281 vdisp_release(&vthis
);
285 static HRESULT
call_function(script_ctx_t
*ctx
, FunctionInstance
*function
, IDispatch
*this_obj
,
286 unsigned argc
, jsval_t
*argv
, BOOL caller_execs_source
, jsval_t
*r
)
288 if(function
->value_proc
)
289 return invoke_value_proc(ctx
, function
, this_obj
, DISPATCH_METHOD
, argc
, argv
, r
);
291 return invoke_source(ctx
, function
, this_obj
, argc
, argv
, FALSE
, caller_execs_source
, r
);
294 static HRESULT
function_to_string(FunctionInstance
*function
, jsstr_t
**ret
)
298 static const WCHAR native_prefixW
[] = {'\n','f','u','n','c','t','i','o','n',' '};
299 static const WCHAR native_suffixW
[] =
300 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
302 if(function
->value_proc
) {
306 name_len
= strlenW(function
->name
);
307 str
= jsstr_alloc_buf(ARRAY_SIZE(native_prefixW
) + ARRAY_SIZE(native_suffixW
) + name_len
, &ptr
);
309 return E_OUTOFMEMORY
;
311 memcpy(ptr
, native_prefixW
, sizeof(native_prefixW
));
312 memcpy(ptr
+= ARRAY_SIZE(native_prefixW
), function
->name
, name_len
*sizeof(WCHAR
));
313 memcpy(ptr
+ name_len
, native_suffixW
, sizeof(native_suffixW
));
315 str
= jsstr_alloc_len(function
->func_code
->source
, function
->func_code
->source_len
);
317 return E_OUTOFMEMORY
;
324 HRESULT
Function_invoke(jsdisp_t
*func_this
, IDispatch
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
326 const BOOL caller_execs_source
= (flags
& DISPATCH_JSCRIPT_CALLEREXECSSOURCE
) != 0;
327 FunctionInstance
*function
;
329 TRACE("func %p this %p\n", func_this
, jsthis
);
331 assert(is_class(func_this
, JSCLASS_FUNCTION
));
332 function
= function_from_jsdisp(func_this
);
334 flags
&= ~DISPATCH_JSCRIPT_INTERNAL_MASK
;
335 if(function
->value_proc
)
336 return invoke_value_proc(function
->dispex
.ctx
, function
, jsthis
, flags
, argc
, argv
, r
);
338 if(flags
== DISPATCH_CONSTRUCT
) {
342 hres
= create_object(function
->dispex
.ctx
, &function
->dispex
, &this_obj
);
346 hres
= invoke_source(function
->dispex
.ctx
, function
, to_disp(this_obj
), argc
, argv
, TRUE
, caller_execs_source
, r
);
347 jsdisp_release(this_obj
);
351 assert(flags
== DISPATCH_METHOD
);
352 return invoke_source(function
->dispex
.ctx
, function
, jsthis
, argc
, argv
, FALSE
, caller_execs_source
, r
);
355 static HRESULT
Function_get_length(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
357 TRACE("%p\n", jsthis
);
359 *r
= jsval_number(function_from_jsdisp(jsthis
)->length
);
363 static HRESULT
Function_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
366 FunctionInstance
*function
;
372 if(!(function
= function_this(jsthis
)))
373 return throw_type_error(ctx
, JS_E_FUNCTION_EXPECTED
, NULL
);
375 hres
= function_to_string(function
, &str
);
380 *r
= jsval_string(str
);
386 static HRESULT
array_to_args(script_ctx_t
*ctx
, jsdisp_t
*arg_array
, unsigned *argc
, jsval_t
**ret
)
392 hres
= jsdisp_propget_name(arg_array
, lengthW
, &val
);
396 hres
= to_uint32(ctx
, val
, &length
);
401 argv
= heap_alloc(length
* sizeof(*argv
));
403 return E_OUTOFMEMORY
;
405 for(i
=0; i
<length
; i
++) {
406 hres
= jsdisp_get_idx(arg_array
, i
, argv
+i
);
407 if(hres
== DISP_E_UNKNOWNNAME
) {
408 argv
[i
] = jsval_undefined();
409 }else if(FAILED(hres
)) {
411 jsval_release(argv
[i
]);
422 static HRESULT
Function_apply(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
424 FunctionInstance
*function
;
425 jsval_t
*args
= NULL
;
427 IDispatch
*this_obj
= NULL
;
432 if(!(function
= function_this(jsthis
)) && (jsthis
->flags
& VDISP_JSDISP
))
433 return throw_type_error(ctx
, JS_E_FUNCTION_EXPECTED
, NULL
);
436 if(!is_undefined(argv
[0]) && !is_null(argv
[0])) {
437 hres
= to_object(ctx
, argv
[0], &this_obj
);
444 jsdisp_t
*arg_array
= NULL
;
446 if(is_object_instance(argv
[1])) {
447 arg_array
= iface_to_jsdisp(get_object(argv
[1]));
449 (!is_class(arg_array
, JSCLASS_ARRAY
) && !is_class(arg_array
, JSCLASS_ARGUMENTS
) )) {
450 jsdisp_release(arg_array
);
456 hres
= array_to_args(ctx
, arg_array
, &cnt
, &args
);
457 jsdisp_release(arg_array
);
459 FIXME("throw TypeError\n");
464 if(SUCCEEDED(hres
)) {
466 hres
= call_function(ctx
, function
, this_obj
, cnt
, args
, (flags
& DISPATCH_JSCRIPT_CALLEREXECSSOURCE
) != 0, r
);
469 hres
= disp_call_value(ctx
, jsthis
->u
.disp
, this_obj
, DISPATCH_METHOD
, cnt
, args
, &res
);
470 if(SUCCEEDED(hres
)) {
480 IDispatch_Release(this_obj
);
481 for(i
=0; i
< cnt
; i
++)
482 jsval_release(args
[i
]);
487 static HRESULT
Function_call(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
490 FunctionInstance
*function
;
491 IDispatch
*this_obj
= NULL
;
497 if(!(function
= function_this(jsthis
)))
498 return throw_type_error(ctx
, JS_E_FUNCTION_EXPECTED
, NULL
);
501 if(!is_undefined(argv
[0]) && !is_null(argv
[0])) {
502 hres
= to_object(ctx
, argv
[0], &this_obj
);
510 hres
= call_function(ctx
, function
, this_obj
, cnt
, argv
+1, (flags
& DISPATCH_JSCRIPT_CALLEREXECSSOURCE
) != 0, r
);
513 IDispatch_Release(this_obj
);
517 HRESULT
Function_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
520 FunctionInstance
*function
;
524 if(!is_vclass(jsthis
, JSCLASS_FUNCTION
)) {
525 ERR("dispex is not a function\n");
529 function
= function_from_jsdisp(jsthis
->u
.jsdisp
);
531 assert(function
->value_proc
!= NULL
);
532 return invoke_value_proc(ctx
, function
, NULL
, flags
, argc
, argv
, r
);
535 HRESULT
Function_get_value(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
542 hres
= function_to_string(function_from_jsdisp(jsthis
), &str
);
546 *r
= jsval_string(str
);
550 static HRESULT
Function_get_arguments(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
552 FunctionInstance
*function
= function_from_jsdisp(jsthis
);
558 for(frame
= ctx
->call_ctx
; frame
; frame
= frame
->prev_frame
) {
559 if(frame
->function_instance
== &function
->dispex
) {
560 if(!frame
->arguments_obj
) {
561 hres
= setup_arguments_object(ctx
, frame
);
565 *r
= jsval_obj(jsdisp_addref(frame
->arguments_obj
));
574 static void Function_destructor(jsdisp_t
*dispex
)
576 FunctionInstance
*This
= function_from_jsdisp(dispex
);
579 release_bytecode(This
->code
);
580 if(This
->scope_chain
)
581 scope_release(This
->scope_chain
);
585 static const builtin_prop_t Function_props
[] = {
586 {applyW
, Function_apply
, PROPF_METHOD
|2},
587 {argumentsW
, NULL
, 0, Function_get_arguments
},
588 {callW
, Function_call
, PROPF_METHOD
|1},
589 {lengthW
, NULL
, 0, Function_get_length
},
590 {toStringW
, Function_toString
, PROPF_METHOD
}
593 static const builtin_info_t Function_info
= {
595 DEFAULT_FUNCTION_VALUE
,
596 ARRAY_SIZE(Function_props
),
602 static const builtin_prop_t FunctionInst_props
[] = {
603 {argumentsW
, NULL
, 0, Function_get_arguments
},
604 {lengthW
, NULL
, 0, Function_get_length
}
607 static const builtin_info_t FunctionInst_info
= {
609 DEFAULT_FUNCTION_VALUE
,
610 ARRAY_SIZE(FunctionInst_props
),
616 static HRESULT
create_function(script_ctx_t
*ctx
, const builtin_info_t
*builtin_info
, DWORD flags
,
617 BOOL funcprot
, jsdisp_t
*prototype
, FunctionInstance
**ret
)
619 FunctionInstance
*function
;
622 function
= heap_alloc_zero(sizeof(FunctionInstance
));
624 return E_OUTOFMEMORY
;
627 hres
= init_dispex(&function
->dispex
, ctx
, builtin_info
, prototype
);
628 else if(builtin_info
)
629 hres
= init_dispex_from_constr(&function
->dispex
, ctx
, builtin_info
, ctx
->function_constr
);
631 hres
= init_dispex_from_constr(&function
->dispex
, ctx
, &FunctionInst_info
, ctx
->function_constr
);
637 function
->flags
= flags
;
638 function
->length
= flags
& PROPF_ARGMASK
;
644 HRESULT
create_builtin_function(script_ctx_t
*ctx
, builtin_invoke_t value_proc
, const WCHAR
*name
,
645 const builtin_info_t
*builtin_info
, DWORD flags
, jsdisp_t
*prototype
, jsdisp_t
**ret
)
647 FunctionInstance
*function
;
650 hres
= create_function(ctx
, builtin_info
, flags
, FALSE
, NULL
, &function
);
655 hres
= jsdisp_define_data_property(&function
->dispex
, lengthW
, 0,
656 jsval_number(function
->length
));
658 hres
= jsdisp_define_data_property(&function
->dispex
, prototypeW
, 0, jsval_obj(prototype
));
660 jsdisp_release(&function
->dispex
);
664 function
->value_proc
= value_proc
;
665 function
->name
= name
;
667 *ret
= &function
->dispex
;
671 static HRESULT
set_constructor_prop(script_ctx_t
*ctx
, jsdisp_t
*constr
, jsdisp_t
*prot
)
673 static const WCHAR constructorW
[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
675 return jsdisp_define_data_property(prot
, constructorW
, PROPF_WRITABLE
| PROPF_CONFIGURABLE
,
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
= jsdisp_define_data_property(&function
->dispex
, prototypeW
, PROPF_WRITABLE
,
713 jsval_obj(prototype
));
715 hres
= set_constructor_prop(ctx
, &function
->dispex
, prototype
);
717 jsdisp_release(&function
->dispex
);
719 jsdisp_release(prototype
);
724 scope_addref(scope_chain
);
725 function
->scope_chain
= scope_chain
;
728 bytecode_addref(code
);
729 function
->code
= code
;
730 function
->func_code
= func_code
;
731 function
->length
= function
->func_code
->param_cnt
;
733 *ret
= &function
->dispex
;
737 static HRESULT
construct_function(script_ctx_t
*ctx
, unsigned argc
, jsval_t
*argv
, IDispatch
**ret
)
739 WCHAR
*str
= NULL
, *ptr
;
740 unsigned len
= 0, i
= 0;
743 jsstr_t
**params
= NULL
;
747 static const WCHAR function_anonymousW
[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
748 static const WCHAR function_beginW
[] = {')',' ','{','\n'};
749 static const WCHAR function_endW
[] = {'\n','}',0};
752 params
= heap_alloc(argc
*sizeof(*params
));
754 return E_OUTOFMEMORY
;
757 len
= (argc
-2)*2; /* separating commas */
758 for(i
=0; i
< argc
; i
++) {
759 hres
= to_string(ctx
, argv
[i
], params
+i
);
762 len
+= jsstr_length(params
[i
]);
766 if(SUCCEEDED(hres
)) {
767 len
+= ARRAY_SIZE(function_anonymousW
) + ARRAY_SIZE(function_beginW
) + ARRAY_SIZE(function_endW
);
768 str
= heap_alloc(len
*sizeof(WCHAR
));
770 memcpy(str
, function_anonymousW
, sizeof(function_anonymousW
));
771 ptr
= str
+ ARRAY_SIZE(function_anonymousW
);
774 ptr
+= jsstr_flush(params
[j
], ptr
);
781 memcpy(ptr
, function_beginW
, sizeof(function_beginW
));
782 ptr
+= ARRAY_SIZE(function_beginW
);
784 ptr
+= jsstr_flush(params
[argc
-1], ptr
);
785 memcpy(ptr
, function_endW
, sizeof(function_endW
));
787 TRACE("%s\n", debugstr_w(str
));
789 hres
= E_OUTOFMEMORY
;
794 jsstr_release(params
[--i
]);
799 hres
= compile_script(ctx
, str
, NULL
, NULL
, FALSE
, FALSE
, &code
);
804 if(code
->global_code
.func_cnt
!= 1 || code
->global_code
.var_cnt
!= 1) {
805 ERR("Invalid parser result!\n");
806 release_bytecode(code
);
810 hres
= create_source_function(ctx
, code
, code
->global_code
.funcs
, NULL
, &function
);
811 release_bytecode(code
);
815 *ret
= to_disp(function
);
819 static HRESULT
FunctionConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
827 case DISPATCH_METHOD
:
828 case DISPATCH_CONSTRUCT
: {
831 hres
= construct_function(ctx
, argc
, argv
, &ret
);
835 *r
= jsval_disp(ret
);
839 FIXME("unimplemented flags %x\n", flags
);
846 static HRESULT
FunctionProt_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
853 HRESULT
init_function_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
)
855 FunctionInstance
*prot
, *constr
;
858 static const WCHAR FunctionW
[] = {'F','u','n','c','t','i','o','n',0};
860 hres
= create_function(ctx
, &Function_info
, PROPF_CONSTR
, TRUE
, object_prototype
, &prot
);
864 prot
->value_proc
= FunctionProt_value
;
865 prot
->name
= prototypeW
;
867 hres
= create_function(ctx
, &FunctionInst_info
, PROPF_CONSTR
|1, TRUE
, &prot
->dispex
, &constr
);
868 if(SUCCEEDED(hres
)) {
869 constr
->value_proc
= FunctionConstr_value
;
870 constr
->name
= FunctionW
;
871 hres
= jsdisp_define_data_property(&constr
->dispex
, prototypeW
, 0, jsval_obj(&prot
->dispex
));
873 hres
= set_constructor_prop(ctx
, &constr
->dispex
, &prot
->dispex
);
875 jsdisp_release(&constr
->dispex
);
877 jsdisp_release(&prot
->dispex
);
881 ctx
->function_constr
= &constr
->dispex
;