Skip to content

Commit

Permalink
objectionary#2032: Generate natives
Browse files Browse the repository at this point in the history
  • Loading branch information
levBagryansky committed Jun 16, 2023
1 parent 0cf8b5a commit 0593938
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.eolang.maven.footprint.FtDefault;
import org.eolang.maven.rust_project.Names;
import org.eolang.maven.rust_project.Native;
import org.eolang.maven.rust_project.Project;
import org.eolang.maven.tojos.ForeignTojo;
import org.eolang.maven.util.Home;
Expand Down Expand Up @@ -141,6 +143,11 @@ public void exec() throws IOException {
code,
dependencies
);
new Native(function, "EOrust.natives").save(
new FtDefault(
generatedDir.toPath().resolve("EOrust").resolve("natives")
)
);
}
}
project.save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void save(final Footprint footprint) throws IOException {
*/
private String transform() {
final String signature = String.format(
"#[no_mangle]%spub extern \"system\" fn Java_EOrust_EOnatives_%s_%s(_env: JNIEnv, _class: JClass,) -> jint {",
"#[no_mangle]%spub extern \"system\" fn Java_EOrust_natives_%s_%s(_env: JNIEnv, _class: JClass,) -> jint {",
System.lineSeparator(),
this.name,
this.name
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import java.nio.file.Path

/**
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 Objectionary.com
Expand All @@ -23,15 +21,41 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.maven.rust_project;

/**
* Entry point for running validation scripts.
* To add new validation create new script in this folder and add it
* to the list below.
*/
Path tests = basedir.toPath().resolve("src").resolve("test").resolve("groovy");
for (it in ['check-folders-numbering.groovy', 'check-all-java-classes-compiled.groovy']) {
def res = evaluate tests.resolve(it).toFile()
println String.format('Verified with %s - OK. Result: %s', it, res)
import org.eolang.maven.footprint.Footprint;

import java.io.IOException;

public class Native {
private final String name;
private final String pack;

public Native(String name, String pack) {
this.name = name;
this.pack = pack;
}

public void save(final Footprint footprint) throws IOException {
footprint.save(
name,
"java",
() -> String.join(
System.lineSeparator(),
String.format(
"package %s;",
pack
),
String.format(
"public class %s {",
name
),
String.format(
"public static native int %s ();",
name
),
"}"
)
);
}
}
true
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void transformsCorrectly(@TempDir final Path temp) throws Exception {
"use jni::sys::{jint};",
"use jni::JNIEnv;",
"#[no_mangle]",
"pub extern \"system\" fn Java_EOrust_EOnatives_simple_simple(_env: JNIEnv, _class: JClass,) -> jint {"
"pub extern \"system\" fn Java_EOrust_natives_simple_simple(_env: JNIEnv, _class: JClass,) -> jint {"
)
);
}
Expand Down
20 changes: 12 additions & 8 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,13 @@
*/
package EOorg.EOeolang;

import EOrust.EOnatives.xhomextardis3xeoxeoxruntimextargetxeoxtest0;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.nio.file.Path;
import java.lang.reflect.Method;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang3.NotImplementedException;
import org.apache.commons.lang3.SystemUtils;
import org.cactoos.text.TextOf;
Expand Down Expand Up @@ -113,9 +108,18 @@ public EOrust(final Phi sigma) {
this,
rho ->
{
System.out.println();
final String name = names.get(
rho.attr("code").get().locator().split(":")[0]
);
final Class Native = Class.forName(
String.format(
"EOrust.natives.%s",
name
)
);
final Method method = Native.getDeclaredMethod(name, null);
return new Data.ToPhi(
(long) xhomextardis3xeoxeoxruntimextargetxeoxtest0.xhomextardis3xeoxeoxruntimextargetxeoxtest0()
Long.valueOf((int) method.invoke(null))
);
}
)
Expand Down

0 comments on commit 0593938

Please sign in to comment.