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
= heap_realloc(script_obj
->global_vars
, cnt
* 2 * sizeof(*new_vars
));
272 new_vars
= heap_alloc(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
= heap_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 static HRESULT
array_access(exec_ctx_t
*ctx
, 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
= heap_alloc(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
), 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(ctx
, 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(ctx
, 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
= heap_alloc_zero(ctx
->func
->array_cnt
* sizeof(SAFEARRAY
*));
1221 return E_OUTOFMEMORY
;
1224 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1226 FIXME("lookup %s failed: %08x\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
;
1251 V_VT(v
) = VT_ARRAY
|VT_BYREF
|VT_VARIANT
;
1252 V_ARRAYREF(v
) = array_ref
;
1256 static HRESULT
array_bounds_from_stack(exec_ctx_t
*ctx
, unsigned dim_cnt
, SAFEARRAYBOUND
**ret
)
1258 SAFEARRAYBOUND
*bounds
;
1263 if(!(bounds
= heap_alloc(dim_cnt
* sizeof(*bounds
))))
1264 return E_OUTOFMEMORY
;
1266 for(i
= 0; i
< dim_cnt
; i
++) {
1267 hres
= to_int(stack_top(ctx
, dim_cnt
- i
- 1), &dim
);
1273 bounds
[i
].cElements
= dim
+ 1;
1274 bounds
[i
].lLbound
= 0;
1277 stack_popn(ctx
, dim_cnt
);
1282 static HRESULT
interp_redim(exec_ctx_t
*ctx
)
1284 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
1285 const unsigned dim_cnt
= ctx
->instr
->arg2
.uint
;
1286 SAFEARRAYBOUND
*bounds
;
1291 TRACE("%s %u\n", debugstr_w(identifier
), dim_cnt
);
1293 hres
= lookup_identifier(ctx
, identifier
, VBDISP_LET
, &ref
);
1295 FIXME("lookup %s failed: %08x\n", debugstr_w(identifier
), hres
);
1299 if(ref
.type
!= REF_VAR
) {
1300 FIXME("got ref.type = %d\n", ref
.type
);
1304 hres
= array_bounds_from_stack(ctx
, dim_cnt
, &bounds
);
1308 array
= SafeArrayCreate(VT_VARIANT
, dim_cnt
, bounds
);
1311 return E_OUTOFMEMORY
;
1313 /* FIXME: We should check if we're not modifying an existing static array here */
1315 VariantClear(ref
.u
.v
);
1316 V_VT(ref
.u
.v
) = VT_ARRAY
|VT_VARIANT
;
1317 V_ARRAY(ref
.u
.v
) = array
;
1321 static HRESULT
interp_redim_preserve(exec_ctx_t
*ctx
)
1323 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
1324 const unsigned dim_cnt
= ctx
->instr
->arg2
.uint
;
1326 SAFEARRAYBOUND
*bounds
;
1331 TRACE("%s %u\n", debugstr_w(identifier
), dim_cnt
);
1333 hres
= lookup_identifier(ctx
, identifier
, VBDISP_LET
, &ref
);
1335 FIXME("lookup %s failed: %08x\n", debugstr_w(identifier
), hres
);
1339 if(ref
.type
!= REF_VAR
) {
1340 FIXME("got ref.type = %d\n", ref
.type
);
1344 if(!(V_VT(ref
.u
.v
) & VT_ARRAY
)) {
1345 FIXME("ReDim Preserve not valid on type %d\n", V_VT(ref
.u
.v
));
1349 array
= V_ARRAY(ref
.u
.v
);
1351 hres
= array_bounds_from_stack(ctx
, dim_cnt
, &bounds
);
1355 if(array
== NULL
|| array
->cDims
== 0) {
1356 /* can initially allocate the array */
1357 array
= SafeArrayCreate(VT_VARIANT
, dim_cnt
, bounds
);
1358 VariantClear(ref
.u
.v
);
1359 V_VT(ref
.u
.v
) = VT_ARRAY
|VT_VARIANT
;
1360 V_ARRAY(ref
.u
.v
) = array
;
1362 } else if(array
->cDims
!= dim_cnt
) {
1363 /* can't otherwise change the number of dimensions */
1364 TRACE("Can't resize %s, cDims %d != %d\n", debugstr_w(identifier
), array
->cDims
, dim_cnt
);
1365 return MAKE_VBSERROR(VBSE_OUT_OF_BOUNDS
);
1367 /* can resize the last dimensions (if others match */
1368 for(i
= 0; i
+1 < dim_cnt
; ++i
) {
1369 if(array
->rgsabound
[array
->cDims
- 1 - i
].cElements
!= bounds
[i
].cElements
) {
1370 TRACE("Can't resize %s, bound[%d] %d != %d\n", debugstr_w(identifier
), i
, array
->rgsabound
[i
].cElements
, bounds
[i
].cElements
);
1371 return MAKE_VBSERROR(VBSE_OUT_OF_BOUNDS
);
1374 return SafeArrayRedim(array
, &bounds
[dim_cnt
-1]);
1378 static HRESULT
interp_step(exec_ctx_t
*ctx
)
1380 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1386 TRACE("%s\n", debugstr_w(ident
));
1388 V_VT(&zero
) = VT_I2
;
1390 hres
= VarCmp(stack_top(ctx
, 0), &zero
, ctx
->script
->lcid
, 0);
1394 gteq_zero
= hres
== VARCMP_GT
|| hres
== VARCMP_EQ
;
1396 hres
= lookup_identifier(ctx
, ident
, VBDISP_ANY
, &ref
);
1400 if(ref
.type
!= REF_VAR
) {
1401 FIXME("%s is not REF_VAR\n", debugstr_w(ident
));
1405 hres
= VarCmp(ref
.u
.v
, stack_top(ctx
, 1), ctx
->script
->lcid
, 0);
1409 if(hres
== VARCMP_EQ
|| hres
== (gteq_zero
? VARCMP_LT
: VARCMP_GT
)) {
1413 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1418 static HRESULT
interp_newenum(exec_ctx_t
*ctx
)
1426 stack_pop_deref(ctx
, &v
);
1427 assert(V_VT(stack_top(ctx
, 0)) == VT_EMPTY
);
1428 r
= stack_top(ctx
, 0);
1431 case VT_DISPATCH
|VT_BYREF
:
1434 DISPPARAMS dp
= {0};
1437 hres
= disp_call(ctx
->script
, V_ISBYREF(v
.v
) ? *V_DISPATCHREF(v
.v
) : V_DISPATCH(v
.v
), DISPID_NEWENUM
, &dp
, &iterv
);
1442 if(V_VT(&iterv
) != VT_UNKNOWN
&& V_VT(&iterv
) != VT_DISPATCH
) {
1443 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv
));
1444 VariantClear(&iterv
);
1448 hres
= IUnknown_QueryInterface(V_UNKNOWN(&iterv
), &IID_IEnumVARIANT
, (void**)&iter
);
1449 IUnknown_Release(V_UNKNOWN(&iterv
));
1451 FIXME("Could not get IEnumVARIANT iface: %08x\n", hres
);
1455 V_VT(r
) = VT_UNKNOWN
;
1456 V_UNKNOWN(r
) = (IUnknown
*)iter
;
1459 case VT_VARIANT
|VT_ARRAY
:
1460 case VT_VARIANT
|VT_ARRAY
|VT_BYREF
: {
1463 hres
= create_safearray_iter(V_ISBYREF(v
.v
) ? *V_ARRAYREF(v
.v
) : V_ARRAY(v
.v
), &iter
);
1467 V_VT(r
) = VT_UNKNOWN
;
1468 V_UNKNOWN(r
) = (IUnknown
*)iter
;
1472 FIXME("Unsupported for %s\n", debugstr_variant(v
.v
));
1480 static HRESULT
interp_enumnext(exec_ctx_t
*ctx
)
1482 const unsigned loop_end
= ctx
->instr
->arg1
.uint
;
1483 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1485 DISPPARAMS dp
= {&v
, &propput_dispid
, 1, 1};
1492 if(V_VT(stack_top(ctx
, 0)) == VT_EMPTY
) {
1493 FIXME("uninitialized\n");
1497 assert(V_VT(stack_top(ctx
, 0)) == VT_UNKNOWN
);
1498 iter
= (IEnumVARIANT
*)V_UNKNOWN(stack_top(ctx
, 0));
1500 V_VT(&v
) = VT_EMPTY
;
1501 hres
= IEnumVARIANT_Next(iter
, 1, &v
, NULL
);
1505 do_continue
= hres
== S_OK
;
1506 hres
= assign_ident(ctx
, ident
, DISPATCH_PROPERTYPUT
|DISPATCH_PROPERTYPUTREF
, &dp
);
1515 instr_jmp(ctx
, loop_end
);
1520 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
1522 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1526 instr_jmp(ctx
, arg
);
1530 static HRESULT
interp_jmp_false(exec_ctx_t
*ctx
)
1532 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1538 hres
= stack_pop_bool(ctx
, &b
);
1545 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1549 static HRESULT
interp_jmp_true(exec_ctx_t
*ctx
)
1551 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1557 hres
= stack_pop_bool(ctx
, &b
);
1562 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1568 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
1576 static HRESULT
interp_retval(exec_ctx_t
*ctx
)
1583 stack_pop_deref(ctx
, &val
);
1586 VariantClear(&ctx
->ret_val
);
1587 ctx
->ret_val
= *val
.v
;
1590 hres
= VariantCopy(&ctx
->ret_val
, val
.v
);
1598 static HRESULT
interp_stop(exec_ctx_t
*ctx
)
1602 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1606 static HRESULT
interp_me(exec_ctx_t
*ctx
)
1614 disp
= (IDispatch
*)&ctx
->vbthis
->IDispatchEx_iface
;
1615 }else if(ctx
->code
->named_item
) {
1616 disp
= (ctx
->code
->named_item
->flags
& SCRIPTITEM_CODEONLY
)
1617 ? (IDispatch
*)&ctx
->code
->named_item
->script_obj
->IDispatchEx_iface
1618 : ctx
->code
->named_item
->disp
;
1622 LIST_FOR_EACH_ENTRY(item
, &ctx
->script
->named_items
, named_item_t
, entry
) {
1623 if(!(item
->flags
& SCRIPTITEM_GLOBALMEMBERS
)) continue;
1628 disp
= (IDispatch
*)&ctx
->script
->script_obj
->IDispatchEx_iface
;
1631 IDispatch_AddRef(disp
);
1632 V_VT(&v
) = VT_DISPATCH
;
1633 V_DISPATCH(&v
) = disp
;
1634 return stack_push(ctx
, &v
);
1637 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
1639 const VARIANT_BOOL arg
= ctx
->instr
->arg1
.lng
;
1642 TRACE("%s\n", arg
? "true" : "false");
1646 return stack_push(ctx
, &v
);
1649 static HRESULT
interp_errmode(exec_ctx_t
*ctx
)
1651 const int err_mode
= ctx
->instr
->arg1
.uint
;
1653 TRACE("%d\n", err_mode
);
1655 ctx
->resume_next
= err_mode
;
1656 clear_ei(&ctx
->script
->ei
);
1660 static HRESULT
interp_string(exec_ctx_t
*ctx
)
1667 V_BSTR(&v
) = SysAllocString(ctx
->instr
->arg1
.str
);
1669 return E_OUTOFMEMORY
;
1671 return stack_push(ctx
, &v
);
1674 static HRESULT
interp_date(exec_ctx_t
*ctx
)
1676 const DATE
*d
= ctx
->instr
->arg1
.date
;
1684 return stack_push(ctx
, &v
);
1687 static HRESULT
interp_int(exec_ctx_t
*ctx
)
1689 const LONG arg
= ctx
->instr
->arg1
.lng
;
1694 if(arg
== (INT16
)arg
) {
1701 return stack_push(ctx
, &v
);
1704 static HRESULT
interp_double(exec_ctx_t
*ctx
)
1706 const DOUBLE
*arg
= ctx
->instr
->arg1
.dbl
;
1709 TRACE("%lf\n", *arg
);
1713 return stack_push(ctx
, &v
);
1716 static HRESULT
interp_empty(exec_ctx_t
*ctx
)
1722 V_VT(&v
) = VT_EMPTY
;
1723 return stack_push(ctx
, &v
);
1726 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1729 return stack_push_null(ctx
);
1732 static HRESULT
interp_nothing(exec_ctx_t
*ctx
)
1738 V_VT(&v
) = VT_DISPATCH
;
1739 V_DISPATCH(&v
) = NULL
;
1740 return stack_push(ctx
, &v
);
1743 static HRESULT
interp_hres(exec_ctx_t
*ctx
)
1745 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1750 V_VT(&v
) = VT_ERROR
;
1752 return stack_push(ctx
, &v
);
1755 static HRESULT
interp_not(exec_ctx_t
*ctx
)
1763 hres
= stack_pop_val(ctx
, &val
);
1767 hres
= VarNot(val
.v
, &v
);
1772 return stack_push(ctx
, &v
);
1775 static HRESULT
interp_and(exec_ctx_t
*ctx
)
1783 hres
= stack_pop_val(ctx
, &r
);
1787 hres
= stack_pop_val(ctx
, &l
);
1788 if(SUCCEEDED(hres
)) {
1789 hres
= VarAnd(l
.v
, r
.v
, &v
);
1796 return stack_push(ctx
, &v
);
1799 static HRESULT
interp_or(exec_ctx_t
*ctx
)
1807 hres
= stack_pop_val(ctx
, &r
);
1811 hres
= stack_pop_val(ctx
, &l
);
1812 if(SUCCEEDED(hres
)) {
1813 hres
= VarOr(l
.v
, r
.v
, &v
);
1820 return stack_push(ctx
, &v
);
1823 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
1831 hres
= stack_pop_val(ctx
, &r
);
1835 hres
= stack_pop_val(ctx
, &l
);
1836 if(SUCCEEDED(hres
)) {
1837 hres
= VarXor(l
.v
, r
.v
, &v
);
1844 return stack_push(ctx
, &v
);
1847 static HRESULT
interp_eqv(exec_ctx_t
*ctx
)
1855 hres
= stack_pop_val(ctx
, &r
);
1859 hres
= stack_pop_val(ctx
, &l
);
1860 if(SUCCEEDED(hres
)) {
1861 hres
= VarEqv(l
.v
, r
.v
, &v
);
1868 return stack_push(ctx
, &v
);
1871 static HRESULT
interp_imp(exec_ctx_t
*ctx
)
1879 hres
= stack_pop_val(ctx
, &r
);
1883 hres
= stack_pop_val(ctx
, &l
);
1884 if(SUCCEEDED(hres
)) {
1885 hres
= VarImp(l
.v
, r
.v
, &v
);
1892 return stack_push(ctx
, &v
);
1895 static HRESULT
var_cmp(exec_ctx_t
*ctx
, VARIANT
*l
, VARIANT
*r
)
1897 TRACE("%s %s\n", debugstr_variant(l
), debugstr_variant(r
));
1899 /* FIXME: Fix comparing string to number */
1901 return VarCmp(l
, r
, ctx
->script
->lcid
, 0);
1904 static HRESULT
cmp_oper(exec_ctx_t
*ctx
)
1909 hres
= stack_pop_val(ctx
, &r
);
1913 hres
= stack_pop_val(ctx
, &l
);
1914 if(SUCCEEDED(hres
)) {
1915 hres
= var_cmp(ctx
, l
.v
, r
.v
);
1923 static HRESULT
interp_equal(exec_ctx_t
*ctx
)
1930 hres
= cmp_oper(ctx
);
1933 if(hres
== VARCMP_NULL
)
1934 return stack_push_null(ctx
);
1937 V_BOOL(&v
) = hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1938 return stack_push(ctx
, &v
);
1941 static HRESULT
interp_nequal(exec_ctx_t
*ctx
)
1948 hres
= cmp_oper(ctx
);
1951 if(hres
== VARCMP_NULL
)
1952 return stack_push_null(ctx
);
1955 V_BOOL(&v
) = hres
!= VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1956 return stack_push(ctx
, &v
);
1959 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
1966 hres
= cmp_oper(ctx
);
1969 if(hres
== VARCMP_NULL
)
1970 return stack_push_null(ctx
);
1973 V_BOOL(&v
) = hres
== VARCMP_GT
? VARIANT_TRUE
: VARIANT_FALSE
;
1974 return stack_push(ctx
, &v
);
1977 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
1984 hres
= cmp_oper(ctx
);
1987 if(hres
== VARCMP_NULL
)
1988 return stack_push_null(ctx
);
1991 V_BOOL(&v
) = hres
== VARCMP_GT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1992 return stack_push(ctx
, &v
);
1995 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
2002 hres
= cmp_oper(ctx
);
2005 if(hres
== VARCMP_NULL
)
2006 return stack_push_null(ctx
);
2009 V_BOOL(&v
) = hres
== VARCMP_LT
? VARIANT_TRUE
: VARIANT_FALSE
;
2010 return stack_push(ctx
, &v
);
2013 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
2020 hres
= cmp_oper(ctx
);
2023 if(hres
== VARCMP_NULL
)
2024 return stack_push_null(ctx
);
2027 V_BOOL(&v
) = hres
== VARCMP_LT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
2028 return stack_push(ctx
, &v
);
2031 static HRESULT
interp_case(exec_ctx_t
*ctx
)
2033 const unsigned arg
= ctx
->instr
->arg1
.uint
;
2039 hres
= stack_pop_val(ctx
, &v
);
2043 hres
= var_cmp(ctx
, stack_top(ctx
, 0), v
.v
);
2048 if(hres
== VARCMP_EQ
) {
2050 instr_jmp(ctx
, arg
);
2058 static HRESULT
interp_is(exec_ctx_t
*ctx
)
2060 IUnknown
*l
= NULL
, *r
= NULL
;
2062 HRESULT hres
= S_OK
;
2066 stack_pop_deref(ctx
, &v
);
2067 if(V_VT(v
.v
) != VT_DISPATCH
&& V_VT(v
.v
) != VT_UNKNOWN
) {
2068 FIXME("Unhandled type %s\n", debugstr_variant(v
.v
));
2070 }else if(V_UNKNOWN(v
.v
)) {
2071 hres
= IUnknown_QueryInterface(V_UNKNOWN(v
.v
), &IID_IUnknown
, (void**)&r
);
2073 if(v
.owned
) VariantClear(v
.v
);
2077 stack_pop_deref(ctx
, &v
);
2078 if(V_VT(v
.v
) != VT_DISPATCH
&& V_VT(v
.v
) != VT_UNKNOWN
) {
2079 FIXME("Unhandled type %s\n", debugstr_variant(v
.v
));
2081 }else if(V_UNKNOWN(v
.v
)) {
2082 hres
= IUnknown_QueryInterface(V_UNKNOWN(v
.v
), &IID_IUnknown
, (void**)&l
);
2084 if(v
.owned
) VariantClear(v
.v
);
2086 if(SUCCEEDED(hres
)) {
2088 V_VT(&res
) = VT_BOOL
;
2090 V_BOOL(&res
) = VARIANT_TRUE
;
2092 V_BOOL(&res
) = VARIANT_FALSE
;
2094 IObjectIdentity
*identity
;
2095 hres
= IUnknown_QueryInterface(l
, &IID_IObjectIdentity
, (void**)&identity
);
2096 if(SUCCEEDED(hres
)) {
2097 hres
= IObjectIdentity_IsEqualObject(identity
, r
);
2098 IObjectIdentity_Release(identity
);
2100 V_BOOL(&res
) = hres
== S_OK
? VARIANT_TRUE
: VARIANT_FALSE
;
2102 hres
= stack_push(ctx
, &res
);
2105 IUnknown_Release(r
);
2107 IUnknown_Release(l
);
2111 static HRESULT
interp_concat(exec_ctx_t
*ctx
)
2119 hres
= stack_pop_val(ctx
, &r
);
2123 hres
= stack_pop_val(ctx
, &l
);
2124 if(SUCCEEDED(hres
)) {
2125 hres
= VarCat(l
.v
, r
.v
, &v
);
2132 return stack_push(ctx
, &v
);
2135 static HRESULT
interp_add(exec_ctx_t
*ctx
)
2143 hres
= stack_pop_val(ctx
, &r
);
2147 hres
= stack_pop_val(ctx
, &l
);
2148 if(SUCCEEDED(hres
)) {
2149 hres
= VarAdd(l
.v
, r
.v
, &v
);
2156 return stack_push(ctx
, &v
);
2159 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
2167 hres
= stack_pop_val(ctx
, &r
);
2171 hres
= stack_pop_val(ctx
, &l
);
2172 if(SUCCEEDED(hres
)) {
2173 hres
= VarSub(l
.v
, r
.v
, &v
);
2180 return stack_push(ctx
, &v
);
2183 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
2191 hres
= stack_pop_val(ctx
, &r
);
2195 hres
= stack_pop_val(ctx
, &l
);
2196 if(SUCCEEDED(hres
)) {
2197 hres
= VarMod(l
.v
, r
.v
, &v
);
2204 return stack_push(ctx
, &v
);
2207 static HRESULT
interp_idiv(exec_ctx_t
*ctx
)
2215 hres
= stack_pop_val(ctx
, &r
);
2219 hres
= stack_pop_val(ctx
, &l
);
2220 if(SUCCEEDED(hres
)) {
2221 hres
= VarIdiv(l
.v
, r
.v
, &v
);
2228 return stack_push(ctx
, &v
);
2231 static HRESULT
interp_div(exec_ctx_t
*ctx
)
2239 hres
= stack_pop_val(ctx
, &r
);
2243 hres
= stack_pop_val(ctx
, &l
);
2244 if(SUCCEEDED(hres
)) {
2245 hres
= VarDiv(l
.v
, r
.v
, &v
);
2252 return stack_push(ctx
, &v
);
2255 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
2263 hres
= stack_pop_val(ctx
, &r
);
2267 hres
= stack_pop_val(ctx
, &l
);
2268 if(SUCCEEDED(hres
)) {
2269 hres
= VarMul(l
.v
, r
.v
, &v
);
2276 return stack_push(ctx
, &v
);
2279 static HRESULT
interp_exp(exec_ctx_t
*ctx
)
2287 hres
= stack_pop_val(ctx
, &r
);
2291 hres
= stack_pop_val(ctx
, &l
);
2292 if(SUCCEEDED(hres
)) {
2293 hres
= VarPow(l
.v
, r
.v
, &v
);
2300 return stack_push(ctx
, &v
);
2303 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
2309 hres
= stack_pop_val(ctx
, &val
);
2313 hres
= VarNeg(val
.v
, &v
);
2318 return stack_push(ctx
, &v
);
2321 static HRESULT
interp_incc(exec_ctx_t
*ctx
)
2323 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
2330 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
2334 if(ref
.type
!= REF_VAR
) {
2335 FIXME("ref.type is not REF_VAR\n");
2339 hres
= VarAdd(stack_top(ctx
, 0), ref
.u
.v
, &v
);
2343 VariantClear(ref
.u
.v
);
2348 static HRESULT
interp_catch(exec_ctx_t
*ctx
)
2350 /* Nothing to do here, the OP is for unwinding only. */
2354 static const instr_func_t op_funcs
[] = {
2355 #define X(x,n,a,b) interp_ ## x,
2360 static const unsigned op_move
[] = {
2361 #define X(x,n,a,b) n,
2366 void release_dynamic_var(dynamic_var_t
*var
)
2368 VariantClear(&var
->v
);
2370 SafeArrayDestroy(var
->array
);
2373 static void release_exec(exec_ctx_t
*ctx
)
2378 VariantClear(&ctx
->ret_val
);
2380 for(var
= ctx
->dynamic_vars
; var
; var
= var
->next
)
2381 release_dynamic_var(var
);
2384 IDispatchEx_Release(&ctx
->vbthis
->IDispatchEx_iface
);
2387 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++)
2388 VariantClear(ctx
->args
+i
);
2392 for(i
=0; i
< ctx
->func
->var_cnt
; i
++)
2393 VariantClear(ctx
->vars
+i
);
2397 for(i
=0; i
< ctx
->func
->array_cnt
; i
++) {
2399 SafeArrayDestroy(ctx
->arrays
[i
]);
2401 heap_free(ctx
->arrays
);
2404 heap_pool_free(&ctx
->heap
);
2405 heap_free(ctx
->args
);
2406 heap_free(ctx
->vars
);
2407 heap_free(ctx
->stack
);
2410 HRESULT
exec_script(script_ctx_t
*ctx
, BOOL extern_caller
, function_t
*func
, vbdisp_t
*vbthis
, DISPPARAMS
*dp
, VARIANT
*res
)
2412 exec_ctx_t exec
= {func
->code_ctx
};
2414 HRESULT hres
= S_OK
;
2416 exec
.code
= func
->code_ctx
;
2418 if(dp
? func
->arg_cnt
!= arg_cnt(dp
) : func
->arg_cnt
) {
2419 FIXME("wrong arg_cnt %d, expected %d\n", dp
? arg_cnt(dp
) : 0, func
->arg_cnt
);
2423 heap_pool_init(&exec
.heap
);
2425 TRACE("%s(", debugstr_w(func
->name
));
2430 exec
.args
= heap_alloc_zero(func
->arg_cnt
* sizeof(VARIANT
));
2432 release_exec(&exec
);
2433 return E_OUTOFMEMORY
;
2436 for(i
=0; i
< func
->arg_cnt
; i
++) {
2438 TRACE("%s%s", i
? ", " : "", debugstr_variant(v
));
2439 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
)) {
2440 if(func
->args
[i
].by_ref
)
2443 hres
= VariantCopyInd(exec
.args
+i
, V_VARIANTREF(v
));
2445 hres
= VariantCopyInd(exec
.args
+i
, v
);
2448 release_exec(&exec
);
2458 exec
.vars
= heap_alloc_zero(func
->var_cnt
* sizeof(VARIANT
));
2460 release_exec(&exec
);
2461 return E_OUTOFMEMORY
;
2467 exec
.stack_size
= 16;
2469 exec
.stack
= heap_alloc(exec
.stack_size
* sizeof(VARIANT
));
2471 release_exec(&exec
);
2472 return E_OUTOFMEMORY
;
2476 IActiveScriptSite_OnEnterScript(ctx
->site
);
2479 IDispatchEx_AddRef(&vbthis
->IDispatchEx_iface
);
2480 exec
.vbthis
= vbthis
;
2483 exec
.instr
= exec
.code
->instrs
+ func
->code_off
;
2488 op
= exec
.instr
->op
;
2489 hres
= op_funcs
[op
](&exec
);
2491 if(hres
!= SCRIPT_E_RECORDED
) {
2494 ctx
->ei
.scode
= hres
= map_hres(hres
);
2495 ctx
->ei
.bstrSource
= get_vbscript_string(VBS_RUNTIME_ERROR
);
2496 ctx
->ei
.bstrDescription
= get_vbscript_error_string(hres
);
2498 hres
= ctx
->ei
.scode
;
2501 if(exec
.resume_next
) {
2504 WARN("Failed %08x in resume next mode\n", hres
);
2507 * Unwinding here is simple. We need to find the next OP_catch, which contains
2508 * information about expected stack size and jump offset on error. Generated
2509 * bytecode needs to guarantee, that simple jump and stack adjustment will
2510 * guarantee proper execution continuation.
2512 while((++exec
.instr
)->op
!= OP_catch
);
2514 TRACE("unwind jmp %d stack_off %d\n", exec
.instr
->arg1
.uint
, exec
.instr
->arg2
.uint
);
2516 clear_error_loc(ctx
);
2517 stack_off
= exec
.instr
->arg2
.uint
;
2518 instr_jmp(&exec
, exec
.instr
->arg1
.uint
);
2520 if(exec
.top
> stack_off
) {
2521 stack_popn(&exec
, exec
.top
-stack_off
);
2522 }else if(exec
.top
< stack_off
) {
2525 V_VT(&v
) = VT_EMPTY
;
2526 while(exec
.top
< stack_off
) {
2527 hres
= stack_push(&exec
, &v
);
2535 if(!ctx
->error_loc_code
) {
2536 grab_vbscode(exec
.code
);
2537 ctx
->error_loc_code
= exec
.code
;
2538 ctx
->error_loc_offset
= exec
.instr
->loc
;
2540 stack_popn(&exec
, exec
.top
);
2545 exec
.instr
+= op_move
[op
];
2553 ctx
->ei
.scode
= hres
;
2554 hres
= report_script_error(ctx
, ctx
->error_loc_code
, ctx
->error_loc_offset
);
2555 clear_error_loc(ctx
);
2557 IActiveScriptSite_OnLeaveScript(ctx
->site
);
2560 if(SUCCEEDED(hres
) && res
) {
2561 *res
= exec
.ret_val
;
2562 V_VT(&exec
.ret_val
) = VT_EMPTY
;
2565 release_exec(&exec
);