@amxts/core 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.
Files changed (247) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +162 -0
  3. package/README.ru.md +162 -0
  4. package/amxmodx/base/include/amxconst.inc +594 -0
  5. package/amxmodx/base/include/amxmisc.inc +902 -0
  6. package/amxmodx/base/include/amxmodx.inc +3447 -0
  7. package/amxmodx/base/include/amxmodx_version.inc +20 -0
  8. package/amxmodx/base/include/cellarray.inc +525 -0
  9. package/amxmodx/base/include/cellstack.inc +166 -0
  10. package/amxmodx/base/include/celltrie.inc +412 -0
  11. package/amxmodx/base/include/core.inc +202 -0
  12. package/amxmodx/base/include/csstats.inc +298 -0
  13. package/amxmodx/base/include/csstats_const.inc +29 -0
  14. package/amxmodx/base/include/cstrike.inc +1287 -0
  15. package/amxmodx/base/include/cstrike_const.inc +521 -0
  16. package/amxmodx/base/include/csx.inc +259 -0
  17. package/amxmodx/base/include/cvars.inc +547 -0
  18. package/amxmodx/base/include/datapack.inc +161 -0
  19. package/amxmodx/base/include/dbi.inc +154 -0
  20. package/amxmodx/base/include/dodconst.inc +152 -0
  21. package/amxmodx/base/include/dodfun.inc +152 -0
  22. package/amxmodx/base/include/dodstats.inc +69 -0
  23. package/amxmodx/base/include/dodx.inc +161 -0
  24. package/amxmodx/base/include/engine.inc +1343 -0
  25. package/amxmodx/base/include/engine_const.inc +310 -0
  26. package/amxmodx/base/include/engine_stocks.inc +248 -0
  27. package/amxmodx/base/include/esf.inc +70 -0
  28. package/amxmodx/base/include/esf_const.inc +74 -0
  29. package/amxmodx/base/include/fakemeta.inc +1127 -0
  30. package/amxmodx/base/include/fakemeta_const.inc +793 -0
  31. package/amxmodx/base/include/fakemeta_stocks.inc +395 -0
  32. package/amxmodx/base/include/fakemeta_util.inc +882 -0
  33. package/amxmodx/base/include/file.inc +666 -0
  34. package/amxmodx/base/include/float.inc +422 -0
  35. package/amxmodx/base/include/fun.inc +333 -0
  36. package/amxmodx/base/include/gameconfig.inc +93 -0
  37. package/amxmodx/base/include/geoip.inc +264 -0
  38. package/amxmodx/base/include/ham_const.inc +4352 -0
  39. package/amxmodx/base/include/hamsandwich.inc +439 -0
  40. package/amxmodx/base/include/hlsdk_const.inc +808 -0
  41. package/amxmodx/base/include/json.inc +777 -0
  42. package/amxmodx/base/include/lang.inc +119 -0
  43. package/amxmodx/base/include/message_const.inc +1130 -0
  44. package/amxmodx/base/include/message_stocks.inc +161 -0
  45. package/amxmodx/base/include/messages.inc +604 -0
  46. package/amxmodx/base/include/newmenus.inc +387 -0
  47. package/amxmodx/base/include/ns.inc +754 -0
  48. package/amxmodx/base/include/ns_const.inc +196 -0
  49. package/amxmodx/base/include/nvault.inc +154 -0
  50. package/amxmodx/base/include/regex.inc +348 -0
  51. package/amxmodx/base/include/sockets.inc +183 -0
  52. package/amxmodx/base/include/sorting.inc +107 -0
  53. package/amxmodx/base/include/sqlx.inc +582 -0
  54. package/amxmodx/base/include/string.inc +786 -0
  55. package/amxmodx/base/include/string_const.inc +156 -0
  56. package/amxmodx/base/include/string_stocks.inc +325 -0
  57. package/amxmodx/base/include/textparse_ini.inc +214 -0
  58. package/amxmodx/base/include/textparse_smc.inc +261 -0
  59. package/amxmodx/base/include/tfcconst.inc +84 -0
  60. package/amxmodx/base/include/tfcstats.inc +62 -0
  61. package/amxmodx/base/include/tfcx.inc +148 -0
  62. package/amxmodx/base/include/time.inc +118 -0
  63. package/amxmodx/base/include/tsconst.inc +154 -0
  64. package/amxmodx/base/include/tsfun.inc +198 -0
  65. package/amxmodx/base/include/tsstats.inc +65 -0
  66. package/amxmodx/base/include/tsx.inc +88 -0
  67. package/amxmodx/base/include/vault.inc +60 -0
  68. package/amxmodx/base/include/vector.inc +131 -0
  69. package/amxmodx/base/include/xs.inc +1570 -0
  70. package/as/amxts.d.ts +153 -0
  71. package/as/constants.ts +4730 -0
  72. package/as/effects.ts +1449 -0
  73. package/as/entities.ts +5482 -0
  74. package/as/events.ts +3054 -0
  75. package/as/facade.ts +4639 -0
  76. package/as/flags.ts +299 -0
  77. package/as/fs.ts +287 -0
  78. package/as/hooks.ts +18536 -0
  79. package/as/kit.ts +25 -0
  80. package/as/lib/check.ts +86 -0
  81. package/as/modules/http.ts +184 -0
  82. package/as/natives.ts +11845 -0
  83. package/as/os.ts +30 -0
  84. package/as/promise.ts +1392 -0
  85. package/as/promise.types.d.ts +82 -0
  86. package/as/remote.ts +288 -0
  87. package/as/tsconfig.json +22 -0
  88. package/as/vector.ts +65 -0
  89. package/bin/amxts.mjs +10 -0
  90. package/includes/menu_core.inc +355 -0
  91. package/includes/order.txt +54 -0
  92. package/includes/sources.json +27 -0
  93. package/includes/universal_config.inc +355 -0
  94. package/lint/oxlint-plugin.mjs +77 -0
  95. package/node_modules/assemblyscript/LICENSE +201 -0
  96. package/node_modules/assemblyscript/NOTICE +93 -0
  97. package/node_modules/assemblyscript/package.json +63 -0
  98. package/node_modules/assemblyscript/std/README.md +6 -0
  99. package/node_modules/assemblyscript/std/assembly/array.ts +526 -0
  100. package/node_modules/assemblyscript/std/assembly/arraybuffer.ts +77 -0
  101. package/node_modules/assemblyscript/std/assembly/atomics.ts +127 -0
  102. package/node_modules/assemblyscript/std/assembly/bindings/asyncify.ts +16 -0
  103. package/node_modules/assemblyscript/std/assembly/bindings/dom.ts +291 -0
  104. package/node_modules/assemblyscript/std/assembly/bindings/node.ts +6 -0
  105. package/node_modules/assemblyscript/std/assembly/builtins.ts +2631 -0
  106. package/node_modules/assemblyscript/std/assembly/compat.ts +2 -0
  107. package/node_modules/assemblyscript/std/assembly/console.ts +42 -0
  108. package/node_modules/assemblyscript/std/assembly/crypto.ts +9 -0
  109. package/node_modules/assemblyscript/std/assembly/dataview.ts +181 -0
  110. package/node_modules/assemblyscript/std/assembly/date.ts +373 -0
  111. package/node_modules/assemblyscript/std/assembly/diagnostics.ts +11 -0
  112. package/node_modules/assemblyscript/std/assembly/error.ts +44 -0
  113. package/node_modules/assemblyscript/std/assembly/function.ts +38 -0
  114. package/node_modules/assemblyscript/std/assembly/index.d.ts +2822 -0
  115. package/node_modules/assemblyscript/std/assembly/iterator.ts +35 -0
  116. package/node_modules/assemblyscript/std/assembly/map.ts +260 -0
  117. package/node_modules/assemblyscript/std/assembly/math.ts +3289 -0
  118. package/node_modules/assemblyscript/std/assembly/memory.ts +123 -0
  119. package/node_modules/assemblyscript/std/assembly/number.ts +388 -0
  120. package/node_modules/assemblyscript/std/assembly/object.ts +36 -0
  121. package/node_modules/assemblyscript/std/assembly/performance.ts +9 -0
  122. package/node_modules/assemblyscript/std/assembly/polyfills.ts +27 -0
  123. package/node_modules/assemblyscript/std/assembly/process.ts +50 -0
  124. package/node_modules/assemblyscript/std/assembly/reference.ts +48 -0
  125. package/node_modules/assemblyscript/std/assembly/regexp.ts +12 -0
  126. package/node_modules/assemblyscript/std/assembly/rt/README.md +83 -0
  127. package/node_modules/assemblyscript/std/assembly/rt/common.ts +81 -0
  128. package/node_modules/assemblyscript/std/assembly/rt/index-incremental.ts +2 -0
  129. package/node_modules/assemblyscript/std/assembly/rt/index-minimal.ts +2 -0
  130. package/node_modules/assemblyscript/std/assembly/rt/index-stub.ts +1 -0
  131. package/node_modules/assemblyscript/std/assembly/rt/index.d.ts +37 -0
  132. package/node_modules/assemblyscript/std/assembly/rt/itcms.ts +419 -0
  133. package/node_modules/assemblyscript/std/assembly/rt/rtrace.ts +15 -0
  134. package/node_modules/assemblyscript/std/assembly/rt/stub.ts +133 -0
  135. package/node_modules/assemblyscript/std/assembly/rt/tcms.ts +254 -0
  136. package/node_modules/assemblyscript/std/assembly/rt/tlsf.ts +592 -0
  137. package/node_modules/assemblyscript/std/assembly/rt.ts +87 -0
  138. package/node_modules/assemblyscript/std/assembly/set.ts +220 -0
  139. package/node_modules/assemblyscript/std/assembly/shared/feature.ts +65 -0
  140. package/node_modules/assemblyscript/std/assembly/shared/runtime.ts +11 -0
  141. package/node_modules/assemblyscript/std/assembly/shared/target.ts +11 -0
  142. package/node_modules/assemblyscript/std/assembly/shared/tsconfig.json +11 -0
  143. package/node_modules/assemblyscript/std/assembly/shared/typeinfo.ts +72 -0
  144. package/node_modules/assemblyscript/std/assembly/staticarray.ts +401 -0
  145. package/node_modules/assemblyscript/std/assembly/string.ts +847 -0
  146. package/node_modules/assemblyscript/std/assembly/symbol.ts +114 -0
  147. package/node_modules/assemblyscript/std/assembly/table.ts +16 -0
  148. package/node_modules/assemblyscript/std/assembly/tsconfig.json +6 -0
  149. package/node_modules/assemblyscript/std/assembly/typedarray.ts +1945 -0
  150. package/node_modules/assemblyscript/std/assembly/uri.ts +17 -0
  151. package/node_modules/assemblyscript/std/assembly/util/bytes.ts +107 -0
  152. package/node_modules/assemblyscript/std/assembly/util/casemap.ts +497 -0
  153. package/node_modules/assemblyscript/std/assembly/util/error.ts +54 -0
  154. package/node_modules/assemblyscript/std/assembly/util/hash.ts +117 -0
  155. package/node_modules/assemblyscript/std/assembly/util/math.ts +1922 -0
  156. package/node_modules/assemblyscript/std/assembly/util/memory.ts +290 -0
  157. package/node_modules/assemblyscript/std/assembly/util/number.ts +873 -0
  158. package/node_modules/assemblyscript/std/assembly/util/sort.ts +313 -0
  159. package/node_modules/assemblyscript/std/assembly/util/string.ts +1202 -0
  160. package/node_modules/assemblyscript/std/assembly/util/uri.ts +275 -0
  161. package/node_modules/assemblyscript/std/assembly/vector.ts +4 -0
  162. package/node_modules/assemblyscript/std/assembly.json +16 -0
  163. package/node_modules/assemblyscript/std/portable/index.d.ts +462 -0
  164. package/node_modules/assemblyscript/std/portable/index.js +416 -0
  165. package/node_modules/assemblyscript/std/portable.json +11 -0
  166. package/node_modules/assemblyscript/std/types/assembly/index.d.ts +1 -0
  167. package/node_modules/assemblyscript/std/types/assembly/package.json +3 -0
  168. package/node_modules/assemblyscript/std/types/portable/index.d.ts +1 -0
  169. package/node_modules/assemblyscript/std/types/portable/package.json +3 -0
  170. package/node_modules/assemblyscript/tsconfig-base.json +13 -0
  171. package/package.json +112 -0
  172. package/runtime/deps/assemblyscript/LICENSE +201 -0
  173. package/runtime/deps/assemblyscript/NOTICE +93 -0
  174. package/runtime/deps/assemblyscript/bin/asc.js +35 -0
  175. package/runtime/deps/assemblyscript/dist/asc.js +25035 -0
  176. package/runtime/deps/assemblyscript/dist/assemblyscript.js +337 -0
  177. package/runtime/deps/assemblyscript/node_modules/binaryen/LICENSE +201 -0
  178. package/runtime/deps/assemblyscript/node_modules/binaryen/index.js +28905 -0
  179. package/runtime/deps/assemblyscript/node_modules/binaryen/package.json +50 -0
  180. package/runtime/deps/assemblyscript/node_modules/long/LICENSE +202 -0
  181. package/runtime/deps/assemblyscript/node_modules/long/index.js +1581 -0
  182. package/runtime/deps/assemblyscript/node_modules/long/package.json +58 -0
  183. package/runtime/deps/assemblyscript/package.json +114 -0
  184. package/runtime/host/amxts.inc +85 -0
  185. package/runtime/licenses/AMXX-LICENSE.txt +34 -0
  186. package/runtime/licenses/AssemblyScript-LICENSE +201 -0
  187. package/runtime/licenses/AssemblyScript-NOTICE +93 -0
  188. package/runtime/licenses/Binaryen-LICENSE +201 -0
  189. package/runtime/licenses/Bun-LICENSE.md +84 -0
  190. package/runtime/licenses/GPL-3.0.txt +674 -0
  191. package/runtime/licenses/LLVM-LICENSE.TXT +279 -0
  192. package/runtime/licenses/README.md +25 -0
  193. package/runtime/licenses/WAMR-LICENSE +219 -0
  194. package/runtime/natives.txt +1095 -0
  195. package/runtime/patches/README.md +59 -0
  196. package/runtime/patches/assemblyscript-0.28.20-amxts.patch +14174 -0
  197. package/runtime/patches/wamr-2.4.5-amxts.patch +364 -0
  198. package/scripts/apply-docs.ts +508 -0
  199. package/scripts/asc.ts +27 -0
  200. package/scripts/auto-imports.ts +258 -0
  201. package/scripts/build-wasm.ts +595 -0
  202. package/scripts/check.ts +93 -0
  203. package/scripts/compile-cache.ts +179 -0
  204. package/scripts/compile.ts +401 -0
  205. package/scripts/docker-server.ts +73 -0
  206. package/scripts/docs/as/amxts.ts +210 -0
  207. package/scripts/docs/as/effects.ts +1684 -0
  208. package/scripts/docs/as/facade.ts +3302 -0
  209. package/scripts/docs/as/fs.ts +173 -0
  210. package/scripts/docs/as/lib/check.ts +68 -0
  211. package/scripts/docs/as/modules/http.ts +66 -0
  212. package/scripts/docs/as/os.ts +16 -0
  213. package/scripts/docs/as/promise.ts +132 -0
  214. package/scripts/docs/as/promise.types.ts +63 -0
  215. package/scripts/docs/as/vector.ts +48 -0
  216. package/scripts/docs/entities.ts +2338 -0
  217. package/scripts/docs/events.ts +245 -0
  218. package/scripts/docs/game.ts +497 -0
  219. package/scripts/docs/messages.ts +148 -0
  220. package/scripts/file-contents.ts +74 -0
  221. package/scripts/includes.ts +134 -0
  222. package/scripts/player-fields.ts +866 -0
  223. package/scripts/plugin-cache.ts +92 -0
  224. package/scripts/plugin-natives.ts +1157 -0
  225. package/scripts/prebuilt.ts +268 -0
  226. package/scripts/prepare.ts +134 -0
  227. package/scripts/project.ts +973 -0
  228. package/scripts/run.ts +28 -0
  229. package/scripts/shared-modules.ts +906 -0
  230. package/scripts/system.ts +33 -0
  231. package/scripts/tracked-fs.ts +115 -0
  232. package/scripts/typed-configs.ts +788 -0
  233. package/scripts/ui.ts +77 -0
  234. package/src/cli-api.mjs +175 -0
  235. package/src/parser/include-parser.ts +506 -0
  236. package/src/system.mjs +155 -0
  237. package/src/testing/compile-cache.ts +31 -0
  238. package/src/testing/compile.ts +222 -0
  239. package/src/testing/coroutines.ts +191 -0
  240. package/src/testing/index.ts +39 -0
  241. package/src/testing/kits.ts +55 -0
  242. package/src/testing/memory.ts +125 -0
  243. package/src/testing/natives.ts +925 -0
  244. package/src/testing/server.ts +1874 -0
  245. package/src/testing/setup.ts +63 -0
  246. package/src/testing/tables.ts +139 -0
  247. package/src/types.ts +51 -0
@@ -0,0 +1,83 @@
1
+ The AssemblyScript Runtime
2
+ ==========================
3
+
4
+ The runtime provides the functionality necessary to dynamically allocate and deallocate memory of objects, arrays and buffers, as well as collect garbage that is no longer used. The current implementation is either a Two-Color Mark & Sweep (TCMS) garbage collector that must be called manually when the execution stack is unwound or an Incremental Tri-Color Mark & Sweep (ITCMS) garbage collector that is fully automated with a shadow stack, implemented on top of a Two-Level Segregate Fit (TLSF) memory manager. It's not designed to be the fastest of its kind, but intentionally focuses on simplicity and ease of integration until we can replace it with the real deal, i.e. Wasm GC.
5
+
6
+ Interface
7
+ ---------
8
+
9
+ ### Garbage collector / `--exportRuntime`
10
+
11
+ * **__new**(size: `usize`, id: `u32` = 0): `usize`<br />
12
+ Dynamically allocates a GC object of at least the specified size and returns its address.
13
+ Alignment is guaranteed to be 16 bytes to fit up to v128 values naturally.
14
+ GC-allocated objects cannot be used with `__realloc` and `__free`.
15
+
16
+ * **__pin**(ptr: `usize`): `usize`<br />
17
+ Pins the object pointed to by `ptr` externally so it and its directly reachable members and indirectly reachable objects do not become garbage collected.
18
+
19
+ * **__unpin**(ptr: `usize`): `void`<br />
20
+ Unpins the object pointed to by `ptr` externally so it can become garbage collected.
21
+
22
+ * **__collect**(): `void`<br />
23
+ Performs a full garbage collection.
24
+
25
+ ### Internals
26
+
27
+ * **__alloc**(size: `usize`): `usize`<br />
28
+ Dynamically allocates a chunk of memory of at least the specified size and returns its address.
29
+ Alignment is guaranteed to be 16 bytes to fit up to v128 values naturally.
30
+
31
+ * **__realloc**(ptr: `usize`, size: `usize`): `usize`<br />
32
+ Dynamically changes the size of a chunk of memory, possibly moving it to a new address.
33
+
34
+ * **__free**(ptr: `usize`): `void`<br />
35
+ Frees a dynamically allocated chunk of memory by its address.
36
+
37
+ * **__renew**(ptr: `usize`, size: `usize`): `usize`<br />
38
+ Like `__realloc`, but for `__new`ed GC objects.
39
+
40
+ * **__link**(parentPtr: `usize`, childPtr: `usize`, expectMultiple: `bool`): `void`<br />
41
+ Introduces a link from a parent object to a child object, i.e. upon `parent.field = child`.
42
+
43
+ * **__visit**(ptr: `usize`, cookie: `u32`): `void`<br />
44
+ Concrete visitor implementation called during traversal. Cookie can be used to indicate one of multiple operations.
45
+
46
+ * **__visit_globals**(cookie: `u32`): `void`<br />
47
+ Calls `__visit` on each global that is of a managed type.
48
+
49
+ * **__visit_members**(ptr: `usize`, cookie: `u32`): `void`<br />
50
+ Calls `__visit` on each member of the object pointed to by `ptr`.
51
+
52
+ * **__typeinfo**(id: `u32`): `RTTIFlags`<br />
53
+ Obtains the runtime type information for objects with the specified runtime id. Runtime type information is a set of flags indicating whether a type is managed, an array or similar, and what the relevant alignments when creating an instance externally are etc.
54
+
55
+ ITCMS / `--runtime incremental`
56
+ -----
57
+
58
+ The Incremental Tri-Color Mark & Sweep garbage collector maintains a separate shadow stack of managed values in the background to achieve full automation. Maintaining another stack introduces some overhead compared to the simpler Two-Color Mark & Sweep garbage collector, but makes it independent of whether the execution stack is unwound or not when it is invoked, so the garbage collector can run interleaved with the program.
59
+
60
+ There are several constants one can experiment with to tweak ITCMS's automation:
61
+
62
+ * `--use ASC_GC_GRANULARITY=1024`<br />
63
+ How often to interrupt. The default of 1024 means "interrupt each 1024 bytes allocated".
64
+ * `--use ASC_GC_STEPFACTOR=200`<br />
65
+ How long to interrupt. The default of 200% means "run at double the speed of allocations".
66
+ * `--use ASC_GC_IDLEFACTOR=200`<br />
67
+ How long to idle. The default of 200% means "wait for memory to double before kicking in again".
68
+ * `--use ASC_GC_MARKCOST=1`<br />
69
+ How costly it is to mark one object. Budget per interrupt is `GRANULARITY * STEPFACTOR / 100`.
70
+ * `--use ASC_GC_SWEEPCOST=10`<br />
71
+ How costly it is to sweep one object. Budget per interrupt is `GRANULARITY * STEPFACTOR / 100`.
72
+
73
+ TCMS / `--runtime minimal`
74
+ ----
75
+
76
+ If automation and low pause times aren't strictly necessary, using the Two-Color Mark & Sweep garbage collector instead by invoking collection manually at appropriate times when the execution stack is unwound may be more performant as it simpler and has less overhead. The execution stack is typically unwound when invoking the collector externally, at a place that is not indirectly called from Wasm.
77
+
78
+ STUB / `--runtime stub`
79
+ ----
80
+
81
+ The stub is a maximally minimal runtime substitute, consisting of a simple and fast bump allocator with no means of freeing up memory again, except when freeing the respective most recently allocated object on top of the bump. Useful where memory is not a concern, and/or where it is sufficient to destroy the whole module including any potential garbage after execution.
82
+
83
+ See also: [Garbage collection](https://www.assemblyscript.org/garbage-collection.html)
@@ -0,0 +1,81 @@
1
+ // Alignment guarantees
2
+
3
+ // @ts-ignore: decorator
4
+ @inline export const AL_BITS: u32 = 4; // 16 bytes to fit up to v128
5
+ // @ts-ignore: decorator
6
+ @inline export const AL_SIZE: usize = 1 << <usize>AL_BITS;
7
+ // @ts-ignore: decorator
8
+ @inline export const AL_MASK: usize = AL_SIZE - 1;
9
+
10
+ // Extra debugging
11
+
12
+ // @ts-ignore: decorator
13
+ @inline export const DEBUG = true;
14
+ // @ts-ignore: decorator
15
+ @inline export const TRACE = false;
16
+ // @ts-ignore: decorator
17
+ @inline export const RTRACE = isDefined(ASC_RTRACE);
18
+ // @ts-ignore: decorator
19
+ @inline export const PROFILE = isDefined(ASC_PROFILE);
20
+
21
+ // Memory manager
22
+
23
+ // ╒════════════ Memory manager block layout (32-bit) ═════════════╕
24
+ // 3 2 1
25
+ // 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 bits
26
+ // ├─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┤
27
+ // │ MM info │ -4
28
+ // ╞>ptr═══════════════════════════════════════════════════════════╡
29
+ // │ ... │
30
+ @unmanaged export class BLOCK {
31
+ /** Memory manager info. */
32
+ mmInfo: usize;
33
+ }
34
+
35
+ /** Overhead of a memory manager block. */
36
+ // @ts-ignore: decorator
37
+ @inline export const BLOCK_OVERHEAD: usize = offsetof<BLOCK>();
38
+
39
+ /** Maximum size of a memory manager block's payload. */
40
+ // @ts-ignore: decorator
41
+ @inline export const BLOCK_MAXSIZE: usize = (1 << 30) - BLOCK_OVERHEAD;
42
+
43
+ // Garbage collector
44
+
45
+ // ╒══════════ Garbage collector object layout (32-bit) ═══════════╕
46
+ // 3 2 1
47
+ // 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 bits
48
+ // ├─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┤
49
+ // │ Memory manager block │ -20
50
+ // ╞═══════════════════════════════════════════════════════════════╡
51
+ // │ GC info │ -16
52
+ // ├───────────────────────────────────────────────────────────────┤
53
+ // │ GC info │ -12
54
+ // ├───────────────────────────────────────────────────────────────┤
55
+ // │ RT id │ -8
56
+ // ├───────────────────────────────────────────────────────────────┤
57
+ // │ RT size │ -4
58
+ // ╞>ptr═══════════════════════════════════════════════════════════╡
59
+ // │ ... │
60
+ @unmanaged export class OBJECT extends BLOCK {
61
+ /** Garbage collector info. */
62
+ gcInfo: u32;
63
+ /** Garbage collector info. */
64
+ gcInfo2: u32;
65
+ /** Runtime class id. */
66
+ rtId: u32;
67
+ /** Runtime object size. */
68
+ rtSize: u32;
69
+ }
70
+
71
+ /** Overhead of a garbage collector object. Excludes memory manager block overhead. */
72
+ // @ts-ignore: decorator
73
+ @inline export const OBJECT_OVERHEAD: usize = (offsetof<OBJECT>() - BLOCK_OVERHEAD + AL_MASK) & ~AL_MASK;
74
+
75
+ /** Maximum size of a garbage collector object's payload. */
76
+ // @ts-ignore: decorator
77
+ @inline export const OBJECT_MAXSIZE: usize = BLOCK_MAXSIZE - OBJECT_OVERHEAD;
78
+
79
+ /** Total of memory manager and garbage collector overhead. */
80
+ // @ts-ignore: decorator
81
+ @inline export const TOTAL_OVERHEAD: usize = BLOCK_OVERHEAD + OBJECT_OVERHEAD;
@@ -0,0 +1,2 @@
1
+ import "./tlsf";
2
+ import "./itcms";
@@ -0,0 +1,2 @@
1
+ import "./tlsf";
2
+ import "./tcms";
@@ -0,0 +1 @@
1
+ import "./stub";
@@ -0,0 +1,37 @@
1
+ // Memory manager
2
+ declare function __alloc(size: usize): usize;
3
+ declare function __realloc(ptr: usize, size: usize): usize;
4
+ declare function __free(ptr: usize): void;
5
+ declare function __reset(): void;
6
+
7
+ // Garbage collector
8
+ declare function __new(size: usize, id: u32): usize;
9
+ declare function __renew(ptr: usize, size: usize): usize;
10
+ declare function __link(parentPtr: usize, childPtr: usize, expectMultiple: bool): void;
11
+ declare function __collect(): void;
12
+
13
+ // Runtime type info
14
+ declare function __typeinfo(id: u32): u32;
15
+
16
+ // Visitors
17
+ declare function __visit(ptr: usize, cookie: i32): void;
18
+ declare function __visit_globals(cookie: u32): void;
19
+ declare function __visit_members(ptr: usize, cookie: u32): void;
20
+
21
+ // Allocation helpers
22
+ declare function __newBuffer(size: usize, id: u32, data?: usize): usize;
23
+ declare function __newArray(length: i32, alignLog2: usize, id: u32, data?: usize): usize;
24
+
25
+ // Finalization
26
+ declare function __finalize(ptr: usize): void;
27
+
28
+ // Debugging
29
+ declare const ASC_RTRACE: bool;
30
+ declare const ASC_PROFILE: bool;
31
+
32
+ // Incremental GC constants
33
+ declare const ASC_GC_GRANULARITY: i32;
34
+ declare const ASC_GC_SWEEPFACTOR: i32;
35
+ declare const ASC_GC_IDLEFACTOR: i32;
36
+ declare const ASC_GC_MARKCOST: i32;
37
+ declare const ASC_GC_SWEEPCOST: i32;
@@ -0,0 +1,419 @@
1
+ import { BLOCK, BLOCK_OVERHEAD, OBJECT_OVERHEAD, OBJECT_MAXSIZE, TOTAL_OVERHEAD, DEBUG, TRACE, RTRACE, PROFILE } from "./common";
2
+ import { onvisit, oncollect, oninterrupt, onyield } from "./rtrace";
3
+ import { TypeinfoFlags } from "../shared/typeinfo";
4
+ import { E_ALLOCATION_TOO_LARGE, E_ALREADY_PINNED, E_NOT_PINNED } from "../util/error";
5
+
6
+ // === ITCMS: An incremental Tri-Color Mark & Sweep garbage collector ===
7
+ // Adapted from Bach Le's μgc, see: https://github.com/bullno1/ugc
8
+
9
+ // ╒═════════════╤══════════════ Colors ═══════════════════════════╕
10
+ // │ Color │ Meaning │
11
+ // ├─────────────┼─────────────────────────────────────────────────┤
12
+ // │ WHITE* │ Unprocessed │
13
+ // │ BLACK* │ Processed │
14
+ // │ GRAY │ Processed with unprocessed children │
15
+ // │ TRANSPARENT │ Manually pinned (always reachable) │
16
+ // └─────────────┴─────────────────────────────────────────────────┘
17
+ // * flipped between cycles
18
+
19
+ // @ts-ignore: decorator
20
+ @lazy let white = 0;
21
+ // @ts-ignore: decorator
22
+ @inline const gray = 2;
23
+ // @ts-ignore: decorator
24
+ @inline const transparent = 3;
25
+ // @ts-ignore: decorator
26
+ @inline const COLOR_MASK = 3;
27
+
28
+ /** Size in memory of all objects currently managed by the GC. */
29
+ // @ts-ignore: decorator
30
+ @lazy let total: usize = 0;
31
+
32
+ /** Currently transitioning from SWEEP to MARK state. */
33
+ // @ts-ignore: decorator
34
+ @inline const STATE_IDLE = 0;
35
+ /** Currently marking reachable objects. */
36
+ // @ts-ignore: decorator
37
+ @inline const STATE_MARK = 1;
38
+ /** Currently sweeping unreachable objects. */
39
+ // @ts-ignore: decorator
40
+ @inline const STATE_SWEEP = 2;
41
+ /** Current collector state. */
42
+ // @ts-ignore: decorator
43
+ @lazy let state = STATE_IDLE;
44
+
45
+ // @ts-ignore: decorator
46
+ @lazy let fromSpace = initLazy(changetype<Object>(memory.data(offsetof<Object>())));
47
+ // @ts-ignore: decorator
48
+ @lazy let toSpace = initLazy(changetype<Object>(memory.data(offsetof<Object>())));
49
+ // @ts-ignore: decorator
50
+ @lazy let pinSpace = initLazy(changetype<Object>(memory.data(offsetof<Object>())));
51
+ // @ts-ignore: decorator
52
+ @lazy let iter: Object = changetype<Object>(0); // unsafe initializion below
53
+
54
+ function initLazy(space: Object): Object {
55
+ space.nextWithColor = changetype<usize>(space);
56
+ space.prev = space;
57
+ return space;
58
+ }
59
+
60
+ /** Visit cookie indicating scanning of an object. */
61
+ // @ts-ignore: decorator
62
+ @inline const VISIT_SCAN = 0;
63
+
64
+ // ╒═══════════════ Managed object layout (32-bit) ════════════════╕
65
+ // 3 2 1
66
+ // 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 bits
67
+ // ├─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┤
68
+ // │ Memory manager block │
69
+ // ╞═══════════════════════════════════════════════════════════╤═══╡
70
+ // │ next │ C │ = nextWithColor
71
+ // ├───────────────────────────────────────────────────────────┴───┤
72
+ // │ prev │
73
+ // ├───────────────────────────────────────────────────────────────┤
74
+ // │ rtId │
75
+ // ├───────────────────────────────────────────────────────────────┤
76
+ // │ rtSize │
77
+ // ╞>ptr═══════════════════════════════════════════════════════════╡
78
+ // │ ... │
79
+ // C: color
80
+
81
+ /** Represents a managed object in memory, consisting of a header followed by the object's data. */
82
+ @unmanaged class Object extends BLOCK {
83
+ /** Pointer to the next object with color flags stored in the alignment bits. */
84
+ nextWithColor: usize; // *u32
85
+ /** Pointer to the previous object. */
86
+ prev: Object; // *u32
87
+ /** Runtime id. */
88
+ rtId: u32;
89
+ /** Runtime size. */
90
+ rtSize: u32;
91
+
92
+ /** Gets the pointer to the next object. */
93
+ get next(): Object {
94
+ return changetype<Object>(this.nextWithColor & ~COLOR_MASK);
95
+ }
96
+
97
+ /** Sets the pointer to the next object. */
98
+ set next(obj: Object) {
99
+ this.nextWithColor = changetype<usize>(obj) | (this.nextWithColor & COLOR_MASK);
100
+ }
101
+
102
+ /** Gets this object's color. */
103
+ get color(): i32 {
104
+ return i32(this.nextWithColor & COLOR_MASK);
105
+ }
106
+
107
+ /** Sets this object's color. */
108
+ set color(color: i32) {
109
+ this.nextWithColor = (this.nextWithColor & ~COLOR_MASK) | color;
110
+ }
111
+
112
+ /** Gets the size of this object in memory. */
113
+ get size(): usize {
114
+ return BLOCK_OVERHEAD + (this.mmInfo & ~3);
115
+ }
116
+
117
+ /** Tests if this object is pointerfree. */
118
+ get isPointerfree(): bool {
119
+ let rtId = this.rtId;
120
+ // 0: Object, 1: ArrayBuffer, 2: String
121
+ return rtId <= idof<string>() || (__typeinfo(rtId) & TypeinfoFlags.POINTERFREE) != 0;
122
+ }
123
+
124
+ /** Unlinks this object from its list. */
125
+ unlink(): void {
126
+ let next = this.next;
127
+ if (next == null) {
128
+ if (DEBUG) assert(this.prev == null && changetype<usize>(this) < __heap_base);
129
+ return; // static data not yet linked
130
+ }
131
+ let prev = this.prev;
132
+ if (DEBUG) assert(prev);
133
+ next.prev = prev;
134
+ prev.next = next;
135
+ }
136
+
137
+ /** Links this object to the specified list, with the given color. */
138
+ linkTo(list: Object, withColor: i32): void {
139
+ let prev = list.prev;
140
+ this.nextWithColor = changetype<usize>(list) | withColor;
141
+ this.prev = prev;
142
+ prev.next = this;
143
+ list.prev = this;
144
+ }
145
+
146
+ /** Marks this object as gray, that is reachable with unscanned children. */
147
+ makeGray(): void {
148
+ if (this == iter) iter = assert(this.prev);
149
+ this.unlink();
150
+ this.linkTo(toSpace, this.isPointerfree ? i32(!white) : gray);
151
+ }
152
+ }
153
+
154
+ /** Visits all objects considered to be program roots. */
155
+ function visitRoots(cookie: u32): void {
156
+ __visit_globals(cookie);
157
+ let pn = pinSpace;
158
+ let iter = pn.next;
159
+ while (iter != pn) {
160
+ if (DEBUG) assert(iter.color == transparent);
161
+ __visit_members(changetype<usize>(iter) + TOTAL_OVERHEAD, cookie);
162
+ iter = iter.next;
163
+ }
164
+ }
165
+
166
+ /** Visits all objects on the stack. */
167
+ function visitStack(cookie: u32): void {
168
+ let ptr = __stack_pointer;
169
+ while (ptr < __heap_base) {
170
+ __visit(load<usize>(ptr), cookie);
171
+ ptr += sizeof<usize>();
172
+ }
173
+ }
174
+
175
+ /** Performs a single step according to the current state. */
176
+ function step(): usize {
177
+ // Magic constants responsible for pause times. Obtained experimentally
178
+ // using the compiler compiling itself. 2048 budget pro run by default.
179
+ const MARKCOST = isDefined(ASC_GC_MARKCOST) ? ASC_GC_MARKCOST : 1;
180
+ const SWEEPCOST = isDefined(ASC_GC_SWEEPCOST) ? ASC_GC_SWEEPCOST : 10;
181
+ let obj: Object;
182
+ switch (state) {
183
+ case STATE_IDLE: {
184
+ state = STATE_MARK;
185
+ visitCount = 0;
186
+ visitRoots(VISIT_SCAN);
187
+ iter = toSpace;
188
+ return visitCount * MARKCOST;
189
+ }
190
+ case STATE_MARK: {
191
+ let black = i32(!white);
192
+ obj = iter.next;
193
+ while (obj != toSpace) {
194
+ iter = obj;
195
+ if (obj.color != black) { // skip already-blacks (pointerfree)
196
+ obj.color = black;
197
+ visitCount = 0;
198
+ __visit_members(changetype<usize>(obj) + TOTAL_OVERHEAD, VISIT_SCAN);
199
+ return visitCount * MARKCOST;
200
+ }
201
+ obj = obj.next;
202
+ }
203
+ visitCount = 0;
204
+ visitRoots(VISIT_SCAN);
205
+ obj = iter.next;
206
+ if (obj == toSpace) {
207
+ visitStack(VISIT_SCAN);
208
+ obj = iter.next;
209
+ while (obj != toSpace) {
210
+ if (obj.color != black) {
211
+ obj.color = black;
212
+ __visit_members(changetype<usize>(obj) + TOTAL_OVERHEAD, VISIT_SCAN);
213
+ }
214
+ obj = obj.next;
215
+ }
216
+ let from = fromSpace;
217
+ fromSpace = toSpace;
218
+ toSpace = from;
219
+ white = black;
220
+ iter = from.next;
221
+ state = STATE_SWEEP;
222
+ }
223
+ return visitCount * MARKCOST;
224
+ }
225
+ case STATE_SWEEP: {
226
+ obj = iter;
227
+ if (obj != toSpace) {
228
+ iter = obj.next;
229
+ if (DEBUG) assert(obj.color == i32(!white)); // old white
230
+ free(obj);
231
+ return SWEEPCOST;
232
+ }
233
+ toSpace.nextWithColor = changetype<usize>(toSpace);
234
+ toSpace.prev = toSpace;
235
+ state = STATE_IDLE;
236
+ break;
237
+ }
238
+ }
239
+ return 0;
240
+ }
241
+
242
+ /** Frees an object. */
243
+ function free(obj: Object): void {
244
+ if (changetype<usize>(obj) < __heap_base) {
245
+ obj.nextWithColor = 0; // may become linked again
246
+ obj.prev = changetype<Object>(0);
247
+ } else {
248
+ total -= obj.size;
249
+ if (isDefined(__finalize)) {
250
+ __finalize(changetype<usize>(obj) + TOTAL_OVERHEAD);
251
+ }
252
+ __free(changetype<usize>(obj) + BLOCK_OVERHEAD);
253
+ }
254
+ }
255
+
256
+ // Garbage collector interface
257
+
258
+ // @ts-ignore: decorator
259
+ @global @unsafe
260
+ export function __new(size: usize, id: i32): usize {
261
+ if (size >= OBJECT_MAXSIZE) throw new Error(E_ALLOCATION_TOO_LARGE);
262
+ if (total >= threshold) interrupt();
263
+ let obj = changetype<Object>(__alloc(OBJECT_OVERHEAD + size) - BLOCK_OVERHEAD);
264
+ obj.rtId = id;
265
+ obj.rtSize = <u32>size;
266
+ obj.linkTo(fromSpace, white); // inits next/prev
267
+ total += obj.size;
268
+ let ptr = changetype<usize>(obj) + TOTAL_OVERHEAD;
269
+ // may be visited before being fully initialized, so must fill
270
+ memory.fill(ptr, 0, size);
271
+ return ptr;
272
+ }
273
+
274
+ // @ts-ignore: decorator
275
+ @global @unsafe
276
+ export function __renew(oldPtr: usize, size: usize): usize {
277
+ let oldObj = changetype<Object>(oldPtr - TOTAL_OVERHEAD);
278
+ // Update object size if its block is large enough
279
+ if (size <= (oldObj.mmInfo & ~3) - OBJECT_OVERHEAD) {
280
+ oldObj.rtSize = <u32>size;
281
+ return oldPtr;
282
+ }
283
+ // If not the same object anymore, we have to move it move it due to the
284
+ // shadow stack potentially still referencing the old object
285
+ let newPtr = __new(size, oldObj.rtId);
286
+ memory.copy(newPtr, oldPtr, min(size, oldObj.rtSize));
287
+ return newPtr;
288
+ }
289
+
290
+ // @ts-ignore: decorator
291
+ @global @unsafe
292
+ export function __link(parentPtr: usize, childPtr: usize, expectMultiple: bool): void {
293
+ // Write barrier is unnecessary if non-incremental
294
+ if (!childPtr) return;
295
+ if (DEBUG) assert(parentPtr);
296
+ let child = changetype<Object>(childPtr - TOTAL_OVERHEAD);
297
+ if (child.color == white) {
298
+ let parent = changetype<Object>(parentPtr - TOTAL_OVERHEAD);
299
+ let parentColor = parent.color;
300
+ if (parentColor == i32(!white)) {
301
+ // Maintain the invariant that no black object may point to a white object.
302
+ if (expectMultiple) {
303
+ // Move the barrier "backward". Suitable for containers receiving multiple stores.
304
+ // Avoids a barrier for subsequent objects stored into the same container.
305
+ parent.makeGray();
306
+ } else {
307
+ // Move the barrier "forward". Suitable for objects receiving isolated stores.
308
+ child.makeGray();
309
+ }
310
+ } else if (parentColor == transparent && state == STATE_MARK) {
311
+ // Pinned objects are considered 'black' during the mark phase.
312
+ child.makeGray();
313
+ }
314
+ }
315
+ }
316
+
317
+ // @ts-ignore: decorator
318
+ @lazy let visitCount = 0;
319
+
320
+ // @ts-ignore: decorator
321
+ @global @unsafe
322
+ export function __visit(ptr: usize, cookie: i32): void {
323
+ if (!ptr) return;
324
+ let obj = changetype<Object>(ptr - TOTAL_OVERHEAD);
325
+ if (RTRACE) if (!onvisit(obj)) return;
326
+ if (obj.color == white) {
327
+ obj.makeGray();
328
+ ++visitCount;
329
+ }
330
+ }
331
+
332
+ // @ts-ignore: decorator
333
+ @global @unsafe
334
+ export function __pin(ptr: usize): usize {
335
+ if (ptr) {
336
+ let obj = changetype<Object>(ptr - TOTAL_OVERHEAD);
337
+ if (obj.color == transparent) {
338
+ throw new Error(E_ALREADY_PINNED);
339
+ }
340
+ obj.unlink(); // from fromSpace
341
+ obj.linkTo(pinSpace, transparent);
342
+ }
343
+ return ptr;
344
+ }
345
+
346
+ // @ts-ignore: decorator
347
+ @global @unsafe
348
+ export function __unpin(ptr: usize): void {
349
+ if (!ptr) return;
350
+ let obj = changetype<Object>(ptr - TOTAL_OVERHEAD);
351
+ if (obj.color != transparent) {
352
+ throw new Error(E_NOT_PINNED);
353
+ }
354
+ if (state == STATE_MARK) {
355
+ // We may be right at the point after marking roots for the second time and
356
+ // entering the sweep phase, in which case the object would be missed if it
357
+ // is not only pinned but also a root. Make sure it isn't missed.
358
+ obj.makeGray();
359
+ } else {
360
+ obj.unlink();
361
+ obj.linkTo(fromSpace, white);
362
+ }
363
+ }
364
+
365
+ // @ts-ignore: decorator
366
+ @global @unsafe
367
+ export function __collect(): void {
368
+ if (TRACE) trace("GC (full) at", 1, total);
369
+ if (state > STATE_IDLE) {
370
+ // finish current cycle
371
+ while (state != STATE_IDLE) step();
372
+ }
373
+ // perform a full cycle
374
+ step();
375
+ while (state != STATE_IDLE) step();
376
+ threshold = <usize>(<u64>total * IDLEFACTOR / 100) + GRANULARITY;
377
+ if (TRACE) trace("GC (full) done at cur/max", 2, total, memory.size() << 16);
378
+ if (RTRACE || PROFILE) oncollect(total);
379
+ }
380
+
381
+ // Garbage collector automation
382
+
383
+ /** How often to interrupt. The default of 1024 means "interrupt each 1024 bytes allocated". */
384
+ // @ts-ignore: decorator
385
+ @inline const GRANULARITY: usize = isDefined(ASC_GC_GRANULARITY) ? ASC_GC_GRANULARITY : 1024;
386
+ /** How long to interrupt. The default of 200% means "run at double the speed of allocations". */
387
+ // @ts-ignore: decorator
388
+ @inline const STEPFACTOR: usize = isDefined(ASC_GC_SWEEPFACTOR) ? ASC_GC_SWEEPFACTOR : 200;
389
+ /** How long to idle. The default of 200% means "wait for memory to double before kicking in again". */
390
+ // @ts-ignore: decorator
391
+ @inline const IDLEFACTOR: usize = isDefined(ASC_GC_IDLEFACTOR) ? ASC_GC_IDLEFACTOR : 200;
392
+
393
+ /** Threshold of memory used by objects to exceed before interrupting again. */
394
+ // @ts-ignore: decorator
395
+ @lazy let threshold: usize = ((<usize>memory.size() << 16) - __heap_base) >> 1;
396
+
397
+ /** Performs a reasonable amount of incremental GC steps. */
398
+ function interrupt(): void {
399
+ if (PROFILE) oninterrupt(total);
400
+ if (TRACE) trace("GC (auto) at", 1, total);
401
+ let budget: isize = GRANULARITY * STEPFACTOR / 100;
402
+ do {
403
+ budget -= step();
404
+ if (state == STATE_IDLE) {
405
+ if (TRACE) trace("└ GC (auto) done at cur/max", 2, total, memory.size() << 16);
406
+ if (IDLEFACTOR % 100 == 0) {
407
+ // optimization for the default GC parameters which idle factor is 200%
408
+ threshold = total * (IDLEFACTOR / 100) + GRANULARITY;
409
+ } else {
410
+ threshold = <usize>((<u64>total * IDLEFACTOR) / 100) + GRANULARITY;
411
+ }
412
+ if (PROFILE) onyield(total);
413
+ return;
414
+ }
415
+ } while (budget > 0);
416
+ if (TRACE) trace("└ GC (auto) ongoing at", 1, total);
417
+ threshold = total + GRANULARITY * usize(total - threshold < GRANULARITY);
418
+ if (PROFILE) onyield(total);
419
+ }
@@ -0,0 +1,15 @@
1
+ import { BLOCK } from "./common";
2
+
3
+ export declare function oninit(heapBase: usize): void;
4
+
5
+ // Memory Allocator
6
+ export declare function onalloc(block: BLOCK): void;
7
+ export declare function onresize(block: BLOCK, oldSizeInclOverhead: usize): void;
8
+ export declare function onmove(oldBlock: BLOCK, newBlock: BLOCK): void;
9
+ export declare function onfree(block: BLOCK): void;
10
+
11
+ // Garbage collector
12
+ export declare function onvisit(block: BLOCK): bool;
13
+ export declare function oncollect(total: usize): void;
14
+ export declare function oninterrupt(total: usize): void;
15
+ export declare function onyield(total: usize): void;