-
Notifications
You must be signed in to change notification settings - Fork 17.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/cgo: permit passing string values directly between Go and C
Permit the C preamble to use the _GoString_ type. Permit Go code to pass string values directly to those C types. Add accessors for C code to retrieve sizes and pointers. Fixes #6907 Change-Id: I190c88319ec88a3ef0ddb99f342a843ba69fcaa3 Reviewed-on: https://go-review.googlesource.com/70890 Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Austin Clements <[email protected]>
- Loading branch information
1 parent
707a4d3
commit 918396b
Showing
7 changed files
with
148 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2017 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 cgotest | ||
|
||
/* | ||
#include <stdlib.h> | ||
#include <string.h> | ||
char* Issue6907CopyString(_GoString_ s) { | ||
size_t n; | ||
const char *p; | ||
char *r; | ||
n = _GoStringLen(s); | ||
p = _GoStringPtr(s); | ||
r = malloc(n + 1); | ||
memmove(r, p, n); | ||
r[n] = '\0'; | ||
return r; | ||
} | ||
*/ | ||
import "C" | ||
|
||
import "testing" | ||
|
||
func test6907(t *testing.T) { | ||
want := "yarn" | ||
if got := C.GoString(C.Issue6907CopyString(want)); got != want { | ||
t.Errorf("C.GoString(C.Issue6907CopyString(%q)) == %q, want %q", want, got, want) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2017 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 cgotest | ||
|
||
/* | ||
extern int CheckIssue6907C(_GoString_); | ||
*/ | ||
import "C" | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
const CString = "C string" | ||
|
||
//export CheckIssue6907Go | ||
func CheckIssue6907Go(s string) C.int { | ||
if s == CString { | ||
return 1 | ||
} | ||
return 0 | ||
} | ||
|
||
func test6907Go(t *testing.T) { | ||
if got := C.CheckIssue6907C(CString); got != 1 { | ||
t.Errorf("C.CheckIssue6907C() == %d, want %d", got, 1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright 2017 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. | ||
|
||
#include <string.h> | ||
|
||
#include "_cgo_export.h" | ||
|
||
int CheckIssue6907C(_GoString_ s) { | ||
return CheckIssue6907Go(s); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters