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
, item
->name
, SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
188 WARN("GetItemInfo failed: %08x\n", hres
);
192 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
193 IUnknown_Release(unk
);
195 WARN("object does not implement IDispatch\n");
201 ref
->u
.obj
= item
->disp
;
206 LIST_FOR_EACH_ENTRY(item
, &ctx
->script
->named_items
, named_item_t
, entry
) {
207 if((item
->flags
& SCRIPTITEM_GLOBALMEMBERS
)) {
208 hres
= disp_get_id(item
->disp
, name
, invoke_type
, FALSE
, &id
);
209 if(SUCCEEDED(hres
)) {
210 ref
->type
= REF_DISP
;
211 ref
->u
.d
.disp
= item
->disp
;
218 ref
->type
= REF_NONE
;
222 static HRESULT
add_dynamic_var(exec_ctx_t
*ctx
, const WCHAR
*name
,
223 BOOL is_const
, VARIANT
**out_var
)
225 dynamic_var_t
*new_var
;
230 heap
= ctx
->func
->type
== FUNC_GLOBAL
? &ctx
->script
->heap
: &ctx
->heap
;
232 new_var
= heap_pool_alloc(heap
, sizeof(*new_var
));
234 return E_OUTOFMEMORY
;
236 size
= (strlenW(name
)+1)*sizeof(WCHAR
);
237 str
= heap_pool_alloc(heap
, size
);
239 return E_OUTOFMEMORY
;
240 memcpy(str
, name
, size
);
242 new_var
->is_const
= is_const
;
243 V_VT(&new_var
->v
) = VT_EMPTY
;
245 if(ctx
->func
->type
== FUNC_GLOBAL
) {
246 new_var
->next
= ctx
->script
->global_vars
;
247 ctx
->script
->global_vars
= new_var
;
249 new_var
->next
= ctx
->dynamic_vars
;
250 ctx
->dynamic_vars
= new_var
;
253 *out_var
= &new_var
->v
;
257 static inline VARIANT
*stack_pop(exec_ctx_t
*ctx
)
260 return ctx
->stack
+ --ctx
->top
;
263 static inline VARIANT
*stack_top(exec_ctx_t
*ctx
, unsigned n
)
265 assert(ctx
->top
>= n
);
266 return ctx
->stack
+ (ctx
->top
-n
-1);
269 static HRESULT
stack_push(exec_ctx_t
*ctx
, VARIANT
*v
)
271 if(ctx
->stack_size
== ctx
->top
) {
274 new_stack
= heap_realloc(ctx
->stack
, ctx
->stack_size
*2*sizeof(*ctx
->stack
));
277 return E_OUTOFMEMORY
;
280 ctx
->stack
= new_stack
;
281 ctx
->stack_size
*= 2;
284 ctx
->stack
[ctx
->top
++] = *v
;
288 static inline HRESULT
stack_push_null(exec_ctx_t
*ctx
)
292 return stack_push(ctx
, &v
);
295 static void stack_popn(exec_ctx_t
*ctx
, unsigned n
)
298 VariantClear(stack_pop(ctx
));
301 static void stack_pop_deref(exec_ctx_t
*ctx
, variant_val_t
*r
)
306 if(V_VT(v
) == (VT_BYREF
|VT_VARIANT
)) {
308 r
->v
= V_VARIANTREF(v
);
315 static inline void release_val(variant_val_t
*v
)
321 static HRESULT
stack_pop_val(exec_ctx_t
*ctx
, variant_val_t
*r
)
323 stack_pop_deref(ctx
, r
);
325 if(V_VT(r
->v
) == VT_DISPATCH
) {
328 hres
= get_disp_value(ctx
->script
, V_DISPATCH(r
->v
), &r
->store
);
330 IDispatch_Release(V_DISPATCH(r
->v
));
341 static HRESULT
stack_assume_val(exec_ctx_t
*ctx
, unsigned n
)
343 VARIANT
*v
= stack_top(ctx
, n
);
346 if(V_VT(v
) == (VT_BYREF
|VT_VARIANT
)) {
347 VARIANT
*ref
= V_VARIANTREF(v
);
350 hres
= VariantCopy(v
, ref
);
355 if(V_VT(v
) == VT_DISPATCH
) {
358 disp
= V_DISPATCH(v
);
359 hres
= get_disp_value(ctx
->script
, disp
, v
);
360 IDispatch_Release(disp
);
368 static int stack_pop_bool(exec_ctx_t
*ctx
, BOOL
*b
)
373 hres
= stack_pop_val(ctx
, &val
);
392 FIXME("unsupported for %s\n", debugstr_variant(val
.v
));
399 static HRESULT
stack_pop_disp(exec_ctx_t
*ctx
, IDispatch
**ret
)
401 VARIANT
*v
= stack_pop(ctx
);
403 if(V_VT(v
) == VT_DISPATCH
) {
404 *ret
= V_DISPATCH(v
);
408 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
409 FIXME("not supported type: %s\n", debugstr_variant(v
));
415 if(V_VT(v
) != VT_DISPATCH
) {
416 FIXME("not disp %s\n", debugstr_variant(v
));
421 IDispatch_AddRef(V_DISPATCH(v
));
422 *ret
= V_DISPATCH(v
);
426 static HRESULT
stack_assume_disp(exec_ctx_t
*ctx
, unsigned n
, IDispatch
**disp
)
428 VARIANT
*v
= stack_top(ctx
, n
), *ref
;
430 if(V_VT(v
) != VT_DISPATCH
) {
431 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
432 FIXME("not supported type: %s\n", debugstr_variant(v
));
436 ref
= V_VARIANTREF(v
);
437 if(V_VT(ref
) != VT_DISPATCH
) {
438 FIXME("not disp %s\n", debugstr_variant(ref
));
442 V_VT(v
) = VT_DISPATCH
;
443 V_DISPATCH(v
) = V_DISPATCH(ref
);
445 IDispatch_AddRef(V_DISPATCH(v
));
449 *disp
= V_DISPATCH(v
);
453 static inline void instr_jmp(exec_ctx_t
*ctx
, unsigned addr
)
455 ctx
->instr
= ctx
->code
->instrs
+ addr
;
458 static void vbstack_to_dp(exec_ctx_t
*ctx
, unsigned arg_cnt
, BOOL is_propput
, DISPPARAMS
*dp
)
460 dp
->cNamedArgs
= is_propput
? 1 : 0;
461 dp
->cArgs
= arg_cnt
+ dp
->cNamedArgs
;
462 dp
->rgdispidNamedArgs
= is_propput
? &propput_dispid
: NULL
;
468 assert(ctx
->top
>= arg_cnt
);
470 for(i
=1; i
*2 <= arg_cnt
; i
++) {
471 tmp
= ctx
->stack
[ctx
->top
-i
];
472 ctx
->stack
[ctx
->top
-i
] = ctx
->stack
[ctx
->top
-arg_cnt
+i
-1];
473 ctx
->stack
[ctx
->top
-arg_cnt
+i
-1] = tmp
;
476 dp
->rgvarg
= ctx
->stack
+ ctx
->top
-dp
->cArgs
;
478 dp
->rgvarg
= is_propput
? ctx
->stack
+ctx
->top
-1 : NULL
;
482 static HRESULT
array_access(exec_ctx_t
*ctx
, SAFEARRAY
*array
, DISPPARAMS
*dp
, VARIANT
**ret
)
484 unsigned i
, argc
= arg_cnt(dp
);
489 FIXME("NULL array\n");
493 hres
= SafeArrayLock(array
);
497 if(array
->cDims
!= argc
) {
498 FIXME("argc %d does not match cDims %d\n", dp
->cArgs
, array
->cDims
);
499 SafeArrayUnlock(array
);
503 indices
= heap_alloc(sizeof(*indices
) * argc
);
505 SafeArrayUnlock(array
);
506 return E_OUTOFMEMORY
;
509 for(i
=0; i
<argc
; i
++) {
510 hres
= to_int(get_arg(dp
, i
), indices
+i
);
513 SafeArrayUnlock(array
);
518 hres
= SafeArrayPtrOfIndex(array
, indices
, (void**)ret
);
519 SafeArrayUnlock(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
;
549 SAFEARRAY
*array
= NULL
;
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 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
560 hres
= disp_call(ctx
->script
, V_DISPATCH(v
), DISPID_VALUE
, &dp
, res
);
565 FIXME("arguments not implemented\n");
572 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
573 hres
= array_access(ctx
, array
, &dp
, &v
);
578 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
583 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
584 hres
= disp_call(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, &dp
, res
);
589 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
590 hres
= exec_script(ctx
->script
, ref
.u
.f
, NULL
, &dp
, res
);
596 FIXME("arguments on object\n");
601 IDispatch_AddRef(ref
.u
.obj
);
602 V_VT(res
) = VT_DISPATCH
;
603 V_DISPATCH(res
) = ref
.u
.obj
;
607 if(res
&& !ctx
->func
->code_ctx
->option_explicit
&& arg_cnt
== 0) {
609 hres
= add_dynamic_var(ctx
, identifier
, FALSE
, &new);
612 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
616 FIXME("%s not found\n", debugstr_w(identifier
));
617 return DISP_E_UNKNOWNNAME
;
620 stack_popn(ctx
, arg_cnt
);
624 static HRESULT
interp_icall(exec_ctx_t
*ctx
)
631 hres
= do_icall(ctx
, &v
);
635 return stack_push(ctx
, &v
);
638 static HRESULT
interp_icallv(exec_ctx_t
*ctx
)
641 return do_icall(ctx
, NULL
);
644 static HRESULT
do_mcall(exec_ctx_t
*ctx
, VARIANT
*res
)
646 const BSTR identifier
= ctx
->instr
->arg1
.bstr
;
647 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
653 hres
= stack_pop_disp(ctx
, &obj
);
662 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
664 hres
= disp_get_id(obj
, identifier
, VBDISP_CALLGET
, FALSE
, &id
);
666 hres
= disp_call(ctx
->script
, obj
, id
, &dp
, res
);
667 IDispatch_Release(obj
);
671 stack_popn(ctx
, arg_cnt
);
675 static HRESULT
interp_mcall(exec_ctx_t
*ctx
)
682 hres
= do_mcall(ctx
, &res
);
686 return stack_push(ctx
, &res
);
689 static HRESULT
interp_mcallv(exec_ctx_t
*ctx
)
693 return do_mcall(ctx
, NULL
);
696 static HRESULT
assign_value(exec_ctx_t
*ctx
, VARIANT
*dst
, VARIANT
*src
, WORD flags
)
700 hres
= VariantCopyInd(dst
, src
);
704 if(V_VT(dst
) == VT_DISPATCH
&& !(flags
& DISPATCH_PROPERTYPUTREF
)) {
707 hres
= get_disp_value(ctx
->script
, V_DISPATCH(dst
), &value
);
708 IDispatch_Release(V_DISPATCH(dst
));
718 static HRESULT
assign_ident(exec_ctx_t
*ctx
, BSTR name
, WORD flags
, DISPPARAMS
*dp
)
723 hres
= lookup_identifier(ctx
, name
, VBDISP_LET
, &ref
);
729 VARIANT
*v
= ref
.u
.v
;
731 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
))
737 if(!(V_VT(v
) & VT_ARRAY
)) {
738 FIXME("array assign on type %d\n", V_VT(v
));
743 case VT_ARRAY
|VT_BYREF
|VT_VARIANT
:
744 array
= *V_ARRAYREF(v
);
746 case VT_ARRAY
|VT_VARIANT
:
750 FIXME("Unsupported array type %x\n", V_VT(v
));
755 FIXME("null array\n");
759 hres
= array_access(ctx
, array
, dp
, &v
);
762 }else if(V_VT(v
) == (VT_ARRAY
|VT_BYREF
|VT_VARIANT
)) {
763 FIXME("non-array assign\n");
767 hres
= assign_value(ctx
, v
, dp
->rgvarg
, flags
);
771 hres
= disp_propput(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, flags
, dp
);
774 FIXME("functions not implemented\n");
780 FIXME("REF_CONST\n");
783 if(ctx
->func
->code_ctx
->option_explicit
) {
784 FIXME("throw exception\n");
790 FIXME("arg_cnt %d not supported\n", arg_cnt(dp
));
794 TRACE("creating variable %s\n", debugstr_w(name
));
795 hres
= add_dynamic_var(ctx
, name
, FALSE
, &new_var
);
797 hres
= assign_value(ctx
, new_var
, dp
->rgvarg
, flags
);
804 static HRESULT
interp_assign_ident(exec_ctx_t
*ctx
)
806 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
807 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
811 TRACE("%s\n", debugstr_w(arg
));
813 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
814 hres
= assign_ident(ctx
, arg
, DISPATCH_PROPERTYPUT
, &dp
);
818 stack_popn(ctx
, arg_cnt
+1);
822 static HRESULT
interp_set_ident(exec_ctx_t
*ctx
)
824 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
825 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
829 TRACE("%s\n", debugstr_w(arg
));
832 FIXME("arguments not supported\n");
836 hres
= stack_assume_disp(ctx
, 0, NULL
);
840 vbstack_to_dp(ctx
, 0, TRUE
, &dp
);
841 hres
= assign_ident(ctx
, ctx
->instr
->arg1
.bstr
, DISPATCH_PROPERTYPUTREF
, &dp
);
849 static HRESULT
interp_assign_member(exec_ctx_t
*ctx
)
851 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
852 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
858 TRACE("%s\n", debugstr_w(identifier
));
860 hres
= stack_assume_disp(ctx
, arg_cnt
+1, &obj
);
869 hres
= disp_get_id(obj
, identifier
, VBDISP_LET
, FALSE
, &id
);
870 if(SUCCEEDED(hres
)) {
871 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
872 hres
= disp_propput(ctx
->script
, obj
, id
, DISPATCH_PROPERTYPUT
, &dp
);
877 stack_popn(ctx
, arg_cnt
+2);
881 static HRESULT
interp_set_member(exec_ctx_t
*ctx
)
883 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
884 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
890 TRACE("%s\n", debugstr_w(identifier
));
893 FIXME("arguments not supported\n");
897 hres
= stack_assume_disp(ctx
, 1, &obj
);
906 hres
= stack_assume_disp(ctx
, 0, NULL
);
910 hres
= disp_get_id(obj
, identifier
, VBDISP_SET
, FALSE
, &id
);
911 if(SUCCEEDED(hres
)) {
912 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
913 hres
= disp_propput(ctx
->script
, obj
, id
, DISPATCH_PROPERTYPUTREF
, &dp
);
922 static HRESULT
interp_const(exec_ctx_t
*ctx
)
924 BSTR arg
= ctx
->instr
->arg1
.bstr
;
929 TRACE("%s\n", debugstr_w(arg
));
931 assert(ctx
->func
->type
== FUNC_GLOBAL
);
933 hres
= lookup_identifier(ctx
, arg
, VBDISP_CALLGET
, &ref
);
937 if(ref
.type
!= REF_NONE
) {
938 FIXME("%s already defined\n", debugstr_w(arg
));
942 hres
= stack_assume_val(ctx
, 0);
946 hres
= add_dynamic_var(ctx
, arg
, TRUE
, &v
);
950 *v
= *stack_pop(ctx
);
954 static HRESULT
interp_val(exec_ctx_t
*ctx
)
962 hres
= stack_pop_val(ctx
, &val
);
968 hres
= VariantCopy(&v
, val
.v
);
973 return stack_push(ctx
, val
.owned
? val
.v
: &v
);
976 static HRESULT
interp_pop(exec_ctx_t
*ctx
)
978 const unsigned n
= ctx
->instr
->arg1
.uint
;
986 static HRESULT
interp_new(exec_ctx_t
*ctx
)
988 const WCHAR
*arg
= ctx
->instr
->arg1
.bstr
;
989 class_desc_t
*class_desc
;
994 static const WCHAR regexpW
[] = {'r','e','g','e','x','p',0};
996 TRACE("%s\n", debugstr_w(arg
));
998 if(!strcmpiW(arg
, regexpW
)) {
999 V_VT(&v
) = VT_DISPATCH
;
1000 hres
= create_regexp(&V_DISPATCH(&v
));
1004 return stack_push(ctx
, &v
);
1007 for(class_desc
= ctx
->script
->classes
; class_desc
; class_desc
= class_desc
->next
) {
1008 if(!strcmpiW(class_desc
->name
, arg
))
1012 FIXME("Class %s not found\n", debugstr_w(arg
));
1016 hres
= create_vbdisp(class_desc
, &obj
);
1020 V_VT(&v
) = VT_DISPATCH
;
1021 V_DISPATCH(&v
) = (IDispatch
*)&obj
->IDispatchEx_iface
;
1022 return stack_push(ctx
, &v
);
1025 static HRESULT
interp_dim(exec_ctx_t
*ctx
)
1027 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
1028 const unsigned array_id
= ctx
->instr
->arg2
.uint
;
1029 const array_desc_t
*array_desc
;
1033 TRACE("%s\n", debugstr_w(ident
));
1035 assert(array_id
< ctx
->func
->array_cnt
);
1037 ctx
->arrays
= heap_alloc_zero(ctx
->func
->array_cnt
* sizeof(SAFEARRAY
*));
1039 return E_OUTOFMEMORY
;
1042 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1044 FIXME("lookup %s failed: %08x\n", debugstr_w(ident
), hres
);
1048 if(ref
.type
!= REF_VAR
) {
1049 FIXME("got ref.type = %d\n", ref
.type
);
1053 if(ctx
->arrays
[array_id
]) {
1054 FIXME("Array already initialized\n");
1058 array_desc
= ctx
->func
->array_descs
+ array_id
;
1059 if(array_desc
->dim_cnt
) {
1060 ctx
->arrays
[array_id
] = SafeArrayCreate(VT_VARIANT
, array_desc
->dim_cnt
, array_desc
->bounds
);
1061 if(!ctx
->arrays
[array_id
])
1062 return E_OUTOFMEMORY
;
1065 V_VT(ref
.u
.v
) = VT_ARRAY
|VT_BYREF
|VT_VARIANT
;
1066 V_ARRAYREF(ref
.u
.v
) = ctx
->arrays
+array_id
;
1070 static HRESULT
interp_step(exec_ctx_t
*ctx
)
1072 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1078 TRACE("%s\n", debugstr_w(ident
));
1080 V_VT(&zero
) = VT_I2
;
1082 hres
= VarCmp(stack_top(ctx
, 0), &zero
, ctx
->script
->lcid
, 0);
1086 gteq_zero
= hres
== VARCMP_GT
|| hres
== VARCMP_EQ
;
1088 hres
= lookup_identifier(ctx
, ident
, VBDISP_ANY
, &ref
);
1092 if(ref
.type
!= REF_VAR
) {
1093 FIXME("%s is not REF_VAR\n", debugstr_w(ident
));
1097 hres
= VarCmp(ref
.u
.v
, stack_top(ctx
, 1), ctx
->script
->lcid
, 0);
1101 if(hres
== VARCMP_EQ
|| hres
== (gteq_zero
? VARCMP_LT
: VARCMP_GT
)) {
1105 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1110 static HRESULT
interp_newenum(exec_ctx_t
*ctx
)
1118 stack_pop_deref(ctx
, &v
);
1119 assert(V_VT(stack_top(ctx
, 0)) == VT_EMPTY
);
1120 r
= stack_top(ctx
, 0);
1123 case VT_DISPATCH
|VT_BYREF
:
1126 DISPPARAMS dp
= {0};
1129 hres
= disp_call(ctx
->script
, V_ISBYREF(v
.v
) ? *V_DISPATCHREF(v
.v
) : V_DISPATCH(v
.v
), DISPID_NEWENUM
, &dp
, &iterv
);
1134 if(V_VT(&iterv
) != VT_UNKNOWN
&& V_VT(&iterv
) != VT_DISPATCH
) {
1135 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv
));
1136 VariantClear(&iterv
);
1140 hres
= IUnknown_QueryInterface(V_UNKNOWN(&iterv
), &IID_IEnumVARIANT
, (void**)&iter
);
1141 IUnknown_Release(V_UNKNOWN(&iterv
));
1143 FIXME("Could not get IEnumVARIANT iface: %08x\n", hres
);
1147 V_VT(r
) = VT_UNKNOWN
;
1148 V_UNKNOWN(r
) = (IUnknown
*)iter
;
1151 case VT_VARIANT
|VT_ARRAY
:
1152 case VT_VARIANT
|VT_ARRAY
|VT_BYREF
: {
1155 hres
= create_safearray_iter(V_ISBYREF(v
.v
) ? *V_ARRAYREF(v
.v
) : V_ARRAY(v
.v
), &iter
);
1159 V_VT(r
) = VT_UNKNOWN
;
1160 V_UNKNOWN(r
) = (IUnknown
*)iter
;
1164 FIXME("Unsupported for %s\n", debugstr_variant(v
.v
));
1172 static HRESULT
interp_enumnext(exec_ctx_t
*ctx
)
1174 const unsigned loop_end
= ctx
->instr
->arg1
.uint
;
1175 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1177 DISPPARAMS dp
= {&v
, &propput_dispid
, 1, 1};
1184 if(V_VT(stack_top(ctx
, 0)) == VT_EMPTY
) {
1185 FIXME("uninitialized\n");
1189 assert(V_VT(stack_top(ctx
, 0)) == VT_UNKNOWN
);
1190 iter
= (IEnumVARIANT
*)V_UNKNOWN(stack_top(ctx
, 0));
1192 V_VT(&v
) = VT_EMPTY
;
1193 hres
= IEnumVARIANT_Next(iter
, 1, &v
, NULL
);
1197 do_continue
= hres
== S_OK
;
1198 hres
= assign_ident(ctx
, ident
, DISPATCH_PROPERTYPUT
|DISPATCH_PROPERTYPUTREF
, &dp
);
1207 instr_jmp(ctx
, loop_end
);
1212 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
1214 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1218 instr_jmp(ctx
, arg
);
1222 static HRESULT
interp_jmp_false(exec_ctx_t
*ctx
)
1224 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1230 hres
= stack_pop_bool(ctx
, &b
);
1237 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1241 static HRESULT
interp_jmp_true(exec_ctx_t
*ctx
)
1243 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1249 hres
= stack_pop_bool(ctx
, &b
);
1254 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1260 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
1268 static HRESULT
interp_stop(exec_ctx_t
*ctx
)
1272 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1276 static HRESULT
interp_me(exec_ctx_t
*ctx
)
1282 IDispatch_AddRef(ctx
->this_obj
);
1283 V_VT(&v
) = VT_DISPATCH
;
1284 V_DISPATCH(&v
) = ctx
->this_obj
;
1285 return stack_push(ctx
, &v
);
1288 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
1290 const VARIANT_BOOL arg
= ctx
->instr
->arg1
.lng
;
1293 TRACE("%s\n", arg
? "true" : "false");
1297 return stack_push(ctx
, &v
);
1300 static HRESULT
interp_errmode(exec_ctx_t
*ctx
)
1302 const int err_mode
= ctx
->instr
->arg1
.uint
;
1304 TRACE("%d\n", err_mode
);
1306 ctx
->resume_next
= err_mode
;
1307 ctx
->script
->err_number
= S_OK
;
1311 static HRESULT
interp_string(exec_ctx_t
*ctx
)
1318 V_BSTR(&v
) = SysAllocString(ctx
->instr
->arg1
.str
);
1320 return E_OUTOFMEMORY
;
1322 return stack_push(ctx
, &v
);
1325 static HRESULT
interp_long(exec_ctx_t
*ctx
)
1327 const LONG arg
= ctx
->instr
->arg1
.lng
;
1334 return stack_push(ctx
, &v
);
1337 static HRESULT
interp_short(exec_ctx_t
*ctx
)
1339 const LONG arg
= ctx
->instr
->arg1
.lng
;
1346 return stack_push(ctx
, &v
);
1349 static HRESULT
interp_double(exec_ctx_t
*ctx
)
1351 const DOUBLE
*arg
= ctx
->instr
->arg1
.dbl
;
1354 TRACE("%lf\n", *arg
);
1358 return stack_push(ctx
, &v
);
1361 static HRESULT
interp_empty(exec_ctx_t
*ctx
)
1367 V_VT(&v
) = VT_EMPTY
;
1368 return stack_push(ctx
, &v
);
1371 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1374 return stack_push_null(ctx
);
1377 static HRESULT
interp_nothing(exec_ctx_t
*ctx
)
1383 V_VT(&v
) = VT_DISPATCH
;
1384 V_DISPATCH(&v
) = NULL
;
1385 return stack_push(ctx
, &v
);
1388 static HRESULT
interp_hres(exec_ctx_t
*ctx
)
1390 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1395 V_VT(&v
) = VT_ERROR
;
1397 return stack_push(ctx
, &v
);
1400 static HRESULT
interp_not(exec_ctx_t
*ctx
)
1408 hres
= stack_pop_val(ctx
, &val
);
1412 hres
= VarNot(val
.v
, &v
);
1417 return stack_push(ctx
, &v
);
1420 static HRESULT
interp_and(exec_ctx_t
*ctx
)
1428 hres
= stack_pop_val(ctx
, &r
);
1432 hres
= stack_pop_val(ctx
, &l
);
1433 if(SUCCEEDED(hres
)) {
1434 hres
= VarAnd(l
.v
, r
.v
, &v
);
1441 return stack_push(ctx
, &v
);
1444 static HRESULT
interp_or(exec_ctx_t
*ctx
)
1452 hres
= stack_pop_val(ctx
, &r
);
1456 hres
= stack_pop_val(ctx
, &l
);
1457 if(SUCCEEDED(hres
)) {
1458 hres
= VarOr(l
.v
, r
.v
, &v
);
1465 return stack_push(ctx
, &v
);
1468 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
1476 hres
= stack_pop_val(ctx
, &r
);
1480 hres
= stack_pop_val(ctx
, &l
);
1481 if(SUCCEEDED(hres
)) {
1482 hres
= VarXor(l
.v
, r
.v
, &v
);
1489 return stack_push(ctx
, &v
);
1492 static HRESULT
interp_eqv(exec_ctx_t
*ctx
)
1500 hres
= stack_pop_val(ctx
, &r
);
1504 hres
= stack_pop_val(ctx
, &l
);
1505 if(SUCCEEDED(hres
)) {
1506 hres
= VarEqv(l
.v
, r
.v
, &v
);
1513 return stack_push(ctx
, &v
);
1516 static HRESULT
interp_imp(exec_ctx_t
*ctx
)
1524 hres
= stack_pop_val(ctx
, &r
);
1528 hres
= stack_pop_val(ctx
, &l
);
1529 if(SUCCEEDED(hres
)) {
1530 hres
= VarImp(l
.v
, r
.v
, &v
);
1537 return stack_push(ctx
, &v
);
1540 static HRESULT
var_cmp(exec_ctx_t
*ctx
, VARIANT
*l
, VARIANT
*r
)
1542 TRACE("%s %s\n", debugstr_variant(l
), debugstr_variant(r
));
1544 /* FIXME: Fix comparing string to number */
1546 return VarCmp(l
, r
, ctx
->script
->lcid
, 0);
1549 static HRESULT
cmp_oper(exec_ctx_t
*ctx
)
1554 hres
= stack_pop_val(ctx
, &r
);
1558 hres
= stack_pop_val(ctx
, &l
);
1559 if(SUCCEEDED(hres
)) {
1560 hres
= var_cmp(ctx
, l
.v
, r
.v
);
1568 static HRESULT
interp_equal(exec_ctx_t
*ctx
)
1575 hres
= cmp_oper(ctx
);
1578 if(hres
== VARCMP_NULL
)
1579 return stack_push_null(ctx
);
1582 V_BOOL(&v
) = hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1583 return stack_push(ctx
, &v
);
1586 static HRESULT
interp_nequal(exec_ctx_t
*ctx
)
1593 hres
= cmp_oper(ctx
);
1596 if(hres
== VARCMP_NULL
)
1597 return stack_push_null(ctx
);
1600 V_BOOL(&v
) = hres
!= VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1601 return stack_push(ctx
, &v
);
1604 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
1611 hres
= cmp_oper(ctx
);
1614 if(hres
== VARCMP_NULL
)
1615 return stack_push_null(ctx
);
1618 V_BOOL(&v
) = hres
== VARCMP_GT
? VARIANT_TRUE
: VARIANT_FALSE
;
1619 return stack_push(ctx
, &v
);
1622 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
1629 hres
= cmp_oper(ctx
);
1632 if(hres
== VARCMP_NULL
)
1633 return stack_push_null(ctx
);
1636 V_BOOL(&v
) = hres
== VARCMP_GT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1637 return stack_push(ctx
, &v
);
1640 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
1647 hres
= cmp_oper(ctx
);
1650 if(hres
== VARCMP_NULL
)
1651 return stack_push_null(ctx
);
1654 V_BOOL(&v
) = hres
== VARCMP_LT
? VARIANT_TRUE
: VARIANT_FALSE
;
1655 return stack_push(ctx
, &v
);
1658 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
1665 hres
= cmp_oper(ctx
);
1668 if(hres
== VARCMP_NULL
)
1669 return stack_push_null(ctx
);
1672 V_BOOL(&v
) = hres
== VARCMP_LT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1673 return stack_push(ctx
, &v
);
1676 static HRESULT
interp_case(exec_ctx_t
*ctx
)
1678 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1684 hres
= stack_pop_val(ctx
, &v
);
1688 hres
= var_cmp(ctx
, stack_top(ctx
, 0), v
.v
);
1693 if(hres
== VARCMP_EQ
) {
1695 instr_jmp(ctx
, arg
);
1703 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, VARIANT_BOOL
*ret
)
1705 IObjectIdentity
*identity
;
1706 IUnknown
*unk1
, *unk2
;
1709 if(disp1
== disp2
) {
1710 *ret
= VARIANT_TRUE
;
1714 if(!disp1
|| !disp2
) {
1715 *ret
= VARIANT_FALSE
;
1719 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
1723 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
1725 IUnknown_Release(unk1
);
1730 *ret
= VARIANT_TRUE
;
1732 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
1733 if(SUCCEEDED(hres
)) {
1734 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
1735 IObjectIdentity_Release(identity
);
1736 *ret
= hres
== S_OK
? VARIANT_TRUE
: VARIANT_FALSE
;
1738 *ret
= VARIANT_FALSE
;
1742 IUnknown_Release(unk1
);
1743 IUnknown_Release(unk2
);
1747 static HRESULT
interp_is(exec_ctx_t
*ctx
)
1755 hres
= stack_pop_disp(ctx
, &r
);
1759 hres
= stack_pop_disp(ctx
, &l
);
1760 if(SUCCEEDED(hres
)) {
1762 hres
= disp_cmp(l
, r
, &V_BOOL(&v
));
1764 IDispatch_Release(l
);
1767 IDispatch_Release(r
);
1771 return stack_push(ctx
, &v
);
1774 static HRESULT
interp_concat(exec_ctx_t
*ctx
)
1782 hres
= stack_pop_val(ctx
, &r
);
1786 hres
= stack_pop_val(ctx
, &l
);
1787 if(SUCCEEDED(hres
)) {
1788 hres
= VarCat(l
.v
, r
.v
, &v
);
1795 return stack_push(ctx
, &v
);
1798 static HRESULT
interp_add(exec_ctx_t
*ctx
)
1806 hres
= stack_pop_val(ctx
, &r
);
1810 hres
= stack_pop_val(ctx
, &l
);
1811 if(SUCCEEDED(hres
)) {
1812 hres
= VarAdd(l
.v
, r
.v
, &v
);
1819 return stack_push(ctx
, &v
);
1822 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
1830 hres
= stack_pop_val(ctx
, &r
);
1834 hres
= stack_pop_val(ctx
, &l
);
1835 if(SUCCEEDED(hres
)) {
1836 hres
= VarSub(l
.v
, r
.v
, &v
);
1843 return stack_push(ctx
, &v
);
1846 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
1854 hres
= stack_pop_val(ctx
, &r
);
1858 hres
= stack_pop_val(ctx
, &l
);
1859 if(SUCCEEDED(hres
)) {
1860 hres
= VarMod(l
.v
, r
.v
, &v
);
1867 return stack_push(ctx
, &v
);
1870 static HRESULT
interp_idiv(exec_ctx_t
*ctx
)
1878 hres
= stack_pop_val(ctx
, &r
);
1882 hres
= stack_pop_val(ctx
, &l
);
1883 if(SUCCEEDED(hres
)) {
1884 hres
= VarIdiv(l
.v
, r
.v
, &v
);
1891 return stack_push(ctx
, &v
);
1894 static HRESULT
interp_div(exec_ctx_t
*ctx
)
1902 hres
= stack_pop_val(ctx
, &r
);
1906 hres
= stack_pop_val(ctx
, &l
);
1907 if(SUCCEEDED(hres
)) {
1908 hres
= VarDiv(l
.v
, r
.v
, &v
);
1915 return stack_push(ctx
, &v
);
1918 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
1926 hres
= stack_pop_val(ctx
, &r
);
1930 hres
= stack_pop_val(ctx
, &l
);
1931 if(SUCCEEDED(hres
)) {
1932 hres
= VarMul(l
.v
, r
.v
, &v
);
1939 return stack_push(ctx
, &v
);
1942 static HRESULT
interp_exp(exec_ctx_t
*ctx
)
1950 hres
= stack_pop_val(ctx
, &r
);
1954 hres
= stack_pop_val(ctx
, &l
);
1955 if(SUCCEEDED(hres
)) {
1956 hres
= VarPow(l
.v
, r
.v
, &v
);
1963 return stack_push(ctx
, &v
);
1966 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
1972 hres
= stack_pop_val(ctx
, &val
);
1976 hres
= VarNeg(val
.v
, &v
);
1981 return stack_push(ctx
, &v
);
1984 static HRESULT
interp_incc(exec_ctx_t
*ctx
)
1986 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
1993 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1997 if(ref
.type
!= REF_VAR
) {
1998 FIXME("ref.type is not REF_VAR\n");
2002 hres
= VarAdd(stack_top(ctx
, 0), ref
.u
.v
, &v
);
2006 VariantClear(ref
.u
.v
);
2011 static HRESULT
interp_catch(exec_ctx_t
*ctx
)
2013 /* Nothing to do here, the OP is for unwinding only. */
2017 static const instr_func_t op_funcs
[] = {
2018 #define X(x,n,a,b) interp_ ## x,
2023 static const unsigned op_move
[] = {
2024 #define X(x,n,a,b) n,
2029 void release_dynamic_vars(dynamic_var_t
*var
)
2032 VariantClear(&var
->v
);
2037 static void release_exec(exec_ctx_t
*ctx
)
2041 VariantClear(&ctx
->ret_val
);
2042 release_dynamic_vars(ctx
->dynamic_vars
);
2045 IDispatch_Release(ctx
->this_obj
);
2048 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++)
2049 VariantClear(ctx
->args
+i
);
2053 for(i
=0; i
< ctx
->func
->var_cnt
; i
++)
2054 VariantClear(ctx
->vars
+i
);
2058 for(i
=0; i
< ctx
->func
->var_cnt
; i
++) {
2060 SafeArrayDestroy(ctx
->arrays
[i
]);
2062 heap_free(ctx
->arrays
);
2065 heap_pool_free(&ctx
->heap
);
2066 heap_free(ctx
->args
);
2067 heap_free(ctx
->vars
);
2068 heap_free(ctx
->stack
);
2071 HRESULT
exec_script(script_ctx_t
*ctx
, function_t
*func
, vbdisp_t
*vbthis
, DISPPARAMS
*dp
, VARIANT
*res
)
2073 exec_ctx_t exec
= {func
->code_ctx
};
2075 HRESULT hres
= S_OK
;
2077 exec
.code
= func
->code_ctx
;
2079 if(dp
? func
->arg_cnt
!= arg_cnt(dp
) : func
->arg_cnt
) {
2080 FIXME("wrong arg_cnt %d, expected %d\n", dp
? arg_cnt(dp
) : 0, func
->arg_cnt
);
2084 heap_pool_init(&exec
.heap
);
2090 exec
.args
= heap_alloc_zero(func
->arg_cnt
* sizeof(VARIANT
));
2092 release_exec(&exec
);
2093 return E_OUTOFMEMORY
;
2096 for(i
=0; i
< func
->arg_cnt
; i
++) {
2098 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
)) {
2099 if(func
->args
[i
].by_ref
)
2102 hres
= VariantCopyInd(exec
.args
+i
, V_VARIANTREF(v
));
2104 hres
= VariantCopyInd(exec
.args
+i
, v
);
2107 release_exec(&exec
);
2116 exec
.vars
= heap_alloc_zero(func
->var_cnt
* sizeof(VARIANT
));
2118 release_exec(&exec
);
2119 return E_OUTOFMEMORY
;
2125 exec
.stack_size
= 16;
2127 exec
.stack
= heap_alloc(exec
.stack_size
* sizeof(VARIANT
));
2129 release_exec(&exec
);
2130 return E_OUTOFMEMORY
;
2134 exec
.this_obj
= (IDispatch
*)&vbthis
->IDispatchEx_iface
;
2135 exec
.vbthis
= vbthis
;
2136 }else if (ctx
->host_global
) {
2137 exec
.this_obj
= ctx
->host_global
;
2139 exec
.this_obj
= (IDispatch
*)&ctx
->script_obj
->IDispatchEx_iface
;
2141 IDispatch_AddRef(exec
.this_obj
);
2143 exec
.instr
= exec
.code
->instrs
+ func
->code_off
;
2148 op
= exec
.instr
->op
;
2149 hres
= op_funcs
[op
](&exec
);
2151 ctx
->err_number
= hres
= map_hres(hres
);
2153 if(exec
.resume_next
) {
2156 WARN("Failed %08x in resume next mode\n", hres
);
2159 * Unwinding here is simple. We need to find the next OP_catch, which contains
2160 * information about expected stack size and jump offset on error. Generated
2161 * bytecode needs to guarantee, that simple jump and stack adjustment will
2162 * guarantee proper execution continuation.
2164 while((++exec
.instr
)->op
!= OP_catch
);
2166 TRACE("unwind jmp %d stack_off %d\n", exec
.instr
->arg1
.uint
, exec
.instr
->arg2
.uint
);
2168 stack_off
= exec
.instr
->arg2
.uint
;
2169 instr_jmp(&exec
, exec
.instr
->arg1
.uint
);
2171 if(exec
.top
> stack_off
) {
2172 stack_popn(&exec
, exec
.top
-stack_off
);
2173 }else if(exec
.top
< stack_off
) {
2176 V_VT(&v
) = VT_EMPTY
;
2177 while(exec
.top
< stack_off
) {
2178 hres
= stack_push(&exec
, &v
);
2186 WARN("Failed %08x\n", hres
);
2187 stack_popn(&exec
, exec
.top
);
2192 exec
.instr
+= op_move
[op
];
2196 if(func
->type
!= FUNC_FUNCTION
&& func
->type
!= FUNC_PROPGET
&& func
->type
!= FUNC_DEFGET
)
2197 assert(V_VT(&exec
.ret_val
) == VT_EMPTY
);
2199 if(SUCCEEDED(hres
) && res
) {
2200 *res
= exec
.ret_val
;
2201 V_VT(&exec
.ret_val
) = VT_EMPTY
;
2204 release_exec(&exec
);