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 4, 2023
2 parents 1bcc69d + 815092c commit 6fecf79
Show file tree
Hide file tree
Showing 10 changed files with 442 additions and 157 deletions.
43 changes: 37 additions & 6 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,9 +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;

/**
Expand Down Expand Up @@ -81,6 +85,11 @@ public class EOrust extends PhDefault {
*/
private final Map<Integer, Phi> phis = new HashMap<>();

/**
* Error that possibly was thrown while the native function execution.
*/
private final AtomicReference<Throwable> error = new AtomicReference<>();

static {
try {
NAMES = load("target/names");
Expand Down Expand Up @@ -159,10 +168,15 @@ public EOrust(final Phi sigma) {
).take(Phi[].class)[0];
return this.translate(
(byte[]) method.invoke(
null, new Universe(
portal, this.phis
null,
new UniverseSafe(
new UniverseDefault(
portal, this.phis
),
this.error
)
)
),
rho.attr("code").get().locator()
);
}
)
Expand Down Expand Up @@ -212,12 +226,16 @@ private static ConcurrentHashMap<String, String> 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;
Expand Down Expand Up @@ -250,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 failed in %s",
insert
);
} else {
throw new ExNative(
String.format("Rust insert failed in %s", 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;
Expand Down
2 changes: 1 addition & 1 deletion eo-runtime/src/main/java/org/eolang/ExFailure.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*
* @since 0.21
*/
public final class ExFailure extends ExAbstract {
public class ExFailure extends ExAbstract {

/**
* Serialization identifier.
Expand Down
58 changes: 58 additions & 0 deletions eo-runtime/src/main/java/org/eolang/ExNative.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

140 changes: 6 additions & 134 deletions eo-runtime/src/main/java/org/eolang/Universe.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,98 +23,26 @@
*/
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
*/
public class Universe {

/**
* Connector to eo objects.
*/
private final Phi connector;

/**
* EO objects that were already found.
*/
private final Map<Integer, Phi> indexed;

/**
* Ctor.
* @param connector Connector.
* @param indexed Map to index eo objects.
*/
public Universe(final Phi connector, final Map<Integer, Phi> 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.
* @param vertex Vertex off object.
* @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.
Expand All @@ -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.
Expand All @@ -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);
}
Loading

2 comments on commit 6fecf79

@0pdd
Copy link

@0pdd 0pdd commented on 6fecf79 Sep 4, 2023

Choose a reason for hiding this comment

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

Puzzle 2442-a6fae757 discovered in eo-runtime/src/main/rust/eo_env/src/eo_env.rs) and submitted as #2471. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on 6fecf79 Sep 4, 2023

Choose a reason for hiding this comment

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

Puzzle 2442-87b4ce00 discovered in eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java) and submitted as #2472. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.