diff --git a/DesktopEditor/doctrenderer/json/json.cpp b/DesktopEditor/doctrenderer/json/json.cpp index 0f38193fca..32d5c07071 100644 --- a/DesktopEditor/doctrenderer/json/json.cpp +++ b/DesktopEditor/doctrenderer/json/json.cpp @@ -2,6 +2,9 @@ #include "json_p.h" #include "json_values.h" +// for working with typed arrays: Alloc() and Free() +#include "js_base.h" + namespace NSJSON { CTypedValue::CTypedValue() : m_type(vtUndefined) @@ -174,9 +177,12 @@ namespace NSJSON int IValue::GetCount() const { - if (m_internal->m_type != CTypedValue::vtArray) + if (m_internal->m_type == CTypedValue::vtArray) + return static_cast(m_internal->m_value.get())->getCount(); + else if (m_internal->m_type == CTypedValue::vtTypedArray) + return static_cast(m_internal->m_value.get())->getCount(); + else return 0; - return static_cast(m_internal->m_value.get())->getCount(); } const CValueRef IValue::Get(int index) const @@ -319,19 +325,20 @@ namespace NSJSON CValue CValue::CreateTypedArray(BYTE* data, int count, bool isExternalize) { - // TODO: - return CValue(); + CValue ret; + ret.m_internal->m_value = std::make_shared(data, count, isExternalize); + ret.m_internal->m_type = CTypedValue::vtTypedArray; + return ret; } BYTE* CValue::AllocTypedArray(size_t size) { - // TODO: - return nullptr; + return NSJSBase::NSAllocator::Alloc(size); } void CValue::FreeTypedArray(BYTE* data, size_t size) { - // TODO: + NSJSBase::NSAllocator::Free(data, size); } CValue CValue::CreateObject() diff --git a/DesktopEditor/doctrenderer/json/json_values.cpp b/DesktopEditor/doctrenderer/json/json_values.cpp index acb92da57c..eaab4b5963 100644 --- a/DesktopEditor/doctrenderer/json/json_values.cpp +++ b/DesktopEditor/doctrenderer/json/json_values.cpp @@ -224,13 +224,16 @@ namespace NSJSON return m_values[index]; } - CTypedArray::CTypedArray(BYTE* data, int len) : m_data(data), m_len(len) + CTypedArray::CTypedArray(BYTE* data, int len, bool isExternalize) : m_data(data), m_len(len), m_isExternalize(isExternalize) { } CTypedArray::~CTypedArray() { - // TODO: + if (!m_isExternalize) + { + CValue::FreeTypedArray(m_data, m_len); + } } BYTE* CTypedArray::getData() diff --git a/DesktopEditor/doctrenderer/json/json_values.h b/DesktopEditor/doctrenderer/json/json_values.h index d9d5a58ba8..d7d378d875 100644 --- a/DesktopEditor/doctrenderer/json/json_values.h +++ b/DesktopEditor/doctrenderer/json/json_values.h @@ -96,7 +96,7 @@ namespace NSJSON class CTypedArray : public IBaseValue { public: - CTypedArray(BYTE* data = nullptr, int len = 0); + CTypedArray(BYTE* data, int len, bool isExternalize = true); ~CTypedArray(); public: @@ -106,6 +106,7 @@ namespace NSJSON private: BYTE* m_data; int m_len; + bool m_isExternalize; }; class CObject : public IBaseValue diff --git a/DesktopEditor/doctrenderer/json/serialization.h b/DesktopEditor/doctrenderer/json/serialization.h index 27f0d133f3..c17722d080 100644 --- a/DesktopEditor/doctrenderer/json/serialization.h +++ b/DesktopEditor/doctrenderer/json/serialization.h @@ -2,7 +2,7 @@ #define SERIALIZATION_H_ #include "json.h" -#include "json_values.h" +//#include "json_values.h" #include "../js_internal/js_base.h" #include @@ -40,12 +40,8 @@ namespace NSJSON } else if (value.IsTypedArray()) { - // TODO: - /* - JSSmart jsTypedArr = NSJSBase::CJSContext::createUint8Array(value.GetData(), value.GetCount()); + JSSmart jsTypedArr = NSJSBase::CJSContext::createUint8Array(const_cast(value.GetData()), value.GetCount()); ret = jsTypedArr->toValue(); - */ - ret = NSJSBase::CJSContext::createUndefined(); } else { diff --git a/DesktopEditor/doctrenderer/test/json/main.cpp b/DesktopEditor/doctrenderer/test/json/main.cpp index 0073b53a9a..30124e2d8d 100644 --- a/DesktopEditor/doctrenderer/test/json/main.cpp +++ b/DesktopEditor/doctrenderer/test/json/main.cpp @@ -80,15 +80,14 @@ int main() numbers[3] = innerArray; textPr["numbers"] = numbers; // create typed array - /* - BYTE* pData = new BYTE[4]; + BYTE* pData = CValue::AllocTypedArray(4); pData[0] = 11; pData[1] = 23; pData[2] = 58; pData[3] = 13; // add typed array - ... - */ + CValue typedArr = CValue::CreateTypedArray(pData, 4, false); + textPr["typedArr"] = typedArr; // convert to JS JSSmart jsObj = toJS(textPr)->toObject();