This repository has been archived on 2025-10-23. You can view files and clone it, but cannot push or open issues or pull requests.
Files
nub-lang-archive/example/src/main.nub
nub31 f6c686a635 ...
2025-09-20 19:53:40 +02:00

57 lines
958 B
Plaintext

module "main"
extern "puts" func puts(text: cstring)
extern "malloc" func malloc(size: u64): ^void
extern "free" func free(address: ^void)
struct Human
{
name: cstring
}
extern "main" func main(args: []cstring): i64
{
let x: ref<Human> = {}
test(x)
return 0
}
func test(x: ref<Human>)
{
}
struct ref<T>
{
value: ^T
count: ^u64
@oncreate
func on_create()
{
puts("on_create")
this.value = @interpret(^T, malloc(@size(T)))
this.count = @interpret(^u64, malloc(@size(u64)))
this.count^ = 1
}
@oncopy
func on_copy()
{
puts("on_copy")
this.count^ = this.count^ + 1
}
@ondestroy
func on_destroy()
{
puts("on_destroy")
this.count^ = this.count^ - 1
if this.count^ <= 0
{
puts("free")
free(@interpret(^void, this.value))
free(@interpret(^void, this.count))
}
}
}