* Sync up to trunk HEAD (r62975).
[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;
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 default:
568 FIXME("arguments not implemented\n");
569 return E_NOTIMPL;
570 }
571
572 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
573 hres = array_access(ctx, array, &dp, &v);
574 if(FAILED(hres))
575 return hres;
576 }
577
578 V_VT(res) = VT_BYREF|VT_VARIANT;
579 V_BYREF(res) = v;
580 break;
581 }
582 case REF_DISP:
583 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
584 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
585 if(FAILED(hres))
586 return hres;
587 break;
588 case REF_FUNC:
589 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
590 hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
591 if(FAILED(hres))
592 return hres;
593 break;
594 case REF_OBJ:
595 if(arg_cnt) {
596 FIXME("arguments on object\n");
597 return E_NOTIMPL;
598 }
599
600 if(res) {
601 IDispatch_AddRef(ref.u.obj);
602 V_VT(res) = VT_DISPATCH;
603 V_DISPATCH(res) = ref.u.obj;
604 }
605 break;
606 case REF_NONE:
607 if(res && !ctx->func->code_ctx->option_explicit && arg_cnt == 0) {
608 VARIANT v, *new;
609 VariantInit(&v);
610 hres = add_dynamic_var(ctx, identifier, FALSE, &v, FALSE, &new);
611 if(FAILED(hres))
612 return hres;
613 V_VT(res) = VT_BYREF|VT_VARIANT;
614 V_BYREF(res) = new;
615 break;
616 }
617 FIXME("%s not found\n", debugstr_w(identifier));
618 return DISP_E_UNKNOWNNAME;
619 }
620
621 stack_popn(ctx, arg_cnt);
622 return S_OK;
623 }
624
625 static HRESULT interp_icall(exec_ctx_t *ctx)
626 {
627 VARIANT v;
628 HRESULT hres;
629
630 TRACE("\n");
631
632 hres = do_icall(ctx, &v);
633 if(FAILED(hres))
634 return hres;
635
636 return stack_push(ctx, &v);
637 }
638
639 static HRESULT interp_icallv(exec_ctx_t *ctx)
640 {
641 TRACE("\n");
642 return do_icall(ctx, NULL);
643 }
644
645 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
646 {
647 const BSTR identifier = ctx->instr->arg1.bstr;
648 const unsigned arg_cnt = ctx->instr->arg2.uint;
649 IDispatch *obj;
650 DISPPARAMS dp;
651 DISPID id;
652 HRESULT hres;
653
654 hres = stack_pop_disp(ctx, &obj);
655 if(FAILED(hres))
656 return hres;
657
658 if(!obj) {
659 FIXME("NULL obj\n");
660 return E_FAIL;
661 }
662
663 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
664
665 hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
666 if(SUCCEEDED(hres))
667 hres = disp_call(ctx->script, obj, id, &dp, res);
668 IDispatch_Release(obj);
669 if(FAILED(hres))
670 return hres;
671
672 stack_popn(ctx, arg_cnt);
673 return S_OK;
674 }
675
676 static HRESULT interp_mcall(exec_ctx_t *ctx)
677 {
678 VARIANT res;
679 HRESULT hres;
680
681 TRACE("\n");
682
683 hres = do_mcall(ctx, &res);
684 if(FAILED(hres))
685 return hres;
686
687 return stack_push(ctx, &res);
688 }
689
690 static HRESULT interp_mcallv(exec_ctx_t *ctx)
691 {
692 TRACE("\n");
693
694 return do_mcall(ctx, NULL);
695 }
696
697 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, DISPPARAMS *dp)
698 {
699 ref_t ref;
700 HRESULT hres;
701
702 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
703 if(FAILED(hres))
704 return hres;
705
706 switch(ref.type) {
707 case REF_VAR: {
708 VARIANT *v = ref.u.v;
709
710 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
711 v = V_VARIANTREF(v);
712
713 if(arg_cnt(dp)) {
714 SAFEARRAY *array;
715
716 if(!(V_VT(v) & VT_ARRAY)) {
717 FIXME("array assign on type %d\n", V_VT(v));
718 return E_FAIL;
719 }
720
721 switch(V_VT(v)) {
722 case VT_ARRAY|VT_BYREF|VT_VARIANT:
723 array = *V_ARRAYREF(v);
724 break;
725 case VT_ARRAY|VT_VARIANT:
726 array = V_ARRAY(v);
727 break;
728 default:
729 FIXME("Unsupported array type %x\n", V_VT(v));
730 return E_NOTIMPL;
731 }
732
733 if(!array) {
734 FIXME("null array\n");
735 return E_FAIL;
736 }
737
738 hres = array_access(ctx, array, dp, &v);
739 if(FAILED(hres))
740 return hres;
741 }else if(V_VT(v) == (VT_ARRAY|VT_BYREF|VT_VARIANT)) {
742 FIXME("non-array assign\n");
743 return E_NOTIMPL;
744 }
745
746 hres = VariantCopyInd(v, dp->rgvarg);
747 break;
748 }
749 case REF_DISP:
750 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, dp);
751 break;
752 case REF_FUNC:
753 FIXME("functions not implemented\n");
754 return E_NOTIMPL;
755 case REF_OBJ:
756 FIXME("REF_OBJ\n");
757 return E_NOTIMPL;
758 case REF_CONST:
759 FIXME("REF_CONST\n");
760 return E_NOTIMPL;
761 case REF_NONE:
762 if(ctx->func->code_ctx->option_explicit) {
763 FIXME("throw exception\n");
764 hres = E_FAIL;
765 }else {
766 if(arg_cnt(dp)) {
767 FIXME("arg_cnt %d not supported\n", arg_cnt(dp));
768 return E_NOTIMPL;
769 }
770
771 TRACE("creating variable %s\n", debugstr_w(name));
772 hres = add_dynamic_var(ctx, name, FALSE, dp->rgvarg, FALSE, NULL);
773 }
774 }
775
776 return hres;
777 }
778
779 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
780 {
781 const BSTR arg = ctx->instr->arg1.bstr;
782 const unsigned arg_cnt = ctx->instr->arg2.uint;
783 DISPPARAMS dp;
784 HRESULT hres;
785
786 TRACE("%s\n", debugstr_w(arg));
787
788 hres = stack_assume_val(ctx, arg_cnt);
789 if(FAILED(hres))
790 return hres;
791
792 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
793 hres = assign_ident(ctx, arg, &dp);
794 if(FAILED(hres))
795 return hres;
796
797 stack_popn(ctx, arg_cnt+1);
798 return S_OK;
799 }
800
801 static HRESULT interp_set_ident(exec_ctx_t *ctx)
802 {
803 const BSTR arg = ctx->instr->arg1.bstr;
804 const unsigned arg_cnt = ctx->instr->arg2.uint;
805 DISPPARAMS dp;
806 HRESULT hres;
807
808 TRACE("%s\n", debugstr_w(arg));
809
810 if(arg_cnt) {
811 FIXME("arguments not supported\n");
812 return E_NOTIMPL;
813 }
814
815 hres = stack_assume_disp(ctx, 0, NULL);
816 if(FAILED(hres))
817 return hres;
818
819 vbstack_to_dp(ctx, 0, TRUE, &dp);
820 hres = assign_ident(ctx, ctx->instr->arg1.bstr, &dp);
821 if(FAILED(hres))
822 return hres;
823
824 stack_popn(ctx, 1);
825 return S_OK;
826 }
827
828 static HRESULT interp_assign_member(exec_ctx_t *ctx)
829 {
830 BSTR identifier = ctx->instr->arg1.bstr;
831 const unsigned arg_cnt = ctx->instr->arg2.uint;
832 IDispatch *obj;
833 DISPPARAMS dp;
834 DISPID id;
835 HRESULT hres;
836
837 TRACE("%s\n", debugstr_w(identifier));
838
839 hres = stack_assume_disp(ctx, arg_cnt+1, &obj);
840 if(FAILED(hres))
841 return hres;
842
843 if(!obj) {
844 FIXME("NULL obj\n");
845 return E_FAIL;
846 }
847
848 hres = stack_assume_val(ctx, arg_cnt);
849 if(FAILED(hres))
850 return hres;
851
852 hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
853 if(SUCCEEDED(hres)) {
854 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
855 hres = disp_propput(ctx->script, obj, id, &dp);
856 }
857 if(FAILED(hres))
858 return hres;
859
860 stack_popn(ctx, arg_cnt+2);
861 return S_OK;
862 }
863
864 static HRESULT interp_set_member(exec_ctx_t *ctx)
865 {
866 BSTR identifier = ctx->instr->arg1.bstr;
867 const unsigned arg_cnt = ctx->instr->arg2.uint;
868 IDispatch *obj;
869 DISPPARAMS dp;
870 DISPID id;
871 HRESULT hres;
872
873 TRACE("%s\n", debugstr_w(identifier));
874
875 if(arg_cnt) {
876 FIXME("arguments not supported\n");
877 return E_NOTIMPL;
878 }
879
880 hres = stack_assume_disp(ctx, 1, &obj);
881 if(FAILED(hres))
882 return hres;
883
884 if(!obj) {
885 FIXME("NULL obj\n");
886 return E_FAIL;
887 }
888
889 hres = stack_assume_disp(ctx, 0, NULL);
890 if(FAILED(hres))
891 return hres;
892
893 hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
894 if(SUCCEEDED(hres)) {
895 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
896 hres = disp_propput(ctx->script, obj, id, &dp);
897 }
898 if(FAILED(hres))
899 return hres;
900
901 stack_popn(ctx, 2);
902 return S_OK;
903 }
904
905 static HRESULT interp_const(exec_ctx_t *ctx)
906 {
907 BSTR arg = ctx->instr->arg1.bstr;
908 variant_val_t val;
909 ref_t ref;
910 HRESULT hres;
911
912 TRACE("%s\n", debugstr_w(arg));
913
914 assert(ctx->func->type == FUNC_GLOBAL);
915
916 hres = lookup_identifier(ctx, arg, VBDISP_CALLGET, &ref);
917 if(FAILED(hres))
918 return hres;
919
920 if(ref.type != REF_NONE) {
921 FIXME("%s already defined\n", debugstr_w(arg));
922 return E_FAIL;
923 }
924
925 hres = stack_pop_val(ctx, &val);
926 if(FAILED(hres))
927 return hres;
928
929 return add_dynamic_var(ctx, arg, TRUE, val.v, val.owned, NULL);
930 }
931
932 static HRESULT interp_val(exec_ctx_t *ctx)
933 {
934 variant_val_t val;
935 VARIANT v;
936 HRESULT hres;
937
938 TRACE("\n");
939
940 hres = stack_pop_val(ctx, &val);
941 if(FAILED(hres))
942 return hres;
943
944 if(!val.owned) {
945 V_VT(&v) = VT_EMPTY;
946 hres = VariantCopy(&v, val.v);
947 if(FAILED(hres))
948 return hres;
949 }
950
951 return stack_push(ctx, val.owned ? val.v : &v);
952 }
953
954 static HRESULT interp_pop(exec_ctx_t *ctx)
955 {
956 const unsigned n = ctx->instr->arg1.uint;
957
958 TRACE("%u\n", n);
959
960 stack_popn(ctx, n);
961 return S_OK;
962 }
963
964 static HRESULT interp_new(exec_ctx_t *ctx)
965 {
966 const WCHAR *arg = ctx->instr->arg1.bstr;
967 class_desc_t *class_desc;
968 vbdisp_t *obj;
969 VARIANT v;
970 HRESULT hres;
971
972 static const WCHAR regexpW[] = {'r','e','g','e','x','p',0};
973
974 TRACE("%s\n", debugstr_w(arg));
975
976 if(!strcmpiW(arg, regexpW)) {
977 V_VT(&v) = VT_DISPATCH;
978 hres = create_regexp(&V_DISPATCH(&v));
979 if(FAILED(hres))
980 return hres;
981
982 return stack_push(ctx, &v);
983 }
984
985 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
986 if(!strcmpiW(class_desc->name, arg))
987 break;
988 }
989 if(!class_desc) {
990 FIXME("Class %s not found\n", debugstr_w(arg));
991 return E_FAIL;
992 }
993
994 hres = create_vbdisp(class_desc, &obj);
995 if(FAILED(hres))
996 return hres;
997
998 V_VT(&v) = VT_DISPATCH;
999 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
1000 return stack_push(ctx, &v);
1001 }
1002
1003 static HRESULT interp_dim(exec_ctx_t *ctx)
1004 {
1005 const BSTR ident = ctx->instr->arg1.bstr;
1006 const unsigned array_id = ctx->instr->arg2.uint;
1007 const array_desc_t *array_desc;
1008 ref_t ref;
1009 HRESULT hres;
1010
1011 TRACE("%s\n", debugstr_w(ident));
1012
1013 assert(array_id < ctx->func->array_cnt);
1014 if(!ctx->arrays) {
1015 ctx->arrays = heap_alloc_zero(ctx->func->array_cnt * sizeof(SAFEARRAY*));
1016 if(!ctx->arrays)
1017 return E_OUTOFMEMORY;
1018 }
1019
1020 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1021 if(FAILED(hres)) {
1022 FIXME("lookup %s failed: %08x\n", debugstr_w(ident), hres);
1023 return hres;
1024 }
1025
1026 if(ref.type != REF_VAR) {
1027 FIXME("got ref.type = %d\n", ref.type);
1028 return E_FAIL;
1029 }
1030
1031 if(ctx->arrays[array_id]) {
1032 FIXME("Array already initialized\n");
1033 return E_FAIL;
1034 }
1035
1036 array_desc = ctx->func->array_descs + array_id;
1037 if(array_desc->dim_cnt) {
1038 ctx->arrays[array_id] = SafeArrayCreate(VT_VARIANT, array_desc->dim_cnt, array_desc->bounds);
1039 if(!ctx->arrays[array_id])
1040 return E_OUTOFMEMORY;
1041 }
1042
1043 V_VT(ref.u.v) = VT_ARRAY|VT_BYREF|VT_VARIANT;
1044 V_ARRAYREF(ref.u.v) = ctx->arrays+array_id;
1045 return S_OK;
1046 }
1047
1048 static HRESULT interp_step(exec_ctx_t *ctx)
1049 {
1050 const BSTR ident = ctx->instr->arg2.bstr;
1051 BOOL gteq_zero;
1052 VARIANT zero;
1053 ref_t ref;
1054 HRESULT hres;
1055
1056 TRACE("%s\n", debugstr_w(ident));
1057
1058 V_VT(&zero) = VT_I2;
1059 V_I2(&zero) = 0;
1060 hres = VarCmp(stack_top(ctx, 0), &zero, ctx->script->lcid, 0);
1061 if(FAILED(hres))
1062 return hres;
1063
1064 gteq_zero = hres == VARCMP_GT || hres == VARCMP_EQ;
1065
1066 hres = lookup_identifier(ctx, ident, VBDISP_ANY, &ref);
1067 if(FAILED(hres))
1068 return hres;
1069
1070 if(ref.type != REF_VAR) {
1071 FIXME("%s is not REF_VAR\n", debugstr_w(ident));
1072 return E_FAIL;
1073 }
1074
1075 hres = VarCmp(ref.u.v, stack_top(ctx, 1), ctx->script->lcid, 0);
1076 if(FAILED(hres))
1077 return hres;
1078
1079 if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT)) {
1080 ctx->instr++;
1081 }else {
1082 stack_popn(ctx, 2);
1083 instr_jmp(ctx, ctx->instr->arg1.uint);
1084 }
1085 return S_OK;
1086 }
1087
1088 static HRESULT interp_newenum(exec_ctx_t *ctx)
1089 {
1090 variant_val_t v;
1091 VARIANT *r;
1092 HRESULT hres;
1093
1094 TRACE("\n");
1095
1096 stack_pop_deref(ctx, &v);
1097 assert(V_VT(stack_top(ctx, 0)) == VT_EMPTY);
1098 r = stack_top(ctx, 0);
1099
1100 switch(V_VT(v.v)) {
1101 case VT_DISPATCH|VT_BYREF:
1102 case VT_DISPATCH: {
1103 IEnumVARIANT *iter;
1104 DISPPARAMS dp = {0};
1105 VARIANT iterv;
1106
1107 hres = disp_call(ctx->script, V_ISBYREF(v.v) ? *V_DISPATCHREF(v.v) : V_DISPATCH(v.v), DISPID_NEWENUM, &dp, &iterv);
1108 release_val(&v);
1109 if(FAILED(hres))
1110 return hres;
1111
1112 if(V_VT(&iterv) != VT_UNKNOWN && V_VT(&iterv) != VT_DISPATCH) {
1113 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv));
1114 VariantClear(&iterv);
1115 return hres;
1116 }
1117
1118 hres = IUnknown_QueryInterface(V_UNKNOWN(&iterv), &IID_IEnumVARIANT, (void**)&iter);
1119 IUnknown_Release(V_UNKNOWN(&iterv));
1120 if(FAILED(hres)) {
1121 FIXME("Could not get IEnumVARIANT iface: %08x\n", hres);
1122 return hres;
1123 }
1124
1125 V_VT(r) = VT_UNKNOWN;
1126 V_UNKNOWN(r) = (IUnknown*)iter;
1127 break;
1128 }
1129 default:
1130 FIXME("Unsupported for %s\n", debugstr_variant(v.v));
1131 release_val(&v);
1132 return E_NOTIMPL;
1133 }
1134
1135 return S_OK;
1136 }
1137
1138 static HRESULT interp_enumnext(exec_ctx_t *ctx)
1139 {
1140 const unsigned loop_end = ctx->instr->arg1.uint;
1141 const BSTR ident = ctx->instr->arg2.bstr;
1142 VARIANT v;
1143 DISPPARAMS dp = {&v, &propput_dispid, 1, 1};
1144 IEnumVARIANT *iter;
1145 BOOL do_continue;
1146 HRESULT hres;
1147
1148 TRACE("\n");
1149
1150 if(V_VT(stack_top(ctx, 0)) == VT_EMPTY) {
1151 FIXME("uninitialized\n");
1152 return E_FAIL;
1153 }
1154
1155 assert(V_VT(stack_top(ctx, 0)) == VT_UNKNOWN);
1156 iter = (IEnumVARIANT*)V_UNKNOWN(stack_top(ctx, 0));
1157
1158 V_VT(&v) = VT_EMPTY;
1159 hres = IEnumVARIANT_Next(iter, 1, &v, NULL);
1160 if(FAILED(hres))
1161 return hres;
1162
1163 do_continue = hres == S_OK;
1164 hres = assign_ident(ctx, ident, &dp);
1165 VariantClear(&v);
1166 if(FAILED(hres))
1167 return hres;
1168
1169 if(do_continue) {
1170 ctx->instr++;
1171 }else {
1172 stack_pop(ctx);
1173 instr_jmp(ctx, loop_end);
1174 }
1175 return S_OK;
1176 }
1177
1178 static HRESULT interp_jmp(exec_ctx_t *ctx)
1179 {
1180 const unsigned arg = ctx->instr->arg1.uint;
1181
1182 TRACE("%u\n", arg);
1183
1184 instr_jmp(ctx, arg);
1185 return S_OK;
1186 }
1187
1188 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
1189 {
1190 const unsigned arg = ctx->instr->arg1.uint;
1191 HRESULT hres;
1192 BOOL b;
1193
1194 TRACE("%u\n", arg);
1195
1196 hres = stack_pop_bool(ctx, &b);
1197 if(FAILED(hres))
1198 return hres;
1199
1200 if(b)
1201 ctx->instr++;
1202 else
1203 instr_jmp(ctx, ctx->instr->arg1.uint);
1204 return S_OK;
1205 }
1206
1207 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
1208 {
1209 const unsigned arg = ctx->instr->arg1.uint;
1210 HRESULT hres;
1211 BOOL b;
1212
1213 TRACE("%u\n", arg);
1214
1215 hres = stack_pop_bool(ctx, &b);
1216 if(FAILED(hres))
1217 return hres;
1218
1219 if(b)
1220 instr_jmp(ctx, ctx->instr->arg1.uint);
1221 else
1222 ctx->instr++;
1223 return S_OK;
1224 }
1225
1226 static HRESULT interp_ret(exec_ctx_t *ctx)
1227 {
1228 TRACE("\n");
1229
1230 ctx->instr = NULL;
1231 return S_OK;
1232 }
1233
1234 static HRESULT interp_stop(exec_ctx_t *ctx)
1235 {
1236 WARN("\n");
1237
1238 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1239 return S_OK;
1240 }
1241
1242 static HRESULT interp_me(exec_ctx_t *ctx)
1243 {
1244 VARIANT v;
1245
1246 TRACE("\n");
1247
1248 IDispatch_AddRef(ctx->this_obj);
1249 V_VT(&v) = VT_DISPATCH;
1250 V_DISPATCH(&v) = ctx->this_obj;
1251 return stack_push(ctx, &v);
1252 }
1253
1254 static HRESULT interp_bool(exec_ctx_t *ctx)
1255 {
1256 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
1257 VARIANT v;
1258
1259 TRACE("%s\n", arg ? "true" : "false");
1260
1261 V_VT(&v) = VT_BOOL;
1262 V_BOOL(&v) = arg;
1263 return stack_push(ctx, &v);
1264 }
1265
1266 static HRESULT interp_errmode(exec_ctx_t *ctx)
1267 {
1268 const int err_mode = ctx->instr->arg1.uint;
1269
1270 TRACE("%d\n", err_mode);
1271
1272 ctx->resume_next = err_mode;
1273 return S_OK;
1274 }
1275
1276 static HRESULT interp_string(exec_ctx_t *ctx)
1277 {
1278 VARIANT v;
1279
1280 TRACE("\n");
1281
1282 V_VT(&v) = VT_BSTR;
1283 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
1284 if(!V_BSTR(&v))
1285 return E_OUTOFMEMORY;
1286
1287 return stack_push(ctx, &v);
1288 }
1289
1290 static HRESULT interp_long(exec_ctx_t *ctx)
1291 {
1292 const LONG arg = ctx->instr->arg1.lng;
1293 VARIANT v;
1294
1295 TRACE("%d\n", arg);
1296
1297 V_VT(&v) = VT_I4;
1298 V_I4(&v) = arg;
1299 return stack_push(ctx, &v);
1300 }
1301
1302 static HRESULT interp_short(exec_ctx_t *ctx)
1303 {
1304 const LONG arg = ctx->instr->arg1.lng;
1305 VARIANT v;
1306
1307 TRACE("%d\n", arg);
1308
1309 V_VT(&v) = VT_I2;
1310 V_I2(&v) = arg;
1311 return stack_push(ctx, &v);
1312 }
1313
1314 static HRESULT interp_double(exec_ctx_t *ctx)
1315 {
1316 const DOUBLE *arg = ctx->instr->arg1.dbl;
1317 VARIANT v;
1318
1319 TRACE("%lf\n", *arg);
1320
1321 V_VT(&v) = VT_R8;
1322 V_R8(&v) = *arg;
1323 return stack_push(ctx, &v);
1324 }
1325
1326 static HRESULT interp_empty(exec_ctx_t *ctx)
1327 {
1328 VARIANT v;
1329
1330 TRACE("\n");
1331
1332 V_VT(&v) = VT_EMPTY;
1333 return stack_push(ctx, &v);
1334 }
1335
1336 static HRESULT interp_null(exec_ctx_t *ctx)
1337 {
1338 TRACE("\n");
1339 return stack_push_null(ctx);
1340 }
1341
1342 static HRESULT interp_nothing(exec_ctx_t *ctx)
1343 {
1344 VARIANT v;
1345
1346 TRACE("\n");
1347
1348 V_VT(&v) = VT_DISPATCH;
1349 V_DISPATCH(&v) = NULL;
1350 return stack_push(ctx, &v);
1351 }
1352
1353 static HRESULT interp_not(exec_ctx_t *ctx)
1354 {
1355 variant_val_t val;
1356 VARIANT v;
1357 HRESULT hres;
1358
1359 TRACE("\n");
1360
1361 hres = stack_pop_val(ctx, &val);
1362 if(FAILED(hres))
1363 return hres;
1364
1365 hres = VarNot(val.v, &v);
1366 release_val(&val);
1367 if(FAILED(hres))
1368 return hres;
1369
1370 return stack_push(ctx, &v);
1371 }
1372
1373 static HRESULT interp_and(exec_ctx_t *ctx)
1374 {
1375 variant_val_t r, l;
1376 VARIANT v;
1377 HRESULT hres;
1378
1379 TRACE("\n");
1380
1381 hres = stack_pop_val(ctx, &r);
1382 if(FAILED(hres))
1383 return hres;
1384
1385 hres = stack_pop_val(ctx, &l);
1386 if(SUCCEEDED(hres)) {
1387 hres = VarAnd(l.v, r.v, &v);
1388 release_val(&l);
1389 }
1390 release_val(&r);
1391 if(FAILED(hres))
1392 return hres;
1393
1394 return stack_push(ctx, &v);
1395 }
1396
1397 static HRESULT interp_or(exec_ctx_t *ctx)
1398 {
1399 variant_val_t r, l;
1400 VARIANT v;
1401 HRESULT hres;
1402
1403 TRACE("\n");
1404
1405 hres = stack_pop_val(ctx, &r);
1406 if(FAILED(hres))
1407 return hres;
1408
1409 hres = stack_pop_val(ctx, &l);
1410 if(SUCCEEDED(hres)) {
1411 hres = VarOr(l.v, r.v, &v);
1412 release_val(&l);
1413 }
1414 release_val(&r);
1415 if(FAILED(hres))
1416 return hres;
1417
1418 return stack_push(ctx, &v);
1419 }
1420
1421 static HRESULT interp_xor(exec_ctx_t *ctx)
1422 {
1423 variant_val_t r, l;
1424 VARIANT v;
1425 HRESULT hres;
1426
1427 TRACE("\n");
1428
1429 hres = stack_pop_val(ctx, &r);
1430 if(FAILED(hres))
1431 return hres;
1432
1433 hres = stack_pop_val(ctx, &l);
1434 if(SUCCEEDED(hres)) {
1435 hres = VarXor(l.v, r.v, &v);
1436 release_val(&l);
1437 }
1438 release_val(&r);
1439 if(FAILED(hres))
1440 return hres;
1441
1442 return stack_push(ctx, &v);
1443 }
1444
1445 static HRESULT interp_eqv(exec_ctx_t *ctx)
1446 {
1447 variant_val_t r, l;
1448 VARIANT v;
1449 HRESULT hres;
1450
1451 TRACE("\n");
1452
1453 hres = stack_pop_val(ctx, &r);
1454 if(FAILED(hres))
1455 return hres;
1456
1457 hres = stack_pop_val(ctx, &l);
1458 if(SUCCEEDED(hres)) {
1459 hres = VarEqv(l.v, r.v, &v);
1460 release_val(&l);
1461 }
1462 release_val(&r);
1463 if(FAILED(hres))
1464 return hres;
1465
1466 return stack_push(ctx, &v);
1467 }
1468
1469 static HRESULT interp_imp(exec_ctx_t *ctx)
1470 {
1471 variant_val_t r, l;
1472 VARIANT v;
1473 HRESULT hres;
1474
1475 TRACE("\n");
1476
1477 hres = stack_pop_val(ctx, &r);
1478 if(FAILED(hres))
1479 return hres;
1480
1481 hres = stack_pop_val(ctx, &l);
1482 if(SUCCEEDED(hres)) {
1483 hres = VarImp(l.v, r.v, &v);
1484 release_val(&l);
1485 }
1486 release_val(&r);
1487 if(FAILED(hres))
1488 return hres;
1489
1490 return stack_push(ctx, &v);
1491 }
1492
1493 static HRESULT var_cmp(exec_ctx_t *ctx, VARIANT *l, VARIANT *r)
1494 {
1495 TRACE("%s %s\n", debugstr_variant(l), debugstr_variant(r));
1496
1497 /* FIXME: Fix comparing string to number */
1498
1499 return VarCmp(l, r, ctx->script->lcid, 0);
1500 }
1501
1502 static HRESULT cmp_oper(exec_ctx_t *ctx)
1503 {
1504 variant_val_t l, r;
1505 HRESULT hres;
1506
1507 hres = stack_pop_val(ctx, &r);
1508 if(FAILED(hres))
1509 return hres;
1510
1511 hres = stack_pop_val(ctx, &l);
1512 if(SUCCEEDED(hres)) {
1513 hres = var_cmp(ctx, l.v, r.v);
1514 release_val(&l);
1515 }
1516
1517 release_val(&r);
1518 return hres;
1519 }
1520
1521 static HRESULT interp_equal(exec_ctx_t *ctx)
1522 {
1523 VARIANT v;
1524 HRESULT hres;
1525
1526 TRACE("\n");
1527
1528 hres = cmp_oper(ctx);
1529 if(FAILED(hres))
1530 return hres;
1531 if(hres == VARCMP_NULL)
1532 return stack_push_null(ctx);
1533
1534 V_VT(&v) = VT_BOOL;
1535 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1536 return stack_push(ctx, &v);
1537 }
1538
1539 static HRESULT interp_nequal(exec_ctx_t *ctx)
1540 {
1541 VARIANT v;
1542 HRESULT hres;
1543
1544 TRACE("\n");
1545
1546 hres = cmp_oper(ctx);
1547 if(FAILED(hres))
1548 return hres;
1549 if(hres == VARCMP_NULL)
1550 return stack_push_null(ctx);
1551
1552 V_VT(&v) = VT_BOOL;
1553 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1554 return stack_push(ctx, &v);
1555 }
1556
1557 static HRESULT interp_gt(exec_ctx_t *ctx)
1558 {
1559 VARIANT v;
1560 HRESULT hres;
1561
1562 TRACE("\n");
1563
1564 hres = cmp_oper(ctx);
1565 if(FAILED(hres))
1566 return hres;
1567 if(hres == VARCMP_NULL)
1568 return stack_push_null(ctx);
1569
1570 V_VT(&v) = VT_BOOL;
1571 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1572 return stack_push(ctx, &v);
1573 }
1574
1575 static HRESULT interp_gteq(exec_ctx_t *ctx)
1576 {
1577 VARIANT v;
1578 HRESULT hres;
1579
1580 TRACE("\n");
1581
1582 hres = cmp_oper(ctx);
1583 if(FAILED(hres))
1584 return hres;
1585 if(hres == VARCMP_NULL)
1586 return stack_push_null(ctx);
1587
1588 V_VT(&v) = VT_BOOL;
1589 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1590 return stack_push(ctx, &v);
1591 }
1592
1593 static HRESULT interp_lt(exec_ctx_t *ctx)
1594 {
1595 VARIANT v;
1596 HRESULT hres;
1597
1598 TRACE("\n");
1599
1600 hres = cmp_oper(ctx);
1601 if(FAILED(hres))
1602 return hres;
1603 if(hres == VARCMP_NULL)
1604 return stack_push_null(ctx);
1605
1606 V_VT(&v) = VT_BOOL;
1607 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1608 return stack_push(ctx, &v);
1609 }
1610
1611 static HRESULT interp_lteq(exec_ctx_t *ctx)
1612 {
1613 VARIANT v;
1614 HRESULT hres;
1615
1616 TRACE("\n");
1617
1618 hres = cmp_oper(ctx);
1619 if(FAILED(hres))
1620 return hres;
1621 if(hres == VARCMP_NULL)
1622 return stack_push_null(ctx);
1623
1624 V_VT(&v) = VT_BOOL;
1625 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1626 return stack_push(ctx, &v);
1627 }
1628
1629 static HRESULT interp_case(exec_ctx_t *ctx)
1630 {
1631 const unsigned arg = ctx->instr->arg1.uint;
1632 variant_val_t v;
1633 HRESULT hres;
1634
1635 TRACE("%d\n", arg);
1636
1637 hres = stack_pop_val(ctx, &v);
1638 if(FAILED(hres))
1639 return hres;
1640
1641 hres = var_cmp(ctx, stack_top(ctx, 0), v.v);
1642 release_val(&v);
1643 if(FAILED(hres))
1644 return hres;
1645
1646 if(hres == VARCMP_EQ) {
1647 stack_popn(ctx, 1);
1648 instr_jmp(ctx, arg);
1649 }else {
1650 ctx->instr++;
1651 }
1652
1653 return S_OK;
1654 }
1655
1656 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1657 {
1658 IObjectIdentity *identity;
1659 IUnknown *unk1, *unk2;
1660 HRESULT hres;
1661
1662 if(disp1 == disp2) {
1663 *ret = VARIANT_TRUE;
1664 return S_OK;
1665 }
1666
1667 if(!disp1 || !disp2) {
1668 *ret = VARIANT_FALSE;
1669 return S_OK;
1670 }
1671
1672 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1673 if(FAILED(hres))
1674 return hres;
1675
1676 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1677 if(FAILED(hres)) {
1678 IUnknown_Release(unk1);
1679 return hres;
1680 }
1681
1682 if(unk1 == unk2) {
1683 *ret = VARIANT_TRUE;
1684 }else {
1685 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
1686 if(SUCCEEDED(hres)) {
1687 hres = IObjectIdentity_IsEqualObject(identity, unk2);
1688 IObjectIdentity_Release(identity);
1689 *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
1690 }else {
1691 *ret = VARIANT_FALSE;
1692 }
1693 }
1694
1695 IUnknown_Release(unk1);
1696 IUnknown_Release(unk2);
1697 return S_OK;
1698 }
1699
1700 static HRESULT interp_is(exec_ctx_t *ctx)
1701 {
1702 IDispatch *l, *r;
1703 VARIANT v;
1704 HRESULT hres;
1705
1706 TRACE("\n");
1707
1708 hres = stack_pop_disp(ctx, &r);
1709 if(FAILED(hres))
1710 return hres;
1711
1712 hres = stack_pop_disp(ctx, &l);
1713 if(SUCCEEDED(hres)) {
1714 V_VT(&v) = VT_BOOL;
1715 hres = disp_cmp(l, r, &V_BOOL(&v));
1716 if(l)
1717 IDispatch_Release(l);
1718 }
1719 if(r)
1720 IDispatch_Release(r);
1721 if(FAILED(hres))
1722 return hres;
1723
1724 return stack_push(ctx, &v);
1725 }
1726
1727 static HRESULT interp_concat(exec_ctx_t *ctx)
1728 {
1729 variant_val_t r, l;
1730 VARIANT v;
1731 HRESULT hres;
1732
1733 TRACE("\n");
1734
1735 hres = stack_pop_val(ctx, &r);
1736 if(FAILED(hres))
1737 return hres;
1738
1739 hres = stack_pop_val(ctx, &l);
1740 if(SUCCEEDED(hres)) {
1741 hres = VarCat(l.v, r.v, &v);
1742 release_val(&l);
1743 }
1744 release_val(&r);
1745 if(FAILED(hres))
1746 return hres;
1747
1748 return stack_push(ctx, &v);
1749 }
1750
1751 static HRESULT interp_add(exec_ctx_t *ctx)
1752 {
1753 variant_val_t r, l;
1754 VARIANT v;
1755 HRESULT hres;
1756
1757 TRACE("\n");
1758
1759 hres = stack_pop_val(ctx, &r);
1760 if(FAILED(hres))
1761 return hres;
1762
1763 hres = stack_pop_val(ctx, &l);
1764 if(SUCCEEDED(hres)) {
1765 hres = VarAdd(l.v, r.v, &v);
1766 release_val(&l);
1767 }
1768 release_val(&r);
1769 if(FAILED(hres))
1770 return hres;
1771
1772 return stack_push(ctx, &v);
1773 }
1774
1775 static HRESULT interp_sub(exec_ctx_t *ctx)
1776 {
1777 variant_val_t r, l;
1778 VARIANT v;
1779 HRESULT hres;
1780
1781 TRACE("\n");
1782
1783 hres = stack_pop_val(ctx, &r);
1784 if(FAILED(hres))
1785 return hres;
1786
1787 hres = stack_pop_val(ctx, &l);
1788 if(SUCCEEDED(hres)) {
1789 hres = VarSub(l.v, r.v, &v);
1790 release_val(&l);
1791 }
1792 release_val(&r);
1793 if(FAILED(hres))
1794 return hres;
1795
1796 return stack_push(ctx, &v);
1797 }
1798
1799 static HRESULT interp_mod(exec_ctx_t *ctx)
1800 {
1801 variant_val_t r, l;
1802 VARIANT v;
1803 HRESULT hres;
1804
1805 TRACE("\n");
1806
1807 hres = stack_pop_val(ctx, &r);
1808 if(FAILED(hres))
1809 return hres;
1810
1811 hres = stack_pop_val(ctx, &l);
1812 if(SUCCEEDED(hres)) {
1813 hres = VarMod(l.v, r.v, &v);
1814 release_val(&l);
1815 }
1816 release_val(&r);
1817 if(FAILED(hres))
1818 return hres;
1819
1820 return stack_push(ctx, &v);
1821 }
1822
1823 static HRESULT interp_idiv(exec_ctx_t *ctx)
1824 {
1825 variant_val_t r, l;
1826 VARIANT v;
1827 HRESULT hres;
1828
1829 TRACE("\n");
1830
1831 hres = stack_pop_val(ctx, &r);
1832 if(FAILED(hres))
1833 return hres;
1834
1835 hres = stack_pop_val(ctx, &l);
1836 if(SUCCEEDED(hres)) {
1837 hres = VarIdiv(l.v, r.v, &v);
1838 release_val(&l);
1839 }
1840 release_val(&r);
1841 if(FAILED(hres))
1842 return hres;
1843
1844 return stack_push(ctx, &v);
1845 }
1846
1847 static HRESULT interp_div(exec_ctx_t *ctx)
1848 {
1849 variant_val_t r, l;
1850 VARIANT v;
1851 HRESULT hres;
1852
1853 TRACE("\n");
1854
1855 hres = stack_pop_val(ctx, &r);
1856 if(FAILED(hres))
1857 return hres;
1858
1859 hres = stack_pop_val(ctx, &l);
1860 if(SUCCEEDED(hres)) {
1861 hres = VarDiv(l.v, r.v, &v);
1862 release_val(&l);
1863 }
1864 release_val(&r);
1865 if(FAILED(hres))
1866 return hres;
1867
1868 return stack_push(ctx, &v);
1869 }
1870
1871 static HRESULT interp_mul(exec_ctx_t *ctx)
1872 {
1873 variant_val_t r, l;
1874 VARIANT v;
1875 HRESULT hres;
1876
1877 TRACE("\n");
1878
1879 hres = stack_pop_val(ctx, &r);
1880 if(FAILED(hres))
1881 return hres;
1882
1883 hres = stack_pop_val(ctx, &l);
1884 if(SUCCEEDED(hres)) {
1885 hres = VarMul(l.v, r.v, &v);
1886 release_val(&l);
1887 }
1888 release_val(&r);
1889 if(FAILED(hres))
1890 return hres;
1891
1892 return stack_push(ctx, &v);
1893 }
1894
1895 static HRESULT interp_exp(exec_ctx_t *ctx)
1896 {
1897 variant_val_t r, l;
1898 VARIANT v;
1899 HRESULT hres;
1900
1901 TRACE("\n");
1902
1903 hres = stack_pop_val(ctx, &r);
1904 if(FAILED(hres))
1905 return hres;
1906
1907 hres = stack_pop_val(ctx, &l);
1908 if(SUCCEEDED(hres)) {
1909 hres = VarPow(l.v, r.v, &v);
1910 release_val(&l);
1911 }
1912 release_val(&r);
1913 if(FAILED(hres))
1914 return hres;
1915
1916 return stack_push(ctx, &v);
1917 }
1918
1919 static HRESULT interp_neg(exec_ctx_t *ctx)
1920 {
1921 variant_val_t val;
1922 VARIANT v;
1923 HRESULT hres;
1924
1925 hres = stack_pop_val(ctx, &val);
1926 if(FAILED(hres))
1927 return hres;
1928
1929 hres = VarNeg(val.v, &v);
1930 release_val(&val);
1931 if(FAILED(hres))
1932 return hres;
1933
1934 return stack_push(ctx, &v);
1935 }
1936
1937 static HRESULT interp_incc(exec_ctx_t *ctx)
1938 {
1939 const BSTR ident = ctx->instr->arg1.bstr;
1940 VARIANT v;
1941 ref_t ref;
1942 HRESULT hres;
1943
1944 TRACE("\n");
1945
1946 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1947 if(FAILED(hres))
1948 return hres;
1949
1950 if(ref.type != REF_VAR) {
1951 FIXME("ref.type is not REF_VAR\n");
1952 return E_FAIL;
1953 }
1954
1955 hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
1956 if(FAILED(hres))
1957 return hres;
1958
1959 VariantClear(ref.u.v);
1960 *ref.u.v = v;
1961 return S_OK;
1962 }
1963
1964 static HRESULT interp_catch(exec_ctx_t *ctx)
1965 {
1966 /* Nothing to do here, the OP is for unwinding only. */
1967 return S_OK;
1968 }
1969
1970 static const instr_func_t op_funcs[] = {
1971 #define X(x,n,a,b) interp_ ## x,
1972 OP_LIST
1973 #undef X
1974 };
1975
1976 static const unsigned op_move[] = {
1977 #define X(x,n,a,b) n,
1978 OP_LIST
1979 #undef X
1980 };
1981
1982 void release_dynamic_vars(dynamic_var_t *var)
1983 {
1984 while(var) {
1985 VariantClear(&var->v);
1986 var = var->next;
1987 }
1988 }
1989
1990 static void release_exec(exec_ctx_t *ctx)
1991 {
1992 unsigned i;
1993
1994 VariantClear(&ctx->ret_val);
1995 release_dynamic_vars(ctx->dynamic_vars);
1996
1997 if(ctx->this_obj)
1998 IDispatch_Release(ctx->this_obj);
1999
2000 if(ctx->args) {
2001 for(i=0; i < ctx->func->arg_cnt; i++)
2002 VariantClear(ctx->args+i);
2003 }
2004
2005 if(ctx->vars) {
2006 for(i=0; i < ctx->func->var_cnt; i++)
2007 VariantClear(ctx->vars+i);
2008 }
2009
2010 if(ctx->arrays) {
2011 for(i=0; i < ctx->func->var_cnt; i++) {
2012 if(ctx->arrays[i])
2013 SafeArrayDestroy(ctx->arrays[i]);
2014 }
2015 heap_free(ctx->arrays);
2016 }
2017
2018 heap_pool_free(&ctx->heap);
2019 heap_free(ctx->args);
2020 heap_free(ctx->vars);
2021 heap_free(ctx->stack);
2022 }
2023
2024 HRESULT exec_script(script_ctx_t *ctx, function_t *func, vbdisp_t *vbthis, DISPPARAMS *dp, VARIANT *res)
2025 {
2026 exec_ctx_t exec = {func->code_ctx};
2027 vbsop_t op;
2028 HRESULT hres = S_OK;
2029
2030 exec.code = func->code_ctx;
2031
2032 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
2033 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
2034 return E_FAIL;
2035 }
2036
2037 heap_pool_init(&exec.heap);
2038
2039 if(func->arg_cnt) {
2040 VARIANT *v;
2041 unsigned i;
2042
2043 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
2044 if(!exec.args) {
2045 release_exec(&exec);
2046 return E_OUTOFMEMORY;
2047 }
2048
2049 for(i=0; i < func->arg_cnt; i++) {
2050 v = get_arg(dp, i);
2051 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
2052 if(func->args[i].by_ref)
2053 exec.args[i] = *v;
2054 else
2055 hres = VariantCopyInd(exec.args+i, V_VARIANTREF(v));
2056 }else {
2057 hres = VariantCopyInd(exec.args+i, v);
2058 }
2059 if(FAILED(hres)) {
2060 release_exec(&exec);
2061 return hres;
2062 }
2063 }
2064 }else {
2065 exec.args = NULL;
2066 }
2067
2068 if(func->var_cnt) {
2069 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
2070 if(!exec.vars) {
2071 release_exec(&exec);
2072 return E_OUTOFMEMORY;
2073 }
2074 }else {
2075 exec.vars = NULL;
2076 }
2077
2078 exec.stack_size = 16;
2079 exec.top = 0;
2080 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
2081 if(!exec.stack) {
2082 release_exec(&exec);
2083 return E_OUTOFMEMORY;
2084 }
2085
2086 if(vbthis) {
2087 exec.this_obj = (IDispatch*)&vbthis->IDispatchEx_iface;
2088 exec.vbthis = vbthis;
2089 }else if (ctx->host_global) {
2090 exec.this_obj = ctx->host_global;
2091 }else {
2092 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
2093 }
2094 IDispatch_AddRef(exec.this_obj);
2095
2096 exec.instr = exec.code->instrs + func->code_off;
2097 exec.script = ctx;
2098 exec.func = func;
2099
2100 while(exec.instr) {
2101 op = exec.instr->op;
2102 hres = op_funcs[op](&exec);
2103 if(FAILED(hres)) {
2104 ctx->err_number = hres = map_hres(hres);
2105
2106 if(exec.resume_next) {
2107 unsigned stack_off;
2108
2109 WARN("Failed %08x in resume next mode\n", hres);
2110
2111 /*
2112 * Unwinding here is simple. We need to find the next OP_catch, which contains
2113 * information about expected stack size and jump offset on error. Generated
2114 * bytecode needs to guarantee, that simple jump and stack adjustment will
2115 * guarantee proper execution continuation.
2116 */
2117 while((++exec.instr)->op != OP_catch);
2118
2119 TRACE("unwind jmp %d stack_off %d\n", exec.instr->arg1.uint, exec.instr->arg2.uint);
2120
2121 stack_off = exec.instr->arg2.uint;
2122 instr_jmp(&exec, exec.instr->arg1.uint);
2123
2124 if(exec.top > stack_off) {
2125 stack_popn(&exec, exec.top-stack_off);
2126 }else if(exec.top < stack_off) {
2127 VARIANT v;
2128
2129 V_VT(&v) = VT_EMPTY;
2130 while(exec.top < stack_off) {
2131 hres = stack_push(&exec, &v);
2132 if(FAILED(hres))
2133 break;
2134 }
2135 }
2136
2137 continue;
2138 }else {
2139 WARN("Failed %08x\n", hres);
2140 stack_popn(&exec, exec.top);
2141 break;
2142 }
2143 }
2144
2145 exec.instr += op_move[op];
2146 }
2147
2148 assert(!exec.top);
2149 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
2150 assert(V_VT(&exec.ret_val) == VT_EMPTY);
2151
2152 if(SUCCEEDED(hres) && res) {
2153 *res = exec.ret_val;
2154 V_VT(&exec.ret_val) = VT_EMPTY;
2155 }
2156
2157 release_exec(&exec);
2158 return hres;
2159 }