omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / tests / native / v2 / host / hfile.om
| 1 | note hfile.om: an OM-2 unit for the host-word checks of tests/native/v2/run.sh (section 11). It uses all |
| 2 | note nine function-mode host words a bridged unit may use (getuid geteuid getgid getegid stat lstat fopen |
| 3 | note fstat fclose) and read, so tools/native refuses it without --host and accepts it with --host. |
| 4 | om2 |
| 5 | export fn ids to ulong |
| 6 | a = ulong getuid |
| 7 | b = ulong geteuid |
| 8 | c = ulong getgid |
| 9 | e = ulong getegid |
| 10 | ret add add a b add c e |
| 11 | end |
| 12 | |
| 13 | note peek p: lstat and stat p, open it (O_NOFOLLOW O_NONBLOCK O_CLOEXEC, 559104 on AArch64), fstat the |
| 14 | note descriptor, read up to 16 bytes, close; the byte count read, or minus errno of the first failure. |
| 15 | export fn peek p view uchar * to long |
| 16 | s = new uchar 128 0 |
| 17 | r = lstat p 0 len p s 0 128 |
| 18 | if ne r 0 |
| 19 | ret r |
| 20 | end |
| 21 | r = stat p 0 len p s 0 128 |
| 22 | if ne r 0 |
| 23 | ret r |
| 24 | end |
| 25 | fd = fopen p 0 len p 559104 0 |
| 26 | if lt fd 0 |
| 27 | ret fd |
| 28 | end |
| 29 | b = new uchar 16 0 |
| 30 | k = fstat int fd s 0 128 |
| 31 | n = read int fd b 0 16 |
| 32 | z = fclose int fd |
| 33 | if ne k 0 |
| 34 | ret k |
| 35 | end |
| 36 | ret n |
| 37 | end |
| 38 | |
| 39 | note size p: the st_size field (offset 48 of the 128-byte AArch64 stat) of p, or minus errno. |
| 40 | export fn size p view uchar * to long |
| 41 | s = new uchar 128 0 |
| 42 | r = stat p 0 len p s 0 128 |
| 43 | if ne r 0 |
| 44 | ret r |
| 45 | end |
| 46 | x = ulong 0 |
| 47 | for i ulong 8 |
| 48 | x = or x shl ulong get s add 48 i mul i 8 |
| 49 | end |
| 50 | ret long x |
| 51 | end |