Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a6a3260

Browse files
Fixing cvtSliceArray
1 parent 938fba2 commit a6a3260

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//go:build js
2+
// +build js
3+
4+
package unsafeheader
5+
6+
// Slice and String is Go's runtime representations which is different
7+
// from GopherJS's runtime representations. By purging these types,
8+
// it will prevent failures in JS where the code compiles fine but
9+
// expects there to be a constructor which doesn't exist when casting
10+
// from GopherJS's representation into Go's representation.
11+
12+
//gopherjs:purge
13+
type Slice struct{}
14+
15+
//gopherjs:purge
16+
type String struct{}

compiler/natives/src/internal/unsafeheader/unsafeheader_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ package unsafeheader_test
55

66
import "testing"
77

8+
func TestTypeMatchesReflectType(t *testing.T) {
9+
t.Skip("GopherJS uses different slice and string implementation than internal/unsafeheader.")
10+
}
11+
12+
//gopherjs:purge
13+
func testHeaderMatchesReflect()
14+
15+
//gopherjs:purge
16+
func typeCompatible()
17+
818
func TestWriteThroughHeader(t *testing.T) {
919
t.Skip("GopherJS uses different slice and string implementation than internal/unsafeheader.")
1020
}

compiler/natives/src/reflect/reflect.go

+15
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,21 @@ func cvtSliceArrayPtr(v Value, t Type) Value {
832832
return Value{t.common(), unsafe.Pointer(array.Unsafe()), v.flag&^(flagIndir|flagAddr|flagKindMask) | flag(Ptr)}
833833
}
834834

835+
// convertOp: []T -> [N]T
836+
func cvtSliceArray(v Value, t Type) Value {
837+
n := t.Len()
838+
if n > v.Len() {
839+
panic("reflect: cannot convert slice with length " + itoa.Itoa(v.Len()) + " to array with length " + itoa.Itoa(n))
840+
}
841+
842+
slice := v.object()
843+
dst := MakeSlice(SliceOf(t.Elem()), n, n).object()
844+
js.Global.Call("$copySlice", dst, slice)
845+
846+
arr := dst.Get("$array")
847+
return Value{t.common(), unsafe.Pointer(arr.Unsafe()), v.flag&^(flagAddr|flagKindMask) | flag(Array)}
848+
}
849+
835850
func Copy(dst, src Value) int {
836851
dk := dst.kind()
837852
if dk != Array && dk != Slice {

compiler/prelude/prelude.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ var $sliceToNativeArray = slice => {
185185
return slice.$array.slice(slice.$offset, slice.$offset + slice.$length);
186186
};
187187

188-
// Convert Go slice to a pointer to an underlying Go array.
188+
// Convert Go slice to a pointer to an underlying Go array, `[]T -> *[N]T`.
189189
//
190190
// Note that an array pointer can be represented by an "unwrapped" native array
191191
// type, and it will be wrapped back into its Go type when necessary.

0 commit comments

Comments
 (0)