Files
zern/src/std/std.zr

1010 lines
26 KiB
Plaintext

const ERR_NONE = 0
const ERR_SYSCALL_FAILED = 1
const ERR_OPEN_FAILED = 2
const ERR_READ_FAILED = 3
const ERR_WRITE_FAILED = 4
const ERR_ALLOC_FAILED = 5
const ERR_CONNECTION_FAILED = 6
func err.code[] : i64
return mem.read64(_builtin_err_code())
func err.msg[] : str
return mem.read64(_builtin_err_msg())
func err.check[] : bool
return err.code() != 0
func err.clear[] : void
err.set(0, 0)
func err.set[code: i64, msg: str] : void
mem.write64(_builtin_err_code(), code)
mem.write64(_builtin_err_msg(), msg)
func must[x: any] : any
if err.check()
panic(err.msg())
return x
func panic[msg: str] : void
io.print("PANIC: ")
io.println(msg)
// for gdb backtrace
_builtin_syscall(SYS_kill, os.getpid(), 6)
os.exit(1)
const MEM_BLOCK_SIZE = 32
struct mem.Block
size: i64
free: bool
next: mem.Block
prev: mem.Block
func mem._align[x: i64] : i64
return (x + 7) & -8
func mem._split_block[blk: mem.Block, needed: i64] : void
if blk->size >= needed + MEM_BLOCK_SIZE + 8
let new_blk: mem.Block = (blk as ptr + MEM_BLOCK_SIZE + needed) as mem.Block
new_blk->size = blk->size - needed - MEM_BLOCK_SIZE
new_blk->free = true
new_blk->next = blk->next
new_blk->prev = blk
if blk->next
let next: mem.Block = blk->next
next->prev = new_blk
else
mem.write64(_builtin_heap_tail(), new_blk)
blk->size = needed
blk->next = new_blk
func mem._request_space?[size: i64] : mem.Block
err.clear()
let needed: i64 = size + MEM_BLOCK_SIZE
let alloc_size: i64 = (needed + 4095) & -4096
// PROT_READ | PROT_WRITE = 3
// MAP_PRIVATE | MAP_ANONYMOUS = 34
// fd = -1, offset = 0
let blk_ptr: ptr = _builtin_syscall(SYS_mmap, 0, alloc_size, 3, 34, -1, 0) as ptr
if blk_ptr == -1
err.set(ERR_ALLOC_FAILED, "mem._request_space: mmap failed")
return 0
let blk: mem.Block = blk_ptr as mem.Block
blk->size = alloc_size - MEM_BLOCK_SIZE
blk->free = false
blk->next = 0 as mem.Block
blk->prev = 0 as mem.Block
let tail: mem.Block = mem.read64(_builtin_heap_tail()) as mem.Block
if tail
tail->next = blk
blk->prev = tail
mem.write64(_builtin_heap_tail(), blk)
return blk
func mem.alloc?[size: i64] : ptr
err.clear()
if size <= 0
err.set(ERR_ALLOC_FAILED, "mem.alloc? called with non-positive size")
return 0
size = mem._align(size)
let cur: mem.Block = mem.read64(_builtin_heap_head())
while cur
if cur->free && cur->size >= size
mem._split_block(cur, size)
cur->free = false
return cur + MEM_BLOCK_SIZE
cur = cur->next
let blk: mem.Block = mem._request_space?(size)
if err.check()
return 0
if !mem.read64(_builtin_heap_head())
mem.write64(_builtin_heap_head(), blk)
mem._split_block(blk, size)
return blk + MEM_BLOCK_SIZE
func mem.alloc[size: i64] : ptr
return must(mem.alloc?(size))
func mem.free[x: ptr] : void
if !x
return 0
let blk: mem.Block = x - MEM_BLOCK_SIZE
blk->free = true
let next: mem.Block = blk->next
if next && next->free
let expected_next: mem.Block = blk + MEM_BLOCK_SIZE + blk->size
if expected_next == next
blk->size = blk->size + MEM_BLOCK_SIZE + next->size
blk->next = next->next
if next->next
let b: mem.Block = next->next
b->prev = blk
if mem.read64(_builtin_heap_tail()) == next
mem.write64(_builtin_heap_tail(), blk)
let prev: mem.Block = blk->prev
if prev && prev->free
let expected_blk: mem.Block = prev + MEM_BLOCK_SIZE + prev->size
if expected_blk == blk
prev->size = prev->size + MEM_BLOCK_SIZE + blk->size
prev->next = blk->next
if blk->next
let b: mem.Block = blk->next
b->prev = prev
if mem.read64(_builtin_heap_tail()) == blk
mem.write64(_builtin_heap_tail(), prev)
blk = prev
let block_total: i64 = blk->size + MEM_BLOCK_SIZE
if (blk & 4095) == 0 && (block_total & 4095) == 0
if blk->prev
let b: mem.Block = blk->prev
b->next = blk->next
else
mem.write64(_builtin_heap_head(), blk->next)
if blk->next
let b: mem.Block = blk->next
b->prev = blk->prev
else
mem.write64(_builtin_heap_tail(), blk->prev)
_builtin_syscall(SYS_munmap, blk, block_total)
func mem.realloc?[x: ptr, new_size: i64] : ptr
err.clear()
if !x
return mem.alloc?(new_size)
if new_size < 0
err.set(ERR_ALLOC_FAILED, "mem.realloc? called with negative size")
return 0
if new_size == 0
mem.free(x)
return 0
new_size = mem._align(new_size)
let blk: mem.Block = x - MEM_BLOCK_SIZE
if blk->size >= new_size
mem._split_block(blk, new_size)
return x
let next: mem.Block = blk->next
let expected_next: mem.Block = blk + MEM_BLOCK_SIZE + blk->size
if next && next->free && expected_next == next
let combined: i64 = blk->size + MEM_BLOCK_SIZE + next->size
if combined >= new_size
blk->size = combined
blk->next = next->next
if next->next
let b: mem.Block = next->next
b->prev = blk
if mem.read64(_builtin_heap_tail()) == next
mem.write64(_builtin_heap_tail(), blk)
mem._split_block(blk, new_size)
return x
let new_ptr: ptr = mem.alloc?(new_size)
if err.check()
return 0
mem.copy(x, new_ptr, math.min(blk->size, new_size))
mem.free(x)
return new_ptr
func mem.zero_and_free[x: ptr, size: i64] : void
mem.zero(x, size)
mem.free(x)
func mem.zero[x: ptr, size: i64] : void
for i in 0..size
x[i] = 0
func mem.copy[src: ptr, dst: ptr, n: i64] : void
if dst > src
let i: i64 = n - 1
while i >= 0
dst[i] = src[i]
i = i - 1
else
for i in 0..n
dst[i] = src[i]
func mem.read8[x: ptr] : u8
return x[0]
func mem.read16[x: ptr] : i64
return x[0] | (x[1] << 8)
func mem.read16be[x: ptr] : i64
return (x[0] << 8) | x[1]
func mem.read32[x: ptr] : i64
return x[0] | (x[1] << 8) | (x[2] << 16) | (x[3] << 24)
func mem.read32be[x: ptr] : i64
return (x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3]
func mem.read64[x: ptr] : i64
return _builtin_read64(x)
func mem.write32[x: ptr, d: i64] : void
x[0] = d & 0xff
x[1] = (d >> 8) & 0xff
x[2] = (d >> 16) & 0xff
x[3] = (d >> 24) & 0xff
func mem.write16[x: ptr, d: i64] : void
x[0] = d & 0xff
x[1] = (d >> 8) & 0xff
func mem.write16be[x: ptr, d: i64] : void
x[0] = (d >> 8) & 0xff
x[1] = d & 0xff
func mem.write64[x: ptr, d: i64] : void
_builtin_set64(x, d)
// see emit_prologue for the io.printf wrapper
func io._printf_impl[s: str, args: ptr] : void
while s[0]
if s[0] == '%'
s = s + 1
if s[0] == 'd'
io.print_i64(mem.read64(args))
args = args + 8
else if s[0] == 'x'
io.print_i64_hex(mem.read64(args))
args = args + 8
else if s[0] == 's'
io.print(mem.read64(args))
args = args + 8
else if s[0] == 'c'
io.print_char(mem.read64(args))
args = args + 8
else if s[0] == '%'
io.print_char('%')
else if s[0] == 0
break
else
io.print_char(s[0])
s = s + 1
func io.print_sized[x: str, size: i64] : void
_builtin_syscall(SYS_write, 1, x, size)
func io.print[x: str] : void
io.print_sized(x, str.len(x))
func io.println[x: str] : void
io.print(x)
io.print("\n")
func io.print_char[x: u8] : void
io.print_sized(^x, 1)
func io.print_bool[x: bool] : void
if x
io.print("true")
else
io.print("false")
func io.print_i64[x: i64] : void
let s: str = str.from_i64(x)
io.print(s)
mem.free(s)
func io.print_i64_hex[x: i64] : void
let s: str = str.hex_from_i64(x)
io.print(s)
mem.free(s)
func io.println_i64[x: i64] : void
let s: str = str.from_i64(x)
io.println(s)
mem.free(s)
func io.read_char[] : u8
let c: u8 = 0
_builtin_syscall(SYS_read, 0, ^c, 1)
return c
func io.read_line[]: str
let MAX_SIZE = 60000
let buffer: str = mem.alloc(MAX_SIZE + 1)
let n: i64 = _builtin_syscall(SYS_read, 0, buffer, MAX_SIZE)
if n < 0
n = 0
buffer[n] = 0
buffer = must(mem.realloc?(buffer, n + 1))
return buffer
struct io.Buffer
data: ptr
size: i64
func io.Buffer.alloc?[size: i64] : io.Buffer
err.clear()
let buffer: io.Buffer = new io.Buffer
buffer->size = size
buffer->data = mem.alloc?(size)
if err.check()
mem.free(buffer)
return 0
return buffer
func io.Buffer.free[buf: io.Buffer] : void
mem.free(buf->data)
mem.free(buf)
func io.file_exists[path: str] : bool
return _builtin_syscall(SYS_faccessat, -100, path, 0, 0) == 0
func io.read_text_file?[path: str] : str
err.clear()
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
if fd < 0
err.set(ERR_OPEN_FAILED, "io.read_text_file?: failed to open file")
return 0
let size: i64 = _builtin_syscall(SYS_lseek, fd, 0, 2)
_builtin_syscall(SYS_lseek, fd, 0, 0)
let buffer: str = mem.alloc?(size + 1)
if err.check()
_builtin_syscall(SYS_close, fd)
return 0
let n: i64 = _builtin_syscall(SYS_read, fd, buffer, size)
_builtin_syscall(SYS_close, fd)
if n != size
mem.free(buffer)
err.set(ERR_READ_FAILED, "io.read_text_file?: failed to read file")
return 0
buffer[n] = 0
return buffer
func io.read_binary_file?[path: str] : io.Buffer
err.clear()
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
if fd < 0
err.set(ERR_OPEN_FAILED, "io.read_binary_file?: failed to open file")
return 0
let buf: io.Buffer = new io.Buffer
buf->size = _builtin_syscall(SYS_lseek, fd, 0, 2)
_builtin_syscall(SYS_lseek, fd, 0, 0)
buf->data = mem.alloc?(buf->size)
if err.check()
io.Buffer.free(buf)
_builtin_syscall(SYS_close, fd)
return 0
let n: i64 = _builtin_syscall(SYS_read, fd, buf->data, buf->size)
_builtin_syscall(SYS_close, fd)
if n != buf->size
io.Buffer.free(buf)
err.set(ERR_READ_FAILED, "io.read_binary_file?: failed to read file")
return 0
return buf
func io.write_file?[path: str, content: str] : void
io.write_binary_file?(path, content, str.len(content))
func io.write_binary_file?[path: str, content: ptr, size: i64] : void
err.clear()
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0x241, 0o644)
if fd < 0
err.set(ERR_OPEN_FAILED, "io.write_binary_file?: failed to open file")
return 0
let n: i64 = _builtin_syscall(SYS_write, fd, content, size)
_builtin_syscall(SYS_close, fd)
if n != size
err.set(ERR_WRITE_FAILED, "io.write_binary_file?: failed to write file")
return 0
func str.len[s: str] : i64
let i = 0
while s[i]
i = i + 1
return i
func str.make_copy[s: str] : str
let size: i64 = str.len(s) + 1
let dup: str = mem.alloc(size)
mem.copy(s, dup, size)
return dup
func str.equal[a: str, b: str] : bool
let i = 0
while a[i] != 0 && b[i] != 0
if a[i] != b[i]
return false
i = i + 1
return a[i] == b[i]
func str.is_whitespace[x: u8] : bool
return x == ' ' || x == 10 || x == 13 || x == 9
func str.is_digit[x: u8] : bool
return x >= '0' && x <= '9'
func str.is_hex_digit[x: u8] : bool
return (x >= '0' && x <= '9') || (x >= 'a' && x <= 'f') || (x >= 'A' && x <= 'F')
func str.is_lowercase[x: u8] : bool
return x >= 'a' && x <= 'z'
func str.is_uppercase[x: u8] : bool
return x >= 'A' && x <= 'Z'
func str.is_letter[x: u8] : bool
return str.is_uppercase(x) || str.is_lowercase(x)
func str.is_alphanumeric[x: u8] : bool
return str.is_letter(x) || str.is_digit(x)
func str.concat[a: str, b: str] : str
let a_len: i64 = str.len(a)
let b_len: i64 = str.len(b)
let out: str = mem.alloc(a_len + b_len + 1)
mem.copy(a, out, a_len)
mem.copy(b, out + a_len, b_len)
out[a_len + b_len] = 0
return out
func str.contains[haystack: str, needle: str] : bool
return str.find(haystack, needle) != -1
func str.find[haystack: str, needle: str] : i64
let haystack_len: i64 = str.len(haystack)
let needle_len: i64 = str.len(needle)
if needle_len == 0
return 0
if needle_len > haystack_len
return -1
for i in 0..(haystack_len - needle_len + 1)
let match: bool = true
for j in 0..needle_len
if haystack[i + j] != needle[j]
match = false
break
if match
return i
return -1
func str.substr[s: str, start: i64, length: i64] : str
if start < 0 || length < 0 || start + length > str.len(s)
panic("str.substr out of bounds")
let out: str = mem.alloc(length + 1)
mem.copy(s + start, out, length)
out[length] = 0
return out
func str.trim[s: str] : str
let len: i64 = str.len(s)
if len == 0
let out: str = mem.alloc(1)
out[0] = 0
return out
let start = 0
let end: i64 = len - 1
while start <= end && str.is_whitespace(s[start])
start = start + 1
while end >= start && str.is_whitespace(s[end])
end = end - 1
return str.substr(s, start, end - start + 1)
func str.split[haystack: str, needle: str]: Array
let haystack_len: i64 = str.len(haystack)
let needle_len: i64 = str.len(needle)
let result: Array = []
if !needle_len
if !haystack_len
return result
else
for i in 0..haystack_len
array.push(result, str.substr(haystack, i, 1))
return result
let start = 0
let i = 0
while i < haystack_len
if i <= haystack_len - needle_len
let match: bool = true
for j in 0..needle_len
if haystack[i + j] != needle[j]
match = false
break
if match
array.push(result, str.substr(haystack, start, i - start))
start = i + needle_len
i = i + needle_len
continue
i = i + 1
array.push(result, str.substr(haystack, start, haystack_len - start))
return result
func str.reverse[s: str] : str
let len: i64 = str.len(s)
let out: str = mem.alloc(len + 1)
for i in 0..len
out[i] = s[len - i - 1]
out[len] = 0
return out
func str.from_i64[n: i64] : str
if n == 0
let out: str = mem.alloc(2)
out[0] = '0'
out[1] = 0
return out
let neg: bool = n < 0
if neg
// negating MIN_I64 causes overflow
if n == -9223372036854775808
return str.make_copy("-9223372036854775808")
n = -n
let buf: str = mem.alloc(21) // enough to fit -MAX_I64
let end = 20
buf[end] = 0
end = end - 1
while n > 0
buf[end] = '0' + (n % 10)
n = n / 10
end = end - 1
if neg
buf[end] = '-'
end = end - 1
let s: str = str.make_copy(buf + end + 1)
mem.free(buf)
return s
func str.hex_from_i64[n: i64] : str
let hex_chars: str = "0123456789abcdef"
if n == 0
let out: str = mem.alloc(2)
out[0] = '0'
out[1] = 0
return out
let mask: i64 = (1 << 60) - 1
let buf: str = mem.alloc(17)
let len = 0
while n != 0
buf[len] = hex_chars[n & 15]
n = (n >> 4) & mask
len = len + 1
let out: str = mem.alloc(len + 1)
let j = 0
while j < len
out[j] = buf[len - 1 - j]
j = j + 1
out[len] = 0
mem.free(buf)
return out
func str.from_char[c: u8] : str
let s: str = mem.alloc(2)
s[0] = c
s[1] = 0
return s
func str.parse_i64[s: str] : i64
let len: i64 = str.len(s)
let i = 0
let sign = 1
if i < len && s[i] == '-'
sign = -1
i = i + 1
let num = 0
while i < len
let d: u8 = s[i]
if d < '0' || d > '9'
break
num = num * 10 + (d - '0')
i = i + 1
return num * sign
func str.hex_encode[s: str, s_len: i64] : str
let hex_chars: str = "0123456789abcdef"
let j = 0
let out: str = mem.alloc(s_len * 2 + 1)
for i in 0..s_len
let high: u8 = (s[i] >> 4) & 15
let low: u8 = s[i] & 15
out[j] = hex_chars[high]
out[j + 1] = hex_chars[low]
j = j + 2
out[j] = 0
return out
func str._hex_digit_to_int[d: u8] : i64
if d >= 'a' && d <= 'f'
return d - 'a' + 10
if d >= 'A' && d <= 'F'
return d - 'A' + 10
return d - '0'
func str.hex_decode[s: str] : str
let s_len: i64 = str.len(s)
if s_len % 2 != 0
panic("invalid hex string passed to str.hex_decode")
let i = 0
let j = 0
let out: str = mem.alloc(s_len / 2 + 1)
while i < s_len
out[j] = str._hex_digit_to_int(s[i]) * 16 + str._hex_digit_to_int(s[i + 1])
i = i + 2
j = j + 1
out[j] = 0
return out
func math.gcd[a: i64, b: i64] : i64
a = math.abs(a)
b = math.abs(b)
while b != 0
let tmp: i64 = b
b = a % b
a = tmp
return a
func math.min[a: i64, b: i64] : i64
if a < b
return a
return b
func math.max[a: i64, b: i64] : i64
if a > b
return a
return b
func math.abs[n: i64] : i64
if n == -9223372036854775808
panic("MIN_I64 passed to math.abs")
if n < 0
return -n
return n
func math.sign[n: i64] : i64
if n < 0
return -1
else if n > 0
return 1
else
return 0
func math.pow[b: i64, e: i64] : i64
if e < 0
panic("negative exponent passed to math.pow")
let out = 1
for i in 0..e
out = out * b
return out
func math.lcm[a: i64, b: i64] : i64
return (a / math.gcd(a, b)) * b
func math.isqrt[n: i64] : i64
if n < 0
panic("negative number passed to math.isqrt")
if n == 0 || n == 1
return n
let guess: i64 = n
let next_guess: i64 = (guess + n / guess) / 2
while next_guess < guess
guess = next_guess
next_guess = (guess + n / guess) / 2
return guess
func math.is_prime[n: i64]: bool
if n <= 1
return false
if n == 2 || n == 3
return true
if n % 2 == 0 || n % 3 == 0
return false
let i = 5
while i * i <= n
if n % i == 0 || n % (i + 2) == 0
return false
i = i + 6
return true
struct Array
data: ptr
size: i64
capacity: i64
func array.new[] : Array
return new Array
func array.new_with_size_zeroed[size: i64] : Array
let xs: Array = new Array
if size > 0
xs->data = must(mem.realloc?(xs->data, size * 8))
mem.zero(xs->data, size * 8)
xs->size = size
xs->capacity = size
return xs
func array.nth[xs: Array, n: i64] : any
if n < 0 || n >= xs->size
panic("array.nth out of bounds")
return mem.read64(xs->data + n * 8)
func array.set[xs: Array, n: i64, x: any] : void
if n < 0 || n >= xs->size
panic("array.set out of bounds")
mem.write64(xs->data + n * 8, x)
func array.push[xs: Array, x: any] : void
if xs->size == xs->capacity
let new_capacity = 4
if xs->capacity != 0
new_capacity = xs->capacity * 2
xs->data = must(mem.realloc?(xs->data, new_capacity * 8))
xs->capacity = new_capacity
mem.write64(xs->data + xs->size * 8, x)
xs->size = xs->size + 1
func array.free[xs: Array] : void
if xs->data != 0
mem.free(xs->data)
mem.free(xs)
func array.pop[xs: Array] : any
if xs->size == 0
panic("array.pop on empty array")
let x: any = array.nth(xs, xs->size - 1)
xs->size = xs->size - 1
return x
func array.slice[xs: Array, start: i64, length: i64] : Array
if start < 0 || length < 0 || start + length > xs->size
panic("array.slice out of bounds")
let new_array: Array = array.new_with_size_zeroed(length)
for i in 0..length
array.set(new_array, i, array.nth(xs, start + i))
return new_array
func array.concat[a: Array, b: Array] : Array
let new_array: Array = array.new_with_size_zeroed(a->size + b->size)
for i in 0..a->size
array.set(new_array, i, array.nth(a, i))
for i in 0..b->size
array.set(new_array, a->size + i, array.nth(b, i))
return new_array
func alg.quicksort[arr: Array] : void
alg._do_quicksort(arr, 0, arr->size - 1)
func alg._do_quicksort[arr: Array, low: i64, high: i64] : void
if low < high
let i: i64 = alg._partition(arr, low, high)
alg._do_quicksort(arr, low, i - 1)
alg._do_quicksort(arr, i + 1, high)
func alg._partition[arr: Array, low: i64, high: i64] : i64
let pivot: i64 = array.nth(arr, high)
let i: i64 = low - 1
for j in (low)..high
if array.nth(arr, j) <= pivot
i = i + 1
let temp: i64 = array.nth(arr ,i)
array.set(arr, i, array.nth(arr, j))
array.set(arr, j, temp)
let temp: i64 = array.nth(arr, i + 1)
array.set(arr, i + 1, array.nth(arr, high))
array.set(arr, high, temp)
return i + 1
func alg.count[arr: Array, item: any] : i64
let count = 0
for i in 0..arr->size
if array.nth(arr, i) == item
count = count + 1
return count
func alg.map[arr: Array, fn: ptr] : Array
let out: Array = array.new_with_size_zeroed(arr->size)
for i in 0..arr->size
array.set(out, i, fn(array.nth(arr, i)))
return out
func alg.filter[arr: Array, fn: ptr] : Array
let out: Array = []
for i in 0..arr->size
if fn(array.nth(arr, i))
array.push(out, array.nth(arr, i))
return out
func alg.reduce[arr: Array, fn: ptr, acc: any] : any
for i in 0..arr->size
acc = fn(acc, array.nth(arr, i))
return acc
func os.exit[code: i64] : void
_builtin_syscall(SYS_exit, code)
func os.getpid[] : i64
return _builtin_syscall(SYS_getpid)
struct os.Timespec
tv_sec: i64
tv_nsec: i64
func os.sleep[ms: i64] : void
if ms < 0
panic("negative time passed to os.sleep")
// TODO: we really shouldnt need a heap allocation to sleep
let req: os.Timespec = new os.Timespec
req->tv_sec = ms / 1000
req->tv_nsec = (ms % 1000) * 1000000
_builtin_syscall(SYS_nanosleep, req, 0)
mem.free(req)
func os.urandom?[n: i64]: ptr
err.clear()
let buffer: ptr = mem.alloc(n)
if err.check()
return 0
let fd: i64 = _builtin_syscall(SYS_openat, -100, "/dev/urandom", 0, 0)
if fd < 0
mem.free(buffer)
err.set(ERR_READ_FAILED, "os.urandom?: failed to open /dev/urandom")
return 0
let bytes_read: i64 = _builtin_syscall(SYS_read, fd, buffer, n)
_builtin_syscall(SYS_close, fd)
if n != bytes_read
mem.free(buffer)
err.set(ERR_READ_FAILED, "os.urandom?: failed to read /dev/urandom")
return 0
return buffer
func os.urandom_i64[]: i64
let buffer: ptr = must(os.urandom?(8))
let n: i64 = mem.read64(buffer)
mem.free(buffer)
return n
struct os.Timeval
tv_sec: i64
tv_usec: i64
func os.time[] : i64
// TODO: we really shouldnt need a heap allocation to check the time
let tv: os.Timeval = new os.Timeval
_builtin_syscall(SYS_gettimeofday, tv, 0)
let out: i64 = tv->tv_sec * 1000 + tv->tv_usec / 1000
mem.free(tv)
return out
// voodoo magic
func os.run_shell_command?[command: str] : i64
err.clear()
let pid: i64 = _builtin_syscall(SYS_fork)
if pid < 0
err.set(ERR_SYSCALL_FAILED, "os.run_shell_command?: failed to fork")
return -1
if pid == 0
let argv: Array = ["sh", "-c", command, 0] // gets freed after child process exits
_builtin_syscall(SYS_execve, "/bin/sh", argv->data, _builtin_environ())
_builtin_syscall(SYS_exit, 1)
else
let status = 0
let wp: i64 = _builtin_syscall(SYS_wait4, pid, ^status, 0, 0)
if wp < 0
err.set(ERR_SYSCALL_FAILED, "os.run_shell_command?: wait() failed")
return -1
let st: i64 = status & 0xffffffff
if (st & 0x7f) == 0
return (st >> 8) & 0xff
else
return -(st & 0x7f)
func os.list_directory?[path: str] : Array
err.clear()
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
if fd < 0
err.set(ERR_OPEN_FAILED, "os.list_directory?: failed to open dir")
return 0
let files: Array = []
let buf: ptr = mem.alloc(1024)
while true
let n: i64 = _builtin_syscall(SYS_getdents64, fd, buf, 1024)
if n < 0
mem.free(buf)
for i in 0..files->size
mem.free(array.nth(files, i))
array.free(files)
_builtin_syscall(SYS_close, fd)
err.set(ERR_SYSCALL_FAILED, "os.list_directory?: getdents64() failed")
return 0
else if n == 0
break
let pos = 0
while pos < n
let len: i64 = mem.read16(buf + pos + 16)
let name: str = buf + pos + 19
if name[0]
let skip: bool = false
// skip if name is exactly '.' or '..'
if name[0] == '.'
if name[1] == 0
skip = true
else if name[1] == '.'
if name[2] == 0
skip = true
if !skip
array.push(files, str.make_copy(name))
pos = pos + len
mem.free(buf)
_builtin_syscall(SYS_close, fd)
return files