Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

HybridT3Highlights.cpp

32 lines1.2 KB

1#include "HybridT3Highlights.hpp"
2​
3#include <cstdint>
4#include <mutex>
5​
6#include "t3_highlights.h"
7​
8namespace margelo::nitro::t3highlights {
9​
10namespace {
11// The lexer's memory is process-global and every JS runtime shares it.
12std::mutex lexerMutex;
13} // namespace
14​
15std::optional<std::shared_ptr<ArrayBuffer>> HybridT3Highlights::tokenize(SourceText source,
16 double language) {
17 if (!(language >= 0 && language <= UINT8_MAX)) return std::nullopt;
18 const auto languageId = static_cast<uint8_t>(language);
19 if (languageId != language) return std::nullopt;
20​
21 std::lock_guard lock(lexerMutex);
22 const uint32_t* records = nullptr;
23 const int64_t count = t3_highlights_tokenize(reinterpret_cast<const uint8_t*>(source.utf8.data()),
24 source.utf8.size(), languageId, &records);
25 if (count < 0) return std::nullopt;
26 const auto byteCount = static_cast<size_t>(count) * sizeof(uint32_t);
27 if (records == nullptr || byteCount == 0) return ArrayBuffer::allocate(0);
28 // The records live in the lexer's memory until its next run.
29 return ArrayBuffer::copy(reinterpret_cast<const uint8_t*>(records), byteCount);
31​
32} // namespace margelo::nitro::t3highlights