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
;
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 FIXME("arguments not implemented\n");
578 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
579 hres
= array_access(ctx
, array
, &dp
, &v
);
584 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
589 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
590 hres
= disp_call(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, &dp
, res
);
595 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
596 hres
= exec_script(ctx
->script
, ref
.u
.f
, NULL
, &dp
, res
);
602 FIXME("arguments on object\n");
607 IDispatch_AddRef(ref
.u
.obj
);
608 V_VT(res
) = VT_DISPATCH
;
609 V_DISPATCH(res
) = ref
.u
.obj
;
613 if(res
&& !ctx
->func
->code_ctx
->option_explicit
&& arg_cnt
== 0) {
616 hres
= add_dynamic_var(ctx
, identifier
, FALSE
, &v
, FALSE
, &new);
619 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
623 FIXME("%s not found\n", debugstr_w(identifier
));
624 return DISP_E_UNKNOWNNAME
;
627 stack_popn(ctx
, arg_cnt
);
631 static HRESULT
interp_icall(exec_ctx_t
*ctx
)
638 hres
= do_icall(ctx
, &v
);
642 return stack_push(ctx
, &v
);
645 static HRESULT
interp_icallv(exec_ctx_t
*ctx
)
648 return do_icall(ctx
, NULL
);
651 static HRESULT
do_mcall(exec_ctx_t
*ctx
, VARIANT
*res
)
653 const BSTR identifier
= ctx
->instr
->arg1
.bstr
;
654 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
660 hres
= stack_pop_disp(ctx
, &obj
);
669 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
671 hres
= disp_get_id(obj
, identifier
, VBDISP_CALLGET
, FALSE
, &id
);
673 hres
= disp_call(ctx
->script
, obj
, id
, &dp
, res
);
674 IDispatch_Release(obj
);
678 stack_popn(ctx
, arg_cnt
);
682 static HRESULT
interp_mcall(exec_ctx_t
*ctx
)
689 hres
= do_mcall(ctx
, &res
);
693 return stack_push(ctx
, &res
);
696 static HRESULT
interp_mcallv(exec_ctx_t
*ctx
)
700 return do_mcall(ctx
, NULL
);
703 static HRESULT
assign_ident(exec_ctx_t
*ctx
, BSTR name
, DISPPARAMS
*dp
)
708 hres
= lookup_identifier(ctx
, name
, VBDISP_LET
, &ref
);
714 VARIANT
*v
= ref
.u
.v
;
716 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
))
722 if(!(V_VT(v
) & VT_ARRAY
)) {
723 FIXME("array assign on type %d\n", V_VT(v
));
728 case VT_ARRAY
|VT_BYREF
|VT_VARIANT
:
729 array
= *V_ARRAYREF(v
);
731 case VT_ARRAY
|VT_VARIANT
:
735 FIXME("Unsupported array type %x\n", V_VT(v
));
740 FIXME("null array\n");
744 hres
= array_access(ctx
, array
, dp
, &v
);
747 }else if(V_VT(v
) == (VT_ARRAY
|VT_BYREF
|VT_VARIANT
)) {
748 FIXME("non-array assign\n");
752 hres
= VariantCopyInd(v
, dp
->rgvarg
);
756 hres
= disp_propput(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, dp
);
759 FIXME("functions not implemented\n");
765 FIXME("REF_CONST\n");
768 if(ctx
->func
->code_ctx
->option_explicit
) {
769 FIXME("throw exception\n");
773 FIXME("arg_cnt %d not supported\n", arg_cnt(dp
));
777 TRACE("creating variable %s\n", debugstr_w(name
));
778 hres
= add_dynamic_var(ctx
, name
, FALSE
, dp
->rgvarg
, FALSE
, NULL
);
785 static HRESULT
interp_assign_ident(exec_ctx_t
*ctx
)
787 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
788 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
792 TRACE("%s\n", debugstr_w(arg
));
794 hres
= stack_assume_val(ctx
, arg_cnt
);
798 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
799 hres
= assign_ident(ctx
, arg
, &dp
);
803 stack_popn(ctx
, arg_cnt
+1);
807 static HRESULT
interp_set_ident(exec_ctx_t
*ctx
)
809 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
810 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
814 TRACE("%s\n", debugstr_w(arg
));
817 FIXME("arguments not supported\n");
821 hres
= stack_assume_disp(ctx
, 0, NULL
);
825 vbstack_to_dp(ctx
, 0, TRUE
, &dp
);
826 hres
= assign_ident(ctx
, ctx
->instr
->arg1
.bstr
, &dp
);
834 static HRESULT
interp_assign_member(exec_ctx_t
*ctx
)
836 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
837 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
843 TRACE("%s\n", debugstr_w(identifier
));
845 hres
= stack_assume_disp(ctx
, arg_cnt
+1, &obj
);
854 hres
= stack_assume_val(ctx
, arg_cnt
);
858 hres
= disp_get_id(obj
, identifier
, VBDISP_LET
, FALSE
, &id
);
859 if(SUCCEEDED(hres
)) {
860 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
861 hres
= disp_propput(ctx
->script
, obj
, id
, &dp
);
866 stack_popn(ctx
, arg_cnt
+2);
870 static HRESULT
interp_set_member(exec_ctx_t
*ctx
)
872 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
873 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
879 TRACE("%s\n", debugstr_w(identifier
));
882 FIXME("arguments not supported\n");
886 hres
= stack_assume_disp(ctx
, 1, &obj
);
895 hres
= stack_assume_disp(ctx
, 0, NULL
);
899 hres
= disp_get_id(obj
, identifier
, VBDISP_SET
, FALSE
, &id
);
900 if(SUCCEEDED(hres
)) {
901 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
902 hres
= disp_propput(ctx
->script
, obj
, id
, &dp
);
911 static HRESULT
interp_const(exec_ctx_t
*ctx
)
913 BSTR arg
= ctx
->instr
->arg1
.bstr
;
918 TRACE("%s\n", debugstr_w(arg
));
920 assert(ctx
->func
->type
== FUNC_GLOBAL
);
922 hres
= lookup_identifier(ctx
, arg
, VBDISP_CALLGET
, &ref
);
926 if(ref
.type
!= REF_NONE
) {
927 FIXME("%s already defined\n", debugstr_w(arg
));
931 hres
= stack_pop_val(ctx
, &val
);
935 return add_dynamic_var(ctx
, arg
, TRUE
, val
.v
, val
.owned
, NULL
);
938 static HRESULT
interp_val(exec_ctx_t
*ctx
)
946 hres
= stack_pop_val(ctx
, &val
);
952 hres
= VariantCopy(&v
, val
.v
);
957 return stack_push(ctx
, val
.owned
? val
.v
: &v
);
960 static HRESULT
interp_pop(exec_ctx_t
*ctx
)
962 const unsigned n
= ctx
->instr
->arg1
.uint
;
970 static HRESULT
interp_new(exec_ctx_t
*ctx
)
972 const WCHAR
*arg
= ctx
->instr
->arg1
.bstr
;
973 class_desc_t
*class_desc
;
978 static const WCHAR regexpW
[] = {'r','e','g','e','x','p',0};
980 TRACE("%s\n", debugstr_w(arg
));
982 if(!strcmpiW(arg
, regexpW
)) {
983 V_VT(&v
) = VT_DISPATCH
;
984 hres
= create_regexp(&V_DISPATCH(&v
));
988 return stack_push(ctx
, &v
);
991 for(class_desc
= ctx
->script
->classes
; class_desc
; class_desc
= class_desc
->next
) {
992 if(!strcmpiW(class_desc
->name
, arg
))
996 FIXME("Class %s not found\n", debugstr_w(arg
));
1000 hres
= create_vbdisp(class_desc
, &obj
);
1004 V_VT(&v
) = VT_DISPATCH
;
1005 V_DISPATCH(&v
) = (IDispatch
*)&obj
->IDispatchEx_iface
;
1006 return stack_push(ctx
, &v
);
1009 static HRESULT
interp_dim(exec_ctx_t
*ctx
)
1011 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
1012 const unsigned array_id
= ctx
->instr
->arg2
.uint
;
1013 const array_desc_t
*array_desc
;
1017 TRACE("%s\n", debugstr_w(ident
));
1019 assert(array_id
< ctx
->func
->array_cnt
);
1021 ctx
->arrays
= heap_alloc_zero(ctx
->func
->array_cnt
* sizeof(SAFEARRAY
*));
1023 return E_OUTOFMEMORY
;
1026 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1028 FIXME("lookup %s failed: %08x\n", debugstr_w(ident
), hres
);
1032 if(ref
.type
!= REF_VAR
) {
1033 FIXME("got ref.type = %d\n", ref
.type
);
1037 if(ctx
->arrays
[array_id
]) {
1038 FIXME("Array already initialized\n");
1042 array_desc
= ctx
->func
->array_descs
+ array_id
;
1043 if(array_desc
->dim_cnt
) {
1044 ctx
->arrays
[array_id
] = SafeArrayCreate(VT_VARIANT
, array_desc
->dim_cnt
, array_desc
->bounds
);
1045 if(!ctx
->arrays
[array_id
])
1046 return E_OUTOFMEMORY
;
1049 V_VT(ref
.u
.v
) = VT_ARRAY
|VT_BYREF
|VT_VARIANT
;
1050 V_ARRAYREF(ref
.u
.v
) = ctx
->arrays
+array_id
;
1054 static HRESULT
interp_step(exec_ctx_t
*ctx
)
1056 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1062 TRACE("%s\n", debugstr_w(ident
));
1064 V_VT(&zero
) = VT_I2
;
1066 hres
= VarCmp(stack_top(ctx
, 0), &zero
, ctx
->script
->lcid
, 0);
1070 gteq_zero
= hres
== VARCMP_GT
|| hres
== VARCMP_EQ
;
1072 hres
= lookup_identifier(ctx
, ident
, VBDISP_ANY
, &ref
);
1076 if(ref
.type
!= REF_VAR
) {
1077 FIXME("%s is not REF_VAR\n", debugstr_w(ident
));
1081 hres
= VarCmp(ref
.u
.v
, stack_top(ctx
, 1), ctx
->script
->lcid
, 0);
1085 if(hres
== VARCMP_EQ
|| hres
== (gteq_zero
? VARCMP_LT
: VARCMP_GT
)) {
1089 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1094 static HRESULT
interp_newenum(exec_ctx_t
*ctx
)
1102 stack_pop_deref(ctx
, &v
);
1103 assert(V_VT(stack_top(ctx
, 0)) == VT_EMPTY
);
1104 r
= stack_top(ctx
, 0);
1107 case VT_DISPATCH
|VT_BYREF
:
1110 DISPPARAMS dp
= {0};
1113 hres
= disp_call(ctx
->script
, V_ISBYREF(v
.v
) ? *V_DISPATCHREF(v
.v
) : V_DISPATCH(v
.v
), DISPID_NEWENUM
, &dp
, &iterv
);
1118 if(V_VT(&iterv
) != VT_UNKNOWN
&& V_VT(&iterv
) != VT_DISPATCH
) {
1119 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv
));
1120 VariantClear(&iterv
);
1124 hres
= IUnknown_QueryInterface(V_UNKNOWN(&iterv
), &IID_IEnumVARIANT
, (void**)&iter
);
1125 IUnknown_Release(V_UNKNOWN(&iterv
));
1127 FIXME("Could not get IEnumVARIANT iface: %08x\n", hres
);
1131 V_VT(r
) = VT_UNKNOWN
;
1132 V_UNKNOWN(r
) = (IUnknown
*)iter
;
1136 FIXME("Unsupported for %s\n", debugstr_variant(v
.v
));
1144 static HRESULT
interp_enumnext(exec_ctx_t
*ctx
)
1146 const unsigned loop_end
= ctx
->instr
->arg1
.uint
;
1147 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1149 DISPPARAMS dp
= {&v
, &propput_dispid
, 1, 1};
1156 if(V_VT(stack_top(ctx
, 0)) == VT_EMPTY
) {
1157 FIXME("uninitialized\n");
1161 assert(V_VT(stack_top(ctx
, 0)) == VT_UNKNOWN
);
1162 iter
= (IEnumVARIANT
*)V_UNKNOWN(stack_top(ctx
, 0));
1164 V_VT(&v
) = VT_EMPTY
;
1165 hres
= IEnumVARIANT_Next(iter
, 1, &v
, NULL
);
1169 do_continue
= hres
== S_OK
;
1170 hres
= assign_ident(ctx
, ident
, &dp
);
1179 instr_jmp(ctx
, loop_end
);
1184 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
1186 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1190 instr_jmp(ctx
, arg
);
1194 static HRESULT
interp_jmp_false(exec_ctx_t
*ctx
)
1196 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1202 hres
= stack_pop_bool(ctx
, &b
);
1209 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1213 static HRESULT
interp_jmp_true(exec_ctx_t
*ctx
)
1215 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1221 hres
= stack_pop_bool(ctx
, &b
);
1226 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1232 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
1240 static HRESULT
interp_stop(exec_ctx_t
*ctx
)
1244 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1248 static HRESULT
interp_me(exec_ctx_t
*ctx
)
1254 IDispatch_AddRef(ctx
->this_obj
);
1255 V_VT(&v
) = VT_DISPATCH
;
1256 V_DISPATCH(&v
) = ctx
->this_obj
;
1257 return stack_push(ctx
, &v
);
1260 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
1262 const VARIANT_BOOL arg
= ctx
->instr
->arg1
.lng
;
1265 TRACE("%s\n", arg
? "true" : "false");
1269 return stack_push(ctx
, &v
);
1272 static HRESULT
interp_errmode(exec_ctx_t
*ctx
)
1274 const int err_mode
= ctx
->instr
->arg1
.uint
;
1276 TRACE("%d\n", err_mode
);
1278 ctx
->resume_next
= err_mode
;
1279 ctx
->script
->err_number
= S_OK
;
1283 static HRESULT
interp_string(exec_ctx_t
*ctx
)
1290 V_BSTR(&v
) = SysAllocString(ctx
->instr
->arg1
.str
);
1292 return E_OUTOFMEMORY
;
1294 return stack_push(ctx
, &v
);
1297 static HRESULT
interp_long(exec_ctx_t
*ctx
)
1299 const LONG arg
= ctx
->instr
->arg1
.lng
;
1306 return stack_push(ctx
, &v
);
1309 static HRESULT
interp_short(exec_ctx_t
*ctx
)
1311 const LONG arg
= ctx
->instr
->arg1
.lng
;
1318 return stack_push(ctx
, &v
);
1321 static HRESULT
interp_double(exec_ctx_t
*ctx
)
1323 const DOUBLE
*arg
= ctx
->instr
->arg1
.dbl
;
1326 TRACE("%lf\n", *arg
);
1330 return stack_push(ctx
, &v
);
1333 static HRESULT
interp_empty(exec_ctx_t
*ctx
)
1339 V_VT(&v
) = VT_EMPTY
;
1340 return stack_push(ctx
, &v
);
1343 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1346 return stack_push_null(ctx
);
1349 static HRESULT
interp_nothing(exec_ctx_t
*ctx
)
1355 V_VT(&v
) = VT_DISPATCH
;
1356 V_DISPATCH(&v
) = NULL
;
1357 return stack_push(ctx
, &v
);
1360 static HRESULT
interp_not(exec_ctx_t
*ctx
)
1368 hres
= stack_pop_val(ctx
, &val
);
1372 hres
= VarNot(val
.v
, &v
);
1377 return stack_push(ctx
, &v
);
1380 static HRESULT
interp_and(exec_ctx_t
*ctx
)
1388 hres
= stack_pop_val(ctx
, &r
);
1392 hres
= stack_pop_val(ctx
, &l
);
1393 if(SUCCEEDED(hres
)) {
1394 hres
= VarAnd(l
.v
, r
.v
, &v
);
1401 return stack_push(ctx
, &v
);
1404 static HRESULT
interp_or(exec_ctx_t
*ctx
)
1412 hres
= stack_pop_val(ctx
, &r
);
1416 hres
= stack_pop_val(ctx
, &l
);
1417 if(SUCCEEDED(hres
)) {
1418 hres
= VarOr(l
.v
, r
.v
, &v
);
1425 return stack_push(ctx
, &v
);
1428 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
1436 hres
= stack_pop_val(ctx
, &r
);
1440 hres
= stack_pop_val(ctx
, &l
);
1441 if(SUCCEEDED(hres
)) {
1442 hres
= VarXor(l
.v
, r
.v
, &v
);
1449 return stack_push(ctx
, &v
);
1452 static HRESULT
interp_eqv(exec_ctx_t
*ctx
)
1460 hres
= stack_pop_val(ctx
, &r
);
1464 hres
= stack_pop_val(ctx
, &l
);
1465 if(SUCCEEDED(hres
)) {
1466 hres
= VarEqv(l
.v
, r
.v
, &v
);
1473 return stack_push(ctx
, &v
);
1476 static HRESULT
interp_imp(exec_ctx_t
*ctx
)
1484 hres
= stack_pop_val(ctx
, &r
);
1488 hres
= stack_pop_val(ctx
, &l
);
1489 if(SUCCEEDED(hres
)) {
1490 hres
= VarImp(l
.v
, r
.v
, &v
);
1497 return stack_push(ctx
, &v
);
1500 static HRESULT
var_cmp(exec_ctx_t
*ctx
, VARIANT
*l
, VARIANT
*r
)
1502 TRACE("%s %s\n", debugstr_variant(l
), debugstr_variant(r
));
1504 /* FIXME: Fix comparing string to number */
1506 return VarCmp(l
, r
, ctx
->script
->lcid
, 0);
1509 static HRESULT
cmp_oper(exec_ctx_t
*ctx
)
1514 hres
= stack_pop_val(ctx
, &r
);
1518 hres
= stack_pop_val(ctx
, &l
);
1519 if(SUCCEEDED(hres
)) {
1520 hres
= var_cmp(ctx
, l
.v
, r
.v
);
1528 static HRESULT
interp_equal(exec_ctx_t
*ctx
)
1535 hres
= cmp_oper(ctx
);
1538 if(hres
== VARCMP_NULL
)
1539 return stack_push_null(ctx
);
1542 V_BOOL(&v
) = hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1543 return stack_push(ctx
, &v
);
1546 static HRESULT
interp_nequal(exec_ctx_t
*ctx
)
1553 hres
= cmp_oper(ctx
);
1556 if(hres
== VARCMP_NULL
)
1557 return stack_push_null(ctx
);
1560 V_BOOL(&v
) = hres
!= VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1561 return stack_push(ctx
, &v
);
1564 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
1571 hres
= cmp_oper(ctx
);
1574 if(hres
== VARCMP_NULL
)
1575 return stack_push_null(ctx
);
1578 V_BOOL(&v
) = hres
== VARCMP_GT
? VARIANT_TRUE
: VARIANT_FALSE
;
1579 return stack_push(ctx
, &v
);
1582 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
1589 hres
= cmp_oper(ctx
);
1592 if(hres
== VARCMP_NULL
)
1593 return stack_push_null(ctx
);
1596 V_BOOL(&v
) = hres
== VARCMP_GT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1597 return stack_push(ctx
, &v
);
1600 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
1607 hres
= cmp_oper(ctx
);
1610 if(hres
== VARCMP_NULL
)
1611 return stack_push_null(ctx
);
1614 V_BOOL(&v
) = hres
== VARCMP_LT
? VARIANT_TRUE
: VARIANT_FALSE
;
1615 return stack_push(ctx
, &v
);
1618 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
1625 hres
= cmp_oper(ctx
);
1628 if(hres
== VARCMP_NULL
)
1629 return stack_push_null(ctx
);
1632 V_BOOL(&v
) = hres
== VARCMP_LT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1633 return stack_push(ctx
, &v
);
1636 static HRESULT
interp_case(exec_ctx_t
*ctx
)
1638 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1644 hres
= stack_pop_val(ctx
, &v
);
1648 hres
= var_cmp(ctx
, stack_top(ctx
, 0), v
.v
);
1653 if(hres
== VARCMP_EQ
) {
1655 instr_jmp(ctx
, arg
);
1663 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, VARIANT_BOOL
*ret
)
1665 IObjectIdentity
*identity
;
1666 IUnknown
*unk1
, *unk2
;
1669 if(disp1
== disp2
) {
1670 *ret
= VARIANT_TRUE
;
1674 if(!disp1
|| !disp2
) {
1675 *ret
= VARIANT_FALSE
;
1679 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
1683 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
1685 IUnknown_Release(unk1
);
1690 *ret
= VARIANT_TRUE
;
1692 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
1693 if(SUCCEEDED(hres
)) {
1694 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
1695 IObjectIdentity_Release(identity
);
1696 *ret
= hres
== S_OK
? VARIANT_TRUE
: VARIANT_FALSE
;
1698 *ret
= VARIANT_FALSE
;
1702 IUnknown_Release(unk1
);
1703 IUnknown_Release(unk2
);
1707 static HRESULT
interp_is(exec_ctx_t
*ctx
)
1715 hres
= stack_pop_disp(ctx
, &r
);
1719 hres
= stack_pop_disp(ctx
, &l
);
1720 if(SUCCEEDED(hres
)) {
1722 hres
= disp_cmp(l
, r
, &V_BOOL(&v
));
1724 IDispatch_Release(l
);
1727 IDispatch_Release(r
);
1731 return stack_push(ctx
, &v
);
1734 static HRESULT
interp_concat(exec_ctx_t
*ctx
)
1742 hres
= stack_pop_val(ctx
, &r
);
1746 hres
= stack_pop_val(ctx
, &l
);
1747 if(SUCCEEDED(hres
)) {
1748 hres
= VarCat(l
.v
, r
.v
, &v
);
1755 return stack_push(ctx
, &v
);
1758 static HRESULT
interp_add(exec_ctx_t
*ctx
)
1766 hres
= stack_pop_val(ctx
, &r
);
1770 hres
= stack_pop_val(ctx
, &l
);
1771 if(SUCCEEDED(hres
)) {
1772 hres
= VarAdd(l
.v
, r
.v
, &v
);
1779 return stack_push(ctx
, &v
);
1782 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
1790 hres
= stack_pop_val(ctx
, &r
);
1794 hres
= stack_pop_val(ctx
, &l
);
1795 if(SUCCEEDED(hres
)) {
1796 hres
= VarSub(l
.v
, r
.v
, &v
);
1803 return stack_push(ctx
, &v
);
1806 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
1814 hres
= stack_pop_val(ctx
, &r
);
1818 hres
= stack_pop_val(ctx
, &l
);
1819 if(SUCCEEDED(hres
)) {
1820 hres
= VarMod(l
.v
, r
.v
, &v
);
1827 return stack_push(ctx
, &v
);
1830 static HRESULT
interp_idiv(exec_ctx_t
*ctx
)
1838 hres
= stack_pop_val(ctx
, &r
);
1842 hres
= stack_pop_val(ctx
, &l
);
1843 if(SUCCEEDED(hres
)) {
1844 hres
= VarIdiv(l
.v
, r
.v
, &v
);
1851 return stack_push(ctx
, &v
);
1854 static HRESULT
interp_div(exec_ctx_t
*ctx
)
1862 hres
= stack_pop_val(ctx
, &r
);
1866 hres
= stack_pop_val(ctx
, &l
);
1867 if(SUCCEEDED(hres
)) {
1868 hres
= VarDiv(l
.v
, r
.v
, &v
);
1875 return stack_push(ctx
, &v
);
1878 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
1886 hres
= stack_pop_val(ctx
, &r
);
1890 hres
= stack_pop_val(ctx
, &l
);
1891 if(SUCCEEDED(hres
)) {
1892 hres
= VarMul(l
.v
, r
.v
, &v
);
1899 return stack_push(ctx
, &v
);
1902 static HRESULT
interp_exp(exec_ctx_t
*ctx
)
1910 hres
= stack_pop_val(ctx
, &r
);
1914 hres
= stack_pop_val(ctx
, &l
);
1915 if(SUCCEEDED(hres
)) {
1916 hres
= VarPow(l
.v
, r
.v
, &v
);
1923 return stack_push(ctx
, &v
);
1926 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
1932 hres
= stack_pop_val(ctx
, &val
);
1936 hres
= VarNeg(val
.v
, &v
);
1941 return stack_push(ctx
, &v
);
1944 static HRESULT
interp_incc(exec_ctx_t
*ctx
)
1946 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
1953 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1957 if(ref
.type
!= REF_VAR
) {
1958 FIXME("ref.type is not REF_VAR\n");
1962 hres
= VarAdd(stack_top(ctx
, 0), ref
.u
.v
, &v
);
1966 VariantClear(ref
.u
.v
);
1971 static HRESULT
interp_catch(exec_ctx_t
*ctx
)
1973 /* Nothing to do here, the OP is for unwinding only. */
1977 static const instr_func_t op_funcs
[] = {
1978 #define X(x,n,a,b) interp_ ## x,
1983 static const unsigned op_move
[] = {
1984 #define X(x,n,a,b) n,
1989 void release_dynamic_vars(dynamic_var_t
*var
)
1992 VariantClear(&var
->v
);
1997 static void release_exec(exec_ctx_t
*ctx
)
2001 VariantClear(&ctx
->ret_val
);
2002 release_dynamic_vars(ctx
->dynamic_vars
);
2005 IDispatch_Release(ctx
->this_obj
);
2008 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++)
2009 VariantClear(ctx
->args
+i
);
2013 for(i
=0; i
< ctx
->func
->var_cnt
; i
++)
2014 VariantClear(ctx
->vars
+i
);
2018 for(i
=0; i
< ctx
->func
->var_cnt
; i
++) {
2020 SafeArrayDestroy(ctx
->arrays
[i
]);
2022 heap_free(ctx
->arrays
);
2025 heap_pool_free(&ctx
->heap
);
2026 heap_free(ctx
->args
);
2027 heap_free(ctx
->vars
);
2028 heap_free(ctx
->stack
);
2031 HRESULT
exec_script(script_ctx_t
*ctx
, function_t
*func
, vbdisp_t
*vbthis
, DISPPARAMS
*dp
, VARIANT
*res
)
2033 exec_ctx_t exec
= {func
->code_ctx
};
2035 HRESULT hres
= S_OK
;
2037 exec
.code
= func
->code_ctx
;
2039 if(dp
? func
->arg_cnt
!= arg_cnt(dp
) : func
->arg_cnt
) {
2040 FIXME("wrong arg_cnt %d, expected %d\n", dp
? arg_cnt(dp
) : 0, func
->arg_cnt
);
2044 heap_pool_init(&exec
.heap
);
2050 exec
.args
= heap_alloc_zero(func
->arg_cnt
* sizeof(VARIANT
));
2052 release_exec(&exec
);
2053 return E_OUTOFMEMORY
;
2056 for(i
=0; i
< func
->arg_cnt
; i
++) {
2058 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
)) {
2059 if(func
->args
[i
].by_ref
)
2062 hres
= VariantCopyInd(exec
.args
+i
, V_VARIANTREF(v
));
2064 hres
= VariantCopyInd(exec
.args
+i
, v
);
2067 release_exec(&exec
);
2076 exec
.vars
= heap_alloc_zero(func
->var_cnt
* sizeof(VARIANT
));
2078 release_exec(&exec
);
2079 return E_OUTOFMEMORY
;
2085 exec
.stack_size
= 16;
2087 exec
.stack
= heap_alloc(exec
.stack_size
* sizeof(VARIANT
));
2089 release_exec(&exec
);
2090 return E_OUTOFMEMORY
;
2094 exec
.this_obj
= (IDispatch
*)&vbthis
->IDispatchEx_iface
;
2095 exec
.vbthis
= vbthis
;
2096 }else if (ctx
->host_global
) {
2097 exec
.this_obj
= ctx
->host_global
;
2099 exec
.this_obj
= (IDispatch
*)&ctx
->script_obj
->IDispatchEx_iface
;
2101 IDispatch_AddRef(exec
.this_obj
);
2103 exec
.instr
= exec
.code
->instrs
+ func
->code_off
;
2108 op
= exec
.instr
->op
;
2109 hres
= op_funcs
[op
](&exec
);
2111 ctx
->err_number
= hres
= map_hres(hres
);
2113 if(exec
.resume_next
) {
2116 WARN("Failed %08x in resume next mode\n", hres
);
2119 * Unwinding here is simple. We need to find the next OP_catch, which contains
2120 * information about expected stack size and jump offset on error. Generated
2121 * bytecode needs to guarantee, that simple jump and stack adjustment will
2122 * guarantee proper execution continuation.
2124 while((++exec
.instr
)->op
!= OP_catch
);
2126 TRACE("unwind jmp %d stack_off %d\n", exec
.instr
->arg1
.uint
, exec
.instr
->arg2
.uint
);
2128 stack_off
= exec
.instr
->arg2
.uint
;
2129 instr_jmp(&exec
, exec
.instr
->arg1
.uint
);
2131 if(exec
.top
> stack_off
) {
2132 stack_popn(&exec
, exec
.top
-stack_off
);
2133 }else if(exec
.top
< stack_off
) {
2136 V_VT(&v
) = VT_EMPTY
;
2137 while(exec
.top
< stack_off
) {
2138 hres
= stack_push(&exec
, &v
);
2146 WARN("Failed %08x\n", hres
);
2147 stack_popn(&exec
, exec
.top
);
2152 exec
.instr
+= op_move
[op
];
2156 if(func
->type
!= FUNC_FUNCTION
&& func
->type
!= FUNC_PROPGET
&& func
->type
!= FUNC_DEFGET
)
2157 assert(V_VT(&exec
.ret_val
) == VT_EMPTY
);
2159 if(SUCCEEDED(hres
) && res
) {
2160 *res
= exec
.ret_val
;
2161 V_VT(&exec
.ret_val
) = VT_EMPTY
;
2164 release_exec(&exec
);