mt-lang 0.2.0 → 0.2.1

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 (189) hide show
  1. checksums.yaml +4 -4
  2. data/docs/index.html +4 -3
  3. data/lib/milk_tea/base.rb +1 -1
  4. data/std/asset_pack.mt +267 -0
  5. data/std/async/libuv_runtime.mt +539 -0
  6. data/std/async/mailbox.mt +191 -0
  7. data/std/async/runtime.mt +87 -0
  8. data/std/async.mt +87 -0
  9. data/std/base64.mt +185 -0
  10. data/std/behavior_tree.mt +396 -0
  11. data/std/binary.mt +314 -0
  12. data/std/binary_heap.mt +157 -0
  13. data/std/bitset.mt +239 -0
  14. data/std/box2d.mt +535 -0
  15. data/std/bytes.mt +46 -0
  16. data/std/c/box2d.mt +1254 -0
  17. data/std/c/cgltf.mt +647 -0
  18. data/std/c/cjson.mt +106 -0
  19. data/std/c/crypto.mt +11 -0
  20. data/std/c/crypto_support.h +51 -0
  21. data/std/c/ctype.mt +19 -0
  22. data/std/c/ctype_bindgen.h +29 -0
  23. data/std/c/curl.mt +1223 -0
  24. data/std/c/enet.mt +473 -0
  25. data/std/c/errno.mt +41 -0
  26. data/std/c/errno_bindgen.h +53 -0
  27. data/std/c/flecs.mt +2945 -0
  28. data/std/c/fs.linux.mt +43 -0
  29. data/std/c/fs.windows.mt +43 -0
  30. data/std/c/fs_support.h +954 -0
  31. data/std/c/gl.mt +2065 -0
  32. data/std/c/gl_registry_helpers.h +10748 -0
  33. data/std/c/glfw.mt +499 -0
  34. data/std/c/libc.mt +167 -0
  35. data/std/c/libuv.mt +1491 -0
  36. data/std/c/math.mt +22 -0
  37. data/std/c/math_bindgen.h +31 -0
  38. data/std/c/miniaudio.mt +3420 -0
  39. data/std/c/pcre2.mt +735 -0
  40. data/std/c/process.mt +58 -0
  41. data/std/c/process_support.h +1147 -0
  42. data/std/c/raygui.mt +1602 -0
  43. data/std/c/raylib.mt +1245 -0
  44. data/std/c/raymath.mt +163 -0
  45. data/std/c/rlgl.mt +434 -0
  46. data/std/c/rpng.mt +60 -0
  47. data/std/c/rres.mt +187 -0
  48. data/std/c/sdl3.mt +4132 -0
  49. data/std/c/sqlite3.mt +951 -0
  50. data/std/c/stb_image.mt +52 -0
  51. data/std/c/stb_image_resize2.mt +128 -0
  52. data/std/c/stb_image_write.mt +19 -0
  53. data/std/c/stb_rect_pack.mt +38 -0
  54. data/std/c/stb_truetype.mt +175 -0
  55. data/std/c/stb_vorbis.mt +31 -0
  56. data/std/c/stdio.mt +90 -0
  57. data/std/c/steamworks.h +5769 -0
  58. data/std/c/steamworks.mt +4771 -0
  59. data/std/c/string.mt +16 -0
  60. data/std/c/string_bindgen.h +36 -0
  61. data/std/c/sync.mt +33 -0
  62. data/std/c/sync_support.h +219 -0
  63. data/std/c/terminal.mt +24 -0
  64. data/std/c/terminal_support.h +408 -0
  65. data/std/c/time.mt +26 -0
  66. data/std/c/tls.mt +24 -0
  67. data/std/c/tls_support.h +626 -0
  68. data/std/c/tracy.mt +53 -0
  69. data/std/c/zlib.mt +20 -0
  70. data/std/c/zlib_support.h +239 -0
  71. data/std/c/zstd.mt +231 -0
  72. data/std/cell.mt +60 -0
  73. data/std/cgltf.mt +115 -0
  74. data/std/cjson.mt +90 -0
  75. data/std/cli.mt +768 -0
  76. data/std/cookie.mt +198 -0
  77. data/std/counter.mt +160 -0
  78. data/std/crypto.mt +74 -0
  79. data/std/cstring.mt +13 -0
  80. data/std/ctype.mt +52 -0
  81. data/std/curl/runtime.mt +261 -0
  82. data/std/curl.mt +38 -0
  83. data/std/deque.mt +348 -0
  84. data/std/encoding.mt +93 -0
  85. data/std/enet.mt +133 -0
  86. data/std/env.mt +41 -0
  87. data/std/errno.mt +47 -0
  88. data/std/flecs.mt +978 -0
  89. data/std/fmt.mt +271 -0
  90. data/std/fs.linux.mt +696 -0
  91. data/std/fs.windows.mt +711 -0
  92. data/std/fsm.mt +267 -0
  93. data/std/gl.mt +2062 -0
  94. data/std/glfw.mt +471 -0
  95. data/std/goap.mt +342 -0
  96. data/std/graph.mt +521 -0
  97. data/std/gzip.mt +77 -0
  98. data/std/hash.mt +429 -0
  99. data/std/http/server.mt +810 -0
  100. data/std/http.mt +1227 -0
  101. data/std/intern.mt +53 -0
  102. data/std/jobs.mt +288 -0
  103. data/std/json.mt +627 -0
  104. data/std/libc.mt +34 -0
  105. data/std/libuv.mt +554 -0
  106. data/std/linear_algebra.mt +243 -0
  107. data/std/linked_map.mt +372 -0
  108. data/std/linked_map_view.mt +49 -0
  109. data/std/linked_set.mt +117 -0
  110. data/std/log.mt +69 -0
  111. data/std/map.mt +418 -0
  112. data/std/math.mt +24 -0
  113. data/std/mem/arena.mt +137 -0
  114. data/std/mem/endian.mt +51 -0
  115. data/std/mem/heap.mt +298 -0
  116. data/std/mem/pool.mt +164 -0
  117. data/std/mem/stack.mt +47 -0
  118. data/std/mem/tracking.mt +119 -0
  119. data/std/miniaudio.mt +1299 -0
  120. data/std/multiset.mt +191 -0
  121. data/std/net/channel.mt +727 -0
  122. data/std/net/clock.mt +237 -0
  123. data/std/net/discovery.mt +277 -0
  124. data/std/net/lobby.mt +813 -0
  125. data/std/net/manager.mt +539 -0
  126. data/std/net/mux.mt +839 -0
  127. data/std/net/nat.mt +122 -0
  128. data/std/net/packet.mt +219 -0
  129. data/std/net/punch.mt +162 -0
  130. data/std/net/rpc.mt +119 -0
  131. data/std/net/session.mt +1157 -0
  132. data/std/net/stun.mt +252 -0
  133. data/std/net/sync.mt +183 -0
  134. data/std/net/turn.mt +459 -0
  135. data/std/net.mt +2944 -0
  136. data/std/oauth2.mt +408 -0
  137. data/std/option.mt +51 -0
  138. data/std/ordered_map.mt +497 -0
  139. data/std/ordered_set.mt +348 -0
  140. data/std/path.mt +368 -0
  141. data/std/pcre2/runtime.mt +35 -0
  142. data/std/pcre2.mt +78 -0
  143. data/std/priority_queue.mt +55 -0
  144. data/std/process.mt +837 -0
  145. data/std/queue.mt +55 -0
  146. data/std/random.mt +146 -0
  147. data/std/raygui.mt +99 -0
  148. data/std/raylib/debug_console.mt +71 -0
  149. data/std/raylib/easing.mt +388 -0
  150. data/std/raylib/packed_assets.mt +255 -0
  151. data/std/raylib/runtime.mt +30 -0
  152. data/std/raylib/tracy_gpu.mt +37 -0
  153. data/std/raylib.mt +1510 -0
  154. data/std/raymath.mt +153 -0
  155. data/std/result.mt +89 -0
  156. data/std/rlgl.mt +268 -0
  157. data/std/rpng.mt +46 -0
  158. data/std/rres.mt +36 -0
  159. data/std/sdl3/runtime.mt +54 -0
  160. data/std/sdl3.mt +1731 -0
  161. data/std/serialize.mt +68 -0
  162. data/std/set.mt +124 -0
  163. data/std/spatial.mt +176 -0
  164. data/std/sqlite3.mt +151 -0
  165. data/std/stack.mt +55 -0
  166. data/std/stb_image.mt +47 -0
  167. data/std/stb_image_resize2.mt +41 -0
  168. data/std/stb_image_write.mt +17 -0
  169. data/std/stb_rect_pack.mt +15 -0
  170. data/std/stb_truetype.mt +77 -0
  171. data/std/stb_vorbis.mt +16 -0
  172. data/std/stdio.mt +88 -0
  173. data/std/steamworks.mt +1542 -0
  174. data/std/str.mt +293 -0
  175. data/std/string.mt +234 -0
  176. data/std/sync.mt +194 -0
  177. data/std/tar.mt +704 -0
  178. data/std/terminal.mt +1002 -0
  179. data/std/testing.mt +266 -0
  180. data/std/thread.mt +120 -0
  181. data/std/time.mt +105 -0
  182. data/std/tls.mt +616 -0
  183. data/std/toml.mt +1310 -0
  184. data/std/tracy.mt +42 -0
  185. data/std/uri.mt +118 -0
  186. data/std/url.mt +372 -0
  187. data/std/vec.mt +433 -0
  188. data/std/zstd.mt +94 -0
  189. metadata +187 -2
data/std/intern.mt ADDED
@@ -0,0 +1,53 @@
1
+ import std.map
2
+ import std.str
3
+ import std.vec
4
+
5
+ public type IdentId = ptr_uint
6
+
7
+ public struct Interner:
8
+ table: map.Map[str, ptr_uint]
9
+ strings: vec.Vec[str]
10
+
11
+
12
+ public function create() -> Interner:
13
+ return Interner(
14
+ table = map.Map[str, ptr_uint].create(),
15
+ strings = vec.Vec[str].create()
16
+ )
17
+
18
+
19
+ public function with_capacity(capacity: ptr_uint) -> Interner:
20
+ return Interner(
21
+ table = map.Map[str, ptr_uint].with_capacity(capacity),
22
+ strings = vec.Vec[str].with_capacity(capacity)
23
+ )
24
+
25
+
26
+ extending Interner:
27
+ public function len() -> ptr_uint:
28
+ return this.strings.len
29
+
30
+
31
+ public editable function intern(text: str) -> ptr_uint:
32
+ let existing = this.table.get(text) else:
33
+ let id = this.strings.len
34
+ this.strings.push(text)
35
+ let _ = this.table.set(text, id)
36
+ return id
37
+
38
+ unsafe:
39
+ return read(existing)
40
+
41
+
42
+ public function lookup(id: ptr_uint) -> Option[str]:
43
+ return this.strings.at(id)
44
+
45
+
46
+ public editable function clear() -> void:
47
+ this.table.clear()
48
+ this.strings.clear()
49
+
50
+
51
+ public editable function release() -> void:
52
+ this.table.release()
53
+ this.strings.release()
data/std/jobs.mt ADDED
@@ -0,0 +1,288 @@
1
+ import std.async as aio
2
+ import std.async.mailbox as aio_mailbox
3
+ import std.deque as deque
4
+ import std.mem.heap as heap
5
+ import std.string as string
6
+ import std.sync as sync
7
+ import std.thread as thread
8
+ import std.vec as vec
9
+
10
+ public struct Error:
11
+ code: int
12
+ message: string.String
13
+
14
+ public struct WorkItem:
15
+ run: fn(arg: ptr[void]) -> void
16
+ complete: fn(arg: ptr[void]) -> void
17
+ arg: ptr[void]
18
+
19
+ public struct Pool:
20
+ state: ptr[PoolState]?
21
+ workers: vec.Vec[thread.Thread]
22
+
23
+ struct PoolState:
24
+ mutex: sync.Mutex
25
+ condition: sync.Condition
26
+ queue: deque.Deque[WorkItem]
27
+ queued_jobs: ptr_uint
28
+ running_jobs: ptr_uint
29
+ stopping: bool
30
+ completions: aio_mailbox.Mailbox[WorkItem]
31
+
32
+
33
+ function jobs_error(code: int, message: str) -> Error:
34
+ return Error(code = code, message = string.String.from_str(message))
35
+
36
+
37
+ function error_from_sync(source: sync.Error) -> Error:
38
+ return Error(code = source.code, message = source.message)
39
+
40
+
41
+ function error_from_thread(source: thread.Error) -> Error:
42
+ return Error(code = source.code, message = source.message)
43
+
44
+
45
+ function error_from_mailbox(source: aio_mailbox.Error) -> Error:
46
+ return Error(code = source.code, message = source.message)
47
+
48
+
49
+ function noop_completion(arg: ptr[void]) -> void:
50
+ unsafe: arg
51
+
52
+
53
+ function stop_pool_state(state: ptr[PoolState]) -> void:
54
+ let mutex = unsafe: read(state).mutex
55
+ mutex.lock()
56
+ defer mutex.unlock()
57
+ unsafe: read(state).stopping = true
58
+ let condition = unsafe: read(state).condition
59
+ condition.broadcast()
60
+
61
+
62
+ function destroy_pool_state(state: ptr[PoolState]) -> void:
63
+ var completions = unsafe: read(state).completions
64
+ completions.release()
65
+
66
+ var queue = unsafe: read(state).queue
67
+ queue.release()
68
+
69
+ var condition = unsafe: read(state).condition
70
+ condition.release()
71
+
72
+ var mutex = unsafe: read(state).mutex
73
+ mutex.release()
74
+
75
+ heap.release(state)
76
+
77
+
78
+ function join_and_release_workers(workers: ref[vec.Vec[thread.Thread]]) -> void:
79
+ while true:
80
+ let maybe_worker = workers.pop()
81
+ match maybe_worker:
82
+ Option.none:
83
+ break
84
+ Option.some as payload:
85
+ var worker = payload.value
86
+ let join_result = worker.join()
87
+ match join_result:
88
+ Result.failure as join_payload:
89
+ join_payload.error.release()
90
+ fatal(c"jobs worker join failed")
91
+ Result.success as join_payload:
92
+ join_payload.value
93
+
94
+ workers.release()
95
+
96
+
97
+ function worker_entry(state_raw: ptr[void]) -> void:
98
+ let state = unsafe: ptr[PoolState]<-state_raw
99
+ while true:
100
+ let mutex = unsafe: read(state).mutex
101
+ mutex.lock()
102
+
103
+ while true:
104
+ let should_wait = unsafe: not read(state).stopping and read(state).queue.is_empty()
105
+ if not should_wait:
106
+ break
107
+
108
+ let condition = unsafe: read(state).condition
109
+ condition.wait(mutex)
110
+
111
+ let should_stop = unsafe: read(state).stopping and read(state).queue.is_empty()
112
+ if should_stop:
113
+ mutex.unlock()
114
+ return
115
+
116
+ let maybe_job = unsafe: read(state).queue.pop_front()
117
+ match maybe_job:
118
+ Option.none:
119
+ mutex.unlock()
120
+ continue
121
+ Option.some as payload:
122
+ unsafe:
123
+ read(state).queued_jobs -= 1
124
+ read(state).running_jobs += 1
125
+ mutex.unlock()
126
+
127
+ let item = payload.value
128
+ item.run(item.arg)
129
+
130
+ let completions = unsafe: read(state).completions
131
+ let send_result = completions.send(item)
132
+ match send_result:
133
+ Result.failure as send_payload:
134
+ send_payload.error.release()
135
+ Result.success as send_payload:
136
+ send_payload.value
137
+
138
+ mutex.lock()
139
+ unsafe: read(state).running_jobs -= 1
140
+ mutex.unlock()
141
+
142
+
143
+ public function create_on(runtime: aio.Runtime, worker_count: ptr_uint) -> Result[Pool, Error]:
144
+ if worker_count == 0:
145
+ return Result[Pool, Error].failure(error = jobs_error(-1, "jobs pool requires worker_count > 0"))
146
+
147
+ let mutex_result = sync.create_mutex()
148
+ match mutex_result:
149
+ Result.failure as payload:
150
+ return Result[Pool, Error].failure(error = error_from_sync(payload.error))
151
+ Result.success as mutex_payload:
152
+ let condition_result = sync.create_condition()
153
+ match condition_result:
154
+ Result.failure as payload:
155
+ mutex_payload.value.release()
156
+ return Result[Pool, Error].failure(error = error_from_sync(payload.error))
157
+ Result.success as condition_payload:
158
+ let mailbox_result = aio_mailbox.create_on[WorkItem](runtime)
159
+ match mailbox_result:
160
+ Result.failure as payload:
161
+ condition_payload.value.release()
162
+ mutex_payload.value.release()
163
+ return Result[Pool, Error].failure(error = error_from_mailbox(payload.error))
164
+ Result.success as mailbox_payload:
165
+ let state = heap.must_alloc_zeroed[PoolState](1)
166
+ unsafe:
167
+ state.mutex = mutex_payload.value
168
+ state.condition = condition_payload.value
169
+ state.queue = deque.Deque[WorkItem].create()
170
+ state.queued_jobs = 0
171
+ state.running_jobs = 0
172
+ state.stopping = false
173
+ state.completions = mailbox_payload.value
174
+
175
+ var workers = vec.Vec[thread.Thread].with_capacity(worker_count)
176
+ var index: ptr_uint = 0
177
+ while index < worker_count:
178
+ let spawn_result = thread.spawn_raw(worker_entry, unsafe: ptr[void]<-state)
179
+ match spawn_result:
180
+ Result.failure as payload:
181
+ let error = error_from_thread(payload.error)
182
+ stop_pool_state(state)
183
+ join_and_release_workers(ref_of(workers))
184
+ destroy_pool_state(state)
185
+ return Result[Pool, Error].failure(error = error)
186
+ Result.success as payload:
187
+ workers.push(payload.value)
188
+ index += 1
189
+
190
+ return Result[Pool, Error].success(value = Pool(state = state, workers = workers))
191
+
192
+
193
+ public function create(worker_count: ptr_uint) -> Result[Pool, Error]:
194
+ return create_on(aio.current_runtime(), worker_count)
195
+
196
+
197
+ extending Error:
198
+ public editable function release() -> void:
199
+ this.message.release()
200
+
201
+
202
+ extending WorkItem:
203
+ public static function create(
204
+ run: fn(arg: ptr[void]) -> void,
205
+ complete: fn(arg: ptr[void]) -> void,
206
+ arg: ptr[void]
207
+ ) -> WorkItem:
208
+ return WorkItem(run = run, complete = complete, arg = arg)
209
+
210
+
211
+ public static function without_completion(run: fn(arg: ptr[void]) -> void, arg: ptr[void]) -> WorkItem:
212
+ return WorkItem(run = run, complete = noop_completion, arg = arg)
213
+
214
+
215
+ extending Pool:
216
+ public editable function release() -> void:
217
+ let state = this.state else:
218
+ return
219
+
220
+ this.state = null
221
+ stop_pool_state(state)
222
+ join_and_release_workers(ref_of(this.workers))
223
+ destroy_pool_state(state)
224
+
225
+
226
+ public function submit(item: WorkItem) -> Result[bool, Error]:
227
+ let state = this.state else:
228
+ return Result[bool, Error].failure(error = jobs_error(-1, "jobs pool is released"))
229
+
230
+ let mutex = unsafe: read(state).mutex
231
+ mutex.lock()
232
+ defer mutex.unlock()
233
+
234
+ if unsafe: read(state).stopping:
235
+ return Result[bool, Error].failure(error = jobs_error(-1, "jobs pool is stopping"))
236
+
237
+ unsafe:
238
+ read(state).queue.push_back(item)
239
+ read(state).queued_jobs += 1
240
+
241
+ let condition = unsafe: read(state).condition
242
+ condition.signal()
243
+ return Result[bool, Error].success(value = true)
244
+
245
+
246
+ public function try_complete_one() -> bool:
247
+ let state = this.state else:
248
+ return false
249
+
250
+ let completions = unsafe: read(state).completions
251
+ match completions.try_recv():
252
+ Option.none:
253
+ return false
254
+ Option.some as payload:
255
+ let item = payload.value
256
+ item.complete(item.arg)
257
+ return true
258
+
259
+
260
+ public function drain_completed() -> ptr_uint:
261
+ var completed: ptr_uint = 0
262
+ while this.try_complete_one():
263
+ completed += 1
264
+ return completed
265
+
266
+
267
+ public function queued_jobs() -> ptr_uint:
268
+ let state = this.state else:
269
+ return 0
270
+
271
+ let mutex = unsafe: read(state).mutex
272
+ mutex.lock()
273
+ defer mutex.unlock()
274
+ return unsafe: read(state).queued_jobs
275
+
276
+
277
+ public function active_jobs() -> ptr_uint:
278
+ let state = this.state else:
279
+ return 0
280
+
281
+ let mutex = unsafe: read(state).mutex
282
+ mutex.lock()
283
+ defer mutex.unlock()
284
+ return unsafe: read(state).queued_jobs + read(state).running_jobs
285
+
286
+
287
+ public function is_idle() -> bool:
288
+ return this.active_jobs() == 0