* Sync up to trunk head (r64716).
[reactos.git] / dll / win32 / vbscript / interp.c
1 /*
2 * Copyright 2011 Jacek Caban for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include "vbscript.h"
20
21 static DISPID propput_dispid = DISPID_PROPERTYPUT;
22
23 typedef struct {
24 vbscode_t *code;
25 instr_t *instr;
26 script_ctx_t *script;
27 function_t *func;
28 IDispatch *this_obj;
29 vbdisp_t *vbthis;
30
31 VARIANT *args;
32 VARIANT *vars;
33 SAFEARRAY **arrays;
34
35 dynamic_var_t *dynamic_vars;
36 heap_pool_t heap;
37
38 BOOL resume_next;
39
40 unsigned stack_size;
41 unsigned top;
42 VARIANT *stack;
43
44 VARIANT ret_val;
45 } exec_ctx_t;
46
47 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
48
49 typedef enum {
50 REF_NONE,
51 REF_DISP,
52 REF_VAR,
53 REF_OBJ,
54 REF_CONST,
55 REF_FUNC
56 } ref_type_t;
57
58 typedef struct {
59 ref_type_t type;
60 union {
61 struct {
62 IDispatch *disp;
63 DISPID id;
64 } d;
65 VARIANT *v;
66 function_t *f;
67 IDispatch *obj;
68 } u;
69 } ref_t;
70
71 typedef struct {
72 VARIANT *v;
73 VARIANT store;
74 BOOL owned;
75 } variant_val_t;
76
77 static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *ref)
78 {
79 while(var) {
80 if(!strcmpiW(var->name, name)) {
81 ref->type = var->is_const ? REF_CONST : REF_VAR;
82 ref->u.v = &var->v;
83 return TRUE;
84 }
85
86 var = var->next;
87 }
88
89 return FALSE;
90 }
91
92 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_t invoke_type, ref_t *ref)
93 {
94 named_item_t *item;
95 function_t *func;
96 unsigned i;
97 DISPID id;
98 HRESULT hres;
99
100 static const WCHAR errW[] = {'e','r','r',0};
101
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)) {
105 ref->type = REF_VAR;
106 ref->u.v = &ctx->ret_val;
107 return S_OK;
108 }
109
110 for(i=0; i < ctx->func->var_cnt; i++) {
111 if(!strcmpiW(ctx->func->vars[i].name, name)) {
112 ref->type = REF_VAR;
113 ref->u.v = ctx->vars+i;
114 return TRUE;
115 }
116 }
117
118 for(i=0; i < ctx->func->arg_cnt; i++) {
119 if(!strcmpiW(ctx->func->args[i].name, name)) {
120 ref->type = REF_VAR;
121 ref->u.v = ctx->args+i;
122 return S_OK;
123 }
124 }
125
126 if(lookup_dynamic_vars(ctx->func->type == FUNC_GLOBAL ? ctx->script->global_vars : ctx->dynamic_vars, name, ref))
127 return S_OK;
128
129 if(ctx->func->type != FUNC_GLOBAL) {
130 if(ctx->vbthis) {
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)) {
134 ref->type = REF_VAR;
135 ref->u.v = ctx->vbthis->props+i;
136 return S_OK;
137 }
138 }
139 }
140
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;
145 ref->u.d.id = id;
146 return S_OK;
147 }
148 }
149
150 if(ctx->func->type != FUNC_GLOBAL && lookup_dynamic_vars(ctx->script->global_vars, name, ref))
151 return S_OK;
152
153 for(func = ctx->script->global_funcs; func; func = func->next) {
154 if(!strcmpiW(func->name, name)) {
155 ref->type = REF_FUNC;
156 ref->u.f = func;
157 return S_OK;
158 }
159 }
160
161 if(!strcmpiW(name, errW)) {
162 ref->type = REF_OBJ;
163 ref->u.obj = (IDispatch*)&ctx->script->err_obj->IDispatchEx_iface;
164 return S_OK;
165 }
166
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;
171 ref->u.d.id = id;
172 return S_OK;
173 }
174
175 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
176 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpiW(item->name, name)) {
177 if(!item->disp) {
178 IUnknown *unk;
179
180 hres = IActiveScriptSite_GetItemInfo(ctx->script->site, name, SCRIPTINFO_IUNKNOWN, &unk, NULL);
181 if(FAILED(hres)) {
182 WARN("GetItemInfo failed: %08x\n", hres);
183 continue;
184 }
185
186 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
187 IUnknown_Release(unk);
188 if(FAILED(hres)) {
189 WARN("object does not implement IDispatch\n");
190 continue;
191 }
192 }
193
194 ref->type = REF_OBJ;
195 ref->u.obj = item->disp;
196 return S_OK;
197 }
198 }
199
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;
206 ref->u.d.id = id;
207 return S_OK;
208 }
209 }
210 }
211
212 ref->type = REF_NONE;
213 return S_OK;
214 }
215
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)
218 {
219 dynamic_var_t *new_var;
220 heap_pool_t *heap;
221 WCHAR *str;
222 unsigned size;
223 HRESULT hres;
224
225 heap = ctx->func->type == FUNC_GLOBAL ? &ctx->script->heap : &ctx->heap;
226
227 new_var = heap_pool_alloc(heap, sizeof(*new_var));
228 if(!new_var)
229 return E_OUTOFMEMORY;
230
231 size = (strlenW(name)+1)*sizeof(WCHAR);
232 str = heap_pool_alloc(heap, size);
233 if(!str)
234 return E_OUTOFMEMORY;
235 memcpy(str, name, size);
236 new_var->name = str;
237 new_var->is_const = is_const;
238
239 if(own_val) {
240 new_var->v = *val;
241 }else {
242 V_VT(&new_var->v) = VT_EMPTY;
243 hres = VariantCopy(&new_var->v, val);
244 if(FAILED(hres))
245 return hres;
246 }
247
248 if(ctx->func->type == FUNC_GLOBAL) {
249 new_var->next = ctx->script->global_vars;
250 ctx->script->global_vars = new_var;
251 }else {
252 new_var->next = ctx->dynamic_vars;
253 ctx->dynamic_vars = new_var;
254 }
255
256 if(out_var)
257 *out_var = &new_var->v;
258
259 return S_OK;
260 }
261
262 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
263 {
264 assert(ctx->top);
265 return ctx->stack + --ctx->top;
266 }
267
268 static inline VARIANT *stack_top(exec_ctx_t *ctx, unsigned n)
269 {
270 assert(ctx->top >= n);
271 return ctx->stack + (ctx->top-n-1);
272 }
273
274 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
275 {
276 if(ctx->stack_size == ctx->top) {
277 VARIANT *new_stack;
278
279 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(*ctx->stack));
280 if(!new_stack) {
281 VariantClear(v);
282 return E_OUTOFMEMORY;
283 }
284
285 ctx->stack = new_stack;
286 ctx->stack_size *= 2;
287 }
288
289 ctx->stack[ctx->top++] = *v;
290 return S_OK;
291 }
292
293 static inline HRESULT stack_push_null(exec_ctx_t *ctx)
294 {
295 VARIANT v;
296 V_VT(&v) = VT_NULL;
297 return stack_push(ctx, &v);
298 }
299
300 static void stack_popn(exec_ctx_t *ctx, unsigned n)
301 {
302 while(n--)
303 VariantClear(stack_pop(ctx));
304 }
305
306 static void stack_pop_deref(exec_ctx_t *ctx, variant_val_t *r)
307 {
308 VARIANT *v;
309
310 v = stack_pop(ctx);
311 if(V_VT(v) == (VT_BYREF|VT_VARIANT)) {
312 r->owned = FALSE;
313 r->v = V_VARIANTREF(v);
314 }else {
315 r->owned = TRUE;
316 r->v = v;
317 }
318 }
319
320 static inline void release_val(variant_val_t *v)
321 {
322 if(v->owned)
323 VariantClear(v->v);
324 }
325
326 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *r)
327 {
328 stack_pop_deref(ctx, r);
329
330 if(V_VT(r->v) == VT_DISPATCH) {
331 DISPPARAMS dp = {0};
332 HRESULT hres;
333
334 hres = disp_call(ctx->script, V_DISPATCH(r->v), DISPID_VALUE, &dp, &r->store);
335 if(r->owned)
336 IDispatch_Release(V_DISPATCH(r->v));
337 if(FAILED(hres))
338 return hres;
339
340 r->owned = TRUE;
341 r->v = &r->store;
342 }
343
344 return S_OK;
345 }
346
347 static HRESULT stack_assume_val(exec_ctx_t *ctx, unsigned n)
348 {
349 VARIANT *v = stack_top(ctx, n);
350 HRESULT hres;
351
352 if(V_VT(v) == (VT_BYREF|VT_VARIANT)) {
353 VARIANT *ref = V_VARIANTREF(v);
354
355 V_VT(v) = VT_EMPTY;
356 hres = VariantCopy(v, ref);
357 if(FAILED(hres))
358 return hres;
359 }
360
361 if(V_VT(v) == VT_DISPATCH) {
362 DISPPARAMS dp = {0};
363 IDispatch *disp;
364
365 disp = V_DISPATCH(v);
366 V_VT(v) = VT_EMPTY;
367 hres = disp_call(ctx->script, disp, DISPID_VALUE, &dp, v);
368 IDispatch_Release(disp);
369 if(FAILED(hres))
370 return hres;
371 }
372
373 return S_OK;
374 }
375
376 static int stack_pop_bool(exec_ctx_t *ctx, BOOL *b)
377 {
378 variant_val_t val;
379 HRESULT hres;
380
381 hres = stack_pop_val(ctx, &val);
382 if(FAILED(hres))
383 return hres;
384
385 switch (V_VT(val.v))
386 {
387 case VT_BOOL:
388 *b = V_BOOL(val.v);
389 break;
390 case VT_NULL:
391 *b = FALSE;
392 break;
393 case VT_I2:
394 *b = V_I2(val.v);
395 break;
396 case VT_I4:
397 *b = V_I4(val.v);
398 break;
399 default:
400 FIXME("unsupported for %s\n", debugstr_variant(val.v));
401 release_val(&val);
402 return E_NOTIMPL;
403 }
404 return S_OK;
405 }
406
407 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
408 {
409 VARIANT *v = stack_pop(ctx);
410
411 if(V_VT(v) == VT_DISPATCH) {
412 *ret = V_DISPATCH(v);
413 return S_OK;
414 }
415
416 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
417 FIXME("not supported type: %s\n", debugstr_variant(v));
418 VariantClear(v);
419 return E_FAIL;
420 }
421
422 v = V_BYREF(v);
423 if(V_VT(v) != VT_DISPATCH) {
424 FIXME("not disp %s\n", debugstr_variant(v));
425 return E_FAIL;
426 }
427
428 if(V_DISPATCH(v))
429 IDispatch_AddRef(V_DISPATCH(v));
430 *ret = V_DISPATCH(v);
431 return S_OK;
432 }
433
434 static HRESULT stack_assume_disp(exec_ctx_t *ctx, unsigned n, IDispatch **disp)
435 {
436 VARIANT *v = stack_top(ctx, n), *ref;
437
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));
441 return E_FAIL;
442 }
443
444 ref = V_VARIANTREF(v);
445 if(V_VT(ref) != VT_DISPATCH) {
446 FIXME("not disp %s\n", debugstr_variant(ref));
447 return E_FAIL;
448 }
449
450 V_VT(v) = VT_DISPATCH;
451 V_DISPATCH(v) = V_DISPATCH(ref);
452 if(V_DISPATCH(v))
453 IDispatch_AddRef(V_DISPATCH(v));
454 }
455
456 if(disp)
457 *disp = V_DISPATCH(v);
458 return S_OK;
459 }
460
461 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
462 {
463 ctx->instr = ctx->code->instrs + addr;
464 }
465
466 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, BOOL is_propput, DISPPARAMS *dp)
467 {
468 dp->cNamedArgs = is_propput ? 1 : 0;
469 dp->cArgs = arg_cnt + dp->cNamedArgs;
470 dp->rgdispidNamedArgs = is_propput ? &propput_dispid : NULL;
471
472 if(arg_cnt) {
473 VARIANT tmp;
474 unsigned i;
475
476 assert(ctx->top >= arg_cnt);
477
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;
482 }
483
484 dp->rgvarg = ctx->stack + ctx->top-dp->cArgs;
485 }else {
486 dp->rgvarg = is_propput ? ctx->stack+ctx->top-1 : NULL;
487 }
488 }
489
490 static HRESULT array_access(exec_ctx_t *ctx, SAFEARRAY *array, DISPPARAMS *dp, VARIANT **ret)
491 {
492 unsigned cell_off = 0, dim_size = 1, i;
493 unsigned argc = arg_cnt(dp);
494 VARIANT *data;
495 LONG idx;
496 HRESULT hres;
497
498 if(!array) {
499 FIXME("NULL array\n");
500 return E_FAIL;
501 }
502
503 if(array->cDims != argc) {
504 FIXME("argc %d does not match cDims %d\n", dp->cArgs, array->cDims);
505 return E_FAIL;
506 }
507
508 for(i=0; i < argc; i++) {
509 hres = to_int(get_arg(dp, i), &idx);
510 if(FAILED(hres))
511 return hres;
512
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);
516 return E_FAIL;
517 }
518
519 cell_off += idx*dim_size;
520 dim_size *= array->rgsabound[i].cElements;
521 }
522
523 hres = SafeArrayAccessData(array, (void**)&data);
524 if(FAILED(hres))
525 return hres;
526
527 *ret = data+cell_off;
528
529 SafeArrayUnaccessData(array);
530 return S_OK;
531 }
532
533 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
534 {
535 BSTR identifier = ctx->instr->arg1.bstr;
536 const unsigned arg_cnt = ctx->instr->arg2.uint;
537 DISPPARAMS dp;
538 ref_t ref;
539 HRESULT hres;
540
541 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
542 if(FAILED(hres))
543 return hres;
544
545 switch(ref.type) {
546 case REF_VAR:
547 case REF_CONST: {
548 VARIANT *v;
549
550 if(!res) {
551 FIXME("REF_VAR no res\n");
552 return E_NOTIMPL;
553 }
554
555 v = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
556
557 if(arg_cnt) {
558 SAFEARRAY *array = NULL;
559
560 switch(V_VT(v)) {
561 case VT_ARRAY|VT_BYREF|VT_VARIANT:
562 array = *V_ARRAYREF(ref.u.v);
563 break;
564 case VT_ARRAY|VT_VARIANT:
565 array = V_ARRAY(ref.u.v);
566 break;
567 case VT_DISPATCH:
568 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
569 hres = disp_call(ctx->script, V_DISPATCH(v), DISPID_VALUE, &dp, res);
570 if(FAILED(hres))
571 return hres;
572 break;
573 default:
574 FIXME("arguments not implemented\n");
575 return E_NOTIMPL;
576 }
577
578 if(!array)
579 break;
580
581 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
582 hres = array_access(ctx, array, &dp, &v);
583 if(FAILED(hres))
584 return hres;
585 }
586
587 V_VT(res) = VT_BYREF|VT_VARIANT;
588 V_BYREF(res) = v;
589 break;
590 }
591 case REF_DISP:
592 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
593 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
594 if(FAILED(hres))
595 return hres;
596 break;
597 case REF_FUNC:
598 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
599 hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
600 if(FAILED(hres))
601 return hres;
602 break;
603 case REF_OBJ:
604 if(arg_cnt) {
605 FIXME("arguments on object\n");
606 return E_NOTIMPL;
607 }
608
609 if(res) {
610 IDispatch_AddRef(ref.u.obj);
611 V_VT(res) = VT_DISPATCH;
612 V_DISPATCH(res) = ref.u.obj;
613 }
614 break;
615 case REF_NONE:
616 if(res && !ctx->func->code_ctx->option_explicit && arg_cnt == 0) {
617 VARIANT v, *new;
618 VariantInit(&v);
619 hres = add_dynamic_var(ctx, identifier, FALSE, &v, FALSE, &new);
620 if(FAILED(hres))
621 return hres;
622 V_VT(res) = VT_BYREF|VT_VARIANT;
623 V_BYREF(res) = new;
624 break;
625 }
626 FIXME("%s not found\n", debugstr_w(identifier));
627 return DISP_E_UNKNOWNNAME;
628 }
629
630 stack_popn(ctx, arg_cnt);
631 return S_OK;
632 }
633
634 static HRESULT interp_icall(exec_ctx_t *ctx)
635 {
636 VARIANT v;
637 HRESULT hres;
638
639 TRACE("\n");
640
641 hres = do_icall(ctx, &v);
642 if(FAILED(hres))
643 return hres;
644
645 return stack_push(ctx, &v);
646 }
647
648 static HRESULT interp_icallv(exec_ctx_t *ctx)
649 {
650 TRACE("\n");
651 return do_icall(ctx, NULL);
652 }
653
654 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
655 {
656 const BSTR identifier = ctx->instr->arg1.bstr;
657 const unsigned arg_cnt = ctx->instr->arg2.uint;
658 IDispatch *obj;
659 DISPPARAMS dp;
660 DISPID id;
661 HRESULT hres;
662
663 hres = stack_pop_disp(ctx, &obj);
664 if(FAILED(hres))
665 return hres;
666
667 if(!obj) {
668 FIXME("NULL obj\n");
669 return E_FAIL;
670 }
671
672 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
673
674 hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
675 if(SUCCEEDED(hres))
676 hres = disp_call(ctx->script, obj, id, &dp, res);
677 IDispatch_Release(obj);
678 if(FAILED(hres))
679 return hres;
680
681 stack_popn(ctx, arg_cnt);
682 return S_OK;
683 }
684
685 static HRESULT interp_mcall(exec_ctx_t *ctx)
686 {
687 VARIANT res;
688 HRESULT hres;
689
690 TRACE("\n");
691
692 hres = do_mcall(ctx, &res);
693 if(FAILED(hres))
694 return hres;
695
696 return stack_push(ctx, &res);
697 }
698
699 static HRESULT interp_mcallv(exec_ctx_t *ctx)
700 {
701 TRACE("\n");
702
703 return do_mcall(ctx, NULL);
704 }
705
706 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, DISPPARAMS *dp)
707 {
708 ref_t ref;
709 HRESULT hres;
710
711 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
712 if(FAILED(hres))
713 return hres;
714
715 switch(ref.type) {
716 case REF_VAR: {
717 VARIANT *v = ref.u.v;
718
719 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
720 v = V_VARIANTREF(v);
721
722 if(arg_cnt(dp)) {
723 SAFEARRAY *array;
724
725 if(!(V_VT(v) & VT_ARRAY)) {
726 FIXME("array assign on type %d\n", V_VT(v));
727 return E_FAIL;
728 }
729
730 switch(V_VT(v)) {
731 case VT_ARRAY|VT_BYREF|VT_VARIANT:
732 array = *V_ARRAYREF(v);
733 break;
734 case VT_ARRAY|VT_VARIANT:
735 array = V_ARRAY(v);
736 break;
737 default:
738 FIXME("Unsupported array type %x\n", V_VT(v));
739 return E_NOTIMPL;
740 }
741
742 if(!array) {
743 FIXME("null array\n");
744 return E_FAIL;
745 }
746
747 hres = array_access(ctx, array, dp, &v);
748 if(FAILED(hres))
749 return hres;
750 }else if(V_VT(v) == (VT_ARRAY|VT_BYREF|VT_VARIANT)) {
751 FIXME("non-array assign\n");
752 return E_NOTIMPL;
753 }
754
755 hres = VariantCopyInd(v, dp->rgvarg);
756 break;
757 }
758 case REF_DISP:
759 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, dp);
760 break;
761 case REF_FUNC:
762 FIXME("functions not implemented\n");
763 return E_NOTIMPL;
764 case REF_OBJ:
765 FIXME("REF_OBJ\n");
766 return E_NOTIMPL;
767 case REF_CONST:
768 FIXME("REF_CONST\n");
769 return E_NOTIMPL;
770 case REF_NONE:
771 if(ctx->func->code_ctx->option_explicit) {
772 FIXME("throw exception\n");
773 hres = E_FAIL;
774 }else {
775 if(arg_cnt(dp)) {
776 FIXME("arg_cnt %d not supported\n", arg_cnt(dp));
777 return E_NOTIMPL;
778 }
779
780 TRACE("creating variable %s\n", debugstr_w(name));
781 hres = add_dynamic_var(ctx, name, FALSE, dp->rgvarg, FALSE, NULL);
782 }
783 }
784
785 return hres;
786 }
787
788 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
789 {
790 const BSTR arg = ctx->instr->arg1.bstr;
791 const unsigned arg_cnt = ctx->instr->arg2.uint;
792 DISPPARAMS dp;
793 HRESULT hres;
794
795 TRACE("%s\n", debugstr_w(arg));
796
797 hres = stack_assume_val(ctx, arg_cnt);
798 if(FAILED(hres))
799 return hres;
800
801 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
802 hres = assign_ident(ctx, arg, &dp);
803 if(FAILED(hres))
804 return hres;
805
806 stack_popn(ctx, arg_cnt+1);
807 return S_OK;
808 }
809
810 static HRESULT interp_set_ident(exec_ctx_t *ctx)
811 {
812 const BSTR arg = ctx->instr->arg1.bstr;
813 const unsigned arg_cnt = ctx->instr->arg2.uint;
814 DISPPARAMS dp;
815 HRESULT hres;
816
817 TRACE("%s\n", debugstr_w(arg));
818
819 if(arg_cnt) {
820 FIXME("arguments not supported\n");
821 return E_NOTIMPL;
822 }
823
824 hres = stack_assume_disp(ctx, 0, NULL);
825 if(FAILED(hres))
826 return hres;
827
828 vbstack_to_dp(ctx, 0, TRUE, &dp);
829 hres = assign_ident(ctx, ctx->instr->arg1.bstr, &dp);
830 if(FAILED(hres))
831 return hres;
832
833 stack_popn(ctx, 1);
834 return S_OK;
835 }
836
837 static HRESULT interp_assign_member(exec_ctx_t *ctx)
838 {
839 BSTR identifier = ctx->instr->arg1.bstr;
840 const unsigned arg_cnt = ctx->instr->arg2.uint;
841 IDispatch *obj;
842 DISPPARAMS dp;
843 DISPID id;
844 HRESULT hres;
845
846 TRACE("%s\n", debugstr_w(identifier));
847
848 hres = stack_assume_disp(ctx, arg_cnt+1, &obj);
849 if(FAILED(hres))
850 return hres;
851
852 if(!obj) {
853 FIXME("NULL obj\n");
854 return E_FAIL;
855 }
856
857 hres = stack_assume_val(ctx, arg_cnt);
858 if(FAILED(hres))
859 return hres;
860
861 hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
862 if(SUCCEEDED(hres)) {
863 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
864 hres = disp_propput(ctx->script, obj, id, &dp);
865 }
866 if(FAILED(hres))
867 return hres;
868
869 stack_popn(ctx, arg_cnt+2);
870 return S_OK;
871 }
872
873 static HRESULT interp_set_member(exec_ctx_t *ctx)
874 {
875 BSTR identifier = ctx->instr->arg1.bstr;
876 const unsigned arg_cnt = ctx->instr->arg2.uint;
877 IDispatch *obj;
878 DISPPARAMS dp;
879 DISPID id;
880 HRESULT hres;
881
882 TRACE("%s\n", debugstr_w(identifier));
883
884 if(arg_cnt) {
885 FIXME("arguments not supported\n");
886 return E_NOTIMPL;
887 }
888
889 hres = stack_assume_disp(ctx, 1, &obj);
890 if(FAILED(hres))
891 return hres;
892
893 if(!obj) {
894 FIXME("NULL obj\n");
895 return E_FAIL;
896 }
897
898 hres = stack_assume_disp(ctx, 0, NULL);
899 if(FAILED(hres))
900 return hres;
901
902 hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
903 if(SUCCEEDED(hres)) {
904 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
905 hres = disp_propput(ctx->script, obj, id, &dp);
906 }
907 if(FAILED(hres))
908 return hres;
909
910 stack_popn(ctx, 2);
911 return S_OK;
912 }
913
914 static HRESULT interp_const(exec_ctx_t *ctx)
915 {
916 BSTR arg = ctx->instr->arg1.bstr;
917 variant_val_t val;
918 ref_t ref;
919 HRESULT hres;
920
921 TRACE("%s\n", debugstr_w(arg));
922
923 assert(ctx->func->type == FUNC_GLOBAL);
924
925 hres = lookup_identifier(ctx, arg, VBDISP_CALLGET, &ref);
926 if(FAILED(hres))
927 return hres;
928
929 if(ref.type != REF_NONE) {
930 FIXME("%s already defined\n", debugstr_w(arg));
931 return E_FAIL;
932 }
933
934 hres = stack_pop_val(ctx, &val);
935 if(FAILED(hres))
936 return hres;
937
938 return add_dynamic_var(ctx, arg, TRUE, val.v, val.owned, NULL);
939 }
940
941 static HRESULT interp_val(exec_ctx_t *ctx)
942 {
943 variant_val_t val;
944 VARIANT v;
945 HRESULT hres;
946
947 TRACE("\n");
948
949 hres = stack_pop_val(ctx, &val);
950 if(FAILED(hres))
951 return hres;
952
953 if(!val.owned) {
954 V_VT(&v) = VT_EMPTY;
955 hres = VariantCopy(&v, val.v);
956 if(FAILED(hres))
957 return hres;
958 }
959
960 return stack_push(ctx, val.owned ? val.v : &v);
961 }
962
963 static HRESULT interp_pop(exec_ctx_t *ctx)
964 {
965 const unsigned n = ctx->instr->arg1.uint;
966
967 TRACE("%u\n", n);
968
969 stack_popn(ctx, n);
970 return S_OK;
971 }
972
973 static HRESULT interp_new(exec_ctx_t *ctx)
974 {
975 const WCHAR *arg = ctx->instr->arg1.bstr;
976 class_desc_t *class_desc;
977 vbdisp_t *obj;
978 VARIANT v;
979 HRESULT hres;
980
981 static const WCHAR regexpW[] = {'r','e','g','e','x','p',0};
982
983 TRACE("%s\n", debugstr_w(arg));
984
985 if(!strcmpiW(arg, regexpW)) {
986 V_VT(&v) = VT_DISPATCH;
987 hres = create_regexp(&V_DISPATCH(&v));
988 if(FAILED(hres))
989 return hres;
990
991 return stack_push(ctx, &v);
992 }
993
994 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
995 if(!strcmpiW(class_desc->name, arg))
996 break;
997 }
998 if(!class_desc) {
999 FIXME("Class %s not found\n", debugstr_w(arg));
1000 return E_FAIL;
1001 }
1002
1003 hres = create_vbdisp(class_desc, &obj);
1004 if(FAILED(hres))
1005 return hres;
1006
1007 V_VT(&v) = VT_DISPATCH;
1008 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
1009 return stack_push(ctx, &v);
1010 }
1011
1012 static HRESULT interp_dim(exec_ctx_t *ctx)
1013 {
1014 const BSTR ident = ctx->instr->arg1.bstr;
1015 const unsigned array_id = ctx->instr->arg2.uint;
1016 const array_desc_t *array_desc;
1017 ref_t ref;
1018 HRESULT hres;
1019
1020 TRACE("%s\n", debugstr_w(ident));
1021
1022 assert(array_id < ctx->func->array_cnt);
1023 if(!ctx->arrays) {
1024 ctx->arrays = heap_alloc_zero(ctx->func->array_cnt * sizeof(SAFEARRAY*));
1025 if(!ctx->arrays)
1026 return E_OUTOFMEMORY;
1027 }
1028
1029 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1030 if(FAILED(hres)) {
1031 FIXME("lookup %s failed: %08x\n", debugstr_w(ident), hres);
1032 return hres;
1033 }
1034
1035 if(ref.type != REF_VAR) {
1036 FIXME("got ref.type = %d\n", ref.type);
1037 return E_FAIL;
1038 }
1039
1040 if(ctx->arrays[array_id]) {
1041 FIXME("Array already initialized\n");
1042 return E_FAIL;
1043 }
1044
1045 array_desc = ctx->func->array_descs + array_id;
1046 if(array_desc->dim_cnt) {
1047 ctx->arrays[array_id] = SafeArrayCreate(VT_VARIANT, array_desc->dim_cnt, array_desc->bounds);
1048 if(!ctx->arrays[array_id])
1049 return E_OUTOFMEMORY;
1050 }
1051
1052 V_VT(ref.u.v) = VT_ARRAY|VT_BYREF|VT_VARIANT;
1053 V_ARRAYREF(ref.u.v) = ctx->arrays+array_id;
1054 return S_OK;
1055 }
1056
1057 static HRESULT interp_step(exec_ctx_t *ctx)
1058 {
1059 const BSTR ident = ctx->instr->arg2.bstr;
1060 BOOL gteq_zero;
1061 VARIANT zero;
1062 ref_t ref;
1063 HRESULT hres;
1064
1065 TRACE("%s\n", debugstr_w(ident));
1066
1067 V_VT(&zero) = VT_I2;
1068 V_I2(&zero) = 0;
1069 hres = VarCmp(stack_top(ctx, 0), &zero, ctx->script->lcid, 0);
1070 if(FAILED(hres))
1071 return hres;
1072
1073 gteq_zero = hres == VARCMP_GT || hres == VARCMP_EQ;
1074
1075 hres = lookup_identifier(ctx, ident, VBDISP_ANY, &ref);
1076 if(FAILED(hres))
1077 return hres;
1078
1079 if(ref.type != REF_VAR) {
1080 FIXME("%s is not REF_VAR\n", debugstr_w(ident));
1081 return E_FAIL;
1082 }
1083
1084 hres = VarCmp(ref.u.v, stack_top(ctx, 1), ctx->script->lcid, 0);
1085 if(FAILED(hres))
1086 return hres;
1087
1088 if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT)) {
1089 ctx->instr++;
1090 }else {
1091 stack_popn(ctx, 2);
1092 instr_jmp(ctx, ctx->instr->arg1.uint);
1093 }
1094 return S_OK;
1095 }
1096
1097 static HRESULT interp_newenum(exec_ctx_t *ctx)
1098 {
1099 variant_val_t v;
1100 VARIANT *r;
1101 HRESULT hres;
1102
1103 TRACE("\n");
1104
1105 stack_pop_deref(ctx, &v);
1106 assert(V_VT(stack_top(ctx, 0)) == VT_EMPTY);
1107 r = stack_top(ctx, 0);
1108
1109 switch(V_VT(v.v)) {
1110 case VT_DISPATCH|VT_BYREF:
1111 case VT_DISPATCH: {
1112 IEnumVARIANT *iter;
1113 DISPPARAMS dp = {0};
1114 VARIANT iterv;
1115
1116 hres = disp_call(ctx->script, V_ISBYREF(v.v) ? *V_DISPATCHREF(v.v) : V_DISPATCH(v.v), DISPID_NEWENUM, &dp, &iterv);
1117 release_val(&v);
1118 if(FAILED(hres))
1119 return hres;
1120
1121 if(V_VT(&iterv) != VT_UNKNOWN && V_VT(&iterv) != VT_DISPATCH) {
1122 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv));
1123 VariantClear(&iterv);
1124 return hres;
1125 }
1126
1127 hres = IUnknown_QueryInterface(V_UNKNOWN(&iterv), &IID_IEnumVARIANT, (void**)&iter);
1128 IUnknown_Release(V_UNKNOWN(&iterv));
1129 if(FAILED(hres)) {
1130 FIXME("Could not get IEnumVARIANT iface: %08x\n", hres);
1131 return hres;
1132 }
1133
1134 V_VT(r) = VT_UNKNOWN;
1135 V_UNKNOWN(r) = (IUnknown*)iter;
1136 break;
1137 }
1138 default:
1139 FIXME("Unsupported for %s\n", debugstr_variant(v.v));
1140 release_val(&v);
1141 return E_NOTIMPL;
1142 }
1143
1144 return S_OK;
1145 }
1146
1147 static HRESULT interp_enumnext(exec_ctx_t *ctx)
1148 {
1149 const unsigned loop_end = ctx->instr->arg1.uint;
1150 const BSTR ident = ctx->instr->arg2.bstr;
1151 VARIANT v;
1152 DISPPARAMS dp = {&v, &propput_dispid, 1, 1};
1153 IEnumVARIANT *iter;
1154 BOOL do_continue;
1155 HRESULT hres;
1156
1157 TRACE("\n");
1158
1159 if(V_VT(stack_top(ctx, 0)) == VT_EMPTY) {
1160 FIXME("uninitialized\n");
1161 return E_FAIL;
1162 }
1163
1164 assert(V_VT(stack_top(ctx, 0)) == VT_UNKNOWN);
1165 iter = (IEnumVARIANT*)V_UNKNOWN(stack_top(ctx, 0));
1166
1167 V_VT(&v) = VT_EMPTY;
1168 hres = IEnumVARIANT_Next(iter, 1, &v, NULL);
1169 if(FAILED(hres))
1170 return hres;
1171
1172 do_continue = hres == S_OK;
1173 hres = assign_ident(ctx, ident, &dp);
1174 VariantClear(&v);
1175 if(FAILED(hres))
1176 return hres;
1177
1178 if(do_continue) {
1179 ctx->instr++;
1180 }else {
1181 stack_pop(ctx);
1182 instr_jmp(ctx, loop_end);
1183 }
1184 return S_OK;
1185 }
1186
1187 static HRESULT interp_jmp(exec_ctx_t *ctx)
1188 {
1189 const unsigned arg = ctx->instr->arg1.uint;
1190
1191 TRACE("%u\n", arg);
1192
1193 instr_jmp(ctx, arg);
1194 return S_OK;
1195 }
1196
1197 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
1198 {
1199 const unsigned arg = ctx->instr->arg1.uint;
1200 HRESULT hres;
1201 BOOL b;
1202
1203 TRACE("%u\n", arg);
1204
1205 hres = stack_pop_bool(ctx, &b);
1206 if(FAILED(hres))
1207 return hres;
1208
1209 if(b)
1210 ctx->instr++;
1211 else
1212 instr_jmp(ctx, ctx->instr->arg1.uint);
1213 return S_OK;
1214 }
1215
1216 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
1217 {
1218 const unsigned arg = ctx->instr->arg1.uint;
1219 HRESULT hres;
1220 BOOL b;
1221
1222 TRACE("%u\n", arg);
1223
1224 hres = stack_pop_bool(ctx, &b);
1225 if(FAILED(hres))
1226 return hres;
1227
1228 if(b)
1229 instr_jmp(ctx, ctx->instr->arg1.uint);
1230 else
1231 ctx->instr++;
1232 return S_OK;
1233 }
1234
1235 static HRESULT interp_ret(exec_ctx_t *ctx)
1236 {
1237 TRACE("\n");
1238
1239 ctx->instr = NULL;
1240 return S_OK;
1241 }
1242
1243 static HRESULT interp_stop(exec_ctx_t *ctx)
1244 {
1245 WARN("\n");
1246
1247 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1248 return S_OK;
1249 }
1250
1251 static HRESULT interp_me(exec_ctx_t *ctx)
1252 {
1253 VARIANT v;
1254
1255 TRACE("\n");
1256
1257 IDispatch_AddRef(ctx->this_obj);
1258 V_VT(&v) = VT_DISPATCH;
1259 V_DISPATCH(&v) = ctx->this_obj;
1260 return stack_push(ctx, &v);
1261 }
1262
1263 static HRESULT interp_bool(exec_ctx_t *ctx)
1264 {
1265 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
1266 VARIANT v;
1267
1268 TRACE("%s\n", arg ? "true" : "false");
1269
1270 V_VT(&v) = VT_BOOL;
1271 V_BOOL(&v) = arg;
1272 return stack_push(ctx, &v);
1273 }
1274
1275 static HRESULT interp_errmode(exec_ctx_t *ctx)
1276 {
1277 const int err_mode = ctx->instr->arg1.uint;
1278
1279 TRACE("%d\n", err_mode);
1280
1281 ctx->resume_next = err_mode;
1282 ctx->script->err_number = S_OK;
1283 return S_OK;
1284 }
1285
1286 static HRESULT interp_string(exec_ctx_t *ctx)
1287 {
1288 VARIANT v;
1289
1290 TRACE("\n");
1291
1292 V_VT(&v) = VT_BSTR;
1293 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
1294 if(!V_BSTR(&v))
1295 return E_OUTOFMEMORY;
1296
1297 return stack_push(ctx, &v);
1298 }
1299
1300 static HRESULT interp_long(exec_ctx_t *ctx)
1301 {
1302 const LONG arg = ctx->instr->arg1.lng;
1303 VARIANT v;
1304
1305 TRACE("%d\n", arg);
1306
1307 V_VT(&v) = VT_I4;
1308 V_I4(&v) = arg;
1309 return stack_push(ctx, &v);
1310 }
1311
1312 static HRESULT interp_short(exec_ctx_t *ctx)
1313 {
1314 const LONG arg = ctx->instr->arg1.lng;
1315 VARIANT v;
1316
1317 TRACE("%d\n", arg);
1318
1319 V_VT(&v) = VT_I2;
1320 V_I2(&v) = arg;
1321 return stack_push(ctx, &v);
1322 }
1323
1324 static HRESULT interp_double(exec_ctx_t *ctx)
1325 {
1326 const DOUBLE *arg = ctx->instr->arg1.dbl;
1327 VARIANT v;
1328
1329 TRACE("%lf\n", *arg);
1330
1331 V_VT(&v) = VT_R8;
1332 V_R8(&v) = *arg;
1333 return stack_push(ctx, &v);
1334 }
1335
1336 static HRESULT interp_empty(exec_ctx_t *ctx)
1337 {
1338 VARIANT v;
1339
1340 TRACE("\n");
1341
1342 V_VT(&v) = VT_EMPTY;
1343 return stack_push(ctx, &v);
1344 }
1345
1346 static HRESULT interp_null(exec_ctx_t *ctx)
1347 {
1348 TRACE("\n");
1349 return stack_push_null(ctx);
1350 }
1351
1352 static HRESULT interp_nothing(exec_ctx_t *ctx)
1353 {
1354 VARIANT v;
1355
1356 TRACE("\n");
1357
1358 V_VT(&v) = VT_DISPATCH;
1359 V_DISPATCH(&v) = NULL;
1360 return stack_push(ctx, &v);
1361 }
1362
1363 static HRESULT interp_not(exec_ctx_t *ctx)
1364 {
1365 variant_val_t val;
1366 VARIANT v;
1367 HRESULT hres;
1368
1369 TRACE("\n");
1370
1371 hres = stack_pop_val(ctx, &val);
1372 if(FAILED(hres))
1373 return hres;
1374
1375 hres = VarNot(val.v, &v);
1376 release_val(&val);
1377 if(FAILED(hres))
1378 return hres;
1379
1380 return stack_push(ctx, &v);
1381 }
1382
1383 static HRESULT interp_and(exec_ctx_t *ctx)
1384 {
1385 variant_val_t r, l;
1386 VARIANT v;
1387 HRESULT hres;
1388
1389 TRACE("\n");
1390
1391 hres = stack_pop_val(ctx, &r);
1392 if(FAILED(hres))
1393 return hres;
1394
1395 hres = stack_pop_val(ctx, &l);
1396 if(SUCCEEDED(hres)) {
1397 hres = VarAnd(l.v, r.v, &v);
1398 release_val(&l);
1399 }
1400 release_val(&r);
1401 if(FAILED(hres))
1402 return hres;
1403
1404 return stack_push(ctx, &v);
1405 }
1406
1407 static HRESULT interp_or(exec_ctx_t *ctx)
1408 {
1409 variant_val_t r, l;
1410 VARIANT v;
1411 HRESULT hres;
1412
1413 TRACE("\n");
1414
1415 hres = stack_pop_val(ctx, &r);
1416 if(FAILED(hres))
1417 return hres;
1418
1419 hres = stack_pop_val(ctx, &l);
1420 if(SUCCEEDED(hres)) {
1421 hres = VarOr(l.v, r.v, &v);
1422 release_val(&l);
1423 }
1424 release_val(&r);
1425 if(FAILED(hres))
1426 return hres;
1427
1428 return stack_push(ctx, &v);
1429 }
1430
1431 static HRESULT interp_xor(exec_ctx_t *ctx)
1432 {
1433 variant_val_t r, l;
1434 VARIANT v;
1435 HRESULT hres;
1436
1437 TRACE("\n");
1438
1439 hres = stack_pop_val(ctx, &r);
1440 if(FAILED(hres))
1441 return hres;
1442
1443 hres = stack_pop_val(ctx, &l);
1444 if(SUCCEEDED(hres)) {
1445 hres = VarXor(l.v, r.v, &v);
1446 release_val(&l);
1447 }
1448 release_val(&r);
1449 if(FAILED(hres))
1450 return hres;
1451
1452 return stack_push(ctx, &v);
1453 }
1454
1455 static HRESULT interp_eqv(exec_ctx_t *ctx)
1456 {
1457 variant_val_t r, l;
1458 VARIANT v;
1459 HRESULT hres;
1460
1461 TRACE("\n");
1462
1463 hres = stack_pop_val(ctx, &r);
1464 if(FAILED(hres))
1465 return hres;
1466
1467 hres = stack_pop_val(ctx, &l);
1468 if(SUCCEEDED(hres)) {
1469 hres = VarEqv(l.v, r.v, &v);
1470 release_val(&l);
1471 }
1472 release_val(&r);
1473 if(FAILED(hres))
1474 return hres;
1475
1476 return stack_push(ctx, &v);
1477 }
1478
1479 static HRESULT interp_imp(exec_ctx_t *ctx)
1480 {
1481 variant_val_t r, l;
1482 VARIANT v;
1483 HRESULT hres;
1484
1485 TRACE("\n");
1486
1487 hres = stack_pop_val(ctx, &r);
1488 if(FAILED(hres))
1489 return hres;
1490
1491 hres = stack_pop_val(ctx, &l);
1492 if(SUCCEEDED(hres)) {
1493 hres = VarImp(l.v, r.v, &v);
1494 release_val(&l);
1495 }
1496 release_val(&r);
1497 if(FAILED(hres))
1498 return hres;
1499
1500 return stack_push(ctx, &v);
1501 }
1502
1503 static HRESULT var_cmp(exec_ctx_t *ctx, VARIANT *l, VARIANT *r)
1504 {
1505 TRACE("%s %s\n", debugstr_variant(l), debugstr_variant(r));
1506
1507 /* FIXME: Fix comparing string to number */
1508
1509 return VarCmp(l, r, ctx->script->lcid, 0);
1510 }
1511
1512 static HRESULT cmp_oper(exec_ctx_t *ctx)
1513 {
1514 variant_val_t l, r;
1515 HRESULT hres;
1516
1517 hres = stack_pop_val(ctx, &r);
1518 if(FAILED(hres))
1519 return hres;
1520
1521 hres = stack_pop_val(ctx, &l);
1522 if(SUCCEEDED(hres)) {
1523 hres = var_cmp(ctx, l.v, r.v);
1524 release_val(&l);
1525 }
1526
1527 release_val(&r);
1528 return hres;
1529 }
1530
1531 static HRESULT interp_equal(exec_ctx_t *ctx)
1532 {
1533 VARIANT v;
1534 HRESULT hres;
1535
1536 TRACE("\n");
1537
1538 hres = cmp_oper(ctx);
1539 if(FAILED(hres))
1540 return hres;
1541 if(hres == VARCMP_NULL)
1542 return stack_push_null(ctx);
1543
1544 V_VT(&v) = VT_BOOL;
1545 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1546 return stack_push(ctx, &v);
1547 }
1548
1549 static HRESULT interp_nequal(exec_ctx_t *ctx)
1550 {
1551 VARIANT v;
1552 HRESULT hres;
1553
1554 TRACE("\n");
1555
1556 hres = cmp_oper(ctx);
1557 if(FAILED(hres))
1558 return hres;
1559 if(hres == VARCMP_NULL)
1560 return stack_push_null(ctx);
1561
1562 V_VT(&v) = VT_BOOL;
1563 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1564 return stack_push(ctx, &v);
1565 }
1566
1567 static HRESULT interp_gt(exec_ctx_t *ctx)
1568 {
1569 VARIANT v;
1570 HRESULT hres;
1571
1572 TRACE("\n");
1573
1574 hres = cmp_oper(ctx);
1575 if(FAILED(hres))
1576 return hres;
1577 if(hres == VARCMP_NULL)
1578 return stack_push_null(ctx);
1579
1580 V_VT(&v) = VT_BOOL;
1581 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1582 return stack_push(ctx, &v);
1583 }
1584
1585 static HRESULT interp_gteq(exec_ctx_t *ctx)
1586 {
1587 VARIANT v;
1588 HRESULT hres;
1589
1590 TRACE("\n");
1591
1592 hres = cmp_oper(ctx);
1593 if(FAILED(hres))
1594 return hres;
1595 if(hres == VARCMP_NULL)
1596 return stack_push_null(ctx);
1597
1598 V_VT(&v) = VT_BOOL;
1599 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1600 return stack_push(ctx, &v);
1601 }
1602
1603 static HRESULT interp_lt(exec_ctx_t *ctx)
1604 {
1605 VARIANT v;
1606 HRESULT hres;
1607
1608 TRACE("\n");
1609
1610 hres = cmp_oper(ctx);
1611 if(FAILED(hres))
1612 return hres;
1613 if(hres == VARCMP_NULL)
1614 return stack_push_null(ctx);
1615
1616 V_VT(&v) = VT_BOOL;
1617 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1618 return stack_push(ctx, &v);
1619 }
1620
1621 static HRESULT interp_lteq(exec_ctx_t *ctx)
1622 {
1623 VARIANT v;
1624 HRESULT hres;
1625
1626 TRACE("\n");
1627
1628 hres = cmp_oper(ctx);
1629 if(FAILED(hres))
1630 return hres;
1631 if(hres == VARCMP_NULL)
1632 return stack_push_null(ctx);
1633
1634 V_VT(&v) = VT_BOOL;
1635 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1636 return stack_push(ctx, &v);
1637 }
1638
1639 static HRESULT interp_case(exec_ctx_t *ctx)
1640 {
1641 const unsigned arg = ctx->instr->arg1.uint;
1642 variant_val_t v;
1643 HRESULT hres;
1644
1645 TRACE("%d\n", arg);
1646
1647 hres = stack_pop_val(ctx, &v);
1648 if(FAILED(hres))
1649 return hres;
1650
1651 hres = var_cmp(ctx, stack_top(ctx, 0), v.v);
1652 release_val(&v);
1653 if(FAILED(hres))
1654 return hres;
1655
1656 if(hres == VARCMP_EQ) {
1657 stack_popn(ctx, 1);
1658 instr_jmp(ctx, arg);
1659 }else {
1660 ctx->instr++;
1661 }
1662
1663 return S_OK;
1664 }
1665
1666 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1667 {
1668 IObjectIdentity *identity;
1669 IUnknown *unk1, *unk2;
1670 HRESULT hres;
1671
1672 if(disp1 == disp2) {
1673 *ret = VARIANT_TRUE;
1674 return S_OK;
1675 }
1676
1677 if(!disp1 || !disp2) {
1678 *ret = VARIANT_FALSE;
1679 return S_OK;
1680 }
1681
1682 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1683 if(FAILED(hres))
1684 return hres;
1685
1686 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1687 if(FAILED(hres)) {
1688 IUnknown_Release(unk1);
1689 return hres;
1690 }
1691
1692 if(unk1 == unk2) {
1693 *ret = VARIANT_TRUE;
1694 }else {
1695 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
1696 if(SUCCEEDED(hres)) {
1697 hres = IObjectIdentity_IsEqualObject(identity, unk2);
1698 IObjectIdentity_Release(identity);
1699 *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
1700 }else {
1701 *ret = VARIANT_FALSE;
1702 }
1703 }
1704
1705 IUnknown_Release(unk1);
1706 IUnknown_Release(unk2);
1707 return S_OK;
1708 }
1709
1710 static HRESULT interp_is(exec_ctx_t *ctx)
1711 {
1712 IDispatch *l, *r;
1713 VARIANT v;
1714 HRESULT hres;
1715
1716 TRACE("\n");
1717
1718 hres = stack_pop_disp(ctx, &r);
1719 if(FAILED(hres))
1720 return hres;
1721
1722 hres = stack_pop_disp(ctx, &l);
1723 if(SUCCEEDED(hres)) {
1724 V_VT(&v) = VT_BOOL;
1725 hres = disp_cmp(l, r, &V_BOOL(&v));
1726 if(l)
1727 IDispatch_Release(l);
1728 }
1729 if(r)
1730 IDispatch_Release(r);
1731 if(FAILED(hres))
1732 return hres;
1733
1734 return stack_push(ctx, &v);
1735 }
1736
1737 static HRESULT interp_concat(exec_ctx_t *ctx)
1738 {
1739 variant_val_t r, l;
1740 VARIANT v;
1741 HRESULT hres;
1742
1743 TRACE("\n");
1744
1745 hres = stack_pop_val(ctx, &r);
1746 if(FAILED(hres))
1747 return hres;
1748
1749 hres = stack_pop_val(ctx, &l);
1750 if(SUCCEEDED(hres)) {
1751 hres = VarCat(l.v, r.v, &v);
1752 release_val(&l);
1753 }
1754 release_val(&r);
1755 if(FAILED(hres))
1756 return hres;
1757
1758 return stack_push(ctx, &v);
1759 }
1760
1761 static HRESULT interp_add(exec_ctx_t *ctx)
1762 {
1763 variant_val_t r, l;
1764 VARIANT v;
1765 HRESULT hres;
1766
1767 TRACE("\n");
1768
1769 hres = stack_pop_val(ctx, &r);
1770 if(FAILED(hres))
1771 return hres;
1772
1773 hres = stack_pop_val(ctx, &l);
1774 if(SUCCEEDED(hres)) {
1775 hres = VarAdd(l.v, r.v, &v);
1776 release_val(&l);
1777 }
1778 release_val(&r);
1779 if(FAILED(hres))
1780 return hres;
1781
1782 return stack_push(ctx, &v);
1783 }
1784
1785 static HRESULT interp_sub(exec_ctx_t *ctx)
1786 {
1787 variant_val_t r, l;
1788 VARIANT v;
1789 HRESULT hres;
1790
1791 TRACE("\n");
1792
1793 hres = stack_pop_val(ctx, &r);
1794 if(FAILED(hres))
1795 return hres;
1796
1797 hres = stack_pop_val(ctx, &l);
1798 if(SUCCEEDED(hres)) {
1799 hres = VarSub(l.v, r.v, &v);
1800 release_val(&l);
1801 }
1802 release_val(&r);
1803 if(FAILED(hres))
1804 return hres;
1805
1806 return stack_push(ctx, &v);
1807 }
1808
1809 static HRESULT interp_mod(exec_ctx_t *ctx)
1810 {
1811 variant_val_t r, l;
1812 VARIANT v;
1813 HRESULT hres;
1814
1815 TRACE("\n");
1816
1817 hres = stack_pop_val(ctx, &r);
1818 if(FAILED(hres))
1819 return hres;
1820
1821 hres = stack_pop_val(ctx, &l);
1822 if(SUCCEEDED(hres)) {
1823 hres = VarMod(l.v, r.v, &v);
1824 release_val(&l);
1825 }
1826 release_val(&r);
1827 if(FAILED(hres))
1828 return hres;
1829
1830 return stack_push(ctx, &v);
1831 }
1832
1833 static HRESULT interp_idiv(exec_ctx_t *ctx)
1834 {
1835 variant_val_t r, l;
1836 VARIANT v;
1837 HRESULT hres;
1838
1839 TRACE("\n");
1840
1841 hres = stack_pop_val(ctx, &r);
1842 if(FAILED(hres))
1843 return hres;
1844
1845 hres = stack_pop_val(ctx, &l);
1846 if(SUCCEEDED(hres)) {
1847 hres = VarIdiv(l.v, r.v, &v);
1848 release_val(&l);
1849 }
1850 release_val(&r);
1851 if(FAILED(hres))
1852 return hres;
1853
1854 return stack_push(ctx, &v);
1855 }
1856
1857 static HRESULT interp_div(exec_ctx_t *ctx)
1858 {
1859 variant_val_t r, l;
1860 VARIANT v;
1861 HRESULT hres;
1862
1863 TRACE("\n");
1864
1865 hres = stack_pop_val(ctx, &r);
1866 if(FAILED(hres))
1867 return hres;
1868
1869 hres = stack_pop_val(ctx, &l);
1870 if(SUCCEEDED(hres)) {
1871 hres = VarDiv(l.v, r.v, &v);
1872 release_val(&l);
1873 }
1874 release_val(&r);
1875 if(FAILED(hres))
1876 return hres;
1877
1878 return stack_push(ctx, &v);
1879 }
1880
1881 static HRESULT interp_mul(exec_ctx_t *ctx)
1882 {
1883 variant_val_t r, l;
1884 VARIANT v;
1885 HRESULT hres;
1886
1887 TRACE("\n");
1888
1889 hres = stack_pop_val(ctx, &r);
1890 if(FAILED(hres))
1891 return hres;
1892
1893 hres = stack_pop_val(ctx, &l);
1894 if(SUCCEEDED(hres)) {
1895 hres = VarMul(l.v, r.v, &v);
1896 release_val(&l);
1897 }
1898 release_val(&r);
1899 if(FAILED(hres))
1900 return hres;
1901
1902 return stack_push(ctx, &v);
1903 }
1904
1905 static HRESULT interp_exp(exec_ctx_t *ctx)
1906 {
1907 variant_val_t r, l;
1908 VARIANT v;
1909 HRESULT hres;
1910
1911 TRACE("\n");
1912
1913 hres = stack_pop_val(ctx, &r);
1914 if(FAILED(hres))
1915 return hres;
1916
1917 hres = stack_pop_val(ctx, &l);
1918 if(SUCCEEDED(hres)) {
1919 hres = VarPow(l.v, r.v, &v);
1920 release_val(&l);
1921 }
1922 release_val(&r);
1923 if(FAILED(hres))
1924 return hres;
1925
1926 return stack_push(ctx, &v);
1927 }
1928
1929 static HRESULT interp_neg(exec_ctx_t *ctx)
1930 {
1931 variant_val_t val;
1932 VARIANT v;
1933 HRESULT hres;
1934
1935 hres = stack_pop_val(ctx, &val);
1936 if(FAILED(hres))
1937 return hres;
1938
1939 hres = VarNeg(val.v, &v);
1940 release_val(&val);
1941 if(FAILED(hres))
1942 return hres;
1943
1944 return stack_push(ctx, &v);
1945 }
1946
1947 static HRESULT interp_incc(exec_ctx_t *ctx)
1948 {
1949 const BSTR ident = ctx->instr->arg1.bstr;
1950 VARIANT v;
1951 ref_t ref;
1952 HRESULT hres;
1953
1954 TRACE("\n");
1955
1956 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1957 if(FAILED(hres))
1958 return hres;
1959
1960 if(ref.type != REF_VAR) {
1961 FIXME("ref.type is not REF_VAR\n");
1962 return E_FAIL;
1963 }
1964
1965 hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
1966 if(FAILED(hres))
1967 return hres;
1968
1969 VariantClear(ref.u.v);
1970 *ref.u.v = v;
1971 return S_OK;
1972 }
1973
1974 static HRESULT interp_catch(exec_ctx_t *ctx)
1975 {
1976 /* Nothing to do here, the OP is for unwinding only. */
1977 return S_OK;
1978 }
1979
1980 static const instr_func_t op_funcs[] = {
1981 #define X(x,n,a,b) interp_ ## x,
1982 OP_LIST
1983 #undef X
1984 };
1985
1986 static const unsigned op_move[] = {
1987 #define X(x,n,a,b) n,
1988 OP_LIST
1989 #undef X
1990 };
1991
1992 void release_dynamic_vars(dynamic_var_t *var)
1993 {
1994 while(var) {
1995 VariantClear(&var->v);
1996 var = var->next;
1997 }
1998 }
1999
2000 static void release_exec(exec_ctx_t *ctx)
2001 {
2002 unsigned i;
2003
2004 VariantClear(&ctx->ret_val);
2005 release_dynamic_vars(ctx->dynamic_vars);
2006
2007 if(ctx->this_obj)
2008 IDispatch_Release(ctx->this_obj);
2009
2010 if(ctx->args) {
2011 for(i=0; i < ctx->func->arg_cnt; i++)
2012 VariantClear(ctx->args+i);
2013 }
2014
2015 if(ctx->vars) {
2016 for(i=0; i < ctx->func->var_cnt; i++)
2017 VariantClear(ctx->vars+i);
2018 }
2019
2020 if(ctx->arrays) {
2021 for(i=0; i < ctx->func->var_cnt; i++) {
2022 if(ctx->arrays[i])
2023 SafeArrayDestroy(ctx->arrays[i]);
2024 }
2025 heap_free(ctx->arrays);
2026 }
2027
2028 heap_pool_free(&ctx->heap);
2029 heap_free(ctx->args);
2030 heap_free(ctx->vars);
2031 heap_free(ctx->stack);
2032 }
2033
2034 HRESULT exec_script(script_ctx_t *ctx, function_t *func, vbdisp_t *vbthis, DISPPARAMS *dp, VARIANT *res)
2035 {
2036 exec_ctx_t exec = {func->code_ctx};
2037 vbsop_t op;
2038 HRESULT hres = S_OK;
2039
2040 exec.code = func->code_ctx;
2041
2042 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
2043 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
2044 return E_FAIL;
2045 }
2046
2047 heap_pool_init(&exec.heap);
2048
2049 if(func->arg_cnt) {
2050 VARIANT *v;
2051 unsigned i;
2052
2053 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
2054 if(!exec.args) {
2055 release_exec(&exec);
2056 return E_OUTOFMEMORY;
2057 }
2058
2059 for(i=0; i < func->arg_cnt; i++) {
2060 v = get_arg(dp, i);
2061 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
2062 if(func->args[i].by_ref)
2063 exec.args[i] = *v;
2064 else
2065 hres = VariantCopyInd(exec.args+i, V_VARIANTREF(v));
2066 }else {
2067 hres = VariantCopyInd(exec.args+i, v);
2068 }
2069 if(FAILED(hres)) {
2070 release_exec(&exec);
2071 return hres;
2072 }
2073 }
2074 }else {
2075 exec.args = NULL;
2076 }
2077
2078 if(func->var_cnt) {
2079 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
2080 if(!exec.vars) {
2081 release_exec(&exec);
2082 return E_OUTOFMEMORY;
2083 }
2084 }else {
2085 exec.vars = NULL;
2086 }
2087
2088 exec.stack_size = 16;
2089 exec.top = 0;
2090 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
2091 if(!exec.stack) {
2092 release_exec(&exec);
2093 return E_OUTOFMEMORY;
2094 }
2095
2096 if(vbthis) {
2097 exec.this_obj = (IDispatch*)&vbthis->IDispatchEx_iface;
2098 exec.vbthis = vbthis;
2099 }else if (ctx->host_global) {
2100 exec.this_obj = ctx->host_global;
2101 }else {
2102 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
2103 }
2104 IDispatch_AddRef(exec.this_obj);
2105
2106 exec.instr = exec.code->instrs + func->code_off;
2107 exec.script = ctx;
2108 exec.func = func;
2109
2110 while(exec.instr) {
2111 op = exec.instr->op;
2112 hres = op_funcs[op](&exec);
2113 if(FAILED(hres)) {
2114 ctx->err_number = hres = map_hres(hres);
2115
2116 if(exec.resume_next) {
2117 unsigned stack_off;
2118
2119 WARN("Failed %08x in resume next mode\n", hres);
2120
2121 /*
2122 * Unwinding here is simple. We need to find the next OP_catch, which contains
2123 * information about expected stack size and jump offset on error. Generated
2124 * bytecode needs to guarantee, that simple jump and stack adjustment will
2125 * guarantee proper execution continuation.
2126 */
2127 while((++exec.instr)->op != OP_catch);
2128
2129 TRACE("unwind jmp %d stack_off %d\n", exec.instr->arg1.uint, exec.instr->arg2.uint);
2130
2131 stack_off = exec.instr->arg2.uint;
2132 instr_jmp(&exec, exec.instr->arg1.uint);
2133
2134 if(exec.top > stack_off) {
2135 stack_popn(&exec, exec.top-stack_off);
2136 }else if(exec.top < stack_off) {
2137 VARIANT v;
2138
2139 V_VT(&v) = VT_EMPTY;
2140 while(exec.top < stack_off) {
2141 hres = stack_push(&exec, &v);
2142 if(FAILED(hres))
2143 break;
2144 }
2145 }
2146
2147 continue;
2148 }else {
2149 WARN("Failed %08x\n", hres);
2150 stack_popn(&exec, exec.top);
2151 break;
2152 }
2153 }
2154
2155 exec.instr += op_move[op];
2156 }
2157
2158 assert(!exec.top);
2159 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
2160 assert(V_VT(&exec.ret_val) == VT_EMPTY);
2161
2162 if(SUCCEEDED(hres) && res) {
2163 *res = exec.ret_val;
2164 V_VT(&exec.ret_val) = VT_EMPTY;
2165 }
2166
2167 release_exec(&exec);
2168 return hres;
2169 }