[go1.20] Added support for unsafe.SliceData #1298
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
go1.20 added
unsafe.SlideData
,unsafe.String
, andunsafe.StringData
.For the following reasons, this PR only adds
unsafe.SliceData
. The tests for this change are in the Go repo tests of this builtin function. This is part of #1270SliceData
unsafe.SliceData
is something that GopherJS can handle because of how, even when empty, GopherJS slices have an array that can be pointed to.The only thing we can't handle is how using the "unspecified memory address" works right now, seen in this playground, but there are no promises for how that should work and no one should be using that pointer like that. In GopherJS assigning to the pointer of the empty slice simply writes to
[0]
on the empty slice which then isn't visible, always returns zero, and is a pointless, but doesn't break anything.The Go compiler ensures that the argument into
SliceData
will be a slice.String
unsafe.String
may be possible to support withBuffer
but would require copying the data into a string. We should probably override it where possible because the point of this function is to not require a copy to be faster than simply performing a conversion,string([]byte{...})
.We may need to we can add this later (with the copy) since it seams like some natives are using this instead of the cast to save memory and/or time. Instead of overriding each location, we could just stub out the cast (kind of like how we didn't override all the atomics but just stubbed out the atomic methods).
StringData
unsafe.StringData
may be possible via a copy to a byte array first but is the same as above, doing the copy makes this no better than a conversion. Or we could make some JS code similar to$indexPtr
that wraps a string into a byte pointer. Until then we should probably override them instead.Also, this is super unsafe because in Go if you get the string data for a literal and attempt to modify it (despite the comment in the docs saying
*byte
should be treated as immutable) it causes a segmentation fault because you can't modify the literal stored in the memory with the opcode. The fault signal can not be recovered from. Here's a playground with that.