Initial project import

This commit is contained in:
Agila Forge Importer committed 2026-07-13 15:20:32 +00:00
commit 23f2d29e32
11957 files changed
+1754577

No files matched your search

+33
View File
@@ -0,0 +1,33 @@
@[has_globals]
module builtin
// Shitty `sbrk` basic `malloc` and `free` impl
// TODO: implement pure V `walloc` later
__global g_heap_base = usize(vwasm_heap_base())
// malloc dynamically allocates a `n` bytes block of memory on the heap.
// malloc returns a `byteptr` pointing to the memory address of the allocated space.
// unlike the `calloc` family of functions - malloc will not zero the memory block.
@[unsafe]
pub fn malloc(n isize) &u8 {
if n <= 0 {
$if no_imports ? {
return unsafe { nil }
} $else {
panic('malloc(n <= 0)')
}
}
res := g_heap_base
g_heap_base += usize(n)
return &u8(res)
}
// free allows for manually freeing memory allocated at the address `ptr`.
// currently does not free any memory.
@[unsafe]
pub fn free(ptr voidptr) {
_ := ptr
}
@@ -0,0 +1,21 @@
module builtin
fn JS.__panic_abort(&u8, int)
fn JS.__writeln(&u8, int)
// panic calls the `__panic_abort` JS panic handler.
@[noreturn]
pub fn panic(s string) {
JS.__panic_abort(s.str, s.len)
for {}
}
// println prints a message with a line end, to stdout. stdout is flushed.
pub fn println(s string) {
JS.__writeln(s.str, s.len)
}
// eprintln prints a message with a line end, to the browser output host.
pub fn eprintln(s string) {
JS.__writeln(s.str, s.len)
}
+119
View File
@@ -0,0 +1,119 @@
module builtin
// vwasm_heap_base returns the base address of the heap.
pub fn vwasm_heap_base() voidptr {
mut rval := unsafe { nil }
asm wasm {
global.get __heap_base
local.set rval
; =r (rval)
}
return rval
}
// vwasm_heap_size returns the size of the main wasm memory in pages.
// Analogous to the `memory.size` instruction.
pub fn vwasm_memory_size() int {
mut rval := 0
asm wasm {
memory.size
local.set rval
; =r (rval)
}
return rval
}
// vwasm_memory_grow grows the main wasm memory by `size` pages.
// Analogous to the `memory.grow` instruction.
pub fn vwasm_memory_grow(size int) int {
mut rval := 0
asm wasm {
local.get size
memory.grow
local.set rval
; =r (rval)
; r (size)
}
return rval
}
// vcalloc dynamically allocates a zeroed `n` bytes block of memory on the heap.
// vcalloc returns a `byteptr` pointing to the memory address of the allocated space.
// vcalloc checks for negative values given in `n`.
@[unsafe]
pub fn vcalloc(n isize) &u8 {
if n <= 0 {
$if no_imports ? {
return unsafe { nil }
} $else {
panic('valloc(n <= 0)')
}
}
res := unsafe { malloc(n) }
asm wasm {
local.get res
i32.const 0x0
local.get n
memory.fill
; ; r (res)
r (n)
}
return res
}
// isnil returns true if an object is nil (only for C objects).
@[inline]
pub fn isnil(v voidptr) bool {
return v == 0
}
// vmemcpy copies n bytes from memory area src to memory area dest.
// The memory areas **CAN** overlap. vmemcpy returns a pointer to `dest`.
@[unsafe]
pub fn vmemcpy(dest voidptr, const_src voidptr, n isize) voidptr {
asm wasm {
local.get dest
local.get const_src
local.get n
memory.copy
; ; r (dest)
r (const_src)
r (n)
}
return dest
}
// vmemmove copies n bytes from memory area src to memory area dest.
// The memory areas **CAN** overlap. vmemmove returns a pointer to `dest`.
@[unsafe]
pub fn vmemmove(dest voidptr, const_src voidptr, n isize) voidptr {
asm wasm {
local.get dest
local.get const_src
local.get n
memory.copy
; ; r (dest)
r (const_src)
r (n)
}
return dest
}
// vmemset fills the first `n` bytes of the memory area pointed to by `s`,
// with the constant byte `c`. It returns a pointer to the memory area `s`.
@[unsafe]
pub fn vmemset(s voidptr, c int, n isize) voidptr {
asm wasm {
local.get s
local.get c
local.get n
memory.fill
; ; r (s)
r (c)
r (n)
}
return s
}
@@ -0,0 +1,258 @@
module builtin
pub type byte = u8
// type i32 = int
// digit pairs in reverse order
const digit_pairs = '00102030405060708090011121314151617181910212223242526272829203132333435363738393041424344454647484940515253545556575859506162636465666768696071727374757677787970818283848586878889809192939495969798999'
pub const min_i8 = i8(-128)
pub const max_i8 = i8(127)
pub const min_i16 = i16(-32768)
pub const max_i16 = i16(32767)
pub const min_i32 = i32(-2147483648)
pub const max_i32 = i32(2147483647)
// -9223372036854775808 is wrong, because C compilers parse literal values
// without sign first, and 9223372036854775808 overflows i64, hence the
// consecutive subtraction by 1
pub const min_i64 = i64(-9223372036854775807 - 1)
pub const max_i64 = i64(9223372036854775807)
pub const min_int = int(min_i32)
pub const max_int = int(max_i32)
pub const min_u8 = u8(0)
pub const max_u8 = u8(255)
pub const min_u16 = u16(0)
pub const max_u16 = u16(65535)
pub const min_u32 = u32(0)
pub const max_u32 = u32(4294967295)
pub const min_u64 = u64(0)
pub const max_u64 = u64(18446744073709551615)
// This implementation is the quickest with gcc -O2
// str_l returns the string representation of the integer nn with max chars.
@[direct_array_access; inline]
fn (nn int) str_l(max int) string {
unsafe {
mut n := i64(nn)
mut d := 0
if n == 0 {
return '0'
}
mut is_neg := false
if n < 0 {
n = -n
is_neg = true
}
mut index := max
mut buf := malloc(max + 1)
buf[index] = 0
index--
for n > 0 {
n1 := int(n / 100)
// calculate the digit_pairs start index
d = int(u32(int(n) - (n1 * 100)) << 1)
n = n1
buf[index] = digit_pairs.str[d]
index--
d++
buf[index] = digit_pairs.str[d]
index--
}
index++
// remove head zero
if d < 20 {
index++
}
// Prepend - if it's negative
if is_neg {
index--
buf[index] = `-`
}
diff := max - index
vmemmove(buf, voidptr(buf + index), diff + 1)
return tos(buf, diff)
// return tos(memdup(&buf[0] + index, (max - index)), (max - index))
}
}
// str returns the value of the `u8` as a `string`.
// Example: assert u8(2).str() == '2'
pub fn (n u8) str() string {
return int(n).str_l(5)
}
// str returns the value of the `i8` as a `string`.
// Example: assert i8(-2).str() == '-2'
pub fn (n i8) str() string {
return int(n).str_l(5)
}
// str returns the value of the `i16` as a `string`.
// Example: assert i16(-20).str() == '-20'
pub fn (n i16) str() string {
return int(n).str_l(7)
}
// str returns the value of the `u16` as a `string`.
// Example: assert u16(20).str() == '20'
pub fn (n u16) str() string {
return int(n).str_l(7)
}
// str returns the value of the `int` as a `string`.
// Example: assert int(-2020).str() == '-2020'
pub fn (n int) str() string {
return n.str_l(12)
}
// str returns the value of the `u32` as a `string`.
// Example: assert u32(20000).str() == '20000'
@[direct_array_access; inline]
pub fn (nn u32) str() string {
unsafe {
mut n := nn
mut d := u32(0)
if n == 0 {
return '0'
}
max := 12
mut buf := malloc(max + 1)
mut index := max
buf[index] = 0
index--
for n > 0 {
n1 := n / u32(100)
d = ((n - (n1 * u32(100))) << u32(1))
n = n1
buf[index] = digit_pairs[int(d)]
index--
d++
buf[index] = digit_pairs[int(d)]
index--
}
index++
// remove head zero
if d < u32(20) {
index++
}
diff := max - index
vmemmove(buf, voidptr(buf + index), diff + 1)
return tos(buf, diff)
// return tos(memdup(&buf[0] + index, (max - index)), (max - index))
}
}
// str returns the value of the `int_literal` as a `string`.
@[inline]
pub fn (n int_literal) str() string {
return i64(n).str()
}
// str returns the value of the `i64` as a `string`.
// Example: assert i64(-200000).str() == '-200000'
@[direct_array_access; inline]
pub fn (nn i64) str() string {
unsafe {
mut n := nn
mut d := i64(0)
if n == 0 {
return '0'
} else if n == min_i64 {
// math.min_i64
return '-9223372036854775808'
}
max := 20
mut buf := malloc(max + 1)
mut is_neg := false
if n < 0 {
n = -n
is_neg = true
}
mut index := max
buf[index] = 0
index--
for n > 0 {
n1 := n / i64(100)
d = (u32(n - (n1 * i64(100))) << i64(1))
n = n1
buf[index] = digit_pairs[int(d)]
index--
d++
buf[index] = digit_pairs[int(d)]
index--
}
index++
// remove head zero
if d < i64(20) {
index++
}
// Prepend - if it's negative
if is_neg {
index--
buf[index] = `-`
}
diff := max - index
vmemmove(buf, voidptr(buf + index), diff + 1)
return tos(buf, diff)
// return tos(memdup(&buf[0] + index, (max - index)), (max - index))
}
}
// str returns the value of the `u64` as a `string`.
// Example: assert u64(2000000).str() == '2000000'
@[direct_array_access; inline]
pub fn (nn u64) str() string {
unsafe {
mut n := nn
mut d := u64(0)
if n == 0 {
return '0'
}
max := 20
mut buf := malloc(max + 1)
mut index := max
buf[index] = 0
index--
for n > 0 {
n1 := n / 100
d = ((n - (n1 * 100)) << 1)
n = n1
buf[index] = digit_pairs[int(d)]
index--
d++
buf[index] = digit_pairs[int(d)]
index--
}
index++
// remove head zero
if d < 20 {
index++
}
diff := max - index
vmemmove(buf, voidptr(buf + index), diff + 1)
return tos(buf, diff)
// return tos(memdup(&buf[0] + index, (max - index)), (max - index))
}
}
// str returns the value of the `bool` as a `string`.
// Example: assert (2 > 1).str() == 'true'
pub fn (b bool) str() string {
if b {
return 'true'
}
return 'false'
}
+97
View File
@@ -0,0 +1,97 @@
module builtin
// String: This is a minimal implementation of the "string" builtin until the WASM backend
// is able to compile the original builtin implementation
pub struct string {
pub:
str &u8
len int
}
// Concatenation operator: var += str / str + str
// Note: This will alloc a new string with the content of these two strings
pub fn (s string) + (other string) string {
if s.len == 0 {
return other
}
if other.len == 0 {
return s
}
total_len := s.len + other.len
result_ptr := unsafe { malloc(total_len) }
if result_ptr == 0 {
panic('string.+: malloc failed')
}
unsafe {
vmemcpy(result_ptr, s.str, s.len)
vmemcpy(result_ptr + s.len, other.str, other.len)
}
return string{
str: result_ptr
len: total_len
}
}
// Equality comparison: checks if two strings are identical
pub fn (s string) == (other string) bool {
if s.len != other.len {
return false
}
for i in 0 .. s.len {
if s[i] != other[i] {
return false
}
}
return true
}
// Less-than comparison: lexicographically compares two strings
pub fn (s string) < (other string) bool {
// Taken from the C Backend
for i in 0 .. s.len {
if i >= other.len || s[i] > other[i] {
return false
} else if s[i] < other[i] {
return true
}
}
if s.len < other.len {
return true
}
return false
}
// at returns the byte at index `idx`.
// Example: assert 'ABC'.at(1) == u8(`B`)
// NOTE: Adapted from C built-in, modified for WASM
pub fn (s string) at(idx int) u8 {
$if !no_bounds_checking {
if idx < 0 || idx >= s.len {
panic('string index out of range')
}
}
return unsafe { s.str[idx] }
}
// Convert C string to V string
pub fn cstring_to_vstring(ptr &u8) string {
if ptr == 0 {
return ''
}
mut len := 0
unsafe {
for ptr[len] != 0 {
len++
}
}
unsafe {
return string{
str: ptr
len: len
}
}
}
@@ -0,0 +1,16 @@
module builtin
// tos creates a V string, given a C style pointer to a 0 terminated block.
// Note: the memory block pointed by s is *reused, not copied*!
// It will panic, when the pointer `s` is 0.
// See also `tos_clone`.
@[unsafe]
pub fn tos(s &u8, len int) string {
if s == 0 {
panic('tos(): nil string')
}
return string{
str: unsafe { s }
len: len
}
}
@@ -0,0 +1,61 @@
module builtin
// print prints a message to stdout. Unlike `println` stdout is not automatically flushed.
pub fn print(s string) {
elm := CIOVec{
buf: s.str
len: usize(s.len)
}
WASM.fd_write(1, &elm, 1, 0)
}
// println prints a message with a line end, to stdout.
pub fn println(s string) {
elm := [CIOVec{
buf: s.str
len: usize(s.len)
}, CIOVec{
buf: c'\n'
len: 1
}]!
WASM.fd_write(1, &elm[0], 2, 0)
}
// eprint prints a message to stderr.
pub fn eprint(s string) {
elm := CIOVec{
buf: s.str
len: usize(s.len)
}
WASM.fd_write(2, &elm, 1, 0)
}
// eprintln prints a message with a line end, to stderr.
pub fn eprintln(s string) {
elm := [CIOVec{
buf: s.str
len: usize(s.len)
}, CIOVec{
buf: c'\n'
len: 1
}]!
WASM.fd_write(2, &elm[0], 2, 0)
}
// exit terminates execution immediately and returns exit `code` to the shell.
@[noreturn]
pub fn exit(code int) {
WASM.proc_exit(code)
}
// panic prints a nice error message, then exits the process with exit code of 1.
@[noreturn]
pub fn panic(s string) {
eprint('V panic: ')
eprintln(s)
exit(1)
}
@@ -0,0 +1,32 @@
module builtin
pub struct CIOVec {
pub:
buf &u8
len usize
}
type Errno = u16
type FileDesc = int
@[wasm_import_namespace: 'wasi_snapshot_preview1']
fn WASM.args_sizes_get(argc &u32, argc_buf_size &u32) Errno
@[wasm_import_namespace: 'wasi_snapshot_preview1']
fn WASM.args_get(argv &&u8, argv_buf &u8) Errno
@[wasm_import_namespace: 'wasi_snapshot_preview1']
fn WASM.fd_write(fd FileDesc, iovs &CIOVec, iovs_len usize, retptr &usize) Errno
@[wasm_import_namespace: 'wasi_snapshot_preview1']
fn WASM.fd_read(fd FileDesc, iovs &CIOVec, iovs_len usize, nread &usize) Errno
@[wasm_import_namespace: 'wasi_snapshot_preview1']
fn WASM.fd_sync(fd FileDesc) Errno
@[wasm_import_namespace: 'wasi_snapshot_preview1']
fn WASM.random_get(buf &u8, buf_len usize) Errno
@[wasm_import_namespace: 'wasi_snapshot_preview1']
@[noreturn]
fn WASM.proc_exit(rval int)