Skip to content

Commit ef98077

Browse files
committed
Remove reflect.SliceHeader usage
reflect.SliceHeader is deprecated in favor of unsafe functions introduced in go 1.20 alloc's fheader was unused, so remove, leaving only unsafeFastStringToReadOnlyBytes to port
1 parent 2348fd0 commit ef98077

File tree

3 files changed

+7
-18
lines changed

3 files changed

+7
-18
lines changed

alloc.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package lua
22

33
import (
4-
"reflect"
54
"unsafe"
65
)
76

@@ -26,21 +25,18 @@ func init() {
2625

2726
// allocator is a fast bulk memory allocator for the LValue.
2827
type allocator struct {
29-
size int
30-
fptrs []float64
31-
fheader *reflect.SliceHeader
28+
size int
29+
fptrs []float64
3230

3331
scratchValue LValue
3432
scratchValueP *iface
3533
}
3634

3735
func newAllocator(size int) *allocator {
3836
al := &allocator{
39-
size: size,
40-
fptrs: make([]float64, 0, size),
41-
fheader: nil,
37+
size: size,
38+
fptrs: make([]float64, 0, size),
4239
}
43-
al.fheader = (*reflect.SliceHeader)(unsafe.Pointer(&al.fptrs))
4440
al.scratchValue = LNumber(0)
4541
al.scratchValueP = (*iface)(unsafe.Pointer(&al.scratchValue))
4642

@@ -63,7 +59,6 @@ func (al *allocator) LNumber2I(v LNumber) LValue {
6359
// check if we need a new alloc page
6460
if cap(al.fptrs) == len(al.fptrs) {
6561
al.fptrs = make([]float64, 0, al.size)
66-
al.fheader = (*reflect.SliceHeader)(unsafe.Pointer(&al.fptrs))
6762
}
6863

6964
// alloc a new float, and store our value into it

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/yuin/gopher-lua
22

3-
go 1.17
3+
go 1.20
44

55
require github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
66

utils.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bufio"
55
"fmt"
66
"io"
7-
"reflect"
87
"strconv"
98
"strings"
109
"time"
@@ -255,11 +254,6 @@ func strCmp(s1, s2 string) int {
255254
}
256255
}
257256

258-
func unsafeFastStringToReadOnlyBytes(s string) (bs []byte) {
259-
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
260-
bh := (*reflect.SliceHeader)(unsafe.Pointer(&bs))
261-
bh.Data = sh.Data
262-
bh.Cap = sh.Len
263-
bh.Len = sh.Len
264-
return
257+
func unsafeFastStringToReadOnlyBytes(s string) []byte {
258+
return unsafe.Slice(unsafe.StringData(s), len(s))
265259
}

0 commit comments

Comments
 (0)