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
);
37 dynamic_var_t
*dynamic_vars
;
49 typedef HRESULT (*instr_func_t
)(exec_ctx_t
*);
79 static BOOL
lookup_dynamic_vars(dynamic_var_t
*var
, const WCHAR
*name
, ref_t
*ref
)
82 if(!strcmpiW(var
->name
, name
)) {
83 ref
->type
= var
->is_const
? REF_CONST
: REF_VAR
;
94 static HRESULT
lookup_identifier(exec_ctx_t
*ctx
, BSTR name
, vbdisp_invoke_type_t invoke_type
, ref_t
*ref
)
102 static const WCHAR errW
[] = {'e','r','r',0};
104 if(invoke_type
== VBDISP_LET
105 && (ctx
->func
->type
== FUNC_FUNCTION
|| ctx
->func
->type
== FUNC_PROPGET
|| ctx
->func
->type
== FUNC_DEFGET
)
106 && !strcmpiW(name
, ctx
->func
->name
)) {
108 ref
->u
.v
= &ctx
->ret_val
;
112 for(i
=0; i
< ctx
->func
->var_cnt
; i
++) {
113 if(!strcmpiW(ctx
->func
->vars
[i
].name
, name
)) {
115 ref
->u
.v
= ctx
->vars
+i
;
120 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++) {
121 if(!strcmpiW(ctx
->func
->args
[i
].name
, name
)) {
123 ref
->u
.v
= ctx
->args
+i
;
128 if(lookup_dynamic_vars(ctx
->func
->type
== FUNC_GLOBAL
? ctx
->script
->global_vars
: ctx
->dynamic_vars
, name
, ref
))
131 if(ctx
->func
->type
!= FUNC_GLOBAL
) {
132 hres
= disp_get_id(ctx
->this_obj
, name
, invoke_type
, TRUE
, &id
);
133 if(SUCCEEDED(hres
)) {
134 ref
->type
= REF_DISP
;
135 ref
->u
.d
.disp
= ctx
->this_obj
;
141 if(ctx
->func
->type
!= FUNC_GLOBAL
&& lookup_dynamic_vars(ctx
->script
->global_vars
, name
, ref
))
144 for(func
= ctx
->script
->global_funcs
; func
; func
= func
->next
) {
145 if(!strcmpiW(func
->name
, name
)) {
146 ref
->type
= REF_FUNC
;
152 if(!strcmpiW(name
, errW
)) {
154 ref
->u
.obj
= (IDispatch
*)&ctx
->script
->err_obj
->IDispatchEx_iface
;
158 hres
= vbdisp_get_id(ctx
->script
->global_obj
, name
, invoke_type
, TRUE
, &id
);
159 if(SUCCEEDED(hres
)) {
160 ref
->type
= REF_DISP
;
161 ref
->u
.d
.disp
= (IDispatch
*)&ctx
->script
->global_obj
->IDispatchEx_iface
;
166 LIST_FOR_EACH_ENTRY(item
, &ctx
->script
->named_items
, named_item_t
, entry
) {
167 if((item
->flags
& SCRIPTITEM_GLOBALMEMBERS
)) {
168 hres
= disp_get_id(item
->disp
, name
, invoke_type
, FALSE
, &id
);
169 if(SUCCEEDED(hres
)) {
170 ref
->type
= REF_DISP
;
171 ref
->u
.d
.disp
= item
->disp
;
177 if((item
->flags
& SCRIPTITEM_ISVISIBLE
) && !strcmpiW(item
->name
, name
)) {
181 hres
= IActiveScriptSite_GetItemInfo(ctx
->script
->site
, name
, SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
183 WARN("GetItemInfo failed: %08x\n", hres
);
187 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
188 IUnknown_Release(unk
);
190 WARN("object does not implement IDispatch\n");
196 ref
->u
.obj
= item
->disp
;
201 ref
->type
= REF_NONE
;
205 static HRESULT
add_dynamic_var(exec_ctx_t
*ctx
, const WCHAR
*name
, BOOL is_const
, VARIANT
*val
, BOOL own_val
)
207 dynamic_var_t
*new_var
;
213 heap
= ctx
->func
->type
== FUNC_GLOBAL
? &ctx
->script
->heap
: &ctx
->heap
;
215 new_var
= vbsheap_alloc(heap
, sizeof(*new_var
));
217 return E_OUTOFMEMORY
;
219 size
= (strlenW(name
)+1)*sizeof(WCHAR
);
220 str
= vbsheap_alloc(heap
, size
);
222 return E_OUTOFMEMORY
;
223 memcpy(str
, name
, size
);
225 new_var
->is_const
= is_const
;
230 hres
= VariantCopy(&new_var
->v
, val
);
235 if(ctx
->func
->type
== FUNC_GLOBAL
) {
236 new_var
->next
= ctx
->script
->global_vars
;
237 ctx
->script
->global_vars
= new_var
;
239 new_var
->next
= ctx
->dynamic_vars
;
240 ctx
->dynamic_vars
= new_var
;
246 static inline VARIANT
*stack_pop(exec_ctx_t
*ctx
)
249 return ctx
->stack
+ --ctx
->top
;
252 static inline VARIANT
*stack_top(exec_ctx_t
*ctx
, unsigned n
)
254 assert(ctx
->top
>= n
);
255 return ctx
->stack
+ (ctx
->top
-n
-1);
258 static HRESULT
stack_push(exec_ctx_t
*ctx
, VARIANT
*v
)
260 if(ctx
->stack_size
== ctx
->top
) {
263 new_stack
= heap_realloc(ctx
->stack
, ctx
->stack_size
*2);
266 return E_OUTOFMEMORY
;
269 ctx
->stack
= new_stack
;
270 ctx
->stack_size
*= 2;
273 ctx
->stack
[ctx
->top
++] = *v
;
277 static void stack_popn(exec_ctx_t
*ctx
, unsigned n
)
280 VariantClear(stack_pop(ctx
));
283 static HRESULT
stack_pop_val(exec_ctx_t
*ctx
, variant_val_t
*v
)
287 var
= stack_pop(ctx
);
289 if(V_VT(var
) == (VT_BYREF
|VT_VARIANT
)) {
291 var
= V_VARIANTREF(var
);
296 if(V_VT(var
) == VT_DISPATCH
) {
300 hres
= disp_call(ctx
->script
, V_DISPATCH(var
), DISPID_VALUE
, &dp
, &v
->store
);
302 IDispatch_Release(V_DISPATCH(var
));
315 static inline void release_val(variant_val_t
*v
)
321 static HRESULT
stack_pop_disp(exec_ctx_t
*ctx
, IDispatch
**ret
)
323 VARIANT
*v
= stack_pop(ctx
);
325 if(V_VT(v
) == VT_DISPATCH
) {
326 *ret
= V_DISPATCH(v
);
330 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
331 FIXME("not supported type: %s\n", debugstr_variant(v
));
337 if(V_VT(v
) != VT_DISPATCH
) {
338 FIXME("not disp %s\n", debugstr_variant(v
));
343 IDispatch_AddRef(V_DISPATCH(v
));
344 *ret
= V_DISPATCH(v
);
348 static inline void instr_jmp(exec_ctx_t
*ctx
, unsigned addr
)
350 ctx
->instr
= ctx
->code
->instrs
+ addr
;
353 static void vbstack_to_dp(exec_ctx_t
*ctx
, unsigned arg_cnt
, DISPPARAMS
*dp
)
356 dp
->rgdispidNamedArgs
= NULL
;
363 assert(ctx
->top
>= arg_cnt
);
365 for(i
=1; i
*2 <= arg_cnt
; i
++) {
366 tmp
= ctx
->stack
[ctx
->top
-i
];
367 ctx
->stack
[ctx
->top
-i
] = ctx
->stack
[ctx
->top
-arg_cnt
+i
-1];
368 ctx
->stack
[ctx
->top
-arg_cnt
+i
-1] = tmp
;
371 dp
->rgvarg
= ctx
->stack
+ ctx
->top
-arg_cnt
;
377 static HRESULT
do_icall(exec_ctx_t
*ctx
, VARIANT
*res
)
379 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
380 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
385 hres
= lookup_identifier(ctx
, identifier
, VBDISP_CALLGET
, &ref
);
389 vbstack_to_dp(ctx
, arg_cnt
, &dp
);
395 FIXME("REF_VAR no res\n");
400 FIXME("arguments not implemented\n");
404 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
405 V_BYREF(res
) = V_VT(ref
.u
.v
) == (VT_VARIANT
|VT_BYREF
) ? V_VARIANTREF(ref
.u
.v
) : ref
.u
.v
;
408 hres
= disp_call(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, &dp
, res
);
413 hres
= exec_script(ctx
->script
, ref
.u
.f
, NULL
, &dp
, res
);
419 FIXME("arguments on object\n");
424 IDispatch_AddRef(ref
.u
.obj
);
425 V_VT(res
) = VT_DISPATCH
;
426 V_DISPATCH(res
) = ref
.u
.obj
;
430 FIXME("%s not found\n", debugstr_w(identifier
));
431 return DISP_E_UNKNOWNNAME
;
434 stack_popn(ctx
, arg_cnt
);
438 static HRESULT
interp_icall(exec_ctx_t
*ctx
)
445 hres
= do_icall(ctx
, &v
);
449 return stack_push(ctx
, &v
);
452 static HRESULT
interp_icallv(exec_ctx_t
*ctx
)
455 return do_icall(ctx
, NULL
);
458 static HRESULT
do_mcall(exec_ctx_t
*ctx
, VARIANT
*res
)
460 const BSTR identifier
= ctx
->instr
->arg1
.bstr
;
461 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
467 hres
= stack_pop_disp(ctx
, &obj
);
476 vbstack_to_dp(ctx
, arg_cnt
, &dp
);
478 hres
= disp_get_id(obj
, identifier
, VBDISP_CALLGET
, FALSE
, &id
);
480 hres
= disp_call(ctx
->script
, obj
, id
, &dp
, res
);
481 IDispatch_Release(obj
);
485 stack_popn(ctx
, arg_cnt
);
489 static HRESULT
interp_mcall(exec_ctx_t
*ctx
)
496 hres
= do_mcall(ctx
, &res
);
500 return stack_push(ctx
, &res
);
503 static HRESULT
interp_mcallv(exec_ctx_t
*ctx
)
507 return do_mcall(ctx
, NULL
);
510 static HRESULT
assign_ident(exec_ctx_t
*ctx
, BSTR name
, VARIANT
*val
, BOOL own_val
)
515 hres
= lookup_identifier(ctx
, name
, VBDISP_LET
, &ref
);
521 VARIANT
*v
= ref
.u
.v
;
523 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
))
531 hres
= VariantCopy(v
, val
);
536 hres
= disp_propput(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, val
);
541 FIXME("functions not implemented\n");
547 FIXME("REF_CONST\n");
550 if(ctx
->func
->code_ctx
->option_explicit
) {
551 FIXME("throw exception\n");
554 TRACE("creating variable %s\n", debugstr_w(name
));
555 hres
= add_dynamic_var(ctx
, name
, FALSE
, val
, own_val
);
562 static HRESULT
interp_assign_ident(exec_ctx_t
*ctx
)
564 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
568 TRACE("%s\n", debugstr_w(arg
));
570 hres
= stack_pop_val(ctx
, &v
);
574 return assign_ident(ctx
, arg
, v
.v
, v
.owned
);
577 static HRESULT
interp_set_ident(exec_ctx_t
*ctx
)
579 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
584 TRACE("%s\n", debugstr_w(arg
));
586 hres
= stack_pop_disp(ctx
, &disp
);
590 V_VT(&v
) = VT_DISPATCH
;
591 V_DISPATCH(&v
) = disp
;
592 return assign_ident(ctx
, ctx
->instr
->arg1
.bstr
, &v
, TRUE
);
595 static HRESULT
interp_assign_member(exec_ctx_t
*ctx
)
597 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
603 TRACE("%s\n", debugstr_w(identifier
));
605 hres
= stack_pop_disp(ctx
, &obj
);
614 hres
= stack_pop_val(ctx
, &val
);
616 IDispatch_Release(obj
);
620 hres
= disp_get_id(obj
, identifier
, VBDISP_LET
, FALSE
, &id
);
622 hres
= disp_propput(ctx
->script
, obj
, id
, val
.v
);
625 IDispatch_Release(obj
);
629 static HRESULT
interp_set_member(exec_ctx_t
*ctx
)
631 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
632 IDispatch
*obj
, *val
;
636 TRACE("%s\n", debugstr_w(identifier
));
638 hres
= stack_pop_disp(ctx
, &obj
);
647 hres
= stack_pop_disp(ctx
, &val
);
649 IDispatch_Release(obj
);
653 hres
= disp_get_id(obj
, identifier
, VBDISP_SET
, FALSE
, &id
);
654 if(SUCCEEDED(hres
)) {
657 V_VT(&v
) = VT_DISPATCH
;
658 V_DISPATCH(&v
) = val
;
659 hres
= disp_propput(ctx
->script
, obj
, id
, &v
);
663 IDispatch_Release(val
);
664 IDispatch_Release(obj
);
668 static HRESULT
interp_const(exec_ctx_t
*ctx
)
670 BSTR arg
= ctx
->instr
->arg1
.bstr
;
675 TRACE("%s\n", debugstr_w(arg
));
677 assert(ctx
->func
->type
== FUNC_GLOBAL
);
679 hres
= lookup_identifier(ctx
, arg
, VBDISP_CALLGET
, &ref
);
683 if(ref
.type
!= REF_NONE
) {
684 FIXME("%s already defined\n", debugstr_w(arg
));
688 hres
= stack_pop_val(ctx
, &val
);
692 return add_dynamic_var(ctx
, arg
, TRUE
, val
.v
, val
.owned
);
695 static HRESULT
interp_val(exec_ctx_t
*ctx
)
703 hres
= stack_pop_val(ctx
, &val
);
709 hres
= VariantCopy(&v
, val
.v
);
714 return stack_push(ctx
, val
.owned
? val
.v
: &v
);
717 static HRESULT
interp_pop(exec_ctx_t
*ctx
)
719 const unsigned n
= ctx
->instr
->arg1
.uint
;
727 static HRESULT
interp_new(exec_ctx_t
*ctx
)
729 const WCHAR
*arg
= ctx
->instr
->arg1
.bstr
;
730 class_desc_t
*class_desc
;
735 TRACE("%s\n", debugstr_w(arg
));
737 for(class_desc
= ctx
->script
->classes
; class_desc
; class_desc
= class_desc
->next
) {
738 if(!strcmpiW(class_desc
->name
, arg
))
742 FIXME("Class %s not found\n", debugstr_w(arg
));
746 hres
= create_vbdisp(class_desc
, &obj
);
750 V_VT(&v
) = VT_DISPATCH
;
751 V_DISPATCH(&v
) = (IDispatch
*)&obj
->IDispatchEx_iface
;
752 return stack_push(ctx
, &v
);
755 static HRESULT
interp_step(exec_ctx_t
*ctx
)
757 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
763 TRACE("%s\n", debugstr_w(ident
));
767 hres
= VarCmp(stack_top(ctx
, 0), &zero
, ctx
->script
->lcid
, 0);
771 gteq_zero
= hres
== VARCMP_GT
|| hres
== VARCMP_EQ
;
773 hres
= lookup_identifier(ctx
, ident
, VBDISP_ANY
, &ref
);
777 if(ref
.type
!= REF_VAR
) {
778 FIXME("%s is not REF_VAR\n", debugstr_w(ident
));
782 hres
= VarCmp(ref
.u
.v
, stack_top(ctx
, 1), ctx
->script
->lcid
, 0);
786 if(hres
== VARCMP_EQ
|| hres
== (gteq_zero
? VARCMP_LT
: VARCMP_GT
))
789 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
793 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
795 const unsigned arg
= ctx
->instr
->arg1
.uint
;
803 static HRESULT
interp_jmp_false(exec_ctx_t
*ctx
)
805 const unsigned arg
= ctx
->instr
->arg1
.uint
;
811 hres
= stack_pop_val(ctx
, &val
);
815 if(V_VT(val
.v
) != VT_BOOL
) {
816 FIXME("unsupported for %s\n", debugstr_variant(val
.v
));
824 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
828 static HRESULT
interp_jmp_true(exec_ctx_t
*ctx
)
830 const unsigned arg
= ctx
->instr
->arg1
.uint
;
836 hres
= stack_pop_val(ctx
, &val
);
840 if(V_VT(val
.v
) != VT_BOOL
) {
841 FIXME("unsupported for %s\n", debugstr_variant(val
.v
));
847 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
853 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
861 static HRESULT
interp_stop(exec_ctx_t
*ctx
)
865 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
869 static HRESULT
interp_me(exec_ctx_t
*ctx
)
875 IDispatch_AddRef(ctx
->this_obj
);
876 V_VT(&v
) = VT_DISPATCH
;
877 V_DISPATCH(&v
) = ctx
->this_obj
;
878 return stack_push(ctx
, &v
);
881 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
883 const VARIANT_BOOL arg
= ctx
->instr
->arg1
.lng
;
886 TRACE("%s\n", arg
? "true" : "false");
890 return stack_push(ctx
, &v
);
893 static HRESULT
interp_errmode(exec_ctx_t
*ctx
)
895 const int err_mode
= ctx
->instr
->arg1
.uint
;
897 TRACE("%d\n", err_mode
);
899 ctx
->resume_next
= err_mode
;
903 static HRESULT
interp_string(exec_ctx_t
*ctx
)
910 V_BSTR(&v
) = SysAllocString(ctx
->instr
->arg1
.str
);
912 return E_OUTOFMEMORY
;
914 return stack_push(ctx
, &v
);
917 static HRESULT
interp_long(exec_ctx_t
*ctx
)
919 const LONG arg
= ctx
->instr
->arg1
.lng
;
926 return stack_push(ctx
, &v
);
929 static HRESULT
interp_short(exec_ctx_t
*ctx
)
931 const LONG arg
= ctx
->instr
->arg1
.lng
;
938 return stack_push(ctx
, &v
);
941 static HRESULT
interp_double(exec_ctx_t
*ctx
)
943 const DOUBLE
*arg
= ctx
->instr
->arg1
.dbl
;
946 TRACE("%lf\n", *arg
);
950 return stack_push(ctx
, &v
);
953 static HRESULT
interp_empty(exec_ctx_t
*ctx
)
960 return stack_push(ctx
, &v
);
963 static HRESULT
interp_null(exec_ctx_t
*ctx
)
970 return stack_push(ctx
, &v
);
973 static HRESULT
interp_nothing(exec_ctx_t
*ctx
)
979 V_VT(&v
) = VT_DISPATCH
;
980 V_DISPATCH(&v
) = NULL
;
981 return stack_push(ctx
, &v
);
984 static HRESULT
interp_not(exec_ctx_t
*ctx
)
992 hres
= stack_pop_val(ctx
, &val
);
996 hres
= VarNot(val
.v
, &v
);
1001 return stack_push(ctx
, &v
);
1004 static HRESULT
interp_and(exec_ctx_t
*ctx
)
1012 hres
= stack_pop_val(ctx
, &r
);
1016 hres
= stack_pop_val(ctx
, &l
);
1017 if(SUCCEEDED(hres
)) {
1018 hres
= VarAnd(l
.v
, r
.v
, &v
);
1025 return stack_push(ctx
, &v
);
1028 static HRESULT
interp_or(exec_ctx_t
*ctx
)
1036 hres
= stack_pop_val(ctx
, &r
);
1040 hres
= stack_pop_val(ctx
, &l
);
1041 if(SUCCEEDED(hres
)) {
1042 hres
= VarOr(l
.v
, r
.v
, &v
);
1049 return stack_push(ctx
, &v
);
1052 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
1060 hres
= stack_pop_val(ctx
, &r
);
1064 hres
= stack_pop_val(ctx
, &l
);
1065 if(SUCCEEDED(hres
)) {
1066 hres
= VarXor(l
.v
, r
.v
, &v
);
1073 return stack_push(ctx
, &v
);
1076 static HRESULT
interp_eqv(exec_ctx_t
*ctx
)
1084 hres
= stack_pop_val(ctx
, &r
);
1088 hres
= stack_pop_val(ctx
, &l
);
1089 if(SUCCEEDED(hres
)) {
1090 hres
= VarEqv(l
.v
, r
.v
, &v
);
1097 return stack_push(ctx
, &v
);
1100 static HRESULT
interp_imp(exec_ctx_t
*ctx
)
1108 hres
= stack_pop_val(ctx
, &r
);
1112 hres
= stack_pop_val(ctx
, &l
);
1113 if(SUCCEEDED(hres
)) {
1114 hres
= VarImp(l
.v
, r
.v
, &v
);
1121 return stack_push(ctx
, &v
);
1124 static HRESULT
cmp_oper(exec_ctx_t
*ctx
)
1129 hres
= stack_pop_val(ctx
, &r
);
1133 hres
= stack_pop_val(ctx
, &l
);
1134 if(SUCCEEDED(hres
)) {
1135 if(V_VT(l
.v
) == VT_NULL
|| V_VT(r
.v
) == VT_NULL
) {
1136 FIXME("comparing nulls is not implemented\n");
1139 hres
= VarCmp(l
.v
, r
.v
, ctx
->script
->lcid
, 0);
1148 static HRESULT
interp_equal(exec_ctx_t
*ctx
)
1155 hres
= cmp_oper(ctx
);
1160 V_BOOL(&v
) = hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1161 return stack_push(ctx
, &v
);
1164 static HRESULT
interp_nequal(exec_ctx_t
*ctx
)
1171 hres
= cmp_oper(ctx
);
1176 V_BOOL(&v
) = hres
!= VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1177 return stack_push(ctx
, &v
);
1180 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
1187 hres
= cmp_oper(ctx
);
1192 V_BOOL(&v
) = hres
== VARCMP_GT
? VARIANT_TRUE
: VARIANT_FALSE
;
1193 return stack_push(ctx
, &v
);
1196 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
1203 hres
= cmp_oper(ctx
);
1208 V_BOOL(&v
) = hres
== VARCMP_GT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1209 return stack_push(ctx
, &v
);
1212 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
1219 hres
= cmp_oper(ctx
);
1224 V_BOOL(&v
) = hres
== VARCMP_LT
? VARIANT_TRUE
: VARIANT_FALSE
;
1225 return stack_push(ctx
, &v
);
1228 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
1235 hres
= cmp_oper(ctx
);
1240 V_BOOL(&v
) = hres
== VARCMP_LT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1241 return stack_push(ctx
, &v
);
1244 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, VARIANT_BOOL
*ret
)
1246 IObjectIdentity
*identity
;
1247 IUnknown
*unk1
, *unk2
;
1250 if(disp1
== disp2
) {
1251 *ret
= VARIANT_TRUE
;
1255 if(!disp1
|| !disp2
) {
1256 *ret
= VARIANT_FALSE
;
1260 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
1264 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
1266 IUnknown_Release(unk1
);
1271 *ret
= VARIANT_TRUE
;
1273 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
1274 if(SUCCEEDED(hres
)) {
1275 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
1276 IObjectIdentity_Release(identity
);
1277 *ret
= hres
== S_OK
? VARIANT_TRUE
: VARIANT_FALSE
;
1279 *ret
= VARIANT_FALSE
;
1283 IUnknown_Release(unk1
);
1284 IUnknown_Release(unk2
);
1288 static HRESULT
interp_is(exec_ctx_t
*ctx
)
1296 hres
= stack_pop_disp(ctx
, &r
);
1300 hres
= stack_pop_disp(ctx
, &l
);
1301 if(SUCCEEDED(hres
)) {
1303 hres
= disp_cmp(l
, r
, &V_BOOL(&v
));
1305 IDispatch_Release(l
);
1308 IDispatch_Release(r
);
1312 return stack_push(ctx
, &v
);
1315 static HRESULT
interp_concat(exec_ctx_t
*ctx
)
1323 hres
= stack_pop_val(ctx
, &r
);
1327 hres
= stack_pop_val(ctx
, &l
);
1328 if(SUCCEEDED(hres
)) {
1329 hres
= VarCat(l
.v
, r
.v
, &v
);
1336 return stack_push(ctx
, &v
);
1339 static HRESULT
interp_add(exec_ctx_t
*ctx
)
1347 hres
= stack_pop_val(ctx
, &r
);
1351 hres
= stack_pop_val(ctx
, &l
);
1352 if(SUCCEEDED(hres
)) {
1353 hres
= VarAdd(l
.v
, r
.v
, &v
);
1360 return stack_push(ctx
, &v
);
1363 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
1371 hres
= stack_pop_val(ctx
, &r
);
1375 hres
= stack_pop_val(ctx
, &l
);
1376 if(SUCCEEDED(hres
)) {
1377 hres
= VarSub(l
.v
, r
.v
, &v
);
1384 return stack_push(ctx
, &v
);
1387 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
1395 hres
= stack_pop_val(ctx
, &r
);
1399 hres
= stack_pop_val(ctx
, &l
);
1400 if(SUCCEEDED(hres
)) {
1401 hres
= VarMod(l
.v
, r
.v
, &v
);
1408 return stack_push(ctx
, &v
);
1411 static HRESULT
interp_idiv(exec_ctx_t
*ctx
)
1419 hres
= stack_pop_val(ctx
, &r
);
1423 hres
= stack_pop_val(ctx
, &l
);
1424 if(SUCCEEDED(hres
)) {
1425 hres
= VarIdiv(l
.v
, r
.v
, &v
);
1432 return stack_push(ctx
, &v
);
1435 static HRESULT
interp_div(exec_ctx_t
*ctx
)
1443 hres
= stack_pop_val(ctx
, &r
);
1447 hres
= stack_pop_val(ctx
, &l
);
1448 if(SUCCEEDED(hres
)) {
1449 hres
= VarDiv(l
.v
, r
.v
, &v
);
1456 return stack_push(ctx
, &v
);
1459 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
1467 hres
= stack_pop_val(ctx
, &r
);
1471 hres
= stack_pop_val(ctx
, &l
);
1472 if(SUCCEEDED(hres
)) {
1473 hres
= VarMul(l
.v
, r
.v
, &v
);
1480 return stack_push(ctx
, &v
);
1483 static HRESULT
interp_exp(exec_ctx_t
*ctx
)
1491 hres
= stack_pop_val(ctx
, &r
);
1495 hres
= stack_pop_val(ctx
, &l
);
1496 if(SUCCEEDED(hres
)) {
1497 hres
= VarPow(l
.v
, r
.v
, &v
);
1504 return stack_push(ctx
, &v
);
1507 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
1513 hres
= stack_pop_val(ctx
, &val
);
1517 hres
= VarNeg(val
.v
, &v
);
1522 return stack_push(ctx
, &v
);
1525 static HRESULT
interp_incc(exec_ctx_t
*ctx
)
1527 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
1534 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1538 if(ref
.type
!= REF_VAR
) {
1539 FIXME("ref.type is not REF_VAR\n");
1543 hres
= VarAdd(stack_top(ctx
, 0), ref
.u
.v
, &v
);
1547 VariantClear(ref
.u
.v
);
1552 static const instr_func_t op_funcs
[] = {
1553 #define X(x,n,a,b) interp_ ## x,
1558 static const unsigned op_move
[] = {
1559 #define X(x,n,a,b) n,
1564 static void release_exec(exec_ctx_t
*ctx
)
1568 VariantClear(&ctx
->ret_val
);
1571 IDispatch_Release(ctx
->this_obj
);
1574 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++)
1575 VariantClear(ctx
->args
+i
);
1579 for(i
=0; i
< ctx
->func
->var_cnt
; i
++)
1580 VariantClear(ctx
->vars
+i
);
1583 vbsheap_free(&ctx
->heap
);
1584 heap_free(ctx
->args
);
1585 heap_free(ctx
->vars
);
1586 heap_free(ctx
->stack
);
1589 HRESULT
exec_script(script_ctx_t
*ctx
, function_t
*func
, IDispatch
*this_obj
, DISPPARAMS
*dp
, VARIANT
*res
)
1591 exec_ctx_t exec
= {func
->code_ctx
};
1593 HRESULT hres
= S_OK
;
1595 exec
.code
= func
->code_ctx
;
1597 if(dp
? func
->arg_cnt
!= arg_cnt(dp
) : func
->arg_cnt
) {
1598 FIXME("wrong arg_cnt %d, expected %d\n", dp
? arg_cnt(dp
) : 0, func
->arg_cnt
);
1602 vbsheap_init(&exec
.heap
);
1608 exec
.args
= heap_alloc_zero(func
->arg_cnt
* sizeof(VARIANT
));
1610 release_exec(&exec
);
1611 return E_OUTOFMEMORY
;
1614 for(i
=0; i
< func
->arg_cnt
; i
++) {
1616 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
)) {
1617 if(func
->args
[i
].by_ref
)
1620 hres
= VariantCopy(exec
.args
+i
, V_VARIANTREF(v
));
1622 hres
= VariantCopy(exec
.args
+i
, v
);
1625 release_exec(&exec
);
1634 exec
.vars
= heap_alloc_zero(func
->var_cnt
* sizeof(VARIANT
));
1636 release_exec(&exec
);
1637 return E_OUTOFMEMORY
;
1643 exec
.stack_size
= 16;
1645 exec
.stack
= heap_alloc(exec
.stack_size
* sizeof(VARIANT
));
1647 release_exec(&exec
);
1648 return E_OUTOFMEMORY
;
1652 exec
.this_obj
= this_obj
;
1653 else if (ctx
->host_global
)
1654 exec
.this_obj
= ctx
->host_global
;
1656 exec
.this_obj
= (IDispatch
*)&ctx
->script_obj
->IDispatchEx_iface
;
1657 IDispatch_AddRef(exec
.this_obj
);
1659 exec
.instr
= exec
.code
->instrs
+ func
->code_off
;
1664 op
= exec
.instr
->op
;
1665 hres
= op_funcs
[op
](&exec
);
1667 if(exec
.resume_next
)
1668 FIXME("Failed %08x in resume next mode\n", hres
);
1670 WARN("Failed %08x\n", hres
);
1671 stack_popn(&exec
, exec
.top
);
1675 exec
.instr
+= op_move
[op
];
1679 if(func
->type
!= FUNC_FUNCTION
&& func
->type
!= FUNC_PROPGET
&& func
->type
!= FUNC_DEFGET
)
1680 assert(V_VT(&exec
.ret_val
) == VT_EMPTY
);
1682 if(SUCCEEDED(hres
) && res
) {
1683 *res
= exec
.ret_val
;
1684 V_VT(&exec
.ret_val
) = VT_EMPTY
;
1687 release_exec(&exec
);