Class: Kobako::Transport::Yielder
- Inherits:
-
Object
- Object
- Kobako::Transport::Yielder
- Defined in:
- lib/kobako/transport/yielder.rb,
sig/kobako/transport/yielder.rbs
Overview
Host-side stand-in for a guest-supplied block.
Each guest call that carries block_given: true gets a Yielder
that the Dispatcher hands to the Service method as &block. The
Service method observes it as an ordinary Ruby Proc through
#to_proc; yield val / block.call(val) invokes #yield, which
serialises the positional args, re-enters the guest via the injected
yield_to_guest lambda, and reifies the YieldResponse into Ruby
control flow:
* +tag 0x01+ ok — return the decoded value to +yield+'s caller
* +tag 0x02+ break — +throw break_tag, value+ so the Dispatcher's
+catch+ frame unwinds the Service method
* +tag 0x04+ error — raise the +{class, message}+ payload at the
Service's yield site
The Dispatcher calls #invalidate! from its ensure block once
dispatch completes; any later call to a stashed Yielder then raises
LocalJumpError — the observable shape of an escaped Yielder.
Instance Method Summary collapse
-
#initialize(yield_to_guest, break_tag, handler) ⇒ Yielder
constructor
yield_to_guestis aString → Stringcallable (the ext's per-dispatchKobako::Runtime::GuestYielder) that #yield invokes to re-enter the guest;break_tagis thecatchthrow tag the Dispatcher matches against to unwind the Service ontag 0x02. -
#invalidate! ⇒ void
Mark this Yielder dead.
-
#restore(value) ⇒ Object
Restore any Capability Handle in a block's ok value to its host object via the injected
Catalog::Handles. -
#to_proc ⇒ Proc
The Proc the Dispatcher passes as
&block, binding #yield so a Service method'syield/block.calldrives the round-trip. -
#yield(*args) ⇒ Object
Re-enter the guest with
argsand reify the YieldResponse into Ruby control flow. -
#yield_failure(payload, default:) ⇒ RuntimeError
Reify a
YieldResponsetag 0x04 payload into aRuntimeErrorthe Service method observes at itsyieldsite.
Constructor Details
#initialize(yield_to_guest, break_tag, handler) ⇒ Yielder
yield_to_guest is a String → String callable (the ext's
per-dispatch Kobako::Runtime::GuestYielder) that
#yield invokes to re-enter the guest; break_tag is the catch
throw tag the Dispatcher matches against to unwind the Service on
tag 0x02. handler is the Sandbox's Kobako::Catalog::Handles,
used to restore a Capability Handle in the block's ok value back to
its host object before it reaches the Service yield site.
38 39 40 41 42 43 |
# File 'lib/kobako/transport/yielder.rb', line 38 def initialize(yield_to_guest, break_tag, handler) @yield_to_guest = yield_to_guest @break_tag = break_tag @handler = handler @active = true end |
Instance Method Details
#invalidate! ⇒ void
This method returns an undefined value.
Mark this Yielder dead. Called by the Dispatcher's ensure block
when the originating dispatch frame returns; any later #yield
call then raises LocalJumpError.
72 73 74 |
# File 'lib/kobako/transport/yielder.rb', line 72 def invalidate! @active = false end |
#restore(value) ⇒ Object
Restore any Capability Handle in a block's ok value to its host
object via the injected Catalog::Handles. Only the
ok path calls this — host code consumes the ok value, whereas a
break value returns to the guest and stays a Handle. Walks nested
Array / Hash one level at a time; a plain value passes through
unchanged.
84 85 86 |
# File 'lib/kobako/transport/yielder.rb', line 84 def restore(value) Kobako::Codec::HandleWalk.deep_restore(value, @handler) end |
#to_proc ⇒ Proc
The Proc the Dispatcher passes as &block, binding #yield so a
Service method's yield / block.call drives the round-trip.
65 66 67 |
# File 'lib/kobako/transport/yielder.rb', line 65 def to_proc method(:yield).to_proc end |
#yield(*args) ⇒ Object
Re-enter the guest with args and reify the YieldResponse into
Ruby control flow. Raises LocalJumpError if called after
#invalidate!. The ok value is consumed by the host Service
method, so a Capability Handle in it is restored to its host object.
The break value unwinds past the Service back to the guest
Member call, so it passes through verbatim — a Handle stays a
Handle and rides back on the same id rather than churning a new one.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/kobako/transport/yielder.rb', line 52 def yield(*args) raise LocalJumpError, "guest block invoked after host dispatch frame returned" unless @active response = Kobako::Transport::Yield.decode(@yield_to_guest.call(Kobako::Codec::Encoder.encode(args))) return restore(response.value) if response.ok? throw @break_tag, response.value if response.break? raise yield_failure(response.value, default: "yield error") end |
#yield_failure(payload, default:) ⇒ RuntimeError
Reify a YieldResponse tag 0x04 payload into a RuntimeError the
Service method observes at its yield site. The {class, message,
backtrace} shape mirrors the Kobako::Transport::Yield tag 0x04
payload; default provides a fallback when the payload is not a
Hash.
93 94 95 96 97 98 99 |
# File 'lib/kobako/transport/yielder.rb', line 93 def yield_failure(payload, default:) return RuntimeError.new(default) unless payload.is_a?(Hash) klass = payload["class"] || "RuntimeError" = payload["message"] || default RuntimeError.new("#{klass}: #{}") end |