Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Aug 18, 2023
2 parents f40f85e + e2f4ed2 commit 2f6a5ee
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 14 deletions.
2 changes: 1 addition & 1 deletion eo-runtime/src/main/eo/org/eolang/rust.eo
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
+rt jvm org.eolang:eo-runtime:0.0.0
+version 0.0.0

[code params] > rust /int
[code params] > rust /?
12 changes: 11 additions & 1 deletion eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.eolang.AtComposite;
import org.eolang.AtFree;
import org.eolang.Data;
import org.eolang.Dataized;
import org.eolang.ExFailure;
import org.eolang.PhDefault;
import org.eolang.Phi;
Expand Down Expand Up @@ -144,8 +145,17 @@ public EOrust(final Phi sigma) {
byte[].class
);
}
final Phi portal = new Dataized(
rho
.attr("params").get()
.attr("Δ").get()
).take(Phi[].class)[0];
return EOrust.translate(
(byte[]) method.invoke(null, new Universe(rho))
(byte[]) method.invoke(
null, new Universe(
portal
)
)
);
}
)
Expand Down
27 changes: 18 additions & 9 deletions eo-runtime/src/main/rust/eo_env/src/eo_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

pub mod eo_enum;
use jni::JNIEnv;
use jni::objects::{JClass, JObject, JValue};
use jni::objects::{JClass, JObject, JValue, JByteArray};

pub struct EOEnv<'local> {
pub java_env: JNIEnv<'local>,
Expand Down Expand Up @@ -98,13 +98,22 @@ impl<'local> EOEnv<'_> {
}
}

/*
* @todo #2237:60min Implement "dataize" method. It must
* call java dataizing method and get byte array from it.
* Then it have to return byte array as a result of dataization.
*/
#[allow(unused_variables)]
pub fn dataize(&mut self, v: u32) -> Option<&[u32]> {
Some(&[0x0])
pub fn dataize(&mut self, v: u32) -> Option<Vec<i8>> {
let java_array = JByteArray::from(
self.java_env
.call_method(
&self.java_obj,
"dataize",
"(I)[B",
&[
JValue::Int(v as i32),
]
).unwrap().l().unwrap());
let size = self.java_env.get_array_length(&java_array).unwrap();
let mut bytes = vec![0; size.try_into().unwrap()];
if self.java_env.get_byte_array_region(&java_array, 0, &mut bytes[0..]).is_err() {
return None;
}
return Some(bytes);
}
}
49 changes: 46 additions & 3 deletions eo-runtime/src/test/eo/org/eolang/rust-tests.eo
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
}
"""
*
[]
assert-that > @
r
$.equal-to
Expand All @@ -57,6 +58,7 @@
}
"""
*
[]
assert-that > @
r
$.equal-to
Expand All @@ -74,6 +76,7 @@
}
"""
*
[]
assert-that > @
r
$.equal-to
Expand All @@ -91,6 +94,7 @@
}
"""
*
[]
assert-that > @
r
$.equal-to
Expand All @@ -109,6 +113,7 @@
}
"""
*
[]
assert-that > @
r
$.not
Expand All @@ -127,6 +132,7 @@
}
"""
*
[]
assert-that > @
r
$.not
Expand All @@ -149,6 +155,7 @@
}
"""
*
[]
assert-that > @
insert
$.not
Expand All @@ -169,6 +176,7 @@
}
"""
*
[]
assert-that > @
copy
$.not
Expand All @@ -183,14 +191,49 @@
use eo_env::eo_enum::EO;
use eo_env::eo_enum::EO::{EOInt};
pub fn foo(env: &mut EOEnv) -> EO {
let v = env.find("^.a") as u32;
let _bytes = env.dataize(v).unwrap();
EOInt(v as i64)
let v = env.find("^.a") as u32;
let _bytes = env.dataize(v).unwrap();
EOInt(v as i64)
}
"""
*
[]
3
assert-that > @
dataized
$.not
$.less-than
0

[] > rust-plus
5 > a
10 > b
QQ.rust > plus
"""
use eo_env::EOEnv;
use eo_env::eo_enum::EO;
use eo_env::eo_enum::EO::{EOInt};
use byteorder::{BigEndian, ReadBytesExt};

pub fn foo(env: &mut EOEnv) -> EO {
let a = env.find("^.a") as u32;
let b = env.find("^.b") as u32;
let bytes_a = env.dataize(a).unwrap();
let mut arr_a = unsafe { &*(&bytes_a[0..] as *const _ as *const [u8]) };

let bytes_b = env.dataize(b).unwrap();
let mut arr_b = unsafe { &*(&bytes_b[0..] as *const _ as *const [u8]) };

let a = arr_a.read_i64::<BigEndian>().unwrap();
let b = arr_b.read_i64::<BigEndian>().unwrap();
println!("sum 5 + 10 = {}", a + b);

EOInt(a + b)
}
"""
*
[]
"byteorder:1.4.3"
assert-that > @
plus
$.equal-to 15

1 comment on commit 2f6a5ee

@0pdd
Copy link

@0pdd 0pdd commented on 2f6a5ee Aug 18, 2023

Choose a reason for hiding this comment

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

Puzzle 2237-4d097292 disappeared from eo-runtime/src/main/rust/eo_env/src/eo_env.rs), that's why I closed #2292. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.