-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Bound Method Realm support (#1257)
When a function is called, it's usually enough to get the pkgpath from where the function is declared, to find out whether we are switching to a new realm or not. But when the function is a bound method, and the method and declared type are defined in a /p/ package, we need to look at the owning (persisting) pkgpath of the receiver, if any. When one is found, it uses that. There are some edge cases to consider, I'm not sure if they are all fixed, but we will find out. Here are some that are considered; 1. if the receiver is an unpersisted new object with method, declared in a p module, it has no realm until it becomes persisted. 2. nil receivers aren't objects, so even if it were stored in a realm, it would be stored by "reference" from a pointervalue which also is not an object, so there's no realm to consider even after persistence. 3. if the method from 1 makes any modifications to the /p/ package's block state, it should result in an error and the tx should fail. 4. if a /p/ declared thing with method gets stored in realm X, it should be possible for realm Y to call it, and have it modify realm X (e.g. say realm X attached a closure to the thing). --------- Signed-off-by: moul <[email protected]> Co-authored-by: moul <[email protected]>
- Loading branch information
Showing
12 changed files
with
252 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module gno.land/p/demo/tests/p_crossrealm |
24 changes: 24 additions & 0 deletions
24
examples/gno.land/p/demo/tests/p_crossrealm/p_crossrealm.gno
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package p_crossrealm | ||
|
||
type Stringer interface { | ||
String() string | ||
} | ||
|
||
type Container struct { | ||
A int | ||
B Stringer | ||
} | ||
|
||
func (c *Container) Touch() *Container { | ||
c.A += 1 | ||
return c | ||
} | ||
|
||
func (c *Container) Print() { | ||
println("A:", c.A) | ||
if c.B == nil { | ||
println("B: undefined") | ||
} else { | ||
println("B:", c.B.String()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package crossrealm | ||
|
||
import ( | ||
"gno.land/p/demo/tests/p_crossrealm" | ||
"gno.land/p/demo/ufmt" | ||
) | ||
|
||
type LocalStruct struct { | ||
A int | ||
} | ||
|
||
func (ls *LocalStruct) String() string { | ||
return ufmt.Sprintf("LocalStruct{%d}", ls.A) | ||
} | ||
|
||
// local is saved locally in this realm | ||
var local *LocalStruct | ||
|
||
func init() { | ||
local = &LocalStruct{A: 123} | ||
} | ||
|
||
// Make1 returns a local object wrapped by a p struct | ||
func Make1() *p_crossrealm.Container { | ||
return &p_crossrealm.Container{ | ||
A: 1, | ||
B: local, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module gno.land/r/demo/tests/crossrealm | ||
|
||
require ( | ||
gno.land/p/demo/tests/p_crossrealm v0.0.0-latest | ||
gno.land/p/demo/ufmt v0.0.0-latest | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Set up GNOROOT in the current directory. | ||
mkdir $WORK/gnovm | ||
symlink $WORK/gnovm/stdlibs -> $GNOROOT/gnovm/stdlibs | ||
env GNOROOT=$WORK | ||
|
||
gno test -v ./examples/gno.land/r/demo/realm2 | ||
|
||
stderr '=== RUN TestDo' | ||
stderr '--- PASS: TestDo.*' | ||
|
||
stderr '=== RUN file/realm2_filetest.gno' | ||
stderr '--- PASS: file/realm2_filetest.*' | ||
|
||
-- examples/gno.land/p/demo/counter/gno.mod -- | ||
module gno.land/p/demo/counter | ||
|
||
-- examples/gno.land/p/demo/counter/counter.gno -- | ||
package counter | ||
|
||
type Counter struct { | ||
n int | ||
} | ||
|
||
func (c *Counter) Inc() { | ||
c.n++ | ||
} | ||
|
||
-- examples/gno.land/r/demo/realm1/realm1.gno -- | ||
package realm1 | ||
|
||
import "gno.land/p/demo/counter" | ||
|
||
var c = counter.Counter{} | ||
|
||
func GetCounter() *counter.Counter { | ||
return &c | ||
} | ||
|
||
-- examples/gno.land/r/demo/realm2/realm2.gno -- | ||
package realm2 | ||
|
||
import ( | ||
"gno.land/r/demo/realm1" | ||
) | ||
|
||
func Do() { | ||
realm1.GetCounter().Inc() | ||
} | ||
|
||
-- examples/gno.land/r/demo/realm2/realm2_filetest.gno -- | ||
// PKGPATH: gno.land/r/tests | ||
package tests | ||
|
||
import "gno.land/r/demo/realm2" | ||
|
||
func main() { | ||
realm2.Do() | ||
println("OK") | ||
} | ||
|
||
// Output: | ||
// OK | ||
|
||
-- examples/gno.land/r/demo/realm2/realm2_test.gno -- | ||
package realm2 | ||
|
||
import "testing" | ||
|
||
func TestDo(t *testing.T) { | ||
Do() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// PKGPATH: gno.land/r/crossrealm_test | ||
package crossrealm_test | ||
|
||
import ( | ||
crossrealm "gno.land/r/demo/tests/crossrealm" | ||
) | ||
|
||
func main() { | ||
// even though we are running within a realm, | ||
// we aren't storing the result of crossrealm.Make1(), | ||
// so this should print fine. | ||
crossrealm.Make1().Touch().Print() | ||
} | ||
|
||
// Output: | ||
// A: 2 | ||
// B: LocalStruct{123} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters