1 // =================================
  2 // Copyright (c) 2022 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 public const byte trap_exit = 0u;
  7 public const byte trap_memory_page_size = 1u;
  8 public const byte trap_heap_start = 2u;
  9 public const byte trap_heap_length = 3u;
 10 public const byte trap_allocate_memory_pages = 4u;
 11 public const byte trap_dump_heap = 5u;
 12 public const byte trap_random_seed = 6u;
 13 public const byte trap_current_time_point = 7u;
 14 public const byte trap_sleep = 8u;
 15 public const byte trap_current_date = 9u;
 16 public const byte trap_current_date_time = 10u;
 17 public const byte trap_times = 11u;
 18 public const byte trap_pow = 12u;
 19 public const byte trap_throw = 13u;
 20 public const byte trap_catch = 14u;
 21 public const byte trap_resume = 15u;
 22 public const byte trap_stack_trace = 16u;
 23 public const byte trap_get_system_error = 17u;
 24 public const byte trap_fork = 18u;
 25 public const byte trap_exec = 19u;
 26 public const byte trap_wait = 20u;
 27 public const byte trap_getpid = 21u;
 28 public const byte trap_create = 22u;
 29 public const byte trap_open = 23u;
 30 public const byte trap_close = 24u;
 31 public const byte trap_read = 25u;
 32 public const byte trap_write = 26u;
 33 public const byte trap_ioctl = 27u;
 34 public const byte trap_unlink = 28u;
 35 public const byte trap_seek = 29u;
 36 public const byte trap_tell = 30u;
 37 public const byte trap_stat = 31u;
 38 public const byte trap_getcwd = 32u;
 39 public const byte trap_chdir = 33u;
 40 public const byte trap_mkdir = 34u;
 41 public const byte trap_opendir = 35u;
 42 public const byte trap_closedir = 36u;
 43 public const byte trap_readdir = 37u;
 44 public const byte trap_utime = 38u;
 45 public const byte trap_mcpy = 39u;
 46 public const byte trap_open_resource = 40u;
 47 public const byte trap_close_resource = 41u;
 48 public const byte trap_get_resource_size = 42u;
 49 public const byte trap_read_resource = 43u;
 50 public const byte trap_decompress = 44u;
 51 public const byte trap_get_decompressed_data_size = 45u;
 52 public const byte trap_get_decompressed_data = 46u;
 53 public const byte trap_close_decompression = 47u;
 54 public const byte trap_get_host_name = 48u;
 55 public const byte trap_get_user_name = 49u;
 56 public const byte trap_pipe = 50u;
 57 public const byte trap_dup = 51u;
 58 public const byte trap_setuid = 52u;
 59 public const byte trap_setgid = 53u;
 60 public const byte trap_getuid = 54u;
 61 public const byte trap_getgid = 55u;
 62 public const byte trap_seteuid = 56u;
 63 public const byte trap_setegid = 57u;
 64 public const byte trap_geteuid = 58u;
 65 public const byte trap_getegid = 59u;
 66 public const byte trap_umask = 60u;
 67 public const byte trap_chmod = 61u;
 68 public const byte trap_chown = 62u;
 69 public const byte trap_rename = 63u;
 70 public const byte trap_add_dir_change_notification = 64u;
 71 public const byte trap_remove_dir_change_notifications = 65u;
 72 public const byte trap_directories_changed = 66u;
 73 public const byte trap_clear_directories_changed = 67u;
 74 public const byte trap_child_times = 68u;
 75 public const byte trap_msgq = 69u;
 76 public const byte trap_close_msgq = 70u;
 77 public const byte trap_wait_msg = 71u;
 78 public const byte trap_put_msg = 72u;
 79 public const byte trap_get_msgq_length = 73u;
 80 public const byte trap_get_msg_size = 74u;
 81 public const byte trap_get_msg = 75u;
 82 public const byte trap_bind_terminal = 76u;
 83 public const byte trap_unbind_terminal = 77u;
 84 public const byte trap_timer_msg = 78u;
 85 public const byte trap_connect = 79u;
 86 public const byte trap_get_debug_mode = 80u;
 87 public const byte trap_set_debug_mode = 81u;
 88 public const byte trap_write_debug_message = 82u;
 89 public const byte trap_kill = 83u;
 90 public const byte trap_waitpid = 84u;
 91 
 92 public cdecl nothrow void exit(byte exitCode)
 93 {
 94     run_at_exits();
 95     trap(0u, trap_exit, 0u, exitCode);
 96 }
 97 
 98 public cdecl nothrow long memory_page_size()
 99 {
100     return cast<long>(trap(0u, trap_memory_page_size, 0u));
101 }
102 
103 public cdecl nothrow long heap_start()
104 {
105     return cast<long>(trap(0u, trap_heap_start, 0u));
106 }
107 
108 public cdecl nothrow long heap_length()
109 {
110     return cast<long>(trap(0u, trap_heap_length, 0u));
111 }
112 
113 public cdecl nothrow long allocate_memory_pages(int numPages)
114 {
115     return cast<long>(trap(0u, trap_allocate_memory_pages, 0u, numPages));
116 }
117 
118 public cdecl nothrow void dump_heap(ulong free, int tag, ulong ptr, ulong size)
119 {
120     trap(0u, trap_dump_heap, 0u, free, tag, ptr, size);
121 }
122 
123 public cdecl nothrow uint random_seed()
124 {
125     return cast<uint>(trap(0u, trap_random_seed, 0u));
126 }
127 
128 public cdecl nothrow long current_time_point()
129 {
130     return cast<long>(trap(0u, trap_current_time_point, 0u));
131 }
132 
133 public cdecl nothrow int sleep(long duration)
134 {
135     return cast<int>(trap(0u, trap_sleep, 0u, duration));
136 }
137 
138 public cdecl nothrow int current_date(short* y, sbyte* m, sbyte* d)
139 {
140     return cast<int>(trap(0u, trap_current_date, 0u, y, m, d));
141 }
142 
143 public cdecl nothrow int current_date_time(short* y, sbyte* m, sbyte* d, int* secs)
144 {
145     return cast<int>(trap(0u, trap_current_date_time, 0u, y, m, d, secs));
146 }
147 
148 public cdecl nothrow int times(long* userTime, long* sleepTime, long* systemTime)
149 {
150     return cast<int>(trap(0u, trap_times, 0u, userTime, sleepTime, systemTime));
151 }
152 
153 public cdecl nothrow double pow(double x, double y)
154 {
155     long lx = *cast<long*>(cast<void*>(&x));
156     long ly = *cast<long*>(cast<void*>(&y));
157     long result = trap(0u, trap_pow, 0u, lx, ly);
158     return *cast<double*>(cast<void*>(&result));
159 }
160 
161 public cdecl nothrow int do_throw(void* exception, ulong exceptionClassId)
162 {
163     return cast<int>(trap(0u, trap_throw, 0u, exception, exceptionClassId));
164 }
165 
166 public cdecl nothrow void* do_catch()
167 {
168     return cast<void*>(cast<ulong>(trap(0u, trap_catch, 0u)));
169 }
170 
171 public cdecl nothrow int resume()
172 {
173     return cast<int>(trap(0u, trap_resume, 0u));
174 }
175 
176 public cdecl nothrow int stack_trace(void* buffer, long bufferSize)
177 {
178     return cast<int>(trap(0u, trap_stack_trace, 0u, buffer, bufferSize));
179 }
180 
181 public cdecl nothrow int get_system_error(int* errorCode, void* buffer, long bufferSize)
182 {
183     return cast<int>(trap(0u, trap_get_system_error, 0u, errorCode, buffer, bufferSize));
184 }
185 
186 public cdecl nothrow int fork()
187 {
188     return cast<int>(trap(0u, trap_fork, 0u));
189 }
190 
191 public cdecl nothrow int exec(const char* file, const char** argv, const char** envp)
192 {
193     return cast<int>(trap(0u, trap_exec, 0u, file, argv, envp));
194 }
195 
196 public cdecl nothrow int wait(byte* exitCode)
197 {
198     return cast<int>(trap(0u, trap_wait, 0u, exitCode));
199 }
200 
201 public cdecl nothrow int getpid()
202 {
203     return cast<int>(trap(0u, trap_getpid, 0u));
204 }
205 
206 public cdecl nothrow int create(const char* path, int mode)
207 {
208     return cast<int>(trap(0u, trap_create, 0u, path, mode));
209 }
210 
211 public enum OpenFlags : int
212 {
213     none = 0, 
214     read = 1 << 0, 
215     write = 1 << 1, 
216     create = 1 << 2, 
217     append = 1 << 3, 
218     truncate = 1 << 4, 
219     text = 1 << 5, 
220     random_access = 1 << 6
221 }
222 
223 public cdecl nothrow int open(const char* path, OpenFlags flags, int mode)
224 {
225     return cast<int>(trap(0u, trap_open, 0u, path, cast<int>(flags), mode));
226 }
227 
228 public cdecl nothrow int close(int fd)
229 {
230     return cast<int>(trap(0u, trap_close, 0u, fd));
231 }
232 
233 public cdecl nothrow long read(int fd, void* buffer, long count)
234 {
235     return cast<long>(trap(0u, trap_read, 0u, fd, buffer, count));
236 }
237 
238 public cdecl nothrow long write(int fd, void* buffer, long count)
239 {
240     return cast<long>(trap(0u, trap_write, 0u, fd, buffer, count));
241 }
242 
243 public enum IOControlItem : int
244 {
245     isConsole = 0, 
246     isHostTextFile = 1, 
247     hasColors = 2, 
248     columns = 3, 
249     rows = 4, 
250     cursor_x = 5, 
251     cursor_y = 6, 
252     set_cursor_pos = 7, 
253     set_cooked = 8, 
254     set_raw = 9, 
255     set_echo = 10, 
256     push_lines = 11, 
257     pop_lines = 12, 
258     tab = 13, 
259     untab = 14, 
260     push_pid = 15, 
261     pop_pid = 16
262 }
263 
264 public cdecl nothrow int ioctl(int fd, IOControlItem item, byte* arg, long argSize)
265 {
266     return cast<int>(trap(0u, trap_ioctl, 0u, fd, item, arg, argSize));
267 }
268 
269 public cdecl nothrow int unlink(const char* path)
270 {
271     return cast<int>(trap(0u, trap_unlink, 0u, path));
272 }
273 
274 public enum Origin : int
275 {
276     seekSet = 0, seekCur = 1, seekEnd = 2
277 }
278     
279 public cdecl nothrow long seek(int fd, long offset, Origin whence)
280 {
281     return cast<long>(trap(0u, trap_seek, 0u, fd, offset, cast<int>(whence)));
282 }
283 
284 public cdecl nothrow long tell(int fd)
285 {
286     return cast<long>(trap(0u, trap_tell, 0u, fd));
287 }
288 
289 public cdecl nothrow int stat(const char* path, byte* statBuf, int statBufSize)
290 {
291     return cast<int>(trap(0u, trap_stat, 0u, path, statBuf, statBufSize));
292 }
293 
294 public cdecl nothrow int getcwd(char* pathBuf, long bufSize)
295 {
296     return cast<int>(trap(0u, trap_getcwd, 0u, pathBuf, bufSize));
297 }
298 
299 public cdecl nothrow int chdir(const char* path)
300 {
301     return cast<int>(trap(0u, trap_chdir, 0u, path));
302 }
303 
304 public cdecl nothrow int mkdir(const char* path, int mode)
305 {
306     return cast<int>(trap(0u, trap_mkdir, 0u, path, mode));
307 }
308 
309 public cdecl nothrow int utime(const char* path, byte* timeBuf, long timeBufSize)
310 {
311     return cast<int>(trap(0u, trap_utime, 0u, path, timeBuf, timeBufSize));
312 }
313 
314 public cdecl nothrow int mcpy(byte* sourceBuffer, byte* destBuffer, long count)
315 {
316     return cast<int>(trap(0u, trap_mcpy, 0u, sourceBuffer, destBuffer, count));
317 }
318 
319 public cdecl nothrow int opendir(const char* path)
320 {
321     return cast<int>(trap(0u, trap_opendir, 0u, path));
322 }
323 
324 public cdecl nothrow int closedir(int dd)
325 {
326     return cast<int>(trap(0u, trap_closedir, 0u, dd));
327 }
328 
329 public cdecl nothrow int readdir(int dd, byte* dirEntryBuf, long dirEntryBufSize)
330 {
331     return cast<int>(trap(0u, trap_readdir, 0u, dd, dirEntryBuf, dirEntryBufSize));
332 }
333 
334 public cdecl nothrow int open_resource(const char* resource_name)
335 {
336     return cast<int>(trap(0u, trap_open_resource, 0u, resource_name));
337 }
338 
339 public cdecl nothrow int close_resource(int rd)
340 {
341     return cast<int>(trap(0u, trap_close_resource, 0u, rd));
342 }
343 
344 public cdecl nothrow long get_resource_size(int rd)
345 {
346     return cast<long>(trap(0u, trap_get_resource_size, 0u, rd));
347 }
348 
349 public cdecl nothrow int read_resource(int rd, long offset, long length, byte* buffer)
350 {
351     return cast<int>(trap(0u, trap_read_resource, 0u, rd, offset, length, buffer));
352 }
353 
354 public cdecl nothrow int decompress(byte* buffer, long count)
355 {
356     return cast<int>(trap(0u, trap_decompress, 0u, buffer, count));
357 }
358 
359 public cdecl nothrow long get_decompressed_data_size(int dd)
360 {
361     return cast<long>(trap(0u, trap_get_decompressed_data_size, 0u, dd));
362 }
363 
364 public cdecl nothrow int get_decompressed_data(int dd, byte* buffer, long count)
365 {
366     return cast<int>(trap(0u, trap_get_decompressed_data, 0u, dd, buffer, count));
367 }
368 
369 public cdecl nothrow int close_decompression(int dd)
370 {
371     return cast<int>(trap(0u, trap_close_decompression, 0u, dd));
372 }
373 
374 public cdecl nothrow int get_host_name(char* buffer, long bufSize)
375 {
376     return cast<int>(trap(0u, trap_get_host_name, 0u, buffer, bufSize));
377 }
378 
379 public cdecl nothrow int get_user_name(char* buffer, long bufSize)
380 {
381     return cast<int>(trap(0u, trap_get_user_name, 0u, buffer, bufSize));
382 }
383 
384 public cdecl nothrow int pipe(int* readerFd, int* writerFd)
385 {
386     return cast<int>(trap(0u, trap_pipe, 0u, readerFd, writerFd));
387 }
388 
389 public cdecl nothrow int dup(int fd)
390 {
391     return cast<int>(trap(0u, trap_dup, 0u, fd));
392 }
393 
394 public cdecl nothrow int setuid(int uid)
395 {
396     return cast<int>(trap(0u, trap_setuid, 0u, uid));
397 }
398 
399 public cdecl nothrow int setgid(int gid)
400 {
401     return cast<int>(trap(0u, trap_setgid, 0u, gid));
402 }
403 
404 public cdecl nothrow int getuid()
405 {
406     return cast<int>(trap(0u, trap_getuid, 0u));
407 }
408 
409 public cdecl nothrow int getgid()
410 {
411     return cast<int>(trap(0u, trap_getgid, 0u));
412 }
413 
414 public cdecl nothrow int seteuid(int euid)
415 {
416     return cast<int>(trap(0u, trap_seteuid, 0u, euid));
417 }
418 
419 public cdecl nothrow int setegid(int egid)
420 {
421     return cast<int>(trap(0u, trap_setegid, 0u, egid));
422 }
423 
424 public cdecl nothrow int geteuid()
425 {
426     return cast<int>(trap(0u, trap_geteuid, 0u));
427 }
428 
429 public cdecl nothrow int getegid()
430 {
431     return cast<int>(trap(0u, trap_getegid, 0u));
432 }
433 
434 public cdecl nothrow int umask(int mask)
435 {
436     return cast<int>(trap(0u, trap_umask, 0u, mask));
437 }
438 
439 public cdecl nothrow int chmod(const char* path, int mode)
440 {
441     return cast<int>(trap(0u, trap_chmod, 0u, path, mode));
442 }
443 
444 public cdecl nothrow int chown(const char* path, int uid, int gid)
445 {
446     return cast<int>(trap(0u, trap_chown, 0u, path, uid, gid));
447 }
448 
449 public cdecl nothrow int rename(const char* sourcePath, const char* targetPath)
450 {
451     return cast<int>(trap(0u, trap_rename, 0u, sourcePath, targetPath));
452 }
453 
454 public cdecl nothrow int add_dir_change_notification(const char* directories)
455 {
456     return cast<int>(trap(0u, trap_add_dir_change_notification, 0u, directories));
457 }
458 
459 public cdecl nothrow int remove_dir_change_notifications()
460 {
461     return cast<int>(trap(0u, trap_remove_dir_change_notifications, 0u));
462 }
463 
464 public cdecl nothrow int directories_changed()
465 {
466     return cast<int>(trap(0u, trap_directories_changed, 0u));
467 }
468 
469 public cdecl nothrow int clear_directories_changed()
470 {
471     return cast<int>(trap(0u, trap_clear_directories_changed, 0u));
472 }
473 
474 public cdecl nothrow int child_times(long* childUserTime, long* childSleepTime, long* childSystemTime)
475 {
476     return cast<int>(trap(0u, trap_child_times, 0u, childUserTime, childSleepTime, childSystemTime));
477 }
478 
479 public cdecl nothrow int msgq(const char* name)
480 {
481     return cast<int>(trap(0u, trap_msgq, 0u, name));
482 }
483 
484 public cdecl nothrow int close_msgq(int md)
485 {
486     return cast<int>(trap(0u, trap_close_msgq, 0u, md));
487 }
488 
489 public cdecl nothrow int wait_msg(int md)
490 {
491     return cast<int>(trap(0u, trap_wait_msg, 0u, md));
492 }
493 
494 public cdecl nothrow int put_msg(int md, byte* msgData, int msgSize)
495 {
496     return cast<int>(trap(0u, trap_put_msg, 0u, md, msgData, msgSize));
497 }
498 
499 public cdecl nothrow int get_msgq_length(int md)
500 {
501     return cast<int>(trap(0u, trap_get_msgq_length, 0u, md));
502 }
503 
504 public cdecl nothrow int get_msg_size(int md)
505 {
506     return cast<int>(trap(0u, trap_get_msg_size, 0u, md));
507 }
508 
509 public cdecl nothrow int get_msg(int md, byte* buffer)
510 {
511     return cast<int>(trap(0u, trap_get_msg, 0u, md, buffer));
512 }
513 
514 public cdecl nothrow int bind_terminal(int md)
515 {
516     return cast<int>(trap(0u, trap_bind_terminal, 0u, md));
517 }
518 
519 public cdecl nothrow int unbind_terminal()
520 {
521     return cast<int>(trap(0u, trap_unbind_terminal, 0u));
522 }
523 
524 public cdecl nothrow int timer_msg(long duration, int md, byte* msgData, int msgSize)
525 {
526     return cast<int>(trap(0u, trap_timer_msg, 0u, duration, md, msgData, msgSize));
527 }
528 
529 public cdecl nothrow int connect(const char* node, const char* service)
530 {
531     return cast<int>(trap(0u, trap_connect, 0u, node, service));
532 }
533 
534 public cdecl nothrow int get_debug_mode()
535 {
536     return cast<int>(trap(0u, trap_get_debug_mode, 0u));
537 }
538 
539 public cdecl nothrow int set_debug_mode(int mode)
540 {
541     return cast<int>(trap(0u, trap_set_debug_mode, 0u, mode));
542 }
543 
544 public cdecl nothrow int write_debug_message(const char* msg)
545 {
546     return cast<int>(trap(0u, trap_write_debug_message, 0u, msg));
547 }
548 
549 public cdecl nothrow int kill(int pid)
550 {
551     return cast<int>(trap(0u, trap_kill, 0u, pid));
552 }
553 
554 public cdecl nothrow int waitpid(int pid, byte* exitCode)
555 {
556     return cast<int>(trap(0u, trap_waitpid, 0u, pid, exitCode));
557 }