Skip to content

Commit

Permalink
Merge pull request #3401 from maxonfjvipon/fix/#3400/fix-try-object
Browse files Browse the repository at this point in the history
fix(#3400): fixed `try` object
  • Loading branch information
yegor256 authored Sep 29, 2024
2 parents 7af834f + c5ee6de commit bb7ac31
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 262 deletions.
10 changes: 7 additions & 3 deletions eo-runtime/src/main/eo/org/eolang/dataized.eo
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
# (dataized some-object).as-bytes > cached
# ```
#
# Dataization is a process of retrieving data (bytes) from an object, by taking its \Delta
# Dataization is a process of retrieving data (bytes) from an object, by taking its Δ
# attribute.
# An example of usage:
#
Expand All @@ -50,9 +50,13 @@
# 5 > five
#
# (dataized foo).as-bytes > result # result is 00-00-00-00-00-00-00-05
# result.as-number > f # f is 5
# result.as-number > f # f is 5
# ```
#
# Here, when `.as-bytes` is taken, the `dataized` dataizes (takes `bytes`) from the object `foo`
# and returns them. The `.as-bytes` is used to prevent double dataization.
[target] > dataized /bytes
[target] > dataized
try > @
target
error ex > [ex]
01-
61 changes: 0 additions & 61 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOdataized.java

This file was deleted.

196 changes: 10 additions & 186 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOtry.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
*/
package EOorg.EOeolang; // NOPMD

import java.util.function.Consumer;
import java.util.function.Function;
import org.eolang.AtVoid;
import org.eolang.Atom;
import org.eolang.Data;
Expand Down Expand Up @@ -60,190 +58,16 @@ public EOtry() {

@Override
public Phi lambda() {
return new PhTry(this.take("main"), this.take("catch"), this.take("finally"));
}

/**
* Object that knows how to deal with {@link EOorg.EOeolang.EOerror.ExError}.
* @since 0.36.0
*/
private static class PhTry implements Phi {
/**
* Body object.
*/
private final Phi body;

/**
* Catch object.
*/
private final Phi ctch;

/**
* Finally object.
*/
private final Phi last;

/**
* Put function.
*/
private final Consumer<Consumer<Phi>> func;

/**
* Ctor.
* @param body Body object
* @param ctch Catch object
* @param last Finally object
*/
PhTry(final Phi body, final Phi ctch, final Phi last) {
this.body = body;
this.ctch = ctch;
this.last = last;
this.func = new TryExecute(body, ctch);
}

@Override
public void attach(final byte[] data) {
this.func.accept(phi -> phi.attach(data));
}

@Override
public byte[] delta() {
return new TryReturn<byte[]>(
this.body, this.ctch, this.last
).apply(Data::delta);
}

@Override
public Phi copy() {
return new PhTry(this.body.copy(), this.ctch.copy(), this.last.copy());
}

@Override
public Phi take(final String name) {
return new TryReturn<Phi>(
this.body, this.ctch, this.last
).apply(phi -> phi.take(name));
}

@Override
public boolean put(final int pos, final Phi object) {
return new TryReturn<Boolean>(
this.body, this.ctch, this.last
).apply(phi -> phi.put(pos, object));
}

@Override
public boolean put(final String name, final Phi object) {
return new TryReturn<Boolean>(
this.body, this.ctch, this.last
).apply(phi -> phi.put(name, object));
}

@Override
public String locator() {
return new TryReturn<String>(
this.body, this.ctch, this.last
).apply(Phi::locator);
}

@Override
public String forma() {
return new TryReturn<String>(
this.body, this.ctch, this.last
).apply(Phi::forma);
}

@Override
public String φTerm() {
return new TryReturn<String>(
this.body, this.ctch, this.last
).apply(Phi::φTerm);
}
}

/**
* Tries to execute given function and catches {@link EOorg.EOeolang.EOerror.ExError}.
* @since 0.36.0
*/
private static class TryExecute implements Consumer<Consumer<Phi>> {
/**
* Body object.
*/
private final Phi body;

/**
* Catch object.
*/
private final Phi ctch;

/**
* Ctor.
* @param main Body object
* @param ctch Catch object
*/
TryExecute(final Phi main, final Phi ctch) {
this.body = main;
this.ctch = ctch;
}

@Override
public void accept(final Consumer<Phi> func) {
try {
func.accept(this.body);
} catch (final EOerror.ExError ex) {
final Phi caught = this.ctch.copy();
caught.put(0, ex.enclosure());
func.accept(caught);
}
}
}

/**
* Tries to return value from given function and catches {@link EOorg.EOeolang.EOerror.ExError}.
* @param <T> Type of return value.
* @since 0.36.0
*/
private static class TryReturn<T> implements Function<Function<Phi, T>, T> {
/**
* Body object.
*/
private final Phi body;

/**
* Catch object.
*/
private final Phi ctch;

/**
* Finally object.
*/
private final Phi last;

/**
* Ctor.
* @param main Body object
* @param ctch Catch object
* @param last Finally object
*/
TryReturn(final Phi main, final Phi ctch, final Phi last) {
this.body = main;
this.ctch = ctch;
this.last = last;
}

@Override
public T apply(final Function<Phi, T> func) {
T result;
try {
result = func.apply(this.body);
} catch (final EOerror.ExError ex) {
final Phi caught = this.ctch.copy();
caught.put(0, ex.enclosure());
result = func.apply(caught);
} finally {
new Dataized(this.last).take();
}
return result;
byte[] result;
try {
result = new Dataized(this.take("main")).take();
} catch (final EOerror.ExError ex) {
final Phi caught = this.take("catch").copy();
caught.put(0, ex.enclosure());
result = new Dataized(caught).take();
} finally {
new Dataized(this.take("finally")).take();
}
return new Data.ToPhi(result);
}
}
13 changes: 3 additions & 10 deletions eo-runtime/src/test/eo/org/eolang/rust-tests.eo
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@
e > [e]
true
0
length.
message
message.as-bytes.size
message

# Test.
Expand Down Expand Up @@ -251,9 +250,7 @@
e > [e]
true
eq. > @
res.slice
0
to-check.length
res.slice 0 to-check.as-bytes.size
to-check

# Test.
Expand Down Expand Up @@ -348,11 +345,7 @@
e > [e]
true
eq. > @
slice.
res
0
length.
message
res.slice 0 message.as-bytes.size
message

# Test.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ void refusesConnectionViaPosixSyscall() {
assert socket >= 0;
final SockaddrIn addr = new SockaddrIn(
(short) CStdLib.AF_INET,
EOsocketTest.htons(1234),
Integer.reverseBytes(CStdLib.INSTANCE.inet_addr(EOsocketTest.LOCALHOST))
EOsocketTest.htons(8080),
Integer.reverseBytes(CStdLib.INSTANCE.inet_addr("127.0.0.0"))
);
final int connected = CStdLib.INSTANCE.connect(socket, addr, addr.size());
MatcherAssert.assertThat(
Expand Down

0 comments on commit bb7ac31

Please sign in to comment.