Skip to content

Commit 9374cdc

Browse files
committed
reduce use of unsafe.Pointer in wingui
1 parent fb5ed10 commit 9374cdc

File tree

4 files changed

+26
-39
lines changed

4 files changed

+26
-39
lines changed

builtin/dll_wingui.go

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"unsafe"
1414

1515
. "github.com/apmckinlay/gsuneido/core"
16-
"github.com/apmckinlay/gsuneido/util/hacks"
1716
"golang.org/x/exp/constraints"
1817
"golang.org/x/sys/windows"
1918
)
@@ -147,31 +146,17 @@ func bufZstr(buf []byte) SuStr {
147146
return SuStr(string(buf))
148147
}
149148

150-
// ptrZstr copies a nul terminated string from an unsafe.Pointer.
149+
// ptrZstr copies a nul terminated string from *byte.
151150
// If nul is not found, then the entire length is returned.
152-
func ptrZstr(p unsafe.Pointer, n int) Value {
151+
func ptrZstr(p *byte, n int) Value {
153152
if p == nil || n == 0 {
154153
return False
155154
}
156-
srcSlice := unsafe.Slice((*byte)(p), n)
157-
i := slices.Index(srcSlice, 0)
158-
if i == -1 {
159-
i = n // No null terminator found, use entire length
155+
src := unsafe.Slice(p, n)
156+
if i := slices.Index(src, 0); i >= 0 {
157+
src = src[:i]
160158
}
161-
buf := make([]byte, i)
162-
copy(buf, srcSlice[:i])
163-
return SuStr(hacks.BStoS(buf))
164-
}
165-
166-
// ptrNstr copies a string of a given length from an unsafe.Pointer
167-
func ptrNstr(p unsafe.Pointer, n uintptr) Value {
168-
if p == nil || n == 0 {
169-
return EmptyStr
170-
}
171-
buf := make([]byte, n)
172-
srcSlice := unsafe.Slice((*byte)(p), n)
173-
copy(buf, srcSlice)
174-
return SuStr(hacks.BStoS(buf))
159+
return SuStr(string(src))
175160
}
176161

177162
// getZstrBs copies the string into the byte slice and adds a nul terminator.

builtin/kernel_wingui.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ func GlobalAlloc(a, b Value) Value {
8585
// dll Kernel32:GlobalLock(pointer handle) pointer
8686
var globalLock = kernel32.MustFindProc("GlobalLock").Addr()
8787

88-
func globallock(handle HANDLE) unsafe.Pointer {
88+
func globallock(handle HANDLE) uintptr {
8989
rtn, _, _ := syscall.SyscallN(globalLock, handle)
90-
return unsafe.Pointer(rtn) //nolint
90+
return rtn
9191
}
9292

9393
var _ = builtin(GlobalLock, "(hMem)")
@@ -116,12 +116,13 @@ var _ = builtin(GlobalAllocData, "(s)")
116116

117117
func GlobalAllocData(a Value) Value {
118118
s := ToStr(a)
119-
handle := globalalloc(GMEM_MOVEABLE, uintptr(len(s)))
120-
if len(s) > 0 {
119+
n := len(s)
120+
handle := globalalloc(GMEM_MOVEABLE, uintptr(n))
121+
if n > 0 {
121122
p := globallock(handle)
122-
assert.That(p != nil)
123+
assert.That(p != 0)
123124
defer globalunlock(handle)
124-
dstSlice := unsafe.Slice((*byte)(p), len(s))
125+
dstSlice := unsafe.Slice((*byte)(unsafe.Pointer(p)), n) //nolint
125126
copy(dstSlice, s)
126127
}
127128
return intRet(handle) // caller must GlobalFree
@@ -136,9 +137,10 @@ func GlobalData(a Value) Value {
136137
return EmptyStr
137138
}
138139
p := globallock(hm)
139-
assert.That(p != nil)
140+
assert.That(p != 0)
140141
defer globalunlock(hm)
141-
return ptrNstr(p, n)
142+
buf := unsafe.Slice((*byte)(unsafe.Pointer(p)), n) //nolint
143+
return SuStr(string(buf))
142144
}
143145

144146
// dll Kernel32:GlobalUnlock(pointer handle) bool

builtin/sendmsg_wingui.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,7 @@ func SendMessageHditem(a, b, c, d Value) Value {
357357
uintptr(unsafe.Pointer(&hdi)))
358358
fromHdItem(&hdi, d)
359359
if cchTextMaxGiven {
360-
d.Put(nil, SuStr("pszText"),
361-
ptrZstr(unsafe.Pointer(hdi.pszText), int(cchTextMax)))
360+
d.Put(nil, SuStr("pszText"), ptrZstr(hdi.pszText, int(cchTextMax)))
362361
ToContainer(d).Delete(nil, SuStr("cchTextMax"))
363362
runtime.KeepAlive(hdi.pszText)
364363
}

builtin/struct_wingui.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ func (*suRect) updateStruct(ob Value, p unsafe.Pointer) {
142142

143143
func rect(_ *Thread, args []Value) Value {
144144
r := toRect(args[0])
145-
return ptrNstr(unsafe.Pointer(r), nRect)
145+
buf := unsafe.Slice((*byte)(unsafe.Pointer(r)), nRect)
146+
return SuStr(hacks.BStoS(buf))
146147
}
147148

148149
//-------------------------------------------------------------------
@@ -226,8 +227,7 @@ func NMTVDISPINFO(a Value) Value {
226227
ob := fromNMHdr(&di.nmhdr)
227228
ob.Put(nil, SuStr("nmhdr"), fromNMHdr(&di.nmhdr))
228229
tvi := fromTVItem(&di.item)
229-
pszText := unsafe.Pointer(di.item.pszText)
230-
tvi.Put(nil, SuStr("pszText"), ptrZstr(pszText, 1024))
230+
tvi.Put(nil, SuStr("pszText"), ptrZstr(di.item.pszText, 1024))
231231
ob.Put(nil, SuStr("item"), tvi)
232232
return ob
233233
}
@@ -475,7 +475,7 @@ type stSCNotification struct {
475475
ch int32
476476
modifiers int32
477477
modificationType int32
478-
text uintptr
478+
text *byte
479479
length int
480480
linesAdded int
481481
message int32
@@ -514,7 +514,7 @@ func SCNotificationText(a Value) Value {
514514
}
515515
scn := (*stSCNotification)(toptr(adr))
516516
ob := fromSCNotification(scn)
517-
ob.Put(nil, SuStr("text"), ptrZstr(toptr(scn.text), 1024))
517+
ob.Put(nil, SuStr("text"), ptrZstr(scn.text, 1024))
518518
return ob
519519
}
520520

@@ -759,11 +759,12 @@ var _ = Global.Builtin("BITMAPINFOHEADER",
759759

760760
func bmih(_ *Thread, args []Value) Value {
761761
bmih := toBitMapInfoHeader(args[0])
762-
return ptrNstr(unsafe.Pointer(&bmih), nBitMapInfoHeader)
762+
buf := unsafe.Slice((*byte)(unsafe.Pointer(bmih)), nBitMapInfoHeader)
763+
return SuStr(hacks.BStoS(buf))
763764
}
764765

765-
func toBitMapInfoHeader(hdr Value) stBitMapInfoHeader {
766-
return stBitMapInfoHeader{
766+
func toBitMapInfoHeader(hdr Value) *stBitMapInfoHeader {
767+
return &stBitMapInfoHeader{
767768
biSize: int32(nBitMapInfoHeader),
768769
biWidth: getInt32(hdr, "biWidth"),
769770
biHeight: getInt32(hdr, "biHeight"),

0 commit comments

Comments
 (0)