Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

persistedcap.gno

1.30 Kb · 31 lines
 1// Package persistedcap stores a closure (constructed by a /p/ factory)
 2// at init, persisting the closure's captured HIV as part of this
 3// realm's state. Foreign-realm callers can grab the closure via
 4// GetCounter() and invoke it; the closure body's write to its
 5// captured HIV is REJECTED by the readonly check because the HIV is
 6// real (NewTime>0) and belongs to a different realm than the caller's
 7// m.Realm. The unreal-HIV exception only applies while the HIV is
 8// transient — persistence elevates it to a normal realm-owned slot.
 9package persistedcap
10
11import "gno.land/p/demo/tests/p_closurecap"
12
13var counter func() int
14
15func init() {
16	counter = p_closurecap.MakeCounter(0)
17}
18
19// GetCounter returns the persisted closure. The caller invokes it
20// directly — because the closure's FuncLit lives in /p/ p_closurecap,
21// PushFrameCall's borrow rule does NOT switch m.Realm to persistedcap
22// at invocation. The closure body's write to the captured HIV (PkgID
23// = persistedcap, persisted) therefore runs under the caller's
24// m.Realm, which is the persisted-closure-capture cross-realm case.
25//
26// SECURITY: deliberate VM-parity-test infrastructure — do NOT copy this
27// pattern in production code. See gno.land/r/tests/vm/crossrealm
28// Closure for the broader rationale.
29func GetCounter() func() int {
30	return counter
31}