From 6ede0fac29e338ba4c17c7683df9cf54a2df41d7 Mon Sep 17 00:00:00 2001 From: levBagryansky <28lev11@gmail.com> Date: Wed, 30 Aug 2023 17:26:50 +0300 Subject: [PATCH 1/5] #2442: Inherited UniverseDefault from Universe interface --- .../src/main/java/EOorg/EOeolang/EOrust.java | 3 +- ...erseTest.java => UniverseDefaultTest.java} | 35 ++++++++++--------- 2 files changed, 21 insertions(+), 17 deletions(-) rename eo-runtime/src/test/java/org/eolang/{UniverseTest.java => UniverseDefaultTest.java} (79%) diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java index 8cffb05e5d..1b1de3b6f8 100644 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java @@ -53,6 +53,7 @@ import org.eolang.PhDefault; import org.eolang.Phi; import org.eolang.Universe; +import org.eolang.UniverseDefault; import org.eolang.XmirObject; /** @@ -159,7 +160,7 @@ public EOrust(final Phi sigma) { ).take(Phi[].class)[0]; return this.translate( (byte[]) method.invoke( - null, new Universe( + null, new UniverseDefault( portal, this.phis ) ) diff --git a/eo-runtime/src/test/java/org/eolang/UniverseTest.java b/eo-runtime/src/test/java/org/eolang/UniverseDefaultTest.java similarity index 79% rename from eo-runtime/src/test/java/org/eolang/UniverseTest.java rename to eo-runtime/src/test/java/org/eolang/UniverseDefaultTest.java index 16bdf06751..d43fd62d17 100644 --- a/eo-runtime/src/test/java/org/eolang/UniverseTest.java +++ b/eo-runtime/src/test/java/org/eolang/UniverseDefaultTest.java @@ -32,10 +32,10 @@ import org.junit.jupiter.api.Test; /** - * Test case for {@link Universe}. + * Test case for {@link UniverseDefault}. * @since 0.31 */ -final class UniverseTest { +final class UniverseDefaultTest { /** * Name of attribute. @@ -45,11 +45,11 @@ final class UniverseTest { @Test void findsSimpleAtt() { final Phi phi = new DummyWithAt(Phi.Φ); - final Universe universe = new Universe(phi); + final UniverseDefault universe = new UniverseDefault(phi); MatcherAssert.assertThat( - universe.find("$.".concat(UniverseTest.ATT)), + universe.find("$.".concat(UniverseDefaultTest.ATT)), Matchers.equalTo( - phi.attr(UniverseTest.ATT).get().hashCode() + phi.attr(UniverseDefaultTest.ATT).get().hashCode() ) ); } @@ -57,17 +57,20 @@ void findsSimpleAtt() { @Test void findsLongAtt() { final Phi phi = new DummyWithStructure(Phi.Φ); - final Universe universe = new Universe(phi); + final UniverseDefault universe = new UniverseDefault(phi); MatcherAssert.assertThat( universe.find( String.format( "$.%s.%s", - UniverseTest.ATT, - UniverseTest.ATT + UniverseDefaultTest.ATT, + UniverseDefaultTest.ATT ) ), Matchers.equalTo( - phi.attr(UniverseTest.ATT).get().attr(UniverseTest.ATT).get().hashCode() + phi + .attr(UniverseDefaultTest.ATT).get() + .attr(UniverseDefaultTest.ATT).get() + .hashCode() ) ); } @@ -75,7 +78,7 @@ void findsLongAtt() { @Test void findsByAbsoluteLoc() { final Map indexed = new HashMap<>(); - final Universe universe = new Universe(Phi.Φ, indexed); + final UniverseDefault universe = new UniverseDefault(Phi.Φ, indexed); final int vertex = universe.find("Q.org.eolang.seq"); MatcherAssert.assertThat( indexed.get(vertex).getClass(), @@ -87,7 +90,7 @@ void findsByAbsoluteLoc() { void throwsIfWrongFind() { Assertions.assertThrows( ExAbstract.class, - () -> new Universe( + () -> new UniverseDefault( new DummyWithStructure(Phi.Φ) ).find("$.wrong-name") ); @@ -95,11 +98,11 @@ void throwsIfWrongFind() { @Test void dataizesIndexed() { - final Universe universe = new Universe( + final UniverseDefault universe = new UniverseDefault( new DummyWithAt(Phi.Φ) ); final int vertex = universe.find( - "$.".concat(UniverseTest.ATT) + "$.".concat(UniverseDefaultTest.ATT) ); MatcherAssert.assertThat( universe.dataize(vertex), @@ -111,7 +114,7 @@ void dataizesIndexed() { void throwsIfWrongDataize() { Assertions.assertThrows( ExAbstract.class, - () -> new Universe( + () -> new UniverseDefault( new DummyWithStructure(Phi.Φ) ).dataize(-1) ); @@ -138,7 +141,7 @@ private static class DummyWithAt extends PhDefault { * @param sigma Sigma. */ DummyWithAt(final Phi sigma) { - this(sigma, UniverseTest.ATT); + this(sigma, UniverseDefaultTest.ATT); } } @@ -154,7 +157,7 @@ private static class DummyWithStructure extends PhDefault { */ DummyWithStructure(final Phi sigma) { super(sigma); - this.add(UniverseTest.ATT, new AtComposite(this, DummyWithAt::new)); + this.add(UniverseDefaultTest.ATT, new AtComposite(this, DummyWithAt::new)); } } } From 687cbe81a269d39b94b4f26592de838b7cbcb063 Mon Sep 17 00:00:00 2001 From: levBagryansky <28lev11@gmail.com> Date: Wed, 30 Aug 2023 17:27:02 +0300 Subject: [PATCH 2/5] #2442: Inherited UniverseDefault from Universe interface --- .../src/main/java/org/eolang/Universe.java | 142 +------------- .../main/java/org/eolang/UniverseDefault.java | 174 ++++++++++++++++++ 2 files changed, 181 insertions(+), 135 deletions(-) create mode 100644 eo-runtime/src/main/java/org/eolang/UniverseDefault.java diff --git a/eo-runtime/src/main/java/org/eolang/Universe.java b/eo-runtime/src/main/java/org/eolang/Universe.java index 7d01794413..63168bd3aa 100644 --- a/eo-runtime/src/main/java/org/eolang/Universe.java +++ b/eo-runtime/src/main/java/org/eolang/Universe.java @@ -23,88 +23,18 @@ */ package org.eolang; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - /** * Class to manipulate eo objects within "Universe" paradigm. - * @since 0.30 + * @since 0.31 */ -public class Universe { - - /** - * Connector to eo objects. - */ - private final Phi connector; - - /** - * EO objects that were already found. - */ - private final Map indexed; - - /** - * Ctor. - * @param connector Connector. - * @param indexed Map to index eo objects. - */ - public Universe(final Phi connector, final Map indexed) { - this.connector = connector; - this.indexed = indexed; - } - - /** - * Ctor. - * @param connector Connector. - */ - public Universe(final Phi connector) { - this( - connector, new HashMap<>() - ); - } - - /** - * Ctor. - */ - public Universe() { - this(Phi.Φ); - } +public interface Universe { /** * Finds vertex of eo object by its location. * @param name Relative location of the object to find like "^.^.some-obj". * @return Vertex of the object to find. */ - public int find(final String name) { - if (name == null) { - throw new IllegalArgumentException( - "Argument name is null" - ); - } - Phi accum; - final String[] atts = Universe.replace(name) - .split("\\."); - if ("Q".equals(atts[0])) { - accum = Phi.Φ; - } else if ("$".equals(atts[0])) { - accum = this.connector; - } else { - throw new ExFailure( - String.format( - "Universe.find starts with %s, but it should start with Q or $ only", - atts[0] - ) - ); - } - atts[0] = ""; - for (final String att: atts) { - if (!"".equals(att)) { - accum = accum.attr(att).get(); - } - } - this.indexed.putIfAbsent(accum.hashCode(), accum); - return accum.hashCode(); - } + int find(String name); /** * Puts data to eo object by vertex. @@ -112,9 +42,7 @@ public int find(final String name) { * @param bytes Data to put. * @checkstyle NonStaticMethodCheck (4 lines) */ - public void put(final int vertex, final byte[] bytes) { - //Empty yet. - } + void put(int vertex, byte[] bytes); /** * Binds child to parent. @@ -126,9 +54,7 @@ public void put(final int vertex, final byte[] bytes) { * but it is called from rust via jni call_method function. * @checkstyle NonStaticMethodCheck (4 lines) */ - public void bind(final int parent, final int child, final String att) { - //Empty yet. - } + void bind(int parent, int child, String att); /** * Copies the eo object. @@ -139,66 +65,12 @@ public void bind(final int parent, final int child, final String att) { * method relates to building a new eo object in rust insert. * @checkstyle NonStaticMethodCheck (4 lines) */ - public int copy(final int vertex) { - return vertex; - } + int copy(int vertex); /** * Dataizes the eo object by vertex and return byte array. * @param vertex Vertex of eo-object. * @return Raw data. */ - public byte[] dataize(final int vertex) { - return new Param( - this.get(vertex), - "Δ" - ).asBytes().take(); - } - - /** - * Find phi by vertex. - * @param vertex Vertex. - * @return Phi. - * @throws ExFailure if vertex does not exist in the map. - */ - private Phi get(final int vertex) { - return Optional.ofNullable( - this.indexed.get(vertex) - ).orElseThrow( - () -> new ExFailure( - String.format( - "Phi object with vertex %d was not indexed.", - vertex - ) - ) - ); - } - - /** - * Replaces specific eo symbols to java symbols. - * @param name Name of eo object. - * @return Correct location. - */ - private static String replace(final String name) { - final StringBuilder builder = new StringBuilder(name.length()); - for (int iter = 0; iter < name.length(); iter += 1) { - final char cur = name.charAt(iter); - switch (cur) { - case '^': - builder.append('ρ'); - break; - case '@': - builder.append('φ'); - break; - case '&': - builder.append('σ'); - break; - default: - builder.append(cur); - break; - } - } - return builder.toString(); - } - + byte[] dataize(int vertex); } diff --git a/eo-runtime/src/main/java/org/eolang/UniverseDefault.java b/eo-runtime/src/main/java/org/eolang/UniverseDefault.java new file mode 100644 index 0000000000..a783ce0029 --- /dev/null +++ b/eo-runtime/src/main/java/org/eolang/UniverseDefault.java @@ -0,0 +1,174 @@ +/* + * 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. + */ +package org.eolang; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +/** + * Default implementation that can be used on the java side. + * @since 0.30 + */ +public final class UniverseDefault implements Universe { + + /** + * Connector to eo objects. + */ + private final Phi connector; + + /** + * EO objects that were already found. + */ + private final Map indexed; + + /** + * Ctor. + * @param connector Connector. + * @param indexed Map to index eo objects. + */ + public UniverseDefault(final Phi connector, final Map indexed) { + this.connector = connector; + this.indexed = indexed; + } + + /** + * Ctor. + * @param connector Connector. + */ + public UniverseDefault(final Phi connector) { + this( + connector, new HashMap<>() + ); + } + + /** + * Ctor. + */ + public UniverseDefault() { + this(Phi.Φ); + } + + @Override + public int find(final String name) { + if (name == null) { + throw new IllegalArgumentException( + "Argument name is null" + ); + } + Phi accum; + final String[] atts = UniverseDefault.replace(name) + .split("\\."); + if ("Q".equals(atts[0])) { + accum = Phi.Φ; + } else if ("$".equals(atts[0])) { + accum = this.connector; + } else { + throw new ExFailure( + String.format( + "Universe.find starts with %s, but it should start with Q or $ only", + atts[0] + ) + ); + } + atts[0] = ""; + for (final String att: atts) { + if (!"".equals(att)) { + accum = accum.attr(att).get(); + } + } + this.indexed.putIfAbsent(accum.hashCode(), accum); + return accum.hashCode(); + } + + @Override + public void put(final int vertex, final byte[] bytes) { + //Empty yet. + } + + @Override + public void bind(final int parent, final int child, final String att) { + //Empty yet. + } + + @Override + public int copy(final int vertex) { + return vertex; + } + + @Override + public byte[] dataize(final int vertex) { + return new Param( + this.get(vertex), + "Δ" + ).asBytes().take(); + } + + /** + * Find phi by vertex. + * @param vertex Vertex. + * @return Phi. + * @throws ExFailure if vertex does not exist in the map. + */ + private Phi get(final int vertex) { + return Optional.ofNullable( + this.indexed.get(vertex) + ).orElseThrow( + () -> new ExFailure( + String.format( + "Phi object with vertex %d was not indexed.", + vertex + ) + ) + ); + } + + /** + * Replaces specific eo symbols to java symbols. + * @param name Name of eo object. + * @return Correct location. + */ + private static String replace(final String name) { + final StringBuilder builder = new StringBuilder(name.length()); + for (int iter = 0; iter < name.length(); iter += 1) { + final char cur = name.charAt(iter); + switch (cur) { + case '^': + builder.append('ρ'); + break; + case '@': + builder.append('φ'); + break; + case '&': + builder.append('σ'); + break; + default: + builder.append(cur); + break; + } + } + return builder.toString(); + } + +} From b29f86755c6185b647cc2f202ca8cd3381cf8951 Mon Sep 17 00:00:00 2001 From: levBagryansky <28lev11@gmail.com> Date: Thu, 31 Aug 2023 18:22:32 +0300 Subject: [PATCH 3/5] #2442: Added UniverseSafe to handle exceptions --- .../src/main/java/EOorg/EOeolang/EOrust.java | 42 ++++++- .../src/main/java/org/eolang/ExFailure.java | 2 +- .../src/main/java/org/eolang/ExNative.java | 58 +++++++++ .../src/main/java/org/eolang/Universe.java | 2 +- .../main/java/org/eolang/UniverseSafe.java | 113 ++++++++++++++++++ .../src/main/rust/eo_env/src/eo_enum.rs | 7 ++ eo-runtime/src/main/rust/eo_env/src/eo_env.rs | 5 + .../src/test/eo/org/eolang/rust-tests.eo | 22 ++++ 8 files changed, 243 insertions(+), 8 deletions(-) create mode 100644 eo-runtime/src/main/java/org/eolang/ExNative.java create mode 100644 eo-runtime/src/main/java/org/eolang/UniverseSafe.java diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java index 1b1de3b6f8..8456c3d451 100644 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java @@ -39,6 +39,7 @@ import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicReference; import org.apache.commons.lang3.NotImplementedException; import org.apache.commons.lang3.SystemUtils; import org.cactoos.bytes.Base64Bytes; @@ -50,10 +51,12 @@ import org.eolang.Data; import org.eolang.Dataized; import org.eolang.ExFailure; +import org.eolang.ExNative; import org.eolang.PhDefault; import org.eolang.Phi; import org.eolang.Universe; import org.eolang.UniverseDefault; +import org.eolang.UniverseSafe; import org.eolang.XmirObject; /** @@ -82,6 +85,11 @@ public class EOrust extends PhDefault { */ private final Map phis = new HashMap<>(); + /** + * Error that possibly was thrown while the native function execution. + */ + private final AtomicReference error = new AtomicReference<>(); + static { try { NAMES = load("target/names"); @@ -160,10 +168,15 @@ public EOrust(final Phi sigma) { ).take(Phi[].class)[0]; return this.translate( (byte[]) method.invoke( - null, new UniverseDefault( - portal, this.phis + null, + new UniverseSafe( + new UniverseDefault( + portal, this.phis + ), + this.error ) - ) + ), + rho.attr("code").get().locator() ); } ) @@ -213,12 +226,16 @@ private static ConcurrentHashMap load(final String src) throws I /** * Translates byte message from rust side to Phi object. * @param message Message that native method returns. + * @param insert Location of the rust insert. * @return Phi object. * @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. + * @todo #2442:45min Improve handling EOError returning. + * It should also send a String message describing error on the + * native side and handle it correctly on the java side. */ - private Phi translate(final byte[] message) { + private Phi translate(final byte[] message, final String insert) { final byte determinant = message[0]; final byte[] content = Arrays.copyOfRange(message, 1, message.length); final Phi ret; @@ -251,9 +268,22 @@ private Phi translate(final byte[] message) { buffer.flip(); ret = new Data.ToPhi(buffer.getLong()); break; + case 5: + if (this.error.get() == null) { + throw new ExNative( + "Rust insert %s failed", + insert + ); + } else { + throw new ExNative( + String.format("Rust insert %s failed", insert), + this.error.get() + ); + } default: - throw new ExFailure( - "Returning Strings and raw bytes is not implemented yet" + throw new ExNative( + "Returning Strings and raw bytes is not implemented yet, insert %s", + insert ); } return ret; diff --git a/eo-runtime/src/main/java/org/eolang/ExFailure.java b/eo-runtime/src/main/java/org/eolang/ExFailure.java index 5853ec38f4..255b6e65e1 100644 --- a/eo-runtime/src/main/java/org/eolang/ExFailure.java +++ b/eo-runtime/src/main/java/org/eolang/ExFailure.java @@ -29,7 +29,7 @@ * * @since 0.21 */ -public final class ExFailure extends ExAbstract { +public class ExFailure extends ExAbstract { /** * Serialization identifier. diff --git a/eo-runtime/src/main/java/org/eolang/ExNative.java b/eo-runtime/src/main/java/org/eolang/ExNative.java new file mode 100644 index 0000000000..9aba1a2212 --- /dev/null +++ b/eo-runtime/src/main/java/org/eolang/ExNative.java @@ -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. + */ + +package org.eolang; + +/** + * Exception in native method. + * + * @since 0.32 + */ +public final class ExNative extends ExFailure { + + private static final long serialVersionUID = 5726845593921315515L; + + /** + * Serialization identifier. + */ + + /** + * Ctor. + * @param cause Exception cause + * @param args Arguments for {@link String#format(String, Object...)} + */ + public ExNative(final String cause, final Object... args) { + super(String.format(cause, args)); + } + + /** + * Ctor. + * @param cause Exception cause + * @param root Cause exception + */ + public ExNative(final String cause, final Throwable root) { + super(cause, root); + } +} + diff --git a/eo-runtime/src/main/java/org/eolang/Universe.java b/eo-runtime/src/main/java/org/eolang/Universe.java index 63168bd3aa..d247be5120 100644 --- a/eo-runtime/src/main/java/org/eolang/Universe.java +++ b/eo-runtime/src/main/java/org/eolang/Universe.java @@ -25,7 +25,7 @@ /** * Class to manipulate eo objects within "Universe" paradigm. - * @since 0.31 + * @since 0.32 */ public interface Universe { diff --git a/eo-runtime/src/main/java/org/eolang/UniverseSafe.java b/eo-runtime/src/main/java/org/eolang/UniverseSafe.java new file mode 100644 index 0000000000..157c48a2d4 --- /dev/null +++ b/eo-runtime/src/main/java/org/eolang/UniverseSafe.java @@ -0,0 +1,113 @@ +/* + * 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. + */ +package org.eolang; + +import java.util.concurrent.atomic.AtomicReference; + +/** + * {@link Universe} to be used from within native function. + * Since we cannot save stacktrace of exception inside native code + * (we can only detect it), we should keep it on the java side. + * More detailed: + * + * www.developer.com/open-source/exception-handling-in-jni, + * + * stackoverflow.com/questions/2054598/how-to-catch-jni-java-exception, + * + * www.iitk.ac.in/esc101/05Aug/tutorial/native1.1/implementing/error.html + * @since 0.32 + * @checkstyle IllegalCatchCheck (200 lines) + */ +public final class UniverseSafe implements Universe { + + /** + * {@link Universe} providing methods. + */ + private final UniverseDefault origin; + + /** + * Exception to be saved. + */ + private final AtomicReference saved; + + /** + * Ctor. + * @param origin Origin. + * @param throwable A cell to keep throwable. + */ + public UniverseSafe(final UniverseDefault origin, final AtomicReference throwable) { + this.origin = origin; + this.saved = throwable; + } + + @Override + public int find(final String name) { + try { + return this.origin.find(name); + } catch (final Throwable throwable) { + this.saved.set(throwable); + throw throwable; + } + } + + @Override + public void put(final int vertex, final byte[] bytes) { + try { + this.origin.put(vertex, bytes); + } catch (final Throwable throwable) { + this.saved.set(throwable); + throw throwable; + } + } + + @Override + public void bind(final int parent, final int child, final String att) { + try { + this.origin.bind(parent, child, att); + } catch (final Throwable throwable) { + this.saved.set(throwable); + throw throwable; + } + } + + @Override + public int copy(final int vertex) { + try { + return this.origin.copy(vertex); + } catch (final Throwable throwable) { + this.saved.set(throwable); + throw throwable; + } + } + + @Override + public byte[] dataize(final int vertex) { + try { + return this.origin.dataize(vertex); + } catch (final Throwable throwable) { + this.saved.set(throwable); + throw throwable; + } + } +} diff --git a/eo-runtime/src/main/rust/eo_env/src/eo_enum.rs b/eo-runtime/src/main/rust/eo_env/src/eo_enum.rs index ded4fae99b..8fbd1ecd4e 100644 --- a/eo-runtime/src/main/rust/eo_env/src/eo_enum.rs +++ b/eo-runtime/src/main/rust/eo_env/src/eo_enum.rs @@ -28,6 +28,7 @@ pub enum EO { EOInt(i64), EOString(String), EORaw(Box<[u8]>), + EOError(String), } impl EO { @@ -53,6 +54,12 @@ impl EO { } EO::EOString(_) => { vec![0xff] } EO::EORaw(_) => { vec![0xff] } + + EO::EOError(_) => { + let mut res: Vec = vec![0; 1]; + res[0] = 5; + res + } } } } 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 dbd98fe28a..d4c9ca2b28 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 @@ -32,6 +32,11 @@ pub struct EOEnv<'local> { java_obj: JObject<'local> } +/* + * @todo #2442:45min Add correct processing of the return value of functions. + * call_method returns Result> which we should not just unwrap. + * We need to check it instead and return None if exception in java side happened. +*/ impl<'local> EOEnv<'_> { pub fn new(java_env: JNIEnv<'local>, _java_class: JClass<'local>, java_obj: JObject<'local>) -> EOEnv<'local> { EOEnv { 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 3dca7a2a62..e52f0166a8 100644 --- a/eo-runtime/src/test/eo/org/eolang/rust-tests.eo +++ b/eo-runtime/src/test/eo/org/eolang/rust-tests.eo @@ -254,3 +254,25 @@ assert-that > @ plus $.equal-to 15 + +[] > rust-error + QQ.rust > err + """ + use eo_env::EOEnv; + use eo_env::eo_enum::EO; + use eo_env::eo_enum::EO::{EOError}; + + pub fn foo(_env: &mut EOEnv) -> EO { + EOError("Error happened".to_string()) + } + """ + * + [] + assert-that > @ + try + [] + err > @ + [e] + e > @ + nop + $.string-starts-with "Rust insert " \ No newline at end of file From 9a3f968a6f740e551e02d71c9a15518b2d3aa609 Mon Sep 17 00:00:00 2001 From: levBagryansky <28lev11@gmail.com> Date: Thu, 31 Aug 2023 18:24:58 +0300 Subject: [PATCH 4/5] #2442: Edited @since tags --- eo-runtime/src/main/java/org/eolang/Universe.java | 2 +- eo-runtime/src/main/java/org/eolang/UniverseDefault.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/eo-runtime/src/main/java/org/eolang/Universe.java b/eo-runtime/src/main/java/org/eolang/Universe.java index d247be5120..a21689606a 100644 --- a/eo-runtime/src/main/java/org/eolang/Universe.java +++ b/eo-runtime/src/main/java/org/eolang/Universe.java @@ -25,7 +25,7 @@ /** * Class to manipulate eo objects within "Universe" paradigm. - * @since 0.32 + * @since 0.30 */ public interface Universe { diff --git a/eo-runtime/src/main/java/org/eolang/UniverseDefault.java b/eo-runtime/src/main/java/org/eolang/UniverseDefault.java index a783ce0029..92eb54fb2b 100644 --- a/eo-runtime/src/main/java/org/eolang/UniverseDefault.java +++ b/eo-runtime/src/main/java/org/eolang/UniverseDefault.java @@ -29,7 +29,7 @@ /** * Default implementation that can be used on the java side. - * @since 0.30 + * @since 0.32 */ public final class UniverseDefault implements Universe { From 815092c8bd37ac27f7bef75859f3789a7d1ac83f Mon Sep 17 00:00:00 2001 From: levBagryansky <28lev11@gmail.com> Date: Fri, 1 Sep 2023 16:51:42 +0300 Subject: [PATCH 5/5] #2442: Edited message error --- eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java | 4 ++-- eo-runtime/src/test/eo/org/eolang/rust-tests.eo | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java index 8456c3d451..ef11780004 100644 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java @@ -271,12 +271,12 @@ private Phi translate(final byte[] message, final String insert) { case 5: if (this.error.get() == null) { throw new ExNative( - "Rust insert %s failed", + "Rust insert failed in %s", insert ); } else { throw new ExNative( - String.format("Rust insert %s failed", insert), + String.format("Rust insert failed in %s", insert), this.error.get() ); } 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 e52f0166a8..b02b78f207 100644 --- a/eo-runtime/src/test/eo/org/eolang/rust-tests.eo +++ b/eo-runtime/src/test/eo/org/eolang/rust-tests.eo @@ -275,4 +275,4 @@ [e] e > @ nop - $.string-starts-with "Rust insert " \ No newline at end of file + $.string-starts-with "Rust insert failed " \ No newline at end of file