Skip to content

Commit

Permalink
🦄 refactor: Rename jni construct
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Mar 26, 2024
1 parent 76d363e commit 1d320fe
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
6 changes: 3 additions & 3 deletions rust/src/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ pub mod program {
let declare = var_decl.declare;
let kind = var_decl.kind;
let range = byte_to_index_map.get_range_by_span(&var_decl.span());
let java_decls = java_array_list.create(env, var_decl.decls.len());
let java_decls = java_array_list.construct(env, var_decl.decls.len());
var_decl.decls.iter().for_each(|var_declarator| {
let java_var_declarator = create_var_declarator(env, byte_to_index_map, var_declarator);
java_array_list.add(env, &java_decls, &java_var_declarator);
Expand Down Expand Up @@ -309,7 +309,7 @@ pub mod program {
'local: 'a,
{
let java_array_list = unsafe { JAVA_ARRAY_LIST.as_ref().unwrap() };
let java_body = java_array_list.create(env, body.len());
let java_body = java_array_list.construct(env, body.len());
java_body
}

Expand Down Expand Up @@ -382,7 +382,7 @@ pub mod program {
'local: 'a,
{
let java_array_list = unsafe { JAVA_ARRAY_LIST.as_ref().unwrap() };
let java_body = java_array_list.create(env, body.len());
let java_body = java_array_list.construct(env, body.len());
body.into_iter().for_each(|stmt| {
let java_stmt = create_stmt(env, byte_to_index_map, stmt);
java_array_list.add(env, &java_body, &java_stmt);
Expand Down
12 changes: 6 additions & 6 deletions rust/src/jni_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub trait ToJniType {
pub struct JavaArrayList {
#[allow(dead_code)]
class: GlobalRef,
method_constructor: JMethodID,
method_construct: JMethodID,
method_add: JMethodID,
}
unsafe impl Send for JavaArrayList {}
Expand All @@ -43,20 +43,20 @@ impl JavaArrayList {
.find_class("java/util/ArrayList")
.expect("Couldn't find class ArrayList");
let class = env.new_global_ref(class).expect("Couldn't globalize class ArrayList");
let method_constructor = env
let method_construct = env
.get_method_id(&class, "<init>", "(I)V")
.expect("Couldn't find method ArrayList::new");
let method_add = env
.get_method_id(&class, "add", "(Ljava/lang/Object;)Z")
.expect("Couldn't find method ArrayList.add");
JavaArrayList {
class,
method_constructor,
method_construct,
method_add,
}
}

pub fn create<'local, 'a>(&self, env: &mut JNIEnv<'local>, initial_capacity: usize) -> JObject<'a>
pub fn construct<'local, 'a>(&self, env: &mut JNIEnv<'local>, initial_capacity: usize) -> JObject<'a>
where
'local: 'a,
{
Expand All @@ -65,8 +65,8 @@ impl JavaArrayList {
};
unsafe {
env
.new_object_unchecked(&self.class, self.method_constructor, &[initial_capacity])
.expect("Couldn't create ArrayList")
.new_object_unchecked(&self.class, self.method_construct, &[initial_capacity])
.expect("Couldn't construct ArrayList")
}
}

Expand Down
28 changes: 14 additions & 14 deletions rust/src/outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use crate::token_utils;

struct JavaParseOutput {
class: GlobalRef,
method_constructor: JMethodID,
method_construct: JMethodID,
}
unsafe impl Send for JavaParseOutput {}
unsafe impl Sync for JavaParseOutput {}
Expand All @@ -49,7 +49,7 @@ impl JavaParseOutput {
let class = env
.new_global_ref(class)
.expect("Couldn't globalize class Swc4jParseOutput");
let method_constructor = env
let method_construct = env
.get_method_id(
&class,
"<init>",
Expand All @@ -58,11 +58,11 @@ impl JavaParseOutput {
.expect("Couldn't find method Swc4jParseOutput::new");
JavaParseOutput {
class,
method_constructor,
method_construct,
}
}

pub fn create<'local, 'a>(&self, env: &mut JNIEnv<'local>, parse_output: &ParseOutput) -> JObject<'a>
pub fn construct<'local, 'a>(&self, env: &mut JNIEnv<'local>, parse_output: &ParseOutput) -> JObject<'a>
where
'local: 'a,
{
Expand Down Expand Up @@ -90,17 +90,17 @@ impl JavaParseOutput {
env
.new_object_unchecked(
&self.class,
self.method_constructor,
self.method_construct,
&[program, media_type, module, script, source_text, tokens],
)
.expect("Couldn't create Swc4jParseOutput")
.expect("Couldn't construct Swc4jParseOutput")
}
}
}

struct JavaTranspileOutput {
class: GlobalRef,
method_constructor: JMethodID,
method_construct: JMethodID,
}
unsafe impl Send for JavaTranspileOutput {}
unsafe impl Sync for JavaTranspileOutput {}
Expand All @@ -113,7 +113,7 @@ impl JavaTranspileOutput {
let class = env
.new_global_ref(class)
.expect("Couldn't globalize class Swc4jTranspileOutput");
let method_constructor = env
let method_construct = env
.get_method_id(
&class,
"<init>",
Expand All @@ -122,11 +122,11 @@ impl JavaTranspileOutput {
.expect("Couldn't find method Swc4jTranspileOutput::new");
JavaTranspileOutput {
class,
method_constructor,
method_construct,
}
}

pub fn create<'local, 'a>(&self, env: &mut JNIEnv<'local>, transpile_output: &TranspileOutput) -> JObject<'a>
pub fn construct<'local, 'a>(&self, env: &mut JNIEnv<'local>, transpile_output: &TranspileOutput) -> JObject<'a>
where
'local: 'a,
{
Expand Down Expand Up @@ -163,7 +163,7 @@ impl JavaTranspileOutput {
env
.new_object_unchecked(
&self.class,
self.method_constructor,
self.method_construct,
&[
program,
code,
Expand All @@ -175,7 +175,7 @@ impl JavaTranspileOutput {
tokens,
],
)
.expect("Couldn't create Swc4jTranspileOutput")
.expect("Couldn't construct Swc4jTranspileOutput")
}
}
}
Expand Down Expand Up @@ -262,7 +262,7 @@ impl ToJniType for ParseOutput {
where
'local: 'a,
{
unsafe { JAVA_PARSE_OUTPUT.as_ref().unwrap() }.create(env, &self)
unsafe { JAVA_PARSE_OUTPUT.as_ref().unwrap() }.construct(env, &self)
}
}

Expand Down Expand Up @@ -316,6 +316,6 @@ impl ToJniType for TranspileOutput {
where
'local: 'a,
{
unsafe { JAVA_TRANSPILE_OUTPUT.as_ref().unwrap() }.create(env, &self)
unsafe { JAVA_TRANSPILE_OUTPUT.as_ref().unwrap() }.construct(env, &self)
}
}
2 changes: 1 addition & 1 deletion rust/src/token_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ pub fn token_and_spans_to_java_list<'local>(
Some(token_and_spans) => {
let java_array_list = unsafe { JAVA_ARRAY_LIST.as_ref().unwrap() };
let java_token_factory = unsafe { JAVA_TOKEN_FACTORY.as_ref().unwrap() };
let list = java_array_list.create(env, token_and_spans.len());
let list = java_array_list.construct(env, token_and_spans.len());
token_and_spans.iter().for_each(|token_and_span| {
let line_break_ahead = token_and_span.had_line_break;
let text = &source_text[Range {
Expand Down

0 comments on commit 1d320fe

Please sign in to comment.