| // #cgo LDFLAGS: -ljniwrapper |
| // #include "jni_wrapper.h" |
| func newContext(env *C.JNIEnv, jContext C.jobject) (*context, error) { |
| if status := C.GetJavaVM(env, &jVM); status != 0 { |
| return nil, fmt.Errorf("couldn't get Java VM from the (Java) environment") |
| // Reference Java context; it will be de-referenced when the go context |
| // created below is garbage-collected (through the finalizer callback we |
| jContext = C.NewGlobalRef(env, jContext) |
| runtime.SetFinalizer(c, func(c *context) { |
| C.AttachCurrentThread(c.jVM, &env, nil) |
| defer C.DetachCurrentThread(c.jVM) |
| C.DeleteGlobalRef(env, c.jContext) |