@yodaos-pkg/ink 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/env.js ADDED
@@ -0,0 +1,336 @@
1
+ let wasmExports = null;
2
+ const allocationSizes = new Map();
3
+ const textDecoder = new TextDecoder('utf-8', { fatal: false });
4
+ const textEncoder = new TextEncoder();
5
+
6
+ export function __setWasmInstance(exports) {
7
+ wasmExports = exports;
8
+ }
9
+
10
+ function requireWasm() {
11
+ if (!wasmExports) {
12
+ throw new Error('Wasm exports are not initialized');
13
+ }
14
+ return wasmExports;
15
+ }
16
+
17
+ function memoryBytes() {
18
+ return new Uint8Array(requireWasm().memory.buffer);
19
+ }
20
+
21
+ function memoryView() {
22
+ return new DataView(requireWasm().memory.buffer);
23
+ }
24
+
25
+ function readCString(ptr) {
26
+ ptr >>>= 0;
27
+ const bytes = memoryBytes();
28
+ let end = ptr;
29
+ while (end < bytes.length && bytes[end] !== 0) {
30
+ end += 1;
31
+ }
32
+ return textDecoder.decode(bytes.subarray(ptr, end));
33
+ }
34
+
35
+ function writeCString(ptr, maxBytes, value) {
36
+ ptr >>>= 0;
37
+ maxBytes >>>= 0;
38
+ if (maxBytes === 0) {
39
+ return textEncoder.encode(value).length;
40
+ }
41
+ const encoded = textEncoder.encode(value);
42
+ const bytes = memoryBytes();
43
+ const writeLen = Math.min(encoded.length, maxBytes - 1);
44
+ bytes.set(encoded.subarray(0, writeLen), ptr);
45
+ bytes[ptr + writeLen] = 0;
46
+ return encoded.length;
47
+ }
48
+
49
+ function trackedMalloc(size, align = 1) {
50
+ const wasm = requireWasm();
51
+ const normalizedSize = Math.max(1, size >>> 0);
52
+ const ptr = wasm.__wbindgen_export(normalizedSize, align >>> 0) >>> 0;
53
+ allocationSizes.set(ptr, normalizedSize);
54
+ return ptr;
55
+ }
56
+
57
+ function trackedFree(ptr) {
58
+ ptr >>>= 0;
59
+ if (ptr === 0) {
60
+ return;
61
+ }
62
+ const wasm = requireWasm();
63
+ const size = allocationSizes.get(ptr);
64
+ if (size === undefined) {
65
+ return;
66
+ }
67
+ allocationSizes.delete(ptr);
68
+ wasm.__wbindgen_export4(ptr, size, 1);
69
+ }
70
+
71
+ function consoleWrite(prefix, text) {
72
+ const value = prefix ? `${prefix}${text}` : text;
73
+ console.log(value);
74
+ }
75
+
76
+ function formatPrintf(fmtPtr) {
77
+ return readCString(fmtPtr);
78
+ }
79
+
80
+ function parseNumberPrefix(text) {
81
+ const trimmed = text.match(/^\s*/)?.[0] ?? '';
82
+ const rest = text.slice(trimmed.length);
83
+ const match = rest.match(
84
+ /^[+-]?(?:inf(?:inity)?|nan(?:\([^)]+\))?|(?:(?:\d+\.?\d*)|(?:\.\d+))(?:[eE][+-]?\d+)?)/i,
85
+ );
86
+ if (!match) {
87
+ return { value: 0, consumed: 0 };
88
+ }
89
+ const literal = match[0];
90
+ return {
91
+ value: Number.parseFloat(literal),
92
+ consumed: trimmed.length + literal.length,
93
+ };
94
+ }
95
+
96
+ function dayOfYear(date) {
97
+ const start = Date.UTC(date.getUTCFullYear(), 0, 1);
98
+ const now = Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate());
99
+ return Math.floor((now - start) / 86400000);
100
+ }
101
+
102
+ export function __assert_fail(assertionPtr, filePtr, line, functionPtr) {
103
+ const assertion = readCString(assertionPtr);
104
+ const file = readCString(filePtr);
105
+ const func = readCString(functionPtr);
106
+ throw new Error(`assertion failed: ${assertion} at ${file}:${line} in ${func}`);
107
+ }
108
+
109
+ export function printf(fmtPtr, _argsPtr) {
110
+ const message = formatPrintf(fmtPtr);
111
+ consoleWrite('', message);
112
+ return message.length | 0;
113
+ }
114
+
115
+ export function abort() {
116
+ throw new Error('abort() called from wasm');
117
+ }
118
+
119
+ export function puts(ptr) {
120
+ const message = readCString(ptr);
121
+ consoleWrite('', message);
122
+ return message.length | 0;
123
+ }
124
+
125
+ export function putchar(ch) {
126
+ const value = String.fromCharCode(ch & 0xff);
127
+ consoleWrite('', value);
128
+ return ch | 0;
129
+ }
130
+
131
+ export function fprintf(_stream, fmtPtr, _argsPtr) {
132
+ const message = formatPrintf(fmtPtr);
133
+ consoleWrite('', message);
134
+ return message.length | 0;
135
+ }
136
+
137
+ export function fwrite(ptr, size, nmemb, _stream) {
138
+ const total = (size >>> 0) * (nmemb >>> 0);
139
+ const bytes = memoryBytes().subarray(ptr >>> 0, (ptr >>> 0) + total);
140
+ consoleWrite('', textDecoder.decode(bytes));
141
+ return nmemb | 0;
142
+ }
143
+
144
+ export function fputc(ch, _stream) {
145
+ return putchar(ch);
146
+ }
147
+
148
+ export function vsnprintf(dst, size, fmtPtr, _argsPtr) {
149
+ return writeCString(dst, size, formatPrintf(fmtPtr)) | 0;
150
+ }
151
+
152
+ export function snprintf(dst, size, fmtPtr, _arg3) {
153
+ return writeCString(dst, size, formatPrintf(fmtPtr)) | 0;
154
+ }
155
+
156
+ export function scalbn(x, n) {
157
+ return x * Math.pow(2, n | 0);
158
+ }
159
+
160
+ export function lrint(x) {
161
+ if (!Number.isFinite(x)) {
162
+ return 0;
163
+ }
164
+ return Math.round(x) | 0;
165
+ }
166
+
167
+ export function realloc(ptr, size) {
168
+ ptr >>>= 0;
169
+ size >>>= 0;
170
+ if (ptr === 0) {
171
+ return trackedMalloc(size);
172
+ }
173
+ const oldSize = allocationSizes.get(ptr) ?? 0;
174
+ const nextPtr = trackedMalloc(size);
175
+ if (oldSize > 0) {
176
+ const bytes = memoryBytes();
177
+ bytes.copyWithin(nextPtr, ptr, ptr + Math.min(oldSize, size));
178
+ trackedFree(ptr);
179
+ }
180
+ return nextPtr;
181
+ }
182
+
183
+ export function strchr(ptr, ch) {
184
+ ptr >>>= 0;
185
+ const target = ch & 0xff;
186
+ const bytes = memoryBytes();
187
+ for (let i = ptr; i < bytes.length; i += 1) {
188
+ const value = bytes[i];
189
+ if (value === target) {
190
+ return i >>> 0;
191
+ }
192
+ if (value === 0) {
193
+ return target === 0 ? i >>> 0 : 0;
194
+ }
195
+ }
196
+ return 0;
197
+ }
198
+
199
+ export function free(ptr) {
200
+ trackedFree(ptr);
201
+ }
202
+
203
+ export function strncmp(aPtr, bPtr, count) {
204
+ const bytes = memoryBytes();
205
+ let a = aPtr >>> 0;
206
+ let b = bPtr >>> 0;
207
+ const limit = count >>> 0;
208
+ for (let i = 0; i < limit; i += 1) {
209
+ const av = bytes[a + i] ?? 0;
210
+ const bv = bytes[b + i] ?? 0;
211
+ if (av !== bv) {
212
+ return av < bv ? -1 : 1;
213
+ }
214
+ if (av === 0) {
215
+ return 0;
216
+ }
217
+ }
218
+ return 0;
219
+ }
220
+
221
+ export function strcmp(aPtr, bPtr) {
222
+ const bytes = memoryBytes();
223
+ let a = aPtr >>> 0;
224
+ let b = bPtr >>> 0;
225
+ while (true) {
226
+ const av = bytes[a] ?? 0;
227
+ const bv = bytes[b] ?? 0;
228
+ if (av !== bv) {
229
+ return av < bv ? -1 : 1;
230
+ }
231
+ if (av === 0) {
232
+ return 0;
233
+ }
234
+ a += 1;
235
+ b += 1;
236
+ }
237
+ }
238
+
239
+ export function memchr(ptr, ch, count) {
240
+ ptr >>>= 0;
241
+ const target = ch & 0xff;
242
+ const limit = count >>> 0;
243
+ const bytes = memoryBytes();
244
+ for (let i = 0; i < limit; i += 1) {
245
+ if (bytes[ptr + i] === target) {
246
+ return (ptr + i) >>> 0;
247
+ }
248
+ }
249
+ return 0;
250
+ }
251
+
252
+ export function calloc(nmemb, size) {
253
+ const total = (nmemb >>> 0) * (size >>> 0);
254
+ const ptr = trackedMalloc(total);
255
+ memoryBytes().fill(0, ptr, ptr + total);
256
+ return ptr;
257
+ }
258
+
259
+ export function malloc(size) {
260
+ return trackedMalloc(size);
261
+ }
262
+
263
+ export function frexp(value, expPtr) {
264
+ if (value === 0 || !Number.isFinite(value)) {
265
+ memoryView().setInt32(expPtr >>> 0, 0, true);
266
+ return value;
267
+ }
268
+ const exponent = Math.floor(Math.log2(Math.abs(value))) + 1;
269
+ memoryView().setInt32(expPtr >>> 0, exponent, true);
270
+ return value / Math.pow(2, exponent);
271
+ }
272
+
273
+ export function strrchr(ptr, ch) {
274
+ ptr >>>= 0;
275
+ const target = ch & 0xff;
276
+ const bytes = memoryBytes();
277
+ let found = 0;
278
+ for (let i = ptr; i < bytes.length; i += 1) {
279
+ const value = bytes[i];
280
+ if (value === target) {
281
+ found = i >>> 0;
282
+ }
283
+ if (value === 0) {
284
+ return target === 0 ? i >>> 0 : found;
285
+ }
286
+ }
287
+ return found;
288
+ }
289
+
290
+ export function vfprintf(stream, fmtPtr, argsPtr) {
291
+ return fprintf(stream, fmtPtr, argsPtr);
292
+ }
293
+
294
+ export function strtod(ptr, endPtrPtr) {
295
+ const text = readCString(ptr);
296
+ const { value, consumed } = parseNumberPrefix(text);
297
+ if (endPtrPtr >>> 0 !== 0) {
298
+ memoryView().setUint32(endPtrPtr >>> 0, ((ptr >>> 0) + consumed) >>> 0, true);
299
+ }
300
+ return value;
301
+ }
302
+
303
+ export function localtime_r(timePtr, resultPtr) {
304
+ const view = memoryView();
305
+ const ptr = timePtr >>> 0;
306
+ let seconds = 0;
307
+ try {
308
+ seconds = Number(view.getBigInt64(ptr, true));
309
+ } catch {
310
+ seconds = view.getInt32(ptr, true);
311
+ }
312
+ const date = new Date(seconds * 1000);
313
+ const out = resultPtr >>> 0;
314
+ view.setInt32(out + 0, date.getSeconds(), true);
315
+ view.setInt32(out + 4, date.getMinutes(), true);
316
+ view.setInt32(out + 8, date.getHours(), true);
317
+ view.setInt32(out + 12, date.getDate(), true);
318
+ view.setInt32(out + 16, date.getMonth(), true);
319
+ view.setInt32(out + 20, date.getFullYear() - 1900, true);
320
+ view.setInt32(out + 24, date.getDay(), true);
321
+ view.setInt32(out + 28, dayOfYear(date), true);
322
+ view.setInt32(out + 32, 0, true);
323
+ return out;
324
+ }
325
+
326
+ export function acosh(x) {
327
+ return Math.acosh(x);
328
+ }
329
+
330
+ export function asinh(x) {
331
+ return Math.asinh(x);
332
+ }
333
+
334
+ export function atanh(x) {
335
+ return Math.atanh(x);
336
+ }
@@ -0,0 +1,115 @@
1
+ export type BundleFileInput =
2
+ | string
3
+ | Uint8Array
4
+ | ArrayBuffer
5
+ | ArrayBufferView;
6
+
7
+ export type BundleFiles =
8
+ | Map<string, BundleFileInput>
9
+ | Array<[string, BundleFileInput]>
10
+ | Record<string, BundleFileInput>;
11
+
12
+ export interface InitInkOptions {
13
+ wasmUrl?: string | URL | Request;
14
+ moduleOrPath?: RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
15
+ }
16
+
17
+ export interface CreateInkViewOptions {
18
+ width: number;
19
+ height: number;
20
+ scaleFactor?: number;
21
+ canvas?: HTMLCanvasElement;
22
+ wasm?: InitInkOptions;
23
+ }
24
+
25
+ export interface OpenBundleOptions {
26
+ appId: string;
27
+ files: BundleFiles;
28
+ initialPage?: string | null;
29
+ query?: string | Record<string, unknown> | null;
30
+ }
31
+
32
+ export interface LoadBundleFromVfsOptions {
33
+ appId: string;
34
+ baseUrl: string;
35
+ fetch?: typeof globalThis.fetch;
36
+ signal?: AbortSignal;
37
+ headers?: HeadersInit;
38
+ }
39
+
40
+ export interface OpenFromVfsOptions extends LoadBundleFromVfsOptions {
41
+ initialPage?: string | null;
42
+ query?: string | Record<string, unknown> | null;
43
+ }
44
+
45
+ export interface VfsManifestEntry {
46
+ path: string;
47
+ size?: number;
48
+ contentType?: string;
49
+ etag?: string;
50
+ }
51
+
52
+ export interface VfsManifest {
53
+ appId: string;
54
+ files: VfsManifestEntry[];
55
+ }
56
+
57
+ export interface LoadedVfsBundle {
58
+ appId: string;
59
+ manifest: VfsManifest;
60
+ files: Map<string, Uint8Array>;
61
+ }
62
+
63
+ export interface BindDomEventsOptions {
64
+ canvas?: HTMLCanvasElement;
65
+ keyboardTarget?: EventTarget | null;
66
+ focusTarget?: HTMLElement | HTMLCanvasElement | null;
67
+ preventWheelDefault?: boolean;
68
+ }
69
+
70
+ export declare function initInk(options?: InitInkOptions): Promise<{
71
+ InkWebView: unknown;
72
+ getInkVersion: () => string;
73
+ }>;
74
+
75
+ export declare function getInkVersion(): string;
76
+
77
+ export declare function normalizeBundleFiles(files: BundleFiles): Map<string, Uint8Array>;
78
+
79
+ export declare function serializeQuery(query: string | Record<string, unknown> | null | undefined): string | null;
80
+
81
+ export declare function createVfsUrls(input: {
82
+ baseUrl: string;
83
+ appId: string;
84
+ }): {
85
+ manifestUrl: string;
86
+ fileUrl(filePath: string): string;
87
+ };
88
+
89
+ export declare function loadBundleFromVfs(options: LoadBundleFromVfsOptions): Promise<LoadedVfsBundle>;
90
+
91
+ export declare class InkView {
92
+ static create(options: CreateInkViewOptions): Promise<InkView>;
93
+ bindCanvas(canvas: HTMLCanvasElement): this;
94
+ bindDomEvents(options?: BindDomEventsOptions): () => void;
95
+ openBundle(options: OpenBundleOptions): this;
96
+ openFromVfs(options: OpenFromVfsOptions): Promise<LoadedVfsBundle>;
97
+ resize(width: number, height: number, scaleFactor?: number): this;
98
+ focus(): this;
99
+ blur(): this;
100
+ notifyUserInteraction(): this;
101
+ render(): boolean;
102
+ requestRender(): this;
103
+ startRendering(): this;
104
+ stopRendering(): this;
105
+ isRunning(): boolean;
106
+ destroy(): void;
107
+ }
108
+
109
+ export declare function createInkView(options: CreateInkViewOptions): Promise<InkView>;
110
+
111
+ export declare function createFrameworkBindings(): {
112
+ ensureLeadingSlash(path: string): string;
113
+ initInk: typeof initInk;
114
+ createInkView: typeof createInkView;
115
+ };