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
, item
->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
**out_var
)
219 dynamic_var_t
*new_var
;
224 heap
= ctx
->func
->type
== FUNC_GLOBAL
? &ctx
->script
->heap
: &ctx
->heap
;
226 new_var
= heap_pool_alloc(heap
, sizeof(*new_var
));
228 return E_OUTOFMEMORY
;
230 size
= (strlenW(name
)+1)*sizeof(WCHAR
);
231 str
= heap_pool_alloc(heap
, size
);
233 return E_OUTOFMEMORY
;
234 memcpy(str
, name
, size
);
236 new_var
->is_const
= is_const
;
237 V_VT(&new_var
->v
) = VT_EMPTY
;
239 if(ctx
->func
->type
== FUNC_GLOBAL
) {
240 new_var
->next
= ctx
->script
->global_vars
;
241 ctx
->script
->global_vars
= new_var
;
243 new_var
->next
= ctx
->dynamic_vars
;
244 ctx
->dynamic_vars
= new_var
;
247 *out_var
= &new_var
->v
;
251 static inline VARIANT
*stack_pop(exec_ctx_t
*ctx
)
254 return ctx
->stack
+ --ctx
->top
;
257 static inline VARIANT
*stack_top(exec_ctx_t
*ctx
, unsigned n
)
259 assert(ctx
->top
>= n
);
260 return ctx
->stack
+ (ctx
->top
-n
-1);
263 static HRESULT
stack_push(exec_ctx_t
*ctx
, VARIANT
*v
)
265 if(ctx
->stack_size
== ctx
->top
) {
268 new_stack
= heap_realloc(ctx
->stack
, ctx
->stack_size
*2*sizeof(*ctx
->stack
));
271 return E_OUTOFMEMORY
;
274 ctx
->stack
= new_stack
;
275 ctx
->stack_size
*= 2;
278 ctx
->stack
[ctx
->top
++] = *v
;
282 static inline HRESULT
stack_push_null(exec_ctx_t
*ctx
)
286 return stack_push(ctx
, &v
);
289 static void stack_popn(exec_ctx_t
*ctx
, unsigned n
)
292 VariantClear(stack_pop(ctx
));
295 static void stack_pop_deref(exec_ctx_t
*ctx
, variant_val_t
*r
)
300 if(V_VT(v
) == (VT_BYREF
|VT_VARIANT
)) {
302 r
->v
= V_VARIANTREF(v
);
309 static inline void release_val(variant_val_t
*v
)
315 static HRESULT
stack_pop_val(exec_ctx_t
*ctx
, variant_val_t
*r
)
317 stack_pop_deref(ctx
, r
);
319 if(V_VT(r
->v
) == VT_DISPATCH
) {
322 hres
= get_disp_value(ctx
->script
, V_DISPATCH(r
->v
), &r
->store
);
324 IDispatch_Release(V_DISPATCH(r
->v
));
335 static HRESULT
stack_assume_val(exec_ctx_t
*ctx
, unsigned n
)
337 VARIANT
*v
= stack_top(ctx
, n
);
340 if(V_VT(v
) == (VT_BYREF
|VT_VARIANT
)) {
341 VARIANT
*ref
= V_VARIANTREF(v
);
344 hres
= VariantCopy(v
, ref
);
349 if(V_VT(v
) == VT_DISPATCH
) {
352 disp
= V_DISPATCH(v
);
353 hres
= get_disp_value(ctx
->script
, disp
, v
);
354 IDispatch_Release(disp
);
362 static int stack_pop_bool(exec_ctx_t
*ctx
, BOOL
*b
)
367 hres
= stack_pop_val(ctx
, &val
);
386 FIXME("unsupported for %s\n", debugstr_variant(val
.v
));
393 static HRESULT
stack_pop_disp(exec_ctx_t
*ctx
, IDispatch
**ret
)
395 VARIANT
*v
= stack_pop(ctx
);
397 if(V_VT(v
) == VT_DISPATCH
) {
398 *ret
= V_DISPATCH(v
);
402 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
403 FIXME("not supported type: %s\n", debugstr_variant(v
));
409 if(V_VT(v
) != VT_DISPATCH
) {
410 FIXME("not disp %s\n", debugstr_variant(v
));
415 IDispatch_AddRef(V_DISPATCH(v
));
416 *ret
= V_DISPATCH(v
);
420 static HRESULT
stack_assume_disp(exec_ctx_t
*ctx
, unsigned n
, IDispatch
**disp
)
422 VARIANT
*v
= stack_top(ctx
, n
), *ref
;
424 if(V_VT(v
) != VT_DISPATCH
) {
425 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
426 FIXME("not supported type: %s\n", debugstr_variant(v
));
430 ref
= V_VARIANTREF(v
);
431 if(V_VT(ref
) != VT_DISPATCH
) {
432 FIXME("not disp %s\n", debugstr_variant(ref
));
436 V_VT(v
) = VT_DISPATCH
;
437 V_DISPATCH(v
) = V_DISPATCH(ref
);
439 IDispatch_AddRef(V_DISPATCH(v
));
443 *disp
= V_DISPATCH(v
);
447 static inline void instr_jmp(exec_ctx_t
*ctx
, unsigned addr
)
449 ctx
->instr
= ctx
->code
->instrs
+ addr
;
452 static void vbstack_to_dp(exec_ctx_t
*ctx
, unsigned arg_cnt
, BOOL is_propput
, DISPPARAMS
*dp
)
454 dp
->cNamedArgs
= is_propput
? 1 : 0;
455 dp
->cArgs
= arg_cnt
+ dp
->cNamedArgs
;
456 dp
->rgdispidNamedArgs
= is_propput
? &propput_dispid
: NULL
;
462 assert(ctx
->top
>= arg_cnt
);
464 for(i
=1; i
*2 <= arg_cnt
; i
++) {
465 tmp
= ctx
->stack
[ctx
->top
-i
];
466 ctx
->stack
[ctx
->top
-i
] = ctx
->stack
[ctx
->top
-arg_cnt
+i
-1];
467 ctx
->stack
[ctx
->top
-arg_cnt
+i
-1] = tmp
;
470 dp
->rgvarg
= ctx
->stack
+ ctx
->top
-dp
->cArgs
;
472 dp
->rgvarg
= is_propput
? ctx
->stack
+ctx
->top
-1 : NULL
;
476 static HRESULT
array_access(exec_ctx_t
*ctx
, SAFEARRAY
*array
, DISPPARAMS
*dp
, VARIANT
**ret
)
478 unsigned cell_off
= 0, dim_size
= 1, i
;
479 unsigned argc
= arg_cnt(dp
);
485 FIXME("NULL array\n");
489 if(array
->cDims
!= argc
) {
490 FIXME("argc %d does not match cDims %d\n", dp
->cArgs
, array
->cDims
);
494 for(i
=0; i
< argc
; i
++) {
495 hres
= to_int(get_arg(dp
, i
), &idx
);
499 idx
-= array
->rgsabound
[i
].lLbound
;
500 if(idx
>= array
->rgsabound
[i
].cElements
) {
501 FIXME("out of bound element %d in dim %d of size %d\n", idx
, i
+1, array
->rgsabound
[i
].cElements
);
505 cell_off
+= idx
*dim_size
;
506 dim_size
*= array
->rgsabound
[i
].cElements
;
509 hres
= SafeArrayAccessData(array
, (void**)&data
);
513 *ret
= data
+cell_off
;
515 SafeArrayUnaccessData(array
);
519 static HRESULT
do_icall(exec_ctx_t
*ctx
, VARIANT
*res
)
521 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
522 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
527 hres
= lookup_identifier(ctx
, identifier
, VBDISP_CALLGET
, &ref
);
537 FIXME("REF_VAR no res\n");
541 v
= V_VT(ref
.u
.v
) == (VT_VARIANT
|VT_BYREF
) ? V_VARIANTREF(ref
.u
.v
) : ref
.u
.v
;
544 SAFEARRAY
*array
= NULL
;
547 case VT_ARRAY
|VT_BYREF
|VT_VARIANT
:
548 array
= *V_ARRAYREF(ref
.u
.v
);
550 case VT_ARRAY
|VT_VARIANT
:
551 array
= V_ARRAY(ref
.u
.v
);
554 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
555 hres
= disp_call(ctx
->script
, V_DISPATCH(v
), DISPID_VALUE
, &dp
, res
);
560 FIXME("arguments not implemented\n");
567 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
568 hres
= array_access(ctx
, array
, &dp
, &v
);
573 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
578 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
579 hres
= disp_call(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, &dp
, res
);
584 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
585 hres
= exec_script(ctx
->script
, ref
.u
.f
, NULL
, &dp
, res
);
591 FIXME("arguments on object\n");
596 IDispatch_AddRef(ref
.u
.obj
);
597 V_VT(res
) = VT_DISPATCH
;
598 V_DISPATCH(res
) = ref
.u
.obj
;
602 if(res
&& !ctx
->func
->code_ctx
->option_explicit
&& arg_cnt
== 0) {
604 hres
= add_dynamic_var(ctx
, identifier
, FALSE
, &new);
607 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
611 FIXME("%s not found\n", debugstr_w(identifier
));
612 return DISP_E_UNKNOWNNAME
;
615 stack_popn(ctx
, arg_cnt
);
619 static HRESULT
interp_icall(exec_ctx_t
*ctx
)
626 hres
= do_icall(ctx
, &v
);
630 return stack_push(ctx
, &v
);
633 static HRESULT
interp_icallv(exec_ctx_t
*ctx
)
636 return do_icall(ctx
, NULL
);
639 static HRESULT
do_mcall(exec_ctx_t
*ctx
, VARIANT
*res
)
641 const BSTR identifier
= ctx
->instr
->arg1
.bstr
;
642 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
648 hres
= stack_pop_disp(ctx
, &obj
);
657 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
659 hres
= disp_get_id(obj
, identifier
, VBDISP_CALLGET
, FALSE
, &id
);
661 hres
= disp_call(ctx
->script
, obj
, id
, &dp
, res
);
662 IDispatch_Release(obj
);
666 stack_popn(ctx
, arg_cnt
);
670 static HRESULT
interp_mcall(exec_ctx_t
*ctx
)
677 hres
= do_mcall(ctx
, &res
);
681 return stack_push(ctx
, &res
);
684 static HRESULT
interp_mcallv(exec_ctx_t
*ctx
)
688 return do_mcall(ctx
, NULL
);
691 static HRESULT
assign_value(exec_ctx_t
*ctx
, VARIANT
*dst
, VARIANT
*src
, WORD flags
)
695 hres
= VariantCopyInd(dst
, src
);
699 if(V_VT(dst
) == VT_DISPATCH
&& !(flags
& DISPATCH_PROPERTYPUTREF
)) {
702 hres
= get_disp_value(ctx
->script
, V_DISPATCH(dst
), &value
);
703 IDispatch_Release(V_DISPATCH(dst
));
713 static HRESULT
assign_ident(exec_ctx_t
*ctx
, BSTR name
, WORD flags
, DISPPARAMS
*dp
)
718 hres
= lookup_identifier(ctx
, name
, VBDISP_LET
, &ref
);
724 VARIANT
*v
= ref
.u
.v
;
726 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
))
732 if(!(V_VT(v
) & VT_ARRAY
)) {
733 FIXME("array assign on type %d\n", V_VT(v
));
738 case VT_ARRAY
|VT_BYREF
|VT_VARIANT
:
739 array
= *V_ARRAYREF(v
);
741 case VT_ARRAY
|VT_VARIANT
:
745 FIXME("Unsupported array type %x\n", V_VT(v
));
750 FIXME("null array\n");
754 hres
= array_access(ctx
, array
, dp
, &v
);
757 }else if(V_VT(v
) == (VT_ARRAY
|VT_BYREF
|VT_VARIANT
)) {
758 FIXME("non-array assign\n");
762 hres
= assign_value(ctx
, v
, dp
->rgvarg
, flags
);
766 hres
= disp_propput(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, flags
, dp
);
769 FIXME("functions not implemented\n");
775 FIXME("REF_CONST\n");
778 if(ctx
->func
->code_ctx
->option_explicit
) {
779 FIXME("throw exception\n");
785 FIXME("arg_cnt %d not supported\n", arg_cnt(dp
));
789 TRACE("creating variable %s\n", debugstr_w(name
));
790 hres
= add_dynamic_var(ctx
, name
, FALSE
, &new_var
);
792 hres
= assign_value(ctx
, new_var
, dp
->rgvarg
, flags
);
799 static HRESULT
interp_assign_ident(exec_ctx_t
*ctx
)
801 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
802 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
806 TRACE("%s\n", debugstr_w(arg
));
808 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
809 hres
= assign_ident(ctx
, arg
, DISPATCH_PROPERTYPUT
, &dp
);
813 stack_popn(ctx
, arg_cnt
+1);
817 static HRESULT
interp_set_ident(exec_ctx_t
*ctx
)
819 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
820 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
824 TRACE("%s\n", debugstr_w(arg
));
827 FIXME("arguments not supported\n");
831 hres
= stack_assume_disp(ctx
, 0, NULL
);
835 vbstack_to_dp(ctx
, 0, TRUE
, &dp
);
836 hres
= assign_ident(ctx
, ctx
->instr
->arg1
.bstr
, DISPATCH_PROPERTYPUTREF
, &dp
);
844 static HRESULT
interp_assign_member(exec_ctx_t
*ctx
)
846 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
847 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
853 TRACE("%s\n", debugstr_w(identifier
));
855 hres
= stack_assume_disp(ctx
, arg_cnt
+1, &obj
);
864 hres
= disp_get_id(obj
, identifier
, VBDISP_LET
, FALSE
, &id
);
865 if(SUCCEEDED(hres
)) {
866 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
867 hres
= disp_propput(ctx
->script
, obj
, id
, DISPATCH_PROPERTYPUT
, &dp
);
872 stack_popn(ctx
, arg_cnt
+2);
876 static HRESULT
interp_set_member(exec_ctx_t
*ctx
)
878 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
879 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
885 TRACE("%s\n", debugstr_w(identifier
));
888 FIXME("arguments not supported\n");
892 hres
= stack_assume_disp(ctx
, 1, &obj
);
901 hres
= stack_assume_disp(ctx
, 0, NULL
);
905 hres
= disp_get_id(obj
, identifier
, VBDISP_SET
, FALSE
, &id
);
906 if(SUCCEEDED(hres
)) {
907 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
908 hres
= disp_propput(ctx
->script
, obj
, id
, DISPATCH_PROPERTYPUTREF
, &dp
);
917 static HRESULT
interp_const(exec_ctx_t
*ctx
)
919 BSTR arg
= ctx
->instr
->arg1
.bstr
;
924 TRACE("%s\n", debugstr_w(arg
));
926 assert(ctx
->func
->type
== FUNC_GLOBAL
);
928 hres
= lookup_identifier(ctx
, arg
, VBDISP_CALLGET
, &ref
);
932 if(ref
.type
!= REF_NONE
) {
933 FIXME("%s already defined\n", debugstr_w(arg
));
937 hres
= stack_assume_val(ctx
, 0);
941 hres
= add_dynamic_var(ctx
, arg
, TRUE
, &v
);
945 *v
= *stack_pop(ctx
);
949 static HRESULT
interp_val(exec_ctx_t
*ctx
)
957 hres
= stack_pop_val(ctx
, &val
);
963 hres
= VariantCopy(&v
, val
.v
);
968 return stack_push(ctx
, val
.owned
? val
.v
: &v
);
971 static HRESULT
interp_pop(exec_ctx_t
*ctx
)
973 const unsigned n
= ctx
->instr
->arg1
.uint
;
981 static HRESULT
interp_new(exec_ctx_t
*ctx
)
983 const WCHAR
*arg
= ctx
->instr
->arg1
.bstr
;
984 class_desc_t
*class_desc
;
989 static const WCHAR regexpW
[] = {'r','e','g','e','x','p',0};
991 TRACE("%s\n", debugstr_w(arg
));
993 if(!strcmpiW(arg
, regexpW
)) {
994 V_VT(&v
) = VT_DISPATCH
;
995 hres
= create_regexp(&V_DISPATCH(&v
));
999 return stack_push(ctx
, &v
);
1002 for(class_desc
= ctx
->script
->classes
; class_desc
; class_desc
= class_desc
->next
) {
1003 if(!strcmpiW(class_desc
->name
, arg
))
1007 FIXME("Class %s not found\n", debugstr_w(arg
));
1011 hres
= create_vbdisp(class_desc
, &obj
);
1015 V_VT(&v
) = VT_DISPATCH
;
1016 V_DISPATCH(&v
) = (IDispatch
*)&obj
->IDispatchEx_iface
;
1017 return stack_push(ctx
, &v
);
1020 static HRESULT
interp_dim(exec_ctx_t
*ctx
)
1022 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
1023 const unsigned array_id
= ctx
->instr
->arg2
.uint
;
1024 const array_desc_t
*array_desc
;
1028 TRACE("%s\n", debugstr_w(ident
));
1030 assert(array_id
< ctx
->func
->array_cnt
);
1032 ctx
->arrays
= heap_alloc_zero(ctx
->func
->array_cnt
* sizeof(SAFEARRAY
*));
1034 return E_OUTOFMEMORY
;
1037 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1039 FIXME("lookup %s failed: %08x\n", debugstr_w(ident
), hres
);
1043 if(ref
.type
!= REF_VAR
) {
1044 FIXME("got ref.type = %d\n", ref
.type
);
1048 if(ctx
->arrays
[array_id
]) {
1049 FIXME("Array already initialized\n");
1053 array_desc
= ctx
->func
->array_descs
+ array_id
;
1054 if(array_desc
->dim_cnt
) {
1055 ctx
->arrays
[array_id
] = SafeArrayCreate(VT_VARIANT
, array_desc
->dim_cnt
, array_desc
->bounds
);
1056 if(!ctx
->arrays
[array_id
])
1057 return E_OUTOFMEMORY
;
1060 V_VT(ref
.u
.v
) = VT_ARRAY
|VT_BYREF
|VT_VARIANT
;
1061 V_ARRAYREF(ref
.u
.v
) = ctx
->arrays
+array_id
;
1065 static HRESULT
interp_step(exec_ctx_t
*ctx
)
1067 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1073 TRACE("%s\n", debugstr_w(ident
));
1075 V_VT(&zero
) = VT_I2
;
1077 hres
= VarCmp(stack_top(ctx
, 0), &zero
, ctx
->script
->lcid
, 0);
1081 gteq_zero
= hres
== VARCMP_GT
|| hres
== VARCMP_EQ
;
1083 hres
= lookup_identifier(ctx
, ident
, VBDISP_ANY
, &ref
);
1087 if(ref
.type
!= REF_VAR
) {
1088 FIXME("%s is not REF_VAR\n", debugstr_w(ident
));
1092 hres
= VarCmp(ref
.u
.v
, stack_top(ctx
, 1), ctx
->script
->lcid
, 0);
1096 if(hres
== VARCMP_EQ
|| hres
== (gteq_zero
? VARCMP_LT
: VARCMP_GT
)) {
1100 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1105 static HRESULT
interp_newenum(exec_ctx_t
*ctx
)
1113 stack_pop_deref(ctx
, &v
);
1114 assert(V_VT(stack_top(ctx
, 0)) == VT_EMPTY
);
1115 r
= stack_top(ctx
, 0);
1118 case VT_DISPATCH
|VT_BYREF
:
1121 DISPPARAMS dp
= {0};
1124 hres
= disp_call(ctx
->script
, V_ISBYREF(v
.v
) ? *V_DISPATCHREF(v
.v
) : V_DISPATCH(v
.v
), DISPID_NEWENUM
, &dp
, &iterv
);
1129 if(V_VT(&iterv
) != VT_UNKNOWN
&& V_VT(&iterv
) != VT_DISPATCH
) {
1130 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv
));
1131 VariantClear(&iterv
);
1135 hres
= IUnknown_QueryInterface(V_UNKNOWN(&iterv
), &IID_IEnumVARIANT
, (void**)&iter
);
1136 IUnknown_Release(V_UNKNOWN(&iterv
));
1138 FIXME("Could not get IEnumVARIANT iface: %08x\n", hres
);
1142 V_VT(r
) = VT_UNKNOWN
;
1143 V_UNKNOWN(r
) = (IUnknown
*)iter
;
1147 FIXME("Unsupported for %s\n", debugstr_variant(v
.v
));
1155 static HRESULT
interp_enumnext(exec_ctx_t
*ctx
)
1157 const unsigned loop_end
= ctx
->instr
->arg1
.uint
;
1158 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1160 DISPPARAMS dp
= {&v
, &propput_dispid
, 1, 1};
1167 if(V_VT(stack_top(ctx
, 0)) == VT_EMPTY
) {
1168 FIXME("uninitialized\n");
1172 assert(V_VT(stack_top(ctx
, 0)) == VT_UNKNOWN
);
1173 iter
= (IEnumVARIANT
*)V_UNKNOWN(stack_top(ctx
, 0));
1175 V_VT(&v
) = VT_EMPTY
;
1176 hres
= IEnumVARIANT_Next(iter
, 1, &v
, NULL
);
1180 do_continue
= hres
== S_OK
;
1181 hres
= assign_ident(ctx
, ident
, DISPATCH_PROPERTYPUT
|DISPATCH_PROPERTYPUTREF
, &dp
);
1190 instr_jmp(ctx
, loop_end
);
1195 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
1197 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1201 instr_jmp(ctx
, arg
);
1205 static HRESULT
interp_jmp_false(exec_ctx_t
*ctx
)
1207 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1213 hres
= stack_pop_bool(ctx
, &b
);
1220 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1224 static HRESULT
interp_jmp_true(exec_ctx_t
*ctx
)
1226 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1232 hres
= stack_pop_bool(ctx
, &b
);
1237 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1243 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
1251 static HRESULT
interp_stop(exec_ctx_t
*ctx
)
1255 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1259 static HRESULT
interp_me(exec_ctx_t
*ctx
)
1265 IDispatch_AddRef(ctx
->this_obj
);
1266 V_VT(&v
) = VT_DISPATCH
;
1267 V_DISPATCH(&v
) = ctx
->this_obj
;
1268 return stack_push(ctx
, &v
);
1271 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
1273 const VARIANT_BOOL arg
= ctx
->instr
->arg1
.lng
;
1276 TRACE("%s\n", arg
? "true" : "false");
1280 return stack_push(ctx
, &v
);
1283 static HRESULT
interp_errmode(exec_ctx_t
*ctx
)
1285 const int err_mode
= ctx
->instr
->arg1
.uint
;
1287 TRACE("%d\n", err_mode
);
1289 ctx
->resume_next
= err_mode
;
1290 ctx
->script
->err_number
= S_OK
;
1294 static HRESULT
interp_string(exec_ctx_t
*ctx
)
1301 V_BSTR(&v
) = SysAllocString(ctx
->instr
->arg1
.str
);
1303 return E_OUTOFMEMORY
;
1305 return stack_push(ctx
, &v
);
1308 static HRESULT
interp_long(exec_ctx_t
*ctx
)
1310 const LONG arg
= ctx
->instr
->arg1
.lng
;
1317 return stack_push(ctx
, &v
);
1320 static HRESULT
interp_short(exec_ctx_t
*ctx
)
1322 const LONG arg
= ctx
->instr
->arg1
.lng
;
1329 return stack_push(ctx
, &v
);
1332 static HRESULT
interp_double(exec_ctx_t
*ctx
)
1334 const DOUBLE
*arg
= ctx
->instr
->arg1
.dbl
;
1337 TRACE("%lf\n", *arg
);
1341 return stack_push(ctx
, &v
);
1344 static HRESULT
interp_empty(exec_ctx_t
*ctx
)
1350 V_VT(&v
) = VT_EMPTY
;
1351 return stack_push(ctx
, &v
);
1354 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1357 return stack_push_null(ctx
);
1360 static HRESULT
interp_nothing(exec_ctx_t
*ctx
)
1366 V_VT(&v
) = VT_DISPATCH
;
1367 V_DISPATCH(&v
) = NULL
;
1368 return stack_push(ctx
, &v
);
1371 static HRESULT
interp_hres(exec_ctx_t
*ctx
)
1373 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1378 V_VT(&v
) = VT_ERROR
;
1380 return stack_push(ctx
, &v
);
1383 static HRESULT
interp_not(exec_ctx_t
*ctx
)
1391 hres
= stack_pop_val(ctx
, &val
);
1395 hres
= VarNot(val
.v
, &v
);
1400 return stack_push(ctx
, &v
);
1403 static HRESULT
interp_and(exec_ctx_t
*ctx
)
1411 hres
= stack_pop_val(ctx
, &r
);
1415 hres
= stack_pop_val(ctx
, &l
);
1416 if(SUCCEEDED(hres
)) {
1417 hres
= VarAnd(l
.v
, r
.v
, &v
);
1424 return stack_push(ctx
, &v
);
1427 static HRESULT
interp_or(exec_ctx_t
*ctx
)
1435 hres
= stack_pop_val(ctx
, &r
);
1439 hres
= stack_pop_val(ctx
, &l
);
1440 if(SUCCEEDED(hres
)) {
1441 hres
= VarOr(l
.v
, r
.v
, &v
);
1448 return stack_push(ctx
, &v
);
1451 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
1459 hres
= stack_pop_val(ctx
, &r
);
1463 hres
= stack_pop_val(ctx
, &l
);
1464 if(SUCCEEDED(hres
)) {
1465 hres
= VarXor(l
.v
, r
.v
, &v
);
1472 return stack_push(ctx
, &v
);
1475 static HRESULT
interp_eqv(exec_ctx_t
*ctx
)
1483 hres
= stack_pop_val(ctx
, &r
);
1487 hres
= stack_pop_val(ctx
, &l
);
1488 if(SUCCEEDED(hres
)) {
1489 hres
= VarEqv(l
.v
, r
.v
, &v
);
1496 return stack_push(ctx
, &v
);
1499 static HRESULT
interp_imp(exec_ctx_t
*ctx
)
1507 hres
= stack_pop_val(ctx
, &r
);
1511 hres
= stack_pop_val(ctx
, &l
);
1512 if(SUCCEEDED(hres
)) {
1513 hres
= VarImp(l
.v
, r
.v
, &v
);
1520 return stack_push(ctx
, &v
);
1523 static HRESULT
var_cmp(exec_ctx_t
*ctx
, VARIANT
*l
, VARIANT
*r
)
1525 TRACE("%s %s\n", debugstr_variant(l
), debugstr_variant(r
));
1527 /* FIXME: Fix comparing string to number */
1529 return VarCmp(l
, r
, ctx
->script
->lcid
, 0);
1532 static HRESULT
cmp_oper(exec_ctx_t
*ctx
)
1537 hres
= stack_pop_val(ctx
, &r
);
1541 hres
= stack_pop_val(ctx
, &l
);
1542 if(SUCCEEDED(hres
)) {
1543 hres
= var_cmp(ctx
, l
.v
, r
.v
);
1551 static HRESULT
interp_equal(exec_ctx_t
*ctx
)
1558 hres
= cmp_oper(ctx
);
1561 if(hres
== VARCMP_NULL
)
1562 return stack_push_null(ctx
);
1565 V_BOOL(&v
) = hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1566 return stack_push(ctx
, &v
);
1569 static HRESULT
interp_nequal(exec_ctx_t
*ctx
)
1576 hres
= cmp_oper(ctx
);
1579 if(hres
== VARCMP_NULL
)
1580 return stack_push_null(ctx
);
1583 V_BOOL(&v
) = hres
!= VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1584 return stack_push(ctx
, &v
);
1587 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
1594 hres
= cmp_oper(ctx
);
1597 if(hres
== VARCMP_NULL
)
1598 return stack_push_null(ctx
);
1601 V_BOOL(&v
) = hres
== VARCMP_GT
? VARIANT_TRUE
: VARIANT_FALSE
;
1602 return stack_push(ctx
, &v
);
1605 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
1612 hres
= cmp_oper(ctx
);
1615 if(hres
== VARCMP_NULL
)
1616 return stack_push_null(ctx
);
1619 V_BOOL(&v
) = hres
== VARCMP_GT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1620 return stack_push(ctx
, &v
);
1623 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
1630 hres
= cmp_oper(ctx
);
1633 if(hres
== VARCMP_NULL
)
1634 return stack_push_null(ctx
);
1637 V_BOOL(&v
) = hres
== VARCMP_LT
? VARIANT_TRUE
: VARIANT_FALSE
;
1638 return stack_push(ctx
, &v
);
1641 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
1648 hres
= cmp_oper(ctx
);
1651 if(hres
== VARCMP_NULL
)
1652 return stack_push_null(ctx
);
1655 V_BOOL(&v
) = hres
== VARCMP_LT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1656 return stack_push(ctx
, &v
);
1659 static HRESULT
interp_case(exec_ctx_t
*ctx
)
1661 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1667 hres
= stack_pop_val(ctx
, &v
);
1671 hres
= var_cmp(ctx
, stack_top(ctx
, 0), v
.v
);
1676 if(hres
== VARCMP_EQ
) {
1678 instr_jmp(ctx
, arg
);
1686 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, VARIANT_BOOL
*ret
)
1688 IObjectIdentity
*identity
;
1689 IUnknown
*unk1
, *unk2
;
1692 if(disp1
== disp2
) {
1693 *ret
= VARIANT_TRUE
;
1697 if(!disp1
|| !disp2
) {
1698 *ret
= VARIANT_FALSE
;
1702 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
1706 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
1708 IUnknown_Release(unk1
);
1713 *ret
= VARIANT_TRUE
;
1715 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
1716 if(SUCCEEDED(hres
)) {
1717 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
1718 IObjectIdentity_Release(identity
);
1719 *ret
= hres
== S_OK
? VARIANT_TRUE
: VARIANT_FALSE
;
1721 *ret
= VARIANT_FALSE
;
1725 IUnknown_Release(unk1
);
1726 IUnknown_Release(unk2
);
1730 static HRESULT
interp_is(exec_ctx_t
*ctx
)
1738 hres
= stack_pop_disp(ctx
, &r
);
1742 hres
= stack_pop_disp(ctx
, &l
);
1743 if(SUCCEEDED(hres
)) {
1745 hres
= disp_cmp(l
, r
, &V_BOOL(&v
));
1747 IDispatch_Release(l
);
1750 IDispatch_Release(r
);
1754 return stack_push(ctx
, &v
);
1757 static HRESULT
interp_concat(exec_ctx_t
*ctx
)
1765 hres
= stack_pop_val(ctx
, &r
);
1769 hres
= stack_pop_val(ctx
, &l
);
1770 if(SUCCEEDED(hres
)) {
1771 hres
= VarCat(l
.v
, r
.v
, &v
);
1778 return stack_push(ctx
, &v
);
1781 static HRESULT
interp_add(exec_ctx_t
*ctx
)
1789 hres
= stack_pop_val(ctx
, &r
);
1793 hres
= stack_pop_val(ctx
, &l
);
1794 if(SUCCEEDED(hres
)) {
1795 hres
= VarAdd(l
.v
, r
.v
, &v
);
1802 return stack_push(ctx
, &v
);
1805 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
1813 hres
= stack_pop_val(ctx
, &r
);
1817 hres
= stack_pop_val(ctx
, &l
);
1818 if(SUCCEEDED(hres
)) {
1819 hres
= VarSub(l
.v
, r
.v
, &v
);
1826 return stack_push(ctx
, &v
);
1829 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
1837 hres
= stack_pop_val(ctx
, &r
);
1841 hres
= stack_pop_val(ctx
, &l
);
1842 if(SUCCEEDED(hres
)) {
1843 hres
= VarMod(l
.v
, r
.v
, &v
);
1850 return stack_push(ctx
, &v
);
1853 static HRESULT
interp_idiv(exec_ctx_t
*ctx
)
1861 hres
= stack_pop_val(ctx
, &r
);
1865 hres
= stack_pop_val(ctx
, &l
);
1866 if(SUCCEEDED(hres
)) {
1867 hres
= VarIdiv(l
.v
, r
.v
, &v
);
1874 return stack_push(ctx
, &v
);
1877 static HRESULT
interp_div(exec_ctx_t
*ctx
)
1885 hres
= stack_pop_val(ctx
, &r
);
1889 hres
= stack_pop_val(ctx
, &l
);
1890 if(SUCCEEDED(hres
)) {
1891 hres
= VarDiv(l
.v
, r
.v
, &v
);
1898 return stack_push(ctx
, &v
);
1901 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
1909 hres
= stack_pop_val(ctx
, &r
);
1913 hres
= stack_pop_val(ctx
, &l
);
1914 if(SUCCEEDED(hres
)) {
1915 hres
= VarMul(l
.v
, r
.v
, &v
);
1922 return stack_push(ctx
, &v
);
1925 static HRESULT
interp_exp(exec_ctx_t
*ctx
)
1933 hres
= stack_pop_val(ctx
, &r
);
1937 hres
= stack_pop_val(ctx
, &l
);
1938 if(SUCCEEDED(hres
)) {
1939 hres
= VarPow(l
.v
, r
.v
, &v
);
1946 return stack_push(ctx
, &v
);
1949 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
1955 hres
= stack_pop_val(ctx
, &val
);
1959 hres
= VarNeg(val
.v
, &v
);
1964 return stack_push(ctx
, &v
);
1967 static HRESULT
interp_incc(exec_ctx_t
*ctx
)
1969 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
1976 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1980 if(ref
.type
!= REF_VAR
) {
1981 FIXME("ref.type is not REF_VAR\n");
1985 hres
= VarAdd(stack_top(ctx
, 0), ref
.u
.v
, &v
);
1989 VariantClear(ref
.u
.v
);
1994 static HRESULT
interp_catch(exec_ctx_t
*ctx
)
1996 /* Nothing to do here, the OP is for unwinding only. */
2000 static const instr_func_t op_funcs
[] = {
2001 #define X(x,n,a,b) interp_ ## x,
2006 static const unsigned op_move
[] = {
2007 #define X(x,n,a,b) n,
2012 void release_dynamic_vars(dynamic_var_t
*var
)
2015 VariantClear(&var
->v
);
2020 static void release_exec(exec_ctx_t
*ctx
)
2024 VariantClear(&ctx
->ret_val
);
2025 release_dynamic_vars(ctx
->dynamic_vars
);
2028 IDispatch_Release(ctx
->this_obj
);
2031 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++)
2032 VariantClear(ctx
->args
+i
);
2036 for(i
=0; i
< ctx
->func
->var_cnt
; i
++)
2037 VariantClear(ctx
->vars
+i
);
2041 for(i
=0; i
< ctx
->func
->var_cnt
; i
++) {
2043 SafeArrayDestroy(ctx
->arrays
[i
]);
2045 heap_free(ctx
->arrays
);
2048 heap_pool_free(&ctx
->heap
);
2049 heap_free(ctx
->args
);
2050 heap_free(ctx
->vars
);
2051 heap_free(ctx
->stack
);
2054 HRESULT
exec_script(script_ctx_t
*ctx
, function_t
*func
, vbdisp_t
*vbthis
, DISPPARAMS
*dp
, VARIANT
*res
)
2056 exec_ctx_t exec
= {func
->code_ctx
};
2058 HRESULT hres
= S_OK
;
2060 exec
.code
= func
->code_ctx
;
2062 if(dp
? func
->arg_cnt
!= arg_cnt(dp
) : func
->arg_cnt
) {
2063 FIXME("wrong arg_cnt %d, expected %d\n", dp
? arg_cnt(dp
) : 0, func
->arg_cnt
);
2067 heap_pool_init(&exec
.heap
);
2073 exec
.args
= heap_alloc_zero(func
->arg_cnt
* sizeof(VARIANT
));
2075 release_exec(&exec
);
2076 return E_OUTOFMEMORY
;
2079 for(i
=0; i
< func
->arg_cnt
; i
++) {
2081 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
)) {
2082 if(func
->args
[i
].by_ref
)
2085 hres
= VariantCopyInd(exec
.args
+i
, V_VARIANTREF(v
));
2087 hres
= VariantCopyInd(exec
.args
+i
, v
);
2090 release_exec(&exec
);
2099 exec
.vars
= heap_alloc_zero(func
->var_cnt
* sizeof(VARIANT
));
2101 release_exec(&exec
);
2102 return E_OUTOFMEMORY
;
2108 exec
.stack_size
= 16;
2110 exec
.stack
= heap_alloc(exec
.stack_size
* sizeof(VARIANT
));
2112 release_exec(&exec
);
2113 return E_OUTOFMEMORY
;
2117 exec
.this_obj
= (IDispatch
*)&vbthis
->IDispatchEx_iface
;
2118 exec
.vbthis
= vbthis
;
2119 }else if (ctx
->host_global
) {
2120 exec
.this_obj
= ctx
->host_global
;
2122 exec
.this_obj
= (IDispatch
*)&ctx
->script_obj
->IDispatchEx_iface
;
2124 IDispatch_AddRef(exec
.this_obj
);
2126 exec
.instr
= exec
.code
->instrs
+ func
->code_off
;
2131 op
= exec
.instr
->op
;
2132 hres
= op_funcs
[op
](&exec
);
2134 ctx
->err_number
= hres
= map_hres(hres
);
2136 if(exec
.resume_next
) {
2139 WARN("Failed %08x in resume next mode\n", hres
);
2142 * Unwinding here is simple. We need to find the next OP_catch, which contains
2143 * information about expected stack size and jump offset on error. Generated
2144 * bytecode needs to guarantee, that simple jump and stack adjustment will
2145 * guarantee proper execution continuation.
2147 while((++exec
.instr
)->op
!= OP_catch
);
2149 TRACE("unwind jmp %d stack_off %d\n", exec
.instr
->arg1
.uint
, exec
.instr
->arg2
.uint
);
2151 stack_off
= exec
.instr
->arg2
.uint
;
2152 instr_jmp(&exec
, exec
.instr
->arg1
.uint
);
2154 if(exec
.top
> stack_off
) {
2155 stack_popn(&exec
, exec
.top
-stack_off
);
2156 }else if(exec
.top
< stack_off
) {
2159 V_VT(&v
) = VT_EMPTY
;
2160 while(exec
.top
< stack_off
) {
2161 hres
= stack_push(&exec
, &v
);
2169 WARN("Failed %08x\n", hres
);
2170 stack_popn(&exec
, exec
.top
);
2175 exec
.instr
+= op_move
[op
];
2179 if(func
->type
!= FUNC_FUNCTION
&& func
->type
!= FUNC_PROPGET
&& func
->type
!= FUNC_DEFGET
)
2180 assert(V_VT(&exec
.ret_val
) == VT_EMPTY
);
2182 if(SUCCEEDED(hres
) && res
) {
2183 *res
= exec
.ret_val
;
2184 V_VT(&exec
.ret_val
) = VT_EMPTY
;
2187 release_exec(&exec
);