Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Sep 20, 2023
2 parents 19c8cf9 + be6c285 commit 7c8c223
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 21 deletions.
3 changes: 0 additions & 3 deletions eo-runtime/src/main/java/org/eolang/Universe.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ public interface Universe {
* @param parent Vertex of the parent eo object.
* @param child Vertex of the child eo object.
* @param att Name of attribute.
* @todo #2237:45min Implement the "bind" method. It has tp
* put data to eo object by vertex. It does nothing now
* but it is called from rust via jni call_method function.
* @checkstyle NonStaticMethodCheck (4 lines)
*/
void bind(int parent, int child, String att);
Expand Down
4 changes: 3 additions & 1 deletion eo-runtime/src/main/java/org/eolang/UniverseDefault.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ public void put(final int vertex, final byte[] bytes) {

@Override
public void bind(final int parent, final int child, final String att) {
//Empty yet.
this.get(parent)
.attr(att)
.put(this.get(child));
}

@Override
Expand Down
13 changes: 8 additions & 5 deletions eo-runtime/src/main/rust/eo_env/src/eo_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl<'local> EOEnv<'_> {
}

pub fn bind(&mut self, v1: u32, v2: u32, name: &str) -> Option<()> {
match self.java_env
let java_val = self.java_env
.call_method(
&self.java_obj,
"bind",
Expand All @@ -83,10 +83,13 @@ impl<'local> EOEnv<'_> {
JValue::Int(v2 as i32),
JValue::from(&JObject::from(self.java_env.new_string(name).unwrap()))
]
).unwrap().v() {
Ok(()) => Some(()),
_ => None,
}
);
return if java_val.is_err() {
self.java_env.exception_clear().ok()?;
None
} else {
java_val.unwrap().v().ok()
};
}

pub fn copy(&mut self, v: u32) -> Option<u32> {
Expand Down
54 changes: 43 additions & 11 deletions eo-runtime/src/test/eo/org/eolang/rust-tests.eo
Original file line number Diff line number Diff line change
Expand Up @@ -211,21 +211,27 @@
"""
use eo_env::EOEnv;
use eo_env::eo_enum::EO;
use eo_env::eo_enum::EO::{EOInt};
use eo_env::eo_enum::EO::{EOError, EOInt};
pub fn foo(env: &mut EOEnv) -> EO {
let v1 = env.find("$.^.a") as u32;
let v2 = env.find("$.^.b") as u32;
env.bind(v1 , v2, "EO-att");
return EOInt(v1 as i64);
let v1 = env.find("$.^.a") as u32;
let v2 = env.find("$.^.b") as u32;
if(env.bind(v1 , v2, "EO-att")).is_none(){
return EOError("bind failed".to_string());
} else {
return EOInt(0 as i64);
}
}
"""
*
[]
assert-that > @
insert
$.not
$.less-than
0
try
[]
insert > @
[e]
e > @
nop
$.string-starts-with "Rust insert failed"

[] > rust-copy-not-fails
123 > a
Expand Down Expand Up @@ -321,7 +327,6 @@
nop
$.all-of
$.string-starts-with "Rust insert failed "
$.string-ends-with "'Custom error'"

[] > rust-put-to-copy
QQ.rust > data
Expand All @@ -345,4 +350,31 @@
assert-that > @
data
$.equal-to
00-1A-EE
00-1A-EE

[] > rust-bind-to-copy
[content] > book
"qwerty" > line
QQ.rust > applied
"""
use eo_env::EOEnv;
use eo_env::eo_enum::EO;
use eo_env::eo_enum::EO::{EOVertex, EOError};

pub fn foo(env: &mut EOEnv) -> EO {
let eobook = env.find("$.^.book");
let copy = env.copy(eobook as u32).unwrap();
let eoline = env.find("$.^.line") as u32;
if env.bind(copy.clone(), eoline, "content").is_none() {
EOError("bind failed".to_string())
} else {
EOVertex(copy)
}
}
"""
*
[]
assert-that > @
applied.content
$.equal-to
"qwerty"
38 changes: 37 additions & 1 deletion eo-runtime/src/test/java/org/eolang/UniverseDefaultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import EOorg.EOeolang.EOseq;
import java.util.HashMap;
import java.util.Map;
import org.cactoos.map.MapOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -145,7 +146,7 @@ void copies() {
}

@Test
void outsToCopy() {
void putsToCopy() {
final Map<Integer, Phi> indexed = new HashMap<>();
final Universe universe = new UniverseDefault(Phi.Φ, indexed);
final int eoint = universe.find("Q.org.eolang.int");
Expand All @@ -159,6 +160,25 @@ void outsToCopy() {
);
}

@Test
void bindsCopyToAbstract() {
final Phi dummy = new DummyAbstract(Phi.Φ);
final Map<Integer, Phi> indexed = new MapOf<>(dummy.hashCode(), dummy);
final Universe universe = new UniverseDefault(dummy, indexed);
final int eoint = universe.find("Q.org.eolang.int");
final int copy = universe.copy(eoint);
universe.put(copy, UniverseDefaultTest.DATA);
universe.bind(
dummy.hashCode(), copy, UniverseDefaultTest.ATT
);
MatcherAssert.assertThat(
new Dataized(dummy.attr(UniverseDefaultTest.ATT).get()).take(),
Matchers.equalTo(
UniverseDefaultTest.DATA
)
);
}

/**
* Dummy phi with plain attribute.
* @since 0.31
Expand Down Expand Up @@ -200,4 +220,20 @@ private static class DummyWithStructure extends PhDefault {
this.add(UniverseDefaultTest.ATT, new AtComposite(this, DummyWithAt::new));
}
}

/**
* Dummy phi with free attribute.
* @since 0.31
*/
private static class DummyAbstract extends PhDefault {

/**
* Ctor.
* @param sigma Sigma
*/
DummyAbstract(final Phi sigma) {
super(sigma);
this.add(UniverseDefaultTest.ATT, new AtFree());
}
}
}

0 comments on commit 7c8c223

Please sign in to comment.