CVE-2023-30188: Loop with Unreachable Exit Condition ('Infinite Loop') (CWE-835)
value: Memory Exhaustion vulnerability in ONLYOFFICE Document Server 4.0.3 through 7.3.2 allows remote attackers to cause a denial of service via crafted JavaScript file.
CWE description: The product contains an iteration or loop with an exit condition that cannot be reached, i.e., an infinite loop.
CVSS v3 base score: 7.5
Published: 2023-08-14T13:15Z
Repository: https://github.com/ONLYOFFICE/core
Language: C++
Files changed: DesktopEditor/doctrenderer/docbuilder_p.cpp, DesktopEditor/doctrenderer/docbuilder_p.h, DesktopEditor/doctrenderer/embed/jsc/jsc_NativeControl.mm, DesktopEditor/doctrenderer/embed/v8/v8_NativeControl.cpp, DesktopEditor/doctrenderer/js_internal/v8/v8_base.cpp, DesktopEditor/doctrenderer/js_internal/v8/v8_base.h
Commit date: 2023-02-04 23:49:41 +0300
Commit message: Fix bug 60433

Diff: diff --git a/DesktopEditor/doctrenderer/docbuilder_p.cpp b/DesktopEditor/doctrenderer/docbuilder_p.cpp
index 7631a34af0..4804c61a00 100644
--- a/DesktopEditor/doctrenderer/docbuilder_p.cpp
+++ b/DesktopEditor/doctrenderer/docbuilder_p.cpp
@@ -74,22 +74,22 @@ CV8RealTimeWorker::~CV8RealTimeWorker()
 
 bool CV8RealTimeWorker::ExecuteCommand(const std::wstring& command, NSDoctRenderer::CDocBuilderValue* retValue)
 {
 	LOGGER_SPEED_START
 
-			if (retValue)
-			retValue->Clear();
+	if (retValue)
+		retValue->Clear();
 
 	std::string commandA = U_TO_UTF8(command);
 	//commandA = "Api." + commandA;
 
 	JSSmart<CJSContextScope> context_scope = m_context->CreateContextScope();
 	JSSmart<CJSTryCatch> try_catch = m_context->GetExceptions();
 
 	LOGGER_SPEED_LAP("compile_command")
 
-			JSSmart<CJSValue> retNativeVal = m_context->runScript(commandA, try_catch);
+	JSSmart<CJSValue> retNativeVal = m_context->runScript(commandA, try_catch);
 	if(try_catch->Check())
 		return false;
 
 	if (retValue)
 	{
@@ -98,11 +98,11 @@ bool CV8RealTimeWorker::ExecuteCommand(const std::wstring& command, NSDoctRender
 		privateRet->m_value = retNativeVal;
 	}
 
 	LOGGER_SPEED_LAP("run_command")
 
-			return true;
+	return true;
 }
 
 std::string CV8RealTimeWorker::GetGlobalVariable()
 {
 	std::string commandA = "JSON.stringify(GlobalVariable);";
@@ -137,22 +137,22 @@ std::wstring CV8RealTimeWorker::GetJSVariable(std::wstring sParam)
 
 bool CV8RealTimeWorker::OpenFile(const std::wstring& sBasePath, const std::wstring& path, const std::string& sString, const std::wstring& sCachePath, CV8Params* pParams)
 {
 	LOGGER_SPEED_START
 
-			JSSmart<CJSContextScope> context_scope = m_context->CreateContextScope();
+	JSSmart<CJSContextScope> context_scope = m_context->CreateContextScope();
 	JSSmart<CJSTryCatch>         try_catch = m_context->GetExceptions();
 
 	LOGGER_SPEED_LAP("compile");
 
 	m_context->runScript(sString, try_catch, sCachePath);
 	if(try_catch->Check())
 		return false;
 
 	LOGGER_SPEED_LAP("run")
 
-			if (true)
+	if (true)
 	{
 		std::string sArg = m_sUtf8ArgumentJSON;
 		if (sArg.empty())
 			sArg = "{}";
 		NSStringUtils::string_replaceA(sArg, "\\", "\\\\");
@@ -247,11 +247,11 @@ bool CV8RealTimeWorker::OpenFile(const std::wstring& sBasePath, const std::wstri
 	if (!bIsBreak)
 		bIsBreak = !this->ExecuteCommand(L"Api.asc_SetSilentMode(true);");
 
 	LOGGER_SPEED_LAP("open")
 
-			return !bIsBreak;
+	return !bIsBreak;
 }
 
 bool CV8RealTimeWorker::SaveFileWithChanges(int type, const std::wstring& _path, const std::wstring& sJsonParams)
 {
 	NSDoctRenderer::DoctRendererFormat::FormatFile _formatDst = NSDoctRenderer::DoctRendererFormat::DOCT;
diff --git a/DesktopEditor/doctrenderer/docbuilder_p.h b/DesktopEditor/doctrenderer/docbuilder_p.h
index 3b3866c2d8..48c833976e 100644
--- a/DesktopEditor/doctrenderer/docbuilder_p.h
+++ b/DesktopEditor/doctrenderer/docbuilder_p.h
@@ -888,11 +888,11 @@ namespace NSDoctRenderer
 		{
 			Init();
 
 			LOGGER_SPEED_START
 
-					CheckFileDir();
+			CheckFileDir();
 			NSDirectory::CreateDirectory(m_sFileDir + L"/changes");
 
 			std::wstring sExtCopy = GetFileCopyExt(path);
 			std::wstring sFileCopy = m_sFileDir + L"/origin." + sExtCopy;
 			MoveFileOpen(path, sFileCopy);
@@ -910,12 +910,12 @@ namespace NSDoctRenderer
 
 			int nReturnCode = ConvertToInternalFormat(m_sFileDir, sFileCopy, params);
 
 			LOGGER_SPEED_LAP("open_convert")
 
-					if (0 == nReturnCode)
-					return 0;
+			if (0 == nReturnCode)
+				return 0;
 
 			NSDirectory::DeleteDirectory(m_sFileDir);
 			m_sFileDir = L"";
 			m_nFileType = -1;
 
diff --git a/DesktopEditor/doctrenderer/embed/jsc/jsc_NativeControl.mm b/DesktopEditor/doctrenderer/embed/jsc/jsc_NativeControl.mm
index 2b8cbdf265..9ee9ce0c32 100644
--- a/DesktopEditor/doctrenderer/embed/jsc/jsc_NativeControl.mm
+++ b/DesktopEditor/doctrenderer/embed/jsc/jsc_NativeControl.mm
@@ -12,12 +12,12 @@ -(JSValue*) GetFontsDirectory;
 -(JSValue*) GetFileString : (JSValue*)file;
 -(JSValue*) GetEditorType;
 -(JSValue*) CheckNextChange;
 -(JSValue*) GetCountChanges;
 -(JSValue*) GetChangesFile : (JSValue*)index;
--(JSValue*) Save_AllocNative : (JSValue*)len;
--(JSValue*) Save_ReAllocNative : (JSValue*)pos : (JSValue*)len;
+//-(JSValue*) Save_AllocNative : (JSValue*)len;
+//-(JSValue*) Save_ReAllocNative : (JSValue*)pos : (JSValue*)len;
 -(JSValue*) Save_End : (JSValue*)pos : (JSValue*)len;
 -(JSValue*) AddImageInChanges : (JSValue*)img;
 -(JSValue*) ConsoleLog : (JSValue*)message;
 -(JSValue*) SaveChanges : (JSValue*)param : (JSValue*)delete_index : (JSValue*)count;
 -(JSValue*) ZipOpen : (JSValue*)name;
@@ -52,12 +52,12 @@ @implementation CJSCNativeControl
 FUNCTION_WRAPPER_JS_1(GetFileString, GetFileString)
 FUNCTION_WRAPPER_JS(GetEditorType, GetEditorType)
 FUNCTION_WRAPPER_JS(CheckNextChange, CheckNextChange)
 FUNCTION_WRAPPER_JS(GetCountChanges, GetCountChanges)
 FUNCTION_WRAPPER_JS_1(GetChangesFile, GetChangesFile)
-FUNCTION_WRAPPER_JS_1(Save_AllocNative, Save_AllocNative)
-FUNCTION_WRAPPER_JS_2(Save_ReAllocNative, Save_ReAllocNative)
+//FUNCTION_WRAPPER_JS_1(Save_AllocNative, Save_AllocNative)
+//FUNCTION_WRAPPER_JS_2(Save_ReAllocNative, Save_ReAllocNative)
 FUNCTION_WRAPPER_JS_2(Save_End, Save_End)
 FUNCTION_WRAPPER_JS_1(AddImageInChanges, AddImageInChanges)
 FUNCTION_WRAPPER_JS_1(ConsoleLog, ConsoleLog)
 FUNCTION_WRAPPER_JS_3(SaveChanges, SaveChanges)
 FUNCTION_WRAPPER_JS_1(ZipOpen, zipOpenFile)
diff --git a/DesktopEditor/doctrenderer/embed/v8/v8_NativeControl.cpp b/DesktopEditor/doctrenderer/embed/v8/v8_NativeControl.cpp
index 3d18f5c511..bc25fddeb4 100644
--- a/DesktopEditor/doctrenderer/embed/v8/v8_NativeControl.cpp
+++ b/DesktopEditor/doctrenderer/embed/v8/v8_NativeControl.cpp
@@ -1,149 +1,161 @@
 #include "../NativeControlEmbed.h"
 #include "../../js_internal/v8/v8_base.h"
 
 namespace NSNativeControl
 {
-    #define CURRENTWRAPPER CNativeControlEmbed
-
-    FUNCTION_WRAPPER_V8(_GetFilePath, GetFilePath)
-    FUNCTION_WRAPPER_V8_1(_SetFilePath, SetFilePath)
-    FUNCTION_WRAPPER_V8(_GetFileId, GetFileId)
-    FUNCTION_WRAPPER_V8_1(_SetFileId, SetFileId)
-    FUNCTION_WRAPPER_V8_1(_GetFileArrayBuffer, GetFileBinary)
-    FUNCTION_WRAPPER_V8_1(_GetFontArrayBuffer, GetFontBinary)
-    FUNCTION_WRAPPER_V8(_GetFontsDirectory, GetFontsDirectory)
-    FUNCTION_WRAPPER_V8_1(_GetFileString, GetFileString)
-    FUNCTION_WRAPPER_V8(_GetEditorType, GetEditorType)
-    FUNCTION_WRAPPER_V8(_CheckNextChange, CheckNextChange)
-    FUNCTION_WRAPPER_V8(_GetChangesCount, GetCountChanges)
-    FUNCTION_WRAPPER_V8_1(_GetChangesFile, GetChangesFile)
-    FUNCTION_WRAPPER_V8_1(_Save_AllocNative, Save_AllocNative)
-    FUNCTION_WRAPPER_V8_2(_Save_ReAllocNative, Save_ReAllocNative)
-    FUNCTION_WRAPPER_V8_2(_Save_End, Save_End)
-    FUNCTION_WRAPPER_V8_1(_AddImageInChanges, AddImageInChanges)
-    FUNCTION_WRAPPER_V8_1(_ConsoleLog, ConsoleLog)
-    FUNCTION_WRAPPER_V8_3(_SaveChanges, SaveChanges)
-    FUNCTION_WRAPPER_V8_1(_zipOpenFile, zipOpenFile)
-    FUNCTION_WRAPPER_V8_1(_zipOpenFileBase64, zipOpenFileBase64)
-    FUNCTION_WRAPPER_V8_1(_zipGetFileAsString, zipGetFileAsString)
-    FUNCTION_WRAPPER_V8_1(_zipGetFileAsBinary, zipGetFileAsBinary)
-    FUNCTION_WRAPPER_V8(_zipCloseFile, zipCloseFile)
-    FUNCTION_WRAPPER_V8_1(_GetImageUrl, GetImageUrl)
-    FUNCTION_WRAPPER_V8_1(_GetImageOriginalSize, GetImageOriginalSize)
-    FUNCTION_WRAPPER_V8(_GetImagesPath, GetImagesPath)
-
-    v8::Handle<v8::ObjectTemplate> CreateNativeControlTemplate(v8::Isolate* isolate)
-    {
-        v8::EscapableHandleScope handle_scope(isolate);
-
-        v8::Local<v8::ObjectTemplate> result = v8::ObjectTemplate::New(V8IsolateOneArg);
-        result->SetInternalFieldCount(1);
-
-        NSV8Objects::Template_Set(result, "SetFilePath",        _SetFilePath);
-        NSV8Objects::Template_Set(result, "GetFilePath",        _GetFilePath);
-        NSV8Objects::Template_Set(result, "SetFileId",          _SetFileId);
-        NSV8Objects::Template_Set(result, "GetFileId",          _GetFileId);
-        NSV8Objects::Template_Set(result, "GetFileBinary",      _GetFileArrayBuffer);
-        NSV8Objects::Template_Set(result, "GetFontBinary",      _GetFontArrayBuffer);
-        NSV8Objects::Template_Set(result, "GetFontsDirectory",  _GetFontsDirectory);
-        NSV8Objects::Template_Set(result, "GetFileString",      _GetFileString);
-        NSV8Objects::Template_Set(result, "GetEditorType",      _GetEditorType);
-        NSV8Objects::Template_Set(result, "CheckNextChange",    _CheckNextChange);
-        NSV8Objects::Template_Set(result, "GetCountChanges",    _GetChangesCount);
-        NSV8Objects::Template_Set(result, "GetChangesFile",     _GetChangesFile);
-        NSV8Objects::Template_Set(result, "Save_AllocNative",   _Save_AllocNative);
-        NSV8Objects::Template_Set(result, "Save_ReAllocNative", _Save_ReAllocNative);
-        NSV8Objects::Template_Set(result, "Save_End",           _Save_End);
-        NSV8Objects::Template_Set(result, "AddImageInChanges",  _AddImageInChanges);
-        NSV8Objects::Template_Set(result, "ConsoleLog",         _ConsoleLog);
-        NSV8Objects::Template_Set(result, "SaveChanges",        _SaveChanges);
-        NSV8Objects::Template_Set(result, "ZipOpen",            _zipOpenFile);
-        NSV8Objects::Template_Set(result, "ZipOpenBase64",      _zipOpenFileBase64);
-        NSV8Objects::Template_Set(result, "ZipFileAsString",    _zipGetFileAsString);
-        NSV8Objects::Template_Set(result, "ZipFileAsBinary",    _zipGetFileAsBinary);
-        NSV8Objects::Template_Set(result, "ZipClose",           _zipCloseFile);
-        NSV8Objects::Template_Set(result, "getImageUrl",        _GetImageUrl);
-        NSV8Objects::Template_Set(result, "getImagesDirectory", _GetImagesPath);
-        NSV8Objects::Template_Set(result, "GetImageOriginalSize", _GetImageOriginalSize);
-
-        return handle_scope.Escape(result);
-    }
-
-    // Без SaveChanges
-    v8::Handle<v8::ObjectTemplate> CreateNativeControlTemplateBuilder(v8::Isolate* isolate)
-    {
-        v8::EscapableHandleScope handle_scope(isolate);
-
-        v8::Local<v8::ObjectTemplate> result = v8::ObjectTemplate::New(V8IsolateOneArg);
-        result->SetInternalFieldCount(1);
-
-        NSV8Objects::Template_Set(result, "SetFilePath",        _SetFilePath);
-        NSV8Objects::Template_Set(result, "GetFilePath",        _GetFilePath);
-        NSV8Objects::Template_Set(result, "SetFileId",          _SetFileId);
-        NSV8Objects::Template_Set(result, "GetFileId",          _GetFileId);
-        NSV8Objects::Template_Set(result, "GetFileBinary",      _GetFileArrayBuffer);
-        NSV8Objects::Template_Set(result, "GetFontBinary",      _GetFontArrayBuffer);
-        NSV8Objects::Template_Set(result, "GetFontsDirectory",  _GetFontsDirectory);
-        NSV8Objects::Template_Set(result, "GetFileString",      _GetFileString);
-        NSV8Objects::Template_Set(result, "GetEditorType",      _GetEditorType);
-        NSV8Objects::Template_Set(result, "CheckNextChange",    _CheckNextChange);
-        NSV8Objects::Template_Set(result, "GetCountChanges",    _GetChangesCount);
-        NSV8Objects::Template_Set(result, "GetChangesFile",     _GetChangesFile);
-        NSV8Objects::Template_Set(result, "Save_AllocNative",   _Save_AllocNative);
-        NSV8Objects::Template_Set(result, "Save_ReAllocNative", _Save_ReAllocNative);
-        NSV8Objects::Template_Set(result, "Save_End",           _Save_End);
-        NSV8Objects::Template_Set(result, "AddImageInChanges",  _AddImageInChanges);
-        NSV8Objects::Template_Set(result, "ConsoleLog",         _ConsoleLog);
-        NSV8Objects::Template_Set(result, "ZipOpen",            _zipOpenFile);
-        NSV8Objects::Template_Set(result, "ZipOpenBase64",      _zipOpenFileBase64);
-        NSV8Objects::Template_Set(result, "ZipFileAsString",    _zipGetFileAsString);
-        NSV8Objects::Template_Set(result, "ZipFileAsBinary",    _zipGetFileAsBinary);
-        NSV8Objects::Template_Set(result, "ZipClose",           _zipCloseFile);
-        NSV8Objects::Template_Set(result, "getImageUrl",        _GetImageUrl);
-        NSV8Objects::Template_Set(result, "getImagesDirectory", _GetImagesPath);
-        NSV8Objects::Template_Set(result, "GetImageOriginalSize", _GetImageOriginalSize);
-
-        return handle_scope.Escape(result);
-    }
-
-    void CreateNativeObject(const v8::FunctionCallbackInfo<v8::Value>& args)
-    {
-        v8::Isolate* isolate = args.GetIsolate();
-        v8::HandleScope scope(isolate);
-
-        v8::Handle<v8::ObjectTemplate> NativeObjectTemplate = CreateNativeControlTemplate(isolate);
-        CNativeControlEmbed* pNativeObject = new CNativeControlEmbed();
-
-        v8::Local<v8::Object> obj = NativeObjectTemplate->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
-        obj->SetInternalField(0, v8::External::New(CV8Worker::GetCurrent(), pNativeObject));
-
-        args.GetReturnValue().Set(obj);
-    }
-
-    // Без SaveChanges
-    void CreateNativeObjectBuilder(const v8::FunctionCallbackInfo<v8::Value>& args)
-    {
-        v8::Isolate* isolate = args.GetIsolate();
-        v8::HandleScope scope(isolate);
-
-        v8::Handle<v8::ObjectTemplate> NativeObjectTemplate = CreateNativeControlTemplateBuilder(isolate);
-        CNativeControlEmbed* pNativeObject = new CNativeControlEmbed();
-
-        v8::Local<v8::Object> obj = NativeObjectTemplate->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
-        obj->SetInternalField(0, v8::External::New(CV8Worker::GetCurrent(), pNativeObject));
-
-        args.GetReturnValue().Set(obj);
-    }
+#define CURRENTWRAPPER CNativeControlEmbed
+
+	FUNCTION_WRAPPER_V8(_GetFilePath, GetFilePath)
+	FUNCTION_WRAPPER_V8_1(_SetFilePath, SetFilePath)
+	FUNCTION_WRAPPER_V8(_GetFileId, GetFileId)
+	FUNCTION_WRAPPER_V8_1(_SetFileId, SetFileId)
+	FUNCTION_WRAPPER_V8_1(_GetFileArrayBuffer, GetFileBinary)
+	FUNCTION_WRAPPER_V8_1(_GetFontArrayBuffer, GetFontBinary)
+	FUNCTION_WRAPPER_V8(_GetFontsDirectory, GetFontsDirectory)
+	FUNCTION_WRAPPER_V8_1(_GetFileString, GetFileString)
+	FUNCTION_WRAPPER_V8(_GetEditorType, GetEditorType)
+	FUNCTION_WRAPPER_V8(_CheckNextChange, CheckNextChange)
+	FUNCTION_WRAPPER_V8(_GetChangesCount, GetCountChanges)
+	FUNCTION_WRAPPER_V8_1(_GetChangesFile, GetChangesFile)
+	FUNCTION_WRAPPER_V8_1(_Save_AllocNative, Save_AllocNative)
+	FUNCTION_WRAPPER_V8_2(_Save_ReAllocNative, Save_ReAllocNative)
+	FUNCTION_WRAPPER_V8_2(_Save_End, Save_End)
+	FUNCTION_WRAPPER_V8_1(_AddImageInChanges, AddImageInChanges)
+	FUNCTION_WRAPPER_V8_1(_ConsoleLog, ConsoleLog)
+	FUNCTION_WRAPPER_V8_3(_SaveChanges, SaveChanges)
+	FUNCTION_WRAPPER_V8_1(_zipOpenFile, zipOpenFile)
+	FUNCTION_WRAPPER_V8_1(_zipOpenFileBase64, zipOpenFileBase64)
+	FUNCTION_WRAPPER_V8_1(_zipGetFileAsString, zipGetFileAsString)
+	FUNCTION_WRAPPER_V8_1(_zipGetFileAsBinary, zipGetFileAsBinary)
+	FUNCTION_WRAPPER_V8(_zipCloseFile, zipCloseFile)
+	FUNCTION_WRAPPER_V8_1(_GetImageUrl, GetImageUrl)
+	FUNCTION_WRAPPER_V8_1(_GetImageOriginalSize, GetImageOriginalSize)
+	FUNCTION_WRAPPER_V8(_GetImagesPath, GetImagesPath)
+
+	v8::Handle<v8::ObjectTemplate> CreateNativeControlTemplate(v8::Isolate* isolate)
+	{
+		v8::EscapableHandleScope handle_scope(isolate);
+
+		v8::Local<v8::ObjectTemplate> result = v8::ObjectTemplate::New(V8IsolateOneArg);
+		result->SetInternalFieldCount(1);
+
+		NSV8Objects::Template_Set(result, "SetFilePath",        _SetFilePath);
+		NSV8Objects::Template_Set(result, "GetFilePath",        _GetFilePath);
+		NSV8Objects::Template_Set(result, "SetFileId",          _SetFileId);
+		NSV8Objects::Template_Set(result, "GetFileId",          _GetFileId);
+		NSV8Objects::Template_Set(result, "GetFileBinary",      _GetFileArrayBuffer);
+		NSV8Objects::Template_Set(result, "GetFontBinary",      _GetFontArrayBuffer);
+		NSV8Objects::Template_Set(result, "GetFontsDirectory",  _GetFontsDirectory);
+		NSV8Objects::Template_Set(result, "GetFileString",      _GetFileString);
+		NSV8Objects::Template_Set(result, "GetEditorType",      _GetEditorType);
+		NSV8Objects::Template_Set(result, "CheckNextChange",    _CheckNextChange);
+		NSV8Objects::Template_Set(result, "GetCountChanges",    _GetChangesCount);
+		NSV8Objects::Template_Set(result, "GetChangesFile",     _GetChangesFile);
+		//NSV8Objects::Template_Set(result, "Save_AllocNative",   _Save_AllocNative);
+		//NSV8Objects::Template_Set(result, "Save_ReAllocNative", _Save_ReAllocNative);
+		NSV8Objects::Template_Set(result, "Save_End",           _Save_End);
+		NSV8Objects::Template_Set(result, "AddImageInChanges",  _AddImageInChanges);
+		NSV8Objects::Template_Set(result, "ConsoleLog",         _ConsoleLog);
+		NSV8Objects::Template_Set(result, "SaveChanges",        _SaveChanges);
+		NSV8Objects::Template_Set(result, "ZipOpen",            _zipOpenFile);
+		NSV8Objects::Template_Set(result, "ZipOpenBase64",      _zipOpenFileBase64);
+		NSV8Objects::Template_Set(result, "ZipFileAsString",    _zipGetFileAsString);
+		NSV8Objects::Template_Set(result, "ZipFileAsBinary",    _zipGetFileAsBinary);
+		NSV8Objects::Template_Set(result, "ZipClose",           _zipCloseFile);
+		NSV8Objects::Template_Set(result, "getImageUrl",        _GetImageUrl);
+		NSV8Objects::Template_Set(result, "getImagesDirectory", _GetImagesPath);
+		NSV8Objects::Template_Set(result, "GetImageOriginalSize", _GetImageOriginalSize);
+
+		return handle_scope.Escape(result);
+	}
+
+	// Без SaveChanges
+	v8::Handle<v8::ObjectTemplate> CreateNativeControlTemplateBuilder(v8::Isolate* isolate)
+	{
+		v8::EscapableHandleScope handle_scope(isolate);
+
+		v8::Local<v8::ObjectTemplate> result = v8::ObjectTemplate::New(V8IsolateOneArg);
+		result->SetInternalFieldCount(1);
+
+		NSV8Objects::Template_Set(result, "SetFilePath",        _SetFilePath);
+		NSV8Objects::Template_Set(result, "GetFilePath",        _GetFilePath);
+		NSV8Objects::Template_Set(result, "SetFileId",          _SetFileId);
+		NSV8Objects::Template_Set(result, "GetFileId",          _GetFileId);
+		NSV8Objects::Template_Set(result, "GetFileBinary",      _GetFileArrayBuffer);
+		NSV8Objects::Template_Set(result, "GetFontBinary",      _GetFontArrayBuffer);
+		NSV8Objects::Template_Set(result, "GetFontsDirectory",  _GetFontsDirectory);
+		NSV8Objects::Template_Set(result, "GetFileString",      _GetFileString);
+		NSV8Objects::Template_Set(result, "GetEditorType",      _GetEditorType);
+		NSV8Objects::Template_Set(result, "CheckNextChange",    _CheckNextChange);
+		NSV8Objects::Template_Set(result, "GetCountChanges",    _GetChangesCount);
+		NSV8Objects::Template_Set(result, "GetChangesFile",     _GetChangesFile);
+		//NSV8Objects::Template_Set(result, "Save_AllocNative",   _Save_AllocNative);
+		//NSV8Objects::Template_Set(result, "Save_ReAllocNative", _Save_ReAllocNative);
+		NSV8Objects::Template_Set(result, "Save_End",           _Save_End);
+		NSV8Objects::Template_Set(result, "AddImageInChanges",  _AddImageInChanges);
+		NSV8Objects::Template_Set(result, "ConsoleLog",         _ConsoleLog);
+		NSV8Objects::Template_Set(result, "ZipOpen",            _zipOpenFile);
+		NSV8Objects::Template_Set(result, "ZipOpenBase64",      _zipOpenFileBase64);
+		NSV8Objects::Template_Set(result, "ZipFileAsString",    _zipGetFileAsString);
+		NSV8Objects::Template_Set(result, "ZipFileAsBinary",    _zipGetFileAsBinary);
+		NSV8Objects::Template_Set(result, "ZipClose",           _zipCloseFile);
+		NSV8Objects::Template_Set(result, "getImageUrl",        _GetImageUrl);
+		NSV8Objects::Template_Set(result, "getImagesDirectory", _GetImagesPath);
+		NSV8Objects::Template_Set(result, "GetImageOriginalSize", _GetImageOriginalSize);
+
+		return handle_scope.Escape(result);
+	}
+
+	void CreateNativeObject(const v8::FunctionCallbackInfo<v8::Value>& args)
+	{
+		v8::Isolate* isolate = args.GetIsolate();
+		v8::HandleScope scope(isolate);
+
+		if (CIsolateAdditionalData::CheckSingletonType(isolate, CIsolateAdditionalData::iadtSingletonNative))
+		{
+			args.GetReturnValue().Set(v8::Undefined(isolate));
+			return;
+		}
+
+		v8::Handle<v8::ObjectTemplate> NativeObjectTemplate = CreateNativeControlTemplate(isolate);
+		CNativeControlEmbed* pNativeObject = new CNativeControlEmbed();
+
+		v8::Local<v8::Object> obj = NativeObjectTemplate->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
+		obj->SetInternalField(0, v8::External::New(CV8Worker::GetCurrent(), pNativeObject));
+
+		args.GetReturnValue().Set(obj);
+	}
+
+	// Без SaveChanges
+	void CreateNativeObjectBuilder(const v8::FunctionCallbackInfo<v8::Value>& args)
+	{
+		v8::Isolate* isolate = args.GetIsolate();
+		v8::HandleScope scope(isolate);
+
+		if (CIsolateAdditionalData::CheckSingletonType(isolate, CIsolateAdditionalData::iadtSingletonNative))
+		{
+			args.GetReturnValue().Set(v8::Undefined(isolate));
+			return;
+		}
+
+		v8::Handle<v8::ObjectTemplate> NativeObjectTemplate = CreateNativeControlTemplateBuilder(isolate);
+		CNativeControlEmbed* pNativeObject = new CNativeControlEmbed();
+
+		v8::Local<v8::Object> obj = NativeObjectTemplate->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
+		obj->SetInternalField(0, v8::External::New(CV8Worker::GetCurrent(), pNativeObject));
+
+		args.GetReturnValue().Set(obj);
+	}
 }
 
 void CNativeControlEmbed::CreateObjectInContext(const std::string& name, JSSmart<CJSContext> context)
 {
-    v8::Isolate* current = CV8Worker::GetCurrent();
-    context->m_internal->m_global->Set(current, name.c_str(), v8::FunctionTemplate::New(current, NSNativeControl::CreateNativeObject));
+	v8::Isolate* current = CV8Worker::GetCurrent();
+	context->m_internal->m_global->Set(current, name.c_str(), v8::FunctionTemplate::New(current, NSNativeControl::CreateNativeObject));
 }
 
 void CNativeControlEmbed::CreateObjectBuilderInContext(const std::string& name, JSSmart<CJSContext> context)
 {
-    v8::Isolate* current = CV8Worker::GetCurrent();
-    context->m_internal->m_global->Set(current, name.c_str(), v8::FunctionTemplate::New(current, NSNativeControl::CreateNativeObjectBuilder));
+	v8::Isolate* current = CV8Worker::GetCurrent();
+	context->m_internal->m_global->Set(current, name.c_str(), v8::FunctionTemplate::New(current, NSNativeControl::CreateNativeObjectBuilder));
 }
diff --git a/DesktopEditor/doctrenderer/js_internal/v8/v8_base.cpp b/DesktopEditor/doctrenderer/js_internal/v8/v8_base.cpp
index 1e25b12362..ce57d1507c 100644
--- a/DesktopEditor/doctrenderer/js_internal/v8/v8_base.cpp
+++ b/DesktopEditor/doctrenderer/js_internal/v8/v8_base.cpp
@@ -217,10 +217,17 @@ namespace NSJSBase
 	void CJSContext::Dispose()
 	{
 #ifdef V8_INSPECTOR
 		v8_debug::disposeInspector(m_internal->m_context);
 #endif
+		unsigned int nEmbedDataCount = m_internal->m_isolate->GetNumberOfDataSlots();
+		for (unsigned int i = 0; i < nEmbedDataCount; ++i)
+		{
+			CIsolateAdditionalData* pAdditionData = (CIsolateAdditionalData*)m_internal->m_isolate->GetData(i);
+			delete pAdditionData;
+		}
+
 		m_internal->m_isolate->Dispose();
 		m_internal->m_isolate = NULL;
 	}
 
 	void CJSContext::CreateContext()
diff --git a/DesktopEditor/doctrenderer/js_internal/v8/v8_base.h b/DesktopEditor/doctrenderer/js_internal/v8/v8_base.h
index 293886553c..aa73978586 100644
--- a/DesktopEditor/doctrenderer/js_internal/v8/v8_base.h
+++ b/DesktopEditor/doctrenderer/js_internal/v8/v8_base.h
@@ -70,10 +70,42 @@ public:
         free(data);
     }
 };
 #endif
 
+class CIsolateAdditionalData
+{
+public:
+	enum IsolateAdditionlDataType {
+		iadtSingletonNative = 0,
+		iadtUndefined = 255
+	};
+
+	IsolateAdditionlDataType m_eType;
+public:
+	CIsolateAdditionalData(const IsolateAdditionlDataType& type = iadtUndefined) { m_eType = type; }
+	virtual ~CIsolateAdditionalData() {}
+
+	static bool CheckSingletonType(v8::Isolate* isolate, const IsolateAdditionlDataType& type, const bool& isAdd = true)
+	{
+		unsigned int nCount = isolate->GetNumberOfDataSlots();
+		for (unsigned int i = 0; i < nCount; ++i)
+		{
+			CIsolateAdditionalData* pData = (CIsolateAdditionalData*)isolate->GetData(i);
+			if (pData->m_eType == type)
+				return true;
+		}
+
+		if (isAdd)
+		{
+			isolate->SetData(nCount, (void*)(new CIsolateAdditionalData(type)));
+		}
+
+		return false;
+	}
+};
+
 class CV8Initializer
 {
 private:
 #ifdef V8_VERSION_89_PLUS
     std::unique_ptr<v8::Platform> m_platform;

Vulnerable code: 			if (retValue)
			retValue->Clear();
			JSSmart<CJSValue> retNativeVal = m_context->runScript(commandA, try_catch);
			return true;
			JSSmart<CJSContextScope> context_scope = m_context->CreateContextScope();
			if (true)
			return !bIsBreak;
					CheckFileDir();
					if (0 == nReturnCode)
					return 0;
-(JSValue*) Save_AllocNative : (JSValue*)len;
-(JSValue*) Save_ReAllocNative : (JSValue*)pos : (JSValue*)len;
FUNCTION_WRAPPER_JS_1(Save_AllocNative, Save_AllocNative)
FUNCTION_WRAPPER_JS_2(Save_ReAllocNative, Save_ReAllocNative)
    #define CURRENTWRAPPER CNativeControlEmbed

    FUNCTION_WRAPPER_V8(_GetFilePath, GetFilePath)
    FUNCTION_WRAPPER_V8_1(_SetFilePath, SetFilePath)
    FUNCTION_WRAPPER_V8(_GetFileId, GetFileId)
    FUNCTION_WRAPPER_V8_1(_SetFileId, SetFileId)
    FUNCTION_WRAPPER_V8_1(_GetFileArrayBuffer, GetFileBinary)
    FUNCTION_WRAPPER_V8_1(_GetFontArrayBuffer, GetFontBinary)
    FUNCTION_WRAPPER_V8(_GetFontsDirectory, GetFontsDirectory)
    FUNCTION_WRAPPER_V8_1(_GetFileString, GetFileString)
    FUNCTION_WRAPPER_V8(_GetEditorType, GetEditorType)
    FUNCTION_WRAPPER_V8(_CheckNextChange, CheckNextChange)
    FUNCTION_WRAPPER_V8(_GetChangesCount, GetCountChanges)
    FUNCTION_WRAPPER_V8_1(_GetChangesFile, GetChangesFile)
    FUNCTION_WRAPPER_V8_1(_Save_AllocNative, Save_AllocNative)
    FUNCTION_WRAPPER_V8_2(_Save_ReAllocNative, Save_ReAllocNative)
    FUNCTION_WRAPPER_V8_2(_Save_End, Save_End)
    FUNCTION_WRAPPER_V8_1(_AddImageInChanges, AddImageInChanges)
    FUNCTION_WRAPPER_V8_1(_ConsoleLog, ConsoleLog)
    FUNCTION_WRAPPER_V8_3(_SaveChanges, SaveChanges)
    FUNCTION_WRAPPER_V8_1(_zipOpenFile, zipOpenFile)
    FUNCTION_WRAPPER_V8_1(_zipOpenFileBase64, zipOpenFileBase64)
    FUNCTION_WRAPPER_V8_1(_zipGetFileAsString, zipGetFileAsString)
    FUNCTION_WRAPPER_V8_1(_zipGetFileAsBinary, zipGetFileAsBinary)
    FUNCTION_WRAPPER_V8(_zipCloseFile, zipCloseFile)
    FUNCTION_WRAPPER_V8_1(_GetImageUrl, GetImageUrl)
    FUNCTION_WRAPPER_V8_1(_GetImageOriginalSize, GetImageOriginalSize)
    FUNCTION_WRAPPER_V8(_GetImagesPath, GetImagesPath)

    v8::Handle<v8::ObjectTemplate> CreateNativeControlTemplate(v8::Isolate* isolate)
    {
        v8::EscapableHandleScope handle_scope(isolate);

        v8::Local<v8::ObjectTemplate> result = v8::ObjectTemplate::New(V8IsolateOneArg);
        result->SetInternalFieldCount(1);

        NSV8Objects::Template_Set(result, "SetFilePath",        _SetFilePath);
        NSV8Objects::Template_Set(result, "GetFilePath",        _GetFilePath);
        NSV8Objects::Template_Set(result, "SetFileId",          _SetFileId);
        NSV8Objects::Template_Set(result, "GetFileId",          _GetFileId);
        NSV8Objects::Template_Set(result, "GetFileBinary",      _GetFileArrayBuffer);
        NSV8Objects::Template_Set(result, "GetFontBinary",      _GetFontArrayBuffer);
        NSV8Objects::Template_Set(result, "GetFontsDirectory",  _GetFontsDirectory);
        NSV8Objects::Template_Set(result, "GetFileString",      _GetFileString);
        NSV8Objects::Template_Set(result, "GetEditorType",      _GetEditorType);
        NSV8Objects::Template_Set(result, "CheckNextChange",    _CheckNextChange);
        NSV8Objects::Template_Set(result, "GetCountChanges",    _GetChangesCount);
        NSV8Objects::Template_Set(result, "GetChangesFile",     _GetChangesFile);
        NSV8Objects::Template_Set(result, "Save_AllocNative",   _Save_AllocNative);
        NSV8Objects::Template_Set(result, "Save_ReAllocNative", _Save_ReAllocNative);
        NSV8Objects::Template_Set(result, "Save_End",           _Save_End);
        NSV8Objects::Template_Set(result, "AddImageInChanges",  _AddImageInChanges);
        NSV8Objects::Template_Set(result, "ConsoleLog",         _ConsoleLog);
        NSV8Objects::Template_Set(result, "SaveChanges",        _SaveChanges);
        NSV8Objects::Template_Set(result, "ZipOpen",            _zipOpenFile);
        NSV8Objects::Template_Set(result, "ZipOpenBase64",      _zipOpenFileBase64);
        NSV8Objects::Template_Set(result, "ZipFileAsString",    _zipGetFileAsString);
        NSV8Objects::Template_Set(result, "ZipFileAsBinary",    _zipGetFileAsBinary);
        NSV8Objects::Template_Set(result, "ZipClose",           _zipCloseFile);
        NSV8Objects::Template_Set(result, "getImageUrl",        _GetImageUrl);
        NSV8Objects::Template_Set(result, "getImagesDirectory", _GetImagesPath);
        NSV8Objects::Template_Set(result, "GetImageOriginalSize", _GetImageOriginalSize);

        return handle_scope.Escape(result);
    }

    // Без SaveChanges
    v8::Handle<v8::ObjectTemplate> CreateNativeControlTemplateBuilder(v8::Isolate* isolate)
    {
        v8::EscapableHandleScope handle_scope(isolate);

        v8::Local<v8::ObjectTemplate> result = v8::ObjectTemplate::New(V8IsolateOneArg);
        result->SetInternalFieldCount(1);

        NSV8Objects::Template_Set(result, "SetFilePath",        _SetFilePath);
        NSV8Objects::Template_Set(result, "GetFilePath",        _GetFilePath);
        NSV8Objects::Template_Set(result, "SetFileId",          _SetFileId);
        NSV8Objects::Template_Set(result, "GetFileId",          _GetFileId);
        NSV8Objects::Template_Set(result, "GetFileBinary",      _GetFileArrayBuffer);
        NSV8Objects::Template_Set(result, "GetFontBinary",      _GetFontArrayBuffer);
        NSV8Objects::Template_Set(result, "GetFontsDirectory",  _GetFontsDirectory);
        NSV8Objects::Template_Set(result, "GetFileString",      _GetFileString);
        NSV8Objects::Template_Set(result, "GetEditorType",      _GetEditorType);
        NSV8Objects::Template_Set(result, "CheckNextChange",    _CheckNextChange);
        NSV8Objects::Template_Set(result, "GetCountChanges",    _GetChangesCount);
        NSV8Objects::Template_Set(result, "GetChangesFile",     _GetChangesFile);
        NSV8Objects::Template_Set(result, "Save_AllocNative",   _Save_AllocNative);
        NSV8Objects::Template_Set(result, "Save_ReAllocNative", _Save_ReAllocNative);
        NSV8Objects::Template_Set(result, "Save_End",           _Save_End);
        NSV8Objects::Template_Set(result, "AddImageInChanges",  _AddImageInChanges);
        NSV8Objects::Template_Set(result, "ConsoleLog",         _ConsoleLog);
        NSV8Objects::Template_Set(result, "ZipOpen",            _zipOpenFile);
        NSV8Objects::Template_Set(result, "ZipOpenBase64",      _zipOpenFileBase64);
        NSV8Objects::Template_Set(result, "ZipFileAsString",    _zipGetFileAsString);
        NSV8Objects::Template_Set(result, "ZipFileAsBinary",    _zipGetFileAsBinary);
        NSV8Objects::Template_Set(result, "ZipClose",           _zipCloseFile);
        NSV8Objects::Template_Set(result, "getImageUrl",        _GetImageUrl);
        NSV8Objects::Template_Set(result, "getImagesDirectory", _GetImagesPath);
        NSV8Objects::Template_Set(result, "GetImageOriginalSize", _GetImageOriginalSize);

        return handle_scope.Escape(result);
    }

    void CreateNativeObject(const v8::FunctionCallbackInfo<v8::Value>& args)
    {
        v8::Isolate* isolate = args.GetIsolate();
        v8::HandleScope scope(isolate);

        v8::Handle<v8::ObjectTemplate> NativeObjectTemplate = CreateNativeControlTemplate(isolate);
        CNativeControlEmbed* pNativeObject = new CNativeControlEmbed();

        v8::Local<v8::Object> obj = NativeObjectTemplate->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
        obj->SetInternalField(0, v8::External::New(CV8Worker::GetCurrent(), pNativeObject));

        args.GetReturnValue().Set(obj);
    }

    // Без SaveChanges
    void CreateNativeObjectBuilder(const v8::FunctionCallbackInfo<v8::Value>& args)
    {
        v8::Isolate* isolate = args.GetIsolate();
        v8::HandleScope scope(isolate);

        v8::Handle<v8::ObjectTemplate> NativeObjectTemplate = CreateNativeControlTemplateBuilder(isolate);
        CNativeControlEmbed* pNativeObject = new CNativeControlEmbed();

        v8::Local<v8::Object> obj = NativeObjectTemplate->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
        obj->SetInternalField(0, v8::External::New(CV8Worker::GetCurrent(), pNativeObject));

        args.GetReturnValue().Set(obj);
    }
    v8::Isolate* current = CV8Worker::GetCurrent();
    context->m_internal->m_global->Set(current, name.c_str(), v8::FunctionTemplate::New(current, NSNativeControl::CreateNativeObject));
    v8::Isolate* current = CV8Worker::GetCurrent();
    context->m_internal->m_global->Set(current, name.c_str(), v8::FunctionTemplate::New(current, NSNativeControl::CreateNativeObjectBuilder));
Fixed code: 	if (retValue)
		retValue->Clear();
	JSSmart<CJSValue> retNativeVal = m_context->runScript(commandA, try_catch);
	return true;
	JSSmart<CJSContextScope> context_scope = m_context->CreateContextScope();
	if (true)
	return !bIsBreak;
			CheckFileDir();
			if (0 == nReturnCode)
				return 0;
//-(JSValue*) Save_AllocNative : (JSValue*)len;
//-(JSValue*) Save_ReAllocNative : (JSValue*)pos : (JSValue*)len;
//FUNCTION_WRAPPER_JS_1(Save_AllocNative, Save_AllocNative)
//FUNCTION_WRAPPER_JS_2(Save_ReAllocNative, Save_ReAllocNative)
#define CURRENTWRAPPER CNativeControlEmbed

	FUNCTION_WRAPPER_V8(_GetFilePath, GetFilePath)
	FUNCTION_WRAPPER_V8_1(_SetFilePath, SetFilePath)
	FUNCTION_WRAPPER_V8(_GetFileId, GetFileId)
	FUNCTION_WRAPPER_V8_1(_SetFileId, SetFileId)
	FUNCTION_WRAPPER_V8_1(_GetFileArrayBuffer, GetFileBinary)
	FUNCTION_WRAPPER_V8_1(_GetFontArrayBuffer, GetFontBinary)
	FUNCTION_WRAPPER_V8(_GetFontsDirectory, GetFontsDirectory)
	FUNCTION_WRAPPER_V8_1(_GetFileString, GetFileString)
	FUNCTION_WRAPPER_V8(_GetEditorType, GetEditorType)
	FUNCTION_WRAPPER_V8(_CheckNextChange, CheckNextChange)
	FUNCTION_WRAPPER_V8(_GetChangesCount, GetCountChanges)
	FUNCTION_WRAPPER_V8_1(_GetChangesFile, GetChangesFile)
	FUNCTION_WRAPPER_V8_1(_Save_AllocNative, Save_AllocNative)
	FUNCTION_WRAPPER_V8_2(_Save_ReAllocNative, Save_ReAllocNative)
	FUNCTION_WRAPPER_V8_2(_Save_End, Save_End)
	FUNCTION_WRAPPER_V8_1(_AddImageInChanges, AddImageInChanges)
	FUNCTION_WRAPPER_V8_1(_ConsoleLog, ConsoleLog)
	FUNCTION_WRAPPER_V8_3(_SaveChanges, SaveChanges)
	FUNCTION_WRAPPER_V8_1(_zipOpenFile, zipOpenFile)
	FUNCTION_WRAPPER_V8_1(_zipOpenFileBase64, zipOpenFileBase64)
	FUNCTION_WRAPPER_V8_1(_zipGetFileAsString, zipGetFileAsString)
	FUNCTION_WRAPPER_V8_1(_zipGetFileAsBinary, zipGetFileAsBinary)
	FUNCTION_WRAPPER_V8(_zipCloseFile, zipCloseFile)
	FUNCTION_WRAPPER_V8_1(_GetImageUrl, GetImageUrl)
	FUNCTION_WRAPPER_V8_1(_GetImageOriginalSize, GetImageOriginalSize)
	FUNCTION_WRAPPER_V8(_GetImagesPath, GetImagesPath)

	v8::Handle<v8::ObjectTemplate> CreateNativeControlTemplate(v8::Isolate* isolate)
	{
		v8::EscapableHandleScope handle_scope(isolate);

		v8::Local<v8::ObjectTemplate> result = v8::ObjectTemplate::New(V8IsolateOneArg);
		result->SetInternalFieldCount(1);

		NSV8Objects::Template_Set(result, "SetFilePath",        _SetFilePath);
		NSV8Objects::Template_Set(result, "GetFilePath",        _GetFilePath);
		NSV8Objects::Template_Set(result, "SetFileId",          _SetFileId);
		NSV8Objects::Template_Set(result, "GetFileId",          _GetFileId);
		NSV8Objects::Template_Set(result, "GetFileBinary",      _GetFileArrayBuffer);
		NSV8Objects::Template_Set(result, "GetFontBinary",      _GetFontArrayBuffer);
		NSV8Objects::Template_Set(result, "GetFontsDirectory",  _GetFontsDirectory);
		NSV8Objects::Template_Set(result, "GetFileString",      _GetFileString);
		NSV8Objects::Template_Set(result, "GetEditorType",      _GetEditorType);
		NSV8Objects::Template_Set(result, "CheckNextChange",    _CheckNextChange);
		NSV8Objects::Template_Set(result, "GetCountChanges",    _GetChangesCount);
		NSV8Objects::Template_Set(result, "GetChangesFile",     _GetChangesFile);
		//NSV8Objects::Template_Set(result, "Save_AllocNative",   _Save_AllocNative);
		//NSV8Objects::Template_Set(result, "Save_ReAllocNative", _Save_ReAllocNative);
		NSV8Objects::Template_Set(result, "Save_End",           _Save_End);
		NSV8Objects::Template_Set(result, "AddImageInChanges",  _AddImageInChanges);
		NSV8Objects::Template_Set(result, "ConsoleLog",         _ConsoleLog);
		NSV8Objects::Template_Set(result, "SaveChanges",        _SaveChanges);
		NSV8Objects::Template_Set(result, "ZipOpen",            _zipOpenFile);
		NSV8Objects::Template_Set(result, "ZipOpenBase64",      _zipOpenFileBase64);
		NSV8Objects::Template_Set(result, "ZipFileAsString",    _zipGetFileAsString);
		NSV8Objects::Template_Set(result, "ZipFileAsBinary",    _zipGetFileAsBinary);
		NSV8Objects::Template_Set(result, "ZipClose",           _zipCloseFile);
		NSV8Objects::Template_Set(result, "getImageUrl",        _GetImageUrl);
		NSV8Objects::Template_Set(result, "getImagesDirectory", _GetImagesPath);
		NSV8Objects::Template_Set(result, "GetImageOriginalSize", _GetImageOriginalSize);

		return handle_scope.Escape(result);
	}

	// Без SaveChanges
	v8::Handle<v8::ObjectTemplate> CreateNativeControlTemplateBuilder(v8::Isolate* isolate)
	{
		v8::EscapableHandleScope handle_scope(isolate);

		v8::Local<v8::ObjectTemplate> result = v8::ObjectTemplate::New(V8IsolateOneArg);
		result->SetInternalFieldCount(1);

		NSV8Objects::Template_Set(result, "SetFilePath",        _SetFilePath);
		NSV8Objects::Template_Set(result, "GetFilePath",        _GetFilePath);
		NSV8Objects::Template_Set(result, "SetFileId",          _SetFileId);
		NSV8Objects::Template_Set(result, "GetFileId",          _GetFileId);
		NSV8Objects::Template_Set(result, "GetFileBinary",      _GetFileArrayBuffer);
		NSV8Objects::Template_Set(result, "GetFontBinary",      _GetFontArrayBuffer);
		NSV8Objects::Template_Set(result, "GetFontsDirectory",  _GetFontsDirectory);
		NSV8Objects::Template_Set(result, "GetFileString",      _GetFileString);
		NSV8Objects::Template_Set(result, "GetEditorType",      _GetEditorType);
		NSV8Objects::Template_Set(result, "CheckNextChange",    _CheckNextChange);
		NSV8Objects::Template_Set(result, "GetCountChanges",    _GetChangesCount);
		NSV8Objects::Template_Set(result, "GetChangesFile",     _GetChangesFile);
		//NSV8Objects::Template_Set(result, "Save_AllocNative",   _Save_AllocNative);
		//NSV8Objects::Template_Set(result, "Save_ReAllocNative", _Save_ReAllocNative);
		NSV8Objects::Template_Set(result, "Save_End",           _Save_End);
		NSV8Objects::Template_Set(result, "AddImageInChanges",  _AddImageInChanges);
		NSV8Objects::Template_Set(result, "ConsoleLog",         _ConsoleLog);
		NSV8Objects::Template_Set(result, "ZipOpen",            _zipOpenFile);
		NSV8Objects::Template_Set(result, "ZipOpenBase64",      _zipOpenFileBase64);
		NSV8Objects::Template_Set(result, "ZipFileAsString",    _zipGetFileAsString);
		NSV8Objects::Template_Set(result, "ZipFileAsBinary",    _zipGetFileAsBinary);
		NSV8Objects::Template_Set(result, "ZipClose",           _zipCloseFile);
		NSV8Objects::Template_Set(result, "getImageUrl",        _GetImageUrl);
		NSV8Objects::Template_Set(result, "getImagesDirectory", _GetImagesPath);
		NSV8Objects::Template_Set(result, "GetImageOriginalSize", _GetImageOriginalSize);

		return handle_scope.Escape(result);
	}

	void CreateNativeObject(const v8::FunctionCallbackInfo<v8::Value>& args)
	{
		v8::Isolate* isolate = args.GetIsolate();
		v8::HandleScope scope(isolate);

		if (CIsolateAdditionalData::CheckSingletonType(isolate, CIsolateAdditionalData::iadtSingletonNative))
		{
			args.GetReturnValue().Set(v8::Undefined(isolate));
			return;
		}

		v8::Handle<v8::ObjectTemplate> NativeObjectTemplate = CreateNativeControlTemplate(isolate);
		CNativeControlEmbed* pNativeObject = new CNativeControlEmbed();

		v8::Local<v8::Object> obj = NativeObjectTemplate->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
		obj->SetInternalField(0, v8::External::New(CV8Worker::GetCurrent(), pNativeObject));

		args.GetReturnValue().Set(obj);
	}

	// Без SaveChanges
	void CreateNativeObjectBuilder(const v8::FunctionCallbackInfo<v8::Value>& args)
	{
		v8::Isolate* isolate = args.GetIsolate();
		v8::HandleScope scope(isolate);

		if (CIsolateAdditionalData::CheckSingletonType(isolate, CIsolateAdditionalData::iadtSingletonNative))
		{
			args.GetReturnValue().Set(v8::Undefined(isolate));
			return;
		}

		v8::Handle<v8::ObjectTemplate> NativeObjectTemplate = CreateNativeControlTemplateBuilder(isolate);
		CNativeControlEmbed* pNativeObject = new CNativeControlEmbed();

		v8::Local<v8::Object> obj = NativeObjectTemplate->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
		obj->SetInternalField(0, v8::External::New(CV8Worker::GetCurrent(), pNativeObject));

		args.GetReturnValue().Set(obj);
	}
	v8::Isolate* current = CV8Worker::GetCurrent();
	context->m_internal->m_global->Set(current, name.c_str(), v8::FunctionTemplate::New(current, NSNativeControl::CreateNativeObject));
	v8::Isolate* current = CV8Worker::GetCurrent();
	context->m_internal->m_global->Set(current, name.c_str(), v8::FunctionTemplate::New(current, NSNativeControl::CreateNativeObjectBuilder));
		unsigned int nEmbedDataCount = m_internal->m_isolate->GetNumberOfDataSlots();
		for (unsigned int i = 0; i < nEmbedDataCount; ++i)
		{
			CIsolateAdditionalData* pAdditionData = (CIsolateAdditionalData*)m_internal->m_isolate->GetData(i);
			delete pAdditionData;
		}

class CIsolateAdditionalData
{
public:
	enum IsolateAdditionlDataType {
		iadtSingletonNative = 0,
		iadtUndefined = 255
	};

	IsolateAdditionlDataType m_eType;
public:
	CIsolateAdditionalData(const IsolateAdditionlDataType& type = iadtUndefined) { m_eType = type; }
	virtual ~CIsolateAdditionalData() {}

	static bool CheckSingletonType(v8::Isolate* isolate, const IsolateAdditionlDataType& type, const bool& isAdd = true)
	{
		unsigned int nCount = isolate->GetNumberOfDataSlots();
		for (unsigned int i = 0; i < nCount; ++i)
		{
			CIsolateAdditionalData* pData = (CIsolateAdditionalData*)isolate->GetData(i);
			if (pData->m_eType == type)
				return true;
		}

		if (isAdd)
		{
			isolate->SetData(nCount, (void*)(new CIsolateAdditionalData(type)));
		}

		return false;
	}
};