Skip to content

Calling a func on a nil interface results in a potentially confusing JS error #563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
flimzy opened this issue Dec 23, 2016 · 3 comments
Labels

Comments

@flimzy
Copy link
Member

flimzy commented Dec 23, 2016

This may or may not be a "bug" per se, but attempting to call a method on a nil interface results in a JS error, rather than a GopherJS runtime error. Example:

package main

func main() {
	foo := GetFoo()
	foo.Foo()
}

type Foo interface {
	Foo()
}

func GetFoo() Foo {
	return nil
}

Results in:

TypeError: foo.Foo is not a function

Or in Chrome:

main.js:146674 Uncaught TypeError: Cannot read property 'Foo' of undefined

This is only undesirable insofar as it tends to suggest a problem with a JS lib.

In Go the same error is reported as:

panic: runtime error: invalid memory address or nil pointer dereference

Something similar would be ideal, IMHO, in GopherJS.

@dmitshur
Copy link
Member

That sounds like a bug. Subtle and not as high value, but still, it's not correct behavior according to the Go spec. Similar to #551.

There may be Go code that relies on the above behavior triggering a panic and then recovering it, such a program would behave incorrectly.

@dmitshur dmitshur added the bug label Dec 23, 2016
@dmitshur
Copy link
Member

dmitshur commented Dec 23, 2016

Playground Links.

https://play.golang.org/p/v6_Exf6XUq

Output:

"runtime error: invalid memory address or nil pointer dereference"

http://www.gopherjs.org/playground/#/v6_Exf6XUq

Output:

"JavaScript error: foo.Foo is not a function"

@flimzy It's not the same as what you reported. Is your GopherJS version the latest?

@flimzy
Copy link
Member Author

flimzy commented Dec 23, 2016

That's what I got in node. My test in chrome may have been invalid. I'll double check when I'm back at a computer after Christmas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants