Skip to content

Commit b641e5a

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 b641e5a

File tree

3 files changed

+7
-21
lines changed

3 files changed

+7
-21
lines changed

alloc.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package lua
22

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

@@ -13,9 +12,6 @@ type iface struct {
1312

1413
const preloadLimit LNumber = 128
1514

16-
var _fv float64
17-
var _uv uintptr
18-
1915
var preloads [int(preloadLimit)]LValue
2016

2117
func init() {
@@ -26,21 +22,18 @@ func init() {
2622

2723
// allocator is a fast bulk memory allocator for the LValue.
2824
type allocator struct {
29-
size int
30-
fptrs []float64
31-
fheader *reflect.SliceHeader
25+
size int
26+
fptrs []float64
3227

3328
scratchValue LValue
3429
scratchValueP *iface
3530
}
3631

3732
func newAllocator(size int) *allocator {
3833
al := &allocator{
39-
size: size,
40-
fptrs: make([]float64, 0, size),
41-
fheader: nil,
34+
size: size,
35+
fptrs: make([]float64, 0, size),
4236
}
43-
al.fheader = (*reflect.SliceHeader)(unsafe.Pointer(&al.fptrs))
4437
al.scratchValue = LNumber(0)
4538
al.scratchValueP = (*iface)(unsafe.Pointer(&al.scratchValue))
4639

@@ -63,7 +56,6 @@ func (al *allocator) LNumber2I(v LNumber) LValue {
6356
// check if we need a new alloc page
6457
if cap(al.fptrs) == len(al.fptrs) {
6558
al.fptrs = make([]float64, 0, al.size)
66-
al.fheader = (*reflect.SliceHeader)(unsafe.Pointer(&al.fptrs))
6759
}
6860

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

go.mod

+1-1
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

+2-8
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)