Skip to content

Commit

Permalink
fix(objectionary#3057): cage.encaged
Browse files Browse the repository at this point in the history
  • Loading branch information
maxonfjvipon committed Apr 6, 2024
1 parent beaf397 commit 05826c7
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
*/
@Versionized
@XmirObject(oname = "cage.encaged.encage")
class EOcage$EOencaged$EOencage extends PhDefault implements Atom {
final class EOcage$EOencaged$EOencage extends PhDefault implements Atom {
/**
* Ctor.
* @param sigma Sigma
Expand All @@ -55,7 +55,7 @@ class EOcage$EOencaged$EOencage extends PhDefault implements Atom {

@Override
public Phi lambda() throws Exception {
Cages.INSTANCE.get().encage(
Cages.INSTANCE.encage(
Math.toIntExact(
new Param(this.take(Attr.RHO), "locator").strong(Long.class)
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,32 @@
import org.eolang.Data;
import org.eolang.Param;
import org.eolang.PhDefault;
import org.eolang.PhTracedLocator;
import org.eolang.Phi;
import org.eolang.Versionized;
import org.eolang.XmirObject;

/**
* Cage.encaged.remove object.
* Cage.encaged.free object.
* @since 0.36.0
*/
@Versionized
@XmirObject(oname = "cage.encaged.remove")
public class EOcage$EOencaged$EOremove extends PhDefault implements Atom {
@XmirObject(oname = "cage.encaged.free")
final class EOcage$EOencaged$EOfree extends PhDefault implements Atom {
/**
* Ctor.
* @param sigma Sigma
*/
EOcage$EOencaged$EOremove(final Phi sigma) {
EOcage$EOencaged$EOfree(final Phi sigma) {
super(sigma);
}

@Override
public Phi lambda() throws Exception {
Cages.INSTANCE.get().remove(
Math.toIntExact(new Param(this.take(Attr.RHO), "locator").strong(Long.class))
final int locator = Math.toIntExact(
new Param(this.take(Attr.RHO), "locator").strong(Long.class)
);
Cages.INSTANCE.remove(locator);
return new Data.ToPhi(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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 org.eolang.Atom;
import org.eolang.Attr;
import org.eolang.Param;
import org.eolang.PhDefault;
import org.eolang.PhTracedLocator;
import org.eolang.Phi;
import org.eolang.Versionized;
import org.eolang.XmirObject;

/**
* Cage.encaged.freed object.
* @since 0.36.0
*/
@Versionized
@XmirObject(oname = "cage.encaged.freed")
final class EOcage$EOencaged$EOfreed extends PhDefault implements Atom {
/**
* Ctor.
* @param sigma Sigma
*/
EOcage$EOencaged$EOfreed(final Phi sigma) {
super(sigma);
}

@Override
public Phi lambda() throws Exception {
final int locator = Math.toIntExact(
new Param(this.take(Attr.RHO), "locator").strong(Long.class)
);
final Phi object = Cages.INSTANCE.get(locator);
Cages.INSTANCE.remove(locator);
return object;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.eolang.Attr;
import org.eolang.Param;
import org.eolang.PhDefault;
import org.eolang.PhTracedLocator;
import org.eolang.Phi;
import org.eolang.Versionized;
import org.eolang.XmirObject;
Expand All @@ -41,7 +42,7 @@
*/
@Versionized
@XmirObject(oname = "cage.encaged.it")
public class EOcage$EOencaged$EOit extends PhDefault implements Atom {
final class EOcage$EOencaged$EOit extends PhDefault implements Atom {
/**
* Ctor.
* @param sigma Sigma
Expand All @@ -52,8 +53,12 @@ public class EOcage$EOencaged$EOit extends PhDefault implements Atom {

@Override
public Phi lambda() throws Exception {
return Cages.INSTANCE.get().get(
Math.toIntExact(new Param(this.take(Attr.RHO), "locator").strong(Long.class))
final int locator = Math.toIntExact(
new Param(this.take(Attr.RHO), "locator").strong(Long.class)
);
return new PhTracedLocator(
Cages.INSTANCE.get(locator),
locator
);
}
}
4 changes: 2 additions & 2 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOφ.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
*/
@Versionized
@XmirObject(oname = "cage.@")
public final class EOcage$EOφ extends PhDefault implements Atom {
final class EOcage$EOφ extends PhDefault implements Atom {
/**
* Ctor.
* @param sigma Sigma
Expand All @@ -57,7 +57,7 @@ public Phi lambda() throws Exception {
encaged.put(
"locator",
new Data.ToPhi(
(long) Cages.INSTANCE.get().encage(
(long) Cages.INSTANCE.init(
this.take(Attr.RHO).take("object")
)
)
Expand Down
2 changes: 1 addition & 1 deletion eo-runtime/src/main/java/EOorg/EOeolang/EOmemory$EOφ.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
*/
@Versionized
@XmirObject(oname = "memory.@")
public final class EOmemory$EOφ extends PhDefault implements Atom {
final class EOmemory$EOφ extends PhDefault implements Atom {
/**
* Ctor.
* @param sigma Sigma
Expand Down
32 changes: 20 additions & 12 deletions eo-runtime/src/test/java/EOorg/EOeolang/EOcageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
import org.junit.jupiter.api.parallel.ExecutionMode;

/**
* Test cases for {@link EOcage} and {@link EOcage$EOnew}.
* Test cases for {@link EOcage} and {@link EOcage$EOφ}.
* @since 0.19
*/
final class EOcageTest {
Expand Down Expand Up @@ -179,10 +179,9 @@ private static void encageTo(final Phi cage, final Phi obj) {
* @return Cage.new object
*/
private static Phi encaged(final Phi obj) {
final Phi cage = Phi.Φ.take("org.eolang.cage");
final Phi encaged = cage.take("new").copy();
encaged.put(0, obj);
return encaged;
final Phi cage = Phi.Φ.take("org.eolang.cage").copy();
cage.put(0, obj);
return cage.take("new");
}

/**
Expand Down Expand Up @@ -212,7 +211,7 @@ void clearDepth() {

@Test
void doesNotThrowIfDataizesConcurrently() {
final Phi cage = Phi.Φ.take("org.eolang.cage").take("new").copy();
final Phi cage = EOcageTest.encaged(new RecursiveDummy());
EOcageTest.encageTo(
cage,
new RecursiveDummy(EOcageTest.RecursionTests.MAX_DEPTH, cage)
Expand Down Expand Up @@ -261,7 +260,9 @@ void rewritesItselfToItselfViaDummy() {

@Test
void throwsExceptionIfRecursion() {
final Phi cage = Phi.Φ.take("org.eolang.cage").take("new").copy();
final Phi cage = EOcageTest.encaged(
Phi.Φ.take("org.eolang.cage").take("encaged")
);
EOcageTest.encageTo(cage, cage);
Assertions.assertThrows(
ExAbstract.class,
Expand All @@ -272,7 +273,7 @@ void throwsExceptionIfRecursion() {

@Test
void doesNotThrowExceptionIfSmallDepth() {
final Phi cage = Phi.Φ.take("org.eolang.cage").take("new").copy();
final Phi cage = EOcageTest.encaged(new RecursiveDummy());
EOcageTest.encageTo(
cage,
new RecursiveDummy(EOcageTest.RecursionTests.MAX_DEPTH / 2, cage)
Expand All @@ -293,7 +294,7 @@ void doesNotThrowExceptionIfSmallDepth() {
*/
@Test
void doesNotThrowExceptionIfMaxDepth() {
final Phi cage = Phi.Φ.take("org.eolang.cage").take("new").copy();
final Phi cage = EOcageTest.encaged(new RecursiveDummy());
EOcageTest.encageTo(
cage,
new RecursiveDummy(EOcageTest.RecursionTests.MAX_DEPTH, cage)
Expand All @@ -311,7 +312,7 @@ void doesNotThrowExceptionIfMaxDepth() {

@Test
void throwsExceptionIfBigDepth() {
final Phi cage = Phi.Φ.take("org.eolang.cage").take("new").copy();
final Phi cage = EOcageTest.encaged(new RecursiveDummy());
EOcageTest.encageTo(
cage,
new RecursiveDummy(EOcageTest.RecursionTests.MAX_DEPTH + 1, cage)
Expand All @@ -337,7 +338,7 @@ private final class RecursiveDummy extends PhDefault implements Atom {
/**
* How many times should we met the cage while dataizing it eventually.
*/
private final int depth;
private final Integer depth;

/**
* The cage.
Expand All @@ -349,12 +350,19 @@ private final class RecursiveDummy extends PhDefault implements Atom {
*/
private final ThreadLocal<Integer> counter;

/**
* Ctor for initialize cage.
*/
RecursiveDummy() {
this(null, null);
}

/**
* Ctor.
* @param depth Depth.
* @param cage Cage.
*/
RecursiveDummy(final int depth, final Phi cage) {
RecursiveDummy(final Integer depth, final Phi cage) {
this.depth = depth;
this.cage = cage;
this.counter = ThreadLocal.withInitial(() -> 0);
Expand Down

0 comments on commit 05826c7

Please sign in to comment.