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_ISVISIBLE
) && !strcmpiW(item
->name
, name
)) {
171 hres
= IActiveScriptSite_GetItemInfo(ctx
->script
->site
, name
, SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
173 WARN("GetItemInfo failed: %08x\n", hres
);
177 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
178 IUnknown_Release(unk
);
180 WARN("object does not implement IDispatch\n");
186 ref
->u
.obj
= item
->disp
;
191 LIST_FOR_EACH_ENTRY(item
, &ctx
->script
->named_items
, named_item_t
, entry
) {
192 if((item
->flags
& SCRIPTITEM_GLOBALMEMBERS
)) {
193 hres
= disp_get_id(item
->disp
, name
, invoke_type
, FALSE
, &id
);
194 if(SUCCEEDED(hres
)) {
195 ref
->type
= REF_DISP
;
196 ref
->u
.d
.disp
= item
->disp
;
203 ref
->type
= REF_NONE
;
207 static HRESULT
add_dynamic_var(exec_ctx_t
*ctx
, const WCHAR
*name
, BOOL is_const
, VARIANT
*val
, BOOL own_val
)
209 dynamic_var_t
*new_var
;
215 heap
= ctx
->func
->type
== FUNC_GLOBAL
? &ctx
->script
->heap
: &ctx
->heap
;
217 new_var
= vbsheap_alloc(heap
, sizeof(*new_var
));
219 return E_OUTOFMEMORY
;
221 size
= (strlenW(name
)+1)*sizeof(WCHAR
);
222 str
= vbsheap_alloc(heap
, size
);
224 return E_OUTOFMEMORY
;
225 memcpy(str
, name
, size
);
227 new_var
->is_const
= is_const
;
232 V_VT(&new_var
->v
) = VT_EMPTY
;
233 hres
= VariantCopy(&new_var
->v
, val
);
238 if(ctx
->func
->type
== FUNC_GLOBAL
) {
239 new_var
->next
= ctx
->script
->global_vars
;
240 ctx
->script
->global_vars
= new_var
;
242 new_var
->next
= ctx
->dynamic_vars
;
243 ctx
->dynamic_vars
= new_var
;
249 static inline VARIANT
*stack_pop(exec_ctx_t
*ctx
)
252 return ctx
->stack
+ --ctx
->top
;
255 static inline VARIANT
*stack_top(exec_ctx_t
*ctx
, unsigned n
)
257 assert(ctx
->top
>= n
);
258 return ctx
->stack
+ (ctx
->top
-n
-1);
261 static HRESULT
stack_push(exec_ctx_t
*ctx
, VARIANT
*v
)
263 if(ctx
->stack_size
== ctx
->top
) {
266 new_stack
= heap_realloc(ctx
->stack
, ctx
->stack_size
*2*sizeof(*ctx
->stack
));
269 return E_OUTOFMEMORY
;
272 ctx
->stack
= new_stack
;
273 ctx
->stack_size
*= 2;
276 ctx
->stack
[ctx
->top
++] = *v
;
280 static void stack_popn(exec_ctx_t
*ctx
, unsigned n
)
283 VariantClear(stack_pop(ctx
));
286 static HRESULT
stack_pop_val(exec_ctx_t
*ctx
, variant_val_t
*v
)
290 var
= stack_pop(ctx
);
292 if(V_VT(var
) == (VT_BYREF
|VT_VARIANT
)) {
294 var
= V_VARIANTREF(var
);
299 if(V_VT(var
) == VT_DISPATCH
) {
303 hres
= disp_call(ctx
->script
, V_DISPATCH(var
), DISPID_VALUE
, &dp
, &v
->store
);
305 IDispatch_Release(V_DISPATCH(var
));
318 static inline void release_val(variant_val_t
*v
)
324 static HRESULT
stack_pop_disp(exec_ctx_t
*ctx
, IDispatch
**ret
)
326 VARIANT
*v
= stack_pop(ctx
);
328 if(V_VT(v
) == VT_DISPATCH
) {
329 *ret
= V_DISPATCH(v
);
333 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
334 FIXME("not supported type: %s\n", debugstr_variant(v
));
340 if(V_VT(v
) != VT_DISPATCH
) {
341 FIXME("not disp %s\n", debugstr_variant(v
));
346 IDispatch_AddRef(V_DISPATCH(v
));
347 *ret
= V_DISPATCH(v
);
351 static HRESULT
stack_assume_disp(exec_ctx_t
*ctx
, unsigned n
, IDispatch
**disp
)
353 VARIANT
*v
= stack_top(ctx
, n
), *ref
;
355 if(V_VT(v
) != VT_DISPATCH
) {
356 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
357 FIXME("not supported type: %s\n", debugstr_variant(v
));
361 ref
= V_VARIANTREF(v
);
362 if(V_VT(ref
) != VT_DISPATCH
) {
363 FIXME("not disp %s\n", debugstr_variant(ref
));
367 V_VT(v
) = VT_DISPATCH
;
368 V_DISPATCH(v
) = V_DISPATCH(ref
);
370 IDispatch_AddRef(V_DISPATCH(v
));
374 *disp
= V_DISPATCH(v
);
378 static inline void instr_jmp(exec_ctx_t
*ctx
, unsigned addr
)
380 ctx
->instr
= ctx
->code
->instrs
+ addr
;
383 static void vbstack_to_dp(exec_ctx_t
*ctx
, unsigned arg_cnt
, DISPPARAMS
*dp
)
386 dp
->rgdispidNamedArgs
= NULL
;
393 assert(ctx
->top
>= arg_cnt
);
395 for(i
=1; i
*2 <= arg_cnt
; i
++) {
396 tmp
= ctx
->stack
[ctx
->top
-i
];
397 ctx
->stack
[ctx
->top
-i
] = ctx
->stack
[ctx
->top
-arg_cnt
+i
-1];
398 ctx
->stack
[ctx
->top
-arg_cnt
+i
-1] = tmp
;
401 dp
->rgvarg
= ctx
->stack
+ ctx
->top
-arg_cnt
;
407 static HRESULT
do_icall(exec_ctx_t
*ctx
, VARIANT
*res
)
409 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
410 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
415 hres
= lookup_identifier(ctx
, identifier
, VBDISP_CALLGET
, &ref
);
419 vbstack_to_dp(ctx
, arg_cnt
, &dp
);
425 FIXME("REF_VAR no res\n");
430 FIXME("arguments not implemented\n");
434 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
435 V_BYREF(res
) = V_VT(ref
.u
.v
) == (VT_VARIANT
|VT_BYREF
) ? V_VARIANTREF(ref
.u
.v
) : ref
.u
.v
;
438 hres
= disp_call(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, &dp
, res
);
443 hres
= exec_script(ctx
->script
, ref
.u
.f
, NULL
, &dp
, res
);
449 FIXME("arguments on object\n");
454 IDispatch_AddRef(ref
.u
.obj
);
455 V_VT(res
) = VT_DISPATCH
;
456 V_DISPATCH(res
) = ref
.u
.obj
;
460 FIXME("%s not found\n", debugstr_w(identifier
));
461 return DISP_E_UNKNOWNNAME
;
464 stack_popn(ctx
, arg_cnt
);
468 static HRESULT
interp_icall(exec_ctx_t
*ctx
)
475 hres
= do_icall(ctx
, &v
);
479 return stack_push(ctx
, &v
);
482 static HRESULT
interp_icallv(exec_ctx_t
*ctx
)
485 return do_icall(ctx
, NULL
);
488 static HRESULT
do_mcall(exec_ctx_t
*ctx
, VARIANT
*res
)
490 const BSTR identifier
= ctx
->instr
->arg1
.bstr
;
491 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
497 hres
= stack_pop_disp(ctx
, &obj
);
506 vbstack_to_dp(ctx
, arg_cnt
, &dp
);
508 hres
= disp_get_id(obj
, identifier
, VBDISP_CALLGET
, FALSE
, &id
);
510 hres
= disp_call(ctx
->script
, obj
, id
, &dp
, res
);
511 IDispatch_Release(obj
);
515 stack_popn(ctx
, arg_cnt
);
519 static HRESULT
interp_mcall(exec_ctx_t
*ctx
)
526 hres
= do_mcall(ctx
, &res
);
530 return stack_push(ctx
, &res
);
533 static HRESULT
interp_mcallv(exec_ctx_t
*ctx
)
537 return do_mcall(ctx
, NULL
);
540 static HRESULT
assign_ident(exec_ctx_t
*ctx
, BSTR name
, VARIANT
*val
, BOOL own_val
)
545 hres
= lookup_identifier(ctx
, name
, VBDISP_LET
, &ref
);
551 VARIANT
*v
= ref
.u
.v
;
553 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
))
561 hres
= VariantCopy(v
, val
);
566 hres
= disp_propput(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, val
);
571 FIXME("functions not implemented\n");
577 FIXME("REF_CONST\n");
580 if(ctx
->func
->code_ctx
->option_explicit
) {
581 FIXME("throw exception\n");
584 TRACE("creating variable %s\n", debugstr_w(name
));
585 hres
= add_dynamic_var(ctx
, name
, FALSE
, val
, own_val
);
592 static HRESULT
interp_assign_ident(exec_ctx_t
*ctx
)
594 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
595 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
599 TRACE("%s\n", debugstr_w(arg
));
602 FIXME("arguments not supported\n");
606 hres
= stack_pop_val(ctx
, &v
);
610 return assign_ident(ctx
, arg
, v
.v
, v
.owned
);
613 static HRESULT
interp_set_ident(exec_ctx_t
*ctx
)
615 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
616 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
621 TRACE("%s\n", debugstr_w(arg
));
624 FIXME("arguments not supported\n");
628 hres
= stack_pop_disp(ctx
, &disp
);
632 V_VT(&v
) = VT_DISPATCH
;
633 V_DISPATCH(&v
) = disp
;
634 return assign_ident(ctx
, ctx
->instr
->arg1
.bstr
, &v
, TRUE
);
637 static HRESULT
interp_assign_member(exec_ctx_t
*ctx
)
639 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
640 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
646 TRACE("%s\n", debugstr_w(identifier
));
649 FIXME("arguments not supported\n");
653 hres
= stack_assume_disp(ctx
, arg_cnt
+1, &obj
);
662 hres
= stack_pop_val(ctx
, &val
);
666 hres
= disp_get_id(obj
, identifier
, VBDISP_LET
, FALSE
, &id
);
668 hres
= disp_propput(ctx
->script
, obj
, id
, val
.v
);
677 static HRESULT
interp_set_member(exec_ctx_t
*ctx
)
679 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
680 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
685 TRACE("%s\n", debugstr_w(identifier
));
688 FIXME("arguments not supported\n");
692 hres
= stack_assume_disp(ctx
, 1, &obj
);
701 hres
= stack_assume_disp(ctx
, 0, NULL
);
705 hres
= disp_get_id(obj
, identifier
, VBDISP_SET
, FALSE
, &id
);
707 hres
= disp_propput(ctx
->script
, obj
, id
, stack_top(ctx
, 0));
715 static HRESULT
interp_const(exec_ctx_t
*ctx
)
717 BSTR arg
= ctx
->instr
->arg1
.bstr
;
722 TRACE("%s\n", debugstr_w(arg
));
724 assert(ctx
->func
->type
== FUNC_GLOBAL
);
726 hres
= lookup_identifier(ctx
, arg
, VBDISP_CALLGET
, &ref
);
730 if(ref
.type
!= REF_NONE
) {
731 FIXME("%s already defined\n", debugstr_w(arg
));
735 hres
= stack_pop_val(ctx
, &val
);
739 return add_dynamic_var(ctx
, arg
, TRUE
, val
.v
, val
.owned
);
742 static HRESULT
interp_val(exec_ctx_t
*ctx
)
750 hres
= stack_pop_val(ctx
, &val
);
756 hres
= VariantCopy(&v
, val
.v
);
761 return stack_push(ctx
, val
.owned
? val
.v
: &v
);
764 static HRESULT
interp_pop(exec_ctx_t
*ctx
)
766 const unsigned n
= ctx
->instr
->arg1
.uint
;
774 static HRESULT
interp_new(exec_ctx_t
*ctx
)
776 const WCHAR
*arg
= ctx
->instr
->arg1
.bstr
;
777 class_desc_t
*class_desc
;
782 TRACE("%s\n", debugstr_w(arg
));
784 for(class_desc
= ctx
->script
->classes
; class_desc
; class_desc
= class_desc
->next
) {
785 if(!strcmpiW(class_desc
->name
, arg
))
789 FIXME("Class %s not found\n", debugstr_w(arg
));
793 hres
= create_vbdisp(class_desc
, &obj
);
797 V_VT(&v
) = VT_DISPATCH
;
798 V_DISPATCH(&v
) = (IDispatch
*)&obj
->IDispatchEx_iface
;
799 return stack_push(ctx
, &v
);
802 static HRESULT
interp_step(exec_ctx_t
*ctx
)
804 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
810 TRACE("%s\n", debugstr_w(ident
));
814 hres
= VarCmp(stack_top(ctx
, 0), &zero
, ctx
->script
->lcid
, 0);
818 gteq_zero
= hres
== VARCMP_GT
|| hres
== VARCMP_EQ
;
820 hres
= lookup_identifier(ctx
, ident
, VBDISP_ANY
, &ref
);
824 if(ref
.type
!= REF_VAR
) {
825 FIXME("%s is not REF_VAR\n", debugstr_w(ident
));
829 hres
= VarCmp(ref
.u
.v
, stack_top(ctx
, 1), ctx
->script
->lcid
, 0);
833 if(hres
== VARCMP_EQ
|| hres
== (gteq_zero
? VARCMP_LT
: VARCMP_GT
))
836 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
840 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
842 const unsigned arg
= ctx
->instr
->arg1
.uint
;
850 static HRESULT
interp_jmp_false(exec_ctx_t
*ctx
)
852 const unsigned arg
= ctx
->instr
->arg1
.uint
;
858 hres
= stack_pop_val(ctx
, &val
);
862 if(V_VT(val
.v
) != VT_BOOL
) {
863 FIXME("unsupported for %s\n", debugstr_variant(val
.v
));
871 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
875 static HRESULT
interp_jmp_true(exec_ctx_t
*ctx
)
877 const unsigned arg
= ctx
->instr
->arg1
.uint
;
883 hres
= stack_pop_val(ctx
, &val
);
887 if(V_VT(val
.v
) != VT_BOOL
) {
888 FIXME("unsupported for %s\n", debugstr_variant(val
.v
));
894 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
900 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
908 static HRESULT
interp_stop(exec_ctx_t
*ctx
)
912 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
916 static HRESULT
interp_me(exec_ctx_t
*ctx
)
922 IDispatch_AddRef(ctx
->this_obj
);
923 V_VT(&v
) = VT_DISPATCH
;
924 V_DISPATCH(&v
) = ctx
->this_obj
;
925 return stack_push(ctx
, &v
);
928 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
930 const VARIANT_BOOL arg
= ctx
->instr
->arg1
.lng
;
933 TRACE("%s\n", arg
? "true" : "false");
937 return stack_push(ctx
, &v
);
940 static HRESULT
interp_errmode(exec_ctx_t
*ctx
)
942 const int err_mode
= ctx
->instr
->arg1
.uint
;
944 TRACE("%d\n", err_mode
);
946 ctx
->resume_next
= err_mode
;
950 static HRESULT
interp_string(exec_ctx_t
*ctx
)
957 V_BSTR(&v
) = SysAllocString(ctx
->instr
->arg1
.str
);
959 return E_OUTOFMEMORY
;
961 return stack_push(ctx
, &v
);
964 static HRESULT
interp_long(exec_ctx_t
*ctx
)
966 const LONG arg
= ctx
->instr
->arg1
.lng
;
973 return stack_push(ctx
, &v
);
976 static HRESULT
interp_short(exec_ctx_t
*ctx
)
978 const LONG arg
= ctx
->instr
->arg1
.lng
;
985 return stack_push(ctx
, &v
);
988 static HRESULT
interp_double(exec_ctx_t
*ctx
)
990 const DOUBLE
*arg
= ctx
->instr
->arg1
.dbl
;
993 TRACE("%lf\n", *arg
);
997 return stack_push(ctx
, &v
);
1000 static HRESULT
interp_empty(exec_ctx_t
*ctx
)
1006 V_VT(&v
) = VT_EMPTY
;
1007 return stack_push(ctx
, &v
);
1010 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1017 return stack_push(ctx
, &v
);
1020 static HRESULT
interp_nothing(exec_ctx_t
*ctx
)
1026 V_VT(&v
) = VT_DISPATCH
;
1027 V_DISPATCH(&v
) = NULL
;
1028 return stack_push(ctx
, &v
);
1031 static HRESULT
interp_not(exec_ctx_t
*ctx
)
1039 hres
= stack_pop_val(ctx
, &val
);
1043 hres
= VarNot(val
.v
, &v
);
1048 return stack_push(ctx
, &v
);
1051 static HRESULT
interp_and(exec_ctx_t
*ctx
)
1059 hres
= stack_pop_val(ctx
, &r
);
1063 hres
= stack_pop_val(ctx
, &l
);
1064 if(SUCCEEDED(hres
)) {
1065 hres
= VarAnd(l
.v
, r
.v
, &v
);
1072 return stack_push(ctx
, &v
);
1075 static HRESULT
interp_or(exec_ctx_t
*ctx
)
1083 hres
= stack_pop_val(ctx
, &r
);
1087 hres
= stack_pop_val(ctx
, &l
);
1088 if(SUCCEEDED(hres
)) {
1089 hres
= VarOr(l
.v
, r
.v
, &v
);
1096 return stack_push(ctx
, &v
);
1099 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
1107 hres
= stack_pop_val(ctx
, &r
);
1111 hres
= stack_pop_val(ctx
, &l
);
1112 if(SUCCEEDED(hres
)) {
1113 hres
= VarXor(l
.v
, r
.v
, &v
);
1120 return stack_push(ctx
, &v
);
1123 static HRESULT
interp_eqv(exec_ctx_t
*ctx
)
1131 hres
= stack_pop_val(ctx
, &r
);
1135 hres
= stack_pop_val(ctx
, &l
);
1136 if(SUCCEEDED(hres
)) {
1137 hres
= VarEqv(l
.v
, r
.v
, &v
);
1144 return stack_push(ctx
, &v
);
1147 static HRESULT
interp_imp(exec_ctx_t
*ctx
)
1155 hres
= stack_pop_val(ctx
, &r
);
1159 hres
= stack_pop_val(ctx
, &l
);
1160 if(SUCCEEDED(hres
)) {
1161 hres
= VarImp(l
.v
, r
.v
, &v
);
1168 return stack_push(ctx
, &v
);
1171 static HRESULT
cmp_oper(exec_ctx_t
*ctx
)
1176 hres
= stack_pop_val(ctx
, &r
);
1180 hres
= stack_pop_val(ctx
, &l
);
1181 if(SUCCEEDED(hres
)) {
1182 if(V_VT(l
.v
) == VT_NULL
|| V_VT(r
.v
) == VT_NULL
) {
1183 FIXME("comparing nulls is not implemented\n");
1186 hres
= VarCmp(l
.v
, r
.v
, ctx
->script
->lcid
, 0);
1195 static HRESULT
interp_equal(exec_ctx_t
*ctx
)
1202 hres
= cmp_oper(ctx
);
1207 V_BOOL(&v
) = hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1208 return stack_push(ctx
, &v
);
1211 static HRESULT
interp_nequal(exec_ctx_t
*ctx
)
1218 hres
= cmp_oper(ctx
);
1223 V_BOOL(&v
) = hres
!= VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1224 return stack_push(ctx
, &v
);
1227 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
1234 hres
= cmp_oper(ctx
);
1239 V_BOOL(&v
) = hres
== VARCMP_GT
? VARIANT_TRUE
: VARIANT_FALSE
;
1240 return stack_push(ctx
, &v
);
1243 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
1250 hres
= cmp_oper(ctx
);
1255 V_BOOL(&v
) = hres
== VARCMP_GT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1256 return stack_push(ctx
, &v
);
1259 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
1266 hres
= cmp_oper(ctx
);
1271 V_BOOL(&v
) = hres
== VARCMP_LT
? VARIANT_TRUE
: VARIANT_FALSE
;
1272 return stack_push(ctx
, &v
);
1275 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
1282 hres
= cmp_oper(ctx
);
1287 V_BOOL(&v
) = hres
== VARCMP_LT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1288 return stack_push(ctx
, &v
);
1291 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, VARIANT_BOOL
*ret
)
1293 IObjectIdentity
*identity
;
1294 IUnknown
*unk1
, *unk2
;
1297 if(disp1
== disp2
) {
1298 *ret
= VARIANT_TRUE
;
1302 if(!disp1
|| !disp2
) {
1303 *ret
= VARIANT_FALSE
;
1307 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
1311 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
1313 IUnknown_Release(unk1
);
1318 *ret
= VARIANT_TRUE
;
1320 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
1321 if(SUCCEEDED(hres
)) {
1322 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
1323 IObjectIdentity_Release(identity
);
1324 *ret
= hres
== S_OK
? VARIANT_TRUE
: VARIANT_FALSE
;
1326 *ret
= VARIANT_FALSE
;
1330 IUnknown_Release(unk1
);
1331 IUnknown_Release(unk2
);
1335 static HRESULT
interp_is(exec_ctx_t
*ctx
)
1343 hres
= stack_pop_disp(ctx
, &r
);
1347 hres
= stack_pop_disp(ctx
, &l
);
1348 if(SUCCEEDED(hres
)) {
1350 hres
= disp_cmp(l
, r
, &V_BOOL(&v
));
1352 IDispatch_Release(l
);
1355 IDispatch_Release(r
);
1359 return stack_push(ctx
, &v
);
1362 static HRESULT
interp_concat(exec_ctx_t
*ctx
)
1370 hres
= stack_pop_val(ctx
, &r
);
1374 hres
= stack_pop_val(ctx
, &l
);
1375 if(SUCCEEDED(hres
)) {
1376 hres
= VarCat(l
.v
, r
.v
, &v
);
1383 return stack_push(ctx
, &v
);
1386 static HRESULT
interp_add(exec_ctx_t
*ctx
)
1394 hres
= stack_pop_val(ctx
, &r
);
1398 hres
= stack_pop_val(ctx
, &l
);
1399 if(SUCCEEDED(hres
)) {
1400 hres
= VarAdd(l
.v
, r
.v
, &v
);
1407 return stack_push(ctx
, &v
);
1410 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
1418 hres
= stack_pop_val(ctx
, &r
);
1422 hres
= stack_pop_val(ctx
, &l
);
1423 if(SUCCEEDED(hres
)) {
1424 hres
= VarSub(l
.v
, r
.v
, &v
);
1431 return stack_push(ctx
, &v
);
1434 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
1442 hres
= stack_pop_val(ctx
, &r
);
1446 hres
= stack_pop_val(ctx
, &l
);
1447 if(SUCCEEDED(hres
)) {
1448 hres
= VarMod(l
.v
, r
.v
, &v
);
1455 return stack_push(ctx
, &v
);
1458 static HRESULT
interp_idiv(exec_ctx_t
*ctx
)
1466 hres
= stack_pop_val(ctx
, &r
);
1470 hres
= stack_pop_val(ctx
, &l
);
1471 if(SUCCEEDED(hres
)) {
1472 hres
= VarIdiv(l
.v
, r
.v
, &v
);
1479 return stack_push(ctx
, &v
);
1482 static HRESULT
interp_div(exec_ctx_t
*ctx
)
1490 hres
= stack_pop_val(ctx
, &r
);
1494 hres
= stack_pop_val(ctx
, &l
);
1495 if(SUCCEEDED(hres
)) {
1496 hres
= VarDiv(l
.v
, r
.v
, &v
);
1503 return stack_push(ctx
, &v
);
1506 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
1514 hres
= stack_pop_val(ctx
, &r
);
1518 hres
= stack_pop_val(ctx
, &l
);
1519 if(SUCCEEDED(hres
)) {
1520 hres
= VarMul(l
.v
, r
.v
, &v
);
1527 return stack_push(ctx
, &v
);
1530 static HRESULT
interp_exp(exec_ctx_t
*ctx
)
1538 hres
= stack_pop_val(ctx
, &r
);
1542 hres
= stack_pop_val(ctx
, &l
);
1543 if(SUCCEEDED(hres
)) {
1544 hres
= VarPow(l
.v
, r
.v
, &v
);
1551 return stack_push(ctx
, &v
);
1554 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
1560 hres
= stack_pop_val(ctx
, &val
);
1564 hres
= VarNeg(val
.v
, &v
);
1569 return stack_push(ctx
, &v
);
1572 static HRESULT
interp_incc(exec_ctx_t
*ctx
)
1574 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
1581 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1585 if(ref
.type
!= REF_VAR
) {
1586 FIXME("ref.type is not REF_VAR\n");
1590 hres
= VarAdd(stack_top(ctx
, 0), ref
.u
.v
, &v
);
1594 VariantClear(ref
.u
.v
);
1599 static const instr_func_t op_funcs
[] = {
1600 #define X(x,n,a,b) interp_ ## x,
1605 static const unsigned op_move
[] = {
1606 #define X(x,n,a,b) n,
1611 void release_dynamic_vars(dynamic_var_t
*var
)
1614 VariantClear(&var
->v
);
1619 static void release_exec(exec_ctx_t
*ctx
)
1623 VariantClear(&ctx
->ret_val
);
1624 release_dynamic_vars(ctx
->dynamic_vars
);
1627 IDispatch_Release(ctx
->this_obj
);
1630 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++)
1631 VariantClear(ctx
->args
+i
);
1635 for(i
=0; i
< ctx
->func
->var_cnt
; i
++)
1636 VariantClear(ctx
->vars
+i
);
1639 vbsheap_free(&ctx
->heap
);
1640 heap_free(ctx
->args
);
1641 heap_free(ctx
->vars
);
1642 heap_free(ctx
->stack
);
1645 HRESULT
exec_script(script_ctx_t
*ctx
, function_t
*func
, IDispatch
*this_obj
, DISPPARAMS
*dp
, VARIANT
*res
)
1647 exec_ctx_t exec
= {func
->code_ctx
};
1649 HRESULT hres
= S_OK
;
1651 exec
.code
= func
->code_ctx
;
1653 if(dp
? func
->arg_cnt
!= arg_cnt(dp
) : func
->arg_cnt
) {
1654 FIXME("wrong arg_cnt %d, expected %d\n", dp
? arg_cnt(dp
) : 0, func
->arg_cnt
);
1658 vbsheap_init(&exec
.heap
);
1664 exec
.args
= heap_alloc_zero(func
->arg_cnt
* sizeof(VARIANT
));
1666 release_exec(&exec
);
1667 return E_OUTOFMEMORY
;
1670 for(i
=0; i
< func
->arg_cnt
; i
++) {
1672 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
)) {
1673 if(func
->args
[i
].by_ref
)
1676 hres
= VariantCopy(exec
.args
+i
, V_VARIANTREF(v
));
1678 hres
= VariantCopy(exec
.args
+i
, v
);
1681 release_exec(&exec
);
1690 exec
.vars
= heap_alloc_zero(func
->var_cnt
* sizeof(VARIANT
));
1692 release_exec(&exec
);
1693 return E_OUTOFMEMORY
;
1699 exec
.stack_size
= 16;
1701 exec
.stack
= heap_alloc(exec
.stack_size
* sizeof(VARIANT
));
1703 release_exec(&exec
);
1704 return E_OUTOFMEMORY
;
1708 exec
.this_obj
= this_obj
;
1709 else if (ctx
->host_global
)
1710 exec
.this_obj
= ctx
->host_global
;
1712 exec
.this_obj
= (IDispatch
*)&ctx
->script_obj
->IDispatchEx_iface
;
1713 IDispatch_AddRef(exec
.this_obj
);
1715 exec
.instr
= exec
.code
->instrs
+ func
->code_off
;
1720 op
= exec
.instr
->op
;
1721 hres
= op_funcs
[op
](&exec
);
1723 if(exec
.resume_next
)
1724 FIXME("Failed %08x in resume next mode\n", hres
);
1726 WARN("Failed %08x\n", hres
);
1727 stack_popn(&exec
, exec
.top
);
1731 exec
.instr
+= op_move
[op
];
1735 if(func
->type
!= FUNC_FUNCTION
&& func
->type
!= FUNC_PROPGET
&& func
->type
!= FUNC_DEFGET
)
1736 assert(V_VT(&exec
.ret_val
) == VT_EMPTY
);
1738 if(SUCCEEDED(hres
) && res
) {
1739 *res
= exec
.ret_val
;
1740 V_VT(&exec
.ret_val
) = VT_EMPTY
;
1743 release_exec(&exec
);