mirror of
https://github.com/hanzoai/dev-desktop.git
synced 2026-11-20 23:08:14 +00:00
33 lines
1.3 KiB
C++
33 lines
1.3 KiB
C++
#include "HybridHanzoHighlights.hpp"
|
|
|
|
#include <cstdint>
|
|
#include <mutex>
|
|
|
|
#include "hanzo_highlights.h"
|
|
|
|
namespace margelo::nitro::hanzohighlights {
|
|
|
|
namespace {
|
|
// The lexer's memory is process-global and every JS runtime shares it.
|
|
std::mutex lexerMutex;
|
|
} // namespace
|
|
|
|
std::optional<std::shared_ptr<ArrayBuffer>> HybridHanzoHighlights::tokenize(SourceText source,
|
|
double language) {
|
|
if (!(language >= 0 && language <= UINT8_MAX)) return std::nullopt;
|
|
const auto languageId = static_cast<uint8_t>(language);
|
|
if (languageId != language) return std::nullopt;
|
|
|
|
std::lock_guard lock(lexerMutex);
|
|
const uint32_t* records = nullptr;
|
|
const int64_t count = hanzo_highlights_tokenize(reinterpret_cast<const uint8_t*>(source.utf8.data()),
|
|
source.utf8.size(), languageId, &records);
|
|
if (count < 0) return std::nullopt;
|
|
const auto byteCount = static_cast<size_t>(count) * sizeof(uint32_t);
|
|
if (records == nullptr || byteCount == 0) return ArrayBuffer::allocate(0);
|
|
// The records live in the lexer's memory until its next run.
|
|
return ArrayBuffer::copy(reinterpret_cast<const uint8_t*>(records), byteCount);
|
|
}
|
|
|
|
} // namespace margelo::nitro::hanzohighlights
|