@shikijs/engine-oniguruma 1.17.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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Pine Wu
4
+ Copyright (c) 2023 Anthony Fu <https://github.com/antfu>
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # @shikijs/engine-oniguruma
2
+
3
+ Engine for Shiki using Oniguruma RegExp engine in WebAssembly.
4
+
5
+ [Documentation](https://shiki.style/guide/regex-engines)
6
+
7
+ ## License
8
+
9
+ MIT
@@ -0,0 +1,70 @@
1
+ interface IOnigCaptureIndex {
2
+ start: number;
3
+ end: number;
4
+ length: number;
5
+ }
6
+ interface IOnigMatch {
7
+ index: number;
8
+ captureIndices: IOnigCaptureIndex[];
9
+ }
10
+ declare const enum FindOption {
11
+ None = 0,
12
+ /**
13
+ * equivalent of ONIG_OPTION_NOT_BEGIN_STRING: (str) isn't considered as begin of string (* fail \A)
14
+ */
15
+ NotBeginString = 1,
16
+ /**
17
+ * equivalent of ONIG_OPTION_NOT_END_STRING: (end) isn't considered as end of string (* fail \z, \Z)
18
+ */
19
+ NotEndString = 2,
20
+ /**
21
+ * equivalent of ONIG_OPTION_NOT_BEGIN_POSITION: (start) isn't considered as start position of search (* fail \G)
22
+ */
23
+ NotBeginPosition = 4,
24
+ /**
25
+ * used for debugging purposes.
26
+ */
27
+ DebugCall = 8
28
+ }
29
+ interface OnigScanner {
30
+ findNextMatchSync(string: string | OnigString, startPosition: number, options: OrMask<FindOption>): IOnigMatch | null;
31
+ dispose?(): void;
32
+ }
33
+ interface OnigString {
34
+ readonly content: string;
35
+ dispose?(): void;
36
+ }
37
+
38
+ /**
39
+ * A union of given const enum values.
40
+ */
41
+ type OrMask<T extends number> = number;
42
+
43
+ type Awaitable<T> = T | Promise<T>;
44
+
45
+ interface PatternScanner extends OnigScanner {
46
+ }
47
+ interface RegexEngineString extends OnigString {
48
+ }
49
+ /**
50
+ * Engine for RegExp matching and scanning.
51
+ */
52
+ interface RegexEngine {
53
+ createScanner: (patterns: string[]) => PatternScanner;
54
+ createString: (s: string) => RegexEngineString;
55
+ }
56
+ interface WebAssemblyInstantiator {
57
+ (importObject: Record<string, Record<string, WebAssembly.ImportValue>> | undefined): Promise<WebAssemblyInstance>;
58
+ }
59
+ type WebAssemblyInstance = WebAssembly.WebAssemblyInstantiatedSource | WebAssembly.Instance | WebAssembly.Instance['exports'];
60
+ type OnigurumaLoadOptions = {
61
+ instantiator: WebAssemblyInstantiator;
62
+ } | {
63
+ default: WebAssemblyInstantiator;
64
+ } | {
65
+ data: ArrayBufferView | ArrayBuffer | Response;
66
+ };
67
+ type LoadWasmOptionsPlain = OnigurumaLoadOptions | WebAssemblyInstantiator | ArrayBufferView | ArrayBuffer | Response;
68
+ type LoadWasmOptions = Awaitable<LoadWasmOptionsPlain> | (() => Awaitable<LoadWasmOptionsPlain>);
69
+
70
+ export type { LoadWasmOptions as L, RegexEngine as R, WebAssemblyInstantiator as W };
@@ -0,0 +1,7 @@
1
+ import { L as LoadWasmOptions, R as RegexEngine } from './chunk-index.d.mjs';
2
+
3
+ declare function loadWasm(options: LoadWasmOptions): Promise<void>;
4
+
5
+ declare function createWasmOnigEngine(options?: LoadWasmOptions | null): Promise<RegexEngine>;
6
+
7
+ export { createWasmOnigEngine, loadWasm };
package/dist/index.mjs ADDED
@@ -0,0 +1,485 @@
1
+ class ShikiError extends Error {
2
+ constructor(message) {
3
+ super(message);
4
+ this.name = 'ShikiError';
5
+ }
6
+ }
7
+
8
+ function getHeapMax() {
9
+ return 2147483648;
10
+ }
11
+ function _emscripten_get_now() {
12
+ return typeof performance !== 'undefined' ? performance.now() : Date.now();
13
+ }
14
+ const alignUp = (x, multiple) => x + ((multiple - (x % multiple)) % multiple);
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 { }
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;
48
+ }
49
+ return false;
50
+ }
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;
91
+ }
92
+ function UTF8ToString(ptr, maxBytesToRead) {
93
+ return ptr ? UTF8ArrayToString(binding.HEAPU8, ptr, maxBytesToRead) : '';
94
+ }
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,
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;
114
+ }
115
+
116
+ /* ---------------------------------------------------------
117
+ * Copyright (C) Microsoft Corporation. All rights reserved.
118
+ *-------------------------------------------------------- */
119
+ let onigBinding = null;
120
+ // let defaultDebugCall = false
121
+ function throwLastOnigError(onigBinding) {
122
+ throw new ShikiError(onigBinding.UTF8ToString(onigBinding.getLastOnigError()));
123
+ }
124
+ 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++;
152
+ }
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++;
231
+ }
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;
243
+ }
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);
276
+ }
277
+ }
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;
287
+ }
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;
297
+ }
298
+ dispose() {
299
+ if (this.ptr === OnigString._sharedPtr)
300
+ OnigString._sharedPtrInUse = false;
301
+ else
302
+ this._onigBinding.ofree(this.ptr);
303
+ }
304
+ }
305
+ 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;
331
+ }
332
+ dispose() {
333
+ this._onigBinding.freeOnigScanner(this._ptr);
334
+ }
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);
350
+ }
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
+ };
380
+ }
381
+ }
382
+ function isInstantiatorOptionsObject(dataOrOptions) {
383
+ return (typeof dataOrOptions.instantiator === 'function');
384
+ }
385
+ function isInstantiatorModule(dataOrOptions) {
386
+ return (typeof dataOrOptions.default === 'function');
387
+ }
388
+ function isDataOptionsObject(dataOrOptions) {
389
+ return (typeof dataOrOptions.data !== 'undefined');
390
+ }
391
+ function isResponse(dataOrOptions) {
392
+ return (typeof Response !== 'undefined' && dataOrOptions instanceof Response);
393
+ }
394
+ 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);
400
+ }
401
+ let initPromise;
402
+ 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();
448
+ return initPromise;
449
+ }
450
+ function _makeArrayBufferLoader(data) {
451
+ return importObject => WebAssembly.instantiate(data, importObject);
452
+ }
453
+ function _makeResponseStreamingLoader(data) {
454
+ return importObject => WebAssembly.instantiateStreaming(data, importObject);
455
+ }
456
+ function _makeResponseNonStreamingLoader(data) {
457
+ return async (importObject) => {
458
+ const arrayBuffer = await data.arrayBuffer();
459
+ return WebAssembly.instantiate(arrayBuffer, importObject);
460
+ };
461
+ }
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
+
472
+ async function createWasmOnigEngine(options) {
473
+ if (options)
474
+ await loadWasm(options);
475
+ return {
476
+ createScanner(patterns) {
477
+ return new OnigScanner(patterns);
478
+ },
479
+ createString(s) {
480
+ return new OnigString(s);
481
+ },
482
+ };
483
+ }
484
+
485
+ export { createWasmOnigEngine, loadWasm };
@@ -0,0 +1 @@
1
+ declare const binary: ArrayBuffer; export default binary;
@@ -0,0 +1,6 @@
1
+ import { W as WebAssemblyInstantiator } from './chunk-index.d.mjs';
2
+
3
+ declare const wasmBinary: ArrayBuffer;
4
+ declare const getWasmInstance: WebAssemblyInstantiator;
5
+
6
+ export { getWasmInstance as default, getWasmInstance, wasmBinary };