Skip to content

Commit 80a3923

Browse files
committed
✨ feat: Add null, number, bigint to ast
1 parent f4046b5 commit 80a3923

16 files changed

+627
-3
lines changed

rust/Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ crate_type = ["cdylib", "rlib"]
1212
debug_print = "1.0.0"
1313
deno_ast = { version = "1.0.1", features = ["module_specifier", "transpiling", "transforms"] }
1414
jni = "0.21.1"
15+
num-bigint = "0.4.4"

rust/src/ast_utils.rs

+163-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use std::ptr::null_mut;
3030
struct JavaSwc4jAstFactory {
3131
#[allow(dead_code)]
3232
class: GlobalRef,
33+
method_create_big_int: JStaticMethodID,
3334
method_create_binding_ident: JStaticMethodID,
3435
method_create_block_stmt: JStaticMethodID,
3536
method_create_bool: JStaticMethodID,
@@ -38,6 +39,8 @@ struct JavaSwc4jAstFactory {
3839
method_create_expr_stmt: JStaticMethodID,
3940
method_create_ident: JStaticMethodID,
4041
method_create_module: JStaticMethodID,
42+
method_create_null: JStaticMethodID,
43+
method_create_number: JStaticMethodID,
4144
method_create_script: JStaticMethodID,
4245
method_create_str: JStaticMethodID,
4346
method_create_var_decl: JStaticMethodID,
@@ -54,6 +57,13 @@ impl JavaSwc4jAstFactory {
5457
let class = env
5558
.new_global_ref(class)
5659
.expect("Couldn't globalize class Swc4jAstFactory");
60+
let method_create_big_int = env
61+
.get_static_method_id(
62+
&class,
63+
"createBigInt",
64+
"(ILjava/lang/String;II)Lcom/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstBigInt;",
65+
)
66+
.expect("Couldn't find method Swc4jAstFactory.createBigInt");
5767
let method_create_binding_ident = env
5868
.get_static_method_id(
5969
&class,
@@ -110,6 +120,20 @@ impl JavaSwc4jAstFactory {
110120
"(Ljava/util/List;Ljava/lang/String;II)Lcom/caoccao/javet/swc4j/ast/program/Swc4jAstModule;",
111121
)
112122
.expect("Couldn't find method Swc4jAstFactory.createModule");
123+
let method_create_null = env
124+
.get_static_method_id(
125+
&class,
126+
"createNull",
127+
"(II)Lcom/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstNull;",
128+
)
129+
.expect("Couldn't find method Swc4jAstFactory.createNull");
130+
let method_create_number = env
131+
.get_static_method_id(
132+
&class,
133+
"createNumber",
134+
"(DLjava/lang/String;II)Lcom/caoccao/javet/swc4j/ast/expr/lit/Swc4jAstNumber;",
135+
)
136+
.expect("Couldn't find method Swc4jAstFactory.createNumber");
113137
let method_create_script = env
114138
.get_static_method_id(
115139
&class,
@@ -140,6 +164,7 @@ impl JavaSwc4jAstFactory {
140164
.expect("Couldn't find method Swc4jAstFactory.createVarDeclarator");
141165
JavaSwc4jAstFactory {
142166
class,
167+
method_create_big_int,
143168
method_create_binding_ident,
144169
method_create_block_stmt,
145170
method_create_bool,
@@ -148,13 +173,53 @@ impl JavaSwc4jAstFactory {
148173
method_create_expr_stmt,
149174
method_create_ident,
150175
method_create_module,
176+
method_create_null,
177+
method_create_number,
151178
method_create_script,
152179
method_create_str,
153180
method_create_var_decl,
154181
method_create_var_declarator,
155182
}
156183
}
157184

185+
pub fn create_big_int<'local, 'a>(
186+
&self,
187+
env: &mut JNIEnv<'local>,
188+
sign: i32,
189+
raw: &Option<String>,
190+
range: &Range<usize>,
191+
) -> JObject<'a>
192+
where
193+
'local: 'a,
194+
{
195+
let sign = jvalue {
196+
i: sign as i32,
197+
};
198+
let java_raw = match &raw {
199+
Some(raw) => converter::string_to_jstring(env, &raw),
200+
None => Default::default(),
201+
};
202+
let raw = jvalue {
203+
l: java_raw.as_raw(),
204+
};
205+
let start_position = jvalue { i: range.start as i32 };
206+
let end_position = jvalue { i: range.end as i32 };
207+
let return_value = unsafe {
208+
env
209+
.call_static_method_unchecked(
210+
&self.class,
211+
self.method_create_big_int,
212+
ReturnType::Object,
213+
&[sign, raw, start_position, end_position],
214+
)
215+
.expect("Couldn't create Swc4jAstBigInt by create_big_int()")
216+
.l()
217+
.expect("Couldn't convert Swc4jAstBigInt by create_big_int()")
218+
};
219+
delete_local_ref!(env, java_raw);
220+
return_value
221+
}
222+
158223
pub fn create_binding_ident<'local, 'a>(
159224
&self,
160225
env: &mut JNIEnv<'local>,
@@ -393,6 +458,69 @@ impl JavaSwc4jAstFactory {
393458
return_value
394459
}
395460

461+
pub fn create_null<'local, 'a>(
462+
&self,
463+
env: &mut JNIEnv<'local>,
464+
range: &Range<usize>,
465+
) -> JObject<'a>
466+
where
467+
'local: 'a,
468+
{
469+
let start_position = jvalue { i: range.start as i32 };
470+
let end_position = jvalue { i: range.end as i32 };
471+
let return_value = unsafe {
472+
env
473+
.call_static_method_unchecked(
474+
&self.class,
475+
self.method_create_null,
476+
ReturnType::Object,
477+
&[start_position, end_position],
478+
)
479+
.expect("Couldn't create Swc4jAstNull by create_null()")
480+
.l()
481+
.expect("Couldn't convert Swc4jAstNull by create_null()")
482+
};
483+
return_value
484+
}
485+
486+
pub fn create_number<'local, 'a>(
487+
&self,
488+
env: &mut JNIEnv<'local>,
489+
value: f64,
490+
raw: &Option<String>,
491+
range: &Range<usize>,
492+
) -> JObject<'a>
493+
where
494+
'local: 'a,
495+
{
496+
let value = jvalue {
497+
d: value as f64,
498+
};
499+
let java_raw = match &raw {
500+
Some(raw) => converter::string_to_jstring(env, &raw),
501+
None => Default::default(),
502+
};
503+
let raw = jvalue {
504+
l: java_raw.as_raw(),
505+
};
506+
let start_position = jvalue { i: range.start as i32 };
507+
let end_position = jvalue { i: range.end as i32 };
508+
let return_value = unsafe {
509+
env
510+
.call_static_method_unchecked(
511+
&self.class,
512+
self.method_create_number,
513+
ReturnType::Object,
514+
&[value, raw, start_position, end_position],
515+
)
516+
.expect("Couldn't create Swc4jAstNumber by create_number()")
517+
.l()
518+
.expect("Couldn't convert Swc4jAstNumber by create_number()")
519+
};
520+
delete_local_ref!(env, java_raw);
521+
return_value
522+
}
523+
396524
pub fn create_script<'local, 'a>(
397525
&self,
398526
env: &mut JNIEnv<'local>,
@@ -2115,6 +2243,17 @@ pub mod program {
21152243

21162244
use deno_ast::swc::ast::*;
21172245

2246+
fn create_big_int<'local, 'a>(env: &mut JNIEnv<'local>, map: &ByteToIndexMap, node: &BigInt) -> JObject<'a>
2247+
where
2248+
'local: 'a,
2249+
{
2250+
let java_ast_factory = unsafe { JAVA_AST_FACTORY.as_ref().unwrap() };
2251+
let range = map.get_range_by_span(&node.span);
2252+
let sign = node.value.sign().get_id();
2253+
let raw = node.raw.as_ref().map(|node| node.as_str().to_owned());
2254+
java_ast_factory.create_big_int(env, sign, &raw, &range)
2255+
}
2256+
21182257
fn create_binding_ident<'local, 'a>(
21192258
env: &mut JNIEnv<'local>,
21202259
map: &ByteToIndexMap,
@@ -2236,6 +2375,9 @@ pub mod program {
22362375
match node {
22372376
Lit::Str(node) => create_str(env, map, node),
22382377
Lit::Bool(node) => create_bool(env, map, node),
2378+
Lit::Null(node) => create_null(env, map, node),
2379+
Lit::Num(node) => create_number(env, map, node),
2380+
Lit::BigInt(node) => create_big_int(env, map, node),
22392381
_ => Default::default(),
22402382
// TODO
22412383
}
@@ -2270,6 +2412,26 @@ pub mod program {
22702412
}
22712413
}
22722414

2415+
fn create_null<'local, 'a>(env: &mut JNIEnv<'local>, map: &ByteToIndexMap, node: &Null) -> JObject<'a>
2416+
where
2417+
'local: 'a,
2418+
{
2419+
let java_ast_factory = unsafe { JAVA_AST_FACTORY.as_ref().unwrap() };
2420+
let range = map.get_range_by_span(&node.span);
2421+
java_ast_factory.create_null(env, &range)
2422+
}
2423+
2424+
fn create_number<'local, 'a>(env: &mut JNIEnv<'local>, map: &ByteToIndexMap, node: &Number) -> JObject<'a>
2425+
where
2426+
'local: 'a,
2427+
{
2428+
let java_ast_factory = unsafe { JAVA_AST_FACTORY.as_ref().unwrap() };
2429+
let range = map.get_range_by_span(&node.span);
2430+
let value = node.value;
2431+
let raw = node.raw.as_ref().map(|node| node.as_str().to_owned());
2432+
java_ast_factory.create_number(env, value, &raw, &range)
2433+
}
2434+
22732435
fn create_pat<'local, 'a>(env: &mut JNIEnv<'local>, map: &ByteToIndexMap, node: &Pat) -> JObject<'a>
22742436
where
22752437
'local: 'a,
@@ -2338,7 +2500,7 @@ pub mod program {
23382500
{
23392501
let java_ast_factory = unsafe { JAVA_AST_FACTORY.as_ref().unwrap() };
23402502
let range = map.get_range_by_span(&node.span);
2341-
let value = &node.value.as_str();
2503+
let value = node.value.as_str();
23422504
let raw = node.raw.as_ref().map(|node| node.as_str().to_owned());
23432505
java_ast_factory.create_str(env, value, &raw, &range)
23442506
}

rust/src/enums.rs

+18
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use jni::objects::{GlobalRef, JMethodID, JObject, JStaticMethodID};
1919
use jni::sys::jvalue;
2020
use jni::JNIEnv;
2121

22+
use num_bigint::Sign;
2223
use deno_ast::swc::ast::{AssignOp, VarDeclKind};
2324
use deno_ast::swc::parser::token::{BinOpToken, Keyword, Token};
2425
pub use deno_ast::{ImportsNotUsedAsValues, MediaType};
@@ -738,6 +739,23 @@ impl JavaParseMode {
738739
}
739740
}
740741

742+
impl IdentifiableEnum<Sign> for Sign {
743+
fn get_id(&self) -> i32 {
744+
match self {
745+
Sign::NoSign => 0,
746+
Sign::Minus => 1,
747+
Sign::Plus => 2,
748+
}
749+
}
750+
fn parse_by_id(id: i32) -> Sign {
751+
match id {
752+
1 => Sign::Minus,
753+
2 => Sign::Plus,
754+
_ => Sign::NoSign,
755+
}
756+
}
757+
}
758+
741759
impl IdentifiableEnum<VarDeclKind> for VarDeclKind {
742760
fn get_id(&self) -> i32 {
743761
match self {

src/main/java/com/caoccao/javet/swc4j/ast/Swc4jAstFactory.java

+26-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
package com.caoccao.javet.swc4j.ast;
1818

1919
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstIdent;
20-
import com.caoccao.javet.swc4j.ast.expr.lit.Swc4jAstBool;
21-
import com.caoccao.javet.swc4j.ast.expr.lit.Swc4jAstStr;
20+
import com.caoccao.javet.swc4j.ast.expr.lit.*;
2221
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstExpr;
2322
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstModuleItem;
2423
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstPat;
@@ -42,6 +41,15 @@ public final class Swc4jAstFactory {
4241
private Swc4jAstFactory() {
4342
}
4443

44+
@Jni2RustMethod
45+
public static Swc4jAstBigInt createBigInt(
46+
int sign,
47+
@Jni2RustParam(optional = true) String raw,
48+
@Jni2RustParamStartPosition int startPosition,
49+
@Jni2RustParamEndPosition int endPosition) {
50+
return new Swc4jAstBigInt(Swc4jAstBigIntSign.parse(sign), raw, startPosition, endPosition);
51+
}
52+
4553
@Jni2RustMethod
4654
public static Swc4jAstBindingIdent createBindingIdent(
4755
Swc4jAstIdent id,
@@ -107,6 +115,22 @@ public static Swc4jAstModule createModule(
107115
return new Swc4jAstModule(body, shebang, startPosition, endPosition);
108116
}
109117

118+
@Jni2RustMethod
119+
public static Swc4jAstNull createNull(
120+
@Jni2RustParamStartPosition int startPosition,
121+
@Jni2RustParamEndPosition int endPosition) {
122+
return new Swc4jAstNull(startPosition, endPosition);
123+
}
124+
125+
@Jni2RustMethod
126+
public static Swc4jAstNumber createNumber(
127+
double value,
128+
@Jni2RustParam(optional = true) String raw,
129+
@Jni2RustParamStartPosition int startPosition,
130+
@Jni2RustParamEndPosition int endPosition) {
131+
return new Swc4jAstNumber(value, raw, startPosition, endPosition);
132+
}
133+
110134
@Jni2RustMethod
111135
public static Swc4jAstScript createScript(
112136
List<ISwc4jAstStmt> body,

0 commit comments

Comments
 (0)