@scriptc/runtime 0.0.8 → 0.0.10

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.
@@ -7,9 +7,14 @@
7
7
  #include "scr_runtime.h"
8
8
 
9
9
  #include <errno.h>
10
+ #include <math.h>
10
11
  #include <stdio.h>
11
12
  #include <stdlib.h>
12
13
  #include <string.h>
14
+ #include <time.h>
15
+ #ifdef __APPLE__
16
+ #include <sys/stat.h> /* lchmod(2) — the fs.lchmodSync ladder's real tail */
17
+ #endif
13
18
 
14
19
  static void scr_bytes_io_oom(void) {
15
20
  scr_trap("scriptc: out of memory\n");
@@ -138,7 +143,7 @@ ScrBytes *scr_crypto_random_bytes(double n) {
138
143
  msg, sizeof msg,
139
144
  "The value of \"size\" is out of range. It must be >= 0 && <= 2147483647. Received %.*s",
140
145
  (int)numlen, num);
141
- scr_throw_error_msg(SCR_ERR_RANGE, msg, (size_t)mlen);
146
+ scr_throw_error_msg_code(SCR_ERR_RANGE, msg, (size_t)mlen, "ERR_OUT_OF_RANGE");
142
147
  return NULL;
143
148
  }
144
149
  ScrBytes *b = scr_bytes_new(SCR_BYTES_U8, n);
@@ -158,3 +163,504 @@ bool scr_process_stderr_write_bytes(const ScrBytes *b) {
158
163
  fwrite(b->data, 1, b->len * scr_bytes_elem_size(b->elem), stderr);
159
164
  return true;
160
165
  }
166
+
167
+ /* ── the checked-dynamic Buffer compare/equals validators ──────────────
168
+ * Node's argument ladders for buf.equals / buf.compare / Buffer.compare
169
+ * over DOM-boxed arguments (the invalid-input probes: string needles,
170
+ * '0' offsets, null/object range args). A well-typed dyn still computes
171
+ * the real answer — validation, not a constant fence. */
172
+
173
+ /* A bytes payload or the API's own ERR_INVALID_ARG_TYPE (borrowed). */
174
+ static ScrBytes *scr_bytes_chk_u8(const ScrDyn *d, const char *argname) {
175
+ if (d->kind != SCR_DYN_BYTES) {
176
+ scr_dyn_arg_type_fail(argname, "an instance of Buffer or Uint8Array", d);
177
+ return NULL;
178
+ }
179
+ return d->v.bytes;
180
+ }
181
+
182
+ double scr_buffer_compare_chk(const ScrDyn *a, const ScrDyn *b) {
183
+ ScrBytes *b1 = scr_bytes_chk_u8(a, "buf1");
184
+ if (!b1) return 0;
185
+ ScrBytes *b2 = scr_bytes_chk_u8(b, "buf2");
186
+ if (!b2) return 0;
187
+ return scr_bytes_compare(b1, b2, 0, 0, 0, 0, 0);
188
+ }
189
+
190
+ bool scr_bytes_equals_chk(const ScrBytes *recv, const ScrDyn *other) {
191
+ ScrBytes *o = scr_bytes_chk_u8(other, "otherBuffer");
192
+ if (!o) return false;
193
+ return scr_bytes_equals(recv, o);
194
+ }
195
+
196
+ /* One offset slot: undefined takes the Node default, non-numbers throw
197
+ * ERR_INVALID_ARG_TYPE "of type number", numbers run validateOffset. */
198
+ static bool scr_bytes_chk_off(const ScrDyn *d, const char *name, double max,
199
+ double dflt, double *out) {
200
+ if (d->kind == SCR_DYN_UNDEF) {
201
+ *out = dflt;
202
+ return true;
203
+ }
204
+ if (d->kind != SCR_DYN_NUM) {
205
+ scr_dyn_arg_type_fail(name, "of type number", d);
206
+ return false;
207
+ }
208
+ *out = d->v.num;
209
+ return scr_bytes_validate_off(name, *out, max);
210
+ }
211
+
212
+ double scr_bytes_compare_chk(const ScrBytes *src, const ScrDyn *target,
213
+ const ScrDyn *ts, const ScrDyn *te,
214
+ const ScrDyn *ss, const ScrDyn *se) {
215
+ ScrBytes *t = scr_bytes_chk_u8(target, "target");
216
+ if (!t) return 0;
217
+ double tsv, tev, ssv, sev;
218
+ if (!scr_bytes_chk_off(ts, "targetStart", 9007199254740991.0, 0, &tsv)) return 0;
219
+ if (!scr_bytes_chk_off(te, "targetEnd", (double)t->len, (double)t->len, &tev)) return 0;
220
+ if (!scr_bytes_chk_off(ss, "sourceStart", 9007199254740991.0, 0, &ssv)) return 0;
221
+ if (!scr_bytes_chk_off(se, "sourceEnd", (double)src->len, (double)src->len, &sev)) return 0;
222
+ /* Every slot validated or defaulted above; nargs 4 revalidates the
223
+ * now-known-good numbers (a no-op) and keeps one comparison core. */
224
+ return scr_bytes_compare(src, t, 4, tsv, tev, ssv, sev);
225
+ }
226
+
227
+ /* new Buffer(number, encoding) — the deprecated ctor's string arm with a
228
+ * non-string first argument: Node's exact ERR_INVALID_ARG_TYPE (the
229
+ * throwing path never fires DEP0005, so compiled silence matches). */
230
+ ScrBytes *scr_buffer_new_string_fail(const ScrDyn *got) {
231
+ scr_dyn_arg_type_fail("string", "of type string", got);
232
+ return NULL;
233
+ }
234
+
235
+ /* fs._toUnixTimestamp — the seconds coercion the utimes family runs on
236
+ * its time arguments (fs.js's toUnixTimestamp, underscore-exported):
237
+ * numeric STRINGS pass ToNumber's loose-equality gate (+time == time —
238
+ * whitespace-only strings answer 0), finite numbers pass (negatives
239
+ * answer now/1000, Node's "past times" shape), everything else throws
240
+ * Node's exact ERR_INVALID_ARG_TYPE. Borrowed; the throw is pending on
241
+ * the dummy 0 return. */
242
+ double scr_fs_to_unix_timestamp(const ScrDyn *t) {
243
+ if (t->kind == SCR_DYN_STR) {
244
+ double n = scr_string_to_number(t->v.str);
245
+ /* +time == time: NaN fails; every parsed number loosely equals its
246
+ * own source string by construction of ToNumber. */
247
+ if (n == n) return n;
248
+ }
249
+ if (t->kind == SCR_DYN_NUM && isfinite(t->v.num)) {
250
+ if (t->v.num < 0) {
251
+ struct timespec ts;
252
+ clock_gettime(CLOCK_REALTIME, &ts);
253
+ return ((double)ts.tv_sec * 1000.0 + (double)ts.tv_nsec / 1e6) / 1000.0;
254
+ }
255
+ return t->v.num;
256
+ }
257
+ scr_dyn_arg_type_fail("time", "an instance of Date or an Time in seconds", t);
258
+ return 0;
259
+ }
260
+
261
+ /* ── the fs argument-validation ladders (checked-dynamic lane) ─────────
262
+ * Each fs.*Chk libCall replicates its API's Node-order validation over
263
+ * DOM values and throws Node's exact typed errors; when every validation
264
+ * passes, the honest tail runs — the real operation where one exists
265
+ * (mkdtempSync, lchmodSync on macOS), the compiler-rendered SC2020 fence
266
+ * otherwise (scr_throw_lowering_fence). All arguments borrowed. */
267
+
268
+ static bool scr_fs_dyn_absent(const ScrDyn *v) {
269
+ return v->kind == SCR_DYN_UNDEF || v->kind == SCR_DYN_NULL;
270
+ }
271
+
272
+ static bool scr_fs_str_is(const ScrStr *s, const char *lit) {
273
+ size_t n = strlen(lit);
274
+ return s->len == n && memcmp(s->data, lit, n) == 0;
275
+ }
276
+
277
+ /* Node's maybeCallback/validateFunction over a callback slot. */
278
+ static bool scr_fs_cb_chk(const ScrDyn *cb, const char *name) {
279
+ if (cb->kind == SCR_DYN_FUNC) return true;
280
+ scr_dyn_arg_type_fail(name, "of type function", cb);
281
+ return false;
282
+ }
283
+
284
+ /* getValidatedPath: strings and Buffers pass (URL instances never reach
285
+ * these ladders — the DOM has no URL kind here, and Node would accept
286
+ * only file: URLs anyway). */
287
+ static bool scr_fs_path_chk(const ScrDyn *p, const char *name) {
288
+ if (p->kind == SCR_DYN_STR || p->kind == SCR_DYN_BYTES) return true;
289
+ scr_dyn_arg_type_fail(name, "of type string or an instance of Buffer or URL", p);
290
+ return false;
291
+ }
292
+
293
+ /* assertEncoding over an options slot (a bare encoding string or an
294
+ * options record's `encoding` member): Node throws ERR_INVALID_ARG_VALUE
295
+ * for any truthy value Buffer.isEncoding rejects. */
296
+ static bool scr_fs_encoding_chk(const ScrDyn *opts) {
297
+ const ScrDyn *enc = opts;
298
+ if (opts->kind == SCR_DYN_OBJ) {
299
+ enc = scr_dyn_obj_get(opts, "encoding", 8);
300
+ if (enc == NULL) return true;
301
+ }
302
+ if (scr_fs_dyn_absent(enc)) return true;
303
+ if (enc->kind == SCR_DYN_STR) {
304
+ if (enc->v.str->len == 0) return true; /* falsy: assertEncoding's `encoding &&` gate */
305
+ if (scr_bytes_is_encoding(enc->v.str)) return true;
306
+ }
307
+ if (enc->kind == SCR_DYN_BOOL && !enc->v.b) return true;
308
+ if (enc->kind == SCR_DYN_NUM && enc->v.num == 0) return true;
309
+ scr_dyn_arg_value_fail("encoding", "is invalid encoding", enc);
310
+ return false;
311
+ }
312
+
313
+ /* parseFileMode: integers 0..2^32-1 pass, octal strings parse, and the
314
+ * rest throw Node's exact ladder (validateUint32's wording). */
315
+ static bool scr_fs_mode_chk(const ScrDyn *m, const char *name) {
316
+ if (m->kind == SCR_DYN_STR) {
317
+ const ScrStr *s = m->v.str;
318
+ bool octal = s->len > 0;
319
+ for (size_t i = 0; octal && i < s->len; i++) {
320
+ if (s->data[i] < '0' || s->data[i] > '7') octal = false;
321
+ }
322
+ if (octal) return true;
323
+ scr_dyn_arg_value_fail(name, "must be a 32-bit unsigned integer or an octal string", m);
324
+ return false;
325
+ }
326
+ if (m->kind != SCR_DYN_NUM) {
327
+ scr_dyn_arg_type_fail(name, "of type number", m);
328
+ return false;
329
+ }
330
+ double v = m->v.num;
331
+ if (!(isfinite(v) && trunc(v) == v)) {
332
+ char recv[48], msg[160];
333
+ scr_num_received(v, recv);
334
+ int len = snprintf(msg, sizeof msg,
335
+ "The value of \"%s\" is out of range. It must be an integer. Received %s",
336
+ name, recv);
337
+ scr_throw_error_msg_code(SCR_ERR_RANGE, msg, (size_t)len, "ERR_OUT_OF_RANGE");
338
+ return false;
339
+ }
340
+ if (v < 0 || v > 4294967295.0) {
341
+ char recv[48], msg[160];
342
+ scr_num_received(v, recv);
343
+ int len = snprintf(msg, sizeof msg,
344
+ "The value of \"%s\" is out of range. It must be >= 0 && <= 4294967295. Received %s",
345
+ name, recv);
346
+ scr_throw_error_msg_code(SCR_ERR_RANGE, msg, (size_t)len, "ERR_OUT_OF_RANGE");
347
+ return false;
348
+ }
349
+ return true;
350
+ }
351
+
352
+ /* fs.exists(path, cb) — the REAL deprecated-API shape: the callback
353
+ * validates synchronously (Node's one throwing arm), and the answer
354
+ * arrives asynchronously through it — string/Buffer paths run the
355
+ * existsSync probe at fire time, every other path kind answers `false`
356
+ * (Node swallows getValidatedPath failures there). The loop-held timer
357
+ * matches Node's I/O-completion timing closely enough for the
358
+ * sequential CLI corpus. */
359
+ #ifndef SCR_LIB
360
+ static void scr_fs_exists_fire(ScrClosure *self) {
361
+ ScrDyn *path = scr_box_get_ref(self->caps[0]); /* +1 */
362
+ ScrDyn *cb = scr_box_get_ref(self->caps[1]); /* +1 */
363
+ bool ans = false;
364
+ if (path->kind == SCR_DYN_STR) {
365
+ ScrStr *p = scr_str_retain(path->v.str);
366
+ ans = scr_fs_exists(p);
367
+ scr_str_release(p);
368
+ } else if (path->kind == SCR_DYN_BYTES) {
369
+ ScrStr *p = scr_str_new((const char *)path->v.bytes->data, path->v.bytes->len);
370
+ ans = scr_fs_exists(p);
371
+ scr_str_release(p);
372
+ }
373
+ ScrDyn *arg = scr_dyn_new_bool(ans);
374
+ ScrDyn *r = scr_dyn_call(cb, &arg, 1, "the fs.exists callback");
375
+ scr_dyn_release(arg);
376
+ scr_dyn_release(r);
377
+ scr_dyn_release(path);
378
+ scr_dyn_release(cb);
379
+ }
380
+ #endif
381
+
382
+ ScrDyn *scr_fs_exists_async(const ScrDyn *path, const ScrDyn *cb) {
383
+ if (!scr_fs_cb_chk(cb, "cb")) return NULL;
384
+ if (path->kind != SCR_DYN_STR && path->kind != SCR_DYN_BYTES) {
385
+ /* Node's wart, kept exactly: a path getValidatedPath rejects answers
386
+ * false through the callback SYNCHRONOUSLY (`return callback(false)`
387
+ * in lib/fs.js exists). */
388
+ ScrDyn *arg = scr_dyn_new_bool(false);
389
+ ScrDyn *r = scr_dyn_call(cb, &arg, 1, "the fs.exists callback");
390
+ scr_dyn_release(arg);
391
+ scr_dyn_release(r);
392
+ if (scr_exc_pending()) return NULL;
393
+ return scr_dyn_retain(scr_dyn_undefined());
394
+ }
395
+ #ifdef SCR_LIB
396
+ /* Library links exclude the event-loop unit, and the compile-time scan
397
+ * refuses the fs.exists surface (SC4005's family) — this arm exists
398
+ * only so the TU links without scr_async. Unreachable by construction. */
399
+ scr_trap("scriptc: internal error: fs.exists async fire reached in a library build — please report this");
400
+ return NULL;
401
+ #else
402
+ ScrClosure *clo = scr_closure_new((void *)scr_fs_exists_fire, 2);
403
+ clo->caps[0] = scr_box_new_obj(scr_dyn_retain_v, scr_dyn_release_v, NULL);
404
+ scr_box_set_ref(clo->caps[0], scr_dyn_retain((ScrDyn *)path));
405
+ clo->caps[1] = scr_box_new_obj(scr_dyn_retain_v, scr_dyn_release_v, NULL);
406
+ scr_box_set_ref(clo->caps[1], scr_dyn_retain((ScrDyn *)cb));
407
+ scr_set_timeout(clo, 0);
408
+ return scr_dyn_retain(scr_dyn_undefined());
409
+ #endif
410
+ }
411
+
412
+ /* fs.mkdtemp(prefix, options?, cb): callback first (makeCallback), then
413
+ * the prefix (getValidatedPath's 'prefix' slot); the async op fences. */
414
+ void scr_fs_mkdtemp_chk(const ScrDyn *prefix, const ScrDyn *cb, const ScrStr *fence) {
415
+ if (!scr_fs_cb_chk(cb, "cb")) return;
416
+ if (!scr_fs_path_chk(prefix, "prefix")) return;
417
+ scr_throw_lowering_fence(fence);
418
+ }
419
+
420
+ /* fs.mkdtempSync(prefix, options?): prefix validates (Node's 'prefix'
421
+ * slot), the options walk accepts only shapes that leave utf8 semantics
422
+ * (absent/empty records, utf8 spellings — invalid encodings throw the
423
+ * assertEncoding ladder, other effectful options fence), then the REAL
424
+ * mkdtemp runs. +1 result or NULL with the exception pending. */
425
+ ScrStr *scr_fs_mkdtemp_sync_chk(const ScrDyn *prefix, const ScrDyn *opts, const ScrStr *fence) {
426
+ if (!scr_fs_path_chk(prefix, "prefix")) return NULL;
427
+ if (!scr_fs_encoding_chk(opts)) return NULL;
428
+ bool utf8 = true;
429
+ if (opts->kind == SCR_DYN_OBJ) {
430
+ for (size_t i = 0; i < opts->v.obj.len; i++) {
431
+ const ScrDynEntry *e = &opts->v.obj.entries[i];
432
+ if (scr_fs_dyn_absent(e->value)) continue;
433
+ if (strcmp(e->key, "encoding") == 0) {
434
+ const ScrDyn *enc = e->value;
435
+ if (!(enc->kind == SCR_DYN_STR &&
436
+ (scr_fs_str_is(enc->v.str, "utf8") || scr_fs_str_is(enc->v.str, "utf-8")))) {
437
+ utf8 = false;
438
+ }
439
+ continue;
440
+ }
441
+ utf8 = false; /* an unmodeled effectful option */
442
+ }
443
+ } else if (opts->kind == SCR_DYN_STR) {
444
+ utf8 = scr_fs_str_is(opts->v.str, "utf8") || scr_fs_str_is(opts->v.str, "utf-8");
445
+ } else if (!scr_fs_dyn_absent(opts)) {
446
+ utf8 = false;
447
+ }
448
+ if (!utf8 || prefix->kind != SCR_DYN_STR) {
449
+ scr_throw_lowering_fence(fence);
450
+ return NULL;
451
+ }
452
+ ScrStr *p = scr_str_retain(prefix->v.str);
453
+ ScrStr *r = scr_fs_mkdtemp(p);
454
+ scr_str_release(p);
455
+ return r;
456
+ }
457
+
458
+ /* fs.readFile(path, options?, cb): Node's order — the callback
459
+ * (maybeCallback), the options walk's assertEncoding, then the path;
460
+ * the async read itself fences. */
461
+ void scr_fs_read_file_chk(const ScrDyn *path, const ScrDyn *opts, const ScrDyn *cb,
462
+ const ScrStr *fence) {
463
+ if (!scr_fs_cb_chk(cb, "cb")) return;
464
+ if (!scr_fs_encoding_chk(opts)) return;
465
+ if (!scr_fs_path_chk(path, "path")) return;
466
+ scr_throw_lowering_fence(fence);
467
+ }
468
+
469
+ /* fs.opendirSync(path, options?): getValidatedPath, then getOptions'
470
+ * assertEncoding; the Dir machinery fences. */
471
+ void scr_fs_opendir_chk(const ScrDyn *path, const ScrDyn *opts, const ScrStr *fence) {
472
+ if (!scr_fs_path_chk(path, "path")) return;
473
+ if (!scr_fs_encoding_chk(opts)) return;
474
+ scr_throw_lowering_fence(fence);
475
+ }
476
+
477
+ /* fs.watchFile(path, options?, listener): the path first, the listener's
478
+ * function check second (Node's watchFile order); real watching fences. */
479
+ void scr_fs_watch_file_chk(const ScrDyn *path, const ScrDyn *listener, const ScrStr *fence) {
480
+ if (!scr_fs_path_chk(path, "path")) return;
481
+ if (listener->kind != SCR_DYN_FUNC) {
482
+ scr_dyn_arg_type_fail("listener", "of type function", listener);
483
+ return;
484
+ }
485
+ scr_throw_lowering_fence(fence);
486
+ }
487
+
488
+ /* fs.lchmod / lchmodSync / fs.promises.lchmod — macOS-only in Node (the
489
+ * callback/sync pair is not even exported elsewhere, so non-APPLE builds
490
+ * answer the not-a-function TypeError; the promise form rejects
491
+ * ERR_METHOD_NOT_IMPLEMENTED, Node's own linux shape). On macOS the
492
+ * validation ladder runs in Node's order and the real lchmod(2) applies
493
+ * where an operation survives it. */
494
+ static bool scr_fs_lchmod_defined(const char *api) {
495
+ #ifdef __APPLE__
496
+ (void)api;
497
+ return true;
498
+ #else
499
+ char msg[64];
500
+ int len = snprintf(msg, sizeof msg, "%s is not a function", api);
501
+ scr_throw_error_msg(SCR_ERR_TYPE, msg, (size_t)len);
502
+ return false;
503
+ #endif
504
+ }
505
+
506
+ void scr_fs_lchmod_chk(const ScrDyn *path, const ScrDyn *mode, const ScrDyn *cb,
507
+ const ScrStr *fence) {
508
+ if (!scr_fs_lchmod_defined("fs.lchmod")) return;
509
+ if (!scr_fs_cb_chk(cb, "cb")) return;
510
+ if (!scr_fs_path_chk(path, "path")) return;
511
+ if (!scr_fs_mode_chk(mode, "mode")) return;
512
+ scr_throw_lowering_fence(fence); /* the async op + callback dispatch */
513
+ }
514
+
515
+ #ifdef __APPLE__
516
+ static void scr_fs_lchmod_apply(const ScrDyn *path, const ScrDyn *mode) {
517
+ ScrStr *p = path->kind == SCR_DYN_STR ? scr_str_retain(path->v.str)
518
+ : scr_str_new((const char *)path->v.bytes->data, path->v.bytes->len);
519
+ double m = mode->kind == SCR_DYN_NUM ? mode->v.num : (double)strtol(mode->v.str->data, NULL, 8);
520
+ if (lchmod(p->data, (mode_t)m) != 0) scr_fs_throw(errno, "lchmod", p);
521
+ scr_str_release(p);
522
+ }
523
+ #endif
524
+
525
+ /* Answers the DOM undefined (+1) on success — lchmodSync's JS value, so
526
+ * return-position uses lower; NULL with the exception pending. */
527
+ ScrDyn *scr_fs_lchmod_sync_chk(const ScrDyn *path, const ScrDyn *mode) {
528
+ if (!scr_fs_lchmod_defined("fs.lchmodSync")) return NULL;
529
+ if (!scr_fs_path_chk(path, "path")) return NULL;
530
+ if (!scr_fs_mode_chk(mode, "mode")) return NULL;
531
+ #ifdef __APPLE__
532
+ scr_fs_lchmod_apply(path, mode);
533
+ if (scr_exc_pending()) return NULL;
534
+ #endif
535
+ return scr_dyn_retain(scr_dyn_undefined());
536
+ }
537
+
538
+ ScrPromise *scr_fsp_lchmod_chk(const ScrDyn *path, const ScrDyn *mode) {
539
+ #ifndef __APPLE__
540
+ (void)path;
541
+ (void)mode;
542
+ static const char ni[] = "The lchmod() method is not implemented";
543
+ scr_throw_error_msg_code(SCR_ERR_ERROR, ni, sizeof ni - 1, "ERR_METHOD_NOT_IMPLEMENTED");
544
+ return scr_promise_settled_void();
545
+ #else
546
+ if (scr_fs_path_chk(path, "path") && scr_fs_mode_chk(mode, "mode")) {
547
+ scr_fs_lchmod_apply(path, mode);
548
+ }
549
+ return scr_promise_settled_void();
550
+ #endif
551
+ }
552
+
553
+ /* fs.read(fd, buffer, offset, length, position, cb) — the full argument
554
+ * ladder in Node's order (buffer, fd, offset, length, position); the
555
+ * async read fences. Bounds follow lib/fs.js read(): offset within the
556
+ * buffer, length within buffer - offset. */
557
+ static bool scr_fs_int_range_chk(const ScrDyn *v, const char *name, double min, double max,
558
+ const char *range) {
559
+ if (v->kind != SCR_DYN_NUM) {
560
+ scr_dyn_arg_type_fail(name, "of type number", v);
561
+ return false;
562
+ }
563
+ double n = v->v.num;
564
+ char recv[48], msg[192];
565
+ if (!(isfinite(n) && trunc(n) == n)) {
566
+ scr_num_received(n, recv);
567
+ int len = snprintf(msg, sizeof msg,
568
+ "The value of \"%s\" is out of range. It must be an integer. Received %s",
569
+ name, recv);
570
+ scr_throw_error_msg_code(SCR_ERR_RANGE, msg, (size_t)len, "ERR_OUT_OF_RANGE");
571
+ return false;
572
+ }
573
+ if (n < min || n > max) {
574
+ scr_num_received(n, recv);
575
+ int len = snprintf(msg, sizeof msg,
576
+ "The value of \"%s\" is out of range. It must be %s. Received %s",
577
+ name, range, recv);
578
+ scr_throw_error_msg_code(SCR_ERR_RANGE, msg, (size_t)len, "ERR_OUT_OF_RANGE");
579
+ return false;
580
+ }
581
+ return true;
582
+ }
583
+
584
+ void scr_fs_read_chk(const ScrDyn *fd, const ScrDyn *buffer, const ScrDyn *offset,
585
+ const ScrDyn *length, const ScrDyn *position, const ScrStr *fence) {
586
+ if (buffer->kind != SCR_DYN_BYTES) {
587
+ scr_dyn_arg_type_fail("buffer", "an instance of Buffer, TypedArray, or DataView", buffer);
588
+ return;
589
+ }
590
+ if (fd->kind != SCR_DYN_NUM) {
591
+ scr_dyn_arg_type_fail("fd", "of type number", fd);
592
+ return;
593
+ }
594
+ double buflen = (double)buffer->v.bytes->len;
595
+ if (!scr_fs_dyn_absent(offset)) {
596
+ /* validateInteger's MAX_SAFE range first, the buffer bound second —
597
+ * Node renders each with its own max. */
598
+ if (!scr_fs_int_range_chk(offset, "offset", 0, 9007199254740991.0,
599
+ ">= 0 && <= 9007199254740991")) {
600
+ return;
601
+ }
602
+ if (offset->v.num > buflen) {
603
+ char range[64];
604
+ snprintf(range, sizeof range, ">= 0 && <= %.0f", buflen);
605
+ if (!scr_fs_int_range_chk(offset, "offset", 0, buflen, range)) return;
606
+ }
607
+ }
608
+ double off = offset->kind == SCR_DYN_NUM ? offset->v.num : 0;
609
+ if (!scr_fs_dyn_absent(length)) {
610
+ if (length->kind != SCR_DYN_NUM || !(isfinite(length->v.num) && trunc(length->v.num) == length->v.num) ||
611
+ length->v.num < 0) {
612
+ /* Node renders the bare ">= 0" form here (checkPosition's cousin). */
613
+ if (length->kind == SCR_DYN_NUM) {
614
+ char recv[48], msg[160];
615
+ scr_num_received(length->v.num, recv);
616
+ const char *shape = (isfinite(length->v.num) && trunc(length->v.num) == length->v.num)
617
+ ? "It must be >= 0."
618
+ : "It must be an integer.";
619
+ int len = snprintf(msg, sizeof msg,
620
+ "The value of \"length\" is out of range. %s Received %s", shape, recv);
621
+ scr_throw_error_msg_code(SCR_ERR_RANGE, msg, (size_t)len, "ERR_OUT_OF_RANGE");
622
+ return;
623
+ }
624
+ scr_dyn_arg_type_fail("length", "of type number", length);
625
+ return;
626
+ }
627
+ if (length->v.num > buflen - off) {
628
+ char recv[48], msg[160];
629
+ scr_num_received(length->v.num, recv);
630
+ int len = snprintf(msg, sizeof msg,
631
+ "The value of \"length\" is out of range. It must be <= %.0f. Received %s",
632
+ buflen - off, recv);
633
+ scr_throw_error_msg_code(SCR_ERR_RANGE, msg, (size_t)len, "ERR_OUT_OF_RANGE");
634
+ return;
635
+ }
636
+ }
637
+ if (!scr_fs_dyn_absent(position)) {
638
+ if (position->kind != SCR_DYN_NUM) {
639
+ scr_dyn_arg_type_fail("position", "of type bigint or integer", position);
640
+ return;
641
+ }
642
+ if (!scr_fs_int_range_chk(position, "position", -1, 9007199254740991.0,
643
+ ">= -1 && <= 9007199254740991")) {
644
+ return;
645
+ }
646
+ }
647
+ scr_throw_lowering_fence(fence);
648
+ }
649
+
650
+ /* createReadStream/createWriteStream(path, options?): getOptions'
651
+ * assertEncoding, the fd member's FileHandle-or-integer contract when
652
+ * present, the path contract otherwise; the stream machinery fences. */
653
+ void scr_fs_stream_opts_chk(const ScrDyn *path, const ScrDyn *opts, const ScrStr *fence) {
654
+ if (!scr_fs_encoding_chk(opts)) return;
655
+ const ScrDyn *fd = opts->kind == SCR_DYN_OBJ ? scr_dyn_obj_get(opts, "fd", 2) : NULL;
656
+ if (fd != NULL && !scr_fs_dyn_absent(fd)) {
657
+ if (fd->kind != SCR_DYN_NUM) {
658
+ scr_dyn_prop_type_fail("options.fd", "of type number or an instance of FileHandle", fd);
659
+ return;
660
+ }
661
+ if (!scr_fs_int_range_chk(fd, "fd", 0, 2147483647.0, ">= 0 && <= 2147483647")) return;
662
+ } else if (!scr_fs_path_chk(path, "path")) {
663
+ return;
664
+ }
665
+ scr_throw_lowering_fence(fence);
666
+ }
package/src/scr_dc.c CHANGED
@@ -132,6 +132,12 @@ static void dc_throw_bad_fn_arg(const ScrDyn *cb, const char *arg_name) {
132
132
  (int)(cb->v.str->len < 28 ? cb->v.str->len : 28), cb->v.str->data);
133
133
  received = detail;
134
134
  break;
135
+ case SCR_DYN_JSVAL:
136
+ /* An engine callback may well BE a function — subscribing island
137
+ * values is unarmed (lane dyn-routing-ops); fence loudly rather
138
+ * than claim "Received an instance of Object". */
139
+ scr_dyn_isl_fence(cb, "subscribing");
140
+ return;
135
141
  default: break;
136
142
  }
137
143
  char buf[160];