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\n", 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
;
987 const array_desc_t
*array_desc
;
991 TRACE("%s\n", debugstr_w(ident
));
993 assert(array_id
< ctx
->func
->array_cnt
);
995 ctx
->arrays
= heap_alloc_zero(ctx
->func
->array_cnt
* sizeof(SAFEARRAY
*));
997 return E_OUTOFMEMORY
;
1000 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1002 FIXME("lookup %s failed: %08x\n", debugstr_w(ident
), hres
);
1006 if(ref
.type
!= REF_VAR
) {
1007 FIXME("got ref.type = %d\n", ref
.type
);
1011 if(ctx
->arrays
[array_id
]) {
1012 FIXME("Array already initialized\n");
1016 array_desc
= ctx
->func
->array_descs
+ array_id
;
1017 if(array_desc
->dim_cnt
) {
1018 ctx
->arrays
[array_id
] = SafeArrayCreate(VT_VARIANT
, array_desc
->dim_cnt
, array_desc
->bounds
);
1019 if(!ctx
->arrays
[array_id
])
1020 return E_OUTOFMEMORY
;
1023 V_VT(ref
.u
.v
) = VT_ARRAY
|VT_BYREF
|VT_VARIANT
;
1024 V_ARRAYREF(ref
.u
.v
) = ctx
->arrays
+array_id
;
1028 static HRESULT
interp_step(exec_ctx_t
*ctx
)
1030 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1036 TRACE("%s\n", debugstr_w(ident
));
1038 V_VT(&zero
) = VT_I2
;
1040 hres
= VarCmp(stack_top(ctx
, 0), &zero
, ctx
->script
->lcid
, 0);
1044 gteq_zero
= hres
== VARCMP_GT
|| hres
== VARCMP_EQ
;
1046 hres
= lookup_identifier(ctx
, ident
, VBDISP_ANY
, &ref
);
1050 if(ref
.type
!= REF_VAR
) {
1051 FIXME("%s is not REF_VAR\n", debugstr_w(ident
));
1055 hres
= VarCmp(ref
.u
.v
, stack_top(ctx
, 1), ctx
->script
->lcid
, 0);
1059 if(hres
== VARCMP_EQ
|| hres
== (gteq_zero
? VARCMP_LT
: VARCMP_GT
)) {
1063 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1068 static HRESULT
interp_newenum(exec_ctx_t
*ctx
)
1079 DISPPARAMS dp
= {0};
1082 hres
= disp_call(ctx
->script
, V_DISPATCH(v
), DISPID_NEWENUM
, &dp
, &iterv
);
1087 if(V_VT(&iterv
) != VT_UNKNOWN
&& V_VT(&iterv
) != VT_DISPATCH
) {
1088 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv
));
1089 VariantClear(&iterv
);
1093 hres
= IUnknown_QueryInterface(V_UNKNOWN(&iterv
), &IID_IEnumVARIANT
, (void**)&iter
);
1094 IUnknown_Release(V_UNKNOWN(&iterv
));
1096 FIXME("Could not get IEnumVARIANT iface: %08x\n", hres
);
1100 V_VT(&r
) = VT_UNKNOWN
;
1101 V_UNKNOWN(&r
) = (IUnknown
*)iter
;
1105 FIXME("Unsupported for %s\n", debugstr_variant(v
));
1110 return stack_push(ctx
, &r
);
1113 static HRESULT
interp_enumnext(exec_ctx_t
*ctx
)
1115 const unsigned loop_end
= ctx
->instr
->arg1
.uint
;
1116 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1118 DISPPARAMS dp
= {&v
, &propput_dispid
, 1, 1};
1125 assert(V_VT(stack_top(ctx
, 0)) == VT_UNKNOWN
);
1126 iter
= (IEnumVARIANT
*)V_UNKNOWN(stack_top(ctx
, 0));
1128 V_VT(&v
) = VT_EMPTY
;
1129 hres
= IEnumVARIANT_Next(iter
, 1, &v
, NULL
);
1133 do_continue
= hres
== S_OK
;
1134 hres
= assign_ident(ctx
, ident
, &dp
);
1143 instr_jmp(ctx
, loop_end
);
1148 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
1150 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1154 instr_jmp(ctx
, arg
);
1158 static HRESULT
interp_jmp_false(exec_ctx_t
*ctx
)
1160 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1166 hres
= stack_pop_bool(ctx
, &b
);
1173 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1177 static HRESULT
interp_jmp_true(exec_ctx_t
*ctx
)
1179 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1185 hres
= stack_pop_bool(ctx
, &b
);
1190 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1196 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
1204 static HRESULT
interp_stop(exec_ctx_t
*ctx
)
1208 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1212 static HRESULT
interp_me(exec_ctx_t
*ctx
)
1218 IDispatch_AddRef(ctx
->this_obj
);
1219 V_VT(&v
) = VT_DISPATCH
;
1220 V_DISPATCH(&v
) = ctx
->this_obj
;
1221 return stack_push(ctx
, &v
);
1224 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
1226 const VARIANT_BOOL arg
= ctx
->instr
->arg1
.lng
;
1229 TRACE("%s\n", arg
? "true" : "false");
1233 return stack_push(ctx
, &v
);
1236 static HRESULT
interp_errmode(exec_ctx_t
*ctx
)
1238 const int err_mode
= ctx
->instr
->arg1
.uint
;
1240 TRACE("%d\n", err_mode
);
1242 ctx
->resume_next
= err_mode
;
1246 static HRESULT
interp_string(exec_ctx_t
*ctx
)
1253 V_BSTR(&v
) = SysAllocString(ctx
->instr
->arg1
.str
);
1255 return E_OUTOFMEMORY
;
1257 return stack_push(ctx
, &v
);
1260 static HRESULT
interp_long(exec_ctx_t
*ctx
)
1262 const LONG arg
= ctx
->instr
->arg1
.lng
;
1269 return stack_push(ctx
, &v
);
1272 static HRESULT
interp_short(exec_ctx_t
*ctx
)
1274 const LONG arg
= ctx
->instr
->arg1
.lng
;
1281 return stack_push(ctx
, &v
);
1284 static HRESULT
interp_double(exec_ctx_t
*ctx
)
1286 const DOUBLE
*arg
= ctx
->instr
->arg1
.dbl
;
1289 TRACE("%lf\n", *arg
);
1293 return stack_push(ctx
, &v
);
1296 static HRESULT
interp_empty(exec_ctx_t
*ctx
)
1302 V_VT(&v
) = VT_EMPTY
;
1303 return stack_push(ctx
, &v
);
1306 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1309 return stack_push_null(ctx
);
1312 static HRESULT
interp_nothing(exec_ctx_t
*ctx
)
1318 V_VT(&v
) = VT_DISPATCH
;
1319 V_DISPATCH(&v
) = NULL
;
1320 return stack_push(ctx
, &v
);
1323 static HRESULT
interp_not(exec_ctx_t
*ctx
)
1331 hres
= stack_pop_val(ctx
, &val
);
1335 hres
= VarNot(val
.v
, &v
);
1340 return stack_push(ctx
, &v
);
1343 static HRESULT
interp_and(exec_ctx_t
*ctx
)
1351 hres
= stack_pop_val(ctx
, &r
);
1355 hres
= stack_pop_val(ctx
, &l
);
1356 if(SUCCEEDED(hres
)) {
1357 hres
= VarAnd(l
.v
, r
.v
, &v
);
1364 return stack_push(ctx
, &v
);
1367 static HRESULT
interp_or(exec_ctx_t
*ctx
)
1375 hres
= stack_pop_val(ctx
, &r
);
1379 hres
= stack_pop_val(ctx
, &l
);
1380 if(SUCCEEDED(hres
)) {
1381 hres
= VarOr(l
.v
, r
.v
, &v
);
1388 return stack_push(ctx
, &v
);
1391 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
1399 hres
= stack_pop_val(ctx
, &r
);
1403 hres
= stack_pop_val(ctx
, &l
);
1404 if(SUCCEEDED(hres
)) {
1405 hres
= VarXor(l
.v
, r
.v
, &v
);
1412 return stack_push(ctx
, &v
);
1415 static HRESULT
interp_eqv(exec_ctx_t
*ctx
)
1423 hres
= stack_pop_val(ctx
, &r
);
1427 hres
= stack_pop_val(ctx
, &l
);
1428 if(SUCCEEDED(hres
)) {
1429 hres
= VarEqv(l
.v
, r
.v
, &v
);
1436 return stack_push(ctx
, &v
);
1439 static HRESULT
interp_imp(exec_ctx_t
*ctx
)
1447 hres
= stack_pop_val(ctx
, &r
);
1451 hres
= stack_pop_val(ctx
, &l
);
1452 if(SUCCEEDED(hres
)) {
1453 hres
= VarImp(l
.v
, r
.v
, &v
);
1460 return stack_push(ctx
, &v
);
1463 static HRESULT
var_cmp(exec_ctx_t
*ctx
, VARIANT
*l
, VARIANT
*r
)
1465 TRACE("%s %s\n", debugstr_variant(l
), debugstr_variant(r
));
1467 /* FIXME: Fix comparing string to number */
1469 return VarCmp(l
, r
, ctx
->script
->lcid
, 0);
1472 static HRESULT
cmp_oper(exec_ctx_t
*ctx
)
1477 hres
= stack_pop_val(ctx
, &r
);
1481 hres
= stack_pop_val(ctx
, &l
);
1482 if(SUCCEEDED(hres
)) {
1483 hres
= var_cmp(ctx
, l
.v
, r
.v
);
1491 static HRESULT
interp_equal(exec_ctx_t
*ctx
)
1498 hres
= cmp_oper(ctx
);
1501 if(hres
== VARCMP_NULL
)
1502 return stack_push_null(ctx
);
1505 V_BOOL(&v
) = hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1506 return stack_push(ctx
, &v
);
1509 static HRESULT
interp_nequal(exec_ctx_t
*ctx
)
1516 hres
= cmp_oper(ctx
);
1519 if(hres
== VARCMP_NULL
)
1520 return stack_push_null(ctx
);
1523 V_BOOL(&v
) = hres
!= VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1524 return stack_push(ctx
, &v
);
1527 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
1534 hres
= cmp_oper(ctx
);
1537 if(hres
== VARCMP_NULL
)
1538 return stack_push_null(ctx
);
1541 V_BOOL(&v
) = hres
== VARCMP_GT
? VARIANT_TRUE
: VARIANT_FALSE
;
1542 return stack_push(ctx
, &v
);
1545 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
1552 hres
= cmp_oper(ctx
);
1555 if(hres
== VARCMP_NULL
)
1556 return stack_push_null(ctx
);
1559 V_BOOL(&v
) = hres
== VARCMP_GT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1560 return stack_push(ctx
, &v
);
1563 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
1570 hres
= cmp_oper(ctx
);
1573 if(hres
== VARCMP_NULL
)
1574 return stack_push_null(ctx
);
1577 V_BOOL(&v
) = hres
== VARCMP_LT
? VARIANT_TRUE
: VARIANT_FALSE
;
1578 return stack_push(ctx
, &v
);
1581 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
1588 hres
= cmp_oper(ctx
);
1591 if(hres
== VARCMP_NULL
)
1592 return stack_push_null(ctx
);
1595 V_BOOL(&v
) = hres
== VARCMP_LT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1596 return stack_push(ctx
, &v
);
1599 static HRESULT
interp_case(exec_ctx_t
*ctx
)
1601 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1607 hres
= stack_pop_val(ctx
, &v
);
1611 hres
= var_cmp(ctx
, stack_top(ctx
, 0), v
.v
);
1616 if(hres
== VARCMP_EQ
) {
1618 instr_jmp(ctx
, arg
);
1626 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, VARIANT_BOOL
*ret
)
1628 IObjectIdentity
*identity
;
1629 IUnknown
*unk1
, *unk2
;
1632 if(disp1
== disp2
) {
1633 *ret
= VARIANT_TRUE
;
1637 if(!disp1
|| !disp2
) {
1638 *ret
= VARIANT_FALSE
;
1642 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
1646 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
1648 IUnknown_Release(unk1
);
1653 *ret
= VARIANT_TRUE
;
1655 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
1656 if(SUCCEEDED(hres
)) {
1657 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
1658 IObjectIdentity_Release(identity
);
1659 *ret
= hres
== S_OK
? VARIANT_TRUE
: VARIANT_FALSE
;
1661 *ret
= VARIANT_FALSE
;
1665 IUnknown_Release(unk1
);
1666 IUnknown_Release(unk2
);
1670 static HRESULT
interp_is(exec_ctx_t
*ctx
)
1678 hres
= stack_pop_disp(ctx
, &r
);
1682 hres
= stack_pop_disp(ctx
, &l
);
1683 if(SUCCEEDED(hres
)) {
1685 hres
= disp_cmp(l
, r
, &V_BOOL(&v
));
1687 IDispatch_Release(l
);
1690 IDispatch_Release(r
);
1694 return stack_push(ctx
, &v
);
1697 static HRESULT
interp_concat(exec_ctx_t
*ctx
)
1705 hres
= stack_pop_val(ctx
, &r
);
1709 hres
= stack_pop_val(ctx
, &l
);
1710 if(SUCCEEDED(hres
)) {
1711 hres
= VarCat(l
.v
, r
.v
, &v
);
1718 return stack_push(ctx
, &v
);
1721 static HRESULT
interp_add(exec_ctx_t
*ctx
)
1729 hres
= stack_pop_val(ctx
, &r
);
1733 hres
= stack_pop_val(ctx
, &l
);
1734 if(SUCCEEDED(hres
)) {
1735 hres
= VarAdd(l
.v
, r
.v
, &v
);
1742 return stack_push(ctx
, &v
);
1745 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
1753 hres
= stack_pop_val(ctx
, &r
);
1757 hres
= stack_pop_val(ctx
, &l
);
1758 if(SUCCEEDED(hres
)) {
1759 hres
= VarSub(l
.v
, r
.v
, &v
);
1766 return stack_push(ctx
, &v
);
1769 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
1777 hres
= stack_pop_val(ctx
, &r
);
1781 hres
= stack_pop_val(ctx
, &l
);
1782 if(SUCCEEDED(hres
)) {
1783 hres
= VarMod(l
.v
, r
.v
, &v
);
1790 return stack_push(ctx
, &v
);
1793 static HRESULT
interp_idiv(exec_ctx_t
*ctx
)
1801 hres
= stack_pop_val(ctx
, &r
);
1805 hres
= stack_pop_val(ctx
, &l
);
1806 if(SUCCEEDED(hres
)) {
1807 hres
= VarIdiv(l
.v
, r
.v
, &v
);
1814 return stack_push(ctx
, &v
);
1817 static HRESULT
interp_div(exec_ctx_t
*ctx
)
1825 hres
= stack_pop_val(ctx
, &r
);
1829 hres
= stack_pop_val(ctx
, &l
);
1830 if(SUCCEEDED(hres
)) {
1831 hres
= VarDiv(l
.v
, r
.v
, &v
);
1838 return stack_push(ctx
, &v
);
1841 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
1849 hres
= stack_pop_val(ctx
, &r
);
1853 hres
= stack_pop_val(ctx
, &l
);
1854 if(SUCCEEDED(hres
)) {
1855 hres
= VarMul(l
.v
, r
.v
, &v
);
1862 return stack_push(ctx
, &v
);
1865 static HRESULT
interp_exp(exec_ctx_t
*ctx
)
1873 hres
= stack_pop_val(ctx
, &r
);
1877 hres
= stack_pop_val(ctx
, &l
);
1878 if(SUCCEEDED(hres
)) {
1879 hres
= VarPow(l
.v
, r
.v
, &v
);
1886 return stack_push(ctx
, &v
);
1889 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
1895 hres
= stack_pop_val(ctx
, &val
);
1899 hres
= VarNeg(val
.v
, &v
);
1904 return stack_push(ctx
, &v
);
1907 static HRESULT
interp_incc(exec_ctx_t
*ctx
)
1909 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
1916 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1920 if(ref
.type
!= REF_VAR
) {
1921 FIXME("ref.type is not REF_VAR\n");
1925 hres
= VarAdd(stack_top(ctx
, 0), ref
.u
.v
, &v
);
1929 VariantClear(ref
.u
.v
);
1934 static const instr_func_t op_funcs
[] = {
1935 #define X(x,n,a,b) interp_ ## x,
1940 static const unsigned op_move
[] = {
1941 #define X(x,n,a,b) n,
1946 void release_dynamic_vars(dynamic_var_t
*var
)
1949 VariantClear(&var
->v
);
1954 static void release_exec(exec_ctx_t
*ctx
)
1958 VariantClear(&ctx
->ret_val
);
1959 release_dynamic_vars(ctx
->dynamic_vars
);
1962 IDispatch_Release(ctx
->this_obj
);
1965 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++)
1966 VariantClear(ctx
->args
+i
);
1970 for(i
=0; i
< ctx
->func
->var_cnt
; i
++)
1971 VariantClear(ctx
->vars
+i
);
1975 for(i
=0; i
< ctx
->func
->var_cnt
; i
++) {
1977 SafeArrayDestroy(ctx
->arrays
[i
]);
1979 heap_free(ctx
->arrays
);
1982 heap_pool_free(&ctx
->heap
);
1983 heap_free(ctx
->args
);
1984 heap_free(ctx
->vars
);
1985 heap_free(ctx
->stack
);
1988 HRESULT
exec_script(script_ctx_t
*ctx
, function_t
*func
, IDispatch
*this_obj
, DISPPARAMS
*dp
, VARIANT
*res
)
1990 exec_ctx_t exec
= {func
->code_ctx
};
1992 HRESULT hres
= S_OK
;
1994 exec
.code
= func
->code_ctx
;
1996 if(dp
? func
->arg_cnt
!= arg_cnt(dp
) : func
->arg_cnt
) {
1997 FIXME("wrong arg_cnt %d, expected %d\n", dp
? arg_cnt(dp
) : 0, func
->arg_cnt
);
2001 heap_pool_init(&exec
.heap
);
2007 exec
.args
= heap_alloc_zero(func
->arg_cnt
* sizeof(VARIANT
));
2009 release_exec(&exec
);
2010 return E_OUTOFMEMORY
;
2013 for(i
=0; i
< func
->arg_cnt
; i
++) {
2015 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
)) {
2016 if(func
->args
[i
].by_ref
)
2019 hres
= VariantCopy(exec
.args
+i
, V_VARIANTREF(v
));
2021 hres
= VariantCopy(exec
.args
+i
, v
);
2024 release_exec(&exec
);
2033 exec
.vars
= heap_alloc_zero(func
->var_cnt
* sizeof(VARIANT
));
2035 release_exec(&exec
);
2036 return E_OUTOFMEMORY
;
2042 exec
.stack_size
= 16;
2044 exec
.stack
= heap_alloc(exec
.stack_size
* sizeof(VARIANT
));
2046 release_exec(&exec
);
2047 return E_OUTOFMEMORY
;
2051 exec
.this_obj
= this_obj
;
2052 else if (ctx
->host_global
)
2053 exec
.this_obj
= ctx
->host_global
;
2055 exec
.this_obj
= (IDispatch
*)&ctx
->script_obj
->IDispatchEx_iface
;
2056 IDispatch_AddRef(exec
.this_obj
);
2058 exec
.instr
= exec
.code
->instrs
+ func
->code_off
;
2063 op
= exec
.instr
->op
;
2064 hres
= op_funcs
[op
](&exec
);
2066 if(exec
.resume_next
)
2067 FIXME("Failed %08x in resume next mode\n", hres
);
2069 WARN("Failed %08x\n", hres
);
2070 stack_popn(&exec
, exec
.top
);
2074 exec
.instr
+= op_move
[op
];
2078 if(func
->type
!= FUNC_FUNCTION
&& func
->type
!= FUNC_PROPGET
&& func
->type
!= FUNC_DEFGET
)
2079 assert(V_VT(&exec
.ret_val
) == VT_EMPTY
);
2081 if(SUCCEEDED(hres
) && res
) {
2082 *res
= exec
.ret_val
;
2083 V_VT(&exec
.ret_val
) = VT_EMPTY
;
2086 release_exec(&exec
);