2 * Copyright 2011 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 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(vbscript
);
27 static DISPID propput_dispid
= DISPID_PROPERTYPUT
;
41 dynamic_var_t
*dynamic_vars
;
53 typedef HRESULT (*instr_func_t
)(exec_ctx_t
*);
83 static BOOL
lookup_dynamic_vars(dynamic_var_t
*var
, const WCHAR
*name
, ref_t
*ref
)
86 if(!strcmpiW(var
->name
, name
)) {
87 ref
->type
= var
->is_const
? REF_CONST
: REF_VAR
;
98 static HRESULT
lookup_identifier(exec_ctx_t
*ctx
, BSTR name
, vbdisp_invoke_type_t invoke_type
, ref_t
*ref
)
106 static const WCHAR errW
[] = {'e','r','r',0};
108 if(invoke_type
== VBDISP_LET
109 && (ctx
->func
->type
== FUNC_FUNCTION
|| ctx
->func
->type
== FUNC_PROPGET
|| ctx
->func
->type
== FUNC_DEFGET
)
110 && !strcmpiW(name
, ctx
->func
->name
)) {
112 ref
->u
.v
= &ctx
->ret_val
;
116 for(i
=0; i
< ctx
->func
->var_cnt
; i
++) {
117 if(!strcmpiW(ctx
->func
->vars
[i
].name
, name
)) {
119 ref
->u
.v
= ctx
->vars
+i
;
124 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++) {
125 if(!strcmpiW(ctx
->func
->args
[i
].name
, name
)) {
127 ref
->u
.v
= ctx
->args
+i
;
132 if(lookup_dynamic_vars(ctx
->func
->type
== FUNC_GLOBAL
? ctx
->script
->global_vars
: ctx
->dynamic_vars
, name
, ref
))
135 if(ctx
->func
->type
!= FUNC_GLOBAL
) {
137 /* FIXME: Bind such identifier while generating bytecode. */
138 for(i
=0; i
< ctx
->vbthis
->desc
->prop_cnt
; i
++) {
139 if(!strcmpiW(ctx
->vbthis
->desc
->props
[i
].name
, name
)) {
141 ref
->u
.v
= ctx
->vbthis
->props
+i
;
147 hres
= disp_get_id(ctx
->this_obj
, name
, invoke_type
, TRUE
, &id
);
148 if(SUCCEEDED(hres
)) {
149 ref
->type
= REF_DISP
;
150 ref
->u
.d
.disp
= ctx
->this_obj
;
156 if(ctx
->func
->type
!= FUNC_GLOBAL
&& lookup_dynamic_vars(ctx
->script
->global_vars
, name
, ref
))
159 for(func
= ctx
->script
->global_funcs
; func
; func
= func
->next
) {
160 if(!strcmpiW(func
->name
, name
)) {
161 ref
->type
= REF_FUNC
;
167 if(!strcmpiW(name
, errW
)) {
169 ref
->u
.obj
= (IDispatch
*)&ctx
->script
->err_obj
->IDispatchEx_iface
;
173 hres
= vbdisp_get_id(ctx
->script
->global_obj
, name
, invoke_type
, TRUE
, &id
);
174 if(SUCCEEDED(hres
)) {
175 ref
->type
= REF_DISP
;
176 ref
->u
.d
.disp
= (IDispatch
*)&ctx
->script
->global_obj
->IDispatchEx_iface
;
181 LIST_FOR_EACH_ENTRY(item
, &ctx
->script
->named_items
, named_item_t
, entry
) {
182 if((item
->flags
& SCRIPTITEM_ISVISIBLE
) && !strcmpiW(item
->name
, name
)) {
186 hres
= IActiveScriptSite_GetItemInfo(ctx
->script
->site
, name
, SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
188 WARN("GetItemInfo failed: %08x\n", hres
);
192 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
193 IUnknown_Release(unk
);
195 WARN("object does not implement IDispatch\n");
201 ref
->u
.obj
= item
->disp
;
206 LIST_FOR_EACH_ENTRY(item
, &ctx
->script
->named_items
, named_item_t
, entry
) {
207 if((item
->flags
& SCRIPTITEM_GLOBALMEMBERS
)) {
208 hres
= disp_get_id(item
->disp
, name
, invoke_type
, FALSE
, &id
);
209 if(SUCCEEDED(hres
)) {
210 ref
->type
= REF_DISP
;
211 ref
->u
.d
.disp
= item
->disp
;
218 ref
->type
= REF_NONE
;
222 static HRESULT
add_dynamic_var(exec_ctx_t
*ctx
, const WCHAR
*name
,
223 BOOL is_const
, VARIANT
**out_var
)
225 dynamic_var_t
*new_var
;
230 heap
= ctx
->func
->type
== FUNC_GLOBAL
? &ctx
->script
->heap
: &ctx
->heap
;
232 new_var
= heap_pool_alloc(heap
, sizeof(*new_var
));
234 return E_OUTOFMEMORY
;
236 size
= (strlenW(name
)+1)*sizeof(WCHAR
);
237 str
= heap_pool_alloc(heap
, size
);
239 return E_OUTOFMEMORY
;
240 memcpy(str
, name
, size
);
242 new_var
->is_const
= is_const
;
243 V_VT(&new_var
->v
) = VT_EMPTY
;
245 if(ctx
->func
->type
== FUNC_GLOBAL
) {
246 new_var
->next
= ctx
->script
->global_vars
;
247 ctx
->script
->global_vars
= new_var
;
249 new_var
->next
= ctx
->dynamic_vars
;
250 ctx
->dynamic_vars
= new_var
;
253 *out_var
= &new_var
->v
;
257 static inline VARIANT
*stack_pop(exec_ctx_t
*ctx
)
260 return ctx
->stack
+ --ctx
->top
;
263 static inline VARIANT
*stack_top(exec_ctx_t
*ctx
, unsigned n
)
265 assert(ctx
->top
>= n
);
266 return ctx
->stack
+ (ctx
->top
-n
-1);
269 static HRESULT
stack_push(exec_ctx_t
*ctx
, VARIANT
*v
)
271 if(ctx
->stack_size
== ctx
->top
) {
274 new_stack
= heap_realloc(ctx
->stack
, ctx
->stack_size
*2*sizeof(*ctx
->stack
));
277 return E_OUTOFMEMORY
;
280 ctx
->stack
= new_stack
;
281 ctx
->stack_size
*= 2;
284 ctx
->stack
[ctx
->top
++] = *v
;
288 static inline HRESULT
stack_push_null(exec_ctx_t
*ctx
)
292 return stack_push(ctx
, &v
);
295 static void stack_popn(exec_ctx_t
*ctx
, unsigned n
)
298 VariantClear(stack_pop(ctx
));
301 static void stack_pop_deref(exec_ctx_t
*ctx
, variant_val_t
*r
)
306 if(V_VT(v
) == (VT_BYREF
|VT_VARIANT
)) {
308 r
->v
= V_VARIANTREF(v
);
315 static inline void release_val(variant_val_t
*v
)
321 static HRESULT
stack_pop_val(exec_ctx_t
*ctx
, variant_val_t
*r
)
323 stack_pop_deref(ctx
, r
);
325 if(V_VT(r
->v
) == VT_DISPATCH
) {
328 hres
= get_disp_value(ctx
->script
, V_DISPATCH(r
->v
), &r
->store
);
330 IDispatch_Release(V_DISPATCH(r
->v
));
341 static HRESULT
stack_assume_val(exec_ctx_t
*ctx
, unsigned n
)
343 VARIANT
*v
= stack_top(ctx
, n
);
346 if(V_VT(v
) == (VT_BYREF
|VT_VARIANT
)) {
347 VARIANT
*ref
= V_VARIANTREF(v
);
350 hres
= VariantCopy(v
, ref
);
355 if(V_VT(v
) == VT_DISPATCH
) {
358 disp
= V_DISPATCH(v
);
359 hres
= get_disp_value(ctx
->script
, disp
, v
);
360 IDispatch_Release(disp
);
368 static int stack_pop_bool(exec_ctx_t
*ctx
, BOOL
*b
)
373 hres
= stack_pop_val(ctx
, &val
);
392 FIXME("unsupported for %s\n", debugstr_variant(val
.v
));
399 static HRESULT
stack_pop_disp(exec_ctx_t
*ctx
, IDispatch
**ret
)
401 VARIANT
*v
= stack_pop(ctx
);
403 if(V_VT(v
) == VT_DISPATCH
) {
404 *ret
= V_DISPATCH(v
);
408 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
409 FIXME("not supported type: %s\n", debugstr_variant(v
));
415 if(V_VT(v
) != VT_DISPATCH
) {
416 FIXME("not disp %s\n", debugstr_variant(v
));
421 IDispatch_AddRef(V_DISPATCH(v
));
422 *ret
= V_DISPATCH(v
);
426 static HRESULT
stack_assume_disp(exec_ctx_t
*ctx
, unsigned n
, IDispatch
**disp
)
428 VARIANT
*v
= stack_top(ctx
, n
), *ref
;
430 if(V_VT(v
) != VT_DISPATCH
) {
431 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
432 FIXME("not supported type: %s\n", debugstr_variant(v
));
436 ref
= V_VARIANTREF(v
);
437 if(V_VT(ref
) != VT_DISPATCH
) {
438 FIXME("not disp %s\n", debugstr_variant(ref
));
442 V_VT(v
) = VT_DISPATCH
;
443 V_DISPATCH(v
) = V_DISPATCH(ref
);
445 IDispatch_AddRef(V_DISPATCH(v
));
449 *disp
= V_DISPATCH(v
);
453 static inline void instr_jmp(exec_ctx_t
*ctx
, unsigned addr
)
455 ctx
->instr
= ctx
->code
->instrs
+ addr
;
458 static void vbstack_to_dp(exec_ctx_t
*ctx
, unsigned arg_cnt
, BOOL is_propput
, DISPPARAMS
*dp
)
460 dp
->cNamedArgs
= is_propput
? 1 : 0;
461 dp
->cArgs
= arg_cnt
+ dp
->cNamedArgs
;
462 dp
->rgdispidNamedArgs
= is_propput
? &propput_dispid
: NULL
;
468 assert(ctx
->top
>= arg_cnt
);
470 for(i
=1; i
*2 <= arg_cnt
; i
++) {
471 tmp
= ctx
->stack
[ctx
->top
-i
];
472 ctx
->stack
[ctx
->top
-i
] = ctx
->stack
[ctx
->top
-arg_cnt
+i
-1];
473 ctx
->stack
[ctx
->top
-arg_cnt
+i
-1] = tmp
;
476 dp
->rgvarg
= ctx
->stack
+ ctx
->top
-dp
->cArgs
;
478 dp
->rgvarg
= is_propput
? ctx
->stack
+ctx
->top
-1 : NULL
;
482 static HRESULT
array_access(exec_ctx_t
*ctx
, SAFEARRAY
*array
, DISPPARAMS
*dp
, VARIANT
**ret
)
484 unsigned cell_off
= 0, dim_size
= 1, i
;
485 unsigned argc
= arg_cnt(dp
);
491 FIXME("NULL array\n");
495 if(array
->cDims
!= argc
) {
496 FIXME("argc %d does not match cDims %d\n", dp
->cArgs
, array
->cDims
);
500 for(i
=0; i
< argc
; i
++) {
501 hres
= to_int(get_arg(dp
, i
), &idx
);
505 idx
-= array
->rgsabound
[i
].lLbound
;
506 if(idx
>= array
->rgsabound
[i
].cElements
) {
507 FIXME("out of bound element %d in dim %d of size %d\n", idx
, i
+1, array
->rgsabound
[i
].cElements
);
511 cell_off
+= idx
*dim_size
;
512 dim_size
*= array
->rgsabound
[i
].cElements
;
515 hres
= SafeArrayAccessData(array
, (void**)&data
);
519 *ret
= data
+cell_off
;
521 SafeArrayUnaccessData(array
);
525 static HRESULT
do_icall(exec_ctx_t
*ctx
, VARIANT
*res
)
527 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
528 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
533 hres
= lookup_identifier(ctx
, identifier
, VBDISP_CALLGET
, &ref
);
543 FIXME("REF_VAR no res\n");
547 v
= V_VT(ref
.u
.v
) == (VT_VARIANT
|VT_BYREF
) ? V_VARIANTREF(ref
.u
.v
) : ref
.u
.v
;
550 SAFEARRAY
*array
= NULL
;
553 case VT_ARRAY
|VT_BYREF
|VT_VARIANT
:
554 array
= *V_ARRAYREF(ref
.u
.v
);
556 case VT_ARRAY
|VT_VARIANT
:
557 array
= V_ARRAY(ref
.u
.v
);
560 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
561 hres
= disp_call(ctx
->script
, V_DISPATCH(v
), DISPID_VALUE
, &dp
, res
);
566 FIXME("arguments not implemented\n");
573 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
574 hres
= array_access(ctx
, array
, &dp
, &v
);
579 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
584 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
585 hres
= disp_call(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, &dp
, res
);
590 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
591 hres
= exec_script(ctx
->script
, ref
.u
.f
, NULL
, &dp
, res
);
597 FIXME("arguments on object\n");
602 IDispatch_AddRef(ref
.u
.obj
);
603 V_VT(res
) = VT_DISPATCH
;
604 V_DISPATCH(res
) = ref
.u
.obj
;
608 if(res
&& !ctx
->func
->code_ctx
->option_explicit
&& arg_cnt
== 0) {
610 hres
= add_dynamic_var(ctx
, identifier
, FALSE
, &new);
613 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
617 FIXME("%s not found\n", debugstr_w(identifier
));
618 return DISP_E_UNKNOWNNAME
;
621 stack_popn(ctx
, arg_cnt
);
625 static HRESULT
interp_icall(exec_ctx_t
*ctx
)
632 hres
= do_icall(ctx
, &v
);
636 return stack_push(ctx
, &v
);
639 static HRESULT
interp_icallv(exec_ctx_t
*ctx
)
642 return do_icall(ctx
, NULL
);
645 static HRESULT
do_mcall(exec_ctx_t
*ctx
, VARIANT
*res
)
647 const BSTR identifier
= ctx
->instr
->arg1
.bstr
;
648 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
654 hres
= stack_pop_disp(ctx
, &obj
);
663 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
665 hres
= disp_get_id(obj
, identifier
, VBDISP_CALLGET
, FALSE
, &id
);
667 hres
= disp_call(ctx
->script
, obj
, id
, &dp
, res
);
668 IDispatch_Release(obj
);
672 stack_popn(ctx
, arg_cnt
);
676 static HRESULT
interp_mcall(exec_ctx_t
*ctx
)
683 hres
= do_mcall(ctx
, &res
);
687 return stack_push(ctx
, &res
);
690 static HRESULT
interp_mcallv(exec_ctx_t
*ctx
)
694 return do_mcall(ctx
, NULL
);
697 static HRESULT
assign_value(exec_ctx_t
*ctx
, VARIANT
*dst
, VARIANT
*src
, WORD flags
)
701 hres
= VariantCopyInd(dst
, src
);
705 if(V_VT(dst
) == VT_DISPATCH
&& !(flags
& DISPATCH_PROPERTYPUTREF
)) {
708 hres
= get_disp_value(ctx
->script
, V_DISPATCH(dst
), &value
);
709 IDispatch_Release(V_DISPATCH(dst
));
719 static HRESULT
assign_ident(exec_ctx_t
*ctx
, BSTR name
, WORD flags
, DISPPARAMS
*dp
)
724 hres
= lookup_identifier(ctx
, name
, VBDISP_LET
, &ref
);
730 VARIANT
*v
= ref
.u
.v
;
732 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
))
738 if(!(V_VT(v
) & VT_ARRAY
)) {
739 FIXME("array assign on type %d\n", V_VT(v
));
744 case VT_ARRAY
|VT_BYREF
|VT_VARIANT
:
745 array
= *V_ARRAYREF(v
);
747 case VT_ARRAY
|VT_VARIANT
:
751 FIXME("Unsupported array type %x\n", V_VT(v
));
756 FIXME("null array\n");
760 hres
= array_access(ctx
, array
, dp
, &v
);
763 }else if(V_VT(v
) == (VT_ARRAY
|VT_BYREF
|VT_VARIANT
)) {
764 FIXME("non-array assign\n");
768 hres
= assign_value(ctx
, v
, dp
->rgvarg
, flags
);
772 hres
= disp_propput(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, flags
, dp
);
775 FIXME("functions not implemented\n");
781 FIXME("REF_CONST\n");
784 if(ctx
->func
->code_ctx
->option_explicit
) {
785 FIXME("throw exception\n");
791 FIXME("arg_cnt %d not supported\n", arg_cnt(dp
));
795 TRACE("creating variable %s\n", debugstr_w(name
));
796 hres
= add_dynamic_var(ctx
, name
, FALSE
, &new_var
);
798 hres
= assign_value(ctx
, new_var
, dp
->rgvarg
, flags
);
805 static HRESULT
interp_assign_ident(exec_ctx_t
*ctx
)
807 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
808 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
812 TRACE("%s\n", debugstr_w(arg
));
814 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
815 hres
= assign_ident(ctx
, arg
, DISPATCH_PROPERTYPUT
, &dp
);
819 stack_popn(ctx
, arg_cnt
+1);
823 static HRESULT
interp_set_ident(exec_ctx_t
*ctx
)
825 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
826 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
830 TRACE("%s\n", debugstr_w(arg
));
833 FIXME("arguments not supported\n");
837 hres
= stack_assume_disp(ctx
, 0, NULL
);
841 vbstack_to_dp(ctx
, 0, TRUE
, &dp
);
842 hres
= assign_ident(ctx
, ctx
->instr
->arg1
.bstr
, DISPATCH_PROPERTYPUTREF
, &dp
);
850 static HRESULT
interp_assign_member(exec_ctx_t
*ctx
)
852 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
853 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
859 TRACE("%s\n", debugstr_w(identifier
));
861 hres
= stack_assume_disp(ctx
, arg_cnt
+1, &obj
);
870 hres
= disp_get_id(obj
, identifier
, VBDISP_LET
, FALSE
, &id
);
871 if(SUCCEEDED(hres
)) {
872 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
873 hres
= disp_propput(ctx
->script
, obj
, id
, DISPATCH_PROPERTYPUT
, &dp
);
878 stack_popn(ctx
, arg_cnt
+2);
882 static HRESULT
interp_set_member(exec_ctx_t
*ctx
)
884 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
885 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
891 TRACE("%s\n", debugstr_w(identifier
));
894 FIXME("arguments not supported\n");
898 hres
= stack_assume_disp(ctx
, 1, &obj
);
907 hres
= stack_assume_disp(ctx
, 0, NULL
);
911 hres
= disp_get_id(obj
, identifier
, VBDISP_SET
, FALSE
, &id
);
912 if(SUCCEEDED(hres
)) {
913 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
914 hres
= disp_propput(ctx
->script
, obj
, id
, DISPATCH_PROPERTYPUTREF
, &dp
);
923 static HRESULT
interp_const(exec_ctx_t
*ctx
)
925 BSTR arg
= ctx
->instr
->arg1
.bstr
;
930 TRACE("%s\n", debugstr_w(arg
));
932 assert(ctx
->func
->type
== FUNC_GLOBAL
);
934 hres
= lookup_identifier(ctx
, arg
, VBDISP_CALLGET
, &ref
);
938 if(ref
.type
!= REF_NONE
) {
939 FIXME("%s already defined\n", debugstr_w(arg
));
943 hres
= stack_assume_val(ctx
, 0);
947 hres
= add_dynamic_var(ctx
, arg
, TRUE
, &v
);
951 *v
= *stack_pop(ctx
);
955 static HRESULT
interp_val(exec_ctx_t
*ctx
)
963 hres
= stack_pop_val(ctx
, &val
);
969 hres
= VariantCopy(&v
, val
.v
);
974 return stack_push(ctx
, val
.owned
? val
.v
: &v
);
977 static HRESULT
interp_pop(exec_ctx_t
*ctx
)
979 const unsigned n
= ctx
->instr
->arg1
.uint
;
987 static HRESULT
interp_new(exec_ctx_t
*ctx
)
989 const WCHAR
*arg
= ctx
->instr
->arg1
.bstr
;
990 class_desc_t
*class_desc
;
995 static const WCHAR regexpW
[] = {'r','e','g','e','x','p',0};
997 TRACE("%s\n", debugstr_w(arg
));
999 if(!strcmpiW(arg
, regexpW
)) {
1000 V_VT(&v
) = VT_DISPATCH
;
1001 hres
= create_regexp(&V_DISPATCH(&v
));
1005 return stack_push(ctx
, &v
);
1008 for(class_desc
= ctx
->script
->classes
; class_desc
; class_desc
= class_desc
->next
) {
1009 if(!strcmpiW(class_desc
->name
, arg
))
1013 FIXME("Class %s not found\n", debugstr_w(arg
));
1017 hres
= create_vbdisp(class_desc
, &obj
);
1021 V_VT(&v
) = VT_DISPATCH
;
1022 V_DISPATCH(&v
) = (IDispatch
*)&obj
->IDispatchEx_iface
;
1023 return stack_push(ctx
, &v
);
1026 static HRESULT
interp_dim(exec_ctx_t
*ctx
)
1028 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
1029 const unsigned array_id
= ctx
->instr
->arg2
.uint
;
1030 const array_desc_t
*array_desc
;
1034 TRACE("%s\n", debugstr_w(ident
));
1036 assert(array_id
< ctx
->func
->array_cnt
);
1038 ctx
->arrays
= heap_alloc_zero(ctx
->func
->array_cnt
* sizeof(SAFEARRAY
*));
1040 return E_OUTOFMEMORY
;
1043 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1045 FIXME("lookup %s failed: %08x\n", debugstr_w(ident
), hres
);
1049 if(ref
.type
!= REF_VAR
) {
1050 FIXME("got ref.type = %d\n", ref
.type
);
1054 if(ctx
->arrays
[array_id
]) {
1055 FIXME("Array already initialized\n");
1059 array_desc
= ctx
->func
->array_descs
+ array_id
;
1060 if(array_desc
->dim_cnt
) {
1061 ctx
->arrays
[array_id
] = SafeArrayCreate(VT_VARIANT
, array_desc
->dim_cnt
, array_desc
->bounds
);
1062 if(!ctx
->arrays
[array_id
])
1063 return E_OUTOFMEMORY
;
1066 V_VT(ref
.u
.v
) = VT_ARRAY
|VT_BYREF
|VT_VARIANT
;
1067 V_ARRAYREF(ref
.u
.v
) = ctx
->arrays
+array_id
;
1071 static HRESULT
interp_step(exec_ctx_t
*ctx
)
1073 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1079 TRACE("%s\n", debugstr_w(ident
));
1081 V_VT(&zero
) = VT_I2
;
1083 hres
= VarCmp(stack_top(ctx
, 0), &zero
, ctx
->script
->lcid
, 0);
1087 gteq_zero
= hres
== VARCMP_GT
|| hres
== VARCMP_EQ
;
1089 hres
= lookup_identifier(ctx
, ident
, VBDISP_ANY
, &ref
);
1093 if(ref
.type
!= REF_VAR
) {
1094 FIXME("%s is not REF_VAR\n", debugstr_w(ident
));
1098 hres
= VarCmp(ref
.u
.v
, stack_top(ctx
, 1), ctx
->script
->lcid
, 0);
1102 if(hres
== VARCMP_EQ
|| hres
== (gteq_zero
? VARCMP_LT
: VARCMP_GT
)) {
1106 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1111 static HRESULT
interp_newenum(exec_ctx_t
*ctx
)
1119 stack_pop_deref(ctx
, &v
);
1120 assert(V_VT(stack_top(ctx
, 0)) == VT_EMPTY
);
1121 r
= stack_top(ctx
, 0);
1124 case VT_DISPATCH
|VT_BYREF
:
1127 DISPPARAMS dp
= {0};
1130 hres
= disp_call(ctx
->script
, V_ISBYREF(v
.v
) ? *V_DISPATCHREF(v
.v
) : V_DISPATCH(v
.v
), DISPID_NEWENUM
, &dp
, &iterv
);
1135 if(V_VT(&iterv
) != VT_UNKNOWN
&& V_VT(&iterv
) != VT_DISPATCH
) {
1136 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv
));
1137 VariantClear(&iterv
);
1141 hres
= IUnknown_QueryInterface(V_UNKNOWN(&iterv
), &IID_IEnumVARIANT
, (void**)&iter
);
1142 IUnknown_Release(V_UNKNOWN(&iterv
));
1144 FIXME("Could not get IEnumVARIANT iface: %08x\n", hres
);
1148 V_VT(r
) = VT_UNKNOWN
;
1149 V_UNKNOWN(r
) = (IUnknown
*)iter
;
1153 FIXME("Unsupported for %s\n", debugstr_variant(v
.v
));
1161 static HRESULT
interp_enumnext(exec_ctx_t
*ctx
)
1163 const unsigned loop_end
= ctx
->instr
->arg1
.uint
;
1164 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1166 DISPPARAMS dp
= {&v
, &propput_dispid
, 1, 1};
1173 if(V_VT(stack_top(ctx
, 0)) == VT_EMPTY
) {
1174 FIXME("uninitialized\n");
1178 assert(V_VT(stack_top(ctx
, 0)) == VT_UNKNOWN
);
1179 iter
= (IEnumVARIANT
*)V_UNKNOWN(stack_top(ctx
, 0));
1181 V_VT(&v
) = VT_EMPTY
;
1182 hres
= IEnumVARIANT_Next(iter
, 1, &v
, NULL
);
1186 do_continue
= hres
== S_OK
;
1187 hres
= assign_ident(ctx
, ident
, DISPATCH_PROPERTYPUT
|DISPATCH_PROPERTYPUTREF
, &dp
);
1196 instr_jmp(ctx
, loop_end
);
1201 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
1203 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1207 instr_jmp(ctx
, arg
);
1211 static HRESULT
interp_jmp_false(exec_ctx_t
*ctx
)
1213 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1219 hres
= stack_pop_bool(ctx
, &b
);
1226 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1230 static HRESULT
interp_jmp_true(exec_ctx_t
*ctx
)
1232 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1238 hres
= stack_pop_bool(ctx
, &b
);
1243 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1249 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
1257 static HRESULT
interp_stop(exec_ctx_t
*ctx
)
1261 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1265 static HRESULT
interp_me(exec_ctx_t
*ctx
)
1271 IDispatch_AddRef(ctx
->this_obj
);
1272 V_VT(&v
) = VT_DISPATCH
;
1273 V_DISPATCH(&v
) = ctx
->this_obj
;
1274 return stack_push(ctx
, &v
);
1277 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
1279 const VARIANT_BOOL arg
= ctx
->instr
->arg1
.lng
;
1282 TRACE("%s\n", arg
? "true" : "false");
1286 return stack_push(ctx
, &v
);
1289 static HRESULT
interp_errmode(exec_ctx_t
*ctx
)
1291 const int err_mode
= ctx
->instr
->arg1
.uint
;
1293 TRACE("%d\n", err_mode
);
1295 ctx
->resume_next
= err_mode
;
1296 ctx
->script
->err_number
= S_OK
;
1300 static HRESULT
interp_string(exec_ctx_t
*ctx
)
1307 V_BSTR(&v
) = SysAllocString(ctx
->instr
->arg1
.str
);
1309 return E_OUTOFMEMORY
;
1311 return stack_push(ctx
, &v
);
1314 static HRESULT
interp_long(exec_ctx_t
*ctx
)
1316 const LONG arg
= ctx
->instr
->arg1
.lng
;
1323 return stack_push(ctx
, &v
);
1326 static HRESULT
interp_short(exec_ctx_t
*ctx
)
1328 const LONG arg
= ctx
->instr
->arg1
.lng
;
1335 return stack_push(ctx
, &v
);
1338 static HRESULT
interp_double(exec_ctx_t
*ctx
)
1340 const DOUBLE
*arg
= ctx
->instr
->arg1
.dbl
;
1343 TRACE("%lf\n", *arg
);
1347 return stack_push(ctx
, &v
);
1350 static HRESULT
interp_empty(exec_ctx_t
*ctx
)
1356 V_VT(&v
) = VT_EMPTY
;
1357 return stack_push(ctx
, &v
);
1360 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1363 return stack_push_null(ctx
);
1366 static HRESULT
interp_nothing(exec_ctx_t
*ctx
)
1372 V_VT(&v
) = VT_DISPATCH
;
1373 V_DISPATCH(&v
) = NULL
;
1374 return stack_push(ctx
, &v
);
1377 static HRESULT
interp_hres(exec_ctx_t
*ctx
)
1379 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1384 V_VT(&v
) = VT_ERROR
;
1386 return stack_push(ctx
, &v
);
1389 static HRESULT
interp_not(exec_ctx_t
*ctx
)
1397 hres
= stack_pop_val(ctx
, &val
);
1401 hres
= VarNot(val
.v
, &v
);
1406 return stack_push(ctx
, &v
);
1409 static HRESULT
interp_and(exec_ctx_t
*ctx
)
1417 hres
= stack_pop_val(ctx
, &r
);
1421 hres
= stack_pop_val(ctx
, &l
);
1422 if(SUCCEEDED(hres
)) {
1423 hres
= VarAnd(l
.v
, r
.v
, &v
);
1430 return stack_push(ctx
, &v
);
1433 static HRESULT
interp_or(exec_ctx_t
*ctx
)
1441 hres
= stack_pop_val(ctx
, &r
);
1445 hres
= stack_pop_val(ctx
, &l
);
1446 if(SUCCEEDED(hres
)) {
1447 hres
= VarOr(l
.v
, r
.v
, &v
);
1454 return stack_push(ctx
, &v
);
1457 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
1465 hres
= stack_pop_val(ctx
, &r
);
1469 hres
= stack_pop_val(ctx
, &l
);
1470 if(SUCCEEDED(hres
)) {
1471 hres
= VarXor(l
.v
, r
.v
, &v
);
1478 return stack_push(ctx
, &v
);
1481 static HRESULT
interp_eqv(exec_ctx_t
*ctx
)
1489 hres
= stack_pop_val(ctx
, &r
);
1493 hres
= stack_pop_val(ctx
, &l
);
1494 if(SUCCEEDED(hres
)) {
1495 hres
= VarEqv(l
.v
, r
.v
, &v
);
1502 return stack_push(ctx
, &v
);
1505 static HRESULT
interp_imp(exec_ctx_t
*ctx
)
1513 hres
= stack_pop_val(ctx
, &r
);
1517 hres
= stack_pop_val(ctx
, &l
);
1518 if(SUCCEEDED(hres
)) {
1519 hres
= VarImp(l
.v
, r
.v
, &v
);
1526 return stack_push(ctx
, &v
);
1529 static HRESULT
var_cmp(exec_ctx_t
*ctx
, VARIANT
*l
, VARIANT
*r
)
1531 TRACE("%s %s\n", debugstr_variant(l
), debugstr_variant(r
));
1533 /* FIXME: Fix comparing string to number */
1535 return VarCmp(l
, r
, ctx
->script
->lcid
, 0);
1538 static HRESULT
cmp_oper(exec_ctx_t
*ctx
)
1543 hres
= stack_pop_val(ctx
, &r
);
1547 hres
= stack_pop_val(ctx
, &l
);
1548 if(SUCCEEDED(hres
)) {
1549 hres
= var_cmp(ctx
, l
.v
, r
.v
);
1557 static HRESULT
interp_equal(exec_ctx_t
*ctx
)
1564 hres
= cmp_oper(ctx
);
1567 if(hres
== VARCMP_NULL
)
1568 return stack_push_null(ctx
);
1571 V_BOOL(&v
) = hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1572 return stack_push(ctx
, &v
);
1575 static HRESULT
interp_nequal(exec_ctx_t
*ctx
)
1582 hres
= cmp_oper(ctx
);
1585 if(hres
== VARCMP_NULL
)
1586 return stack_push_null(ctx
);
1589 V_BOOL(&v
) = hres
!= VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1590 return stack_push(ctx
, &v
);
1593 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
1600 hres
= cmp_oper(ctx
);
1603 if(hres
== VARCMP_NULL
)
1604 return stack_push_null(ctx
);
1607 V_BOOL(&v
) = hres
== VARCMP_GT
? VARIANT_TRUE
: VARIANT_FALSE
;
1608 return stack_push(ctx
, &v
);
1611 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
1618 hres
= cmp_oper(ctx
);
1621 if(hres
== VARCMP_NULL
)
1622 return stack_push_null(ctx
);
1625 V_BOOL(&v
) = hres
== VARCMP_GT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1626 return stack_push(ctx
, &v
);
1629 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
1636 hres
= cmp_oper(ctx
);
1639 if(hres
== VARCMP_NULL
)
1640 return stack_push_null(ctx
);
1643 V_BOOL(&v
) = hres
== VARCMP_LT
? VARIANT_TRUE
: VARIANT_FALSE
;
1644 return stack_push(ctx
, &v
);
1647 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
1654 hres
= cmp_oper(ctx
);
1657 if(hres
== VARCMP_NULL
)
1658 return stack_push_null(ctx
);
1661 V_BOOL(&v
) = hres
== VARCMP_LT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1662 return stack_push(ctx
, &v
);
1665 static HRESULT
interp_case(exec_ctx_t
*ctx
)
1667 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1673 hres
= stack_pop_val(ctx
, &v
);
1677 hres
= var_cmp(ctx
, stack_top(ctx
, 0), v
.v
);
1682 if(hres
== VARCMP_EQ
) {
1684 instr_jmp(ctx
, arg
);
1692 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, VARIANT_BOOL
*ret
)
1694 IObjectIdentity
*identity
;
1695 IUnknown
*unk1
, *unk2
;
1698 if(disp1
== disp2
) {
1699 *ret
= VARIANT_TRUE
;
1703 if(!disp1
|| !disp2
) {
1704 *ret
= VARIANT_FALSE
;
1708 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
1712 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
1714 IUnknown_Release(unk1
);
1719 *ret
= VARIANT_TRUE
;
1721 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
1722 if(SUCCEEDED(hres
)) {
1723 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
1724 IObjectIdentity_Release(identity
);
1725 *ret
= hres
== S_OK
? VARIANT_TRUE
: VARIANT_FALSE
;
1727 *ret
= VARIANT_FALSE
;
1731 IUnknown_Release(unk1
);
1732 IUnknown_Release(unk2
);
1736 static HRESULT
interp_is(exec_ctx_t
*ctx
)
1744 hres
= stack_pop_disp(ctx
, &r
);
1748 hres
= stack_pop_disp(ctx
, &l
);
1749 if(SUCCEEDED(hres
)) {
1751 hres
= disp_cmp(l
, r
, &V_BOOL(&v
));
1753 IDispatch_Release(l
);
1756 IDispatch_Release(r
);
1760 return stack_push(ctx
, &v
);
1763 static HRESULT
interp_concat(exec_ctx_t
*ctx
)
1771 hres
= stack_pop_val(ctx
, &r
);
1775 hres
= stack_pop_val(ctx
, &l
);
1776 if(SUCCEEDED(hres
)) {
1777 hres
= VarCat(l
.v
, r
.v
, &v
);
1784 return stack_push(ctx
, &v
);
1787 static HRESULT
interp_add(exec_ctx_t
*ctx
)
1795 hres
= stack_pop_val(ctx
, &r
);
1799 hres
= stack_pop_val(ctx
, &l
);
1800 if(SUCCEEDED(hres
)) {
1801 hres
= VarAdd(l
.v
, r
.v
, &v
);
1808 return stack_push(ctx
, &v
);
1811 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
1819 hres
= stack_pop_val(ctx
, &r
);
1823 hres
= stack_pop_val(ctx
, &l
);
1824 if(SUCCEEDED(hres
)) {
1825 hres
= VarSub(l
.v
, r
.v
, &v
);
1832 return stack_push(ctx
, &v
);
1835 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
1843 hres
= stack_pop_val(ctx
, &r
);
1847 hres
= stack_pop_val(ctx
, &l
);
1848 if(SUCCEEDED(hres
)) {
1849 hres
= VarMod(l
.v
, r
.v
, &v
);
1856 return stack_push(ctx
, &v
);
1859 static HRESULT
interp_idiv(exec_ctx_t
*ctx
)
1867 hres
= stack_pop_val(ctx
, &r
);
1871 hres
= stack_pop_val(ctx
, &l
);
1872 if(SUCCEEDED(hres
)) {
1873 hres
= VarIdiv(l
.v
, r
.v
, &v
);
1880 return stack_push(ctx
, &v
);
1883 static HRESULT
interp_div(exec_ctx_t
*ctx
)
1891 hres
= stack_pop_val(ctx
, &r
);
1895 hres
= stack_pop_val(ctx
, &l
);
1896 if(SUCCEEDED(hres
)) {
1897 hres
= VarDiv(l
.v
, r
.v
, &v
);
1904 return stack_push(ctx
, &v
);
1907 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
1915 hres
= stack_pop_val(ctx
, &r
);
1919 hres
= stack_pop_val(ctx
, &l
);
1920 if(SUCCEEDED(hres
)) {
1921 hres
= VarMul(l
.v
, r
.v
, &v
);
1928 return stack_push(ctx
, &v
);
1931 static HRESULT
interp_exp(exec_ctx_t
*ctx
)
1939 hres
= stack_pop_val(ctx
, &r
);
1943 hres
= stack_pop_val(ctx
, &l
);
1944 if(SUCCEEDED(hres
)) {
1945 hres
= VarPow(l
.v
, r
.v
, &v
);
1952 return stack_push(ctx
, &v
);
1955 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
1961 hres
= stack_pop_val(ctx
, &val
);
1965 hres
= VarNeg(val
.v
, &v
);
1970 return stack_push(ctx
, &v
);
1973 static HRESULT
interp_incc(exec_ctx_t
*ctx
)
1975 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
1982 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1986 if(ref
.type
!= REF_VAR
) {
1987 FIXME("ref.type is not REF_VAR\n");
1991 hres
= VarAdd(stack_top(ctx
, 0), ref
.u
.v
, &v
);
1995 VariantClear(ref
.u
.v
);
2000 static HRESULT
interp_catch(exec_ctx_t
*ctx
)
2002 /* Nothing to do here, the OP is for unwinding only. */
2006 static const instr_func_t op_funcs
[] = {
2007 #define X(x,n,a,b) interp_ ## x,
2012 static const unsigned op_move
[] = {
2013 #define X(x,n,a,b) n,
2018 void release_dynamic_vars(dynamic_var_t
*var
)
2021 VariantClear(&var
->v
);
2026 static void release_exec(exec_ctx_t
*ctx
)
2030 VariantClear(&ctx
->ret_val
);
2031 release_dynamic_vars(ctx
->dynamic_vars
);
2034 IDispatch_Release(ctx
->this_obj
);
2037 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++)
2038 VariantClear(ctx
->args
+i
);
2042 for(i
=0; i
< ctx
->func
->var_cnt
; i
++)
2043 VariantClear(ctx
->vars
+i
);
2047 for(i
=0; i
< ctx
->func
->var_cnt
; i
++) {
2049 SafeArrayDestroy(ctx
->arrays
[i
]);
2051 heap_free(ctx
->arrays
);
2054 heap_pool_free(&ctx
->heap
);
2055 heap_free(ctx
->args
);
2056 heap_free(ctx
->vars
);
2057 heap_free(ctx
->stack
);
2060 HRESULT
exec_script(script_ctx_t
*ctx
, function_t
*func
, vbdisp_t
*vbthis
, DISPPARAMS
*dp
, VARIANT
*res
)
2062 exec_ctx_t exec
= {func
->code_ctx
};
2064 HRESULT hres
= S_OK
;
2066 exec
.code
= func
->code_ctx
;
2068 if(dp
? func
->arg_cnt
!= arg_cnt(dp
) : func
->arg_cnt
) {
2069 FIXME("wrong arg_cnt %d, expected %d\n", dp
? arg_cnt(dp
) : 0, func
->arg_cnt
);
2073 heap_pool_init(&exec
.heap
);
2079 exec
.args
= heap_alloc_zero(func
->arg_cnt
* sizeof(VARIANT
));
2081 release_exec(&exec
);
2082 return E_OUTOFMEMORY
;
2085 for(i
=0; i
< func
->arg_cnt
; i
++) {
2087 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
)) {
2088 if(func
->args
[i
].by_ref
)
2091 hres
= VariantCopyInd(exec
.args
+i
, V_VARIANTREF(v
));
2093 hres
= VariantCopyInd(exec
.args
+i
, v
);
2096 release_exec(&exec
);
2105 exec
.vars
= heap_alloc_zero(func
->var_cnt
* sizeof(VARIANT
));
2107 release_exec(&exec
);
2108 return E_OUTOFMEMORY
;
2114 exec
.stack_size
= 16;
2116 exec
.stack
= heap_alloc(exec
.stack_size
* sizeof(VARIANT
));
2118 release_exec(&exec
);
2119 return E_OUTOFMEMORY
;
2123 exec
.this_obj
= (IDispatch
*)&vbthis
->IDispatchEx_iface
;
2124 exec
.vbthis
= vbthis
;
2125 }else if (ctx
->host_global
) {
2126 exec
.this_obj
= ctx
->host_global
;
2128 exec
.this_obj
= (IDispatch
*)&ctx
->script_obj
->IDispatchEx_iface
;
2130 IDispatch_AddRef(exec
.this_obj
);
2132 exec
.instr
= exec
.code
->instrs
+ func
->code_off
;
2137 op
= exec
.instr
->op
;
2138 hres
= op_funcs
[op
](&exec
);
2140 ctx
->err_number
= hres
= map_hres(hres
);
2142 if(exec
.resume_next
) {
2145 WARN("Failed %08x in resume next mode\n", hres
);
2148 * Unwinding here is simple. We need to find the next OP_catch, which contains
2149 * information about expected stack size and jump offset on error. Generated
2150 * bytecode needs to guarantee, that simple jump and stack adjustment will
2151 * guarantee proper execution continuation.
2153 while((++exec
.instr
)->op
!= OP_catch
);
2155 TRACE("unwind jmp %d stack_off %d\n", exec
.instr
->arg1
.uint
, exec
.instr
->arg2
.uint
);
2157 stack_off
= exec
.instr
->arg2
.uint
;
2158 instr_jmp(&exec
, exec
.instr
->arg1
.uint
);
2160 if(exec
.top
> stack_off
) {
2161 stack_popn(&exec
, exec
.top
-stack_off
);
2162 }else if(exec
.top
< stack_off
) {
2165 V_VT(&v
) = VT_EMPTY
;
2166 while(exec
.top
< stack_off
) {
2167 hres
= stack_push(&exec
, &v
);
2175 WARN("Failed %08x\n", hres
);
2176 stack_popn(&exec
, exec
.top
);
2181 exec
.instr
+= op_move
[op
];
2185 if(func
->type
!= FUNC_FUNCTION
&& func
->type
!= FUNC_PROPGET
&& func
->type
!= FUNC_DEFGET
)
2186 assert(V_VT(&exec
.ret_val
) == VT_EMPTY
);
2188 if(SUCCEEDED(hres
) && res
) {
2189 *res
= exec
.ret_val
;
2190 V_VT(&exec
.ret_val
) = VT_EMPTY
;
2193 release_exec(&exec
);