@amritk/nish-aarch64-linux 0.46.0 → 0.48.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.
Files changed (46) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +29 -18
  3. package/package.json +1 -1
  4. package/runtime/gfx/README.md +55 -0
  5. package/runtime/gfx/nish-gfx.c +1063 -0
  6. package/runtime/gfx/nish-gfx.h +221 -0
  7. package/runtime/gfx/nish-gfx.overrides.json +14 -0
  8. package/runtime/nish.d.ts +22 -5
  9. package/runtime/nish.h +1 -0
  10. package/runtime/nish.mjs +1 -0
  11. package/runtime/runtime-bytes.c +167 -7
  12. package/runtime/runtime-net.c +122 -52
  13. package/runtime/runtime-uring.c +150 -69
  14. package/runtime/sqlite/nish-sqlite.c +166 -39
  15. package/scripts/build.sh +88 -3
  16. package/std/README.md +2 -2
  17. package/std/base64.ts +28 -30
  18. package/std/compress/deflate.ts +1371 -704
  19. package/std/crypto/aes.ts +81 -45
  20. package/std/crypto/bcrypt-batch.ts +23 -4
  21. package/std/crypto/bcrypt.ts +36 -11
  22. package/std/crypto/chacha20poly1305.ts +93 -52
  23. package/std/crypto/x25519.ts +7 -4
  24. package/std/host/game.ts +52 -20
  25. package/std/host/ring.ts +115 -31
  26. package/std/host/text.ts +9 -3
  27. package/std/json.ts +83 -0
  28. package/std/math.ts +161 -131
  29. package/std/net/handler.ts +24 -6
  30. package/std/net/http-server.ts +358 -141
  31. package/std/net/http1-server.ts +44 -13
  32. package/std/net/prefork.ts +1 -2
  33. package/std/net/static.ts +7 -2
  34. package/std/random.ts +34 -51
  35. package/std/raster.ts +377 -113
  36. package/std/regex.ts +46 -15
  37. package/std/release.ts +19 -13
  38. package/std/sqlite.ts +380 -16
  39. package/std/synth.ts +20 -3
  40. package/std/temporal.ts +740 -363
  41. package/std/term-io.ts +81 -33
  42. package/std/term-pixels.ts +287 -112
  43. package/std/term-probe.ts +7 -3
  44. package/std/term-rate.ts +142 -66
  45. package/std/term.ts +782 -406
  46. package/std/testing.ts +14 -7
@@ -0,0 +1,221 @@
1
+ /*
2
+ * nish-gfx.h: the C surface Nish programs draw through.
3
+ *
4
+ * A thin, synchronous shim over wgpu-native (WebGPU) and SDL3. It exists so
5
+ * that `nish c-import` can read it: every parameter is a scalar, an opaque
6
+ * handle or a struct passed by pointer; there are no unions, no structs by
7
+ * value, no callbacks and no variadics. A byte buffer is a `void *` or
8
+ * `const void *` followed by its `size_t` length. WebGPU enum and flag values
9
+ * cross as plain uint32_t, with webgpu.h's numbering. This header does not
10
+ * include webgpu.h or SDL.h; only nish-gfx.c does.
11
+ *
12
+ * Errors never cross as callbacks. Every device-lost and uncaptured-error
13
+ * report is copied into a fixed ring inside the device and drained with
14
+ * nishgfx_error_next(); a report that finds the ring full is counted by
15
+ * nishgfx_errors_dropped(). Failures before a device exists (no adapter, a
16
+ * refused device) are queued on the instance the same way.
17
+ */
18
+ #ifndef NISH_GFX_H
19
+ #define NISH_GFX_H
20
+
21
+ #include <stddef.h>
22
+ #include <stdint.h>
23
+
24
+ #ifdef __cplusplus
25
+ extern "C" {
26
+ #endif
27
+
28
+ typedef struct NishGfxInstance NishGfxInstance;
29
+ typedef struct NishGfxWindow NishGfxWindow;
30
+ typedef struct NishGfxSurface NishGfxSurface;
31
+ typedef struct NishGfxAdapter NishGfxAdapter;
32
+ typedef struct NishGfxDevice NishGfxDevice;
33
+ typedef struct NishGfxBuffer NishGfxBuffer;
34
+ typedef struct NishGfxShader NishGfxShader;
35
+ typedef struct NishGfxPipelineDesc NishGfxPipelineDesc;
36
+ typedef struct NishGfxPipeline NishGfxPipeline;
37
+ typedef struct NishGfxBindGroupDesc NishGfxBindGroupDesc;
38
+ typedef struct NishGfxBindGroup NishGfxBindGroup;
39
+ typedef struct NishGfxEncoder NishGfxEncoder;
40
+ typedef struct NishGfxPass NishGfxPass;
41
+ typedef struct NishGfxView NishGfxView;
42
+ typedef struct NishGfxOffscreen NishGfxOffscreen;
43
+
44
+ /* Error kinds nishgfx_error_next() answers. */
45
+ #define NISHGFX_ERROR_NONE 0
46
+ #define NISHGFX_ERROR_VALIDATION 1
47
+ #define NISHGFX_ERROR_OUT_OF_MEMORY 2
48
+ #define NISHGFX_ERROR_INTERNAL 3
49
+ #define NISHGFX_ERROR_DEVICE_LOST 4
50
+
51
+ /* The error ring's size, per instance and per device. */
52
+ #define NISHGFX_ERROR_RING 16
53
+ /* The longest message an entry keeps, without its NUL. */
54
+ #define NISHGFX_ERROR_MESSAGE_MAX 255
55
+
56
+ /* Event kinds nishgfx_event_next() answers. */
57
+ #define NISHGFX_EVENT_NONE 0
58
+ #define NISHGFX_EVENT_QUIT 1
59
+ #define NISHGFX_EVENT_KEY_DOWN 2
60
+ #define NISHGFX_EVENT_KEY_UP 3
61
+ #define NISHGFX_EVENT_MOUSE_MOVE 4
62
+ #define NISHGFX_EVENT_MOUSE_DOWN 5
63
+ #define NISHGFX_EVENT_MOUSE_UP 6
64
+ #define NISHGFX_EVENT_WHEEL 7
65
+ #define NISHGFX_EVENT_RESIZED 8
66
+
67
+ /* The event ring's size, per window. */
68
+ #define NISHGFX_EVENT_RING 64
69
+
70
+ /* One input or window event. Fields a kind does not use are zero. */
71
+ typedef struct NishGfxEvent {
72
+ uint32_t kind; /* NISHGFX_EVENT_* */
73
+ uint32_t key; /* SDL keycode, for KEY_DOWN and KEY_UP */
74
+ uint32_t button; /* 1 left, 2 middle, 3 right, for MOUSE_DOWN and MOUSE_UP */
75
+ uint32_t modifiers; /* SDL_Keymod bits, for keys */
76
+ float x, y; /* pointer position in window pixels */
77
+ float dx, dy; /* motion or wheel delta */
78
+ uint32_t width, height; /* new size in pixels, for RESIZED */
79
+ } NishGfxEvent;
80
+
81
+ /* A buffer to create. `usage` is WGPUBufferUsage bits. */
82
+ typedef struct NishGfxBufferDesc {
83
+ uint64_t size;
84
+ uint32_t usage;
85
+ uint32_t mapped_at_creation;
86
+ } NishGfxBufferDesc;
87
+
88
+ /* ---- instance, window, surface ---------------------------------------- */
89
+
90
+ /* A WebGPU instance. Answers null if wgpu-native refuses one. */
91
+ NishGfxInstance *nishgfx_instance_create(void);
92
+ /* Release it last: every surface, adapter and device made from it keeps it,
93
+ * and a device's readback drives events through it. */
94
+ void nishgfx_instance_release(NishGfxInstance *instance);
95
+ /* Drain one queued instance error (adapter or device refusal) into msg,
96
+ * NUL-terminated and cut to cap - 1 bytes; answers its kind, 0 when empty. */
97
+ int32_t nishgfx_instance_error_next(NishGfxInstance *instance, void *msg, size_t cap);
98
+
99
+ /* An SDL3 window of width x height pixels; `title` is NUL-terminated UTF-8.
100
+ * Answers null for a 0 side or one past INT32_MAX, when SDL has no video
101
+ * driver, when memory or SDL's window properties run out, and when SDL
102
+ * refuses the window; SDL_GetError() says why for the SDL failures. */
103
+ NishGfxWindow *nishgfx_window_create(const char *title, uint32_t width, uint32_t height);
104
+ /* Release a surface made on the window first: it reads the window's size. */
105
+ void nishgfx_window_release(NishGfxWindow *window);
106
+ /* Move every pending SDL event into the window's ring; answers how many were
107
+ * queued. Events past the ring's size are dropped and counted. */
108
+ uint32_t nishgfx_events_poll(NishGfxWindow *window);
109
+ /* Take the oldest queued event into *out; answers its kind, 0 when empty. */
110
+ uint32_t nishgfx_event_next(NishGfxWindow *window, NishGfxEvent *out);
111
+ uint32_t nishgfx_events_dropped(NishGfxWindow *window);
112
+
113
+ /* A surface on the window, through Metal, Wayland or X11 as SDL's current
114
+ * video driver says. Answers null (and queues an instance error) otherwise. */
115
+ NishGfxSurface *nishgfx_surface_create(NishGfxInstance *instance, NishGfxWindow *window);
116
+ void nishgfx_surface_release(NishGfxSurface *surface);
117
+ /* Configure for width x height. vsync != 0 is Fifo; otherwise Mailbox, then
118
+ * Immediate, then Fifo, whichever the surface offers first. Answers 0, or -1
119
+ * (and queues a device error) for a 0 side or a surface with no format. */
120
+ int32_t nishgfx_surface_configure(NishGfxSurface *surface, NishGfxAdapter *adapter, NishGfxDevice *device,
121
+ uint32_t width, uint32_t height, uint32_t vsync);
122
+ /* The WGPUTextureFormat configure picked. */
123
+ uint32_t nishgfx_surface_format(NishGfxSurface *surface);
124
+ /* The next frame's view, or null to skip this frame (an outdated, lost or
125
+ * timed-out surface is reconfigured at the window's pixel size, or left until
126
+ * a frame where that size is above 0; any other failure queues a device
127
+ * error). The view lives until nishgfx_surface_present(). */
128
+ NishGfxView *nishgfx_surface_acquire(NishGfxSurface *surface);
129
+ void nishgfx_surface_present(NishGfxSurface *surface);
130
+
131
+ /* ---- adapter, device, errors ------------------------------------------- */
132
+
133
+ /* An adapter, compatible with `surface` when it is not null. power is a
134
+ * WGPUPowerPreference. Answers null and queues an instance error on failure. */
135
+ NishGfxAdapter *nishgfx_adapter_request(NishGfxInstance *instance, NishGfxSurface *surface, uint32_t power);
136
+ void nishgfx_adapter_release(NishGfxAdapter *adapter);
137
+ /* A device with default limits. Answers null and queues an instance error on
138
+ * failure. Its device-lost and uncaptured-error reports go to its ring. */
139
+ NishGfxDevice *nishgfx_device_request(NishGfxInstance *instance, NishGfxAdapter *adapter);
140
+ /* Release every buffer, shader, pipeline, bind group, offscreen target and
141
+ * surface made from the device first: one used after this can report an
142
+ * error into the freed device. */
143
+ void nishgfx_device_release(NishGfxDevice *device);
144
+ /* Drain one queued device error into msg (as nishgfx_instance_error_next). */
145
+ int32_t nishgfx_error_next(NishGfxDevice *device, void *msg, size_t cap);
146
+ /* How many reports found the ring full, since the device was made. */
147
+ uint32_t nishgfx_errors_dropped(NishGfxDevice *device);
148
+
149
+ /* ---- resources --------------------------------------------------------- */
150
+
151
+ /* `label` may be null. Answers null when wgpu-native does. */
152
+ NishGfxBuffer *nishgfx_buffer_create(NishGfxDevice *device, const NishGfxBufferDesc *desc, const char *label);
153
+ void nishgfx_buffer_release(NishGfxBuffer *buffer);
154
+ /* Copy len bytes of data into buffer at offset, on the device's queue. */
155
+ void nishgfx_queue_write(NishGfxDevice *device, NishGfxBuffer *buffer, uint64_t offset,
156
+ const void *data, size_t len);
157
+ /* A shader module from NUL-terminated WGSL. A compile error is queued on the
158
+ * device; the module answered is then invalid but safe to pass on. */
159
+ NishGfxShader *nishgfx_shader_create(NishGfxDevice *device, const char *wgsl);
160
+ void nishgfx_shader_release(NishGfxShader *shader);
161
+
162
+ /* A render pipeline builder: one vertex buffer of `stride` bytes (0 for no
163
+ * vertex buffer), one color target of `color_format`, triangle list. */
164
+ NishGfxPipelineDesc *nishgfx_pipeline_desc_new(NishGfxShader *shader, const char *vs_entry, const char *fs_entry,
165
+ uint32_t color_format, uint64_t stride);
166
+ /* Add a vertex attribute (a WGPUVertexFormat at offset, shader location).
167
+ * Answers 0, or -1 once the builder holds 16. */
168
+ int32_t nishgfx_pipeline_desc_attribute(NishGfxPipelineDesc *desc, uint32_t format, uint64_t offset,
169
+ uint32_t location);
170
+ /* Build the pipeline and free the builder, either way. */
171
+ NishGfxPipeline *nishgfx_pipeline_create(NishGfxDevice *device, NishGfxPipelineDesc *desc);
172
+ void nishgfx_pipeline_release(NishGfxPipeline *pipeline);
173
+
174
+ /* A bind group builder for group `index` of the pipeline's layout. */
175
+ NishGfxBindGroupDesc *nishgfx_bind_group_desc_new(NishGfxPipeline *pipeline, uint32_t index);
176
+ /* Bind size bytes of buffer at offset to `binding`; size 0 binds the rest of
177
+ * the buffer from offset. Answers 0, or -1 once the builder holds 16. */
178
+ int32_t nishgfx_bind_group_desc_buffer(NishGfxBindGroupDesc *desc, uint32_t binding, NishGfxBuffer *buffer,
179
+ uint64_t offset, uint64_t size);
180
+ /* Build the bind group and free the builder, either way. */
181
+ NishGfxBindGroup *nishgfx_bind_group_create(NishGfxDevice *device, NishGfxBindGroupDesc *desc);
182
+ void nishgfx_bind_group_release(NishGfxBindGroup *group);
183
+
184
+ /* ---- encoding ---------------------------------------------------------- */
185
+
186
+ NishGfxEncoder *nishgfx_encoder_create(NishGfxDevice *device);
187
+ /* Finish the encoder, submit it on the device's queue, and free it. */
188
+ void nishgfx_encoder_submit(NishGfxDevice *device, NishGfxEncoder *encoder);
189
+
190
+ /* A render pass on one color view. load != 0 keeps the view's contents;
191
+ * otherwise it is cleared to (r, g, b, a). */
192
+ NishGfxPass *nishgfx_pass_begin(NishGfxEncoder *encoder, NishGfxView *view, uint32_t load,
193
+ double r, double g, double b, double a);
194
+ void nishgfx_pass_set_pipeline(NishGfxPass *pass, NishGfxPipeline *pipeline);
195
+ void nishgfx_pass_set_bind_group(NishGfxPass *pass, uint32_t index, NishGfxBindGroup *group);
196
+ /* size 0 binds the rest of the buffer from offset. */
197
+ void nishgfx_pass_set_vertex_buffer(NishGfxPass *pass, uint32_t slot, NishGfxBuffer *buffer, uint64_t offset,
198
+ uint64_t size);
199
+ void nishgfx_pass_draw(NishGfxPass *pass, uint32_t vertices, uint32_t instances, uint32_t first_vertex,
200
+ uint32_t first_instance);
201
+ /* End the pass and free it. */
202
+ void nishgfx_pass_end(NishGfxPass *pass);
203
+
204
+ /* ---- offscreen --------------------------------------------------------- */
205
+
206
+ /* A width x height RGBA8Unorm render target with readback; needs no window
207
+ * and initialises no SDL video. */
208
+ NishGfxOffscreen *nishgfx_offscreen_create(NishGfxDevice *device, uint32_t width, uint32_t height);
209
+ void nishgfx_offscreen_release(NishGfxOffscreen *offscreen);
210
+ /* The target's view, valid until the offscreen is released. */
211
+ NishGfxView *nishgfx_offscreen_view(NishGfxOffscreen *offscreen);
212
+ /* Copy the target's pixels, width * 4 bytes per row, top row first, into dst.
213
+ * Answers 0; -1 when cap < width * height * 4 (dst untouched); -2 when the
214
+ * map fails (an error is queued on the device). */
215
+ int32_t nishgfx_offscreen_read(NishGfxOffscreen *offscreen, void *dst, size_t cap);
216
+
217
+ #ifdef __cplusplus
218
+ }
219
+ #endif
220
+
221
+ #endif /* NISH_GFX_H */
@@ -0,0 +1,14 @@
1
+ {
2
+ "header": "nish-gfx.h",
3
+ "allow": ["nishgfx_*"],
4
+ "functions": {
5
+ "nishgfx_instance_error_next": { "msg": { "buffer": "cap" } },
6
+ "nishgfx_error_next": { "msg": { "buffer": "cap" } },
7
+ "nishgfx_queue_write": { "data": { "buffer": "len" } },
8
+ "nishgfx_offscreen_read": { "dst": { "buffer": "cap" } },
9
+ "nishgfx_buffer_create": { "label": { "nullable": true } },
10
+ "nishgfx_adapter_request": { "surface": { "nullable": true } },
11
+ "nishgfx_pass_set_bind_group": { "group": { "nullable": true } },
12
+ "nishgfx_pass_set_vertex_buffer": { "buffer": { "nullable": true } }
13
+ }
14
+ }
package/runtime/nish.d.ts CHANGED
@@ -1674,14 +1674,16 @@ declare function tcpListen(host: string, port: i32, backlog: i32): i32;
1674
1674
  * Flag 8 (`NET_LISTEN_RING` in `nish:net`) accepts into io_uring slots on
1675
1675
  * Linux, so connections are not bound by the descriptor limit; where rings are
1676
1676
  * refused the listener serves descriptors and `netRingState` says why. Elsewhere
1677
- * the flag is accepted and does nothing.
1677
+ * the flag is accepted and does nothing. Flag 16 (`NET_LISTEN_RING_WIDE`), with 8, makes each
1678
+ * ring hold as many slots as the descriptor limit allows, up to 65,536 rather than 16,384: fewer
1679
+ * rings for a busy loop to enter, at about 110 bytes a slot per ring up front.
1678
1680
  *
1679
1681
  * @param host the numeric address to bind.
1680
1682
  * @param port the port, 0 for any.
1681
1683
  * @param backlog the kernel's queue of unaccepted connections.
1682
- * @param flags 1, 2, 4 and 8 as above.
1684
+ * @param flags 1, 2, 4, 8 and 16 as above.
1683
1685
  * @returns the listener's descriptor, or a negative errno: -22 for any other bit (a negative
1684
- * `flags` included) or 4 without 1, before a socket is made; -95 for flag 4 off Linux; -98
1686
+ * `flags` included), 4 without 1 or 16 without 8, before a socket is made; -95 for flag 4 off Linux; -98
1685
1687
  * for a port in use; -23 once the process has opened 31 ring listeners, closed or not.
1686
1688
  */
1687
1689
  declare function tcpListenFlags(host: string, port: i32, backlog: i32, flags: i32): i32;
@@ -2277,6 +2279,15 @@ declare const NET_UNIX_SEQPACKET: i32;
2277
2279
  * descriptors, and `netRingState` says why. Inert on Darwin. Capability: none.
2278
2280
  */
2279
2281
  declare const NET_LISTEN_RING: i32;
2282
+ /**
2283
+ * `tcpListenFlags` flag 16, with `NET_LISTEN_RING`: each of the listener's rings holds as many
2284
+ * slots as `RLIMIT_NOFILE` allows, up to 65,536, rather than up to 16,384. A busy loop enters
2285
+ * each ring once a pass, so fewer, wider rings cost it less; each slot costs about 110 bytes a
2286
+ * ring up front, about 7 MB for a ring of 65,536. A ring listener under `--sandbox` takes rings
2287
+ * made before its filter, at the narrow width. Without `NET_LISTEN_RING` it is -22. Inert on
2288
+ * Darwin. Capability: none.
2289
+ */
2290
+ declare const NET_LISTEN_RING_WIDE: i32;
2280
2291
  /**
2281
2292
  * Whether listeners opened with `NET_LISTEN_RING` accept into rings: 1 when they do, 0 until a
2282
2293
  * listener has found rings or a refusal, or the negative errno the kernel refused rings with (-1
@@ -3220,13 +3231,17 @@ declare module "nish:net" {
3220
3231
  * memory serves descriptors itself, and a later listener asks again. Off
3221
3232
  * Linux the flag is accepted and does nothing.
3222
3233
  *
3234
+ * Flag 16, `NET_LISTEN_RING_WIDE`, with flag 8, makes each ring hold as
3235
+ * many slots as `RLIMIT_NOFILE` allows, up to 65,536 rather than 16,384.
3236
+ *
3223
3237
  * @param host A numeric host, as for `tcpListen`.
3224
3238
  * @param port The port.
3225
3239
  * @param backlog The length of the accept queue.
3226
3240
  * @param flags The sum of the flags: `3` is both, `5` and `7` steer, `8`
3227
- * accepts into rings, and `0` is `tcpListen` exactly.
3241
+ * accepts into rings, `24` into wide ones, and `0` is `tcpListen` exactly.
3228
3242
  * @returns The listener's descriptor or the failure: `-22` for any other
3229
- * bit, a negative `flags`, or flag 4 without 1, before a socket is made;
3243
+ * bit, a negative `flags`, flag 4 without 1, or flag 16 without 8, before
3244
+ * a socket is made;
3230
3245
  * `-95` for flag 4 off Linux; `-23` once the process has opened 31 ring
3231
3246
  * listeners, closed or not.
3232
3247
  */
@@ -3683,6 +3698,8 @@ declare module "nish:net" {
3683
3698
  export const NET_UNIX_SEQPACKET: i32;
3684
3699
  /** `tcpListenFlags` flag 8: accept into io_uring slots instead of descriptors. */
3685
3700
  export const NET_LISTEN_RING: i32;
3701
+ /** `tcpListenFlags` flag 16, with flag 8: rings of up to 65,536 slots rather than 16,384. */
3702
+ export const NET_LISTEN_RING_WIDE: i32;
3686
3703
  /**
3687
3704
  * Whether `NET_LISTEN_RING` listeners accept into rings.
3688
3705
  *
package/runtime/nish.h CHANGED
@@ -303,6 +303,7 @@ int nish_f64_format(double v, char out[NISH_F64_MAX]);
303
303
  end, or -1, with nothing written, when `at < 0` or they do not fit before
304
304
  `len`. `buf` may be null only when nothing fits. */
305
305
  int32_t nish_json_put_f64(uint8_t *buf, int32_t len, int32_t at, double v);
306
+ int32_t nish_json_put_string(uint8_t *buf, int32_t len, int32_t at, const uint8_t *src, int64_t from, int64_t to);
306
307
  nish_str *nish_str_from_i64(int64_t v);
307
308
  /* Unsigned decimal (WP15). u8/u16/u32 are zero-extended by the caller, so one
308
309
  symbol serves every unsigned width. */
package/runtime/nish.mjs CHANGED
@@ -185,6 +185,7 @@ provide("NET_UNIX_UNLINK_STALE", 1);
185
185
  provide("NET_UNIX_STREAM", 1);
186
186
  provide("NET_UNIX_SEQPACKET", 2);
187
187
  provide("NET_LISTEN_RING", 8);
188
+ provide("NET_LISTEN_RING_WIDE", 16);
188
189
 
189
190
  // `nish:term`. Whether a descriptor is a terminal and how big stdout's is have
190
191
  // Node's answers; reading stdin, waiting for it and raw mode throw, each naming
@@ -247,14 +247,28 @@ static inline unsigned nish_run_stops_sse2(int cls, __m128i v) {
247
247
  return (unsigned)_mm_movemask_epi8(_mm_or_si128(stop, v));
248
248
  }
249
249
 
250
+ static inline unsigned nish_plain_stops_sse2(const uint8_t *p) {
251
+ __m128i v = _mm_loadu_si128((const __m128i *)p);
252
+ __m128i stop = _mm_or_si128(_mm_or_si128(_mm_cmpeq_epi8(v, _mm_set1_epi8(0x22)), _mm_cmpeq_epi8(v, _mm_set1_epi8(0x5c))),
253
+ _mm_cmpeq_epi8(_mm_min_epu8(v, _mm_set1_epi8(0x1f)), v));
254
+ return (unsigned)_mm_movemask_epi8(stop);
255
+ }
256
+
257
+ /* A span of 16 bytes or more ends with one block that overlaps the last one
258
+ rather than a byte at a time: it lies inside the span, and the bytes it
259
+ shares with the blocks already read are masked off, so a stop among them
260
+ is not found twice. */
250
261
  static int64_t nish_plain_base(int cls, const uint8_t *p, int64_t i, int64_t to) {
262
+ int64_t from = i;
251
263
  for (; to - i >= 16; i += 16) {
252
- __m128i v = _mm_loadu_si128((const __m128i *)(p + i));
253
- __m128i stop = _mm_or_si128(_mm_or_si128(_mm_cmpeq_epi8(v, _mm_set1_epi8(0x22)), _mm_cmpeq_epi8(v, _mm_set1_epi8(0x5c))),
254
- _mm_cmpeq_epi8(_mm_min_epu8(v, _mm_set1_epi8(0x1f)), v));
255
- unsigned m = (unsigned)_mm_movemask_epi8(stop);
264
+ unsigned m = nish_plain_stops_sse2(p + i);
256
265
  if (m != 0) return i + __builtin_ctz(m);
257
266
  }
267
+ if (i < to && to - from >= 16) {
268
+ int64_t at = to - 16;
269
+ unsigned m = nish_plain_stops_sse2(p + at) & (~0u << (i - at));
270
+ return m != 0 ? at + __builtin_ctz(m) : to;
271
+ }
258
272
  return nish_plain_scalar(cls, p, i, to);
259
273
  }
260
274
 
@@ -357,13 +371,24 @@ static inline uint64_t nish_run_stops_neon(int cls, uint8x16_t v) {
357
371
  return vget_lane_u64(vreinterpret_u64_u8(vshrn_n_u16(vreinterpretq_u16_u8(stop), 4)), 0);
358
372
  }
359
373
 
374
+ static inline uint64_t nish_plain_stops_neon(const uint8_t *p) {
375
+ uint8x16_t v = vld1q_u8(p);
376
+ uint8x16_t stop = vorrq_u8(vorrq_u8(vceqq_u8(v, vdupq_n_u8(0x22)), vceqq_u8(v, vdupq_n_u8(0x5c))), vcleq_u8(v, vdupq_n_u8(0x1f)));
377
+ return vget_lane_u64(vreinterpret_u64_u8(vshrn_n_u16(vreinterpretq_u16_u8(stop), 4)), 0);
378
+ }
379
+
380
+ /* The SSE2 path's overlapping last block, four mask bits a byte. */
360
381
  static int64_t nish_plain_base(int cls, const uint8_t *p, int64_t i, int64_t to) {
382
+ int64_t from = i;
361
383
  for (; to - i >= 16; i += 16) {
362
- uint8x16_t v = vld1q_u8(p + i);
363
- uint8x16_t stop = vorrq_u8(vorrq_u8(vceqq_u8(v, vdupq_n_u8(0x22)), vceqq_u8(v, vdupq_n_u8(0x5c))), vcleq_u8(v, vdupq_n_u8(0x1f)));
364
- uint64_t m = vget_lane_u64(vreinterpret_u64_u8(vshrn_n_u16(vreinterpretq_u16_u8(stop), 4)), 0);
384
+ uint64_t m = nish_plain_stops_neon(p + i);
365
385
  if (m != 0) return i + (int64_t)(__builtin_ctzll(m) >> 2);
366
386
  }
387
+ if (i < to && to - from >= 16) {
388
+ int64_t at = to - 16;
389
+ uint64_t m = nish_plain_stops_neon(p + at) & (~0ull << (4 * (i - at)));
390
+ return m != 0 ? at + (int64_t)(__builtin_ctzll(m) >> 2) : to;
391
+ }
367
392
  return nish_plain_scalar(cls, p, i, to);
368
393
  }
369
394
 
@@ -483,3 +508,138 @@ int64_t nish_bytes_skip_json_text(const uint8_t *p, int64_t from, int64_t to) {
483
508
  int64_t nish_bytes_skip_json_plain(const uint8_t *p, int64_t from, int64_t to) {
484
509
  return NISH_PLAIN_PATH(NISH_RUN_PLAIN, p, from, to);
485
510
  }
511
+
512
+ /* One byte JSON escapes, at `buf[o]`: `\"`, `\\`, the five short escapes, or
513
+ `\u00XX` in lower-case hex, as std/json.ts's `jsonShortEscape` and
514
+ `jsonHexByte` spell them. Answers the new end, or -1 when it does not fit
515
+ before `cap`. */
516
+ static int64_t nish_json_escape_byte(uint8_t *buf, int64_t o, int64_t cap, uint8_t c) {
517
+ uint8_t letter = c == 0x22 || c == 0x5c ? c
518
+ : c == 0x0a ? 'n'
519
+ : c == 0x09 ? 't'
520
+ : c == 0x0d ? 'r'
521
+ : c == 0x08 ? 'b'
522
+ : c == 0x0c ? 'f'
523
+ : 0;
524
+ if (letter) {
525
+ if (cap - o < 2) return -1;
526
+ buf[o] = 0x5c;
527
+ buf[o + 1] = letter;
528
+ return o + 2;
529
+ }
530
+ if (cap - o < 6) return -1;
531
+ static const char hex[] = "0123456789abcdef";
532
+ buf[o] = 0x5c;
533
+ buf[o + 1] = 'u';
534
+ buf[o + 2] = '0';
535
+ buf[o + 3] = '0';
536
+ buf[o + 4] = (uint8_t)hex[c >> 4];
537
+ buf[o + 5] = (uint8_t)hex[c & 15];
538
+ return o + 6;
539
+ }
540
+
541
+ #if defined(NISH_SIMD_SSE2)
542
+ /* The stops among the `n` bytes at `p`, 4 <= n <= 16, as bits from 0 for
543
+ p[0]: read as two loads that may overlap, of 4, 8 or 16 bytes, never
544
+ outside the `n`. */
545
+ static inline unsigned nish_json_stops_short(const uint8_t *p, int64_t n) {
546
+ __m128i v;
547
+ unsigned lo, hi;
548
+ if (n >= 8) {
549
+ v = _mm_unpacklo_epi64(_mm_loadl_epi64((const __m128i *)p), _mm_loadl_epi64((const __m128i *)(p + n - 8)));
550
+ unsigned m = (unsigned)_mm_movemask_epi8(_mm_or_si128(
551
+ _mm_or_si128(_mm_cmpeq_epi8(v, _mm_set1_epi8(0x22)), _mm_cmpeq_epi8(v, _mm_set1_epi8(0x5c))),
552
+ _mm_cmpeq_epi8(_mm_min_epu8(v, _mm_set1_epi8(0x1f)), v)));
553
+ lo = m & 0xff;
554
+ hi = (m >> 8) << (n - 8);
555
+ } else {
556
+ uint32_t a, b;
557
+ __builtin_memcpy(&a, p, 4);
558
+ __builtin_memcpy(&b, p + n - 4, 4);
559
+ v = _mm_unpacklo_epi32(_mm_cvtsi32_si128((int)a), _mm_cvtsi32_si128((int)b));
560
+ unsigned m = (unsigned)_mm_movemask_epi8(_mm_or_si128(
561
+ _mm_or_si128(_mm_cmpeq_epi8(v, _mm_set1_epi8(0x22)), _mm_cmpeq_epi8(v, _mm_set1_epi8(0x5c))),
562
+ _mm_cmpeq_epi8(_mm_min_epu8(v, _mm_set1_epi8(0x1f)), v)));
563
+ lo = m & 0xf;
564
+ hi = ((m >> 4) & 0xf) << (n - 4);
565
+ }
566
+ return lo | hi;
567
+ }
568
+ #endif
569
+
570
+ /* The first byte of `p[i, to)` JSON escapes, or `to`: `skipJsonPlain`'s
571
+ answer. With SSE2 every window of 4 bytes or more is read in blocks that
572
+ lie inside it, the last overlapping the one before. */
573
+ static inline int64_t nish_json_plain_end(const uint8_t *p, int64_t i, int64_t to) {
574
+ #if defined(NISH_SIMD_SSE2)
575
+ int64_t n = to - i;
576
+ if (n >= 16) {
577
+ for (; to - i >= 16; i += 16) {
578
+ unsigned m = nish_plain_stops_sse2(p + i);
579
+ if (m != 0) return i + __builtin_ctz(m);
580
+ }
581
+ if (i == to) return to;
582
+ int64_t at = to - 16;
583
+ unsigned m = nish_plain_stops_sse2(p + at) & (~0u << (i - at));
584
+ return m != 0 ? at + __builtin_ctz(m) : to;
585
+ }
586
+ if (n >= 4) {
587
+ unsigned m = nish_json_stops_short(p + i, n);
588
+ return m != 0 ? i + __builtin_ctz(m) : to;
589
+ }
590
+ #endif
591
+ while (i < to && p[i] >= 0x20 && p[i] != 0x22 && p[i] != 0x5c) i++;
592
+ return i;
593
+ }
594
+
595
+ /* `n` bytes from `s` to `d`, which do not overlap, in moves of 16, 8 or 4
596
+ bytes that may overlap one another, never outside the `n`. */
597
+ static inline void nish_json_copy(uint8_t *d, const uint8_t *s, int64_t n) {
598
+ if (n >= 16) {
599
+ int64_t k = 0;
600
+ for (; n - k > 16; k += 16) __builtin_memcpy(d + k, s + k, 16);
601
+ __builtin_memcpy(d + n - 16, s + n - 16, 16);
602
+ } else if (n >= 8) {
603
+ uint64_t a, b;
604
+ __builtin_memcpy(&a, s, 8);
605
+ __builtin_memcpy(&b, s + n - 8, 8);
606
+ __builtin_memcpy(d, &a, 8);
607
+ __builtin_memcpy(d + n - 8, &b, 8);
608
+ } else if (n >= 4) {
609
+ uint32_t a, b;
610
+ __builtin_memcpy(&a, s, 4);
611
+ __builtin_memcpy(&b, s + n - 4, 4);
612
+ __builtin_memcpy(d, &a, 4);
613
+ __builtin_memcpy(d + n - 4, &b, 4);
614
+ } else {
615
+ for (int64_t k = 0; k < n; k++) d[k] = s[k];
616
+ }
617
+ }
618
+
619
+ /* `jsonPutString`'s walk (std/json.ts; the emitter makes the call,
620
+ src/kernels.ts): `src[from, to)` as a JSON string, its quotes and escapes
621
+ included, written into `buf` from `at`. Answers the new end, or -1 when
622
+ `at` is negative or the text does not fit before `len`, the array's length
623
+ as the Nish code's `toI32` reads it; the bytes from `at` may then have been
624
+ written, and the caller's cursor has not moved. The caller has checked
625
+ `0 <= from <= to <= src's length`. A run of bytes that need no escape is
626
+ found by `nish_json_plain_end` and copied in one piece, so a string with
627
+ nothing to escape is one scan and one copy. */
628
+ int32_t nish_json_put_string(uint8_t *buf, int32_t len, int32_t at, const uint8_t *src, int64_t from, int64_t to) {
629
+ int64_t cap = len, o = at, i = from;
630
+ if (at < 0 || o >= cap) return -1;
631
+ buf[o++] = 0x22;
632
+ while (i < to) {
633
+ int64_t j = nish_json_plain_end(src, i, to);
634
+ if (cap - o < j - i) return -1;
635
+ nish_json_copy(buf + o, src + i, j - i);
636
+ o += j - i;
637
+ if (j == to) break;
638
+ o = nish_json_escape_byte(buf, o, cap, src[j]);
639
+ if (o < 0) return -1;
640
+ i = j + 1;
641
+ }
642
+ if (o >= cap) return -1;
643
+ buf[o++] = 0x22;
644
+ return (int32_t)o;
645
+ }