Skip to content

Commit

Permalink
🦄 refactor: Add more delete_local_ref
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Mar 22, 2024
1 parent 3a8efeb commit 6a74383
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
52 changes: 40 additions & 12 deletions rust/src/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,16 @@ impl JavaAstTokenFactory {
'local: 'a,
{
let java_ast_token_type = unsafe { JAVA_AST_TOKEN_TYPE.as_ref().unwrap() };
let ast_token_type = java_ast_token_type.parse(env, ast_token_type.get_id());
let java_ast_token_type = java_ast_token_type.parse(env, ast_token_type.get_id());
let ast_token_type = jvalue {
l: java_ast_token_type.as_raw(),
};
let start_position = jvalue { i: range.start as i32 };
let end_position = jvalue { i: range.end as i32 };
let line_break_ahead = jvalue {
z: line_break_ahead as u8,
};
unsafe {
let token = unsafe {
env
.call_static_method_unchecked(
&self.class,
Expand All @@ -172,7 +175,11 @@ impl JavaAstTokenFactory {
.expect("Couldn't create Swc4jAstTokenAssignOperator")
.l()
.expect("Couldn't convert Swc4jAstTokenAssignOperator")
}
};
env
.delete_local_ref(java_ast_token_type)
.expect("Couldn't delete local ast token type");
token
}

pub fn create_binary_operator<'local, 'a>(
Expand All @@ -186,13 +193,16 @@ impl JavaAstTokenFactory {
'local: 'a,
{
let java_ast_token_type = unsafe { JAVA_AST_TOKEN_TYPE.as_ref().unwrap() };
let ast_token_type = java_ast_token_type.parse(env, ast_token_type.get_id());
let java_ast_token_type = java_ast_token_type.parse(env, ast_token_type.get_id());
let ast_token_type = jvalue {
l: java_ast_token_type.as_raw(),
};
let start_position = jvalue { i: range.start as i32 };
let end_position = jvalue { i: range.end as i32 };
let line_break_ahead = jvalue {
z: line_break_ahead as u8,
};
unsafe {
let token = unsafe {
env
.call_static_method_unchecked(
&self.class,
Expand All @@ -203,7 +213,11 @@ impl JavaAstTokenFactory {
.expect("Couldn't create Swc4jAstTokenBinaryOperator")
.l()
.expect("Couldn't convert Swc4jAstTokenBinaryOperator")
}
};
env
.delete_local_ref(java_ast_token_type)
.expect("Couldn't delete local ast token type");
token
}

pub fn create_false<'local, 'a>(
Expand Down Expand Up @@ -245,13 +259,16 @@ impl JavaAstTokenFactory {
'local: 'a,
{
let java_ast_token_type = unsafe { JAVA_AST_TOKEN_TYPE.as_ref().unwrap() };
let ast_token_type = java_ast_token_type.parse(env, ast_token_type.get_id());
let java_ast_token_type = java_ast_token_type.parse(env, ast_token_type.get_id());
let ast_token_type = jvalue {
l: java_ast_token_type.as_raw(),
};
let start_position = jvalue { i: range.start as i32 };
let end_position = jvalue { i: range.end as i32 };
let line_break_ahead = jvalue {
z: line_break_ahead as u8,
};
unsafe {
let token = unsafe {
env
.call_static_method_unchecked(
&self.class,
Expand All @@ -262,7 +279,11 @@ impl JavaAstTokenFactory {
.expect("Couldn't create Swc4jAstTokenGenericOperator")
.l()
.expect("Couldn't convert Swc4jAstTokenGenericOperator")
}
};
env
.delete_local_ref(java_ast_token_type)
.expect("Couldn't delete local ast token type");
token
}

pub fn create_ident_known<'local, 'a>(
Expand Down Expand Up @@ -311,13 +332,16 @@ impl JavaAstTokenFactory {
'local: 'a,
{
let java_ast_token_type = unsafe { JAVA_AST_TOKEN_TYPE.as_ref().unwrap() };
let ast_token_type = java_ast_token_type.parse(env, ast_token_type.get_id());
let java_ast_token_type = java_ast_token_type.parse(env, ast_token_type.get_id());
let ast_token_type = jvalue {
l: java_ast_token_type.as_raw(),
};
let start_position = jvalue { i: range.start as i32 };
let end_position = jvalue { i: range.end as i32 };
let line_break_ahead = jvalue {
z: line_break_ahead as u8,
};
unsafe {
let token = unsafe {
env
.call_static_method_unchecked(
&self.class,
Expand All @@ -328,7 +352,11 @@ impl JavaAstTokenFactory {
.expect("Couldn't create Swc4jAstTokenKeyword")
.l()
.expect("Couldn't convert Swc4jAstTokenKeyword")
}
};
env
.delete_local_ref(java_ast_token_type)
.expect("Couldn't delete local ast token type");
token
}

pub fn create_null<'local, 'a>(
Expand Down
10 changes: 7 additions & 3 deletions rust/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ impl AstTokenType {
Token::PlusPlus => AstTokenType::PlusPlus,
Token::MinusMinus => AstTokenType::MinusMinus,
Token::Tilde => AstTokenType::Tilde,
_ => AstTokenType::Unknown,
_ => panic!("Unexpected token {:?}", token),
}
}

Expand Down Expand Up @@ -507,13 +507,17 @@ impl JavaAstTokenType {
AstTokenType::parse_by_id(id)
}

pub fn parse<'local>(&self, env: &mut JNIEnv<'local>, id: i32) -> jvalue {
pub fn parse<'local, 'a>(&self, env: &mut JNIEnv<'local>, id: i32) -> JObject<'a>
where
'local: 'a,
{
let id = jvalue { i: id };
unsafe {
env
.call_static_method_unchecked(&self.class, self.method_parse, ReturnType::Object, &[id])
.expect("Object is expected")
.as_jni()
.l()
.expect("Couldn't convert to JObject")
}
}
}
Expand Down

0 comments on commit 6a74383

Please sign in to comment.