Skip to content

Commit

Permalink
Add *env* special and test for empty initial actor environment
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Mar 5, 2024
1 parent 8984bcd commit 22909f4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions convex-core/src/main/java/convex/core/lang/Symbols.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ public class Symbols {
public static final Symbol STAR_SEQUENCE = intern("*sequence*");
public static final Symbol STAR_KEY = intern("*key*");
public static final Symbol STAR_SCOPE = intern("*scope*");
public static final Symbol STAR_ENV= intern("*env*");

public static final Symbol STAR_LANG = intern("*lang*");

Expand Down
5 changes: 4 additions & 1 deletion convex-core/src/main/java/convex/core/lang/ops/Special.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Special<T extends ACell> extends AOp<T> {

private final byte opCode;

private static int NUM_SPECIALS=18;
private static int NUM_SPECIALS=19;
private static final int BASE=Ops.SPECIAL_BASE;
private static final int LIMIT=BASE+NUM_SPECIALS;
public static final Symbol[] SYMBOLS=new Symbol[NUM_SPECIALS];
Expand All @@ -48,6 +48,7 @@ public class Special<T extends ACell> extends AOp<T> {
private static final byte S_SCOPE=BASE+15;
private static final byte S_JUICE_LIMIT=BASE+16;
private static final byte S_CONTROLLER=BASE+17;
private static final byte S_ENV=BASE+18;

static {
reg(S_JUICE,Symbols.STAR_JUICE);
Expand All @@ -68,6 +69,7 @@ public class Special<T extends ACell> extends AOp<T> {
reg(S_SCOPE,Symbols.STAR_SCOPE);
reg(S_JUICE_LIMIT,Symbols.STAR_JUICE_LIMIT);
reg(S_CONTROLLER,Symbols.STAR_CONTROLLER);
reg(S_ENV,Symbols.STAR_ENV);
}

private static byte reg(byte opCode, Symbol sym) {
Expand Down Expand Up @@ -116,6 +118,7 @@ public Context execute(Context ctx) {
case S_JUICE_LIMIT: ctx= ctx.withResult(CVMLong.create(ctx.getJuiceLimit())); break;
case S_SCOPE: ctx= ctx.withResult(ctx.getScope()); break;
case S_CONTROLLER: ctx= ctx.withResult(ctx.getAccountStatus().getController()); break;
case S_ENV: ctx= ctx.withResult(ctx.getEnvironment()); break;
default:
throw new Error("Bad Opcode"+opCode);
}
Expand Down
18 changes: 16 additions & 2 deletions convex-core/src/test/java/convex/core/lang/ActorTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package convex.core.lang;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;

import org.junit.jupiter.api.Test;

import convex.core.Coin;
import convex.core.data.Address;
import convex.core.data.Maps;
import convex.core.data.prim.CVMLong;

import static convex.test.Assertions.*;
Expand All @@ -16,8 +20,7 @@ public class ActorTest extends ACVMTest {
public void testScopedCall() {
Context c=context();

c=step(c,"(def a1 (deploy '(defn ^:callable? check [] [*address* :s *scope*])))");
assertNotError(c);
c=exec(c,"(def a1 (deploy '(defn ^:callable? check [] [*address* :s *scope*])))");

assertEquals(eval(c,"[a1 :s :foo]"),eval(c,"(call [a1 :foo] (check))"));
assertEquals(eval(c,"[a1 :s nil]"),eval(c,"(call a1 (check))"));
Expand All @@ -33,6 +36,17 @@ public void testScopedCall() {
assertEquals(eval(c,"[:foo [a1 :s :bar] :foo]"),eval(c,"(call [a2 :foo] (nest [a1 :bar]))"));
}

@Test
public void testEnvironment() {
Context c=context();
c=exec(c,"(def A (deploy `(set-controller ~*address*)))");

Address A=c.getResult();
assertNotNull(A);

assertSame(Maps.empty(),eval(c,"(eval-as A '*env*)"));
}

@Test
public void testBadScopedCalls() {
Context c=context();
Expand Down

0 comments on commit 22909f4

Please sign in to comment.