mirror of
https://github.com/golang/go.git
synced 2025-05-07 16:43:03 +00:00
make every func literal expression allocate,
so that == on func means that the functions originated in the same execution of a func literal or definition. before, there was an inconsistency: func() {x++} != func() {x++} but func() {} == func() {} this CL makes the second case != too, just like make(map[int]int) != make(map[int]int) R=r DELTA=202 (71 added, 62 deleted, 69 changed) OCL=32393 CL=32398
This commit is contained in:
parent
83940d7c4a
commit
9346c6d901
@ -553,9 +553,6 @@ funclit1(Node *ntype, NodeList *body)
|
|||||||
// as we referred to variables from the outer function,
|
// as we referred to variables from the outer function,
|
||||||
// we accumulated a list of PHEAP names in func->cvars.
|
// we accumulated a list of PHEAP names in func->cvars.
|
||||||
narg = 0;
|
narg = 0;
|
||||||
if(func->cvars == nil)
|
|
||||||
ft = type;
|
|
||||||
else {
|
|
||||||
// add PHEAP versions as function arguments.
|
// add PHEAP versions as function arguments.
|
||||||
in = nil;
|
in = nil;
|
||||||
for(l=func->cvars; l; l=l->next) {
|
for(l=func->cvars; l; l=l->next) {
|
||||||
@ -583,7 +580,7 @@ funclit1(Node *ntype, NodeList *body)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add a dummy arg for the closure's caller pc
|
// add a dummy arg for the closure's caller pc
|
||||||
d = nod(ODCLFIELD, a, N);
|
d = nod(ODCLFIELD, N, N);
|
||||||
d->type = types[TUINTPTR];
|
d->type = types[TUINTPTR];
|
||||||
in = list(in, d);
|
in = list(in, d);
|
||||||
|
|
||||||
@ -622,7 +619,6 @@ funclit1(Node *ntype, NodeList *body)
|
|||||||
|
|
||||||
ft = functype(N, in, out);
|
ft = functype(N, in, out);
|
||||||
ft->outnamed = type->outnamed;
|
ft->outnamed = type->outnamed;
|
||||||
}
|
|
||||||
|
|
||||||
// declare function.
|
// declare function.
|
||||||
vargen++;
|
vargen++;
|
||||||
@ -642,10 +638,6 @@ funclit1(Node *ntype, NodeList *body)
|
|||||||
funcdepth--;
|
funcdepth--;
|
||||||
autodcl = func->dcl;
|
autodcl = func->dcl;
|
||||||
|
|
||||||
// if there's no closure, we can use f directly
|
|
||||||
if(func->cvars == nil)
|
|
||||||
return f;
|
|
||||||
|
|
||||||
// build up type for this instance of the closure func.
|
// build up type for this instance of the closure func.
|
||||||
in = nil;
|
in = nil;
|
||||||
d = nod(ODCLFIELD, N, N); // siz
|
d = nod(ODCLFIELD, N, N); // siz
|
||||||
|
@ -43,6 +43,8 @@ sys·closure(int32 siz, byte *fn, byte *arg0)
|
|||||||
p = mal(n);
|
p = mal(n);
|
||||||
*ret = p;
|
*ret = p;
|
||||||
q = p + n - siz;
|
q = p + n - siz;
|
||||||
|
|
||||||
|
if(siz > 0) {
|
||||||
mcpy(q, (byte*)&arg0, siz);
|
mcpy(q, (byte*)&arg0, siz);
|
||||||
|
|
||||||
// SUBL $siz, SP
|
// SUBL $siz, SP
|
||||||
@ -79,6 +81,7 @@ sys·closure(int32 siz, byte *fn, byte *arg0)
|
|||||||
*p++ = 0xf3;
|
*p++ = 0xf3;
|
||||||
*p++ = 0xa5;
|
*p++ = 0xa5;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// call fn
|
// call fn
|
||||||
pcrel = fn - (p+5);
|
pcrel = fn - (p+5);
|
||||||
|
@ -43,6 +43,8 @@ sys·closure(int32 siz, byte *fn, byte *arg0)
|
|||||||
p = mal(n);
|
p = mal(n);
|
||||||
*ret = p;
|
*ret = p;
|
||||||
q = p + n - siz;
|
q = p + n - siz;
|
||||||
|
|
||||||
|
if(siz > 0) {
|
||||||
mcpy(q, (byte*)&arg0, siz);
|
mcpy(q, (byte*)&arg0, siz);
|
||||||
|
|
||||||
// SUBQ $siz, SP
|
// SUBQ $siz, SP
|
||||||
@ -82,7 +84,7 @@ sys·closure(int32 siz, byte *fn, byte *arg0)
|
|||||||
*p++ = 0x48;
|
*p++ = 0x48;
|
||||||
*p++ = 0xa5;
|
*p++ = 0xa5;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// call fn
|
// call fn
|
||||||
pcrel = fn - (p+5);
|
pcrel = fn - (p+5);
|
||||||
|
@ -73,6 +73,10 @@ func h() {
|
|||||||
f(500);
|
f(500);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newfunc() (func(int) int) {
|
||||||
|
return func(x int) int { return x }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
go f();
|
go f();
|
||||||
@ -85,4 +89,12 @@ func main() {
|
|||||||
|
|
||||||
go h();
|
go h();
|
||||||
check([]int{100,200,101,201,500,101,201,500});
|
check([]int{100,200,101,201,500,101,201,500});
|
||||||
|
|
||||||
|
x, y := newfunc(), newfunc();
|
||||||
|
if x == y {
|
||||||
|
panicln("newfunc returned same func");
|
||||||
|
}
|
||||||
|
if x(1) != 1 || y(2) != 2 {
|
||||||
|
panicln("newfunc returned broken funcs");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user