Skip to content

Commit

Permalink
Better handling for parent environment lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Sep 28, 2024
1 parent d9a8199 commit 4b75ec2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 46 deletions.
50 changes: 13 additions & 37 deletions convex-core/src/main/java/convex/core/lang/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -565,18 +565,7 @@ public Context lookupDynamic(Address address, Symbol symbol) {
* @return Metadata for given symbol (may be empty) or null if undeclared
*/
public AHashMap<ACell,ACell> lookupMeta(Symbol sym) {
AHashMap<Symbol, ACell> env=getEnvironment();
if ((env!=null)&&env.containsKey(sym)) {
return getMetadata().get(sym,Maps.empty());
}
AccountStatus as = getAliasedAccount(env);
if (as==null) return null;

env=as.getEnvironment();
if (env.containsKey(sym)) {
return as.getMetadata().get(sym,Maps.empty());
}
return null;
return lookupMeta(getAddress(),sym);
}

/**
Expand All @@ -586,12 +575,16 @@ public AHashMap<ACell,ACell> lookupMeta(Symbol sym) {
* @return Metadata for given symbol (may be empty) or null if undeclared
*/
public AHashMap<ACell,ACell> lookupMeta(Address address,Symbol sym) {
if (address==null) return lookupMeta(sym);
AccountStatus as=getAccountStatus(address);
if (as==null) return null;
AHashMap<Symbol, ACell> env=as.getEnvironment();
if (env.containsKey(sym)) {
return as.getMetadata().get(sym,Maps.empty());
AccountStatus as=(address==null)?getAccountStatus():getAccountStatus(address);
for (int i=0; i<16; i++) {
if (as==null) return null;
AHashMap<Symbol, ACell> env=as.getEnvironment();
if (env.containsKey(sym)) {
return as.getMetadata().get(sym,Maps.empty());
}
address=getParentAddress(as);
if (address==null) return null;
as=getAccountStatus(address);
}
return null;
}
Expand Down Expand Up @@ -685,23 +678,11 @@ private MapEntry<Symbol,ACell> lookupDynamicEntry(AccountStatus as,Symbol sym) {
MapEntry<Symbol,ACell> result=as.getEnvironment().getEntry(sym);
if (result!=null) return result;

Address parent=as.getParent();
as=(parent==null)?null:getAccountStatus(parent);
Address parent=getParentAddress(as);
as=getAccountStatus(parent); // if not found, will be null
}
return null;
}

/**
* Looks up the account for an Symbol alias in the given environment.
* @param env
* @param path An alias path
* @return AccountStatus for the alias, or null if not present
*/
private AccountStatus getAliasedAccount(AHashMap<Symbol, ACell> env) {
// TODO: alternative core accounts
return getCoreAccount();
}


/**
* Gets the account status for the current Address
Expand All @@ -717,11 +698,6 @@ public AccountStatus getAccountStatus() {
return chainState.state.getAccount(a);
}


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

/**
* Gets the holdings map for the current account.
* @return Map of holdings, or null if the current account does not exist.
Expand Down
2 changes: 1 addition & 1 deletion convex-core/src/test/java/convex/core/lang/ACVMTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected Context context() {
}

/**
* Builds the Context for this test class instance. Subclasses may override
* Builds the base Context for this test class instance. Subclasses may override
* to generate a separate context
* @param ctx Context to modify
* @return
Expand Down
4 changes: 2 additions & 2 deletions convex-core/src/test/java/convex/core/lang/CompilerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -854,12 +854,12 @@ public void testStaticCompilation() {

// Constant addresses should also get static compilation
assertEquals(Constant.of(Core.TRANSFER), eval("(compile '#8/transfer)"));
assertEquals(Constant.of(Core.COUNT), eval("(compile '#1/count)"));
} else {
assertEquals(Lookup.create(Address.create(8), Symbols.COUNT), eval("(compile 'count)"));
}

// Aliases that don't hit static definitions compile to dynamic lookup
assertEquals(Lookup.create(Address.create(1), Symbols.COUNT), eval("(compile '#1/count)"));
// Aliases that don't hit real accounts compile to dynamic lookup
assertEquals(Lookup.create(Address.create(8888), Symbols.TRANSFER), eval("(compile '#8888/transfer)"));
}

Expand Down
3 changes: 3 additions & 0 deletions convex-core/src/test/java/convex/core/lang/ContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@

/**
* Tests for basic execution Context mechanics and internals
*
*/
public class ContextTest extends ACVMTest {

// TODO: should probably make independent of base state init
protected ContextTest() {
super(BaseTest.STATE);
}
Expand Down
9 changes: 3 additions & 6 deletions convex-core/src/test/java/convex/core/lang/CoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2670,9 +2670,7 @@ public void testLookupSyntax() {
AHashMap<ACell,ACell> countMeta=Core.METADATA.get(Symbols.COUNT);
assertSame(countMeta, (eval("(lookup-meta 'count)")));
assertSame(countMeta, (eval("(lookup-meta "+Init.CORE_ADDRESS+ " 'count)")));

// Not actually defined in current address
assertNull(eval("(lookup-meta *address* 'count)"));
assertSame(countMeta, eval("(lookup-meta *address* 'count)"));

assertNull(eval("(lookup-meta 'non-existent-symbol)"));
assertNull(eval("(lookup-meta #666666 'count)")); // invalid address
Expand Down Expand Up @@ -4265,11 +4263,10 @@ public void testDefinedQ() {
assertTrue(evalB("(do (def foobar [2 3]) (defined? 'foobar))"));
assertTrue(evalB("(do (def foobar [2 3]) (defined? *address* 'foobar))"));

// Note: defined by reference to core. TODO: is this OK?
assertTrue(evalB("(defined? 'count)"));

// Not defined in explicit environment. TODO: is this OK?
assertFalse(evalB("(defined? *address* 'count)"));
// Not defined in explicit environment, but indirectly by core. TODO: is this OK?
assertTrue(evalB("(defined? *address* 'count)"));

// invalid names
assertCastError(step("(defined? :count)")); // not a Symbol
Expand Down

0 comments on commit 4b75ec2

Please sign in to comment.