File tree 4 files changed +42
-1
lines changed
4 files changed +42
-1
lines changed Original file line number Diff line number Diff line change
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 {}
Original file line number Diff line number Diff line change @@ -5,6 +5,16 @@ package unsafeheader_test
5
5
6
6
import "testing"
7
7
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
+
8
18
func TestWriteThroughHeader (t * testing.T ) {
9
19
t .Skip ("GopherJS uses different slice and string implementation than internal/unsafeheader." )
10
20
}
Original file line number Diff line number Diff line change @@ -832,6 +832,21 @@ func cvtSliceArrayPtr(v Value, t Type) Value {
832
832
return Value {t .common (), unsafe .Pointer (array .Unsafe ()), v .flag &^(flagIndir | flagAddr | flagKindMask ) | flag (Ptr )}
833
833
}
834
834
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
+
835
850
func Copy (dst , src Value ) int {
836
851
dk := dst .kind ()
837
852
if dk != Array && dk != Slice {
Original file line number Diff line number Diff line change @@ -185,7 +185,7 @@ var $sliceToNativeArray = slice => {
185
185
return slice . $array . slice ( slice . $offset , slice . $offset + slice . $length ) ;
186
186
} ;
187
187
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` .
189
189
//
190
190
// Note that an array pointer can be represented by an "unwrapped" native array
191
191
// type, and it will be wrapped back into its Go type when necessary.
You can’t perform that action at this time.
0 commit comments