@amritk/nish-x86_64-linux 0.37.0 → 0.38.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.
@@ -0,0 +1,86 @@
1
+ /**
2
+ * `nish/host/text` — text in module storage, for a wasm module and the page
3
+ * that drives it (`docs/wp50-wasm-host.md` §7).
4
+ *
5
+ * A string crosses out of a module as an export's result or a host import's
6
+ * argument, but an export cannot take one: the page writes text into a `u8[]`
7
+ * the module declares as module storage and passes the byte count. These are
8
+ * the module's halves of both directions:
9
+ *
10
+ * import { textRead, textWrite, textWriteInt } from "nish/host/text";
11
+ *
12
+ * export const INBOX = new Array<u8>(256);
13
+ * export const OUTBOX = new Array<u8>(256);
14
+ *
15
+ * // The page: n = new TextEncoder().encodeInto(text, views.INBOX).written
16
+ * export function echo(n: i32): i32 {
17
+ * const text = textRead(INBOX, n);
18
+ * if (text === null) return -1;
19
+ * return textWriteInt(OUTBOX, textWrite(OUTBOX, 0, text), toI64(n));
20
+ * }
21
+ * // The page: new TextDecoder().decode(views.OUTBOX.subarray(0, count))
22
+ *
23
+ * A writer takes the offset to write at and returns the offset after what it
24
+ * wrote, so writes chain. Text that does not fit writes nothing and returns
25
+ * -1, and every writer passes a -1 offset straight through, so a chain that
26
+ * overflowed anywhere ends in -1 with the bytes before the failed write left
27
+ * as they were. Nothing here traps on what a page wrote.
28
+ */
29
+
30
+ /**
31
+ * Writes `s` as its UTF-8 bytes at offset `at`, or nothing when it does not fit.
32
+ *
33
+ * @param buf - the text's storage
34
+ * @param at - the offset to write at; a negative one is an earlier failure
35
+ * @param s - the text
36
+ * @returns the offset after the text, or -1 when `at` is negative or past the
37
+ * buffer, or the text does not fit
38
+ */
39
+ export const textWrite = (buf: u8[], at: i32, s: string): i32 => {
40
+ const length: i32 = toI32(buf.length)
41
+ const n: i32 = toI32(s.length)
42
+ if (at < 0 || at > length || n > length - at) {
43
+ return -1
44
+ }
45
+ for (let i: i32 = 0; i < n; i++) {
46
+ buf[at + i] = toU8(s.charCodeAt(i))
47
+ }
48
+ return at + n
49
+ }
50
+
51
+ /**
52
+ * Writes `n` in decimal, as a template literal formats it.
53
+ *
54
+ * @param buf - the text's storage
55
+ * @param at - the offset to write at; a negative one is an earlier failure
56
+ * @param n - the integer
57
+ * @returns the offset after the digits, or -1 as `textWrite` answers it
58
+ */
59
+ export const textWriteInt = (buf: u8[], at: i32, n: i64): i32 => textWrite(buf, at, `${n}`)
60
+
61
+ /**
62
+ * Writes `x` in its shortest round-tripping form, as a template literal
63
+ * formats it: `0.30000000000000004`, `1e+21`, `NaN`, `-Infinity`.
64
+ *
65
+ * @param buf - the text's storage
66
+ * @param at - the offset to write at; a negative one is an earlier failure
67
+ * @param x - the number
68
+ * @returns the offset after the text, or -1 as `textWrite` answers it
69
+ */
70
+ export const textWriteF64 = (buf: u8[], at: i32, x: f64): i32 => textWrite(buf, at, `${x}`)
71
+
72
+ /**
73
+ * The first `n` bytes of `buf` as a string: what a page's `encodeInto` wrote.
74
+ * The bytes are not checked as UTF-8; a sequence that is not becomes U+FFFD
75
+ * when the string next crosses to the page.
76
+ *
77
+ * @param buf - the text's storage
78
+ * @param n - the byte count the page passed
79
+ * @returns the string, or null when `n` is negative or past the buffer
80
+ */
81
+ export const textRead = (buf: readonly u8[], n: i32): string | null => {
82
+ if (n < 0 || n > toI32(buf.length)) {
83
+ return null
84
+ }
85
+ return String.fromBytes(buf, 0, n)
86
+ }