Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
3bac16a6
Commit
3bac16a6
authored
Apr 19, 2011
by
Russ Cox
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
reflect: allow Slice of arrays
R=r CC=golang-dev
https://golang.org/cl/4444049
parent
b331f3cf
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
55 additions
and
17 deletions
+55
-17
reflect.c
src/cmd/gc/reflect.c
+19
-9
all_test.go
src/pkg/reflect/all_test.go
+14
-0
type.go
src/pkg/reflect/type.go
+1
-0
value.go
src/pkg/reflect/value.go
+18
-6
type.go
src/pkg/runtime/type.go
+3
-2
No files found.
src/cmd/gc/reflect.c
View file @
3bac16a6
...
...
@@ -690,7 +690,7 @@ dtypesym(Type *t)
int
ot
,
xt
,
n
,
isddd
,
dupok
;
Sym
*
s
,
*
s1
,
*
s2
;
Sig
*
a
,
*
m
;
Type
*
t1
,
*
tbase
;
Type
*
t1
,
*
tbase
,
*
t2
;
if
(
isideal
(
t
))
fatal
(
"dtypesym %T"
,
t
);
...
...
@@ -727,15 +727,25 @@ ok:
break
;
case
TARRAY
:
// ../../pkg/runtime/type.go:/ArrayType
s1
=
dtypesym
(
t
->
type
);
ot
=
dcommontype
(
s
,
ot
,
t
);
xt
=
ot
-
2
*
widthptr
;
ot
=
dsymptr
(
s
,
ot
,
s1
,
0
);
if
(
t
->
bound
<
0
)
ot
=
duintptr
(
s
,
ot
,
-
1
);
else
if
(
t
->
bound
>=
0
)
{
// ../../pkg/runtime/type.go:/ArrayType
s1
=
dtypesym
(
t
->
type
);
t2
=
typ
(
TARRAY
);
t2
->
type
=
t
->
type
;
t2
->
bound
=
-
1
;
// slice
s2
=
dtypesym
(
t2
);
ot
=
dcommontype
(
s
,
ot
,
t
);
xt
=
ot
-
2
*
widthptr
;
ot
=
dsymptr
(
s
,
ot
,
s1
,
0
);
ot
=
dsymptr
(
s
,
ot
,
s2
,
0
);
ot
=
duintptr
(
s
,
ot
,
t
->
bound
);
}
else
{
// ../../pkg/runtime/type.go:/SliceType
s1
=
dtypesym
(
t
->
type
);
ot
=
dcommontype
(
s
,
ot
,
t
);
xt
=
ot
-
2
*
widthptr
;
ot
=
dsymptr
(
s
,
ot
,
s1
,
0
);
}
break
;
case
TCHAN
:
...
...
src/pkg/reflect/all_test.go
View file @
3bac16a6
...
...
@@ -1435,3 +1435,17 @@ func TestSmallNegativeInt(t *testing.T) {
t
.
Errorf
(
"int16(-1).Int() returned %v"
,
v
.
Int
())
}
}
func
TestSlice
(
t
*
testing
.
T
)
{
xs
:=
[]
int
{
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
}
v
:=
NewValue
(
xs
)
.
Slice
(
3
,
5
)
.
Interface
()
.
([]
int
)
if
len
(
v
)
!=
2
||
v
[
0
]
!=
4
||
v
[
1
]
!=
5
{
t
.
Errorf
(
"xs.Slice(3, 5) = %v"
,
v
)
}
xa
:=
[
7
]
int
{
10
,
20
,
30
,
40
,
50
,
60
,
70
}
v
=
NewValue
(
&
xa
)
.
Elem
()
.
Slice
(
2
,
5
)
.
Interface
()
.
([]
int
)
if
len
(
v
)
!=
3
||
v
[
0
]
!=
30
||
v
[
1
]
!=
40
||
v
[
2
]
!=
50
{
t
.
Errorf
(
"xa.Slice(2, 5) = %v"
,
v
)
}
}
src/pkg/reflect/type.go
View file @
3bac16a6
...
...
@@ -260,6 +260,7 @@ const (
type
arrayType
struct
{
commonType
"array"
elem
*
runtime
.
Type
slice
*
runtime
.
Type
len
uintptr
}
...
...
src/pkg/reflect/value.go
View file @
3bac16a6
...
...
@@ -1244,20 +1244,32 @@ func (v Value) SetString(x string) {
*
(
*
string
)(
iv
.
addr
)
=
x
}
// BUG(rsc): Value.Slice should allow slicing arrays.
// Slice returns a slice of v.
// It panics if v's Kind is not Slice.
// It panics if v's Kind is not
Array or
Slice.
func
(
v
Value
)
Slice
(
beg
,
end
int
)
Value
{
iv
:=
v
.
internal
()
iv
.
mustBe
(
Slice
)
if
iv
.
kind
!=
Array
&&
iv
.
kind
!=
Slice
{
panic
(
&
ValueError
{
"reflect.Value.Slice"
,
iv
.
kind
})
}
cap
:=
v
.
Cap
()
if
beg
<
0
||
end
<
beg
||
end
>
cap
{
panic
(
"reflect.Value.Slice: slice index out of bounds"
)
}
typ
:=
iv
.
typ
.
toType
()
var
typ
Type
var
base
uintptr
switch
iv
.
kind
{
case
Array
:
if
iv
.
flag
&
flagAddr
==
0
{
panic
(
"reflect.Value.Slice: slice of unaddressable array"
)
}
typ
=
toType
((
*
arrayType
)(
unsafe
.
Pointer
(
iv
.
typ
))
.
slice
)
base
=
uintptr
(
iv
.
addr
)
case
Slice
:
typ
=
iv
.
typ
.
toType
()
base
=
(
*
SliceHeader
)(
iv
.
addr
)
.
Data
}
s
:=
new
(
SliceHeader
)
s
.
Data
=
uintptr
((
*
SliceHeader
)(
iv
.
addr
)
.
Data
)
+
uintptr
(
beg
)
*
typ
.
Elem
()
.
Size
()
s
.
Data
=
base
+
uintptr
(
beg
)
*
typ
.
Elem
()
.
Size
()
s
.
Len
=
end
-
beg
s
.
Cap
=
cap
-
beg
return
valueFromAddr
(
iv
.
flag
&
flagRO
,
typ
,
unsafe
.
Pointer
(
s
))
...
...
src/pkg/runtime/type.go
View file @
3bac16a6
...
...
@@ -117,8 +117,9 @@ type UnsafePointerType commonType
// ArrayType represents a fixed array type.
type
ArrayType
struct
{
commonType
elem
*
Type
// array element type
len
uintptr
elem
*
Type
// array element type
slice
*
Type
// slice type
len
uintptr
}
// SliceType represents a slice type.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment