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 scope_chain_t
*scope_chain
;
33 function_code_t
*func_code
;
38 static inline FunctionInstance
*function_from_vdisp(vdisp_t
*vdisp
)
40 return (FunctionInstance
*)vdisp
->u
.jsdisp
;
43 static inline FunctionInstance
*function_this(vdisp_t
*jsthis
)
45 return is_vclass(jsthis
, JSCLASS_FUNCTION
) ? function_from_vdisp(jsthis
) : NULL
;
48 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
50 static const WCHAR lengthW
[] = {'l','e','n','g','t','h',0};
51 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
52 static const WCHAR applyW
[] = {'a','p','p','l','y',0};
53 static const WCHAR callW
[] = {'c','a','l','l',0};
54 static const WCHAR argumentsW
[] = {'a','r','g','u','m','e','n','t','s',0};
56 static IDispatch
*get_this(DISPPARAMS
*dp
)
60 for(i
=0; i
< dp
->cNamedArgs
; i
++) {
61 if(dp
->rgdispidNamedArgs
[i
] == DISPID_THIS
) {
62 if(V_VT(dp
->rgvarg
+i
) == VT_DISPATCH
)
63 return V_DISPATCH(dp
->rgvarg
+i
);
65 WARN("This is not VT_DISPATCH\n");
70 TRACE("no this passed\n");
74 static HRESULT
init_parameters(jsdisp_t
*var_disp
, FunctionInstance
*function
, DISPPARAMS
*dp
,
81 V_VT(&var_empty
) = VT_EMPTY
;
84 for(i
=0; i
< function
->func_code
->param_cnt
; i
++) {
85 hres
= jsdisp_propput_name(var_disp
, function
->func_code
->params
[i
],
86 i
< cargs
? get_arg(dp
,i
) : &var_empty
, ei
);
94 static HRESULT
Arguments_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
95 VARIANT
*retv
, jsexcept_t
*ei
)
101 static const builtin_info_t Arguments_info
= {
103 {NULL
, Arguments_value
, 0},
109 static HRESULT
create_arguments(script_ctx_t
*ctx
, IDispatch
*calee
, DISPPARAMS
*dp
,
110 jsexcept_t
*ei
, jsdisp_t
**ret
)
117 static const WCHAR caleeW
[] = {'c','a','l','l','e','e',0};
119 args
= heap_alloc_zero(sizeof(jsdisp_t
));
121 return E_OUTOFMEMORY
;
123 hres
= init_dispex_from_constr(args
, ctx
, &Arguments_info
, ctx
->object_constr
);
129 for(i
=0; i
< arg_cnt(dp
); i
++) {
130 hres
= jsdisp_propput_idx(args
, i
, get_arg(dp
,i
), ei
);
135 if(SUCCEEDED(hres
)) {
137 V_I4(&var
) = arg_cnt(dp
);
138 hres
= jsdisp_propput_name(args
, lengthW
, &var
, ei
);
140 if(SUCCEEDED(hres
)) {
141 V_VT(&var
) = VT_DISPATCH
;
142 V_DISPATCH(&var
) = calee
;
143 hres
= jsdisp_propput_name(args
, caleeW
, &var
, ei
);
148 jsdisp_release(args
);
156 static HRESULT
create_var_disp(script_ctx_t
*ctx
, FunctionInstance
*function
, jsdisp_t
*arg_disp
,
157 DISPPARAMS
*dp
, jsexcept_t
*ei
, jsdisp_t
**ret
)
163 hres
= create_dispex(ctx
, NULL
, NULL
, &var_disp
);
167 var_set_jsdisp(&var
, arg_disp
);
168 hres
= jsdisp_propput_name(var_disp
, argumentsW
, &var
, ei
);
170 hres
= init_parameters(var_disp
, function
, dp
, ei
);
172 jsdisp_release(var_disp
);
180 static HRESULT
invoke_source(script_ctx_t
*ctx
, FunctionInstance
*function
, IDispatch
*this_obj
, DISPPARAMS
*dp
,
181 VARIANT
*retv
, jsexcept_t
*ei
)
183 jsdisp_t
*var_disp
, *arg_disp
;
184 exec_ctx_t
*exec_ctx
;
185 scope_chain_t
*scope
;
188 if(!function
->func_code
) {
189 FIXME("no source\n");
193 hres
= create_arguments(ctx
, to_disp(&function
->dispex
), dp
, ei
, &arg_disp
);
197 hres
= create_var_disp(ctx
, function
, arg_disp
, dp
, ei
, &var_disp
);
199 jsdisp_release(arg_disp
);
203 hres
= scope_push(function
->scope_chain
, var_disp
, &scope
);
204 if(SUCCEEDED(hres
)) {
205 hres
= create_exec_ctx(ctx
, this_obj
, var_disp
, scope
, FALSE
, &exec_ctx
);
206 scope_release(scope
);
208 jsdisp_release(var_disp
);
209 if(SUCCEEDED(hres
)) {
212 prev_args
= function
->arguments
;
213 function
->arguments
= arg_disp
;
214 hres
= exec_source(exec_ctx
, function
->code
, function
->func_code
, FALSE
, ei
, retv
);
215 function
->arguments
= prev_args
;
217 jsdisp_release(arg_disp
);
218 exec_release(exec_ctx
);
224 static HRESULT
invoke_constructor(script_ctx_t
*ctx
, FunctionInstance
*function
, DISPPARAMS
*dp
,
225 VARIANT
*retv
, jsexcept_t
*ei
)
231 hres
= create_object(ctx
, &function
->dispex
, &this_obj
);
235 hres
= invoke_source(ctx
, function
, to_disp(this_obj
), dp
, &var
, ei
);
237 jsdisp_release(this_obj
);
241 if(V_VT(&var
) == VT_DISPATCH
) {
242 jsdisp_release(this_obj
);
243 V_VT(retv
) = VT_DISPATCH
;
244 V_DISPATCH(retv
) = V_DISPATCH(&var
);
247 var_set_jsdisp(retv
, this_obj
);
252 static HRESULT
invoke_value_proc(script_ctx_t
*ctx
, FunctionInstance
*function
, IDispatch
*this_disp
, WORD flags
, DISPPARAMS
*dp
,
253 VARIANT
*retv
, jsexcept_t
*ei
)
259 set_disp(&vthis
, this_disp
);
260 else if(ctx
->host_global
)
261 set_disp(&vthis
, ctx
->host_global
);
263 set_jsdisp(&vthis
, ctx
->global
);
265 hres
= function
->value_proc(ctx
, &vthis
, flags
, dp
, retv
, ei
);
267 vdisp_release(&vthis
);
271 static HRESULT
call_function(script_ctx_t
*ctx
, FunctionInstance
*function
, IDispatch
*this_obj
, DISPPARAMS
*args
,
272 VARIANT
*retv
, jsexcept_t
*ei
)
274 if(function
->value_proc
)
275 return invoke_value_proc(ctx
, function
, this_obj
, DISPATCH_METHOD
, args
, retv
, ei
);
277 return invoke_source(ctx
, function
, this_obj
, args
, retv
, ei
);
280 static HRESULT
function_to_string(FunctionInstance
*function
, BSTR
*ret
)
284 static const WCHAR native_prefixW
[] = {'\n','f','u','n','c','t','i','o','n',' '};
285 static const WCHAR native_suffixW
[] =
286 {'(',')',' ','{','\n',' ',' ',' ',' ','[','n','a','t','i','v','e',' ','c','o','d','e',']','\n','}','\n'};
288 if(function
->value_proc
) {
291 name_len
= strlenW(function
->name
);
292 str
= SysAllocStringLen(NULL
, sizeof(native_prefixW
) + name_len
*sizeof(WCHAR
) + sizeof(native_suffixW
));
294 return E_OUTOFMEMORY
;
296 memcpy(str
, native_prefixW
, sizeof(native_prefixW
));
297 memcpy(str
+ sizeof(native_prefixW
)/sizeof(WCHAR
), function
->name
, name_len
*sizeof(WCHAR
));
298 memcpy(str
+ sizeof(native_prefixW
)/sizeof(WCHAR
) + name_len
, native_suffixW
, sizeof(native_suffixW
));
300 str
= SysAllocStringLen(function
->func_code
->source
, function
->func_code
->source_len
);
302 return E_OUTOFMEMORY
;
309 static HRESULT
Function_length(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
310 VARIANT
*retv
, jsexcept_t
*ei
)
312 FunctionInstance
*This
= function_from_vdisp(jsthis
);
314 TRACE("%p %d\n", This
, This
->length
);
317 case DISPATCH_PROPERTYGET
:
319 V_I4(retv
) = This
->length
;
322 FIXME("unimplemented flags %x\n", flags
);
329 static HRESULT
Function_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
330 VARIANT
*retv
, jsexcept_t
*ei
)
332 FunctionInstance
*function
;
338 if(!(function
= function_this(jsthis
)))
339 return throw_type_error(ctx
, ei
, JS_E_FUNCTION_EXPECTED
, NULL
);
341 hres
= function_to_string(function
, &str
);
346 V_VT(retv
) = VT_BSTR
;
354 static HRESULT
array_to_args(script_ctx_t
*ctx
, jsdisp_t
*arg_array
, jsexcept_t
*ei
, DISPPARAMS
*args
)
360 hres
= jsdisp_propget_name(arg_array
, lengthW
, &var
, ei
);
364 hres
= to_uint32(ctx
, &var
, ei
, &length
);
369 argv
= heap_alloc(length
* sizeof(VARIANT
));
371 return E_OUTOFMEMORY
;
373 for(i
=0; i
<length
; i
++) {
374 hres
= jsdisp_get_idx(arg_array
, i
, argv
+i
, ei
);
375 if(hres
== DISP_E_UNKNOWNNAME
)
376 V_VT(argv
+i
) = VT_EMPTY
;
377 else if(FAILED(hres
)) {
379 VariantClear(argv
+i
);
385 args
->cArgs
= length
;
390 static HRESULT
Function_apply(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
391 VARIANT
*retv
, jsexcept_t
*ei
)
393 FunctionInstance
*function
;
394 DISPPARAMS args
= {NULL
,NULL
,0,0};
396 IDispatch
*this_obj
= NULL
;
401 if(!(function
= function_this(jsthis
)))
402 return throw_type_error(ctx
, ei
, JS_E_FUNCTION_EXPECTED
, NULL
);
406 VARIANT
*v
= get_arg(dp
,0);
408 if(V_VT(v
) != VT_EMPTY
&& V_VT(v
) != VT_NULL
) {
409 hres
= to_object(ctx
, v
, &this_obj
);
416 jsdisp_t
*arg_array
= NULL
;
418 if(V_VT(get_arg(dp
,1)) == VT_DISPATCH
) {
419 arg_array
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(get_arg(dp
,1)));
421 (!is_class(arg_array
, JSCLASS_ARRAY
) && !is_class(arg_array
, JSCLASS_ARGUMENTS
) )) {
422 jsdisp_release(arg_array
);
428 hres
= array_to_args(ctx
, arg_array
, ei
, &args
);
429 jsdisp_release(arg_array
);
431 FIXME("throw TypeError\n");
437 hres
= call_function(ctx
, function
, this_obj
, &args
, retv
, ei
);
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
)
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
, JS_E_FUNCTION_EXPECTED
, 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
);
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
)
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
);
503 return invoke_source(ctx
, function
, get_this(dp
), dp
, retv
, ei
);
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
);
522 return invoke_constructor(ctx
, function
, dp
, retv
, ei
);
525 FIXME("not implemented flags %x\n", flags
);
532 static HRESULT
Function_arguments(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
,
533 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
)
535 FunctionInstance
*function
= (FunctionInstance
*)jsthis
->u
.jsdisp
;
541 case DISPATCH_PROPERTYGET
: {
542 if(function
->arguments
) {
543 jsdisp_addref(function
->arguments
);
544 var_set_jsdisp(retv
, function
->arguments
);
546 V_VT(retv
) = VT_NULL
;
550 case DISPATCH_PROPERTYPUT
:
553 FIXME("unimplemented flags %x\n", flags
);
560 static void Function_destructor(jsdisp_t
*dispex
)
562 FunctionInstance
*This
= (FunctionInstance
*)dispex
;
565 release_bytecode(This
->code
);
566 if(This
->scope_chain
)
567 scope_release(This
->scope_chain
);
571 static const builtin_prop_t Function_props
[] = {
572 {applyW
, Function_apply
, PROPF_METHOD
|2},
573 {argumentsW
, Function_arguments
, 0},
574 {callW
, Function_call
, PROPF_METHOD
|1},
575 {lengthW
, Function_length
, 0},
576 {toStringW
, Function_toString
, PROPF_METHOD
}
579 static const builtin_info_t Function_info
= {
581 {NULL
, Function_value
, 0},
582 sizeof(Function_props
)/sizeof(*Function_props
),
588 static HRESULT
create_function(script_ctx_t
*ctx
, const builtin_info_t
*builtin_info
, DWORD flags
,
589 BOOL funcprot
, jsdisp_t
*prototype
, FunctionInstance
**ret
)
591 FunctionInstance
*function
;
594 function
= heap_alloc_zero(sizeof(FunctionInstance
));
596 return E_OUTOFMEMORY
;
599 hres
= init_dispex(&function
->dispex
, ctx
, &Function_info
, prototype
);
600 else if(builtin_info
)
601 hres
= init_dispex_from_constr(&function
->dispex
, ctx
, builtin_info
, ctx
->function_constr
);
603 hres
= init_dispex_from_constr(&function
->dispex
, ctx
, &Function_info
, ctx
->function_constr
);
607 function
->flags
= flags
;
608 function
->length
= flags
& PROPF_ARGMASK
;
614 static HRESULT
set_prototype(script_ctx_t
*ctx
, jsdisp_t
*dispex
, jsdisp_t
*prototype
)
619 var_set_jsdisp(&var
, prototype
);
620 memset(&jsexcept
, 0, sizeof(jsexcept
));
622 return jsdisp_propput_name(dispex
, prototypeW
, &var
, &jsexcept
);
625 HRESULT
create_builtin_function(script_ctx_t
*ctx
, builtin_invoke_t value_proc
, const WCHAR
*name
,
626 const builtin_info_t
*builtin_info
, DWORD flags
, jsdisp_t
*prototype
, jsdisp_t
**ret
)
628 FunctionInstance
*function
;
631 hres
= create_function(ctx
, builtin_info
, flags
, FALSE
, NULL
, &function
);
639 V_I4(&var
) = function
->length
;
640 hres
= jsdisp_propput_const(&function
->dispex
, lengthW
, &var
);
644 hres
= set_prototype(ctx
, &function
->dispex
, prototype
);
646 jsdisp_release(&function
->dispex
);
650 function
->value_proc
= value_proc
;
651 function
->name
= name
;
653 *ret
= &function
->dispex
;
657 HRESULT
create_source_function(script_ctx_t
*ctx
, bytecode_t
*code
, function_code_t
*func_code
,
658 scope_chain_t
*scope_chain
, jsdisp_t
**ret
)
660 FunctionInstance
*function
;
664 hres
= create_object(ctx
, NULL
, &prototype
);
668 hres
= create_function(ctx
, NULL
, PROPF_CONSTR
, FALSE
, NULL
, &function
);
669 if(SUCCEEDED(hres
)) {
670 hres
= set_prototype(ctx
, &function
->dispex
, prototype
);
672 jsdisp_release(&function
->dispex
);
674 jsdisp_release(prototype
);
679 scope_addref(scope_chain
);
680 function
->scope_chain
= scope_chain
;
683 bytecode_addref(code
);
684 function
->code
= code
;
685 function
->func_code
= func_code
;
686 function
->length
= function
->func_code
->param_cnt
;
688 *ret
= &function
->dispex
;
692 static HRESULT
construct_function(script_ctx_t
*ctx
, DISPPARAMS
*dp
, jsexcept_t
*ei
, IDispatch
**ret
)
694 WCHAR
*str
= NULL
, *ptr
;
695 DWORD argc
, len
= 0, l
;
702 static const WCHAR function_anonymousW
[] = {'f','u','n','c','t','i','o','n',' ','a','n','o','n','y','m','o','u','s','('};
703 static const WCHAR function_beginW
[] = {')',' ','{','\n'};
704 static const WCHAR function_endW
[] = {'\n','}',0};
708 params
= heap_alloc(argc
*sizeof(BSTR
));
710 return E_OUTOFMEMORY
;
713 len
= (argc
-2)*2; /* separating commas */
714 for(i
=0; i
< argc
; i
++) {
715 hres
= to_string(ctx
, get_arg(dp
,i
), ei
, params
+i
);
718 len
+= SysStringLen(params
[i
]);
722 if(SUCCEEDED(hres
)) {
723 len
+= (sizeof(function_anonymousW
) + sizeof(function_beginW
) + sizeof(function_endW
)) / sizeof(WCHAR
);
724 str
= heap_alloc(len
*sizeof(WCHAR
));
726 memcpy(str
, function_anonymousW
, sizeof(function_anonymousW
));
727 ptr
= str
+ sizeof(function_anonymousW
)/sizeof(WCHAR
);
730 l
= SysStringLen(params
[j
]);
731 memcpy(ptr
, params
[j
], l
*sizeof(WCHAR
));
739 memcpy(ptr
, function_beginW
, sizeof(function_beginW
));
740 ptr
+= sizeof(function_beginW
)/sizeof(WCHAR
);
742 l
= SysStringLen(params
[argc
-1]);
743 memcpy(ptr
, params
[argc
-1], l
*sizeof(WCHAR
));
746 memcpy(ptr
, function_endW
, sizeof(function_endW
));
748 TRACE("%s\n", debugstr_w(str
));
750 hres
= E_OUTOFMEMORY
;
755 SysFreeString(params
[i
]);
760 hres
= compile_script(ctx
, str
, NULL
, FALSE
, FALSE
, &code
);
765 if(code
->global_code
.func_cnt
!= 1 || code
->global_code
.var_cnt
) {
766 ERR("Invalid parser result!\n");
767 release_bytecode(code
);
771 hres
= create_source_function(ctx
, code
, code
->global_code
.funcs
, NULL
, &function
);
772 release_bytecode(code
);
776 *ret
= to_disp(function
);
780 static HRESULT
FunctionConstr_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
781 VARIANT
*retv
, jsexcept_t
*ei
)
788 case DISPATCH_CONSTRUCT
: {
791 hres
= construct_function(ctx
, dp
, ei
, &ret
);
795 V_VT(retv
) = VT_DISPATCH
;
796 V_DISPATCH(retv
) = ret
;
800 FIXME("unimplemented flags %x\n", flags
);
807 static HRESULT
FunctionProt_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
808 VARIANT
*retv
, jsexcept_t
*ei
)
814 HRESULT
init_function_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
)
816 FunctionInstance
*prot
, *constr
;
819 static const WCHAR FunctionW
[] = {'F','u','n','c','t','i','o','n',0};
821 hres
= create_function(ctx
, NULL
, PROPF_CONSTR
, TRUE
, object_prototype
, &prot
);
825 prot
->value_proc
= FunctionProt_value
;
826 prot
->name
= prototypeW
;
828 hres
= create_function(ctx
, NULL
, PROPF_CONSTR
|1, TRUE
, &prot
->dispex
, &constr
);
829 if(SUCCEEDED(hres
)) {
830 constr
->value_proc
= FunctionConstr_value
;
831 constr
->name
= FunctionW
;
832 hres
= set_prototype(ctx
, &constr
->dispex
, &prot
->dispex
);
834 jsdisp_release(&constr
->dispex
);
836 jsdisp_release(&prot
->dispex
);
840 ctx
->function_constr
= &constr
->dispex
;