-
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: make JNI's jobject type map to uintptr in Go
The jobject type is declared as a pointer, but some JVMs (Dalvik, ART) store non-pointer values in them. In Go, we must use uintptr instead of a real pointer for these types. This is similar to the CoreFoundation types on Darwin which were "fixed" in CL 66332. Update #22906 Update #21897 RELNOTE=yes Change-Id: I0d4c664501d89a696c2fb037c995503caabf8911 Reviewed-on: https://go-review.googlesource.com/81876 Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
- Loading branch information
Showing
6 changed files
with
406 additions
and
10 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,74 @@ | ||
// 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. | ||
|
||
// +build cgo | ||
|
||
package cgotest | ||
|
||
/* | ||
// It's going to be hard to include a whole real JVM to test this. | ||
// So we'll simulate a really easy JVM using just the parts we need. | ||
// This is the relevant part of jni.h. | ||
struct _jobject; | ||
typedef struct _jobject *jobject; | ||
typedef jobject jclass; | ||
typedef jobject jthrowable; | ||
typedef jobject jstring; | ||
typedef jobject jarray; | ||
typedef jarray jbooleanArray; | ||
typedef jarray jbyteArray; | ||
typedef jarray jcharArray; | ||
typedef jarray jshortArray; | ||
typedef jarray jintArray; | ||
typedef jarray jlongArray; | ||
typedef jarray jfloatArray; | ||
typedef jarray jdoubleArray; | ||
typedef jarray jobjectArray; | ||
typedef jobject jweak; | ||
// Note: jvalue is already a non-pointer type due to it being a C union. | ||
*/ | ||
import "C" | ||
import ( | ||
"testing" | ||
) | ||
|
||
func test22906(t *testing.T) { | ||
var x1 C.jobject = 0 // Note: 0, not nil. That makes sure we use uintptr for these types. | ||
_ = x1 | ||
var x2 C.jclass = 0 | ||
_ = x2 | ||
var x3 C.jthrowable = 0 | ||
_ = x3 | ||
var x4 C.jstring = 0 | ||
_ = x4 | ||
var x5 C.jarray = 0 | ||
_ = x5 | ||
var x6 C.jbooleanArray = 0 | ||
_ = x6 | ||
var x7 C.jbyteArray = 0 | ||
_ = x7 | ||
var x8 C.jcharArray = 0 | ||
_ = x8 | ||
var x9 C.jshortArray = 0 | ||
_ = x9 | ||
var x10 C.jintArray = 0 | ||
_ = x10 | ||
var x11 C.jlongArray = 0 | ||
_ = x11 | ||
var x12 C.jfloatArray = 0 | ||
_ = x12 | ||
var x13 C.jdoubleArray = 0 | ||
_ = x13 | ||
var x14 C.jobjectArray = 0 | ||
_ = x14 | ||
var x15 C.jweak = 0 | ||
_ = x15 | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// 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 main | ||
|
||
import ( | ||
"go/ast" | ||
) | ||
|
||
func init() { | ||
register(jniFix) | ||
} | ||
|
||
var jniFix = fix{ | ||
name: "jni", | ||
date: "2017-12-04", | ||
f: jnifix, | ||
desc: `Fixes initializers of JNI's jobject and subtypes`, | ||
disabled: false, | ||
} | ||
|
||
// Old state: | ||
// type jobject *_jobject | ||
// New state: | ||
// type jobject uintptr | ||
// and similar for subtypes of jobject. | ||
// This fix finds nils initializing these types and replaces the nils with 0s. | ||
func jnifix(f *ast.File) bool { | ||
return typefix(f, func(s string) bool { | ||
switch s { | ||
case "C.jobject": | ||
return true | ||
case "C.jclass": | ||
return true | ||
case "C.jthrowable": | ||
return true | ||
case "C.jstring": | ||
return true | ||
case "C.jarray": | ||
return true | ||
case "C.jbooleanArray": | ||
return true | ||
case "C.jbyteArray": | ||
return true | ||
case "C.jcharArray": | ||
return true | ||
case "C.jshortArray": | ||
return true | ||
case "C.jintArray": | ||
return true | ||
case "C.jlongArray": | ||
return true | ||
case "C.jfloatArray": | ||
return true | ||
case "C.jdoubleArray": | ||
return true | ||
case "C.jobjectArray": | ||
return true | ||
case "C.jweak": | ||
return true | ||
} | ||
return false | ||
}) | ||
} |
Oops, something went wrong.