Skip to content

Commit 92f71c0

Browse files
authored
Merge pull request #1321 from Workiva/add2Reflects
[go1.20] Removing some uses of `unsafeheader.Slice` from reflect
2 parents 938fba2 + ba80a57 commit 92f71c0

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

compiler/natives/src/reflect/reflect.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,10 @@ func getJsTag(tag string) string {
13251325
return ""
13261326
}
13271327

1328+
func (v Value) UnsafePointer() unsafe.Pointer {
1329+
return unsafe.Pointer(v.Pointer())
1330+
}
1331+
13281332
func (v Value) grow(n int) {
13291333
if n < 0 {
13301334
panic(`reflect.Value.Grow: negative len`)
@@ -1343,6 +1347,32 @@ func (v Value) grow(n int) {
13431347
}
13441348
}
13451349

1350+
// extendSlice is used by native reflect.Append and reflect.AppendSlice
1351+
// Overridden to avoid the use of `unsafeheader.Slice` since GopherJS
1352+
// uses different slice implementation.
1353+
func (v Value) extendSlice(n int) Value {
1354+
v.mustBeExported()
1355+
v.mustBe(Slice)
1356+
1357+
s := v.object()
1358+
sNil := jsType(v.typ).Get(`nil`)
1359+
fl := flagIndir | flag(Slice)
1360+
if s == sNil && n <= 0 {
1361+
return makeValue(v.typ, wrapJsObject(v.typ, sNil), fl)
1362+
}
1363+
1364+
newSlice := jsType(v.typ).New(s.Get("$array"))
1365+
newSlice.Set("$offset", s.Get("$offset"))
1366+
newSlice.Set("$length", s.Get("$length"))
1367+
newSlice.Set("$capacity", s.Get("$capacity"))
1368+
1369+
v2 := makeValue(v.typ, wrapJsObject(v.typ, newSlice), fl)
1370+
v2.grow(n)
1371+
s2 := v2.object()
1372+
s2.Set(`$length`, s2.Get(`$length`).Int()+n)
1373+
return v2
1374+
}
1375+
13461376
func (v Value) Index(i int) Value {
13471377
switch k := v.kind(); k {
13481378
case Array:
@@ -1399,6 +1429,11 @@ func (v Value) InterfaceData() [2]uintptr {
13991429
panic(errors.New("InterfaceData is not supported by GopherJS"))
14001430
}
14011431

1432+
func (v Value) SetZero() {
1433+
v.mustBeAssignable()
1434+
v.Set(Zero(v.typ))
1435+
}
1436+
14021437
func (v Value) IsNil() bool {
14031438
switch k := v.kind(); k {
14041439
case Ptr, Slice:
@@ -1438,6 +1473,9 @@ func (v Value) Len() int {
14381473
}
14391474
}
14401475

1476+
//gopherjs:purge Not used since Len() is overridden.
1477+
func (v Value) lenNonSlice() int
1478+
14411479
func (v Value) Pointer() uintptr {
14421480
switch k := v.kind(); k {
14431481
case Chan, Map, Ptr, UnsafePointer:

0 commit comments

Comments
 (0)