Files
sakurac/core/functions/function.sakura
T

164 lines
6.7 KiB
Plaintext
Raw Normal View History

2026-10-26 10:14:11 -05:00
#! @file function.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains `function`, one callable of a signature, whatever its type, which it
#! owns, as C++'s `std::function` and Rust's `Box<dyn FnMut>`: for a callable kept
#! past the call that is given it, where its type is not one to name.
module core;
#! A callable of one signature, whatever its type, which it owns and closes where
#! it closes: a functor, a function, a lambda, or the address of a C function, as
#! `function<def(&event): void>`.
#!
#! It holds a callable of 24 bytes or fewer, aligned to 8 or less, in the value
#! itself, as it does a function value, a C function's address, and most
#! functors, and a larger one in a `box`. A call goes through one table of what
#! the held type does, which each type has one of, so calling it costs one load
#! and one indirect call more than calling what it holds.
#!
#! It is moved, never copied: `move` leaves the one moved from empty. Its zero
#! value holds nothing, so a field or a variable needs none given, and calling a
#! function that holds nothing stops the program, as reaching through `null`
#! does.
#!
#! ```sakura
#! var handlers := vector::<function<def(&event): void>>::new();
#! handlers.append(function::new(tally {}));
#! handlers.append(function::new(def(e: &event) { println(e.name); }));
#! for &h in handlers { h(&e); }
#! ```
#! @tparam S The signature, as `def(i64): bool`.
#! @remarks A lambda refers to the locals it uses, so a function holding one
#! refers to them too, and must not outlive them. What must outlive the
#! frame making it is a functor holding what it needs. A function is no
#! constant, since a constant would keep the words of what it holds but
#! not the function a function value among them is.
pub struct function<S> : invokable<S>, owning where S: signature {
var _ops: *function_ops<S>; #! What the held type does, or `null` where it holds nothing.
var _held: u64[3]; #! The callable where it fits, or a `box` of it.
}
# What a function does with the type it holds: one constant for each type, so its
# address names the type.
struct function_ops<S> where S: signature {
var call: def(*mut u8, S::params...): S::result; # Calls the callable held at the buffer.
var close: def(*mut u8): void; # Closes it, and its box where it has one.
}
# Whether a function holds a callable of a type in its buffer, rather than in a box.
def function_ops<S>::fits<F>(): bool {
return size_of::<F>() <= 24 && align_of::<F>() <= 8;
}
# The table of what a function does with a type it holds, at one address for each
# type.
def function_ops<S>::of<F>(): *function_ops<S> where F: invokable<S> {
if const function_ops::<S>::fits::<F>() {
const held: function_ops<S> = function_ops::<S> {
call = function_ops::<S>::call_held::<F>,
close = function_ops::<S>::close_held::<F>
};
return &held as *function_ops<S>;
} else {
const boxed: function_ops<S> = function_ops::<S> {
call = function_ops::<S>::call_boxed::<F>,
close = function_ops::<S>::close_boxed::<F>
};
return &boxed as *function_ops<S>;
}
}
# Calls a callable held in the buffer.
def function_ops<S>::call_held<F>(at: *mut u8, args: S::params...): S::result where F: invokable<S> {
return (at as *mut F)[0](...args);
}
# Closes a callable held in the buffer.
def function_ops<S>::close_held<F>(at: *mut u8): void {
close(through(at as *mut F));
}
# Calls a callable the buffer holds a box of.
def function_ops<S>::call_boxed<F>(at: *mut u8, args: S::params...): S::result where F: invokable<S> {
val held := &mut (at as *mut box<F>)[0].get();
return held(...args);
}
# Closes the box the buffer holds, which closes the callable and gives back its
# memory.
def function_ops<S>::close_boxed<F>(at: *mut u8): void {
close(through(at as *mut box<F>));
}
#! Makes a function holding a callable: a functor, a function value, named or a
#! lambda, the address of a C function, or a method taken through its type, as
#! `vec2::length`, which takes its receiver first.
#! @tparam F The callable's type.
#! @param callable The callable, which the function then owns, so it keeps
#! what a lambda given for it refers to.
#! @returns The function.
pub def function<S>::new<F>(@kept callable: F): function<S> where F: invokable<S> {
var made := function::<S> { _ops = function_ops::<S>::of::<F>() };
# Reached through the buffer's address as a number, which no constant is made
# by, since a constant would keep the callable's words but not the function a
# function value among them is.
if const function_ops::<S>::fits::<F>() {
(made.buffer() as *mut F)[0] = move(&mut callable);
} else {
(made.buffer() as *mut box<F>)[0] = box::<F>::new(move(&mut callable));
}
return made;
}
#! Calls what a function holds, as `f(args)` does.
#! @param this The function, which must hold a callable.
#! @param args The arguments, one of each of the signature's parameter types.
#! @returns What the call gives.
#! @remarks The callable may change what it holds, as a functor's `invoke`
#! taking `&mut this` does, though the function is called as it is, as
#! a function value is.
pub def function<S>::invoke(&this, args: S::params...): S::result {
return (*this._ops).call(this.buffer(), ...args);
}
# The buffer's address, which a callable held there is called with, and which it
# may change through, as calling a function value may change what it refers to.
def function<S>::buffer(&this): *mut u8 {
return (&this._held as *u8) as u64 as *mut u8;
}
#! Whether a function holds a callable, which one moved from or closed does not.
#! @param this The function.
#! @returns Whether it does.
pub def function<S>::holds(&this): bool {
return this._ops != null;
}
#! Lends the callable a function holds, where it is of a type.
#! @tparam F The type asked for.
#! @param this The function.
#! @returns The callable, good while the function holds it, or `nothing` where
#! it holds nothing, or a callable of another type.
pub def function<S>::get<F>(&this): maybe<&F> where F: invokable<S> {
if this._ops != function_ops::<S>::of::<F>() {
return nothing {};
}
if const function_ops::<S>::fits::<F>() {
return through(&this._held as *F);
} else {
return (&this._held as *box<F>)[0].get();
}
}
#! Closes what a function holds.
#! @param this The function, which holds nothing after, so closing it again
#! does nothing.
pub def function<S>::close(&mut this): void {
val ops := this._ops;
return if ops == null;
this._ops = null;
(*ops).close(&mut this._held as *mut u8);
}