diff --git a/DesktopEditor/doctrenderer/docbuilder.cpp b/DesktopEditor/doctrenderer/docbuilder.cpp index bfb37ac573..c6dd3c5856 100644 --- a/DesktopEditor/doctrenderer/docbuilder.cpp +++ b/DesktopEditor/doctrenderer/docbuilder.cpp @@ -47,14 +47,6 @@ namespace NSDoctRenderer RELEASEOBJECT(m_pInternal); } - int CDocBuilder::OpenFile(const wchar_t* path, const wchar_t* params) - { - m_pInternal->m_nFileType = -1; - if (!NSDirectory::Exists(m_pInternal->m_sTmpFolder)) - NSDirectory::CreateDirectory(m_pInternal->m_sTmpFolder); - - return m_pInternal->OpenFile(path, params); - } int CDocBuilder::SaveFile(const int& type, const wchar_t* path, const wchar_t* params) { return m_pInternal->SaveFile(type, path, params); diff --git a/DesktopEditor/doctrenderer/docbuilder.h b/DesktopEditor/doctrenderer/docbuilder.h index 1177046c84..5580500cbf 100644 --- a/DesktopEditor/doctrenderer/docbuilder.h +++ b/DesktopEditor/doctrenderer/docbuilder.h @@ -526,6 +526,7 @@ namespace NSDoctRenderer CDocBuilder_Private* m_pInternal; friend class CBuilderDocumentEmbed; + friend class CBuilderEmbed; }; /** diff --git a/DesktopEditor/doctrenderer/docbuilder_p.cpp b/DesktopEditor/doctrenderer/docbuilder_p.cpp index e23743cc8c..adb3c60f56 100644 --- a/DesktopEditor/doctrenderer/docbuilder_p.cpp +++ b/DesktopEditor/doctrenderer/docbuilder_p.cpp @@ -82,7 +82,7 @@ CV8RealTimeWorker::~CV8RealTimeWorker() m_context->Dispose(); } -bool CV8RealTimeWorker::ExecuteCommand(const std::wstring& command, NSDoctRenderer::CDocBuilderValue* retValue) +bool CV8RealTimeWorker::ExecuteCommand(const std::wstring& command, NSDoctRenderer::CDocBuilderValue* retValue, const bool& isEnterContext) { LOGGER_SPEED_START(); @@ -92,14 +92,20 @@ bool CV8RealTimeWorker::ExecuteCommand(const std::wstring& command, NSDoctRender std::string commandA = U_TO_UTF8(command); //commandA = "Api." + commandA; - CJSContextScope scope(m_context); + if (isEnterContext) + m_context->Enter(); + JSSmart try_catch = m_context->GetExceptions(); LOGGER_SPEED_LAP("compile_command"); JSSmart retNativeVal = m_context->runScript(commandA, try_catch); if(try_catch->Check()) + { + if (isEnterContext) + m_context->Exit(); return false; + } if (retValue) { @@ -110,6 +116,9 @@ bool CV8RealTimeWorker::ExecuteCommand(const std::wstring& command, NSDoctRender LOGGER_SPEED_LAP("run_command"); + if (isEnterContext) + m_context->Exit(); + return true; } @@ -344,7 +353,17 @@ bool CV8RealTimeWorker::OpenFile(const std::wstring& sBasePath, const std::wstri return !bIsBreak; } -bool CV8RealTimeWorker::SaveFileWithChanges(int type, const std::wstring& _path, const std::wstring& sJsonParams) +bool CV8RealTimeWorker::NewSimpleJSInstance() +{ + return InitVariables(); +} + +bool CV8RealTimeWorker::IsSimpleJSInstance() +{ + return (-1 == m_nFileType); +} + +bool CV8RealTimeWorker::SaveFileWithChanges(int type, const std::wstring& _path, const std::wstring& sJsonParams, const bool& isEnterContext) { NSDoctRenderer::DoctRendererFormat::FormatFile _formatDst = NSDoctRenderer::DoctRendererFormat::DOCT; if (type & AVS_OFFICESTUDIO_FILE_PRESENTATION) @@ -370,7 +389,9 @@ bool CV8RealTimeWorker::SaveFileWithChanges(int type, const std::wstring& _path, } } - CJSContextScope scope(m_context); + if (isEnterContext) + m_context->Enter(); + JSSmart try_catch = m_context->GetExceptions(); NSNativeControl::CNativeControl* pNative = NULL; @@ -399,7 +420,7 @@ bool CV8RealTimeWorker::SaveFileWithChanges(int type, const std::wstring& _path, bIsSilentMode = true; if (bIsSilentMode) - this->ExecuteCommand(L"Api.asc_SetSilentMode(false);"); + this->ExecuteCommand(L"Api.asc_SetSilentMode(false);", NULL, isEnterContext); std::wstring strError; bool bIsError = Doct_renderer_SaveFile_ForBuilder(_formatDst, @@ -411,7 +432,10 @@ bool CV8RealTimeWorker::SaveFileWithChanges(int type, const std::wstring& _path, sJsonParams); if (bIsSilentMode) - this->ExecuteCommand(L"Api.asc_SetSilentMode(true);"); + this->ExecuteCommand(L"Api.asc_SetSilentMode(true);", NULL, isEnterContext); + + if (isEnterContext) + m_context->Exit(); return bIsError; } @@ -1239,8 +1263,29 @@ namespace NSDoctRenderer nCount = nIndex; } + int CDocBuilder::OpenFile(const wchar_t* path, const wchar_t* params) + { + if (m_pInternal->m_nFileType != -1 && m_pInternal->m_bIsOpenedFromSimpleJS) + { + m_pInternal->m_bIsOpenedFromSimpleJS = false; + return true; + } + + m_pInternal->m_nFileType = -1; + if (!NSDirectory::Exists(m_pInternal->m_sTmpFolder)) + NSDirectory::CreateDirectory(m_pInternal->m_sTmpFolder); + + return m_pInternal->OpenFile(path, params); + } + bool CDocBuilder::CreateFile(const int& type) { + if (m_pInternal->m_nFileType != -1 && m_pInternal->m_bIsOpenedFromSimpleJS) + { + m_pInternal->m_bIsOpenedFromSimpleJS = false; + return true; + } + m_pInternal->m_nFileType = -1; if (!NSDirectory::Exists(m_pInternal->m_sTmpFolder)) NSDirectory::CreateDirectory(m_pInternal->m_sTmpFolder); @@ -1399,7 +1444,7 @@ namespace NSDoctRenderer if (!sJsCommands.empty()) { std::wstring sUnicodeCommand = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)sJsCommands.c_str(), (LONG)sJsCommands.length()); - bIsNoError = this->ExecuteCommand(sUnicodeCommand.c_str()); + bIsNoError = this->m_pInternal->ExecuteCommand(sUnicodeCommand.c_str(), NULL, bIsBuilderJSCloseFile); sJsCommands = ""; if (!bIsNoError) return false; @@ -1421,6 +1466,10 @@ namespace NSDoctRenderer if (0 == _builder_params[nCheckParam].find(L"jsValue(") && _builder_params[nCheckParam].length() > 9) { std::wstring sParam = _builder_params[nCheckParam].substr(8, _builder_params[nCheckParam].length() - 9); + + if (NULL == m_pInternal->m_pWorker) + m_pInternal->CheckWorker(); + _builder_params[nCheckParam] = m_pInternal->m_pWorker->GetJSVariable(sParam); } } @@ -1486,7 +1535,7 @@ namespace NSDoctRenderer { // Такого быть не должно!!! Так как результат никуда не сохранится. пустое действие. std::wstring sUnicodeCommand = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)sJsCommands.c_str(), (LONG)sJsCommands.length()); - bool bIsNoError = this->ExecuteCommand(sUnicodeCommand.c_str()); + bool bIsNoError = this->m_pInternal->ExecuteCommand(sUnicodeCommand.c_str(), NULL, true); sJsCommands = ""; if (!bIsNoError) return false; diff --git a/DesktopEditor/doctrenderer/docbuilder_p.h b/DesktopEditor/doctrenderer/docbuilder_p.h index 9d0298e0b2..6603297f22 100644 --- a/DesktopEditor/doctrenderer/docbuilder_p.h +++ b/DesktopEditor/doctrenderer/docbuilder_p.h @@ -437,14 +437,17 @@ public: static void _LOGGING_ERROR_(const std::wstring& strType, const std::wstring& strError); - bool ExecuteCommand(const std::wstring& command, NSDoctRenderer::CDocBuilderValue* retValue = NULL); + bool ExecuteCommand(const std::wstring& command, NSDoctRenderer::CDocBuilderValue* retValue = NULL, const bool& isEnterContext = true); std::string GetGlobalVariable(); std::wstring GetJSVariable(std::wstring sParam); bool OpenFile(const std::wstring& sBasePath, const std::wstring& path, const NSDoctRenderer::DoctRendererEditorType& editorType, NSDoctRenderer::CDoctRendererConfig* config, CV8Params* pParams = NULL); - bool SaveFileWithChanges(int type, const std::wstring& _path, const std::wstring& sJsonParams = L""); + bool SaveFileWithChanges(int type, const std::wstring& _path, const std::wstring& sJsonParams = L"", const bool& isEnterContext = true); bool InitVariables(); + + bool NewSimpleJSInstance(); + bool IsSimpleJSInstance(); }; namespace NSDoctRenderer @@ -478,11 +481,13 @@ namespace NSDoctRenderer NSDoctRenderer::CDocBuilder* m_pParent; + bool m_bIsOpenedFromSimpleJS; + static std::wstring m_sExternalDirectory; public: CDocBuilder_Private() : CDoctRendererConfig(), m_sTmpFolder(NSFile::CFileBinary::GetTempPath()), m_nFileType(-1), m_pWorker(NULL), m_pAdditionalData(NULL), m_bIsInit(false), m_bIsServerSafeVersion(false), - m_sGlobalVariable(""), m_bIsGlobalVariableUse(false), m_pParent(NULL), m_sCommandsBeforeContextCreated(L"") + m_sGlobalVariable(""), m_bIsGlobalVariableUse(false), m_pParent(NULL), m_sCommandsBeforeContextCreated(L""), m_bIsOpenedFromSimpleJS(false) { } @@ -641,6 +646,13 @@ namespace NSDoctRenderer { NSDirectory::CreateDirectory(m_sFileDir + L"/media"); NSDirectory::CreateDirectory(m_sFileDir + L"/changes"); + + if (m_pWorker && m_pWorker->IsSimpleJSInstance() && !m_bIsOpenedFromSimpleJS) + { + RELEASEOBJECT(m_pWorker); + CheckWorker(); + } + return bRet; } return bRet; @@ -931,7 +943,14 @@ namespace NSDoctRenderer LOGGER_SPEED_LAP("open_convert"); if (0 == nReturnCode) + { + if (m_pWorker && m_pWorker->IsSimpleJSInstance() && !m_bIsOpenedFromSimpleJS) + { + RELEASEOBJECT(m_pWorker); + CheckWorker(); + } return 0; + } NSDirectory::DeleteDirectory(m_sFileDir); m_sFileDir = L""; @@ -978,7 +997,7 @@ namespace NSDoctRenderer return _path; } - int SaveFile(const int& type, const std::wstring& path, const wchar_t* params = NULL) + int SaveFile(const int& type, const std::wstring& path, const wchar_t* params = NULL, const bool& isEnterContext = true) { Init(); @@ -1026,7 +1045,7 @@ namespace NSDoctRenderer } } - this->m_pWorker->SaveFileWithChanges(type, m_sFileDir + L"/Editor2.bin", sJsonParams); + this->m_pWorker->SaveFileWithChanges(type, m_sFileDir + L"/Editor2.bin", sJsonParams, isEnterContext); sFileBin = L"/Editor2.bin"; } @@ -1226,17 +1245,21 @@ namespace NSDoctRenderer if (NULL == m_pWorker) { NSDoctRenderer::DoctRendererEditorType editorType = GetEditorType(); - if (NSDoctRenderer::DoctRendererEditorType::INVALID == editorType) - return false; - m_pWorker = new CV8RealTimeWorker(m_pParent, editorType, this); m_pWorker->m_sUtf8ArgumentJSON = m_oParams.m_sArgumentJSON; m_pWorker->m_sGlobalVariable = m_sGlobalVariable; m_pWorker->m_sJSCodeStart = U_TO_UTF8(m_sCommandsBeforeContextCreated); - m_sCommandsBeforeContextCreated = L""; m_pWorker->m_nFileType = m_nFileType; + if (-1 != m_nFileType) + m_sCommandsBeforeContextCreated = L""; + else + { + m_pWorker->NewSimpleJSInstance(); + return true; + } + CV8Params oParams; oParams.IsServerSaveVersion = m_bIsServerSafeVersion; oParams.DocumentDirectory = m_sFileDir; @@ -1252,12 +1275,12 @@ namespace NSDoctRenderer return SaveFile(nType, path, params); } - bool ExecuteCommand(const std::wstring& command, CDocBuilderValue* retValue = NULL) + bool ExecuteCommand(const std::wstring& command, CDocBuilderValue* retValue = NULL, const bool& forceExecute = false) { if (command.length() < 7 && !retValue) // minimum command (!!!) return true; - if (m_nFileType == -1) + if (m_nFileType == -1 && !forceExecute) { m_sCommandsBeforeContextCreated += command; return true; @@ -1266,7 +1289,20 @@ namespace NSDoctRenderer Init(); if (CheckWorker()) - return m_pWorker->ExecuteCommand(command, retValue); + { + bool bIsOpenedFromSimpleJSOld = m_bIsOpenedFromSimpleJS; + bool bResult = m_pWorker->ExecuteCommand(command, retValue); + if (!bResult && !bIsOpenedFromSimpleJSOld && m_bIsOpenedFromSimpleJS) + { + RELEASEOBJECT(m_pWorker); + CheckWorker(); + return m_pWorker->ExecuteCommand(command, retValue); + } + else + { + return bResult; + } + } return false; } diff --git a/DesktopEditor/doctrenderer/embed/NativeBuilderEmbed.cpp b/DesktopEditor/doctrenderer/embed/NativeBuilderEmbed.cpp index b37f5e26a0..dcdeab51ae 100644 --- a/DesktopEditor/doctrenderer/embed/NativeBuilderEmbed.cpp +++ b/DesktopEditor/doctrenderer/embed/NativeBuilderEmbed.cpp @@ -8,7 +8,22 @@ JSSmart CBuilderEmbed::OpenFile(JSSmart sPath, JSSmarttoStringW(); std::wstring Params = sParams->toStringW(); + + // Since we now use snapshots, and we can't always determine which editor is needed + // (since the code may be BEFORE opening the file). And if the opening came from + // builderJS.OpenFile - in this case we skip reopening. + NSDoctRenderer::CDocBuilder_Private* pPrivate = GetPrivate(); + if (pPrivate->m_pWorker->IsSimpleJSInstance()) + pPrivate->m_bIsOpenedFromSimpleJS = true; + int ret = m_pBuilder->OpenFile(Path.c_str(), Params.c_str()); + + if (pPrivate->m_pWorker->IsSimpleJSInstance()) + { + JSSmart current = CJSContext::GetCurrent(); + current->runScript("throw 0;"); + } + return CJSContext::createInt(ret); } @@ -20,7 +35,21 @@ JSSmart CBuilderEmbed::CreateFile(JSSmart type) else nFormat = type->toInt32(); + // Since we now use snapshots, and we can't always determine which editor is needed + // (since the code may be BEFORE opening the file). And if the opening came from + // builderJS.OpenFile - in this case we skip reopening. + NSDoctRenderer::CDocBuilder_Private* pPrivate = GetPrivate(); + if (pPrivate->m_pWorker->IsSimpleJSInstance()) + pPrivate->m_bIsOpenedFromSimpleJS = true; + bool ret = m_pBuilder->CreateFile(nFormat); + + if (pPrivate->m_pWorker->IsSimpleJSInstance()) + { + JSSmart current = CJSContext::GetCurrent(); + current->runScript("throw 0;"); + } + return CJSContext::createBool(ret); } @@ -41,7 +70,7 @@ JSSmart CBuilderEmbed::SaveFile(JSSmart type, JSSmarttoStringW(); std::wstring sParams = params->toStringW(); - int ret = m_pBuilder->SaveFile(nFormat, sPath.c_str(), sParams.empty() ? NULL : sParams.c_str()); + int ret = GetPrivate()->SaveFile(nFormat, sPath.c_str(), sParams.empty() ? NULL : sParams.c_str(), false); return CJSContext::createInt(ret); } diff --git a/DesktopEditor/doctrenderer/embed/NativeBuilderEmbed.h b/DesktopEditor/doctrenderer/embed/NativeBuilderEmbed.h index a37c3ff0ca..62625747d9 100644 --- a/DesktopEditor/doctrenderer/embed/NativeBuilderEmbed.h +++ b/DesktopEditor/doctrenderer/embed/NativeBuilderEmbed.h @@ -50,6 +50,7 @@ public: ~CBuilderEmbed() { if(m_pBuilder) RELEASEOBJECT(m_pBuilder); } virtual void* getObject() { return (void*)m_pBuilder; } + NSDoctRenderer::CDocBuilder_Private* GetPrivate() { return m_pBuilder->GetPrivate(); } public: JSSmart OpenFile(JSSmart sPath, JSSmart sParams);