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
*val
, BOOL own_val
, VARIANT
**out_var
)
225 dynamic_var_t
*new_var
;
231 heap
= ctx
->func
->type
== FUNC_GLOBAL
? &ctx
->script
->heap
: &ctx
->heap
;
233 new_var
= heap_pool_alloc(heap
, sizeof(*new_var
));
235 return E_OUTOFMEMORY
;
237 size
= (strlenW(name
)+1)*sizeof(WCHAR
);
238 str
= heap_pool_alloc(heap
, size
);
240 return E_OUTOFMEMORY
;
241 memcpy(str
, name
, size
);
243 new_var
->is_const
= is_const
;
248 V_VT(&new_var
->v
) = VT_EMPTY
;
249 hres
= VariantCopy(&new_var
->v
, val
);
254 if(ctx
->func
->type
== FUNC_GLOBAL
) {
255 new_var
->next
= ctx
->script
->global_vars
;
256 ctx
->script
->global_vars
= new_var
;
258 new_var
->next
= ctx
->dynamic_vars
;
259 ctx
->dynamic_vars
= new_var
;
263 *out_var
= &new_var
->v
;
268 static inline VARIANT
*stack_pop(exec_ctx_t
*ctx
)
271 return ctx
->stack
+ --ctx
->top
;
274 static inline VARIANT
*stack_top(exec_ctx_t
*ctx
, unsigned n
)
276 assert(ctx
->top
>= n
);
277 return ctx
->stack
+ (ctx
->top
-n
-1);
280 static HRESULT
stack_push(exec_ctx_t
*ctx
, VARIANT
*v
)
282 if(ctx
->stack_size
== ctx
->top
) {
285 new_stack
= heap_realloc(ctx
->stack
, ctx
->stack_size
*2*sizeof(*ctx
->stack
));
288 return E_OUTOFMEMORY
;
291 ctx
->stack
= new_stack
;
292 ctx
->stack_size
*= 2;
295 ctx
->stack
[ctx
->top
++] = *v
;
299 static inline HRESULT
stack_push_null(exec_ctx_t
*ctx
)
303 return stack_push(ctx
, &v
);
306 static void stack_popn(exec_ctx_t
*ctx
, unsigned n
)
309 VariantClear(stack_pop(ctx
));
312 static void stack_pop_deref(exec_ctx_t
*ctx
, variant_val_t
*r
)
317 if(V_VT(v
) == (VT_BYREF
|VT_VARIANT
)) {
319 r
->v
= V_VARIANTREF(v
);
326 static inline void release_val(variant_val_t
*v
)
332 static HRESULT
stack_pop_val(exec_ctx_t
*ctx
, variant_val_t
*r
)
334 stack_pop_deref(ctx
, r
);
336 if(V_VT(r
->v
) == VT_DISPATCH
) {
340 hres
= disp_call(ctx
->script
, V_DISPATCH(r
->v
), DISPID_VALUE
, &dp
, &r
->store
);
342 IDispatch_Release(V_DISPATCH(r
->v
));
353 static HRESULT
stack_assume_val(exec_ctx_t
*ctx
, unsigned n
)
355 VARIANT
*v
= stack_top(ctx
, n
);
358 if(V_VT(v
) == (VT_BYREF
|VT_VARIANT
)) {
359 VARIANT
*ref
= V_VARIANTREF(v
);
362 hres
= VariantCopy(v
, ref
);
367 if(V_VT(v
) == VT_DISPATCH
) {
371 disp
= V_DISPATCH(v
);
373 hres
= disp_call(ctx
->script
, disp
, DISPID_VALUE
, &dp
, v
);
374 IDispatch_Release(disp
);
382 static int stack_pop_bool(exec_ctx_t
*ctx
, BOOL
*b
)
387 hres
= stack_pop_val(ctx
, &val
);
406 FIXME("unsupported for %s\n", debugstr_variant(val
.v
));
413 static HRESULT
stack_pop_disp(exec_ctx_t
*ctx
, IDispatch
**ret
)
415 VARIANT
*v
= stack_pop(ctx
);
417 if(V_VT(v
) == VT_DISPATCH
) {
418 *ret
= V_DISPATCH(v
);
422 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
423 FIXME("not supported type: %s\n", debugstr_variant(v
));
429 if(V_VT(v
) != VT_DISPATCH
) {
430 FIXME("not disp %s\n", debugstr_variant(v
));
435 IDispatch_AddRef(V_DISPATCH(v
));
436 *ret
= V_DISPATCH(v
);
440 static HRESULT
stack_assume_disp(exec_ctx_t
*ctx
, unsigned n
, IDispatch
**disp
)
442 VARIANT
*v
= stack_top(ctx
, n
), *ref
;
444 if(V_VT(v
) != VT_DISPATCH
) {
445 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
446 FIXME("not supported type: %s\n", debugstr_variant(v
));
450 ref
= V_VARIANTREF(v
);
451 if(V_VT(ref
) != VT_DISPATCH
) {
452 FIXME("not disp %s\n", debugstr_variant(ref
));
456 V_VT(v
) = VT_DISPATCH
;
457 V_DISPATCH(v
) = V_DISPATCH(ref
);
459 IDispatch_AddRef(V_DISPATCH(v
));
463 *disp
= V_DISPATCH(v
);
467 static inline void instr_jmp(exec_ctx_t
*ctx
, unsigned addr
)
469 ctx
->instr
= ctx
->code
->instrs
+ addr
;
472 static void vbstack_to_dp(exec_ctx_t
*ctx
, unsigned arg_cnt
, BOOL is_propput
, DISPPARAMS
*dp
)
474 dp
->cNamedArgs
= is_propput
? 1 : 0;
475 dp
->cArgs
= arg_cnt
+ dp
->cNamedArgs
;
476 dp
->rgdispidNamedArgs
= is_propput
? &propput_dispid
: NULL
;
482 assert(ctx
->top
>= arg_cnt
);
484 for(i
=1; i
*2 <= arg_cnt
; i
++) {
485 tmp
= ctx
->stack
[ctx
->top
-i
];
486 ctx
->stack
[ctx
->top
-i
] = ctx
->stack
[ctx
->top
-arg_cnt
+i
-1];
487 ctx
->stack
[ctx
->top
-arg_cnt
+i
-1] = tmp
;
490 dp
->rgvarg
= ctx
->stack
+ ctx
->top
-dp
->cArgs
;
492 dp
->rgvarg
= is_propput
? ctx
->stack
+ctx
->top
-1 : NULL
;
496 static HRESULT
array_access(exec_ctx_t
*ctx
, SAFEARRAY
*array
, DISPPARAMS
*dp
, VARIANT
**ret
)
498 unsigned cell_off
= 0, dim_size
= 1, i
;
499 unsigned argc
= arg_cnt(dp
);
505 FIXME("NULL array\n");
509 if(array
->cDims
!= argc
) {
510 FIXME("argc %d does not match cDims %d\n", dp
->cArgs
, array
->cDims
);
514 for(i
=0; i
< argc
; i
++) {
515 hres
= to_int(get_arg(dp
, i
), &idx
);
519 idx
-= array
->rgsabound
[i
].lLbound
;
520 if(idx
>= array
->rgsabound
[i
].cElements
) {
521 FIXME("out of bound element %d in dim %d of size %d\n", idx
, i
+1, array
->rgsabound
[i
].cElements
);
525 cell_off
+= idx
*dim_size
;
526 dim_size
*= array
->rgsabound
[i
].cElements
;
529 hres
= SafeArrayAccessData(array
, (void**)&data
);
533 *ret
= data
+cell_off
;
535 SafeArrayUnaccessData(array
);
539 static HRESULT
do_icall(exec_ctx_t
*ctx
, VARIANT
*res
)
541 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
542 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
547 hres
= lookup_identifier(ctx
, identifier
, VBDISP_CALLGET
, &ref
);
557 FIXME("REF_VAR no res\n");
561 v
= V_VT(ref
.u
.v
) == (VT_VARIANT
|VT_BYREF
) ? V_VARIANTREF(ref
.u
.v
) : ref
.u
.v
;
564 SAFEARRAY
*array
= NULL
;
567 case VT_ARRAY
|VT_BYREF
|VT_VARIANT
:
568 array
= *V_ARRAYREF(ref
.u
.v
);
570 case VT_ARRAY
|VT_VARIANT
:
571 array
= V_ARRAY(ref
.u
.v
);
574 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
575 hres
= disp_call(ctx
->script
, V_DISPATCH(v
), DISPID_VALUE
, &dp
, res
);
580 FIXME("arguments not implemented\n");
587 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
588 hres
= array_access(ctx
, array
, &dp
, &v
);
593 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
598 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
599 hres
= disp_call(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, &dp
, res
);
604 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
605 hres
= exec_script(ctx
->script
, ref
.u
.f
, NULL
, &dp
, res
);
611 FIXME("arguments on object\n");
616 IDispatch_AddRef(ref
.u
.obj
);
617 V_VT(res
) = VT_DISPATCH
;
618 V_DISPATCH(res
) = ref
.u
.obj
;
622 if(res
&& !ctx
->func
->code_ctx
->option_explicit
&& arg_cnt
== 0) {
625 hres
= add_dynamic_var(ctx
, identifier
, FALSE
, &v
, FALSE
, &new);
628 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
632 FIXME("%s not found\n", debugstr_w(identifier
));
633 return DISP_E_UNKNOWNNAME
;
636 stack_popn(ctx
, arg_cnt
);
640 static HRESULT
interp_icall(exec_ctx_t
*ctx
)
647 hres
= do_icall(ctx
, &v
);
651 return stack_push(ctx
, &v
);
654 static HRESULT
interp_icallv(exec_ctx_t
*ctx
)
657 return do_icall(ctx
, NULL
);
660 static HRESULT
do_mcall(exec_ctx_t
*ctx
, VARIANT
*res
)
662 const BSTR identifier
= ctx
->instr
->arg1
.bstr
;
663 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
669 hres
= stack_pop_disp(ctx
, &obj
);
678 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
680 hres
= disp_get_id(obj
, identifier
, VBDISP_CALLGET
, FALSE
, &id
);
682 hres
= disp_call(ctx
->script
, obj
, id
, &dp
, res
);
683 IDispatch_Release(obj
);
687 stack_popn(ctx
, arg_cnt
);
691 static HRESULT
interp_mcall(exec_ctx_t
*ctx
)
698 hres
= do_mcall(ctx
, &res
);
702 return stack_push(ctx
, &res
);
705 static HRESULT
interp_mcallv(exec_ctx_t
*ctx
)
709 return do_mcall(ctx
, NULL
);
712 static HRESULT
assign_ident(exec_ctx_t
*ctx
, BSTR name
, DISPPARAMS
*dp
)
717 hres
= lookup_identifier(ctx
, name
, VBDISP_LET
, &ref
);
723 VARIANT
*v
= ref
.u
.v
;
725 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
))
731 if(!(V_VT(v
) & VT_ARRAY
)) {
732 FIXME("array assign on type %d\n", V_VT(v
));
737 case VT_ARRAY
|VT_BYREF
|VT_VARIANT
:
738 array
= *V_ARRAYREF(v
);
740 case VT_ARRAY
|VT_VARIANT
:
744 FIXME("Unsupported array type %x\n", V_VT(v
));
749 FIXME("null array\n");
753 hres
= array_access(ctx
, array
, dp
, &v
);
756 }else if(V_VT(v
) == (VT_ARRAY
|VT_BYREF
|VT_VARIANT
)) {
757 FIXME("non-array assign\n");
761 hres
= VariantCopyInd(v
, dp
->rgvarg
);
765 hres
= disp_propput(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, dp
);
768 FIXME("functions not implemented\n");
774 FIXME("REF_CONST\n");
777 if(ctx
->func
->code_ctx
->option_explicit
) {
778 FIXME("throw exception\n");
782 FIXME("arg_cnt %d not supported\n", arg_cnt(dp
));
786 TRACE("creating variable %s\n", debugstr_w(name
));
787 hres
= add_dynamic_var(ctx
, name
, FALSE
, dp
->rgvarg
, FALSE
, NULL
);
794 static HRESULT
interp_assign_ident(exec_ctx_t
*ctx
)
796 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
797 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
801 TRACE("%s\n", debugstr_w(arg
));
803 hres
= stack_assume_val(ctx
, arg_cnt
);
807 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
808 hres
= assign_ident(ctx
, arg
, &dp
);
812 stack_popn(ctx
, arg_cnt
+1);
816 static HRESULT
interp_set_ident(exec_ctx_t
*ctx
)
818 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
819 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
823 TRACE("%s\n", debugstr_w(arg
));
826 FIXME("arguments not supported\n");
830 hres
= stack_assume_disp(ctx
, 0, NULL
);
834 vbstack_to_dp(ctx
, 0, TRUE
, &dp
);
835 hres
= assign_ident(ctx
, ctx
->instr
->arg1
.bstr
, &dp
);
843 static HRESULT
interp_assign_member(exec_ctx_t
*ctx
)
845 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
846 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
852 TRACE("%s\n", debugstr_w(identifier
));
854 hres
= stack_assume_disp(ctx
, arg_cnt
+1, &obj
);
863 hres
= stack_assume_val(ctx
, arg_cnt
);
867 hres
= disp_get_id(obj
, identifier
, VBDISP_LET
, FALSE
, &id
);
868 if(SUCCEEDED(hres
)) {
869 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
870 hres
= disp_propput(ctx
->script
, obj
, id
, &dp
);
875 stack_popn(ctx
, arg_cnt
+2);
879 static HRESULT
interp_set_member(exec_ctx_t
*ctx
)
881 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
882 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
888 TRACE("%s\n", debugstr_w(identifier
));
891 FIXME("arguments not supported\n");
895 hres
= stack_assume_disp(ctx
, 1, &obj
);
904 hres
= stack_assume_disp(ctx
, 0, NULL
);
908 hres
= disp_get_id(obj
, identifier
, VBDISP_SET
, FALSE
, &id
);
909 if(SUCCEEDED(hres
)) {
910 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
911 hres
= disp_propput(ctx
->script
, obj
, id
, &dp
);
920 static HRESULT
interp_const(exec_ctx_t
*ctx
)
922 BSTR arg
= ctx
->instr
->arg1
.bstr
;
927 TRACE("%s\n", debugstr_w(arg
));
929 assert(ctx
->func
->type
== FUNC_GLOBAL
);
931 hres
= lookup_identifier(ctx
, arg
, VBDISP_CALLGET
, &ref
);
935 if(ref
.type
!= REF_NONE
) {
936 FIXME("%s already defined\n", debugstr_w(arg
));
940 hres
= stack_pop_val(ctx
, &val
);
944 return add_dynamic_var(ctx
, arg
, TRUE
, val
.v
, val
.owned
, NULL
);
947 static HRESULT
interp_val(exec_ctx_t
*ctx
)
955 hres
= stack_pop_val(ctx
, &val
);
961 hres
= VariantCopy(&v
, val
.v
);
966 return stack_push(ctx
, val
.owned
? val
.v
: &v
);
969 static HRESULT
interp_pop(exec_ctx_t
*ctx
)
971 const unsigned n
= ctx
->instr
->arg1
.uint
;
979 static HRESULT
interp_new(exec_ctx_t
*ctx
)
981 const WCHAR
*arg
= ctx
->instr
->arg1
.bstr
;
982 class_desc_t
*class_desc
;
987 static const WCHAR regexpW
[] = {'r','e','g','e','x','p',0};
989 TRACE("%s\n", debugstr_w(arg
));
991 if(!strcmpiW(arg
, regexpW
)) {
992 V_VT(&v
) = VT_DISPATCH
;
993 hres
= create_regexp(&V_DISPATCH(&v
));
997 return stack_push(ctx
, &v
);
1000 for(class_desc
= ctx
->script
->classes
; class_desc
; class_desc
= class_desc
->next
) {
1001 if(!strcmpiW(class_desc
->name
, arg
))
1005 FIXME("Class %s not found\n", debugstr_w(arg
));
1009 hres
= create_vbdisp(class_desc
, &obj
);
1013 V_VT(&v
) = VT_DISPATCH
;
1014 V_DISPATCH(&v
) = (IDispatch
*)&obj
->IDispatchEx_iface
;
1015 return stack_push(ctx
, &v
);
1018 static HRESULT
interp_dim(exec_ctx_t
*ctx
)
1020 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
1021 const unsigned array_id
= ctx
->instr
->arg2
.uint
;
1022 const array_desc_t
*array_desc
;
1026 TRACE("%s\n", debugstr_w(ident
));
1028 assert(array_id
< ctx
->func
->array_cnt
);
1030 ctx
->arrays
= heap_alloc_zero(ctx
->func
->array_cnt
* sizeof(SAFEARRAY
*));
1032 return E_OUTOFMEMORY
;
1035 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1037 FIXME("lookup %s failed: %08x\n", debugstr_w(ident
), hres
);
1041 if(ref
.type
!= REF_VAR
) {
1042 FIXME("got ref.type = %d\n", ref
.type
);
1046 if(ctx
->arrays
[array_id
]) {
1047 FIXME("Array already initialized\n");
1051 array_desc
= ctx
->func
->array_descs
+ array_id
;
1052 if(array_desc
->dim_cnt
) {
1053 ctx
->arrays
[array_id
] = SafeArrayCreate(VT_VARIANT
, array_desc
->dim_cnt
, array_desc
->bounds
);
1054 if(!ctx
->arrays
[array_id
])
1055 return E_OUTOFMEMORY
;
1058 V_VT(ref
.u
.v
) = VT_ARRAY
|VT_BYREF
|VT_VARIANT
;
1059 V_ARRAYREF(ref
.u
.v
) = ctx
->arrays
+array_id
;
1063 static HRESULT
interp_step(exec_ctx_t
*ctx
)
1065 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1071 TRACE("%s\n", debugstr_w(ident
));
1073 V_VT(&zero
) = VT_I2
;
1075 hres
= VarCmp(stack_top(ctx
, 0), &zero
, ctx
->script
->lcid
, 0);
1079 gteq_zero
= hres
== VARCMP_GT
|| hres
== VARCMP_EQ
;
1081 hres
= lookup_identifier(ctx
, ident
, VBDISP_ANY
, &ref
);
1085 if(ref
.type
!= REF_VAR
) {
1086 FIXME("%s is not REF_VAR\n", debugstr_w(ident
));
1090 hres
= VarCmp(ref
.u
.v
, stack_top(ctx
, 1), ctx
->script
->lcid
, 0);
1094 if(hres
== VARCMP_EQ
|| hres
== (gteq_zero
? VARCMP_LT
: VARCMP_GT
)) {
1098 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1103 static HRESULT
interp_newenum(exec_ctx_t
*ctx
)
1111 stack_pop_deref(ctx
, &v
);
1112 assert(V_VT(stack_top(ctx
, 0)) == VT_EMPTY
);
1113 r
= stack_top(ctx
, 0);
1116 case VT_DISPATCH
|VT_BYREF
:
1119 DISPPARAMS dp
= {0};
1122 hres
= disp_call(ctx
->script
, V_ISBYREF(v
.v
) ? *V_DISPATCHREF(v
.v
) : V_DISPATCH(v
.v
), DISPID_NEWENUM
, &dp
, &iterv
);
1127 if(V_VT(&iterv
) != VT_UNKNOWN
&& V_VT(&iterv
) != VT_DISPATCH
) {
1128 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv
));
1129 VariantClear(&iterv
);
1133 hres
= IUnknown_QueryInterface(V_UNKNOWN(&iterv
), &IID_IEnumVARIANT
, (void**)&iter
);
1134 IUnknown_Release(V_UNKNOWN(&iterv
));
1136 FIXME("Could not get IEnumVARIANT iface: %08x\n", hres
);
1140 V_VT(r
) = VT_UNKNOWN
;
1141 V_UNKNOWN(r
) = (IUnknown
*)iter
;
1145 FIXME("Unsupported for %s\n", debugstr_variant(v
.v
));
1153 static HRESULT
interp_enumnext(exec_ctx_t
*ctx
)
1155 const unsigned loop_end
= ctx
->instr
->arg1
.uint
;
1156 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1158 DISPPARAMS dp
= {&v
, &propput_dispid
, 1, 1};
1165 if(V_VT(stack_top(ctx
, 0)) == VT_EMPTY
) {
1166 FIXME("uninitialized\n");
1170 assert(V_VT(stack_top(ctx
, 0)) == VT_UNKNOWN
);
1171 iter
= (IEnumVARIANT
*)V_UNKNOWN(stack_top(ctx
, 0));
1173 V_VT(&v
) = VT_EMPTY
;
1174 hres
= IEnumVARIANT_Next(iter
, 1, &v
, NULL
);
1178 do_continue
= hres
== S_OK
;
1179 hres
= assign_ident(ctx
, ident
, &dp
);
1188 instr_jmp(ctx
, loop_end
);
1193 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
1195 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1199 instr_jmp(ctx
, arg
);
1203 static HRESULT
interp_jmp_false(exec_ctx_t
*ctx
)
1205 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1211 hres
= stack_pop_bool(ctx
, &b
);
1218 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1222 static HRESULT
interp_jmp_true(exec_ctx_t
*ctx
)
1224 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1230 hres
= stack_pop_bool(ctx
, &b
);
1235 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1241 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
1249 static HRESULT
interp_stop(exec_ctx_t
*ctx
)
1253 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1257 static HRESULT
interp_me(exec_ctx_t
*ctx
)
1263 IDispatch_AddRef(ctx
->this_obj
);
1264 V_VT(&v
) = VT_DISPATCH
;
1265 V_DISPATCH(&v
) = ctx
->this_obj
;
1266 return stack_push(ctx
, &v
);
1269 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
1271 const VARIANT_BOOL arg
= ctx
->instr
->arg1
.lng
;
1274 TRACE("%s\n", arg
? "true" : "false");
1278 return stack_push(ctx
, &v
);
1281 static HRESULT
interp_errmode(exec_ctx_t
*ctx
)
1283 const int err_mode
= ctx
->instr
->arg1
.uint
;
1285 TRACE("%d\n", err_mode
);
1287 ctx
->resume_next
= err_mode
;
1288 ctx
->script
->err_number
= S_OK
;
1292 static HRESULT
interp_string(exec_ctx_t
*ctx
)
1299 V_BSTR(&v
) = SysAllocString(ctx
->instr
->arg1
.str
);
1301 return E_OUTOFMEMORY
;
1303 return stack_push(ctx
, &v
);
1306 static HRESULT
interp_long(exec_ctx_t
*ctx
)
1308 const LONG arg
= ctx
->instr
->arg1
.lng
;
1315 return stack_push(ctx
, &v
);
1318 static HRESULT
interp_short(exec_ctx_t
*ctx
)
1320 const LONG arg
= ctx
->instr
->arg1
.lng
;
1327 return stack_push(ctx
, &v
);
1330 static HRESULT
interp_double(exec_ctx_t
*ctx
)
1332 const DOUBLE
*arg
= ctx
->instr
->arg1
.dbl
;
1335 TRACE("%lf\n", *arg
);
1339 return stack_push(ctx
, &v
);
1342 static HRESULT
interp_empty(exec_ctx_t
*ctx
)
1348 V_VT(&v
) = VT_EMPTY
;
1349 return stack_push(ctx
, &v
);
1352 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1355 return stack_push_null(ctx
);
1358 static HRESULT
interp_nothing(exec_ctx_t
*ctx
)
1364 V_VT(&v
) = VT_DISPATCH
;
1365 V_DISPATCH(&v
) = NULL
;
1366 return stack_push(ctx
, &v
);
1369 static HRESULT
interp_not(exec_ctx_t
*ctx
)
1377 hres
= stack_pop_val(ctx
, &val
);
1381 hres
= VarNot(val
.v
, &v
);
1386 return stack_push(ctx
, &v
);
1389 static HRESULT
interp_and(exec_ctx_t
*ctx
)
1397 hres
= stack_pop_val(ctx
, &r
);
1401 hres
= stack_pop_val(ctx
, &l
);
1402 if(SUCCEEDED(hres
)) {
1403 hres
= VarAnd(l
.v
, r
.v
, &v
);
1410 return stack_push(ctx
, &v
);
1413 static HRESULT
interp_or(exec_ctx_t
*ctx
)
1421 hres
= stack_pop_val(ctx
, &r
);
1425 hres
= stack_pop_val(ctx
, &l
);
1426 if(SUCCEEDED(hres
)) {
1427 hres
= VarOr(l
.v
, r
.v
, &v
);
1434 return stack_push(ctx
, &v
);
1437 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
1445 hres
= stack_pop_val(ctx
, &r
);
1449 hres
= stack_pop_val(ctx
, &l
);
1450 if(SUCCEEDED(hres
)) {
1451 hres
= VarXor(l
.v
, r
.v
, &v
);
1458 return stack_push(ctx
, &v
);
1461 static HRESULT
interp_eqv(exec_ctx_t
*ctx
)
1469 hres
= stack_pop_val(ctx
, &r
);
1473 hres
= stack_pop_val(ctx
, &l
);
1474 if(SUCCEEDED(hres
)) {
1475 hres
= VarEqv(l
.v
, r
.v
, &v
);
1482 return stack_push(ctx
, &v
);
1485 static HRESULT
interp_imp(exec_ctx_t
*ctx
)
1493 hres
= stack_pop_val(ctx
, &r
);
1497 hres
= stack_pop_val(ctx
, &l
);
1498 if(SUCCEEDED(hres
)) {
1499 hres
= VarImp(l
.v
, r
.v
, &v
);
1506 return stack_push(ctx
, &v
);
1509 static HRESULT
var_cmp(exec_ctx_t
*ctx
, VARIANT
*l
, VARIANT
*r
)
1511 TRACE("%s %s\n", debugstr_variant(l
), debugstr_variant(r
));
1513 /* FIXME: Fix comparing string to number */
1515 return VarCmp(l
, r
, ctx
->script
->lcid
, 0);
1518 static HRESULT
cmp_oper(exec_ctx_t
*ctx
)
1523 hres
= stack_pop_val(ctx
, &r
);
1527 hres
= stack_pop_val(ctx
, &l
);
1528 if(SUCCEEDED(hres
)) {
1529 hres
= var_cmp(ctx
, l
.v
, r
.v
);
1537 static HRESULT
interp_equal(exec_ctx_t
*ctx
)
1544 hres
= cmp_oper(ctx
);
1547 if(hres
== VARCMP_NULL
)
1548 return stack_push_null(ctx
);
1551 V_BOOL(&v
) = hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1552 return stack_push(ctx
, &v
);
1555 static HRESULT
interp_nequal(exec_ctx_t
*ctx
)
1562 hres
= cmp_oper(ctx
);
1565 if(hres
== VARCMP_NULL
)
1566 return stack_push_null(ctx
);
1569 V_BOOL(&v
) = hres
!= VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1570 return stack_push(ctx
, &v
);
1573 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
1580 hres
= cmp_oper(ctx
);
1583 if(hres
== VARCMP_NULL
)
1584 return stack_push_null(ctx
);
1587 V_BOOL(&v
) = hres
== VARCMP_GT
? VARIANT_TRUE
: VARIANT_FALSE
;
1588 return stack_push(ctx
, &v
);
1591 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
1598 hres
= cmp_oper(ctx
);
1601 if(hres
== VARCMP_NULL
)
1602 return stack_push_null(ctx
);
1605 V_BOOL(&v
) = hres
== VARCMP_GT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1606 return stack_push(ctx
, &v
);
1609 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
1616 hres
= cmp_oper(ctx
);
1619 if(hres
== VARCMP_NULL
)
1620 return stack_push_null(ctx
);
1623 V_BOOL(&v
) = hres
== VARCMP_LT
? VARIANT_TRUE
: VARIANT_FALSE
;
1624 return stack_push(ctx
, &v
);
1627 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
1634 hres
= cmp_oper(ctx
);
1637 if(hres
== VARCMP_NULL
)
1638 return stack_push_null(ctx
);
1641 V_BOOL(&v
) = hres
== VARCMP_LT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1642 return stack_push(ctx
, &v
);
1645 static HRESULT
interp_case(exec_ctx_t
*ctx
)
1647 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1653 hres
= stack_pop_val(ctx
, &v
);
1657 hres
= var_cmp(ctx
, stack_top(ctx
, 0), v
.v
);
1662 if(hres
== VARCMP_EQ
) {
1664 instr_jmp(ctx
, arg
);
1672 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, VARIANT_BOOL
*ret
)
1674 IObjectIdentity
*identity
;
1675 IUnknown
*unk1
, *unk2
;
1678 if(disp1
== disp2
) {
1679 *ret
= VARIANT_TRUE
;
1683 if(!disp1
|| !disp2
) {
1684 *ret
= VARIANT_FALSE
;
1688 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
1692 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
1694 IUnknown_Release(unk1
);
1699 *ret
= VARIANT_TRUE
;
1701 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
1702 if(SUCCEEDED(hres
)) {
1703 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
1704 IObjectIdentity_Release(identity
);
1705 *ret
= hres
== S_OK
? VARIANT_TRUE
: VARIANT_FALSE
;
1707 *ret
= VARIANT_FALSE
;
1711 IUnknown_Release(unk1
);
1712 IUnknown_Release(unk2
);
1716 static HRESULT
interp_is(exec_ctx_t
*ctx
)
1724 hres
= stack_pop_disp(ctx
, &r
);
1728 hres
= stack_pop_disp(ctx
, &l
);
1729 if(SUCCEEDED(hres
)) {
1731 hres
= disp_cmp(l
, r
, &V_BOOL(&v
));
1733 IDispatch_Release(l
);
1736 IDispatch_Release(r
);
1740 return stack_push(ctx
, &v
);
1743 static HRESULT
interp_concat(exec_ctx_t
*ctx
)
1751 hres
= stack_pop_val(ctx
, &r
);
1755 hres
= stack_pop_val(ctx
, &l
);
1756 if(SUCCEEDED(hres
)) {
1757 hres
= VarCat(l
.v
, r
.v
, &v
);
1764 return stack_push(ctx
, &v
);
1767 static HRESULT
interp_add(exec_ctx_t
*ctx
)
1775 hres
= stack_pop_val(ctx
, &r
);
1779 hres
= stack_pop_val(ctx
, &l
);
1780 if(SUCCEEDED(hres
)) {
1781 hres
= VarAdd(l
.v
, r
.v
, &v
);
1788 return stack_push(ctx
, &v
);
1791 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
1799 hres
= stack_pop_val(ctx
, &r
);
1803 hres
= stack_pop_val(ctx
, &l
);
1804 if(SUCCEEDED(hres
)) {
1805 hres
= VarSub(l
.v
, r
.v
, &v
);
1812 return stack_push(ctx
, &v
);
1815 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
1823 hres
= stack_pop_val(ctx
, &r
);
1827 hres
= stack_pop_val(ctx
, &l
);
1828 if(SUCCEEDED(hres
)) {
1829 hres
= VarMod(l
.v
, r
.v
, &v
);
1836 return stack_push(ctx
, &v
);
1839 static HRESULT
interp_idiv(exec_ctx_t
*ctx
)
1847 hres
= stack_pop_val(ctx
, &r
);
1851 hres
= stack_pop_val(ctx
, &l
);
1852 if(SUCCEEDED(hres
)) {
1853 hres
= VarIdiv(l
.v
, r
.v
, &v
);
1860 return stack_push(ctx
, &v
);
1863 static HRESULT
interp_div(exec_ctx_t
*ctx
)
1871 hres
= stack_pop_val(ctx
, &r
);
1875 hres
= stack_pop_val(ctx
, &l
);
1876 if(SUCCEEDED(hres
)) {
1877 hres
= VarDiv(l
.v
, r
.v
, &v
);
1884 return stack_push(ctx
, &v
);
1887 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
1895 hres
= stack_pop_val(ctx
, &r
);
1899 hres
= stack_pop_val(ctx
, &l
);
1900 if(SUCCEEDED(hres
)) {
1901 hres
= VarMul(l
.v
, r
.v
, &v
);
1908 return stack_push(ctx
, &v
);
1911 static HRESULT
interp_exp(exec_ctx_t
*ctx
)
1919 hres
= stack_pop_val(ctx
, &r
);
1923 hres
= stack_pop_val(ctx
, &l
);
1924 if(SUCCEEDED(hres
)) {
1925 hres
= VarPow(l
.v
, r
.v
, &v
);
1932 return stack_push(ctx
, &v
);
1935 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
1941 hres
= stack_pop_val(ctx
, &val
);
1945 hres
= VarNeg(val
.v
, &v
);
1950 return stack_push(ctx
, &v
);
1953 static HRESULT
interp_incc(exec_ctx_t
*ctx
)
1955 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
1962 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1966 if(ref
.type
!= REF_VAR
) {
1967 FIXME("ref.type is not REF_VAR\n");
1971 hres
= VarAdd(stack_top(ctx
, 0), ref
.u
.v
, &v
);
1975 VariantClear(ref
.u
.v
);
1980 static HRESULT
interp_catch(exec_ctx_t
*ctx
)
1982 /* Nothing to do here, the OP is for unwinding only. */
1986 static const instr_func_t op_funcs
[] = {
1987 #define X(x,n,a,b) interp_ ## x,
1992 static const unsigned op_move
[] = {
1993 #define X(x,n,a,b) n,
1998 void release_dynamic_vars(dynamic_var_t
*var
)
2001 VariantClear(&var
->v
);
2006 static void release_exec(exec_ctx_t
*ctx
)
2010 VariantClear(&ctx
->ret_val
);
2011 release_dynamic_vars(ctx
->dynamic_vars
);
2014 IDispatch_Release(ctx
->this_obj
);
2017 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++)
2018 VariantClear(ctx
->args
+i
);
2022 for(i
=0; i
< ctx
->func
->var_cnt
; i
++)
2023 VariantClear(ctx
->vars
+i
);
2027 for(i
=0; i
< ctx
->func
->var_cnt
; i
++) {
2029 SafeArrayDestroy(ctx
->arrays
[i
]);
2031 heap_free(ctx
->arrays
);
2034 heap_pool_free(&ctx
->heap
);
2035 heap_free(ctx
->args
);
2036 heap_free(ctx
->vars
);
2037 heap_free(ctx
->stack
);
2040 HRESULT
exec_script(script_ctx_t
*ctx
, function_t
*func
, vbdisp_t
*vbthis
, DISPPARAMS
*dp
, VARIANT
*res
)
2042 exec_ctx_t exec
= {func
->code_ctx
};
2044 HRESULT hres
= S_OK
;
2046 exec
.code
= func
->code_ctx
;
2048 if(dp
? func
->arg_cnt
!= arg_cnt(dp
) : func
->arg_cnt
) {
2049 FIXME("wrong arg_cnt %d, expected %d\n", dp
? arg_cnt(dp
) : 0, func
->arg_cnt
);
2053 heap_pool_init(&exec
.heap
);
2059 exec
.args
= heap_alloc_zero(func
->arg_cnt
* sizeof(VARIANT
));
2061 release_exec(&exec
);
2062 return E_OUTOFMEMORY
;
2065 for(i
=0; i
< func
->arg_cnt
; i
++) {
2067 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
)) {
2068 if(func
->args
[i
].by_ref
)
2071 hres
= VariantCopyInd(exec
.args
+i
, V_VARIANTREF(v
));
2073 hres
= VariantCopyInd(exec
.args
+i
, v
);
2076 release_exec(&exec
);
2085 exec
.vars
= heap_alloc_zero(func
->var_cnt
* sizeof(VARIANT
));
2087 release_exec(&exec
);
2088 return E_OUTOFMEMORY
;
2094 exec
.stack_size
= 16;
2096 exec
.stack
= heap_alloc(exec
.stack_size
* sizeof(VARIANT
));
2098 release_exec(&exec
);
2099 return E_OUTOFMEMORY
;
2103 exec
.this_obj
= (IDispatch
*)&vbthis
->IDispatchEx_iface
;
2104 exec
.vbthis
= vbthis
;
2105 }else if (ctx
->host_global
) {
2106 exec
.this_obj
= ctx
->host_global
;
2108 exec
.this_obj
= (IDispatch
*)&ctx
->script_obj
->IDispatchEx_iface
;
2110 IDispatch_AddRef(exec
.this_obj
);
2112 exec
.instr
= exec
.code
->instrs
+ func
->code_off
;
2117 op
= exec
.instr
->op
;
2118 hres
= op_funcs
[op
](&exec
);
2120 ctx
->err_number
= hres
= map_hres(hres
);
2122 if(exec
.resume_next
) {
2125 WARN("Failed %08x in resume next mode\n", hres
);
2128 * Unwinding here is simple. We need to find the next OP_catch, which contains
2129 * information about expected stack size and jump offset on error. Generated
2130 * bytecode needs to guarantee, that simple jump and stack adjustment will
2131 * guarantee proper execution continuation.
2133 while((++exec
.instr
)->op
!= OP_catch
);
2135 TRACE("unwind jmp %d stack_off %d\n", exec
.instr
->arg1
.uint
, exec
.instr
->arg2
.uint
);
2137 stack_off
= exec
.instr
->arg2
.uint
;
2138 instr_jmp(&exec
, exec
.instr
->arg1
.uint
);
2140 if(exec
.top
> stack_off
) {
2141 stack_popn(&exec
, exec
.top
-stack_off
);
2142 }else if(exec
.top
< stack_off
) {
2145 V_VT(&v
) = VT_EMPTY
;
2146 while(exec
.top
< stack_off
) {
2147 hres
= stack_push(&exec
, &v
);
2155 WARN("Failed %08x\n", hres
);
2156 stack_popn(&exec
, exec
.top
);
2161 exec
.instr
+= op_move
[op
];
2165 if(func
->type
!= FUNC_FUNCTION
&& func
->type
!= FUNC_PROPGET
&& func
->type
!= FUNC_DEFGET
)
2166 assert(V_VT(&exec
.ret_val
) == VT_EMPTY
);
2168 if(SUCCEEDED(hres
) && res
) {
2169 *res
= exec
.ret_val
;
2170 V_VT(&exec
.ret_val
) = VT_EMPTY
;
2173 release_exec(&exec
);