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
;
35 dynamic_var_t
*dynamic_vars
;
47 typedef HRESULT (*instr_func_t
)(exec_ctx_t
*);
77 static BOOL
lookup_dynamic_vars(dynamic_var_t
*var
, const WCHAR
*name
, ref_t
*ref
)
80 if(!strcmpiW(var
->name
, name
)) {
81 ref
->type
= var
->is_const
? REF_CONST
: REF_VAR
;
92 static HRESULT
lookup_identifier(exec_ctx_t
*ctx
, BSTR name
, vbdisp_invoke_type_t invoke_type
, ref_t
*ref
)
100 static const WCHAR errW
[] = {'e','r','r',0};
102 if(invoke_type
== VBDISP_LET
103 && (ctx
->func
->type
== FUNC_FUNCTION
|| ctx
->func
->type
== FUNC_PROPGET
|| ctx
->func
->type
== FUNC_DEFGET
)
104 && !strcmpiW(name
, ctx
->func
->name
)) {
106 ref
->u
.v
= &ctx
->ret_val
;
110 for(i
=0; i
< ctx
->func
->var_cnt
; i
++) {
111 if(!strcmpiW(ctx
->func
->vars
[i
].name
, name
)) {
113 ref
->u
.v
= ctx
->vars
+i
;
118 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++) {
119 if(!strcmpiW(ctx
->func
->args
[i
].name
, name
)) {
121 ref
->u
.v
= ctx
->args
+i
;
126 if(lookup_dynamic_vars(ctx
->func
->type
== FUNC_GLOBAL
? ctx
->script
->global_vars
: ctx
->dynamic_vars
, name
, ref
))
129 if(ctx
->func
->type
!= FUNC_GLOBAL
) {
131 /* FIXME: Bind such identifier while generating bytecode. */
132 for(i
=0; i
< ctx
->vbthis
->desc
->prop_cnt
; i
++) {
133 if(!strcmpiW(ctx
->vbthis
->desc
->props
[i
].name
, name
)) {
135 ref
->u
.v
= ctx
->vbthis
->props
+i
;
141 hres
= disp_get_id(ctx
->this_obj
, name
, invoke_type
, TRUE
, &id
);
142 if(SUCCEEDED(hres
)) {
143 ref
->type
= REF_DISP
;
144 ref
->u
.d
.disp
= ctx
->this_obj
;
150 if(ctx
->func
->type
!= FUNC_GLOBAL
&& lookup_dynamic_vars(ctx
->script
->global_vars
, name
, ref
))
153 for(func
= ctx
->script
->global_funcs
; func
; func
= func
->next
) {
154 if(!strcmpiW(func
->name
, name
)) {
155 ref
->type
= REF_FUNC
;
161 if(!strcmpiW(name
, errW
)) {
163 ref
->u
.obj
= (IDispatch
*)&ctx
->script
->err_obj
->IDispatchEx_iface
;
167 hres
= vbdisp_get_id(ctx
->script
->global_obj
, name
, invoke_type
, TRUE
, &id
);
168 if(SUCCEEDED(hres
)) {
169 ref
->type
= REF_DISP
;
170 ref
->u
.d
.disp
= (IDispatch
*)&ctx
->script
->global_obj
->IDispatchEx_iface
;
175 LIST_FOR_EACH_ENTRY(item
, &ctx
->script
->named_items
, named_item_t
, entry
) {
176 if((item
->flags
& SCRIPTITEM_ISVISIBLE
) && !strcmpiW(item
->name
, name
)) {
180 hres
= IActiveScriptSite_GetItemInfo(ctx
->script
->site
, name
, SCRIPTINFO_IUNKNOWN
, &unk
, NULL
);
182 WARN("GetItemInfo failed: %08x\n", hres
);
186 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)&item
->disp
);
187 IUnknown_Release(unk
);
189 WARN("object does not implement IDispatch\n");
195 ref
->u
.obj
= item
->disp
;
200 LIST_FOR_EACH_ENTRY(item
, &ctx
->script
->named_items
, named_item_t
, entry
) {
201 if((item
->flags
& SCRIPTITEM_GLOBALMEMBERS
)) {
202 hres
= disp_get_id(item
->disp
, name
, invoke_type
, FALSE
, &id
);
203 if(SUCCEEDED(hres
)) {
204 ref
->type
= REF_DISP
;
205 ref
->u
.d
.disp
= item
->disp
;
212 ref
->type
= REF_NONE
;
216 static HRESULT
add_dynamic_var(exec_ctx_t
*ctx
, const WCHAR
*name
,
217 BOOL is_const
, VARIANT
*val
, BOOL own_val
, VARIANT
**out_var
)
219 dynamic_var_t
*new_var
;
225 heap
= ctx
->func
->type
== FUNC_GLOBAL
? &ctx
->script
->heap
: &ctx
->heap
;
227 new_var
= heap_pool_alloc(heap
, sizeof(*new_var
));
229 return E_OUTOFMEMORY
;
231 size
= (strlenW(name
)+1)*sizeof(WCHAR
);
232 str
= heap_pool_alloc(heap
, size
);
234 return E_OUTOFMEMORY
;
235 memcpy(str
, name
, size
);
237 new_var
->is_const
= is_const
;
242 V_VT(&new_var
->v
) = VT_EMPTY
;
243 hres
= VariantCopy(&new_var
->v
, val
);
248 if(ctx
->func
->type
== FUNC_GLOBAL
) {
249 new_var
->next
= ctx
->script
->global_vars
;
250 ctx
->script
->global_vars
= new_var
;
252 new_var
->next
= ctx
->dynamic_vars
;
253 ctx
->dynamic_vars
= new_var
;
257 *out_var
= &new_var
->v
;
262 static inline VARIANT
*stack_pop(exec_ctx_t
*ctx
)
265 return ctx
->stack
+ --ctx
->top
;
268 static inline VARIANT
*stack_top(exec_ctx_t
*ctx
, unsigned n
)
270 assert(ctx
->top
>= n
);
271 return ctx
->stack
+ (ctx
->top
-n
-1);
274 static HRESULT
stack_push(exec_ctx_t
*ctx
, VARIANT
*v
)
276 if(ctx
->stack_size
== ctx
->top
) {
279 new_stack
= heap_realloc(ctx
->stack
, ctx
->stack_size
*2*sizeof(*ctx
->stack
));
282 return E_OUTOFMEMORY
;
285 ctx
->stack
= new_stack
;
286 ctx
->stack_size
*= 2;
289 ctx
->stack
[ctx
->top
++] = *v
;
293 static inline HRESULT
stack_push_null(exec_ctx_t
*ctx
)
297 return stack_push(ctx
, &v
);
300 static void stack_popn(exec_ctx_t
*ctx
, unsigned n
)
303 VariantClear(stack_pop(ctx
));
306 static void stack_pop_deref(exec_ctx_t
*ctx
, variant_val_t
*r
)
311 if(V_VT(v
) == (VT_BYREF
|VT_VARIANT
)) {
313 r
->v
= V_VARIANTREF(v
);
320 static inline void release_val(variant_val_t
*v
)
326 static HRESULT
stack_pop_val(exec_ctx_t
*ctx
, variant_val_t
*r
)
328 stack_pop_deref(ctx
, r
);
330 if(V_VT(r
->v
) == VT_DISPATCH
) {
334 hres
= disp_call(ctx
->script
, V_DISPATCH(r
->v
), DISPID_VALUE
, &dp
, &r
->store
);
336 IDispatch_Release(V_DISPATCH(r
->v
));
347 static HRESULT
stack_assume_val(exec_ctx_t
*ctx
, unsigned n
)
349 VARIANT
*v
= stack_top(ctx
, n
);
352 if(V_VT(v
) == (VT_BYREF
|VT_VARIANT
)) {
353 VARIANT
*ref
= V_VARIANTREF(v
);
356 hres
= VariantCopy(v
, ref
);
361 if(V_VT(v
) == VT_DISPATCH
) {
365 disp
= V_DISPATCH(v
);
367 hres
= disp_call(ctx
->script
, disp
, DISPID_VALUE
, &dp
, v
);
368 IDispatch_Release(disp
);
376 static int stack_pop_bool(exec_ctx_t
*ctx
, BOOL
*b
)
381 hres
= stack_pop_val(ctx
, &val
);
400 FIXME("unsupported for %s\n", debugstr_variant(val
.v
));
407 static HRESULT
stack_pop_disp(exec_ctx_t
*ctx
, IDispatch
**ret
)
409 VARIANT
*v
= stack_pop(ctx
);
411 if(V_VT(v
) == VT_DISPATCH
) {
412 *ret
= V_DISPATCH(v
);
416 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
417 FIXME("not supported type: %s\n", debugstr_variant(v
));
423 if(V_VT(v
) != VT_DISPATCH
) {
424 FIXME("not disp %s\n", debugstr_variant(v
));
429 IDispatch_AddRef(V_DISPATCH(v
));
430 *ret
= V_DISPATCH(v
);
434 static HRESULT
stack_assume_disp(exec_ctx_t
*ctx
, unsigned n
, IDispatch
**disp
)
436 VARIANT
*v
= stack_top(ctx
, n
), *ref
;
438 if(V_VT(v
) != VT_DISPATCH
) {
439 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
440 FIXME("not supported type: %s\n", debugstr_variant(v
));
444 ref
= V_VARIANTREF(v
);
445 if(V_VT(ref
) != VT_DISPATCH
) {
446 FIXME("not disp %s\n", debugstr_variant(ref
));
450 V_VT(v
) = VT_DISPATCH
;
451 V_DISPATCH(v
) = V_DISPATCH(ref
);
453 IDispatch_AddRef(V_DISPATCH(v
));
457 *disp
= V_DISPATCH(v
);
461 static inline void instr_jmp(exec_ctx_t
*ctx
, unsigned addr
)
463 ctx
->instr
= ctx
->code
->instrs
+ addr
;
466 static void vbstack_to_dp(exec_ctx_t
*ctx
, unsigned arg_cnt
, BOOL is_propput
, DISPPARAMS
*dp
)
468 dp
->cNamedArgs
= is_propput
? 1 : 0;
469 dp
->cArgs
= arg_cnt
+ dp
->cNamedArgs
;
470 dp
->rgdispidNamedArgs
= is_propput
? &propput_dispid
: NULL
;
476 assert(ctx
->top
>= arg_cnt
);
478 for(i
=1; i
*2 <= arg_cnt
; i
++) {
479 tmp
= ctx
->stack
[ctx
->top
-i
];
480 ctx
->stack
[ctx
->top
-i
] = ctx
->stack
[ctx
->top
-arg_cnt
+i
-1];
481 ctx
->stack
[ctx
->top
-arg_cnt
+i
-1] = tmp
;
484 dp
->rgvarg
= ctx
->stack
+ ctx
->top
-dp
->cArgs
;
486 dp
->rgvarg
= is_propput
? ctx
->stack
+ctx
->top
-1 : NULL
;
490 static HRESULT
array_access(exec_ctx_t
*ctx
, SAFEARRAY
*array
, DISPPARAMS
*dp
, VARIANT
**ret
)
492 unsigned cell_off
= 0, dim_size
= 1, i
;
493 unsigned argc
= arg_cnt(dp
);
499 FIXME("NULL array\n");
503 if(array
->cDims
!= argc
) {
504 FIXME("argc %d does not match cDims %d\n", dp
->cArgs
, array
->cDims
);
508 for(i
=0; i
< argc
; i
++) {
509 hres
= to_int(get_arg(dp
, i
), &idx
);
513 idx
-= array
->rgsabound
[i
].lLbound
;
514 if(idx
>= array
->rgsabound
[i
].cElements
) {
515 FIXME("out of bound element %d in dim %d of size %d\n", idx
, i
+1, array
->rgsabound
[i
].cElements
);
519 cell_off
+= idx
*dim_size
;
520 dim_size
*= array
->rgsabound
[i
].cElements
;
523 hres
= SafeArrayAccessData(array
, (void**)&data
);
527 *ret
= data
+cell_off
;
529 SafeArrayUnaccessData(array
);
533 static HRESULT
do_icall(exec_ctx_t
*ctx
, VARIANT
*res
)
535 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
536 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
541 hres
= lookup_identifier(ctx
, identifier
, VBDISP_CALLGET
, &ref
);
551 FIXME("REF_VAR no res\n");
555 v
= V_VT(ref
.u
.v
) == (VT_VARIANT
|VT_BYREF
) ? V_VARIANTREF(ref
.u
.v
) : ref
.u
.v
;
561 case VT_ARRAY
|VT_BYREF
|VT_VARIANT
:
562 array
= *V_ARRAYREF(ref
.u
.v
);
564 case VT_ARRAY
|VT_VARIANT
:
565 array
= V_ARRAY(ref
.u
.v
);
568 FIXME("arguments not implemented\n");
572 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
573 hres
= array_access(ctx
, array
, &dp
, &v
);
578 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
583 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
584 hres
= disp_call(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, &dp
, res
);
589 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
590 hres
= exec_script(ctx
->script
, ref
.u
.f
, NULL
, &dp
, res
);
596 FIXME("arguments on object\n");
601 IDispatch_AddRef(ref
.u
.obj
);
602 V_VT(res
) = VT_DISPATCH
;
603 V_DISPATCH(res
) = ref
.u
.obj
;
607 if(res
&& !ctx
->func
->code_ctx
->option_explicit
&& arg_cnt
== 0) {
610 hres
= add_dynamic_var(ctx
, identifier
, FALSE
, &v
, FALSE
, &new);
613 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
617 FIXME("%s not found\n", debugstr_w(identifier
));
618 return DISP_E_UNKNOWNNAME
;
621 stack_popn(ctx
, arg_cnt
);
625 static HRESULT
interp_icall(exec_ctx_t
*ctx
)
632 hres
= do_icall(ctx
, &v
);
636 return stack_push(ctx
, &v
);
639 static HRESULT
interp_icallv(exec_ctx_t
*ctx
)
642 return do_icall(ctx
, NULL
);
645 static HRESULT
do_mcall(exec_ctx_t
*ctx
, VARIANT
*res
)
647 const BSTR identifier
= ctx
->instr
->arg1
.bstr
;
648 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
654 hres
= stack_pop_disp(ctx
, &obj
);
663 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
665 hres
= disp_get_id(obj
, identifier
, VBDISP_CALLGET
, FALSE
, &id
);
667 hres
= disp_call(ctx
->script
, obj
, id
, &dp
, res
);
668 IDispatch_Release(obj
);
672 stack_popn(ctx
, arg_cnt
);
676 static HRESULT
interp_mcall(exec_ctx_t
*ctx
)
683 hres
= do_mcall(ctx
, &res
);
687 return stack_push(ctx
, &res
);
690 static HRESULT
interp_mcallv(exec_ctx_t
*ctx
)
694 return do_mcall(ctx
, NULL
);
697 static HRESULT
assign_ident(exec_ctx_t
*ctx
, BSTR name
, DISPPARAMS
*dp
)
702 hres
= lookup_identifier(ctx
, name
, VBDISP_LET
, &ref
);
708 VARIANT
*v
= ref
.u
.v
;
710 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
))
716 if(!(V_VT(v
) & VT_ARRAY
)) {
717 FIXME("array assign on type %d\n", V_VT(v
));
722 case VT_ARRAY
|VT_BYREF
|VT_VARIANT
:
723 array
= *V_ARRAYREF(v
);
725 case VT_ARRAY
|VT_VARIANT
:
729 FIXME("Unsupported array type %x\n", V_VT(v
));
734 FIXME("null array\n");
738 hres
= array_access(ctx
, array
, dp
, &v
);
741 }else if(V_VT(v
) == (VT_ARRAY
|VT_BYREF
|VT_VARIANT
)) {
742 FIXME("non-array assign\n");
746 hres
= VariantCopyInd(v
, dp
->rgvarg
);
750 hres
= disp_propput(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, dp
);
753 FIXME("functions not implemented\n");
759 FIXME("REF_CONST\n");
762 if(ctx
->func
->code_ctx
->option_explicit
) {
763 FIXME("throw exception\n");
767 FIXME("arg_cnt %d not supported\n", arg_cnt(dp
));
771 TRACE("creating variable %s\n", debugstr_w(name
));
772 hres
= add_dynamic_var(ctx
, name
, FALSE
, dp
->rgvarg
, FALSE
, NULL
);
779 static HRESULT
interp_assign_ident(exec_ctx_t
*ctx
)
781 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
782 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
786 TRACE("%s\n", debugstr_w(arg
));
788 hres
= stack_assume_val(ctx
, arg_cnt
);
792 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
793 hres
= assign_ident(ctx
, arg
, &dp
);
797 stack_popn(ctx
, arg_cnt
+1);
801 static HRESULT
interp_set_ident(exec_ctx_t
*ctx
)
803 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
804 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
808 TRACE("%s\n", debugstr_w(arg
));
811 FIXME("arguments not supported\n");
815 hres
= stack_assume_disp(ctx
, 0, NULL
);
819 vbstack_to_dp(ctx
, 0, TRUE
, &dp
);
820 hres
= assign_ident(ctx
, ctx
->instr
->arg1
.bstr
, &dp
);
828 static HRESULT
interp_assign_member(exec_ctx_t
*ctx
)
830 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
831 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
837 TRACE("%s\n", debugstr_w(identifier
));
839 hres
= stack_assume_disp(ctx
, arg_cnt
+1, &obj
);
848 hres
= stack_assume_val(ctx
, arg_cnt
);
852 hres
= disp_get_id(obj
, identifier
, VBDISP_LET
, FALSE
, &id
);
853 if(SUCCEEDED(hres
)) {
854 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
855 hres
= disp_propput(ctx
->script
, obj
, id
, &dp
);
860 stack_popn(ctx
, arg_cnt
+2);
864 static HRESULT
interp_set_member(exec_ctx_t
*ctx
)
866 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
867 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
873 TRACE("%s\n", debugstr_w(identifier
));
876 FIXME("arguments not supported\n");
880 hres
= stack_assume_disp(ctx
, 1, &obj
);
889 hres
= stack_assume_disp(ctx
, 0, NULL
);
893 hres
= disp_get_id(obj
, identifier
, VBDISP_SET
, FALSE
, &id
);
894 if(SUCCEEDED(hres
)) {
895 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
896 hres
= disp_propput(ctx
->script
, obj
, id
, &dp
);
905 static HRESULT
interp_const(exec_ctx_t
*ctx
)
907 BSTR arg
= ctx
->instr
->arg1
.bstr
;
912 TRACE("%s\n", debugstr_w(arg
));
914 assert(ctx
->func
->type
== FUNC_GLOBAL
);
916 hres
= lookup_identifier(ctx
, arg
, VBDISP_CALLGET
, &ref
);
920 if(ref
.type
!= REF_NONE
) {
921 FIXME("%s already defined\n", debugstr_w(arg
));
925 hres
= stack_pop_val(ctx
, &val
);
929 return add_dynamic_var(ctx
, arg
, TRUE
, val
.v
, val
.owned
, NULL
);
932 static HRESULT
interp_val(exec_ctx_t
*ctx
)
940 hres
= stack_pop_val(ctx
, &val
);
946 hres
= VariantCopy(&v
, val
.v
);
951 return stack_push(ctx
, val
.owned
? val
.v
: &v
);
954 static HRESULT
interp_pop(exec_ctx_t
*ctx
)
956 const unsigned n
= ctx
->instr
->arg1
.uint
;
964 static HRESULT
interp_new(exec_ctx_t
*ctx
)
966 const WCHAR
*arg
= ctx
->instr
->arg1
.bstr
;
967 class_desc_t
*class_desc
;
972 static const WCHAR regexpW
[] = {'r','e','g','e','x','p',0};
974 TRACE("%s\n", debugstr_w(arg
));
976 if(!strcmpiW(arg
, regexpW
)) {
977 V_VT(&v
) = VT_DISPATCH
;
978 hres
= create_regexp(&V_DISPATCH(&v
));
982 return stack_push(ctx
, &v
);
985 for(class_desc
= ctx
->script
->classes
; class_desc
; class_desc
= class_desc
->next
) {
986 if(!strcmpiW(class_desc
->name
, arg
))
990 FIXME("Class %s not found\n", debugstr_w(arg
));
994 hres
= create_vbdisp(class_desc
, &obj
);
998 V_VT(&v
) = VT_DISPATCH
;
999 V_DISPATCH(&v
) = (IDispatch
*)&obj
->IDispatchEx_iface
;
1000 return stack_push(ctx
, &v
);
1003 static HRESULT
interp_dim(exec_ctx_t
*ctx
)
1005 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
1006 const unsigned array_id
= ctx
->instr
->arg2
.uint
;
1007 const array_desc_t
*array_desc
;
1011 TRACE("%s\n", debugstr_w(ident
));
1013 assert(array_id
< ctx
->func
->array_cnt
);
1015 ctx
->arrays
= heap_alloc_zero(ctx
->func
->array_cnt
* sizeof(SAFEARRAY
*));
1017 return E_OUTOFMEMORY
;
1020 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1022 FIXME("lookup %s failed: %08x\n", debugstr_w(ident
), hres
);
1026 if(ref
.type
!= REF_VAR
) {
1027 FIXME("got ref.type = %d\n", ref
.type
);
1031 if(ctx
->arrays
[array_id
]) {
1032 FIXME("Array already initialized\n");
1036 array_desc
= ctx
->func
->array_descs
+ array_id
;
1037 if(array_desc
->dim_cnt
) {
1038 ctx
->arrays
[array_id
] = SafeArrayCreate(VT_VARIANT
, array_desc
->dim_cnt
, array_desc
->bounds
);
1039 if(!ctx
->arrays
[array_id
])
1040 return E_OUTOFMEMORY
;
1043 V_VT(ref
.u
.v
) = VT_ARRAY
|VT_BYREF
|VT_VARIANT
;
1044 V_ARRAYREF(ref
.u
.v
) = ctx
->arrays
+array_id
;
1048 static HRESULT
interp_step(exec_ctx_t
*ctx
)
1050 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1056 TRACE("%s\n", debugstr_w(ident
));
1058 V_VT(&zero
) = VT_I2
;
1060 hres
= VarCmp(stack_top(ctx
, 0), &zero
, ctx
->script
->lcid
, 0);
1064 gteq_zero
= hres
== VARCMP_GT
|| hres
== VARCMP_EQ
;
1066 hres
= lookup_identifier(ctx
, ident
, VBDISP_ANY
, &ref
);
1070 if(ref
.type
!= REF_VAR
) {
1071 FIXME("%s is not REF_VAR\n", debugstr_w(ident
));
1075 hres
= VarCmp(ref
.u
.v
, stack_top(ctx
, 1), ctx
->script
->lcid
, 0);
1079 if(hres
== VARCMP_EQ
|| hres
== (gteq_zero
? VARCMP_LT
: VARCMP_GT
)) {
1083 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1088 static HRESULT
interp_newenum(exec_ctx_t
*ctx
)
1096 stack_pop_deref(ctx
, &v
);
1097 assert(V_VT(stack_top(ctx
, 0)) == VT_EMPTY
);
1098 r
= stack_top(ctx
, 0);
1101 case VT_DISPATCH
|VT_BYREF
:
1104 DISPPARAMS dp
= {0};
1107 hres
= disp_call(ctx
->script
, V_ISBYREF(v
.v
) ? *V_DISPATCHREF(v
.v
) : V_DISPATCH(v
.v
), DISPID_NEWENUM
, &dp
, &iterv
);
1112 if(V_VT(&iterv
) != VT_UNKNOWN
&& V_VT(&iterv
) != VT_DISPATCH
) {
1113 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv
));
1114 VariantClear(&iterv
);
1118 hres
= IUnknown_QueryInterface(V_UNKNOWN(&iterv
), &IID_IEnumVARIANT
, (void**)&iter
);
1119 IUnknown_Release(V_UNKNOWN(&iterv
));
1121 FIXME("Could not get IEnumVARIANT iface: %08x\n", hres
);
1125 V_VT(r
) = VT_UNKNOWN
;
1126 V_UNKNOWN(r
) = (IUnknown
*)iter
;
1130 FIXME("Unsupported for %s\n", debugstr_variant(v
.v
));
1138 static HRESULT
interp_enumnext(exec_ctx_t
*ctx
)
1140 const unsigned loop_end
= ctx
->instr
->arg1
.uint
;
1141 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1143 DISPPARAMS dp
= {&v
, &propput_dispid
, 1, 1};
1150 if(V_VT(stack_top(ctx
, 0)) == VT_EMPTY
) {
1151 FIXME("uninitialized\n");
1155 assert(V_VT(stack_top(ctx
, 0)) == VT_UNKNOWN
);
1156 iter
= (IEnumVARIANT
*)V_UNKNOWN(stack_top(ctx
, 0));
1158 V_VT(&v
) = VT_EMPTY
;
1159 hres
= IEnumVARIANT_Next(iter
, 1, &v
, NULL
);
1163 do_continue
= hres
== S_OK
;
1164 hres
= assign_ident(ctx
, ident
, &dp
);
1173 instr_jmp(ctx
, loop_end
);
1178 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
1180 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1184 instr_jmp(ctx
, arg
);
1188 static HRESULT
interp_jmp_false(exec_ctx_t
*ctx
)
1190 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1196 hres
= stack_pop_bool(ctx
, &b
);
1203 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1207 static HRESULT
interp_jmp_true(exec_ctx_t
*ctx
)
1209 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1215 hres
= stack_pop_bool(ctx
, &b
);
1220 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1226 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
1234 static HRESULT
interp_stop(exec_ctx_t
*ctx
)
1238 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1242 static HRESULT
interp_me(exec_ctx_t
*ctx
)
1248 IDispatch_AddRef(ctx
->this_obj
);
1249 V_VT(&v
) = VT_DISPATCH
;
1250 V_DISPATCH(&v
) = ctx
->this_obj
;
1251 return stack_push(ctx
, &v
);
1254 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
1256 const VARIANT_BOOL arg
= ctx
->instr
->arg1
.lng
;
1259 TRACE("%s\n", arg
? "true" : "false");
1263 return stack_push(ctx
, &v
);
1266 static HRESULT
interp_errmode(exec_ctx_t
*ctx
)
1268 const int err_mode
= ctx
->instr
->arg1
.uint
;
1270 TRACE("%d\n", err_mode
);
1272 ctx
->resume_next
= err_mode
;
1276 static HRESULT
interp_string(exec_ctx_t
*ctx
)
1283 V_BSTR(&v
) = SysAllocString(ctx
->instr
->arg1
.str
);
1285 return E_OUTOFMEMORY
;
1287 return stack_push(ctx
, &v
);
1290 static HRESULT
interp_long(exec_ctx_t
*ctx
)
1292 const LONG arg
= ctx
->instr
->arg1
.lng
;
1299 return stack_push(ctx
, &v
);
1302 static HRESULT
interp_short(exec_ctx_t
*ctx
)
1304 const LONG arg
= ctx
->instr
->arg1
.lng
;
1311 return stack_push(ctx
, &v
);
1314 static HRESULT
interp_double(exec_ctx_t
*ctx
)
1316 const DOUBLE
*arg
= ctx
->instr
->arg1
.dbl
;
1319 TRACE("%lf\n", *arg
);
1323 return stack_push(ctx
, &v
);
1326 static HRESULT
interp_empty(exec_ctx_t
*ctx
)
1332 V_VT(&v
) = VT_EMPTY
;
1333 return stack_push(ctx
, &v
);
1336 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1339 return stack_push_null(ctx
);
1342 static HRESULT
interp_nothing(exec_ctx_t
*ctx
)
1348 V_VT(&v
) = VT_DISPATCH
;
1349 V_DISPATCH(&v
) = NULL
;
1350 return stack_push(ctx
, &v
);
1353 static HRESULT
interp_not(exec_ctx_t
*ctx
)
1361 hres
= stack_pop_val(ctx
, &val
);
1365 hres
= VarNot(val
.v
, &v
);
1370 return stack_push(ctx
, &v
);
1373 static HRESULT
interp_and(exec_ctx_t
*ctx
)
1381 hres
= stack_pop_val(ctx
, &r
);
1385 hres
= stack_pop_val(ctx
, &l
);
1386 if(SUCCEEDED(hres
)) {
1387 hres
= VarAnd(l
.v
, r
.v
, &v
);
1394 return stack_push(ctx
, &v
);
1397 static HRESULT
interp_or(exec_ctx_t
*ctx
)
1405 hres
= stack_pop_val(ctx
, &r
);
1409 hres
= stack_pop_val(ctx
, &l
);
1410 if(SUCCEEDED(hres
)) {
1411 hres
= VarOr(l
.v
, r
.v
, &v
);
1418 return stack_push(ctx
, &v
);
1421 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
1429 hres
= stack_pop_val(ctx
, &r
);
1433 hres
= stack_pop_val(ctx
, &l
);
1434 if(SUCCEEDED(hres
)) {
1435 hres
= VarXor(l
.v
, r
.v
, &v
);
1442 return stack_push(ctx
, &v
);
1445 static HRESULT
interp_eqv(exec_ctx_t
*ctx
)
1453 hres
= stack_pop_val(ctx
, &r
);
1457 hres
= stack_pop_val(ctx
, &l
);
1458 if(SUCCEEDED(hres
)) {
1459 hres
= VarEqv(l
.v
, r
.v
, &v
);
1466 return stack_push(ctx
, &v
);
1469 static HRESULT
interp_imp(exec_ctx_t
*ctx
)
1477 hres
= stack_pop_val(ctx
, &r
);
1481 hres
= stack_pop_val(ctx
, &l
);
1482 if(SUCCEEDED(hres
)) {
1483 hres
= VarImp(l
.v
, r
.v
, &v
);
1490 return stack_push(ctx
, &v
);
1493 static HRESULT
var_cmp(exec_ctx_t
*ctx
, VARIANT
*l
, VARIANT
*r
)
1495 TRACE("%s %s\n", debugstr_variant(l
), debugstr_variant(r
));
1497 /* FIXME: Fix comparing string to number */
1499 return VarCmp(l
, r
, ctx
->script
->lcid
, 0);
1502 static HRESULT
cmp_oper(exec_ctx_t
*ctx
)
1507 hres
= stack_pop_val(ctx
, &r
);
1511 hres
= stack_pop_val(ctx
, &l
);
1512 if(SUCCEEDED(hres
)) {
1513 hres
= var_cmp(ctx
, l
.v
, r
.v
);
1521 static HRESULT
interp_equal(exec_ctx_t
*ctx
)
1528 hres
= cmp_oper(ctx
);
1531 if(hres
== VARCMP_NULL
)
1532 return stack_push_null(ctx
);
1535 V_BOOL(&v
) = hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1536 return stack_push(ctx
, &v
);
1539 static HRESULT
interp_nequal(exec_ctx_t
*ctx
)
1546 hres
= cmp_oper(ctx
);
1549 if(hres
== VARCMP_NULL
)
1550 return stack_push_null(ctx
);
1553 V_BOOL(&v
) = hres
!= VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1554 return stack_push(ctx
, &v
);
1557 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
1564 hres
= cmp_oper(ctx
);
1567 if(hres
== VARCMP_NULL
)
1568 return stack_push_null(ctx
);
1571 V_BOOL(&v
) = hres
== VARCMP_GT
? VARIANT_TRUE
: VARIANT_FALSE
;
1572 return stack_push(ctx
, &v
);
1575 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
1582 hres
= cmp_oper(ctx
);
1585 if(hres
== VARCMP_NULL
)
1586 return stack_push_null(ctx
);
1589 V_BOOL(&v
) = hres
== VARCMP_GT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1590 return stack_push(ctx
, &v
);
1593 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
1600 hres
= cmp_oper(ctx
);
1603 if(hres
== VARCMP_NULL
)
1604 return stack_push_null(ctx
);
1607 V_BOOL(&v
) = hres
== VARCMP_LT
? VARIANT_TRUE
: VARIANT_FALSE
;
1608 return stack_push(ctx
, &v
);
1611 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
1618 hres
= cmp_oper(ctx
);
1621 if(hres
== VARCMP_NULL
)
1622 return stack_push_null(ctx
);
1625 V_BOOL(&v
) = hres
== VARCMP_LT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1626 return stack_push(ctx
, &v
);
1629 static HRESULT
interp_case(exec_ctx_t
*ctx
)
1631 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1637 hres
= stack_pop_val(ctx
, &v
);
1641 hres
= var_cmp(ctx
, stack_top(ctx
, 0), v
.v
);
1646 if(hres
== VARCMP_EQ
) {
1648 instr_jmp(ctx
, arg
);
1656 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, VARIANT_BOOL
*ret
)
1658 IObjectIdentity
*identity
;
1659 IUnknown
*unk1
, *unk2
;
1662 if(disp1
== disp2
) {
1663 *ret
= VARIANT_TRUE
;
1667 if(!disp1
|| !disp2
) {
1668 *ret
= VARIANT_FALSE
;
1672 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
1676 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
1678 IUnknown_Release(unk1
);
1683 *ret
= VARIANT_TRUE
;
1685 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
1686 if(SUCCEEDED(hres
)) {
1687 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
1688 IObjectIdentity_Release(identity
);
1689 *ret
= hres
== S_OK
? VARIANT_TRUE
: VARIANT_FALSE
;
1691 *ret
= VARIANT_FALSE
;
1695 IUnknown_Release(unk1
);
1696 IUnknown_Release(unk2
);
1700 static HRESULT
interp_is(exec_ctx_t
*ctx
)
1708 hres
= stack_pop_disp(ctx
, &r
);
1712 hres
= stack_pop_disp(ctx
, &l
);
1713 if(SUCCEEDED(hres
)) {
1715 hres
= disp_cmp(l
, r
, &V_BOOL(&v
));
1717 IDispatch_Release(l
);
1720 IDispatch_Release(r
);
1724 return stack_push(ctx
, &v
);
1727 static HRESULT
interp_concat(exec_ctx_t
*ctx
)
1735 hres
= stack_pop_val(ctx
, &r
);
1739 hres
= stack_pop_val(ctx
, &l
);
1740 if(SUCCEEDED(hres
)) {
1741 hres
= VarCat(l
.v
, r
.v
, &v
);
1748 return stack_push(ctx
, &v
);
1751 static HRESULT
interp_add(exec_ctx_t
*ctx
)
1759 hres
= stack_pop_val(ctx
, &r
);
1763 hres
= stack_pop_val(ctx
, &l
);
1764 if(SUCCEEDED(hres
)) {
1765 hres
= VarAdd(l
.v
, r
.v
, &v
);
1772 return stack_push(ctx
, &v
);
1775 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
1783 hres
= stack_pop_val(ctx
, &r
);
1787 hres
= stack_pop_val(ctx
, &l
);
1788 if(SUCCEEDED(hres
)) {
1789 hres
= VarSub(l
.v
, r
.v
, &v
);
1796 return stack_push(ctx
, &v
);
1799 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
1807 hres
= stack_pop_val(ctx
, &r
);
1811 hres
= stack_pop_val(ctx
, &l
);
1812 if(SUCCEEDED(hres
)) {
1813 hres
= VarMod(l
.v
, r
.v
, &v
);
1820 return stack_push(ctx
, &v
);
1823 static HRESULT
interp_idiv(exec_ctx_t
*ctx
)
1831 hres
= stack_pop_val(ctx
, &r
);
1835 hres
= stack_pop_val(ctx
, &l
);
1836 if(SUCCEEDED(hres
)) {
1837 hres
= VarIdiv(l
.v
, r
.v
, &v
);
1844 return stack_push(ctx
, &v
);
1847 static HRESULT
interp_div(exec_ctx_t
*ctx
)
1855 hres
= stack_pop_val(ctx
, &r
);
1859 hres
= stack_pop_val(ctx
, &l
);
1860 if(SUCCEEDED(hres
)) {
1861 hres
= VarDiv(l
.v
, r
.v
, &v
);
1868 return stack_push(ctx
, &v
);
1871 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
1879 hres
= stack_pop_val(ctx
, &r
);
1883 hres
= stack_pop_val(ctx
, &l
);
1884 if(SUCCEEDED(hres
)) {
1885 hres
= VarMul(l
.v
, r
.v
, &v
);
1892 return stack_push(ctx
, &v
);
1895 static HRESULT
interp_exp(exec_ctx_t
*ctx
)
1903 hres
= stack_pop_val(ctx
, &r
);
1907 hres
= stack_pop_val(ctx
, &l
);
1908 if(SUCCEEDED(hres
)) {
1909 hres
= VarPow(l
.v
, r
.v
, &v
);
1916 return stack_push(ctx
, &v
);
1919 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
1925 hres
= stack_pop_val(ctx
, &val
);
1929 hres
= VarNeg(val
.v
, &v
);
1934 return stack_push(ctx
, &v
);
1937 static HRESULT
interp_incc(exec_ctx_t
*ctx
)
1939 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
1946 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1950 if(ref
.type
!= REF_VAR
) {
1951 FIXME("ref.type is not REF_VAR\n");
1955 hres
= VarAdd(stack_top(ctx
, 0), ref
.u
.v
, &v
);
1959 VariantClear(ref
.u
.v
);
1964 static HRESULT
interp_catch(exec_ctx_t
*ctx
)
1966 /* Nothing to do here, the OP is for unwinding only. */
1970 static const instr_func_t op_funcs
[] = {
1971 #define X(x,n,a,b) interp_ ## x,
1976 static const unsigned op_move
[] = {
1977 #define X(x,n,a,b) n,
1982 void release_dynamic_vars(dynamic_var_t
*var
)
1985 VariantClear(&var
->v
);
1990 static void release_exec(exec_ctx_t
*ctx
)
1994 VariantClear(&ctx
->ret_val
);
1995 release_dynamic_vars(ctx
->dynamic_vars
);
1998 IDispatch_Release(ctx
->this_obj
);
2001 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++)
2002 VariantClear(ctx
->args
+i
);
2006 for(i
=0; i
< ctx
->func
->var_cnt
; i
++)
2007 VariantClear(ctx
->vars
+i
);
2011 for(i
=0; i
< ctx
->func
->var_cnt
; i
++) {
2013 SafeArrayDestroy(ctx
->arrays
[i
]);
2015 heap_free(ctx
->arrays
);
2018 heap_pool_free(&ctx
->heap
);
2019 heap_free(ctx
->args
);
2020 heap_free(ctx
->vars
);
2021 heap_free(ctx
->stack
);
2024 HRESULT
exec_script(script_ctx_t
*ctx
, function_t
*func
, vbdisp_t
*vbthis
, DISPPARAMS
*dp
, VARIANT
*res
)
2026 exec_ctx_t exec
= {func
->code_ctx
};
2028 HRESULT hres
= S_OK
;
2030 exec
.code
= func
->code_ctx
;
2032 if(dp
? func
->arg_cnt
!= arg_cnt(dp
) : func
->arg_cnt
) {
2033 FIXME("wrong arg_cnt %d, expected %d\n", dp
? arg_cnt(dp
) : 0, func
->arg_cnt
);
2037 heap_pool_init(&exec
.heap
);
2043 exec
.args
= heap_alloc_zero(func
->arg_cnt
* sizeof(VARIANT
));
2045 release_exec(&exec
);
2046 return E_OUTOFMEMORY
;
2049 for(i
=0; i
< func
->arg_cnt
; i
++) {
2051 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
)) {
2052 if(func
->args
[i
].by_ref
)
2055 hres
= VariantCopyInd(exec
.args
+i
, V_VARIANTREF(v
));
2057 hres
= VariantCopyInd(exec
.args
+i
, v
);
2060 release_exec(&exec
);
2069 exec
.vars
= heap_alloc_zero(func
->var_cnt
* sizeof(VARIANT
));
2071 release_exec(&exec
);
2072 return E_OUTOFMEMORY
;
2078 exec
.stack_size
= 16;
2080 exec
.stack
= heap_alloc(exec
.stack_size
* sizeof(VARIANT
));
2082 release_exec(&exec
);
2083 return E_OUTOFMEMORY
;
2087 exec
.this_obj
= (IDispatch
*)&vbthis
->IDispatchEx_iface
;
2088 exec
.vbthis
= vbthis
;
2089 }else if (ctx
->host_global
) {
2090 exec
.this_obj
= ctx
->host_global
;
2092 exec
.this_obj
= (IDispatch
*)&ctx
->script_obj
->IDispatchEx_iface
;
2094 IDispatch_AddRef(exec
.this_obj
);
2096 exec
.instr
= exec
.code
->instrs
+ func
->code_off
;
2101 op
= exec
.instr
->op
;
2102 hres
= op_funcs
[op
](&exec
);
2104 ctx
->err_number
= hres
= map_hres(hres
);
2106 if(exec
.resume_next
) {
2109 WARN("Failed %08x in resume next mode\n", hres
);
2112 * Unwinding here is simple. We need to find the next OP_catch, which contains
2113 * information about expected stack size and jump offset on error. Generated
2114 * bytecode needs to guarantee, that simple jump and stack adjustment will
2115 * guarantee proper execution continuation.
2117 while((++exec
.instr
)->op
!= OP_catch
);
2119 TRACE("unwind jmp %d stack_off %d\n", exec
.instr
->arg1
.uint
, exec
.instr
->arg2
.uint
);
2121 stack_off
= exec
.instr
->arg2
.uint
;
2122 instr_jmp(&exec
, exec
.instr
->arg1
.uint
);
2124 if(exec
.top
> stack_off
) {
2125 stack_popn(&exec
, exec
.top
-stack_off
);
2126 }else if(exec
.top
< stack_off
) {
2129 V_VT(&v
) = VT_EMPTY
;
2130 while(exec
.top
< stack_off
) {
2131 hres
= stack_push(&exec
, &v
);
2139 WARN("Failed %08x\n", hres
);
2140 stack_popn(&exec
, exec
.top
);
2145 exec
.instr
+= op_move
[op
];
2149 if(func
->type
!= FUNC_FUNCTION
&& func
->type
!= FUNC_PROPGET
&& func
->type
!= FUNC_DEFGET
)
2150 assert(V_VT(&exec
.ret_val
) == VT_EMPTY
);
2152 if(SUCCEEDED(hres
) && res
) {
2153 *res
= exec
.ret_val
;
2154 V_VT(&exec
.ret_val
) = VT_EMPTY
;
2157 release_exec(&exec
);