Permalink
Browse files

compiler/natives/src/strings: Simple Builder implementation for js ar…

…chitecture.

Can't use package unsafe, so use a simple type conversion from []byte
to string in Builder.String method.

Further optimizations are deferred for later (most likely not needed
because Go->wasm will be ready by the time they're needed).

Related to golang/go#18990.
  • Loading branch information...
dmitshur committed Dec 15, 2017
1 parent d308a5b commit d05d98650d93201ef4c8c5d4122f30f39b5911f9
Showing with 10 additions and 0 deletions.
  1. +10 −0 compiler/natives/src/strings/strings.go
@@ -45,3 +45,13 @@ func Count(s, sep string) int {
}
return n
}
func (b *Builder) String() string {
// Upstream Builder.String relies on package unsafe. We can't do that.
// TODO: It's possible that the entire strings.Builder API can be implemented
// more efficiently for GOARCH=js specifically (avoid using []byte, instead
// use a String directly; or some JavaScript string builder API if one exists).
// But this is more work, defer doing it until there's a need shown via profiling,
// and there are benchmarks available (see https://github.com/golang/go/issues/18990#issuecomment-352068533).
return string(b.buf)
}

0 comments on commit d05d986

Please sign in to comment.