Skip to content

Commit

Permalink
✨ feat: Complete code gen for ast factory
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Mar 26, 2024
1 parent 9c15c39 commit 6454780
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
14 changes: 7 additions & 7 deletions rust/src/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,14 @@ impl JavaSwc4jAstFactory {
.delete_local_ref(java_shebang)
.expect("Couldn't delete local shebang");
return_value
}
/* JavaSwc4jAstFactory End */
}

pub fn create_script<'local, 'a>(
&self,
env: &mut JNIEnv<'local>,
body: &JObject<'_>,
shebang: Option<String>,
range: Range<usize>,
shebang: &Option<String>,
range: &Range<usize>,
) -> JObject<'a>
where
'local: 'a,
Expand All @@ -122,7 +121,7 @@ impl JavaSwc4jAstFactory {
};
let start_position = jvalue { i: range.start as i32 };
let end_position = jvalue { i: range.end as i32 };
let ast = unsafe {
let return_value = unsafe {
env
.call_static_method_unchecked(
&self.class,
Expand All @@ -137,9 +136,10 @@ impl JavaSwc4jAstFactory {
env
.delete_local_ref(java_shebang)
.expect("Couldn't delete local shebang");
ast
return_value
}
}
/* JavaSwc4jAstFactory End */

static mut JAVA_AST_FACTORY: Option<JavaSwc4jAstFactory> = None;

Expand Down Expand Up @@ -368,7 +368,7 @@ pub mod program {
let shebang: Option<String> = script.shebang.to_owned().map(|s| s.to_string());
let range = byte_to_index_map.get_range_by_span(&script.span());
let body = create_script_body(env, byte_to_index_map, &script.body);
let java_script = java_ast_factory.create_script(env, &body, shebang, range);
let java_script = java_ast_factory.create_script(env, &body, &shebang, &range);
env.delete_local_ref(body).expect("Couldn't delete local script body");
java_script
}
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/caoccao/javet/swc4j/ast/Swc4jAstFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

import com.caoccao.javet.swc4j.ast.program.Swc4jAstModule;
import com.caoccao.javet.swc4j.ast.program.Swc4jAstScript;
import com.caoccao.javet.swc4j.jni2rust.*;
import com.caoccao.javet.swc4j.jni2rust.Jni2RustClass;
import com.caoccao.javet.swc4j.jni2rust.Jni2RustMethod;
import com.caoccao.javet.swc4j.jni2rust.Jni2RustMethodMode;
import com.caoccao.javet.swc4j.jni2rust.Jni2RustParam;

import java.util.List;

Expand Down Expand Up @@ -61,9 +64,12 @@ public static Swc4jAstModule createModule(
* @return the ast script
* @since 0.2.0
*/
@Jni2RustMethod(mode = Jni2RustMethodMode.Manual)
@Jni2RustMethod
public static Swc4jAstScript createScript(
List<Swc4jAst> body, String shebang, int startPosition, int endPosition) {
List<Swc4jAst> body,
@Jni2RustParam(optional = true) String shebang,
@Jni2RustParam(rustType = "range: &Range<usize>", preCall = "jvalue { i: range.start as i32 }") int startPosition,
@Jni2RustParam(rustType = "range: &Range<usize>", preCall = "jvalue { i: range.end as i32 }") int endPosition) {
return new Swc4jAstScript(body, shebang, startPosition, endPosition);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ protected void getCodeMethods(List<String> lines) {
}
// return
lines.add(" return_value");
lines.add(" }");
});
}

Expand Down

0 comments on commit 6454780

Please sign in to comment.