Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Apr 3, 2024
2 parents a152223 + 719781d commit a3fa8c0
Show file tree
Hide file tree
Showing 13 changed files with 581 additions and 498 deletions.
2 changes: 1 addition & 1 deletion .codacy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# are not available for the project. For example a lot of
# package name contains capital letter and such names are conventional.
exclude_paths:
- "eo-runtime/src/main/java/EOorg/EOeolang/EOmemory$EOalloc.java"
- "eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOnew.java"
5 changes: 4 additions & 1 deletion eo-runtime/src/main/eo/org/eolang/cage.eo
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@
# This object is doing exactly the same as "memory", but allows
# you to store objects, not only data. In other words, it doesn't
# do dataization when objects are being stored.
[enclosure] > cage /?
[] > cage
# Make new `cage` for an object. After application this `new` object starts behave
# like `it` object.
[it] > new /?
174 changes: 174 additions & 0 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOnew.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 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.
*/

/*
* @checkstyle PackageNameCheck (4 lines)
*/

package EOorg.EOeolang;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import org.eolang.AtSimple;
import org.eolang.Atom;
import org.eolang.Attr;
import org.eolang.Data;
import org.eolang.ExFailure;
import org.eolang.PhDefault;
import org.eolang.PhTracedLocator;
import org.eolang.PhWrite;
import org.eolang.Phi;
import org.eolang.Term;
import org.eolang.XmirObject;

/**
* Cage.alloc object.
* @since 0.36.0
* @checkstyle TypeNameCheck (5 lines)
*/
@XmirObject(oname = "cage.new")
public final class EOcage$EOnew extends PhDefault implements Atom {
/**
* Locator calculator.
*/
private static final AtomicInteger LOCATOR = new AtomicInteger(0);

/**
* Cage for objects.
*/
private static final ConcurrentHashMap<Integer, Phi> CAGES = new ConcurrentHashMap<>(0);

/**
* Ctor.
* @param sigma Sigma
*/
EOcage$EOnew(final Phi sigma) {
super(sigma);
this.add("it", new EOcage$EOnew.AtEncaged());
this.add(
"encage",
new AtSimple(
new PhWrite(
this,
"it",
rho -> new Data.ToPhi(true)
)
)
);
}

@Override
public Phi lambda() throws Exception {
return this.take("it");
}

/**
* Attribute that stores object.
* @since 0.36.0
*/
private static class AtEncaged implements Attr {
/**
* Locator of encaged object.
*/
private Integer locator;

/**
* Form of the stored object.
*/
private String forma;

/**
* Ctor.
*/
AtEncaged() {
this(null, null);
}

/**
* Ctor for copying.
* @param locator Locator of object in memory
* @param form The form of the object
*/
AtEncaged(final Integer locator, final String form) {
this.locator = locator;
this.forma = form;
}

@Override
public Attr copy(final Phi self) {
return new EOcage$EOnew.AtEncaged(this.locator, this.forma);
}

@Override
public Phi get() {
if (this.locator == null || !EOcage$EOnew.CAGES.containsKey(this.locator)) {
throw new ExFailure(
"There's no object in storage, can't read"
);
}
return new PhTracedLocator(EOcage$EOnew.CAGES.get(this.locator), this.locator);
}

@Override
public void put(final Phi phi) {
if (this.forma == null) {
this.forma = phi.forma();
} else if (!this.forma.equals(phi.forma())) {
throw new ExFailure(
"Can't write an object formed by %s because object formed by %s was saved before",
phi.forma(),
this.forma
);
}
if (this.locator == null) {
synchronized (EOcage$EOnew.LOCATOR) {
this.locator = EOcage$EOnew.LOCATOR.incrementAndGet();
}
}
EOcage$EOnew.CAGES.put(this.locator, phi);
}

@Override
public String φTerm() {
final String txt;
if (this.locator == null || !EOcage$EOnew.CAGES.containsKey(this.locator)) {
txt = Term.EMPTY;
} else {
txt = EOcage$EOnew.CAGES.get(this.locator).φTerm();
}
return txt;
}

@Override
public String toString() {
final String txt;
if (this.locator == null || !EOcage$EOnew.CAGES.containsKey(this.locator)) {
txt = Term.EMPTY;
} else {
txt = EOcage$EOnew.CAGES.get(this.locator).toString();
}
return txt;
}
}
}
47 changes: 17 additions & 30 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOmemory$EOalloc.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
package EOorg.EOeolang;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicInteger;
import org.eolang.AtSimple;
import org.eolang.AtVoid;
import org.eolang.Atom;
import org.eolang.Attr;
import org.eolang.Data;
import org.eolang.Dataized;
import org.eolang.ExFailure;
import org.eolang.PhDefault;
import org.eolang.PhWrite;
import org.eolang.Phi;
import org.eolang.Term;
import org.eolang.XmirObject;
Expand All @@ -52,12 +52,12 @@ public final class EOmemory$EOalloc extends PhDefault implements Atom {
/**
* Locator calculator.
*/
private static final AtomicLong LOCATOR = new AtomicLong(0L);
private static final AtomicInteger LOCATOR = new AtomicInteger(0);

/**
* Memory.
*/
private static final ConcurrentHashMap<Long, Phi> MEMORY = new ConcurrentHashMap<>(0);
private static final ConcurrentHashMap<Integer, Phi> MEMORY = new ConcurrentHashMap<>(0);

/**
* Ctor.
Expand All @@ -66,7 +66,16 @@ public final class EOmemory$EOalloc extends PhDefault implements Atom {
EOmemory$EOalloc(final Phi sigma) {
super(sigma);
this.add("data", new EOmemory$EOalloc.AtMalloc());
this.add("write", new AtSimple(new EOmemory$EOalloc.Write(this)));
this.add(
"write",
new AtSimple(
new PhWrite(
this,
"data",
rho -> rho.take("data")
)
)
);
}

@Override
Expand All @@ -80,9 +89,9 @@ public Phi lambda() throws Exception {
*/
private static class AtMalloc implements Attr {
/**
* Object in memory.
* Locator of object in memory.
*/
private Long locator;
private Integer locator;

/**
* Allocated bytes length.
Expand All @@ -101,7 +110,7 @@ private static class AtMalloc implements Attr {
* @param locator Locator of object in memory
* @param length Allocated bytes length
*/
AtMalloc(final Long locator, final Integer length) {
AtMalloc(final Integer locator, final Integer length) {
this.locator = locator;
this.length = length;
}
Expand Down Expand Up @@ -163,26 +172,4 @@ public String toString() {
return txt;
}
}

/**
* Memory.alloc.write object.
* @since 0.36.0
*/
private static class Write extends PhDefault implements Atom {
/**
* Ctor.
* @param sigma Sigma
*/
Write(final Phi sigma) {
super(sigma);
this.add("data", new AtVoid("data"));
}

@Override
public Phi lambda() {
final Phi rho = this.take(Attr.RHO);
rho.put("data", this.take("data"));
return rho.take("data");
}
}
}
10 changes: 1 addition & 9 deletions eo-runtime/src/main/java/org/eolang/PhDefault.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,11 @@ public int hashCode() {

@Override
public String toString() {
String result = String.format(
return String.format(
"%sν%d",
this.getClass().getCanonicalName(),
this.vertex
);
if (this.attrs.containsKey(Attr.DELTA)) {
result = String.format(
"%s=%s",
result,
this.attrs.get(Attr.DELTA).toString()
);
}
return result;
}

@Override
Expand Down
8 changes: 7 additions & 1 deletion eo-runtime/src/main/java/org/eolang/PhSafe.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ public String forma() {

@Override
public byte[] delta() {
return this.origin.delta();
try {
return this.origin.delta();
} catch (final ExFailure ex) {
throw new EOerror.ExError(
new Data.ToPhi(EOerror.message(ex))
);
}
}
}
Loading

2 comments on commit a3fa8c0

@0pdd
Copy link

@0pdd 0pdd commented on a3fa8c0 Apr 3, 2024

Choose a reason for hiding this comment

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

Puzzle 2931-0ee2db02 disappeared from eo-runtime/src/test/eo/org/eolang/cage-tests.eo), that's why I closed #3001. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link

@0pdd 0pdd commented on a3fa8c0 Apr 3, 2024

Choose a reason for hiding this comment

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

Puzzle 2931-0ed69df1 disappeared from eo-runtime/src/test/eo/org/eolang/runtime-tests.eo), that's why I closed #3021. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.