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
21 static DISPID propput_dispid
= DISPID_PROPERTYPUT
;
33 dynamic_var_t
*dynamic_vars
;
45 typedef HRESULT (*instr_func_t
)(exec_ctx_t
*);
75 static BOOL
lookup_dynamic_vars(dynamic_var_t
*var
, const WCHAR
*name
, ref_t
*ref
)
78 if(!strcmpiW(var
->name
, name
)) {
79 ref
->type
= var
->is_const
? REF_CONST
: REF_VAR
;
90 static HRESULT
lookup_identifier(exec_ctx_t
*ctx
, BSTR name
, vbdisp_invoke_type_t invoke_type
, ref_t
*ref
)
98 static const WCHAR errW
[] = {'e','r','r',0};
100 if(invoke_type
== VBDISP_LET
101 && (ctx
->func
->type
== FUNC_FUNCTION
|| ctx
->func
->type
== FUNC_PROPGET
|| ctx
->func
->type
== FUNC_DEFGET
)
102 && !strcmpiW(name
, ctx
->func
->name
)) {
104 ref
->u
.v
= &ctx
->ret_val
;
108 for(i
=0; i
< ctx
->func
->var_cnt
; i
++) {
109 if(!strcmpiW(ctx
->func
->vars
[i
].name
, name
)) {
111 ref
->u
.v
= ctx
->vars
+i
;
116 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++) {
117 if(!strcmpiW(ctx
->func
->args
[i
].name
, name
)) {
119 ref
->u
.v
= ctx
->args
+i
;
124 if(lookup_dynamic_vars(ctx
->func
->type
== FUNC_GLOBAL
? ctx
->script
->global_vars
: ctx
->dynamic_vars
, name
, ref
))
127 if(ctx
->func
->type
!= FUNC_GLOBAL
) {
128 hres
= disp_get_id(ctx
->this_obj
, name
, invoke_type
, TRUE
, &id
);
129 if(SUCCEEDED(hres
)) {
130 ref
->type
= REF_DISP
;
131 ref
->u
.d
.disp
= ctx
->this_obj
;
137 if(ctx
->func
->type
!= FUNC_GLOBAL
&& lookup_dynamic_vars(ctx
->script
->global_vars
, name
, ref
))
140 for(func
= ctx
->script
->global_funcs
; func
; func
= func
->next
) {
141 if(!strcmpiW(func
->name
, name
)) {
142 ref
->type
= REF_FUNC
;
148 if(!strcmpiW(name
, errW
)) {
150 ref
->u
.obj
= (IDispatch
*)&ctx
->script
->err_obj
->IDispatchEx_iface
;
154 hres
= vbdisp_get_id(ctx
->script
->global_obj
, name
, invoke_type
, TRUE
, &id
);
155 if(SUCCEEDED(hres
)) {
156 ref
->type
= REF_DISP
;
157 ref
->u
.d
.disp
= (IDispatch
*)&ctx
->script
->global_obj
->IDispatchEx_iface
;
162 LIST_FOR_EACH_ENTRY(item
, &ctx
->script
->named_items
, named_item_t
, entry
) {
163 if((item
->flags
& SCRIPTITEM_ISVISIBLE
) && !strcmpiW(item
->name
, name
)) {
167 hres
= IActiveScriptSite_GetItemInfo(ctx
->script
->site
, name
, SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
169 WARN("GetItemInfo failed: %08x\n", hres
);
173 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
174 IUnknown_Release(unk
);
176 WARN("object does not implement IDispatch\n");
182 ref
->u
.obj
= item
->disp
;
187 LIST_FOR_EACH_ENTRY(item
, &ctx
->script
->named_items
, named_item_t
, entry
) {
188 if((item
->flags
& SCRIPTITEM_GLOBALMEMBERS
)) {
189 hres
= disp_get_id(item
->disp
, name
, invoke_type
, FALSE
, &id
);
190 if(SUCCEEDED(hres
)) {
191 ref
->type
= REF_DISP
;
192 ref
->u
.d
.disp
= item
->disp
;
199 ref
->type
= REF_NONE
;
203 static HRESULT
add_dynamic_var(exec_ctx_t
*ctx
, const WCHAR
*name
,
204 BOOL is_const
, VARIANT
*val
, BOOL own_val
, VARIANT
**out_var
)
206 dynamic_var_t
*new_var
;
212 heap
= ctx
->func
->type
== FUNC_GLOBAL
? &ctx
->script
->heap
: &ctx
->heap
;
214 new_var
= heap_pool_alloc(heap
, sizeof(*new_var
));
216 return E_OUTOFMEMORY
;
218 size
= (strlenW(name
)+1)*sizeof(WCHAR
);
219 str
= heap_pool_alloc(heap
, size
);
221 return E_OUTOFMEMORY
;
222 memcpy(str
, name
, size
);
224 new_var
->is_const
= is_const
;
229 V_VT(&new_var
->v
) = VT_EMPTY
;
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
;
244 *out_var
= &new_var
->v
;
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 inline HRESULT
stack_push_null(exec_ctx_t
*ctx
)
284 return stack_push(ctx
, &v
);
287 static void stack_popn(exec_ctx_t
*ctx
, unsigned n
)
290 VariantClear(stack_pop(ctx
));
293 static HRESULT
stack_pop_val(exec_ctx_t
*ctx
, variant_val_t
*v
)
297 var
= stack_pop(ctx
);
299 if(V_VT(var
) == (VT_BYREF
|VT_VARIANT
)) {
301 var
= V_VARIANTREF(var
);
306 if(V_VT(var
) == VT_DISPATCH
) {
310 hres
= disp_call(ctx
->script
, V_DISPATCH(var
), DISPID_VALUE
, &dp
, &v
->store
);
312 IDispatch_Release(V_DISPATCH(var
));
325 static HRESULT
stack_assume_val(exec_ctx_t
*ctx
, unsigned n
)
327 VARIANT
*v
= stack_top(ctx
, n
);
330 if(V_VT(v
) == (VT_BYREF
|VT_VARIANT
)) {
331 VARIANT
*ref
= V_VARIANTREF(v
);
334 hres
= VariantCopy(v
, ref
);
339 if(V_VT(v
) == VT_DISPATCH
) {
343 disp
= V_DISPATCH(v
);
345 hres
= disp_call(ctx
->script
, disp
, DISPID_VALUE
, &dp
, v
);
346 IDispatch_Release(disp
);
354 static inline void release_val(variant_val_t
*v
)
360 static int stack_pop_bool(exec_ctx_t
*ctx
, BOOL
*b
)
365 hres
= stack_pop_val(ctx
, &val
);
384 FIXME("unsupported for %s\n", debugstr_variant(val
.v
));
391 static HRESULT
stack_pop_disp(exec_ctx_t
*ctx
, IDispatch
**ret
)
393 VARIANT
*v
= stack_pop(ctx
);
395 if(V_VT(v
) == VT_DISPATCH
) {
396 *ret
= V_DISPATCH(v
);
400 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
401 FIXME("not supported type: %s\n", debugstr_variant(v
));
407 if(V_VT(v
) != VT_DISPATCH
) {
408 FIXME("not disp %s\n", debugstr_variant(v
));
413 IDispatch_AddRef(V_DISPATCH(v
));
414 *ret
= V_DISPATCH(v
);
418 static HRESULT
stack_assume_disp(exec_ctx_t
*ctx
, unsigned n
, IDispatch
**disp
)
420 VARIANT
*v
= stack_top(ctx
, n
), *ref
;
422 if(V_VT(v
) != VT_DISPATCH
) {
423 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
424 FIXME("not supported type: %s\n", debugstr_variant(v
));
428 ref
= V_VARIANTREF(v
);
429 if(V_VT(ref
) != VT_DISPATCH
) {
430 FIXME("not disp %s\n", debugstr_variant(ref
));
434 V_VT(v
) = VT_DISPATCH
;
435 V_DISPATCH(v
) = V_DISPATCH(ref
);
437 IDispatch_AddRef(V_DISPATCH(v
));
441 *disp
= V_DISPATCH(v
);
445 static inline void instr_jmp(exec_ctx_t
*ctx
, unsigned addr
)
447 ctx
->instr
= ctx
->code
->instrs
+ addr
;
450 static void vbstack_to_dp(exec_ctx_t
*ctx
, unsigned arg_cnt
, BOOL is_propput
, DISPPARAMS
*dp
)
452 dp
->cNamedArgs
= is_propput
? 1 : 0;
453 dp
->cArgs
= arg_cnt
+ dp
->cNamedArgs
;
454 dp
->rgdispidNamedArgs
= is_propput
? &propput_dispid
: NULL
;
460 assert(ctx
->top
>= arg_cnt
);
462 for(i
=1; i
*2 <= arg_cnt
; i
++) {
463 tmp
= ctx
->stack
[ctx
->top
-i
];
464 ctx
->stack
[ctx
->top
-i
] = ctx
->stack
[ctx
->top
-arg_cnt
+i
-1];
465 ctx
->stack
[ctx
->top
-arg_cnt
+i
-1] = tmp
;
468 dp
->rgvarg
= ctx
->stack
+ ctx
->top
-dp
->cArgs
;
470 dp
->rgvarg
= is_propput
? ctx
->stack
+ctx
->top
-1 : NULL
;
474 static HRESULT
do_icall(exec_ctx_t
*ctx
, VARIANT
*res
)
476 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
477 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
482 hres
= lookup_identifier(ctx
, identifier
, VBDISP_CALLGET
, &ref
);
486 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
492 FIXME("REF_VAR no res\n");
497 FIXME("arguments not implemented\n");
501 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
502 V_BYREF(res
) = V_VT(ref
.u
.v
) == (VT_VARIANT
|VT_BYREF
) ? V_VARIANTREF(ref
.u
.v
) : ref
.u
.v
;
505 hres
= disp_call(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, &dp
, res
);
510 hres
= exec_script(ctx
->script
, ref
.u
.f
, NULL
, &dp
, res
);
516 FIXME("arguments on object\n");
521 IDispatch_AddRef(ref
.u
.obj
);
522 V_VT(res
) = VT_DISPATCH
;
523 V_DISPATCH(res
) = ref
.u
.obj
;
527 if(res
&& !ctx
->func
->code_ctx
->option_explicit
&& arg_cnt
== 0) {
530 hres
= add_dynamic_var(ctx
, identifier
, FALSE
, &v
, FALSE
, &new);
533 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
537 FIXME("%s not found\n", debugstr_w(identifier
));
538 return DISP_E_UNKNOWNNAME
;
541 stack_popn(ctx
, arg_cnt
);
545 static HRESULT
interp_icall(exec_ctx_t
*ctx
)
552 hres
= do_icall(ctx
, &v
);
556 return stack_push(ctx
, &v
);
559 static HRESULT
interp_icallv(exec_ctx_t
*ctx
)
562 return do_icall(ctx
, NULL
);
565 static HRESULT
do_mcall(exec_ctx_t
*ctx
, VARIANT
*res
)
567 const BSTR identifier
= ctx
->instr
->arg1
.bstr
;
568 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
574 hres
= stack_pop_disp(ctx
, &obj
);
583 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
585 hres
= disp_get_id(obj
, identifier
, VBDISP_CALLGET
, FALSE
, &id
);
587 hres
= disp_call(ctx
->script
, obj
, id
, &dp
, res
);
588 IDispatch_Release(obj
);
592 stack_popn(ctx
, arg_cnt
);
596 static HRESULT
interp_mcall(exec_ctx_t
*ctx
)
603 hres
= do_mcall(ctx
, &res
);
607 return stack_push(ctx
, &res
);
610 static HRESULT
interp_mcallv(exec_ctx_t
*ctx
)
614 return do_mcall(ctx
, NULL
);
617 static HRESULT
assign_ident(exec_ctx_t
*ctx
, BSTR name
, DISPPARAMS
*dp
)
622 hres
= lookup_identifier(ctx
, name
, VBDISP_LET
, &ref
);
628 VARIANT
*v
= ref
.u
.v
;
631 FIXME("arg_cnt %d not supported\n", arg_cnt(dp
));
635 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
))
638 hres
= VariantCopy(v
, dp
->rgvarg
);
642 hres
= disp_propput(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, dp
);
645 FIXME("functions not implemented\n");
651 FIXME("REF_CONST\n");
654 if(ctx
->func
->code_ctx
->option_explicit
) {
655 FIXME("throw exception\n");
659 FIXME("arg_cnt %d not supported\n", arg_cnt(dp
));
663 TRACE("creating variable %s\n", debugstr_w(name
));
664 hres
= add_dynamic_var(ctx
, name
, FALSE
, dp
->rgvarg
, FALSE
, NULL
);
671 static HRESULT
interp_assign_ident(exec_ctx_t
*ctx
)
673 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
674 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
678 TRACE("%s\n", debugstr_w(arg
));
680 hres
= stack_assume_val(ctx
, arg_cnt
);
684 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
685 hres
= assign_ident(ctx
, arg
, &dp
);
689 stack_popn(ctx
, arg_cnt
+1);
693 static HRESULT
interp_set_ident(exec_ctx_t
*ctx
)
695 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
696 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
700 TRACE("%s\n", debugstr_w(arg
));
703 FIXME("arguments not supported\n");
707 hres
= stack_assume_disp(ctx
, 0, NULL
);
711 vbstack_to_dp(ctx
, 0, TRUE
, &dp
);
712 hres
= assign_ident(ctx
, ctx
->instr
->arg1
.bstr
, &dp
);
720 static HRESULT
interp_assign_member(exec_ctx_t
*ctx
)
722 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
723 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
729 TRACE("%s\n", debugstr_w(identifier
));
731 hres
= stack_assume_disp(ctx
, arg_cnt
+1, &obj
);
740 hres
= stack_assume_val(ctx
, arg_cnt
);
744 hres
= disp_get_id(obj
, identifier
, VBDISP_LET
, FALSE
, &id
);
745 if(SUCCEEDED(hres
)) {
746 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
747 hres
= disp_propput(ctx
->script
, obj
, id
, &dp
);
752 stack_popn(ctx
, arg_cnt
+2);
756 static HRESULT
interp_set_member(exec_ctx_t
*ctx
)
758 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
759 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
765 TRACE("%s\n", debugstr_w(identifier
));
768 FIXME("arguments not supported\n");
772 hres
= stack_assume_disp(ctx
, 1, &obj
);
781 hres
= stack_assume_disp(ctx
, 0, NULL
);
785 hres
= disp_get_id(obj
, identifier
, VBDISP_SET
, FALSE
, &id
);
786 if(SUCCEEDED(hres
)) {
787 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
788 hres
= disp_propput(ctx
->script
, obj
, id
, &dp
);
797 static HRESULT
interp_const(exec_ctx_t
*ctx
)
799 BSTR arg
= ctx
->instr
->arg1
.bstr
;
804 TRACE("%s\n", debugstr_w(arg
));
806 assert(ctx
->func
->type
== FUNC_GLOBAL
);
808 hres
= lookup_identifier(ctx
, arg
, VBDISP_CALLGET
, &ref
);
812 if(ref
.type
!= REF_NONE
) {
813 FIXME("%s already defined\n", debugstr_w(arg
));
817 hres
= stack_pop_val(ctx
, &val
);
821 return add_dynamic_var(ctx
, arg
, TRUE
, val
.v
, val
.owned
, NULL
);
824 static HRESULT
interp_val(exec_ctx_t
*ctx
)
832 hres
= stack_pop_val(ctx
, &val
);
838 hres
= VariantCopy(&v
, val
.v
);
843 return stack_push(ctx
, val
.owned
? val
.v
: &v
);
846 static HRESULT
interp_pop(exec_ctx_t
*ctx
)
848 const unsigned n
= ctx
->instr
->arg1
.uint
;
856 static HRESULT
interp_new(exec_ctx_t
*ctx
)
858 const WCHAR
*arg
= ctx
->instr
->arg1
.bstr
;
859 class_desc_t
*class_desc
;
864 TRACE("%s\n", debugstr_w(arg
));
866 for(class_desc
= ctx
->script
->classes
; class_desc
; class_desc
= class_desc
->next
) {
867 if(!strcmpiW(class_desc
->name
, arg
))
871 FIXME("Class %s not found\n", debugstr_w(arg
));
875 hres
= create_vbdisp(class_desc
, &obj
);
879 V_VT(&v
) = VT_DISPATCH
;
880 V_DISPATCH(&v
) = (IDispatch
*)&obj
->IDispatchEx_iface
;
881 return stack_push(ctx
, &v
);
884 static HRESULT
interp_step(exec_ctx_t
*ctx
)
886 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
892 TRACE("%s\n", debugstr_w(ident
));
896 hres
= VarCmp(stack_top(ctx
, 0), &zero
, ctx
->script
->lcid
, 0);
900 gteq_zero
= hres
== VARCMP_GT
|| hres
== VARCMP_EQ
;
902 hres
= lookup_identifier(ctx
, ident
, VBDISP_ANY
, &ref
);
906 if(ref
.type
!= REF_VAR
) {
907 FIXME("%s is not REF_VAR\n", debugstr_w(ident
));
911 hres
= VarCmp(ref
.u
.v
, stack_top(ctx
, 1), ctx
->script
->lcid
, 0);
915 if(hres
== VARCMP_EQ
|| hres
== (gteq_zero
? VARCMP_LT
: VARCMP_GT
)) {
919 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
924 static HRESULT
interp_newenum(exec_ctx_t
*ctx
)
938 hres
= disp_call(ctx
->script
, V_DISPATCH(v
), DISPID_NEWENUM
, &dp
, &iterv
);
943 if(V_VT(&iterv
) != VT_UNKNOWN
&& V_VT(&iterv
) != VT_DISPATCH
) {
944 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv
));
945 VariantClear(&iterv
);
949 hres
= IUnknown_QueryInterface(V_UNKNOWN(&iterv
), &IID_IEnumVARIANT
, (void**)&iter
);
950 IUnknown_Release(V_UNKNOWN(&iterv
));
952 FIXME("Could not get IEnumVARIANT iface: %08x\n", hres
);
956 V_VT(&r
) = VT_UNKNOWN
;
957 V_UNKNOWN(&r
) = (IUnknown
*)iter
;
961 FIXME("Unsupported for %s\n", debugstr_variant(v
));
966 return stack_push(ctx
, &r
);
969 static HRESULT
interp_enumnext(exec_ctx_t
*ctx
)
971 const unsigned loop_end
= ctx
->instr
->arg1
.uint
;
972 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
974 DISPPARAMS dp
= {&v
, &propput_dispid
, 1, 1};
981 assert(V_VT(stack_top(ctx
, 0)) == VT_UNKNOWN
);
982 iter
= (IEnumVARIANT
*)V_UNKNOWN(stack_top(ctx
, 0));
985 hres
= IEnumVARIANT_Next(iter
, 1, &v
, NULL
);
989 do_continue
= hres
== S_OK
;
990 hres
= assign_ident(ctx
, ident
, &dp
);
999 instr_jmp(ctx
, loop_end
);
1004 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
1006 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1010 instr_jmp(ctx
, arg
);
1014 static HRESULT
interp_jmp_false(exec_ctx_t
*ctx
)
1016 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1022 hres
= stack_pop_bool(ctx
, &b
);
1029 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1033 static HRESULT
interp_jmp_true(exec_ctx_t
*ctx
)
1035 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1041 hres
= stack_pop_bool(ctx
, &b
);
1046 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1052 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
1060 static HRESULT
interp_stop(exec_ctx_t
*ctx
)
1064 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1068 static HRESULT
interp_me(exec_ctx_t
*ctx
)
1074 IDispatch_AddRef(ctx
->this_obj
);
1075 V_VT(&v
) = VT_DISPATCH
;
1076 V_DISPATCH(&v
) = ctx
->this_obj
;
1077 return stack_push(ctx
, &v
);
1080 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
1082 const VARIANT_BOOL arg
= ctx
->instr
->arg1
.lng
;
1085 TRACE("%s\n", arg
? "true" : "false");
1089 return stack_push(ctx
, &v
);
1092 static HRESULT
interp_errmode(exec_ctx_t
*ctx
)
1094 const int err_mode
= ctx
->instr
->arg1
.uint
;
1096 TRACE("%d\n", err_mode
);
1098 ctx
->resume_next
= err_mode
;
1102 static HRESULT
interp_string(exec_ctx_t
*ctx
)
1109 V_BSTR(&v
) = SysAllocString(ctx
->instr
->arg1
.str
);
1111 return E_OUTOFMEMORY
;
1113 return stack_push(ctx
, &v
);
1116 static HRESULT
interp_long(exec_ctx_t
*ctx
)
1118 const LONG arg
= ctx
->instr
->arg1
.lng
;
1125 return stack_push(ctx
, &v
);
1128 static HRESULT
interp_short(exec_ctx_t
*ctx
)
1130 const LONG arg
= ctx
->instr
->arg1
.lng
;
1137 return stack_push(ctx
, &v
);
1140 static HRESULT
interp_double(exec_ctx_t
*ctx
)
1142 const DOUBLE
*arg
= ctx
->instr
->arg1
.dbl
;
1145 TRACE("%lf\n", *arg
);
1149 return stack_push(ctx
, &v
);
1152 static HRESULT
interp_empty(exec_ctx_t
*ctx
)
1158 V_VT(&v
) = VT_EMPTY
;
1159 return stack_push(ctx
, &v
);
1162 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1165 return stack_push_null(ctx
);
1168 static HRESULT
interp_nothing(exec_ctx_t
*ctx
)
1174 V_VT(&v
) = VT_DISPATCH
;
1175 V_DISPATCH(&v
) = NULL
;
1176 return stack_push(ctx
, &v
);
1179 static HRESULT
interp_not(exec_ctx_t
*ctx
)
1187 hres
= stack_pop_val(ctx
, &val
);
1191 hres
= VarNot(val
.v
, &v
);
1196 return stack_push(ctx
, &v
);
1199 static HRESULT
interp_and(exec_ctx_t
*ctx
)
1207 hres
= stack_pop_val(ctx
, &r
);
1211 hres
= stack_pop_val(ctx
, &l
);
1212 if(SUCCEEDED(hres
)) {
1213 hres
= VarAnd(l
.v
, r
.v
, &v
);
1220 return stack_push(ctx
, &v
);
1223 static HRESULT
interp_or(exec_ctx_t
*ctx
)
1231 hres
= stack_pop_val(ctx
, &r
);
1235 hres
= stack_pop_val(ctx
, &l
);
1236 if(SUCCEEDED(hres
)) {
1237 hres
= VarOr(l
.v
, r
.v
, &v
);
1244 return stack_push(ctx
, &v
);
1247 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
1255 hres
= stack_pop_val(ctx
, &r
);
1259 hres
= stack_pop_val(ctx
, &l
);
1260 if(SUCCEEDED(hres
)) {
1261 hres
= VarXor(l
.v
, r
.v
, &v
);
1268 return stack_push(ctx
, &v
);
1271 static HRESULT
interp_eqv(exec_ctx_t
*ctx
)
1279 hres
= stack_pop_val(ctx
, &r
);
1283 hres
= stack_pop_val(ctx
, &l
);
1284 if(SUCCEEDED(hres
)) {
1285 hres
= VarEqv(l
.v
, r
.v
, &v
);
1292 return stack_push(ctx
, &v
);
1295 static HRESULT
interp_imp(exec_ctx_t
*ctx
)
1303 hres
= stack_pop_val(ctx
, &r
);
1307 hres
= stack_pop_val(ctx
, &l
);
1308 if(SUCCEEDED(hres
)) {
1309 hres
= VarImp(l
.v
, r
.v
, &v
);
1316 return stack_push(ctx
, &v
);
1319 static HRESULT
var_cmp(exec_ctx_t
*ctx
, VARIANT
*l
, VARIANT
*r
)
1321 TRACE("%s %s\n", debugstr_variant(l
), debugstr_variant(r
));
1323 /* FIXME: Fix comparing string to number */
1325 return VarCmp(l
, r
, ctx
->script
->lcid
, 0);
1328 static HRESULT
cmp_oper(exec_ctx_t
*ctx
)
1333 hres
= stack_pop_val(ctx
, &r
);
1337 hres
= stack_pop_val(ctx
, &l
);
1338 if(SUCCEEDED(hres
)) {
1339 hres
= var_cmp(ctx
, l
.v
, r
.v
);
1347 static HRESULT
interp_equal(exec_ctx_t
*ctx
)
1354 hres
= cmp_oper(ctx
);
1357 if(hres
== VARCMP_NULL
)
1358 return stack_push_null(ctx
);
1361 V_BOOL(&v
) = hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1362 return stack_push(ctx
, &v
);
1365 static HRESULT
interp_nequal(exec_ctx_t
*ctx
)
1372 hres
= cmp_oper(ctx
);
1375 if(hres
== VARCMP_NULL
)
1376 return stack_push_null(ctx
);
1379 V_BOOL(&v
) = hres
!= VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1380 return stack_push(ctx
, &v
);
1383 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
1390 hres
= cmp_oper(ctx
);
1393 if(hres
== VARCMP_NULL
)
1394 return stack_push_null(ctx
);
1397 V_BOOL(&v
) = hres
== VARCMP_GT
? VARIANT_TRUE
: VARIANT_FALSE
;
1398 return stack_push(ctx
, &v
);
1401 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
1408 hres
= cmp_oper(ctx
);
1411 if(hres
== VARCMP_NULL
)
1412 return stack_push_null(ctx
);
1415 V_BOOL(&v
) = hres
== VARCMP_GT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1416 return stack_push(ctx
, &v
);
1419 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
1426 hres
= cmp_oper(ctx
);
1429 if(hres
== VARCMP_NULL
)
1430 return stack_push_null(ctx
);
1433 V_BOOL(&v
) = hres
== VARCMP_LT
? VARIANT_TRUE
: VARIANT_FALSE
;
1434 return stack_push(ctx
, &v
);
1437 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
1444 hres
= cmp_oper(ctx
);
1447 if(hres
== VARCMP_NULL
)
1448 return stack_push_null(ctx
);
1451 V_BOOL(&v
) = hres
== VARCMP_LT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1452 return stack_push(ctx
, &v
);
1455 static HRESULT
interp_case(exec_ctx_t
*ctx
)
1457 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1463 hres
= stack_pop_val(ctx
, &v
);
1467 hres
= var_cmp(ctx
, stack_top(ctx
, 0), v
.v
);
1472 if(hres
== VARCMP_EQ
) {
1474 instr_jmp(ctx
, arg
);
1482 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, VARIANT_BOOL
*ret
)
1484 IObjectIdentity
*identity
;
1485 IUnknown
*unk1
, *unk2
;
1488 if(disp1
== disp2
) {
1489 *ret
= VARIANT_TRUE
;
1493 if(!disp1
|| !disp2
) {
1494 *ret
= VARIANT_FALSE
;
1498 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
1502 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
1504 IUnknown_Release(unk1
);
1509 *ret
= VARIANT_TRUE
;
1511 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
1512 if(SUCCEEDED(hres
)) {
1513 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
1514 IObjectIdentity_Release(identity
);
1515 *ret
= hres
== S_OK
? VARIANT_TRUE
: VARIANT_FALSE
;
1517 *ret
= VARIANT_FALSE
;
1521 IUnknown_Release(unk1
);
1522 IUnknown_Release(unk2
);
1526 static HRESULT
interp_is(exec_ctx_t
*ctx
)
1534 hres
= stack_pop_disp(ctx
, &r
);
1538 hres
= stack_pop_disp(ctx
, &l
);
1539 if(SUCCEEDED(hres
)) {
1541 hres
= disp_cmp(l
, r
, &V_BOOL(&v
));
1543 IDispatch_Release(l
);
1546 IDispatch_Release(r
);
1550 return stack_push(ctx
, &v
);
1553 static HRESULT
interp_concat(exec_ctx_t
*ctx
)
1561 hres
= stack_pop_val(ctx
, &r
);
1565 hres
= stack_pop_val(ctx
, &l
);
1566 if(SUCCEEDED(hres
)) {
1567 hres
= VarCat(l
.v
, r
.v
, &v
);
1574 return stack_push(ctx
, &v
);
1577 static HRESULT
interp_add(exec_ctx_t
*ctx
)
1585 hres
= stack_pop_val(ctx
, &r
);
1589 hres
= stack_pop_val(ctx
, &l
);
1590 if(SUCCEEDED(hres
)) {
1591 hres
= VarAdd(l
.v
, r
.v
, &v
);
1598 return stack_push(ctx
, &v
);
1601 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
1609 hres
= stack_pop_val(ctx
, &r
);
1613 hres
= stack_pop_val(ctx
, &l
);
1614 if(SUCCEEDED(hres
)) {
1615 hres
= VarSub(l
.v
, r
.v
, &v
);
1622 return stack_push(ctx
, &v
);
1625 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
1633 hres
= stack_pop_val(ctx
, &r
);
1637 hres
= stack_pop_val(ctx
, &l
);
1638 if(SUCCEEDED(hres
)) {
1639 hres
= VarMod(l
.v
, r
.v
, &v
);
1646 return stack_push(ctx
, &v
);
1649 static HRESULT
interp_idiv(exec_ctx_t
*ctx
)
1657 hres
= stack_pop_val(ctx
, &r
);
1661 hres
= stack_pop_val(ctx
, &l
);
1662 if(SUCCEEDED(hres
)) {
1663 hres
= VarIdiv(l
.v
, r
.v
, &v
);
1670 return stack_push(ctx
, &v
);
1673 static HRESULT
interp_div(exec_ctx_t
*ctx
)
1681 hres
= stack_pop_val(ctx
, &r
);
1685 hres
= stack_pop_val(ctx
, &l
);
1686 if(SUCCEEDED(hres
)) {
1687 hres
= VarDiv(l
.v
, r
.v
, &v
);
1694 return stack_push(ctx
, &v
);
1697 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
1705 hres
= stack_pop_val(ctx
, &r
);
1709 hres
= stack_pop_val(ctx
, &l
);
1710 if(SUCCEEDED(hres
)) {
1711 hres
= VarMul(l
.v
, r
.v
, &v
);
1718 return stack_push(ctx
, &v
);
1721 static HRESULT
interp_exp(exec_ctx_t
*ctx
)
1729 hres
= stack_pop_val(ctx
, &r
);
1733 hres
= stack_pop_val(ctx
, &l
);
1734 if(SUCCEEDED(hres
)) {
1735 hres
= VarPow(l
.v
, r
.v
, &v
);
1742 return stack_push(ctx
, &v
);
1745 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
1751 hres
= stack_pop_val(ctx
, &val
);
1755 hres
= VarNeg(val
.v
, &v
);
1760 return stack_push(ctx
, &v
);
1763 static HRESULT
interp_incc(exec_ctx_t
*ctx
)
1765 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
1772 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1776 if(ref
.type
!= REF_VAR
) {
1777 FIXME("ref.type is not REF_VAR\n");
1781 hres
= VarAdd(stack_top(ctx
, 0), ref
.u
.v
, &v
);
1785 VariantClear(ref
.u
.v
);
1790 static const instr_func_t op_funcs
[] = {
1791 #define X(x,n,a,b) interp_ ## x,
1796 static const unsigned op_move
[] = {
1797 #define X(x,n,a,b) n,
1802 void release_dynamic_vars(dynamic_var_t
*var
)
1805 VariantClear(&var
->v
);
1810 static void release_exec(exec_ctx_t
*ctx
)
1814 VariantClear(&ctx
->ret_val
);
1815 release_dynamic_vars(ctx
->dynamic_vars
);
1818 IDispatch_Release(ctx
->this_obj
);
1821 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++)
1822 VariantClear(ctx
->args
+i
);
1826 for(i
=0; i
< ctx
->func
->var_cnt
; i
++)
1827 VariantClear(ctx
->vars
+i
);
1830 heap_pool_free(&ctx
->heap
);
1831 heap_free(ctx
->args
);
1832 heap_free(ctx
->vars
);
1833 heap_free(ctx
->stack
);
1836 HRESULT
exec_script(script_ctx_t
*ctx
, function_t
*func
, IDispatch
*this_obj
, DISPPARAMS
*dp
, VARIANT
*res
)
1838 exec_ctx_t exec
= {func
->code_ctx
};
1840 HRESULT hres
= S_OK
;
1842 exec
.code
= func
->code_ctx
;
1844 if(dp
? func
->arg_cnt
!= arg_cnt(dp
) : func
->arg_cnt
) {
1845 FIXME("wrong arg_cnt %d, expected %d\n", dp
? arg_cnt(dp
) : 0, func
->arg_cnt
);
1849 heap_pool_init(&exec
.heap
);
1855 exec
.args
= heap_alloc_zero(func
->arg_cnt
* sizeof(VARIANT
));
1857 release_exec(&exec
);
1858 return E_OUTOFMEMORY
;
1861 for(i
=0; i
< func
->arg_cnt
; i
++) {
1863 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
)) {
1864 if(func
->args
[i
].by_ref
)
1867 hres
= VariantCopy(exec
.args
+i
, V_VARIANTREF(v
));
1869 hres
= VariantCopy(exec
.args
+i
, v
);
1872 release_exec(&exec
);
1881 exec
.vars
= heap_alloc_zero(func
->var_cnt
* sizeof(VARIANT
));
1883 release_exec(&exec
);
1884 return E_OUTOFMEMORY
;
1890 exec
.stack_size
= 16;
1892 exec
.stack
= heap_alloc(exec
.stack_size
* sizeof(VARIANT
));
1894 release_exec(&exec
);
1895 return E_OUTOFMEMORY
;
1899 exec
.this_obj
= this_obj
;
1900 else if (ctx
->host_global
)
1901 exec
.this_obj
= ctx
->host_global
;
1903 exec
.this_obj
= (IDispatch
*)&ctx
->script_obj
->IDispatchEx_iface
;
1904 IDispatch_AddRef(exec
.this_obj
);
1906 exec
.instr
= exec
.code
->instrs
+ func
->code_off
;
1911 op
= exec
.instr
->op
;
1912 hres
= op_funcs
[op
](&exec
);
1914 if(exec
.resume_next
)
1915 FIXME("Failed %08x in resume next mode\n", hres
);
1917 WARN("Failed %08x\n", hres
);
1918 stack_popn(&exec
, exec
.top
);
1922 exec
.instr
+= op_move
[op
];
1926 if(func
->type
!= FUNC_FUNCTION
&& func
->type
!= FUNC_PROPGET
&& func
->type
!= FUNC_DEFGET
)
1927 assert(V_VT(&exec
.ret_val
) == VT_EMPTY
);
1929 if(SUCCEEDED(hres
) && res
) {
1930 *res
= exec
.ret_val
;
1931 V_VT(&exec
.ret_val
) = VT_EMPTY
;
1934 release_exec(&exec
);