@scriptc/cli-darwin-x64 0.2.3 → 0.2.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 (141) hide show
  1. package/dist/THIRD_PARTY_NOTICES +1 -1
  2. package/dist/bin/scriptc +0 -0
  3. package/dist/bin/scriptc.json +1 -1
  4. package/dist/lib/declarations/scriptc-node-fallback.d.ts +915 -215
  5. package/dist/lib/declarations/scriptc-overrides.d.ts +5 -1
  6. package/dist/lib/declarations/scriptc.d.ts +22 -2
  7. package/dist/lib/llvm/bin/scriptc-llvm-codegen +0 -0
  8. package/dist/lib/llvm/package.json +1 -1
  9. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/default/scr_array.o +0 -0
  10. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/default/scr_bigint.o +0 -0
  11. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/default/scr_bytes.o +0 -0
  12. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/default/scr_bytes_io.o +0 -0
  13. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/default/scr_copying.o +0 -0
  14. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/default/scr_cycle.o +0 -0
  15. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/default/scr_json.o +0 -0
  16. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/default/scr_lib.o +0 -0
  17. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/default/scr_map.o +0 -0
  18. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/default/scr_number.o +0 -0
  19. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/default/scr_regex.o +0 -0
  20. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/default/scr_string.o +0 -0
  21. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/default/scr_util_compare.o +0 -0
  22. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/dynamic/scr_array.o +0 -0
  23. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/dynamic/scr_bigint.o +0 -0
  24. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/dynamic/scr_bytes.o +0 -0
  25. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/dynamic/scr_bytes_io.o +0 -0
  26. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/dynamic/scr_copying.o +0 -0
  27. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/dynamic/scr_cycle.o +0 -0
  28. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/dynamic/scr_json.o +0 -0
  29. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/dynamic/scr_lib.o +0 -0
  30. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/dynamic/scr_map.o +0 -0
  31. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/dynamic/scr_number.o +0 -0
  32. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/dynamic/scr_regex.o +0 -0
  33. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/dynamic/scr_string.o +0 -0
  34. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/dynamic/scr_util_compare.o +0 -0
  35. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/dynamic-text-decoder-legacy/scr_bytes.o +0 -0
  36. package/dist/lib/runtime-darwin-x64/artifacts/dev/runtime/text-decoder-legacy/scr_bytes.o +0 -0
  37. package/dist/lib/runtime-darwin-x64/artifacts/library-dev/runtime/default/scr_array.o +0 -0
  38. package/dist/lib/runtime-darwin-x64/artifacts/library-dev/runtime/default/scr_bigint.o +0 -0
  39. package/dist/lib/runtime-darwin-x64/artifacts/library-dev/runtime/default/scr_bytes.o +0 -0
  40. package/dist/lib/runtime-darwin-x64/artifacts/library-dev/runtime/default/scr_bytes_io.o +0 -0
  41. package/dist/lib/runtime-darwin-x64/artifacts/library-dev/runtime/default/scr_copying.o +0 -0
  42. package/dist/lib/runtime-darwin-x64/artifacts/library-dev/runtime/default/scr_cycle.o +0 -0
  43. package/dist/lib/runtime-darwin-x64/artifacts/library-dev/runtime/default/scr_json.o +0 -0
  44. package/dist/lib/runtime-darwin-x64/artifacts/library-dev/runtime/default/scr_lib.o +0 -0
  45. package/dist/lib/runtime-darwin-x64/artifacts/library-dev/runtime/default/scr_map.o +0 -0
  46. package/dist/lib/runtime-darwin-x64/artifacts/library-dev/runtime/default/scr_number.o +0 -0
  47. package/dist/lib/runtime-darwin-x64/artifacts/library-dev/runtime/default/scr_regex.o +0 -0
  48. package/dist/lib/runtime-darwin-x64/artifacts/library-dev/runtime/default/scr_string.o +0 -0
  49. package/dist/lib/runtime-darwin-x64/artifacts/library-dev/runtime/text-decoder-legacy/scr_bytes.o +0 -0
  50. package/dist/lib/runtime-darwin-x64/artifacts/library-release/runtime/default/scr_array.o +0 -0
  51. package/dist/lib/runtime-darwin-x64/artifacts/library-release/runtime/default/scr_bigint.o +0 -0
  52. package/dist/lib/runtime-darwin-x64/artifacts/library-release/runtime/default/scr_bytes.o +0 -0
  53. package/dist/lib/runtime-darwin-x64/artifacts/library-release/runtime/default/scr_bytes_io.o +0 -0
  54. package/dist/lib/runtime-darwin-x64/artifacts/library-release/runtime/default/scr_copying.o +0 -0
  55. package/dist/lib/runtime-darwin-x64/artifacts/library-release/runtime/default/scr_cycle.o +0 -0
  56. package/dist/lib/runtime-darwin-x64/artifacts/library-release/runtime/default/scr_json.o +0 -0
  57. package/dist/lib/runtime-darwin-x64/artifacts/library-release/runtime/default/scr_lib.o +0 -0
  58. package/dist/lib/runtime-darwin-x64/artifacts/library-release/runtime/default/scr_map.o +0 -0
  59. package/dist/lib/runtime-darwin-x64/artifacts/library-release/runtime/default/scr_number.o +0 -0
  60. package/dist/lib/runtime-darwin-x64/artifacts/library-release/runtime/default/scr_regex.o +0 -0
  61. package/dist/lib/runtime-darwin-x64/artifacts/library-release/runtime/default/scr_string.o +0 -0
  62. package/dist/lib/runtime-darwin-x64/artifacts/library-release/runtime/text-decoder-legacy/scr_bytes.o +0 -0
  63. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-dev/runtime/default/scr_array.o +0 -0
  64. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-dev/runtime/default/scr_bigint.o +0 -0
  65. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-dev/runtime/default/scr_bytes.o +0 -0
  66. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-dev/runtime/default/scr_bytes_io.o +0 -0
  67. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-dev/runtime/default/scr_copying.o +0 -0
  68. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-dev/runtime/default/scr_cycle.o +0 -0
  69. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-dev/runtime/default/scr_json.o +0 -0
  70. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-dev/runtime/default/scr_lib.o +0 -0
  71. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-dev/runtime/default/scr_map.o +0 -0
  72. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-dev/runtime/default/scr_number.o +0 -0
  73. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-dev/runtime/default/scr_regex.o +0 -0
  74. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-dev/runtime/default/scr_string.o +0 -0
  75. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-dev/runtime/text-decoder-legacy/scr_bytes.o +0 -0
  76. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-release/runtime/default/scr_array.o +0 -0
  77. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-release/runtime/default/scr_bigint.o +0 -0
  78. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-release/runtime/default/scr_bytes.o +0 -0
  79. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-release/runtime/default/scr_bytes_io.o +0 -0
  80. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-release/runtime/default/scr_copying.o +0 -0
  81. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-release/runtime/default/scr_cycle.o +0 -0
  82. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-release/runtime/default/scr_json.o +0 -0
  83. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-release/runtime/default/scr_lib.o +0 -0
  84. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-release/runtime/default/scr_map.o +0 -0
  85. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-release/runtime/default/scr_number.o +0 -0
  86. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-release/runtime/default/scr_regex.o +0 -0
  87. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-release/runtime/default/scr_string.o +0 -0
  88. package/dist/lib/runtime-darwin-x64/artifacts/library-thread-release/runtime/text-decoder-legacy/scr_bytes.o +0 -0
  89. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/default/scr_array.o +0 -0
  90. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/default/scr_bigint.o +0 -0
  91. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/default/scr_bytes.o +0 -0
  92. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/default/scr_bytes_io.o +0 -0
  93. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/default/scr_copying.o +0 -0
  94. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/default/scr_cycle.o +0 -0
  95. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/default/scr_json.o +0 -0
  96. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/default/scr_lib.o +0 -0
  97. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/default/scr_map.o +0 -0
  98. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/default/scr_number.o +0 -0
  99. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/default/scr_regex.o +0 -0
  100. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/default/scr_string.o +0 -0
  101. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/default/scr_util_compare.o +0 -0
  102. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/dynamic/scr_array.o +0 -0
  103. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/dynamic/scr_bigint.o +0 -0
  104. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/dynamic/scr_bytes.o +0 -0
  105. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/dynamic/scr_bytes_io.o +0 -0
  106. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/dynamic/scr_copying.o +0 -0
  107. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/dynamic/scr_cycle.o +0 -0
  108. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/dynamic/scr_json.o +0 -0
  109. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/dynamic/scr_lib.o +0 -0
  110. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/dynamic/scr_map.o +0 -0
  111. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/dynamic/scr_number.o +0 -0
  112. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/dynamic/scr_regex.o +0 -0
  113. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/dynamic/scr_string.o +0 -0
  114. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/dynamic/scr_util_compare.o +0 -0
  115. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/dynamic-text-decoder-legacy/scr_bytes.o +0 -0
  116. package/dist/lib/runtime-darwin-x64/artifacts/release/runtime/text-decoder-legacy/scr_bytes.o +0 -0
  117. package/dist/lib/runtime-darwin-x64/package.json +1 -1
  118. package/dist/lib/runtime-darwin-x64/runtime-pack.json +215 -215
  119. package/dist/lib/runtime-sources/package.json +1 -1
  120. package/dist/lib/runtime-sources/src/scr_array.c +317 -40
  121. package/dist/lib/runtime-sources/src/scr_async.test.ts +16 -6
  122. package/dist/lib/runtime-sources/src/scr_bigint.c +15 -0
  123. package/dist/lib/runtime-sources/src/scr_bytes.c +437 -216
  124. package/dist/lib/runtime-sources/src/scr_bytes.test.c +85 -0
  125. package/dist/lib/runtime-sources/src/scr_bytes_io.c +0 -34
  126. package/dist/lib/runtime-sources/src/scr_copying.c +12 -2
  127. package/dist/lib/runtime-sources/src/scr_cycle.c +84 -60
  128. package/dist/lib/runtime-sources/src/scr_json.c +494 -161
  129. package/dist/lib/runtime-sources/src/scr_json.test.c +323 -1
  130. package/dist/lib/runtime-sources/src/scr_json.test.ts +58 -25
  131. package/dist/lib/runtime-sources/src/scr_key.h +68 -0
  132. package/dist/lib/runtime-sources/src/scr_lib.c +177 -89
  133. package/dist/lib/runtime-sources/src/scr_map.c +268 -96
  134. package/dist/lib/runtime-sources/src/scr_number.c +2 -4
  135. package/dist/lib/runtime-sources/src/scr_numeric.h +25 -0
  136. package/dist/lib/runtime-sources/src/scr_numeric.test.c +37 -0
  137. package/dist/lib/runtime-sources/src/scr_numeric.test.ts +33 -0
  138. package/dist/lib/runtime-sources/src/scr_regex.c +255 -145
  139. package/dist/lib/runtime-sources/src/scr_runtime.h +76 -19
  140. package/dist/lib/runtime-sources/src/scr_string.c +184 -42
  141. package/package.json +1 -1
@@ -0,0 +1,85 @@
1
+ #include "scr_runtime.h"
2
+ #include <assert.h>
3
+ #include <math.h>
4
+ #include <string.h>
5
+
6
+ long scr_bytes_live_count(void);
7
+
8
+ /* Foreign wrappers may alias without a shared owner and may be unaligned. */
9
+ static ScrBytes *external(uint8_t *data, ScrBytesElem elem, size_t count) {
10
+ ScrBytes *view = scr_bytes_from_external(data, count * scr_bytes_elem_size(elem));
11
+ view->elem = elem;
12
+ view->len = count;
13
+ return view;
14
+ }
15
+
16
+ static void external_set(void) {
17
+ const size_t starts[] = {1, 9, 41, 3001};
18
+ uint8_t storage[6000], expected[6000];
19
+ for (ScrBytesElem from = SCR_BYTES_U8; from <= SCR_BYTES_U8C; from++) {
20
+ for (ScrBytesElem to = SCR_BYTES_U8; to <= SCR_BYTES_U8C; to++) {
21
+ for (size_t j = 0; j < sizeof starts / sizeof starts[0]; j++) {
22
+ memset(storage, 0x5a, sizeof storage);
23
+ ScrBytes *src = external(storage + 9, from, 259);
24
+ ScrBytes *dst = external(storage + starts[j], to, 263);
25
+ for (size_t i = 0; i < src->len; i++) scr_bytes_set(src, i, (double)i * 3.5 - 257);
26
+ memcpy(expected, storage, sizeof storage);
27
+ ScrBytes *want = external(expected + starts[j], to, 263);
28
+ for (size_t i = 0; i < src->len; i++) scr_bytes_set(want, i + 2, scr_bytes_get(src, i));
29
+ scr_bytes_set_from(dst, src, 2);
30
+ assert(!scr_exc_pending());
31
+ assert(memcmp(storage, expected, sizeof storage) == 0);
32
+ scr_bytes_release(want);
33
+ scr_bytes_release(dst);
34
+ scr_bytes_release(src);
35
+ }
36
+ }
37
+ }
38
+ }
39
+
40
+ static void copied_storage(void) {
41
+ /* Same-kind copies preserve NaN payloads, signed zero and ownership. */
42
+ const uint64_t bits[] = {UINT64_C(0x7ff8000000000042), UINT64_C(0x8000000000000000),
43
+ UINT64_C(0xfff0000000000000)};
44
+ ScrBytes *src = scr_bytes_new(SCR_BYTES_F64, 3);
45
+ memcpy(src->data, bits, sizeof bits);
46
+ ScrBytes *copy = scr_bytes_convert(SCR_BYTES_F64, src);
47
+ ScrBytes *slice = scr_bytes_slice(src, 0, 3);
48
+ assert(copy->data != src->data && slice->data != src->data);
49
+ assert(memcmp(copy->data, bits, sizeof bits) == 0);
50
+ assert(memcmp(slice->data, bits, sizeof bits) == 0);
51
+ ScrBytes *view = scr_bytes_subarray(copy, 1, 3);
52
+ scr_bytes_release(copy);
53
+ scr_bytes_release(src);
54
+ assert(memcmp(view->data, bits + 1, sizeof bits - sizeof bits[0]) == 0);
55
+ scr_bytes_release(view);
56
+ scr_bytes_release(slice);
57
+ }
58
+
59
+ static void filled_storage(void) {
60
+ uint8_t storage[2200], expected[2200];
61
+ const double values[] = {-0.0, -1.5, 2.5, 255.5, 4294967297.0, NAN, INFINITY};
62
+ for (ScrBytesElem elem = SCR_BYTES_U8; elem <= SCR_BYTES_U8C; elem++) {
63
+ for (size_t j = 0; j < sizeof values / sizeof values[0]; j++) {
64
+ memset(storage, 0xa5, sizeof storage);
65
+ memcpy(expected, storage, sizeof storage);
66
+ ScrBytes *dst = external(storage + 1, elem, 263);
67
+ ScrBytes *want = external(expected + 1, elem, 263);
68
+ for (size_t i = 2; i < 261; i++) scr_bytes_set(want, i, values[j]);
69
+ ScrBytes *result = scr_bytes_fill_elem(dst, values[j], 2, 261);
70
+ assert(result == dst && dst->rc == 2);
71
+ assert(memcmp(storage, expected, sizeof storage) == 0);
72
+ scr_bytes_release(result);
73
+ scr_bytes_release(dst);
74
+ scr_bytes_release(want);
75
+ }
76
+ }
77
+ }
78
+
79
+ void scr_bytes_test_bulk_storage(void) {
80
+ long before = scr_bytes_live_count();
81
+ external_set();
82
+ copied_storage();
83
+ filled_storage();
84
+ assert(scr_bytes_live_count() == before);
85
+ }
@@ -37,40 +37,6 @@ static void scr_bytes_io_oom(void) {
37
37
 
38
38
  /* ── fs (the Buffer forms of scr_lib.c's utf8 pair) ────────────────────── */
39
39
 
40
- ScrBytes *scr_fs_read_file_bytes(ScrStr *path) {
41
- FILE *f = fopen(path->data, "rb");
42
- if (!f) {
43
- scr_fs_throw(errno, "open", path);
44
- return NULL;
45
- }
46
- size_t cap = 4096, len = 0;
47
- uint8_t *buf = malloc(cap);
48
- if (!buf) scr_bytes_io_oom();
49
- for (;;) {
50
- if (cap - len < 2048) {
51
- cap *= 2;
52
- uint8_t *grown = realloc(buf, cap);
53
- if (!grown) scr_bytes_io_oom();
54
- buf = grown;
55
- }
56
- size_t n = fread(buf + len, 1, cap - len, f);
57
- len += n;
58
- if (n == 0) break;
59
- }
60
- if (ferror(f)) {
61
- int e = errno;
62
- fclose(f);
63
- free(buf);
64
- scr_fs_throw(e, "read", path);
65
- return NULL;
66
- }
67
- fclose(f);
68
- ScrBytes *b = scr_bytes_new(SCR_BYTES_U8, (double)len);
69
- memcpy(b->data, buf, len);
70
- free(buf);
71
- return b;
72
- }
73
-
74
40
  /* readFileSync's runtime-encoding form (a JS helper's untyped `enc`
75
41
  * parameter — test/common fixtures.js): undefined/null answer a Buffer,
76
42
  * utf8 answers a string, Node's other real encodings meet the loud
@@ -125,9 +125,19 @@ ScrArr *scr_arr_with_undefined(ScrArr *a, double index) {
125
125
 
126
126
  ScrBytes *scr_bytes_to_reversed(const ScrBytes *b) {
127
127
  ScrBytes *out = scr_bytes_new(b->elem, (double)b->len);
128
+ size_t width = scr_bytes_elem_size(b->elem);
129
+ /* Copy the stored representation without numeric conversion. Views may
130
+ * be unaligned, so fixed-width memcpy preserves both alignment safety
131
+ * and floating-point payload bits. The width is invariant for the loop. */
128
132
  for (size_t i = 0; i < b->len; i++) {
129
- scr_bytes_set(out, (double)i,
130
- scr_bytes_get(b, (double)(b->len - i - 1)));
133
+ const uint8_t *source = b->data + (b->len - i - 1) * width;
134
+ uint8_t *target = out->data + i * width;
135
+ switch (width) {
136
+ case 1: *target = *source; break;
137
+ case 2: memcpy(target, source, 2); break;
138
+ case 4: memcpy(target, source, 4); break;
139
+ case 8: memcpy(target, source, 8); break;
140
+ }
131
141
  }
132
142
  return out;
133
143
  }
@@ -9,7 +9,7 @@
9
9
  * markGray trial-delete: decrement rc once per internal edge;
10
10
  * scan nodes still rc > 0 are externally referenced — re-blacken
11
11
  * their subgraph and restore the trial decrements;
12
- * collectWhite everything left white is a dead cycle: free it, releasing
12
+ * gather filter the scanned white set, then free it, releasing
13
13
  * only edges that LEAVE the white set (each member's
14
14
  * teardown releases its untraced children; traced edges were
15
15
  * already accounted by markGray).
@@ -26,9 +26,10 @@
26
26
  * count was already given up (which would over-decrement and free live
27
27
  * data).
28
28
  *
29
- * Recursion depth equals the traced structure's depth — same property as
30
- * the existing recursive releases (deep lists recurse deeply; acceptable
31
- * for now, revisit with an explicit stack if it ever traps).
29
+ * The main graph phases keep a bounded depth-first walk and defer deeper
30
+ * traces to reusable worklists. Ordinary RC destruction and cross-generation
31
+ * releases are separate paths. White candidates are recorded during scan
32
+ * and filtered after outside roots settle, avoiding a separate gather walk.
32
33
  *
33
34
  * ── generations ──────────────────────────────────────────────────────
34
35
  * A pass costs O(objects reachable from its candidates), and a candidate
@@ -154,6 +155,12 @@ static SCR_TL ScrVec scr_cands;
154
155
  /* Nursery survivors awaiting promotion (see scr_scan_black). */
155
156
  static SCR_TL ScrVec scr_promote;
156
157
 
158
+ /* Deferred traces cap recursive traversal without changing the common
159
+ * depth-first walk. Restore can run inside scan, so it has its own stack. */
160
+ static SCR_TL ScrVec scr_pending;
161
+ static SCR_TL ScrVec scr_restore_pending;
162
+ #define SCR_CYC_WALK_DEPTH 64
163
+
157
164
  /* The gathered white set (freed after the walk completes). */
158
165
  static SCR_TL ScrVec scr_white;
159
166
 
@@ -281,14 +288,15 @@ void scr_cyc_on_dead(void *obj) {
281
288
  void scr_cyc_on_release(void *obj) {
282
289
  ScrCycHdr *h = scr_cyc_hdr(obj);
283
290
  h->color = SCR_CYC_PURPLE;
284
- if (!h->buffered) {
285
- ScrVec *b = &scr_roots[h->gen];
286
- if (b->n == b->cap) scr_cyc_grow(b);
287
- h->buffered = 1;
288
- h->buf_index = b->n;
289
- b->v[b->n++] = obj;
290
- scr_cyc_nbuffered++;
291
- }
291
+ /* Releasing an existing candidate cannot advance the trigger. A pass
292
+ * always re-arms after its teardowns, including any candidates they add. */
293
+ if (h->buffered) return;
294
+ ScrVec *b = &scr_roots[h->gen];
295
+ if (b->n == b->cap) scr_cyc_grow(b);
296
+ h->buffered = 1;
297
+ h->buf_index = b->n;
298
+ b->v[b->n++] = obj;
299
+ scr_cyc_nbuffered++;
292
300
  /* Never re-entered: a teardown's releases of untraced children can buffer
293
301
  * new candidates mid-collection, but they only wait for the next pass. */
294
302
  if (!scr_collecting && scr_cyc_nbuffered >= scr_cyc_trigger)
@@ -306,69 +314,68 @@ void scr_cyc_on_release(void *obj) {
306
314
  ((child) == NULL || SCR_RC(child) == SIZE_MAX \
307
315
  || scr_cyc_hdr(child)->gen > scr_gen_limit)
308
316
 
309
- static void scr_mark_gray(void *obj);
317
+ /* Trace callbacks forward an integer depth through their opaque context;
318
+ * it is never dereferenced. The ABI already carries it through every edge. */
319
+ static void scr_mark_gray(void *obj, uintptr_t depth);
310
320
  static void scr_mg_visit(void *child, void *ctx) {
311
- (void)ctx;
312
321
  if (SCR_CYC_SKIP(child)) return;
313
- SCR_RC(child) -= 1; /* trial-delete this internal edge */
314
- scr_mark_gray(child);
322
+ SCR_RC(child) -= 1;
323
+ scr_mark_gray(child, (uintptr_t)ctx);
315
324
  }
316
- static void scr_mark_gray(void *obj) {
325
+ static void scr_mark_gray(void *obj, uintptr_t depth) {
317
326
  ScrCycHdr *h = scr_cyc_hdr(obj);
318
327
  if (h->color == SCR_CYC_GRAY) return;
319
328
  h->color = SCR_CYC_GRAY;
320
- h->trace(obj, scr_mg_visit, NULL);
329
+ if (depth == SCR_CYC_WALK_DEPTH) {
330
+ scr_cyc_push(&scr_pending, obj);
331
+ return;
332
+ }
333
+ h->trace(obj, scr_mg_visit, (void *)(depth + 1));
321
334
  }
322
335
 
323
- static void scr_scan_black(void *obj);
336
+ static void scr_scan_black(void *obj, uintptr_t depth);
324
337
  static void scr_sb_visit(void *child, void *ctx) {
325
- (void)ctx;
326
338
  if (SCR_CYC_SKIP(child)) return;
327
- SCR_RC(child) += 1; /* restore the trial decrement */
328
- if (scr_cyc_hdr(child)->color != SCR_CYC_BLACK) scr_scan_black(child);
339
+ SCR_RC(child) += 1;
340
+ if (scr_cyc_hdr(child)->color != SCR_CYC_BLACK)
341
+ scr_scan_black(child, (uintptr_t)ctx);
329
342
  }
330
- static void scr_scan_black(void *obj) {
343
+ static void scr_scan_black(void *obj, uintptr_t depth) {
331
344
  ScrCycHdr *h = scr_cyc_hdr(obj);
332
345
  h->color = SCR_CYC_BLACK;
333
- /* Blackening is what proves a walked object survives, so this is where
334
- * promotion is decided — but it is only RECORDED here. Raising `gen` now
335
- * would hide the object from SCR_CYC_SKIP partway through the pass, and
336
- * this phase has trial decrements left to restore. */
337
- if (h->gen < SCR_CYC_MATURE)
338
- scr_cyc_push(&scr_promote, obj);
339
- h->trace(obj, scr_sb_visit, NULL);
346
+ if (h->gen < SCR_CYC_MATURE) scr_cyc_push(&scr_promote, obj);
347
+ if (depth == SCR_CYC_WALK_DEPTH) {
348
+ scr_cyc_push(&scr_restore_pending, obj);
349
+ return;
350
+ }
351
+ h->trace(obj, scr_sb_visit, (void *)(depth + 1));
340
352
  }
341
353
 
342
- static void scr_scan(void *obj);
354
+ static void scr_scan(void *obj, uintptr_t depth);
343
355
  static void scr_scan_visit(void *child, void *ctx) {
344
- (void)ctx;
345
356
  if (SCR_CYC_SKIP(child)) return;
346
- scr_scan(child);
357
+ scr_scan(child, (uintptr_t)ctx);
347
358
  }
348
- static void scr_scan(void *obj) {
359
+ static void scr_scan(void *obj, uintptr_t depth) {
349
360
  ScrCycHdr *h = scr_cyc_hdr(obj);
350
361
  if (h->color != SCR_CYC_GRAY) return;
351
362
  if (SCR_RC(obj) > 0) {
352
- /* Externally referenced: this whole subgraph stays. */
353
- scr_scan_black(obj);
363
+ scr_scan_black(obj, 0);
364
+ while (scr_restore_pending.n) {
365
+ void *next = scr_restore_pending.v[--scr_restore_pending.n];
366
+ scr_cyc_hdr(next)->trace(next, scr_sb_visit, NULL);
367
+ }
354
368
  return;
355
369
  }
356
370
  h->color = SCR_CYC_WHITE;
357
- h->trace(obj, scr_scan_visit, NULL);
358
- }
359
-
360
- static void scr_collect_white(void *obj);
361
- static void scr_cw_visit(void *child, void *ctx) {
362
- (void)ctx;
363
- if (SCR_CYC_SKIP(child)) return;
364
- scr_collect_white(child);
365
- }
366
- static void scr_collect_white(void *obj) {
367
- ScrCycHdr *h = scr_cyc_hdr(obj);
368
- if (h->color != SCR_CYC_WHITE || h->buffered) return;
369
- h->color = SCR_CYC_DOOMED; /* gathered — don't gather twice */
370
- h->trace(obj, scr_cw_visit, NULL);
371
+ /* Record each whitened object once. A later outside root can restore it;
372
+ * filter the list only after every scan finishes. No gather trace needed. */
371
373
  scr_cyc_push(&scr_white, obj);
374
+ if (depth == SCR_CYC_WALK_DEPTH) {
375
+ scr_cyc_push(&scr_pending, obj);
376
+ return;
377
+ }
378
+ h->trace(obj, scr_scan_visit, (void *)(depth + 1));
372
379
  }
373
380
 
374
381
  /* ── cross-generation edges ───────────────────────────────────────────── */
@@ -443,12 +450,10 @@ static size_t scr_cyc_pass(unsigned gen_limit) {
443
450
  scr_gen_limit = gen_limit;
444
451
 
445
452
  /* markRoots: keep live candidates (still purple), drop the rest — an
446
- * object re-retained since buffering is black; one grayed by an earlier
447
- * candidate's walk is already covered by that candidate's subgraph. The
448
- * buffers are drained up front (markGray only touches counts directly,
449
- * so nothing can re-buffer underneath us) which leaves collectWhite free
450
- * to gather members of an earlier root's cycle. Candidates ABOVE the
451
- * limit keep their buffer slots and wait for a full pass. */
453
+ * object re-retained since buffering is black. Drain the buffers before
454
+ * walking: tracing only adjusts counts and worklists, so nothing can
455
+ * re-buffer underneath us. Candidates ABOVE the limit keep their slots
456
+ * and wait for a full pass. */
452
457
  scr_cands.n = 0;
453
458
  for (unsigned g = 0; g <= gen_limit; g++) {
454
459
  for (size_t i = 0; i < scr_roots[g].n; i++) {
@@ -460,12 +465,31 @@ static size_t scr_cyc_pass(unsigned gen_limit) {
460
465
  }
461
466
  scr_roots[g].n = 0;
462
467
  }
463
- for (size_t i = 0; i < scr_cands.n; i++) scr_mark_gray(scr_cands.v[i]);
464
-
465
- for (size_t i = 0; i < scr_cands.n; i++) scr_scan(scr_cands.v[i]);
468
+ for (size_t i = 0; i < scr_cands.n; i++) scr_mark_gray(scr_cands.v[i], 0);
469
+ while (scr_pending.n) {
470
+ void *obj = scr_pending.v[--scr_pending.n];
471
+ scr_cyc_hdr(obj)->trace(obj, scr_mg_visit, NULL);
472
+ }
466
473
 
467
474
  scr_white.n = 0;
468
- for (size_t i = 0; i < scr_cands.n; i++) scr_collect_white(scr_cands.v[i]);
475
+ for (size_t i = 0; i < scr_cands.n; i++) scr_scan(scr_cands.v[i], 0);
476
+ while (scr_pending.n) {
477
+ void *obj = scr_pending.v[--scr_pending.n];
478
+ /* A later outside root may have restored this deferred white node and
479
+ * its descendants already. Never scan a black node's edges again. */
480
+ if (scr_cyc_hdr(obj)->color == SCR_CYC_WHITE)
481
+ scr_cyc_hdr(obj)->trace(obj, scr_scan_visit, NULL);
482
+ }
483
+
484
+ size_t dead = 0;
485
+ for (size_t i = 0; i < scr_white.n; i++) {
486
+ void *obj = scr_white.v[i];
487
+ ScrCycHdr *h = scr_cyc_hdr(obj);
488
+ if (h->color != SCR_CYC_WHITE) continue;
489
+ h->color = SCR_CYC_DOOMED;
490
+ scr_white.v[dead++] = obj;
491
+ }
492
+ scr_white.n = dead;
469
493
 
470
494
  /* Pin the cross-generation targets of everything about to be freed, while
471
495
  * `gen` still holds the values the walk filtered on (promotion below is