2 * Copyright 2011 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(vbscript
);
27 static DISPID propput_dispid
= DISPID_PROPERTYPUT
;
40 dynamic_var_t
*dynamic_vars
;
52 typedef HRESULT (*instr_func_t
)(exec_ctx_t
*);
82 static BOOL
lookup_dynamic_vars(dynamic_var_t
*var
, const WCHAR
*name
, ref_t
*ref
)
85 if(!wcsicmp(var
->name
, name
)) {
86 ref
->type
= var
->is_const
? REF_CONST
: REF_VAR
;
97 static BOOL
lookup_global_vars(ScriptDisp
*script
, const WCHAR
*name
, ref_t
*ref
)
99 dynamic_var_t
**vars
= script
->global_vars
;
100 size_t i
, cnt
= script
->global_vars_cnt
;
102 for(i
= 0; i
< cnt
; i
++) {
103 if(!wcsicmp(vars
[i
]->name
, name
)) {
104 ref
->type
= vars
[i
]->is_const
? REF_CONST
: REF_VAR
;
105 ref
->u
.v
= &vars
[i
]->v
;
113 static BOOL
lookup_global_funcs(ScriptDisp
*script
, const WCHAR
*name
, ref_t
*ref
)
115 function_t
**funcs
= script
->global_funcs
;
116 size_t i
, cnt
= script
->global_funcs_cnt
;
118 for(i
= 0; i
< cnt
; i
++) {
119 if(!wcsicmp(funcs
[i
]->name
, name
)) {
120 ref
->type
= REF_FUNC
;
129 static HRESULT
lookup_identifier(exec_ctx_t
*ctx
, BSTR name
, vbdisp_invoke_type_t invoke_type
, ref_t
*ref
)
131 ScriptDisp
*script_obj
= ctx
->script
->script_obj
;
137 if((ctx
->func
->type
== FUNC_FUNCTION
|| ctx
->func
->type
== FUNC_PROPGET
)
138 && !wcsicmp(name
, ctx
->func
->name
)) {
140 ref
->u
.v
= &ctx
->ret_val
;
144 if(ctx
->func
->type
!= FUNC_GLOBAL
) {
145 for(i
=0; i
< ctx
->func
->var_cnt
; i
++) {
146 if(!wcsicmp(ctx
->func
->vars
[i
].name
, name
)) {
148 ref
->u
.v
= ctx
->vars
+i
;
153 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++) {
154 if(!wcsicmp(ctx
->func
->args
[i
].name
, name
)) {
156 ref
->u
.v
= ctx
->args
+i
;
161 if(lookup_dynamic_vars(ctx
->dynamic_vars
, name
, ref
))
165 /* FIXME: Bind such identifier while generating bytecode. */
166 for(i
=0; i
< ctx
->vbthis
->desc
->prop_cnt
; i
++) {
167 if(!wcsicmp(ctx
->vbthis
->desc
->props
[i
].name
, name
)) {
169 ref
->u
.v
= ctx
->vbthis
->props
+i
;
174 hres
= vbdisp_get_id(ctx
->vbthis
, name
, invoke_type
, TRUE
, &id
);
175 if(SUCCEEDED(hres
)) {
176 ref
->type
= REF_DISP
;
177 ref
->u
.d
.disp
= (IDispatch
*)&ctx
->vbthis
->IDispatchEx_iface
;
184 if(ctx
->code
->named_item
) {
185 if(lookup_global_vars(ctx
->code
->named_item
->script_obj
, name
, ref
))
187 if(lookup_global_funcs(ctx
->code
->named_item
->script_obj
, name
, ref
))
191 if(ctx
->func
->code_ctx
->named_item
&& ctx
->func
->code_ctx
->named_item
->disp
&&
192 !(ctx
->func
->code_ctx
->named_item
->flags
& SCRIPTITEM_CODEONLY
))
194 hres
= disp_get_id(ctx
->func
->code_ctx
->named_item
->disp
, name
, invoke_type
, TRUE
, &id
);
195 if(SUCCEEDED(hres
)) {
196 ref
->type
= REF_DISP
;
197 ref
->u
.d
.disp
= ctx
->func
->code_ctx
->named_item
->disp
;
203 if(lookup_global_vars(script_obj
, name
, ref
))
205 if(lookup_global_funcs(script_obj
, name
, ref
))
208 hres
= get_builtin_id(ctx
->script
->global_obj
, name
, &id
);
209 if(SUCCEEDED(hres
)) {
210 ref
->type
= REF_DISP
;
211 ref
->u
.d
.disp
= &ctx
->script
->global_obj
->IDispatch_iface
;
216 item
= lookup_named_item(ctx
->script
, name
, SCRIPTITEM_ISVISIBLE
);
217 if(item
&& item
->disp
) {
219 ref
->u
.obj
= item
->disp
;
223 LIST_FOR_EACH_ENTRY(item
, &ctx
->script
->named_items
, named_item_t
, entry
) {
224 if((item
->flags
& SCRIPTITEM_GLOBALMEMBERS
)) {
225 hres
= disp_get_id(item
->disp
, name
, invoke_type
, FALSE
, &id
);
226 if(SUCCEEDED(hres
)) {
227 ref
->type
= REF_DISP
;
228 ref
->u
.d
.disp
= item
->disp
;
235 ref
->type
= REF_NONE
;
239 static HRESULT
add_dynamic_var(exec_ctx_t
*ctx
, const WCHAR
*name
,
240 BOOL is_const
, VARIANT
**out_var
)
242 ScriptDisp
*script_obj
= ctx
->code
->named_item
? ctx
->code
->named_item
->script_obj
: ctx
->script
->script_obj
;
243 dynamic_var_t
*new_var
;
248 heap
= ctx
->func
->type
== FUNC_GLOBAL
? &script_obj
->heap
: &ctx
->heap
;
250 new_var
= heap_pool_alloc(heap
, sizeof(*new_var
));
252 return E_OUTOFMEMORY
;
254 size
= (lstrlenW(name
)+1)*sizeof(WCHAR
);
255 str
= heap_pool_alloc(heap
, size
);
257 return E_OUTOFMEMORY
;
258 memcpy(str
, name
, size
);
260 new_var
->is_const
= is_const
;
261 new_var
->array
= NULL
;
262 V_VT(&new_var
->v
) = VT_EMPTY
;
264 if(ctx
->func
->type
== FUNC_GLOBAL
) {
265 size_t cnt
= script_obj
->global_vars_cnt
+ 1;
266 if(cnt
> script_obj
->global_vars_size
) {
267 dynamic_var_t
**new_vars
;
268 if(script_obj
->global_vars
)
269 new_vars
= heap_realloc(script_obj
->global_vars
, cnt
* 2 * sizeof(*new_vars
));
271 new_vars
= heap_alloc(cnt
* 2 * sizeof(*new_vars
));
273 return E_OUTOFMEMORY
;
274 script_obj
->global_vars
= new_vars
;
275 script_obj
->global_vars_size
= cnt
* 2;
277 script_obj
->global_vars
[script_obj
->global_vars_cnt
++] = new_var
;
279 new_var
->next
= ctx
->dynamic_vars
;
280 ctx
->dynamic_vars
= new_var
;
283 *out_var
= &new_var
->v
;
287 void clear_ei(EXCEPINFO
*ei
)
289 SysFreeString(ei
->bstrSource
);
290 SysFreeString(ei
->bstrDescription
);
291 SysFreeString(ei
->bstrHelpFile
);
292 memset(ei
, 0, sizeof(*ei
));
295 static void clear_error_loc(script_ctx_t
*ctx
)
297 if(ctx
->error_loc_code
) {
298 release_vbscode(ctx
->error_loc_code
);
299 ctx
->error_loc_code
= NULL
;
303 static inline VARIANT
*stack_pop(exec_ctx_t
*ctx
)
306 return ctx
->stack
+ --ctx
->top
;
309 static inline VARIANT
*stack_top(exec_ctx_t
*ctx
, unsigned n
)
311 assert(ctx
->top
>= n
);
312 return ctx
->stack
+ (ctx
->top
-n
-1);
315 static HRESULT
stack_push(exec_ctx_t
*ctx
, VARIANT
*v
)
317 if(ctx
->stack_size
== ctx
->top
) {
320 new_stack
= heap_realloc(ctx
->stack
, ctx
->stack_size
*2*sizeof(*ctx
->stack
));
323 return E_OUTOFMEMORY
;
326 ctx
->stack
= new_stack
;
327 ctx
->stack_size
*= 2;
330 ctx
->stack
[ctx
->top
++] = *v
;
334 static inline HRESULT
stack_push_null(exec_ctx_t
*ctx
)
338 return stack_push(ctx
, &v
);
341 static void stack_popn(exec_ctx_t
*ctx
, unsigned n
)
344 VariantClear(stack_pop(ctx
));
347 static void stack_pop_deref(exec_ctx_t
*ctx
, variant_val_t
*r
)
352 if(V_VT(v
) == (VT_BYREF
|VT_VARIANT
)) {
354 r
->v
= V_VARIANTREF(v
);
361 static inline void release_val(variant_val_t
*v
)
367 static HRESULT
stack_pop_val(exec_ctx_t
*ctx
, variant_val_t
*r
)
369 stack_pop_deref(ctx
, r
);
371 if(V_VT(r
->v
) == VT_DISPATCH
) {
374 hres
= get_disp_value(ctx
->script
, V_DISPATCH(r
->v
), &r
->store
);
375 if(r
->owned
&& V_DISPATCH(r
->v
))
376 IDispatch_Release(V_DISPATCH(r
->v
));
387 static HRESULT
stack_assume_val(exec_ctx_t
*ctx
, unsigned n
)
389 VARIANT
*v
= stack_top(ctx
, n
);
392 if(V_VT(v
) == (VT_BYREF
|VT_VARIANT
)) {
393 VARIANT
*ref
= V_VARIANTREF(v
);
396 hres
= VariantCopy(v
, ref
);
401 if(V_VT(v
) == VT_DISPATCH
) {
404 disp
= V_DISPATCH(v
);
405 hres
= get_disp_value(ctx
->script
, disp
, v
);
407 IDispatch_Release(disp
);
415 static int stack_pop_bool(exec_ctx_t
*ctx
, BOOL
*b
)
420 hres
= stack_pop_val(ctx
, &val
);
440 FIXME("unsupported for %s\n", debugstr_variant(val
.v
));
447 static HRESULT
stack_pop_disp(exec_ctx_t
*ctx
, IDispatch
**ret
)
449 VARIANT
*v
= stack_pop(ctx
);
451 if(V_VT(v
) == VT_DISPATCH
) {
452 *ret
= V_DISPATCH(v
);
456 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
457 FIXME("not supported type: %s\n", debugstr_variant(v
));
463 if(V_VT(v
) != VT_DISPATCH
) {
464 FIXME("not disp %s\n", debugstr_variant(v
));
469 IDispatch_AddRef(V_DISPATCH(v
));
470 *ret
= V_DISPATCH(v
);
474 static HRESULT
stack_assume_disp(exec_ctx_t
*ctx
, unsigned n
, IDispatch
**disp
)
476 VARIANT
*v
= stack_top(ctx
, n
), *ref
;
478 if(V_VT(v
) != VT_DISPATCH
&& (disp
|| V_VT(v
) != VT_UNKNOWN
)) {
479 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
480 FIXME("not supported type: %s\n", debugstr_variant(v
));
484 ref
= V_VARIANTREF(v
);
485 if(V_VT(ref
) != VT_DISPATCH
&& (disp
|| V_VT(ref
) != VT_UNKNOWN
)) {
486 FIXME("not disp %s\n", debugstr_variant(ref
));
491 V_UNKNOWN(v
) = V_UNKNOWN(ref
);
493 IUnknown_AddRef(V_UNKNOWN(v
));
497 *disp
= V_DISPATCH(v
);
501 static inline void instr_jmp(exec_ctx_t
*ctx
, unsigned addr
)
503 ctx
->instr
= ctx
->code
->instrs
+ addr
;
506 static void vbstack_to_dp(exec_ctx_t
*ctx
, unsigned arg_cnt
, BOOL is_propput
, DISPPARAMS
*dp
)
508 dp
->cNamedArgs
= is_propput
? 1 : 0;
509 dp
->cArgs
= arg_cnt
+ dp
->cNamedArgs
;
510 dp
->rgdispidNamedArgs
= is_propput
? &propput_dispid
: NULL
;
516 assert(ctx
->top
>= arg_cnt
);
518 for(i
=1; i
*2 <= arg_cnt
; i
++) {
519 tmp
= ctx
->stack
[ctx
->top
-i
];
520 ctx
->stack
[ctx
->top
-i
] = ctx
->stack
[ctx
->top
-arg_cnt
+i
-1];
521 ctx
->stack
[ctx
->top
-arg_cnt
+i
-1] = tmp
;
524 dp
->rgvarg
= ctx
->stack
+ ctx
->top
-dp
->cArgs
;
526 dp
->rgvarg
= is_propput
? ctx
->stack
+ctx
->top
-1 : NULL
;
530 static HRESULT
array_access(exec_ctx_t
*ctx
, SAFEARRAY
*array
, DISPPARAMS
*dp
, VARIANT
**ret
)
532 unsigned i
, argc
= arg_cnt(dp
);
537 FIXME("NULL array\n");
541 hres
= SafeArrayLock(array
);
545 if(array
->cDims
!= argc
) {
546 FIXME("argc %d does not match cDims %d\n", dp
->cArgs
, array
->cDims
);
547 SafeArrayUnlock(array
);
551 indices
= heap_alloc(sizeof(*indices
) * argc
);
553 SafeArrayUnlock(array
);
554 return E_OUTOFMEMORY
;
557 for(i
=0; i
<argc
; i
++) {
558 hres
= to_int(get_arg(dp
, i
), indices
+i
);
561 SafeArrayUnlock(array
);
566 hres
= SafeArrayPtrOfIndex(array
, indices
, (void**)ret
);
567 SafeArrayUnlock(array
);
572 static HRESULT
variant_call(exec_ctx_t
*ctx
, VARIANT
*v
, unsigned arg_cnt
, VARIANT
*res
)
574 SAFEARRAY
*array
= NULL
;
578 TRACE("%s\n", debugstr_variant(v
));
580 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
))
584 case VT_ARRAY
|VT_BYREF
|VT_VARIANT
:
585 array
= *V_ARRAYREF(v
);
587 case VT_ARRAY
|VT_VARIANT
:
591 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
592 hres
= disp_call(ctx
->script
, V_DISPATCH(v
), DISPID_VALUE
, &dp
, res
);
595 FIXME("unsupported on %s\n", debugstr_variant(v
));
605 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
606 hres
= array_access(ctx
, array
, &dp
, &v
);
610 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
614 stack_popn(ctx
, arg_cnt
);
618 static HRESULT
do_icall(exec_ctx_t
*ctx
, VARIANT
*res
)
620 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
621 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
626 TRACE("%s %u\n", debugstr_w(identifier
), arg_cnt
);
628 hres
= lookup_identifier(ctx
, identifier
, VBDISP_CALLGET
, &ref
);
636 return variant_call(ctx
, ref
.u
.v
, arg_cnt
, res
);
639 FIXME("REF_VAR no res\n");
643 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
644 V_BYREF(res
) = V_VT(ref
.u
.v
) == (VT_VARIANT
|VT_BYREF
) ? V_VARIANTREF(ref
.u
.v
) : ref
.u
.v
;
647 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
648 hres
= disp_call(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, &dp
, res
);
653 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
654 hres
= exec_script(ctx
->script
, FALSE
, ref
.u
.f
, NULL
, &dp
, res
);
660 FIXME("arguments on object\n");
665 IDispatch_AddRef(ref
.u
.obj
);
666 V_VT(res
) = VT_DISPATCH
;
667 V_DISPATCH(res
) = ref
.u
.obj
;
671 if(res
&& !ctx
->func
->code_ctx
->option_explicit
&& arg_cnt
== 0) {
673 hres
= add_dynamic_var(ctx
, identifier
, FALSE
, &new);
676 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
680 FIXME("%s not found\n", debugstr_w(identifier
));
681 return DISP_E_UNKNOWNNAME
;
684 stack_popn(ctx
, arg_cnt
);
688 static HRESULT
interp_icall(exec_ctx_t
*ctx
)
695 hres
= do_icall(ctx
, &v
);
699 return stack_push(ctx
, &v
);
702 static HRESULT
interp_icallv(exec_ctx_t
*ctx
)
705 return do_icall(ctx
, NULL
);
708 static HRESULT
interp_vcall(exec_ctx_t
*ctx
)
710 const unsigned arg_cnt
= ctx
->instr
->arg1
.uint
;
717 hres
= variant_call(ctx
, v
, arg_cnt
, &res
);
722 return stack_push(ctx
, &res
);
725 static HRESULT
interp_vcallv(exec_ctx_t
*ctx
)
727 const unsigned arg_cnt
= ctx
->instr
->arg1
.uint
;
734 hres
= variant_call(ctx
, v
, arg_cnt
, NULL
);
739 static HRESULT
do_mcall(exec_ctx_t
*ctx
, VARIANT
*res
)
741 const BSTR identifier
= ctx
->instr
->arg1
.bstr
;
742 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
748 hres
= stack_pop_disp(ctx
, &obj
);
757 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
759 hres
= disp_get_id(obj
, identifier
, VBDISP_CALLGET
, FALSE
, &id
);
761 hres
= disp_call(ctx
->script
, obj
, id
, &dp
, res
);
762 IDispatch_Release(obj
);
766 stack_popn(ctx
, arg_cnt
);
770 static HRESULT
interp_mcall(exec_ctx_t
*ctx
)
777 hres
= do_mcall(ctx
, &res
);
781 return stack_push(ctx
, &res
);
784 static HRESULT
interp_mcallv(exec_ctx_t
*ctx
)
788 return do_mcall(ctx
, NULL
);
791 static HRESULT
assign_value(exec_ctx_t
*ctx
, VARIANT
*dst
, VARIANT
*src
, WORD flags
)
796 V_VT(&value
) = VT_EMPTY
;
797 hres
= VariantCopyInd(&value
, src
);
801 if(V_VT(&value
) == VT_DISPATCH
&& !(flags
& DISPATCH_PROPERTYPUTREF
)) {
802 IDispatch
*disp
= V_DISPATCH(&value
);
804 V_VT(&value
) = VT_EMPTY
;
805 hres
= get_disp_value(ctx
->script
, disp
, &value
);
807 IDispatch_Release(disp
);
817 static HRESULT
assign_ident(exec_ctx_t
*ctx
, BSTR name
, WORD flags
, DISPPARAMS
*dp
)
822 hres
= lookup_identifier(ctx
, name
, VBDISP_LET
, &ref
);
828 VARIANT
*v
= ref
.u
.v
;
830 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
))
836 if(V_VT(v
) == VT_DISPATCH
) {
837 hres
= disp_propput(ctx
->script
, V_DISPATCH(v
), DISPID_VALUE
, flags
, dp
);
841 if(!(V_VT(v
) & VT_ARRAY
)) {
842 FIXME("array assign on type %d\n", V_VT(v
));
847 case VT_ARRAY
|VT_BYREF
|VT_VARIANT
:
848 array
= *V_ARRAYREF(v
);
850 case VT_ARRAY
|VT_VARIANT
:
854 FIXME("Unsupported array type %x\n", V_VT(v
));
859 FIXME("null array\n");
863 hres
= array_access(ctx
, array
, dp
, &v
);
866 }else if(V_VT(v
) == (VT_ARRAY
|VT_BYREF
|VT_VARIANT
)) {
867 FIXME("non-array assign\n");
871 hres
= assign_value(ctx
, v
, dp
->rgvarg
, flags
);
875 hres
= disp_propput(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, flags
, dp
);
878 FIXME("functions not implemented\n");
884 FIXME("REF_CONST\n");
887 if(ctx
->func
->code_ctx
->option_explicit
) {
888 FIXME("throw exception\n");
894 FIXME("arg_cnt %d not supported\n", arg_cnt(dp
));
898 TRACE("creating variable %s\n", debugstr_w(name
));
899 hres
= add_dynamic_var(ctx
, name
, FALSE
, &new_var
);
901 hres
= assign_value(ctx
, new_var
, dp
->rgvarg
, flags
);
908 static HRESULT
interp_assign_ident(exec_ctx_t
*ctx
)
910 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
911 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
915 TRACE("%s\n", debugstr_w(arg
));
917 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
918 hres
= assign_ident(ctx
, arg
, DISPATCH_PROPERTYPUT
, &dp
);
922 stack_popn(ctx
, arg_cnt
+1);
926 static HRESULT
interp_set_ident(exec_ctx_t
*ctx
)
928 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
929 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
933 TRACE("%s %u\n", debugstr_w(arg
), arg_cnt
);
935 hres
= stack_assume_disp(ctx
, arg_cnt
, NULL
);
939 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
940 hres
= assign_ident(ctx
, arg
, DISPATCH_PROPERTYPUTREF
, &dp
);
944 stack_popn(ctx
, arg_cnt
+ 1);
948 static HRESULT
interp_assign_member(exec_ctx_t
*ctx
)
950 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
951 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
957 TRACE("%s\n", debugstr_w(identifier
));
959 hres
= stack_assume_disp(ctx
, arg_cnt
+1, &obj
);
968 hres
= disp_get_id(obj
, identifier
, VBDISP_LET
, FALSE
, &id
);
969 if(SUCCEEDED(hres
)) {
970 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
971 hres
= disp_propput(ctx
->script
, obj
, id
, DISPATCH_PROPERTYPUT
, &dp
);
976 stack_popn(ctx
, arg_cnt
+2);
980 static HRESULT
interp_set_member(exec_ctx_t
*ctx
)
982 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
983 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
989 TRACE("%s\n", debugstr_w(identifier
));
991 hres
= stack_assume_disp(ctx
, arg_cnt
+1, &obj
);
1000 hres
= stack_assume_disp(ctx
, arg_cnt
, NULL
);
1004 hres
= disp_get_id(obj
, identifier
, VBDISP_SET
, FALSE
, &id
);
1005 if(SUCCEEDED(hres
)) {
1006 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
1007 hres
= disp_propput(ctx
->script
, obj
, id
, DISPATCH_PROPERTYPUTREF
, &dp
);
1012 stack_popn(ctx
, arg_cnt
+2);
1016 static HRESULT
interp_const(exec_ctx_t
*ctx
)
1018 BSTR arg
= ctx
->instr
->arg1
.bstr
;
1023 TRACE("%s\n", debugstr_w(arg
));
1025 assert(ctx
->func
->type
== FUNC_GLOBAL
);
1027 hres
= lookup_identifier(ctx
, arg
, VBDISP_CALLGET
, &ref
);
1031 if(ref
.type
!= REF_NONE
) {
1032 FIXME("%s already defined\n", debugstr_w(arg
));
1036 hres
= stack_assume_val(ctx
, 0);
1040 hres
= add_dynamic_var(ctx
, arg
, TRUE
, &v
);
1044 *v
= *stack_pop(ctx
);
1048 static HRESULT
interp_val(exec_ctx_t
*ctx
)
1056 hres
= stack_pop_val(ctx
, &val
);
1061 V_VT(&v
) = VT_EMPTY
;
1062 hres
= VariantCopy(&v
, val
.v
);
1067 return stack_push(ctx
, val
.owned
? val
.v
: &v
);
1070 static HRESULT
interp_pop(exec_ctx_t
*ctx
)
1072 const unsigned n
= ctx
->instr
->arg1
.uint
;
1080 static HRESULT
interp_stack(exec_ctx_t
*ctx
)
1082 const unsigned n
= ctx
->instr
->arg1
.uint
;
1089 return MAKE_VBSERROR(505);
1090 assert(n
< ctx
->top
);
1092 V_VT(&v
) = VT_EMPTY
;
1093 hres
= VariantCopy(&v
, ctx
->stack
+ n
);
1097 return stack_push(ctx
, &v
);
1100 static HRESULT
interp_deref(exec_ctx_t
*ctx
)
1102 VARIANT copy
, *v
= stack_top(ctx
, 0);
1105 TRACE("%s\n", debugstr_variant(v
));
1107 if(V_VT(v
) != (VT_BYREF
|VT_VARIANT
))
1110 V_VT(©
) = VT_EMPTY
;
1111 hres
= VariantCopy(©
, V_VARIANTREF(v
));
1117 static HRESULT
interp_new(exec_ctx_t
*ctx
)
1119 const WCHAR
*arg
= ctx
->instr
->arg1
.bstr
;
1120 class_desc_t
*class_desc
= NULL
;
1125 TRACE("%s\n", debugstr_w(arg
));
1127 if(!wcsicmp(arg
, L
"regexp")) {
1128 V_VT(&v
) = VT_DISPATCH
;
1129 hres
= create_regexp(&V_DISPATCH(&v
));
1133 return stack_push(ctx
, &v
);
1136 if(ctx
->code
->named_item
)
1137 for(class_desc
= ctx
->code
->named_item
->script_obj
->classes
; class_desc
; class_desc
= class_desc
->next
)
1138 if(!wcsicmp(class_desc
->name
, arg
))
1141 for(class_desc
= ctx
->script
->script_obj
->classes
; class_desc
; class_desc
= class_desc
->next
)
1142 if(!wcsicmp(class_desc
->name
, arg
))
1145 FIXME("Class %s not found\n", debugstr_w(arg
));
1149 hres
= create_vbdisp(class_desc
, &obj
);
1153 V_VT(&v
) = VT_DISPATCH
;
1154 V_DISPATCH(&v
) = (IDispatch
*)&obj
->IDispatchEx_iface
;
1155 return stack_push(ctx
, &v
);
1158 static HRESULT
interp_dim(exec_ctx_t
*ctx
)
1160 ScriptDisp
*script_obj
= ctx
->code
->named_item
? ctx
->code
->named_item
->script_obj
: ctx
->script
->script_obj
;
1161 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
1162 const unsigned array_id
= ctx
->instr
->arg2
.uint
;
1163 const array_desc_t
*array_desc
;
1164 SAFEARRAY
**array_ref
;
1168 TRACE("%s\n", debugstr_w(ident
));
1170 assert(array_id
< ctx
->func
->array_cnt
);
1172 if(ctx
->func
->type
== FUNC_GLOBAL
) {
1174 for(i
= 0; i
< script_obj
->global_vars_cnt
; i
++) {
1175 if(!wcsicmp(script_obj
->global_vars
[i
]->name
, ident
))
1178 assert(i
< script_obj
->global_vars_cnt
);
1179 v
= &script_obj
->global_vars
[i
]->v
;
1180 array_ref
= &script_obj
->global_vars
[i
]->array
;
1185 ctx
->arrays
= heap_alloc_zero(ctx
->func
->array_cnt
* sizeof(SAFEARRAY
*));
1187 return E_OUTOFMEMORY
;
1190 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1192 FIXME("lookup %s failed: %08x\n", debugstr_w(ident
), hres
);
1196 if(ref
.type
!= REF_VAR
) {
1197 FIXME("got ref.type = %d\n", ref
.type
);
1202 array_ref
= ctx
->arrays
+ array_id
;
1206 FIXME("Array already initialized\n");
1210 array_desc
= ctx
->func
->array_descs
+ array_id
;
1211 if(array_desc
->dim_cnt
) {
1212 *array_ref
= SafeArrayCreate(VT_VARIANT
, array_desc
->dim_cnt
, array_desc
->bounds
);
1214 return E_OUTOFMEMORY
;
1217 V_VT(v
) = VT_ARRAY
|VT_BYREF
|VT_VARIANT
;
1218 V_ARRAYREF(v
) = array_ref
;
1222 static HRESULT
array_bounds_from_stack(exec_ctx_t
*ctx
, unsigned dim_cnt
, SAFEARRAYBOUND
**ret
)
1224 SAFEARRAYBOUND
*bounds
;
1229 if(!(bounds
= heap_alloc(dim_cnt
* sizeof(*bounds
))))
1230 return E_OUTOFMEMORY
;
1232 for(i
= 0; i
< dim_cnt
; i
++) {
1233 hres
= to_int(stack_top(ctx
, dim_cnt
- i
- 1), &dim
);
1239 bounds
[i
].cElements
= dim
+ 1;
1240 bounds
[i
].lLbound
= 0;
1243 stack_popn(ctx
, dim_cnt
);
1248 static HRESULT
interp_redim(exec_ctx_t
*ctx
)
1250 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
1251 const unsigned dim_cnt
= ctx
->instr
->arg2
.uint
;
1252 SAFEARRAYBOUND
*bounds
;
1257 TRACE("%s %u\n", debugstr_w(identifier
), dim_cnt
);
1259 hres
= lookup_identifier(ctx
, identifier
, VBDISP_LET
, &ref
);
1261 FIXME("lookup %s failed: %08x\n", debugstr_w(identifier
), hres
);
1265 if(ref
.type
!= REF_VAR
) {
1266 FIXME("got ref.type = %d\n", ref
.type
);
1270 hres
= array_bounds_from_stack(ctx
, dim_cnt
, &bounds
);
1274 array
= SafeArrayCreate(VT_VARIANT
, dim_cnt
, bounds
);
1277 return E_OUTOFMEMORY
;
1279 /* FIXME: We should check if we're not modifying an existing static array here */
1281 VariantClear(ref
.u
.v
);
1282 V_VT(ref
.u
.v
) = VT_ARRAY
|VT_VARIANT
;
1283 V_ARRAY(ref
.u
.v
) = array
;
1287 static HRESULT
interp_redim_preserve(exec_ctx_t
*ctx
)
1289 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
1290 const unsigned dim_cnt
= ctx
->instr
->arg2
.uint
;
1292 SAFEARRAYBOUND
*bounds
;
1297 TRACE("%s %u\n", debugstr_w(identifier
), dim_cnt
);
1299 hres
= lookup_identifier(ctx
, identifier
, VBDISP_LET
, &ref
);
1301 FIXME("lookup %s failed: %08x\n", debugstr_w(identifier
), hres
);
1305 if(ref
.type
!= REF_VAR
) {
1306 FIXME("got ref.type = %d\n", ref
.type
);
1310 if(!(V_VT(ref
.u
.v
) & VT_ARRAY
)) {
1311 FIXME("ReDim Preserve not valid on type %d\n", V_VT(ref
.u
.v
));
1315 array
= V_ARRAY(ref
.u
.v
);
1317 hres
= array_bounds_from_stack(ctx
, dim_cnt
, &bounds
);
1321 if(array
== NULL
|| array
->cDims
== 0) {
1322 /* can initially allocate the array */
1323 array
= SafeArrayCreate(VT_VARIANT
, dim_cnt
, bounds
);
1324 VariantClear(ref
.u
.v
);
1325 V_VT(ref
.u
.v
) = VT_ARRAY
|VT_VARIANT
;
1326 V_ARRAY(ref
.u
.v
) = array
;
1328 } else if(array
->cDims
!= dim_cnt
) {
1329 /* can't otherwise change the number of dimensions */
1330 TRACE("Can't resize %s, cDims %d != %d\n", debugstr_w(identifier
), array
->cDims
, dim_cnt
);
1331 return MAKE_VBSERROR(VBSE_OUT_OF_BOUNDS
);
1333 /* can resize the last dimensions (if others match */
1334 for(i
= 0; i
+1 < dim_cnt
; ++i
) {
1335 if(array
->rgsabound
[array
->cDims
- 1 - i
].cElements
!= bounds
[i
].cElements
) {
1336 TRACE("Can't resize %s, bound[%d] %d != %d\n", debugstr_w(identifier
), i
, array
->rgsabound
[i
].cElements
, bounds
[i
].cElements
);
1337 return MAKE_VBSERROR(VBSE_OUT_OF_BOUNDS
);
1340 return SafeArrayRedim(array
, &bounds
[dim_cnt
-1]);
1344 static HRESULT
interp_step(exec_ctx_t
*ctx
)
1346 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1352 TRACE("%s\n", debugstr_w(ident
));
1354 V_VT(&zero
) = VT_I2
;
1356 hres
= VarCmp(stack_top(ctx
, 0), &zero
, ctx
->script
->lcid
, 0);
1360 gteq_zero
= hres
== VARCMP_GT
|| hres
== VARCMP_EQ
;
1362 hres
= lookup_identifier(ctx
, ident
, VBDISP_ANY
, &ref
);
1366 if(ref
.type
!= REF_VAR
) {
1367 FIXME("%s is not REF_VAR\n", debugstr_w(ident
));
1371 hres
= VarCmp(ref
.u
.v
, stack_top(ctx
, 1), ctx
->script
->lcid
, 0);
1375 if(hres
== VARCMP_EQ
|| hres
== (gteq_zero
? VARCMP_LT
: VARCMP_GT
)) {
1379 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1384 static HRESULT
interp_newenum(exec_ctx_t
*ctx
)
1392 stack_pop_deref(ctx
, &v
);
1393 assert(V_VT(stack_top(ctx
, 0)) == VT_EMPTY
);
1394 r
= stack_top(ctx
, 0);
1397 case VT_DISPATCH
|VT_BYREF
:
1400 DISPPARAMS dp
= {0};
1403 hres
= disp_call(ctx
->script
, V_ISBYREF(v
.v
) ? *V_DISPATCHREF(v
.v
) : V_DISPATCH(v
.v
), DISPID_NEWENUM
, &dp
, &iterv
);
1408 if(V_VT(&iterv
) != VT_UNKNOWN
&& V_VT(&iterv
) != VT_DISPATCH
) {
1409 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv
));
1410 VariantClear(&iterv
);
1414 hres
= IUnknown_QueryInterface(V_UNKNOWN(&iterv
), &IID_IEnumVARIANT
, (void**)&iter
);
1415 IUnknown_Release(V_UNKNOWN(&iterv
));
1417 FIXME("Could not get IEnumVARIANT iface: %08x\n", hres
);
1421 V_VT(r
) = VT_UNKNOWN
;
1422 V_UNKNOWN(r
) = (IUnknown
*)iter
;
1425 case VT_VARIANT
|VT_ARRAY
:
1426 case VT_VARIANT
|VT_ARRAY
|VT_BYREF
: {
1429 hres
= create_safearray_iter(V_ISBYREF(v
.v
) ? *V_ARRAYREF(v
.v
) : V_ARRAY(v
.v
), &iter
);
1433 V_VT(r
) = VT_UNKNOWN
;
1434 V_UNKNOWN(r
) = (IUnknown
*)iter
;
1438 FIXME("Unsupported for %s\n", debugstr_variant(v
.v
));
1446 static HRESULT
interp_enumnext(exec_ctx_t
*ctx
)
1448 const unsigned loop_end
= ctx
->instr
->arg1
.uint
;
1449 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1451 DISPPARAMS dp
= {&v
, &propput_dispid
, 1, 1};
1458 if(V_VT(stack_top(ctx
, 0)) == VT_EMPTY
) {
1459 FIXME("uninitialized\n");
1463 assert(V_VT(stack_top(ctx
, 0)) == VT_UNKNOWN
);
1464 iter
= (IEnumVARIANT
*)V_UNKNOWN(stack_top(ctx
, 0));
1466 V_VT(&v
) = VT_EMPTY
;
1467 hres
= IEnumVARIANT_Next(iter
, 1, &v
, NULL
);
1471 do_continue
= hres
== S_OK
;
1472 hres
= assign_ident(ctx
, ident
, DISPATCH_PROPERTYPUT
|DISPATCH_PROPERTYPUTREF
, &dp
);
1481 instr_jmp(ctx
, loop_end
);
1486 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
1488 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1492 instr_jmp(ctx
, arg
);
1496 static HRESULT
interp_jmp_false(exec_ctx_t
*ctx
)
1498 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1504 hres
= stack_pop_bool(ctx
, &b
);
1511 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1515 static HRESULT
interp_jmp_true(exec_ctx_t
*ctx
)
1517 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1523 hres
= stack_pop_bool(ctx
, &b
);
1528 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1534 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
1542 static HRESULT
interp_retval(exec_ctx_t
*ctx
)
1549 stack_pop_deref(ctx
, &val
);
1552 VariantClear(&ctx
->ret_val
);
1553 ctx
->ret_val
= *val
.v
;
1556 hres
= VariantCopy(&ctx
->ret_val
, val
.v
);
1564 static HRESULT
interp_stop(exec_ctx_t
*ctx
)
1568 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1572 static HRESULT
interp_me(exec_ctx_t
*ctx
)
1580 disp
= (IDispatch
*)&ctx
->vbthis
->IDispatchEx_iface
;
1581 }else if(ctx
->code
->named_item
) {
1582 disp
= (ctx
->code
->named_item
->flags
& SCRIPTITEM_CODEONLY
)
1583 ? (IDispatch
*)&ctx
->code
->named_item
->script_obj
->IDispatchEx_iface
1584 : ctx
->code
->named_item
->disp
;
1588 LIST_FOR_EACH_ENTRY(item
, &ctx
->script
->named_items
, named_item_t
, entry
) {
1589 if(!(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
)) continue;
1594 disp
= (IDispatch
*)&ctx
->script
->script_obj
->IDispatchEx_iface
;
1597 IDispatch_AddRef(disp
);
1598 V_VT(&v
) = VT_DISPATCH
;
1599 V_DISPATCH(&v
) = disp
;
1600 return stack_push(ctx
, &v
);
1603 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
1605 const VARIANT_BOOL arg
= ctx
->instr
->arg1
.lng
;
1608 TRACE("%s\n", arg
? "true" : "false");
1612 return stack_push(ctx
, &v
);
1615 static HRESULT
interp_errmode(exec_ctx_t
*ctx
)
1617 const int err_mode
= ctx
->instr
->arg1
.uint
;
1619 TRACE("%d\n", err_mode
);
1621 ctx
->resume_next
= err_mode
;
1622 clear_ei(&ctx
->script
->ei
);
1626 static HRESULT
interp_string(exec_ctx_t
*ctx
)
1633 V_BSTR(&v
) = SysAllocString(ctx
->instr
->arg1
.str
);
1635 return E_OUTOFMEMORY
;
1637 return stack_push(ctx
, &v
);
1640 static HRESULT
interp_int(exec_ctx_t
*ctx
)
1642 const LONG arg
= ctx
->instr
->arg1
.lng
;
1647 if(arg
== (INT16
)arg
) {
1654 return stack_push(ctx
, &v
);
1657 static HRESULT
interp_double(exec_ctx_t
*ctx
)
1659 const DOUBLE
*arg
= ctx
->instr
->arg1
.dbl
;
1662 TRACE("%lf\n", *arg
);
1666 return stack_push(ctx
, &v
);
1669 static HRESULT
interp_empty(exec_ctx_t
*ctx
)
1675 V_VT(&v
) = VT_EMPTY
;
1676 return stack_push(ctx
, &v
);
1679 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1682 return stack_push_null(ctx
);
1685 static HRESULT
interp_nothing(exec_ctx_t
*ctx
)
1691 V_VT(&v
) = VT_DISPATCH
;
1692 V_DISPATCH(&v
) = NULL
;
1693 return stack_push(ctx
, &v
);
1696 static HRESULT
interp_hres(exec_ctx_t
*ctx
)
1698 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1703 V_VT(&v
) = VT_ERROR
;
1705 return stack_push(ctx
, &v
);
1708 static HRESULT
interp_not(exec_ctx_t
*ctx
)
1716 hres
= stack_pop_val(ctx
, &val
);
1720 hres
= VarNot(val
.v
, &v
);
1725 return stack_push(ctx
, &v
);
1728 static HRESULT
interp_and(exec_ctx_t
*ctx
)
1736 hres
= stack_pop_val(ctx
, &r
);
1740 hres
= stack_pop_val(ctx
, &l
);
1741 if(SUCCEEDED(hres
)) {
1742 hres
= VarAnd(l
.v
, r
.v
, &v
);
1749 return stack_push(ctx
, &v
);
1752 static HRESULT
interp_or(exec_ctx_t
*ctx
)
1760 hres
= stack_pop_val(ctx
, &r
);
1764 hres
= stack_pop_val(ctx
, &l
);
1765 if(SUCCEEDED(hres
)) {
1766 hres
= VarOr(l
.v
, r
.v
, &v
);
1773 return stack_push(ctx
, &v
);
1776 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
1784 hres
= stack_pop_val(ctx
, &r
);
1788 hres
= stack_pop_val(ctx
, &l
);
1789 if(SUCCEEDED(hres
)) {
1790 hres
= VarXor(l
.v
, r
.v
, &v
);
1797 return stack_push(ctx
, &v
);
1800 static HRESULT
interp_eqv(exec_ctx_t
*ctx
)
1808 hres
= stack_pop_val(ctx
, &r
);
1812 hres
= stack_pop_val(ctx
, &l
);
1813 if(SUCCEEDED(hres
)) {
1814 hres
= VarEqv(l
.v
, r
.v
, &v
);
1821 return stack_push(ctx
, &v
);
1824 static HRESULT
interp_imp(exec_ctx_t
*ctx
)
1832 hres
= stack_pop_val(ctx
, &r
);
1836 hres
= stack_pop_val(ctx
, &l
);
1837 if(SUCCEEDED(hres
)) {
1838 hres
= VarImp(l
.v
, r
.v
, &v
);
1845 return stack_push(ctx
, &v
);
1848 static HRESULT
var_cmp(exec_ctx_t
*ctx
, VARIANT
*l
, VARIANT
*r
)
1850 TRACE("%s %s\n", debugstr_variant(l
), debugstr_variant(r
));
1852 /* FIXME: Fix comparing string to number */
1854 return VarCmp(l
, r
, ctx
->script
->lcid
, 0);
1857 static HRESULT
cmp_oper(exec_ctx_t
*ctx
)
1862 hres
= stack_pop_val(ctx
, &r
);
1866 hres
= stack_pop_val(ctx
, &l
);
1867 if(SUCCEEDED(hres
)) {
1868 hres
= var_cmp(ctx
, l
.v
, r
.v
);
1876 static HRESULT
interp_equal(exec_ctx_t
*ctx
)
1883 hres
= cmp_oper(ctx
);
1886 if(hres
== VARCMP_NULL
)
1887 return stack_push_null(ctx
);
1890 V_BOOL(&v
) = hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1891 return stack_push(ctx
, &v
);
1894 static HRESULT
interp_nequal(exec_ctx_t
*ctx
)
1901 hres
= cmp_oper(ctx
);
1904 if(hres
== VARCMP_NULL
)
1905 return stack_push_null(ctx
);
1908 V_BOOL(&v
) = hres
!= VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1909 return stack_push(ctx
, &v
);
1912 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
1919 hres
= cmp_oper(ctx
);
1922 if(hres
== VARCMP_NULL
)
1923 return stack_push_null(ctx
);
1926 V_BOOL(&v
) = hres
== VARCMP_GT
? VARIANT_TRUE
: VARIANT_FALSE
;
1927 return stack_push(ctx
, &v
);
1930 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
1937 hres
= cmp_oper(ctx
);
1940 if(hres
== VARCMP_NULL
)
1941 return stack_push_null(ctx
);
1944 V_BOOL(&v
) = hres
== VARCMP_GT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1945 return stack_push(ctx
, &v
);
1948 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
1955 hres
= cmp_oper(ctx
);
1958 if(hres
== VARCMP_NULL
)
1959 return stack_push_null(ctx
);
1962 V_BOOL(&v
) = hres
== VARCMP_LT
? VARIANT_TRUE
: VARIANT_FALSE
;
1963 return stack_push(ctx
, &v
);
1966 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
1973 hres
= cmp_oper(ctx
);
1976 if(hres
== VARCMP_NULL
)
1977 return stack_push_null(ctx
);
1980 V_BOOL(&v
) = hres
== VARCMP_LT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1981 return stack_push(ctx
, &v
);
1984 static HRESULT
interp_case(exec_ctx_t
*ctx
)
1986 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1992 hres
= stack_pop_val(ctx
, &v
);
1996 hres
= var_cmp(ctx
, stack_top(ctx
, 0), v
.v
);
2001 if(hres
== VARCMP_EQ
) {
2003 instr_jmp(ctx
, arg
);
2011 static HRESULT
interp_is(exec_ctx_t
*ctx
)
2013 IUnknown
*l
= NULL
, *r
= NULL
;
2015 HRESULT hres
= S_OK
;
2019 stack_pop_deref(ctx
, &v
);
2020 if(V_VT(v
.v
) != VT_DISPATCH
&& V_VT(v
.v
) != VT_UNKNOWN
) {
2021 FIXME("Unhandled type %s\n", debugstr_variant(v
.v
));
2023 }else if(V_UNKNOWN(v
.v
)) {
2024 hres
= IUnknown_QueryInterface(V_UNKNOWN(v
.v
), &IID_IUnknown
, (void**)&r
);
2026 if(v
.owned
) VariantClear(v
.v
);
2030 stack_pop_deref(ctx
, &v
);
2031 if(V_VT(v
.v
) != VT_DISPATCH
&& V_VT(v
.v
) != VT_UNKNOWN
) {
2032 FIXME("Unhandled type %s\n", debugstr_variant(v
.v
));
2034 }else if(V_UNKNOWN(v
.v
)) {
2035 hres
= IUnknown_QueryInterface(V_UNKNOWN(v
.v
), &IID_IUnknown
, (void**)&l
);
2037 if(v
.owned
) VariantClear(v
.v
);
2039 if(SUCCEEDED(hres
)) {
2041 V_VT(&res
) = VT_BOOL
;
2043 V_BOOL(&res
) = VARIANT_TRUE
;
2045 V_BOOL(&res
) = VARIANT_FALSE
;
2047 IObjectIdentity
*identity
;
2048 hres
= IUnknown_QueryInterface(l
, &IID_IObjectIdentity
, (void**)&identity
);
2049 if(SUCCEEDED(hres
)) {
2050 hres
= IObjectIdentity_IsEqualObject(identity
, r
);
2051 IObjectIdentity_Release(identity
);
2053 V_BOOL(&res
) = hres
== S_OK
? VARIANT_TRUE
: VARIANT_FALSE
;
2055 hres
= stack_push(ctx
, &res
);
2058 IUnknown_Release(r
);
2060 IUnknown_Release(l
);
2064 static HRESULT
interp_concat(exec_ctx_t
*ctx
)
2072 hres
= stack_pop_val(ctx
, &r
);
2076 hres
= stack_pop_val(ctx
, &l
);
2077 if(SUCCEEDED(hres
)) {
2078 hres
= VarCat(l
.v
, r
.v
, &v
);
2085 return stack_push(ctx
, &v
);
2088 static HRESULT
interp_add(exec_ctx_t
*ctx
)
2096 hres
= stack_pop_val(ctx
, &r
);
2100 hres
= stack_pop_val(ctx
, &l
);
2101 if(SUCCEEDED(hres
)) {
2102 hres
= VarAdd(l
.v
, r
.v
, &v
);
2109 return stack_push(ctx
, &v
);
2112 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
2120 hres
= stack_pop_val(ctx
, &r
);
2124 hres
= stack_pop_val(ctx
, &l
);
2125 if(SUCCEEDED(hres
)) {
2126 hres
= VarSub(l
.v
, r
.v
, &v
);
2133 return stack_push(ctx
, &v
);
2136 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
2144 hres
= stack_pop_val(ctx
, &r
);
2148 hres
= stack_pop_val(ctx
, &l
);
2149 if(SUCCEEDED(hres
)) {
2150 hres
= VarMod(l
.v
, r
.v
, &v
);
2157 return stack_push(ctx
, &v
);
2160 static HRESULT
interp_idiv(exec_ctx_t
*ctx
)
2168 hres
= stack_pop_val(ctx
, &r
);
2172 hres
= stack_pop_val(ctx
, &l
);
2173 if(SUCCEEDED(hres
)) {
2174 hres
= VarIdiv(l
.v
, r
.v
, &v
);
2181 return stack_push(ctx
, &v
);
2184 static HRESULT
interp_div(exec_ctx_t
*ctx
)
2192 hres
= stack_pop_val(ctx
, &r
);
2196 hres
= stack_pop_val(ctx
, &l
);
2197 if(SUCCEEDED(hres
)) {
2198 hres
= VarDiv(l
.v
, r
.v
, &v
);
2205 return stack_push(ctx
, &v
);
2208 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
2216 hres
= stack_pop_val(ctx
, &r
);
2220 hres
= stack_pop_val(ctx
, &l
);
2221 if(SUCCEEDED(hres
)) {
2222 hres
= VarMul(l
.v
, r
.v
, &v
);
2229 return stack_push(ctx
, &v
);
2232 static HRESULT
interp_exp(exec_ctx_t
*ctx
)
2240 hres
= stack_pop_val(ctx
, &r
);
2244 hres
= stack_pop_val(ctx
, &l
);
2245 if(SUCCEEDED(hres
)) {
2246 hres
= VarPow(l
.v
, r
.v
, &v
);
2253 return stack_push(ctx
, &v
);
2256 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
2262 hres
= stack_pop_val(ctx
, &val
);
2266 hres
= VarNeg(val
.v
, &v
);
2271 return stack_push(ctx
, &v
);
2274 static HRESULT
interp_incc(exec_ctx_t
*ctx
)
2276 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
2283 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
2287 if(ref
.type
!= REF_VAR
) {
2288 FIXME("ref.type is not REF_VAR\n");
2292 hres
= VarAdd(stack_top(ctx
, 0), ref
.u
.v
, &v
);
2296 VariantClear(ref
.u
.v
);
2301 static HRESULT
interp_catch(exec_ctx_t
*ctx
)
2303 /* Nothing to do here, the OP is for unwinding only. */
2307 static const instr_func_t op_funcs
[] = {
2308 #define X(x,n,a,b) interp_ ## x,
2313 static const unsigned op_move
[] = {
2314 #define X(x,n,a,b) n,
2319 void release_dynamic_var(dynamic_var_t
*var
)
2321 VariantClear(&var
->v
);
2323 SafeArrayDestroy(var
->array
);
2326 static void release_exec(exec_ctx_t
*ctx
)
2331 VariantClear(&ctx
->ret_val
);
2333 for(var
= ctx
->dynamic_vars
; var
; var
= var
->next
)
2334 release_dynamic_var(var
);
2337 IDispatchEx_Release(&ctx
->vbthis
->IDispatchEx_iface
);
2340 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++)
2341 VariantClear(ctx
->args
+i
);
2345 for(i
=0; i
< ctx
->func
->var_cnt
; i
++)
2346 VariantClear(ctx
->vars
+i
);
2350 for(i
=0; i
< ctx
->func
->array_cnt
; i
++) {
2352 SafeArrayDestroy(ctx
->arrays
[i
]);
2354 heap_free(ctx
->arrays
);
2357 heap_pool_free(&ctx
->heap
);
2358 heap_free(ctx
->args
);
2359 heap_free(ctx
->vars
);
2360 heap_free(ctx
->stack
);
2363 HRESULT
exec_script(script_ctx_t
*ctx
, BOOL extern_caller
, function_t
*func
, vbdisp_t
*vbthis
, DISPPARAMS
*dp
, VARIANT
*res
)
2365 exec_ctx_t exec
= {func
->code_ctx
};
2367 HRESULT hres
= S_OK
;
2369 exec
.code
= func
->code_ctx
;
2371 if(dp
? func
->arg_cnt
!= arg_cnt(dp
) : func
->arg_cnt
) {
2372 FIXME("wrong arg_cnt %d, expected %d\n", dp
? arg_cnt(dp
) : 0, func
->arg_cnt
);
2376 heap_pool_init(&exec
.heap
);
2378 TRACE("%s(", debugstr_w(func
->name
));
2383 exec
.args
= heap_alloc_zero(func
->arg_cnt
* sizeof(VARIANT
));
2385 release_exec(&exec
);
2386 return E_OUTOFMEMORY
;
2389 for(i
=0; i
< func
->arg_cnt
; i
++) {
2391 TRACE("%s%s", i
? ", " : "", debugstr_variant(v
));
2392 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
)) {
2393 if(func
->args
[i
].by_ref
)
2396 hres
= VariantCopyInd(exec
.args
+i
, V_VARIANTREF(v
));
2398 hres
= VariantCopyInd(exec
.args
+i
, v
);
2401 release_exec(&exec
);
2411 exec
.vars
= heap_alloc_zero(func
->var_cnt
* sizeof(VARIANT
));
2413 release_exec(&exec
);
2414 return E_OUTOFMEMORY
;
2420 exec
.stack_size
= 16;
2422 exec
.stack
= heap_alloc(exec
.stack_size
* sizeof(VARIANT
));
2424 release_exec(&exec
);
2425 return E_OUTOFMEMORY
;
2429 IActiveScriptSite_OnEnterScript(ctx
->site
);
2432 IDispatchEx_AddRef(&vbthis
->IDispatchEx_iface
);
2433 exec
.vbthis
= vbthis
;
2436 exec
.instr
= exec
.code
->instrs
+ func
->code_off
;
2441 op
= exec
.instr
->op
;
2442 hres
= op_funcs
[op
](&exec
);
2444 if(hres
!= SCRIPT_E_RECORDED
) {
2447 ctx
->ei
.scode
= hres
= map_hres(hres
);
2448 ctx
->ei
.bstrSource
= get_vbscript_string(VBS_RUNTIME_ERROR
);
2449 ctx
->ei
.bstrDescription
= get_vbscript_error_string(hres
);
2451 hres
= ctx
->ei
.scode
;
2454 if(exec
.resume_next
) {
2457 WARN("Failed %08x in resume next mode\n", hres
);
2460 * Unwinding here is simple. We need to find the next OP_catch, which contains
2461 * information about expected stack size and jump offset on error. Generated
2462 * bytecode needs to guarantee, that simple jump and stack adjustment will
2463 * guarantee proper execution continuation.
2465 while((++exec
.instr
)->op
!= OP_catch
);
2467 TRACE("unwind jmp %d stack_off %d\n", exec
.instr
->arg1
.uint
, exec
.instr
->arg2
.uint
);
2469 clear_error_loc(ctx
);
2470 stack_off
= exec
.instr
->arg2
.uint
;
2471 instr_jmp(&exec
, exec
.instr
->arg1
.uint
);
2473 if(exec
.top
> stack_off
) {
2474 stack_popn(&exec
, exec
.top
-stack_off
);
2475 }else if(exec
.top
< stack_off
) {
2478 V_VT(&v
) = VT_EMPTY
;
2479 while(exec
.top
< stack_off
) {
2480 hres
= stack_push(&exec
, &v
);
2488 if(!ctx
->error_loc_code
) {
2489 grab_vbscode(exec
.code
);
2490 ctx
->error_loc_code
= exec
.code
;
2491 ctx
->error_loc_offset
= exec
.instr
->loc
;
2493 stack_popn(&exec
, exec
.top
);
2498 exec
.instr
+= op_move
[op
];
2506 ctx
->ei
.scode
= hres
;
2507 hres
= report_script_error(ctx
, ctx
->error_loc_code
, ctx
->error_loc_offset
);
2508 clear_error_loc(ctx
);
2510 IActiveScriptSite_OnLeaveScript(ctx
->site
);
2513 if(SUCCEEDED(hres
) && res
) {
2514 *res
= exec
.ret_val
;
2515 V_VT(&exec
.ret_val
) = VT_EMPTY
;
2518 release_exec(&exec
);