@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,2 @@
1
+ export type ReturnType<T> = returnof<T>;
2
+ export type NonNullable<T> = nonnull<T>;
@@ -0,0 +1,42 @@
1
+ import {
2
+ console as binding
3
+ } from "./bindings/dom";
4
+
5
+ export namespace console {
6
+
7
+ export function assert<T>(condition: T, message: string = ""): void {
8
+ binding.assert(!!condition, message);
9
+ }
10
+
11
+ export function log(message: string = ""): void {
12
+ binding.log(message);
13
+ }
14
+
15
+ export function debug(message: string = ""): void {
16
+ binding.debug(message);
17
+ }
18
+
19
+ export function info(message: string = ""): void {
20
+ binding.info(message);
21
+ }
22
+
23
+ export function warn(message: string = ""): void {
24
+ binding.warn(message);
25
+ }
26
+
27
+ export function error(message: string = ""): void {
28
+ binding.error(message);
29
+ }
30
+
31
+ export function time(label: string = "default"): void {
32
+ binding.time(label);
33
+ }
34
+
35
+ export function timeLog(label: string = "default"): void {
36
+ binding.timeLog(label);
37
+ }
38
+
39
+ export function timeEnd(label: string = "default"): void {
40
+ binding.timeEnd(label);
41
+ }
42
+ }
@@ -0,0 +1,9 @@
1
+ import {
2
+ crypto as crypto_binding
3
+ } from "bindings/dom";
4
+
5
+ export namespace crypto {
6
+ export function getRandomValues(array: Uint8Array): void {
7
+ crypto_binding.getRandomValues(array);
8
+ }
9
+ }
@@ -0,0 +1,181 @@
1
+ import { BLOCK_MAXSIZE } from "./rt/common";
2
+ import { ArrayBuffer } from "./arraybuffer";
3
+ import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH } from "./util/error";
4
+
5
+ // TODO: there is probably a smarter way to check byteOffset for accesses larger than 1 byte
6
+
7
+ export class DataView {
8
+
9
+ readonly buffer: ArrayBuffer;
10
+ @unsafe readonly dataStart: usize;
11
+ readonly byteLength: i32;
12
+
13
+ get byteOffset(): i32 {
14
+ return <i32>(this.dataStart - changetype<usize>(this.buffer));
15
+ }
16
+
17
+ constructor(
18
+ buffer: ArrayBuffer,
19
+ byteOffset: i32 = 0,
20
+ byteLength: i32 = buffer.byteLength
21
+ ) {
22
+ if (
23
+ i32(<u32>byteLength > <u32>BLOCK_MAXSIZE) |
24
+ i32(<u32>byteOffset + byteLength > <u32>buffer.byteLength)
25
+ ) throw new RangeError(E_INVALIDLENGTH);
26
+ this.buffer = buffer; // links
27
+ let dataStart = changetype<usize>(buffer) + <usize>byteOffset;
28
+ this.dataStart = dataStart;
29
+ this.byteLength = byteLength;
30
+ }
31
+
32
+ getFloat32(byteOffset: i32, littleEndian: bool = false): f32 {
33
+ if (
34
+ (byteOffset >>> 31) | i32(byteOffset + 4 > this.byteLength)
35
+ ) throw new RangeError(E_INDEXOUTOFRANGE);
36
+ return littleEndian
37
+ ? load<f32>(this.dataStart + <usize>byteOffset)
38
+ : reinterpret<f32>(bswap<u32>(load<u32>(this.dataStart + <usize>byteOffset)));
39
+ }
40
+
41
+ getFloat64(byteOffset: i32, littleEndian: bool = false): f64 {
42
+ if (
43
+ (byteOffset >>> 31) | i32(byteOffset + 8 > this.byteLength)
44
+ ) throw new RangeError(E_INDEXOUTOFRANGE);
45
+ return littleEndian
46
+ ? load<f64>(this.dataStart + <usize>byteOffset)
47
+ : reinterpret<f64>(bswap<u64>(load<u64>(this.dataStart + <usize>byteOffset)));
48
+ }
49
+
50
+ getInt8(byteOffset: i32): i8 {
51
+ if (<u32>byteOffset >= <u32>this.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
52
+ return load<i8>(this.dataStart + <usize>byteOffset);
53
+ }
54
+
55
+ getInt16(byteOffset: i32, littleEndian: bool = false): i16 {
56
+ if (
57
+ (byteOffset >>> 31) | i32(byteOffset + 2 > this.byteLength)
58
+ ) throw new RangeError(E_INDEXOUTOFRANGE);
59
+ let result: i16 = load<i16>(this.dataStart + <usize>byteOffset);
60
+ return littleEndian ? result : bswap<u16>(result);
61
+ }
62
+
63
+ getInt32(byteOffset: i32, littleEndian: bool = false): i32 {
64
+ if (
65
+ (byteOffset >>> 31) | i32(byteOffset + 4 > this.byteLength)
66
+ ) throw new RangeError(E_INDEXOUTOFRANGE);
67
+ let result: i32 = load<i32>(this.dataStart + <usize>byteOffset);
68
+ return littleEndian ? result : bswap<u32>(result);
69
+ }
70
+
71
+ getUint8(byteOffset: i32): u8 {
72
+ if (<u32>byteOffset >= <u32>this.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
73
+ return load<u8>(this.dataStart + <usize>byteOffset);
74
+ }
75
+
76
+ getUint16(byteOffset: i32, littleEndian: bool = false): u16 {
77
+ if (
78
+ (byteOffset >>> 31) | i32(byteOffset + 2 > this.byteLength)
79
+ ) throw new RangeError(E_INDEXOUTOFRANGE);
80
+ let result: u16 = load<u16>(this.dataStart + <usize>byteOffset);
81
+ return littleEndian ? result : bswap<u16>(result);
82
+ }
83
+
84
+ getUint32(byteOffset: i32, littleEndian: bool = false): u32 {
85
+ if (
86
+ (byteOffset >>> 31) | i32(byteOffset + 4 > this.byteLength)
87
+ ) throw new RangeError(E_INDEXOUTOFRANGE);
88
+ let result: u32 = load<u32>(this.dataStart + <usize>byteOffset);
89
+ return littleEndian ? result : bswap<u32>(result);
90
+ }
91
+
92
+ setFloat32(byteOffset: i32, value: f32, littleEndian: bool = false): void {
93
+ if (
94
+ (byteOffset >>> 31) | i32(byteOffset + 4 > this.byteLength)
95
+ ) throw new RangeError(E_INDEXOUTOFRANGE);
96
+ if (littleEndian) store<f32>(this.dataStart + <usize>byteOffset, value);
97
+ else store<u32>(this.dataStart + <usize>byteOffset, bswap<u32>(reinterpret<u32>(value)));
98
+ }
99
+
100
+ setFloat64(byteOffset: i32, value: f64, littleEndian: bool = false): void {
101
+ if (
102
+ (byteOffset >>> 31) | i32(byteOffset + 8 > this.byteLength)
103
+ ) throw new RangeError(E_INDEXOUTOFRANGE);
104
+ if (littleEndian) store<f64>(this.dataStart + <usize>byteOffset, value);
105
+ else store<u64>(this.dataStart + <usize>byteOffset, bswap<u64>(reinterpret<u64>(value)));
106
+ }
107
+
108
+ setInt8(byteOffset: i32, value: i8): void {
109
+ if (<u32>byteOffset >= <u32>this.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
110
+ store<i8>(this.dataStart + <usize>byteOffset, value);
111
+ }
112
+
113
+ setInt16(byteOffset: i32, value: i16, littleEndian: bool = false): void {
114
+ if (
115
+ (byteOffset >>> 31) | i32(byteOffset + 2 > this.byteLength)
116
+ ) throw new RangeError(E_INDEXOUTOFRANGE);
117
+ store<i16>(this.dataStart + <usize>byteOffset, littleEndian ? value : bswap<u16>(value));
118
+ }
119
+
120
+ setInt32(byteOffset: i32, value: i32, littleEndian: bool = false): void {
121
+ if (
122
+ (byteOffset >>> 31) | i32(byteOffset + 4 > this.byteLength)
123
+ ) throw new RangeError(E_INDEXOUTOFRANGE);
124
+ store<i32>(this.dataStart + <usize>byteOffset, littleEndian ? value : bswap<u32>(value));
125
+ }
126
+
127
+ setUint8(byteOffset: i32, value: u8): void {
128
+ if (<u32>byteOffset >= <u32>this.byteLength) throw new RangeError(E_INDEXOUTOFRANGE);
129
+ store<u8>(this.dataStart + <usize>byteOffset, value);
130
+ }
131
+
132
+ setUint16(byteOffset: i32, value: u16, littleEndian: bool = false): void {
133
+ if (
134
+ (byteOffset >>> 31) | i32(byteOffset + 2 > this.byteLength)
135
+ ) throw new RangeError(E_INDEXOUTOFRANGE);
136
+ store<u16>(this.dataStart + <usize>byteOffset, littleEndian ? value : bswap<u16>(value));
137
+ }
138
+
139
+ setUint32(byteOffset: i32, value: u32, littleEndian: bool = false): void {
140
+ if (
141
+ (byteOffset >>> 31) | i32(byteOffset + 4 > this.byteLength)
142
+ ) throw new RangeError(E_INDEXOUTOFRANGE);
143
+ store<u32>(this.dataStart + <usize>byteOffset, littleEndian ? value : bswap<u32>(value));
144
+ }
145
+
146
+ // Non-standard additions that make sense in WebAssembly, but won't work in JS:
147
+
148
+ getInt64(byteOffset: i32, littleEndian: bool = false): i64 {
149
+ if (
150
+ (byteOffset >>> 31) | i32(byteOffset + 8 > this.byteLength)
151
+ ) throw new RangeError(E_INDEXOUTOFRANGE);
152
+ let result: i64 = load<i64>(this.dataStart + <usize>byteOffset);
153
+ return littleEndian ? result : bswap<u64>(result);
154
+ }
155
+
156
+ getUint64(byteOffset: i32, littleEndian: bool = false): u64 {
157
+ if (
158
+ (byteOffset >>> 31) | i32(byteOffset + 8 > this.byteLength)
159
+ ) throw new RangeError(E_INDEXOUTOFRANGE);
160
+ let result = load<u64>(this.dataStart + <usize>byteOffset);
161
+ return littleEndian ? result : bswap<u64>(result);
162
+ }
163
+
164
+ setInt64(byteOffset: i32, value: i64, littleEndian: bool = false): void {
165
+ if (
166
+ (byteOffset >>> 31) | i32(byteOffset + 8 > this.byteLength)
167
+ ) throw new RangeError(E_INDEXOUTOFRANGE);
168
+ store<i64>(this.dataStart + <usize>byteOffset, littleEndian ? value : bswap<u64>(value));
169
+ }
170
+
171
+ setUint64(byteOffset: i32, value: u64, littleEndian: bool = false): void {
172
+ if (
173
+ (byteOffset >>> 31) | i32(byteOffset + 8 > this.byteLength)
174
+ ) throw new RangeError(E_INDEXOUTOFRANGE);
175
+ store<u64>(this.dataStart + <usize>byteOffset, littleEndian ? value : bswap<u64>(value));
176
+ }
177
+
178
+ toString(): string {
179
+ return "[object DataView]";
180
+ }
181
+ }
@@ -0,0 +1,373 @@
1
+ import { E_INVALIDDATE } from "util/error";
2
+ import { Date as Date_binding } from "./bindings/dom";
3
+
4
+ // @ts-ignore: decorator
5
+ @inline const
6
+ MILLIS_LIMIT = 8640000000000000,
7
+ MILLIS_PER_DAY = 1000 * 60 * 60 * 24,
8
+ MILLIS_PER_HOUR = 1000 * 60 * 60,
9
+ MILLIS_PER_MINUTE = 1000 * 60,
10
+ MILLIS_PER_SECOND = 1000,
11
+ YEARS_PER_EPOCH = 400,
12
+ DAYS_PER_EPOCH = 146097, // 400 * 365 + 400 / 4 - 400 / 100 + 1
13
+ EPOCH_OFFSET = 719468; // Jan 1, 1970
14
+
15
+ // ymdFromEpochDays returns values via globals to avoid allocations
16
+ // @ts-ignore: decorator
17
+ @lazy let _month: i32, _day: i32;
18
+
19
+ export class Date {
20
+ private year: i32 = 0;
21
+ private month: i32 = 0;
22
+ private day: i32 = 0;
23
+
24
+ @inline static UTC(
25
+ year: i32,
26
+ month: i32 = 0,
27
+ day: i32 = 1,
28
+ hour: i32 = 0,
29
+ minute: i32 = 0,
30
+ second: i32 = 0,
31
+ millisecond: i32 = 0
32
+ ): i64 {
33
+ if (year >= 0 && year <= 99) year += 1900;
34
+ let ms = epochMillis(year, month + 1, day, hour, minute, second, millisecond);
35
+ if (invalidDate(ms)) throw new RangeError(E_INVALIDDATE);
36
+ return ms;
37
+ }
38
+
39
+ @inline static now(): i64 {
40
+ return <i64>Date_binding.now();
41
+ }
42
+
43
+ // It can parse only ISO 8601 inputs like YYYY-MM-DDTHH:MM:SS.000Z
44
+ @inline static parse(dateString: string): Date {
45
+ return this.fromString(dateString);
46
+ }
47
+
48
+ static fromString(dateTimeString: string): Date {
49
+ if (!dateTimeString.length) throw new RangeError(E_INVALIDDATE);
50
+ var
51
+ hour: i32 = 0,
52
+ min: i32 = 0,
53
+ sec: i32 = 0,
54
+ ms: i32 = 0,
55
+ offsetMs: i32 = 0;
56
+
57
+ let dateString = dateTimeString;
58
+ let posT = dateTimeString.indexOf("T");
59
+ if (~posT) {
60
+ // includes a time component
61
+ let timeString: string;
62
+ dateString = dateTimeString.substring(0, posT);
63
+ timeString = dateTimeString.substring(posT + 1);
64
+
65
+ // might end with an offset ("Z", "+05:30", "-08:00", etc.)
66
+ for (let i = timeString.length - 1; i >= 0; i--) {
67
+ let c = timeString.charCodeAt(i);
68
+ if (c == 90) { // Z
69
+ timeString = timeString.substring(0, i);
70
+ break;
71
+ } else if (c == 43 || c == 45) { // + or -
72
+ if (i == timeString.length - 1) {
73
+ throw new RangeError(E_INVALIDDATE);
74
+ }
75
+
76
+ let posColon = timeString.indexOf(":", i + 1);
77
+ if (~posColon) {
78
+ let offsetHours = i32.parse(timeString.substring(i + 1, posColon));
79
+ let offsetMinutes = i32.parse(timeString.substring(posColon + 1));
80
+ offsetMs = (offsetHours * 60 + offsetMinutes) * MILLIS_PER_MINUTE;
81
+ } else {
82
+ let offsetHours = i32.parse(timeString.substring(i + 1));
83
+ offsetMs = offsetHours * MILLIS_PER_HOUR;
84
+ }
85
+
86
+ if (c == 45) offsetMs = -offsetMs; // negative offset
87
+ timeString = timeString.substring(0, i);
88
+ break;
89
+ }
90
+ }
91
+
92
+ // parse the HH:MM:SS component
93
+ let timeParts = timeString.split(":");
94
+ let len = timeParts.length;
95
+ if (len <= 1) throw new RangeError(E_INVALIDDATE);
96
+
97
+ hour = i32.parse(timeParts[0]);
98
+ min = i32.parse(timeParts[1]);
99
+ if (len >= 3) {
100
+ let secAndFrac = timeParts[2];
101
+ let posDot = secAndFrac.indexOf(".");
102
+ if (~posDot) {
103
+ // includes fractional seconds (truncate to milliseconds)
104
+ sec = i32.parse(secAndFrac.substring(0, posDot));
105
+ ms = i32.parse(secAndFrac.substr(posDot + 1, 3).padEnd(3, "0"));
106
+ } else {
107
+ sec = i32.parse(secAndFrac);
108
+ }
109
+ }
110
+ }
111
+
112
+ // parse the YYYY-MM-DD component
113
+ let parts = dateString.split("-");
114
+ let year = i32.parse(parts[0]);
115
+ let month = 1, day = 1;
116
+ let len = parts.length;
117
+ if (len >= 2) {
118
+ month = i32.parse(parts[1]);
119
+ if (len >= 3) {
120
+ day = i32.parse(parts[2]);
121
+ }
122
+ }
123
+
124
+ return new Date(epochMillis(year, month, day, hour, min, sec, ms) - offsetMs);
125
+ }
126
+
127
+ constructor(private epochMillis: i64) {
128
+ // this differs from JavaScript which prefer return NaN or "Invalid Date" string
129
+ // instead throwing exception.
130
+ if (invalidDate(epochMillis)) throw new RangeError(E_INVALIDDATE);
131
+
132
+ this.year = dateFromEpoch(epochMillis);
133
+ this.month = _month;
134
+ this.day = _day;
135
+ }
136
+
137
+ @inline getTime(): i64 {
138
+ return this.epochMillis;
139
+ }
140
+
141
+ setTime(time: i64): i64 {
142
+ if (invalidDate(time)) throw new RangeError(E_INVALIDDATE);
143
+
144
+ this.epochMillis = time;
145
+ this.year = dateFromEpoch(time);
146
+ this.month = _month;
147
+ this.day = _day;
148
+
149
+ return time;
150
+ }
151
+
152
+ @inline getUTCFullYear(): i32 {
153
+ return this.year;
154
+ }
155
+
156
+ @inline getUTCMonth(): i32 {
157
+ return this.month - 1;
158
+ }
159
+
160
+ @inline getUTCDate(): i32 {
161
+ return this.day;
162
+ }
163
+
164
+ @inline getUTCDay(): i32 {
165
+ return dayOfWeek(this.year, this.month, this.day);
166
+ }
167
+
168
+ getUTCHours(): i32 {
169
+ return i32(euclidRem(this.epochMillis, MILLIS_PER_DAY)) / MILLIS_PER_HOUR;
170
+ }
171
+
172
+ getUTCMinutes(): i32 {
173
+ return i32(euclidRem(this.epochMillis, MILLIS_PER_HOUR)) / MILLIS_PER_MINUTE;
174
+ }
175
+
176
+ getUTCSeconds(): i32 {
177
+ return i32(euclidRem(this.epochMillis, MILLIS_PER_MINUTE)) / MILLIS_PER_SECOND;
178
+ }
179
+
180
+ getUTCMilliseconds(): i32 {
181
+ return i32(euclidRem(this.epochMillis, MILLIS_PER_SECOND));
182
+ }
183
+
184
+ setUTCMilliseconds(millis: i32): void {
185
+ this.setTime(this.epochMillis + (millis - this.getUTCMilliseconds()));
186
+ }
187
+
188
+ setUTCSeconds(seconds: i32): void {
189
+ this.setTime(this.epochMillis + (seconds - this.getUTCSeconds()) * MILLIS_PER_SECOND);
190
+ }
191
+
192
+ setUTCMinutes(minutes: i32): void {
193
+ this.setTime(this.epochMillis + (minutes - this.getUTCMinutes()) * MILLIS_PER_MINUTE);
194
+ }
195
+
196
+ setUTCHours(hours: i32): void {
197
+ this.setTime(this.epochMillis + (hours - this.getUTCHours()) * MILLIS_PER_HOUR);
198
+ }
199
+
200
+ setUTCDate(day: i32): void {
201
+ if (this.day == day) return;
202
+ this.setTime(join(this.year, this.month, day, this.epochMillis));
203
+ }
204
+
205
+ setUTCMonth(month: i32, day: i32 = this.day): void {
206
+ if (this.month == month + 1) return;
207
+ this.setTime(join(this.year, month + 1, day, this.epochMillis));
208
+ }
209
+
210
+ setUTCFullYear(year: i32): void {
211
+ if (this.year == year) return;
212
+ this.setTime(join(year, this.month, this.day, this.epochMillis));
213
+ }
214
+
215
+ toISOString(): string {
216
+ // TODO: add more low-level helper which combine toString and padStart without extra allocation
217
+
218
+ let yr = this.year;
219
+ let isNeg = yr < 0;
220
+ let year = (isNeg || yr >= 10000)
221
+ ? (isNeg ? "-" : "+") + stringify(abs(yr), 6)
222
+ : stringify(yr, 4);
223
+ let month = stringify(this.month, 2);
224
+ let day = stringify(this.day);
225
+ let hours = stringify(this.getUTCHours());
226
+ let mins = stringify(this.getUTCMinutes());
227
+ let secs = stringify(this.getUTCSeconds());
228
+ let ms = stringify(this.getUTCMilliseconds(), 3);
229
+
230
+ return `${year}-${month}-${day}T${hours}:${mins}:${secs}.${ms}Z`;
231
+ }
232
+
233
+ toUTCString(): string {
234
+ const
235
+ weeks: StaticArray<string> = [
236
+ "Sun, ", "Mon, ", "Tue, ", "Wed, ", "Thu, ", "Fri, ", "Sat, "
237
+ ],
238
+ months: StaticArray<string> = [
239
+ " Jan ", " Feb ", " Mar ", " Apr ", " May ", " Jun ",
240
+ " Jul ", " Aug ", " Sep ", " Oct ", " Nov ", " Dec "
241
+ ];
242
+
243
+ let mo = this.month;
244
+ let da = this.day;
245
+ let yr = this.year;
246
+ let wd = dayOfWeek(yr, mo, da);
247
+ let year = stringify(abs(yr), 4);
248
+ let month = unchecked(months[mo - 1]);
249
+ let week = unchecked(weeks[wd]);
250
+ let day = stringify(da);
251
+ let hours = stringify(this.getUTCHours());
252
+ let mins = stringify(this.getUTCMinutes());
253
+ let secs = stringify(this.getUTCSeconds());
254
+
255
+ return `${week}${day}${month}${yr < 0 ? "-" : ""}${year} ${hours}:${mins}:${secs} GMT`;
256
+ }
257
+
258
+ toDateString(): string {
259
+ // TODO: use u64 static data instead 4 chars
260
+ // also use stream itoa variants.
261
+ const
262
+ weeks: StaticArray<string> = [
263
+ "Sun ", "Mon ", "Tue ", "Wed ", "Thu ", "Fri ", "Sat "
264
+ ],
265
+ months: StaticArray<string> = [
266
+ "Jan ", "Feb ", "Mar ", "Apr ", "May ", "Jun ",
267
+ "Jul ", "Aug ", "Sep ", "Oct ", "Nov ", "Dec "
268
+ ];
269
+
270
+ let mo = this.month;
271
+ let da = this.day;
272
+ let yr = this.year;
273
+ let wd = dayOfWeek(yr, mo, da);
274
+ let year = stringify(abs(yr), 4);
275
+ let month = unchecked(months[mo - 1]);
276
+ let week = unchecked(weeks[wd]);
277
+ let day = stringify(da);
278
+
279
+ return `${week}${month}${day}${yr < 0 ? " -" : " "}${year}`;
280
+ }
281
+
282
+ // Note: it uses UTC time instead local time (without timezone offset)
283
+ toTimeString(): string {
284
+ let hours = stringify(this.getUTCHours());
285
+ let mins = stringify(this.getUTCMinutes());
286
+ let secs = stringify(this.getUTCSeconds());
287
+ // TODO: add timezone
288
+ return `${hours}:${mins}:${secs}`;
289
+ }
290
+
291
+ // Note: it uses UTC datetime instead local datetime (without timezone offset)
292
+ toString(): string {
293
+ return `${this.toDateString()} ${this.toTimeString()}`;
294
+ }
295
+ }
296
+
297
+ function epochMillis(
298
+ year: i32,
299
+ month: i32,
300
+ day: i32,
301
+ hour: i32,
302
+ minute: i32,
303
+ second: i32,
304
+ milliseconds: i32
305
+ ): i64 {
306
+ return (
307
+ daysSinceEpoch(year, month, day) * MILLIS_PER_DAY +
308
+ hour * MILLIS_PER_HOUR +
309
+ minute * MILLIS_PER_MINUTE +
310
+ second * MILLIS_PER_SECOND +
311
+ milliseconds
312
+ );
313
+ }
314
+
315
+ // @ts-ignore: decorator
316
+ @inline function floorDiv<T extends number>(a: T, b: T): T {
317
+ return (a - (a < 0 ? b - 1 : 0)) / b as T;
318
+ }
319
+
320
+ // @ts-ignore: decorator
321
+ @inline function euclidRem<T extends number>(a: T, b: T): T {
322
+ let m = a % b;
323
+ return m + (m < 0 ? b : 0) as T;
324
+ }
325
+
326
+ function invalidDate(millis: i64): bool {
327
+ // @ts-ignore
328
+ return (millis < -MILLIS_LIMIT) | (millis > MILLIS_LIMIT);
329
+ }
330
+
331
+ // Based on "Euclidean Affine Functions and Applications to Calendar Algorithms"
332
+ // Paper: https://arxiv.org/pdf/2102.06959.pdf
333
+ function dateFromEpoch(ms: i64): i32 {
334
+ let da = (<i32>floorDiv(ms, MILLIS_PER_DAY) * 4 + EPOCH_OFFSET * 4) | 3;
335
+ let q0 = floorDiv(da, DAYS_PER_EPOCH); // [0, 146096]
336
+ let r1 = <u32>da - q0 * DAYS_PER_EPOCH;
337
+ let u1 = u64(r1 | 3) * 2939745;
338
+ let dm1 = <u32>u1 / 11758980;
339
+ let n1 = 2141 * dm1 + 197913;
340
+ let year = 100 * q0 + i32(u1 >>> 32);
341
+ _day = (n1 & 0xFFFF) / 2141 + 1; // [1, 31]
342
+ _month = (n1 >>> 16) - (dm1 >= 306 ? 12 : 0); // [1, 12]
343
+ return year + (dm1 >= 306 ? 1 : 0);
344
+ }
345
+
346
+ // http://howardhinnant.github.io/date_algorithms.html#days_from_civil
347
+ function daysSinceEpoch(year: i32, month: i32, day: i32): i64 {
348
+ year -= i32(month <= 2);
349
+ let era = <u32>floorDiv(year, YEARS_PER_EPOCH);
350
+ let yoe = <u32>year - era * YEARS_PER_EPOCH; // [0, 399]
351
+ let doy = <u32>(153 * (month + (month > 2 ? -3 : 9)) + 2) / 5 + day - 1; // [0, 365]
352
+ let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; // [0, 146096]
353
+ return <i64><i32>(era * 146097 + doe - EPOCH_OFFSET);
354
+ }
355
+
356
+ // TomohikoSakamoto algorithm from https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
357
+ function dayOfWeek(year: i32, month: i32, day: i32): i32 {
358
+ const tab = memory.data<u8>([0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]);
359
+
360
+ year -= i32(month <= 2);
361
+ const cent = floorDiv(year >> 2, 100 >> 2);
362
+ year += (year >> 2) - cent + (cent >> 2);
363
+ month = <i32>load<u8>(tab + month - 1);
364
+ return euclidRem(year + month + day, 7);
365
+ }
366
+
367
+ function stringify(value: i32, padding: i32 = 2): string {
368
+ return value.toString().padStart(padding, "0");
369
+ }
370
+
371
+ function join(year: i32, month: i32, day: i32, ms: i64): i64 {
372
+ return daysSinceEpoch(year, month, day) * MILLIS_PER_DAY + euclidRem(ms, MILLIS_PER_DAY);
373
+ }
@@ -0,0 +1,11 @@
1
+ // @ts-ignore: decorator
2
+ @builtin
3
+ export declare function ERROR(message?: string): void;
4
+
5
+ // @ts-ignore: decorator
6
+ @builtin
7
+ export declare function WARNING(message?: string): void;
8
+
9
+ // @ts-ignore: decorator
10
+ @builtin
11
+ export declare function INFO(message?: string): void;
@@ -0,0 +1,44 @@
1
+ export class Error {
2
+
3
+ name: string = "Error";
4
+ stack: string = ""; // TODO
5
+
6
+ constructor(
7
+ public message: string = ""
8
+ ) {}
9
+
10
+ toString(): string {
11
+ let message = this.message;
12
+ return message.length
13
+ ? this.name + ": " + message
14
+ : this.name;
15
+ }
16
+ }
17
+
18
+ export class RangeError extends Error {
19
+ constructor(message: string = "") {
20
+ super(message);
21
+ this.name = "RangeError";
22
+ }
23
+ }
24
+
25
+ export class TypeError extends Error {
26
+ constructor(message: string = "") {
27
+ super(message);
28
+ this.name = "TypeError";
29
+ }
30
+ }
31
+
32
+ export class SyntaxError extends Error {
33
+ constructor(message: string = "") {
34
+ super(message);
35
+ this.name = "SyntaxError";
36
+ }
37
+ }
38
+
39
+ export class URIError extends Error {
40
+ constructor(message: string = "") {
41
+ super(message);
42
+ this.name = "URIError";
43
+ }
44
+ }