Skip to content

Commit fad8e94

Browse files
committed
Refactoring of lookups via parent accounts
1 parent 640c769 commit fad8e94

File tree

4 files changed

+40
-33
lines changed

4 files changed

+40
-33
lines changed

convex-core/src/main/java/convex/core/data/AccountStatus.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class AccountStatus extends ARecord {
5252
private static final int HAS_METADATA=1<<FORMAT.indexFor(Keywords.METADATA);
5353
private static final int HAS_PARENT=1<<FORMAT.indexFor(Keywords.PARENT);
5454

55-
protected static final int INCLUSION_MASK=0xff;
55+
protected static final int INCLUSION_MASK=0x01ff;
5656

5757
private AccountStatus(long sequence, AccountKey publicKey, long balance,
5858
long memory,

convex-core/src/main/java/convex/core/lang/Compiler.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,11 @@ private static Context compileSymbol(Symbol sym, Context context) {
198198
}
199199

200200
// Regular symbol lookup in environment
201-
Address address=context.getAddress();
202-
return compileEnvSymbol(address,sym,context);
201+
return compileEnvSymbol(sym,context);
203202
}
204203

205-
private static Context compileEnvSymbol(Address address,Symbol sym, Context context) {
206-
// Optional code for :static embedding
204+
private static Context compileEnvSymbol(Symbol sym, Context context) {
205+
Address address=context.getAddress();// Optional code for :static embedding
207206
if (Constants.OPT_STATIC) {
208207
// Get metadata for symbol.
209208
AHashMap<ACell, ACell> meta=context.lookupMeta(sym);

convex-core/src/main/java/convex/core/lang/Context.java

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -700,28 +700,32 @@ public MapEntry<Symbol,ACell> lookupDynamicEntry(Address address,Symbol sym) {
700700
return lookupDynamicEntry(as,sym);
701701
}
702702

703-
704-
705703
private MapEntry<Symbol,ACell> lookupDynamicEntry(AccountStatus as,Symbol sym) {
706704
// Get environment for Address, or default to initial environment
707-
AHashMap<Symbol, ACell> env = (as==null)?Core.ENVIRONMENT:as.getEnvironment();
708-
709-
710-
MapEntry<Symbol,ACell> result=env.getEntry(sym);
705+
for (int i=0; i<16; i++) {
706+
if (as==null) return Core.ENVIRONMENT.getEntry(sym);
711707

712-
if (result==null) {
713-
AccountStatus aliasAccount=getAliasedAccount(env);
714-
result = lookupAliasedEntry(aliasAccount,sym);
708+
MapEntry<Symbol,ACell> result=as.getEnvironment().getEntry(sym);
709+
if (result!=null) return result;
710+
711+
Address parent=as.getParent();
712+
as=(parent==null)?null:getAccountStatus(parent);
715713
}
716-
return result;
714+
return null;
717715
}
718-
719-
private MapEntry<Symbol,ACell> lookupAliasedEntry(AccountStatus as,Symbol sym) {
720-
if (as==null) return null;
721-
AHashMap<Symbol, ACell> env = as.getEnvironment();
722-
return env.getEntry(sym);
716+
717+
/**
718+
* Looks up the account for an Symbol alias in the given environment.
719+
* @param env
720+
* @param path An alias path
721+
* @return AccountStatus for the alias, or null if not present
722+
*/
723+
private AccountStatus getAliasedAccount(AHashMap<Symbol, ACell> env) {
724+
// TODO: alternative core accounts
725+
return getCoreAccount();
723726
}
724727

728+
725729
/**
726730
* Gets the account status for the current Address
727731
*
@@ -736,16 +740,6 @@ public AccountStatus getAccountStatus() {
736740
return chainState.state.getAccount(a);
737741
}
738742

739-
/**
740-
* Looks up the account for an Symbol alias in the given environment.
741-
* @param env
742-
* @param path An alias path
743-
* @return AccountStatus for the alias, or null if not present
744-
*/
745-
private AccountStatus getAliasedAccount(AHashMap<Symbol, ACell> env) {
746-
// TODO: alternative core accounts
747-
return getCoreAccount();
748-
}
749743

750744
private AccountStatus getCoreAccount() {
751745
return getState().getAccount(Core.CORE_ADDRESS);

convex-core/src/test/java/convex/core/lang/CoreTest.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3007,11 +3007,25 @@ public void testDeploy() {
30073007

30083008
@Test
30093009
public void testParent() {
3010-
Context ctx = exec(context(),"(def dad (deploy `(defn resolve [x] :foo) `(set-controller ~*address*)))");
3010+
Context ctx = context();
3011+
3012+
// Create parent actor
3013+
ctx = exec(ctx,"(def dad (deploy `(defn baz [x] :foo) `(set-controller ~*address*) `(def CONST ^:static :bar)))");
3014+
Address dad=ctx.getResult();
3015+
3016+
// create a child actor
3017+
ctx=exec(ctx,("(def son (deploy `(set-parent ~dad) `(set-controller ~*address*)))"));
3018+
3019+
// call a function in parent
3020+
assertEquals(Keywords.FOO,eval(ctx,"(query-as son `(baz 'convex.core))"));
3021+
3022+
assertEquals(Keywords.FOO,eval(ctx,"(son/baz :doesnt-matter)"));
30113023

3012-
ctx=exec(ctx,("(def son (deploy `(set-parent ~dad)))"));
3024+
// *parent* is correctly set
3025+
assertEquals(dad,eval(ctx,"(query-as son '*parent*)"));
30133026

3014-
// assertEquals(Keywords.FOO,eval(ctx,"(query-as son '(resolve convex.core))"));
3027+
// compilation of constants from parent
3028+
assertEquals(Constant.of(Keywords.BAR),eval(ctx,"(query-as son '(compile CONST))"));
30153029
}
30163030

30173031
@Test

0 commit comments

Comments
 (0)