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(0utrap_exit0uexitCode);
 96 }
 97 
 98 public cdecl nothrow long memory_page_size()
 99 {
100     return cast<long>(trap(0utrap_memory_page_size0u));
101 }
102 
103 public cdecl nothrow long heap_start()
104 {
105     return cast<long>(trap(0utrap_heap_start0u));
106 }
107 
108 public cdecl nothrow long heap_length()
109 {
110     return cast<long>(trap(0utrap_heap_length0u));
111 }
112 
113 public cdecl nothrow long allocate_memory_pages(int numPages)
114 {
115     return cast<long>(trap(0utrap_allocate_memory_pages0unumPages));
116 }
117 
118 public cdecl nothrow void dump_heap(ulong freeint tagulong ptrulong size)
119 {
120     trap(0utrap_dump_heap0ufreetagptrsize);
121 }
122 
123 public cdecl nothrow uint random_seed()
124 {
125     return cast<uint>(trap(0utrap_random_seed0u));
126 }
127 
128 public cdecl nothrow long current_time_point()
129 {
130     return cast<long>(trap(0utrap_current_time_point0u));
131 }
132 
133 public cdecl nothrow int sleep(long duration)
134 {
135     return cast<int>(trap(0utrap_sleep0uduration));
136 }
137 
138 public cdecl nothrow int current_date(short* ysbyte* msbyte* d)
139 {
140     return cast<int>(trap(0utrap_current_date0uymd));
141 }
142 
143 public cdecl nothrow int current_date_time(short* ysbyte* msbyte* dint* secs)
144 {
145     return cast<int>(trap(0utrap_current_date_time0uymdsecs));
146 }
147 
148 public cdecl nothrow int times(long* userTimelong* sleepTimelong* systemTime)
149 {
150     return cast<int>(trap(0utrap_times0uuserTimesleepTimesystemTime));
151 }
152 
153 public cdecl nothrow double pow(double xdouble y)
154 {
155     long lx = *cast<long*>(cast<void*>(&x));
156     long ly = *cast<long*>(cast<void*>(&y));
157     long result = trap(0utrap_pow0ulxly);
158     return *cast<double*>(cast<void*>(&result));
159 }
160 
161 public cdecl nothrow int do_throw(void* exceptionulong exceptionClassId)
162 {
163     return cast<int>(trap(0utrap_throw0uexceptionexceptionClassId));
164 }
165 
166 public cdecl nothrow void* do_catch()
167 {
168     return cast<void*>(cast<ulong>(trap(0utrap_catch0u)));
169 }
170 
171 public cdecl nothrow int resume()
172 {
173     return cast<int>(trap(0utrap_resume0u));
174 }
175 
176 public cdecl nothrow int stack_trace(void* bufferlong bufferSize)
177 {
178     return cast<int>(trap(0utrap_stack_trace0ubufferbufferSize));
179 }
180 
181 public cdecl nothrow int get_system_error(int* errorCodevoid* bufferlong bufferSize)
182 {
183     return cast<int>(trap(0utrap_get_system_error0uerrorCodebufferbufferSize));
184 }
185 
186 public cdecl nothrow int fork()
187 {
188     return cast<int>(trap(0utrap_fork0u));
189 }
190 
191 public cdecl nothrow int exec(const char* fileconst char** argvconst char** envp)
192 {
193     return cast<int>(trap(0utrap_exec0ufileargvenvp));
194 }
195 
196 public cdecl nothrow int wait(byte* exitCode)
197 {
198     return cast<int>(trap(0utrap_wait0uexitCode));
199 }
200 
201 public cdecl nothrow int getpid()
202 {
203     return cast<int>(trap(0utrap_getpid0u));
204 }
205 
206 public cdecl nothrow int create(const char* pathint mode)
207 {
208     return cast<int>(trap(0utrap_create0upathmode));
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* pathOpenFlags flagsint mode)
224 {
225     return cast<int>(trap(0utrap_open0upathcast<int>(flags)mode));
226 }
227 
228 public cdecl nothrow int close(int fd)
229 {
230     return cast<int>(trap(0utrap_close0ufd));
231 }
232 
233 public cdecl nothrow long read(int fdvoid* bufferlong count)
234 {
235     return cast<long>(trap(0utrap_read0ufdbuffercount));
236 }
237 
238 public cdecl nothrow long write(int fdvoid* bufferlong count)
239 {
240     return cast<long>(trap(0utrap_write0ufdbuffercount));
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 fdIOControlItem itembyte* arglong argSize)
265 {
266     return cast<int>(trap(0utrap_ioctl0ufditemargargSize));
267 }
268 
269 public cdecl nothrow int unlink(const char* path)
270 {
271     return cast<int>(trap(0utrap_unlink0upath));
272 }
273 
274 public enum Origin : int
275 {
276     seekSet = 0seekCur = 1seekEnd = 2
277 }
278     
279 public cdecl nothrow long seek(int fdlong offsetOrigin whence)
280 {
281     return cast<long>(trap(0utrap_seek0ufdoffsetcast<int>(whence)));
282 }
283 
284 public cdecl nothrow long tell(int fd)
285 {
286     return cast<long>(trap(0utrap_tell0ufd));
287 }
288 
289 public cdecl nothrow int stat(const char* pathbyte* statBufint statBufSize)
290 {
291     return cast<int>(trap(0utrap_stat0upathstatBufstatBufSize));
292 }
293 
294 public cdecl nothrow int getcwd(char* pathBuflong bufSize)
295 {
296     return cast<int>(trap(0utrap_getcwd0upathBufbufSize));
297 }
298 
299 public cdecl nothrow int chdir(const char* path)
300 {
301     return cast<int>(trap(0utrap_chdir0upath));
302 }
303 
304 public cdecl nothrow int mkdir(const char* pathint mode)
305 {
306     return cast<int>(trap(0utrap_mkdir0upathmode));
307 }
308 
309 public cdecl nothrow int utime(const char* pathbyte* timeBuflong timeBufSize)
310 {
311     return cast<int>(trap(0utrap_utime0upathtimeBuftimeBufSize));
312 }
313 
314 public cdecl nothrow int mcpy(byte* sourceBufferbyte* destBufferlong count)
315 {
316     return cast<int>(trap(0utrap_mcpy0usourceBufferdestBuffercount));
317 }
318 
319 public cdecl nothrow int opendir(const char* path)
320 {
321     return cast<int>(trap(0utrap_opendir0upath));
322 }
323 
324 public cdecl nothrow int closedir(int dd)
325 {
326     return cast<int>(trap(0utrap_closedir0udd));
327 }
328 
329 public cdecl nothrow int readdir(int ddbyte* dirEntryBuflong dirEntryBufSize)
330 {
331     return cast<int>(trap(0utrap_readdir0udddirEntryBufdirEntryBufSize));
332 }
333 
334 public cdecl nothrow int open_resource(const char* resource_name)
335 {
336     return cast<int>(trap(0utrap_open_resource0uresource_name));
337 }
338 
339 public cdecl nothrow int close_resource(int rd)
340 {
341     return cast<int>(trap(0utrap_close_resource0urd));
342 }
343 
344 public cdecl nothrow long get_resource_size(int rd)
345 {
346     return cast<long>(trap(0utrap_get_resource_size0urd));
347 }
348 
349 public cdecl nothrow int read_resource(int rdlong offsetlong lengthbyte* buffer)
350 {
351     return cast<int>(trap(0utrap_read_resource0urdoffsetlengthbuffer));
352 }
353 
354 public cdecl nothrow int decompress(byte* bufferlong count)
355 {
356     return cast<int>(trap(0utrap_decompress0ubuffercount));
357 }
358 
359 public cdecl nothrow long get_decompressed_data_size(int dd)
360 {
361     return cast<long>(trap(0utrap_get_decompressed_data_size0udd));
362 }
363 
364 public cdecl nothrow int get_decompressed_data(int ddbyte* bufferlong count)
365 {
366     return cast<int>(trap(0utrap_get_decompressed_data0uddbuffercount));
367 }
368 
369 public cdecl nothrow int close_decompression(int dd)
370 {
371     return cast<int>(trap(0utrap_close_decompression0udd));
372 }
373 
374 public cdecl nothrow int get_host_name(char* bufferlong bufSize)
375 {
376     return cast<int>(trap(0utrap_get_host_name0ubufferbufSize));
377 }
378 
379 public cdecl nothrow int get_user_name(char* bufferlong bufSize)
380 {
381     return cast<int>(trap(0utrap_get_user_name0ubufferbufSize));
382 }
383 
384 public cdecl nothrow int pipe(int* readerFdint* writerFd)
385 {
386     return cast<int>(trap(0utrap_pipe0ureaderFdwriterFd));
387 }
388 
389 public cdecl nothrow int dup(int fd)
390 {
391     return cast<int>(trap(0utrap_dup0ufd));
392 }
393 
394 public cdecl nothrow int setuid(int uid)
395 {
396     return cast<int>(trap(0utrap_setuid0uuid));
397 }
398 
399 public cdecl nothrow int setgid(int gid)
400 {
401     return cast<int>(trap(0utrap_setgid0ugid));
402 }
403 
404 public cdecl nothrow int getuid()
405 {
406     return cast<int>(trap(0utrap_getuid0u));
407 }
408 
409 public cdecl nothrow int getgid()
410 {
411     return cast<int>(trap(0utrap_getgid0u));
412 }
413 
414 public cdecl nothrow int seteuid(int euid)
415 {
416     return cast<int>(trap(0utrap_seteuid0ueuid));
417 }
418 
419 public cdecl nothrow int setegid(int egid)
420 {
421     return cast<int>(trap(0utrap_setegid0uegid));
422 }
423 
424 public cdecl nothrow int geteuid()
425 {
426     return cast<int>(trap(0utrap_geteuid0u));
427 }
428 
429 public cdecl nothrow int getegid()
430 {
431     return cast<int>(trap(0utrap_getegid0u));
432 }
433 
434 public cdecl nothrow int umask(int mask)
435 {
436     return cast<int>(trap(0utrap_umask0umask));
437 }
438 
439 public cdecl nothrow int chmod(const char* pathint mode)
440 {
441     return cast<int>(trap(0utrap_chmod0upathmode));
442 }
443 
444 public cdecl nothrow int chown(const char* pathint uidint gid)
445 {
446     return cast<int>(trap(0utrap_chown0upathuidgid));
447 }
448 
449 public cdecl nothrow int rename(const char* sourcePathconst char* targetPath)
450 {
451     return cast<int>(trap(0utrap_rename0usourcePathtargetPath));
452 }
453 
454 public cdecl nothrow int add_dir_change_notification(const char* directories)
455 {
456     return cast<int>(trap(0utrap_add_dir_change_notification0udirectories));
457 }
458 
459 public cdecl nothrow int remove_dir_change_notifications()
460 {
461     return cast<int>(trap(0utrap_remove_dir_change_notifications0u));
462 }
463 
464 public cdecl nothrow int directories_changed()
465 {
466     return cast<int>(trap(0utrap_directories_changed0u));
467 }
468 
469 public cdecl nothrow int clear_directories_changed()
470 {
471     return cast<int>(trap(0utrap_clear_directories_changed0u));
472 }
473 
474 public cdecl nothrow int child_times(long* childUserTimelong* childSleepTimelong* childSystemTime)
475 {
476     return cast<int>(trap(0utrap_child_times0uchildUserTimechildSleepTimechildSystemTime));
477 }
478 
479 public cdecl nothrow int msgq(const char* name)
480 {
481     return cast<int>(trap(0utrap_msgq0uname));
482 }
483 
484 public cdecl nothrow int close_msgq(int md)
485 {
486     return cast<int>(trap(0utrap_close_msgq0umd));
487 }
488 
489 public cdecl nothrow int wait_msg(int md)
490 {
491     return cast<int>(trap(0utrap_wait_msg0umd));
492 }
493 
494 public cdecl nothrow int put_msg(int mdbyte* msgDataint msgSize)
495 {
496     return cast<int>(trap(0utrap_put_msg0umdmsgDatamsgSize));
497 }
498 
499 public cdecl nothrow int get_msgq_length(int md)
500 {
501     return cast<int>(trap(0utrap_get_msgq_length0umd));
502 }
503 
504 public cdecl nothrow int get_msg_size(int md)
505 {
506     return cast<int>(trap(0utrap_get_msg_size0umd));
507 }
508 
509 public cdecl nothrow int get_msg(int mdbyte* buffer)
510 {
511     return cast<int>(trap(0utrap_get_msg0umdbuffer));
512 }
513 
514 public cdecl nothrow int bind_terminal(int md)
515 {
516     return cast<int>(trap(0utrap_bind_terminal0umd));
517 }
518 
519 public cdecl nothrow int unbind_terminal()
520 {
521     return cast<int>(trap(0utrap_unbind_terminal0u));
522 }
523 
524 public cdecl nothrow int timer_msg(long durationint mdbyte* msgDataint msgSize)
525 {
526     return cast<int>(trap(0utrap_timer_msg0udurationmdmsgDatamsgSize));
527 }
528 
529 public cdecl nothrow int connect(const char* nodeconst char* service)
530 {
531     return cast<int>(trap(0utrap_connect0unodeservice));
532 }
533 
534 public cdecl nothrow int get_debug_mode()
535 {
536     return cast<int>(trap(0utrap_get_debug_mode0u));
537 }
538 
539 public cdecl nothrow int set_debug_mode(int mode)
540 {
541     return cast<int>(trap(0utrap_set_debug_mode0umode));
542 }
543 
544 public cdecl nothrow int write_debug_message(const char* msg)
545 {
546     return cast<int>(trap(0utrap_write_debug_message0umsg));
547 }
548 
549 public cdecl nothrow int kill(int pid)
550 {
551     return cast<int>(trap(0utrap_kill0upid));
552 }
553 
554 public cdecl nothrow int waitpid(int pidbyte* exitCode)
555 {
556     return cast<int>(trap(0utrap_waitpid0upidexitCode));
557 }