@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.
- package/LICENSE +21 -0
- package/README.md +162 -0
- package/README.ru.md +162 -0
- package/amxmodx/base/include/amxconst.inc +594 -0
- package/amxmodx/base/include/amxmisc.inc +902 -0
- package/amxmodx/base/include/amxmodx.inc +3447 -0
- package/amxmodx/base/include/amxmodx_version.inc +20 -0
- package/amxmodx/base/include/cellarray.inc +525 -0
- package/amxmodx/base/include/cellstack.inc +166 -0
- package/amxmodx/base/include/celltrie.inc +412 -0
- package/amxmodx/base/include/core.inc +202 -0
- package/amxmodx/base/include/csstats.inc +298 -0
- package/amxmodx/base/include/csstats_const.inc +29 -0
- package/amxmodx/base/include/cstrike.inc +1287 -0
- package/amxmodx/base/include/cstrike_const.inc +521 -0
- package/amxmodx/base/include/csx.inc +259 -0
- package/amxmodx/base/include/cvars.inc +547 -0
- package/amxmodx/base/include/datapack.inc +161 -0
- package/amxmodx/base/include/dbi.inc +154 -0
- package/amxmodx/base/include/dodconst.inc +152 -0
- package/amxmodx/base/include/dodfun.inc +152 -0
- package/amxmodx/base/include/dodstats.inc +69 -0
- package/amxmodx/base/include/dodx.inc +161 -0
- package/amxmodx/base/include/engine.inc +1343 -0
- package/amxmodx/base/include/engine_const.inc +310 -0
- package/amxmodx/base/include/engine_stocks.inc +248 -0
- package/amxmodx/base/include/esf.inc +70 -0
- package/amxmodx/base/include/esf_const.inc +74 -0
- package/amxmodx/base/include/fakemeta.inc +1127 -0
- package/amxmodx/base/include/fakemeta_const.inc +793 -0
- package/amxmodx/base/include/fakemeta_stocks.inc +395 -0
- package/amxmodx/base/include/fakemeta_util.inc +882 -0
- package/amxmodx/base/include/file.inc +666 -0
- package/amxmodx/base/include/float.inc +422 -0
- package/amxmodx/base/include/fun.inc +333 -0
- package/amxmodx/base/include/gameconfig.inc +93 -0
- package/amxmodx/base/include/geoip.inc +264 -0
- package/amxmodx/base/include/ham_const.inc +4352 -0
- package/amxmodx/base/include/hamsandwich.inc +439 -0
- package/amxmodx/base/include/hlsdk_const.inc +808 -0
- package/amxmodx/base/include/json.inc +777 -0
- package/amxmodx/base/include/lang.inc +119 -0
- package/amxmodx/base/include/message_const.inc +1130 -0
- package/amxmodx/base/include/message_stocks.inc +161 -0
- package/amxmodx/base/include/messages.inc +604 -0
- package/amxmodx/base/include/newmenus.inc +387 -0
- package/amxmodx/base/include/ns.inc +754 -0
- package/amxmodx/base/include/ns_const.inc +196 -0
- package/amxmodx/base/include/nvault.inc +154 -0
- package/amxmodx/base/include/regex.inc +348 -0
- package/amxmodx/base/include/sockets.inc +183 -0
- package/amxmodx/base/include/sorting.inc +107 -0
- package/amxmodx/base/include/sqlx.inc +582 -0
- package/amxmodx/base/include/string.inc +786 -0
- package/amxmodx/base/include/string_const.inc +156 -0
- package/amxmodx/base/include/string_stocks.inc +325 -0
- package/amxmodx/base/include/textparse_ini.inc +214 -0
- package/amxmodx/base/include/textparse_smc.inc +261 -0
- package/amxmodx/base/include/tfcconst.inc +84 -0
- package/amxmodx/base/include/tfcstats.inc +62 -0
- package/amxmodx/base/include/tfcx.inc +148 -0
- package/amxmodx/base/include/time.inc +118 -0
- package/amxmodx/base/include/tsconst.inc +154 -0
- package/amxmodx/base/include/tsfun.inc +198 -0
- package/amxmodx/base/include/tsstats.inc +65 -0
- package/amxmodx/base/include/tsx.inc +88 -0
- package/amxmodx/base/include/vault.inc +60 -0
- package/amxmodx/base/include/vector.inc +131 -0
- package/amxmodx/base/include/xs.inc +1570 -0
- package/as/amxts.d.ts +153 -0
- package/as/constants.ts +4730 -0
- package/as/effects.ts +1449 -0
- package/as/entities.ts +5482 -0
- package/as/events.ts +3054 -0
- package/as/facade.ts +4639 -0
- package/as/flags.ts +299 -0
- package/as/fs.ts +287 -0
- package/as/hooks.ts +18536 -0
- package/as/kit.ts +25 -0
- package/as/lib/check.ts +86 -0
- package/as/modules/http.ts +184 -0
- package/as/natives.ts +11845 -0
- package/as/os.ts +30 -0
- package/as/promise.ts +1392 -0
- package/as/promise.types.d.ts +82 -0
- package/as/remote.ts +288 -0
- package/as/tsconfig.json +22 -0
- package/as/vector.ts +65 -0
- package/bin/amxts.mjs +10 -0
- package/includes/menu_core.inc +355 -0
- package/includes/order.txt +54 -0
- package/includes/sources.json +27 -0
- package/includes/universal_config.inc +355 -0
- package/lint/oxlint-plugin.mjs +77 -0
- package/node_modules/assemblyscript/LICENSE +201 -0
- package/node_modules/assemblyscript/NOTICE +93 -0
- package/node_modules/assemblyscript/package.json +63 -0
- package/node_modules/assemblyscript/std/README.md +6 -0
- package/node_modules/assemblyscript/std/assembly/array.ts +526 -0
- package/node_modules/assemblyscript/std/assembly/arraybuffer.ts +77 -0
- package/node_modules/assemblyscript/std/assembly/atomics.ts +127 -0
- package/node_modules/assemblyscript/std/assembly/bindings/asyncify.ts +16 -0
- package/node_modules/assemblyscript/std/assembly/bindings/dom.ts +291 -0
- package/node_modules/assemblyscript/std/assembly/bindings/node.ts +6 -0
- package/node_modules/assemblyscript/std/assembly/builtins.ts +2631 -0
- package/node_modules/assemblyscript/std/assembly/compat.ts +2 -0
- package/node_modules/assemblyscript/std/assembly/console.ts +42 -0
- package/node_modules/assemblyscript/std/assembly/crypto.ts +9 -0
- package/node_modules/assemblyscript/std/assembly/dataview.ts +181 -0
- package/node_modules/assemblyscript/std/assembly/date.ts +373 -0
- package/node_modules/assemblyscript/std/assembly/diagnostics.ts +11 -0
- package/node_modules/assemblyscript/std/assembly/error.ts +44 -0
- package/node_modules/assemblyscript/std/assembly/function.ts +38 -0
- package/node_modules/assemblyscript/std/assembly/index.d.ts +2822 -0
- package/node_modules/assemblyscript/std/assembly/iterator.ts +35 -0
- package/node_modules/assemblyscript/std/assembly/map.ts +260 -0
- package/node_modules/assemblyscript/std/assembly/math.ts +3289 -0
- package/node_modules/assemblyscript/std/assembly/memory.ts +123 -0
- package/node_modules/assemblyscript/std/assembly/number.ts +388 -0
- package/node_modules/assemblyscript/std/assembly/object.ts +36 -0
- package/node_modules/assemblyscript/std/assembly/performance.ts +9 -0
- package/node_modules/assemblyscript/std/assembly/polyfills.ts +27 -0
- package/node_modules/assemblyscript/std/assembly/process.ts +50 -0
- package/node_modules/assemblyscript/std/assembly/reference.ts +48 -0
- package/node_modules/assemblyscript/std/assembly/regexp.ts +12 -0
- package/node_modules/assemblyscript/std/assembly/rt/README.md +83 -0
- package/node_modules/assemblyscript/std/assembly/rt/common.ts +81 -0
- package/node_modules/assemblyscript/std/assembly/rt/index-incremental.ts +2 -0
- package/node_modules/assemblyscript/std/assembly/rt/index-minimal.ts +2 -0
- package/node_modules/assemblyscript/std/assembly/rt/index-stub.ts +1 -0
- package/node_modules/assemblyscript/std/assembly/rt/index.d.ts +37 -0
- package/node_modules/assemblyscript/std/assembly/rt/itcms.ts +419 -0
- package/node_modules/assemblyscript/std/assembly/rt/rtrace.ts +15 -0
- package/node_modules/assemblyscript/std/assembly/rt/stub.ts +133 -0
- package/node_modules/assemblyscript/std/assembly/rt/tcms.ts +254 -0
- package/node_modules/assemblyscript/std/assembly/rt/tlsf.ts +592 -0
- package/node_modules/assemblyscript/std/assembly/rt.ts +87 -0
- package/node_modules/assemblyscript/std/assembly/set.ts +220 -0
- package/node_modules/assemblyscript/std/assembly/shared/feature.ts +65 -0
- package/node_modules/assemblyscript/std/assembly/shared/runtime.ts +11 -0
- package/node_modules/assemblyscript/std/assembly/shared/target.ts +11 -0
- package/node_modules/assemblyscript/std/assembly/shared/tsconfig.json +11 -0
- package/node_modules/assemblyscript/std/assembly/shared/typeinfo.ts +72 -0
- package/node_modules/assemblyscript/std/assembly/staticarray.ts +401 -0
- package/node_modules/assemblyscript/std/assembly/string.ts +847 -0
- package/node_modules/assemblyscript/std/assembly/symbol.ts +114 -0
- package/node_modules/assemblyscript/std/assembly/table.ts +16 -0
- package/node_modules/assemblyscript/std/assembly/tsconfig.json +6 -0
- package/node_modules/assemblyscript/std/assembly/typedarray.ts +1945 -0
- package/node_modules/assemblyscript/std/assembly/uri.ts +17 -0
- package/node_modules/assemblyscript/std/assembly/util/bytes.ts +107 -0
- package/node_modules/assemblyscript/std/assembly/util/casemap.ts +497 -0
- package/node_modules/assemblyscript/std/assembly/util/error.ts +54 -0
- package/node_modules/assemblyscript/std/assembly/util/hash.ts +117 -0
- package/node_modules/assemblyscript/std/assembly/util/math.ts +1922 -0
- package/node_modules/assemblyscript/std/assembly/util/memory.ts +290 -0
- package/node_modules/assemblyscript/std/assembly/util/number.ts +873 -0
- package/node_modules/assemblyscript/std/assembly/util/sort.ts +313 -0
- package/node_modules/assemblyscript/std/assembly/util/string.ts +1202 -0
- package/node_modules/assemblyscript/std/assembly/util/uri.ts +275 -0
- package/node_modules/assemblyscript/std/assembly/vector.ts +4 -0
- package/node_modules/assemblyscript/std/assembly.json +16 -0
- package/node_modules/assemblyscript/std/portable/index.d.ts +462 -0
- package/node_modules/assemblyscript/std/portable/index.js +416 -0
- package/node_modules/assemblyscript/std/portable.json +11 -0
- package/node_modules/assemblyscript/std/types/assembly/index.d.ts +1 -0
- package/node_modules/assemblyscript/std/types/assembly/package.json +3 -0
- package/node_modules/assemblyscript/std/types/portable/index.d.ts +1 -0
- package/node_modules/assemblyscript/std/types/portable/package.json +3 -0
- package/node_modules/assemblyscript/tsconfig-base.json +13 -0
- package/package.json +112 -0
- package/runtime/deps/assemblyscript/LICENSE +201 -0
- package/runtime/deps/assemblyscript/NOTICE +93 -0
- package/runtime/deps/assemblyscript/bin/asc.js +35 -0
- package/runtime/deps/assemblyscript/dist/asc.js +25035 -0
- package/runtime/deps/assemblyscript/dist/assemblyscript.js +337 -0
- package/runtime/deps/assemblyscript/node_modules/binaryen/LICENSE +201 -0
- package/runtime/deps/assemblyscript/node_modules/binaryen/index.js +28905 -0
- package/runtime/deps/assemblyscript/node_modules/binaryen/package.json +50 -0
- package/runtime/deps/assemblyscript/node_modules/long/LICENSE +202 -0
- package/runtime/deps/assemblyscript/node_modules/long/index.js +1581 -0
- package/runtime/deps/assemblyscript/node_modules/long/package.json +58 -0
- package/runtime/deps/assemblyscript/package.json +114 -0
- package/runtime/host/amxts.inc +85 -0
- package/runtime/licenses/AMXX-LICENSE.txt +34 -0
- package/runtime/licenses/AssemblyScript-LICENSE +201 -0
- package/runtime/licenses/AssemblyScript-NOTICE +93 -0
- package/runtime/licenses/Binaryen-LICENSE +201 -0
- package/runtime/licenses/Bun-LICENSE.md +84 -0
- package/runtime/licenses/GPL-3.0.txt +674 -0
- package/runtime/licenses/LLVM-LICENSE.TXT +279 -0
- package/runtime/licenses/README.md +25 -0
- package/runtime/licenses/WAMR-LICENSE +219 -0
- package/runtime/natives.txt +1095 -0
- package/runtime/patches/README.md +59 -0
- package/runtime/patches/assemblyscript-0.28.20-amxts.patch +14174 -0
- package/runtime/patches/wamr-2.4.5-amxts.patch +364 -0
- package/scripts/apply-docs.ts +508 -0
- package/scripts/asc.ts +27 -0
- package/scripts/auto-imports.ts +258 -0
- package/scripts/build-wasm.ts +595 -0
- package/scripts/check.ts +93 -0
- package/scripts/compile-cache.ts +179 -0
- package/scripts/compile.ts +401 -0
- package/scripts/docker-server.ts +73 -0
- package/scripts/docs/as/amxts.ts +210 -0
- package/scripts/docs/as/effects.ts +1684 -0
- package/scripts/docs/as/facade.ts +3302 -0
- package/scripts/docs/as/fs.ts +173 -0
- package/scripts/docs/as/lib/check.ts +68 -0
- package/scripts/docs/as/modules/http.ts +66 -0
- package/scripts/docs/as/os.ts +16 -0
- package/scripts/docs/as/promise.ts +132 -0
- package/scripts/docs/as/promise.types.ts +63 -0
- package/scripts/docs/as/vector.ts +48 -0
- package/scripts/docs/entities.ts +2338 -0
- package/scripts/docs/events.ts +245 -0
- package/scripts/docs/game.ts +497 -0
- package/scripts/docs/messages.ts +148 -0
- package/scripts/file-contents.ts +74 -0
- package/scripts/includes.ts +134 -0
- package/scripts/player-fields.ts +866 -0
- package/scripts/plugin-cache.ts +92 -0
- package/scripts/plugin-natives.ts +1157 -0
- package/scripts/prebuilt.ts +268 -0
- package/scripts/prepare.ts +134 -0
- package/scripts/project.ts +973 -0
- package/scripts/run.ts +28 -0
- package/scripts/shared-modules.ts +906 -0
- package/scripts/system.ts +33 -0
- package/scripts/tracked-fs.ts +115 -0
- package/scripts/typed-configs.ts +788 -0
- package/scripts/ui.ts +77 -0
- package/src/cli-api.mjs +175 -0
- package/src/parser/include-parser.ts +506 -0
- package/src/system.mjs +155 -0
- package/src/testing/compile-cache.ts +31 -0
- package/src/testing/compile.ts +222 -0
- package/src/testing/coroutines.ts +191 -0
- package/src/testing/index.ts +39 -0
- package/src/testing/kits.ts +55 -0
- package/src/testing/memory.ts +125 -0
- package/src/testing/natives.ts +925 -0
- package/src/testing/server.ts +1874 -0
- package/src/testing/setup.ts +63 -0
- package/src/testing/tables.ts +139 -0
- package/src/types.ts +51 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { AL_MASK, OBJECT, OBJECT_OVERHEAD, BLOCK_MAXSIZE, BLOCK_OVERHEAD, BLOCK, OBJECT_MAXSIZE } from "./common";
|
|
2
|
+
import { E_ALLOCATION_TOO_LARGE } from "../util/error";
|
|
3
|
+
|
|
4
|
+
// === A minimal runtime stub ===
|
|
5
|
+
|
|
6
|
+
// @ts-ignore: decorator
|
|
7
|
+
@lazy let startOffset: usize = ((__heap_base + BLOCK_OVERHEAD + AL_MASK) & ~AL_MASK) - BLOCK_OVERHEAD;
|
|
8
|
+
// @ts-ignore: decorator
|
|
9
|
+
@lazy let offset: usize = startOffset;
|
|
10
|
+
|
|
11
|
+
function maybeGrowMemory(newOffset: usize): void {
|
|
12
|
+
// assumes newOffset is aligned
|
|
13
|
+
let pagesBefore = memory.size();
|
|
14
|
+
let maxOffset = ((<usize>pagesBefore << 16) + AL_MASK) & ~AL_MASK;
|
|
15
|
+
if (newOffset > maxOffset) {
|
|
16
|
+
let pagesNeeded = <i32>(((newOffset - maxOffset + 0xffff) & ~0xffff) >>> 16);
|
|
17
|
+
let pagesWanted = max(pagesBefore, pagesNeeded); // double memory
|
|
18
|
+
if (memory.grow(pagesWanted) < 0) {
|
|
19
|
+
if (memory.grow(pagesNeeded) < 0) unreachable(); // out of memory
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
offset = newOffset;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// @ts-ignore: decorator
|
|
26
|
+
@inline function computeSize(size: usize): usize {
|
|
27
|
+
return ((size + BLOCK_OVERHEAD + AL_MASK) & ~AL_MASK) - BLOCK_OVERHEAD;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// @ts-ignore: decorator
|
|
31
|
+
@unsafe @global
|
|
32
|
+
export function __alloc(size: usize): usize {
|
|
33
|
+
if (size > BLOCK_MAXSIZE) throw new Error(E_ALLOCATION_TOO_LARGE);
|
|
34
|
+
let block = changetype<BLOCK>(offset);
|
|
35
|
+
let ptr = offset + BLOCK_OVERHEAD;
|
|
36
|
+
let payloadSize = computeSize(size);
|
|
37
|
+
maybeGrowMemory(ptr + payloadSize);
|
|
38
|
+
block.mmInfo = payloadSize;
|
|
39
|
+
return ptr;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// @ts-ignore: decorator
|
|
43
|
+
@unsafe @global
|
|
44
|
+
export function __realloc(ptr: usize, size: usize): usize {
|
|
45
|
+
assert(ptr != 0 && !(ptr & AL_MASK)); // must exist and be aligned
|
|
46
|
+
let block = changetype<BLOCK>(ptr - BLOCK_OVERHEAD);
|
|
47
|
+
let actualSize = block.mmInfo;
|
|
48
|
+
let isLast = ptr + actualSize == offset;
|
|
49
|
+
let payloadSize = computeSize(size);
|
|
50
|
+
if (size > actualSize) {
|
|
51
|
+
if (isLast) { // last block: grow
|
|
52
|
+
if (size > BLOCK_MAXSIZE) throw new Error(E_ALLOCATION_TOO_LARGE);
|
|
53
|
+
maybeGrowMemory(ptr + payloadSize);
|
|
54
|
+
block.mmInfo = payloadSize;
|
|
55
|
+
} else { // copy to new block at least double the size
|
|
56
|
+
let newPtr = __alloc(max<usize>(payloadSize, actualSize << 1));
|
|
57
|
+
memory.copy(newPtr, ptr, actualSize);
|
|
58
|
+
block = changetype<BLOCK>((ptr = newPtr) - BLOCK_OVERHEAD);
|
|
59
|
+
}
|
|
60
|
+
} else if (isLast) { // last block: shrink
|
|
61
|
+
offset = ptr + payloadSize;
|
|
62
|
+
block.mmInfo = payloadSize;
|
|
63
|
+
}
|
|
64
|
+
return ptr;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// @ts-ignore: decorator
|
|
68
|
+
@unsafe @global
|
|
69
|
+
export function __free(ptr: usize): void {
|
|
70
|
+
assert(ptr != 0 && !(ptr & AL_MASK)); // must exist and be aligned
|
|
71
|
+
let block = changetype<BLOCK>(ptr - BLOCK_OVERHEAD);
|
|
72
|
+
if (ptr + block.mmInfo == offset) { // last block: discard
|
|
73
|
+
offset = changetype<usize>(block);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// @ts-ignore: decorator
|
|
78
|
+
@unsafe @global
|
|
79
|
+
export function __reset(): void { // special
|
|
80
|
+
offset = startOffset;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// @ts-ignore: decorator
|
|
84
|
+
@unsafe @global
|
|
85
|
+
export function __new(size: usize, id: u32): usize {
|
|
86
|
+
if (size > OBJECT_MAXSIZE) throw new Error(E_ALLOCATION_TOO_LARGE);
|
|
87
|
+
let ptr = __alloc(OBJECT_OVERHEAD + size);
|
|
88
|
+
let object = changetype<OBJECT>(ptr - BLOCK_OVERHEAD);
|
|
89
|
+
object.gcInfo = 0;
|
|
90
|
+
object.gcInfo2 = 0;
|
|
91
|
+
object.rtId = id;
|
|
92
|
+
object.rtSize = <u32>size;
|
|
93
|
+
return ptr + OBJECT_OVERHEAD;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// @ts-ignore: decorator
|
|
97
|
+
@unsafe @global
|
|
98
|
+
export function __renew(oldPtr: usize, size: usize): usize {
|
|
99
|
+
if (size > OBJECT_MAXSIZE) throw new Error(E_ALLOCATION_TOO_LARGE);
|
|
100
|
+
let newPtr = __realloc(oldPtr - OBJECT_OVERHEAD, OBJECT_OVERHEAD + size);
|
|
101
|
+
changetype<OBJECT>(newPtr - BLOCK_OVERHEAD).rtSize = <u32>size;
|
|
102
|
+
return newPtr + OBJECT_OVERHEAD;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// @ts-ignore: decorator
|
|
106
|
+
@global @unsafe
|
|
107
|
+
export function __link(parentPtr: usize, childPtr: usize, expectMultiple: bool): void {
|
|
108
|
+
// nop
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// @ts-ignore: decorator
|
|
112
|
+
@global @unsafe
|
|
113
|
+
export function __pin(ptr: usize): usize {
|
|
114
|
+
return ptr;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// @ts-ignore: decorator
|
|
118
|
+
@global @unsafe
|
|
119
|
+
export function __unpin(ptr: usize): void {
|
|
120
|
+
// nop
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// @ts-ignore: decorator
|
|
124
|
+
@global @unsafe
|
|
125
|
+
function __visit(ptr: usize, cookie: u32): void { // eslint-disable-line @typescript-eslint/no-unused-vars
|
|
126
|
+
// nop
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// @ts-ignore: decorator
|
|
130
|
+
@global @unsafe
|
|
131
|
+
export function __collect(): void {
|
|
132
|
+
// nop
|
|
133
|
+
}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import { BLOCK, BLOCK_OVERHEAD, OBJECT_OVERHEAD, OBJECT_MAXSIZE, TOTAL_OVERHEAD, DEBUG, TRACE, RTRACE } from "./common";
|
|
2
|
+
import { onvisit, oncollect } from "./rtrace";
|
|
3
|
+
import { E_ALLOCATION_TOO_LARGE, E_ALREADY_PINNED, E_NOT_PINNED } from "../util/error";
|
|
4
|
+
|
|
5
|
+
// === TCMS: A Two-Color Mark & Sweep garbage collector ===
|
|
6
|
+
|
|
7
|
+
// ╒═════════════╤══════════════ Colors ═══════════════════════════╕
|
|
8
|
+
// │ Color │ Meaning │
|
|
9
|
+
// ├─────────────┼─────────────────────────────────────────────────┤
|
|
10
|
+
// │ WHITE* │ Unreachable │
|
|
11
|
+
// │ BLACK* │ Reachable │
|
|
12
|
+
// │ TRANSPARENT │ Manually pinned (always reachable) │
|
|
13
|
+
// └─────────────┴─────────────────────────────────────────────────┘
|
|
14
|
+
// * flipped between cycles
|
|
15
|
+
|
|
16
|
+
// @ts-ignore: decorator
|
|
17
|
+
@lazy let white = 0;
|
|
18
|
+
// @ts-ignore: decorator
|
|
19
|
+
@inline const transparent = 3;
|
|
20
|
+
// @ts-ignore: decorator
|
|
21
|
+
@inline const COLOR_MASK = 3;
|
|
22
|
+
|
|
23
|
+
/** Size in memory of all objects currently managed by the GC. */
|
|
24
|
+
// @ts-ignore: decorator
|
|
25
|
+
@lazy let total: usize = 0;
|
|
26
|
+
|
|
27
|
+
// @ts-ignore: decorator
|
|
28
|
+
@lazy let fromSpace = initLazy(changetype<Object>(memory.data(offsetof<Object>())));
|
|
29
|
+
// @ts-ignore: decorator
|
|
30
|
+
@lazy let toSpace = initLazy(changetype<Object>(memory.data(offsetof<Object>())));
|
|
31
|
+
// @ts-ignore: decorator
|
|
32
|
+
@lazy let pinSpace = initLazy(changetype<Object>(memory.data(offsetof<Object>())));
|
|
33
|
+
|
|
34
|
+
function initLazy(space: Object): Object {
|
|
35
|
+
space.nextWithColor = changetype<usize>(space);
|
|
36
|
+
space.prev = space;
|
|
37
|
+
return space;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Visit cookie indicating scanning of an object. */
|
|
41
|
+
// @ts-ignore: decorator
|
|
42
|
+
@inline const VISIT_SCAN = 0;
|
|
43
|
+
|
|
44
|
+
// ╒═══════════════ Managed object layout (32-bit) ════════════════╕
|
|
45
|
+
// 3 2 1
|
|
46
|
+
// 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
|
|
47
|
+
// ├─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┤
|
|
48
|
+
// │ Memory manager block │
|
|
49
|
+
// ╞═══════════════════════════════════════════════════════════╤═══╡
|
|
50
|
+
// │ next │ C │ = nextWithColor
|
|
51
|
+
// ├───────────────────────────────────────────────────────────┴───┤
|
|
52
|
+
// │ prev │
|
|
53
|
+
// ├───────────────────────────────────────────────────────────────┤
|
|
54
|
+
// │ rtId │
|
|
55
|
+
// ├───────────────────────────────────────────────────────────────┤
|
|
56
|
+
// │ rtSize │
|
|
57
|
+
// ╞>ptr═══════════════════════════════════════════════════════════╡
|
|
58
|
+
// │ ... │
|
|
59
|
+
// C: color
|
|
60
|
+
|
|
61
|
+
/** Represents a managed object in memory, consisting of a header followed by the object's data. */
|
|
62
|
+
@unmanaged class Object extends BLOCK {
|
|
63
|
+
/** Pointer to the next object with color flags stored in the alignment bits. */
|
|
64
|
+
nextWithColor: usize; // *u32
|
|
65
|
+
/** Pointer to the previous object. */
|
|
66
|
+
prev: Object; // *u32
|
|
67
|
+
/** Runtime id. */
|
|
68
|
+
rtId: u32;
|
|
69
|
+
/** Runtime size. */
|
|
70
|
+
rtSize: u32;
|
|
71
|
+
|
|
72
|
+
/** Gets the pointer to the next object. */
|
|
73
|
+
get next(): Object {
|
|
74
|
+
return changetype<Object>(this.nextWithColor & ~COLOR_MASK);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Sets the pointer to the next object. */
|
|
78
|
+
set next(obj: Object) {
|
|
79
|
+
this.nextWithColor = changetype<usize>(obj) | (this.nextWithColor & COLOR_MASK);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Gets this object's color. */
|
|
83
|
+
get color(): i32 {
|
|
84
|
+
return i32(this.nextWithColor & COLOR_MASK);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Sets this object's color. */
|
|
88
|
+
set color(color: i32) {
|
|
89
|
+
this.nextWithColor = (this.nextWithColor & ~COLOR_MASK) | color;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Gets the size of this object in memory. */
|
|
93
|
+
get size(): usize {
|
|
94
|
+
return BLOCK_OVERHEAD + (this.mmInfo & ~3);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Unlinks this object from its list. */
|
|
98
|
+
unlink(): void {
|
|
99
|
+
let next = this.next;
|
|
100
|
+
if (next == null) {
|
|
101
|
+
if (DEBUG) assert(this.prev == null && changetype<usize>(this) < __heap_base);
|
|
102
|
+
return; // static data not yet linked
|
|
103
|
+
}
|
|
104
|
+
let prev = this.prev;
|
|
105
|
+
if (DEBUG) assert(prev);
|
|
106
|
+
next.prev = prev;
|
|
107
|
+
prev.next = next;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Links this object to the specified list, with the given color. */
|
|
111
|
+
linkTo(list: Object, withColor: i32): void {
|
|
112
|
+
let prev = list.prev;
|
|
113
|
+
this.nextWithColor = changetype<usize>(list) | withColor;
|
|
114
|
+
this.prev = prev;
|
|
115
|
+
prev.next = this;
|
|
116
|
+
list.prev = this;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Garbage collector interface
|
|
121
|
+
|
|
122
|
+
// @ts-ignore: decorator
|
|
123
|
+
@global @unsafe
|
|
124
|
+
export function __new(size: usize, id: i32): usize {
|
|
125
|
+
if (size > OBJECT_MAXSIZE) throw new Error(E_ALLOCATION_TOO_LARGE);
|
|
126
|
+
let obj = changetype<Object>(__alloc(OBJECT_OVERHEAD + size) - BLOCK_OVERHEAD);
|
|
127
|
+
obj.rtId = id;
|
|
128
|
+
obj.rtSize = <u32>size;
|
|
129
|
+
obj.linkTo(fromSpace, white);
|
|
130
|
+
total += obj.size;
|
|
131
|
+
return changetype<usize>(obj) + TOTAL_OVERHEAD;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// @ts-ignore: decorator
|
|
135
|
+
@global @unsafe
|
|
136
|
+
export function __renew(oldPtr: usize, size: usize): usize {
|
|
137
|
+
let oldObj = changetype<Object>(oldPtr - TOTAL_OVERHEAD);
|
|
138
|
+
if (oldPtr < __heap_base) { // move to heap for simplicity
|
|
139
|
+
let newPtr = __new(size, oldObj.rtId);
|
|
140
|
+
memory.copy(newPtr, oldPtr, min(size, oldObj.rtSize));
|
|
141
|
+
return newPtr;
|
|
142
|
+
}
|
|
143
|
+
if (size > OBJECT_MAXSIZE) throw new Error(E_ALLOCATION_TOO_LARGE);
|
|
144
|
+
total -= oldObj.size;
|
|
145
|
+
let newPtr = __realloc(oldPtr - OBJECT_OVERHEAD, OBJECT_OVERHEAD + size) + OBJECT_OVERHEAD;
|
|
146
|
+
let newObj = changetype<Object>(newPtr - TOTAL_OVERHEAD);
|
|
147
|
+
newObj.rtSize = <u32>size;
|
|
148
|
+
|
|
149
|
+
// Replace with new object
|
|
150
|
+
newObj.next.prev = newObj;
|
|
151
|
+
newObj.prev.next = newObj;
|
|
152
|
+
|
|
153
|
+
total += newObj.size;
|
|
154
|
+
return newPtr;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// @ts-ignore: decorator
|
|
158
|
+
@global @unsafe
|
|
159
|
+
export function __link(parentPtr: usize, childPtr: usize, expectMultiple: bool): void {
|
|
160
|
+
// nop
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// @ts-ignore: decorator
|
|
164
|
+
@global @unsafe
|
|
165
|
+
export function __visit(ptr: usize, cookie: i32): void {
|
|
166
|
+
if (!ptr) return;
|
|
167
|
+
let obj = changetype<Object>(ptr - TOTAL_OVERHEAD);
|
|
168
|
+
if (RTRACE) if (!onvisit(obj)) return;
|
|
169
|
+
if (obj.color == white) {
|
|
170
|
+
obj.unlink(); // from fromSpace
|
|
171
|
+
obj.linkTo(toSpace, i32(!white));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// @ts-ignore: decorator
|
|
176
|
+
@global @unsafe
|
|
177
|
+
export function __pin(ptr: usize): usize {
|
|
178
|
+
if (ptr) {
|
|
179
|
+
let obj = changetype<Object>(ptr - TOTAL_OVERHEAD);
|
|
180
|
+
if (obj.color == transparent) {
|
|
181
|
+
throw new Error(E_ALREADY_PINNED);
|
|
182
|
+
}
|
|
183
|
+
obj.unlink(); // from fromSpace
|
|
184
|
+
obj.linkTo(pinSpace, transparent);
|
|
185
|
+
}
|
|
186
|
+
return ptr;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// @ts-ignore: decorator
|
|
190
|
+
@global @unsafe
|
|
191
|
+
export function __unpin(ptr: usize): void {
|
|
192
|
+
if (!ptr) return;
|
|
193
|
+
let obj = changetype<Object>(ptr - TOTAL_OVERHEAD);
|
|
194
|
+
if (obj.color != transparent) {
|
|
195
|
+
throw new Error(E_NOT_PINNED);
|
|
196
|
+
}
|
|
197
|
+
obj.unlink(); // from pinSpace
|
|
198
|
+
obj.linkTo(fromSpace, white);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// @ts-ignore: decorator
|
|
202
|
+
@global @unsafe
|
|
203
|
+
export function __collect(): void {
|
|
204
|
+
if (TRACE) trace("GC at", 1, total);
|
|
205
|
+
|
|
206
|
+
// Mark roots (add to toSpace)
|
|
207
|
+
__visit_globals(VISIT_SCAN);
|
|
208
|
+
|
|
209
|
+
// Mark direct members of pinned objects (add to toSpace)
|
|
210
|
+
let pn = pinSpace;
|
|
211
|
+
let iter = pn.next;
|
|
212
|
+
while (iter != pn) {
|
|
213
|
+
if (DEBUG) assert(iter.color == transparent);
|
|
214
|
+
__visit_members(changetype<usize>(iter) + TOTAL_OVERHEAD, VISIT_SCAN);
|
|
215
|
+
iter = iter.next;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Mark what's reachable from toSpace
|
|
219
|
+
let black = i32(!white);
|
|
220
|
+
let to = toSpace;
|
|
221
|
+
iter = to.next;
|
|
222
|
+
while (iter != to) {
|
|
223
|
+
if (DEBUG) assert(iter.color == black);
|
|
224
|
+
__visit_members(changetype<usize>(iter) + TOTAL_OVERHEAD, VISIT_SCAN);
|
|
225
|
+
iter = iter.next;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// Sweep what's left in fromSpace
|
|
229
|
+
let from = fromSpace;
|
|
230
|
+
iter = from.next;
|
|
231
|
+
while (iter != from) {
|
|
232
|
+
if (DEBUG) assert(iter.color == white);
|
|
233
|
+
let newNext = iter.next;
|
|
234
|
+
if (changetype<usize>(iter) < __heap_base) {
|
|
235
|
+
iter.nextWithColor = 0; // may become linked again
|
|
236
|
+
iter.prev = changetype<Object>(0);
|
|
237
|
+
} else {
|
|
238
|
+
total -= iter.size;
|
|
239
|
+
if (isDefined(__finalize)) __finalize(changetype<usize>(iter) + TOTAL_OVERHEAD);
|
|
240
|
+
__free(changetype<usize>(iter) + BLOCK_OVERHEAD);
|
|
241
|
+
}
|
|
242
|
+
iter = newNext;
|
|
243
|
+
}
|
|
244
|
+
from.nextWithColor = changetype<usize>(from);
|
|
245
|
+
from.prev = from;
|
|
246
|
+
|
|
247
|
+
// Flip spaces and colors
|
|
248
|
+
fromSpace = to;
|
|
249
|
+
toSpace = from;
|
|
250
|
+
white = black;
|
|
251
|
+
|
|
252
|
+
if (TRACE) trace("GC done at", 1, total);
|
|
253
|
+
if (RTRACE) oncollect(total);
|
|
254
|
+
}
|