Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jul 25, 2023
2 parents 7864f50 + 89612a4 commit 44fe52e
Show file tree
Hide file tree
Showing 14 changed files with 260 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void save(final Footprint footprint) throws IOException {
this.name
),
String.format(
" public static native int %s",
" public static native byte[] %s",
this.name
),
" (final EOrust eo);",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ public PrimeModule(final String method, final String file) {
"mod foo;",
"use foo::foo;",
"use jni::JNIEnv;",
"use jni::objects::{JClass, JObject};",
"use jni::sys::{jint};",
"use jni::objects::{JByteArray, JClass, JObject};",
"use eo_env::EOEnv;",
"#[no_mangle]",
"pub extern \"system\" fn",
String.format("Java_EOrust_natives_%s_%s", method, method),
"<'local> (env: JNIEnv<'local>, _class: JClass<'local>, universe: JObject<'local>) -> jint",
"{ return foo(EOEnv::new(env, _class, universe)); }"
"<'local> (env: JNIEnv<'local>, _class: JClass<'local>, universe: JObject<'local>) -> JByteArray<'local>",
"{ let mut eo_env = EOEnv::new(env, _class, universe); ",
"let arr = foo(&mut eo_env).eo2vec();",
"eo_env.java_env.byte_array_from_slice(&arr.as_slice()).unwrap() }"
),
file
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package org.eolang.maven;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import org.cactoos.text.TextOf;
import org.eolang.jucs.ClasspathSource;
Expand Down Expand Up @@ -71,11 +70,11 @@ void parsesSimpleEoProgram(@TempDir final Path temp) throws Exception {
new TextOf(res.get(rust)).asString(),
Matchers.stringContainsInOrder(
"use rand::Rng;",
"pub fn foo(mut env: EOEnv<'_>) -> i32 {",
"pub fn foo(_env: &EOEnv) -> EO {",
" let mut rng = rand::thread_rng();",
" print!(\"Hello world\");",
" let i = rng.gen::<i32>();",
" i",
" let i = rng.gen::<i64>();",
" EOInt(i)",
"}"
)
);
Expand All @@ -88,13 +87,13 @@ void parsesSimpleEoProgram(@TempDir final Path temp) throws Exception {
)
)
).asString(),
Matchers.containsString("public static native int")
Matchers.containsString("public static native byte[]")
);
}

@Test
void binarizesTwiceRustProgram(@TempDir final Path temp) throws Exception {
final Path src = Paths.get("src/test/resources/org/eolang/maven/binarize/twice-rust.eo");
final Path src = BinarizeMojoTest.SRC.resolve("twice-rust.eo");
final FakeMaven maven;
synchronized (BinarizeParseMojoTest.class) {
maven = new FakeMaven(temp).withProgram(src);
Expand All @@ -119,14 +118,18 @@ void binarizesTwiceRustProgram(@TempDir final Path temp) throws Exception {
MatcherAssert.assertThat(
new TextOf(res.get(one)).asString(),
Matchers.stringContainsInOrder(
"pub fn foo(mut _env: EOEnv<'_>) -> i32 {",
"use eo_env::eo_enum::EO;",
"use eo_env::eo_enum::EO::{EOInt};",
"pub fn foo(_env: &EOEnv) -> EO {",
"println!(\"{}\", x);"
)
);
MatcherAssert.assertThat(
new TextOf(res.get(two)).asString(),
Matchers.stringContainsInOrder(
"pub fn foo(mut _env: EOEnv<'_>) -> i32 {",
"use eo_env::eo_enum::EO;",
"use eo_env::eo_enum::EO::{EOInt};",
"pub fn foo(_env: &EOEnv) -> EO {",
"print!(\"Hello 大 2\");"
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public <T extends AbstractMojo> FakeMaven execute(final Class<T> mojo) throws IO
this.params.putIfAbsent("objectionary", new Objectionary.Fake());
this.params.putIfAbsent(
"eoEnvDir",
new File("src/test/resources/org/eolang/maven/binarize/eo_env")
new File("../eo-runtime/src/main/rust/eo_env")
);
}
final Moja<T> moja = new Moja<>(mojo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void savesCorrectly(@TempDir final Path temp) throws Exception {
"package mypackage;",
"import EOorg.EOeolang.EOrust;",
"public class name {",
" public static native int name",
" public static native byte[] name",
" (final EOrust eo);",
"}"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void savesCorrectly(@TempDir final Path temp) throws Exception {
).asString(),
Matchers.stringContainsInOrder(
String.format("Java_EOrust_natives_%s_%s", method, method),
"<'local> (env: JNIEnv<'local>, _class: JClass<'local>, universe: JObject<'local>) -> jint"
"<'local> (env: JNIEnv<'local>, _class: JClass<'local>, universe: JObject<'local>) -> JByteArray"
)
);
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
"""
use rand::Rng;
use eo_env::EOEnv;
use eo_env::eo_enum::EO;
use eo_env::eo_enum::EO::{EOInt};

pub fn foo(mut env: EOEnv<'_>) -> i32 {
pub fn foo(_env: &EOEnv) -> EO {
let mut rng = rand::thread_rng();
print!("Hello world");
let i = rng.gen::<i32>();
i
let i = rng.gen::<i64>();
EOInt(i)
}
"""
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
QQ.rust > r
"""
use eo_env::EOEnv;
use eo_env::eo_enum::EO;
use eo_env::eo_enum::EO::{EOInt};

pub fn foo(mut _env: EOEnv<'_>) -> i32 {
pub fn foo(_env: &EOEnv) -> EO {
let x = rand::random::<i32>();
println!("{}", x);
x
EOInt(x as i64)
}
"""
*
Expand All @@ -20,11 +22,13 @@
QQ.rust > r
"""
use eo_env::EOEnv;
use eo_env::eo_enum::EO;
use eo_env::eo_enum::EO::{EOInt};

pub fn foo(mut _env: EOEnv<'_>) -> i32 {
pub fn foo(_env: &EOEnv) -> EO {
print!("Hello 大 2");
let x = rand::random::<i32>();
x
EOInt(x as i64)
}
"""
*
Expand Down
60 changes: 57 additions & 3 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.commons.lang3.SystemUtils;
Expand All @@ -56,6 +58,10 @@
* @checkstyle MethodNameCheck (100 lines)
* @checkstyle LineLengthCheck (100 lines)
* @checkstyle TypeNameCheck (5 lines)
* @todo #2283:90min Create Universe class. Now its functionality is
* assigned to "EORust", which is why it is overcomplicated. "Universe"
* should perform a model of interaction with "eo" objects through
* methods "find", "put", "copy", "dataize" and "bind".
*/
@XmirObject(oname = "rust")
public class EOrust extends PhDefault {
Expand Down Expand Up @@ -128,9 +134,17 @@ public EOrust(final Phi sigma) {
"EOrust.natives.%s",
name
)
).getDeclaredMethod(name, new Class[]{EOrust.class});
return new Data.ToPhi(
Long.valueOf((int) method.invoke(null, this))
).getDeclaredMethod(name, EOrust.class);
if (method.getReturnType() != byte[].class) {
throw new ExFailure(
"Return type of %s is %s, required %s",
method,
method.getReturnType(),
byte[].class
);
}
return EOrust.translate(
(byte[]) method.invoke(null, this)
);
}
)
Expand Down Expand Up @@ -228,4 +242,44 @@ private static ConcurrentHashMap<String, String> load(final String src) throws I
);
}
}

/**
* Translates byte message from rust side to Phi object.
* @param message Message that native method returns.
* @return Phi object.
* @todo #2283:45min Implement handling of vertex returning.
* It must convert message array from 1 to last byte to the int
* and return eo object with corresponding vertex then.
* @todo #2283:45min Implement handling of String returning.
* It must convert message array from 1 to last byte to the String
* and return eo object with converted String Data.
*/
private static Phi translate(final byte[] message) {
final byte determinant = message[0];
final byte[] content = Arrays.copyOfRange(message, 1, message.length);
final Phi ret;
switch (determinant) {
case 0:
throw new ExFailure(
"Returning vertex is not implemented yet"
);
case 1:
ByteBuffer buffer = ByteBuffer.allocate(Double.BYTES);
buffer.put(content);
buffer.flip();
ret = new Data.ToPhi(buffer.getDouble());
break;
case 2:
buffer = ByteBuffer.allocate(Long.BYTES);
buffer.put(content);
buffer.flip();
ret = new Data.ToPhi(buffer.getLong());
break;
default:
throw new ExFailure(
"Returning Strings and raw bytes is not implemented yet"
);
}
return ret;
}
}
58 changes: 58 additions & 0 deletions eo-runtime/src/main/rust/eo_env/src/eo_enum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

pub enum EO {
EOVertex(u32),
EOFloat(f64),
EOInt(i64),
EOString(String),
EORaw(Box<[u8]>),
}

impl EO {
pub fn eo2vec(&self) -> Vec<u8> {
match self {
EO::EOVertex(v) => {
let mut res: Vec<u8> = vec![0; 1 + 4];
res[0] = 0;
res[1..].copy_from_slice(&v.to_le_bytes());
res
}
EO::EOFloat(x) => {
let mut res = vec![0; 1 + 8];
res[0] = 1;
res[1..].copy_from_slice(&x.to_be_bytes());
res
}
EO::EOInt(x) => {
let mut res: Vec<u8> = vec![0; 1 + 8];
res[0] = 2;
res[1..].copy_from_slice(&x.to_be_bytes());
res
}
EO::EOString(_) => { vec![0xff] }
EO::EORaw(_) => { vec![0xff] }
}
}
}
Loading

3 comments on commit 44fe52e

@0pdd
Copy link

@0pdd 0pdd commented on 44fe52e Jul 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 2283-0094b893 discovered in eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java) and submitted as #2313. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on 44fe52e Jul 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 2283-27003227 discovered in eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java) and submitted as #2314. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on 44fe52e Jul 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 2283-ddadafec discovered in eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java) and submitted as #2315. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.