///|
/// Whether a transport can stop a call at its time limit.
#derive(Eq, Debug)
pub(all) enum Enforcement {
/// The transport ends a call at the limit.
Enforced
/// Nothing can end a call. The host writes a warning when a call ends late.
NotEnforced
}
///|
pub extend Enforcement with Eq::{equal, not_equal}
///|
pub extend Enforcement with @debug.Debug::{to_repr}
///|
/// A call that gave no response. The plugin is unusable after it.
#derive(Eq, Debug)
pub(all) suberror TransportFault {
/// The code of the plugin ended with a trap, with this text.
Trapped(String)
/// The transport ended the call at this limit.
LimitReached(@lander_core.TimeLimit)
/// The plugin needed more memory than its limit.
MemoryLimitReached
/// The plugin called a host function that the application does not permit.
Denied(function~ : String)
/// The connection to the plugin is lost, for this reason.
Lost(String)
}
///|
pub extend TransportFault with Eq::{equal, not_equal}
///|
pub extend TransportFault with @debug.Debug::{to_repr}
///|
/// Text for people that describes the fault.
pub fn TransportFault::message(self : TransportFault) -> String {
match self {
Trapped(text) => "the plugin trapped: \{text}"
LimitReached(limit) => "the call did not end in \{limit}"
MemoryLimitReached => "the plugin needed more memory than its limit"
Denied(function~) =>
"the plugin called the host function \{function}, which the application does not permit"
Lost(reason) => "the connection to the plugin is lost: \{reason}"
}
}
///|
/// A connection to one plugin instance. It carries an operation and the request
/// bytes to the plugin, and gives the status and the response bytes back. It does
/// not know the contract rules; `Launch` and `Session` hold them.
pub(open) trait Transport {
/// Make one call. The host makes one call at a time. It raises a
/// `TransportFault` when there is no response.
async fn call(Self, Operation, Bytes) -> Response
/// Whether `call` ends at the call limit of the connection.
fn enforcement(Self) -> Enforcement
/// The wake requests that the plugin makes from its own threads, or `None` when
/// it cannot make them. The host takes the flag after each call and each 50 ms
/// while no call runs, and then calls `tick`. By default, `None`.
fn wake_flag(Self) -> WakeFlag? = _
}
///|
impl Transport with fn wake_flag(_self) {
None
}
///|
/// A flag that a plugin sets from any thread to ask for a tick. Taking it gives
/// whether it was set, and clears it, so the requests before one look give one tick.
pub struct WakeFlag {
priv take : () -> Bool
}
///|
/// A flag that `take` reads and clears.
pub fn WakeFlag::new(take : () -> Bool) -> WakeFlag {
{ take, }
}
///|
/// Whether the plugin asked for a tick since the last time. The flag is clear after it.
pub fn WakeFlag::take(self : WakeFlag) -> Bool {
(self.take)()
}
///|
/// A plugin that a host can connect to: a MoonBit `Service`, or a value of one of
/// the other packages of this module.
pub(open) trait Plugin {
/// Make a new instance, and run the body with its transport. Each call of the
/// transport has the time limit `limit`; the transport ends a call at that limit
/// when its enforcement is `Enforced`. The instance is released when the body
/// ends, also after an error or a cancellation. The host waits for the release,
/// so it must not wait without a limit. The plugin writes its entries to the log,
/// and an entry of a call arrives before the response of that call.
async fn connect(
Self,
PluginLog,
@lander_core.TimeLimit,
async (&Transport) -> Unit,
) -> Unit
}
///|
priv struct LocalTransport {
instance : ServiceInstance
}
///|
impl Transport for LocalTransport with fn call(self, operation, request) {
self.instance.respond(operation, request)
}
///|
impl Transport for LocalTransport with fn enforcement(_self) {
NotEnforced
}
///|
/// A MoonBit service runs in the host process. Nothing can end its calls, and a
/// call blocks the event loop of the host until it returns.
pub impl Plugin for Service with fn connect(self, log, _limit, body) {
body({ instance: self.instance(log), })
}