vbscript: Added Round implementation.

This commit is contained in:
Jacek Caban 2012-09-26 14:36:01 +02:00 committed by Alexandre Julliard
parent 5accb12c12
commit 5c211059a0
2 changed files with 84 additions and 2 deletions

View File

@ -17,6 +17,7 @@
*/ */
#include <assert.h> #include <assert.h>
#include <math.h>
#include "vbscript.h" #include "vbscript.h"
#include "vbscript_defs.h" #include "vbscript_defs.h"
@ -98,6 +99,16 @@ static HRESULT return_int(VARIANT *res, int val)
return S_OK; return S_OK;
} }
static inline HRESULT return_double(VARIANT *res, double val)
{
if(res) {
V_VT(res) = VT_R8;
V_R8(res) = val;
}
return S_OK;
}
static inline HRESULT return_null(VARIANT *res) static inline HRESULT return_null(VARIANT *res)
{ {
if(res) if(res)
@ -122,6 +133,40 @@ static HRESULT to_int(VARIANT *v, int *ret)
return S_OK; return S_OK;
} }
static HRESULT to_double(VARIANT *v, double *ret)
{
switch(V_VT(v)) {
case VT_I2:
*ret = V_I2(v);
break;
case VT_I4:
*ret = V_I4(v);
break;
case VT_R4:
*ret = V_R4(v);
break;
case VT_R8:
*ret = V_R8(v);
break;
case VT_BSTR: {
VARIANT dst;
HRESULT hres;
V_VT(&dst) = VT_EMPTY;
hres = VariantChangeType(&dst, v, VARIANT_LOCALBOOL, VT_R8);
if(FAILED(hres))
return hres;
*ret = V_R8(&dst);
break;
}
default:
FIXME("arg %s not supported\n", debugstr_variant(v));
return E_NOTIMPL;
}
return S_OK;
}
static HRESULT to_string(VARIANT *v, BSTR *ret) static HRESULT to_string(VARIANT *v, BSTR *ret)
{ {
VARIANT dst; VARIANT dst;
@ -1110,8 +1155,30 @@ static HRESULT Global_MonthName(vbdisp_t *This, VARIANT *arg, unsigned args_cnt,
static HRESULT Global_Round(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res) static HRESULT Global_Round(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
{ {
FIXME("\n"); double n;
return E_NOTIMPL; HRESULT hres;
TRACE("%s\n", debugstr_variant(arg));
if(!res)
return S_OK;
switch(V_VT(arg)) {
case VT_I2:
case VT_I4:
case VT_BOOL:
*res = *arg;
return S_OK;
case VT_R8:
n = V_R8(arg);
break;
default:
hres = to_double(arg, &n);
if(FAILED(hres))
return hres;
}
return return_double(res, round(n));
} }
static HRESULT Global_Escape(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res) static HRESULT Global_Escape(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)

View File

@ -183,4 +183,19 @@ Call ok(Space(1) = " ", "Space(1) = " & Space(1) & """")
Call ok(Space(0) = "", "Space(0) = " & Space(0) & """") Call ok(Space(0) = "", "Space(0) = " & Space(0) & """")
Call ok(Space(5) = " ", "Space(5) = " & Space(5) & """") Call ok(Space(5) = " ", "Space(5) = " & Space(5) & """")
Sub TestRound(val, exval, vt)
Call ok(Round(val) = exval, "Round(" & val & ") = " & Round(val))
Call ok(getVT(Round(val)) = vt, "getVT(Round(" & val & ")) = " & getVT(Round(val)))
End Sub
TestRound 3, 3, "VT_I2"
TestRound 3.3, 3, "VT_R8"
TestRound 3.8, 4, "VT_R8"
TestRound 3.5, 4, "VT_R8"
TestRound -3.3, -3, "VT_R8"
TestRound -3.5, -4, "VT_R8"
TestRound "2", 2, "VT_R8"
TestRound true, true, "VT_BOOL"
TestRound false, false, "VT_BOOL"
Call reportSuccess() Call reportSuccess()