improve extension abi (dont rely on exceptions across dlls)

This commit is contained in:
Akash Mozumdar 2018-10-09 17:43:33 -04:00
parent 586e22e4a5
commit 39a0161989
3 changed files with 22 additions and 19 deletions

View File

@ -35,16 +35,13 @@ bool DispatchSentenceToExtensions(std::wstring& sentence, std::unordered_map<std
InfoForExtension* miscInfoTraverser = miscInfoLinkedList; InfoForExtension* miscInfoTraverser = miscInfoLinkedList;
for (auto& i : miscInfo) miscInfoTraverser = miscInfoTraverser->nextProperty = new InfoForExtension{ i.first.c_str(), i.second, nullptr }; for (auto& i : miscInfo) miscInfoTraverser = miscInfoTraverser->nextProperty = new InfoForExtension{ i.first.c_str(), i.second, nullptr };
std::shared_lock<std::shared_mutex> extenLock(extenMutex); std::shared_lock<std::shared_mutex> extenLock(extenMutex);
try
{
for (auto i : extensions) for (auto i : extensions)
{ {
wchar_t* prev = sentenceBuffer; wchar_t* nextBuffer = i.second(sentenceBuffer, miscInfoLinkedList);
sentenceBuffer = i.second(sentenceBuffer, miscInfoLinkedList); if (nextBuffer == nullptr) { success = false; break; }
if (sentenceBuffer != prev) HeapFree(GetProcessHeap(), 0, prev); if (nextBuffer != sentenceBuffer) HeapFree(GetProcessHeap(), 0, sentenceBuffer);
sentenceBuffer = nextBuffer;
} }
}
catch (...) { success = false; }
sentence = std::wstring(sentenceBuffer); sentence = std::wstring(sentenceBuffer);
HeapFree(GetProcessHeap(), 0, sentenceBuffer); HeapFree(GetProcessHeap(), 0, sentenceBuffer);
delete miscInfoLinkedList; delete miscInfoLinkedList;

View File

@ -23,6 +23,8 @@ struct SentenceInfo
} }
}; };
struct InvalidSentence {};
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo); bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo);
/** /**
@ -31,20 +33,24 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo);
* You should not modify this sentence. If you want Textractor to receive a modified sentence, copy it into your own buffer and return that. * You should not modify this sentence. If you want Textractor to receive a modified sentence, copy it into your own buffer and return that.
* Please allocate the buffer using HeapAlloc() and not new[] or malloc() or something else: Textractor uses HeapFree() to free it. * Please allocate the buffer using HeapAlloc() and not new[] or malloc() or something else: Textractor uses HeapFree() to free it.
* Param miscInfo: pointer to start of singly linked list containing misc info about the sentence. * Param miscInfo: pointer to start of singly linked list containing misc info about the sentence.
* Return value: pointer to sentence Textractor takes for future processing and display. * Return value: pointer to sentence Textractor takes for future processing and display. If nullptr, Textractor will 'eat' the sentence.
* Return 'sentence' unless you created a new sentence/buffer as mentioned above. * Return 'sentence' unless you created a new sentence/buffer as mentioned above.
* Textractor will display the sentence after all extensions have had a chance to process and/or modify it. * Textractor will display the sentence after all extensions have had a chance to process and/or modify it.
* THIS FUNCTION MAY BE RUN SEVERAL TIMES CONCURRENTLY: PLEASE ENSURE THAT IT IS THREAD SAFE! * THIS FUNCTION MAY BE RUN SEVERAL TIMES CONCURRENTLY: PLEASE ENSURE THAT IT IS THREAD SAFE!
*/ */
extern "C" __declspec(dllexport) const wchar_t* OnNewSentence(const wchar_t* sentenceArr, const InfoForExtension* miscInfo) extern "C" __declspec(dllexport) const wchar_t* OnNewSentence(const wchar_t* sentence, const InfoForExtension* miscInfo)
{ {
std::wstring sentence(sentenceArr); try
if (ProcessSentence(sentence, SentenceInfo{ miscInfo })) {
std::wstring sentenceStr(sentence);
if (ProcessSentence(sentenceStr, SentenceInfo{ miscInfo }))
{ {
// No need to worry about freeing this: Textractor does it for you. // No need to worry about freeing this: Textractor does it for you.
wchar_t* newSentence = (wchar_t*)HeapAlloc(GetProcessHeap(), 0, (sentence.size() + 1) * sizeof(wchar_t)); wchar_t* newSentence = (wchar_t*)HeapAlloc(GetProcessHeap(), 0, (sentenceStr.size() + 1) * sizeof(wchar_t));
wcscpy_s(newSentence, sentence.size() + 1, sentence.c_str()); wcscpy_s(newSentence, sentenceStr.size() + 1, sentenceStr.c_str());
return newSentence; return newSentence;
} }
else return sentenceArr; else return sentence;
}
catch (InvalidSentence) { return nullptr; }
} }

View File

@ -25,7 +25,7 @@ bool RemoveRepeatedChars(std::wstring& sentence)
bool RemoveCyclicRepeats(std::wstring& sentence) bool RemoveCyclicRepeats(std::wstring& sentence)
{ {
if (sentence == L"") throw std::exception(); if (sentence == L"") throw InvalidSentence();
int junkLength = 0; int junkLength = 0;
wchar_t junk[2000] = {}; wchar_t junk[2000] = {};
while (wcsstr(sentence.c_str() + junkLength, junk)) while (wcsstr(sentence.c_str() + junkLength, junk))
@ -47,7 +47,7 @@ bool RemoveRepeatedSentences(std::wstring& sentence, int64_t handle)
static std::set<std::pair<int64_t, std::wstring>> seenSentences; static std::set<std::pair<int64_t, std::wstring>> seenSentences;
static std::mutex m; static std::mutex m;
std::lock_guard<std::mutex> l(m); std::lock_guard<std::mutex> l(m);
if (seenSentences.count({ handle, sentence }) != 0) throw std::exception(); if (seenSentences.count({ handle, sentence }) != 0) throw InvalidSentence();
seenSentences.insert({ handle, sentence }); seenSentences.insert({ handle, sentence });
return false; return false;
} }