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
;
40 dynamic_var_t
*dynamic_vars
;
52 typedef HRESULT (*instr_func_t
)(exec_ctx_t
*);
82 static BOOL
lookup_dynamic_vars(dynamic_var_t
*var
, const WCHAR
*name
, ref_t
*ref
)
85 if(!strcmpiW(var
->name
, name
)) {
86 ref
->type
= var
->is_const
? REF_CONST
: REF_VAR
;
97 static HRESULT
lookup_identifier(exec_ctx_t
*ctx
, BSTR name
, vbdisp_invoke_type_t invoke_type
, ref_t
*ref
)
105 static const WCHAR errW
[] = {'e','r','r',0};
107 if(invoke_type
== VBDISP_LET
108 && (ctx
->func
->type
== FUNC_FUNCTION
|| ctx
->func
->type
== FUNC_PROPGET
|| ctx
->func
->type
== FUNC_DEFGET
)
109 && !strcmpiW(name
, ctx
->func
->name
)) {
111 ref
->u
.v
= &ctx
->ret_val
;
115 for(i
=0; i
< ctx
->func
->var_cnt
; i
++) {
116 if(!strcmpiW(ctx
->func
->vars
[i
].name
, name
)) {
118 ref
->u
.v
= ctx
->vars
+i
;
123 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++) {
124 if(!strcmpiW(ctx
->func
->args
[i
].name
, name
)) {
126 ref
->u
.v
= ctx
->args
+i
;
131 if(lookup_dynamic_vars(ctx
->func
->type
== FUNC_GLOBAL
? ctx
->script
->global_vars
: ctx
->dynamic_vars
, name
, ref
))
134 if(ctx
->func
->type
!= FUNC_GLOBAL
) {
135 hres
= disp_get_id(ctx
->this_obj
, name
, invoke_type
, TRUE
, &id
);
136 if(SUCCEEDED(hres
)) {
137 ref
->type
= REF_DISP
;
138 ref
->u
.d
.disp
= ctx
->this_obj
;
144 if(ctx
->func
->type
!= FUNC_GLOBAL
&& lookup_dynamic_vars(ctx
->script
->global_vars
, name
, ref
))
147 for(func
= ctx
->script
->global_funcs
; func
; func
= func
->next
) {
148 if(!strcmpiW(func
->name
, name
)) {
149 ref
->type
= REF_FUNC
;
155 if(!strcmpiW(name
, errW
)) {
157 ref
->u
.obj
= (IDispatch
*)&ctx
->script
->err_obj
->IDispatchEx_iface
;
161 hres
= vbdisp_get_id(ctx
->script
->global_obj
, name
, invoke_type
, TRUE
, &id
);
162 if(SUCCEEDED(hres
)) {
163 ref
->type
= REF_DISP
;
164 ref
->u
.d
.disp
= (IDispatch
*)&ctx
->script
->global_obj
->IDispatchEx_iface
;
169 LIST_FOR_EACH_ENTRY(item
, &ctx
->script
->named_items
, named_item_t
, entry
) {
170 if((item
->flags
& SCRIPTITEM_ISVISIBLE
) && !strcmpiW(item
->name
, name
)) {
174 hres
= IActiveScriptSite_GetItemInfo(ctx
->script
->site
, name
, SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
176 WARN("GetItemInfo failed: %08x\n", hres
);
180 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
181 IUnknown_Release(unk
);
183 WARN("object does not implement IDispatch\n");
189 ref
->u
.obj
= item
->disp
;
194 LIST_FOR_EACH_ENTRY(item
, &ctx
->script
->named_items
, named_item_t
, entry
) {
195 if((item
->flags
& SCRIPTITEM_GLOBALMEMBERS
)) {
196 hres
= disp_get_id(item
->disp
, name
, invoke_type
, FALSE
, &id
);
197 if(SUCCEEDED(hres
)) {
198 ref
->type
= REF_DISP
;
199 ref
->u
.d
.disp
= item
->disp
;
206 ref
->type
= REF_NONE
;
210 static HRESULT
add_dynamic_var(exec_ctx_t
*ctx
, const WCHAR
*name
,
211 BOOL is_const
, VARIANT
*val
, BOOL own_val
, VARIANT
**out_var
)
213 dynamic_var_t
*new_var
;
219 heap
= ctx
->func
->type
== FUNC_GLOBAL
? &ctx
->script
->heap
: &ctx
->heap
;
221 new_var
= heap_pool_alloc(heap
, sizeof(*new_var
));
223 return E_OUTOFMEMORY
;
225 size
= (strlenW(name
)+1)*sizeof(WCHAR
);
226 str
= heap_pool_alloc(heap
, size
);
228 return E_OUTOFMEMORY
;
229 memcpy(str
, name
, size
);
231 new_var
->is_const
= is_const
;
236 V_VT(&new_var
->v
) = VT_EMPTY
;
237 hres
= VariantCopy(&new_var
->v
, val
);
242 if(ctx
->func
->type
== FUNC_GLOBAL
) {
243 new_var
->next
= ctx
->script
->global_vars
;
244 ctx
->script
->global_vars
= new_var
;
246 new_var
->next
= ctx
->dynamic_vars
;
247 ctx
->dynamic_vars
= new_var
;
251 *out_var
= &new_var
->v
;
256 static inline VARIANT
*stack_pop(exec_ctx_t
*ctx
)
259 return ctx
->stack
+ --ctx
->top
;
262 static inline VARIANT
*stack_top(exec_ctx_t
*ctx
, unsigned n
)
264 assert(ctx
->top
>= n
);
265 return ctx
->stack
+ (ctx
->top
-n
-1);
268 static HRESULT
stack_push(exec_ctx_t
*ctx
, VARIANT
*v
)
270 if(ctx
->stack_size
== ctx
->top
) {
273 new_stack
= heap_realloc(ctx
->stack
, ctx
->stack_size
*2*sizeof(*ctx
->stack
));
276 return E_OUTOFMEMORY
;
279 ctx
->stack
= new_stack
;
280 ctx
->stack_size
*= 2;
283 ctx
->stack
[ctx
->top
++] = *v
;
287 static inline HRESULT
stack_push_null(exec_ctx_t
*ctx
)
291 return stack_push(ctx
, &v
);
294 static void stack_popn(exec_ctx_t
*ctx
, unsigned n
)
297 VariantClear(stack_pop(ctx
));
300 static HRESULT
stack_pop_val(exec_ctx_t
*ctx
, variant_val_t
*v
)
304 var
= stack_pop(ctx
);
306 if(V_VT(var
) == (VT_BYREF
|VT_VARIANT
)) {
308 var
= V_VARIANTREF(var
);
313 if(V_VT(var
) == VT_DISPATCH
) {
317 hres
= disp_call(ctx
->script
, V_DISPATCH(var
), DISPID_VALUE
, &dp
, &v
->store
);
319 IDispatch_Release(V_DISPATCH(var
));
332 static HRESULT
stack_assume_val(exec_ctx_t
*ctx
, unsigned n
)
334 VARIANT
*v
= stack_top(ctx
, n
);
337 if(V_VT(v
) == (VT_BYREF
|VT_VARIANT
)) {
338 VARIANT
*ref
= V_VARIANTREF(v
);
341 hres
= VariantCopy(v
, ref
);
346 if(V_VT(v
) == VT_DISPATCH
) {
350 disp
= V_DISPATCH(v
);
352 hres
= disp_call(ctx
->script
, disp
, DISPID_VALUE
, &dp
, v
);
353 IDispatch_Release(disp
);
361 static inline void release_val(variant_val_t
*v
)
367 static int stack_pop_bool(exec_ctx_t
*ctx
, BOOL
*b
)
372 hres
= stack_pop_val(ctx
, &val
);
391 FIXME("unsupported for %s\n", debugstr_variant(val
.v
));
398 static HRESULT
stack_pop_disp(exec_ctx_t
*ctx
, IDispatch
**ret
)
400 VARIANT
*v
= stack_pop(ctx
);
402 if(V_VT(v
) == VT_DISPATCH
) {
403 *ret
= V_DISPATCH(v
);
407 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
408 FIXME("not supported type: %s\n", debugstr_variant(v
));
414 if(V_VT(v
) != VT_DISPATCH
) {
415 FIXME("not disp %s\n", debugstr_variant(v
));
420 IDispatch_AddRef(V_DISPATCH(v
));
421 *ret
= V_DISPATCH(v
);
425 static HRESULT
stack_assume_disp(exec_ctx_t
*ctx
, unsigned n
, IDispatch
**disp
)
427 VARIANT
*v
= stack_top(ctx
, n
), *ref
;
429 if(V_VT(v
) != VT_DISPATCH
) {
430 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
431 FIXME("not supported type: %s\n", debugstr_variant(v
));
435 ref
= V_VARIANTREF(v
);
436 if(V_VT(ref
) != VT_DISPATCH
) {
437 FIXME("not disp %s\n", debugstr_variant(ref
));
441 V_VT(v
) = VT_DISPATCH
;
442 V_DISPATCH(v
) = V_DISPATCH(ref
);
444 IDispatch_AddRef(V_DISPATCH(v
));
448 *disp
= V_DISPATCH(v
);
452 static inline void instr_jmp(exec_ctx_t
*ctx
, unsigned addr
)
454 ctx
->instr
= ctx
->code
->instrs
+ addr
;
457 static void vbstack_to_dp(exec_ctx_t
*ctx
, unsigned arg_cnt
, BOOL is_propput
, DISPPARAMS
*dp
)
459 dp
->cNamedArgs
= is_propput
? 1 : 0;
460 dp
->cArgs
= arg_cnt
+ dp
->cNamedArgs
;
461 dp
->rgdispidNamedArgs
= is_propput
? &propput_dispid
: NULL
;
467 assert(ctx
->top
>= arg_cnt
);
469 for(i
=1; i
*2 <= arg_cnt
; i
++) {
470 tmp
= ctx
->stack
[ctx
->top
-i
];
471 ctx
->stack
[ctx
->top
-i
] = ctx
->stack
[ctx
->top
-arg_cnt
+i
-1];
472 ctx
->stack
[ctx
->top
-arg_cnt
+i
-1] = tmp
;
475 dp
->rgvarg
= ctx
->stack
+ ctx
->top
-dp
->cArgs
;
477 dp
->rgvarg
= is_propput
? ctx
->stack
+ctx
->top
-1 : NULL
;
481 static HRESULT
array_access(exec_ctx_t
*ctx
, SAFEARRAY
*array
, DISPPARAMS
*dp
, VARIANT
**ret
)
483 unsigned cell_off
= 0, dim_size
= 1, i
;
484 unsigned argc
= arg_cnt(dp
);
490 FIXME("NULL array\n");
494 if(array
->cDims
!= argc
) {
495 FIXME("argc %d does not match cDims %d\n", dp
->cArgs
, array
->cDims
);
499 for(i
=0; i
< argc
; i
++) {
500 hres
= to_int(get_arg(dp
, i
), &idx
);
504 idx
-= array
->rgsabound
[i
].lLbound
;
505 if(idx
>= array
->rgsabound
[i
].cElements
) {
506 FIXME("out of bound element %d in dim %d of size %d\n", idx
, i
+1, array
->rgsabound
[i
].cElements
);
510 cell_off
+= idx
*dim_size
;
511 dim_size
*= array
->rgsabound
[i
].cElements
;
514 hres
= SafeArrayAccessData(array
, (void**)&data
);
518 *ret
= data
+cell_off
;
520 SafeArrayUnaccessData(array
);
524 static HRESULT
do_icall(exec_ctx_t
*ctx
, VARIANT
*res
)
526 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
527 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
532 hres
= lookup_identifier(ctx
, identifier
, VBDISP_CALLGET
, &ref
);
542 FIXME("REF_VAR no res\n");
546 v
= V_VT(ref
.u
.v
) == (VT_VARIANT
|VT_BYREF
) ? V_VARIANTREF(ref
.u
.v
) : ref
.u
.v
;
552 case VT_ARRAY
|VT_BYREF
|VT_VARIANT
:
553 array
= *V_ARRAYREF(ref
.u
.v
);
555 case VT_ARRAY
|VT_VARIANT
:
556 array
= V_ARRAY(ref
.u
.v
);
559 FIXME("arguments not implemented\n");
563 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
564 hres
= array_access(ctx
, array
, &dp
, &v
);
569 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
574 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
575 hres
= disp_call(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, &dp
, res
);
580 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
581 hres
= exec_script(ctx
->script
, ref
.u
.f
, NULL
, &dp
, res
);
587 FIXME("arguments on object\n");
592 IDispatch_AddRef(ref
.u
.obj
);
593 V_VT(res
) = VT_DISPATCH
;
594 V_DISPATCH(res
) = ref
.u
.obj
;
598 if(res
&& !ctx
->func
->code_ctx
->option_explicit
&& arg_cnt
== 0) {
601 hres
= add_dynamic_var(ctx
, identifier
, FALSE
, &v
, FALSE
, &new);
604 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
608 FIXME("%s not found\n", debugstr_w(identifier
));
609 return DISP_E_UNKNOWNNAME
;
612 stack_popn(ctx
, arg_cnt
);
616 static HRESULT
interp_icall(exec_ctx_t
*ctx
)
623 hres
= do_icall(ctx
, &v
);
627 return stack_push(ctx
, &v
);
630 static HRESULT
interp_icallv(exec_ctx_t
*ctx
)
633 return do_icall(ctx
, NULL
);
636 static HRESULT
do_mcall(exec_ctx_t
*ctx
, VARIANT
*res
)
638 const BSTR identifier
= ctx
->instr
->arg1
.bstr
;
639 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
645 hres
= stack_pop_disp(ctx
, &obj
);
654 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
656 hres
= disp_get_id(obj
, identifier
, VBDISP_CALLGET
, FALSE
, &id
);
658 hres
= disp_call(ctx
->script
, obj
, id
, &dp
, res
);
659 IDispatch_Release(obj
);
663 stack_popn(ctx
, arg_cnt
);
667 static HRESULT
interp_mcall(exec_ctx_t
*ctx
)
674 hres
= do_mcall(ctx
, &res
);
678 return stack_push(ctx
, &res
);
681 static HRESULT
interp_mcallv(exec_ctx_t
*ctx
)
685 return do_mcall(ctx
, NULL
);
688 static HRESULT
assign_ident(exec_ctx_t
*ctx
, BSTR name
, DISPPARAMS
*dp
)
693 hres
= lookup_identifier(ctx
, name
, VBDISP_LET
, &ref
);
699 VARIANT
*v
= ref
.u
.v
;
701 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
))
707 if(!(V_VT(v
) & VT_ARRAY
)) {
708 FIXME("array assign on type %d\n", V_VT(v
));
713 case VT_ARRAY
|VT_BYREF
|VT_VARIANT
:
714 array
= *V_ARRAYREF(v
);
716 case VT_ARRAY
|VT_VARIANT
:
720 FIXME("Unsupported array type %x\v", V_VT(v
));
725 FIXME("null array\n");
729 hres
= array_access(ctx
, array
, dp
, &v
);
732 }else if(V_VT(v
) == (VT_ARRAY
|VT_BYREF
|VT_VARIANT
)) {
733 FIXME("non-array assign\n");
737 hres
= VariantCopyInd(v
, dp
->rgvarg
);
741 hres
= disp_propput(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, dp
);
744 FIXME("functions not implemented\n");
750 FIXME("REF_CONST\n");
753 if(ctx
->func
->code_ctx
->option_explicit
) {
754 FIXME("throw exception\n");
758 FIXME("arg_cnt %d not supported\n", arg_cnt(dp
));
762 TRACE("creating variable %s\n", debugstr_w(name
));
763 hres
= add_dynamic_var(ctx
, name
, FALSE
, dp
->rgvarg
, FALSE
, NULL
);
770 static HRESULT
interp_assign_ident(exec_ctx_t
*ctx
)
772 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
773 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
777 TRACE("%s\n", debugstr_w(arg
));
779 hres
= stack_assume_val(ctx
, arg_cnt
);
783 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
784 hres
= assign_ident(ctx
, arg
, &dp
);
788 stack_popn(ctx
, arg_cnt
+1);
792 static HRESULT
interp_set_ident(exec_ctx_t
*ctx
)
794 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
795 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
799 TRACE("%s\n", debugstr_w(arg
));
802 FIXME("arguments not supported\n");
806 hres
= stack_assume_disp(ctx
, 0, NULL
);
810 vbstack_to_dp(ctx
, 0, TRUE
, &dp
);
811 hres
= assign_ident(ctx
, ctx
->instr
->arg1
.bstr
, &dp
);
819 static HRESULT
interp_assign_member(exec_ctx_t
*ctx
)
821 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
822 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
828 TRACE("%s\n", debugstr_w(identifier
));
830 hres
= stack_assume_disp(ctx
, arg_cnt
+1, &obj
);
839 hres
= stack_assume_val(ctx
, arg_cnt
);
843 hres
= disp_get_id(obj
, identifier
, VBDISP_LET
, FALSE
, &id
);
844 if(SUCCEEDED(hres
)) {
845 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
846 hres
= disp_propput(ctx
->script
, obj
, id
, &dp
);
851 stack_popn(ctx
, arg_cnt
+2);
855 static HRESULT
interp_set_member(exec_ctx_t
*ctx
)
857 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
858 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
864 TRACE("%s\n", debugstr_w(identifier
));
867 FIXME("arguments not supported\n");
871 hres
= stack_assume_disp(ctx
, 1, &obj
);
880 hres
= stack_assume_disp(ctx
, 0, NULL
);
884 hres
= disp_get_id(obj
, identifier
, VBDISP_SET
, FALSE
, &id
);
885 if(SUCCEEDED(hres
)) {
886 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
887 hres
= disp_propput(ctx
->script
, obj
, id
, &dp
);
896 static HRESULT
interp_const(exec_ctx_t
*ctx
)
898 BSTR arg
= ctx
->instr
->arg1
.bstr
;
903 TRACE("%s\n", debugstr_w(arg
));
905 assert(ctx
->func
->type
== FUNC_GLOBAL
);
907 hres
= lookup_identifier(ctx
, arg
, VBDISP_CALLGET
, &ref
);
911 if(ref
.type
!= REF_NONE
) {
912 FIXME("%s already defined\n", debugstr_w(arg
));
916 hres
= stack_pop_val(ctx
, &val
);
920 return add_dynamic_var(ctx
, arg
, TRUE
, val
.v
, val
.owned
, NULL
);
923 static HRESULT
interp_val(exec_ctx_t
*ctx
)
931 hres
= stack_pop_val(ctx
, &val
);
937 hres
= VariantCopy(&v
, val
.v
);
942 return stack_push(ctx
, val
.owned
? val
.v
: &v
);
945 static HRESULT
interp_pop(exec_ctx_t
*ctx
)
947 const unsigned n
= ctx
->instr
->arg1
.uint
;
955 static HRESULT
interp_new(exec_ctx_t
*ctx
)
957 const WCHAR
*arg
= ctx
->instr
->arg1
.bstr
;
958 class_desc_t
*class_desc
;
963 TRACE("%s\n", debugstr_w(arg
));
965 for(class_desc
= ctx
->script
->classes
; class_desc
; class_desc
= class_desc
->next
) {
966 if(!strcmpiW(class_desc
->name
, arg
))
970 FIXME("Class %s not found\n", debugstr_w(arg
));
974 hres
= create_vbdisp(class_desc
, &obj
);
978 V_VT(&v
) = VT_DISPATCH
;
979 V_DISPATCH(&v
) = (IDispatch
*)&obj
->IDispatchEx_iface
;
980 return stack_push(ctx
, &v
);
983 static HRESULT
interp_dim(exec_ctx_t
*ctx
)
985 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
986 const unsigned array_id
= ctx
->instr
->arg2
.uint
;
990 TRACE("%s\n", debugstr_w(ident
));
992 assert(array_id
< ctx
->func
->array_cnt
);
994 ctx
->arrays
= heap_alloc_zero(ctx
->func
->array_cnt
* sizeof(SAFEARRAY
*));
996 return E_OUTOFMEMORY
;
999 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1001 FIXME("lookup %s failed: %08x\n", debugstr_w(ident
), hres
);
1005 if(ref
.type
!= REF_VAR
) {
1006 FIXME("got ref.type = %d\n", ref
.type
);
1010 if(!ctx
->arrays
[array_id
]) {
1011 const array_desc_t
*array_desc
;
1013 array_desc
= ctx
->func
->array_descs
+ array_id
;
1014 if(array_desc
->dim_cnt
) {
1015 ctx
->arrays
[array_id
] = SafeArrayCreate(VT_VARIANT
, array_desc
->dim_cnt
, array_desc
->bounds
);
1016 if(!ctx
->arrays
[array_id
])
1017 return E_OUTOFMEMORY
;
1021 V_VT(ref
.u
.v
) = VT_ARRAY
|VT_BYREF
|VT_VARIANT
;
1022 V_ARRAYREF(ref
.u
.v
) = ctx
->arrays
+array_id
;
1026 static HRESULT
interp_step(exec_ctx_t
*ctx
)
1028 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1034 TRACE("%s\n", debugstr_w(ident
));
1036 V_VT(&zero
) = VT_I2
;
1038 hres
= VarCmp(stack_top(ctx
, 0), &zero
, ctx
->script
->lcid
, 0);
1042 gteq_zero
= hres
== VARCMP_GT
|| hres
== VARCMP_EQ
;
1044 hres
= lookup_identifier(ctx
, ident
, VBDISP_ANY
, &ref
);
1048 if(ref
.type
!= REF_VAR
) {
1049 FIXME("%s is not REF_VAR\n", debugstr_w(ident
));
1053 hres
= VarCmp(ref
.u
.v
, stack_top(ctx
, 1), ctx
->script
->lcid
, 0);
1057 if(hres
== VARCMP_EQ
|| hres
== (gteq_zero
? VARCMP_LT
: VARCMP_GT
)) {
1061 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1066 static HRESULT
interp_newenum(exec_ctx_t
*ctx
)
1077 DISPPARAMS dp
= {0};
1080 hres
= disp_call(ctx
->script
, V_DISPATCH(v
), DISPID_NEWENUM
, &dp
, &iterv
);
1085 if(V_VT(&iterv
) != VT_UNKNOWN
&& V_VT(&iterv
) != VT_DISPATCH
) {
1086 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv
));
1087 VariantClear(&iterv
);
1091 hres
= IUnknown_QueryInterface(V_UNKNOWN(&iterv
), &IID_IEnumVARIANT
, (void**)&iter
);
1092 IUnknown_Release(V_UNKNOWN(&iterv
));
1094 FIXME("Could not get IEnumVARIANT iface: %08x\n", hres
);
1098 V_VT(&r
) = VT_UNKNOWN
;
1099 V_UNKNOWN(&r
) = (IUnknown
*)iter
;
1103 FIXME("Unsupported for %s\n", debugstr_variant(v
));
1108 return stack_push(ctx
, &r
);
1111 static HRESULT
interp_enumnext(exec_ctx_t
*ctx
)
1113 const unsigned loop_end
= ctx
->instr
->arg1
.uint
;
1114 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1116 DISPPARAMS dp
= {&v
, &propput_dispid
, 1, 1};
1123 assert(V_VT(stack_top(ctx
, 0)) == VT_UNKNOWN
);
1124 iter
= (IEnumVARIANT
*)V_UNKNOWN(stack_top(ctx
, 0));
1126 V_VT(&v
) = VT_EMPTY
;
1127 hres
= IEnumVARIANT_Next(iter
, 1, &v
, NULL
);
1131 do_continue
= hres
== S_OK
;
1132 hres
= assign_ident(ctx
, ident
, &dp
);
1141 instr_jmp(ctx
, loop_end
);
1146 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
1148 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1152 instr_jmp(ctx
, arg
);
1156 static HRESULT
interp_jmp_false(exec_ctx_t
*ctx
)
1158 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1164 hres
= stack_pop_bool(ctx
, &b
);
1171 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1175 static HRESULT
interp_jmp_true(exec_ctx_t
*ctx
)
1177 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1183 hres
= stack_pop_bool(ctx
, &b
);
1188 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1194 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
1202 static HRESULT
interp_stop(exec_ctx_t
*ctx
)
1206 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1210 static HRESULT
interp_me(exec_ctx_t
*ctx
)
1216 IDispatch_AddRef(ctx
->this_obj
);
1217 V_VT(&v
) = VT_DISPATCH
;
1218 V_DISPATCH(&v
) = ctx
->this_obj
;
1219 return stack_push(ctx
, &v
);
1222 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
1224 const VARIANT_BOOL arg
= ctx
->instr
->arg1
.lng
;
1227 TRACE("%s\n", arg
? "true" : "false");
1231 return stack_push(ctx
, &v
);
1234 static HRESULT
interp_errmode(exec_ctx_t
*ctx
)
1236 const int err_mode
= ctx
->instr
->arg1
.uint
;
1238 TRACE("%d\n", err_mode
);
1240 ctx
->resume_next
= err_mode
;
1244 static HRESULT
interp_string(exec_ctx_t
*ctx
)
1251 V_BSTR(&v
) = SysAllocString(ctx
->instr
->arg1
.str
);
1253 return E_OUTOFMEMORY
;
1255 return stack_push(ctx
, &v
);
1258 static HRESULT
interp_long(exec_ctx_t
*ctx
)
1260 const LONG arg
= ctx
->instr
->arg1
.lng
;
1267 return stack_push(ctx
, &v
);
1270 static HRESULT
interp_short(exec_ctx_t
*ctx
)
1272 const LONG arg
= ctx
->instr
->arg1
.lng
;
1279 return stack_push(ctx
, &v
);
1282 static HRESULT
interp_double(exec_ctx_t
*ctx
)
1284 const DOUBLE
*arg
= ctx
->instr
->arg1
.dbl
;
1287 TRACE("%lf\n", *arg
);
1291 return stack_push(ctx
, &v
);
1294 static HRESULT
interp_empty(exec_ctx_t
*ctx
)
1300 V_VT(&v
) = VT_EMPTY
;
1301 return stack_push(ctx
, &v
);
1304 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1307 return stack_push_null(ctx
);
1310 static HRESULT
interp_nothing(exec_ctx_t
*ctx
)
1316 V_VT(&v
) = VT_DISPATCH
;
1317 V_DISPATCH(&v
) = NULL
;
1318 return stack_push(ctx
, &v
);
1321 static HRESULT
interp_not(exec_ctx_t
*ctx
)
1329 hres
= stack_pop_val(ctx
, &val
);
1333 hres
= VarNot(val
.v
, &v
);
1338 return stack_push(ctx
, &v
);
1341 static HRESULT
interp_and(exec_ctx_t
*ctx
)
1349 hres
= stack_pop_val(ctx
, &r
);
1353 hres
= stack_pop_val(ctx
, &l
);
1354 if(SUCCEEDED(hres
)) {
1355 hres
= VarAnd(l
.v
, r
.v
, &v
);
1362 return stack_push(ctx
, &v
);
1365 static HRESULT
interp_or(exec_ctx_t
*ctx
)
1373 hres
= stack_pop_val(ctx
, &r
);
1377 hres
= stack_pop_val(ctx
, &l
);
1378 if(SUCCEEDED(hres
)) {
1379 hres
= VarOr(l
.v
, r
.v
, &v
);
1386 return stack_push(ctx
, &v
);
1389 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
1397 hres
= stack_pop_val(ctx
, &r
);
1401 hres
= stack_pop_val(ctx
, &l
);
1402 if(SUCCEEDED(hres
)) {
1403 hres
= VarXor(l
.v
, r
.v
, &v
);
1410 return stack_push(ctx
, &v
);
1413 static HRESULT
interp_eqv(exec_ctx_t
*ctx
)
1421 hres
= stack_pop_val(ctx
, &r
);
1425 hres
= stack_pop_val(ctx
, &l
);
1426 if(SUCCEEDED(hres
)) {
1427 hres
= VarEqv(l
.v
, r
.v
, &v
);
1434 return stack_push(ctx
, &v
);
1437 static HRESULT
interp_imp(exec_ctx_t
*ctx
)
1445 hres
= stack_pop_val(ctx
, &r
);
1449 hres
= stack_pop_val(ctx
, &l
);
1450 if(SUCCEEDED(hres
)) {
1451 hres
= VarImp(l
.v
, r
.v
, &v
);
1458 return stack_push(ctx
, &v
);
1461 static HRESULT
var_cmp(exec_ctx_t
*ctx
, VARIANT
*l
, VARIANT
*r
)
1463 TRACE("%s %s\n", debugstr_variant(l
), debugstr_variant(r
));
1465 /* FIXME: Fix comparing string to number */
1467 return VarCmp(l
, r
, ctx
->script
->lcid
, 0);
1470 static HRESULT
cmp_oper(exec_ctx_t
*ctx
)
1475 hres
= stack_pop_val(ctx
, &r
);
1479 hres
= stack_pop_val(ctx
, &l
);
1480 if(SUCCEEDED(hres
)) {
1481 hres
= var_cmp(ctx
, l
.v
, r
.v
);
1489 static HRESULT
interp_equal(exec_ctx_t
*ctx
)
1496 hres
= cmp_oper(ctx
);
1499 if(hres
== VARCMP_NULL
)
1500 return stack_push_null(ctx
);
1503 V_BOOL(&v
) = hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1504 return stack_push(ctx
, &v
);
1507 static HRESULT
interp_nequal(exec_ctx_t
*ctx
)
1514 hres
= cmp_oper(ctx
);
1517 if(hres
== VARCMP_NULL
)
1518 return stack_push_null(ctx
);
1521 V_BOOL(&v
) = hres
!= VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1522 return stack_push(ctx
, &v
);
1525 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
1532 hres
= cmp_oper(ctx
);
1535 if(hres
== VARCMP_NULL
)
1536 return stack_push_null(ctx
);
1539 V_BOOL(&v
) = hres
== VARCMP_GT
? VARIANT_TRUE
: VARIANT_FALSE
;
1540 return stack_push(ctx
, &v
);
1543 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
1550 hres
= cmp_oper(ctx
);
1553 if(hres
== VARCMP_NULL
)
1554 return stack_push_null(ctx
);
1557 V_BOOL(&v
) = hres
== VARCMP_GT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1558 return stack_push(ctx
, &v
);
1561 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
1568 hres
= cmp_oper(ctx
);
1571 if(hres
== VARCMP_NULL
)
1572 return stack_push_null(ctx
);
1575 V_BOOL(&v
) = hres
== VARCMP_LT
? VARIANT_TRUE
: VARIANT_FALSE
;
1576 return stack_push(ctx
, &v
);
1579 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
1586 hres
= cmp_oper(ctx
);
1589 if(hres
== VARCMP_NULL
)
1590 return stack_push_null(ctx
);
1593 V_BOOL(&v
) = hres
== VARCMP_LT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1594 return stack_push(ctx
, &v
);
1597 static HRESULT
interp_case(exec_ctx_t
*ctx
)
1599 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1605 hres
= stack_pop_val(ctx
, &v
);
1609 hres
= var_cmp(ctx
, stack_top(ctx
, 0), v
.v
);
1614 if(hres
== VARCMP_EQ
) {
1616 instr_jmp(ctx
, arg
);
1624 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, VARIANT_BOOL
*ret
)
1626 IObjectIdentity
*identity
;
1627 IUnknown
*unk1
, *unk2
;
1630 if(disp1
== disp2
) {
1631 *ret
= VARIANT_TRUE
;
1635 if(!disp1
|| !disp2
) {
1636 *ret
= VARIANT_FALSE
;
1640 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
1644 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
1646 IUnknown_Release(unk1
);
1651 *ret
= VARIANT_TRUE
;
1653 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
1654 if(SUCCEEDED(hres
)) {
1655 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
1656 IObjectIdentity_Release(identity
);
1657 *ret
= hres
== S_OK
? VARIANT_TRUE
: VARIANT_FALSE
;
1659 *ret
= VARIANT_FALSE
;
1663 IUnknown_Release(unk1
);
1664 IUnknown_Release(unk2
);
1668 static HRESULT
interp_is(exec_ctx_t
*ctx
)
1676 hres
= stack_pop_disp(ctx
, &r
);
1680 hres
= stack_pop_disp(ctx
, &l
);
1681 if(SUCCEEDED(hres
)) {
1683 hres
= disp_cmp(l
, r
, &V_BOOL(&v
));
1685 IDispatch_Release(l
);
1688 IDispatch_Release(r
);
1692 return stack_push(ctx
, &v
);
1695 static HRESULT
interp_concat(exec_ctx_t
*ctx
)
1703 hres
= stack_pop_val(ctx
, &r
);
1707 hres
= stack_pop_val(ctx
, &l
);
1708 if(SUCCEEDED(hres
)) {
1709 hres
= VarCat(l
.v
, r
.v
, &v
);
1716 return stack_push(ctx
, &v
);
1719 static HRESULT
interp_add(exec_ctx_t
*ctx
)
1727 hres
= stack_pop_val(ctx
, &r
);
1731 hres
= stack_pop_val(ctx
, &l
);
1732 if(SUCCEEDED(hres
)) {
1733 hres
= VarAdd(l
.v
, r
.v
, &v
);
1740 return stack_push(ctx
, &v
);
1743 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
1751 hres
= stack_pop_val(ctx
, &r
);
1755 hres
= stack_pop_val(ctx
, &l
);
1756 if(SUCCEEDED(hres
)) {
1757 hres
= VarSub(l
.v
, r
.v
, &v
);
1764 return stack_push(ctx
, &v
);
1767 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
1775 hres
= stack_pop_val(ctx
, &r
);
1779 hres
= stack_pop_val(ctx
, &l
);
1780 if(SUCCEEDED(hres
)) {
1781 hres
= VarMod(l
.v
, r
.v
, &v
);
1788 return stack_push(ctx
, &v
);
1791 static HRESULT
interp_idiv(exec_ctx_t
*ctx
)
1799 hres
= stack_pop_val(ctx
, &r
);
1803 hres
= stack_pop_val(ctx
, &l
);
1804 if(SUCCEEDED(hres
)) {
1805 hres
= VarIdiv(l
.v
, r
.v
, &v
);
1812 return stack_push(ctx
, &v
);
1815 static HRESULT
interp_div(exec_ctx_t
*ctx
)
1823 hres
= stack_pop_val(ctx
, &r
);
1827 hres
= stack_pop_val(ctx
, &l
);
1828 if(SUCCEEDED(hres
)) {
1829 hres
= VarDiv(l
.v
, r
.v
, &v
);
1836 return stack_push(ctx
, &v
);
1839 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
1847 hres
= stack_pop_val(ctx
, &r
);
1851 hres
= stack_pop_val(ctx
, &l
);
1852 if(SUCCEEDED(hres
)) {
1853 hres
= VarMul(l
.v
, r
.v
, &v
);
1860 return stack_push(ctx
, &v
);
1863 static HRESULT
interp_exp(exec_ctx_t
*ctx
)
1871 hres
= stack_pop_val(ctx
, &r
);
1875 hres
= stack_pop_val(ctx
, &l
);
1876 if(SUCCEEDED(hres
)) {
1877 hres
= VarPow(l
.v
, r
.v
, &v
);
1884 return stack_push(ctx
, &v
);
1887 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
1893 hres
= stack_pop_val(ctx
, &val
);
1897 hres
= VarNeg(val
.v
, &v
);
1902 return stack_push(ctx
, &v
);
1905 static HRESULT
interp_incc(exec_ctx_t
*ctx
)
1907 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
1914 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1918 if(ref
.type
!= REF_VAR
) {
1919 FIXME("ref.type is not REF_VAR\n");
1923 hres
= VarAdd(stack_top(ctx
, 0), ref
.u
.v
, &v
);
1927 VariantClear(ref
.u
.v
);
1932 static const instr_func_t op_funcs
[] = {
1933 #define X(x,n,a,b) interp_ ## x,
1938 static const unsigned op_move
[] = {
1939 #define X(x,n,a,b) n,
1944 void release_dynamic_vars(dynamic_var_t
*var
)
1947 VariantClear(&var
->v
);
1952 static void release_exec(exec_ctx_t
*ctx
)
1956 VariantClear(&ctx
->ret_val
);
1957 release_dynamic_vars(ctx
->dynamic_vars
);
1960 IDispatch_Release(ctx
->this_obj
);
1963 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++)
1964 VariantClear(ctx
->args
+i
);
1968 for(i
=0; i
< ctx
->func
->var_cnt
; i
++)
1969 VariantClear(ctx
->vars
+i
);
1973 for(i
=0; i
< ctx
->func
->var_cnt
; i
++) {
1975 SafeArrayDestroy(ctx
->arrays
[i
]);
1977 heap_free(ctx
->arrays
);
1980 heap_pool_free(&ctx
->heap
);
1981 heap_free(ctx
->args
);
1982 heap_free(ctx
->vars
);
1983 heap_free(ctx
->stack
);
1986 HRESULT
exec_script(script_ctx_t
*ctx
, function_t
*func
, IDispatch
*this_obj
, DISPPARAMS
*dp
, VARIANT
*res
)
1988 exec_ctx_t exec
= {func
->code_ctx
};
1990 HRESULT hres
= S_OK
;
1992 exec
.code
= func
->code_ctx
;
1994 if(dp
? func
->arg_cnt
!= arg_cnt(dp
) : func
->arg_cnt
) {
1995 FIXME("wrong arg_cnt %d, expected %d\n", dp
? arg_cnt(dp
) : 0, func
->arg_cnt
);
1999 heap_pool_init(&exec
.heap
);
2005 exec
.args
= heap_alloc_zero(func
->arg_cnt
* sizeof(VARIANT
));
2007 release_exec(&exec
);
2008 return E_OUTOFMEMORY
;
2011 for(i
=0; i
< func
->arg_cnt
; i
++) {
2013 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
)) {
2014 if(func
->args
[i
].by_ref
)
2017 hres
= VariantCopy(exec
.args
+i
, V_VARIANTREF(v
));
2019 hres
= VariantCopy(exec
.args
+i
, v
);
2022 release_exec(&exec
);
2031 exec
.vars
= heap_alloc_zero(func
->var_cnt
* sizeof(VARIANT
));
2033 release_exec(&exec
);
2034 return E_OUTOFMEMORY
;
2040 exec
.stack_size
= 16;
2042 exec
.stack
= heap_alloc(exec
.stack_size
* sizeof(VARIANT
));
2044 release_exec(&exec
);
2045 return E_OUTOFMEMORY
;
2049 exec
.this_obj
= this_obj
;
2050 else if (ctx
->host_global
)
2051 exec
.this_obj
= ctx
->host_global
;
2053 exec
.this_obj
= (IDispatch
*)&ctx
->script_obj
->IDispatchEx_iface
;
2054 IDispatch_AddRef(exec
.this_obj
);
2056 exec
.instr
= exec
.code
->instrs
+ func
->code_off
;
2061 op
= exec
.instr
->op
;
2062 hres
= op_funcs
[op
](&exec
);
2064 if(exec
.resume_next
)
2065 FIXME("Failed %08x in resume next mode\n", hres
);
2067 WARN("Failed %08x\n", hres
);
2068 stack_popn(&exec
, exec
.top
);
2072 exec
.instr
+= op_move
[op
];
2076 if(func
->type
!= FUNC_FUNCTION
&& func
->type
!= FUNC_PROPGET
&& func
->type
!= FUNC_DEFGET
)
2077 assert(V_VT(&exec
.ret_val
) == VT_EMPTY
);
2079 if(SUCCEEDED(hres
) && res
) {
2080 *res
= exec
.ret_val
;
2081 V_VT(&exec
.ret_val
) = VT_EMPTY
;
2084 release_exec(&exec
);