Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func TestBuild(t *testing.T) {
}
if minor >= 24 {
tests = append(tests, "typealias.go")
tests = append(tests, "weak.go")
}

if *testTarget != "" {
Expand Down
7 changes: 7 additions & 0 deletions src/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ func registerWeakPointer(ptr unsafe.Pointer) unsafe.Pointer {
return ptr
}

//go:linkname makeStrongFromWeak weak.runtime_makeStrongFromWeak
func makeStrongFromWeak(ptr unsafe.Pointer) unsafe.Pointer {
// Weak pointers are not weak here. registerWeakPointer above returns the
// pointer that it got, so the value stays and this is the identity too.
return ptr
}

var godebugUpdate func(string, string)

//go:linkname godebug_setUpdate internal/godebug.setUpdate
Expand Down
21 changes: 21 additions & 0 deletions testdata/weak.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"runtime"
"weak"
)

type value struct {
n int
}

func main() {
v := &value{n: 42}
p := weak.Make(v)
if got := p.Value(); got == nil {
println("weak pointer lost its value")
} else {
println("weak value:", got.n)
}
runtime.KeepAlive(v)
}
1 change: 1 addition & 0 deletions testdata/weak.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
weak value: 42