Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed May 8, 2024
2 parents a71be6d + 142cdf3 commit be4c314
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 20 deletions.
17 changes: 15 additions & 2 deletions eo-maven-plugin/src/main/java/org/eolang/maven/rust/Names.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ public String name(final String loc) {
return cached.computeIfAbsent(
loc,
key -> String.format(
"%s%d",
"%s%d_%s",
Names.PREFIX,
cached.size()
cached.size(),
Names.trim(key)
)
);
}
Expand Down Expand Up @@ -145,6 +146,18 @@ public void save() throws IOException {
);
}

/**
* Format loc: Remove non-ascii symbols, replace 桅.org.eolang with QQ
* and limit length of name characters.
* @param loc Locator of Rust insert.
* @return Formatted name.
*/
private static String trim(final String loc) {
final String replaced = loc.replace("桅.org.eolang", "QQ")
.replaceAll("[^a-zA-Z0-9.-]", "").replaceAll("[. -]", "_");
return replaced.substring(Math.max(replaced.length() - 64, 0));
}

/**
* Prestructor to initialize this.all.
* @param dest Directory where to load from.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public PrimeModule(final String method, final String file) {
"use eo::eo_enum::EO::EOError;",
"#[no_mangle]",
"pub extern \"system\" fn",
String.format("Java_EOrust_natives_%s_%s", method, method),
translate(method),
"<'local> (env: JNIEnv<'local>, _class: JClass<'local>, universe: JObject<'local>) -> JByteArray<'local>",
"{ let mut eo = Portal::new(env, _class, universe); ",
"let arr = foo(&mut eo)",
Expand All @@ -58,4 +58,35 @@ public PrimeModule(final String method, final String file) {
file
);
}

/**
* Translates ("mangles") Java method name to native method name.
* For example, translated "native0_custom" to
* Java_EOrust_natives_native0_1custom_native0_1custom.
* See {@link PrimeModule#translate(String, String)} for motivation.
* @param jname Name of this function (and class) in Java.
* @return How it named in Rust.
*/
private static String translate(final String jname) {
return PrimeModule.translate(
"EOrust.natives.".concat(jname),
jname
);
}

/**
* Translates ("mangles") Java method name to native method name according to
* <a href="https://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/design.html">documentation</a>
* or <a href="https://stackoverflow.com/questions/32470463/what-is-the-naming-convention-for-java-native-interface-method-and-module-name">stackoverflow</a>.
* @param clazz Simple name of class.
* @param method Name of method.
* @return How it named in Rust.
*/
private static String translate(final String clazz, final String method) {
return String.format(
"Java_%s_%s",
clazz.replace("_", "_1").replace(".", "_"),
method.replace("_", "_1")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,19 @@ void doesNotRecompile(@TempDir final Path temp) throws IOException {
}
maven.execute(new FakeMaven.Binarize());
final File executable = cache
.resolve("Lib/native0/target/debug/")
.resolve("Lib/native0_QQ_custom_creates_object_r_0/target/debug/")
.resolve(RustNode.LIB)
.toFile();
final long first = executable.lastModified();
maven.execute(new FakeMaven.Binarize());
final long second = executable.lastModified();
MatcherAssert.assertThat(
BinarizeParseTest.TO_ADD_MESSAGE,
"Check that file exists",
first,
Matchers.not(0L)
);
MatcherAssert.assertThat(
BinarizeParseTest.TO_ADD_MESSAGE,
"The cached program should not be recompiled",
second,
Matchers.equalTo(first)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ void parsesSimpleEoProgram(@TempDir final Path temp) throws Exception {
BinarizeMojoTest.SRC.resolve("simple-rust.eo")
);
final String function = String.format(
"%s0",
"%s0_QQ_custom_creates_object_r_0",
Names.PREFIX
);
final String rust = String.format(
"target/Lib/%s/src/foo.rs",
function
);
MatcherAssert.assertThat(
BinarizeParseTest.TO_ADD_MESSAGE,
"The program exists",
res, Matchers.hasKey(rust)
);
MatcherAssert.assertThat(
BinarizeParseTest.TO_ADD_MESSAGE,
"Correct content of Rust file",
new TextOf(res.get(rust)).asString(),
Matchers.stringContainsInOrder(
"use rand::Rng;",
Expand All @@ -90,7 +90,7 @@ void parsesSimpleEoProgram(@TempDir final Path temp) throws Exception {
)
);
MatcherAssert.assertThat(
BinarizeParseTest.TO_ADD_MESSAGE,
"Correct content of Java file",
new TextOf(
res.get(
String.format(
Expand All @@ -110,11 +110,11 @@ void binarizesTwiceRustProgram(@TempDir final Path temp) throws Exception {
BinarizeMojoTest.SRC.resolve("twice-rust.eo")
);
final String one = String.format(
"target/Lib/%s0/src/foo.rs",
"target/Lib/%s0_QQ_custom_hello_world_1_r_0/src/foo.rs",
Names.PREFIX
);
final String two = String.format(
"target/Lib/%s1/src/foo.rs",
"target/Lib/%s1_QQ_custom_hello_world_2_r_0/src/foo.rs",
Names.PREFIX
);
MatcherAssert.assertThat(
Expand Down Expand Up @@ -165,7 +165,7 @@ void createsCorrectRustProject(@TempDir final Path temp) throws Exception {
BinarizeMojoTest.SRC.resolve("twice-rust.eo")
);
final String dir = String.format(
"target/Lib/%s1/",
"target/Lib/%s1_QQ_custom_hello_world_2_r_0/",
Names.PREFIX
);
final String cargo = dir.concat("Cargo.toml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import org.cactoos.text.TextOf;
import org.eolang.maven.BinarizeParseTest;
import org.eolang.maven.footprint.FtDefault;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

/**
* Test case for {@link Module}.
* Test case for {@link PrimeModule}.
*
* @since 0.1
*/
Expand All @@ -45,12 +44,12 @@ void savesCorrectly(@TempDir final Path temp) throws Exception {
final String name = "name";
new PrimeModule(method, name).save(new FtDefault(temp));
MatcherAssert.assertThat(
BinarizeParseTest.TO_ADD_MESSAGE,
"Check that PrimeModule is saved correctly",
new TextOf(
temp.resolve(Paths.get(name.concat(".rs")))
).asString(),
Matchers.stringContainsInOrder(
String.format("Java_EOrust_natives_%s_%s", method, method),
"Java_EOrust_natives_my_1method_my_1method",
"<'local> (env: JNIEnv<'local>, _class: JClass<'local>, universe: JObject<'local>) -> JByteArray"
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ void generatesRust(@TempDir final Path temp) throws IOException {
"Check that necessary files are created",
out,
Matchers.hasItems(
lib.resolve("native0/src/foo.rs"),
lib.resolve("native0/src/lib.rs"),
gen.resolve("native0.java")
lib.resolve("native0_QQ_custom_rust_r_0/src/foo.rs"),
lib.resolve("native0_QQ_custom_rust_r_0/src/lib.rs"),
gen.resolve("native0_QQ_custom_rust_r_0.java")
)
);
}
Expand Down
18 changes: 18 additions & 0 deletions eo-runtime/src/test/eo/org/eolang/rust-tests.eo
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@
+package org.eolang
+version 0.0.0

# Works with long names correctly.
[] > rust-long-variable
QQ.rust > very-looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
"""
use eo::Portal;
use eo::eo_enum::EO;
use eo::eo_enum::EO::{EOInt};

pub fn foo(_portal: &mut Portal) -> Option<EO> {
Some(EOInt(0))
}
"""
[]
*
eq. > @
very-looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
0

# Test
[] > rust-returns-positive-int
QQ.rust > r
Expand Down

0 comments on commit be4c314

Please sign in to comment.