<p>
<font color=red>
Implementation does not honor the restriction on goto statements and targets (no intervening declarations).
-<br/>
-cap() does not work on chans.
-<br/>
-len() does not work on chans.
</font>
</p>
break;
case OLEN:
- if(istype(nl->type, TMAP)) {
- // map has len in the first 32-bit word.
+ if(istype(nl->type, TMAP) || istype(nl->type, TCHAN)) {
+ // map and chan have len in the first 32-bit word.
// a zero pointer means zero length
regalloc(&n1, types[tptr], res);
cgen(nl, &n1);
break;
case OCAP:
+ if(istype(nl->type, TCHAN)) {
+ // chan has cap in the second 32-bit word.
+ // a zero pointer means zero length
+ regalloc(&n1, types[tptr], res);
+ cgen(nl, &n1);
+
+ nodconst(&n2, types[tptr], 0);
+ gins(optoas(OCMP, types[tptr]), &n1, &n2);
+ p1 = gbranch(optoas(OEQ, types[tptr]), T);
+
+ n2 = n1;
+ n2.op = OINDREG;
+ n2.xoffset = 4;
+ n2.type = types[TINT32];
+ gmove(&n2, &n1);
+
+ patch(p1, pc);
+
+ gmove(&n1, res);
+ regfree(&n1);
+ break;
+ }
if(isslice(nl->type)) {
regalloc(&n1, types[tptr], res);
agen(nl, &n1);
break;
case OLEN:
- if(istype(nl->type, TMAP)) {
+ if(istype(nl->type, TMAP) || istype(nl->type, TCHAN)) {
// map has len in the first 32-bit word.
// a zero pointer means zero length
tempalloc(&n1, types[tptr]);
break;
case OCAP:
+ if(istype(nl->type, TCHAN)) {
+ // chan has cap in the second 32-bit word.
+ // a zero pointer means zero length
+ regalloc(&n1, types[tptr], res);
+ cgen(nl, &n1);
+
+ nodconst(&n2, types[tptr], 0);
+ gins(optoas(OCMP, types[tptr]), &n1, &n2);
+ p1 = gbranch(optoas(OEQ, types[tptr]), T);
+
+ n2 = n1;
+ n2.op = OINDREG;
+ n2.xoffset = 4;
+ n2.type = types[TINT32];
+ gmove(&n2, &n1);
+
+ patch(p1, pc);
+
+ gmove(&n1, res);
+ regfree(&n1);
+ break;
+ }
if(isslice(nl->type)) {
igen(nl, &n1, res);
n1.op = OINDREG;
okforbool[TBOOL] = 1;
okforcap[TARRAY] = 1;
- //okforcap[TCHAN] = 1;
- //okforcap[TMAP] = 1;
+ okforcap[TCHAN] = 1;
okforlen[TARRAY] = 1;
- //okforlen[TCHAN] = 1;
+ okforlen[TCHAN] = 1;
okforlen[TMAP] = 1;
okforlen[TSTRING] = 1;
struct Hchan
{
+ uint32 qcount; // total data in the q
+ uint32 dataqsiz; // size of the circular q
uint16 elemsize;
uint16 closed; // Wclosed Rclosed errorcount
- uint32 dataqsiz; // size of the circular q
- uint32 qcount; // total data in the q
Alg* elemalg; // interface for element type
Link* senddataq; // pointer for sender
Link* recvdataq; // pointer for receiver
--- /dev/null
+// $G $D/$F.go && $L $F.$A && ./$A.out
+
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+func main() {
+ c := make(chan int, 10);
+ if len(c) != 0 || cap(c) != 10 {
+ panicln("chan len/cap ", len(c), cap(c), " want 0 10");
+ }
+
+ for i := 0; i < 3; i++ {
+ c <- i;
+ }
+ if len(c) != 3 || cap(c) != 10 {
+ panicln("chan len/cap ", len(c), cap(c), " want 3 10");
+ }
+
+ c = make(chan int);
+ if len(c) != 0 || cap(c) != 0 {
+ panicln("chan len/cap ", len(c), cap(c), " want 0 0");
+ }
+}
+