mirror of
https://github.com/ONLYOFFICE/core.git
synced 2026-04-07 13:55:33 +08:00
cashed scripts data scheme (may be disable)
This commit is contained in:
@ -172,7 +172,7 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OpenFile(const std::wstring& sBasePath, const std::wstring& path, const std::string& sString)
|
||||
bool OpenFile(const std::wstring& sBasePath, const std::wstring& path, const std::string& sString, const std::wstring& sCachePath)
|
||||
{
|
||||
LOGGER_SPEED_START
|
||||
|
||||
@ -181,7 +181,15 @@ public:
|
||||
v8::TryCatch try_catch;
|
||||
|
||||
v8::Local<v8::String> source = v8::String::NewFromUtf8(m_isolate, sString.c_str());
|
||||
v8::Local<v8::Script> script = v8::Script::Compile(source);
|
||||
v8::Local<v8::Script> script;
|
||||
|
||||
CCacheDataScript oCachedScript(sCachePath);
|
||||
if (sCachePath.empty())
|
||||
script = v8::Script::Compile(source);
|
||||
else
|
||||
{
|
||||
script = oCachedScript.Compile(m_context, source);
|
||||
}
|
||||
|
||||
LOGGER_SPEED_LAP("compile")
|
||||
|
||||
@ -463,6 +471,8 @@ namespace NSDoctRenderer
|
||||
|
||||
CDocBuilderParams m_oParams;
|
||||
bool m_bIsInit;
|
||||
|
||||
bool m_bIsCacheScript;
|
||||
public:
|
||||
CDocBuilder_Private()
|
||||
{
|
||||
@ -478,6 +488,7 @@ namespace NSDoctRenderer
|
||||
|
||||
m_pAdditionalData = NULL;
|
||||
m_bIsInit = false;
|
||||
m_bIsCacheScript = true;
|
||||
}
|
||||
|
||||
void Init()
|
||||
@ -1125,7 +1136,11 @@ namespace NSDoctRenderer
|
||||
m_pWorker->m_nFileType = m_nFileType;
|
||||
m_pWorker->m_sUtf8ArgumentJSON = m_oParams.m_sArgumentJSON;
|
||||
|
||||
bool bOpen = m_pWorker->OpenFile(m_sX2tPath, m_sFileDir, GetScript());
|
||||
std::wstring sCachePath = L"";
|
||||
if (m_bIsCacheScript)
|
||||
sCachePath = GetScriptCache();
|
||||
|
||||
bool bOpen = m_pWorker->OpenFile(m_sX2tPath, m_sFileDir, GetScript(), sCachePath);
|
||||
if (!bOpen)
|
||||
return false;
|
||||
}
|
||||
@ -1181,6 +1196,37 @@ namespace NSDoctRenderer
|
||||
return strScript;
|
||||
}
|
||||
|
||||
std::wstring GetScriptCache()
|
||||
{
|
||||
std::vector<std::wstring>* arSdkFiles = NULL;
|
||||
switch (m_nFileType)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
arSdkFiles = &m_arDoctSDK;
|
||||
break;
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
arSdkFiles = &m_arPpttSDK;
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
arSdkFiles = &m_arXlstSDK;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return L"";
|
||||
}
|
||||
|
||||
if (0 < arSdkFiles->size())
|
||||
{
|
||||
return NSCommon::GetDirectoryName(*arSdkFiles->begin()) + L"/sdk-all.cache";
|
||||
}
|
||||
return L"";
|
||||
}
|
||||
|
||||
std::string ReadScriptFile(const std::wstring& strFile)
|
||||
{
|
||||
NSFile::CFileBinary oFile;
|
||||
@ -1450,6 +1496,8 @@ namespace NSDoctRenderer
|
||||
m_pInternal->m_oParams.m_bCheckFonts = true;
|
||||
else if (sParam == "--work-directory")
|
||||
m_pInternal->m_oParams.m_sWorkDir = std::wstring(value);
|
||||
else if (sParam == "--cache-scripts")
|
||||
m_pInternal->m_bIsCacheScript = (std::wstring(value) == L"true");
|
||||
else if (sParam == "--argument")
|
||||
{
|
||||
std::wstring sArg(value);
|
||||
|
||||
@ -70,6 +70,8 @@ namespace NSDoctRenderer
|
||||
|
||||
bool m_bIsOnlyOnePage;
|
||||
|
||||
bool m_bIsCachedScripts;
|
||||
|
||||
public:
|
||||
CExecuteParams() : m_arChanges()
|
||||
{
|
||||
@ -94,6 +96,7 @@ namespace NSDoctRenderer
|
||||
m_nSaveToPDFParams = 0;
|
||||
|
||||
m_bIsOnlyOnePage = false;
|
||||
m_bIsCachedScripts = true;
|
||||
}
|
||||
~CExecuteParams()
|
||||
{
|
||||
@ -151,6 +154,9 @@ namespace NSDoctRenderer
|
||||
if (nParams & 0x02)
|
||||
m_nSaveToPDFParams = 1;
|
||||
|
||||
if (nParams & 0x04)
|
||||
m_bIsCachedScripts = false;
|
||||
|
||||
m_bIsOnlyOnePage = (oNode.ReadValueInt(L"OnlyOnePage", 0) == 1) ? true : false;
|
||||
|
||||
return true;
|
||||
@ -584,7 +590,7 @@ namespace NSDoctRenderer
|
||||
}
|
||||
|
||||
|
||||
bool ExecuteScript(const std::string& strScript, std::wstring& strError, std::wstring& strReturnParams)
|
||||
bool ExecuteScript(const std::string& strScript, const std::wstring& sCachePath, std::wstring& strError, std::wstring& strReturnParams)
|
||||
{
|
||||
bool bIsBreak = false;
|
||||
v8::Isolate* isolate = CV8Worker::getInitializer()->CreateNew();
|
||||
@ -604,7 +610,15 @@ namespace NSDoctRenderer
|
||||
v8::Context::Scope context_scope(context);
|
||||
v8::TryCatch try_catch;
|
||||
v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, strScript.c_str());
|
||||
v8::Local<v8::Script> script = v8::Script::Compile(source);
|
||||
v8::Local<v8::Script> script;
|
||||
|
||||
CCacheDataScript oCachedScript(sCachePath);
|
||||
if (sCachePath.empty())
|
||||
script = v8::Script::Compile(source);
|
||||
else
|
||||
{
|
||||
script = oCachedScript.Compile(context, source);
|
||||
}
|
||||
|
||||
// COMPILE
|
||||
if (try_catch.HasCaught())
|
||||
@ -1096,8 +1110,14 @@ namespace NSDoctRenderer
|
||||
strScript += "\n\n";
|
||||
}
|
||||
|
||||
std::wstring sCachePath = L"";
|
||||
if (NULL != arSdkFiles)
|
||||
{
|
||||
if (m_pInternal->m_oParams.m_bIsCachedScripts && (0 < arSdkFiles->size()))
|
||||
{
|
||||
sCachePath = NSFile::GetDirectoryName(*arSdkFiles->begin()) + L"/sdk-all.cache";
|
||||
}
|
||||
|
||||
for (std::vector<std::wstring>::iterator i = arSdkFiles->begin(); i != arSdkFiles->end(); i++)
|
||||
{
|
||||
strScript += m_pInternal->ReadScriptFile(*i);
|
||||
@ -1109,7 +1129,7 @@ namespace NSDoctRenderer
|
||||
strScript += "\n$.ready();";
|
||||
|
||||
std::wstring sReturnParams = L"";
|
||||
bool bResult = m_pInternal->ExecuteScript(strScript, strError, sReturnParams);
|
||||
bool bResult = m_pInternal->ExecuteScript(strScript, sCachePath, strError, sReturnParams);
|
||||
|
||||
if (strError.length() != 0)
|
||||
{
|
||||
|
||||
@ -977,4 +977,69 @@ bool Doct_renderer_SaveFile_ForBuilder(int nFormat, const std::wstring& strDstFi
|
||||
v8::TryCatch& try_catch,
|
||||
std::wstring& strError);
|
||||
|
||||
class CCacheDataScript
|
||||
{
|
||||
private:
|
||||
BYTE* Data;
|
||||
int Length;
|
||||
|
||||
v8::ScriptCompiler::Source* Source;
|
||||
v8::ScriptCompiler::CachedData* CachedData;
|
||||
|
||||
std::wstring Path;
|
||||
|
||||
public:
|
||||
CCacheDataScript(const std::wstring& sPath)
|
||||
{
|
||||
Data = NULL;
|
||||
Length = 0;
|
||||
|
||||
if (!sPath.empty())
|
||||
{
|
||||
BYTE* _data = NULL;
|
||||
DWORD _data_length = 0;
|
||||
if (NSFile::CFileBinary::ReadAllBytes(sPath, &_data, _data_length))
|
||||
{
|
||||
Data = _data;
|
||||
Length = (int)_data_length;
|
||||
}
|
||||
}
|
||||
|
||||
Source = NULL;
|
||||
CachedData = NULL;
|
||||
Path = sPath;
|
||||
}
|
||||
~CCacheDataScript()
|
||||
{
|
||||
//RELEASEOBJECT(Source);
|
||||
//RELEASEOBJECT(CachedData);
|
||||
RELEASEARRAYOBJECTS(Data);
|
||||
}
|
||||
|
||||
v8::Local<v8::Script> Compile(const v8::Local<v8::Context>& _context, const v8::Local<v8::String>& source)
|
||||
{
|
||||
v8::Local<v8::Script> script;
|
||||
if (NULL == Data)
|
||||
{
|
||||
Source = new v8::ScriptCompiler::Source(source);
|
||||
script = v8::ScriptCompiler::Compile(_context, Source, v8::ScriptCompiler::kProduceCodeCache).ToLocalChecked();
|
||||
|
||||
const v8::ScriptCompiler::CachedData* _cachedData = Source->GetCachedData();
|
||||
NSFile::CFileBinary oFileTest;
|
||||
if (oFileTest.CreateFileW(Path))
|
||||
{
|
||||
oFileTest.WriteFile(_cachedData->data, (DWORD)_cachedData->length);
|
||||
oFileTest.CloseFile();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
CachedData = new v8::ScriptCompiler::CachedData(Data, Length);
|
||||
Source = new v8::ScriptCompiler::Source(source, CachedData);
|
||||
script = v8::ScriptCompiler::Compile(_context, Source, v8::ScriptCompiler::kConsumeCodeCache).ToLocalChecked();
|
||||
}
|
||||
return script;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // NATIVECONTROL
|
||||
|
||||
Reference in New Issue
Block a user