diff options
-rw-r--r-- | fxjse/context.h | 2 | ||||
-rw-r--r-- | fxjse/include/cfxjse_value.h | 103 | ||||
-rw-r--r-- | fxjse/value.cpp | 47 |
3 files changed, 76 insertions, 76 deletions
diff --git a/fxjse/context.h b/fxjse/context.h index 91356a09b8..869fbfb1fb 100644 --- a/fxjse/context.h +++ b/fxjse/context.h @@ -26,7 +26,7 @@ class CFXJSE_Context { CFXJSE_HostObject* lpGlobalObject = nullptr); ~CFXJSE_Context(); - V8_INLINE v8::Isolate* GetRuntime(void) { return m_pIsolate; } + v8::Isolate* GetRuntime(void) { return m_pIsolate; } std::unique_ptr<CFXJSE_Value> GetGlobalObject(); void EnableCompatibleMode(); FX_BOOL ExecuteScript(const FX_CHAR* szScript, diff --git a/fxjse/include/cfxjse_value.h b/fxjse/include/cfxjse_value.h index e01f64cfff..74b4bab2e2 100644 --- a/fxjse/include/cfxjse_value.h +++ b/fxjse/include/cfxjse_value.h @@ -9,51 +9,11 @@ #include "fxjse/scope_inline.h" -V8_INLINE static double FXJSE_ftod(FX_FLOAT fNumber) { - if (sizeof(FX_FLOAT) != 4) { - ASSERT(FALSE); - return fNumber; - } - - uint32_t nFloatBits = (uint32_t&)fNumber; - uint8_t nExponent = (uint8_t)(nFloatBits >> 16 >> 7); - if (nExponent == 0 || nExponent == 255) - return fNumber; - - int8_t nErrExp = nExponent - 127 - 23; - if (nErrExp >= 0) - return fNumber; - - double dwError = pow(2.0, nErrExp), dwErrorHalf = dwError / 2; - double dNumber = fNumber, dNumberAbs = fabs(fNumber); - double dNumberAbsMin = dNumberAbs - dwErrorHalf, - dNumberAbsMax = dNumberAbs + dwErrorHalf; - int32_t iErrPos = 0; - if (floor(dNumberAbsMin) == floor(dNumberAbsMax)) { - dNumberAbsMin = fmod(dNumberAbsMin, 1.0); - dNumberAbsMax = fmod(dNumberAbsMax, 1.0); - int32_t iErrPosMin = 1, iErrPosMax = 38; - do { - int32_t iMid = (iErrPosMin + iErrPosMax) / 2; - double dPow = pow(10.0, iMid); - if (floor(dNumberAbsMin * dPow) == floor(dNumberAbsMax * dPow)) { - iErrPosMin = iMid + 1; - } else { - iErrPosMax = iMid; - } - } while (iErrPosMin < iErrPosMax); - iErrPos = iErrPosMax; - } - double dPow = pow(10.0, iErrPos); - return fNumber < 0 ? ceil(dNumber * dPow - 0.5) / dPow - : floor(dNumber * dPow + 0.5) / dPow; -} - class CFXJSE_Value { public: CFXJSE_Value(v8::Isolate* pIsolate) : m_pIsolate(pIsolate) {} - V8_INLINE FX_BOOL IsUndefined() const { + FX_BOOL IsUndefined() const { if (m_hValue.IsEmpty()) { return FALSE; } @@ -62,7 +22,7 @@ class CFXJSE_Value { v8::Local<v8::Value>::New(m_pIsolate, m_hValue); return hValue->IsUndefined(); } - V8_INLINE FX_BOOL IsNull() const { + FX_BOOL IsNull() const { if (m_hValue.IsEmpty()) { return FALSE; } @@ -71,7 +31,7 @@ class CFXJSE_Value { v8::Local<v8::Value>::New(m_pIsolate, m_hValue); return hValue->IsNull(); } - V8_INLINE FX_BOOL IsBoolean() const { + FX_BOOL IsBoolean() const { if (m_hValue.IsEmpty()) { return FALSE; } @@ -80,7 +40,7 @@ class CFXJSE_Value { v8::Local<v8::Value>::New(m_pIsolate, m_hValue); return hValue->IsBoolean(); } - V8_INLINE FX_BOOL IsString() const { + FX_BOOL IsString() const { if (m_hValue.IsEmpty()) { return FALSE; } @@ -89,7 +49,7 @@ class CFXJSE_Value { v8::Local<v8::Value>::New(m_pIsolate, m_hValue); return hValue->IsString(); } - V8_INLINE FX_BOOL IsNumber() const { + FX_BOOL IsNumber() const { if (m_hValue.IsEmpty()) { return FALSE; } @@ -98,7 +58,7 @@ class CFXJSE_Value { v8::Local<v8::Value>::New(m_pIsolate, m_hValue); return hValue->IsNumber(); } - V8_INLINE FX_BOOL IsInteger() const { + FX_BOOL IsInteger() const { if (m_hValue.IsEmpty()) { return FALSE; } @@ -107,7 +67,7 @@ class CFXJSE_Value { v8::Local<v8::Value>::New(m_pIsolate, m_hValue); return hValue->IsInt32(); } - V8_INLINE FX_BOOL IsObject() const { + FX_BOOL IsObject() const { if (m_hValue.IsEmpty()) { return FALSE; } @@ -116,7 +76,7 @@ class CFXJSE_Value { v8::Local<v8::Value>::New(m_pIsolate, m_hValue); return hValue->IsObject(); } - V8_INLINE FX_BOOL IsArray() const { + FX_BOOL IsArray() const { if (m_hValue.IsEmpty()) { return FALSE; } @@ -125,7 +85,7 @@ class CFXJSE_Value { v8::Local<v8::Value>::New(m_pIsolate, m_hValue); return hValue->IsArray(); } - V8_INLINE FX_BOOL IsFunction() const { + FX_BOOL IsFunction() const { if (m_hValue.IsEmpty()) { return FALSE; } @@ -134,7 +94,7 @@ class CFXJSE_Value { v8::Local<v8::Value>::New(m_pIsolate, m_hValue); return hValue->IsFunction(); } - V8_INLINE FX_BOOL IsDate() const { + FX_BOOL IsDate() const { if (m_hValue.IsEmpty()) { return FALSE; } @@ -144,35 +104,35 @@ class CFXJSE_Value { return hValue->IsDate(); } - V8_INLINE FX_BOOL ToBoolean() const { + FX_BOOL ToBoolean() const { ASSERT(!m_hValue.IsEmpty()); CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue); return static_cast<FX_BOOL>(hValue->BooleanValue()); } - V8_INLINE FX_FLOAT ToFloat() const { + FX_FLOAT ToFloat() const { ASSERT(!m_hValue.IsEmpty()); CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue); return static_cast<FX_FLOAT>(hValue->NumberValue()); } - V8_INLINE double ToDouble() const { + double ToDouble() const { ASSERT(!m_hValue.IsEmpty()); CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue); return static_cast<double>(hValue->NumberValue()); } - V8_INLINE int32_t ToInteger() const { + int32_t ToInteger() const { ASSERT(!m_hValue.IsEmpty()); CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue); return static_cast<int32_t>(hValue->NumberValue()); } - V8_INLINE CFX_ByteString ToString() const { + CFX_ByteString ToString() const { ASSERT(!m_hValue.IsEmpty()); CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); v8::Local<v8::Value> hValue = @@ -181,51 +141,46 @@ class CFXJSE_Value { v8::String::Utf8Value hStringVal(hString); return CFX_ByteString(*hStringVal); } - V8_INLINE CFX_WideString ToWideString() const { + CFX_WideString ToWideString() const { return CFX_WideString::FromUTF8(ToString().AsStringC()); } CFXJSE_HostObject* ToHostObject(CFXJSE_Class* lpClass) const; - V8_INLINE void SetUndefined() { + void SetUndefined() { CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); v8::Local<v8::Value> hValue = v8::Undefined(m_pIsolate); m_hValue.Reset(m_pIsolate, hValue); } - V8_INLINE void SetNull() { + void SetNull() { CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); v8::Local<v8::Value> hValue = v8::Null(m_pIsolate); m_hValue.Reset(m_pIsolate, hValue); } - V8_INLINE void SetBoolean(FX_BOOL bBoolean) { + void SetBoolean(FX_BOOL bBoolean) { CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); v8::Local<v8::Value> hValue = v8::Boolean::New(m_pIsolate, bBoolean != FALSE); m_hValue.Reset(m_pIsolate, hValue); } - V8_INLINE void SetInteger(int32_t nInteger) { + void SetInteger(int32_t nInteger) { CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); v8::Local<v8::Value> hValue = v8::Integer::New(m_pIsolate, nInteger); m_hValue.Reset(m_pIsolate, hValue); } - V8_INLINE void SetDouble(double dDouble) { + void SetDouble(double dDouble) { CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); v8::Local<v8::Value> hValue = v8::Number::New(m_pIsolate, dDouble); m_hValue.Reset(m_pIsolate, hValue); } - V8_INLINE void SetString(const CFX_ByteStringC& szString) { + void SetString(const CFX_ByteStringC& szString) { CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); v8::Local<v8::Value> hValue = v8::String::NewFromUtf8( m_pIsolate, reinterpret_cast<const char*>(szString.raw_str()), v8::String::kNormalString, szString.GetLength()); m_hValue.Reset(m_pIsolate, hValue); } - V8_INLINE void SetFloat(FX_FLOAT fFloat) { - CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); - v8::Local<v8::Value> pValue = - v8::Number::New(m_pIsolate, FXJSE_ftod(fFloat)); - m_hValue.Reset(m_pIsolate, pValue); - } - V8_INLINE void SetJSObject() { + void SetFloat(FX_FLOAT fFloat); + void SetJSObject() { CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); v8::Local<v8::Value> hValue = v8::Object::New(m_pIsolate); m_hValue.Reset(m_pIsolate, hValue); @@ -253,14 +208,12 @@ class CFXJSE_Value { uint32_t nArgCount, CFXJSE_Value** lpArgs); - V8_INLINE v8::Isolate* GetIsolate() const { return m_pIsolate; } - V8_INLINE const v8::Global<v8::Value>& DirectGetValue() const { - return m_hValue; - } - V8_INLINE void ForceSetValue(v8::Local<v8::Value> hValue) { + v8::Isolate* GetIsolate() const { return m_pIsolate; } + const v8::Global<v8::Value>& DirectGetValue() const { return m_hValue; } + void ForceSetValue(v8::Local<v8::Value> hValue) { m_hValue.Reset(m_pIsolate, hValue); } - V8_INLINE void Assign(const CFXJSE_Value* lpValue) { + void Assign(const CFXJSE_Value* lpValue) { ASSERT(lpValue); if (lpValue) { m_hValue.Reset(m_pIsolate, lpValue->m_hValue); diff --git a/fxjse/value.cpp b/fxjse/value.cpp index a49ee312f3..743169dd7b 100644 --- a/fxjse/value.cpp +++ b/fxjse/value.cpp @@ -11,6 +11,47 @@ #include "fxjse/context.h" #include "fxjse/include/cfxjse_class.h" +namespace { + +double FXJSE_ftod(FX_FLOAT fNumber) { + static_assert(sizeof(FX_FLOAT) == 4, "FX_FLOAT of incorrect size"); + + uint32_t nFloatBits = (uint32_t&)fNumber; + uint8_t nExponent = (uint8_t)(nFloatBits >> 23); + if (nExponent == 0 || nExponent == 255) + return fNumber; + + int8_t nErrExp = nExponent - 150; + if (nErrExp >= 0) + return fNumber; + + double dwError = pow(2.0, nErrExp), dwErrorHalf = dwError / 2; + double dNumber = fNumber, dNumberAbs = fabs(fNumber); + double dNumberAbsMin = dNumberAbs - dwErrorHalf, + dNumberAbsMax = dNumberAbs + dwErrorHalf; + int32_t iErrPos = 0; + if (floor(dNumberAbsMin) == floor(dNumberAbsMax)) { + dNumberAbsMin = fmod(dNumberAbsMin, 1.0); + dNumberAbsMax = fmod(dNumberAbsMax, 1.0); + int32_t iErrPosMin = 1, iErrPosMax = 38; + do { + int32_t iMid = (iErrPosMin + iErrPosMax) / 2; + double dPow = pow(10.0, iMid); + if (floor(dNumberAbsMin * dPow) == floor(dNumberAbsMax * dPow)) { + iErrPosMin = iMid + 1; + } else { + iErrPosMax = iMid; + } + } while (iErrPosMin < iErrPosMax); + iErrPos = iErrPosMax; + } + double dPow = pow(10.0, iErrPos); + return fNumber < 0 ? ceil(dNumber * dPow - 0.5) / dPow + : floor(dNumber * dPow + 0.5) / dPow; +} + +} // namespace + void FXJSE_ThrowMessage(const CFX_ByteStringC& utf8Message) { v8::Isolate* pIsolate = v8::Isolate::GetCurrent(); ASSERT(pIsolate); @@ -77,6 +118,12 @@ void CFXJSE_Value::SetDate(double dDouble) { m_hValue.Reset(m_pIsolate, hDate); } +void CFXJSE_Value::SetFloat(FX_FLOAT fFloat) { + CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate); + v8::Local<v8::Value> pValue = v8::Number::New(m_pIsolate, FXJSE_ftod(fFloat)); + m_hValue.Reset(m_pIsolate, pValue); +} + FX_BOOL CFXJSE_Value::SetObjectProperty(const CFX_ByteStringC& szPropName, CFXJSE_Value* lpPropValue) { ASSERT(lpPropValue); |