-
Notifications
You must be signed in to change notification settings - Fork 570
Support go:linkname compiler directive #1000
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
Comments
Having thought about this feature for a while I think there are two main difficulties with implementing it in GopherJS context:
In terms of behavior, there seems to be two main ways this directive is used. First it is to "import" a function implementation from another package even if it is unexported. This has an effect similar to force-exporting a symbol. Note that this can't be substituted with an import in a general case because it may create a circular dependency between packages. For example, package somepackage
//go:linkname doSomething otherpacakge.doSomethingInternal
func doSomething() error package otherpackage
func doSomethingInternal() error {
// The actual implementation goes here.
return nil
} The second use case is to "insert" a function implementation from one package into another, again potentially violating exporting rules: package somepackage
// This function is actually implemented elsewhere.
func doSomethingInternal() error package otherpackage
//go:linkname doSomething somepackage.doSomethingInternal
func doSomething() error {
// The actual implementation goes here
return nil
} Also it's worth noting that the feature can be used for variables as well, but I haven't actually encountered that, so we could get away with a partial implementation here. |
A few more notes after playing with the Go's implementation of go:linkname:
|
Side note, the discussion of this feature has gotten split between this issue and #989 (comment), so I'm explicitly cross-referencing it here. |
After doing more research, I think here are the requirements for the
Here's a basic test case that the implementation must be able to pass. nevkontakte@fc975de |
This is now implemented and documented in https://github.com/gopherjs/gopherjs/blob/master/doc/pargma.md#golinkname |
Motivation: this directive is actively used in low-level standard library code, in particular with the
runtime
package. Not having it available in GopherJS forces us to introduce inelegant hacks in standard library overlays. This functionality becomes particularly important when a function is exposed into several packages, making then share state (e.g.runtime.fastrand()
).Specification from https://pkg.go.dev/cmd/compile:
The text was updated successfully, but these errors were encountered: