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

crossrealm.gno

1.83 Kb · 77 lines
 1package crossrealm_b
 2
 3import (
 4	"chain/banker"
 5	"chain/runtime/unsafe"
 6
 7	"gno.land/r/tests/vm/crossrealm"
 8)
 9
10type fooer struct {
11	s string
12}
13
14func (f *fooer) SetS(newVal string) {
15	f.s = newVal
16}
17
18func (f *fooer) Foo(cur realm) {
19	println("hello " + f.s + " cur=" + unsafe.CurrentRealm().PkgPath() + " prev=" + unsafe.PreviousRealm().PkgPath())
20}
21
22func (f *fooer) Bar() {
23	println("hello " + f.s + " cur=" + unsafe.CurrentRealm().PkgPath() + " prev=" + unsafe.PreviousRealm().PkgPath())
24}
25
26var (
27	Fooer              = &fooer{s: "A"}
28	FooerGetter        = func() crossrealm.Fooer { return Fooer }
29	FooerGetterBuilder = func() crossrealm.FooerGetter { return func() crossrealm.Fooer { return Fooer } }
30)
31
32var Closure func()
33
34func SetClosure(cur realm, f func()) {
35	Closure = f
36}
37
38var Object any
39
40func SetObject(cur realm, x any) {
41	Object = x
42}
43
44func GetObject() any {
45	return Object
46}
47
48func IncrementObject(cur realm) any {
49	ptr := Object.(*int)
50	*ptr += 1
51	return Object
52}
53
54var n int
55
56// NOTE should be non-crossing
57func IncrGlobal() {
58	n++
59}
60
61// TrySubOn attempts to mint a sub-realm token on a passed-in realm
62// value. Non-crossing (`_ int` discriminator), so the caller's cur
63// remains the topmost crossing cur — but borrow rule #1 runs this
64// body with m.Realm = crossrealm_b, so rlm.Sub must reject: a foreign
65// realm must not mint sub-identities in the caller's namespace.
66func TrySubOn(_ int, rlm realm) {
67	rlm.Sub("stolen")
68}
69
70// TryBankerOnPrevious attempts to construct a RealmSend banker over the
71// CALLER via cur.Previous() (which is NOT IsCurrent). NewBanker must
72// reject it — otherwise a callee could set pkgAddr to the caller's
73// address and drain it through the pkgAddr==from gate. Regression guard
74// for the IsCurrent check in NewBanker.
75func TryBankerOnPrevious(cur realm) {
76	banker.NewBanker(banker.BankerTypeRealmSend, cur.Previous())
77}