]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: make the char * pointer in GoString const
authorIan Lance Taylor <iant@golang.org>
Fri, 20 Nov 2015 01:03:01 +0000 (17:03 -0800)
committerIan Lance Taylor <iant@golang.org>
Sun, 29 Nov 2015 16:55:40 +0000 (16:55 +0000)
This makes it more convenient for C code to use GoString with string
constants.  Since Go string values are immutable, the const qualifier is
appropriate in C.

Change-Id: I5fb3cdce2ce5079f1f0467a1544bb3a1eb27b811
Reviewed-on: https://go-review.googlesource.com/17067
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
misc/cgo/test/callback.go
misc/cgo/test/callback_c.c
misc/cgo/test/cgo_test.go
src/cmd/cgo/out.go

index bf468008a382296e38959a2132d8e648710301b8..21d1df59eddcbf84edd62f670060a9e4b5388168 100644 (file)
@@ -12,6 +12,7 @@ void callPanic(void);
 int callGoReturnVal(void);
 int returnAfterGrow(void);
 int returnAfterGrowFromGo(void);
+void callGoWithString(void);
 */
 import "C"
 
@@ -276,6 +277,22 @@ func goReturnVal() (r C.int) {
        return
 }
 
+// Test that C can pass in a Go string from a string constant.
+func testCallGoWithString(t *testing.T) {
+       C.callGoWithString()
+       want := "string passed from C to Go"
+       if stringFromGo != want {
+               t.Errorf("string passed through C is %s, want %s", stringFromGo, want)
+       }
+}
+
+var stringFromGo string
+
+//export goWithString
+func goWithString(s string) {
+       stringFromGo = s
+}
+
 func testCallbackStack(t *testing.T) {
        // Make cgo call and callback with different amount of stack stack available.
        // We do not do any explicit checks, just ensure that it does not crash.
index 5bb642534019ad545a87ee6d97a96d0796085dcc..5ae237b14c1b1f050d8528118e98aadd074d4f03 100644 (file)
@@ -2,6 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+#include <string.h>
 #include <sys/types.h>
 #include <unistd.h>
 #include "_cgo_export.h"
@@ -80,3 +81,10 @@ returnAfterGrowFromGo(void)
        return goReturnVal();
 }
 
+void
+callGoWithString(void)
+{
+       extern void goWithString(GoString);
+       const char *str = "string passed from C to Go";
+       goWithString((GoString){str, strlen(str)});
+}
index 08800479536dcaa033fed2322f00735cb989d185..48fb6f18b7fb537144b10c174a02eadd74ca6e3e 100644 (file)
@@ -68,5 +68,6 @@ func Test10303(t *testing.T)                 { test10303(t, 10) }
 func Test11925(t *testing.T)                 { test11925(t) }
 func Test12030(t *testing.T)                 { test12030(t) }
 func TestGCC68255(t *testing.T)              { testGCC68255(t) }
+func TestCallGoWithString(t *testing.T)      { testCallGoWithString(t) }
 
 func BenchmarkCgoCall(b *testing.B) { benchCgoCall(b) }
index 473bc9a9273c8e53724227a2a6f827d454f5e11d..8aca64a874ab485448a9ecc4f4fc5b469bf639df 100644 (file)
@@ -1429,7 +1429,7 @@ typedef __complex double GoComplex128;
 */
 typedef char _check_for_GOINTBITS_bit_pointer_matching_GoInt[sizeof(void*)==GOINTBITS/8 ? 1:-1];
 
-typedef struct { char *p; GoInt n; } GoString;
+typedef struct { const char *p; GoInt n; } GoString;
 typedef void *GoMap;
 typedef void *GoChan;
 typedef struct { void *t; void *v; } GoInterface;