Skip to content

Commit e1ea8c8

Browse files
committed
🦄 refactor: Revise error handling 3
1 parent 6c89744 commit e1ea8c8

File tree

15 files changed

+6486
-5996
lines changed

15 files changed

+6486
-5996
lines changed

rust/src/ast_utils.rs

+5,739-5,394
Large diffs are not rendered by default.

rust/src/comment_utils.rs

+37-35
Original file line numberDiff line numberDiff line change
@@ -15,67 +15,69 @@
1515
* limitations under the License.
1616
*/
1717

18+
use anyhow::{Error, Result};
1819
use deno_ast::swc::common::comments::Comment;
1920
use deno_ast::MultiThreadedComments;
20-
2121
use jni::objects::{GlobalRef, JMethodID, JObject};
2222
use jni::JNIEnv;
2323

2424
use crate::jni_utils::*;
2525
use crate::span_utils::{ByteToIndexMap, ToJavaWithMap};
2626

2727
impl ToJavaWithMap<ByteToIndexMap> for Comment {
28-
fn to_java_with_map<'local, 'a>(&self, env: &mut JNIEnv<'local>, map: &'_ ByteToIndexMap) -> JObject<'a>
28+
fn to_java_with_map<'local, 'a>(&self, env: &mut JNIEnv<'local>, map: &'_ ByteToIndexMap) -> Result<JObject<'a>>
2929
where
3030
'local: 'a,
3131
{
3232
let text = &self.text;
33-
let java_kind = self.kind.to_java(env);
34-
let java_span = map.get_span_ex_by_span(&self.span).to_java(env);
35-
let return_value = unsafe { JAVA_COMMENT.as_ref().unwrap() }.construct(env, text, &java_kind, &java_span);
33+
let java_kind = self.kind.to_java(env)?;
34+
let java_span = map.get_span_ex_by_span(&self.span).to_java(env)?;
35+
let return_value = unsafe { JAVA_COMMENT.as_ref().unwrap() }
36+
.construct(env, text, &java_kind, &java_span)
37+
.map_err(Error::msg);
3638
delete_local_ref!(env, java_kind);
3739
delete_local_ref!(env, java_span);
3840
return_value
3941
}
4042
}
4143

4244
impl ToJavaWithMap<ByteToIndexMap> for MultiThreadedComments {
43-
fn to_java_with_map<'local, 'a>(&self, env: &mut JNIEnv<'local>, map: &'_ ByteToIndexMap) -> JObject<'a>
45+
fn to_java_with_map<'local, 'a>(&self, env: &mut JNIEnv<'local>, map: &'_ ByteToIndexMap) -> Result<JObject<'a>>
4446
where
4547
'local: 'a,
4648
{
4749
let leading = self.leading_map();
4850
let trailing = self.trailing_map();
49-
let java_leading = map_new(env, leading.len());
50-
leading.iter().for_each(|(key, value)| {
51-
let key_span_ex = map.get_span_ex_by_byte_pos(&key);
52-
let java_position = integer_value_of(env, key_span_ex.start as i32);
53-
let java_comments = list_new(env, value.len());
54-
value.iter().for_each(|comment| {
55-
let java_comment = comment.to_java_with_map(env, map);
56-
list_add(env, &java_comments, &java_comment);
51+
let java_leading = map_new(env, leading.len())?;
52+
for (position, comments) in leading.iter() {
53+
let key_span_ex = map.get_span_ex_by_byte_pos(&position);
54+
let java_position = integer_value_of(env, key_span_ex.start as i32)?;
55+
let java_comments = list_new(env, comments.len())?;
56+
for comment in comments.iter() {
57+
let java_comment = comment.to_java_with_map(env, map)?;
58+
list_add(env, &java_comments, &java_comment)?;
5759
delete_local_ref!(env, java_comment);
58-
});
59-
let java_return_value = map_put(env, &java_leading, &java_position, &java_comments);
60+
};
61+
let java_return_value = map_put(env, &java_leading, &java_position, &java_comments)?;
6062
delete_local_ref!(env, java_position);
6163
delete_local_ref!(env, java_comments);
6264
delete_local_ref!(env, java_return_value);
63-
});
64-
let java_trailing = map_new(env, trailing.len());
65-
trailing.iter().for_each(|(key, value)| {
66-
let key_span_ex = map.get_span_ex_by_byte_pos(&key);
67-
let java_position = integer_value_of(env, key_span_ex.start as i32);
68-
let java_comments = list_new(env, value.len());
69-
value.iter().for_each(|comment| {
70-
let java_comment = comment.to_java_with_map(env, map);
71-
list_add(env, &java_comments, &java_comment);
65+
};
66+
let java_trailing = map_new(env, trailing.len())?;
67+
for (position, comments) in trailing.iter() {
68+
let key_span_ex = map.get_span_ex_by_byte_pos(&position);
69+
let java_position = integer_value_of(env, key_span_ex.start as i32)?;
70+
let java_comments = list_new(env, comments.len())?;
71+
for comment in comments.iter() {
72+
let java_comment = comment.to_java_with_map(env, map)?;
73+
list_add(env, &java_comments, &java_comment)?;
7274
delete_local_ref!(env, java_comment);
73-
});
74-
let java_return_value = map_put(env, &java_trailing, &java_position, &java_comments);
75+
};
76+
let java_return_value = map_put(env, &java_trailing, &java_position, &java_comments)?;
7577
delete_local_ref!(env, java_position);
7678
delete_local_ref!(env, java_comments);
7779
delete_local_ref!(env, java_return_value);
78-
});
80+
};
7981
let return_value = unsafe { JAVA_COMMENTS.as_ref().unwrap() }.construct(env, &java_leading, &java_trailing);
8082
delete_local_ref!(env, java_leading);
8183
delete_local_ref!(env, java_trailing);
@@ -120,7 +122,7 @@ impl JavaSwc4jComment {
120122
text: &str,
121123
kind: &JObject<'_>,
122124
span: &JObject<'_>,
123-
) -> JObject<'a>
125+
) -> Result<JObject<'a>>
124126
where
125127
'local: 'a,
126128
{
@@ -134,9 +136,9 @@ impl JavaSwc4jComment {
134136
self.method_construct,
135137
&[text, kind, span],
136138
"Swc4jComment construct()"
137-
);
139+
)?;
138140
delete_local_ref!(env, java_text);
139-
return_value
141+
Ok(return_value)
140142
}
141143
}
142144
/* JavaSwc4jComment End */
@@ -177,7 +179,7 @@ impl JavaSwc4jComments {
177179
env: &mut JNIEnv<'local>,
178180
leading: &JObject<'_>,
179181
trailing: &JObject<'_>,
180-
) -> JObject<'a>
182+
) -> Result<JObject<'a>>
181183
where
182184
'local: 'a,
183185
{
@@ -189,8 +191,8 @@ impl JavaSwc4jComments {
189191
self.method_construct,
190192
&[leading, trailing],
191193
"Swc4jComments construct()"
192-
);
193-
return_value
194+
)?;
195+
Ok(return_value)
194196
}
195197
}
196198
/* JavaSwc4jComments End */
@@ -209,7 +211,7 @@ pub fn comments_new<'local, 'a>(
209211
env: &mut JNIEnv<'local>,
210212
comments: &MultiThreadedComments,
211213
map: &ByteToIndexMap,
212-
) -> JObject<'a>
214+
) -> Result<JObject<'a>>
213215
where
214216
'local: 'a,
215217
{

rust/src/enums.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
use anyhow::Result;
1819
use jni::objects::{GlobalRef, JMethodID, JObject, JStaticMethodID};
1920
use jni::JNIEnv;
2021

@@ -68,7 +69,7 @@ macro_rules! declare_identifiable_enum {
6869
}
6970
}
7071

71-
pub fn parse<'local, 'a>(&self, env: &mut JNIEnv<'local>, id: i32) -> JObject<'a>
72+
pub fn parse<'local, 'a>(&self, env: &mut JNIEnv<'local>, id: i32) -> Result<JObject<'a>>
7273
where
7374
'local: 'a,
7475
{
@@ -80,24 +81,25 @@ macro_rules! declare_identifiable_enum {
8081
static mut $static_name: Option<$struct_name> = None;
8182

8283
impl<'local> FromJava<'local> for $enum_name {
83-
fn from_java(env: &mut JNIEnv<'local>, obj: &JObject<'_>) -> $enum_name {
84+
fn from_java(env: &mut JNIEnv<'local>, obj: &JObject<'_>) -> Result<Box<$enum_name>> {
8485
let id = call_as_int!(
8586
env,
8687
obj.as_ref(),
8788
$static_name.as_ref().unwrap().method_get_id,
8889
&[],
8990
"getId()"
90-
);
91-
$enum_name::parse_by_id(id)
91+
)?;
92+
Ok(Box::new($enum_name::parse_by_id(id)))
9293
}
9394
}
9495

9596
impl ToJava for $enum_name {
96-
fn to_java<'local, 'a>(&self, env: &mut JNIEnv<'local>) -> JObject<'a>
97+
fn to_java<'local, 'a>(&self, env: &mut JNIEnv<'local>) -> Result<JObject<'a>>
9798
where
9899
'local: 'a,
99100
{
100-
unsafe { $static_name.as_ref().unwrap() }.parse(env, self.get_id())
101+
let id = self.get_id();
102+
unsafe { $static_name.as_ref().unwrap() }.parse(env, id)
101103
}
102104
}
103105
};

rust/src/error.rs

+44-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
*/
1717

1818
use jni::objects::{GlobalRef, JStaticMethodID, JThrowable};
19+
use jni::sys::jobject;
1920
use jni::JNIEnv;
21+
use std::ptr::null_mut;
2022

2123
use crate::jni_utils::*;
2224

2325
struct JavaCoreException {
2426
pub class: GlobalRef,
2527
pub method_parse_error: JStaticMethodID,
28+
pub method_transform_error: JStaticMethodID,
2629
pub method_transpile_error: JStaticMethodID,
2730
}
2831
unsafe impl Send for JavaCoreException {}
@@ -43,6 +46,13 @@ impl JavaCoreException {
4346
"(Ljava/lang/String;)Lcom/caoccao/javet/swc4j/exceptions/Swc4jCoreException;",
4447
)
4548
.expect("Couldn't find static method Swc4jCoreException.parseError");
49+
let method_transform_error = env
50+
.get_static_method_id(
51+
&class,
52+
"transformError",
53+
"(Ljava/lang/String;)Lcom/caoccao/javet/swc4j/exceptions/Swc4jCoreException;",
54+
)
55+
.expect("Couldn't find static method Swc4jCoreException.transformError");
4656
let method_transpile_error = env
4757
.get_static_method_id(
4858
&class,
@@ -53,14 +63,31 @@ impl JavaCoreException {
5363
JavaCoreException {
5464
class,
5565
method_parse_error,
66+
method_transform_error,
5667
method_transpile_error,
5768
}
5869
}
5970

6071
pub fn throw_parse_error<'local, 'a>(&self, env: &mut JNIEnv<'local>, message: &'a str) {
6172
let java_message = string_to_jstring!(env, message);
6273
let message = object_to_jvalue!(java_message);
63-
let exception = call_static_as_object!(env, &self.class, &self.method_parse_error, &[message], "parseError()");
74+
let exception = call_static_as_object!(env, &self.class, &self.method_parse_error, &[message], "parseError()")
75+
.expect("Couldn't call static method Swc4jCoreException.parseError()");
76+
let exception = unsafe { JThrowable::from_raw(exception.as_raw()) };
77+
let _ = env.throw(exception);
78+
}
79+
80+
pub fn throw_transform_error<'local, 'a>(&self, env: &mut JNIEnv<'local>, message: &'a str) {
81+
let java_message = string_to_jstring!(env, message);
82+
let message = object_to_jvalue!(java_message);
83+
let exception = call_static_as_object!(
84+
env,
85+
&self.class,
86+
&self.method_transform_error,
87+
&[message],
88+
"transformError()"
89+
)
90+
.expect("Couldn't call static method Swc4jCoreException.transformError()");
6491
let exception = unsafe { JThrowable::from_raw(exception.as_raw()) };
6592
let _ = env.throw(exception);
6693
}
@@ -74,7 +101,8 @@ impl JavaCoreException {
74101
&self.method_transpile_error,
75102
&[message],
76103
"transpileError()"
77-
);
104+
)
105+
.expect("Couldn't call static method Swc4jCoreException.transpileError()");
78106
let exception = unsafe { JThrowable::from_raw(exception.as_raw()) };
79107
let _ = env.throw(exception);
80108
}
@@ -88,17 +116,29 @@ pub fn init<'local>(env: &mut JNIEnv<'local>) {
88116
}
89117
}
90118

91-
pub fn throw_parse_error<'local, 'a>(env: &mut JNIEnv<'local>, message: &'a str) {
119+
pub fn throw_parse_error<'local, 'a>(env: &mut JNIEnv<'local>, message: &'a str) -> jobject {
92120
unsafe {
93121
JAVA_CORE_EXCEPTION.as_ref().unwrap().throw_parse_error(env, message);
94122
}
123+
null_mut()
124+
}
125+
126+
pub fn throw_transform_error<'local, 'a>(env: &mut JNIEnv<'local>, message: &'a str) -> jobject {
127+
unsafe {
128+
JAVA_CORE_EXCEPTION
129+
.as_ref()
130+
.unwrap()
131+
.throw_transform_error(env, message);
132+
}
133+
null_mut()
95134
}
96135

97-
pub fn throw_transpile_error<'local, 'a>(env: &mut JNIEnv<'local>, message: &'a str) {
136+
pub fn throw_transpile_error<'local, 'a>(env: &mut JNIEnv<'local>, message: &'a str) -> jobject {
98137
unsafe {
99138
JAVA_CORE_EXCEPTION
100139
.as_ref()
101140
.unwrap()
102141
.throw_transpile_error(env, message);
103142
}
143+
null_mut()
104144
}

0 commit comments

Comments
 (0)