#! @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`: 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`. #! #! 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::>::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 : invokable, owning where S: signature { var _ops: *function_ops; #! 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 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::fits(): bool { return size_of::() <= 24 && align_of::() <= 8; } # The table of what a function does with a type it holds, at one address for each # type. def function_ops::of(): *function_ops where F: invokable { if const function_ops::::fits::() { const held: function_ops = function_ops:: { call = function_ops::::call_held::, close = function_ops::::close_held:: }; return &held as *function_ops; } else { const boxed: function_ops = function_ops:: { call = function_ops::::call_boxed::, close = function_ops::::close_boxed:: }; return &boxed as *function_ops; } } # Calls a callable held in the buffer. def function_ops::call_held(at: *mut u8, args: S::params...): S::result where F: invokable { return (at as *mut F)[0](...args); } # Closes a callable held in the buffer. def function_ops::close_held(at: *mut u8): void { close(through(at as *mut F)); } # Calls a callable the buffer holds a box of. def function_ops::call_boxed(at: *mut u8, args: S::params...): S::result where F: invokable { val held := &mut (at as *mut box)[0].get(); return held(...args); } # Closes the box the buffer holds, which closes the callable and gives back its # memory. def function_ops::close_boxed(at: *mut u8): void { close(through(at as *mut box)); } #! 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. #! @returns The function. pub def function::new(callable: F): function where F: invokable { var made := function:: { _ops = function_ops::::of::() }; # 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::::fits::() { (made.buffer() as *mut F)[0] = move(&mut callable); } else { (made.buffer() as *mut box)[0] = box::::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::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::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::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::get(&this): maybe<&F> where F: invokable { if this._ops != function_ops::::of::() { return nothing {}; } if const function_ops::::fits::() { return through(&this._held as *F); } else { return (&this._held as *box)[0].get(); } } #! Closes what a function holds. #! @param this The function, which holds nothing after, so closing it again #! does nothing. pub def function::close(&mut this): void { val ops := this._ops; return if ops == null; this._ops = null; (*ops).close(&mut this._held as *mut u8); }