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(invoke_type
!= VBDISP_CALLGET
138 && (ctx
->func
->type
== FUNC_FUNCTION
|| ctx
->func
->type
== FUNC_PROPGET
)
139 && !wcsicmp(name
, ctx
->func
->name
)) {
141 ref
->u
.v
= &ctx
->ret_val
;
145 if(ctx
->func
->type
!= FUNC_GLOBAL
) {
146 for(i
=0; i
< ctx
->func
->var_cnt
; i
++) {
147 if(!wcsicmp(ctx
->func
->vars
[i
].name
, name
)) {
149 ref
->u
.v
= ctx
->vars
+i
;
154 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++) {
155 if(!wcsicmp(ctx
->func
->args
[i
].name
, name
)) {
157 ref
->u
.v
= ctx
->args
+i
;
162 if(lookup_dynamic_vars(ctx
->dynamic_vars
, name
, ref
))
166 /* FIXME: Bind such identifier while generating bytecode. */
167 for(i
=0; i
< ctx
->vbthis
->desc
->prop_cnt
; i
++) {
168 if(!wcsicmp(ctx
->vbthis
->desc
->props
[i
].name
, name
)) {
170 ref
->u
.v
= ctx
->vbthis
->props
+i
;
175 hres
= vbdisp_get_id(ctx
->vbthis
, name
, invoke_type
, TRUE
, &id
);
176 if(SUCCEEDED(hres
)) {
177 ref
->type
= REF_DISP
;
178 ref
->u
.d
.disp
= (IDispatch
*)&ctx
->vbthis
->IDispatchEx_iface
;
185 if(ctx
->code
->named_item
) {
186 if(lookup_global_vars(ctx
->code
->named_item
->script_obj
, name
, ref
))
188 if(lookup_global_funcs(ctx
->code
->named_item
->script_obj
, name
, ref
))
192 if(ctx
->func
->code_ctx
->named_item
&& ctx
->func
->code_ctx
->named_item
->disp
&&
193 !(ctx
->func
->code_ctx
->named_item
->flags
& SCRIPTITEM_CODEONLY
))
195 hres
= disp_get_id(ctx
->func
->code_ctx
->named_item
->disp
, name
, invoke_type
, TRUE
, &id
);
196 if(SUCCEEDED(hres
)) {
197 ref
->type
= REF_DISP
;
198 ref
->u
.d
.disp
= ctx
->func
->code_ctx
->named_item
->disp
;
204 if(lookup_global_vars(script_obj
, name
, ref
))
206 if(lookup_global_funcs(script_obj
, name
, ref
))
209 hres
= get_builtin_id(ctx
->script
->global_obj
, name
, &id
);
210 if(SUCCEEDED(hres
)) {
211 ref
->type
= REF_DISP
;
212 ref
->u
.d
.disp
= &ctx
->script
->global_obj
->IDispatch_iface
;
217 item
= lookup_named_item(ctx
->script
, name
, SCRIPTITEM_ISVISIBLE
);
218 if(item
&& item
->disp
) {
220 ref
->u
.obj
= item
->disp
;
224 LIST_FOR_EACH_ENTRY(item
, &ctx
->script
->named_items
, named_item_t
, entry
) {
225 if((item
->flags
& SCRIPTITEM_GLOBALMEMBERS
)) {
226 hres
= disp_get_id(item
->disp
, name
, invoke_type
, FALSE
, &id
);
227 if(SUCCEEDED(hres
)) {
228 ref
->type
= REF_DISP
;
229 ref
->u
.d
.disp
= item
->disp
;
236 ref
->type
= REF_NONE
;
240 static HRESULT
add_dynamic_var(exec_ctx_t
*ctx
, const WCHAR
*name
,
241 BOOL is_const
, VARIANT
**out_var
)
243 ScriptDisp
*script_obj
= ctx
->code
->named_item
? ctx
->code
->named_item
->script_obj
: ctx
->script
->script_obj
;
244 dynamic_var_t
*new_var
;
249 heap
= ctx
->func
->type
== FUNC_GLOBAL
? &script_obj
->heap
: &ctx
->heap
;
251 new_var
= heap_pool_alloc(heap
, sizeof(*new_var
));
253 return E_OUTOFMEMORY
;
255 size
= (lstrlenW(name
)+1)*sizeof(WCHAR
);
256 str
= heap_pool_alloc(heap
, size
);
258 return E_OUTOFMEMORY
;
259 memcpy(str
, name
, size
);
261 new_var
->is_const
= is_const
;
262 new_var
->array
= NULL
;
263 V_VT(&new_var
->v
) = VT_EMPTY
;
265 if(ctx
->func
->type
== FUNC_GLOBAL
) {
266 size_t cnt
= script_obj
->global_vars_cnt
+ 1;
267 if(cnt
> script_obj
->global_vars_size
) {
268 dynamic_var_t
**new_vars
;
269 if(script_obj
->global_vars
)
270 new_vars
= realloc(script_obj
->global_vars
, cnt
* 2 * sizeof(*new_vars
));
272 new_vars
= malloc(cnt
* 2 * sizeof(*new_vars
));
274 return E_OUTOFMEMORY
;
275 script_obj
->global_vars
= new_vars
;
276 script_obj
->global_vars_size
= cnt
* 2;
278 script_obj
->global_vars
[script_obj
->global_vars_cnt
++] = new_var
;
280 new_var
->next
= ctx
->dynamic_vars
;
281 ctx
->dynamic_vars
= new_var
;
284 *out_var
= &new_var
->v
;
288 void clear_ei(EXCEPINFO
*ei
)
290 SysFreeString(ei
->bstrSource
);
291 SysFreeString(ei
->bstrDescription
);
292 SysFreeString(ei
->bstrHelpFile
);
293 memset(ei
, 0, sizeof(*ei
));
296 static void clear_error_loc(script_ctx_t
*ctx
)
298 if(ctx
->error_loc_code
) {
299 release_vbscode(ctx
->error_loc_code
);
300 ctx
->error_loc_code
= NULL
;
304 static inline VARIANT
*stack_pop(exec_ctx_t
*ctx
)
307 return ctx
->stack
+ --ctx
->top
;
310 static inline VARIANT
*stack_top(exec_ctx_t
*ctx
, unsigned n
)
312 assert(ctx
->top
>= n
);
313 return ctx
->stack
+ (ctx
->top
-n
-1);
316 static HRESULT
stack_push(exec_ctx_t
*ctx
, VARIANT
*v
)
318 if(ctx
->stack_size
== ctx
->top
) {
321 new_stack
= realloc(ctx
->stack
, ctx
->stack_size
*2*sizeof(*ctx
->stack
));
324 return E_OUTOFMEMORY
;
327 ctx
->stack
= new_stack
;
328 ctx
->stack_size
*= 2;
331 ctx
->stack
[ctx
->top
++] = *v
;
335 static inline HRESULT
stack_push_null(exec_ctx_t
*ctx
)
339 return stack_push(ctx
, &v
);
342 static void stack_popn(exec_ctx_t
*ctx
, unsigned n
)
345 VariantClear(stack_pop(ctx
));
348 static void stack_pop_deref(exec_ctx_t
*ctx
, variant_val_t
*r
)
353 if(V_VT(v
) == (VT_BYREF
|VT_VARIANT
)) {
355 r
->v
= V_VARIANTREF(v
);
362 static inline void release_val(variant_val_t
*v
)
368 static HRESULT
stack_pop_val(exec_ctx_t
*ctx
, variant_val_t
*r
)
370 stack_pop_deref(ctx
, r
);
372 if(V_VT(r
->v
) == VT_DISPATCH
) {
375 hres
= get_disp_value(ctx
->script
, V_DISPATCH(r
->v
), &r
->store
);
376 if(r
->owned
&& V_DISPATCH(r
->v
))
377 IDispatch_Release(V_DISPATCH(r
->v
));
388 static HRESULT
stack_assume_val(exec_ctx_t
*ctx
, unsigned n
)
390 VARIANT
*v
= stack_top(ctx
, n
);
393 if(V_VT(v
) == (VT_BYREF
|VT_VARIANT
)) {
394 VARIANT
*ref
= V_VARIANTREF(v
);
397 hres
= VariantCopy(v
, ref
);
402 if(V_VT(v
) == VT_DISPATCH
) {
405 disp
= V_DISPATCH(v
);
406 hres
= get_disp_value(ctx
->script
, disp
, v
);
408 IDispatch_Release(disp
);
416 static int stack_pop_bool(exec_ctx_t
*ctx
, BOOL
*b
)
422 hres
= stack_pop_val(ctx
, &val
);
442 hres
= VarBoolFromStr(V_BSTR(val
.v
), ctx
->script
->lcid
, 0, &vb
);
448 FIXME("unsupported for %s\n", debugstr_variant(val
.v
));
455 static HRESULT
stack_pop_disp(exec_ctx_t
*ctx
, IDispatch
**ret
)
457 VARIANT
*v
= stack_pop(ctx
);
459 if(V_VT(v
) == VT_DISPATCH
) {
460 *ret
= V_DISPATCH(v
);
464 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
465 FIXME("not supported type: %s\n", debugstr_variant(v
));
471 if(V_VT(v
) != VT_DISPATCH
) {
472 FIXME("not disp %s\n", debugstr_variant(v
));
477 IDispatch_AddRef(V_DISPATCH(v
));
478 *ret
= V_DISPATCH(v
);
482 static HRESULT
stack_assume_disp(exec_ctx_t
*ctx
, unsigned n
, IDispatch
**disp
)
484 VARIANT
*v
= stack_top(ctx
, n
), *ref
;
486 if(V_VT(v
) != VT_DISPATCH
&& (disp
|| V_VT(v
) != VT_UNKNOWN
)) {
487 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
488 FIXME("not supported type: %s\n", debugstr_variant(v
));
492 ref
= V_VARIANTREF(v
);
493 if(V_VT(ref
) != VT_DISPATCH
&& (disp
|| V_VT(ref
) != VT_UNKNOWN
)) {
494 FIXME("not disp %s\n", debugstr_variant(ref
));
499 V_UNKNOWN(v
) = V_UNKNOWN(ref
);
501 IUnknown_AddRef(V_UNKNOWN(v
));
505 *disp
= V_DISPATCH(v
);
509 static inline void instr_jmp(exec_ctx_t
*ctx
, unsigned addr
)
511 ctx
->instr
= ctx
->code
->instrs
+ addr
;
514 static void vbstack_to_dp(exec_ctx_t
*ctx
, unsigned arg_cnt
, BOOL is_propput
, DISPPARAMS
*dp
)
516 dp
->cNamedArgs
= is_propput
? 1 : 0;
517 dp
->cArgs
= arg_cnt
+ dp
->cNamedArgs
;
518 dp
->rgdispidNamedArgs
= is_propput
? &propput_dispid
: NULL
;
524 assert(ctx
->top
>= arg_cnt
);
526 for(i
=1; i
*2 <= arg_cnt
; i
++) {
527 tmp
= ctx
->stack
[ctx
->top
-i
];
528 ctx
->stack
[ctx
->top
-i
] = ctx
->stack
[ctx
->top
-arg_cnt
+i
-1];
529 ctx
->stack
[ctx
->top
-arg_cnt
+i
-1] = tmp
;
532 dp
->rgvarg
= ctx
->stack
+ ctx
->top
-dp
->cArgs
;
534 dp
->rgvarg
= is_propput
? ctx
->stack
+ctx
->top
-1 : NULL
;
538 HRESULT
array_access(SAFEARRAY
*array
, DISPPARAMS
*dp
, VARIANT
**ret
)
540 unsigned i
, argc
= arg_cnt(dp
);
545 FIXME("NULL array\n");
549 hres
= SafeArrayLock(array
);
553 if(array
->cDims
!= argc
) {
554 FIXME("argc %d does not match cDims %d\n", dp
->cArgs
, array
->cDims
);
555 SafeArrayUnlock(array
);
559 indices
= malloc(sizeof(*indices
) * argc
);
561 SafeArrayUnlock(array
);
562 return E_OUTOFMEMORY
;
565 for(i
=0; i
<argc
; i
++) {
566 hres
= to_int(get_arg(dp
, i
), (int *)(indices
+i
));
569 SafeArrayUnlock(array
);
574 hres
= SafeArrayPtrOfIndex(array
, indices
, (void**)ret
);
575 SafeArrayUnlock(array
);
580 static HRESULT
variant_call(exec_ctx_t
*ctx
, VARIANT
*v
, unsigned arg_cnt
, VARIANT
*res
)
582 SAFEARRAY
*array
= NULL
;
586 TRACE("%s\n", debugstr_variant(v
));
588 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
))
592 case VT_ARRAY
|VT_BYREF
|VT_VARIANT
:
593 array
= *V_ARRAYREF(v
);
595 case VT_ARRAY
|VT_VARIANT
:
599 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
600 hres
= disp_call(ctx
->script
, V_DISPATCH(v
), DISPID_VALUE
, &dp
, res
);
603 FIXME("unsupported on %s\n", debugstr_variant(v
));
613 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
614 hres
= array_access(array
, &dp
, &v
);
618 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
622 stack_popn(ctx
, arg_cnt
);
626 static HRESULT
do_icall(exec_ctx_t
*ctx
, VARIANT
*res
, BSTR identifier
, unsigned arg_cnt
)
632 TRACE("%s %u\n", debugstr_w(identifier
), arg_cnt
);
634 hres
= lookup_identifier(ctx
, identifier
, VBDISP_CALLGET
, &ref
);
642 return variant_call(ctx
, ref
.u
.v
, arg_cnt
, res
);
645 FIXME("REF_VAR no res\n");
649 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
650 V_BYREF(res
) = V_VT(ref
.u
.v
) == (VT_VARIANT
|VT_BYREF
) ? V_VARIANTREF(ref
.u
.v
) : ref
.u
.v
;
653 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
654 hres
= disp_call(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, &dp
, res
);
659 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
660 hres
= exec_script(ctx
->script
, FALSE
, ref
.u
.f
, NULL
, &dp
, res
);
666 FIXME("arguments on object\n");
671 IDispatch_AddRef(ref
.u
.obj
);
672 V_VT(res
) = VT_DISPATCH
;
673 V_DISPATCH(res
) = ref
.u
.obj
;
677 if(res
&& !ctx
->func
->code_ctx
->option_explicit
&& arg_cnt
== 0) {
679 hres
= add_dynamic_var(ctx
, identifier
, FALSE
, &new);
682 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
686 FIXME("%s not found\n", debugstr_w(identifier
));
687 return DISP_E_UNKNOWNNAME
;
690 stack_popn(ctx
, arg_cnt
);
694 static HRESULT
interp_icall(exec_ctx_t
*ctx
)
696 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
697 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
703 hres
= do_icall(ctx
, &v
, identifier
, arg_cnt
);
707 return stack_push(ctx
, &v
);
710 static HRESULT
interp_icallv(exec_ctx_t
*ctx
)
712 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
713 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
717 return do_icall(ctx
, NULL
, identifier
, arg_cnt
);
720 static HRESULT
interp_vcall(exec_ctx_t
*ctx
)
722 const unsigned arg_cnt
= ctx
->instr
->arg1
.uint
;
729 hres
= variant_call(ctx
, v
, arg_cnt
, &res
);
734 return stack_push(ctx
, &res
);
737 static HRESULT
interp_vcallv(exec_ctx_t
*ctx
)
739 const unsigned arg_cnt
= ctx
->instr
->arg1
.uint
;
746 hres
= variant_call(ctx
, v
, arg_cnt
, NULL
);
751 static HRESULT
do_mcall(exec_ctx_t
*ctx
, VARIANT
*res
)
753 const BSTR identifier
= ctx
->instr
->arg1
.bstr
;
754 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
760 hres
= stack_pop_disp(ctx
, &obj
);
769 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
771 hres
= disp_get_id(obj
, identifier
, VBDISP_CALLGET
, FALSE
, &id
);
773 hres
= disp_call(ctx
->script
, obj
, id
, &dp
, res
);
774 IDispatch_Release(obj
);
778 stack_popn(ctx
, arg_cnt
);
782 static HRESULT
interp_mcall(exec_ctx_t
*ctx
)
789 hres
= do_mcall(ctx
, &res
);
793 return stack_push(ctx
, &res
);
796 static HRESULT
interp_mcallv(exec_ctx_t
*ctx
)
800 return do_mcall(ctx
, NULL
);
803 static HRESULT
interp_ident(exec_ctx_t
*ctx
)
805 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
809 TRACE("%s\n", debugstr_w(identifier
));
811 if((ctx
->func
->type
== FUNC_FUNCTION
|| ctx
->func
->type
== FUNC_PROPGET
)
812 && !wcsicmp(identifier
, ctx
->func
->name
)) {
813 V_VT(&v
) = VT_BYREF
|VT_VARIANT
;
814 V_BYREF(&v
) = &ctx
->ret_val
;
815 return stack_push(ctx
, &v
);
818 hres
= do_icall(ctx
, &v
, identifier
, 0);
822 return stack_push(ctx
, &v
);
825 static HRESULT
assign_value(exec_ctx_t
*ctx
, VARIANT
*dst
, VARIANT
*src
, WORD flags
)
830 V_VT(&value
) = VT_EMPTY
;
831 hres
= VariantCopyInd(&value
, src
);
835 if(V_VT(&value
) == VT_DISPATCH
&& !(flags
& DISPATCH_PROPERTYPUTREF
)) {
836 IDispatch
*disp
= V_DISPATCH(&value
);
838 V_VT(&value
) = VT_EMPTY
;
839 hres
= get_disp_value(ctx
->script
, disp
, &value
);
841 IDispatch_Release(disp
);
851 static HRESULT
assign_ident(exec_ctx_t
*ctx
, BSTR name
, WORD flags
, DISPPARAMS
*dp
)
856 hres
= lookup_identifier(ctx
, name
, VBDISP_LET
, &ref
);
862 VARIANT
*v
= ref
.u
.v
;
864 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
))
870 if(V_VT(v
) == VT_DISPATCH
) {
871 hres
= disp_propput(ctx
->script
, V_DISPATCH(v
), DISPID_VALUE
, flags
, dp
);
875 if(!(V_VT(v
) & VT_ARRAY
)) {
876 FIXME("array assign on type %d\n", V_VT(v
));
881 case VT_ARRAY
|VT_BYREF
|VT_VARIANT
:
882 array
= *V_ARRAYREF(v
);
884 case VT_ARRAY
|VT_VARIANT
:
888 FIXME("Unsupported array type %x\n", V_VT(v
));
893 FIXME("null array\n");
897 hres
= array_access(array
, dp
, &v
);
900 }else if(V_VT(v
) == (VT_ARRAY
|VT_BYREF
|VT_VARIANT
)) {
901 FIXME("non-array assign\n");
905 hres
= assign_value(ctx
, v
, dp
->rgvarg
, flags
);
909 hres
= disp_propput(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, flags
, dp
);
912 FIXME("functions not implemented\n");
918 FIXME("REF_CONST\n");
921 if(ctx
->func
->code_ctx
->option_explicit
) {
922 FIXME("throw exception\n");
928 FIXME("arg_cnt %d not supported\n", arg_cnt(dp
));
932 TRACE("creating variable %s\n", debugstr_w(name
));
933 hres
= add_dynamic_var(ctx
, name
, FALSE
, &new_var
);
935 hres
= assign_value(ctx
, new_var
, dp
->rgvarg
, flags
);
942 static HRESULT
interp_assign_ident(exec_ctx_t
*ctx
)
944 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
945 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
949 TRACE("%s\n", debugstr_w(arg
));
951 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
952 hres
= assign_ident(ctx
, arg
, DISPATCH_PROPERTYPUT
, &dp
);
956 stack_popn(ctx
, arg_cnt
+1);
960 static HRESULT
interp_set_ident(exec_ctx_t
*ctx
)
962 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
963 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
967 TRACE("%s %u\n", debugstr_w(arg
), arg_cnt
);
969 hres
= stack_assume_disp(ctx
, arg_cnt
, NULL
);
973 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
974 hres
= assign_ident(ctx
, arg
, DISPATCH_PROPERTYPUTREF
, &dp
);
978 stack_popn(ctx
, arg_cnt
+ 1);
982 static HRESULT
interp_assign_member(exec_ctx_t
*ctx
)
984 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
985 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
991 TRACE("%s\n", debugstr_w(identifier
));
993 hres
= stack_assume_disp(ctx
, arg_cnt
+1, &obj
);
1002 hres
= disp_get_id(obj
, identifier
, VBDISP_LET
, FALSE
, &id
);
1003 if(SUCCEEDED(hres
)) {
1004 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
1005 hres
= disp_propput(ctx
->script
, obj
, id
, DISPATCH_PROPERTYPUT
, &dp
);
1010 stack_popn(ctx
, arg_cnt
+2);
1014 static HRESULT
interp_set_member(exec_ctx_t
*ctx
)
1016 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
1017 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
1023 TRACE("%s\n", debugstr_w(identifier
));
1025 hres
= stack_assume_disp(ctx
, arg_cnt
+1, &obj
);
1030 FIXME("NULL obj\n");
1034 hres
= stack_assume_disp(ctx
, arg_cnt
, NULL
);
1038 hres
= disp_get_id(obj
, identifier
, VBDISP_SET
, FALSE
, &id
);
1039 if(SUCCEEDED(hres
)) {
1040 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
1041 hres
= disp_propput(ctx
->script
, obj
, id
, DISPATCH_PROPERTYPUTREF
, &dp
);
1046 stack_popn(ctx
, arg_cnt
+2);
1050 static HRESULT
interp_const(exec_ctx_t
*ctx
)
1052 BSTR arg
= ctx
->instr
->arg1
.bstr
;
1057 TRACE("%s\n", debugstr_w(arg
));
1059 assert(ctx
->func
->type
== FUNC_GLOBAL
);
1061 hres
= lookup_identifier(ctx
, arg
, VBDISP_CALLGET
, &ref
);
1065 if(ref
.type
!= REF_NONE
) {
1066 FIXME("%s already defined\n", debugstr_w(arg
));
1070 hres
= stack_assume_val(ctx
, 0);
1074 hres
= add_dynamic_var(ctx
, arg
, TRUE
, &v
);
1078 *v
= *stack_pop(ctx
);
1082 static HRESULT
interp_val(exec_ctx_t
*ctx
)
1090 hres
= stack_pop_val(ctx
, &val
);
1095 V_VT(&v
) = VT_EMPTY
;
1096 hres
= VariantCopy(&v
, val
.v
);
1101 return stack_push(ctx
, val
.owned
? val
.v
: &v
);
1104 static HRESULT
interp_pop(exec_ctx_t
*ctx
)
1106 const unsigned n
= ctx
->instr
->arg1
.uint
;
1114 static HRESULT
interp_stack(exec_ctx_t
*ctx
)
1116 const unsigned n
= ctx
->instr
->arg1
.uint
;
1123 return MAKE_VBSERROR(505);
1124 assert(n
< ctx
->top
);
1126 V_VT(&v
) = VT_EMPTY
;
1127 hres
= VariantCopy(&v
, ctx
->stack
+ n
);
1131 return stack_push(ctx
, &v
);
1134 static HRESULT
interp_deref(exec_ctx_t
*ctx
)
1136 VARIANT copy
, *v
= stack_top(ctx
, 0);
1139 TRACE("%s\n", debugstr_variant(v
));
1141 if(V_VT(v
) != (VT_BYREF
|VT_VARIANT
))
1144 V_VT(©
) = VT_EMPTY
;
1145 hres
= VariantCopy(©
, V_VARIANTREF(v
));
1151 static HRESULT
interp_new(exec_ctx_t
*ctx
)
1153 const WCHAR
*arg
= ctx
->instr
->arg1
.bstr
;
1154 class_desc_t
*class_desc
= NULL
;
1159 TRACE("%s\n", debugstr_w(arg
));
1161 if(!wcsicmp(arg
, L
"regexp")) {
1162 V_VT(&v
) = VT_DISPATCH
;
1163 hres
= create_regexp(&V_DISPATCH(&v
));
1167 return stack_push(ctx
, &v
);
1170 if(ctx
->code
->named_item
)
1171 for(class_desc
= ctx
->code
->named_item
->script_obj
->classes
; class_desc
; class_desc
= class_desc
->next
)
1172 if(!wcsicmp(class_desc
->name
, arg
))
1175 for(class_desc
= ctx
->script
->script_obj
->classes
; class_desc
; class_desc
= class_desc
->next
)
1176 if(!wcsicmp(class_desc
->name
, arg
))
1179 FIXME("Class %s not found\n", debugstr_w(arg
));
1183 hres
= create_vbdisp(class_desc
, &obj
);
1187 V_VT(&v
) = VT_DISPATCH
;
1188 V_DISPATCH(&v
) = (IDispatch
*)&obj
->IDispatchEx_iface
;
1189 return stack_push(ctx
, &v
);
1192 static HRESULT
interp_dim(exec_ctx_t
*ctx
)
1194 ScriptDisp
*script_obj
= ctx
->code
->named_item
? ctx
->code
->named_item
->script_obj
: ctx
->script
->script_obj
;
1195 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
1196 const unsigned array_id
= ctx
->instr
->arg2
.uint
;
1197 const array_desc_t
*array_desc
;
1198 SAFEARRAY
**array_ref
;
1202 TRACE("%s\n", debugstr_w(ident
));
1204 assert(array_id
< ctx
->func
->array_cnt
);
1206 if(ctx
->func
->type
== FUNC_GLOBAL
) {
1208 for(i
= 0; i
< script_obj
->global_vars_cnt
; i
++) {
1209 if(!wcsicmp(script_obj
->global_vars
[i
]->name
, ident
))
1212 assert(i
< script_obj
->global_vars_cnt
);
1213 v
= &script_obj
->global_vars
[i
]->v
;
1214 array_ref
= &script_obj
->global_vars
[i
]->array
;
1219 ctx
->arrays
= calloc(ctx
->func
->array_cnt
, sizeof(SAFEARRAY
*));
1221 return E_OUTOFMEMORY
;
1224 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1226 FIXME("lookup %s failed: %08lx\n", debugstr_w(ident
), hres
);
1230 if(ref
.type
!= REF_VAR
) {
1231 FIXME("got ref.type = %d\n", ref
.type
);
1236 array_ref
= ctx
->arrays
+ array_id
;
1240 FIXME("Array already initialized\n");
1244 array_desc
= ctx
->func
->array_descs
+ array_id
;
1245 if(array_desc
->dim_cnt
) {
1246 *array_ref
= SafeArrayCreate(VT_VARIANT
, array_desc
->dim_cnt
, array_desc
->bounds
);
1248 return E_OUTOFMEMORY
;
1249 (*array_ref
)->fFeatures
|= (FADF_FIXEDSIZE
| FADF_STATIC
);
1252 V_VT(v
) = VT_ARRAY
|VT_BYREF
|VT_VARIANT
;
1253 V_ARRAYREF(v
) = array_ref
;
1257 static HRESULT
array_bounds_from_stack(exec_ctx_t
*ctx
, unsigned dim_cnt
, SAFEARRAYBOUND
**ret
)
1259 SAFEARRAYBOUND
*bounds
;
1264 if(!(bounds
= malloc(dim_cnt
* sizeof(*bounds
))))
1265 return E_OUTOFMEMORY
;
1267 for(i
= 0; i
< dim_cnt
; i
++) {
1268 hres
= to_int(stack_top(ctx
, dim_cnt
- i
- 1), &dim
);
1274 bounds
[i
].cElements
= dim
+ 1;
1275 bounds
[i
].lLbound
= 0;
1278 stack_popn(ctx
, dim_cnt
);
1283 static HRESULT
interp_redim(exec_ctx_t
*ctx
)
1285 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
1286 const unsigned dim_cnt
= ctx
->instr
->arg2
.uint
;
1288 SAFEARRAYBOUND
*bounds
;
1293 TRACE("%s %u\n", debugstr_w(identifier
), dim_cnt
);
1295 hres
= lookup_identifier(ctx
, identifier
, VBDISP_LET
, &ref
);
1297 FIXME("lookup %s failed: %08lx\n", debugstr_w(identifier
), hres
);
1301 if(ref
.type
!= REF_VAR
) {
1302 FIXME("got ref.type = %d\n", ref
.type
);
1308 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
)) {
1309 v
= V_VARIANTREF(v
);
1313 SAFEARRAY
*sa
= V_ISBYREF(v
) ? *V_ARRAYREF(v
) : V_ARRAY(v
);
1314 if(sa
->fFeatures
& FADF_FIXEDSIZE
)
1315 return MAKE_VBSERROR(VBSE_ARRAY_LOCKED
);
1318 hres
= array_bounds_from_stack(ctx
, dim_cnt
, &bounds
);
1322 array
= SafeArrayCreate(VT_VARIANT
, dim_cnt
, bounds
);
1325 return E_OUTOFMEMORY
;
1328 V_VT(v
) = VT_ARRAY
|VT_VARIANT
;
1334 static HRESULT
interp_redim_preserve(exec_ctx_t
*ctx
)
1336 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
1337 const unsigned dim_cnt
= ctx
->instr
->arg2
.uint
;
1340 SAFEARRAYBOUND
*bounds
;
1345 TRACE("%s %u\n", debugstr_w(identifier
), dim_cnt
);
1347 hres
= lookup_identifier(ctx
, identifier
, VBDISP_LET
, &ref
);
1349 FIXME("lookup %s failed: %08lx\n", debugstr_w(identifier
), hres
);
1353 if(ref
.type
!= REF_VAR
) {
1354 FIXME("got ref.type = %d\n", ref
.type
);
1360 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
)) {
1361 v
= V_VARIANTREF(v
);
1364 if(!(V_VT(v
) & VT_ARRAY
)) {
1365 FIXME("ReDim Preserve not valid on type %d\n", V_VT(v
));
1369 array
= V_ISBYREF(v
) ? *V_ARRAYREF(v
) : V_ARRAY(v
);
1371 hres
= array_bounds_from_stack(ctx
, dim_cnt
, &bounds
);
1375 if(array
== NULL
|| array
->cDims
== 0) {
1376 /* can initially allocate the array */
1377 array
= SafeArrayCreate(VT_VARIANT
, dim_cnt
, bounds
);
1379 hres
= E_OUTOFMEMORY
;
1382 V_VT(v
) = VT_ARRAY
|VT_VARIANT
;
1385 } else if(array
->cDims
!= dim_cnt
) {
1386 /* can't otherwise change the number of dimensions */
1387 TRACE("Can't resize %s, cDims %d != %d\n", debugstr_w(identifier
), array
->cDims
, dim_cnt
);
1388 hres
= MAKE_VBSERROR(VBSE_OUT_OF_BOUNDS
);
1390 /* can resize the last dimensions (if others match */
1391 for(i
= 0; i
+1 < dim_cnt
; ++i
) {
1392 if(array
->rgsabound
[array
->cDims
- 1 - i
].cElements
!= bounds
[i
].cElements
) {
1393 TRACE("Can't resize %s, bound[%d] %ld != %ld\n", debugstr_w(identifier
), i
, array
->rgsabound
[i
].cElements
, bounds
[i
].cElements
);
1394 hres
= MAKE_VBSERROR(VBSE_OUT_OF_BOUNDS
);
1399 hres
= SafeArrayRedim(array
, &bounds
[dim_cnt
-1]);
1405 static HRESULT
interp_step(exec_ctx_t
*ctx
)
1407 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1413 TRACE("%s\n", debugstr_w(ident
));
1415 V_VT(&zero
) = VT_I2
;
1417 hres
= VarCmp(stack_top(ctx
, 0), &zero
, ctx
->script
->lcid
, 0);
1421 gteq_zero
= hres
== VARCMP_GT
|| hres
== VARCMP_EQ
;
1423 hres
= lookup_identifier(ctx
, ident
, VBDISP_ANY
, &ref
);
1427 if(ref
.type
!= REF_VAR
) {
1428 FIXME("%s is not REF_VAR\n", debugstr_w(ident
));
1432 hres
= VarCmp(ref
.u
.v
, stack_top(ctx
, 1), ctx
->script
->lcid
, 0);
1436 if(hres
== VARCMP_EQ
|| hres
== (gteq_zero
? VARCMP_LT
: VARCMP_GT
)) {
1440 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1445 static HRESULT
interp_newenum(exec_ctx_t
*ctx
)
1453 stack_pop_deref(ctx
, &v
);
1454 assert(V_VT(stack_top(ctx
, 0)) == VT_EMPTY
);
1455 r
= stack_top(ctx
, 0);
1458 case VT_DISPATCH
|VT_BYREF
:
1461 DISPPARAMS dp
= {0};
1464 hres
= disp_call(ctx
->script
, V_ISBYREF(v
.v
) ? *V_DISPATCHREF(v
.v
) : V_DISPATCH(v
.v
), DISPID_NEWENUM
, &dp
, &iterv
);
1469 if(V_VT(&iterv
) != VT_UNKNOWN
&& V_VT(&iterv
) != VT_DISPATCH
) {
1470 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv
));
1471 VariantClear(&iterv
);
1475 hres
= IUnknown_QueryInterface(V_UNKNOWN(&iterv
), &IID_IEnumVARIANT
, (void**)&iter
);
1476 IUnknown_Release(V_UNKNOWN(&iterv
));
1478 FIXME("Could not get IEnumVARIANT iface: %08lx\n", hres
);
1482 V_VT(r
) = VT_UNKNOWN
;
1483 V_UNKNOWN(r
) = (IUnknown
*)iter
;
1486 case VT_VARIANT
|VT_ARRAY
:
1487 case VT_VARIANT
|VT_ARRAY
|VT_BYREF
: {
1490 hres
= create_safearray_iter(V_ISBYREF(v
.v
) ? *V_ARRAYREF(v
.v
) : V_ARRAY(v
.v
), v
.owned
&& !V_ISBYREF(v
.v
), &iter
);
1494 V_VT(r
) = VT_UNKNOWN
;
1495 V_UNKNOWN(r
) = (IUnknown
*)iter
;
1499 FIXME("Unsupported for %s\n", debugstr_variant(v
.v
));
1507 static HRESULT
interp_enumnext(exec_ctx_t
*ctx
)
1509 const unsigned loop_end
= ctx
->instr
->arg1
.uint
;
1510 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1512 DISPPARAMS dp
= {&v
, &propput_dispid
, 1, 1};
1519 if(V_VT(stack_top(ctx
, 0)) == VT_EMPTY
) {
1520 FIXME("uninitialized\n");
1524 assert(V_VT(stack_top(ctx
, 0)) == VT_UNKNOWN
);
1525 iter
= (IEnumVARIANT
*)V_UNKNOWN(stack_top(ctx
, 0));
1527 V_VT(&v
) = VT_EMPTY
;
1528 hres
= IEnumVARIANT_Next(iter
, 1, &v
, NULL
);
1532 do_continue
= hres
== S_OK
;
1533 hres
= assign_ident(ctx
, ident
, DISPATCH_PROPERTYPUT
|DISPATCH_PROPERTYPUTREF
, &dp
);
1542 instr_jmp(ctx
, loop_end
);
1547 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
1549 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1553 instr_jmp(ctx
, arg
);
1557 static HRESULT
interp_jmp_false(exec_ctx_t
*ctx
)
1559 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1565 hres
= stack_pop_bool(ctx
, &b
);
1572 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1576 static HRESULT
interp_jmp_true(exec_ctx_t
*ctx
)
1578 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1584 hres
= stack_pop_bool(ctx
, &b
);
1589 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1595 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
1603 static HRESULT
interp_retval(exec_ctx_t
*ctx
)
1610 stack_pop_deref(ctx
, &val
);
1613 VariantClear(&ctx
->ret_val
);
1614 ctx
->ret_val
= *val
.v
;
1617 hres
= VariantCopy(&ctx
->ret_val
, val
.v
);
1625 static HRESULT
interp_stop(exec_ctx_t
*ctx
)
1629 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1633 static HRESULT
interp_me(exec_ctx_t
*ctx
)
1641 disp
= (IDispatch
*)&ctx
->vbthis
->IDispatchEx_iface
;
1642 }else if(ctx
->code
->named_item
) {
1643 disp
= (ctx
->code
->named_item
->flags
& SCRIPTITEM_CODEONLY
)
1644 ? (IDispatch
*)&ctx
->code
->named_item
->script_obj
->IDispatchEx_iface
1645 : ctx
->code
->named_item
->disp
;
1649 LIST_FOR_EACH_ENTRY(item
, &ctx
->script
->named_items
, named_item_t
, entry
) {
1650 if(!(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
)) continue;
1655 disp
= (IDispatch
*)&ctx
->script
->script_obj
->IDispatchEx_iface
;
1658 IDispatch_AddRef(disp
);
1659 V_VT(&v
) = VT_DISPATCH
;
1660 V_DISPATCH(&v
) = disp
;
1661 return stack_push(ctx
, &v
);
1664 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
1666 const VARIANT_BOOL arg
= ctx
->instr
->arg1
.lng
;
1669 TRACE("%s\n", arg
? "true" : "false");
1673 return stack_push(ctx
, &v
);
1676 static HRESULT
interp_errmode(exec_ctx_t
*ctx
)
1678 const int err_mode
= ctx
->instr
->arg1
.uint
;
1680 TRACE("%d\n", err_mode
);
1682 ctx
->resume_next
= err_mode
;
1683 clear_ei(&ctx
->script
->ei
);
1687 static HRESULT
interp_string(exec_ctx_t
*ctx
)
1694 V_BSTR(&v
) = SysAllocString(ctx
->instr
->arg1
.str
);
1696 return E_OUTOFMEMORY
;
1698 return stack_push(ctx
, &v
);
1701 static HRESULT
interp_date(exec_ctx_t
*ctx
)
1703 const DATE
*d
= ctx
->instr
->arg1
.date
;
1711 return stack_push(ctx
, &v
);
1714 static HRESULT
interp_int(exec_ctx_t
*ctx
)
1716 const LONG arg
= ctx
->instr
->arg1
.lng
;
1719 TRACE("%ld\n", arg
);
1721 if(arg
== (INT16
)arg
) {
1728 return stack_push(ctx
, &v
);
1731 static HRESULT
interp_double(exec_ctx_t
*ctx
)
1733 const DOUBLE
*arg
= ctx
->instr
->arg1
.dbl
;
1736 TRACE("%lf\n", *arg
);
1740 return stack_push(ctx
, &v
);
1743 static HRESULT
interp_empty(exec_ctx_t
*ctx
)
1749 V_VT(&v
) = VT_EMPTY
;
1750 return stack_push(ctx
, &v
);
1753 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1756 return stack_push_null(ctx
);
1759 static HRESULT
interp_nothing(exec_ctx_t
*ctx
)
1765 V_VT(&v
) = VT_DISPATCH
;
1766 V_DISPATCH(&v
) = NULL
;
1767 return stack_push(ctx
, &v
);
1770 static HRESULT
interp_hres(exec_ctx_t
*ctx
)
1772 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1777 V_VT(&v
) = VT_ERROR
;
1779 return stack_push(ctx
, &v
);
1782 static HRESULT
interp_not(exec_ctx_t
*ctx
)
1790 hres
= stack_pop_val(ctx
, &val
);
1794 hres
= VarNot(val
.v
, &v
);
1799 return stack_push(ctx
, &v
);
1802 static HRESULT
interp_and(exec_ctx_t
*ctx
)
1810 hres
= stack_pop_val(ctx
, &r
);
1814 hres
= stack_pop_val(ctx
, &l
);
1815 if(SUCCEEDED(hres
)) {
1816 hres
= VarAnd(l
.v
, r
.v
, &v
);
1823 return stack_push(ctx
, &v
);
1826 static HRESULT
interp_or(exec_ctx_t
*ctx
)
1834 hres
= stack_pop_val(ctx
, &r
);
1838 hres
= stack_pop_val(ctx
, &l
);
1839 if(SUCCEEDED(hres
)) {
1840 hres
= VarOr(l
.v
, r
.v
, &v
);
1847 return stack_push(ctx
, &v
);
1850 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
1858 hres
= stack_pop_val(ctx
, &r
);
1862 hres
= stack_pop_val(ctx
, &l
);
1863 if(SUCCEEDED(hres
)) {
1864 hres
= VarXor(l
.v
, r
.v
, &v
);
1871 return stack_push(ctx
, &v
);
1874 static HRESULT
interp_eqv(exec_ctx_t
*ctx
)
1882 hres
= stack_pop_val(ctx
, &r
);
1886 hres
= stack_pop_val(ctx
, &l
);
1887 if(SUCCEEDED(hres
)) {
1888 hres
= VarEqv(l
.v
, r
.v
, &v
);
1895 return stack_push(ctx
, &v
);
1898 static HRESULT
interp_imp(exec_ctx_t
*ctx
)
1906 hres
= stack_pop_val(ctx
, &r
);
1910 hres
= stack_pop_val(ctx
, &l
);
1911 if(SUCCEEDED(hres
)) {
1912 hres
= VarImp(l
.v
, r
.v
, &v
);
1919 return stack_push(ctx
, &v
);
1922 static HRESULT
var_cmp(exec_ctx_t
*ctx
, VARIANT
*l
, VARIANT
*r
)
1924 TRACE("%s %s\n", debugstr_variant(l
), debugstr_variant(r
));
1926 /* FIXME: Fix comparing string to number */
1928 return VarCmp(l
, r
, ctx
->script
->lcid
, 0);
1931 static HRESULT
cmp_oper(exec_ctx_t
*ctx
)
1936 hres
= stack_pop_val(ctx
, &r
);
1940 hres
= stack_pop_val(ctx
, &l
);
1941 if(SUCCEEDED(hres
)) {
1942 hres
= var_cmp(ctx
, l
.v
, r
.v
);
1950 static HRESULT
interp_equal(exec_ctx_t
*ctx
)
1957 hres
= cmp_oper(ctx
);
1960 if(hres
== VARCMP_NULL
)
1961 return stack_push_null(ctx
);
1964 V_BOOL(&v
) = hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1965 return stack_push(ctx
, &v
);
1968 static HRESULT
interp_nequal(exec_ctx_t
*ctx
)
1975 hres
= cmp_oper(ctx
);
1978 if(hres
== VARCMP_NULL
)
1979 return stack_push_null(ctx
);
1982 V_BOOL(&v
) = hres
!= VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1983 return stack_push(ctx
, &v
);
1986 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
1993 hres
= cmp_oper(ctx
);
1996 if(hres
== VARCMP_NULL
)
1997 return stack_push_null(ctx
);
2000 V_BOOL(&v
) = hres
== VARCMP_GT
? VARIANT_TRUE
: VARIANT_FALSE
;
2001 return stack_push(ctx
, &v
);
2004 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
2011 hres
= cmp_oper(ctx
);
2014 if(hres
== VARCMP_NULL
)
2015 return stack_push_null(ctx
);
2018 V_BOOL(&v
) = hres
== VARCMP_GT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
2019 return stack_push(ctx
, &v
);
2022 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
2029 hres
= cmp_oper(ctx
);
2032 if(hres
== VARCMP_NULL
)
2033 return stack_push_null(ctx
);
2036 V_BOOL(&v
) = hres
== VARCMP_LT
? VARIANT_TRUE
: VARIANT_FALSE
;
2037 return stack_push(ctx
, &v
);
2040 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
2047 hres
= cmp_oper(ctx
);
2050 if(hres
== VARCMP_NULL
)
2051 return stack_push_null(ctx
);
2054 V_BOOL(&v
) = hres
== VARCMP_LT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
2055 return stack_push(ctx
, &v
);
2058 static HRESULT
interp_case(exec_ctx_t
*ctx
)
2060 const unsigned arg
= ctx
->instr
->arg1
.uint
;
2066 hres
= stack_pop_val(ctx
, &v
);
2070 hres
= var_cmp(ctx
, stack_top(ctx
, 0), v
.v
);
2075 if(hres
== VARCMP_EQ
) {
2077 instr_jmp(ctx
, arg
);
2085 static HRESULT
interp_is(exec_ctx_t
*ctx
)
2087 IUnknown
*l
= NULL
, *r
= NULL
;
2089 HRESULT hres
= S_OK
;
2093 stack_pop_deref(ctx
, &v
);
2094 if(V_VT(v
.v
) != VT_DISPATCH
&& V_VT(v
.v
) != VT_UNKNOWN
) {
2095 FIXME("Unhandled type %s\n", debugstr_variant(v
.v
));
2097 }else if(V_UNKNOWN(v
.v
)) {
2098 hres
= IUnknown_QueryInterface(V_UNKNOWN(v
.v
), &IID_IUnknown
, (void**)&r
);
2100 if(v
.owned
) VariantClear(v
.v
);
2104 stack_pop_deref(ctx
, &v
);
2105 if(V_VT(v
.v
) != VT_DISPATCH
&& V_VT(v
.v
) != VT_UNKNOWN
) {
2106 FIXME("Unhandled type %s\n", debugstr_variant(v
.v
));
2108 }else if(V_UNKNOWN(v
.v
)) {
2109 hres
= IUnknown_QueryInterface(V_UNKNOWN(v
.v
), &IID_IUnknown
, (void**)&l
);
2111 if(v
.owned
) VariantClear(v
.v
);
2113 if(SUCCEEDED(hres
)) {
2115 V_VT(&res
) = VT_BOOL
;
2117 V_BOOL(&res
) = VARIANT_TRUE
;
2119 V_BOOL(&res
) = VARIANT_FALSE
;
2121 IObjectIdentity
*identity
;
2122 hres
= IUnknown_QueryInterface(l
, &IID_IObjectIdentity
, (void**)&identity
);
2123 if(SUCCEEDED(hres
)) {
2124 hres
= IObjectIdentity_IsEqualObject(identity
, r
);
2125 IObjectIdentity_Release(identity
);
2127 V_BOOL(&res
) = hres
== S_OK
? VARIANT_TRUE
: VARIANT_FALSE
;
2129 hres
= stack_push(ctx
, &res
);
2132 IUnknown_Release(r
);
2134 IUnknown_Release(l
);
2138 static HRESULT
interp_concat(exec_ctx_t
*ctx
)
2146 hres
= stack_pop_val(ctx
, &r
);
2150 hres
= stack_pop_val(ctx
, &l
);
2151 if(SUCCEEDED(hres
)) {
2152 hres
= VarCat(l
.v
, r
.v
, &v
);
2159 return stack_push(ctx
, &v
);
2162 static HRESULT
interp_add(exec_ctx_t
*ctx
)
2170 hres
= stack_pop_val(ctx
, &r
);
2174 hres
= stack_pop_val(ctx
, &l
);
2175 if(SUCCEEDED(hres
)) {
2176 hres
= VarAdd(l
.v
, r
.v
, &v
);
2183 return stack_push(ctx
, &v
);
2186 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
2194 hres
= stack_pop_val(ctx
, &r
);
2198 hres
= stack_pop_val(ctx
, &l
);
2199 if(SUCCEEDED(hres
)) {
2200 hres
= VarSub(l
.v
, r
.v
, &v
);
2207 return stack_push(ctx
, &v
);
2210 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
2218 hres
= stack_pop_val(ctx
, &r
);
2222 hres
= stack_pop_val(ctx
, &l
);
2223 if(SUCCEEDED(hres
)) {
2224 hres
= VarMod(l
.v
, r
.v
, &v
);
2231 return stack_push(ctx
, &v
);
2234 static HRESULT
interp_idiv(exec_ctx_t
*ctx
)
2242 hres
= stack_pop_val(ctx
, &r
);
2246 hres
= stack_pop_val(ctx
, &l
);
2247 if(SUCCEEDED(hres
)) {
2248 hres
= VarIdiv(l
.v
, r
.v
, &v
);
2255 return stack_push(ctx
, &v
);
2258 static HRESULT
interp_div(exec_ctx_t
*ctx
)
2266 hres
= stack_pop_val(ctx
, &r
);
2270 hres
= stack_pop_val(ctx
, &l
);
2271 if(SUCCEEDED(hres
)) {
2272 hres
= VarDiv(l
.v
, r
.v
, &v
);
2279 return stack_push(ctx
, &v
);
2282 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
2290 hres
= stack_pop_val(ctx
, &r
);
2294 hres
= stack_pop_val(ctx
, &l
);
2295 if(SUCCEEDED(hres
)) {
2296 hres
= VarMul(l
.v
, r
.v
, &v
);
2303 return stack_push(ctx
, &v
);
2306 static HRESULT
interp_exp(exec_ctx_t
*ctx
)
2314 hres
= stack_pop_val(ctx
, &r
);
2318 hres
= stack_pop_val(ctx
, &l
);
2319 if(SUCCEEDED(hres
)) {
2320 hres
= VarPow(l
.v
, r
.v
, &v
);
2327 return stack_push(ctx
, &v
);
2330 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
2336 hres
= stack_pop_val(ctx
, &val
);
2340 hres
= VarNeg(val
.v
, &v
);
2345 return stack_push(ctx
, &v
);
2348 static HRESULT
interp_incc(exec_ctx_t
*ctx
)
2350 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
2357 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
2361 if(ref
.type
!= REF_VAR
) {
2362 FIXME("ref.type is not REF_VAR\n");
2366 hres
= VarAdd(stack_top(ctx
, 0), ref
.u
.v
, &v
);
2370 VariantClear(ref
.u
.v
);
2375 static HRESULT
interp_catch(exec_ctx_t
*ctx
)
2377 /* Nothing to do here, the OP is for unwinding only. */
2381 static const instr_func_t op_funcs
[] = {
2382 #define X(x,n,a,b) interp_ ## x,
2387 static const unsigned op_move
[] = {
2388 #define X(x,n,a,b) n,
2393 void release_dynamic_var(dynamic_var_t
*var
)
2395 VariantClear(&var
->v
);
2397 SafeArrayDestroy(var
->array
);
2400 static void release_exec(exec_ctx_t
*ctx
)
2405 VariantClear(&ctx
->ret_val
);
2407 for(var
= ctx
->dynamic_vars
; var
; var
= var
->next
)
2408 release_dynamic_var(var
);
2411 IDispatchEx_Release(&ctx
->vbthis
->IDispatchEx_iface
);
2414 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++)
2415 VariantClear(ctx
->args
+i
);
2419 for(i
=0; i
< ctx
->func
->var_cnt
; i
++)
2420 VariantClear(ctx
->vars
+i
);
2424 for(i
=0; i
< ctx
->func
->array_cnt
; i
++) {
2426 SafeArrayDestroy(ctx
->arrays
[i
]);
2431 heap_pool_free(&ctx
->heap
);
2437 HRESULT
exec_script(script_ctx_t
*ctx
, BOOL extern_caller
, function_t
*func
, vbdisp_t
*vbthis
, DISPPARAMS
*dp
, VARIANT
*res
)
2439 exec_ctx_t exec
= {func
->code_ctx
};
2441 HRESULT hres
= S_OK
;
2443 exec
.code
= func
->code_ctx
;
2445 if(dp
? func
->arg_cnt
!= arg_cnt(dp
) : func
->arg_cnt
) {
2446 FIXME("wrong arg_cnt %d, expected %d\n", dp
? arg_cnt(dp
) : 0, func
->arg_cnt
);
2450 heap_pool_init(&exec
.heap
);
2452 TRACE("%s args=%u\n", debugstr_w(func
->name
),func
->arg_cnt
);
2457 exec
.args
= calloc(func
->arg_cnt
, sizeof(VARIANT
));
2459 release_exec(&exec
);
2460 return E_OUTOFMEMORY
;
2463 for(i
=0; i
< func
->arg_cnt
; i
++) {
2465 TRACE(" [%d] %s\n", i
, debugstr_variant(v
));
2466 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
)) {
2467 if(func
->args
[i
].by_ref
)
2470 hres
= VariantCopyInd(exec
.args
+i
, V_VARIANTREF(v
));
2472 hres
= VariantCopyInd(exec
.args
+i
, v
);
2475 release_exec(&exec
);
2484 exec
.vars
= calloc(func
->var_cnt
, sizeof(VARIANT
));
2486 release_exec(&exec
);
2487 return E_OUTOFMEMORY
;
2493 exec
.stack_size
= 16;
2495 exec
.stack
= malloc(exec
.stack_size
* sizeof(VARIANT
));
2497 release_exec(&exec
);
2498 return E_OUTOFMEMORY
;
2502 IActiveScriptSite_OnEnterScript(ctx
->site
);
2505 IDispatchEx_AddRef(&vbthis
->IDispatchEx_iface
);
2506 exec
.vbthis
= vbthis
;
2509 exec
.instr
= exec
.code
->instrs
+ func
->code_off
;
2514 op
= exec
.instr
->op
;
2515 hres
= op_funcs
[op
](&exec
);
2517 if(hres
!= SCRIPT_E_RECORDED
) {
2518 /* SCRIPT_E_RECORDED means ctx->ei is already populated */
2520 ctx
->ei
.scode
= hres
;
2523 if(!ctx
->ei
.bstrDescription
)
2524 map_vbs_exception(&ctx
->ei
);
2526 if(exec
.resume_next
) {
2529 WARN("Failed %08lx in resume next mode\n", ctx
->ei
.scode
);
2532 * Unwinding here is simple. We need to find the next OP_catch, which contains
2533 * information about expected stack size and jump offset on error. Generated
2534 * bytecode needs to guarantee, that simple jump and stack adjustment will
2535 * guarantee proper execution continuation.
2537 while((++exec
.instr
)->op
!= OP_catch
);
2539 TRACE("unwind jmp %d stack_off %d\n", exec
.instr
->arg1
.uint
, exec
.instr
->arg2
.uint
);
2541 clear_error_loc(ctx
);
2542 stack_off
= exec
.instr
->arg2
.uint
;
2543 instr_jmp(&exec
, exec
.instr
->arg1
.uint
);
2545 if(exec
.top
> stack_off
) {
2546 stack_popn(&exec
, exec
.top
-stack_off
);
2547 }else if(exec
.top
< stack_off
) {
2550 V_VT(&v
) = VT_EMPTY
;
2551 while(exec
.top
< stack_off
) {
2552 hres
= stack_push(&exec
, &v
);
2560 if(!ctx
->error_loc_code
) {
2561 grab_vbscode(exec
.code
);
2562 ctx
->error_loc_code
= exec
.code
;
2563 ctx
->error_loc_offset
= exec
.instr
->loc
;
2565 stack_popn(&exec
, exec
.top
);
2570 exec
.instr
+= op_move
[op
];
2578 ctx
->ei
.scode
= hres
;
2579 hres
= report_script_error(ctx
, ctx
->error_loc_code
, ctx
->error_loc_offset
);
2580 clear_error_loc(ctx
);
2582 IActiveScriptSite_OnLeaveScript(ctx
->site
);
2585 if(SUCCEEDED(hres
) && res
) {
2586 *res
= exec
.ret_val
;
2587 V_VT(&exec
.ret_val
) = VT_EMPTY
;
2590 release_exec(&exec
);