@lakutata/ffi 0.0.0-stage → 3.1.5

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.
Files changed (129) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +163 -2
  3. package/README.zh-CN.md +140 -0
  4. package/dist/cjs/components/NativeLibrary.d.ts +229 -0
  5. package/dist/cjs/components/NativeLibrary.js +346 -0
  6. package/dist/cjs/decorators/Library.d.ts +61 -0
  7. package/dist/cjs/decorators/Library.js +44 -0
  8. package/dist/cjs/decorators/Native.d.ts +69 -0
  9. package/dist/cjs/decorators/Native.js +90 -0
  10. package/dist/cjs/exceptions/NativeDeclarationException.d.ts +12 -0
  11. package/dist/cjs/exceptions/NativeDeclarationException.js +19 -0
  12. package/dist/cjs/exceptions/NativeLibraryClosedException.d.ts +12 -0
  13. package/dist/cjs/exceptions/NativeLibraryClosedException.js +19 -0
  14. package/dist/cjs/exceptions/NativeLibraryNotFoundException.d.ts +11 -0
  15. package/dist/cjs/exceptions/NativeLibraryNotFoundException.js +18 -0
  16. package/dist/cjs/exceptions/NativeMemoryException.d.ts +11 -0
  17. package/dist/cjs/exceptions/NativeMemoryException.js +18 -0
  18. package/dist/cjs/exports/FFI.d.ts +12 -0
  19. package/dist/cjs/exports/FFI.js +40 -0
  20. package/dist/cjs/exports/Generator.d.ts +2 -0
  21. package/dist/cjs/exports/Generator.js +10 -0
  22. package/dist/cjs/lib/Memory.d.ts +188 -0
  23. package/dist/cjs/lib/Memory.js +227 -0
  24. package/dist/cjs/lib/Runtime.d.ts +98 -0
  25. package/dist/cjs/lib/Runtime.js +209 -0
  26. package/dist/cjs/lib/backend/Functions.d.ts +65 -0
  27. package/dist/cjs/lib/backend/Functions.js +214 -0
  28. package/dist/cjs/lib/backend/KoffiTypes.d.ts +53 -0
  29. package/dist/cjs/lib/backend/KoffiTypes.js +426 -0
  30. package/dist/cjs/lib/declarations/Builtins.d.ts +33 -0
  31. package/dist/cjs/lib/declarations/Builtins.js +179 -0
  32. package/dist/cjs/lib/declarations/Layout.d.ts +41 -0
  33. package/dist/cjs/lib/declarations/Layout.js +180 -0
  34. package/dist/cjs/lib/declarations/Lexer.d.ts +40 -0
  35. package/dist/cjs/lib/declarations/Lexer.js +325 -0
  36. package/dist/cjs/lib/declarations/Parser.d.ts +90 -0
  37. package/dist/cjs/lib/declarations/Parser.js +1425 -0
  38. package/dist/cjs/lib/declarations/Platform.d.ts +50 -0
  39. package/dist/cjs/lib/declarations/Platform.js +27 -0
  40. package/dist/cjs/lib/declarations/Types.d.ts +173 -0
  41. package/dist/cjs/lib/declarations/Types.js +179 -0
  42. package/dist/cjs/lib/generator/CText.d.ts +14 -0
  43. package/dist/cjs/lib/generator/CText.js +133 -0
  44. package/dist/cjs/lib/generator/Generator.d.ts +136 -0
  45. package/dist/cjs/lib/generator/Generator.js +395 -0
  46. package/dist/cjs/lib/generator/Preprocessor.d.ts +60 -0
  47. package/dist/cjs/lib/generator/Preprocessor.js +94 -0
  48. package/dist/cjs/lib/generator/TypeScriptTypes.d.ts +35 -0
  49. package/dist/cjs/lib/generator/TypeScriptTypes.js +153 -0
  50. package/dist/cjs/lib/library/NativeCallback.d.ts +44 -0
  51. package/dist/cjs/lib/library/NativeCallback.js +66 -0
  52. package/dist/cjs/lib/library/NativeLibraryHandle.d.ts +268 -0
  53. package/dist/cjs/lib/library/NativeLibraryHandle.js +540 -0
  54. package/dist/cjs/lib/library/Resolve.d.ts +49 -0
  55. package/dist/cjs/lib/library/Resolve.js +130 -0
  56. package/dist/cjs/lib/thread/NativeThreads.d.ts +80 -0
  57. package/dist/cjs/lib/thread/NativeThreads.js +384 -0
  58. package/dist/cjs/lib/thread/NativeWorker.d.ts +6 -0
  59. package/dist/cjs/lib/thread/NativeWorker.js +96 -0
  60. package/dist/cjs/lib/thread/NativeWorkerCode.d.ts +5 -0
  61. package/dist/cjs/lib/thread/NativeWorkerCode.js +4 -0
  62. package/dist/cjs/lib/thread/PackageRequire.d.ts +6 -0
  63. package/dist/cjs/lib/thread/PackageRequire.js +4 -0
  64. package/dist/cjs/lib/thread/ThreadProtocol.d.ts +77 -0
  65. package/dist/cjs/lib/thread/ThreadProtocol.js +2 -0
  66. package/dist/cjs/package.json +1 -0
  67. package/dist/esm/components/NativeLibrary.js +338 -0
  68. package/dist/esm/decorators/Library.js +40 -0
  69. package/dist/esm/decorators/Native.js +85 -0
  70. package/dist/esm/exceptions/NativeDeclarationException.js +15 -0
  71. package/dist/esm/exceptions/NativeLibraryClosedException.js +15 -0
  72. package/dist/esm/exceptions/NativeLibraryNotFoundException.js +14 -0
  73. package/dist/esm/exceptions/NativeMemoryException.js +14 -0
  74. package/dist/esm/exports/FFI.js +10 -0
  75. package/dist/esm/exports/Generator.js +2 -0
  76. package/dist/esm/lib/Memory.js +210 -0
  77. package/dist/esm/lib/Runtime.js +192 -0
  78. package/dist/esm/lib/backend/Functions.js +206 -0
  79. package/dist/esm/lib/backend/KoffiTypes.js +418 -0
  80. package/dist/esm/lib/declarations/Builtins.js +174 -0
  81. package/dist/esm/lib/declarations/Layout.js +175 -0
  82. package/dist/esm/lib/declarations/Lexer.js +321 -0
  83. package/dist/esm/lib/declarations/Parser.js +1421 -0
  84. package/dist/esm/lib/declarations/Platform.js +23 -0
  85. package/dist/esm/lib/declarations/Types.js +170 -0
  86. package/dist/esm/lib/generator/CText.js +129 -0
  87. package/dist/esm/lib/generator/Generator.js +388 -0
  88. package/dist/esm/lib/generator/Preprocessor.js +87 -0
  89. package/dist/esm/lib/generator/TypeScriptTypes.js +149 -0
  90. package/dist/esm/lib/library/NativeCallback.js +59 -0
  91. package/dist/esm/lib/library/NativeLibraryHandle.js +532 -0
  92. package/dist/esm/lib/library/Resolve.js +122 -0
  93. package/dist/esm/lib/thread/NativeThreads.js +377 -0
  94. package/dist/esm/lib/thread/NativeWorker.js +93 -0
  95. package/dist/esm/lib/thread/NativeWorkerCode.js +1 -0
  96. package/dist/esm/lib/thread/PackageRequire.js +2 -0
  97. package/dist/esm/lib/thread/ThreadProtocol.js +1 -0
  98. package/dist/types/components/NativeLibrary.d.ts +229 -0
  99. package/dist/types/decorators/Library.d.ts +61 -0
  100. package/dist/types/decorators/Native.d.ts +69 -0
  101. package/dist/types/exceptions/NativeDeclarationException.d.ts +12 -0
  102. package/dist/types/exceptions/NativeLibraryClosedException.d.ts +12 -0
  103. package/dist/types/exceptions/NativeLibraryNotFoundException.d.ts +11 -0
  104. package/dist/types/exceptions/NativeMemoryException.d.ts +11 -0
  105. package/dist/types/exports/FFI.d.ts +12 -0
  106. package/dist/types/exports/Generator.d.ts +2 -0
  107. package/dist/types/lib/Memory.d.ts +188 -0
  108. package/dist/types/lib/Runtime.d.ts +98 -0
  109. package/dist/types/lib/backend/Functions.d.ts +65 -0
  110. package/dist/types/lib/backend/KoffiTypes.d.ts +53 -0
  111. package/dist/types/lib/declarations/Builtins.d.ts +33 -0
  112. package/dist/types/lib/declarations/Layout.d.ts +41 -0
  113. package/dist/types/lib/declarations/Lexer.d.ts +40 -0
  114. package/dist/types/lib/declarations/Parser.d.ts +90 -0
  115. package/dist/types/lib/declarations/Platform.d.ts +50 -0
  116. package/dist/types/lib/declarations/Types.d.ts +173 -0
  117. package/dist/types/lib/generator/CText.d.ts +14 -0
  118. package/dist/types/lib/generator/Generator.d.ts +136 -0
  119. package/dist/types/lib/generator/Preprocessor.d.ts +60 -0
  120. package/dist/types/lib/generator/TypeScriptTypes.d.ts +35 -0
  121. package/dist/types/lib/library/NativeCallback.d.ts +44 -0
  122. package/dist/types/lib/library/NativeLibraryHandle.d.ts +268 -0
  123. package/dist/types/lib/library/Resolve.d.ts +49 -0
  124. package/dist/types/lib/thread/NativeThreads.d.ts +80 -0
  125. package/dist/types/lib/thread/NativeWorker.d.ts +6 -0
  126. package/dist/types/lib/thread/NativeWorkerCode.d.ts +5 -0
  127. package/dist/types/lib/thread/PackageRequire.d.ts +6 -0
  128. package/dist/types/lib/thread/ThreadProtocol.d.ts +77 -0
  129. package/package.json +85 -4
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runNativeWorker = runNativeWorker;
4
+ const node_worker_threads_1 = require("node:worker_threads");
5
+ const NativeLibraryHandle_js_1 = require("../library/NativeLibraryHandle.js");
6
+ const Runtime_js_1 = require("../Runtime.js");
7
+ /**
8
+ * An error as a thread sends it
9
+ * @param error
10
+ */
11
+ function threadError(error) {
12
+ return error instanceof Error ? { name: error.name, message: error.message } : { name: 'Error', message: String(error) };
13
+ }
14
+ /**
15
+ * The buffers of values sent back, transferred rather than copied (they are the copies of the thread)
16
+ * @param values
17
+ */
18
+ function transferables(values) {
19
+ const buffers = new Set();
20
+ for (const value of values)
21
+ if (ArrayBuffer.isView(value) && value.buffer instanceof ArrayBuffer)
22
+ buffers.add(value.buffer);
23
+ return [...buffers];
24
+ }
25
+ /**
26
+ * Serve the calls of a library in this worker thread: it loads the library, binds the functions it is asked to bind
27
+ * (synchronous: the thread is the one waiting), calls them in the order of the messages, and sends back their results,
28
+ * the arguments asked back (the output parameters, the buffers written) and its errno after each call
29
+ */
30
+ function runNativeWorker() {
31
+ const port = node_worker_threads_1.parentPort;
32
+ const data = node_worker_threads_1.workerData;
33
+ const post = (message, transfer = []) => port.postMessage(message, transfer);
34
+ let library;
35
+ try {
36
+ library = new NativeLibraryHandle_js_1.NativeLibraryHandle(data.file, data.declarations, { crt: data.crt });
37
+ }
38
+ catch (error) {
39
+ post({ type: 'failed', error: threadError(error) });
40
+ port.close();
41
+ return;
42
+ }
43
+ //The functions bound, or why they could not be
44
+ const functions = new Map();
45
+ port.on('message', (message) => {
46
+ switch (message.type) {
47
+ case 'bind':
48
+ try {
49
+ functions.set(message.id, message.named ? library.function(message.declaration, message.options) : library.func(message.declaration, message.options));
50
+ }
51
+ catch (error) {
52
+ functions.set(message.id, threadError(error));
53
+ }
54
+ return;
55
+ case 'declare':
56
+ try {
57
+ library.declare(message.declarations);
58
+ }
59
+ catch {
60
+ //The thread of the library read them first: it reported their error
61
+ }
62
+ return;
63
+ case 'call': {
64
+ const bound = functions.get(message.function);
65
+ let value;
66
+ let error;
67
+ if (typeof bound !== 'function') {
68
+ error = bound ?? { name: 'Error', message: 'The function is not bound in the thread' };
69
+ }
70
+ else {
71
+ try {
72
+ value = bound(...message.args);
73
+ }
74
+ catch (thrown) {
75
+ error = threadError(thrown);
76
+ }
77
+ }
78
+ //GetLastError first: reading the errno of the C library of Windows is a call too
79
+ const windowsError = (0, Runtime_js_1.nativeWindowsError)();
80
+ const errno = library.errno();
81
+ const back = message.back.map((index) => message.args[index]);
82
+ post({ type: 'result', id: message.id, ...error ? { error: error } : { value: value }, back: back, errno: errno, ...windowsError !== undefined ? { windowsError: windowsError } : {} }, transferables(back));
83
+ return;
84
+ }
85
+ case 'close': {
86
+ const end = () => {
87
+ post({ type: 'closed' });
88
+ port.close();
89
+ };
90
+ library.close().then(end, end);
91
+ return;
92
+ }
93
+ }
94
+ });
95
+ post({ type: 'ready' });
96
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * The code of the worker threads of the libraries: NativeWorker and what it uses bundled into one script, written by the
3
+ * build (scripts/worker.mjs) in place of this empty one
4
+ */
5
+ export declare const nativeWorkerCode: string;
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.nativeWorkerCode = void 0;
4
+ exports.nativeWorkerCode = "var require=typeof require==='function'?require:typeof process.getBuiltinModule==='function'?process.getBuiltinModule('node:module').createRequire(process.cwd()+'/'):function(){throw new Error('The worker threads of @lakutata/ffi evaluated as an ES module (--input-type=module) need process.getBuiltinModule (Node.js 20.16, 22.3 or later)')};globalThis[Symbol.for('lakutata.ffi.koffi')]=require(require('node:worker_threads').workerData.koffi);\nvar te=Object.create;var dt=Object.defineProperty;var ee=Object.getOwnPropertyDescriptor;var ie=Object.getOwnPropertyNames;var ne=Object.getPrototypeOf;var se=Object.prototype.hasOwnProperty;var f=(s,t)=>dt(s,\"name\",{value:t,configurable:true});var re=(s,t)=>()=>{try{return t||s((t={exports:{}}).exports,t),t.exports}catch(e){throw t=0,e}};var oe=(s,t,e,i)=>{if(t&&typeof t===\"object\"||typeof t===\"function\"){for(let n of ie(t))if(!se.call(s,n)&&n!==e)dt(s,n,{get:()=>t[n],enumerable:!(i=ee(t,n))||i.enumerable})}return s};var S=(s,t,e)=>(e=s!=null?te(ne(s)):{},oe(t||!s||!s.__esModule?dt(e,\"default\",{value:s,enumerable:true}):e,s));var P=re((Be,$t)=>{\"use strict\";$t.exports=globalThis[Symbol.for(\"lakutata.ffi.koffi\")]});var lt=require(\"node:worker_threads\");var F=S(P(),1);var A=class extends Error{static{f(this,\"Exception\")}errno=\"E_EXCEPTION\";constructor(t){super(t);this.name=new.target.name}};var w=class extends A{static{f(this,\"NativeDeclarationException\")}constructor(){super(...arguments);this.errno=\"E_NATIVE_DECLARATION\"}};var L=class extends A{static{f(this,\"NativeLibraryClosedException\")}constructor(){super(...arguments);this.errno=\"E_NATIVE_LIBRARY_CLOSED\"}};var X=S(P(),1);function Tt(){return{types:new Map,tags:new Map,functions:new Map,variables:new Map,constants:new Map,macros:new Map}}f(Tt,\"emptyDeclarations\");var mt={kind:\"primitive\",name:\"void\"};function V(s,t){switch(s){case\"void\":return 0;case\"bool\":case\"char\":case\"schar\":case\"uchar\":case\"int8\":case\"uint8\":return 1;case\"short\":case\"ushort\":case\"int16\":case\"uint16\":case\"char16\":return 2;case\"int\":case\"uint\":case\"int32\":case\"uint32\":case\"char32\":case\"float\":return 4;case\"long\":case\"ulong\":return t.longSize;case\"longlong\":case\"ulonglong\":case\"int64\":case\"uint64\":case\"double\":return 8;case\"size\":case\"ssize\":case\"intptr\":case\"uintptr\":case\"ptrdiff\":return t.pointerSize;case\"wchar\":return t.wcharSize}}f(V,\"primitiveSize\");function nt(s,t){switch(s){case\"char\":return true;case\"schar\":case\"short\":case\"int\":case\"long\":case\"longlong\":case\"int8\":case\"int16\":case\"int32\":case\"int64\":case\"ssize\":case\"intptr\":case\"ptrdiff\":case\"float\":case\"double\":return true;case\"wchar\":return t.os!==\"win32\";default:return false}}f(nt,\"primitiveSigned\");function D(s){switch(s){case\"long\":case\"ulong\":case\"longlong\":case\"ulonglong\":case\"int64\":case\"uint64\":case\"size\":case\"ssize\":case\"intptr\":case\"uintptr\":case\"ptrdiff\":return true;default:return false}}f(D,\"primitiveBig\");function x(s){switch(s.kind){case\"primitive\":return ae[s.name];case\"pointer\":return`${s.constTarget?\"const \":\"\"}${x(s.to)} *`;case\"array\":return`${x(s.of)}[${s.length}]`;case\"struct\":case\"union\":return`${s.kind} ${s.tag??\"(anonymous)\"}`;case\"enum\":return`enum ${s.tag??\"(anonymous)\"}`;case\"function\":return`${x(s.result)} (${s.parameters.map(t=>x(t.type)).concat(s.variadic?[\"...\"]:[]).join(\", \")})`}}f(x,\"typeText\");var ae={void:\"void\",bool:\"bool\",char:\"char\",schar:\"signed char\",uchar:\"unsigned char\",short:\"short\",ushort:\"unsigned short\",int:\"int\",uint:\"unsigned int\",long:\"long\",ulong:\"unsigned long\",longlong:\"long long\",ulonglong:\"unsigned long long\",int8:\"int8_t\",uint8:\"uint8_t\",int16:\"int16_t\",uint16:\"uint16_t\",int32:\"int32_t\",uint32:\"uint32_t\",int64:\"int64_t\",uint64:\"uint64_t\",size:\"size_t\",ssize:\"ssize_t\",intptr:\"intptr_t\",uintptr:\"uintptr_t\",ptrdiff:\"ptrdiff_t\",char16:\"char16_t\",char32:\"char32_t\",wchar:\"wchar_t\",float:\"float\",double:\"double\"};var E=S(P(),1);var Et=new WeakMap;function j(s,t){switch(s.kind){case\"primitive\":return V(s.name,t);case\"pointer\":return t.pointerSize;case\"array\":return j(s.of,t)*s.length;case\"enum\":return V(s.base,t);case\"struct\":case\"union\":return U(s,t).size;case\"function\":throw new w(`A function has no size: ${x(s)}`)}}f(j,\"sizeOf\");function z(s,t){switch(s.kind){case\"primitive\":case\"enum\":{const e=V(s.kind===\"primitive\"?s.name:s.base,t);return e===8?t.int64Alignment:Math.max(e,1)}case\"pointer\":return t.pointerSize;case\"array\":return z(s.of,t);case\"struct\":case\"union\":return U(s,t).alignment;case\"function\":throw new w(`A function has no alignment: ${x(s)}`)}}f(z,\"alignmentOf\");function C(s,t){return Math.ceil(s/t)*t}f(C,\"alignUp\");function U(s,t){let e=Et.get(s);const i=e?.get(t);if(i)return i;if(!s.members)throw new w(`The ${s.kind} ${s.tag??\"(anonymous)\"} is declared but not defined: it is used through pointers only`);const n=s.kind===\"union\"?ce(s,t):ue(s,t);if(!e)Et.set(s,e=new Map);e.set(t,n);return n}f(U,\"layoutOf\");function Ot(s,t,e){const i=Math.max(z(s.type,e),s.align??1);return t.pack?Math.min(i,t.pack):i}f(Ot,\"memberAlignment\");function ce(s,t){let e=0;let i=s.align??1;const n=[];for(const r of s.members){if(r.bits!==void 0)throw new w(`The bitfield ${r.name||\"(unnamed)\"} of a union is not supported`);const o=j(r.type,t);i=Math.max(i,Ot(r,s,t));e=Math.max(e,o);n.push({member:r,offset:0,size:o})}return{size:C(e,i),alignment:i,members:n,bitfields:false}}f(ce,\"unionLayout\");function ue(s,t){let e=0;let i=s.align??1;let n=false;let r;const o=[];for(const a of s.members){if(a.bits===void 0){r=void 0;const y=Ot(a,s,t);const v=C(Math.ceil(e/8),y);const l=j(a.type,t);o.push({member:a,offset:v,size:l});e=(v+l)*8;i=Math.max(i,y);continue}n=true;if(s.pack!==void 0)throw new w(`The bitfield ${a.name||\"(unnamed)\"} of a packed struct is not supported`);if(a.type.kind!==\"primitive\"&&a.type.kind!==\"enum\")throw new w(`The bitfield ${a.name||\"(unnamed)\"} must have an integer type`);const c=j(a.type,t);const h=z(a.type,t);const p=a.bits;if(p>c*8)throw new w(`The bitfield ${a.name||\"(unnamed)\"} is wider than its type (${p} bits)`);if(t.bitfields===\"msvc\"){if(p===0){if(r){e=C((r.offset+r.size)*8,h*8);i=Math.max(i,h)}r=void 0;continue}if(!r||r.size!==c||r.used+p>c*8){const y=r?r.offset+r.size:Math.ceil(e/8);r={offset:C(y,h),size:c,used:0};i=Math.max(i,h)}o.push({member:a,offset:r.offset,size:c,bitOffset:r.offset*8+r.used,bits:p});r.used+=p;e=(r.offset+r.size)*8;continue}if(a.name||t.bitfields===\"aapcs\")i=Math.max(i,h);if(p===0){e=C(e,h*8);continue}if(e%(c*8)+p>c*8)e=C(e,h*8);o.push({member:a,offset:Math.floor(e/(c*8))*c,size:c,bitOffset:e,bits:p});e+=p}if(r)e=(r.offset+r.size)*8;return{size:C(Math.ceil(e/8),i),alignment:i,members:o,bitfields:n}}f(ue,\"structLayout\");var At={void:\"void\",bool:\"bool\",char:\"char\",schar:\"int8_t\",uchar:\"uchar\",short:\"short\",ushort:\"ushort\",int:\"int\",uint:\"uint\",long:\"long\",ulong:\"ulong\",longlong:\"longlong\",ulonglong:\"ulonglong\",int8:\"int8_t\",uint8:\"uint8_t\",int16:\"int16_t\",uint16:\"uint16_t\",int32:\"int32_t\",uint32:\"uint32_t\",int64:\"int64_t\",uint64:\"uint64_t\",size:\"size_t\",ssize:\"intptr_t\",intptr:\"intptr_t\",uintptr:\"uintptr_t\",ptrdiff:\"intptr_t\",char16:\"char16_t\",char32:\"char32_t\",wchar:\"wchar_t\",float:\"float\",double:\"double\"};var B=\"__lakutata_\";function pt(s){return typeof s===\"number\"?BigInt(s):s}f(pt,\"toBigInt\");function Lt(s){if(s.kind===\"primitive\")return s.name;if(s.kind===\"enum\")return s.base;return void 0}f(Lt,\"integerName\");function It(s,t){return Math.ceil(s/t)*t}f(It,\"alignUp\");var st=class{static{f(this,\"KoffiTypes\")}#r;#n=new WeakMap;#c=new WeakMap;#a=new WeakMap;constructor(t){this.#r=t}type(t){switch(t.kind){case\"primitive\":return At[t.name];case\"enum\":return At[t.base];case\"pointer\":return this.#o(t.to);case\"array\":return this.#u(t,()=>E.default.array(this.type(t.of),t.length));case\"struct\":case\"union\":if(t.tag===\"va_list\"&&!t.members)throw new w(\"A va_list cannot be passed: call the function of the library that takes the arguments themselves\");return this.#f(t);case\"function\":throw new w(`A function is not a value: use a pointer to it (${x(t)})`)}}prototype(t){const e=this.#a.get(t);if(e)return e;if(t.variadic)throw new w(`A callback cannot be variadic: ${x(t)}`);const i=E.default.proto(`__${t.convention}`,this.type(t.result),t.parameters.map(n=>this.type(n.type)));this.#a.set(t,i);return i}converter(t){const e=this.#c.get(t);if(e!==void 0)return e;let i=null;switch(t.kind){case\"primitive\":case\"enum\":if(D(Lt(t)))i={fromNative:pt};break;case\"array\":{const n=this.converter(t.of);if(n){i={...n.toNative?{toNative:f(r=>Array.isArray(r)?r.map(n.toNative):r,\"toNative\")}:{},...n.fromNative?{fromNative:f(r=>r&&typeof r!==\"string\"?Array.from(r,n.fromNative):r,\"fromNative\")}:{}}}break}case\"struct\":case\"union\":if(t.members){this.#c.set(t,null);i=this.#v(t)}break}this.#c.set(t,i);return i}#o(t){if(t.kind===\"primitive\"){if(t.name===\"char\")return\"char *\";if(t.name===\"char16\")return\"char16_t *\";if(t.name===\"wchar\")return\"wchar_t *\";if(t.name===\"void\")return\"void *\"}if(t.kind===\"function\")return this.#u(t,()=>E.default.pointer(this.prototype(t)));if((t.kind===\"struct\"||t.kind===\"union\")&&!t.members)return this.#u(t,()=>E.default.pointer(E.default.opaque()));return E.default.pointer(this.type(t))}#u(t,e){let i=this.#n.get(t);if(!i){i=e();this.#n.set(t,i)}return i}#f(t){const e=this.#n.get(t);if(e)return e;const i=U(t,this.#r);const n=E.default.opaque();this.#n.set(t,n);const r=this.#b(t,i);const o=t.kind===\"union\"?E.default.union(n,r):E.default.struct(n,r);this.#n.set(t,o);const a=t.kind===\"struct\"?i.members.find(c=>c.bits===void 0&&!!o.members?.[c.member.name]&&o.members[c.member.name].offset!==c.offset):void 0;if(o.size!==i.size||o.alignment!==i.alignment||a){throw new w(`The layout of ${x(t)} could not be given to koffi (${o.size} bytes aligned on ${o.alignment}, ${i.size} on ${i.alignment} expected${a?`, ${a.member.name} at ${o.members[a.member.name].offset}, ${a.offset} expected`:\"\"})`)}return o}#b(t,e){const i=[];if(t.kind===\"union\"){for(const c of e.members)i.push({name:c.member.name,type:this.type(c.member.type),offset:0,size:c.size,alignment:this.#h(c.member,t)})}else{let c=0;for(const h of this.#l(e)){if(\"bitfields\"in h){i.push({name:`${B}bits${c++}`,type:E.default.array(\"uint8_t\",h.size,\"Typed\"),offset:h.offset,size:h.size,alignment:1});continue}if(h.member.type.kind===\"array\"&&h.member.type.length===0)continue;i.push({name:h.member.name,type:this.type(h.member.type),offset:h.offset,size:h.size,alignment:this.#h(h.member,t)})}}const n={};let r=0;let o=1;let a=0;i.forEach((c,h)=>{if(t.kind===\"struct\"&&It(r,c.alignment)!==c.offset){n[`${B}pad${a++}`]=[1,E.default.array(\"uint8_t\",c.offset-r,\"Typed\")];r=c.offset}const p=h===0&&e.alignment>c.alignment&&this.#w(i,e)?e.alignment:c.alignment;n[c.name]=p===this.#p(c.type)?c.type:[p,c.type];o=Math.max(o,p);r=t.kind===\"struct\"?c.offset+c.size:Math.max(r,c.size)});if(!i.length)n[`${B}pad${a++}`]=[e.alignment,E.default.array(\"uint8_t\",e.size||1,\"Typed\")];else if(t.kind===\"struct\"&&It(r,Math.max(o,e.alignment))<e.size)n[`${B}pad${a++}`]=[1,E.default.array(\"uint8_t\",e.size-r,\"Typed\")];return n}#w(t,e){return Math.max(1,...t.map(i=>i.alignment))<e.alignment}#p(t){return E.default.type(t).alignment}#h(t,e){const i=Math.max(z(t.type,this.#r),t.align??1);return e.pack?Math.min(i,e.pack):i}#l(t){const e=[];for(const i of t.members){if(i.bits===void 0){e.push({member:i.member,offset:i.offset,size:i.size});continue}const n=Math.floor(i.bitOffset/8);const r=Math.ceil((i.bitOffset+i.bits)/8);const o=e[e.length-1];if(o&&\"bitfields\"in o&&n<=o.offset+o.size){o.bitfields.push(i);o.size=Math.max(o.size,r-o.offset)}else{e.push({bitfields:[i],offset:n,size:r-n})}}return e}#v(t){const e=U(t,this.#r);const i=t.kind===\"struct\"?this.#l(e):e.members.map(l=>({member:l.member,offset:0,size:l.size}));const n=this.#f(t);const r=Object.keys(n.members??{}).some(l=>l.startsWith(B));const o=[];const a=[];let c=0;for(const l of i){if(\"bitfields\"in l){const m=`${B}bits${c++}`;for(const u of l.bitfields){if(!u.member.name)continue;const d=Lt(u.member.type);a.push({name:u.member.name,chunk:m,offset:u.bitOffset-l.offset*8,bits:u.bits,signed:nt(d,this.#r),big:D(d)})}continue}if(l.member.type.kind===\"array\"&&l.member.type.length===0)continue;o.push({name:l.member.name,converter:this.converter(l.member.type)})}const h=o.filter(l=>!!l.converter);if(!h.length&&!a.length&&!r)return null;const p=new Map(i.filter(l=>\"bitfields\"in l).map((l,m)=>[`${B}bits${m}`,l.size]));const y=h.filter(l=>!!l.converter.toNative);const v=y.length>0||a.length>0;return{...v?{toNative:f(l=>{if(!l||typeof l!==\"object\"||ArrayBuffer.isView(l))return l;const m={...l};for(const u of y)if(u.name in m)m[u.name]=u.converter.toNative(m[u.name]);if(a.length){const u=new Map([...p].map(([d,g])=>[d,new Uint8Array(g)]));for(const d of a){const g=m[d.name];delete m[d.name];if(g===void 0||g===null)continue;he(u.get(d.chunk),d.offset,d.bits,BigInt(g))}for(const[d,g]of u)m[d]=g}return m},\"toNative\")}:{},fromNative:f(l=>{if(!l||typeof l!==\"object\")return l;const m={};for(const u of o)if(u.name in l)m[u.name]=u.converter?.fromNative?u.converter.fromNative(l[u.name]):l[u.name];for(const u of a){const d=fe(l[u.chunk],u.offset,u.bits,u.signed);m[u.name]=u.big?d:Number(d)}return m},\"fromNative\")}}};function fe(s,t,e,i){let n=0n;for(let r=e-1;r>=0;r--){const o=t+r;n=n<<1n|BigInt(s[o>>3]>>(o&7)&1)}return i?BigInt.asIntN(e,n):n}f(fe,\"readBits\");function he(s,t,e,i){const n=BigInt.asUintN(e,i);for(let r=0;r<e;r++){const o=t+r;if(n>>BigInt(r)&1n)s[o>>3]|=1<<(o&7);else s[o>>3]&=~(1<<(o&7))}}f(he,\"writeBits\");function zt(s){if(typeof s!==\"bigint\")return s;if(s>BigInt(Number.MAX_SAFE_INTEGER)||s<BigInt(Number.MIN_SAFE_INTEGER))throw new RangeError(`${s} is not a safe integer: declare the function without int64: 'number' to get a bigint`);return Number(s)}f(zt,\"toSafeNumber\");function St(s){return s.kind===\"pointer\"&&s.to.kind===\"primitive\"&&[\"char\",\"char16\",\"wchar\"].includes(s.to.name)}f(St,\"isString\");function Pt(s,t){const e=s.to.name;return X.default.disposable(e===\"char\"?\"char *\":e===\"char16\"?\"char16_t *\":\"wchar_t *\",t)}f(Pt,\"disposableString\");function Y(s,t,e){const i=s.parameters.map(r=>t.converter(r.type));const n=t.converter(s.result);if(!i.some(r=>!!r?.fromNative)&&!n?.toNative)return e;return(...r)=>{for(let a=0;a<i.length;a++)if(i[a]?.fromNative)r[a]=i[a].fromNative(r[a]);const o=e(...r);return n?.toNative?n.toNative(o):o}}f(Y,\"convertedCallback\");function le(s,t,e){const i=[];s.parameters.forEach((n,r)=>{const o=n.type.kind===\"pointer\"?n.type.to:void 0;if(n.direction===\"in\"){if(o?.kind===\"function\"){const h=o;const p=f(()=>void 0,\"probe\");if(Y(h,t,p)===p)return;i.push({index:r,before:f(y=>typeof y===\"function\"?Y(h,t,y):y,\"before\")});return}const c=t.converter(o&&(o.kind===\"struct\"||o.kind===\"union\")?o:n.type);if(c?.toNative&&(n.type.kind!==\"pointer\"||o?.kind===\"struct\"||o?.kind===\"union\")){i.push({index:r,before:f(h=>_t(h)?c.toNative(h):h,\"before\")})}return}if(!o)return;if(o.kind===\"primitive\"&&D(o.name)||o.kind===\"enum\"&&D(o.base)){const c=e.int64===\"number\"?zt:pt;i.push({index:r,after:f(h=>{if(Array.isArray(h))h[0]=c(h[0])},\"after\")});return}const a=t.converter(o);if(a&&(o.kind===\"struct\"||o.kind===\"union\")){i.push({index:r,before:f(c=>_t(c)?n.direction===\"inout\"&&a.toNative?a.toNative(c):{...c}:c,\"before\"),after:f((c,h)=>{if(_t(c)&&h!==c)Object.assign(c,a.fromNative?a.fromNative(h):h)},\"after\")})}});return i}f(le,\"argumentSteps\");function _t(s){return!!s&&typeof s===\"object\"&&!ArrayBuffer.isView(s)&&!Array.isArray(s)&&Object.getPrototypeOf(s)===Object.prototype}f(_t,\"isPlainObject\");function Rt(s,t,e,i,n={}){const r=t.type;if(n.async&&r.variadic)throw new w(`The variadic function ${t.name} cannot be called asynchronously`);const o=n.free&&St(r.result)?Pt(r.result,n.free):e.type(r.result);const a=r.parameters.map(m=>{const u=m.type.kind===\"pointer\"?m.type.to:void 0;if(m.direction===\"in\")return e.type(m.type);if(!u)throw new w(`The parameter ${m.name??\"\"} of ${t.name} is ${m.direction===\"out\"?\"_Out_\":\"_Inout_\"} but not a pointer (${x(m.type)})`.replace(\" \",\" \"));const d=n.free&&St(u)?X.default.pointer(Pt(u,n.free)):e.type(m.type);return m.direction===\"out\"?X.default.out(d):X.default.inout(d)});let c;try{c=s.func(`__${r.convention}`,t.symbol,o,r.variadic?[...a,\"...\"]:a)}catch(m){throw new w(`The function ${t.name} cannot be bound (${t.symbol}): ${m.message}`)}const h=le(r,e,n);const p=n.int64===\"number\"&&(r.result.kind===\"primitive\"&&D(r.result.name))?zt:e.converter(r.result)?.fromNative;const y=r.parameters.length;const v=f(m=>{const u=m.slice();for(const d of h)if(d.before)u[d.index]=d.before(m[d.index]);if(r.variadic){for(let d=y;d<u.length;d+=2)if(typeof u[d]===\"string\")u[d]=i(u[d])}return u},\"prepare\");const l=f((m,u,d)=>{for(const g of h)if(g.after)g.after(m[g.index],u[g.index]);return p?p(d):d},\"finish\");if(!n.async){if(!h.length&&!p&&!r.variadic)return c;return(...m)=>{const u=v(m);return l(m,u,c(...u))}}return(...m)=>{try{n.calls?.start()}catch(d){return Promise.reject(d)}let u;try{u=v(m)}catch(d){n.calls?.end();return Promise.reject(d)}return new Promise((d,g)=>{try{c.async(...u,(b,N)=>{n.calls?.end();if(b)return g(b);try{d(l(m,u,N))}catch(k){g(k)}})}catch(b){n.calls?.end();g(b)}})}}f(Rt,\"bindFunction\");var _=f(s=>({kind:\"primitive\",name:s}),\"primitive\");var $=f((s,t=false)=>({kind:\"pointer\",to:s,constTarget:t}),\"pointer\");var de={kind:\"struct\",tag:\"FILE\"};var me={kind:\"struct\",tag:\"va_list\"};var pe={kind:\"struct\",tag:\"_GUID\",members:[{name:\"Data1\",type:_(\"uint32\")},{name:\"Data2\",type:_(\"uint16\")},{name:\"Data3\",type:_(\"uint16\")},{name:\"Data4\",type:{kind:\"array\",of:_(\"uint8\"),length:8}}]};var _e=[\"HANDLE\",\"HWND\",\"HINSTANCE\",\"HMODULE\",\"HKEY\",\"HDC\",\"HGLOBAL\",\"HLOCAL\",\"HMENU\",\"HICON\",\"HBITMAP\",\"HBRUSH\",\"HFONT\",\"HPEN\",\"HRGN\",\"HCURSOR\",\"HMONITOR\",\"HRSRC\",\"HGDIOBJ\",\"HACCEL\",\"HDESK\",\"HWINSTA\",\"HHOOK\",\"HEVENT\",\"HKL\",\"HPALETTE\",\"HDROP\",\"HDEVINFO\",\"SC_HANDLE\",\"SERVICE_STATUS_HANDLE\",\"PSID\",\"HCRYPTPROV\"];function Mt(s){const t=s.os===\"win32\";const e=s.os===\"darwin\";const i=new Map;const n=f((r,o)=>{for(const a of typeof r===\"string\"?[r]:r)i.set(a,o)},\"set\");for(const r of[8,16,32,64]){n([`int${r}_t`,`int_least${r}_t`,`int_fast${r}_t`,`__int${r}_t`],_(`int${r}`));n([`uint${r}_t`,`uint_least${r}_t`,`uint_fast${r}_t`,`__uint${r}_t`,`u_int${r}_t`],_(`uint${r}`))}n([\"intmax_t\"],_(\"int64\"));n([\"uintmax_t\"],_(\"uint64\"));n([\"size_t\",\"rsize_t\"],_(\"size\"));n([\"ssize_t\"],_(\"ssize\"));n([\"intptr_t\"],_(\"intptr\"));n([\"uintptr_t\"],_(\"uintptr\"));n([\"ptrdiff_t\"],_(\"ptrdiff\"));n([\"wchar_t\"],_(\"wchar\"));n([\"char16_t\"],_(\"char16\"));n([\"char32_t\"],_(\"char32\"));n([\"bool\"],_(\"bool\"));n([\"time_t\"],_(t?\"int64\":\"long\"));n([\"suseconds_t\"],_(e?\"int32\":\"long\"));n([\"useconds_t\"],_(\"uint32\"));n([\"off_t\"],_(e?\"int64\":\"long\"));n([\"clock_t\"],_(e?\"ulong\":\"long\"));n([\"pid_t\"],_(\"int32\"));n([\"uid_t\",\"gid_t\",\"socklen_t\"],_(\"uint32\"));n([\"mode_t\"],_(e?\"uint16\":\"uint32\"));n([\"FILE\"],de);n([\"va_list\",\"__builtin_va_list\",\"__gnuc_va_list\",\"__darwin_va_list\",\"__va_list\"],me);n([\"u_char\"],_(\"uint8\"));n([\"u_short\"],_(\"uint16\"));n([\"u_int\"],_(\"uint32\"));n([\"u_long\"],_(\"ulong\"));n([\"u_quad_t\"],_(\"uint64\"));n([\"quad_t\"],_(\"int64\"));n([\"caddr_t\"],$(_(\"char\")));n([\"BYTE\",\"UCHAR\",\"BOOLEAN\",\"UINT8\"],_(\"uint8\"));n([\"CHAR\",\"CCHAR\"],_(\"char\"));n([\"INT8\"],_(\"int8\"));n([\"WCHAR\"],_(\"char16\"));n([\"SHORT\",\"INT16\"],_(\"int16\"));n([\"USHORT\",\"WORD\",\"ATOM\",\"LANGID\",\"UINT16\"],_(\"uint16\"));n([\"INT\",\"LONG\",\"BOOL\",\"HRESULT\",\"NTSTATUS\",\"LONG32\",\"INT32\",\"HFILE\"],_(\"int32\"));n([\"UINT\",\"ULONG\",\"DWORD\",\"DWORD32\",\"ULONG32\",\"UINT32\",\"COLORREF\",\"LCID\"],_(\"uint32\"));n([\"LONGLONG\",\"LONG64\",\"INT64\"],_(\"int64\"));n([\"ULONGLONG\",\"DWORDLONG\",\"QWORD\",\"DWORD64\",\"ULONG64\",\"UINT64\"],_(\"uint64\"));n([\"FLOAT\"],_(\"float\"));n([\"DOUBLE\"],_(\"double\"));n([\"INT_PTR\",\"LONG_PTR\",\"LPARAM\",\"LRESULT\",\"SSIZE_T\"],_(\"intptr\"));n([\"UINT_PTR\",\"ULONG_PTR\",\"DWORD_PTR\",\"WPARAM\",\"SIZE_T\"],_(\"uintptr\"));n(_e,$(_(\"void\")));n([\"PVOID\",\"LPVOID\"],$(_(\"void\")));n([\"LPCVOID\",\"PCVOID\"],$(_(\"void\"),true));n([\"LPSTR\",\"PSTR\",\"PCHAR\"],$(_(\"char\")));n([\"LPCSTR\",\"PCSTR\"],$(_(\"char\"),true));n([\"LPWSTR\",\"PWSTR\",\"PWCHAR\"],$(_(\"char16\")));n([\"LPCWSTR\",\"PCWSTR\"],$(_(\"char16\"),true));n([\"LPBYTE\",\"PBYTE\",\"PUCHAR\",\"PBOOLEAN\"],$(_(\"uint8\")));n([\"LPWORD\",\"PWORD\",\"PUSHORT\"],$(_(\"uint16\")));n([\"LPDWORD\",\"PDWORD\",\"PULONG\",\"PUINT\",\"LPUINT\"],$(_(\"uint32\")));n([\"LPLONG\",\"PLONG\",\"LPINT\",\"PINT\",\"LPBOOL\",\"PBOOL\"],$(_(\"int32\")));n([\"PULONGLONG\",\"PDWORD64\"],$(_(\"uint64\")));n([\"PLONGLONG\"],$(_(\"int64\")));n([\"PSIZE_T\",\"PULONG_PTR\",\"PDWORD_PTR\"],$(_(\"uintptr\")));n([\"PHANDLE\",\"LPHANDLE\"],$($(_(\"void\"))));n([\"GUID\",\"UUID\",\"CLSID\",\"IID\"],pe);return i}f(Mt,\"builtinTypes\");var Z=new Map([[\"__cdecl\",\"cdecl\"],[\"_cdecl\",\"cdecl\"],[\"__stdcall\",\"stdcall\"],[\"_stdcall\",\"stdcall\"],[\"__fastcall\",\"fastcall\"],[\"_fastcall\",\"fastcall\"],[\"__thiscall\",\"thiscall\"],[\"WINAPI\",\"stdcall\"],[\"APIENTRY\",\"stdcall\"],[\"CALLBACK\",\"stdcall\"],[\"PASCAL\",\"stdcall\"],[\"NTAPI\",\"stdcall\"],[\"STDMETHODCALLTYPE\",\"stdcall\"],[\"STDAPICALLTYPE\",\"stdcall\"],[\"WINAPIV\",\"cdecl\"],[\"CDECL\",\"cdecl\"]]);var q=new Set([\"volatile\",\"restrict\",\"__restrict\",\"__restrict__\",\"_Nullable\",\"_Nonnull\",\"_Null_unspecified\",\"__nullable\",\"__nonnull\",\"__null_unspecified\",\"_Nullable_result\",\"__unaligned\",\"__ptr32\",\"__ptr64\",\"__w64\",\"__sptr\",\"__uptr\",\"FAR\",\"NEAR\",\"far\",\"near\",\"OPTIONAL\",\"register\",\"auto\",\"__extension__\",\"_Noreturn\",\"noreturn\",\"__volatile__\",\"__kindof\",\"__cdecl__\",\"__weak\",\"__strong\",\"__autoreleasing\",\"__unsafe_unretained\"]);var W=new Set([\"const\",\"__const\",\"__const__\",\"CONST\"]);var Dt=new Set([\"inline\",\"__inline\",\"__inline__\",\"__forceinline\",\"_inline\",\"FORCEINLINE\"]);var ge=[\"_In_\",\"_Out\",\"_Inout\",\"_COM_Outptr\",\"_Ret_\",\"_Success_\",\"_Check_return_\",\"_Must_inspect_result_\",\"_Printf_format_string_\",\"_Scanf_format_string_\",\"_Field_\",\"_Frees_ptr_\",\"_Pre_\",\"_Post_\",\"_Deref_\",\"_Reserved_\",\"_Null_terminated_\",\"_NullNull_terminated_\",\"_Use_decl_annotations_\",\"_When_\",\"_At_\",\"_At_buffer_\",\"_Always_\",\"_Return_type_success_\",\"_Struct_size_bytes_\",\"_Readable_\",\"_Writable_\",\"__drv_\",\"_IRQL_\",\"_Kernel_\",\"_Function_class_\",\"_Callback_\",\"_Notnull_\",\"_Maybenull_\",\"_Const_\",\"_Translates_\",\"_Points_to_data_\",\"_Literal_\",\"_Notliteral_\",\"_Strict_type_match_\",\"_Analysis_\",\"_Acquires_\",\"_Releases_\",\"_Requires_\",\"_Guarded_by_\",\"_Interlocked_\",\"_Inexpressible_\",\"_Pre_satisfies_\",\"_Outref_\",\"_Ret_maybenull_\",\"_Result_\",\"_Group_\",\"_On_failure_\"];function Q(s){if(s===\"IN\")return\"in\";if(s===\"OUT\")return\"out\";if(!ge.some(t=>s.startsWith(t))&&![\"_In_\",\"_Out_\",\"_Inout_\"].includes(s))return void 0;if(s.startsWith(\"_Inout\"))return\"inout\";if(s.startsWith(\"_Out\")||s.startsWith(\"_COM_Outptr\"))return\"out\";if(s.startsWith(\"_In_\"))return\"in\";return null}f(Q,\"annotationDirection\");var be=[\"...\",\"<<=\",\">>=\",\"<<\",\">>\",\"<=\",\">=\",\"==\",\"!=\",\"&&\",\"||\",\"->\",\"++\",\"--\",\"##\",\"::\"];var we=\"{}()[];,*:=+-/%&|^~!?<>.#\";var ye={n:10,t:9,r:13,\"0\":0,a:7,b:8,f:12,v:11,\"\\\\\":92,\"'\":39,'\"':34,\"?\":63,e:27};var ve=[\"if\",\"ifdef\",\"ifndef\",\"elif\",\"elifdef\",\"elifndef\",\"else\",\"endif\"];function ke(s,t){let e=t;while(e<s.length&&s[e]!==\"\\n\"){const i=s[e];if(i===\"\\\\\"&&s[e+1]===\"\\n\"){e+=2}else if(i===\"\\\\\"&&s[e+1]===\"\\r\"&&s[e+2]===\"\\n\"){e+=3}else if(i===\"/\"&&s[e+1]===\"*\"){const n=s.indexOf(\"*/\",e+2);e=n<0?s.length:n+2}else if(i===\"/\"&&s[e+1]===\"/\"){while(e<s.length&&s[e]!==\"\\n\")e++}else if(i==='\"'||i===\"'\"){e++;while(e<s.length&&s[e]!==i&&s[e]!==\"\\n\")e+=s[e]===\"\\\\\"?2:1;if(s[e]===i)e++}else{e++}}return e}f(ke,\"directiveEnd\");function xe(s){let t=\"\";let e=0;while(e<s.length){const i=s[e];if(i===\"/\"&&s[e+1]===\"*\"){const n=s.indexOf(\"*/\",e+2);e=n<0?s.length:n+2;t+=\" \"}else if(i===\"/\"&&s[e+1]===\"/\"){while(e<s.length&&s[e]!==\"\\n\")e++}else if(i==='\"'||i===\"'\"){let n=e+1;while(n<s.length&&s[n]!==i&&s[n]!==\"\\n\")n+=s[n]===\"\\\\\"?2:1;t+=s.slice(e,Math.min(n+1,s.length));e=n+1}else if(i===\"\\\\\"&&(s[e+1]===\"\\n\"||s[e+1]===\"\\r\"&&s[e+2]===\"\\n\")){e+=s[e+1]===\"\\n\"?2:3;t+=\" \"}else{t+=i;e++}}return t.trim()}f(xe,\"withoutComments\");function Ne(s){const t=[0];for(let e=0;e<s.length;e++)if(s.charCodeAt(e)===10)t.push(e+1);return t}f(Ne,\"lineStartsOf\");function rt(s,t){let e=0;let i=s.length-1;while(e<i){const n=e+i+1>>1;if(s[n]<=t)e=n;else i=n-1}return{line:e+1,column:t-s[e]+1}}f(rt,\"positionOf\");function gt(s,t,e){return new w(`${s} (line ${t}, column ${e})`)}f(gt,\"declarationError\");function $e(s){const t=s.match(/^(?:line\\s+)?(\\d+)(?:\\s+\"((?:[^\"\\\\]|\\\\.)*)\")?/);if(!t)return void 0;return{line:Number(t[1]),...t[2]!==void 0?{file:t[2].replace(/\\\\(.)/g,\"$1\")}:{}}}f($e,\"lineMarker\");function H(s){const t=[];const e=s.length;let i=0;let n=true;const r=Ne(s);let o;let a=0;let c=false;const h=f((l,m)=>{const{line:u,column:d}=rt(r,m);throw gt(l,u,d)},\"fail\");const p=f((l,m,u,d={})=>{const{line:g,column:b}=rt(r,m);t.push({kind:l,text:d.text??s.slice(m,u),value:d.value,float:d.float,line:g,column:b,...c?{file:o,fileLine:g+a}:{},...d.source!==void 0?{source:d.source}:{}})},\"push\");while(i<e){const l=s[i];if(l===\"\\n\"){n=true;i++;continue}if(l===\" \"||l===\"\t\"||l===\"\\r\"||l===\"\\f\"||l===\"\\v\"){i++;continue}if(l===\"\\\\\"&&(s[i+1]===\"\\n\"||s[i+1]===\"\\r\"&&s[i+2]===\"\\n\")){i+=s[i+1]===\"\\n\"?2:3;continue}if(l===\"/\"&&s[i+1]===\"/\"){while(i<e&&s[i]!==\"\\n\")i++;continue}if(l===\"/\"&&s[i+1]===\"*\"){const u=s.indexOf(\"*/\",i+2);if(u<0)h(\"A comment is not closed\",i);i=u+2;continue}if(l===\"#\"&&n){const u=i;const d=ke(s,i);const g=xe(s.slice(u+1,d));const b=g.match(/^[A-Za-z_]\\w*/)?.[0]??\"\";if(ve.includes(b))h(`The directive #${b} is not supported: run the header through the preprocessor first (cc -E, cl /P)`,u);const N=b===\"\"||b===\"line\"?$e(g):void 0;if(N){c=true;if(N.file!==void 0)o=N.file;a=N.line-(rt(r,u).line+1)}const k=g.match(/^pragma\\s+pack\\s*\\(([^)]*)\\)/);if(k)p(\"pragma\",u,d,{text:`pack(${k[1].replace(/\\s+/g,\"\")})`});if(b===\"define\"||b===\"undef\")p(b,u,d,{text:g.slice(b.length).trim()});i=d;continue}n=false;if(/[A-Za-z_$]/.test(l)){let u=i+1;while(u<e&&/[\\w$]/.test(s[u]))u++;const d=s.slice(i,u);if((s[u]===\"'\"||s[u]==='\"')&&[\"L\",\"u\",\"U\",\"u8\"].includes(d)){i=u;continue}p(\"identifier\",i,u);i=u;continue}if(/[0-9]/.test(l)||l===\".\"&&/[0-9]/.test(s[i+1]??\"\")){let u=i;const d=/^0[xX]/.test(s.slice(i,i+2));if(d)u+=2;while(u<e&&(/[\\w.]/.test(s[u])||/[eEpP]/.test(s[u-1])&&/[+-]/.test(s[u])&&(!d||/[pP]/.test(s[u-1]))))u++;const g=s.slice(i,u);const b=g.replace(/(?:[uU](?:ll|LL|l|L)?|(?:ll|LL|l|L)[uU]?|[uU]?[iI](?:8|16|32|64))$/,\"\");const N=d?/[.pP]/.test(b):/[.eE]/.test(b)||/[fF]$/.test(g);if(N){p(\"number\",i,u,{float:true})}else{let k;try{if(/^0[bB]/.test(b))k=BigInt(b);else if(/^0[0-7]+$/.test(b))k=BigInt(`0o${b.slice(1)}`);else k=BigInt(b)}catch{return h(`The number ${g} is not valid`,i)}p(\"number\",i,u,{value:k})}i=u;continue}if(l===\"'\"||l==='\"'){let u=i+1;const d=[];while(u<e&&s[u]!==l){if(s[u]===\"\\n\")h(l==='\"'?\"A string is not closed\":\"A character is not closed\",i);if(s[u]===\"\\\\\"){const g=s[u+1];if(g===\"x\"){const b=s.slice(u+2).match(/^[0-9a-fA-F]+/)?.[0]??\"\";d.push(parseInt(b||\"0\",16));u+=2+b.length}else if(/[0-7]/.test(g)){const b=s.slice(u+1).match(/^[0-7]{1,3}/)[0];d.push(parseInt(b,8));u+=1+b.length}else{d.push(ye[g]??g.charCodeAt(0));u+=2}}else{d.push(s.codePointAt(u));u+=s.codePointAt(u)>65535?2:1}}if(u>=e)h(l==='\"'?\"A string is not closed\":\"A character is not closed\",i);if(l===\"'\")p(\"char\",i,u+1,{value:BigInt(d[0]??0)});else p(\"string\",i,u+1,{text:String.fromCodePoint(...d),source:s.slice(i,u+1)});i=u+1;continue}const m=be.find(u=>s.startsWith(u,i));if(m){p(\"punctuator\",i,i+m.length);i+=m.length;continue}if(we.includes(l)){p(\"punctuator\",i,i+1);i++;continue}h(`Unexpected character ${JSON.stringify(l)}`,i)}const{line:y,column:v}=rt(r,e);t.push({kind:\"end\",text:\"\",line:y,column:v});return t}f(H,\"tokenize\");var bt=new Set([\"void\",\"char\",\"short\",\"int\",\"long\",\"float\",\"double\",\"signed\",\"unsigned\",\"__signed\",\"__signed__\",\"_Bool\",\"__int8\",\"__int16\",\"__int32\",\"__int64\",\"__int128\",\"_Complex\",\"__complex__\",\"_Float16\",\"__fp16\",\"_Float128\",\"__float128\"]);var R=new Set([\"__attribute__\",\"__attribute\",\"__declspec\"]);var Ct=new Set([\"__asm__\",\"__asm\",\"asm\"]);var Te=new Set([\"_Alignas\",\"alignas\",\"__alignas\"]);var K=class{static{f(this,\"DeclarationParser\")}#r;#n;#c;#a=[];#o=0;#u;#f=[];#b;#w;#p;constructor(t,e=Tt()){this.#r=t;this.#n=e;this.#c=Mt(t)}get declarations(){return this.#n}parse(t){this.#a=this.#h(H(t));this.#o=0;while(!this.#y(\"end\"))this.#g();this.#O();return this.#n}readSource(t){const e=[];const i=new Map;this.#p=i;try{this.#a=this.#h(H(t))}finally{this.#p=void 0}this.#o=0;this.#b=e;try{while(!this.#y(\"end\"))this.#T()}finally{this.#b=void 0}this.#O();return{declarations:this.#n,items:e,macros:i}}prototype(t){this.#a=this.#h(H(t));this.#o=0;const e=this.#e();const i=this.#x(false);const n=this.#N(false);const{type:r}=n.build(i.base,i.constant);if(r.kind!==\"function\"||!n.name)throw this.#i(\"A function prototype is expected\",e);this.#m();const o=this.#M()??n.asm;if(this.#s(\";\"))this.#t();if(!this.#y(\"end\"))throw this.#i(`Unexpected ${this.#$(this.#e())} after the prototype`,this.#e());return{name:n.name,symbol:this.#L(n.name,o),type:this.#z(r,n.convention??i.convention)}}typeName(t){this.#a=this.#h(H(t));this.#o=0;const e=this.#x(false);const i=this.#N(true);if(i.name)throw this.#i(`A type is expected, not a declaration of ${i.name}`,i.token);if(!this.#y(\"end\"))throw this.#i(`Unexpected ${this.#$(this.#e())} after the type`,this.#e());return i.build(e.base,e.constant).type}constant(t){this.#a=this.#h(H(t));this.#o=0;const e=this.#D();if(!this.#y(\"end\"))throw this.#i(`Unexpected ${this.#$(this.#e())} after the expression`,this.#e());return e.value}#h(t){const e=this.#n.macros;const i=[];const n=f((r,o,a)=>{const c=r.kind===\"identifier\"&&!o.has(r.text)?e.get(r.text):void 0;if(c===null&&!this.#p)throw this.#i(`The macro ${r.text} takes arguments: the macros with arguments are not supported (write what it expands to)`,a);if(!c)return void i.push(r);const h=new Set(o).add(r.text);for(const p of c)n({...p,line:a.line,column:a.column,...a.file!==void 0?{file:a.file,fileLine:a.fileLine}:{}},h,a)},\"expandInto\");for(const r of t){if(r.kind===\"define\"){this.#v(r);continue}if(r.kind===\"undef\"){e.delete(r.text.match(/^[A-Za-z_]\\w*/)?.[0]??\"\");continue}n(r,new Set,r)}return this.#l(i)}#l(t){if(!t.some(i=>i.kind===\"identifier\"&&i.text===\"__pragma\"))return t;const e=[];for(let i=0;i<t.length;i++){const n=t[i];if(n.kind!==\"identifier\"||n.text!==\"__pragma\"||t[i+1]?.text!==\"(\"){e.push(n);continue}let r=0;let o=i+1;for(;o<t.length-1;o++){if(t[o].kind!==\"punctuator\")continue;if(t[o].text===\"(\")r++;else if(t[o].text===\")\"&&--r===0)break}const a=t.slice(i+2,o);if(a[0]?.text===\"pack\"&&a[1]?.text===\"(\")e.push({...n,kind:\"pragma\",text:`pack(${a.slice(2,-1).map(c=>c.text).join(\"\")})`});i=o}return e}#v(t){const e=t.text.match(/^[A-Za-z_]\\w*/)?.[0];if(!e)throw this.#i(\"#define without a name\",t);const i=t.text.slice(e.length);this.#p?.set(e,{...t.file!==void 0?{file:t.file}:{},...t.fileLine!==void 0?{line:t.fileLine}:{}});if(i.startsWith(\"(\")){this.#n.macros.set(e,null);return}const n=[];let r;try{r=H(i)}catch(o){if(!this.#p)throw o;this.#n.macros.set(e,null);return}for(const o of r){if(o.kind===\"end\")break;if(o.kind===\"pragma\"||o.kind===\"define\"||o.kind===\"undef\")continue;n.push({kind:o.kind,text:o.text,...o.value!==void 0?{value:o.value}:{},...o.float?{float:true}:{}})}this.#n.macros.set(e,n)}#O(){const t=this.#a;const e=this.#o;for(const[i,n]of this.#n.macros){if(!n?.length||this.#n.constants.has(i))continue;try{const r={kind:\"end\",text:\"\",line:0,column:0};this.#a=[...this.#h([{kind:\"identifier\",text:i,line:0,column:0}]),r];this.#o=0;const o=this.#k();if(this.#y(\"end\"))this.#n.constants.set(i,o.value)}catch{}}this.#a=t;this.#o=e}#T(){const t=this.#o;const e=this.#e();if(e.kind===\"identifier\"&&e.text===\"extern\"&&this.#e(1).kind===\"string\"&&this.#s(\"{\",2)){this.#o+=3;while(!this.#s(\"}\")&&!this.#y(\"end\"))this.#T();if(this.#s(\"}\"))this.#t();return}const i=this.#u;const n=[];this.#w=n;let r;try{this.#g()}catch(o){r=(o instanceof Error?o.message:String(o)).replace(/ \\(line \\d+, column \\d+\\)$/,\"\");this.#o=this.#A(t)}finally{this.#w=void 0}this.#b.push({...e.file!==void 0?{file:e.file}:{},...e.fileLine!==void 0?{line:e.fileLine}:{},tokens:this.#a.slice(t,this.#o),names:n,...i!==void 0?{pack:i}:{},...r!==void 0?{error:r}:{}})}#A(t){const e=this.#a;let i=0;let n=false;for(let r=t;r<e.length-1;r++){const o=e[r];if(o.kind===\"pragma\"&&i===0&&r>t)return r;if(o.kind!==\"punctuator\")continue;if(o.text===\"{\"&&i===0)n=r>t&&e[r-1].text===\")\";if(o.text===\"(\"||o.text===\"[\"||o.text===\"{\"){i++}else if(o.text===\")\"||o.text===\"]\"||o.text===\"}\"){i=Math.max(0,i-1);if(o.text===\"}\"&&i===0&&n)return e[r+1]?.text===\";\"?r+2:r+1}else if(o.text===\";\"&&i===0){return r+1}}return e.length-1}#_(t){this.#w?.push(t)}#g(){const t=this.#e();if(t.kind===\"pragma\"){this.#t();return this.#U(t)}if(this.#s(\";\")){this.#t();return}if(t.kind===\"identifier\"&&t.text===\"extern\"&&this.#e(1).kind===\"string\"){this.#t();this.#t();if(this.#s(\"{\")){this.#t();while(!this.#s(\"}\")){if(this.#y(\"end\"))throw this.#i('extern \"C\" { is not closed',t);this.#g()}this.#t()}return}if(t.kind===\"identifier\"&&[\"_Static_assert\",\"static_assert\"].includes(t.text)){this.#t();this.#E();this.#d(\";\");return}const e=this.#x(true);if(this.#s(\";\")){this.#t();return}for(;;){const i=this.#N(false);this.#m();const n=this.#M()??i.asm;this.#m();if(this.#s(\"{\")){this.#E();return}let r;if(this.#s(\"=\")){this.#t();r=this.#K()}this.#P(e,i,n,r);if(this.#s(\",\")){this.#t();continue}this.#d(\";\");return}}#P(t,e,i,n){if(!e.name)throw this.#i(\"A name is expected in the declaration\",this.#e());const r=e.name;const{type:o,constant:a}=e.build(t.base,t.constant);if(t.storage===\"typedef\"){const c=this.#n.types.get(r);if(c&&c!==o&&x(c)!==x(o))throw this.#i(`The type ${r} is declared again as another type (${x(c)}, then ${x(o)})`,e.token);this.#n.types.set(r,o);this.#_(`type ${r}`);return}if(o.kind===\"function\"){if(t.storage===\"static\")return;this.#n.functions.set(r,{name:r,symbol:this.#L(r,i),type:this.#z(o,e.convention??t.convention)});this.#_(`function ${r}`);return}if(t.storage===\"static\"){if(n&&a){this.#n.constants.set(r,n.value);this.#_(`constant ${r}`)}return}if(t.threadLocal)throw this.#i(`The variable ${r} is thread-local: it cannot be reached through the library`,e.token);this.#n.variables.set(r,{name:r,symbol:this.#L(r,i),type:o,constant:a});this.#_(`variable ${r}`)}#L(t,e){if(e===void 0)return t;return this.#r.underscoredSymbols&&e.startsWith(\"_\")?e.slice(1):e}#z(t,e){if(t.variadic)return t;return e&&e!==t.convention?{...t,convention:e}:t}#U(t){const e=t.text.slice(5,-1).split(\",\").filter(Boolean);const i=f(n=>{if(n===void 0)return void 0;const r=Number(n);if(![1,2,4,8,16].includes(r))throw this.#i(`#pragma pack(${n}) is not a packing of 1, 2, 4, 8 or 16`,t);return r>=16?void 0:r},\"value\");if(!e.length){this.#u=void 0}else if(e[0]===\"push\"){this.#f.push(this.#u);const n=e.slice(1).find(r=>/^\\d+$/.test(r));if(n!==void 0)this.#u=i(n)}else if(e[0]===\"pop\"){this.#u=this.#f.length?this.#f.pop():void 0}else{this.#u=i(e[0])}}#x(t){const e=this.#e();const i=[];let n;const r={base:mt,inline:false,constant:false,threadLocal:false};for(;;){const o=this.#e();if(o.kind!==\"identifier\"){if(this.#s(\"[\")&&this.#s(\"[\",1)){this.#E();continue}break}const a=o.text;if(W.has(a)){this.#t();r.constant=true;continue}if(q.has(a)){this.#t();continue}if(Dt.has(a)){this.#t();r.inline=true;continue}if(Z.has(a)){this.#t();r.convention=Z.get(a);continue}if(R.has(a)){const c=this.#m()?.align;if(c)r.align=c;continue}if(Te.has(a)){r.align=this.#q();continue}if(a===\"typedef\"||a===\"extern\"||a===\"static\"){if(!t)throw this.#i(`${a} is not allowed here`,o);this.#t();r.storage=a;continue}if(a===\"_Thread_local\"||a===\"__thread\"||a===\"thread_local\"){this.#t();r.threadLocal=true;continue}if(a===\"struct\"||a===\"union\"){if(n||i.length)throw this.#i(`${a} after another type`,o);n=this.#G(a);continue}if(a===\"enum\"){if(n||i.length)throw this.#i(`enum after another type`,o);n=this.#V();continue}if(bt.has(a)){if(n)throw this.#i(`${a} after another type`,o);this.#t();i.push(a);continue}if(!this.#I(a)){const c=Q(a);if(c!==void 0){this.#t();if(this.#s(\"(\"))this.#E();if(c)r.direction=c;continue}}if(!n&&!i.length){const c=this.#I(a);if(c){this.#t();n=c;continue}throw this.#B(o)}break}if(i.length)n=this.#F(i,e);if(!n)throw this.#e().kind===\"identifier\"?this.#B(this.#e()):this.#i(`A type is expected, not ${this.#$(this.#e())}`,this.#e());r.base=n;return r}#B(t){const e=/^[A-Z][A-Z0-9_]*$/.test(t.text)?\": a macro? give its #define with the declarations\":\"\";return this.#i(`Unknown type ${t.text}${e}`,t)}#F(t,e){const i=f(h=>t.filter(p=>p===h).length,\"count\");const n=i(\"unsigned\")>0;const r=i(\"signed\")+i(\"__signed\")+i(\"__signed__\")>0;const o=i(\"long\");const a=f(h=>({kind:\"primitive\",name:h}),\"name\");const c=f(h=>t.includes(h),\"has\");if([\"__int128\",\"_Complex\",\"__complex__\",\"_Float16\",\"__fp16\",\"_Float128\",\"__float128\"].some(c))throw this.#i(`The type ${t.join(\" \")} is not supported`,e);if(c(\"void\"))return mt;if(c(\"_Bool\"))return a(\"bool\");if(c(\"float\"))return a(\"float\");if(c(\"double\")){if(o)throw this.#i(\"long double is not supported\",e);return a(\"double\")}if(c(\"char\"))return a(n?\"uchar\":r?\"schar\":\"char\");if(c(\"__int8\"))return a(n?\"uint8\":\"int8\");if(c(\"__int16\"))return a(n?\"uint16\":\"int16\");if(c(\"__int32\"))return a(n?\"uint32\":\"int32\");if(c(\"__int64\"))return a(n?\"uint64\":\"int64\");if(c(\"short\"))return a(n?\"ushort\":\"short\");if(o>=2)return a(n?\"ulonglong\":\"longlong\");if(o===1)return a(n?\"ulong\":\"long\");return a(n?\"uint\":\"int\")}#I(t){return this.#n.types.get(t)??this.#c.get(t)}#G(t){const e=this.#t();let i=this.#m();let n;let r;if(this.#e().kind===\"identifier\"&&!R.has(this.#e().text)){r=this.#t();n=r.text;i=this.#m()??i}const o=`${t} ${n}`;let a;if(n){const l=this.#n.tags.get(o);if(l&&l.kind!==t)throw this.#i(`${n} is a ${l.kind}, not a ${t}`,r);a=l}if(!this.#s(\"{\")){if(!n)throw this.#i(`A ${t} without a name must be defined`,e);if(!a){a={kind:t,tag:n};this.#n.tags.set(o,a)}return a}if(a?.members)throw this.#i(`The ${t} ${n} is defined again`,r);if(!a){a={kind:t,tag:n};if(n)this.#n.tags.set(o,a)}this.#t();const c=[];let h=0;while(!this.#s(\"}\")){if(this.#y(\"end\"))throw this.#i(`The ${t} ${n??\"\"} is not closed`.replace(\" \",\" \"),e);if(this.#e().kind===\"pragma\"){this.#U(this.#t());continue}if(this.#s(\";\")){this.#t();continue}const l=this.#x(false);if(this.#s(\";\")){this.#t();if(l.base.kind===\"struct\"||l.base.kind===\"union\")c.push({name:`_${h++}`,type:l.base,align:l.align});continue}for(;;){let m;if(!this.#s(\":\"))m=this.#N(false);let u;if(this.#s(\":\")){this.#t();const N=this.#k();u=Number(N.value);if(u<0)throw this.#i(\"A bitfield has a negative width\",this.#e())}const d=this.#m();if(!m?.name&&u===void 0)throw this.#i(\"A member name is expected\",this.#e());const g=m?m.build(l.base,l.constant).type:l.base;if(g.kind===\"function\")throw this.#i(`The member ${m?.name} is a function: a pointer to a function is expected`,m?.token??this.#e());if(g.kind===\"struct\"||g.kind===\"union\"){if(!g.members)throw this.#i(`The member ${m?.name} has the type ${x(g)}, declared but not defined`,m?.token??this.#e())}const b=d?.align??m?.align??l.align;c.push({name:m?.name??\"\",type:g,...u!==void 0?{bits:u}:{},...b?{align:b}:{}});if(this.#s(\",\")){this.#t();continue}this.#d(\";\");break}}this.#t();const p=this.#m();const y=!!(i?.packed||p?.packed);a.members=c;if(n)this.#_(o);if(y)a.pack=1;else if(this.#u!==void 0)a.pack=this.#u;const v=p?.align??i?.align;if(v)a.align=v;return a}#V(){const t=this.#t();this.#m();let e;if(this.#e().kind===\"identifier\"&&!R.has(this.#e().text))e=this.#t().text;this.#m();let i;if(this.#s(\":\")){this.#t();const c=this.#x(false).base;if(c.kind!==\"primitive\"||c.name===\"void\"||c.name===\"float\"||c.name===\"double\")throw this.#i(\"The type of an enumeration must be an integer type\",t);i=c.name}const n=`enum ${e}`;if(!this.#s(\"{\")){const c=e?this.#n.tags.get(n):void 0;if(c?.kind===\"enum\")return c;if(!e)throw this.#i(\"An enumeration without a name must be defined\",t);const h={kind:\"enum\",tag:e,base:i??\"int\",values:new Map};this.#n.tags.set(n,h);return h}this.#t();const r=new Map;let o=0n;while(!this.#s(\"}\")){const c=this.#t();if(c.kind!==\"identifier\")throw this.#i(`An enumerator name is expected, not ${this.#$(c)}`,c);this.#m();if(this.#s(\"=\")){this.#t();o=this.#k().value}r.set(c.text,o);this.#n.constants.set(c.text,o);this.#_(`constant ${c.text}`);o++;if(this.#s(\",\"))this.#t();else if(!this.#s(\"}\"))throw this.#i(`, or } is expected, not ${this.#$(this.#e())}`,this.#e())}this.#t();this.#m();const a={kind:\"enum\",tag:e,base:i??this.#j([...r.values()]),values:r};if(e){this.#n.tags.set(n,a);this.#_(n)}return a}#j(t){const e=t.some(n=>n<0n);const i=f((n,r)=>t.every(o=>o>=n&&o<=r),\"fits\");if(i(-(2n**31n),2n**31n-1n))return\"int\";if(!e&&i(0n,2n**32n-1n))return\"uint\";if(this.#r.bitfields===\"msvc\")return\"int\";if(i(-(2n**63n),2n**63n-1n))return\"longlong\";return\"ulonglong\"}#N(t){const e=[];let i;let n;let r;for(;;){const l=this.#e();if(this.#s(\"*\")){this.#t();e.push(false);continue}if(l.kind!==\"identifier\")break;if(W.has(l.text)&&e.length){this.#t();e[e.length-1]=true;continue}if(W.has(l.text)||q.has(l.text)){this.#t();continue}if(Z.has(l.text)){this.#t();i=Z.get(l.text);continue}if(R.has(l.text)){r=this.#m()?.align??r;continue}const m=this.#I(l.text)?void 0:Q(l.text);if(m!==void 0){this.#t();if(this.#s(\"(\"))this.#E();if(m)n=m;continue}break}let o;let a;let c;if(this.#e().kind===\"identifier\"){a=this.#t();o=a.text}else if(this.#s(\"(\")&&this.#Y()){this.#t();c=this.#N(t);this.#d(\")\")}const h=[];for(;;){if(this.#s(\"[\")){this.#t();if(this.#s(\"]\")){this.#t();h.push({array:0});continue}while(this.#e().kind===\"identifier\"&&(this.#e().text===\"static\"||W.has(this.#e().text)||q.has(this.#e().text)))this.#t();const l=this.#k();this.#d(\"]\");if(l.value<0n)throw this.#i(\"An array has a negative length\",this.#e());h.push({array:Number(l.value)});continue}if(this.#s(\"(\")){h.push({function:this.#Z()});continue}break}while(this.#e().kind===\"identifier\"&&(R.has(this.#e().text)||q.has(this.#e().text))){if(R.has(this.#e().text))r=this.#m()?.align??r;else this.#t()}let p;if(this.#e().kind===\"identifier\"&&Ct.has(this.#e().text))p=this.#M();const y=c?.convention??i;const v={name:o??c?.name,token:a??c?.token,convention:c?i:void 0,asm:p??c?.asm,direction:n??c?.direction,align:r??c?.align,build:f((l,m)=>{let u=l;let d=m;for(const g of e){u={kind:\"pointer\",to:u,constTarget:d};d=g}for(let g=h.length-1;g>=0;g--){const b=h[g];if(\"array\"in b){if(u.kind===\"function\")throw this.#i(\"An array of functions is not allowed\",a??this.#e());u={kind:\"array\",of:u,length:b.array}}else{u={kind:\"function\",result:u,parameters:b.function.parameters,variadic:b.function.variadic,convention:(c&&!b.function.variadic?y:void 0)??\"cdecl\"}}}if(c)return c.build(u,d&&!h.length);return{type:u,constant:d}},\"build\")};if(!c&&i)v.convention=i;return v}#Y(){const t=this.#e(1);if(t.kind===\"punctuator\")return t.text===\"*\"||t.text===\"(\"||t.text===\"[\";if(t.kind!==\"identifier\")return false;if(Z.has(t.text)||R.has(t.text))return true;if(bt.has(t.text)||W.has(t.text)||[\"struct\",\"union\",\"enum\"].includes(t.text)||this.#I(t.text))return false;return Q(t.text)===void 0}#Z(){const t=this.#t();const e=[];let i=false;if(this.#s(\")\")){this.#t();return{parameters:e,variadic:false}}if(this.#e().text===\"void\"&&this.#s(\")\",1)){this.#t();this.#t();return{parameters:e,variadic:false}}if(this.#e().kind===\"identifier\"&&!this.#R(this.#e())&&(this.#s(\",\",1)||this.#s(\")\",1))){throw this.#i(`Unknown type ${this.#e().text} (a parameter needs a type: the declarations of the old style, names without types, are not supported)`,this.#e())}for(;;){if(this.#s(\"...\")){this.#t();i=true;this.#d(\")\");break}const n=this.#x(false);const r=this.#N(true);let{type:o}=r.build(n.base,n.constant);if(o.kind===\"array\")o={kind:\"pointer\",to:o.of,constTarget:n.constant};if(o.kind===\"function\")o={kind:\"pointer\",to:o,constTarget:false};if(o.kind===\"primitive\"&&o.name===\"void\")throw this.#i(\"A parameter cannot be void\",t);e.push({...r.name?{name:r.name}:{},type:o,direction:r.direction??n.direction??\"in\"});if(this.#s(\",\")){this.#t();continue}this.#d(\")\");break}return{parameters:e,variadic:i}}#R(t){if(t.kind!==\"identifier\")return false;const e=t.text;return bt.has(e)||W.has(e)||q.has(e)||[\"struct\",\"union\",\"enum\"].includes(e)||!!this.#I(e)||Q(e)!==void 0}#m(){let t;while(this.#e().kind===\"identifier\"&&R.has(this.#e().text)){const e=this.#t();if(!this.#s(\"(\"))throw this.#i(`( is expected after ${e.text}`,e);const i=this.#o;this.#E();const n=this.#a.slice(i+1,this.#o-1);t??={packed:false};for(let r=0;r<n.length;r++){const o=n[r].text.replace(/^__(.*)__$/,\"$1\");if(o===\"packed\")t.packed=true;if(o===\"aligned\"||o===\"align\"){if(n[r+1]?.text!==\"(\"){t.align=16}else{const a=n[r+2]?.value;if(a===void 0)throw this.#i(`The alignment of ${o} must be a number`,n[r]);t.align=Number(a)}}}if(t.align!==void 0&&![1,2,4,8].includes(t.align))throw this.#i(`An alignment of ${t.align} bytes is not supported (1, 2, 4 or 8)`,e)}return t}#q(){const t=this.#t();this.#d(\"(\");let e;if(this.#R(this.#e())){const i=this.#x(false);e=z(this.#N(true).build(i.base,i.constant).type,this.#r)}else{e=Number(this.#k().value)}this.#d(\")\");if(![1,2,4,8].includes(e))throw this.#i(`An alignment of ${e} bytes is not supported (1, 2, 4 or 8)`,t);return e}#M(){if(this.#e().kind!==\"identifier\"||!Ct.has(this.#e().text))return void 0;this.#t();this.#d(\"(\");let t=\"\";while(this.#e().kind===\"string\")t+=this.#t().text;this.#d(\")\");return t}#K(){if(this.#s(\"{\")){this.#E();return void 0}const t=this.#o;try{return this.#k()}catch{this.#o=t;let e=0;while(!this.#y(\"end\")){const i=this.#e();if(i.kind===\"punctuator\"&&\"([{\".includes(i.text))e++;if(i.kind===\"punctuator\"&&\")]}\".includes(i.text))e--;if(e<=0&&i.kind===\"punctuator\"&&(i.text===\",\"||i.text===\";\"))break;this.#t()}return void 0}}#D(){let t=this.#k();while(this.#s(\",\")){this.#t();t=this.#k()}return t}#k(){const t=this.#W(0);if(!this.#s(\"?\"))return t;this.#t();const e=this.#D();this.#d(\":\");const i=this.#k();return t.value!==0n?e:i}#W(t){let e=this.#C();for(;;){const i=this.#e();const n=i.kind===\"punctuator\"?Ee[i.text]:void 0;if(n===void 0||n<t)return e;this.#t();const r=this.#W(n+1);e=this.#J(i,e,r)}}#J(t,e,i){const n=Math.max(e.bits,i.bits);const r=e.unsigned&&e.bits>=i.bits||i.unsigned&&i.bits>=e.bits;const o=e.value;const a=i.value;const c=f((p,y={unsigned:r,bits:n})=>this.#S(p,y.unsigned,y.bits),\"result\");const h=f(p=>({value:p?1n:0n,unsigned:false,bits:32}),\"truth\");switch(t.text){case\"*\":return c(o*a);case\"/\":case\"%\":if(a===0n)throw this.#i(\"Division by zero in a constant expression\",t);return c(t.text===\"/\"?o/a:o%a);case\"+\":return c(o+a);case\"-\":return c(o-a);case\"<<\":return c(o<<a,e);case\">>\":return c(o>>a,e);case\"<\":return h(o<a);case\">\":return h(o>a);case\"<=\":return h(o<=a);case\">=\":return h(o>=a);case\"==\":return h(o===a);case\"!=\":return h(o!==a);case\"&\":return c(o&a);case\"^\":return c(o^a);case\"|\":return c(o|a);case\"&&\":return h(o!==0n&&a!==0n);case\"||\":return h(o!==0n||a!==0n)}throw this.#i(`The operator ${t.text} is not allowed in a constant expression`,t)}#S(t,e,i){if(e)return{value:BigInt.asUintN(i,t),unsigned:true,bits:i};return{value:t,unsigned:false,bits:i}}#C(){const t=this.#e();if(t.kind===\"punctuator\"){if([\"+\",\"-\",\"~\",\"!\"].includes(t.text)){this.#t();const e=this.#C();switch(t.text){case\"+\":return e;case\"-\":return this.#S(-e.value,e.unsigned,e.bits);case\"~\":return this.#S(~e.value,e.unsigned,e.bits);default:return{value:e.value===0n?1n:0n,unsigned:false,bits:32}}}if(t.text===\"(\"&&this.#R(this.#e(1))){this.#t();const e=this.#H();this.#d(\")\");return this.#X(this.#C(),e,t)}}if(t.kind===\"identifier\"&&(t.text===\"sizeof\"||[\"_Alignof\",\"alignof\",\"__alignof__\",\"__alignof\"].includes(t.text))){this.#t();this.#d(\"(\");if(!this.#R(this.#e()))throw this.#i(`${t.text} of an expression is not supported: give a type`,t);const e=this.#H();this.#d(\")\");const i=t.text===\"sizeof\"?j(e,this.#r):z(e,this.#r);return{value:BigInt(i),unsigned:true,bits:this.#r.pointerSize*8}}return this.#Q()}#H(){const t=this.#x(false);return this.#N(true).build(t.base,t.constant).type}#X(t,e,i){const n=e.kind===\"primitive\"?e.name:e.kind===\"enum\"?e.base:void 0;if(e.kind===\"pointer\")return this.#S(t.value,true,this.#r.pointerSize*8);if(!n||n===\"void\"||n===\"float\"||n===\"double\")throw this.#i(`A cast to ${x(e)} is not allowed in a constant expression`,i);const r=V(n,this.#r)*8;if(n===\"bool\")return{value:t.value===0n?0n:1n,unsigned:true,bits:8};const o=!nt(n,this.#r);return o?this.#S(t.value,true,r):{value:BigInt.asIntN(r,t.value),unsigned:false,bits:r}}#Q(){const t=this.#t();if(t.kind===\"number\"){if(t.float)throw this.#i(`The floating-point number ${t.text} is not allowed in an integer constant expression`,t);return this.#tt(t)}if(t.kind===\"char\")return{value:t.value,unsigned:false,bits:32};if(t.kind===\"identifier\"){const e=this.#n.constants.get(t.text);if(e===void 0)throw this.#i(`Unknown constant ${t.text}: a macro is not known (write its value)`,t);return{value:e,unsigned:e>2n**31n-1n,bits:e>2n**32n-1n||e<-(2n**31n)?64:32}}if(t.kind===\"punctuator\"&&t.text===\"(\"){const e=this.#D();this.#d(\")\");return e}throw this.#i(`A constant expression is expected, not ${this.#$(t)}`,t)}#tt(t){const e=t.value;const i=t.text.match(/[uUlL]+$/)?.[0].toLowerCase()??\"\";const n=i.includes(\"u\");const r=(i.match(/l/g)??[]).length;const o=!/^0/.test(t.text)||t.text===\"0\";const a=this.#r.longSize*8;const c=[];if(r===0)c.push({bits:32,unsigned:false},{bits:32,unsigned:true});if(r<=1)c.push({bits:a,unsigned:false},{bits:a,unsigned:true});c.push({bits:64,unsigned:false},{bits:64,unsigned:true});for(const h of c){if(n&&!h.unsigned)continue;if(o&&!n&&h.unsigned)continue;const p=h.unsigned?2n**BigInt(h.bits)-1n:2n**BigInt(h.bits-1)-1n;if(e<=p)return{value:e,unsigned:h.unsigned,bits:h.bits}}throw this.#i(`The number ${t.text} is too large`,t)}#E(){const t=this.#t();const e={\"(\":\")\",\"[\":\"]\",\"{\":\"}\"};const i=[e[t.text]];while(i.length){const n=this.#t();if(n.kind===\"end\")throw this.#i(`${t.text} is not closed`,t);if(n.kind!==\"punctuator\")continue;if(e[n.text])i.push(e[n.text]);else if(n.text===i[i.length-1])i.pop()}}#e(t=0){return this.#a[Math.min(this.#o+t,this.#a.length-1)]}#t(){const t=this.#e();if(this.#o<this.#a.length-1)this.#o++;return t}#y(t){return this.#e().kind===t}#s(t,e=0){const i=this.#e(e);return i.kind===\"punctuator\"&&i.text===t}#d(t){if(!this.#s(t))throw this.#i(`${t} is expected, not ${this.#$(this.#e())}`,this.#e());return this.#t()}#$(t){return t.kind===\"end\"?\"the end\":JSON.stringify(t.text)}#i(t,e){return gt(t,e.line,e.column)}};var Ee={\"||\":1,\"&&\":2,\"|\":3,\"^\":4,\"&\":5,\"==\":6,\"!=\":6,\"<\":7,\">\":7,\"<=\":7,\">=\":7,\"<<\":8,\">>\":8,\"+\":9,\"-\":9,\"*\":10,\"/\":10,\"%\":10};function Oe(s,t){const e=![\"ia32\",\"arm\",\"x32\"].includes(t);const i=s===\"win32\";return{os:s,arch:t,pointerSize:e?8:4,longSize:e&&!i?8:4,wcharSize:i?2:4,int64Alignment:!e&&!i&&t===\"ia32\"?4:8,bitfields:i?\"msvc\":(t===\"arm\"||t===\"arm64\")&&s!==\"darwin\"?\"aapcs\":\"sysv\",underscoredSymbols:s===\"darwin\"}}f(Oe,\"platformOf\");var J=Oe(process.platform,process.arch);var O=S(P(),1);var I=class extends A{static{f(this,\"NativeMemoryException\")}constructor(){super(...arguments);this.errno=\"E_NATIVE_MEMORY\"}};var at=require(\"node:fs\");var ot=S(require(\"node:path\"),1);var Bt=S(P(),1);var M=class extends A{static{f(this,\"NativeLibraryNotFoundException\")}constructor(){super(...arguments);this.errno=\"E_NATIVE_LIBRARY_NOT_FOUND\"}};function Ae(){try{const s=(0,at.readdirSync)(\"/lib\").find(t=>/^ld-musl-.*\\.so\\.1$/.test(t));return s?`/lib/${s}`:void 0}catch{return void 0}}f(Ae,\"muslLoader\");function Le(s,t){switch(t){case\"darwin\":return\"/usr/lib/libSystem.B.dylib\";case\"win32\":return\"ucrtbase.dll\";case\"linux\":return Ae()??(s===\"c\"?\"libc.so.6\":\"libm.so.6\");case\"freebsd\":return s===\"c\"?\"libc.so.7\":\"libm.so.5\";default:return s===\"c\"?\"libc.so\":\"libm.so\"}}f(Le,\"systemLibrary\");function Ut(s,t=process.platform){if(s===\"c\"||s===\"m\")return[Le(s,t)];if(/[/\\\\]/.test(s)||/\\.(dylib|dll|so)(\\.\\d+)*$/i.test(s))return[s];switch(t){case\"darwin\":return[`lib${s}.dylib`,`${s}.dylib`];case\"win32\":return[`${s}.dll`,`lib${s}.dll`];default:return[`lib${s}.so`,`${s}.so`]}}f(Ut,\"fileNames\");function Ie(s,t=process.platform){if(typeof s===\"string\")return Ut(s,t);const e=s[t]??s.default;if(e===void 0)throw new M(`The library has no name for ${t} (give one for ${t}, or a default)`);return(typeof e===\"string\"?[e]:e).flatMap(i=>Ut(i,t))}f(Ie,\"libraryNames\");function wt(s,t={}){const e=[];const i=f(a=>{try{return Bt.default.load(a)}catch(c){e.push(`${a}: ${c.message}`);return void 0}},\"attempt\");if(t.path){const a=ot.default.resolve(t.path);const c=i(a);if(c)return{library:c,file:a};throw new M(`The library cannot be loaded:\n${e.join(\"\\n\")}`)}const n=[...t.search??[],...t.packaged?[t.packaged]:[]].map(a=>ot.default.resolve(a));const r=Ie(s);for(const a of r){if(/[/\\\\]/.test(a)){const c=i(a);if(c)return{library:c,file:a};continue}for(const c of n){const h=ot.default.join(c,a);if(!(0,at.existsSync)(h)){e.push(`${h}: not found`);continue}const p=i(h);if(p)return{library:p,file:h}}}for(const a of r.filter(c=>!/[/\\\\]/.test(c))){const c=i(a);if(c)return{library:c,file:a}}const o=process.platform===\"linux\"&&r.some(a=>a.endsWith(\".so\"))?\"\\nOn Linux, the libraries of the system are often installed with their version only (libsqlite3.so.0): give that name.\":\"\";throw new M(`The library ${r.join(\" or \")} cannot be loaded:\n${e.join(\"\\n\")}${o}`)}f(wt,\"loadLibrary\");var T=new st(J);var tt=class{static{f(this,\"TypeNames\")}#r;#n=new Map;constructor(t){this.#r=t}type(t){let e=this.#n.get(t);if(!e){e=this.#r.typeName(t);this.#n.set(t,e)}return e}koffi(t){return T.type(this.type(t))}};var Ui=new tt(new K(J));function et(s,t){if(s===null||s===void 0||s===0n||s===0)throw new I(`${t}: the pointer is null`);if(typeof s===\"number\")return BigInt(s);if(typeof s!==\"bigint\")throw new I(`${t}: a pointer (a bigint) is expected, not ${typeof s}`);return s}f(et,\"checkedPointer\");function Se(s,t){if(!Number.isInteger(s)||s<0)throw new I(`${t}: the length must be a non-negative integer (${s})`);return s}f(Se,\"checkedLength\");function Ft(s,t,e,i){const n=s.type(e);const r=et(t,\"read\");const o=T.converter(n);if(i===void 0){const c=O.default.decode(r,T.type(n));return o?.fromNative?o.fromNative(c):c}const a=Array.from(O.default.decode(r,T.type(n),Se(i,\"readArray\")));return o?.fromNative?a.map(o.fromNative):a}f(Ft,\"readNative\");function Gt(s,t,e,i){const n=s.type(e);const r=T.converter(n);O.default.encode(et(t,\"write\"),T.type(n),r?.toNative?r.toNative(i):i)}f(Gt,\"writeNative\");function Vt(s,t,e=1){if(!Number.isInteger(e)||e<1)throw new I(`allocMemory: the count must be a positive integer (${e})`);const i=O.default.alloc(s.koffi(t),e);return typeof i===\"bigint\"?i:O.default.address(i)}f(Vt,\"allocateNative\");function yt(s){O.default.free(et(s,\"freeMemory\"))}f(yt,\"freeNative\");var Wt=new Map;var Ht;function jt(s=\"ucrtbase.dll\"){if(process.platform!==\"win32\")return O.default.errno();const t=s.toLowerCase();let e=Wt.get(t);if(!e){e=O.default.load(s).func(\"_errno\",\"int *\",[]);Wt.set(t,e)}return O.default.decode(e(),\"int\")}f(jt,\"nativeErrno\");function ct(){if(process.platform!==\"win32\")return void 0;Ht??=O.default.load(\"kernel32.dll\").func(\"__stdcall\",\"GetLastError\",\"uint32_t\",[]);return Ht()}f(ct,\"nativeWindowsError\");function Yt(s,t){const e=s.type(t);if(e.kind===\"function\")throw new w(`A function has no size: ${t}`);return O.default.sizeof(T.type(e))}f(Yt,\"typeSize\");var qt=require(\"node:worker_threads\");var it=S(P(),1);var vt=\"\";var kt=void 0;var Pe=5e3;function Zt(s){const t={NativeDeclarationException:w,NativeLibraryClosedException:L,NativeLibraryNotFoundException:M,NativeMemoryException:I,TypeError,RangeError};const e=new(t[s.name]??Error)(s.message);if(!t[s.name])e.name=s.name;return e}f(Zt,\"errorOf\");function ze(s){if(s.kind===\"primitive\")return s.name===\"void\"?void 0:s.name===\"bool\"?false:0;if(s.kind===\"enum\")return 0;if(s.kind===\"pointer\")return null;return{}}f(ze,\"zeroOf\");function Re(s){if(!s)return true;if(s.direction!==\"in\")return true;return s.type.kind===\"pointer\"&&!s.type.constTarget}f(Re,\"written\");var ut=class{static{f(this,\"NativeThreads\")}#r;#n;#c;#a=[];#o=1;#u=1;#f=false;constructor(t,e,i,n){if(!vt||!kt)throw new Error(\"The code of the worker threads of the libraries is not built (scripts/worker.mjs)\");this.#r={koffi:kt.resolve(\"koffi\"),file:t,declarations:e,...n?{crt:n}:{}};this.#n=i;this.#c=Array.from({length:i},()=>this.#v())}get ready(){return Promise.all(this.#c.map(t=>t?.ready??Promise.resolve())).then(()=>void 0)}get count(){return this.#n}declare(t){this.#a.push(t);for(const e of this.#c)if(e&&!e.exited)this.#l(e,{type:\"declare\",declarations:t})}bind(t){if(this.#f)return;for(const e of this.#c)if(e&&!e.exited)this.#p(e,t)}call(t,e){if(this.#f)return Promise.reject(new L(`The library ${this.#r.file} is closed`));const i=this.#h();const n=this.#p(i,t);const r=t.type.type;const o=e.slice();const a=[];const c=new Map;const h=[];const p=[];let y;for(let l=0;l<o.length;l++){const m=o[l];const u=r.parameters[l];if(typeof m===\"function\"){const d=u?.type.kind===\"pointer\"?u.type.to:void 0;if(!d||d.kind!==\"function\")return this.#w(p,new TypeError(`The argument ${l} of ${t.type.name} is a function, but its parameter is not a pointer to a function`));const g=d;const b=ze(g.result);const N=f((...G)=>{try{return m(...G)}catch(Qt){y??={error:Qt};return b}},\"safe\");const k=it.default.register(Y(g,T,N),it.default.pointer(T.prototype(g)));p.push(k);o[l]=k;continue}if(ArrayBuffer.isView(m)){const d=new Uint8Array(m.byteLength);d.set(new Uint8Array(m.buffer,m.byteOffset,m.byteLength));const g=Buffer.isBuffer(m)||m instanceof Uint8Array?d:m instanceof DataView?new DataView(d.buffer):new m.constructor(d.buffer);o[l]=g;h.push(d.buffer);if(Re(u)){a.push(l);c.set(l,m)}continue}if(u&&u.direction!==\"in\"&&m&&typeof m===\"object\")a.push(l)}const v=this.#u++;return new Promise((l,m)=>{const u=f(()=>{for(const d of p)it.default.unregister(d)},\"release\");i.calls.set(v,{resolve:l,reject:f(d=>{u();m(d)},\"reject\"),abandon:m,settle:f(d=>{u();a.forEach((g,b)=>{const N=e[g];const k=d.back[b];const G=c.get(g);if(G&&ArrayBuffer.isView(k))new Uint8Array(G.buffer,G.byteOffset,G.byteLength).set(new Uint8Array(k.buffer,k.byteOffset,k.byteLength));else if(Array.isArray(N)&&Array.isArray(k))N.splice(0,N.length,...k);else if(N&&typeof N===\"object\"&&k&&typeof k===\"object\")Object.assign(N,k)});if(y)return m(y.error);if(d.error)return m(Zt(d.error));l({value:d.value,errno:d.errno,...d.windowsError!==void 0?{windowsError:d.windowsError}:{}})},\"settle\")});try{this.#l(i,{type:\"call\",id:v,function:n,args:o,back:a},h)}catch(d){i.calls.delete(v);u();m(d)}})}async stopThread(t){await this.#c[t]?.worker.terminate()}async close(){if(this.#f)return;this.#f=true;await Promise.all(this.#c.map(async t=>{if(!t||t.exited)return;if(t.calls.size){const o=new L(`The library ${this.#r.file} is closed while a call runs in its thread`);for(const a of t.calls.values())a.abandon(o);t.calls.clear();this.#b(t);return}const e=new Promise(o=>{t.worker.once(\"exit\",()=>o())});this.#l(t,{type:\"close\"});let i;const n=new Promise(o=>{i=setTimeout(()=>o(true),Pe)});const r=await Promise.race([e,n]);clearTimeout(i);if(r)this.#b(t)}))}#b(t){t.worker.terminate().catch(()=>void 0)}#w(t,e){for(const i of t)it.default.unregister(i);return Promise.reject(e)}#p(t,e){let i=t.bound.get(e);if(i===void 0){i=this.#o++;t.bound.set(e,i);this.#l(t,{type:\"bind\",id:i,declaration:e.declaration,named:e.named,options:e.options})}return i}#h(){for(let e=0;e<this.#c.length;e++){if(!this.#c[e]||this.#c[e].exited)this.#c[e]=this.#v()}let t=this.#c[0];for(const e of this.#c)if(e.calls.size<t.calls.size)t=e;return t}#l(t,e,i=[]){t.worker.postMessage(e,i);if(e.type===\"call\")t.worker.ref()}#v(){const t=new qt.Worker(vt,{eval:true,workerData:{...this.#r,declarations:[this.#r.declarations,...this.#a].join(\"\\n\")}});let e=f(()=>void 0,\"readied\");let i=f(()=>void 0,\"failed\");const n=new Promise((c,h)=>{e=c;i=h});n.catch(()=>void 0);const r={worker:t,ready:n,calls:new Map,bound:new Map,exited:false};const o=f(()=>{if(!r.calls.size)t.unref()},\"refresh\");t.on(\"message\",c=>{switch(c.type){case\"ready\":e();o();return;case\"failed\":r.error=Zt(c.error);i(r.error);o();return;case\"result\":{const h=r.calls.get(c.id);r.calls.delete(c.id);o();h?.settle(c);return}case\"closed\":return}});const a=f(c=>{if(r.exited)return;r.exited=true;i(c);for(const h of r.calls.values())h.reject(r.error??c);r.calls.clear()},\"end\");t.on(\"error\",c=>a(c));t.on(\"exit\",c=>a(new Error(`The worker thread of the library ${this.#r.file} ended (exit code ${c})`)));return r}};var Kt=S(P(),1);var ft=class{static{f(this,\"NativeCallback\")}#r;#n;#c=false;constructor(t,e){this.#r=t;this.#n=e}get pointer(){return this.#r}get released(){return this.#c}release(){if(this.#c)return;this.#c=true;Kt.default.unregister(this.#r);this.#n(this)}};var xt=class{static{f(this,\"LibraryModel\")}#r;constructor(t){this.parser=new K(J);this.names=new tt(this.parser);this.#r=new Map;if(t)this.parser.parse(t)}prototype(t){let e=this.#r.get(t);if(!e){e=this.parser.prototype(t);this.#r.set(t,e)}return e}};var Jt=new Map;function Me(s){let t=Jt.get(s);if(!t){t=new xt(s);Jt.set(s,t)}return t}f(Me,\"modelOf\");function De(s){switch(s.kind){case\"primitive\":if(s.name===\"void\")return void 0;if(s.name===\"bool\")return false;return 0;case\"enum\":return 0;case\"pointer\":return null;default:return{}}}f(De,\"zeroOf\");var ht=class{static{f(this,\"NativeLibraryHandle\")}#r;#n;#c;#a;#o;#u;#f=new Map;#b=new Set;#w=new Set;#p=new Map;#h;#l;#v;#O;#T=0;#A=[];#_=false;constructor(t,e=\"\",i={}){this.#u=i;this.#c=Me(e);this.#a=this.#c.parser;this.#o=this.#c.names;const{library:n,file:r}=wt(t,i);this.#r=n;this.#n=r;if(i.threads!==void 0&&(!Number.isInteger(i.threads)||i.threads<0))throw new w(`The threads of a library are a number of threads (${i.threads})`);if(i.threads)this.#l=new ut(r,e,i.threads,i.crt);this.#h={start:f(()=>{if(this.#_)throw new L(`The library ${this.#n} is closed`);this.#T++},\"start\"),end:f(()=>{if(--this.#T>0)return;const o=this.#A;this.#A=[];for(const a of o)a()},\"end\")}}get file(){return this.#n}get closed(){return this.#_}get pending(){return this.#T}get threads(){return this.#l?.count??0}get ready(){return this.#l?.ready??Promise.resolve()}get constants(){return Object.fromEntries(this.#a.declarations.constants)}declare(t){this.#a.parse(t);this.#l?.declare(t)}func(t,e={}){this.#g();const i=`${t}\\0${JSON.stringify(e)}`;let n=this.#f.get(i);if(!n){n=this.#P(this.#c.prototype(t),e,t,false);this.#f.set(i,n)}return n}function(t,e={}){this.#g();const i=this.#a.declarations.functions.get(t);if(!i)throw new w(`The function ${t} is not declared`);const n=`\u0001${t}\\0${JSON.stringify(e)}`;let r=this.#f.get(n);if(!r){r=this.#P(i,e,t,true);this.#f.set(n,r)}return r}variable(t){this.#g();let e=t.trim();if(!/^[A-Za-z_]\\w*$/.test(e)){const c=new Set(this.#a.declarations.variables.keys());this.#a.parse(/;\\s*$/.test(e)?e:`${e};`);e=[...this.#a.declarations.variables.keys()].find(h=>!c.has(h))??e.match(/(\\w+)\\s*(\\[[^\\]]*\\])?\\s*;?\\s*$/)?.[1]??e}const i=this.#a.declarations.variables.get(e);if(!i)throw new w(`The variable ${e} is not declared`);let n;try{n=this.#r.symbol(i.symbol)}catch(c){throw new w(`The variable ${e} is not in the library (${i.symbol}): ${c.message}`)}const r=i.type;const o=r.kind===\"array\"&&r.length===0;const a=T.converter(r);return{address:n,get:f(()=>{this.#g();if(o){const h=r.of;return h.kind===\"primitive\"&&h.name===\"char\"?F.default.decode.string(n):n}const c=F.default.decode(n,T.type(r));return a?.fromNative?a.fromNative(c):c},\"get\"),set:f(c=>{this.#g();if(i.constant)throw new w(`The variable ${e} is const`);if(o)throw new w(`The array ${e} has no length: write its elements through its address`);F.default.encode(n,T.type(r),a?.toNative?a.toNative(c):c)},\"set\")}}callback(t,e){this.#g();let i;if(t.includes(\"(\")){i=this.#a.prototype(t).type}else{const h=this.#o.type(t);const p=h.kind===\"pointer\"?h.to:h;if(p.kind!==\"function\")throw new w(`The type ${t} is not a function type (${x(h)})`);i=p}const n=this.#u.onCallbackError??(h=>console.error(\"A callback of a native library threw:\",h));const r=De(i.result);const o=f((...h)=>{try{return e(...h)}catch(p){n(p);return r}},\"safe\");const a=F.default.register(Y(i,T,o),F.default.pointer(T.prototype(i)));const c=new ft(a,h=>this.#b.delete(h));this.#b.add(c);return c}read(t,e,i){this.#g();return Ft(this.#o,t,e,i)}write(t,e,i){this.#g();Gt(this.#o,t,e,i)}sizeOf(t){return Yt(this.#o,t)}offsetOf(t,e){const i=this.#o.type(t);if(i.kind!==\"struct\"&&i.kind!==\"union\")throw new w(`${t} is not a struct or a union`);const n=U(i,J).members.find(r=>r.member.name===e);if(!n)throw new w(`${t} has no member ${e}`);return n.offset}alloc(t,e=1){this.#g();const i=Vt(this.#o,t,e);this.#w.add(i);return i}free(t){const e=et(t,\"free\");this.#w.delete(e);yt(e)}errno(){return this.#v??jt(this.#u.crt)}windowsError(){return this.#O??ct()}drain(t=Infinity){if(!this.#T)return Promise.resolve(true);return new Promise(e=>{const i=Number.isFinite(t)?setTimeout(()=>e(false),t):void 0;this.#A.push(()=>{if(i)clearTimeout(i);e(true)})})}async close(t=1e4){if(this.#_)return;this.#_=true;const e=await this.drain(t);await this.#l?.close();this.#f.clear();if(!e)return;for(const i of[...this.#b])i.release();for(const i of this.#w)yt(i);this.#w.clear();if(!this.#u.keepLoaded)this.#r.unload()}#g(){if(this.#_)throw new L(`The library ${this.#n} is closed`)}#P(t,e,i,n){if(e.async&&this.#l)return this.#L(this.#l,{declaration:i,named:n,options:{free:e.free,int64:e.int64},type:t});return Rt(this.#r,t,T,r=>this.#o.koffi(r),{async:e.async,int64:e.int64,calls:this.#h,...e.free?{free:this.#z(e.free)}:{}})}#L(t,e){t.bind(e);return(...i)=>{try{this.#h.start()}catch(n){return Promise.reject(n)}return t.call(e,i).then(n=>{this.#v=n.errno;this.#O=n.windowsError;this.#h.end();return n.value},n=>{this.#h.end();throw n})}}#z(t){if(t===true)return i=>F.default.free(i);let e=this.#p.get(t);if(!e){try{e=this.#r.func(t,\"void\",[\"void *\"])}catch(i){throw new w(`The function ${t} freeing the strings is not in the library: ${i.message}`)}this.#p.set(t,e)}return e}};function Nt(s){return s instanceof Error?{name:s.name,message:s.message}:{name:\"Error\",message:String(s)}}f(Nt,\"threadError\");function Ce(s){const t=new Set;for(const e of s)if(ArrayBuffer.isView(e)&&e.buffer instanceof ArrayBuffer)t.add(e.buffer);return[...t]}f(Ce,\"transferables\");function Xt(){const s=lt.parentPort;const t=lt.workerData;const e=f((r,o=[])=>s.postMessage(r,o),\"post\");let i;try{i=new ht(t.file,t.declarations,{crt:t.crt})}catch(r){e({type:\"failed\",error:Nt(r)});s.close();return}const n=new Map;s.on(\"message\",r=>{switch(r.type){case\"bind\":try{n.set(r.id,r.named?i.function(r.declaration,r.options):i.func(r.declaration,r.options))}catch(o){n.set(r.id,Nt(o))}return;case\"declare\":try{i.declare(r.declarations)}catch{}return;case\"call\":{const o=n.get(r.function);let a;let c;if(typeof o!==\"function\"){c=o??{name:\"Error\",message:\"The function is not bound in the thread\"}}else{try{a=o(...r.args)}catch(v){c=Nt(v)}}const h=ct();const p=i.errno();const y=r.back.map(v=>r.args[v]);e({type:\"result\",id:r.id,...c?{error:c}:{value:a},back:y,errno:p,...h!==void 0?{windowsError:h}:{}},Ce(y));return}case\"close\":{const o=f(()=>{e({type:\"closed\"});s.close()},\"end\");i.close().then(o,o);return}}});e({type:\"ready\"})}f(Xt,\"runNativeWorker\");Xt();\n";
@@ -0,0 +1,6 @@
1
+ /**
2
+ * The require of the package (createRequire(import.meta.url) in the ES modules, require in CommonJS), written by the
3
+ * build (scripts/worker.mjs) in place of this one: it resolves koffi for the worker threads, from the package (in a
4
+ * packed application, from its native tree)
5
+ */
6
+ export declare const packageRequire: NodeJS.Require | undefined;
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.packageRequire = void 0;
4
+ exports.packageRequire = require;
@@ -0,0 +1,77 @@
1
+ /**
2
+ * What a worker thread of a library is started with
3
+ */
4
+ export interface NativeWorkerData {
5
+ /**
6
+ * The path of koffi, resolved by the thread of the library
7
+ */
8
+ readonly koffi: string;
9
+ /**
10
+ * The file of the library, as the thread of the library loaded it
11
+ */
12
+ readonly file: string;
13
+ /**
14
+ * The C declarations of the library
15
+ */
16
+ readonly declarations: string;
17
+ /**
18
+ * On Windows, the C library whose errno is read after each call
19
+ */
20
+ readonly crt?: string;
21
+ }
22
+ /**
23
+ * The options a function is bound with in a thread (the synchronous ones: the thread calls it synchronously)
24
+ */
25
+ export interface ThreadFunctionOptions {
26
+ readonly free?: string | boolean;
27
+ readonly int64?: 'bigint' | 'number';
28
+ }
29
+ /**
30
+ * An error sent from a thread: made again by its name
31
+ */
32
+ export interface ThreadError {
33
+ readonly name: string;
34
+ readonly message: string;
35
+ }
36
+ /**
37
+ * The messages to a thread: a function bound (a prototype, or the name of a declared function), declarations added, a
38
+ * call (the arguments to send back after it), the end
39
+ */
40
+ export type ToNativeWorker = {
41
+ readonly type: 'bind';
42
+ readonly id: number;
43
+ readonly declaration: string;
44
+ readonly named: boolean;
45
+ readonly options: ThreadFunctionOptions;
46
+ } | {
47
+ readonly type: 'declare';
48
+ readonly declarations: string;
49
+ } | {
50
+ readonly type: 'call';
51
+ readonly id: number;
52
+ readonly function: number;
53
+ readonly args: unknown[];
54
+ readonly back: readonly number[];
55
+ } | {
56
+ readonly type: 'close';
57
+ };
58
+ /**
59
+ * The messages from a thread: the library loaded (or not), the result of a call (its value or its error, the arguments
60
+ * asked back, the errno of the thread after it), the end
61
+ */
62
+ export type FromNativeWorker = {
63
+ readonly type: 'ready';
64
+ } | {
65
+ readonly type: 'failed';
66
+ readonly error: ThreadError;
67
+ } | {
68
+ readonly type: 'result';
69
+ readonly id: number;
70
+ readonly value?: unknown;
71
+ readonly error?: ThreadError;
72
+ readonly back: readonly unknown[];
73
+ readonly errno: number;
74
+ readonly windowsError?: number;
75
+ } | {
76
+ readonly type: 'closed';
77
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}