@shikijs/engine-oniguruma 1.25.1 → 1.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,505 +1,455 @@
1
1
  class ShikiError extends Error {
2
- constructor(message) {
3
- super(message);
4
- this.name = 'ShikiError';
5
- }
2
+ constructor(message) {
3
+ super(message);
4
+ this.name = "ShikiError";
5
+ }
6
6
  }
7
7
 
8
8
  function getHeapMax() {
9
- return 2147483648;
9
+ return 2147483648;
10
10
  }
11
11
  function _emscripten_get_now() {
12
- return typeof performance !== 'undefined' ? performance.now() : Date.now();
12
+ return typeof performance !== "undefined" ? performance.now() : Date.now();
13
13
  }
14
- const alignUp = (x, multiple) => x + ((multiple - (x % multiple)) % multiple);
14
+ const alignUp = (x, multiple) => x + (multiple - x % multiple) % multiple;
15
15
  async function main(init) {
16
- let wasmMemory;
17
- let buffer;
18
- const binding = {};
19
- function updateGlobalBufferAndViews(buf) {
20
- buffer = buf;
21
- binding.HEAPU8 = new Uint8Array(buf);
22
- binding.HEAPU32 = new Uint32Array(buf);
23
- }
24
- function _emscripten_memcpy_big(dest, src, num) {
25
- binding.HEAPU8.copyWithin(dest, src, src + num);
26
- }
27
- function emscripten_realloc_buffer(size) {
28
- try {
29
- wasmMemory.grow((size - buffer.byteLength + 65535) >>> 16);
30
- updateGlobalBufferAndViews(wasmMemory.buffer);
31
- return 1;
32
- }
33
- catch { }
16
+ let wasmMemory;
17
+ let buffer;
18
+ const binding = {};
19
+ function updateGlobalBufferAndViews(buf) {
20
+ buffer = buf;
21
+ binding.HEAPU8 = new Uint8Array(buf);
22
+ binding.HEAPU32 = new Uint32Array(buf);
23
+ }
24
+ function _emscripten_memcpy_big(dest, src, num) {
25
+ binding.HEAPU8.copyWithin(dest, src, src + num);
26
+ }
27
+ function emscripten_realloc_buffer(size) {
28
+ try {
29
+ wasmMemory.grow(size - buffer.byteLength + 65535 >>> 16);
30
+ updateGlobalBufferAndViews(wasmMemory.buffer);
31
+ return 1;
32
+ } catch {
34
33
  }
35
- function _emscripten_resize_heap(requestedSize) {
36
- const oldSize = binding.HEAPU8.length;
37
- requestedSize = requestedSize >>> 0;
38
- const maxHeapSize = getHeapMax();
39
- if (requestedSize > maxHeapSize)
40
- return false;
41
- for (let cutDown = 1; cutDown <= 4; cutDown *= 2) {
42
- let overGrownHeapSize = oldSize * (1 + 0.2 / cutDown);
43
- overGrownHeapSize = Math.min(overGrownHeapSize, requestedSize + 100663296);
44
- const newSize = Math.min(maxHeapSize, alignUp(Math.max(requestedSize, overGrownHeapSize), 65536));
45
- const replacement = emscripten_realloc_buffer(newSize);
46
- if (replacement)
47
- return true;
48
- }
49
- return false;
34
+ }
35
+ function _emscripten_resize_heap(requestedSize) {
36
+ const oldSize = binding.HEAPU8.length;
37
+ requestedSize = requestedSize >>> 0;
38
+ const maxHeapSize = getHeapMax();
39
+ if (requestedSize > maxHeapSize)
40
+ return false;
41
+ for (let cutDown = 1; cutDown <= 4; cutDown *= 2) {
42
+ let overGrownHeapSize = oldSize * (1 + 0.2 / cutDown);
43
+ overGrownHeapSize = Math.min(overGrownHeapSize, requestedSize + 100663296);
44
+ const newSize = Math.min(maxHeapSize, alignUp(Math.max(requestedSize, overGrownHeapSize), 65536));
45
+ const replacement = emscripten_realloc_buffer(newSize);
46
+ if (replacement)
47
+ return true;
50
48
  }
51
- const UTF8Decoder = typeof TextDecoder != 'undefined' ? new TextDecoder('utf8') : undefined;
52
- function UTF8ArrayToString(heapOrArray, idx, maxBytesToRead = 1024) {
53
- const endIdx = idx + maxBytesToRead;
54
- let endPtr = idx;
55
- while (heapOrArray[endPtr] && !(endPtr >= endIdx))
56
- ++endPtr;
57
- if (endPtr - idx > 16 && heapOrArray.buffer && UTF8Decoder) {
58
- return UTF8Decoder.decode(heapOrArray.subarray(idx, endPtr));
59
- }
60
- let str = '';
61
- while (idx < endPtr) {
62
- let u0 = heapOrArray[idx++];
63
- if (!(u0 & 128)) {
64
- str += String.fromCharCode(u0);
65
- continue;
66
- }
67
- const u1 = heapOrArray[idx++] & 63;
68
- if ((u0 & 224) === 192) {
69
- str += String.fromCharCode(((u0 & 31) << 6) | u1);
70
- continue;
71
- }
72
- const u2 = heapOrArray[idx++] & 63;
73
- if ((u0 & 240) === 224) {
74
- u0 = ((u0 & 15) << 12) | (u1 << 6) | u2;
75
- }
76
- else {
77
- u0 = ((u0 & 7) << 18)
78
- | (u1 << 12)
79
- | (u2 << 6)
80
- | (heapOrArray[idx++] & 63);
81
- }
82
- if (u0 < 65536) {
83
- str += String.fromCharCode(u0);
84
- }
85
- else {
86
- const ch = u0 - 65536;
87
- str += String.fromCharCode(55296 | (ch >> 10), 56320 | (ch & 1023));
88
- }
89
- }
90
- return str;
49
+ return false;
50
+ }
51
+ const UTF8Decoder = typeof TextDecoder != "undefined" ? new TextDecoder("utf8") : void 0;
52
+ function UTF8ArrayToString(heapOrArray, idx, maxBytesToRead = 1024) {
53
+ const endIdx = idx + maxBytesToRead;
54
+ let endPtr = idx;
55
+ while (heapOrArray[endPtr] && !(endPtr >= endIdx))
56
+ ++endPtr;
57
+ if (endPtr - idx > 16 && heapOrArray.buffer && UTF8Decoder) {
58
+ return UTF8Decoder.decode(heapOrArray.subarray(idx, endPtr));
91
59
  }
92
- function UTF8ToString(ptr, maxBytesToRead) {
93
- return ptr ? UTF8ArrayToString(binding.HEAPU8, ptr, maxBytesToRead) : '';
60
+ let str = "";
61
+ while (idx < endPtr) {
62
+ let u0 = heapOrArray[idx++];
63
+ if (!(u0 & 128)) {
64
+ str += String.fromCharCode(u0);
65
+ continue;
66
+ }
67
+ const u1 = heapOrArray[idx++] & 63;
68
+ if ((u0 & 224) === 192) {
69
+ str += String.fromCharCode((u0 & 31) << 6 | u1);
70
+ continue;
71
+ }
72
+ const u2 = heapOrArray[idx++] & 63;
73
+ if ((u0 & 240) === 224) {
74
+ u0 = (u0 & 15) << 12 | u1 << 6 | u2;
75
+ } else {
76
+ u0 = (u0 & 7) << 18 | u1 << 12 | u2 << 6 | heapOrArray[idx++] & 63;
77
+ }
78
+ if (u0 < 65536) {
79
+ str += String.fromCharCode(u0);
80
+ } else {
81
+ const ch = u0 - 65536;
82
+ str += String.fromCharCode(55296 | ch >> 10, 56320 | ch & 1023);
83
+ }
94
84
  }
95
- const asmLibraryArg = {
96
- emscripten_get_now: _emscripten_get_now,
97
- emscripten_memcpy_big: _emscripten_memcpy_big,
98
- emscripten_resize_heap: _emscripten_resize_heap,
99
- fd_write: () => 0,
85
+ return str;
86
+ }
87
+ function UTF8ToString(ptr, maxBytesToRead) {
88
+ return ptr ? UTF8ArrayToString(binding.HEAPU8, ptr, maxBytesToRead) : "";
89
+ }
90
+ const asmLibraryArg = {
91
+ emscripten_get_now: _emscripten_get_now,
92
+ emscripten_memcpy_big: _emscripten_memcpy_big,
93
+ emscripten_resize_heap: _emscripten_resize_heap,
94
+ fd_write: () => 0
95
+ };
96
+ async function createWasm() {
97
+ const info = {
98
+ env: asmLibraryArg,
99
+ wasi_snapshot_preview1: asmLibraryArg
100
100
  };
101
- async function createWasm() {
102
- const info = {
103
- env: asmLibraryArg,
104
- wasi_snapshot_preview1: asmLibraryArg,
105
- };
106
- const exports = await init(info);
107
- wasmMemory = exports.memory;
108
- updateGlobalBufferAndViews(wasmMemory.buffer);
109
- Object.assign(binding, exports);
110
- binding.UTF8ToString = UTF8ToString;
111
- }
112
- await createWasm();
113
- return binding;
101
+ const exports = await init(info);
102
+ wasmMemory = exports.memory;
103
+ updateGlobalBufferAndViews(wasmMemory.buffer);
104
+ Object.assign(binding, exports);
105
+ binding.UTF8ToString = UTF8ToString;
106
+ }
107
+ await createWasm();
108
+ return binding;
114
109
  }
115
110
 
116
- /* ---------------------------------------------------------
117
- * Copyright (C) Microsoft Corporation. All rights reserved.
118
- *-------------------------------------------------------- */
111
+ var __defProp = Object.defineProperty;
112
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
113
+ var __publicField = (obj, key, value) => {
114
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
115
+ return value;
116
+ };
119
117
  let onigBinding = null;
120
- // let defaultDebugCall = false
121
- function throwLastOnigError(onigBinding) {
122
- throw new ShikiError(onigBinding.UTF8ToString(onigBinding.getLastOnigError()));
118
+ function throwLastOnigError(onigBinding2) {
119
+ throw new ShikiError(onigBinding2.UTF8ToString(onigBinding2.getLastOnigError()));
123
120
  }
124
121
  class UtfString {
125
- static _utf8ByteLength(str) {
126
- let result = 0;
127
- for (let i = 0, len = str.length; i < len; i++) {
128
- const charCode = str.charCodeAt(i);
129
- let codepoint = charCode;
130
- let wasSurrogatePair = false;
131
- if (charCode >= 0xD800 && charCode <= 0xDBFF) {
132
- // Hit a high surrogate, try to look for a matching low surrogate
133
- if (i + 1 < len) {
134
- const nextCharCode = str.charCodeAt(i + 1);
135
- if (nextCharCode >= 0xDC00 && nextCharCode <= 0xDFFF) {
136
- // Found the matching low surrogate
137
- codepoint = (((charCode - 0xD800) << 10) + 0x10000) | (nextCharCode - 0xDC00);
138
- wasSurrogatePair = true;
139
- }
140
- }
141
- }
142
- if (codepoint <= 0x7F)
143
- result += 1;
144
- else if (codepoint <= 0x7FF)
145
- result += 2;
146
- else if (codepoint <= 0xFFFF)
147
- result += 3;
148
- else
149
- result += 4;
150
- if (wasSurrogatePair)
151
- i++;
122
+ constructor(str) {
123
+ __publicField(this, "utf16Length");
124
+ __publicField(this, "utf8Length");
125
+ __publicField(this, "utf16Value");
126
+ __publicField(this, "utf8Value");
127
+ __publicField(this, "utf16OffsetToUtf8");
128
+ __publicField(this, "utf8OffsetToUtf16");
129
+ const utf16Length = str.length;
130
+ const utf8Length = UtfString._utf8ByteLength(str);
131
+ const computeIndicesMapping = utf8Length !== utf16Length;
132
+ const utf16OffsetToUtf8 = computeIndicesMapping ? new Uint32Array(utf16Length + 1) : null;
133
+ if (computeIndicesMapping)
134
+ utf16OffsetToUtf8[utf16Length] = utf8Length;
135
+ const utf8OffsetToUtf16 = computeIndicesMapping ? new Uint32Array(utf8Length + 1) : null;
136
+ if (computeIndicesMapping)
137
+ utf8OffsetToUtf16[utf8Length] = utf16Length;
138
+ const utf8Value = new Uint8Array(utf8Length);
139
+ let i8 = 0;
140
+ for (let i16 = 0; i16 < utf16Length; i16++) {
141
+ const charCode = str.charCodeAt(i16);
142
+ let codePoint = charCode;
143
+ let wasSurrogatePair = false;
144
+ if (charCode >= 55296 && charCode <= 56319) {
145
+ if (i16 + 1 < utf16Length) {
146
+ const nextCharCode = str.charCodeAt(i16 + 1);
147
+ if (nextCharCode >= 56320 && nextCharCode <= 57343) {
148
+ codePoint = (charCode - 55296 << 10) + 65536 | nextCharCode - 56320;
149
+ wasSurrogatePair = true;
150
+ }
152
151
  }
153
- return result;
154
- }
155
- utf16Length;
156
- utf8Length;
157
- utf16Value;
158
- utf8Value;
159
- utf16OffsetToUtf8;
160
- utf8OffsetToUtf16;
161
- constructor(str) {
162
- const utf16Length = str.length;
163
- const utf8Length = UtfString._utf8ByteLength(str);
164
- const computeIndicesMapping = (utf8Length !== utf16Length);
165
- const utf16OffsetToUtf8 = computeIndicesMapping ? new Uint32Array(utf16Length + 1) : null;
166
- if (computeIndicesMapping)
167
- utf16OffsetToUtf8[utf16Length] = utf8Length;
168
- const utf8OffsetToUtf16 = computeIndicesMapping ? new Uint32Array(utf8Length + 1) : null;
169
- if (computeIndicesMapping)
170
- utf8OffsetToUtf16[utf8Length] = utf16Length;
171
- const utf8Value = new Uint8Array(utf8Length);
172
- let i8 = 0;
173
- for (let i16 = 0; i16 < utf16Length; i16++) {
174
- const charCode = str.charCodeAt(i16);
175
- let codePoint = charCode;
176
- let wasSurrogatePair = false;
177
- if (charCode >= 0xD800 && charCode <= 0xDBFF) {
178
- // Hit a high surrogate, try to look for a matching low surrogate
179
- if (i16 + 1 < utf16Length) {
180
- const nextCharCode = str.charCodeAt(i16 + 1);
181
- if (nextCharCode >= 0xDC00 && nextCharCode <= 0xDFFF) {
182
- // Found the matching low surrogate
183
- codePoint = (((charCode - 0xD800) << 10) + 0x10000) | (nextCharCode - 0xDC00);
184
- wasSurrogatePair = true;
185
- }
186
- }
187
- }
188
- if (computeIndicesMapping) {
189
- utf16OffsetToUtf8[i16] = i8;
190
- if (wasSurrogatePair)
191
- utf16OffsetToUtf8[i16 + 1] = i8;
192
- if (codePoint <= 0x7F) {
193
- utf8OffsetToUtf16[i8 + 0] = i16;
194
- }
195
- else if (codePoint <= 0x7FF) {
196
- utf8OffsetToUtf16[i8 + 0] = i16;
197
- utf8OffsetToUtf16[i8 + 1] = i16;
198
- }
199
- else if (codePoint <= 0xFFFF) {
200
- utf8OffsetToUtf16[i8 + 0] = i16;
201
- utf8OffsetToUtf16[i8 + 1] = i16;
202
- utf8OffsetToUtf16[i8 + 2] = i16;
203
- }
204
- else {
205
- utf8OffsetToUtf16[i8 + 0] = i16;
206
- utf8OffsetToUtf16[i8 + 1] = i16;
207
- utf8OffsetToUtf16[i8 + 2] = i16;
208
- utf8OffsetToUtf16[i8 + 3] = i16;
209
- }
210
- }
211
- if (codePoint <= 0x7F) {
212
- utf8Value[i8++] = codePoint;
213
- }
214
- else if (codePoint <= 0x7FF) {
215
- utf8Value[i8++] = 0b11000000 | ((codePoint & 0b00000000000000000000011111000000) >>> 6);
216
- utf8Value[i8++] = 0b10000000 | ((codePoint & 0b00000000000000000000000000111111) >>> 0);
217
- }
218
- else if (codePoint <= 0xFFFF) {
219
- utf8Value[i8++] = 0b11100000 | ((codePoint & 0b00000000000000001111000000000000) >>> 12);
220
- utf8Value[i8++] = 0b10000000 | ((codePoint & 0b00000000000000000000111111000000) >>> 6);
221
- utf8Value[i8++] = 0b10000000 | ((codePoint & 0b00000000000000000000000000111111) >>> 0);
222
- }
223
- else {
224
- utf8Value[i8++] = 0b11110000 | ((codePoint & 0b00000000000111000000000000000000) >>> 18);
225
- utf8Value[i8++] = 0b10000000 | ((codePoint & 0b00000000000000111111000000000000) >>> 12);
226
- utf8Value[i8++] = 0b10000000 | ((codePoint & 0b00000000000000000000111111000000) >>> 6);
227
- utf8Value[i8++] = 0b10000000 | ((codePoint & 0b00000000000000000000000000111111) >>> 0);
228
- }
229
- if (wasSurrogatePair)
230
- i16++;
152
+ }
153
+ if (computeIndicesMapping) {
154
+ utf16OffsetToUtf8[i16] = i8;
155
+ if (wasSurrogatePair)
156
+ utf16OffsetToUtf8[i16 + 1] = i8;
157
+ if (codePoint <= 127) {
158
+ utf8OffsetToUtf16[i8 + 0] = i16;
159
+ } else if (codePoint <= 2047) {
160
+ utf8OffsetToUtf16[i8 + 0] = i16;
161
+ utf8OffsetToUtf16[i8 + 1] = i16;
162
+ } else if (codePoint <= 65535) {
163
+ utf8OffsetToUtf16[i8 + 0] = i16;
164
+ utf8OffsetToUtf16[i8 + 1] = i16;
165
+ utf8OffsetToUtf16[i8 + 2] = i16;
166
+ } else {
167
+ utf8OffsetToUtf16[i8 + 0] = i16;
168
+ utf8OffsetToUtf16[i8 + 1] = i16;
169
+ utf8OffsetToUtf16[i8 + 2] = i16;
170
+ utf8OffsetToUtf16[i8 + 3] = i16;
231
171
  }
232
- this.utf16Length = utf16Length;
233
- this.utf8Length = utf8Length;
234
- this.utf16Value = str;
235
- this.utf8Value = utf8Value;
236
- this.utf16OffsetToUtf8 = utf16OffsetToUtf8;
237
- this.utf8OffsetToUtf16 = utf8OffsetToUtf16;
238
- }
239
- createString(onigBinding) {
240
- const result = onigBinding.omalloc(this.utf8Length);
241
- onigBinding.HEAPU8.set(this.utf8Value, result);
242
- return result;
172
+ }
173
+ if (codePoint <= 127) {
174
+ utf8Value[i8++] = codePoint;
175
+ } else if (codePoint <= 2047) {
176
+ utf8Value[i8++] = 192 | (codePoint & 1984) >>> 6;
177
+ utf8Value[i8++] = 128 | (codePoint & 63) >>> 0;
178
+ } else if (codePoint <= 65535) {
179
+ utf8Value[i8++] = 224 | (codePoint & 61440) >>> 12;
180
+ utf8Value[i8++] = 128 | (codePoint & 4032) >>> 6;
181
+ utf8Value[i8++] = 128 | (codePoint & 63) >>> 0;
182
+ } else {
183
+ utf8Value[i8++] = 240 | (codePoint & 1835008) >>> 18;
184
+ utf8Value[i8++] = 128 | (codePoint & 258048) >>> 12;
185
+ utf8Value[i8++] = 128 | (codePoint & 4032) >>> 6;
186
+ utf8Value[i8++] = 128 | (codePoint & 63) >>> 0;
187
+ }
188
+ if (wasSurrogatePair)
189
+ i16++;
243
190
  }
244
- }
245
- class OnigString {
246
- static LAST_ID = 0;
247
- static _sharedPtr = 0; // a pointer to a string of 10000 bytes
248
- static _sharedPtrInUse = false;
249
- id = (++OnigString.LAST_ID);
250
- _onigBinding;
251
- content;
252
- utf16Length;
253
- utf8Length;
254
- utf16OffsetToUtf8;
255
- utf8OffsetToUtf16;
256
- ptr;
257
- constructor(str) {
258
- if (!onigBinding)
259
- throw new ShikiError('Must invoke loadWasm first.');
260
- this._onigBinding = onigBinding;
261
- this.content = str;
262
- const utfString = new UtfString(str);
263
- this.utf16Length = utfString.utf16Length;
264
- this.utf8Length = utfString.utf8Length;
265
- this.utf16OffsetToUtf8 = utfString.utf16OffsetToUtf8;
266
- this.utf8OffsetToUtf16 = utfString.utf8OffsetToUtf16;
267
- if (this.utf8Length < 10000 && !OnigString._sharedPtrInUse) {
268
- if (!OnigString._sharedPtr)
269
- OnigString._sharedPtr = onigBinding.omalloc(10000);
270
- OnigString._sharedPtrInUse = true;
271
- onigBinding.HEAPU8.set(utfString.utf8Value, OnigString._sharedPtr);
272
- this.ptr = OnigString._sharedPtr;
273
- }
274
- else {
275
- this.ptr = utfString.createString(onigBinding);
191
+ this.utf16Length = utf16Length;
192
+ this.utf8Length = utf8Length;
193
+ this.utf16Value = str;
194
+ this.utf8Value = utf8Value;
195
+ this.utf16OffsetToUtf8 = utf16OffsetToUtf8;
196
+ this.utf8OffsetToUtf16 = utf8OffsetToUtf16;
197
+ }
198
+ static _utf8ByteLength(str) {
199
+ let result = 0;
200
+ for (let i = 0, len = str.length; i < len; i++) {
201
+ const charCode = str.charCodeAt(i);
202
+ let codepoint = charCode;
203
+ let wasSurrogatePair = false;
204
+ if (charCode >= 55296 && charCode <= 56319) {
205
+ if (i + 1 < len) {
206
+ const nextCharCode = str.charCodeAt(i + 1);
207
+ if (nextCharCode >= 56320 && nextCharCode <= 57343) {
208
+ codepoint = (charCode - 55296 << 10) + 65536 | nextCharCode - 56320;
209
+ wasSurrogatePair = true;
210
+ }
276
211
  }
212
+ }
213
+ if (codepoint <= 127)
214
+ result += 1;
215
+ else if (codepoint <= 2047)
216
+ result += 2;
217
+ else if (codepoint <= 65535)
218
+ result += 3;
219
+ else
220
+ result += 4;
221
+ if (wasSurrogatePair)
222
+ i++;
277
223
  }
278
- convertUtf8OffsetToUtf16(utf8Offset) {
279
- if (this.utf8OffsetToUtf16) {
280
- if (utf8Offset < 0)
281
- return 0;
282
- if (utf8Offset > this.utf8Length)
283
- return this.utf16Length;
284
- return this.utf8OffsetToUtf16[utf8Offset];
285
- }
286
- return utf8Offset;
224
+ return result;
225
+ }
226
+ createString(onigBinding2) {
227
+ const result = onigBinding2.omalloc(this.utf8Length);
228
+ onigBinding2.HEAPU8.set(this.utf8Value, result);
229
+ return result;
230
+ }
231
+ }
232
+ const _OnigString = class {
233
+ constructor(str) {
234
+ __publicField(this, "id", ++_OnigString.LAST_ID);
235
+ __publicField(this, "_onigBinding");
236
+ __publicField(this, "content");
237
+ __publicField(this, "utf16Length");
238
+ __publicField(this, "utf8Length");
239
+ __publicField(this, "utf16OffsetToUtf8");
240
+ __publicField(this, "utf8OffsetToUtf16");
241
+ __publicField(this, "ptr");
242
+ if (!onigBinding)
243
+ throw new ShikiError("Must invoke loadWasm first.");
244
+ this._onigBinding = onigBinding;
245
+ this.content = str;
246
+ const utfString = new UtfString(str);
247
+ this.utf16Length = utfString.utf16Length;
248
+ this.utf8Length = utfString.utf8Length;
249
+ this.utf16OffsetToUtf8 = utfString.utf16OffsetToUtf8;
250
+ this.utf8OffsetToUtf16 = utfString.utf8OffsetToUtf16;
251
+ if (this.utf8Length < 1e4 && !_OnigString._sharedPtrInUse) {
252
+ if (!_OnigString._sharedPtr)
253
+ _OnigString._sharedPtr = onigBinding.omalloc(1e4);
254
+ _OnigString._sharedPtrInUse = true;
255
+ onigBinding.HEAPU8.set(utfString.utf8Value, _OnigString._sharedPtr);
256
+ this.ptr = _OnigString._sharedPtr;
257
+ } else {
258
+ this.ptr = utfString.createString(onigBinding);
287
259
  }
288
- convertUtf16OffsetToUtf8(utf16Offset) {
289
- if (this.utf16OffsetToUtf8) {
290
- if (utf16Offset < 0)
291
- return 0;
292
- if (utf16Offset > this.utf16Length)
293
- return this.utf8Length;
294
- return this.utf16OffsetToUtf8[utf16Offset];
295
- }
296
- return utf16Offset;
260
+ }
261
+ convertUtf8OffsetToUtf16(utf8Offset) {
262
+ if (this.utf8OffsetToUtf16) {
263
+ if (utf8Offset < 0)
264
+ return 0;
265
+ if (utf8Offset > this.utf8Length)
266
+ return this.utf16Length;
267
+ return this.utf8OffsetToUtf16[utf8Offset];
297
268
  }
298
- dispose() {
299
- if (this.ptr === OnigString._sharedPtr)
300
- OnigString._sharedPtrInUse = false;
301
- else
302
- this._onigBinding.ofree(this.ptr);
269
+ return utf8Offset;
270
+ }
271
+ convertUtf16OffsetToUtf8(utf16Offset) {
272
+ if (this.utf16OffsetToUtf8) {
273
+ if (utf16Offset < 0)
274
+ return 0;
275
+ if (utf16Offset > this.utf16Length)
276
+ return this.utf8Length;
277
+ return this.utf16OffsetToUtf8[utf16Offset];
303
278
  }
304
- }
279
+ return utf16Offset;
280
+ }
281
+ dispose() {
282
+ if (this.ptr === _OnigString._sharedPtr)
283
+ _OnigString._sharedPtrInUse = false;
284
+ else
285
+ this._onigBinding.ofree(this.ptr);
286
+ }
287
+ };
288
+ let OnigString = _OnigString;
289
+ __publicField(OnigString, "LAST_ID", 0);
290
+ __publicField(OnigString, "_sharedPtr", 0);
291
+ // a pointer to a string of 10000 bytes
292
+ __publicField(OnigString, "_sharedPtrInUse", false);
305
293
  class OnigScanner {
306
- _onigBinding;
307
- _ptr;
308
- constructor(patterns) {
309
- if (!onigBinding)
310
- throw new ShikiError('Must invoke loadWasm first.');
311
- const strPtrsArr = [];
312
- const strLenArr = [];
313
- for (let i = 0, len = patterns.length; i < len; i++) {
314
- const utfString = new UtfString(patterns[i]);
315
- strPtrsArr[i] = utfString.createString(onigBinding);
316
- strLenArr[i] = utfString.utf8Length;
317
- }
318
- const strPtrsPtr = onigBinding.omalloc(4 * patterns.length);
319
- onigBinding.HEAPU32.set(strPtrsArr, strPtrsPtr / 4);
320
- const strLenPtr = onigBinding.omalloc(4 * patterns.length);
321
- onigBinding.HEAPU32.set(strLenArr, strLenPtr / 4);
322
- const scannerPtr = onigBinding.createOnigScanner(strPtrsPtr, strLenPtr, patterns.length);
323
- for (let i = 0, len = patterns.length; i < len; i++)
324
- onigBinding.ofree(strPtrsArr[i]);
325
- onigBinding.ofree(strLenPtr);
326
- onigBinding.ofree(strPtrsPtr);
327
- if (scannerPtr === 0)
328
- throwLastOnigError(onigBinding);
329
- this._onigBinding = onigBinding;
330
- this._ptr = scannerPtr;
294
+ constructor(patterns) {
295
+ __publicField(this, "_onigBinding");
296
+ __publicField(this, "_ptr");
297
+ if (!onigBinding)
298
+ throw new ShikiError("Must invoke loadWasm first.");
299
+ const strPtrsArr = [];
300
+ const strLenArr = [];
301
+ for (let i = 0, len = patterns.length; i < len; i++) {
302
+ const utfString = new UtfString(patterns[i]);
303
+ strPtrsArr[i] = utfString.createString(onigBinding);
304
+ strLenArr[i] = utfString.utf8Length;
331
305
  }
332
- dispose() {
333
- this._onigBinding.freeOnigScanner(this._ptr);
306
+ const strPtrsPtr = onigBinding.omalloc(4 * patterns.length);
307
+ onigBinding.HEAPU32.set(strPtrsArr, strPtrsPtr / 4);
308
+ const strLenPtr = onigBinding.omalloc(4 * patterns.length);
309
+ onigBinding.HEAPU32.set(strLenArr, strLenPtr / 4);
310
+ const scannerPtr = onigBinding.createOnigScanner(strPtrsPtr, strLenPtr, patterns.length);
311
+ for (let i = 0, len = patterns.length; i < len; i++)
312
+ onigBinding.ofree(strPtrsArr[i]);
313
+ onigBinding.ofree(strLenPtr);
314
+ onigBinding.ofree(strPtrsPtr);
315
+ if (scannerPtr === 0)
316
+ throwLastOnigError(onigBinding);
317
+ this._onigBinding = onigBinding;
318
+ this._ptr = scannerPtr;
319
+ }
320
+ dispose() {
321
+ this._onigBinding.freeOnigScanner(this._ptr);
322
+ }
323
+ findNextMatchSync(string, startPosition, arg) {
324
+ let options = 0 /* None */;
325
+ if (typeof arg === "number") {
326
+ options = arg;
334
327
  }
335
- findNextMatchSync(string, startPosition, arg) {
336
- // let debugCall = defaultDebugCall
337
- let options = 0 /* FindOption.None */;
338
- if (typeof arg === 'number') {
339
- // if (arg & FindOption.DebugCall)
340
- // debugCall = true
341
- options = arg;
342
- }
343
- if (typeof string === 'string') {
344
- string = new OnigString(string);
345
- const result = this._findNextMatchSync(string, startPosition, false, options);
346
- string.dispose();
347
- return result;
348
- }
349
- return this._findNextMatchSync(string, startPosition, false, options);
328
+ if (typeof string === "string") {
329
+ string = new OnigString(string);
330
+ const result = this._findNextMatchSync(string, startPosition, false, options);
331
+ string.dispose();
332
+ return result;
350
333
  }
351
- _findNextMatchSync(string, startPosition, debugCall, options) {
352
- const onigBinding = this._onigBinding;
353
- // let resultPtr: Pointer
354
- // if (debugCall)
355
- // resultPtr = onigBinding.findNextOnigScannerMatchDbg(this._ptr, string.id, string.ptr, string.utf8Length, string.convertUtf16OffsetToUtf8(startPosition), options)
356
- // else
357
- const resultPtr = onigBinding.findNextOnigScannerMatch(this._ptr, string.id, string.ptr, string.utf8Length, string.convertUtf16OffsetToUtf8(startPosition), options);
358
- if (resultPtr === 0) {
359
- // no match
360
- return null;
361
- }
362
- const HEAPU32 = onigBinding.HEAPU32;
363
- let offset = resultPtr / 4; // byte offset -> uint32 offset
364
- const index = HEAPU32[offset++];
365
- const count = HEAPU32[offset++];
366
- const captureIndices = [];
367
- for (let i = 0; i < count; i++) {
368
- const beg = string.convertUtf8OffsetToUtf16(HEAPU32[offset++]);
369
- const end = string.convertUtf8OffsetToUtf16(HEAPU32[offset++]);
370
- captureIndices[i] = {
371
- start: beg,
372
- end,
373
- length: end - beg,
374
- };
375
- }
376
- return {
377
- index,
378
- captureIndices,
379
- };
334
+ return this._findNextMatchSync(string, startPosition, false, options);
335
+ }
336
+ _findNextMatchSync(string, startPosition, debugCall, options) {
337
+ const onigBinding2 = this._onigBinding;
338
+ const resultPtr = onigBinding2.findNextOnigScannerMatch(this._ptr, string.id, string.ptr, string.utf8Length, string.convertUtf16OffsetToUtf8(startPosition), options);
339
+ if (resultPtr === 0) {
340
+ return null;
380
341
  }
342
+ const HEAPU32 = onigBinding2.HEAPU32;
343
+ let offset = resultPtr / 4;
344
+ const index = HEAPU32[offset++];
345
+ const count = HEAPU32[offset++];
346
+ const captureIndices = [];
347
+ for (let i = 0; i < count; i++) {
348
+ const beg = string.convertUtf8OffsetToUtf16(HEAPU32[offset++]);
349
+ const end = string.convertUtf8OffsetToUtf16(HEAPU32[offset++]);
350
+ captureIndices[i] = {
351
+ start: beg,
352
+ end,
353
+ length: end - beg
354
+ };
355
+ }
356
+ return {
357
+ index,
358
+ captureIndices
359
+ };
360
+ }
381
361
  }
382
362
  function isInstantiatorOptionsObject(dataOrOptions) {
383
- return (typeof dataOrOptions.instantiator === 'function');
363
+ return typeof dataOrOptions.instantiator === "function";
384
364
  }
385
365
  function isInstantiatorModule(dataOrOptions) {
386
- return (typeof dataOrOptions.default === 'function');
366
+ return typeof dataOrOptions.default === "function";
387
367
  }
388
368
  function isDataOptionsObject(dataOrOptions) {
389
- return (typeof dataOrOptions.data !== 'undefined');
369
+ return typeof dataOrOptions.data !== "undefined";
390
370
  }
391
371
  function isResponse(dataOrOptions) {
392
- return (typeof Response !== 'undefined' && dataOrOptions instanceof Response);
372
+ return typeof Response !== "undefined" && dataOrOptions instanceof Response;
393
373
  }
394
374
  function isArrayBuffer(data) {
395
- return (typeof ArrayBuffer !== 'undefined' && (data instanceof ArrayBuffer || ArrayBuffer.isView(data)))
396
- // eslint-disable-next-line node/prefer-global/buffer
397
- || (typeof Buffer !== 'undefined' && Buffer.isBuffer?.(data))
398
- || (typeof SharedArrayBuffer !== 'undefined' && data instanceof SharedArrayBuffer)
399
- || (typeof Uint32Array !== 'undefined' && data instanceof Uint32Array);
375
+ return typeof ArrayBuffer !== "undefined" && (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) || typeof Buffer !== "undefined" && Buffer.isBuffer?.(data) || typeof SharedArrayBuffer !== "undefined" && data instanceof SharedArrayBuffer || typeof Uint32Array !== "undefined" && data instanceof Uint32Array;
400
376
  }
401
377
  let initPromise;
402
378
  function loadWasm(options) {
403
- if (initPromise)
404
- return initPromise;
405
- async function _load() {
406
- onigBinding = await main(async (info) => {
407
- let instance = options;
408
- instance = await instance;
409
- if (typeof instance === 'function')
410
- instance = await instance(info);
411
- if (typeof instance === 'function')
412
- instance = await instance(info);
413
- if (isInstantiatorOptionsObject(instance)) {
414
- instance = await instance.instantiator(info);
415
- }
416
- else if (isInstantiatorModule(instance)) {
417
- instance = await instance.default(info);
418
- }
419
- else {
420
- if (isDataOptionsObject(instance))
421
- instance = instance.data;
422
- if (isResponse(instance)) {
423
- if (typeof WebAssembly.instantiateStreaming === 'function')
424
- instance = await _makeResponseStreamingLoader(instance)(info);
425
- else
426
- instance = await _makeResponseNonStreamingLoader(instance)(info);
427
- }
428
- else if (isArrayBuffer(instance)) {
429
- instance = await _makeArrayBufferLoader(instance)(info);
430
- }
431
- // import("shiki/onig.wasm") returns `{ default: WebAssembly.Module }` on cloudflare workers
432
- // https://developers.cloudflare.com/workers/wrangler/bundling/
433
- else if (instance instanceof WebAssembly.Module) {
434
- instance = await _makeArrayBufferLoader(instance)(info);
435
- }
436
- else if ('default' in instance && instance.default instanceof WebAssembly.Module) {
437
- instance = await _makeArrayBufferLoader(instance.default)(info);
438
- }
439
- }
440
- if ('instance' in instance)
441
- instance = instance.instance;
442
- if ('exports' in instance)
443
- instance = instance.exports;
444
- return instance;
445
- });
446
- }
447
- initPromise = _load();
379
+ if (initPromise)
448
380
  return initPromise;
381
+ async function _load() {
382
+ onigBinding = await main(async (info) => {
383
+ let instance = options;
384
+ instance = await instance;
385
+ if (typeof instance === "function")
386
+ instance = await instance(info);
387
+ if (typeof instance === "function")
388
+ instance = await instance(info);
389
+ if (isInstantiatorOptionsObject(instance)) {
390
+ instance = await instance.instantiator(info);
391
+ } else if (isInstantiatorModule(instance)) {
392
+ instance = await instance.default(info);
393
+ } else {
394
+ if (isDataOptionsObject(instance))
395
+ instance = instance.data;
396
+ if (isResponse(instance)) {
397
+ if (typeof WebAssembly.instantiateStreaming === "function")
398
+ instance = await _makeResponseStreamingLoader(instance)(info);
399
+ else
400
+ instance = await _makeResponseNonStreamingLoader(instance)(info);
401
+ } else if (isArrayBuffer(instance)) {
402
+ instance = await _makeArrayBufferLoader(instance)(info);
403
+ } else if (instance instanceof WebAssembly.Module) {
404
+ instance = await _makeArrayBufferLoader(instance)(info);
405
+ } else if ("default" in instance && instance.default instanceof WebAssembly.Module) {
406
+ instance = await _makeArrayBufferLoader(instance.default)(info);
407
+ }
408
+ }
409
+ if ("instance" in instance)
410
+ instance = instance.instance;
411
+ if ("exports" in instance)
412
+ instance = instance.exports;
413
+ return instance;
414
+ });
415
+ }
416
+ initPromise = _load();
417
+ return initPromise;
449
418
  }
450
419
  function _makeArrayBufferLoader(data) {
451
- return importObject => WebAssembly.instantiate(data, importObject);
420
+ return (importObject) => WebAssembly.instantiate(data, importObject);
452
421
  }
453
422
  function _makeResponseStreamingLoader(data) {
454
- return importObject => WebAssembly.instantiateStreaming(data, importObject);
423
+ return (importObject) => WebAssembly.instantiateStreaming(data, importObject);
455
424
  }
456
425
  function _makeResponseNonStreamingLoader(data) {
457
- return async (importObject) => {
458
- const arrayBuffer = await data.arrayBuffer();
459
- return WebAssembly.instantiate(arrayBuffer, importObject);
460
- };
426
+ return async (importObject) => {
427
+ const arrayBuffer = await data.arrayBuffer();
428
+ return WebAssembly.instantiate(arrayBuffer, importObject);
429
+ };
461
430
  }
462
- // export function createOnigString(str: string) {
463
- // return new OnigString(str)
464
- // }
465
- // export function createOnigScanner(patterns: string[]) {
466
- // return new OnigScanner(patterns)
467
- // }
468
- // export function setDefaultDebugCall(_defaultDebugCall: boolean): void {
469
- // defaultDebugCall = _defaultDebugCall
470
- // }
471
431
 
472
432
  let _defaultWasmLoader;
473
- /**
474
- * Set the default wasm loader for `loadWasm`.
475
- * @internal
476
- */
477
433
  function setDefaultWasmLoader(_loader) {
478
- _defaultWasmLoader = _loader;
434
+ _defaultWasmLoader = _loader;
479
435
  }
480
- /**
481
- * @internal
482
- */
483
436
  function getDefaultWasmLoader() {
484
- return _defaultWasmLoader;
437
+ return _defaultWasmLoader;
485
438
  }
486
439
  async function createOnigurumaEngine(options) {
487
- if (options)
488
- await loadWasm(options);
489
- return {
490
- createScanner(patterns) {
491
- return new OnigScanner(patterns);
492
- },
493
- createString(s) {
494
- return new OnigString(s);
495
- },
496
- };
440
+ if (options)
441
+ await loadWasm(options);
442
+ return {
443
+ createScanner(patterns) {
444
+ return new OnigScanner(patterns.map((p) => typeof p === "string" ? p : p.source));
445
+ },
446
+ createString(s) {
447
+ return new OnigString(s);
448
+ }
449
+ };
497
450
  }
498
- /**
499
- * Deprecated. Use `createOnigurumaEngine` instead.
500
- */
501
451
  async function createWasmOnigEngine(options) {
502
- return createOnigurumaEngine(options);
452
+ return createOnigurumaEngine(options);
503
453
  }
504
454
 
505
455
  export { createOnigurumaEngine, createWasmOnigEngine, getDefaultWasmLoader, loadWasm, setDefaultWasmLoader };