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
);
27 static DISPID propput_dispid
= DISPID_PROPERTYPUT
;
41 dynamic_var_t
*dynamic_vars
;
53 typedef HRESULT (*instr_func_t
)(exec_ctx_t
*);
83 static BOOL
lookup_dynamic_vars(dynamic_var_t
*var
, const WCHAR
*name
, ref_t
*ref
)
86 if(!strcmpiW(var
->name
, name
)) {
87 ref
->type
= var
->is_const
? REF_CONST
: REF_VAR
;
98 static HRESULT
lookup_identifier(exec_ctx_t
*ctx
, BSTR name
, vbdisp_invoke_type_t invoke_type
, ref_t
*ref
)
107 static const WCHAR errW
[] = {'e','r','r',0};
109 if(invoke_type
== VBDISP_LET
110 && (ctx
->func
->type
== FUNC_FUNCTION
|| ctx
->func
->type
== FUNC_PROPGET
|| ctx
->func
->type
== FUNC_DEFGET
)
111 && !strcmpiW(name
, ctx
->func
->name
)) {
113 ref
->u
.v
= &ctx
->ret_val
;
117 for(i
=0; i
< ctx
->func
->var_cnt
; i
++) {
118 if(!strcmpiW(ctx
->func
->vars
[i
].name
, name
)) {
120 ref
->u
.v
= ctx
->vars
+i
;
125 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++) {
126 if(!strcmpiW(ctx
->func
->args
[i
].name
, name
)) {
128 ref
->u
.v
= ctx
->args
+i
;
133 if(lookup_dynamic_vars(ctx
->func
->type
== FUNC_GLOBAL
? ctx
->script
->global_vars
: ctx
->dynamic_vars
, name
, ref
))
136 if(ctx
->func
->type
!= FUNC_GLOBAL
) {
138 /* FIXME: Bind such identifier while generating bytecode. */
139 for(i
=0; i
< ctx
->vbthis
->desc
->prop_cnt
; i
++) {
140 if(!strcmpiW(ctx
->vbthis
->desc
->props
[i
].name
, name
)) {
142 ref
->u
.v
= ctx
->vbthis
->props
+i
;
148 hres
= disp_get_id(ctx
->this_obj
, name
, invoke_type
, TRUE
, &id
);
149 if(SUCCEEDED(hres
)) {
150 ref
->type
= REF_DISP
;
151 ref
->u
.d
.disp
= ctx
->this_obj
;
157 if(ctx
->func
->code_ctx
->context
) {
158 hres
= disp_get_id(ctx
->func
->code_ctx
->context
, name
, invoke_type
, TRUE
, &id
);
159 if(SUCCEEDED(hres
)) {
160 ref
->type
= REF_DISP
;
161 ref
->u
.d
.disp
= ctx
->func
->code_ctx
->context
;
167 if(ctx
->func
->type
!= FUNC_GLOBAL
&& lookup_dynamic_vars(ctx
->script
->global_vars
, name
, ref
))
170 for(func
= ctx
->script
->global_funcs
; func
; func
= func
->next
) {
171 if(!strcmpiW(func
->name
, name
)) {
172 ref
->type
= REF_FUNC
;
178 if(!strcmpiW(name
, errW
)) {
180 ref
->u
.obj
= (IDispatch
*)&ctx
->script
->err_obj
->IDispatchEx_iface
;
184 hres
= vbdisp_get_id(ctx
->script
->global_obj
, name
, invoke_type
, TRUE
, &id
);
185 if(SUCCEEDED(hres
)) {
186 ref
->type
= REF_DISP
;
187 ref
->u
.d
.disp
= (IDispatch
*)&ctx
->script
->global_obj
->IDispatchEx_iface
;
192 disp
= lookup_named_item(ctx
->script
, name
, SCRIPTITEM_ISVISIBLE
);
199 LIST_FOR_EACH_ENTRY(item
, &ctx
->script
->named_items
, named_item_t
, entry
) {
200 if((item
->flags
& SCRIPTITEM_GLOBALMEMBERS
)) {
201 hres
= disp_get_id(item
->disp
, name
, invoke_type
, FALSE
, &id
);
202 if(SUCCEEDED(hres
)) {
203 ref
->type
= REF_DISP
;
204 ref
->u
.d
.disp
= item
->disp
;
211 ref
->type
= REF_NONE
;
215 static HRESULT
add_dynamic_var(exec_ctx_t
*ctx
, const WCHAR
*name
,
216 BOOL is_const
, VARIANT
**out_var
)
218 dynamic_var_t
*new_var
;
223 heap
= ctx
->func
->type
== FUNC_GLOBAL
? &ctx
->script
->heap
: &ctx
->heap
;
225 new_var
= heap_pool_alloc(heap
, sizeof(*new_var
));
227 return E_OUTOFMEMORY
;
229 size
= (strlenW(name
)+1)*sizeof(WCHAR
);
230 str
= heap_pool_alloc(heap
, size
);
232 return E_OUTOFMEMORY
;
233 memcpy(str
, name
, size
);
235 new_var
->is_const
= is_const
;
236 V_VT(&new_var
->v
) = VT_EMPTY
;
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
;
246 *out_var
= &new_var
->v
;
250 static inline VARIANT
*stack_pop(exec_ctx_t
*ctx
)
253 return ctx
->stack
+ --ctx
->top
;
256 static inline VARIANT
*stack_top(exec_ctx_t
*ctx
, unsigned n
)
258 assert(ctx
->top
>= n
);
259 return ctx
->stack
+ (ctx
->top
-n
-1);
262 static HRESULT
stack_push(exec_ctx_t
*ctx
, VARIANT
*v
)
264 if(ctx
->stack_size
== ctx
->top
) {
267 new_stack
= heap_realloc(ctx
->stack
, ctx
->stack_size
*2*sizeof(*ctx
->stack
));
270 return E_OUTOFMEMORY
;
273 ctx
->stack
= new_stack
;
274 ctx
->stack_size
*= 2;
277 ctx
->stack
[ctx
->top
++] = *v
;
281 static inline HRESULT
stack_push_null(exec_ctx_t
*ctx
)
285 return stack_push(ctx
, &v
);
288 static void stack_popn(exec_ctx_t
*ctx
, unsigned n
)
291 VariantClear(stack_pop(ctx
));
294 static void stack_pop_deref(exec_ctx_t
*ctx
, variant_val_t
*r
)
299 if(V_VT(v
) == (VT_BYREF
|VT_VARIANT
)) {
301 r
->v
= V_VARIANTREF(v
);
308 static inline void release_val(variant_val_t
*v
)
314 static HRESULT
stack_pop_val(exec_ctx_t
*ctx
, variant_val_t
*r
)
316 stack_pop_deref(ctx
, r
);
318 if(V_VT(r
->v
) == VT_DISPATCH
) {
321 hres
= get_disp_value(ctx
->script
, V_DISPATCH(r
->v
), &r
->store
);
323 IDispatch_Release(V_DISPATCH(r
->v
));
334 static HRESULT
stack_assume_val(exec_ctx_t
*ctx
, unsigned n
)
336 VARIANT
*v
= stack_top(ctx
, n
);
339 if(V_VT(v
) == (VT_BYREF
|VT_VARIANT
)) {
340 VARIANT
*ref
= V_VARIANTREF(v
);
343 hres
= VariantCopy(v
, ref
);
348 if(V_VT(v
) == VT_DISPATCH
) {
351 disp
= V_DISPATCH(v
);
352 hres
= get_disp_value(ctx
->script
, disp
, v
);
353 IDispatch_Release(disp
);
361 static int stack_pop_bool(exec_ctx_t
*ctx
, BOOL
*b
)
366 hres
= stack_pop_val(ctx
, &val
);
385 FIXME("unsupported for %s\n", debugstr_variant(val
.v
));
392 static HRESULT
stack_pop_disp(exec_ctx_t
*ctx
, IDispatch
**ret
)
394 VARIANT
*v
= stack_pop(ctx
);
396 if(V_VT(v
) == VT_DISPATCH
) {
397 *ret
= V_DISPATCH(v
);
401 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
402 FIXME("not supported type: %s\n", debugstr_variant(v
));
408 if(V_VT(v
) != VT_DISPATCH
) {
409 FIXME("not disp %s\n", debugstr_variant(v
));
414 IDispatch_AddRef(V_DISPATCH(v
));
415 *ret
= V_DISPATCH(v
);
419 static HRESULT
stack_assume_disp(exec_ctx_t
*ctx
, unsigned n
, IDispatch
**disp
)
421 VARIANT
*v
= stack_top(ctx
, n
), *ref
;
423 if(V_VT(v
) != VT_DISPATCH
) {
424 if(V_VT(v
) != (VT_VARIANT
|VT_BYREF
)) {
425 FIXME("not supported type: %s\n", debugstr_variant(v
));
429 ref
= V_VARIANTREF(v
);
430 if(V_VT(ref
) != VT_DISPATCH
) {
431 FIXME("not disp %s\n", debugstr_variant(ref
));
435 V_VT(v
) = VT_DISPATCH
;
436 V_DISPATCH(v
) = V_DISPATCH(ref
);
438 IDispatch_AddRef(V_DISPATCH(v
));
442 *disp
= V_DISPATCH(v
);
446 static inline void instr_jmp(exec_ctx_t
*ctx
, unsigned addr
)
448 ctx
->instr
= ctx
->code
->instrs
+ addr
;
451 static void vbstack_to_dp(exec_ctx_t
*ctx
, unsigned arg_cnt
, BOOL is_propput
, DISPPARAMS
*dp
)
453 dp
->cNamedArgs
= is_propput
? 1 : 0;
454 dp
->cArgs
= arg_cnt
+ dp
->cNamedArgs
;
455 dp
->rgdispidNamedArgs
= is_propput
? &propput_dispid
: NULL
;
461 assert(ctx
->top
>= arg_cnt
);
463 for(i
=1; i
*2 <= arg_cnt
; i
++) {
464 tmp
= ctx
->stack
[ctx
->top
-i
];
465 ctx
->stack
[ctx
->top
-i
] = ctx
->stack
[ctx
->top
-arg_cnt
+i
-1];
466 ctx
->stack
[ctx
->top
-arg_cnt
+i
-1] = tmp
;
469 dp
->rgvarg
= ctx
->stack
+ ctx
->top
-dp
->cArgs
;
471 dp
->rgvarg
= is_propput
? ctx
->stack
+ctx
->top
-1 : NULL
;
475 static HRESULT
array_access(exec_ctx_t
*ctx
, SAFEARRAY
*array
, DISPPARAMS
*dp
, VARIANT
**ret
)
477 unsigned i
, argc
= arg_cnt(dp
);
482 FIXME("NULL array\n");
486 hres
= SafeArrayLock(array
);
490 if(array
->cDims
!= argc
) {
491 FIXME("argc %d does not match cDims %d\n", dp
->cArgs
, array
->cDims
);
492 SafeArrayUnlock(array
);
496 indices
= heap_alloc(sizeof(*indices
) * argc
);
498 SafeArrayUnlock(array
);
499 return E_OUTOFMEMORY
;
502 for(i
=0; i
<argc
; i
++) {
503 hres
= to_int(get_arg(dp
, i
), indices
+i
);
506 SafeArrayUnlock(array
);
511 hres
= SafeArrayPtrOfIndex(array
, indices
, (void**)ret
);
512 SafeArrayUnlock(array
);
517 static HRESULT
do_icall(exec_ctx_t
*ctx
, VARIANT
*res
)
519 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
520 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
525 hres
= lookup_identifier(ctx
, identifier
, VBDISP_CALLGET
, &ref
);
535 FIXME("REF_VAR no res\n");
539 v
= V_VT(ref
.u
.v
) == (VT_VARIANT
|VT_BYREF
) ? V_VARIANTREF(ref
.u
.v
) : ref
.u
.v
;
542 SAFEARRAY
*array
= NULL
;
545 case VT_ARRAY
|VT_BYREF
|VT_VARIANT
:
546 array
= *V_ARRAYREF(ref
.u
.v
);
548 case VT_ARRAY
|VT_VARIANT
:
549 array
= V_ARRAY(ref
.u
.v
);
552 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
553 hres
= disp_call(ctx
->script
, V_DISPATCH(v
), DISPID_VALUE
, &dp
, res
);
558 FIXME("arguments not implemented\n");
565 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
566 hres
= array_access(ctx
, array
, &dp
, &v
);
571 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
576 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
577 hres
= disp_call(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, &dp
, res
);
582 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
583 hres
= exec_script(ctx
->script
, ref
.u
.f
, NULL
, &dp
, res
);
589 FIXME("arguments on object\n");
594 IDispatch_AddRef(ref
.u
.obj
);
595 V_VT(res
) = VT_DISPATCH
;
596 V_DISPATCH(res
) = ref
.u
.obj
;
600 if(res
&& !ctx
->func
->code_ctx
->option_explicit
&& arg_cnt
== 0) {
602 hres
= add_dynamic_var(ctx
, identifier
, FALSE
, &new);
605 V_VT(res
) = VT_BYREF
|VT_VARIANT
;
609 FIXME("%s not found\n", debugstr_w(identifier
));
610 return DISP_E_UNKNOWNNAME
;
613 stack_popn(ctx
, arg_cnt
);
617 static HRESULT
interp_icall(exec_ctx_t
*ctx
)
624 hres
= do_icall(ctx
, &v
);
628 return stack_push(ctx
, &v
);
631 static HRESULT
interp_icallv(exec_ctx_t
*ctx
)
634 return do_icall(ctx
, NULL
);
637 static HRESULT
do_mcall(exec_ctx_t
*ctx
, VARIANT
*res
)
639 const BSTR identifier
= ctx
->instr
->arg1
.bstr
;
640 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
646 hres
= stack_pop_disp(ctx
, &obj
);
655 vbstack_to_dp(ctx
, arg_cnt
, FALSE
, &dp
);
657 hres
= disp_get_id(obj
, identifier
, VBDISP_CALLGET
, FALSE
, &id
);
659 hres
= disp_call(ctx
->script
, obj
, id
, &dp
, res
);
660 IDispatch_Release(obj
);
664 stack_popn(ctx
, arg_cnt
);
668 static HRESULT
interp_mcall(exec_ctx_t
*ctx
)
675 hres
= do_mcall(ctx
, &res
);
679 return stack_push(ctx
, &res
);
682 static HRESULT
interp_mcallv(exec_ctx_t
*ctx
)
686 return do_mcall(ctx
, NULL
);
689 static HRESULT
assign_value(exec_ctx_t
*ctx
, VARIANT
*dst
, VARIANT
*src
, WORD flags
)
693 hres
= VariantCopyInd(dst
, src
);
697 if(V_VT(dst
) == VT_DISPATCH
&& !(flags
& DISPATCH_PROPERTYPUTREF
)) {
700 hres
= get_disp_value(ctx
->script
, V_DISPATCH(dst
), &value
);
701 IDispatch_Release(V_DISPATCH(dst
));
711 static HRESULT
assign_ident(exec_ctx_t
*ctx
, BSTR name
, WORD flags
, DISPPARAMS
*dp
)
716 hres
= lookup_identifier(ctx
, name
, VBDISP_LET
, &ref
);
722 VARIANT
*v
= ref
.u
.v
;
724 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
))
730 if(!(V_VT(v
) & VT_ARRAY
)) {
731 FIXME("array assign on type %d\n", V_VT(v
));
736 case VT_ARRAY
|VT_BYREF
|VT_VARIANT
:
737 array
= *V_ARRAYREF(v
);
739 case VT_ARRAY
|VT_VARIANT
:
743 FIXME("Unsupported array type %x\n", V_VT(v
));
748 FIXME("null array\n");
752 hres
= array_access(ctx
, array
, dp
, &v
);
755 }else if(V_VT(v
) == (VT_ARRAY
|VT_BYREF
|VT_VARIANT
)) {
756 FIXME("non-array assign\n");
760 hres
= assign_value(ctx
, v
, dp
->rgvarg
, flags
);
764 hres
= disp_propput(ctx
->script
, ref
.u
.d
.disp
, ref
.u
.d
.id
, flags
, dp
);
767 FIXME("functions not implemented\n");
773 FIXME("REF_CONST\n");
776 if(ctx
->func
->code_ctx
->option_explicit
) {
777 FIXME("throw exception\n");
783 FIXME("arg_cnt %d not supported\n", arg_cnt(dp
));
787 TRACE("creating variable %s\n", debugstr_w(name
));
788 hres
= add_dynamic_var(ctx
, name
, FALSE
, &new_var
);
790 hres
= assign_value(ctx
, new_var
, dp
->rgvarg
, flags
);
797 static HRESULT
interp_assign_ident(exec_ctx_t
*ctx
)
799 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
800 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
804 TRACE("%s\n", debugstr_w(arg
));
806 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
807 hres
= assign_ident(ctx
, arg
, DISPATCH_PROPERTYPUT
, &dp
);
811 stack_popn(ctx
, arg_cnt
+1);
815 static HRESULT
interp_set_ident(exec_ctx_t
*ctx
)
817 const BSTR arg
= ctx
->instr
->arg1
.bstr
;
818 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
822 TRACE("%s\n", debugstr_w(arg
));
825 FIXME("arguments not supported\n");
829 hres
= stack_assume_disp(ctx
, 0, NULL
);
833 vbstack_to_dp(ctx
, 0, TRUE
, &dp
);
834 hres
= assign_ident(ctx
, ctx
->instr
->arg1
.bstr
, DISPATCH_PROPERTYPUTREF
, &dp
);
842 static HRESULT
interp_assign_member(exec_ctx_t
*ctx
)
844 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
845 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
851 TRACE("%s\n", debugstr_w(identifier
));
853 hres
= stack_assume_disp(ctx
, arg_cnt
+1, &obj
);
862 hres
= disp_get_id(obj
, identifier
, VBDISP_LET
, FALSE
, &id
);
863 if(SUCCEEDED(hres
)) {
864 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
865 hres
= disp_propput(ctx
->script
, obj
, id
, DISPATCH_PROPERTYPUT
, &dp
);
870 stack_popn(ctx
, arg_cnt
+2);
874 static HRESULT
interp_set_member(exec_ctx_t
*ctx
)
876 BSTR identifier
= ctx
->instr
->arg1
.bstr
;
877 const unsigned arg_cnt
= ctx
->instr
->arg2
.uint
;
883 TRACE("%s\n", debugstr_w(identifier
));
886 FIXME("arguments not supported\n");
890 hres
= stack_assume_disp(ctx
, 1, &obj
);
899 hres
= stack_assume_disp(ctx
, 0, NULL
);
903 hres
= disp_get_id(obj
, identifier
, VBDISP_SET
, FALSE
, &id
);
904 if(SUCCEEDED(hres
)) {
905 vbstack_to_dp(ctx
, arg_cnt
, TRUE
, &dp
);
906 hres
= disp_propput(ctx
->script
, obj
, id
, DISPATCH_PROPERTYPUTREF
, &dp
);
915 static HRESULT
interp_const(exec_ctx_t
*ctx
)
917 BSTR arg
= ctx
->instr
->arg1
.bstr
;
922 TRACE("%s\n", debugstr_w(arg
));
924 assert(ctx
->func
->type
== FUNC_GLOBAL
);
926 hres
= lookup_identifier(ctx
, arg
, VBDISP_CALLGET
, &ref
);
930 if(ref
.type
!= REF_NONE
) {
931 FIXME("%s already defined\n", debugstr_w(arg
));
935 hres
= stack_assume_val(ctx
, 0);
939 hres
= add_dynamic_var(ctx
, arg
, TRUE
, &v
);
943 *v
= *stack_pop(ctx
);
947 static HRESULT
interp_val(exec_ctx_t
*ctx
)
955 hres
= stack_pop_val(ctx
, &val
);
961 hres
= VariantCopy(&v
, val
.v
);
966 return stack_push(ctx
, val
.owned
? val
.v
: &v
);
969 static HRESULT
interp_pop(exec_ctx_t
*ctx
)
971 const unsigned n
= ctx
->instr
->arg1
.uint
;
979 static HRESULT
interp_new(exec_ctx_t
*ctx
)
981 const WCHAR
*arg
= ctx
->instr
->arg1
.bstr
;
982 class_desc_t
*class_desc
;
987 static const WCHAR regexpW
[] = {'r','e','g','e','x','p',0};
989 TRACE("%s\n", debugstr_w(arg
));
991 if(!strcmpiW(arg
, regexpW
)) {
992 V_VT(&v
) = VT_DISPATCH
;
993 hres
= create_regexp(&V_DISPATCH(&v
));
997 return stack_push(ctx
, &v
);
1000 for(class_desc
= ctx
->script
->classes
; class_desc
; class_desc
= class_desc
->next
) {
1001 if(!strcmpiW(class_desc
->name
, arg
))
1005 FIXME("Class %s not found\n", debugstr_w(arg
));
1009 hres
= create_vbdisp(class_desc
, &obj
);
1013 V_VT(&v
) = VT_DISPATCH
;
1014 V_DISPATCH(&v
) = (IDispatch
*)&obj
->IDispatchEx_iface
;
1015 return stack_push(ctx
, &v
);
1018 static HRESULT
interp_dim(exec_ctx_t
*ctx
)
1020 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
1021 const unsigned array_id
= ctx
->instr
->arg2
.uint
;
1022 const array_desc_t
*array_desc
;
1026 TRACE("%s\n", debugstr_w(ident
));
1028 assert(array_id
< ctx
->func
->array_cnt
);
1030 ctx
->arrays
= heap_alloc_zero(ctx
->func
->array_cnt
* sizeof(SAFEARRAY
*));
1032 return E_OUTOFMEMORY
;
1035 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1037 FIXME("lookup %s failed: %08x\n", debugstr_w(ident
), hres
);
1041 if(ref
.type
!= REF_VAR
) {
1042 FIXME("got ref.type = %d\n", ref
.type
);
1046 if(ctx
->arrays
[array_id
]) {
1047 FIXME("Array already initialized\n");
1051 array_desc
= ctx
->func
->array_descs
+ array_id
;
1052 if(array_desc
->dim_cnt
) {
1053 ctx
->arrays
[array_id
] = SafeArrayCreate(VT_VARIANT
, array_desc
->dim_cnt
, array_desc
->bounds
);
1054 if(!ctx
->arrays
[array_id
])
1055 return E_OUTOFMEMORY
;
1058 V_VT(ref
.u
.v
) = VT_ARRAY
|VT_BYREF
|VT_VARIANT
;
1059 V_ARRAYREF(ref
.u
.v
) = ctx
->arrays
+array_id
;
1063 static HRESULT
interp_step(exec_ctx_t
*ctx
)
1065 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1071 TRACE("%s\n", debugstr_w(ident
));
1073 V_VT(&zero
) = VT_I2
;
1075 hres
= VarCmp(stack_top(ctx
, 0), &zero
, ctx
->script
->lcid
, 0);
1079 gteq_zero
= hres
== VARCMP_GT
|| hres
== VARCMP_EQ
;
1081 hres
= lookup_identifier(ctx
, ident
, VBDISP_ANY
, &ref
);
1085 if(ref
.type
!= REF_VAR
) {
1086 FIXME("%s is not REF_VAR\n", debugstr_w(ident
));
1090 hres
= VarCmp(ref
.u
.v
, stack_top(ctx
, 1), ctx
->script
->lcid
, 0);
1094 if(hres
== VARCMP_EQ
|| hres
== (gteq_zero
? VARCMP_LT
: VARCMP_GT
)) {
1098 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1103 static HRESULT
interp_newenum(exec_ctx_t
*ctx
)
1111 stack_pop_deref(ctx
, &v
);
1112 assert(V_VT(stack_top(ctx
, 0)) == VT_EMPTY
);
1113 r
= stack_top(ctx
, 0);
1116 case VT_DISPATCH
|VT_BYREF
:
1119 DISPPARAMS dp
= {0};
1122 hres
= disp_call(ctx
->script
, V_ISBYREF(v
.v
) ? *V_DISPATCHREF(v
.v
) : V_DISPATCH(v
.v
), DISPID_NEWENUM
, &dp
, &iterv
);
1127 if(V_VT(&iterv
) != VT_UNKNOWN
&& V_VT(&iterv
) != VT_DISPATCH
) {
1128 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv
));
1129 VariantClear(&iterv
);
1133 hres
= IUnknown_QueryInterface(V_UNKNOWN(&iterv
), &IID_IEnumVARIANT
, (void**)&iter
);
1134 IUnknown_Release(V_UNKNOWN(&iterv
));
1136 FIXME("Could not get IEnumVARIANT iface: %08x\n", hres
);
1140 V_VT(r
) = VT_UNKNOWN
;
1141 V_UNKNOWN(r
) = (IUnknown
*)iter
;
1144 case VT_VARIANT
|VT_ARRAY
:
1145 case VT_VARIANT
|VT_ARRAY
|VT_BYREF
: {
1148 hres
= create_safearray_iter(V_ISBYREF(v
.v
) ? *V_ARRAYREF(v
.v
) : V_ARRAY(v
.v
), &iter
);
1152 V_VT(r
) = VT_UNKNOWN
;
1153 V_UNKNOWN(r
) = (IUnknown
*)iter
;
1157 FIXME("Unsupported for %s\n", debugstr_variant(v
.v
));
1165 static HRESULT
interp_enumnext(exec_ctx_t
*ctx
)
1167 const unsigned loop_end
= ctx
->instr
->arg1
.uint
;
1168 const BSTR ident
= ctx
->instr
->arg2
.bstr
;
1170 DISPPARAMS dp
= {&v
, &propput_dispid
, 1, 1};
1177 if(V_VT(stack_top(ctx
, 0)) == VT_EMPTY
) {
1178 FIXME("uninitialized\n");
1182 assert(V_VT(stack_top(ctx
, 0)) == VT_UNKNOWN
);
1183 iter
= (IEnumVARIANT
*)V_UNKNOWN(stack_top(ctx
, 0));
1185 V_VT(&v
) = VT_EMPTY
;
1186 hres
= IEnumVARIANT_Next(iter
, 1, &v
, NULL
);
1190 do_continue
= hres
== S_OK
;
1191 hres
= assign_ident(ctx
, ident
, DISPATCH_PROPERTYPUT
|DISPATCH_PROPERTYPUTREF
, &dp
);
1200 instr_jmp(ctx
, loop_end
);
1205 static HRESULT
interp_jmp(exec_ctx_t
*ctx
)
1207 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1211 instr_jmp(ctx
, arg
);
1215 static HRESULT
interp_jmp_false(exec_ctx_t
*ctx
)
1217 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1223 hres
= stack_pop_bool(ctx
, &b
);
1230 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1234 static HRESULT
interp_jmp_true(exec_ctx_t
*ctx
)
1236 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1242 hres
= stack_pop_bool(ctx
, &b
);
1247 instr_jmp(ctx
, ctx
->instr
->arg1
.uint
);
1253 static HRESULT
interp_ret(exec_ctx_t
*ctx
)
1261 static HRESULT
interp_stop(exec_ctx_t
*ctx
)
1265 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1269 static HRESULT
interp_me(exec_ctx_t
*ctx
)
1275 IDispatch_AddRef(ctx
->this_obj
);
1276 V_VT(&v
) = VT_DISPATCH
;
1277 V_DISPATCH(&v
) = ctx
->this_obj
;
1278 return stack_push(ctx
, &v
);
1281 static HRESULT
interp_bool(exec_ctx_t
*ctx
)
1283 const VARIANT_BOOL arg
= ctx
->instr
->arg1
.lng
;
1286 TRACE("%s\n", arg
? "true" : "false");
1290 return stack_push(ctx
, &v
);
1293 static HRESULT
interp_errmode(exec_ctx_t
*ctx
)
1295 const int err_mode
= ctx
->instr
->arg1
.uint
;
1297 TRACE("%d\n", err_mode
);
1299 ctx
->resume_next
= err_mode
;
1300 ctx
->script
->err_number
= S_OK
;
1304 static HRESULT
interp_string(exec_ctx_t
*ctx
)
1311 V_BSTR(&v
) = SysAllocString(ctx
->instr
->arg1
.str
);
1313 return E_OUTOFMEMORY
;
1315 return stack_push(ctx
, &v
);
1318 static HRESULT
interp_long(exec_ctx_t
*ctx
)
1320 const LONG arg
= ctx
->instr
->arg1
.lng
;
1327 return stack_push(ctx
, &v
);
1330 static HRESULT
interp_short(exec_ctx_t
*ctx
)
1332 const LONG arg
= ctx
->instr
->arg1
.lng
;
1339 return stack_push(ctx
, &v
);
1342 static HRESULT
interp_double(exec_ctx_t
*ctx
)
1344 const DOUBLE
*arg
= ctx
->instr
->arg1
.dbl
;
1347 TRACE("%lf\n", *arg
);
1351 return stack_push(ctx
, &v
);
1354 static HRESULT
interp_empty(exec_ctx_t
*ctx
)
1360 V_VT(&v
) = VT_EMPTY
;
1361 return stack_push(ctx
, &v
);
1364 static HRESULT
interp_null(exec_ctx_t
*ctx
)
1367 return stack_push_null(ctx
);
1370 static HRESULT
interp_nothing(exec_ctx_t
*ctx
)
1376 V_VT(&v
) = VT_DISPATCH
;
1377 V_DISPATCH(&v
) = NULL
;
1378 return stack_push(ctx
, &v
);
1381 static HRESULT
interp_hres(exec_ctx_t
*ctx
)
1383 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1388 V_VT(&v
) = VT_ERROR
;
1390 return stack_push(ctx
, &v
);
1393 static HRESULT
interp_not(exec_ctx_t
*ctx
)
1401 hres
= stack_pop_val(ctx
, &val
);
1405 hres
= VarNot(val
.v
, &v
);
1410 return stack_push(ctx
, &v
);
1413 static HRESULT
interp_and(exec_ctx_t
*ctx
)
1421 hres
= stack_pop_val(ctx
, &r
);
1425 hres
= stack_pop_val(ctx
, &l
);
1426 if(SUCCEEDED(hres
)) {
1427 hres
= VarAnd(l
.v
, r
.v
, &v
);
1434 return stack_push(ctx
, &v
);
1437 static HRESULT
interp_or(exec_ctx_t
*ctx
)
1445 hres
= stack_pop_val(ctx
, &r
);
1449 hres
= stack_pop_val(ctx
, &l
);
1450 if(SUCCEEDED(hres
)) {
1451 hres
= VarOr(l
.v
, r
.v
, &v
);
1458 return stack_push(ctx
, &v
);
1461 static HRESULT
interp_xor(exec_ctx_t
*ctx
)
1469 hres
= stack_pop_val(ctx
, &r
);
1473 hres
= stack_pop_val(ctx
, &l
);
1474 if(SUCCEEDED(hres
)) {
1475 hres
= VarXor(l
.v
, r
.v
, &v
);
1482 return stack_push(ctx
, &v
);
1485 static HRESULT
interp_eqv(exec_ctx_t
*ctx
)
1493 hres
= stack_pop_val(ctx
, &r
);
1497 hres
= stack_pop_val(ctx
, &l
);
1498 if(SUCCEEDED(hres
)) {
1499 hres
= VarEqv(l
.v
, r
.v
, &v
);
1506 return stack_push(ctx
, &v
);
1509 static HRESULT
interp_imp(exec_ctx_t
*ctx
)
1517 hres
= stack_pop_val(ctx
, &r
);
1521 hres
= stack_pop_val(ctx
, &l
);
1522 if(SUCCEEDED(hres
)) {
1523 hres
= VarImp(l
.v
, r
.v
, &v
);
1530 return stack_push(ctx
, &v
);
1533 static HRESULT
var_cmp(exec_ctx_t
*ctx
, VARIANT
*l
, VARIANT
*r
)
1535 TRACE("%s %s\n", debugstr_variant(l
), debugstr_variant(r
));
1537 /* FIXME: Fix comparing string to number */
1539 return VarCmp(l
, r
, ctx
->script
->lcid
, 0);
1542 static HRESULT
cmp_oper(exec_ctx_t
*ctx
)
1547 hres
= stack_pop_val(ctx
, &r
);
1551 hres
= stack_pop_val(ctx
, &l
);
1552 if(SUCCEEDED(hres
)) {
1553 hres
= var_cmp(ctx
, l
.v
, r
.v
);
1561 static HRESULT
interp_equal(exec_ctx_t
*ctx
)
1568 hres
= cmp_oper(ctx
);
1571 if(hres
== VARCMP_NULL
)
1572 return stack_push_null(ctx
);
1575 V_BOOL(&v
) = hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1576 return stack_push(ctx
, &v
);
1579 static HRESULT
interp_nequal(exec_ctx_t
*ctx
)
1586 hres
= cmp_oper(ctx
);
1589 if(hres
== VARCMP_NULL
)
1590 return stack_push_null(ctx
);
1593 V_BOOL(&v
) = hres
!= VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1594 return stack_push(ctx
, &v
);
1597 static HRESULT
interp_gt(exec_ctx_t
*ctx
)
1604 hres
= cmp_oper(ctx
);
1607 if(hres
== VARCMP_NULL
)
1608 return stack_push_null(ctx
);
1611 V_BOOL(&v
) = hres
== VARCMP_GT
? VARIANT_TRUE
: VARIANT_FALSE
;
1612 return stack_push(ctx
, &v
);
1615 static HRESULT
interp_gteq(exec_ctx_t
*ctx
)
1622 hres
= cmp_oper(ctx
);
1625 if(hres
== VARCMP_NULL
)
1626 return stack_push_null(ctx
);
1629 V_BOOL(&v
) = hres
== VARCMP_GT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1630 return stack_push(ctx
, &v
);
1633 static HRESULT
interp_lt(exec_ctx_t
*ctx
)
1640 hres
= cmp_oper(ctx
);
1643 if(hres
== VARCMP_NULL
)
1644 return stack_push_null(ctx
);
1647 V_BOOL(&v
) = hres
== VARCMP_LT
? VARIANT_TRUE
: VARIANT_FALSE
;
1648 return stack_push(ctx
, &v
);
1651 static HRESULT
interp_lteq(exec_ctx_t
*ctx
)
1658 hres
= cmp_oper(ctx
);
1661 if(hres
== VARCMP_NULL
)
1662 return stack_push_null(ctx
);
1665 V_BOOL(&v
) = hres
== VARCMP_LT
|| hres
== VARCMP_EQ
? VARIANT_TRUE
: VARIANT_FALSE
;
1666 return stack_push(ctx
, &v
);
1669 static HRESULT
interp_case(exec_ctx_t
*ctx
)
1671 const unsigned arg
= ctx
->instr
->arg1
.uint
;
1677 hres
= stack_pop_val(ctx
, &v
);
1681 hres
= var_cmp(ctx
, stack_top(ctx
, 0), v
.v
);
1686 if(hres
== VARCMP_EQ
) {
1688 instr_jmp(ctx
, arg
);
1696 static HRESULT
disp_cmp(IDispatch
*disp1
, IDispatch
*disp2
, VARIANT_BOOL
*ret
)
1698 IObjectIdentity
*identity
;
1699 IUnknown
*unk1
, *unk2
;
1702 if(disp1
== disp2
) {
1703 *ret
= VARIANT_TRUE
;
1707 if(!disp1
|| !disp2
) {
1708 *ret
= VARIANT_FALSE
;
1712 hres
= IDispatch_QueryInterface(disp1
, &IID_IUnknown
, (void**)&unk1
);
1716 hres
= IDispatch_QueryInterface(disp2
, &IID_IUnknown
, (void**)&unk2
);
1718 IUnknown_Release(unk1
);
1723 *ret
= VARIANT_TRUE
;
1725 hres
= IUnknown_QueryInterface(unk1
, &IID_IObjectIdentity
, (void**)&identity
);
1726 if(SUCCEEDED(hres
)) {
1727 hres
= IObjectIdentity_IsEqualObject(identity
, unk2
);
1728 IObjectIdentity_Release(identity
);
1729 *ret
= hres
== S_OK
? VARIANT_TRUE
: VARIANT_FALSE
;
1731 *ret
= VARIANT_FALSE
;
1735 IUnknown_Release(unk1
);
1736 IUnknown_Release(unk2
);
1740 static HRESULT
interp_is(exec_ctx_t
*ctx
)
1748 hres
= stack_pop_disp(ctx
, &r
);
1752 hres
= stack_pop_disp(ctx
, &l
);
1753 if(SUCCEEDED(hres
)) {
1755 hres
= disp_cmp(l
, r
, &V_BOOL(&v
));
1757 IDispatch_Release(l
);
1760 IDispatch_Release(r
);
1764 return stack_push(ctx
, &v
);
1767 static HRESULT
interp_concat(exec_ctx_t
*ctx
)
1775 hres
= stack_pop_val(ctx
, &r
);
1779 hres
= stack_pop_val(ctx
, &l
);
1780 if(SUCCEEDED(hres
)) {
1781 hres
= VarCat(l
.v
, r
.v
, &v
);
1788 return stack_push(ctx
, &v
);
1791 static HRESULT
interp_add(exec_ctx_t
*ctx
)
1799 hres
= stack_pop_val(ctx
, &r
);
1803 hres
= stack_pop_val(ctx
, &l
);
1804 if(SUCCEEDED(hres
)) {
1805 hres
= VarAdd(l
.v
, r
.v
, &v
);
1812 return stack_push(ctx
, &v
);
1815 static HRESULT
interp_sub(exec_ctx_t
*ctx
)
1823 hres
= stack_pop_val(ctx
, &r
);
1827 hres
= stack_pop_val(ctx
, &l
);
1828 if(SUCCEEDED(hres
)) {
1829 hres
= VarSub(l
.v
, r
.v
, &v
);
1836 return stack_push(ctx
, &v
);
1839 static HRESULT
interp_mod(exec_ctx_t
*ctx
)
1847 hres
= stack_pop_val(ctx
, &r
);
1851 hres
= stack_pop_val(ctx
, &l
);
1852 if(SUCCEEDED(hres
)) {
1853 hres
= VarMod(l
.v
, r
.v
, &v
);
1860 return stack_push(ctx
, &v
);
1863 static HRESULT
interp_idiv(exec_ctx_t
*ctx
)
1871 hres
= stack_pop_val(ctx
, &r
);
1875 hres
= stack_pop_val(ctx
, &l
);
1876 if(SUCCEEDED(hres
)) {
1877 hres
= VarIdiv(l
.v
, r
.v
, &v
);
1884 return stack_push(ctx
, &v
);
1887 static HRESULT
interp_div(exec_ctx_t
*ctx
)
1895 hres
= stack_pop_val(ctx
, &r
);
1899 hres
= stack_pop_val(ctx
, &l
);
1900 if(SUCCEEDED(hres
)) {
1901 hres
= VarDiv(l
.v
, r
.v
, &v
);
1908 return stack_push(ctx
, &v
);
1911 static HRESULT
interp_mul(exec_ctx_t
*ctx
)
1919 hres
= stack_pop_val(ctx
, &r
);
1923 hres
= stack_pop_val(ctx
, &l
);
1924 if(SUCCEEDED(hres
)) {
1925 hres
= VarMul(l
.v
, r
.v
, &v
);
1932 return stack_push(ctx
, &v
);
1935 static HRESULT
interp_exp(exec_ctx_t
*ctx
)
1943 hres
= stack_pop_val(ctx
, &r
);
1947 hres
= stack_pop_val(ctx
, &l
);
1948 if(SUCCEEDED(hres
)) {
1949 hres
= VarPow(l
.v
, r
.v
, &v
);
1956 return stack_push(ctx
, &v
);
1959 static HRESULT
interp_neg(exec_ctx_t
*ctx
)
1965 hres
= stack_pop_val(ctx
, &val
);
1969 hres
= VarNeg(val
.v
, &v
);
1974 return stack_push(ctx
, &v
);
1977 static HRESULT
interp_incc(exec_ctx_t
*ctx
)
1979 const BSTR ident
= ctx
->instr
->arg1
.bstr
;
1986 hres
= lookup_identifier(ctx
, ident
, VBDISP_LET
, &ref
);
1990 if(ref
.type
!= REF_VAR
) {
1991 FIXME("ref.type is not REF_VAR\n");
1995 hres
= VarAdd(stack_top(ctx
, 0), ref
.u
.v
, &v
);
1999 VariantClear(ref
.u
.v
);
2004 static HRESULT
interp_catch(exec_ctx_t
*ctx
)
2006 /* Nothing to do here, the OP is for unwinding only. */
2010 static const instr_func_t op_funcs
[] = {
2011 #define X(x,n,a,b) interp_ ## x,
2016 static const unsigned op_move
[] = {
2017 #define X(x,n,a,b) n,
2022 void release_dynamic_vars(dynamic_var_t
*var
)
2025 VariantClear(&var
->v
);
2030 static void release_exec(exec_ctx_t
*ctx
)
2034 VariantClear(&ctx
->ret_val
);
2035 release_dynamic_vars(ctx
->dynamic_vars
);
2038 IDispatch_Release(ctx
->this_obj
);
2041 for(i
=0; i
< ctx
->func
->arg_cnt
; i
++)
2042 VariantClear(ctx
->args
+i
);
2046 for(i
=0; i
< ctx
->func
->var_cnt
; i
++)
2047 VariantClear(ctx
->vars
+i
);
2051 for(i
=0; i
< ctx
->func
->var_cnt
; i
++) {
2053 SafeArrayDestroy(ctx
->arrays
[i
]);
2055 heap_free(ctx
->arrays
);
2058 heap_pool_free(&ctx
->heap
);
2059 heap_free(ctx
->args
);
2060 heap_free(ctx
->vars
);
2061 heap_free(ctx
->stack
);
2064 HRESULT
exec_script(script_ctx_t
*ctx
, function_t
*func
, vbdisp_t
*vbthis
, DISPPARAMS
*dp
, VARIANT
*res
)
2066 exec_ctx_t exec
= {func
->code_ctx
};
2068 HRESULT hres
= S_OK
;
2070 exec
.code
= func
->code_ctx
;
2072 if(dp
? func
->arg_cnt
!= arg_cnt(dp
) : func
->arg_cnt
) {
2073 FIXME("wrong arg_cnt %d, expected %d\n", dp
? arg_cnt(dp
) : 0, func
->arg_cnt
);
2077 heap_pool_init(&exec
.heap
);
2083 exec
.args
= heap_alloc_zero(func
->arg_cnt
* sizeof(VARIANT
));
2085 release_exec(&exec
);
2086 return E_OUTOFMEMORY
;
2089 for(i
=0; i
< func
->arg_cnt
; i
++) {
2091 if(V_VT(v
) == (VT_VARIANT
|VT_BYREF
)) {
2092 if(func
->args
[i
].by_ref
)
2095 hres
= VariantCopyInd(exec
.args
+i
, V_VARIANTREF(v
));
2097 hres
= VariantCopyInd(exec
.args
+i
, v
);
2100 release_exec(&exec
);
2109 exec
.vars
= heap_alloc_zero(func
->var_cnt
* sizeof(VARIANT
));
2111 release_exec(&exec
);
2112 return E_OUTOFMEMORY
;
2118 exec
.stack_size
= 16;
2120 exec
.stack
= heap_alloc(exec
.stack_size
* sizeof(VARIANT
));
2122 release_exec(&exec
);
2123 return E_OUTOFMEMORY
;
2127 exec
.this_obj
= (IDispatch
*)&vbthis
->IDispatchEx_iface
;
2128 exec
.vbthis
= vbthis
;
2129 }else if (ctx
->host_global
) {
2130 exec
.this_obj
= ctx
->host_global
;
2132 exec
.this_obj
= (IDispatch
*)&ctx
->script_obj
->IDispatchEx_iface
;
2134 IDispatch_AddRef(exec
.this_obj
);
2136 exec
.instr
= exec
.code
->instrs
+ func
->code_off
;
2141 op
= exec
.instr
->op
;
2142 hres
= op_funcs
[op
](&exec
);
2144 ctx
->err_number
= hres
= map_hres(hres
);
2146 if(exec
.resume_next
) {
2149 WARN("Failed %08x in resume next mode\n", hres
);
2152 * Unwinding here is simple. We need to find the next OP_catch, which contains
2153 * information about expected stack size and jump offset on error. Generated
2154 * bytecode needs to guarantee, that simple jump and stack adjustment will
2155 * guarantee proper execution continuation.
2157 while((++exec
.instr
)->op
!= OP_catch
);
2159 TRACE("unwind jmp %d stack_off %d\n", exec
.instr
->arg1
.uint
, exec
.instr
->arg2
.uint
);
2161 stack_off
= exec
.instr
->arg2
.uint
;
2162 instr_jmp(&exec
, exec
.instr
->arg1
.uint
);
2164 if(exec
.top
> stack_off
) {
2165 stack_popn(&exec
, exec
.top
-stack_off
);
2166 }else if(exec
.top
< stack_off
) {
2169 V_VT(&v
) = VT_EMPTY
;
2170 while(exec
.top
< stack_off
) {
2171 hres
= stack_push(&exec
, &v
);
2179 WARN("Failed %08x\n", hres
);
2180 stack_popn(&exec
, exec
.top
);
2185 exec
.instr
+= op_move
[op
];
2189 if(func
->type
!= FUNC_FUNCTION
&& func
->type
!= FUNC_PROPGET
&& func
->type
!= FUNC_DEFGET
)
2190 assert(V_VT(&exec
.ret_val
) == VT_EMPTY
);
2192 if(SUCCEEDED(hres
) && res
) {
2193 *res
= exec
.ret_val
;
2194 V_VT(&exec
.ret_val
) = VT_EMPTY
;
2197 release_exec(&exec
);