[VBSCRIPT] Sync with Wine Staging 1.7.37. CORE-9246
[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 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_hres(exec_ctx_t *ctx)
1364 {
1365 const unsigned arg = ctx->instr->arg1.uint;
1366 VARIANT v;
1367
1368 TRACE("%d\n", arg);
1369
1370 V_VT(&v) = VT_ERROR;
1371 V_ERROR(&v) = arg;
1372 return stack_push(ctx, &v);
1373 }
1374
1375 static HRESULT interp_not(exec_ctx_t *ctx)
1376 {
1377 variant_val_t val;
1378 VARIANT v;
1379 HRESULT hres;
1380
1381 TRACE("\n");
1382
1383 hres = stack_pop_val(ctx, &val);
1384 if(FAILED(hres))
1385 return hres;
1386
1387 hres = VarNot(val.v, &v);
1388 release_val(&val);
1389 if(FAILED(hres))
1390 return hres;
1391
1392 return stack_push(ctx, &v);
1393 }
1394
1395 static HRESULT interp_and(exec_ctx_t *ctx)
1396 {
1397 variant_val_t r, l;
1398 VARIANT v;
1399 HRESULT hres;
1400
1401 TRACE("\n");
1402
1403 hres = stack_pop_val(ctx, &r);
1404 if(FAILED(hres))
1405 return hres;
1406
1407 hres = stack_pop_val(ctx, &l);
1408 if(SUCCEEDED(hres)) {
1409 hres = VarAnd(l.v, r.v, &v);
1410 release_val(&l);
1411 }
1412 release_val(&r);
1413 if(FAILED(hres))
1414 return hres;
1415
1416 return stack_push(ctx, &v);
1417 }
1418
1419 static HRESULT interp_or(exec_ctx_t *ctx)
1420 {
1421 variant_val_t r, l;
1422 VARIANT v;
1423 HRESULT hres;
1424
1425 TRACE("\n");
1426
1427 hres = stack_pop_val(ctx, &r);
1428 if(FAILED(hres))
1429 return hres;
1430
1431 hres = stack_pop_val(ctx, &l);
1432 if(SUCCEEDED(hres)) {
1433 hres = VarOr(l.v, r.v, &v);
1434 release_val(&l);
1435 }
1436 release_val(&r);
1437 if(FAILED(hres))
1438 return hres;
1439
1440 return stack_push(ctx, &v);
1441 }
1442
1443 static HRESULT interp_xor(exec_ctx_t *ctx)
1444 {
1445 variant_val_t r, l;
1446 VARIANT v;
1447 HRESULT hres;
1448
1449 TRACE("\n");
1450
1451 hres = stack_pop_val(ctx, &r);
1452 if(FAILED(hres))
1453 return hres;
1454
1455 hres = stack_pop_val(ctx, &l);
1456 if(SUCCEEDED(hres)) {
1457 hres = VarXor(l.v, r.v, &v);
1458 release_val(&l);
1459 }
1460 release_val(&r);
1461 if(FAILED(hres))
1462 return hres;
1463
1464 return stack_push(ctx, &v);
1465 }
1466
1467 static HRESULT interp_eqv(exec_ctx_t *ctx)
1468 {
1469 variant_val_t r, l;
1470 VARIANT v;
1471 HRESULT hres;
1472
1473 TRACE("\n");
1474
1475 hres = stack_pop_val(ctx, &r);
1476 if(FAILED(hres))
1477 return hres;
1478
1479 hres = stack_pop_val(ctx, &l);
1480 if(SUCCEEDED(hres)) {
1481 hres = VarEqv(l.v, r.v, &v);
1482 release_val(&l);
1483 }
1484 release_val(&r);
1485 if(FAILED(hres))
1486 return hres;
1487
1488 return stack_push(ctx, &v);
1489 }
1490
1491 static HRESULT interp_imp(exec_ctx_t *ctx)
1492 {
1493 variant_val_t r, l;
1494 VARIANT v;
1495 HRESULT hres;
1496
1497 TRACE("\n");
1498
1499 hres = stack_pop_val(ctx, &r);
1500 if(FAILED(hres))
1501 return hres;
1502
1503 hres = stack_pop_val(ctx, &l);
1504 if(SUCCEEDED(hres)) {
1505 hres = VarImp(l.v, r.v, &v);
1506 release_val(&l);
1507 }
1508 release_val(&r);
1509 if(FAILED(hres))
1510 return hres;
1511
1512 return stack_push(ctx, &v);
1513 }
1514
1515 static HRESULT var_cmp(exec_ctx_t *ctx, VARIANT *l, VARIANT *r)
1516 {
1517 TRACE("%s %s\n", debugstr_variant(l), debugstr_variant(r));
1518
1519 /* FIXME: Fix comparing string to number */
1520
1521 return VarCmp(l, r, ctx->script->lcid, 0);
1522 }
1523
1524 static HRESULT cmp_oper(exec_ctx_t *ctx)
1525 {
1526 variant_val_t l, r;
1527 HRESULT hres;
1528
1529 hres = stack_pop_val(ctx, &r);
1530 if(FAILED(hres))
1531 return hres;
1532
1533 hres = stack_pop_val(ctx, &l);
1534 if(SUCCEEDED(hres)) {
1535 hres = var_cmp(ctx, l.v, r.v);
1536 release_val(&l);
1537 }
1538
1539 release_val(&r);
1540 return hres;
1541 }
1542
1543 static HRESULT interp_equal(exec_ctx_t *ctx)
1544 {
1545 VARIANT v;
1546 HRESULT hres;
1547
1548 TRACE("\n");
1549
1550 hres = cmp_oper(ctx);
1551 if(FAILED(hres))
1552 return hres;
1553 if(hres == VARCMP_NULL)
1554 return stack_push_null(ctx);
1555
1556 V_VT(&v) = VT_BOOL;
1557 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1558 return stack_push(ctx, &v);
1559 }
1560
1561 static HRESULT interp_nequal(exec_ctx_t *ctx)
1562 {
1563 VARIANT v;
1564 HRESULT hres;
1565
1566 TRACE("\n");
1567
1568 hres = cmp_oper(ctx);
1569 if(FAILED(hres))
1570 return hres;
1571 if(hres == VARCMP_NULL)
1572 return stack_push_null(ctx);
1573
1574 V_VT(&v) = VT_BOOL;
1575 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1576 return stack_push(ctx, &v);
1577 }
1578
1579 static HRESULT interp_gt(exec_ctx_t *ctx)
1580 {
1581 VARIANT v;
1582 HRESULT hres;
1583
1584 TRACE("\n");
1585
1586 hres = cmp_oper(ctx);
1587 if(FAILED(hres))
1588 return hres;
1589 if(hres == VARCMP_NULL)
1590 return stack_push_null(ctx);
1591
1592 V_VT(&v) = VT_BOOL;
1593 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1594 return stack_push(ctx, &v);
1595 }
1596
1597 static HRESULT interp_gteq(exec_ctx_t *ctx)
1598 {
1599 VARIANT v;
1600 HRESULT hres;
1601
1602 TRACE("\n");
1603
1604 hres = cmp_oper(ctx);
1605 if(FAILED(hres))
1606 return hres;
1607 if(hres == VARCMP_NULL)
1608 return stack_push_null(ctx);
1609
1610 V_VT(&v) = VT_BOOL;
1611 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1612 return stack_push(ctx, &v);
1613 }
1614
1615 static HRESULT interp_lt(exec_ctx_t *ctx)
1616 {
1617 VARIANT v;
1618 HRESULT hres;
1619
1620 TRACE("\n");
1621
1622 hres = cmp_oper(ctx);
1623 if(FAILED(hres))
1624 return hres;
1625 if(hres == VARCMP_NULL)
1626 return stack_push_null(ctx);
1627
1628 V_VT(&v) = VT_BOOL;
1629 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1630 return stack_push(ctx, &v);
1631 }
1632
1633 static HRESULT interp_lteq(exec_ctx_t *ctx)
1634 {
1635 VARIANT v;
1636 HRESULT hres;
1637
1638 TRACE("\n");
1639
1640 hres = cmp_oper(ctx);
1641 if(FAILED(hres))
1642 return hres;
1643 if(hres == VARCMP_NULL)
1644 return stack_push_null(ctx);
1645
1646 V_VT(&v) = VT_BOOL;
1647 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1648 return stack_push(ctx, &v);
1649 }
1650
1651 static HRESULT interp_case(exec_ctx_t *ctx)
1652 {
1653 const unsigned arg = ctx->instr->arg1.uint;
1654 variant_val_t v;
1655 HRESULT hres;
1656
1657 TRACE("%d\n", arg);
1658
1659 hres = stack_pop_val(ctx, &v);
1660 if(FAILED(hres))
1661 return hres;
1662
1663 hres = var_cmp(ctx, stack_top(ctx, 0), v.v);
1664 release_val(&v);
1665 if(FAILED(hres))
1666 return hres;
1667
1668 if(hres == VARCMP_EQ) {
1669 stack_popn(ctx, 1);
1670 instr_jmp(ctx, arg);
1671 }else {
1672 ctx->instr++;
1673 }
1674
1675 return S_OK;
1676 }
1677
1678 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1679 {
1680 IObjectIdentity *identity;
1681 IUnknown *unk1, *unk2;
1682 HRESULT hres;
1683
1684 if(disp1 == disp2) {
1685 *ret = VARIANT_TRUE;
1686 return S_OK;
1687 }
1688
1689 if(!disp1 || !disp2) {
1690 *ret = VARIANT_FALSE;
1691 return S_OK;
1692 }
1693
1694 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1695 if(FAILED(hres))
1696 return hres;
1697
1698 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1699 if(FAILED(hres)) {
1700 IUnknown_Release(unk1);
1701 return hres;
1702 }
1703
1704 if(unk1 == unk2) {
1705 *ret = VARIANT_TRUE;
1706 }else {
1707 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
1708 if(SUCCEEDED(hres)) {
1709 hres = IObjectIdentity_IsEqualObject(identity, unk2);
1710 IObjectIdentity_Release(identity);
1711 *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
1712 }else {
1713 *ret = VARIANT_FALSE;
1714 }
1715 }
1716
1717 IUnknown_Release(unk1);
1718 IUnknown_Release(unk2);
1719 return S_OK;
1720 }
1721
1722 static HRESULT interp_is(exec_ctx_t *ctx)
1723 {
1724 IDispatch *l, *r;
1725 VARIANT v;
1726 HRESULT hres;
1727
1728 TRACE("\n");
1729
1730 hres = stack_pop_disp(ctx, &r);
1731 if(FAILED(hres))
1732 return hres;
1733
1734 hres = stack_pop_disp(ctx, &l);
1735 if(SUCCEEDED(hres)) {
1736 V_VT(&v) = VT_BOOL;
1737 hres = disp_cmp(l, r, &V_BOOL(&v));
1738 if(l)
1739 IDispatch_Release(l);
1740 }
1741 if(r)
1742 IDispatch_Release(r);
1743 if(FAILED(hres))
1744 return hres;
1745
1746 return stack_push(ctx, &v);
1747 }
1748
1749 static HRESULT interp_concat(exec_ctx_t *ctx)
1750 {
1751 variant_val_t r, l;
1752 VARIANT v;
1753 HRESULT hres;
1754
1755 TRACE("\n");
1756
1757 hres = stack_pop_val(ctx, &r);
1758 if(FAILED(hres))
1759 return hres;
1760
1761 hres = stack_pop_val(ctx, &l);
1762 if(SUCCEEDED(hres)) {
1763 hres = VarCat(l.v, r.v, &v);
1764 release_val(&l);
1765 }
1766 release_val(&r);
1767 if(FAILED(hres))
1768 return hres;
1769
1770 return stack_push(ctx, &v);
1771 }
1772
1773 static HRESULT interp_add(exec_ctx_t *ctx)
1774 {
1775 variant_val_t r, l;
1776 VARIANT v;
1777 HRESULT hres;
1778
1779 TRACE("\n");
1780
1781 hres = stack_pop_val(ctx, &r);
1782 if(FAILED(hres))
1783 return hres;
1784
1785 hres = stack_pop_val(ctx, &l);
1786 if(SUCCEEDED(hres)) {
1787 hres = VarAdd(l.v, r.v, &v);
1788 release_val(&l);
1789 }
1790 release_val(&r);
1791 if(FAILED(hres))
1792 return hres;
1793
1794 return stack_push(ctx, &v);
1795 }
1796
1797 static HRESULT interp_sub(exec_ctx_t *ctx)
1798 {
1799 variant_val_t r, l;
1800 VARIANT v;
1801 HRESULT hres;
1802
1803 TRACE("\n");
1804
1805 hres = stack_pop_val(ctx, &r);
1806 if(FAILED(hres))
1807 return hres;
1808
1809 hres = stack_pop_val(ctx, &l);
1810 if(SUCCEEDED(hres)) {
1811 hres = VarSub(l.v, r.v, &v);
1812 release_val(&l);
1813 }
1814 release_val(&r);
1815 if(FAILED(hres))
1816 return hres;
1817
1818 return stack_push(ctx, &v);
1819 }
1820
1821 static HRESULT interp_mod(exec_ctx_t *ctx)
1822 {
1823 variant_val_t r, l;
1824 VARIANT v;
1825 HRESULT hres;
1826
1827 TRACE("\n");
1828
1829 hres = stack_pop_val(ctx, &r);
1830 if(FAILED(hres))
1831 return hres;
1832
1833 hres = stack_pop_val(ctx, &l);
1834 if(SUCCEEDED(hres)) {
1835 hres = VarMod(l.v, r.v, &v);
1836 release_val(&l);
1837 }
1838 release_val(&r);
1839 if(FAILED(hres))
1840 return hres;
1841
1842 return stack_push(ctx, &v);
1843 }
1844
1845 static HRESULT interp_idiv(exec_ctx_t *ctx)
1846 {
1847 variant_val_t r, l;
1848 VARIANT v;
1849 HRESULT hres;
1850
1851 TRACE("\n");
1852
1853 hres = stack_pop_val(ctx, &r);
1854 if(FAILED(hres))
1855 return hres;
1856
1857 hres = stack_pop_val(ctx, &l);
1858 if(SUCCEEDED(hres)) {
1859 hres = VarIdiv(l.v, r.v, &v);
1860 release_val(&l);
1861 }
1862 release_val(&r);
1863 if(FAILED(hres))
1864 return hres;
1865
1866 return stack_push(ctx, &v);
1867 }
1868
1869 static HRESULT interp_div(exec_ctx_t *ctx)
1870 {
1871 variant_val_t r, l;
1872 VARIANT v;
1873 HRESULT hres;
1874
1875 TRACE("\n");
1876
1877 hres = stack_pop_val(ctx, &r);
1878 if(FAILED(hres))
1879 return hres;
1880
1881 hres = stack_pop_val(ctx, &l);
1882 if(SUCCEEDED(hres)) {
1883 hres = VarDiv(l.v, r.v, &v);
1884 release_val(&l);
1885 }
1886 release_val(&r);
1887 if(FAILED(hres))
1888 return hres;
1889
1890 return stack_push(ctx, &v);
1891 }
1892
1893 static HRESULT interp_mul(exec_ctx_t *ctx)
1894 {
1895 variant_val_t r, l;
1896 VARIANT v;
1897 HRESULT hres;
1898
1899 TRACE("\n");
1900
1901 hres = stack_pop_val(ctx, &r);
1902 if(FAILED(hres))
1903 return hres;
1904
1905 hres = stack_pop_val(ctx, &l);
1906 if(SUCCEEDED(hres)) {
1907 hres = VarMul(l.v, r.v, &v);
1908 release_val(&l);
1909 }
1910 release_val(&r);
1911 if(FAILED(hres))
1912 return hres;
1913
1914 return stack_push(ctx, &v);
1915 }
1916
1917 static HRESULT interp_exp(exec_ctx_t *ctx)
1918 {
1919 variant_val_t r, l;
1920 VARIANT v;
1921 HRESULT hres;
1922
1923 TRACE("\n");
1924
1925 hres = stack_pop_val(ctx, &r);
1926 if(FAILED(hres))
1927 return hres;
1928
1929 hres = stack_pop_val(ctx, &l);
1930 if(SUCCEEDED(hres)) {
1931 hres = VarPow(l.v, r.v, &v);
1932 release_val(&l);
1933 }
1934 release_val(&r);
1935 if(FAILED(hres))
1936 return hres;
1937
1938 return stack_push(ctx, &v);
1939 }
1940
1941 static HRESULT interp_neg(exec_ctx_t *ctx)
1942 {
1943 variant_val_t val;
1944 VARIANT v;
1945 HRESULT hres;
1946
1947 hres = stack_pop_val(ctx, &val);
1948 if(FAILED(hres))
1949 return hres;
1950
1951 hres = VarNeg(val.v, &v);
1952 release_val(&val);
1953 if(FAILED(hres))
1954 return hres;
1955
1956 return stack_push(ctx, &v);
1957 }
1958
1959 static HRESULT interp_incc(exec_ctx_t *ctx)
1960 {
1961 const BSTR ident = ctx->instr->arg1.bstr;
1962 VARIANT v;
1963 ref_t ref;
1964 HRESULT hres;
1965
1966 TRACE("\n");
1967
1968 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1969 if(FAILED(hres))
1970 return hres;
1971
1972 if(ref.type != REF_VAR) {
1973 FIXME("ref.type is not REF_VAR\n");
1974 return E_FAIL;
1975 }
1976
1977 hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
1978 if(FAILED(hres))
1979 return hres;
1980
1981 VariantClear(ref.u.v);
1982 *ref.u.v = v;
1983 return S_OK;
1984 }
1985
1986 static HRESULT interp_catch(exec_ctx_t *ctx)
1987 {
1988 /* Nothing to do here, the OP is for unwinding only. */
1989 return S_OK;
1990 }
1991
1992 static const instr_func_t op_funcs[] = {
1993 #define X(x,n,a,b) interp_ ## x,
1994 OP_LIST
1995 #undef X
1996 };
1997
1998 static const unsigned op_move[] = {
1999 #define X(x,n,a,b) n,
2000 OP_LIST
2001 #undef X
2002 };
2003
2004 void release_dynamic_vars(dynamic_var_t *var)
2005 {
2006 while(var) {
2007 VariantClear(&var->v);
2008 var = var->next;
2009 }
2010 }
2011
2012 static void release_exec(exec_ctx_t *ctx)
2013 {
2014 unsigned i;
2015
2016 VariantClear(&ctx->ret_val);
2017 release_dynamic_vars(ctx->dynamic_vars);
2018
2019 if(ctx->this_obj)
2020 IDispatch_Release(ctx->this_obj);
2021
2022 if(ctx->args) {
2023 for(i=0; i < ctx->func->arg_cnt; i++)
2024 VariantClear(ctx->args+i);
2025 }
2026
2027 if(ctx->vars) {
2028 for(i=0; i < ctx->func->var_cnt; i++)
2029 VariantClear(ctx->vars+i);
2030 }
2031
2032 if(ctx->arrays) {
2033 for(i=0; i < ctx->func->var_cnt; i++) {
2034 if(ctx->arrays[i])
2035 SafeArrayDestroy(ctx->arrays[i]);
2036 }
2037 heap_free(ctx->arrays);
2038 }
2039
2040 heap_pool_free(&ctx->heap);
2041 heap_free(ctx->args);
2042 heap_free(ctx->vars);
2043 heap_free(ctx->stack);
2044 }
2045
2046 HRESULT exec_script(script_ctx_t *ctx, function_t *func, vbdisp_t *vbthis, DISPPARAMS *dp, VARIANT *res)
2047 {
2048 exec_ctx_t exec = {func->code_ctx};
2049 vbsop_t op;
2050 HRESULT hres = S_OK;
2051
2052 exec.code = func->code_ctx;
2053
2054 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
2055 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
2056 return E_FAIL;
2057 }
2058
2059 heap_pool_init(&exec.heap);
2060
2061 if(func->arg_cnt) {
2062 VARIANT *v;
2063 unsigned i;
2064
2065 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
2066 if(!exec.args) {
2067 release_exec(&exec);
2068 return E_OUTOFMEMORY;
2069 }
2070
2071 for(i=0; i < func->arg_cnt; i++) {
2072 v = get_arg(dp, i);
2073 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
2074 if(func->args[i].by_ref)
2075 exec.args[i] = *v;
2076 else
2077 hres = VariantCopyInd(exec.args+i, V_VARIANTREF(v));
2078 }else {
2079 hres = VariantCopyInd(exec.args+i, v);
2080 }
2081 if(FAILED(hres)) {
2082 release_exec(&exec);
2083 return hres;
2084 }
2085 }
2086 }else {
2087 exec.args = NULL;
2088 }
2089
2090 if(func->var_cnt) {
2091 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
2092 if(!exec.vars) {
2093 release_exec(&exec);
2094 return E_OUTOFMEMORY;
2095 }
2096 }else {
2097 exec.vars = NULL;
2098 }
2099
2100 exec.stack_size = 16;
2101 exec.top = 0;
2102 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
2103 if(!exec.stack) {
2104 release_exec(&exec);
2105 return E_OUTOFMEMORY;
2106 }
2107
2108 if(vbthis) {
2109 exec.this_obj = (IDispatch*)&vbthis->IDispatchEx_iface;
2110 exec.vbthis = vbthis;
2111 }else if (ctx->host_global) {
2112 exec.this_obj = ctx->host_global;
2113 }else {
2114 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
2115 }
2116 IDispatch_AddRef(exec.this_obj);
2117
2118 exec.instr = exec.code->instrs + func->code_off;
2119 exec.script = ctx;
2120 exec.func = func;
2121
2122 while(exec.instr) {
2123 op = exec.instr->op;
2124 hres = op_funcs[op](&exec);
2125 if(FAILED(hres)) {
2126 ctx->err_number = hres = map_hres(hres);
2127
2128 if(exec.resume_next) {
2129 unsigned stack_off;
2130
2131 WARN("Failed %08x in resume next mode\n", hres);
2132
2133 /*
2134 * Unwinding here is simple. We need to find the next OP_catch, which contains
2135 * information about expected stack size and jump offset on error. Generated
2136 * bytecode needs to guarantee, that simple jump and stack adjustment will
2137 * guarantee proper execution continuation.
2138 */
2139 while((++exec.instr)->op != OP_catch);
2140
2141 TRACE("unwind jmp %d stack_off %d\n", exec.instr->arg1.uint, exec.instr->arg2.uint);
2142
2143 stack_off = exec.instr->arg2.uint;
2144 instr_jmp(&exec, exec.instr->arg1.uint);
2145
2146 if(exec.top > stack_off) {
2147 stack_popn(&exec, exec.top-stack_off);
2148 }else if(exec.top < stack_off) {
2149 VARIANT v;
2150
2151 V_VT(&v) = VT_EMPTY;
2152 while(exec.top < stack_off) {
2153 hres = stack_push(&exec, &v);
2154 if(FAILED(hres))
2155 break;
2156 }
2157 }
2158
2159 continue;
2160 }else {
2161 WARN("Failed %08x\n", hres);
2162 stack_popn(&exec, exec.top);
2163 break;
2164 }
2165 }
2166
2167 exec.instr += op_move[op];
2168 }
2169
2170 assert(!exec.top);
2171 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
2172 assert(V_VT(&exec.ret_val) == VT_EMPTY);
2173
2174 if(SUCCEEDED(hres) && res) {
2175 *res = exec.ret_val;
2176 V_VT(&exec.ret_val) = VT_EMPTY;
2177 }
2178
2179 release_exec(&exec);
2180 return hres;
2181 }