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
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
28 builtin_invoke_t value_proc
;
31 source_elements_t
*source
;
32 parameter_t
*parameters
;
33 scope_chain_t
*scope_chain
;
40 static inline FunctionInstance
*function_from_vdisp(vdisp_t
*vdisp
)
42 return (FunctionInstance
*)vdisp
->u
.jsdisp
;
45 static inline FunctionInstance
*function_this(vdisp_t
*jsthis
)
47 return is_vclass(jsthis
, JSCLASS_FUNCTION
) ? function_from_vdisp(jsthis
) : NULL
;
50 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
52 static const WCHAR lengthW
[] = {'l','e','n','g','t','h',0};
53 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
54 static const WCHAR applyW
[] = {'a','p','p','l','y',0};
55 static const WCHAR callW
[] = {'c','a','l','l',0};
57 static IDispatch
*get_this(DISPPARAMS
*dp
)
61 for(i
=0; i
< dp
->cNamedArgs
; i
++) {
62 if(dp
->rgdispidNamedArgs
[i
] == DISPID_THIS
) {
63 if(V_VT(dp
->rgvarg
+i
) == VT_DISPATCH
)
64 return V_DISPATCH(dp
->rgvarg
+i
);
66 WARN("This is not VT_DISPATCH\n");
71 TRACE("no this passed\n");
75 static HRESULT
init_parameters(DispatchEx
*var_disp
, FunctionInstance
*function
, DISPPARAMS
*dp
,
76 jsexcept_t
*ei
, IServiceProvider
*caller
)
83 V_VT(&var_empty
) = VT_EMPTY
;
86 for(param
= function
->parameters
; param
; param
= param
->next
) {
87 hres
= jsdisp_propput_name(var_disp
, param
->identifier
,
88 i
< cargs
? get_arg(dp
,i
) : &var_empty
, ei
, caller
);
98 static HRESULT
Arguments_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
99 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
105 static const builtin_info_t Arguments_info
= {
107 {NULL
, Arguments_value
, 0},
113 static HRESULT
create_arguments(script_ctx_t
*ctx
, IDispatch
*calee
, DISPPARAMS
*dp
,
114 jsexcept_t
*ei
, IServiceProvider
*caller
, DispatchEx
**ret
)
121 static const WCHAR caleeW
[] = {'c','a','l','l','e','e',0};
123 args
= heap_alloc_zero(sizeof(DispatchEx
));
125 return E_OUTOFMEMORY
;
127 hres
= init_dispex_from_constr(args
, ctx
, &Arguments_info
, ctx
->object_constr
);
133 for(i
=0; i
< arg_cnt(dp
); i
++) {
134 hres
= jsdisp_propput_idx(args
, i
, get_arg(dp
,i
), ei
, caller
);
139 if(SUCCEEDED(hres
)) {
141 V_I4(&var
) = arg_cnt(dp
);
142 hres
= jsdisp_propput_name(args
, lengthW
, &var
, ei
, caller
);
144 if(SUCCEEDED(hres
)) {
145 V_VT(&var
) = VT_DISPATCH
;
146 V_DISPATCH(&var
) = calee
;
147 hres
= jsdisp_propput_name(args
, caleeW
, &var
, ei
, caller
);
152 jsdisp_release(args
);
160 static HRESULT
create_var_disp(script_ctx_t
*ctx
, FunctionInstance
*function
, DISPPARAMS
*dp
, jsexcept_t
*ei
,
161 IServiceProvider
*caller
, DispatchEx
**ret
)
163 DispatchEx
*var_disp
, *arg_disp
;
166 static const WCHAR argumentsW
[] = {'a','r','g','u','m','e','n','t','s',0};
168 hres
= create_dispex(ctx
, NULL
, NULL
, &var_disp
);
172 hres
= create_arguments(ctx
, (IDispatch
*)_IDispatchEx_(&function
->dispex
),
173 dp
, ei
, caller
, &arg_disp
);
174 if(SUCCEEDED(hres
)) {
177 V_VT(&var
) = VT_DISPATCH
;
178 V_DISPATCH(&var
) = (IDispatch
*)_IDispatchEx_(arg_disp
);
179 hres
= jsdisp_propput_name(var_disp
, argumentsW
, &var
, ei
, caller
);
180 jsdisp_release(arg_disp
);
184 hres
= init_parameters(var_disp
, function
, dp
, ei
, caller
);
186 jsdisp_release(var_disp
);
194 static HRESULT
invoke_source(script_ctx_t
*ctx
, FunctionInstance
*function
, IDispatch
*this_obj
, DISPPARAMS
*dp
,
195 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
197 DispatchEx
*var_disp
;
198 exec_ctx_t
*exec_ctx
;
199 scope_chain_t
*scope
;
202 if(!function
->source
) {
203 FIXME("no source\n");
207 hres
= create_var_disp(ctx
, function
, dp
, ei
, caller
, &var_disp
);
211 hres
= scope_push(function
->scope_chain
, var_disp
, &scope
);
212 if(SUCCEEDED(hres
)) {
213 hres
= create_exec_ctx(ctx
, this_obj
, var_disp
, scope
, &exec_ctx
);
214 scope_release(scope
);
216 jsdisp_release(var_disp
);
220 hres
= exec_source(exec_ctx
, function
->parser
, function
->source
, EXECT_FUNCTION
, ei
, retv
);
221 exec_release(exec_ctx
);
226 static HRESULT
invoke_constructor(script_ctx_t
*ctx
, FunctionInstance
*function
, DISPPARAMS
*dp
,
227 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
229 DispatchEx
*this_obj
;
233 hres
= create_object(ctx
, &function
->dispex
, &this_obj
);
237 hres
= invoke_source(ctx
, function
, (IDispatch
*)_IDispatchEx_(this_obj
), dp
, &var
, ei
, caller
);
239 jsdisp_release(this_obj
);
243 V_VT(retv
) = VT_DISPATCH
;
244 if(V_VT(&var
) == VT_DISPATCH
) {
245 jsdisp_release(this_obj
);
246 V_DISPATCH(retv
) = V_DISPATCH(&var
);
249 V_DISPATCH(retv
) = (IDispatch
*)_IDispatchEx_(this_obj
);
254 static HRESULT
invoke_value_proc(script_ctx_t
*ctx
, FunctionInstance
*function
, IDispatch
*this_disp
, WORD flags
, DISPPARAMS
*dp
,
255 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
261 set_disp(&vthis
, this_disp
);
262 else if(ctx
->host_global
)
263 set_disp(&vthis
, ctx
->host_global
);
265 set_jsdisp(&vthis
, ctx
->global
);
267 hres
= function
->value_proc(ctx
, &vthis
, flags
, dp
, retv
, ei
, caller
);
269 vdisp_release(&vthis
);
273 static HRESULT
call_function(script_ctx_t
*ctx
, FunctionInstance
*function
, IDispatch
*this_obj
, DISPPARAMS
*args
,
274 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
276 if(function
->value_proc
)
277 return invoke_value_proc(ctx
, function
, this_obj
, DISPATCH_METHOD
, args
, retv
, ei
, caller
);
279 return invoke_source(ctx
, function
, this_obj
, args
, retv
, ei
, caller
);
282 static HRESULT
function_to_string(FunctionInstance
*function
, BSTR
*ret
)
286 static const WCHAR native_prefixW
[] = {'\n','f','u','n','c','t','i','o','n',' '};
287 static const WCHAR native_suffixW
[] =
288 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
290 if(function
->value_proc
) {
293 name_len
= strlenW(function
->name
);
294 str
= SysAllocStringLen(NULL
, sizeof(native_prefixW
) + name_len
*sizeof(WCHAR
) + sizeof(native_suffixW
));
296 return E_OUTOFMEMORY
;
298 memcpy(str
, native_prefixW
, sizeof(native_prefixW
));
299 memcpy(str
+ sizeof(native_prefixW
)/sizeof(WCHAR
), function
->name
, name_len
*sizeof(WCHAR
));
300 memcpy(str
+ sizeof(native_prefixW
)/sizeof(WCHAR
) + name_len
, native_suffixW
, sizeof(native_suffixW
));
302 str
= SysAllocStringLen(function
->src_str
, function
->src_len
);
304 return E_OUTOFMEMORY
;
311 static HRESULT
Function_length(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
312 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
314 FunctionInstance
*This
= function_from_vdisp(jsthis
);
316 TRACE("%p %d\n", This
, This
->length
);
319 case DISPATCH_PROPERTYGET
:
321 V_I4(retv
) = This
->length
;
324 FIXME("unimplemented flags %x\n", flags
);
331 static HRESULT
Function_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
332 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
334 FunctionInstance
*function
;
340 if(!(function
= function_this(jsthis
)))
341 return throw_type_error(ctx
, ei
, IDS_NOT_FUNC
, NULL
);
343 hres
= function_to_string(function
, &str
);
348 V_VT(retv
) = VT_BSTR
;
356 static HRESULT
array_to_args(script_ctx_t
*ctx
, DispatchEx
*arg_array
, jsexcept_t
*ei
, IServiceProvider
*caller
,
363 hres
= jsdisp_propget_name(arg_array
, lengthW
, &var
, ei
, NULL
/*FIXME*/);
367 hres
= to_uint32(ctx
, &var
, ei
, &length
);
372 argv
= heap_alloc(length
* sizeof(VARIANT
));
374 return E_OUTOFMEMORY
;
376 for(i
=0; i
<length
; i
++) {
377 hres
= jsdisp_propget_idx(arg_array
, i
, argv
+i
, ei
, caller
);
380 VariantClear(argv
+i
);
386 args
->cArgs
= length
;
391 static HRESULT
Function_apply(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
392 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
394 FunctionInstance
*function
;
395 DISPPARAMS args
= {NULL
,NULL
,0,0};
397 IDispatch
*this_obj
= NULL
;
402 if(!(function
= function_this(jsthis
)))
403 return throw_type_error(ctx
, ei
, IDS_NOT_FUNC
, NULL
);
407 VARIANT
*v
= get_arg(dp
,0);
409 if(V_VT(v
) != VT_EMPTY
&& V_VT(v
) != VT_NULL
) {
410 hres
= to_object(ctx
, v
, &this_obj
);
417 DispatchEx
*arg_array
= NULL
;
419 if(V_VT(get_arg(dp
,1)) == VT_DISPATCH
) {
420 arg_array
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(get_arg(dp
,1)));
422 (!is_class(arg_array
, JSCLASS_ARRAY
) && !is_class(arg_array
, JSCLASS_ARGUMENTS
) )) {
423 jsdisp_release(arg_array
);
429 hres
= array_to_args(ctx
, arg_array
, ei
, caller
, &args
);
430 jsdisp_release(arg_array
);
432 FIXME("throw TypeError\n");
437 hres
= call_function(ctx
, function
, this_obj
, &args
, retv
, ei
, caller
);
440 IDispatch_Release(this_obj
);
441 for(i
=0; i
<args
.cArgs
; i
++)
442 VariantClear(args
.rgvarg
+i
);
443 heap_free(args
.rgvarg
);
447 static HRESULT
Function_call(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
448 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
450 FunctionInstance
*function
;
451 DISPPARAMS args
= {NULL
,NULL
,0,0};
452 IDispatch
*this_obj
= NULL
;
458 if(!(function
= function_this(jsthis
)))
459 return throw_type_error(ctx
, ei
, IDS_NOT_FUNC
, NULL
);
463 VARIANT
*v
= get_arg(dp
,0);
465 if(V_VT(v
) != VT_EMPTY
&& V_VT(v
) != VT_NULL
) {
466 hres
= to_object(ctx
, v
, &this_obj
);
475 args
.rgvarg
= dp
->rgvarg
+ dp
->cArgs
- args
.cArgs
-1;
477 hres
= call_function(ctx
, function
, this_obj
, &args
, retv
, ei
, caller
);
480 IDispatch_Release(this_obj
);
484 HRESULT
Function_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
485 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
487 FunctionInstance
*function
;
491 if(!is_vclass(jsthis
, JSCLASS_FUNCTION
)) {
492 ERR("dispex is not a function\n");
496 function
= (FunctionInstance
*)jsthis
->u
.jsdisp
;
499 case DISPATCH_METHOD
:
500 if(function
->value_proc
)
501 return invoke_value_proc(ctx
, function
, get_this(dp
), flags
, dp
, retv
, ei
, caller
);
503 return invoke_source(ctx
, function
, get_this(dp
), dp
, retv
, ei
, caller
);
505 case DISPATCH_PROPERTYGET
: {
509 hres
= function_to_string(function
, &str
);
513 V_VT(retv
) = VT_BSTR
;
518 case DISPATCH_CONSTRUCT
:
519 if(function
->value_proc
)
520 return invoke_value_proc(ctx
, function
, get_this(dp
), flags
, dp
, retv
, ei
, caller
);
522 return invoke_constructor(ctx
, function
, dp
, retv
, ei
, caller
);
525 FIXME("not implemented flags %x\n", flags
);
532 static void Function_destructor(DispatchEx
*dispex
)
534 FunctionInstance
*This
= (FunctionInstance
*)dispex
;
537 parser_release(This
->parser
);
538 if(This
->scope_chain
)
539 scope_release(This
->scope_chain
);
543 static const builtin_prop_t Function_props
[] = {
544 {applyW
, Function_apply
, PROPF_METHOD
|2},
545 {callW
, Function_call
, PROPF_METHOD
|1},
546 {lengthW
, Function_length
, 0},
547 {toStringW
, Function_toString
, PROPF_METHOD
}
550 static const builtin_info_t Function_info
= {
552 {NULL
, Function_value
, 0},
553 sizeof(Function_props
)/sizeof(*Function_props
),
559 static HRESULT
create_function(script_ctx_t
*ctx
, const builtin_info_t
*builtin_info
, DWORD flags
,
560 BOOL funcprot
, DispatchEx
*prototype
, FunctionInstance
**ret
)
562 FunctionInstance
*function
;
565 function
= heap_alloc_zero(sizeof(FunctionInstance
));
567 return E_OUTOFMEMORY
;
570 hres
= init_dispex(&function
->dispex
, ctx
, &Function_info
, prototype
);
571 else if(builtin_info
)
572 hres
= init_dispex_from_constr(&function
->dispex
, ctx
, builtin_info
, ctx
->function_constr
);
574 hres
= init_dispex_from_constr(&function
->dispex
, ctx
, &Function_info
, ctx
->function_constr
);
578 function
->flags
= flags
;
579 function
->length
= flags
& PROPF_ARGMASK
;
585 static HRESULT
set_prototype(script_ctx_t
*ctx
, DispatchEx
*dispex
, DispatchEx
*prototype
)
590 V_VT(&var
) = VT_DISPATCH
;
591 V_DISPATCH(&var
) = (IDispatch
*)_IDispatchEx_(prototype
);
592 memset(&jsexcept
, 0, sizeof(jsexcept
));
594 return jsdisp_propput_name(dispex
, prototypeW
, &var
, &jsexcept
, NULL
/*FIXME*/);
597 HRESULT
create_builtin_function(script_ctx_t
*ctx
, builtin_invoke_t value_proc
, const WCHAR
*name
,
598 const builtin_info_t
*builtin_info
, DWORD flags
, DispatchEx
*prototype
, DispatchEx
**ret
)
600 FunctionInstance
*function
;
603 hres
= create_function(ctx
, builtin_info
, flags
, FALSE
, NULL
, &function
);
607 hres
= set_prototype(ctx
, &function
->dispex
, prototype
);
609 jsdisp_release(&function
->dispex
);
613 function
->value_proc
= value_proc
;
614 function
->name
= name
;
616 *ret
= &function
->dispex
;
620 HRESULT
create_source_function(parser_ctx_t
*ctx
, parameter_t
*parameters
, source_elements_t
*source
,
621 scope_chain_t
*scope_chain
, const WCHAR
*src_str
, DWORD src_len
, DispatchEx
**ret
)
623 FunctionInstance
*function
;
624 DispatchEx
*prototype
;
629 hres
= create_object(ctx
->script
, NULL
, &prototype
);
633 hres
= create_function(ctx
->script
, NULL
, PROPF_CONSTR
, FALSE
, NULL
, &function
);
634 if(SUCCEEDED(hres
)) {
635 hres
= set_prototype(ctx
->script
, &function
->dispex
, prototype
);
637 jsdisp_release(&function
->dispex
);
639 jsdisp_release(prototype
);
643 function
->source
= source
;
644 function
->parameters
= parameters
;
647 scope_addref(scope_chain
);
648 function
->scope_chain
= scope_chain
;
652 function
->parser
= ctx
;
654 for(iter
= parameters
; iter
; iter
= iter
->next
)
656 function
->length
= length
;
658 function
->src_str
= src_str
;
659 function
->src_len
= src_len
;
661 *ret
= &function
->dispex
;
665 static HRESULT
construct_function(script_ctx_t
*ctx
, DISPPARAMS
*dp
, jsexcept_t
*ei
, IDispatch
**ret
)
667 function_expression_t
*expr
;
668 WCHAR
*str
= NULL
, *ptr
;
669 DWORD argc
, len
= 0, l
;
670 parser_ctx_t
*parser
;
671 DispatchEx
*function
;
676 static const WCHAR function_anonymousW
[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
677 static const WCHAR function_beginW
[] = {')',' ','{','\n'};
678 static const WCHAR function_endW
[] = {'\n','}',0};
682 params
= heap_alloc(argc
*sizeof(BSTR
));
684 return E_OUTOFMEMORY
;
687 len
= (argc
-2)*2; /* separating commas */
688 for(i
=0; i
< argc
; i
++) {
689 hres
= to_string(ctx
, get_arg(dp
,i
), ei
, params
+i
);
692 len
+= SysStringLen(params
[i
]);
696 if(SUCCEEDED(hres
)) {
697 len
+= (sizeof(function_anonymousW
) + sizeof(function_beginW
) + sizeof(function_endW
)) / sizeof(WCHAR
);
698 str
= heap_alloc(len
*sizeof(WCHAR
));
700 memcpy(str
, function_anonymousW
, sizeof(function_anonymousW
));
701 ptr
= str
+ sizeof(function_anonymousW
)/sizeof(WCHAR
);
704 l
= SysStringLen(params
[j
]);
705 memcpy(ptr
, params
[j
], l
*sizeof(WCHAR
));
713 memcpy(ptr
, function_beginW
, sizeof(function_beginW
));
714 ptr
+= sizeof(function_beginW
)/sizeof(WCHAR
);
716 l
= SysStringLen(params
[argc
-1]);
717 memcpy(ptr
, params
[argc
-1], l
*sizeof(WCHAR
));
720 memcpy(ptr
, function_endW
, sizeof(function_endW
));
722 TRACE("%s\n", debugstr_w(str
));
724 hres
= E_OUTOFMEMORY
;
729 SysFreeString(params
[i
]);
734 hres
= script_parse(ctx
, str
, NULL
, &parser
);
739 if(!parser
->source
|| !parser
->source
->functions
|| parser
->source
->functions
->next
|| parser
->source
->variables
) {
740 ERR("Invalid parser result!\n");
741 parser_release(parser
);
744 expr
= parser
->source
->functions
->expr
;
746 hres
= create_source_function(parser
, expr
->parameter_list
, expr
->source_elements
, NULL
, expr
->src_str
,
747 expr
->src_len
, &function
);
748 parser_release(parser
);
752 *ret
= (IDispatch
*)_IDispatchEx_(function
);
756 static HRESULT
FunctionConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
757 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
764 case DISPATCH_CONSTRUCT
: {
767 hres
= construct_function(ctx
, dp
, ei
, &ret
);
771 V_VT(retv
) = VT_DISPATCH
;
772 V_DISPATCH(retv
) = ret
;
776 FIXME("unimplemented flags %x\n", flags
);
783 static HRESULT
FunctionProt_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
784 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
790 HRESULT
init_function_constr(script_ctx_t
*ctx
, DispatchEx
*object_prototype
)
792 FunctionInstance
*prot
, *constr
;
795 static const WCHAR FunctionW
[] = {'F','u','n','c','t','i','o','n',0};
797 hres
= create_function(ctx
, NULL
, PROPF_CONSTR
, TRUE
, object_prototype
, &prot
);
801 prot
->value_proc
= FunctionProt_value
;
802 prot
->name
= prototypeW
;
804 hres
= create_function(ctx
, NULL
, PROPF_CONSTR
|1, TRUE
, &prot
->dispex
, &constr
);
805 if(SUCCEEDED(hres
)) {
806 constr
->value_proc
= FunctionConstr_value
;
807 constr
->name
= FunctionW
;
808 hres
= set_prototype(ctx
, &constr
->dispex
, &prot
->dispex
);
810 jsdisp_release(&constr
->dispex
);
812 jsdisp_release(&prot
->dispex
);
816 ctx
->function_constr
= &constr
->dispex
;