Fix for snapshots

This commit is contained in:
Oleg Korshul
2024-08-28 13:08:06 +03:00
parent 06ee659edc
commit 447e1f5ccc
3 changed files with 10 additions and 107 deletions

View File

@ -44,11 +44,15 @@ void CV8RealTimeWorker::_LOGGING_ERROR_(const std::wstring& strType, const std::
using namespace NSJSBase;
CV8RealTimeWorker::CV8RealTimeWorker(NSDoctRenderer::CDocBuilder* pBuilder)
CV8RealTimeWorker::CV8RealTimeWorker(NSDoctRenderer::CDocBuilder* pBuilder, const NSDoctRenderer::DoctRendererEditorType& type, NSDoctRenderer::CDoctRendererConfig* config)
{
m_nFileType = -1;
m_context = new CJSContext();
if (NSDoctRenderer::DoctRendererEditorType::INVALID == type)
m_context = new CJSContext();
else
m_context = NSDoctRenderer::CreateEditorContext(type, config);
CJSContextScope scope(m_context);
CJSContext::Embed<CNativeControlEmbed>(false);
@ -249,7 +253,8 @@ bool CV8RealTimeWorker::OpenFile(const std::wstring& sBasePath, const std::wstri
LOGGER_SPEED_LAP("compile");
NSDoctRenderer::RunEditor(editorType, m_context, try_catch, config);
if (!m_context->isSnapshotUsed())
NSDoctRenderer::RunEditor(editorType, m_context, try_catch, config);
if(try_catch->Check())
return false;

View File

@ -415,7 +415,7 @@ public:
public:
CV8RealTimeWorker(NSDoctRenderer::CDocBuilder* pBuilder);
CV8RealTimeWorker(NSDoctRenderer::CDocBuilder* pBuilder, const NSDoctRenderer::DoctRendererEditorType& type, NSDoctRenderer::CDoctRendererConfig* config);
~CV8RealTimeWorker();
public:
@ -1226,7 +1226,7 @@ namespace NSDoctRenderer
{
if (NULL == m_pWorker)
{
m_pWorker = new CV8RealTimeWorker(m_pParent);
m_pWorker = new CV8RealTimeWorker(m_pParent, GetEditorType(), this);
m_pWorker->m_sUtf8ArgumentJSON = m_oParams.m_sArgumentJSON;
m_pWorker->m_sGlobalVariable = m_sGlobalVariable;

View File

@ -1039,108 +1039,6 @@ public:
#endif
//////////////////////////////////////////////////////////////////////////////
#if 0
class CSnapshotScript
{
public:
bool m_bIsExist;
v8::StartupData m_oStartupData;
CSnapshotScript(const std::wstring& sDir = L"")
{
m_bIsExist = false;
m_oStartupData.data = NULL;
m_oStartupData.raw_size = 0;
std::wstring sFile = sDir + L"/heap_snapshot.bin";
if (NSFile::CFileBinary::Exists(sFile))
{
m_bIsExist = true;
NSFile::CFileBinary oFile;
oFile.OpenFile(sFile);
m_oStartupData.raw_size = (int)oFile.GetFileSize();
m_oStartupData.data = new char[m_oStartupData.raw_size];
DWORD dwRead = 0;
oFile.ReadFile((BYTE*)m_oStartupData.data, (DWORD)m_oStartupData.raw_size, dwRead);
oFile.CloseFile();
}
}
bool Generate(const std::string& sScript)
{
m_oStartupData = {NULL, 0};
{
v8::SnapshotCreator snapshot_creator;
// Obtain an isolate provided by SnapshotCreator.
v8::Isolate* isolate = snapshot_creator.GetIsolate();
{
v8::HandleScope scope(isolate);
// Create a new context and optionally run some script.
v8::Local<v8::Context> context = v8::Context::New(isolate);
//v8::Context::Scope context_scope(context);
if (!RunExtraCode(isolate, context, sScript.c_str(), "<embedded>"))
return false;
// Add the possibly customized context to the SnapshotCreator.
snapshot_creator.SetDefaultContext(context);
}
// Use the SnapshotCreator to create the snapshot blob.
m_oStartupData = snapshot_creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear);
}
return true;
}
void Save(const std::wstring& sDir)
{
if (m_oStartupData.data == NULL)
return;
std::wstring sFile = sDir + L"/heap_snapshot.bin";
NSFile::CFileBinary oFile;
oFile.CreateFile(sFile);
oFile.WriteFile((BYTE*)m_oStartupData.data, (DWORD)m_oStartupData.raw_size);
oFile.CloseFile();
}
bool RunExtraCode(v8::Isolate* isolate, v8::Local<v8::Context> context, const char* utf8_source, const char* name)
{
// Run custom script if provided.
v8::TryCatch try_catch(isolate);
v8::Local<v8::String> source_string;
if (!v8::String::NewFromUtf8(isolate, utf8_source, v8::NewStringType::kNormal).ToLocal(&source_string))
return false;
v8::Local<v8::String> resource_name = v8::String::NewFromUtf8(isolate, name, v8::NewStringType::kNormal).ToLocalChecked();
v8::ScriptOrigin origin(resource_name);
v8::ScriptCompiler::Source source(source_string, origin);
v8::Local<v8::Script> script;
bool bRet = v8::ScriptCompiler::Compile(context, &source).ToLocal(&script);
if (try_catch.HasCaught())
{
std::string strCode = to_cstringA(try_catch.Message()->GetSourceLine());
std::string strException = to_cstringA(try_catch.Message()->Get());
return false;
}
script->Run();
if (try_catch.HasCaught())
{
std::string strCode = to_cstringA(try_catch.Message()->GetSourceLine());
std::string strException = to_cstringA(try_catch.Message()->Get());
return false;
}
return true;
}
};
#endif
bool Doct_renderer_SaveFile_ForBuilder(int nFormat, const std::wstring& strDstFile,
NSNativeControl::CNativeControl* pNative,