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
21 static DISPID propput_dispid
= DISPID_PROPERTYPUT
;
35 dynamic_var_t
*dynamic_vars
;
47 typedef HRESULT (*instr_func_t
)(exec_ctx_t
*);
77 static BOOL
lookup_dynamic_vars(dynamic_var_t
*var
, const WCHAR
*name
, ref_t
*ref
)
80 if(!strcmpiW(var
->name
, name
)) {
81 ref
->type
= var
->is_const
? REF_CONST
: REF_VAR
;
92 static HRESULT
lookup_identifier(exec_ctx_t
*ctx
, BSTR name
, vbdisp_invoke_type_t invoke_type
, ref_t
*ref
)
100 static const WCHAR errW
[] = {'e','r','r',0};
102 if(invoke_type
== VBDISP_LET
103 && (ctx
->func
->type
== FUNC_FUNCTION
|| ctx
->func
->type
== FUNC_PROPGET
|| ctx
->func
->type
== FUNC_DEFGET
)
104 && !strcmpiW(name
, ctx
->func
->name
)) {
106 ref
->u
.v
= &ctx
->ret_val
;
110 for(i
=0; i
< ctx
->func
->var_cnt
; i
++) {
111 if(!strcmpiW(ctx
->func
->vars
[i
].name
, name
)) {
113 ref
->u
.v
= ctx
->vars
+i
;
118 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++) {
119 if(!strcmpiW(ctx
->func
->args
[i
].name
, name
)) {
121 ref
->u
.v
= ctx
->args
+i
;
126 if(lookup_dynamic_vars(ctx
->func
->type
== FUNC_GLOBAL
? ctx
->script
->global_vars
: ctx
->dynamic_vars
, name
, ref
))
129 if(ctx
->func
->type
!= FUNC_GLOBAL
) {
131 /* FIXME: Bind such identifier while generating bytecode. */
132 for(i
=0; i
< ctx
->vbthis
->desc
->prop_cnt
; i
++) {
133 if(!strcmpiW(ctx
->vbthis
->desc
->props
[i
].name
, name
)) {
135 ref
->u
.v
= ctx
->vbthis
->props
+i
;
141 hres
= disp_get_id(ctx
->this_obj
, name
, invoke_type
, TRUE
, &id
);
142 if(SUCCEEDED(hres
)) {
143 ref
->type
= REF_DISP
;
144 ref
->u
.d
.disp
= ctx
->this_obj
;
150 if(ctx
->func
->type
!= FUNC_GLOBAL
&& lookup_dynamic_vars(ctx
->script
->global_vars
, name
, ref
))
153 for(func
= ctx
->script
->global_funcs
; func
; func
= func
->next
) {
154 if(!strcmpiW(func
->name
, name
)) {
155 ref
->type
= REF_FUNC
;
161 if(!strcmpiW(name
, errW
)) {
163 ref
->u
.obj
= (IDispatch
*)&ctx
->script
->err_obj
->IDispatchEx_iface
;
167 hres
= vbdisp_get_id(ctx
->script
->global_obj
, name
, invoke_type
, TRUE
, &id
);
168 if(SUCCEEDED(hres
)) {
169 ref
->type
= REF_DISP
;
170 ref
->u
.d
.disp
= (IDispatch
*)&ctx
->script
->global_obj
->IDispatchEx_iface
;
175 LIST_FOR_EACH_ENTRY(item
, &ctx
->script
->named_items
, named_item_t
, entry
) {
176 if((item
->flags
& SCRIPTITEM_ISVISIBLE
) && !strcmpiW(item
->name
, name
)) {
180 hres
= IActiveScriptSite_GetItemInfo(ctx
->script
->site
, name
, SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
182 WARN("GetItemInfo failed: %08x\n", hres
);
186 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
187 IUnknown_Release(unk
);
189 WARN("object does not implement IDispatch\n");
195 ref
->u
.obj
= item
->disp
;
200 LIST_FOR_EACH_ENTRY(item
, &ctx
->script
->named_items
, named_item_t
, entry
) {
201 if((item
->flags
& SCRIPTITEM_GLOBALMEMBERS
)) {
202 hres
= disp_get_id(item
->disp
, name
, invoke_type
, FALSE
, &id
);
203 if(SUCCEEDED(hres
)) {
204 ref
->type
= REF_DISP
;
205 ref
->u
.d
.disp
= item
->disp
;
212 ref
->type
= REF_NONE
;
216 static HRESULT
add_dynamic_var(exec_ctx_t
*ctx
, const WCHAR
*name
,
217 BOOL is_const
, VARIANT
*val
, BOOL own_val
, VARIANT
**out_var
)
219 dynamic_var_t
*new_var
;
225 heap
= ctx
->func
->type
== FUNC_GLOBAL
? &ctx
->script
->heap
: &ctx
->heap
;
227 new_var
= heap_pool_alloc(heap
, sizeof(*new_var
));
229 return E_OUTOFMEMORY
;
231 size
= (strlenW(name
)+1)*sizeof(WCHAR
);
232 str
= heap_pool_alloc(heap
, size
);
234 return E_OUTOFMEMORY
;
235 memcpy(str
, name
, size
);
237 new_var
->is_const
= is_const
;
242 V_VT(&new_var
->v
) = VT_EMPTY
;
243 hres
= VariantCopy(&new_var
->v
, val
);
248 if(ctx
->func
->type
== FUNC_GLOBAL
) {
249 new_var
->next
= ctx
->script
->global_vars
;
250 ctx
->script
->global_vars
= new_var
;
252 new_var
->next
= ctx
->dynamic_vars
;
253 ctx
->dynamic_vars
= new_var
;
257 *out_var
= &new_var
->v
;
262 static inline VARIANT
*stack_pop(exec_ctx_t
*ctx
)
265 return ctx
->stack
+ --ctx
->top
;
268 static inline VARIANT
*stack_top(exec_ctx_t
*ctx
, unsigned n
)
270 assert(ctx
->top
>= n
);
271 return ctx
->stack
+ (ctx
->top
-n
-1);
274 static HRESULT
stack_push(exec_ctx_t
*ctx
, VARIANT
*v
)
276 if(ctx
->stack_size
== ctx
->top
) {
279 new_stack
= heap_realloc(ctx
->stack
, ctx
->stack_size
*2*sizeof(*ctx
->stack
));
282 return E_OUTOFMEMORY
;
285 ctx
->stack
= new_stack
;
286 ctx
->stack_size
*= 2;
289 ctx
->stack
[ctx
->top
++] = *v
;
293 static inline HRESULT
stack_push_null(exec_ctx_t
*ctx
)
297 return stack_push(ctx
, &v
);
300 static void stack_popn(exec_ctx_t
*ctx
, unsigned n
)
303 VariantClear(stack_pop(ctx
));
306 static void stack_pop_deref(exec_ctx_t
*ctx
, variant_val_t
*r
)
311 if(V_VT(v
) == (VT_BYREF
|VT_VARIANT
)) {
313 r
->v
= V_VARIANTREF(v
);
320 static inline void release_val(variant_val_t
*v
)
326 static HRESULT
stack_pop_val(exec_ctx_t
*ctx
, variant_val_t
*r
)
328 stack_pop_deref(ctx
, r
);
330 if(V_VT(r
->v
) == VT_DISPATCH
) {
334 hres
= disp_call(ctx
->script
, V_DISPATCH(r
->v
), DISPID_VALUE
, &dp
, &r
->store
);
336 IDispatch_Release(V_DISPATCH(r
->v
));
347 static HRESULT
stack_assume_val(exec_ctx_t
*ctx
, unsigned n
)
349 VARIANT
*v
= stack_top(ctx
, n
);
352 if(V_VT(v
) == (VT_BYREF
|VT_VARIANT
)) {
353 VARIANT
*ref
= V_VARIANTREF(v
);
356 hres
= VariantCopy(v
, ref
);
361 if(V_VT(v
) == VT_DISPATCH
) {
365 disp
= V_DISPATCH(v
);
367 hres
= disp_call(ctx
->script
, disp
, DISPID_VALUE
, &dp
, v
);
368 IDispatch_Release(disp
);
376 static int stack_pop_bool(exec_ctx_t
*ctx
, BOOL
*b
)
381 hres
= stack_pop_val(ctx
, &val
);
400 FIXME("unsupported for %s\n", debugstr_variant(val
.v
));
407 static HRESULT
stack_pop_disp(exec_ctx_t
*ctx
, IDispatch
**ret
)
409 VARIANT
*v
= stack_pop(ctx
);
411 if(V_VT(v
) == VT_DISPATCH
) {
412 *ret
= V_DISPATCH(v
);
416 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
417 FIXME("not supported type: %s\n", debugstr_variant(v
));
423 if(V_VT(v
) != VT_DISPATCH
) {
424 FIXME("not disp %s\n", debugstr_variant(v
));
429 IDispatch_AddRef(V_DISPATCH(v
));
430 *ret
= V_DISPATCH(v
);
434 static HRESULT
stack_assume_disp(exec_ctx_t
*ctx
, unsigned n
, IDispatch
**disp
)
436 VARIANT
*v
= stack_top(ctx
, n
), *ref
;
438 if(V_VT(v
) != VT_DISPATCH
) {
439 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
440 FIXME("not supported type: %s\n", debugstr_variant(v
));
444 ref
= V_VARIANTREF(v
);
445 if(V_VT(ref
) != VT_DISPATCH
) {
446 FIXME("not disp %s\n", debugstr_variant(ref
));
450 V_VT(v
) = VT_DISPATCH
;
451 V_DISPATCH(v
) = V_DISPATCH(ref
);
453 IDispatch_AddRef(V_DISPATCH(v
));
457 *disp
= V_DISPATCH(v
);
461 static inline void instr_jmp(exec_ctx_t
*ctx
, unsigned addr
)
463 ctx
->instr
= ctx
->code
->instrs
+ addr
;
466 static void vbstack_to_dp(exec_ctx_t
*ctx
, unsigned arg_cnt
, BOOL is_propput
, DISPPARAMS
*dp
)
468 dp
->cNamedArgs
= is_propput
? 1 : 0;
469 dp
->cArgs
= arg_cnt
+ dp
->cNamedArgs
;
470 dp
->rgdispidNamedArgs
= is_propput
? &propput_dispid
: NULL
;
476 assert(ctx
->top
>= arg_cnt
);
478 for(i
=1; i
*2 <= arg_cnt
; i
++) {
479 tmp
= ctx
->stack
[ctx
->top
-i
];
480 ctx
->stack
[ctx
->top
-i
] = ctx
->stack
[ctx
->top
-arg_cnt
+i
-1];
481 ctx
->stack
[ctx
->top
-arg_cnt
+i
-1] = tmp
;
484 dp
->rgvarg
= ctx
->stack
+ ctx
->top
-dp
->cArgs
;
486 dp
->rgvarg
= is_propput
? ctx
->stack
+ctx
->top
-1 : NULL
;
490 static HRESULT
array_access(exec_ctx_t
*ctx
, SAFEARRAY
*array
, DISPPARAMS
*dp
, VARIANT
**ret
)
492 unsigned cell_off
= 0, dim_size
= 1, i
;
493 unsigned argc
= arg_cnt(dp
);
499 FIXME("NULL array\n");
503 if(array
->cDims
!= argc
) {
504 FIXME("argc %d does not match cDims %d\n", dp
->cArgs
, array
->cDims
);
508 for(i
=0; i
< argc
; i
++) {
509 hres
= to_int(get_arg(dp
, i
), &idx
);
513 idx
-= array
->rgsabound
[i
].lLbound
;
514 if(idx
>= array
->rgsabound
[i
].cElements
) {
515 FIXME("out of bound element %d in dim %d of size %d\n", idx
, i
+1, array
->rgsabound
[i
].cElements
);
519 cell_off
+= idx
*dim_size
;
520 dim_size
*= array
->rgsabound
[i
].cElements
;
523 hres
= SafeArrayAccessData(array
, (void**)&data
);
527 *ret
= data
+cell_off
;
529 SafeArrayUnaccessData(array
);
533 static HRESULT
do_icall(exec_ctx_t
*ctx
, VARIANT
*res
)
535 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
536 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
541 hres
= lookup_identifier(ctx
, identifier
, VBDISP_CALLGET
, &ref
);
551 FIXME("REF_VAR no res\n");
555 v
= V_VT(ref
.u
.v
) == (VT_VARIANT
|VT_BYREF
) ? V_VARIANTREF(ref
.u
.v
) : ref
.u
.v
;
558 SAFEARRAY
*array
= NULL
;
561 case VT_ARRAY
|VT_BYREF
|VT_VARIANT
:
562 array
= *V_ARRAYREF(ref
.u
.v
);
564 case VT_ARRAY
|VT_VARIANT
:
565 array
= V_ARRAY(ref
.u
.v
);
568 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
569 hres
= disp_call(ctx
->script
, V_DISPATCH(v
), DISPID_VALUE
, &dp
, res
);
574 FIXME("arguments not implemented\n");
581 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
582 hres
= array_access(ctx
, array
, &dp
, &v
);
587 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
592 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
593 hres
= disp_call(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, &dp
, res
);
598 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
599 hres
= exec_script(ctx
->script
, ref
.u
.f
, NULL
, &dp
, res
);
605 FIXME("arguments on object\n");
610 IDispatch_AddRef(ref
.u
.obj
);
611 V_VT(res
) = VT_DISPATCH
;
612 V_DISPATCH(res
) = ref
.u
.obj
;
616 if(res
&& !ctx
->func
->code_ctx
->option_explicit
&& arg_cnt
== 0) {
619 hres
= add_dynamic_var(ctx
, identifier
, FALSE
, &v
, FALSE
, &new);
622 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
626 FIXME("%s not found\n", debugstr_w(identifier
));
627 return DISP_E_UNKNOWNNAME
;
630 stack_popn(ctx
, arg_cnt
);
634 static HRESULT
interp_icall(exec_ctx_t
*ctx
)
641 hres
= do_icall(ctx
, &v
);
645 return stack_push(ctx
, &v
);
648 static HRESULT
interp_icallv(exec_ctx_t
*ctx
)
651 return do_icall(ctx
, NULL
);
654 static HRESULT
do_mcall(exec_ctx_t
*ctx
, VARIANT
*res
)
656 const BSTR identifier
= ctx
->instr
->arg1
.bstr
;
657 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
663 hres
= stack_pop_disp(ctx
, &obj
);
672 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
674 hres
= disp_get_id(obj
, identifier
, VBDISP_CALLGET
, FALSE
, &id
);
676 hres
= disp_call(ctx
->script
, obj
, id
, &dp
, res
);
677 IDispatch_Release(obj
);
681 stack_popn(ctx
, arg_cnt
);
685 static HRESULT
interp_mcall(exec_ctx_t
*ctx
)
692 hres
= do_mcall(ctx
, &res
);
696 return stack_push(ctx
, &res
);
699 static HRESULT
interp_mcallv(exec_ctx_t
*ctx
)
703 return do_mcall(ctx
, NULL
);
706 static HRESULT
assign_ident(exec_ctx_t
*ctx
, BSTR name
, DISPPARAMS
*dp
)
711 hres
= lookup_identifier(ctx
, name
, VBDISP_LET
, &ref
);
717 VARIANT
*v
= ref
.u
.v
;
719 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
))
725 if(!(V_VT(v
) & VT_ARRAY
)) {
726 FIXME("array assign on type %d\n", V_VT(v
));
731 case VT_ARRAY
|VT_BYREF
|VT_VARIANT
:
732 array
= *V_ARRAYREF(v
);
734 case VT_ARRAY
|VT_VARIANT
:
738 FIXME("Unsupported array type %x\n", V_VT(v
));
743 FIXME("null array\n");
747 hres
= array_access(ctx
, array
, dp
, &v
);
750 }else if(V_VT(v
) == (VT_ARRAY
|VT_BYREF
|VT_VARIANT
)) {
751 FIXME("non-array assign\n");
755 hres
= VariantCopyInd(v
, dp
->rgvarg
);
759 hres
= disp_propput(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, dp
);
762 FIXME("functions not implemented\n");
768 FIXME("REF_CONST\n");
771 if(ctx
->func
->code_ctx
->option_explicit
) {
772 FIXME("throw exception\n");
776 FIXME("arg_cnt %d not supported\n", arg_cnt(dp
));
780 TRACE("creating variable %s\n", debugstr_w(name
));
781 hres
= add_dynamic_var(ctx
, name
, FALSE
, dp
->rgvarg
, FALSE
, NULL
);
788 static HRESULT
interp_assign_ident(exec_ctx_t
*ctx
)
790 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
791 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
795 TRACE("%s\n", debugstr_w(arg
));
797 hres
= stack_assume_val(ctx
, arg_cnt
);
801 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
802 hres
= assign_ident(ctx
, arg
, &dp
);
806 stack_popn(ctx
, arg_cnt
+1);
810 static HRESULT
interp_set_ident(exec_ctx_t
*ctx
)
812 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
813 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
817 TRACE("%s\n", debugstr_w(arg
));
820 FIXME("arguments not supported\n");
824 hres
= stack_assume_disp(ctx
, 0, NULL
);
828 vbstack_to_dp(ctx
, 0, TRUE
, &dp
);
829 hres
= assign_ident(ctx
, ctx
->instr
->arg1
.bstr
, &dp
);
837 static HRESULT
interp_assign_member(exec_ctx_t
*ctx
)
839 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
840 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
846 TRACE("%s\n", debugstr_w(identifier
));
848 hres
= stack_assume_disp(ctx
, arg_cnt
+1, &obj
);
857 hres
= stack_assume_val(ctx
, arg_cnt
);
861 hres
= disp_get_id(obj
, identifier
, VBDISP_LET
, FALSE
, &id
);
862 if(SUCCEEDED(hres
)) {
863 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
864 hres
= disp_propput(ctx
->script
, obj
, id
, &dp
);
869 stack_popn(ctx
, arg_cnt
+2);
873 static HRESULT
interp_set_member(exec_ctx_t
*ctx
)
875 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
876 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
882 TRACE("%s\n", debugstr_w(identifier
));
885 FIXME("arguments not supported\n");
889 hres
= stack_assume_disp(ctx
, 1, &obj
);
898 hres
= stack_assume_disp(ctx
, 0, NULL
);
902 hres
= disp_get_id(obj
, identifier
, VBDISP_SET
, FALSE
, &id
);
903 if(SUCCEEDED(hres
)) {
904 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
905 hres
= disp_propput(ctx
->script
, obj
, id
, &dp
);
914 static HRESULT
interp_const(exec_ctx_t
*ctx
)
916 BSTR arg
= ctx
->instr
->arg1
.bstr
;
921 TRACE("%s\n", debugstr_w(arg
));
923 assert(ctx
->func
->type
== FUNC_GLOBAL
);
925 hres
= lookup_identifier(ctx
, arg
, VBDISP_CALLGET
, &ref
);
929 if(ref
.type
!= REF_NONE
) {
930 FIXME("%s already defined\n", debugstr_w(arg
));
934 hres
= stack_pop_val(ctx
, &val
);
938 return add_dynamic_var(ctx
, arg
, TRUE
, val
.v
, val
.owned
, NULL
);
941 static HRESULT
interp_val(exec_ctx_t
*ctx
)
949 hres
= stack_pop_val(ctx
, &val
);
955 hres
= VariantCopy(&v
, val
.v
);
960 return stack_push(ctx
, val
.owned
? val
.v
: &v
);
963 static HRESULT
interp_pop(exec_ctx_t
*ctx
)
965 const unsigned n
= ctx
->instr
->arg1
.uint
;
973 static HRESULT
interp_new(exec_ctx_t
*ctx
)
975 const WCHAR
*arg
= ctx
->instr
->arg1
.bstr
;
976 class_desc_t
*class_desc
;
981 static const WCHAR regexpW
[] = {'r','e','g','e','x','p',0};
983 TRACE("%s\n", debugstr_w(arg
));
985 if(!strcmpiW(arg
, regexpW
)) {
986 V_VT(&v
) = VT_DISPATCH
;
987 hres
= create_regexp(&V_DISPATCH(&v
));
991 return stack_push(ctx
, &v
);
994 for(class_desc
= ctx
->script
->classes
; class_desc
; class_desc
= class_desc
->next
) {
995 if(!strcmpiW(class_desc
->name
, arg
))
999 FIXME("Class %s not found\n", debugstr_w(arg
));
1003 hres
= create_vbdisp(class_desc
, &obj
);
1007 V_VT(&v
) = VT_DISPATCH
;
1008 V_DISPATCH(&v
) = (IDispatch
*)&obj
->IDispatchEx_iface
;
1009 return stack_push(ctx
, &v
);
1012 static HRESULT
interp_dim(exec_ctx_t
*ctx
)
1014 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
1015 const unsigned array_id
= ctx
->instr
->arg2
.uint
;
1016 const array_desc_t
*array_desc
;
1020 TRACE("%s\n", debugstr_w(ident
));
1022 assert(array_id
< ctx
->func
->array_cnt
);
1024 ctx
->arrays
= heap_alloc_zero(ctx
->func
->array_cnt
* sizeof(SAFEARRAY
*));
1026 return E_OUTOFMEMORY
;
1029 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1031 FIXME("lookup %s failed: %08x\n", debugstr_w(ident
), hres
);
1035 if(ref
.type
!= REF_VAR
) {
1036 FIXME("got ref.type = %d\n", ref
.type
);
1040 if(ctx
->arrays
[array_id
]) {
1041 FIXME("Array already initialized\n");
1045 array_desc
= ctx
->func
->array_descs
+ array_id
;
1046 if(array_desc
->dim_cnt
) {
1047 ctx
->arrays
[array_id
] = SafeArrayCreate(VT_VARIANT
, array_desc
->dim_cnt
, array_desc
->bounds
);
1048 if(!ctx
->arrays
[array_id
])
1049 return E_OUTOFMEMORY
;
1052 V_VT(ref
.u
.v
) = VT_ARRAY
|VT_BYREF
|VT_VARIANT
;
1053 V_ARRAYREF(ref
.u
.v
) = ctx
->arrays
+array_id
;
1057 static HRESULT
interp_step(exec_ctx_t
*ctx
)
1059 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1065 TRACE("%s\n", debugstr_w(ident
));
1067 V_VT(&zero
) = VT_I2
;
1069 hres
= VarCmp(stack_top(ctx
, 0), &zero
, ctx
->script
->lcid
, 0);
1073 gteq_zero
= hres
== VARCMP_GT
|| hres
== VARCMP_EQ
;
1075 hres
= lookup_identifier(ctx
, ident
, VBDISP_ANY
, &ref
);
1079 if(ref
.type
!= REF_VAR
) {
1080 FIXME("%s is not REF_VAR\n", debugstr_w(ident
));
1084 hres
= VarCmp(ref
.u
.v
, stack_top(ctx
, 1), ctx
->script
->lcid
, 0);
1088 if(hres
== VARCMP_EQ
|| hres
== (gteq_zero
? VARCMP_LT
: VARCMP_GT
)) {
1092 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1097 static HRESULT
interp_newenum(exec_ctx_t
*ctx
)
1105 stack_pop_deref(ctx
, &v
);
1106 assert(V_VT(stack_top(ctx
, 0)) == VT_EMPTY
);
1107 r
= stack_top(ctx
, 0);
1110 case VT_DISPATCH
|VT_BYREF
:
1113 DISPPARAMS dp
= {0};
1116 hres
= disp_call(ctx
->script
, V_ISBYREF(v
.v
) ? *V_DISPATCHREF(v
.v
) : V_DISPATCH(v
.v
), DISPID_NEWENUM
, &dp
, &iterv
);
1121 if(V_VT(&iterv
) != VT_UNKNOWN
&& V_VT(&iterv
) != VT_DISPATCH
) {
1122 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv
));
1123 VariantClear(&iterv
);
1127 hres
= IUnknown_QueryInterface(V_UNKNOWN(&iterv
), &IID_IEnumVARIANT
, (void**)&iter
);
1128 IUnknown_Release(V_UNKNOWN(&iterv
));
1130 FIXME("Could not get IEnumVARIANT iface: %08x\n", hres
);
1134 V_VT(r
) = VT_UNKNOWN
;
1135 V_UNKNOWN(r
) = (IUnknown
*)iter
;
1139 FIXME("Unsupported for %s\n", debugstr_variant(v
.v
));
1147 static HRESULT
interp_enumnext(exec_ctx_t
*ctx
)
1149 const unsigned loop_end
= ctx
->instr
->arg1
.uint
;
1150 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1152 DISPPARAMS dp
= {&v
, &propput_dispid
, 1, 1};
1159 if(V_VT(stack_top(ctx
, 0)) == VT_EMPTY
) {
1160 FIXME("uninitialized\n");
1164 assert(V_VT(stack_top(ctx
, 0)) == VT_UNKNOWN
);
1165 iter
= (IEnumVARIANT
*)V_UNKNOWN(stack_top(ctx
, 0));
1167 V_VT(&v
) = VT_EMPTY
;
1168 hres
= IEnumVARIANT_Next(iter
, 1, &v
, NULL
);
1172 do_continue
= hres
== S_OK
;
1173 hres
= assign_ident(ctx
, ident
, &dp
);
1182 instr_jmp(ctx
, loop_end
);
1187 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
1189 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1193 instr_jmp(ctx
, arg
);
1197 static HRESULT
interp_jmp_false(exec_ctx_t
*ctx
)
1199 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1205 hres
= stack_pop_bool(ctx
, &b
);
1212 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1216 static HRESULT
interp_jmp_true(exec_ctx_t
*ctx
)
1218 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1224 hres
= stack_pop_bool(ctx
, &b
);
1229 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1235 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
1243 static HRESULT
interp_stop(exec_ctx_t
*ctx
)
1247 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1251 static HRESULT
interp_me(exec_ctx_t
*ctx
)
1257 IDispatch_AddRef(ctx
->this_obj
);
1258 V_VT(&v
) = VT_DISPATCH
;
1259 V_DISPATCH(&v
) = ctx
->this_obj
;
1260 return stack_push(ctx
, &v
);
1263 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
1265 const VARIANT_BOOL arg
= ctx
->instr
->arg1
.lng
;
1268 TRACE("%s\n", arg
? "true" : "false");
1272 return stack_push(ctx
, &v
);
1275 static HRESULT
interp_errmode(exec_ctx_t
*ctx
)
1277 const int err_mode
= ctx
->instr
->arg1
.uint
;
1279 TRACE("%d\n", err_mode
);
1281 ctx
->resume_next
= err_mode
;
1282 ctx
->script
->err_number
= S_OK
;
1286 static HRESULT
interp_string(exec_ctx_t
*ctx
)
1293 V_BSTR(&v
) = SysAllocString(ctx
->instr
->arg1
.str
);
1295 return E_OUTOFMEMORY
;
1297 return stack_push(ctx
, &v
);
1300 static HRESULT
interp_long(exec_ctx_t
*ctx
)
1302 const LONG arg
= ctx
->instr
->arg1
.lng
;
1309 return stack_push(ctx
, &v
);
1312 static HRESULT
interp_short(exec_ctx_t
*ctx
)
1314 const LONG arg
= ctx
->instr
->arg1
.lng
;
1321 return stack_push(ctx
, &v
);
1324 static HRESULT
interp_double(exec_ctx_t
*ctx
)
1326 const DOUBLE
*arg
= ctx
->instr
->arg1
.dbl
;
1329 TRACE("%lf\n", *arg
);
1333 return stack_push(ctx
, &v
);
1336 static HRESULT
interp_empty(exec_ctx_t
*ctx
)
1342 V_VT(&v
) = VT_EMPTY
;
1343 return stack_push(ctx
, &v
);
1346 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1349 return stack_push_null(ctx
);
1352 static HRESULT
interp_nothing(exec_ctx_t
*ctx
)
1358 V_VT(&v
) = VT_DISPATCH
;
1359 V_DISPATCH(&v
) = NULL
;
1360 return stack_push(ctx
, &v
);
1363 static HRESULT
interp_hres(exec_ctx_t
*ctx
)
1365 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1370 V_VT(&v
) = VT_ERROR
;
1372 return stack_push(ctx
, &v
);
1375 static HRESULT
interp_not(exec_ctx_t
*ctx
)
1383 hres
= stack_pop_val(ctx
, &val
);
1387 hres
= VarNot(val
.v
, &v
);
1392 return stack_push(ctx
, &v
);
1395 static HRESULT
interp_and(exec_ctx_t
*ctx
)
1403 hres
= stack_pop_val(ctx
, &r
);
1407 hres
= stack_pop_val(ctx
, &l
);
1408 if(SUCCEEDED(hres
)) {
1409 hres
= VarAnd(l
.v
, r
.v
, &v
);
1416 return stack_push(ctx
, &v
);
1419 static HRESULT
interp_or(exec_ctx_t
*ctx
)
1427 hres
= stack_pop_val(ctx
, &r
);
1431 hres
= stack_pop_val(ctx
, &l
);
1432 if(SUCCEEDED(hres
)) {
1433 hres
= VarOr(l
.v
, r
.v
, &v
);
1440 return stack_push(ctx
, &v
);
1443 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
1451 hres
= stack_pop_val(ctx
, &r
);
1455 hres
= stack_pop_val(ctx
, &l
);
1456 if(SUCCEEDED(hres
)) {
1457 hres
= VarXor(l
.v
, r
.v
, &v
);
1464 return stack_push(ctx
, &v
);
1467 static HRESULT
interp_eqv(exec_ctx_t
*ctx
)
1475 hres
= stack_pop_val(ctx
, &r
);
1479 hres
= stack_pop_val(ctx
, &l
);
1480 if(SUCCEEDED(hres
)) {
1481 hres
= VarEqv(l
.v
, r
.v
, &v
);
1488 return stack_push(ctx
, &v
);
1491 static HRESULT
interp_imp(exec_ctx_t
*ctx
)
1499 hres
= stack_pop_val(ctx
, &r
);
1503 hres
= stack_pop_val(ctx
, &l
);
1504 if(SUCCEEDED(hres
)) {
1505 hres
= VarImp(l
.v
, r
.v
, &v
);
1512 return stack_push(ctx
, &v
);
1515 static HRESULT
var_cmp(exec_ctx_t
*ctx
, VARIANT
*l
, VARIANT
*r
)
1517 TRACE("%s %s\n", debugstr_variant(l
), debugstr_variant(r
));
1519 /* FIXME: Fix comparing string to number */
1521 return VarCmp(l
, r
, ctx
->script
->lcid
, 0);
1524 static HRESULT
cmp_oper(exec_ctx_t
*ctx
)
1529 hres
= stack_pop_val(ctx
, &r
);
1533 hres
= stack_pop_val(ctx
, &l
);
1534 if(SUCCEEDED(hres
)) {
1535 hres
= var_cmp(ctx
, l
.v
, r
.v
);
1543 static HRESULT
interp_equal(exec_ctx_t
*ctx
)
1550 hres
= cmp_oper(ctx
);
1553 if(hres
== VARCMP_NULL
)
1554 return stack_push_null(ctx
);
1557 V_BOOL(&v
) = hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1558 return stack_push(ctx
, &v
);
1561 static HRESULT
interp_nequal(exec_ctx_t
*ctx
)
1568 hres
= cmp_oper(ctx
);
1571 if(hres
== VARCMP_NULL
)
1572 return stack_push_null(ctx
);
1575 V_BOOL(&v
) = hres
!= VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1576 return stack_push(ctx
, &v
);
1579 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
1586 hres
= cmp_oper(ctx
);
1589 if(hres
== VARCMP_NULL
)
1590 return stack_push_null(ctx
);
1593 V_BOOL(&v
) = hres
== VARCMP_GT
? VARIANT_TRUE
: VARIANT_FALSE
;
1594 return stack_push(ctx
, &v
);
1597 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
1604 hres
= cmp_oper(ctx
);
1607 if(hres
== VARCMP_NULL
)
1608 return stack_push_null(ctx
);
1611 V_BOOL(&v
) = hres
== VARCMP_GT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1612 return stack_push(ctx
, &v
);
1615 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
1622 hres
= cmp_oper(ctx
);
1625 if(hres
== VARCMP_NULL
)
1626 return stack_push_null(ctx
);
1629 V_BOOL(&v
) = hres
== VARCMP_LT
? VARIANT_TRUE
: VARIANT_FALSE
;
1630 return stack_push(ctx
, &v
);
1633 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
1640 hres
= cmp_oper(ctx
);
1643 if(hres
== VARCMP_NULL
)
1644 return stack_push_null(ctx
);
1647 V_BOOL(&v
) = hres
== VARCMP_LT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1648 return stack_push(ctx
, &v
);
1651 static HRESULT
interp_case(exec_ctx_t
*ctx
)
1653 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1659 hres
= stack_pop_val(ctx
, &v
);
1663 hres
= var_cmp(ctx
, stack_top(ctx
, 0), v
.v
);
1668 if(hres
== VARCMP_EQ
) {
1670 instr_jmp(ctx
, arg
);
1678 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, VARIANT_BOOL
*ret
)
1680 IObjectIdentity
*identity
;
1681 IUnknown
*unk1
, *unk2
;
1684 if(disp1
== disp2
) {
1685 *ret
= VARIANT_TRUE
;
1689 if(!disp1
|| !disp2
) {
1690 *ret
= VARIANT_FALSE
;
1694 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
1698 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
1700 IUnknown_Release(unk1
);
1705 *ret
= VARIANT_TRUE
;
1707 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
1708 if(SUCCEEDED(hres
)) {
1709 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
1710 IObjectIdentity_Release(identity
);
1711 *ret
= hres
== S_OK
? VARIANT_TRUE
: VARIANT_FALSE
;
1713 *ret
= VARIANT_FALSE
;
1717 IUnknown_Release(unk1
);
1718 IUnknown_Release(unk2
);
1722 static HRESULT
interp_is(exec_ctx_t
*ctx
)
1730 hres
= stack_pop_disp(ctx
, &r
);
1734 hres
= stack_pop_disp(ctx
, &l
);
1735 if(SUCCEEDED(hres
)) {
1737 hres
= disp_cmp(l
, r
, &V_BOOL(&v
));
1739 IDispatch_Release(l
);
1742 IDispatch_Release(r
);
1746 return stack_push(ctx
, &v
);
1749 static HRESULT
interp_concat(exec_ctx_t
*ctx
)
1757 hres
= stack_pop_val(ctx
, &r
);
1761 hres
= stack_pop_val(ctx
, &l
);
1762 if(SUCCEEDED(hres
)) {
1763 hres
= VarCat(l
.v
, r
.v
, &v
);
1770 return stack_push(ctx
, &v
);
1773 static HRESULT
interp_add(exec_ctx_t
*ctx
)
1781 hres
= stack_pop_val(ctx
, &r
);
1785 hres
= stack_pop_val(ctx
, &l
);
1786 if(SUCCEEDED(hres
)) {
1787 hres
= VarAdd(l
.v
, r
.v
, &v
);
1794 return stack_push(ctx
, &v
);
1797 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
1805 hres
= stack_pop_val(ctx
, &r
);
1809 hres
= stack_pop_val(ctx
, &l
);
1810 if(SUCCEEDED(hres
)) {
1811 hres
= VarSub(l
.v
, r
.v
, &v
);
1818 return stack_push(ctx
, &v
);
1821 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
1829 hres
= stack_pop_val(ctx
, &r
);
1833 hres
= stack_pop_val(ctx
, &l
);
1834 if(SUCCEEDED(hres
)) {
1835 hres
= VarMod(l
.v
, r
.v
, &v
);
1842 return stack_push(ctx
, &v
);
1845 static HRESULT
interp_idiv(exec_ctx_t
*ctx
)
1853 hres
= stack_pop_val(ctx
, &r
);
1857 hres
= stack_pop_val(ctx
, &l
);
1858 if(SUCCEEDED(hres
)) {
1859 hres
= VarIdiv(l
.v
, r
.v
, &v
);
1866 return stack_push(ctx
, &v
);
1869 static HRESULT
interp_div(exec_ctx_t
*ctx
)
1877 hres
= stack_pop_val(ctx
, &r
);
1881 hres
= stack_pop_val(ctx
, &l
);
1882 if(SUCCEEDED(hres
)) {
1883 hres
= VarDiv(l
.v
, r
.v
, &v
);
1890 return stack_push(ctx
, &v
);
1893 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
1901 hres
= stack_pop_val(ctx
, &r
);
1905 hres
= stack_pop_val(ctx
, &l
);
1906 if(SUCCEEDED(hres
)) {
1907 hres
= VarMul(l
.v
, r
.v
, &v
);
1914 return stack_push(ctx
, &v
);
1917 static HRESULT
interp_exp(exec_ctx_t
*ctx
)
1925 hres
= stack_pop_val(ctx
, &r
);
1929 hres
= stack_pop_val(ctx
, &l
);
1930 if(SUCCEEDED(hres
)) {
1931 hres
= VarPow(l
.v
, r
.v
, &v
);
1938 return stack_push(ctx
, &v
);
1941 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
1947 hres
= stack_pop_val(ctx
, &val
);
1951 hres
= VarNeg(val
.v
, &v
);
1956 return stack_push(ctx
, &v
);
1959 static HRESULT
interp_incc(exec_ctx_t
*ctx
)
1961 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
1968 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1972 if(ref
.type
!= REF_VAR
) {
1973 FIXME("ref.type is not REF_VAR\n");
1977 hres
= VarAdd(stack_top(ctx
, 0), ref
.u
.v
, &v
);
1981 VariantClear(ref
.u
.v
);
1986 static HRESULT
interp_catch(exec_ctx_t
*ctx
)
1988 /* Nothing to do here, the OP is for unwinding only. */
1992 static const instr_func_t op_funcs
[] = {
1993 #define X(x,n,a,b) interp_ ## x,
1998 static const unsigned op_move
[] = {
1999 #define X(x,n,a,b) n,
2004 void release_dynamic_vars(dynamic_var_t
*var
)
2007 VariantClear(&var
->v
);
2012 static void release_exec(exec_ctx_t
*ctx
)
2016 VariantClear(&ctx
->ret_val
);
2017 release_dynamic_vars(ctx
->dynamic_vars
);
2020 IDispatch_Release(ctx
->this_obj
);
2023 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++)
2024 VariantClear(ctx
->args
+i
);
2028 for(i
=0; i
< ctx
->func
->var_cnt
; i
++)
2029 VariantClear(ctx
->vars
+i
);
2033 for(i
=0; i
< ctx
->func
->var_cnt
; i
++) {
2035 SafeArrayDestroy(ctx
->arrays
[i
]);
2037 heap_free(ctx
->arrays
);
2040 heap_pool_free(&ctx
->heap
);
2041 heap_free(ctx
->args
);
2042 heap_free(ctx
->vars
);
2043 heap_free(ctx
->stack
);
2046 HRESULT
exec_script(script_ctx_t
*ctx
, function_t
*func
, vbdisp_t
*vbthis
, DISPPARAMS
*dp
, VARIANT
*res
)
2048 exec_ctx_t exec
= {func
->code_ctx
};
2050 HRESULT hres
= S_OK
;
2052 exec
.code
= func
->code_ctx
;
2054 if(dp
? func
->arg_cnt
!= arg_cnt(dp
) : func
->arg_cnt
) {
2055 FIXME("wrong arg_cnt %d, expected %d\n", dp
? arg_cnt(dp
) : 0, func
->arg_cnt
);
2059 heap_pool_init(&exec
.heap
);
2065 exec
.args
= heap_alloc_zero(func
->arg_cnt
* sizeof(VARIANT
));
2067 release_exec(&exec
);
2068 return E_OUTOFMEMORY
;
2071 for(i
=0; i
< func
->arg_cnt
; i
++) {
2073 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
)) {
2074 if(func
->args
[i
].by_ref
)
2077 hres
= VariantCopyInd(exec
.args
+i
, V_VARIANTREF(v
));
2079 hres
= VariantCopyInd(exec
.args
+i
, v
);
2082 release_exec(&exec
);
2091 exec
.vars
= heap_alloc_zero(func
->var_cnt
* sizeof(VARIANT
));
2093 release_exec(&exec
);
2094 return E_OUTOFMEMORY
;
2100 exec
.stack_size
= 16;
2102 exec
.stack
= heap_alloc(exec
.stack_size
* sizeof(VARIANT
));
2104 release_exec(&exec
);
2105 return E_OUTOFMEMORY
;
2109 exec
.this_obj
= (IDispatch
*)&vbthis
->IDispatchEx_iface
;
2110 exec
.vbthis
= vbthis
;
2111 }else if (ctx
->host_global
) {
2112 exec
.this_obj
= ctx
->host_global
;
2114 exec
.this_obj
= (IDispatch
*)&ctx
->script_obj
->IDispatchEx_iface
;
2116 IDispatch_AddRef(exec
.this_obj
);
2118 exec
.instr
= exec
.code
->instrs
+ func
->code_off
;
2123 op
= exec
.instr
->op
;
2124 hres
= op_funcs
[op
](&exec
);
2126 ctx
->err_number
= hres
= map_hres(hres
);
2128 if(exec
.resume_next
) {
2131 WARN("Failed %08x in resume next mode\n", hres
);
2134 * Unwinding here is simple. We need to find the next OP_catch, which contains
2135 * information about expected stack size and jump offset on error. Generated
2136 * bytecode needs to guarantee, that simple jump and stack adjustment will
2137 * guarantee proper execution continuation.
2139 while((++exec
.instr
)->op
!= OP_catch
);
2141 TRACE("unwind jmp %d stack_off %d\n", exec
.instr
->arg1
.uint
, exec
.instr
->arg2
.uint
);
2143 stack_off
= exec
.instr
->arg2
.uint
;
2144 instr_jmp(&exec
, exec
.instr
->arg1
.uint
);
2146 if(exec
.top
> stack_off
) {
2147 stack_popn(&exec
, exec
.top
-stack_off
);
2148 }else if(exec
.top
< stack_off
) {
2151 V_VT(&v
) = VT_EMPTY
;
2152 while(exec
.top
< stack_off
) {
2153 hres
= stack_push(&exec
, &v
);
2161 WARN("Failed %08x\n", hres
);
2162 stack_popn(&exec
, exec
.top
);
2167 exec
.instr
+= op_move
[op
];
2171 if(func
->type
!= FUNC_FUNCTION
&& func
->type
!= FUNC_PROPGET
&& func
->type
!= FUNC_DEFGET
)
2172 assert(V_VT(&exec
.ret_val
) == VT_EMPTY
);
2174 if(SUCCEEDED(hres
) && res
) {
2175 *res
= exec
.ret_val
;
2176 V_VT(&exec
.ret_val
) = VT_EMPTY
;
2179 release_exec(&exec
);