From ee2af0bcaec0fa48f46935fdf5ec1f7afa2aca63 Mon Sep 17 00:00:00 2001 From: levBagryansky <28lev11@gmail.com> Date: Tue, 19 Sep 2023 12:16:48 +0300 Subject: [PATCH 1/3] #2294: typo. Edited testname. --- eo-runtime/src/test/java/org/eolang/UniverseDefaultTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eo-runtime/src/test/java/org/eolang/UniverseDefaultTest.java b/eo-runtime/src/test/java/org/eolang/UniverseDefaultTest.java index 5cf8be15a7..2b9f55059d 100644 --- a/eo-runtime/src/test/java/org/eolang/UniverseDefaultTest.java +++ b/eo-runtime/src/test/java/org/eolang/UniverseDefaultTest.java @@ -145,7 +145,7 @@ void copies() { } @Test - void outsToCopy() { + void putsToCopy() { final Map indexed = new HashMap<>(); final Universe universe = new UniverseDefault(Phi.Φ, indexed); final int eoint = universe.find("Q.org.eolang.int"); From 980e7816b42b59fc483c9e5b8431173485896f40 Mon Sep 17 00:00:00 2001 From: levBagryansky <28lev11@gmail.com> Date: Tue, 19 Sep 2023 14:48:13 +0300 Subject: [PATCH 2/3] #2294: Implemented bind --- .../src/main/java/org/eolang/Universe.java | 3 -- .../main/java/org/eolang/UniverseDefault.java | 4 +- eo-runtime/src/main/rust/eo_env/src/eo_env.rs | 13 +++-- .../src/test/eo/org/eolang/rust-tests.eo | 53 +++++++++++++++---- .../java/org/eolang/UniverseDefaultTest.java | 36 +++++++++++++ 5 files changed, 90 insertions(+), 19 deletions(-) diff --git a/eo-runtime/src/main/java/org/eolang/Universe.java b/eo-runtime/src/main/java/org/eolang/Universe.java index 31a8c3b227..6092f57799 100644 --- a/eo-runtime/src/main/java/org/eolang/Universe.java +++ b/eo-runtime/src/main/java/org/eolang/Universe.java @@ -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); diff --git a/eo-runtime/src/main/java/org/eolang/UniverseDefault.java b/eo-runtime/src/main/java/org/eolang/UniverseDefault.java index cb64114358..6991eabf68 100644 --- a/eo-runtime/src/main/java/org/eolang/UniverseDefault.java +++ b/eo-runtime/src/main/java/org/eolang/UniverseDefault.java @@ -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 diff --git a/eo-runtime/src/main/rust/eo_env/src/eo_env.rs b/eo-runtime/src/main/rust/eo_env/src/eo_env.rs index 363c5080e5..8a6e75882c 100644 --- a/eo-runtime/src/main/rust/eo_env/src/eo_env.rs +++ b/eo-runtime/src/main/rust/eo_env/src/eo_env.rs @@ -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", @@ -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 { diff --git a/eo-runtime/src/test/eo/org/eolang/rust-tests.eo b/eo-runtime/src/test/eo/org/eolang/rust-tests.eo index 1ef7d287d2..f1c2c5820c 100644 --- a/eo-runtime/src/test/eo/org/eolang/rust-tests.eo +++ b/eo-runtime/src/test/eo/org/eolang/rust-tests.eo @@ -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 @@ -345,4 +351,31 @@ assert-that > @ data $.equal-to - 00-1A-EE \ No newline at end of file + 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" diff --git a/eo-runtime/src/test/java/org/eolang/UniverseDefaultTest.java b/eo-runtime/src/test/java/org/eolang/UniverseDefaultTest.java index 2b9f55059d..108ecb37a6 100644 --- a/eo-runtime/src/test/java/org/eolang/UniverseDefaultTest.java +++ b/eo-runtime/src/test/java/org/eolang/UniverseDefaultTest.java @@ -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; @@ -159,6 +160,25 @@ void putsToCopy() { ); } + @Test + void bindsCopyToAbstract() { + final Phi dummy = new DummyAbstract(Phi.Φ); + final Map 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 @@ -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()); + } + } } From be6c2856dd3c1ee0e9d94cdad7c5d214a13fdbc1 Mon Sep 17 00:00:00 2001 From: levBagryansky <28lev11@gmail.com> Date: Tue, 19 Sep 2023 15:07:27 +0300 Subject: [PATCH 3/3] #2294: removed string-ends-with because it should be string-contains which is not works at the moment --- eo-runtime/src/test/eo/org/eolang/rust-tests.eo | 1 - 1 file changed, 1 deletion(-) diff --git a/eo-runtime/src/test/eo/org/eolang/rust-tests.eo b/eo-runtime/src/test/eo/org/eolang/rust-tests.eo index f1c2c5820c..a7559cfd74 100644 --- a/eo-runtime/src/test/eo/org/eolang/rust-tests.eo +++ b/eo-runtime/src/test/eo/org/eolang/rust-tests.eo @@ -327,7 +327,6 @@ nop $.all-of $.string-starts-with "Rust insert failed " - $.string-ends-with "'Custom error'" [] > rust-put-to-copy QQ.rust > data