Open History to see the last commit
1#include "t3_highlights.h"
2
3#include <stdbool.h>
4#include <string.h>
5
6#include "highlights.h"
7#include "wasm-rt-impl.h"
8
9// The control block and buffer layout mirror the JavaScript glue in
10// @pierre/highlights (`HighlightsHighlighter` in dist/highlighter.js).
11#define INPUT_OFFSET WASM_DEFAULT_PAGE_SIZE
12#define INPUT_PADDING 96u
13#define MODE_UTF16_LINE_RECORDS 3u
14// Like the JavaScript glue, a small input after a large one gets a fresh
15// instance so the grown memory is released.
16#define RESET_AFTER_PAGES 128u
17
18struct w2c_env {
19 w2c_highlights* instance;
20};
21
22static w2c_highlights instance;
23static struct w2c_env env = {&instance};
24static bool instantiated = false;
25
26#include "generated/id_ranges.inc"
27
28static bool in_ranges(const uint32_t ranges[][2], size_t count, uint32_t code_point) {
29 size_t low = 0;
30 size_t high = count;
31 while (low < high) {
32 size_t mid = low + (high - low) / 2;
33 if (code_point < ranges[mid][0]) {
34 high = mid;
35 } else if (code_point > ranges[mid][1]) {
36 low = mid + 1;
37 } else {
38 return true;
39 }
40 }
41 return false;
42}
43
44// Decodes like the JavaScript glue, including its leniency for malformed
45// sequences, so both builds classify the same bytes the same way.
46static u32 id_class(struct w2c_env* host, u32 ptr, u32 len, bool start) {
47 wasm_rt_memory_t* memory = w2c_highlights_memory(host->instance);
48 if ((uint64_t)ptr + 4 > memory->size) return 0;
49 const uint8_t* b = memory->data + ptr;
50 uint32_t code_point;
51 if (len == 2) {
52 code_point = (uint32_t)(b[0] & 31) << 6 | (b[1] & 63);
53 } else if (len == 3) {
54 code_point = (uint32_t)(b[0] & 15) << 12 | (uint32_t)(b[1] & 63) << 6 | (b[2] & 63);
55 } else {
56 code_point = (uint32_t)(b[0] & 7) << 18 | (uint32_t)(b[1] & 63) << 12 |
57 (uint32_t)(b[2] & 63) << 6 | (b[3] & 63);
58 }
59 if (code_point > 0x10ffff) return 0;
60 return start ? in_ranges(id_start_ranges, sizeof(id_start_ranges) / sizeof(id_start_ranges[0]),
61 code_point)
62 : in_ranges(id_continue_ranges,
63 sizeof(id_continue_ranges) / sizeof(id_continue_ranges[0]), code_point);
64}
65
66u32 w2c_env_is_id_start(struct w2c_env* host, u32 ptr, u32 len) {
67 return id_class(host, ptr, len, true);
68}
69
70u32 w2c_env_is_id_continue(struct w2c_env* host, u32 ptr, u32 len) {
71 return id_class(host, ptr, len, false);
72}
73
74static void reset_instance(void) {
75 if (!instantiated) return;
76 wasm2c_highlights_free(&instance);
77 instantiated = false;
78}
79
80static void ensure_instance(void) {
81 if (instantiated) return;
82 wasm_rt_init();
83 wasm2c_highlights_instantiate(&instance, &env);
84 instantiated = true;
85}
86
87static void write_u32(uint8_t* at, uint32_t value) {
88 at[0] = value & 0xff;
89 at[1] = (value >> 8) & 0xff;
90 at[2] = (value >> 16) & 0xff;
91 at[3] = value >> 24;
92}
93
94static uint32_t read_u32(const uint8_t* at) {
95 return (uint32_t)at[0] | (uint32_t)at[1] << 8 | (uint32_t)at[2] << 16 | (uint32_t)at[3] << 24;
96}
97
98int64_t t3_highlights_tokenize(const uint8_t* source, size_t length, uint8_t language,
99 const uint32_t** records) {
100 *records = NULL;
101 if (length > UINT32_MAX - WASM_DEFAULT_PAGE_SIZE * 2) return -1;
102
103 if (instantiated && w2c_highlights_memory(&instance)->pages > RESET_AFTER_PAGES &&
104 length <= WASM_DEFAULT_PAGE_SIZE) {
105 reset_instance();
106 }
107 ensure_instance();
108
109 wasm_rt_memory_t* memory = w2c_highlights_memory(&instance);
110 uint64_t needed_pages = 1 + (length + INPUT_PADDING + WASM_DEFAULT_PAGE_SIZE - 1) / WASM_DEFAULT_PAGE_SIZE;
111 if (needed_pages > memory->pages &&
112 wasm_rt_grow_memory(memory, needed_pages - memory->pages) == (uint64_t)-1) {
113 return -1;
114 }
115
116 uint8_t* data = memory->data;
117 if (length > 0) memcpy(data + INPUT_OFFSET, source, length);
118 data[INPUT_OFFSET + length] = 0;
119 data[0] = language;
120 data[1] = MODE_UTF16_LINE_RECORDS;
121 write_u32(data + 2, (uint32_t)length);
122
123 // `wasm_rt_impl_try` without the exception-handling runtime this module does
124 // not use. A trap longjmps back here.
125 WASM_RT_SAVE_STACK_DEPTH();
126 if (WASM_RT_SETJMP(g_wasm_rt_jmp_buf) != WASM_RT_TRAP_NONE) {
127 reset_instance();
128 return -1;
129 }
130 w2c_highlights_highlight(&instance);
131
132 // The lexer can grow memory, which moves it.
133 data = memory->data;
134 uint32_t start = read_u32(data + 6);
135 uint32_t byte_length = read_u32(data + 10);
136 if ((uint64_t)start + byte_length > memory->size || start % 4 != 0) return -1;
137 *records = (const uint32_t*)(data + start);
138 return byte_length / 4;
139}