Skip to content

Commit

Permalink
🐞 fix: Fix span for binding ident
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed May 12, 2024
1 parent 8bea69a commit 66b7236
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 41 deletions.
72 changes: 42 additions & 30 deletions rust/src/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::jni_utils::*;
use crate::span_utils::{ByteToIndexMap, RegisterWithMap, ToJavaWithMap};

use deno_ast::swc::ast::*;
use deno_ast::swc::common::{Spanned, DUMMY_SP};
use deno_ast::swc::common::{Span, Spanned, DUMMY_SP};
use num_bigint::{BigInt as BigIntValue, BigUint, Sign};

use std::ptr::null_mut;
Expand Down Expand Up @@ -70,23 +70,52 @@ impl<'local> FromJava<'local> for BigInt {
};
let data: BigUint = optional_raw
.as_ref()
.map_or_else(|| Default::default(), |raw| {
let mut raw = raw.to_owned();
while raw.ends_with("n") {
raw.truncate(raw.len() - 1);
}
BigUint::parse_bytes(&raw.as_bytes(), 10)
})
.map_or_else(
|| Default::default(),
|raw| {
let mut raw = raw.to_owned();
while raw.ends_with("n") {
raw.truncate(raw.len() - 1);
}
BigUint::parse_bytes(&raw.as_bytes(), 10)
},
)
.unwrap_or_else(|| Default::default());
delete_local_ref!(env, java_optional_raw);
let value = BigIntValue::from_biguint(sign, data);
let value = Box::new(value);
let raw = optional_raw.map(|raw| raw.into());
BigInt {
span,
value,
raw,
}
BigInt { span, value, raw }
}
}

impl ToJavaWithMap<ByteToIndexMap> for BindingIdent {
fn to_java_with_map<'local, 'a>(&self, env: &mut JNIEnv<'local>, map: &'_ ByteToIndexMap) -> JObject<'a>
where
'local: 'a,
{
let span = if let Some(type_ann) = self.type_ann.as_ref() {

This comment has been minimized.

Copy link
@caoccao

caoccao May 12, 2024

Author Owner

Detail of this hack is in this issue.

Span {
lo: self.id.span.lo,
hi: type_ann.as_ref().span.hi,
ctxt: self.id.span.ctxt,
}
} else {
self.span()
};
let java_span_ex = map.get_span_ex_by_span(&span).to_java(env);
let java_id = self.id.to_java_with_map(env, map);
let java_optional_type_ann = self.type_ann.as_ref().map(|node| node.to_java_with_map(env, map));
let return_value = unsafe { JAVA_CLASS_BINDING_IDENT.as_ref().unwrap() }.construct(
env,
&java_id,
&java_optional_type_ann,
&java_span_ex,
);
delete_local_optional_ref!(env, java_optional_type_ann);
delete_local_ref!(env, java_id);
delete_local_ref!(env, java_span_ex);
return_value
}
}

Expand Down Expand Up @@ -26619,23 +26648,6 @@ impl RegisterWithMap<ByteToIndexMap> for BindingIdent {
}
}

impl ToJavaWithMap<ByteToIndexMap> for BindingIdent {
fn to_java_with_map<'local, 'a>(&self, env: &mut JNIEnv<'local>, map: &'_ ByteToIndexMap) -> JObject<'a>
where
'local: 'a,
{
let java_span_ex = map.get_span_ex_by_span(&self.span()).to_java(env);
let java_id = self.id.to_java_with_map(env, map);
let java_optional_type_ann = self.type_ann.as_ref().map(|node| node.to_java_with_map(env, map));
let return_value = unsafe { JAVA_CLASS_BINDING_IDENT.as_ref().unwrap() }
.construct(env, &java_id, &java_optional_type_ann, &java_span_ex);
delete_local_optional_ref!(env, java_optional_type_ann);
delete_local_ref!(env, java_id);
delete_local_ref!(env, java_span_ex);
return_value
}
}

impl<'local> FromJava<'local> for BindingIdent {
#[allow(unused_variables)]
fn from_java(env: &mut JNIEnv<'local>, jobj: &JObject<'_>) -> Self {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import java.util.List;
import java.util.Optional;

@Jni2RustClass(filePath = Jni2RustFilePath.AstUtils, customCreation = true)
@Jni2RustClass(filePath = Jni2RustFilePath.AstUtils, customFromJava = true, customToJava = true)
public class Swc4jAstBigInt
extends Swc4jAst
implements ISwc4jAstLit, ISwc4jAstPropName, ISwc4jAstTsLit {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.util.List;
import java.util.Optional;

@Jni2RustClass(filePath = Jni2RustFilePath.AstUtils, span = false)
@Jni2RustClass(filePath = Jni2RustFilePath.AstUtils, span = false, customToJava = true)
public class Swc4jAstBindingIdent
extends Swc4jAst
implements ISwc4jAstPat, ISwc4jAstTsFnParam, ISwc4jAstTsParamPropParam, ISwc4jAstSimpleAssignTarget {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Jni2RustClass {
boolean customCreation() default false;
boolean customFromJava() default false;

boolean customToJava() default false;

Jni2RustFilePath filePath() default Jni2RustFilePath.None;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void testWithTsTypeAnn() throws Swc4jCoreException {
Swc4jAstVarDeclarator varDeclarator = assertAst(
varDecl, varDecl.getDecls().get(0), Swc4jAstType.VarDeclarator, 6, 15);
Swc4jAstBindingIdent bindingIdent = assertAst(
varDeclarator, varDeclarator.getName().as(Swc4jAstBindingIdent.class), Swc4jAstType.BindingIdent, 6, 7);
varDeclarator, varDeclarator.getName().as(Swc4jAstBindingIdent.class), Swc4jAstType.BindingIdent, 6, 15);
Swc4jAstIdent ident = assertAst(
bindingIdent, bindingIdent.getId(), Swc4jAstType.Ident, 6, 7);
assertEquals("a", ident.getSym());
Expand All @@ -72,7 +72,7 @@ public void testWithTsTypeAnnAndDefaultValue() throws Exception {
Swc4jAstAssignPat assignPat = assertAst(
param, param.getPat().as(Swc4jAstAssignPat.class), Swc4jAstType.AssignPat, 11, 24);
Swc4jAstBindingIdent bindingIdent = assertAst(
assignPat, assignPat.getLeft().as(Swc4jAstBindingIdent.class), Swc4jAstType.BindingIdent, 11, 12);
assignPat, assignPat.getLeft().as(Swc4jAstBindingIdent.class), Swc4jAstType.BindingIdent, 11, 20);
ident = assertAst(
bindingIdent, bindingIdent.getId(), Swc4jAstType.Ident, 11, 12);
assertEquals("a", ident.getSym());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ public String getRawName() {
return clazz.getSimpleName().substring(clazz.isInterface() ? 9 : 8);
}

public boolean isCustomCreation() {
return optionalJni2RustClass.map(Jni2RustClass::customCreation).orElse(false);
public boolean isCustomFromJava() {
return optionalJni2RustClass.map(Jni2RustClass::customFromJava).orElse(false);
}

public boolean isCustomToJava() {
return optionalJni2RustClass.map(Jni2RustClass::customToJava).orElse(false);
}

public boolean isIgnore() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.caoccao.javet.swc4j.outputs.Swc4jTransformOutput;
import com.caoccao.javet.swc4j.outputs.Swc4jTranspileOutput;
import com.caoccao.javet.swc4j.plugins.ISwc4jPluginHost;
import com.caoccao.javet.swc4j.plugins.Swc4jPluginHost;
import com.caoccao.javet.swc4j.span.Swc4jSpan;
import com.caoccao.javet.swc4j.tokens.Swc4jTokenFactory;
import com.caoccao.javet.swc4j.utils.*;
Expand Down Expand Up @@ -338,8 +337,7 @@ public void testStructRegistrationAndCreation() throws IOException {
lines.add(" }");
lines.add("}\n");
// AST
if (!jni2RustClassUtils.isCustomCreation()) {
// ToJava
if (!jni2RustClassUtils.isCustomToJava()) {
final List<String> args = new ArrayList<>();
final List<String> javaVars = new ArrayList<>();
final List<String> javaOptionalVars = new ArrayList<>();
Expand Down Expand Up @@ -498,7 +496,8 @@ public void testStructRegistrationAndCreation() throws IOException {
lines.add(" return_value");
lines.add(" }");
lines.add("}\n");
// FromJava
}
if (!jni2RustClassUtils.isCustomFromJava()) {
lines.add(String.format("impl<'local> FromJava<'local> for %s {", className));
lines.add(" #[allow(unused_variables)]");
lines.add(" fn from_java(env: &mut JNIEnv<'local>, jobj: &JObject<'_>) -> Self {");
Expand Down

0 comments on commit 66b7236

Please sign in to comment.