Release 3.0.2.
[wine.git] / dlls / vbscript / global.c
bloba5df54acad503a92acab740a8ea5de7b9009a718
1 /*
2 * Copyright 2011 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <assert.h>
20 #include <math.h>
22 #include "vbscript.h"
23 #include "vbscript_defs.h"
25 #include "mshtmhst.h"
26 #include "objsafe.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
32 #define VB_E_CANNOT_CREATE_OBJ 0x800a01ad
33 #define VB_E_MK_PARSE_ERROR 0x800a01b0
35 /* Defined as extern in urlmon.idl, but not exported by uuid.lib */
36 const GUID GUID_CUSTOM_CONFIRMOBJECTSAFETY =
37 {0x10200490,0xfa38,0x11d0,{0xac,0x0e,0x00,0xa0,0xc9,0xf,0xff,0xc0}};
39 static const WCHAR emptyW[] = {0};
40 static const WCHAR vbscriptW[] = {'V','B','S','c','r','i','p','t',0};
42 static IInternetHostSecurityManager *get_sec_mgr(script_ctx_t *ctx)
44 IInternetHostSecurityManager *secmgr;
45 IServiceProvider *sp;
46 HRESULT hres;
48 if(!ctx->site)
49 return NULL;
51 if(ctx->secmgr)
52 return ctx->secmgr;
54 hres = IActiveScriptSite_QueryInterface(ctx->site, &IID_IServiceProvider, (void**)&sp);
55 if(FAILED(hres))
56 return NULL;
58 hres = IServiceProvider_QueryService(sp, &SID_SInternetHostSecurityManager, &IID_IInternetHostSecurityManager,
59 (void**)&secmgr);
60 IServiceProvider_Release(sp);
61 if(FAILED(hres))
62 return NULL;
64 return ctx->secmgr = secmgr;
67 static HRESULT return_string(VARIANT *res, const WCHAR *str)
69 BSTR ret;
71 if(!res)
72 return S_OK;
74 ret = SysAllocString(str);
75 if(!ret)
76 return E_OUTOFMEMORY;
78 V_VT(res) = VT_BSTR;
79 V_BSTR(res) = ret;
80 return S_OK;
83 static HRESULT return_bstr(VARIANT *res, BSTR str)
85 if(res) {
86 V_VT(res) = VT_BSTR;
87 V_BSTR(res) = str;
88 }else {
89 SysFreeString(str);
91 return S_OK;
94 static HRESULT return_bool(VARIANT *res, BOOL val)
96 if(res) {
97 V_VT(res) = VT_BOOL;
98 V_BOOL(res) = val ? VARIANT_TRUE : VARIANT_FALSE;
100 return S_OK;
103 static HRESULT return_short(VARIANT *res, short val)
105 if(res) {
106 V_VT(res) = VT_I2;
107 V_I2(res) = val;
110 return S_OK;
113 static HRESULT return_int(VARIANT *res, int val)
115 if(res) {
116 V_VT(res) = VT_I4;
117 V_I4(res) = val;
120 return S_OK;
123 static inline HRESULT return_double(VARIANT *res, double val)
125 if(res) {
126 V_VT(res) = VT_R8;
127 V_R8(res) = val;
130 return S_OK;
133 static inline HRESULT return_float(VARIANT *res, float val)
135 if(res) {
136 V_VT(res) = VT_R4;
137 V_R4(res) = val;
140 return S_OK;
143 static inline HRESULT return_null(VARIANT *res)
145 if(res)
146 V_VT(res) = VT_NULL;
147 return S_OK;
150 static inline HRESULT return_date(VARIANT *res, double date)
152 if(res) {
153 V_VT(res) = VT_DATE;
154 V_DATE(res) = date;
156 return S_OK;
159 HRESULT to_int(VARIANT *v, int *ret)
161 VARIANT r;
162 HRESULT hres;
164 V_VT(&r) = VT_EMPTY;
165 hres = VariantChangeType(&r, v, 0, VT_I4);
166 if(FAILED(hres))
167 return hres;
169 *ret = V_I4(&r);
170 return S_OK;
173 static HRESULT to_double(VARIANT *v, double *ret)
175 VARIANT dst;
176 HRESULT hres;
178 V_VT(&dst) = VT_EMPTY;
179 hres = VariantChangeType(&dst, v, 0, VT_R8);
180 if(FAILED(hres))
181 return hres;
183 *ret = V_R8(&dst);
184 return S_OK;
187 static HRESULT to_string(VARIANT *v, BSTR *ret)
189 VARIANT dst;
190 HRESULT hres;
192 V_VT(&dst) = VT_EMPTY;
193 hres = VariantChangeType(&dst, v, VARIANT_LOCALBOOL, VT_BSTR);
194 if(FAILED(hres))
195 return hres;
197 *ret = V_BSTR(&dst);
198 return S_OK;
201 static HRESULT set_object_site(script_ctx_t *ctx, IUnknown *obj)
203 IObjectWithSite *obj_site;
204 IUnknown *ax_site;
205 HRESULT hres;
207 hres = IUnknown_QueryInterface(obj, &IID_IObjectWithSite, (void**)&obj_site);
208 if(FAILED(hres))
209 return S_OK;
211 ax_site = create_ax_site(ctx);
212 if(ax_site) {
213 hres = IObjectWithSite_SetSite(obj_site, ax_site);
214 IUnknown_Release(ax_site);
216 else
217 hres = E_OUTOFMEMORY;
218 IObjectWithSite_Release(obj_site);
219 return hres;
222 static IUnknown *create_object(script_ctx_t *ctx, const WCHAR *progid)
224 IInternetHostSecurityManager *secmgr = NULL;
225 struct CONFIRMSAFETY cs;
226 IClassFactoryEx *cfex;
227 IClassFactory *cf;
228 DWORD policy_size;
229 BYTE *bpolicy;
230 IUnknown *obj;
231 DWORD policy;
232 GUID guid;
233 HRESULT hres;
235 hres = CLSIDFromProgID(progid, &guid);
236 if(FAILED(hres))
237 return NULL;
239 TRACE("GUID %s\n", debugstr_guid(&guid));
241 if(ctx->safeopt & INTERFACE_USES_SECURITY_MANAGER) {
242 secmgr = get_sec_mgr(ctx);
243 if(!secmgr)
244 return NULL;
246 policy = 0;
247 hres = IInternetHostSecurityManager_ProcessUrlAction(secmgr, URLACTION_ACTIVEX_RUN,
248 (BYTE*)&policy, sizeof(policy), (BYTE*)&guid, sizeof(GUID), 0, 0);
249 if(FAILED(hres) || policy != URLPOLICY_ALLOW)
250 return NULL;
253 hres = CoGetClassObject(&guid, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, NULL, &IID_IClassFactory, (void**)&cf);
254 if(FAILED(hres))
255 return NULL;
257 hres = IClassFactory_QueryInterface(cf, &IID_IClassFactoryEx, (void**)&cfex);
258 if(SUCCEEDED(hres)) {
259 FIXME("Use IClassFactoryEx\n");
260 IClassFactoryEx_Release(cfex);
263 hres = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (void**)&obj);
264 if(FAILED(hres))
265 return NULL;
267 if(secmgr) {
268 cs.clsid = guid;
269 cs.pUnk = obj;
270 cs.dwFlags = 0;
271 hres = IInternetHostSecurityManager_QueryCustomPolicy(secmgr, &GUID_CUSTOM_CONFIRMOBJECTSAFETY,
272 &bpolicy, &policy_size, (BYTE*)&cs, sizeof(cs), 0);
273 if(SUCCEEDED(hres)) {
274 policy = policy_size >= sizeof(DWORD) ? *(DWORD*)bpolicy : URLPOLICY_DISALLOW;
275 CoTaskMemFree(bpolicy);
278 if(FAILED(hres) || policy != URLPOLICY_ALLOW) {
279 IUnknown_Release(obj);
280 return NULL;
284 hres = set_object_site(ctx, obj);
285 if(FAILED(hres)) {
286 IUnknown_Release(obj);
287 return NULL;
290 return obj;
293 static HRESULT show_msgbox(script_ctx_t *ctx, BSTR prompt, unsigned type, BSTR orig_title, VARIANT *res)
295 SCRIPTUICHANDLING uic_handling = SCRIPTUICHANDLING_ALLOW;
296 IActiveScriptSiteUIControl *ui_control;
297 IActiveScriptSiteWindow *acts_window;
298 WCHAR *title_buf = NULL;
299 const WCHAR *title;
300 HWND hwnd = NULL;
301 int ret = 0;
302 HRESULT hres;
304 hres = IActiveScriptSite_QueryInterface(ctx->site, &IID_IActiveScriptSiteUIControl, (void**)&ui_control);
305 if(SUCCEEDED(hres)) {
306 hres = IActiveScriptSiteUIControl_GetUIBehavior(ui_control, SCRIPTUICITEM_MSGBOX, &uic_handling);
307 IActiveScriptSiteUIControl_Release(ui_control);
308 if(FAILED(hres))
309 uic_handling = SCRIPTUICHANDLING_ALLOW;
312 switch(uic_handling) {
313 case SCRIPTUICHANDLING_ALLOW:
314 break;
315 case SCRIPTUICHANDLING_NOUIDEFAULT:
316 return return_short(res, 0);
317 default:
318 FIXME("blocked\n");
319 return E_FAIL;
322 hres = IActiveScriptSite_QueryInterface(ctx->site, &IID_IActiveScriptSiteWindow, (void**)&acts_window);
323 if(FAILED(hres)) {
324 FIXME("No IActiveScriptSiteWindow\n");
325 return hres;
328 if(ctx->safeopt & INTERFACE_USES_SECURITY_MANAGER) {
329 if(orig_title && *orig_title) {
330 WCHAR *ptr;
332 title = title_buf = heap_alloc(sizeof(vbscriptW) + (strlenW(orig_title)+2)*sizeof(WCHAR));
333 if(!title)
334 return E_OUTOFMEMORY;
336 memcpy(title_buf, vbscriptW, sizeof(vbscriptW));
337 ptr = title_buf + sizeof(vbscriptW)/sizeof(WCHAR)-1;
339 *ptr++ = ':';
340 *ptr++ = ' ';
341 strcpyW(ptr, orig_title);
342 }else {
343 title = vbscriptW;
345 }else {
346 title = orig_title ? orig_title : emptyW;
349 hres = IActiveScriptSiteWindow_GetWindow(acts_window, &hwnd);
350 if(SUCCEEDED(hres)) {
351 hres = IActiveScriptSiteWindow_EnableModeless(acts_window, FALSE);
352 if(SUCCEEDED(hres)) {
353 ret = MessageBoxW(hwnd, prompt, title, type);
354 hres = IActiveScriptSiteWindow_EnableModeless(acts_window, TRUE);
358 heap_free(title_buf);
359 IActiveScriptSiteWindow_Release(acts_window);
360 if(FAILED(hres)) {
361 FIXME("failed: %08x\n", hres);
362 return hres;
365 return return_short(res, ret);
368 static HRESULT Global_CCur(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
370 VARIANT v;
371 HRESULT hres;
373 TRACE("%s\n", debugstr_variant(arg));
375 assert(args_cnt == 1);
377 V_VT(&v) = VT_EMPTY;
378 hres = VariantChangeType(&v, arg, 0, VT_CY);
379 if(FAILED(hres))
380 return hres;
382 if(!res) {
383 VariantClear(&v);
384 return DISP_E_BADVARTYPE;
387 *res = v;
388 return S_OK;
391 static HRESULT Global_CInt(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
393 VARIANT v;
394 HRESULT hres;
396 TRACE("%s\n", debugstr_variant(arg));
398 assert(args_cnt == 1);
400 V_VT(&v) = VT_EMPTY;
401 hres = VariantChangeType(&v, arg, 0, VT_I2);
402 if(FAILED(hres))
403 return hres;
405 if(!res)
406 return DISP_E_BADVARTYPE;
407 else {
408 *res = v;
409 return S_OK;
413 static HRESULT Global_CLng(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
415 int i;
416 HRESULT hres;
418 TRACE("%s\n", debugstr_variant(arg));
420 assert(args_cnt == 1);
422 hres = to_int(arg, &i);
423 if(FAILED(hres))
424 return hres;
425 if(!res)
426 return DISP_E_BADVARTYPE;
428 return return_int(res, i);
431 static HRESULT Global_CBool(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
433 VARIANT v;
434 HRESULT hres;
436 TRACE("%s\n", debugstr_variant(arg));
438 assert(args_cnt == 1);
440 V_VT(&v) = VT_EMPTY;
441 hres = VariantChangeType(&v, arg, VARIANT_LOCALBOOL, VT_BOOL);
442 if(FAILED(hres))
443 return hres;
445 if(res)
446 *res = v;
447 else
448 VariantClear(&v);
449 return S_OK;
452 static HRESULT Global_CByte(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
454 VARIANT v;
455 HRESULT hres;
457 TRACE("%s\n", debugstr_variant(arg));
459 assert(args_cnt == 1);
461 V_VT(&v) = VT_EMPTY;
462 hres = VariantChangeType(&v, arg, VARIANT_LOCALBOOL, VT_UI1);
463 if(FAILED(hres))
464 return hres;
466 if(!res) {
467 VariantClear(&v);
468 return DISP_E_BADVARTYPE;
471 *res = v;
472 return S_OK;
475 static HRESULT Global_CDate(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
477 FIXME("\n");
478 return E_NOTIMPL;
481 static HRESULT Global_CDbl(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
483 VARIANT v;
484 HRESULT hres;
486 TRACE("%s\n", debugstr_variant(arg));
488 assert(args_cnt == 1);
490 V_VT(&v) = VT_EMPTY;
491 hres = VariantChangeType(&v, arg, 0, VT_R8);
492 if(FAILED(hres))
493 return hres;
495 if(!res)
496 return DISP_E_BADVARTYPE;
497 else {
498 *res = v;
499 return S_OK;
503 static HRESULT Global_CSng(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
505 VARIANT v;
506 HRESULT hres;
508 TRACE("%s\n", debugstr_variant(arg));
510 assert(args_cnt == 1);
512 V_VT(&v) = VT_EMPTY;
513 hres = VariantChangeType(&v, arg, 0, VT_R4);
514 if(FAILED(hres))
515 return hres;
517 if(!res)
518 return DISP_E_BADVARTYPE;
520 *res = v;
521 return S_OK;
524 static HRESULT Global_CStr(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
526 BSTR str;
527 HRESULT hres;
529 TRACE("%s\n", debugstr_variant(arg));
531 hres = to_string(arg, &str);
532 if(FAILED(hres))
533 return hres;
535 return return_bstr(res, str);
538 static inline WCHAR hex_char(unsigned n)
540 return n < 10 ? '0'+n : 'A'+n-10;
543 static HRESULT Global_Hex(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
545 WCHAR buf[17], *ptr;
546 DWORD n;
547 HRESULT hres;
548 int ret;
550 TRACE("%s\n", debugstr_variant(arg));
552 switch(V_VT(arg)) {
553 case VT_I2:
554 n = (WORD)V_I2(arg);
555 break;
556 case VT_NULL:
557 if(res)
558 V_VT(res) = VT_NULL;
559 return S_OK;
560 default:
561 hres = to_int(arg, &ret);
562 if(FAILED(hres))
563 return hres;
564 else
565 n = ret;
568 buf[16] = 0;
569 ptr = buf+15;
571 if(n) {
572 do {
573 *ptr-- = hex_char(n & 0xf);
574 n >>= 4;
575 }while(n);
576 ptr++;
577 }else {
578 *ptr = '0';
581 return return_string(res, ptr);
584 static HRESULT Global_Oct(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
586 HRESULT hres;
587 WCHAR buf[23], *ptr;
588 DWORD n;
589 int ret;
591 TRACE("%s\n", debugstr_variant(arg));
593 switch(V_VT(arg)) {
594 case VT_I2:
595 n = (WORD)V_I2(arg);
596 break;
597 case VT_NULL:
598 if(res)
599 V_VT(res) = VT_NULL;
600 return S_OK;
601 default:
602 hres = to_int(arg, &ret);
603 if(FAILED(hres))
604 return hres;
605 else
606 n = ret;
609 buf[22] = 0;
610 ptr = buf + 21;
612 if(n) {
613 do {
614 *ptr-- = '0' + (n & 0x7);
615 n >>= 3;
616 }while(n);
617 ptr++;
618 }else {
619 *ptr = '0';
622 return return_string(res, ptr);
625 static HRESULT Global_VarType(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
627 TRACE("(%s)\n", debugstr_variant(arg));
629 assert(args_cnt == 1);
631 if(V_VT(arg) & ~VT_TYPEMASK) {
632 FIXME("not supported %s\n", debugstr_variant(arg));
633 return E_NOTIMPL;
636 return return_short(res, V_VT(arg));
639 static HRESULT Global_IsDate(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
641 FIXME("\n");
642 return E_NOTIMPL;
645 static HRESULT Global_IsEmpty(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
647 TRACE("(%s)\n", debugstr_variant(arg));
649 assert(args_cnt == 1);
651 if(res) {
652 V_VT(res) = VT_BOOL;
653 V_BOOL(res) = V_VT(arg) == VT_EMPTY ? VARIANT_TRUE : VARIANT_FALSE;
655 return S_OK;
658 static HRESULT Global_IsNull(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
660 TRACE("(%s)\n", debugstr_variant(arg));
662 assert(args_cnt == 1);
664 if(res) {
665 V_VT(res) = VT_BOOL;
666 V_BOOL(res) = V_VT(arg) == VT_NULL ? VARIANT_TRUE : VARIANT_FALSE;
668 return S_OK;
671 static HRESULT Global_IsNumeric(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
673 HRESULT hres;
674 double d;
676 TRACE("(%s)\n", debugstr_variant(arg));
678 assert(args_cnt == 1);
680 hres = to_double(arg, &d);
682 return return_bool(res, SUCCEEDED(hres));
685 static HRESULT Global_IsArray(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
687 FIXME("\n");
688 return E_NOTIMPL;
691 static HRESULT Global_IsObject(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
693 TRACE("(%s)\n", debugstr_variant(arg));
695 assert(args_cnt == 1);
697 if(res) {
698 V_VT(res) = VT_BOOL;
699 V_BOOL(res) = V_VT(arg) == VT_DISPATCH ? VARIANT_TRUE : VARIANT_FALSE;
701 return S_OK;
704 static HRESULT Global_Atn(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
706 HRESULT hres;
707 double d;
709 hres = to_double(arg, &d);
710 if(FAILED(hres))
711 return hres;
713 return return_double(res, atan(d));
716 static HRESULT Global_Cos(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
718 HRESULT hres;
719 double d;
721 hres = to_double(arg, &d);
722 if(FAILED(hres))
723 return hres;
725 return return_double(res, cos(d));
728 static HRESULT Global_Sin(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
730 HRESULT hres;
731 double d;
733 hres = to_double(arg, &d);
734 if(FAILED(hres))
735 return hres;
737 return return_double(res, sin(d));
740 static HRESULT Global_Tan(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
742 HRESULT hres;
743 double d;
745 hres = to_double(arg, &d);
746 if(FAILED(hres))
747 return hres;
749 return return_double(res, tan(d));
752 static HRESULT Global_Exp(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
754 HRESULT hres;
755 double d;
757 hres = to_double(arg, &d);
758 if(FAILED(hres))
759 return hres;
761 return return_double(res, exp(d));
764 static HRESULT Global_Log(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
766 HRESULT hres;
767 double d;
769 hres = to_double(arg, &d);
770 if(FAILED(hres))
771 return hres;
773 if(d <= 0)
774 return MAKE_VBSERROR(VBSE_ILLEGAL_FUNC_CALL);
775 else
776 return return_double(res, log(d));
779 static HRESULT Global_Sqr(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
781 HRESULT hres;
782 double d;
784 hres = to_double(arg, &d);
785 if(FAILED(hres))
786 return hres;
788 if(d < 0)
789 return MAKE_VBSERROR(VBSE_ILLEGAL_FUNC_CALL);
790 else
791 return return_double(res, sqrt(d));
794 static HRESULT Global_Randomize(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
796 FIXME("\n");
797 return E_NOTIMPL;
800 static HRESULT Global_Rnd(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
802 FIXME("\n");
803 return E_NOTIMPL;
806 static HRESULT Global_Timer(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
808 SYSTEMTIME lt;
809 double sec;
811 GetLocalTime(&lt);
812 sec = lt.wHour * 3600 + lt.wMinute * 60 + lt.wSecond + lt.wMilliseconds / 1000.0;
813 return return_float(res, sec);
817 static HRESULT Global_LBound(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
819 FIXME("\n");
820 return E_NOTIMPL;
823 static HRESULT Global_UBound(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
825 SAFEARRAY *sa;
826 HRESULT hres;
827 LONG ubound;
828 int dim;
830 assert(args_cnt == 1 || args_cnt == 2);
832 TRACE("%s %s\n", debugstr_variant(arg), args_cnt == 2 ? debugstr_variant(arg + 1) : "1");
834 switch(V_VT(arg)) {
835 case VT_VARIANT|VT_ARRAY:
836 sa = V_ARRAY(arg);
837 break;
838 case VT_VARIANT|VT_ARRAY|VT_BYREF:
839 sa = *V_ARRAYREF(arg);
840 break;
841 default:
842 FIXME("arg %s not supported\n", debugstr_variant(arg));
843 return E_NOTIMPL;
846 if(args_cnt == 2) {
847 hres = to_int(arg + 1, &dim);
848 if(FAILED(hres))
849 return hres;
850 }else {
851 dim = 1;
854 hres = SafeArrayGetUBound(sa, dim, &ubound);
855 if(FAILED(hres))
856 return hres;
858 return return_int(res, ubound);
861 static HRESULT Global_RGB(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
863 HRESULT hres;
864 int i, color[3];
866 TRACE("%s %s %s\n", debugstr_variant(arg), debugstr_variant(arg + 1), debugstr_variant(arg + 2));
868 assert(args_cnt == 3);
870 for(i = 0; i < 3; i++) {
871 hres = to_int(arg + i, color + i);
872 if(FAILED(hres))
873 return hres;
874 if(color[i] > 255)
875 color[i] = 255;
876 if(color[i] < 0)
877 return MAKE_VBSERROR(VBSE_ILLEGAL_FUNC_CALL);
880 return return_int(res, RGB(color[0], color[1], color[2]));
883 static HRESULT Global_Len(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
885 DWORD len;
886 HRESULT hres;
888 TRACE("%s\n", debugstr_variant(arg));
890 if(V_VT(arg) == VT_NULL)
891 return return_null(res);
893 if(V_VT(arg) != VT_BSTR) {
894 BSTR str;
896 hres = to_string(arg, &str);
897 if(FAILED(hres))
898 return hres;
900 len = SysStringLen(str);
901 SysFreeString(str);
902 }else {
903 len = SysStringLen(V_BSTR(arg));
906 return return_int(res, len);
909 static HRESULT Global_LenB(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
911 FIXME("\n");
912 return E_NOTIMPL;
915 static HRESULT Global_Left(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
917 BSTR str, ret, conv_str = NULL;
918 int len, str_len;
919 HRESULT hres;
921 TRACE("(%s %s)\n", debugstr_variant(args+1), debugstr_variant(args));
923 if(V_VT(args) == VT_BSTR) {
924 str = V_BSTR(args);
925 }else {
926 hres = to_string(args, &conv_str);
927 if(FAILED(hres))
928 return hres;
929 str = conv_str;
932 hres = to_int(args+1, &len);
933 if(FAILED(hres))
934 return hres;
936 if(len < 0) {
937 FIXME("len = %d\n", len);
938 return E_FAIL;
941 str_len = SysStringLen(str);
942 if(len > str_len)
943 len = str_len;
945 ret = SysAllocStringLen(str, len);
946 SysFreeString(conv_str);
947 if(!ret)
948 return E_OUTOFMEMORY;
950 return return_bstr(res, ret);
953 static HRESULT Global_LeftB(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
955 FIXME("\n");
956 return E_NOTIMPL;
959 static HRESULT Global_Right(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
961 BSTR str, ret, conv_str = NULL;
962 int len, str_len;
963 HRESULT hres;
965 TRACE("(%s %s)\n", debugstr_variant(args), debugstr_variant(args+1));
967 if(V_VT(args+1) == VT_BSTR) {
968 str = V_BSTR(args);
969 }else {
970 hres = to_string(args, &conv_str);
971 if(FAILED(hres))
972 return hres;
973 str = conv_str;
976 hres = to_int(args+1, &len);
977 if(FAILED(hres))
978 return hres;
980 if(len < 0) {
981 FIXME("len = %d\n", len);
982 return E_FAIL;
985 str_len = SysStringLen(str);
986 if(len > str_len)
987 len = str_len;
989 ret = SysAllocStringLen(str+str_len-len, len);
990 SysFreeString(conv_str);
991 if(!ret)
992 return E_OUTOFMEMORY;
994 return return_bstr(res, ret);
997 static HRESULT Global_RightB(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
999 FIXME("\n");
1000 return E_NOTIMPL;
1003 static HRESULT Global_Mid(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
1005 int len = -1, start, str_len;
1006 BSTR str;
1007 HRESULT hres;
1009 TRACE("(%s %s ...)\n", debugstr_variant(args), debugstr_variant(args+1));
1011 assert(args_cnt == 2 || args_cnt == 3);
1013 if(V_VT(args) != VT_BSTR) {
1014 FIXME("args[0] = %s\n", debugstr_variant(args));
1015 return E_NOTIMPL;
1018 str = V_BSTR(args);
1020 hres = to_int(args+1, &start);
1021 if(FAILED(hres))
1022 return hres;
1024 if(args_cnt == 3) {
1025 hres = to_int(args+2, &len);
1026 if(FAILED(hres))
1027 return hres;
1029 if(len < 0) {
1030 FIXME("len = %d\n", len);
1031 return E_FAIL;
1036 str_len = SysStringLen(str);
1037 start--;
1038 if(start > str_len)
1039 start = str_len;
1041 if(len == -1)
1042 len = str_len-start;
1043 else if(len > str_len-start)
1044 len = str_len-start;
1046 if(res) {
1047 V_VT(res) = VT_BSTR;
1048 V_BSTR(res) = SysAllocStringLen(str+start, len);
1049 if(!V_BSTR(res))
1050 return E_OUTOFMEMORY;
1053 return S_OK;
1056 static HRESULT Global_MidB(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1058 FIXME("\n");
1059 return E_NOTIMPL;
1062 static HRESULT Global_StrComp(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
1064 BSTR left, right;
1065 int mode, ret;
1066 HRESULT hres;
1067 short val;
1069 TRACE("(%s %s ...)\n", debugstr_variant(args), debugstr_variant(args+1));
1071 assert(args_cnt == 2 || args_cnt == 3);
1073 if (args_cnt == 3) {
1074 hres = to_int(args+2, &mode);
1075 if(FAILED(hres))
1076 return hres;
1078 if (mode != 0 && mode != 1) {
1079 FIXME("unknown compare mode = %d\n", mode);
1080 return E_FAIL;
1083 else
1084 mode = 0;
1086 hres = to_string(args, &left);
1087 if(FAILED(hres))
1088 return hres;
1090 hres = to_string(args+1, &right);
1091 if(FAILED(hres))
1093 SysFreeString(left);
1094 return hres;
1097 ret = mode ? strcmpiW(left, right) : strcmpW(left, right);
1098 val = ret < 0 ? -1 : (ret > 0 ? 1 : 0);
1100 SysFreeString(left);
1101 SysFreeString(right);
1102 return return_short(res, val);
1105 static HRESULT Global_LCase(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1107 BSTR str;
1108 HRESULT hres;
1110 TRACE("%s\n", debugstr_variant(arg));
1112 if(V_VT(arg) == VT_NULL) {
1113 if(res)
1114 V_VT(res) = VT_NULL;
1115 return S_OK;
1118 hres = to_string(arg, &str);
1119 if(FAILED(hres))
1120 return hres;
1122 if(res) {
1123 WCHAR *ptr;
1125 for(ptr = str; *ptr; ptr++)
1126 *ptr = tolowerW(*ptr);
1128 V_VT(res) = VT_BSTR;
1129 V_BSTR(res) = str;
1130 }else {
1131 SysFreeString(str);
1133 return S_OK;
1136 static HRESULT Global_UCase(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1138 BSTR str;
1139 HRESULT hres;
1141 TRACE("%s\n", debugstr_variant(arg));
1143 if(V_VT(arg) == VT_NULL) {
1144 if(res)
1145 V_VT(res) = VT_NULL;
1146 return S_OK;
1149 hres = to_string(arg, &str);
1150 if(FAILED(hres))
1151 return hres;
1153 if(res) {
1154 WCHAR *ptr;
1156 for(ptr = str; *ptr; ptr++)
1157 *ptr = toupperW(*ptr);
1159 V_VT(res) = VT_BSTR;
1160 V_BSTR(res) = str;
1161 }else {
1162 SysFreeString(str);
1164 return S_OK;
1167 static HRESULT Global_LTrim(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1169 BSTR str, conv_str = NULL;
1170 WCHAR *ptr;
1171 HRESULT hres;
1173 TRACE("%s\n", debugstr_variant(arg));
1175 if(V_VT(arg) == VT_BSTR) {
1176 str = V_BSTR(arg);
1177 }else {
1178 hres = to_string(arg, &conv_str);
1179 if(FAILED(hres))
1180 return hres;
1181 str = conv_str;
1184 for(ptr = str; *ptr && isspaceW(*ptr); ptr++);
1186 str = SysAllocString(ptr);
1187 SysFreeString(conv_str);
1188 if(!str)
1189 return E_OUTOFMEMORY;
1191 return return_bstr(res, str);
1194 static HRESULT Global_RTrim(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1196 BSTR str, conv_str = NULL;
1197 WCHAR *ptr;
1198 HRESULT hres;
1200 TRACE("%s\n", debugstr_variant(arg));
1202 if(V_VT(arg) == VT_BSTR) {
1203 str = V_BSTR(arg);
1204 }else {
1205 hres = to_string(arg, &conv_str);
1206 if(FAILED(hres))
1207 return hres;
1208 str = conv_str;
1211 for(ptr = str+SysStringLen(str); ptr-1 > str && isspaceW(*(ptr-1)); ptr--);
1213 str = SysAllocStringLen(str, ptr-str);
1214 SysFreeString(conv_str);
1215 if(!str)
1216 return E_OUTOFMEMORY;
1218 return return_bstr(res, str);
1221 static HRESULT Global_Trim(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1223 BSTR str, conv_str = NULL;
1224 WCHAR *begin_ptr, *end_ptr;
1225 HRESULT hres;
1227 TRACE("%s\n", debugstr_variant(arg));
1229 if(V_VT(arg) == VT_BSTR) {
1230 str = V_BSTR(arg);
1231 }else {
1232 hres = to_string(arg, &conv_str);
1233 if(FAILED(hres))
1234 return hres;
1235 str = conv_str;
1238 for(begin_ptr = str; *begin_ptr && isspaceW(*begin_ptr); begin_ptr++);
1239 for(end_ptr = str+SysStringLen(str); end_ptr-1 > begin_ptr && isspaceW(*(end_ptr-1)); end_ptr--);
1241 str = SysAllocStringLen(begin_ptr, end_ptr-begin_ptr);
1242 SysFreeString(conv_str);
1243 if(!str)
1244 return E_OUTOFMEMORY;
1246 return return_bstr(res, str);
1249 static HRESULT Global_Space(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1251 BSTR str;
1252 int n, i;
1253 HRESULT hres;
1255 TRACE("%s\n", debugstr_variant(arg));
1257 hres = to_int(arg, &n);
1258 if(FAILED(hres))
1259 return hres;
1261 if(n < 0) {
1262 FIXME("n = %d\n", n);
1263 return E_NOTIMPL;
1266 if(!res)
1267 return S_OK;
1269 str = SysAllocStringLen(NULL, n);
1270 if(!str)
1271 return E_OUTOFMEMORY;
1273 for(i=0; i<n; i++)
1274 str[i] = ' ';
1276 V_VT(res) = VT_BSTR;
1277 V_BSTR(res) = str;
1278 return S_OK;
1281 static HRESULT Global_String(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1283 FIXME("\n");
1284 return E_NOTIMPL;
1287 static HRESULT Global_InStr(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
1289 VARIANT *startv, *str1v, *str2v;
1290 BSTR str1, str2;
1291 int start, ret;
1292 HRESULT hres;
1294 TRACE("\n");
1296 assert(2 <= args_cnt && args_cnt <= 4);
1298 switch(args_cnt) {
1299 case 2:
1300 startv = NULL;
1301 str1v = args;
1302 str2v = args+1;
1303 break;
1304 case 3:
1305 startv = args;
1306 str1v = args+1;
1307 str2v = args+2;
1308 break;
1309 case 4:
1310 FIXME("unsupported compare argument %s\n", debugstr_variant(args));
1311 return E_NOTIMPL;
1312 DEFAULT_UNREACHABLE;
1315 if(startv) {
1316 hres = to_int(startv, &start);
1317 if(FAILED(hres))
1318 return hres;
1319 if(--start < 0) {
1320 FIXME("start %d\n", start);
1321 return E_FAIL;
1323 }else {
1324 start = 0;
1327 if(V_VT(str1v) == VT_NULL || V_VT(str2v) == VT_NULL)
1328 return return_null(res);
1330 if(V_VT(str1v) != VT_BSTR) {
1331 FIXME("Unsupported str1 type %s\n", debugstr_variant(str1v));
1332 return E_NOTIMPL;
1334 str1 = V_BSTR(str1v);
1336 if(V_VT(str2v) != VT_BSTR) {
1337 FIXME("Unsupported str2 type %s\n", debugstr_variant(str2v));
1338 return E_NOTIMPL;
1340 str2 = V_BSTR(str2v);
1342 if(start < SysStringLen(str1)) {
1343 WCHAR *ptr;
1345 ptr = strstrW(str1+start, str2);
1346 ret = ptr ? ptr-str1+1 : 0;
1347 }else {
1348 ret = 0;
1351 return return_int(res, ret);
1354 static HRESULT Global_InStrB(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1356 FIXME("\n");
1357 return E_NOTIMPL;
1360 static HRESULT Global_AscB(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1362 FIXME("\n");
1363 return E_NOTIMPL;
1366 static HRESULT Global_ChrB(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1368 FIXME("\n");
1369 return E_NOTIMPL;
1372 static HRESULT Global_Asc(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1374 FIXME("\n");
1375 return E_NOTIMPL;
1378 /* The function supports only single-byte and double-byte character sets. It
1379 * ignores language specified by IActiveScriptSite::GetLCID. The argument needs
1380 * to be in range of short or unsigned short. */
1381 static HRESULT Global_Chr(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1383 int cp, c, len = 0;
1384 CPINFO cpi;
1385 WCHAR ch;
1386 char buf[2];
1387 HRESULT hres;
1389 TRACE("%s\n", debugstr_variant(arg));
1391 hres = to_int(arg, &c);
1392 if(FAILED(hres))
1393 return hres;
1395 cp = GetACP();
1396 if(!GetCPInfo(cp, &cpi))
1397 cpi.MaxCharSize = 1;
1399 if((c!=(short)c && c!=(unsigned short)c) ||
1400 (unsigned short)c>=(cpi.MaxCharSize>1 ? 0x10000 : 0x100)) {
1401 WARN("invalid arg %d\n", c);
1402 return MAKE_VBSERROR(VBSE_ILLEGAL_FUNC_CALL);
1405 if(c>>8)
1406 buf[len++] = c>>8;
1407 if(!len || IsDBCSLeadByteEx(cp, buf[0]))
1408 buf[len++] = c;
1409 if(!MultiByteToWideChar(CP_ACP, 0, buf, len, &ch, 1)) {
1410 WARN("invalid arg %d, cp %d\n", c, cp);
1411 return E_FAIL;
1414 if(res) {
1415 V_VT(res) = VT_BSTR;
1416 V_BSTR(res) = SysAllocStringLen(&ch, 1);
1417 if(!V_BSTR(res))
1418 return E_OUTOFMEMORY;
1420 return S_OK;
1423 static HRESULT Global_AscW(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1425 FIXME("\n");
1426 return E_NOTIMPL;
1429 static HRESULT Global_ChrW(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1431 FIXME("\n");
1432 return E_NOTIMPL;
1435 static HRESULT Global_Abs(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1437 HRESULT hres;
1438 VARIANT dst;
1440 TRACE("(%s)\n", debugstr_variant(arg));
1442 assert(args_cnt == 1);
1444 hres = VarAbs(arg, &dst);
1445 if(FAILED(hres))
1446 return hres;
1448 if (res)
1449 *res = dst;
1450 else
1451 VariantClear(&dst);
1453 return S_OK;
1456 static HRESULT Global_Fix(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1458 HRESULT hres;
1459 VARIANT dst;
1461 TRACE("(%s)\n", debugstr_variant(arg));
1463 assert(args_cnt == 1);
1465 hres = VarFix(arg, &dst);
1466 if(FAILED(hres))
1467 return hres;
1469 if (res)
1470 *res = dst;
1471 else
1472 VariantClear(&dst);
1474 return S_OK;
1477 static HRESULT Global_Int(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1479 HRESULT hres;
1480 VARIANT dst;
1482 TRACE("(%s)\n", debugstr_variant(arg));
1484 assert(args_cnt == 1);
1486 hres = VarInt(arg, &dst);
1487 if(FAILED(hres))
1488 return hres;
1490 if (res)
1491 *res = dst;
1492 else
1493 VariantClear(&dst);
1495 return S_OK;
1498 static HRESULT Global_Sgn(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1500 double v;
1501 short val;
1502 HRESULT hres;
1504 TRACE("(%s)\n", debugstr_variant(arg));
1506 assert(args_cnt == 1);
1508 if(V_VT(arg) == VT_NULL)
1509 return MAKE_VBSERROR(VBSE_ILLEGAL_NULL_USE);
1511 hres = to_double(arg, &v);
1512 if (FAILED(hres))
1513 return hres;
1515 val = v == 0 ? 0 : (v > 0 ? 1 : -1);
1516 return return_short(res, val);
1519 static HRESULT Global_Now(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1521 SYSTEMTIME lt;
1522 double date;
1524 TRACE("\n");
1526 GetLocalTime(&lt);
1527 SystemTimeToVariantTime(&lt, &date);
1528 return return_date(res, date);
1531 static HRESULT Global_Date(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1533 SYSTEMTIME lt;
1534 UDATE ud;
1535 DATE date;
1536 HRESULT hres;
1538 TRACE("\n");
1540 GetLocalTime(&lt);
1541 ud.st = lt;
1542 ud.wDayOfYear = 0;
1543 hres = VarDateFromUdateEx(&ud, 0, VAR_DATEVALUEONLY, &date);
1544 if(FAILED(hres))
1545 return hres;
1546 return return_date(res, date);
1549 static HRESULT Global_Time(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1551 SYSTEMTIME lt;
1552 UDATE ud;
1553 DATE time;
1554 HRESULT hres;
1556 TRACE("\n");
1558 GetLocalTime(&lt);
1559 ud.st = lt;
1560 ud.wDayOfYear = 0;
1561 hres = VarDateFromUdateEx(&ud, 0, VAR_TIMEVALUEONLY, &time);
1562 if(FAILED(hres))
1563 return hres;
1564 return return_date(res, time);
1567 static HRESULT Global_Day(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1569 FIXME("\n");
1570 return E_NOTIMPL;
1573 static HRESULT Global_Month(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1575 FIXME("\n");
1576 return E_NOTIMPL;
1579 static HRESULT Global_Weekday(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1581 FIXME("\n");
1582 return E_NOTIMPL;
1585 static HRESULT Global_Year(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1587 FIXME("\n");
1588 return E_NOTIMPL;
1591 static HRESULT Global_Hour(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1593 FIXME("\n");
1594 return E_NOTIMPL;
1597 static HRESULT Global_Minute(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1599 FIXME("\n");
1600 return E_NOTIMPL;
1603 static HRESULT Global_Second(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1605 FIXME("\n");
1606 return E_NOTIMPL;
1609 static HRESULT Global_DateValue(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1611 FIXME("\n");
1612 return E_NOTIMPL;
1615 static HRESULT Global_TimeValue(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1617 FIXME("\n");
1618 return E_NOTIMPL;
1621 static HRESULT Global_DateSerial(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1623 FIXME("\n");
1624 return E_NOTIMPL;
1627 static HRESULT Global_TimeSerial(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1629 FIXME("\n");
1630 return E_NOTIMPL;
1633 static HRESULT Global_InputBox(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1635 FIXME("\n");
1636 return E_NOTIMPL;
1639 static HRESULT Global_MsgBox(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
1641 BSTR prompt, title = NULL;
1642 int type = MB_OK;
1643 HRESULT hres;
1645 TRACE("\n");
1647 assert(1 <= args_cnt && args_cnt <= 5);
1649 hres = to_string(args, &prompt);
1650 if(FAILED(hres))
1651 return hres;
1653 if(args_cnt > 1)
1654 hres = to_int(args+1, &type);
1656 if(SUCCEEDED(hres) && args_cnt > 2)
1657 hres = to_string(args+2, &title);
1659 if(SUCCEEDED(hres) && args_cnt > 3) {
1660 FIXME("unsupported arg_cnt %d\n", args_cnt);
1661 hres = E_NOTIMPL;
1664 if(SUCCEEDED(hres))
1665 hres = show_msgbox(This->desc->ctx, prompt, type, title, res);
1667 SysFreeString(prompt);
1668 SysFreeString(title);
1669 return hres;
1672 static HRESULT Global_CreateObject(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1674 IUnknown *obj;
1675 HRESULT hres;
1677 TRACE("(%s)\n", debugstr_variant(arg));
1679 if(V_VT(arg) != VT_BSTR) {
1680 FIXME("non-bstr arg\n");
1681 return E_INVALIDARG;
1684 obj = create_object(This->desc->ctx, V_BSTR(arg));
1685 if(!obj)
1686 return VB_E_CANNOT_CREATE_OBJ;
1688 if(res) {
1689 hres = IUnknown_QueryInterface(obj, &IID_IDispatch, (void**)&V_DISPATCH(res));
1690 if(FAILED(hres))
1691 return hres;
1693 V_VT(res) = VT_DISPATCH;
1696 IUnknown_Release(obj);
1697 return S_OK;
1700 static HRESULT Global_GetObject(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
1702 IBindCtx *bind_ctx;
1703 IUnknown *obj_unk;
1704 IDispatch *disp;
1705 ULONG eaten = 0;
1706 IMoniker *mon;
1707 HRESULT hres;
1709 TRACE("%s %s\n", args_cnt ? debugstr_variant(args) : "", args_cnt > 1 ? debugstr_variant(args+1) : "");
1711 if(args_cnt != 1 || V_VT(args) != VT_BSTR) {
1712 FIXME("unsupported args\n");
1713 return E_NOTIMPL;
1716 if(This->desc->ctx->safeopt & (INTERFACE_USES_SECURITY_MANAGER|INTERFACESAFE_FOR_UNTRUSTED_DATA)) {
1717 WARN("blocked in current safety mode\n");
1718 return VB_E_CANNOT_CREATE_OBJ;
1721 hres = CreateBindCtx(0, &bind_ctx);
1722 if(FAILED(hres))
1723 return hres;
1725 hres = MkParseDisplayName(bind_ctx, V_BSTR(args), &eaten, &mon);
1726 if(SUCCEEDED(hres)) {
1727 hres = IMoniker_BindToObject(mon, bind_ctx, NULL, &IID_IUnknown, (void**)&obj_unk);
1728 IMoniker_Release(mon);
1729 }else {
1730 hres = MK_E_SYNTAX;
1732 IBindCtx_Release(bind_ctx);
1733 if(FAILED(hres))
1734 return hres;
1736 hres = set_object_site(This->desc->ctx, obj_unk);
1737 if(FAILED(hres)) {
1738 IUnknown_Release(obj_unk);
1739 return hres;
1742 hres = IUnknown_QueryInterface(obj_unk, &IID_IDispatch, (void**)&disp);
1743 if(SUCCEEDED(hres)) {
1744 if(res) {
1745 V_VT(res) = VT_DISPATCH;
1746 V_DISPATCH(res) = disp;
1747 }else {
1748 IDispatch_Release(disp);
1750 }else {
1751 FIXME("object does not support IDispatch\n");
1754 return hres;
1757 static HRESULT Global_DateAdd(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1759 FIXME("\n");
1760 return E_NOTIMPL;
1763 static HRESULT Global_DateDiff(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1765 FIXME("\n");
1766 return E_NOTIMPL;
1769 static HRESULT Global_DatePart(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1771 FIXME("\n");
1772 return E_NOTIMPL;
1775 static HRESULT Global_TypeName(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1777 static const WCHAR ByteW[] = {'B', 'y', 't', 'e', 0};
1778 static const WCHAR IntegerW[] = {'I', 'n', 't', 'e', 'g', 'e', 'r', 0};
1779 static const WCHAR LongW[] = {'L', 'o', 'n', 'g', 0};
1780 static const WCHAR SingleW[] = {'S', 'i', 'n', 'g', 'l', 'e', 0};
1781 static const WCHAR DoubleW[] = {'D', 'o', 'u', 'b', 'l', 'e', 0};
1782 static const WCHAR CurrencyW[] = {'C', 'u', 'r', 'r', 'e', 'n', 'c', 'y', 0};
1783 static const WCHAR DecimalW[] = {'D', 'e', 'c', 'i', 'm', 'a', 'l', 0};
1784 static const WCHAR DateW[] = {'D', 'a', 't', 'e', 0};
1785 static const WCHAR StringW[] = {'S', 't', 'r', 'i', 'n', 'g', 0};
1786 static const WCHAR BooleanW[] = {'B', 'o', 'o', 'l', 'e', 'a', 'n', 0};
1787 static const WCHAR EmptyW[] = {'E', 'm', 'p', 't', 'y', 0};
1788 static const WCHAR NullW[] = {'N', 'u', 'l', 'l', 0};
1790 TRACE("(%s)\n", debugstr_variant(arg));
1792 assert(args_cnt == 1);
1794 switch(V_VT(arg)) {
1795 case VT_UI1:
1796 return return_string(res, ByteW);
1797 case VT_I2:
1798 return return_string(res, IntegerW);
1799 case VT_I4:
1800 return return_string(res, LongW);
1801 case VT_R4:
1802 return return_string(res, SingleW);
1803 case VT_R8:
1804 return return_string(res, DoubleW);
1805 case VT_CY:
1806 return return_string(res, CurrencyW);
1807 case VT_DECIMAL:
1808 return return_string(res, DecimalW);
1809 case VT_DATE:
1810 return return_string(res, DateW);
1811 case VT_BSTR:
1812 return return_string(res, StringW);
1813 case VT_BOOL:
1814 return return_string(res, BooleanW);
1815 case VT_EMPTY:
1816 return return_string(res, EmptyW);
1817 case VT_NULL:
1818 return return_string(res, NullW);
1819 default:
1820 FIXME("arg %s not supported\n", debugstr_variant(arg));
1821 return E_NOTIMPL;
1825 static HRESULT Global_Array(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1827 SAFEARRAYBOUND bounds;
1828 SAFEARRAY *sa;
1829 VARIANT *data;
1830 HRESULT hres;
1831 unsigned i;
1833 TRACE("arg_cnt=%u\n", args_cnt);
1835 bounds.lLbound = 0;
1836 bounds.cElements = args_cnt;
1837 sa = SafeArrayCreate(VT_VARIANT, 1, &bounds);
1838 if(!sa)
1839 return E_OUTOFMEMORY;
1841 hres = SafeArrayAccessData(sa, (void**)&data);
1842 if(FAILED(hres)) {
1843 SafeArrayDestroy(sa);
1844 return hres;
1847 for(i=0; i<args_cnt; i++) {
1848 hres = VariantCopyInd(data+i, arg+i);
1849 if(FAILED(hres)) {
1850 SafeArrayUnaccessData(sa);
1851 SafeArrayDestroy(sa);
1852 return hres;
1855 SafeArrayUnaccessData(sa);
1857 if(res) {
1858 V_VT(res) = VT_ARRAY|VT_VARIANT;
1859 V_ARRAY(res) = sa;
1860 }else {
1861 SafeArrayDestroy(sa);
1864 return S_OK;
1867 static HRESULT Global_Erase(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1869 FIXME("\n");
1870 return E_NOTIMPL;
1873 static HRESULT Global_Filter(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1875 FIXME("\n");
1876 return E_NOTIMPL;
1879 static HRESULT Global_Join(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1881 FIXME("\n");
1882 return E_NOTIMPL;
1885 static HRESULT Global_Split(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1887 FIXME("\n");
1888 return E_NOTIMPL;
1891 static HRESULT Global_Replace(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1893 FIXME("\n");
1894 return E_NOTIMPL;
1897 static HRESULT Global_StrReverse(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1899 WCHAR *ptr1, *ptr2, ch;
1900 BSTR ret;
1901 HRESULT hres;
1903 TRACE("%s\n", debugstr_variant(arg));
1905 hres = to_string(arg, &ret);
1906 if(FAILED(hres))
1907 return hres;
1909 ptr1 = ret;
1910 ptr2 = ret + SysStringLen(ret)-1;
1911 while(ptr1 < ptr2) {
1912 ch = *ptr1;
1913 *ptr1++ = *ptr2;
1914 *ptr2-- = ch;
1917 return return_bstr(res, ret);
1920 static HRESULT Global_InStrRev(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
1922 int start, ret = 0;
1923 BSTR str1, str2;
1924 HRESULT hres;
1926 TRACE("%s %s arg_cnt=%u\n", debugstr_variant(args), debugstr_variant(args+1), args_cnt);
1928 if(args_cnt > 3) {
1929 FIXME("Unsupported args\n");
1930 return E_NOTIMPL;
1933 assert(2 <= args_cnt && args_cnt <= 4);
1935 if(V_VT(args) == VT_NULL || V_VT(args+1) == VT_NULL || (args_cnt > 2 && V_VT(args+2) == VT_NULL))
1936 return MAKE_VBSERROR(VBSE_ILLEGAL_NULL_USE);
1938 hres = to_string(args, &str1);
1939 if(FAILED(hres))
1940 return hres;
1942 hres = to_string(args+1, &str2);
1943 if(SUCCEEDED(hres)) {
1944 if(args_cnt > 2) {
1945 hres = to_int(args+2, &start);
1946 if(SUCCEEDED(hres) && start <= 0) {
1947 FIXME("Unsupported start %d\n", start);
1948 hres = E_NOTIMPL;
1950 }else {
1951 start = SysStringLen(str1);
1953 } else {
1954 str2 = NULL;
1957 if(SUCCEEDED(hres)) {
1958 const WCHAR *ptr;
1959 size_t len;
1961 len = SysStringLen(str2);
1962 if(start >= len && start <= SysStringLen(str1)) {
1963 for(ptr = str1+start-SysStringLen(str2); ptr >= str1; ptr--) {
1964 if(!memcmp(ptr, str2, len*sizeof(WCHAR))) {
1965 ret = ptr-str1+1;
1966 break;
1972 SysFreeString(str1);
1973 SysFreeString(str2);
1974 if(FAILED(hres))
1975 return hres;
1977 return return_int(res, ret);
1980 static HRESULT Global_LoadPicture(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1982 FIXME("\n");
1983 return E_NOTIMPL;
1986 static HRESULT Global_ScriptEngine(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1988 TRACE("%s\n", debugstr_variant(arg));
1990 assert(args_cnt == 0);
1992 return return_string(res, vbscriptW);
1995 static HRESULT Global_ScriptEngineMajorVersion(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
1997 TRACE("%s\n", debugstr_variant(arg));
1999 assert(args_cnt == 0);
2001 return return_int(res, VBSCRIPT_MAJOR_VERSION);
2004 static HRESULT Global_ScriptEngineMinorVersion(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2006 TRACE("%s\n", debugstr_variant(arg));
2008 assert(args_cnt == 0);
2010 return return_int(res, VBSCRIPT_MINOR_VERSION);
2013 static HRESULT Global_ScriptEngineBuildVersion(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2015 TRACE("%s\n", debugstr_variant(arg));
2017 assert(args_cnt == 0);
2019 return return_int(res, VBSCRIPT_BUILD_VERSION);
2022 static HRESULT Global_FormatNumber(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2024 FIXME("\n");
2025 return E_NOTIMPL;
2028 static HRESULT Global_FormatCurrency(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2030 FIXME("\n");
2031 return E_NOTIMPL;
2034 static HRESULT Global_FormatPercent(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2036 FIXME("\n");
2037 return E_NOTIMPL;
2040 static HRESULT Global_FormatDateTime(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2042 FIXME("\n");
2043 return E_NOTIMPL;
2046 static HRESULT Global_WeekdayName(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
2048 int weekday, first_day = 1, abbrev = 0;
2049 BSTR ret;
2050 HRESULT hres;
2052 TRACE("\n");
2054 assert(1 <= args_cnt && args_cnt <= 3);
2056 hres = to_int(args, &weekday);
2057 if(FAILED(hres))
2058 return hres;
2060 if(args_cnt > 1) {
2061 hres = to_int(args+1, &abbrev);
2062 if(FAILED(hres))
2063 return hres;
2065 if(args_cnt == 3) {
2066 hres = to_int(args+2, &first_day);
2067 if(FAILED(hres))
2068 return hres;
2072 hres = VarWeekdayName(weekday, abbrev, first_day, 0, &ret);
2073 if(FAILED(hres))
2074 return hres;
2076 return return_bstr(res, ret);
2079 static HRESULT Global_MonthName(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
2081 int month, abbrev = 0;
2082 BSTR ret;
2083 HRESULT hres;
2085 TRACE("\n");
2087 assert(args_cnt == 1 || args_cnt == 2);
2089 hres = to_int(args, &month);
2090 if(FAILED(hres))
2091 return hres;
2093 if(args_cnt == 2) {
2094 hres = to_int(args+1, &abbrev);
2095 if(FAILED(hres))
2096 return hres;
2099 hres = VarMonthName(month, abbrev, 0, &ret);
2100 if(FAILED(hres))
2101 return hres;
2103 return return_bstr(res, ret);
2106 static HRESULT Global_Round(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2108 double n;
2109 HRESULT hres;
2111 TRACE("%s\n", debugstr_variant(arg));
2113 if(!res)
2114 return S_OK;
2116 switch(V_VT(arg)) {
2117 case VT_I2:
2118 case VT_I4:
2119 case VT_BOOL:
2120 *res = *arg;
2121 return S_OK;
2122 case VT_R8:
2123 n = V_R8(arg);
2124 break;
2125 default:
2126 hres = to_double(arg, &n);
2127 if(FAILED(hres))
2128 return hres;
2131 return return_double(res, round(n));
2134 static HRESULT Global_Escape(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2136 FIXME("\n");
2137 return E_NOTIMPL;
2140 static HRESULT Global_Unescape(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2142 FIXME("\n");
2143 return E_NOTIMPL;
2146 static HRESULT Global_Eval(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2148 FIXME("\n");
2149 return E_NOTIMPL;
2152 static HRESULT Global_Execute(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2154 FIXME("\n");
2155 return E_NOTIMPL;
2158 static HRESULT Global_ExecuteGlobal(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2160 FIXME("\n");
2161 return E_NOTIMPL;
2164 static HRESULT Global_GetRef(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
2166 FIXME("\n");
2167 return E_NOTIMPL;
2170 static const string_constant_t vbCr = {1, {'\r'}};
2171 static const string_constant_t vbCrLf = {2, {'\r','\n'}};
2172 static const string_constant_t vbNewLine = {2, {'\r','\n'}};
2173 static const string_constant_t vbFormFeed = {1, {0xc}};
2174 static const string_constant_t vbLf = {1, {'\n'}};
2175 static const string_constant_t vbNullChar = {1};
2176 static const string_constant_t vbNullString = {0};
2177 static const string_constant_t vbTab = {1, {'\t'}};
2178 static const string_constant_t vbVerticalTab = {1, {0xb}};
2180 static const builtin_prop_t global_props[] = {
2181 {DISPID_GLOBAL_VBUSESYSTEM, NULL, BP_GET, VT_I2, 0},
2182 {DISPID_GLOBAL_USESYSTEMDAYOFWEEK, NULL, BP_GET, VT_I2, 0},
2183 {DISPID_GLOBAL_VBSUNDAY, NULL, BP_GET, VT_I2, 1},
2184 {DISPID_GLOBAL_VBMONDAY, NULL, BP_GET, VT_I2, 2},
2185 {DISPID_GLOBAL_VBTUESDAY, NULL, BP_GET, VT_I2, 3},
2186 {DISPID_GLOBAL_VBWEDNESDAY, NULL, BP_GET, VT_I2, 4},
2187 {DISPID_GLOBAL_VBTHURSDAY, NULL, BP_GET, VT_I2, 5},
2188 {DISPID_GLOBAL_VBFRIDAY, NULL, BP_GET, VT_I2, 6},
2189 {DISPID_GLOBAL_VBSATURDAY, NULL, BP_GET, VT_I2, 7},
2190 {DISPID_GLOBAL_VBFIRSTJAN1, NULL, BP_GET, VT_I2, 1},
2191 {DISPID_GLOBAL_VBFIRSTFOURDAYS, NULL, BP_GET, VT_I2, 2},
2192 {DISPID_GLOBAL_VBFIRSTFULLWEEK, NULL, BP_GET, VT_I2, 3},
2193 {DISPID_GLOBAL_VBOKONLY, NULL, BP_GET, VT_I2, MB_OK},
2194 {DISPID_GLOBAL_VBOKCANCEL, NULL, BP_GET, VT_I2, MB_OKCANCEL},
2195 {DISPID_GLOBAL_VBABORTRETRYIGNORE, NULL, BP_GET, VT_I2, MB_ABORTRETRYIGNORE},
2196 {DISPID_GLOBAL_VBYESNOCANCEL, NULL, BP_GET, VT_I2, MB_YESNOCANCEL},
2197 {DISPID_GLOBAL_VBYESNO, NULL, BP_GET, VT_I2, MB_YESNO},
2198 {DISPID_GLOBAL_VBRETRYCANCEL, NULL, BP_GET, VT_I2, MB_RETRYCANCEL},
2199 {DISPID_GLOBAL_VBCRITICAL, NULL, BP_GET, VT_I2, MB_ICONHAND},
2200 {DISPID_GLOBAL_VBQUESTION, NULL, BP_GET, VT_I2, MB_ICONQUESTION},
2201 {DISPID_GLOBAL_VBEXCLAMATION, NULL, BP_GET, VT_I2, MB_ICONEXCLAMATION},
2202 {DISPID_GLOBAL_VBINFORMATION, NULL, BP_GET, VT_I2, MB_ICONASTERISK},
2203 {DISPID_GLOBAL_VBDEFAULTBUTTON1, NULL, BP_GET, VT_I2, MB_DEFBUTTON1},
2204 {DISPID_GLOBAL_VBDEFAULTBUTTON2, NULL, BP_GET, VT_I2, MB_DEFBUTTON2},
2205 {DISPID_GLOBAL_VBDEFAULTBUTTON3, NULL, BP_GET, VT_I2, MB_DEFBUTTON3},
2206 {DISPID_GLOBAL_VBDEFAULTBUTTON4, NULL, BP_GET, VT_I2, MB_DEFBUTTON4},
2207 {DISPID_GLOBAL_VBAPPLICATIONMODAL, NULL, BP_GET, VT_I2, MB_APPLMODAL},
2208 {DISPID_GLOBAL_VBSYSTEMMODAL, NULL, BP_GET, VT_I2, MB_SYSTEMMODAL},
2209 {DISPID_GLOBAL_VBOK, NULL, BP_GET, VT_I2, IDOK},
2210 {DISPID_GLOBAL_VBCANCEL, NULL, BP_GET, VT_I2, IDCANCEL},
2211 {DISPID_GLOBAL_VBABORT, NULL, BP_GET, VT_I2, IDABORT},
2212 {DISPID_GLOBAL_VBRETRY, NULL, BP_GET, VT_I2, IDRETRY},
2213 {DISPID_GLOBAL_VBIGNORE, NULL, BP_GET, VT_I2, IDIGNORE},
2214 {DISPID_GLOBAL_VBYES, NULL, BP_GET, VT_I2, IDYES},
2215 {DISPID_GLOBAL_VBNO, NULL, BP_GET, VT_I2, IDNO},
2216 {DISPID_GLOBAL_VBEMPTY, NULL, BP_GET, VT_I2, VT_EMPTY},
2217 {DISPID_GLOBAL_VBNULL, NULL, BP_GET, VT_I2, VT_NULL},
2218 {DISPID_GLOBAL_VBINTEGER, NULL, BP_GET, VT_I2, VT_I2},
2219 {DISPID_GLOBAL_VBLONG, NULL, BP_GET, VT_I2, VT_I4},
2220 {DISPID_GLOBAL_VBSINGLE, NULL, BP_GET, VT_I2, VT_R4},
2221 {DISPID_GLOBAL_VBDOUBLE, NULL, BP_GET, VT_I2, VT_R8},
2222 {DISPID_GLOBAL_VBCURRENCY, NULL, BP_GET, VT_I2, VT_CY},
2223 {DISPID_GLOBAL_VBDATE, NULL, BP_GET, VT_I2, VT_DATE},
2224 {DISPID_GLOBAL_VBSTRING, NULL, BP_GET, VT_I2, VT_BSTR},
2225 {DISPID_GLOBAL_VBOBJECT, NULL, BP_GET, VT_I2, VT_DISPATCH},
2226 {DISPID_GLOBAL_VBERROR, NULL, BP_GET, VT_I2, VT_ERROR},
2227 {DISPID_GLOBAL_VBBOOLEAN, NULL, BP_GET, VT_I2, VT_BOOL},
2228 {DISPID_GLOBAL_VBVARIANT, NULL, BP_GET, VT_I2, VT_VARIANT},
2229 {DISPID_GLOBAL_VBDATAOBJECT, NULL, BP_GET, VT_I2, VT_UNKNOWN},
2230 {DISPID_GLOBAL_VBDECIMAL, NULL, BP_GET, VT_I2, VT_DECIMAL},
2231 {DISPID_GLOBAL_VBBYTE, NULL, BP_GET, VT_I2, VT_UI1},
2232 {DISPID_GLOBAL_VBARRAY, NULL, BP_GET, VT_I2, VT_ARRAY},
2233 {DISPID_GLOBAL_VBTRUE, NULL, BP_GET, VT_I2, VARIANT_TRUE},
2234 {DISPID_GLOBAL_VBFALSE, NULL, BP_GET, VT_I2, VARIANT_FALSE},
2235 {DISPID_GLOBAL_VBUSEDEFAULT, NULL, BP_GET, VT_I2, -2},
2236 {DISPID_GLOBAL_VBBINARYCOMPARE, NULL, BP_GET, VT_I2, 0},
2237 {DISPID_GLOBAL_VBTEXTCOMPARE, NULL, BP_GET, VT_I2, 1},
2238 {DISPID_GLOBAL_VBDATABASECOMPARE, NULL, BP_GET, VT_I2, 2},
2239 {DISPID_GLOBAL_VBGENERALDATE, NULL, BP_GET, VT_I2, 0},
2240 {DISPID_GLOBAL_VBLONGDATE, NULL, BP_GET, VT_I2, 1},
2241 {DISPID_GLOBAL_VBSHORTDATE, NULL, BP_GET, VT_I2, 2},
2242 {DISPID_GLOBAL_VBLONGTIME, NULL, BP_GET, VT_I2, 3},
2243 {DISPID_GLOBAL_VBSHORTTIME, NULL, BP_GET, VT_I2, 4},
2244 {DISPID_GLOBAL_VBOBJECTERROR, NULL, BP_GET, VT_I4, 0x80040000},
2245 {DISPID_GLOBAL_VBBLACK, NULL, BP_GET, VT_I4, 0x000000},
2246 {DISPID_GLOBAL_VBBLUE, NULL, BP_GET, VT_I4, 0xff0000},
2247 {DISPID_GLOBAL_VBCYAN, NULL, BP_GET, VT_I4, 0xffff00},
2248 {DISPID_GLOBAL_VBGREEN, NULL, BP_GET, VT_I4, 0x00ff00},
2249 {DISPID_GLOBAL_VBMAGENTA, NULL, BP_GET, VT_I4, 0xff00ff},
2250 {DISPID_GLOBAL_VBRED, NULL, BP_GET, VT_I4, 0x0000ff},
2251 {DISPID_GLOBAL_VBWHITE, NULL, BP_GET, VT_I4, 0xffffff},
2252 {DISPID_GLOBAL_VBYELLOW, NULL, BP_GET, VT_I4, 0x00ffff},
2253 {DISPID_GLOBAL_VBCR, NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbCr},
2254 {DISPID_GLOBAL_VBCRLF, NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbCrLf},
2255 {DISPID_GLOBAL_VBNEWLINE, NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbNewLine},
2256 {DISPID_GLOBAL_VBFORMFEED, NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbFormFeed},
2257 {DISPID_GLOBAL_VBLF, NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbLf},
2258 {DISPID_GLOBAL_VBNULLCHAR, NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbNullChar},
2259 {DISPID_GLOBAL_VBNULLSTRING, NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbNullString},
2260 {DISPID_GLOBAL_VBTAB, NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbTab},
2261 {DISPID_GLOBAL_VBVERTICALTAB, NULL, BP_GET, VT_BSTR, (UINT_PTR)&vbVerticalTab},
2262 {DISPID_GLOBAL_CCUR, Global_CCur, 0, 1},
2263 {DISPID_GLOBAL_CINT, Global_CInt, 0, 1},
2264 {DISPID_GLOBAL_CLNG, Global_CLng, 0, 1},
2265 {DISPID_GLOBAL_CBOOL, Global_CBool, 0, 1},
2266 {DISPID_GLOBAL_CBYTE, Global_CByte, 0, 1},
2267 {DISPID_GLOBAL_CDATE, Global_CDate, 0, 1},
2268 {DISPID_GLOBAL_CDBL, Global_CDbl, 0, 1},
2269 {DISPID_GLOBAL_CSNG, Global_CSng, 0, 1},
2270 {DISPID_GLOBAL_CSTR, Global_CStr, 0, 1},
2271 {DISPID_GLOBAL_HEX, Global_Hex, 0, 1},
2272 {DISPID_GLOBAL_OCT, Global_Oct, 0, 1},
2273 {DISPID_GLOBAL_VARTYPE, Global_VarType, 0, 1},
2274 {DISPID_GLOBAL_ISDATE, Global_IsDate, 0, 1},
2275 {DISPID_GLOBAL_ISEMPTY, Global_IsEmpty, 0, 1},
2276 {DISPID_GLOBAL_ISNULL, Global_IsNull, 0, 1},
2277 {DISPID_GLOBAL_ISNUMERIC, Global_IsNumeric, 0, 1},
2278 {DISPID_GLOBAL_ISARRAY, Global_IsArray, 0, 1},
2279 {DISPID_GLOBAL_ISOBJECT, Global_IsObject, 0, 1},
2280 {DISPID_GLOBAL_ATN, Global_Atn, 0, 1},
2281 {DISPID_GLOBAL_COS, Global_Cos, 0, 1},
2282 {DISPID_GLOBAL_SIN, Global_Sin, 0, 1},
2283 {DISPID_GLOBAL_TAN, Global_Tan, 0, 1},
2284 {DISPID_GLOBAL_EXP, Global_Exp, 0, 1},
2285 {DISPID_GLOBAL_LOG, Global_Log, 0, 1},
2286 {DISPID_GLOBAL_SQR, Global_Sqr, 0, 1},
2287 {DISPID_GLOBAL_RANDOMIZE, Global_Randomize, 0, 1},
2288 {DISPID_GLOBAL_RND, Global_Rnd, 0, 1},
2289 {DISPID_GLOBAL_TIMER, Global_Timer, 0, 0},
2290 {DISPID_GLOBAL_LBOUND, Global_LBound, 0, 1},
2291 {DISPID_GLOBAL_UBOUND, Global_UBound, 0, 1, 2},
2292 {DISPID_GLOBAL_RGB, Global_RGB, 0, 3},
2293 {DISPID_GLOBAL_LEN, Global_Len, 0, 1},
2294 {DISPID_GLOBAL_LENB, Global_LenB, 0, 1},
2295 {DISPID_GLOBAL_LEFT, Global_Left, 0, 2},
2296 {DISPID_GLOBAL_LEFTB, Global_LeftB, 0, 2},
2297 {DISPID_GLOBAL_RIGHT, Global_Right, 0, 2},
2298 {DISPID_GLOBAL_RIGHTB, Global_RightB, 0, 2},
2299 {DISPID_GLOBAL_MID, Global_Mid, 0, 2, 3},
2300 {DISPID_GLOBAL_MIDB, Global_MidB, 0, 2, 3},
2301 {DISPID_GLOBAL_STRCOMP, Global_StrComp, 0, 2, 3},
2302 {DISPID_GLOBAL_LCASE, Global_LCase, 0, 1},
2303 {DISPID_GLOBAL_UCASE, Global_UCase, 0, 1},
2304 {DISPID_GLOBAL_LTRIM, Global_LTrim, 0, 1},
2305 {DISPID_GLOBAL_RTRIM, Global_RTrim, 0, 1},
2306 {DISPID_GLOBAL_TRIM, Global_Trim, 0, 1},
2307 {DISPID_GLOBAL_SPACE, Global_Space, 0, 1},
2308 {DISPID_GLOBAL_STRING, Global_String, 0, 0, 2},
2309 {DISPID_GLOBAL_INSTR, Global_InStr, 0, 2, 4},
2310 {DISPID_GLOBAL_INSTRB, Global_InStrB, 0, 3, 4},
2311 {DISPID_GLOBAL_ASCB, Global_AscB, 0, 1},
2312 {DISPID_GLOBAL_CHRB, Global_ChrB, 0, 1},
2313 {DISPID_GLOBAL_ASC, Global_Asc, 0, 1},
2314 {DISPID_GLOBAL_CHR, Global_Chr, 0, 1},
2315 {DISPID_GLOBAL_ASCW, Global_AscW, 0, 1},
2316 {DISPID_GLOBAL_CHRW, Global_ChrW, 0, 1},
2317 {DISPID_GLOBAL_ABS, Global_Abs, 0, 1},
2318 {DISPID_GLOBAL_FIX, Global_Fix, 0, 1},
2319 {DISPID_GLOBAL_INT, Global_Int, 0, 1},
2320 {DISPID_GLOBAL_SGN, Global_Sgn, 0, 1},
2321 {DISPID_GLOBAL_NOW, Global_Now, 0, 0},
2322 {DISPID_GLOBAL_DATE, Global_Date, 0, 0},
2323 {DISPID_GLOBAL_TIME, Global_Time, 0, 0},
2324 {DISPID_GLOBAL_DAY, Global_Day, 0, 1},
2325 {DISPID_GLOBAL_MONTH, Global_Month, 0, 1},
2326 {DISPID_GLOBAL_WEEKDAY, Global_Weekday, 0, 1, 2},
2327 {DISPID_GLOBAL_YEAR, Global_Year, 0, 1},
2328 {DISPID_GLOBAL_HOUR, Global_Hour, 0, 1},
2329 {DISPID_GLOBAL_MINUTE, Global_Minute, 0, 1},
2330 {DISPID_GLOBAL_SECOND, Global_Second, 0, 1},
2331 {DISPID_GLOBAL_DATEVALUE, Global_DateValue, 0, 1},
2332 {DISPID_GLOBAL_TIMEVALUE, Global_TimeValue, 0, 1},
2333 {DISPID_GLOBAL_DATESERIAL, Global_DateSerial, 0, 3},
2334 {DISPID_GLOBAL_TIMESERIAL, Global_TimeSerial, 0, 3},
2335 {DISPID_GLOBAL_INPUTBOX, Global_InputBox, 0, 1, 7},
2336 {DISPID_GLOBAL_MSGBOX, Global_MsgBox, 0, 1, 5},
2337 {DISPID_GLOBAL_CREATEOBJECT, Global_CreateObject, 0, 1},
2338 {DISPID_GLOBAL_GETOBJECT, Global_GetObject, 0, 0, 2},
2339 {DISPID_GLOBAL_DATEADD, Global_DateAdd, 0, 3},
2340 {DISPID_GLOBAL_DATEDIFF, Global_DateDiff, 0, 3, 5},
2341 {DISPID_GLOBAL_DATEPART, Global_DatePart, 0, 2, 4},
2342 {DISPID_GLOBAL_TYPENAME, Global_TypeName, 0, 1},
2343 {DISPID_GLOBAL_ARRAY, Global_Array, 0, 0, MAXDWORD},
2344 {DISPID_GLOBAL_ERASE, Global_Erase, 0, 1},
2345 {DISPID_GLOBAL_FILTER, Global_Filter, 0, 2, 4},
2346 {DISPID_GLOBAL_JOIN, Global_Join, 0, 1, 2},
2347 {DISPID_GLOBAL_SPLIT, Global_Split, 0, 1, 4},
2348 {DISPID_GLOBAL_REPLACE, Global_Replace, 0, 3, 6},
2349 {DISPID_GLOBAL_STRREVERSE, Global_StrReverse, 0, 1},
2350 {DISPID_GLOBAL_INSTRREV, Global_InStrRev, 0, 2, 4},
2351 {DISPID_GLOBAL_LOADPICTURE, Global_LoadPicture, 0, 1},
2352 {DISPID_GLOBAL_SCRIPTENGINE, Global_ScriptEngine, 0, 0},
2353 {DISPID_GLOBAL_SCRIPTENGINEMAJORVERSION, Global_ScriptEngineMajorVersion, 0, 0},
2354 {DISPID_GLOBAL_SCRIPTENGINEMINORVERSION, Global_ScriptEngineMinorVersion, 0, 0},
2355 {DISPID_GLOBAL_SCRIPTENGINEBUILDVERSION, Global_ScriptEngineBuildVersion, 0, 0},
2356 {DISPID_GLOBAL_FORMATNUMBER, Global_FormatNumber, 0, 1, 5},
2357 {DISPID_GLOBAL_FORMATCURRENCY, Global_FormatCurrency, 0, 1, 5},
2358 {DISPID_GLOBAL_FORMATPERCENT, Global_FormatPercent, 0, 1, 5},
2359 {DISPID_GLOBAL_FORMATDATETIME, Global_FormatDateTime, 0, 1, 2},
2360 {DISPID_GLOBAL_WEEKDAYNAME, Global_WeekdayName, 0, 1, 3},
2361 {DISPID_GLOBAL_MONTHNAME, Global_MonthName, 0, 1, 2},
2362 {DISPID_GLOBAL_ROUND, Global_Round, 0, 1, 2},
2363 {DISPID_GLOBAL_ESCAPE, Global_Escape, 0, 1},
2364 {DISPID_GLOBAL_UNESCAPE, Global_Unescape, 0, 1},
2365 {DISPID_GLOBAL_EVAL, Global_Eval, 0, 1},
2366 {DISPID_GLOBAL_EXECUTE, Global_Execute, 0, 1},
2367 {DISPID_GLOBAL_EXECUTEGLOBAL, Global_ExecuteGlobal, 0, 1},
2368 {DISPID_GLOBAL_GETREF, Global_GetRef, 0, 1},
2369 {DISPID_GLOBAL_VBMSGBOXHELPBUTTON, NULL, BP_GET, VT_I4, MB_HELP},
2370 {DISPID_GLOBAL_VBMSGBOXSETFOREGROUND, NULL, BP_GET, VT_I4, MB_SETFOREGROUND},
2371 {DISPID_GLOBAL_VBMSGBOXRIGHT, NULL, BP_GET, VT_I4, MB_RIGHT},
2372 {DISPID_GLOBAL_VBMSGBOXRTLREADING, NULL, BP_GET, VT_I4, MB_RTLREADING}
2375 static HRESULT Err_Description(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
2377 FIXME("\n");
2378 return E_NOTIMPL;
2381 static HRESULT Err_HelpContext(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
2383 FIXME("\n");
2384 return E_NOTIMPL;
2387 static HRESULT Err_HelpFile(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
2389 FIXME("\n");
2390 return E_NOTIMPL;
2393 static HRESULT Err_Number(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
2395 HRESULT hres;
2397 TRACE("\n");
2399 if(!This->desc)
2400 return E_UNEXPECTED;
2402 if(args_cnt) {
2403 FIXME("setter not implemented\n");
2404 return E_NOTIMPL;
2407 hres = This->desc->ctx->err_number;
2408 return return_int(res, HRESULT_FACILITY(hres) == FACILITY_VBS ? HRESULT_CODE(hres) : hres);
2411 static HRESULT Err_Source(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
2413 FIXME("\n");
2414 return E_NOTIMPL;
2417 static HRESULT Err_Clear(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
2419 TRACE("\n");
2421 if(!This->desc)
2422 return E_UNEXPECTED;
2424 This->desc->ctx->err_number = S_OK;
2425 return S_OK;
2428 static HRESULT Err_Raise(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
2430 FIXME("\n");
2431 return E_NOTIMPL;
2434 static const builtin_prop_t err_props[] = {
2435 {DISPID_ERR_DESCRIPTION, Err_Description, BP_GETPUT},
2436 {DISPID_ERR_HELPCONTEXT, Err_HelpContext, BP_GETPUT},
2437 {DISPID_ERR_HELPFILE, Err_HelpFile, BP_GETPUT},
2438 {DISPID_ERR_NUMBER, Err_Number, BP_GETPUT},
2439 {DISPID_ERR_SOURCE, Err_Source, BP_GETPUT},
2440 {DISPID_ERR_CLEAR, Err_Clear},
2441 {DISPID_ERR_RAISE, Err_Raise, 0, 5},
2444 HRESULT init_global(script_ctx_t *ctx)
2446 HRESULT hres;
2448 ctx->global_desc.ctx = ctx;
2449 ctx->global_desc.builtin_prop_cnt = sizeof(global_props)/sizeof(*global_props);
2450 ctx->global_desc.builtin_props = global_props;
2452 hres = get_typeinfo(GlobalObj_tid, &ctx->global_desc.typeinfo);
2453 if(FAILED(hres))
2454 return hres;
2456 hres = create_vbdisp(&ctx->global_desc, &ctx->global_obj);
2457 if(FAILED(hres))
2458 return hres;
2460 hres = create_script_disp(ctx, &ctx->script_obj);
2461 if(FAILED(hres))
2462 return hres;
2464 ctx->err_desc.ctx = ctx;
2465 ctx->err_desc.builtin_prop_cnt = sizeof(err_props)/sizeof(*err_props);
2466 ctx->err_desc.builtin_props = err_props;
2468 hres = get_typeinfo(ErrObj_tid, &ctx->err_desc.typeinfo);
2469 if(FAILED(hres))
2470 return hres;
2472 return create_vbdisp(&ctx->err_desc, &ctx->err_obj);