pitrou commented on code in PR #13465:
URL: https://github.com/apache/arrow/pull/13465#discussion_r910102218


##########
java/c/src/main/cpp/jni_wrapper.cc:
##########
@@ -148,16 +176,99 @@ void release_exported(T* base) {
   // Mark released
   base->release = nullptr;
 }
+
+int ArrowArrayStreamGetSchema(ArrowArrayStream* stream, ArrowSchema* out) {
+  assert(stream->private_data != nullptr);
+  InnerPrivateData* private_data =
+      reinterpret_cast<InnerPrivateData*>(stream->private_data);
+  JNIEnvGuard guard(private_data->vm_);
+  JNIEnv* env = guard.env();
+
+  const long out_addr = static_cast<long>(reinterpret_cast<uintptr_t>(out));
+  const int err_code = env->CallIntMethod(private_data->j_private_data_,
+                                          kPrivateDataGetSchemaMethod, 
out_addr);
+  if (env->ExceptionCheck()) {
+    env->ExceptionDescribe();

Review Comment:
   If there's an exception, should it perhaps participate in `last_error_`?



##########
java/c/src/main/cpp/jni_wrapper.cc:
##########
@@ -148,16 +176,99 @@ void release_exported(T* base) {
   // Mark released
   base->release = nullptr;
 }
+
+int ArrowArrayStreamGetSchema(ArrowArrayStream* stream, ArrowSchema* out) {
+  assert(stream->private_data != nullptr);
+  InnerPrivateData* private_data =
+      reinterpret_cast<InnerPrivateData*>(stream->private_data);
+  JNIEnvGuard guard(private_data->vm_);
+  JNIEnv* env = guard.env();
+
+  const long out_addr = static_cast<long>(reinterpret_cast<uintptr_t>(out));
+  const int err_code = env->CallIntMethod(private_data->j_private_data_,
+                                          kPrivateDataGetSchemaMethod, 
out_addr);
+  if (env->ExceptionCheck()) {
+    env->ExceptionDescribe();
+    env->ExceptionClear();
+    return EIO;
+  }
+  return err_code;
+}
+
+int ArrowArrayStreamGetNext(ArrowArrayStream* stream, ArrowArray* out) {
+  assert(stream->private_data != nullptr);
+  InnerPrivateData* private_data =
+      reinterpret_cast<InnerPrivateData*>(stream->private_data);
+  JNIEnvGuard guard(private_data->vm_);
+  JNIEnv* env = guard.env();
+
+  const long out_addr = static_cast<long>(reinterpret_cast<uintptr_t>(out));
+  const int err_code = env->CallIntMethod(private_data->j_private_data_,
+                                          kPrivateDataGetNextMethod, out_addr);
+  if (env->ExceptionCheck()) {
+    env->ExceptionDescribe();
+    env->ExceptionClear();
+    return EIO;
+  }
+  return err_code;
+}
+
+const char* ArrowArrayStreamGetLastError(ArrowArrayStream* stream) {
+  assert(stream->private_data != nullptr);
+  InnerPrivateData* private_data =
+      reinterpret_cast<InnerPrivateData*>(stream->private_data);
+  JNIEnvGuard guard(private_data->vm_);
+  JNIEnv* env = guard.env();
+
+  jobject error_data =
+      env->GetObjectField(private_data->j_private_data_, 
kPrivateDataLastErrorField);
+  if (!error_data) return nullptr;
+
+  auto arr = reinterpret_cast<jbyteArray>(error_data);
+  jbyte* error_bytes = env->GetByteArrayElements(arr, nullptr);
+  if (!error_bytes) return nullptr;
+
+  char* error_str = reinterpret_cast<char*>(error_bytes);
+  private_data->last_error_ = std::string(error_str, std::strlen(error_str));
+
+  env->ReleaseByteArrayElements(arr, error_bytes, JNI_ABORT);
+  return private_data->last_error_.c_str();
+}
+
+void ArrowArrayStreamRelease(ArrowArrayStream* stream) {
+  // This should not be called on already released structure
+  assert(stream->release != nullptr);
+  // Release all data directly owned by the struct
+  InnerPrivateData* private_data =
+      reinterpret_cast<InnerPrivateData*>(stream->private_data);
+
+  JNIEnvGuard guard(private_data->vm_);
+  JNIEnv* env = guard.env();
+
+  env->CallObjectMethod(private_data->j_private_data_, 
kPrivateDataCloseMethod);
+  if (env->ExceptionCheck()) {
+    env->ExceptionDescribe();
+    env->ExceptionClear();
+    ThrowPendingException("Error calling close of private data");

Review Comment:
   Is this right? The release callback could be called from any context, such 
as a Python thread or R interpreter. In those contexts, a C++ exception would 
probably crash the process (or silently exit the thread)?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to