Skip to content

Commit

Permalink
🦄 refactor: Revise ast program
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Mar 25, 2024
1 parent 43e987b commit 11b3fca
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 201 deletions.
124 changes: 95 additions & 29 deletions rust/src/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,58 @@ pub fn init<'local>(env: &mut JNIEnv<'local>) {
}
}

pub mod span {
use crate::position_utils::ByteToIndexMap;
use deno_ast::swc::ast::*;

fn register_decl(byte_to_index_map: &mut ByteToIndexMap, decl: &Decl) {
match decl {
Decl::Var(var_decl) => register_var_decl(byte_to_index_map, var_decl.as_ref()),
_ => {}
};
}

fn register_module(byte_to_index_map: &mut ByteToIndexMap, module: &Module) {
byte_to_index_map.register_by_span(&module.span);
}

fn register_script(byte_to_index_map: &mut ByteToIndexMap, script: &Script) {
byte_to_index_map.register_by_span(&script.span);
script
.body
.iter()
.for_each(|stmt| register_stmt(byte_to_index_map, stmt))
}

fn register_stmt(byte_to_index_map: &mut ByteToIndexMap, stmt: &Stmt) {
match stmt {
Stmt::Decl(decl) => register_decl(byte_to_index_map, decl),
_ => {}
};
}

pub fn register_program(byte_to_index_map: &mut ByteToIndexMap, program: &Program) {
if program.is_module() {
register_module(byte_to_index_map, program.as_module().unwrap());
} else if program.is_script() {
register_script(byte_to_index_map, program.as_script().unwrap());
}
}

fn register_var_decl(byte_to_index_map: &mut ByteToIndexMap, var_decl: &VarDecl) {
byte_to_index_map.register_by_span(&var_decl.span);
var_decl
.decls
.iter()
.for_each(|var_declarator| register_var_declarator(byte_to_index_map, var_declarator));
}

fn register_var_declarator(byte_to_index_map: &mut ByteToIndexMap, var_declarator: &VarDeclarator) {
byte_to_index_map.register_by_span(&var_declarator.span);
// TODO
}
}

fn create_decl_var<'local, 'a>(
env: &mut JNIEnv<'local>,
byte_to_index_map: &ByteToIndexMap,
Expand Down Expand Up @@ -188,7 +240,7 @@ where
Default::default()
}

pub fn create_module<'local, 'a>(
pub fn create_program<'local, 'a>(
env: &mut JNIEnv<'local>,
byte_to_index_map: &ByteToIndexMap,
program: &Option<Arc<Program>>,
Expand All @@ -197,22 +249,44 @@ where
'local: 'a,
{
match program {
Some(arc_program) => match arc_program.as_module() {
Some(module) => {
let java_ast_factory = unsafe { JAVA_AST_FACTORY.as_ref().unwrap() };
let shebang: Option<String> = module.shebang.to_owned().map(|s| s.to_string());
let range = byte_to_index_map.get_range_by_span(&module.span());
let body = create_module_body(env, byte_to_index_map, &module.body);
let java_module = java_ast_factory.create_module(env, &body, shebang, range);
env.delete_local_ref(body).expect("Couldn't delete local module body");
java_module
Some(arc_program) => {
if arc_program.is_module() {
create_module(
env,
byte_to_index_map,
arc_program.as_module().expect("Couldn't get module"),
)
} else if arc_program.is_script() {
create_script(
env,
byte_to_index_map,
arc_program.as_script().expect("Couldn't get script"),
)
} else {
Default::default()
}
None => Default::default(),
},
}
None => Default::default(),
}
}

fn create_module<'local, 'a>(
env: &mut JNIEnv<'local>,
byte_to_index_map: &ByteToIndexMap,
module: &Module,
) -> JObject<'a>
where
'local: 'a,
{
let java_ast_factory = unsafe { JAVA_AST_FACTORY.as_ref().unwrap() };
let shebang: Option<String> = module.shebang.to_owned().map(|s| s.to_string());
let range = byte_to_index_map.get_range_by_span(&module.span());
let body = create_module_body(env, byte_to_index_map, &module.body);
let java_module = java_ast_factory.create_module(env, &body, shebang, range);
env.delete_local_ref(body).expect("Couldn't delete local module body");
java_module
}

fn create_module_body<'local, 'a>(
env: &mut JNIEnv<'local>,
byte_to_index_map: &ByteToIndexMap,
Expand Down Expand Up @@ -269,29 +343,21 @@ where
}
}

pub fn create_script<'local, 'a>(
fn create_script<'local, 'a>(
env: &mut JNIEnv<'local>,
byte_to_index_map: &ByteToIndexMap,
program: &Option<Arc<Program>>,
script: &Script,
) -> JObject<'a>
where
'local: 'a,
{
match program {
Some(arc_program) => match arc_program.as_script() {
Some(script) => {
let java_ast_factory = unsafe { JAVA_AST_FACTORY.as_ref().unwrap() };
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);
env.delete_local_ref(body).expect("Couldn't delete local script body");
java_script
}
None => Default::default(),
},
None => Default::default(),
}
let java_ast_factory = unsafe { JAVA_AST_FACTORY.as_ref().unwrap() };
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);
env.delete_local_ref(body).expect("Couldn't delete local script body");
java_script
}

fn create_script_body<'local, 'a>(
Expand Down
2 changes: 1 addition & 1 deletion rust/src/jni_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl JavaArrayList {
let class = env.new_global_ref(class).expect("Couldn't globalize class ArrayList");
let method_constructor = env
.get_method_id(&class, "<init>", "(I)V")
.expect("Couldn't find method ArrayList.ArrayList");
.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");
Expand Down
72 changes: 12 additions & 60 deletions rust/src/outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ use jni::JNIEnv;
use std::ptr::null_mut;
use std::sync::Arc;

use crate::token_utils;
use crate::ast_utils;
use crate::converter;
use crate::enums::*;
use crate::jni_utils::ToJniType;
use crate::options::*;
use crate::position_utils::ByteToIndexMap;
use crate::token_utils;

struct JavaParseOutput {
class: GlobalRef,
Expand All @@ -53,9 +53,9 @@ impl JavaParseOutput {
.get_method_id(
&class,
"<init>",
"(Lcom/caoccao/javet/swc4j/ast/program/Swc4jAstModule;Lcom/caoccao/javet/swc4j/ast/program/Swc4jAstScript;Lcom/caoccao/javet/swc4j/enums/Swc4jMediaType;ZZLjava/lang/String;Ljava/util/List;)V",
"(Lcom/caoccao/javet/swc4j/ast/program/Swc4jAstProgram;Lcom/caoccao/javet/swc4j/enums/Swc4jMediaType;ZZLjava/lang/String;Ljava/util/List;)V",
)
.expect("Couldn't find method Swc4jParseOutput.Swc4jParseOutput");
.expect("Couldn't find method Swc4jParseOutput::new");
JavaParseOutput {
class,
method_constructor,
Expand All @@ -67,10 +67,8 @@ impl JavaParseOutput {
'local: 'a,
{
let byte_to_index_map = parse_output.get_byte_to_index_map();
let ast_module = ast_utils::create_module(env, &byte_to_index_map, &parse_output.program);
let ast_script = ast_utils::create_script(env, &byte_to_index_map, &parse_output.program);
let ast_module = jvalue { l: ast_module.as_raw() };
let ast_script = jvalue { l: ast_script.as_raw() };
let program = ast_utils::create_program(env, &byte_to_index_map, &parse_output.program);
let program = jvalue { l: program.as_raw() };
let java_media_type = unsafe { JAVA_MEDIA_TYPE.as_ref().unwrap() };
let media_type = java_media_type.parse(env, parse_output.media_type.get_id());
let module = jvalue {
Expand All @@ -93,7 +91,7 @@ impl JavaParseOutput {
.new_object_unchecked(
&self.class,
self.method_constructor,
&[ast_module, ast_script, media_type, module, script, source_text, tokens],
&[program, media_type, module, script, source_text, tokens],
)
.expect("Couldn't create Swc4jParseOutput")
}
Expand All @@ -119,9 +117,9 @@ impl JavaTranspileOutput {
.get_method_id(
&class,
"<init>",
"(Lcom/caoccao/javet/swc4j/ast/program/Swc4jAstModule;Lcom/caoccao/javet/swc4j/ast/program/Swc4jAstScript;Ljava/lang/String;Lcom/caoccao/javet/swc4j/enums/Swc4jMediaType;ZZLjava/lang/String;Ljava/lang/String;Ljava/util/List;)V",
"(Lcom/caoccao/javet/swc4j/ast/program/Swc4jAstProgram;Ljava/lang/String;Lcom/caoccao/javet/swc4j/enums/Swc4jMediaType;ZZLjava/lang/String;Ljava/lang/String;Ljava/util/List;)V",
)
.expect("Couldn't find method Swc4jTranspileOutput.Swc4jTranspileOutput");
.expect("Couldn't find method Swc4jTranspileOutput::new");
JavaTranspileOutput {
class,
method_constructor,
Expand All @@ -133,10 +131,8 @@ impl JavaTranspileOutput {
'local: 'a,
{
let byte_to_index_map = transpile_output.parse_output.get_byte_to_index_map();
let ast_module = ast_utils::create_module(env, &byte_to_index_map, &transpile_output.parse_output.program);
let ast_script = ast_utils::create_script(env, &byte_to_index_map, &transpile_output.parse_output.program);
let ast_module = jvalue { l: ast_module.as_raw() };
let ast_script = jvalue { l: ast_script.as_raw() };
let program = ast_utils::create_program(env, &byte_to_index_map, &transpile_output.parse_output.program);
let program = jvalue { l: program.as_raw() };
let code = jvalue {
l: converter::string_to_jstring(env, &transpile_output.code).as_raw(),
};
Expand Down Expand Up @@ -169,8 +165,7 @@ impl JavaTranspileOutput {
&self.class,
self.method_constructor,
&[
ast_module,
ast_script,
program,
code,
media_type,
module,
Expand Down Expand Up @@ -236,11 +231,7 @@ impl ParseOutput {
let mut byte_to_index_map = ByteToIndexMap::new();
match &self.program {
Some(program) => {
if program.is_module() {
self.register_module(&mut byte_to_index_map, program.as_module().unwrap());
} else if program.is_script() {
self.register_script(&mut byte_to_index_map, program.as_script().unwrap());
}
ast_utils::span::register_program(&mut byte_to_index_map, program);
}
None => {}
}
Expand All @@ -264,45 +255,6 @@ impl ParseOutput {
byte_to_index_map.update(&utf8_byte_length, char_count);
byte_to_index_map
}

fn register_decl(&self, byte_to_index_map: &mut ByteToIndexMap, decl: &Decl) {
match decl {
Decl::Var(var_decl) => self.register_var_decl(byte_to_index_map, var_decl.as_ref()),
_ => {}
};
}

fn register_module(&self, byte_to_index_map: &mut ByteToIndexMap, module: &Module) {
byte_to_index_map.register_by_span(&module.span);
}

fn register_script(&self, byte_to_index_map: &mut ByteToIndexMap, script: &Script) {
byte_to_index_map.register_by_span(&script.span);
script
.body
.iter()
.for_each(|stmt| self.register_stmt(byte_to_index_map, stmt))
}

fn register_stmt(&self, byte_to_index_map: &mut ByteToIndexMap, stmt: &Stmt) {
match stmt {
Stmt::Decl(decl) => self.register_decl(byte_to_index_map, decl),
_ => {}
};
}

fn register_var_decl(&self, byte_to_index_map: &mut ByteToIndexMap, var_decl: &VarDecl) {
byte_to_index_map.register_by_span(&var_decl.span);
var_decl
.decls
.iter()
.for_each(|var_declarator| self.register_var_declarator(byte_to_index_map, var_declarator));
}

fn register_var_declarator(&self, byte_to_index_map: &mut ByteToIndexMap, var_declarator: &VarDeclarator) {
byte_to_index_map.register_by_span(&var_declarator.span);
// TODO
}
}

impl ToJniType for ParseOutput {
Expand Down
6 changes: 3 additions & 3 deletions rust/src/token_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -942,11 +942,11 @@ impl JavaAstTokenFactory {
}
}

pub static mut JAVA_token_FACTORY: Option<JavaAstTokenFactory> = None;
pub static mut JAVA_TOKEN_FACTORY: Option<JavaAstTokenFactory> = None;

pub fn init<'local>(env: &mut JNIEnv<'local>) {
unsafe {
JAVA_token_FACTORY = Some(JavaAstTokenFactory::new(env));
JAVA_TOKEN_FACTORY = Some(JavaAstTokenFactory::new(env));
}
}

Expand All @@ -960,7 +960,7 @@ pub fn token_and_spans_to_java_list<'local>(
l: match token_and_spans {
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 java_token_factory = unsafe { JAVA_TOKEN_FACTORY.as_ref().unwrap() };
let list = java_array_list.create(env, token_and_spans.len());
token_and_spans.iter().for_each(|token_and_span| {
let line_break_ahead = token_and_span.had_line_break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
import com.caoccao.javet.swc4j.enums.Swc4jAstType;

/**
* The type Base Swc4j ast text.
* The type Swc4j ast text.
*
* @since 0.2.0
*/
public abstract class BaseSwc4jAstText extends BaseSwc4jAst {
public abstract class Swc4JAstText extends Swc4jAst {
/**
* The Text.
*
Expand All @@ -32,15 +32,15 @@ public abstract class BaseSwc4jAstText extends BaseSwc4jAst {
protected final String text;

/**
* Instantiates a new Base Swc4j ast text.
* Instantiates a new Swc4j ast text.
*
* @param type the type
* @param text the text
* @param startPosition the start position
* @param endPosition the end position
* @since 0.2.0
*/
public BaseSwc4jAstText(Swc4jAstType type, String text, int startPosition, int endPosition) {
protected Swc4JAstText(Swc4jAstType type, String text, int startPosition, int endPosition) {
super(type, startPosition, endPosition);
this.text = text;
}
Expand Down
Loading

0 comments on commit 11b3fca

Please sign in to comment.