Comments (4)
One more interesting example from attempted workaround
package main
import (
"fmt"
"github.com/cosmos72/gomacro/fast"
"github.com/holiman/uint256"
)
func main() {
code := `
import (
"github.com/holiman/uint256"
)
sign := uint256.Int.Sign
func Foo(x *uint256.Int) int {
return sign(x)
}
`
interp := fast.New()
interp.Eval(code)
foo := interp.ValueOf("Foo").Interface().(func(int2 *uint256.Int) int)
z := uint256.NewInt(123)
fmt.Println(foo(z))
}
Fails with
// debug: running "go get github.com/holiman/uint256" ...
go: added github.com/holiman/uint256 v1.2.1
// debug: running "go mod tidy" ...
// debug: compiling plugin "/home/divashchenko/go/src/gomacro.imports/gomacro_pid_37124/import_1" ...
panic: reflect.Value.Addr of unaddressable value
goroutine 1 [running]:
reflect.Value.Addr({0x17fe4e0?, 0xc0002cb2c0?, 0x1?})
/usr/lib/go-1.19/src/reflect/value.go:271 +0x65
github.com/cosmos72/gomacro/xreflect.Value.Addr(...)
/home/divashchenko/go/pkg/mod/github.com/cosmos72/[email protected]/xreflect/value.go:114
github.com/cosmos72/gomacro/fast.(*Comp).compileMethodAsFunc.func6({0xc0005965d0?, 0x1, 0x1})
/home/divashchenko/go/pkg/mod/github.com/cosmos72/[email protected]/fast/selector.go:909 +0xbc
github.com/cosmos72/gomacro/xreflect.MakeFunc.func1({0xc0005965b8, 0x1, 0x1?})
/home/divashchenko/go/pkg/mod/github.com/cosmos72/[email protected]/xreflect/wrap.go:39 +0xaa
reflect.Value.call({0x1658c20?, 0xc0002fbf70?, 0x7f3bfc913a68?}, {0x18027ed, 0x4}, {0xc0005965a0, 0x1, 0x1658c20?})
/usr/lib/go-1.19/src/reflect/value.go:584 +0x8c5
reflect.Value.Call({0x1658c20?, 0xc0002fbf70?, 0xc0002b7830?}, {0xc0005965a0?, 0x100000000000475?, 0xc000197760?})
/usr/lib/go-1.19/src/reflect/value.go:368 +0xbc
github.com/cosmos72/gomacro/xreflect.Value.Call({{0x1658c20?, 0xc0002fbf70?, 0x0?}}, {0xc000596588, 0x1, 0x92ddd2?})
/home/divashchenko/go/pkg/mod/github.com/cosmos72/[email protected]/xreflect/value.go:127 +0x113
github.com/cosmos72/gomacro/fast.callxr({{0x1658c20?, 0xc0002fbf70?, 0xc0002cb2c0?}}, {0xc000596588, 0x1, 0x1})
/home/divashchenko/go/pkg/mod/github.com/cosmos72/[email protected]/fast/call.go:453 +0x8e
github.com/cosmos72/gomacro/fast.(*Comp).callnret1.func2(0x17fe4e0?)
/home/divashchenko/go/pkg/mod/github.com/cosmos72/[email protected]/fast/callnret1.go:58 +0xee
github.com/cosmos72/gomacro/fast.(*Comp).varSetExpr.func3(0xc0001da320)
/home/divashchenko/go/pkg/mod/github.com/cosmos72/[email protected]/fast/var_set.go:2344 +0x2f
github.com/cosmos72/gomacro/fast.exec.func1(0xc0001da320?)
/home/divashchenko/go/pkg/mod/github.com/cosmos72/[email protected]/fast/code.go:171 +0x236
github.com/cosmos72/gomacro/fast.(*Comp).funcGeneric.func2.1({0xc000596540, 0x1, 0x0?})
/home/divashchenko/go/pkg/mod/github.com/cosmos72/[email protected]/fast/function.go:438 +0xf0
github.com/cosmos72/gomacro/xreflect.MakeFunc.func1({0xc000596528, 0x1, 0x1?})
/home/divashchenko/go/pkg/mod/github.com/cosmos72/[email protected]/xreflect/wrap.go:39 +0xaa
main.main()
/home/divashchenko/workspace/tmp/goplay/main.go:33 +0x7e
It seems that in fast/selector.go:843
it falls into wrong branch (with comment // method declared by interpreted code, manually retrieve it.
). In following snippet
var x uint256.Int
interp := fast.New()
typ := interp.TypeOf(x)
fmt.Println(typ.ReflectType().NumMethod())
something strange happens with typ.ReflectType(), it has NumMethods() == 0
.
Tried to dig into it a bit more with no luck, but I hope it hepls :)
from gomacro.
Sorry for the very late answer.
Yes, it's definitely strange because running the same code directly from gomacro repl works:
import "github.com/holiman/uint256"
func Foo(x *uint256.Int) int {
return x.Sign()
}
y := uint256.NewInt(123)
Foo(y)
produces the following output:
// debug: running "go get github.com/holiman/uint256" ...
go: added github.com/holiman/uint256 v1.2.4
// debug: running "go mod tidy" ...
// debug: compiling plugin "/home/max/go/src/gomacro.imports/gomacro_pid_17289/import_1" ...
1 // int
from gomacro.
I am debugging this issue, and it's specific to methods attached to non-struct types.
Package github.com/holiman/uint256
defines type Int
as follows:
type Int [4]uint64
then defines methods with pointer receiver *Int
, as for example
func (z *Int) Sign() int {
// ...
}
Which is perfectly legal, but less tested in gomacro than methods attached to structs.
Continuing the analysis...
from gomacro.
Fixed in commit 960f329
It was a bug in method lookup: it checked for types that cannot have methods and failed early in such case,
but the logic for the early failure was bugged and worked by chance only if experimental generics were enabled.
from gomacro.
Related Issues (20)
- I would like a full working example how to inject my application context(types and functions) in the REPL HOT 9
- Extract struct with methods from script to compiled code HOT 2
- Proxy can mix the called method of the imported structure HOT 3
- integrate with https://github.com/go-rod/rod HOT 2
- I'm unable to compile code importing gomacro on Mac OS m1 pro HOT 7
- Is there a way to Eval a folder HOT 5
- recursive type error cases: call of reflect.Value.Interface on zero Value HOT 6
- can't import fsm package HOT 4
- Different compilation behaviour from native go HOT 2
- undefined: pkgpath HOT 3
- Cannot convert struct to interface if it has an embedded struct with a method HOT 4
- Cannot import third party packages on Linux (not even the ones in the README) HOT 2
- Idea for making adding third party packages to Gomacro easier when plugins aren't available HOT 3
- recursive type error cases: call of reflect.Value.Interface on zero Value
- can not import 3rd party packages HOT 13
- package <package> exports zero constants, functions, types, and variables HOT 3
- Caching of interp.Eval1 results HOT 13
- Way to disable go get import and enforce to use only defined import list HOT 3
- An error will be reported when using third-party packages on Windows HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gomacro.