|
1 | | -use crate::classes; |
| 1 | +use crate::native::java; |
| 2 | +use crate::native::java::lang::String::StringInterner; |
| 3 | +use crate::native::jni::reference_from_jobject; |
| 4 | +use crate::objects::method::Method; |
| 5 | +use crate::objects::reference::Reference; |
| 6 | +use crate::symbols::sym; |
2 | 7 | use crate::thread::JavaThread; |
| 8 | +use crate::thread::exceptions::Throws; |
| 9 | +use crate::{classes, java_call}; |
3 | 10 |
|
4 | 11 | use core::ffi::c_char; |
| 12 | +use std::{ptr, slice}; |
5 | 13 |
|
6 | | -use jni::sys::{JNIEnv, jboolean, jclass, jint, jthrowable}; |
| 14 | +use ::jni::sys::{JNI_ERR, JNI_OK, JNIEnv, jboolean, jclass, jint, jthrowable}; |
| 15 | +use common::unicode; |
| 16 | +use instructions::Operand; |
| 17 | +use libc::strlen; |
7 | 18 |
|
8 | 19 | #[unsafe(no_mangle)] |
9 | 20 | pub extern "system" fn Throw(env: *mut JNIEnv, obj: jthrowable) -> jint { |
10 | | - unimplemented!("jni::Throw"); |
| 21 | + let thread = JavaThread::current(); |
| 22 | + assert_eq!(thread.env().raw(), env); |
| 23 | + |
| 24 | + let Some(throwable) = (unsafe { reference_from_jobject(obj) }) else { |
| 25 | + return JNI_ERR; |
| 26 | + }; |
| 27 | + |
| 28 | + if !throwable.is_instance_of(crate::globals::classes::java_lang_Throwable()) { |
| 29 | + return JNI_ERR; |
| 30 | + } |
| 31 | + |
| 32 | + thread.set_pending_exception(throwable); |
| 33 | + |
| 34 | + JNI_OK |
11 | 35 | } |
12 | 36 |
|
13 | 37 | #[unsafe(no_mangle)] |
14 | 38 | pub extern "system" fn ThrowNew(env: *mut JNIEnv, clazz: jclass, msg: *const c_char) -> jint { |
15 | | - unimplemented!("jni::ThrowNew"); |
| 39 | + let thread = JavaThread::current(); |
| 40 | + assert_eq!(thread.env().raw(), env); |
| 41 | + |
| 42 | + let Some(mirror) = (unsafe { reference_from_jobject(clazz) }) else { |
| 43 | + return JNI_ERR; |
| 44 | + }; |
| 45 | + |
| 46 | + let mut message = None; |
| 47 | + if !msg.is_null() { |
| 48 | + // SAFETY: It's entirely up to the caller to pass in a valid string |
| 49 | + let len = unsafe { strlen(msg) }; |
| 50 | + |
| 51 | + // SAFETY: c_char is always 8 bits |
| 52 | + let utf = unsafe { slice::from_raw_parts(msg.cast::<u8>(), len) }; |
| 53 | + |
| 54 | + if let Ok(utf_8) = unicode::decode(utf) { |
| 55 | + message = Some(utf_8); |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + let throwable_class = mirror.extract_target_class(); |
| 60 | + |
| 61 | + let constructor; |
| 62 | + if message.is_none() { |
| 63 | + constructor = throwable_class |
| 64 | + .resolve_method(sym!(object_initializer_name), sym!(void_method_signature)); |
| 65 | + } else { |
| 66 | + constructor = throwable_class |
| 67 | + .resolve_method(sym!(object_initializer_name), sym!(String_void_signature)); |
| 68 | + } |
| 69 | + |
| 70 | + match constructor { |
| 71 | + Throws::Ok(constructor) => match message { |
| 72 | + Some(message) => { |
| 73 | + let string = StringInterner::intern(message); |
| 74 | + java_call!( |
| 75 | + thread, |
| 76 | + constructor, |
| 77 | + Operand::Reference(Reference::class(string)) |
| 78 | + ); |
| 79 | + }, |
| 80 | + None => { |
| 81 | + java_call!(thread, constructor); |
| 82 | + }, |
| 83 | + }, |
| 84 | + Throws::Exception(e) => { |
| 85 | + e.throw(thread); |
| 86 | + return JNI_ERR; |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + JNI_OK |
16 | 91 | } |
17 | 92 |
|
18 | 93 | #[unsafe(no_mangle)] |
|
0 commit comments