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