Skip to content

Commit

Permalink
revert 393e955: use resolve location to rewrite import
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Oct 18, 2024
1 parent 02b9413 commit 2e72919
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 27 deletions.
32 changes: 8 additions & 24 deletions runtime/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -1092,40 +1092,24 @@ func (e *interpreterEnvironment) newCompositeValueFunctionsHandler() interpreter
func (e *interpreterEnvironment) loadContract(
inter *interpreter.Interpreter,
compositeType *sema.CompositeType,
constructorGenerator func(common.Address) *interpreter.HostFunctionValue,
invocationRange ast.Range,
_ func(common.Address) *interpreter.HostFunctionValue,
_ ast.Range,
) *interpreter.CompositeValue {

var contractValue interpreter.Value

location := compositeType.Location
switch location := location.(type) {
case common.IdentifierLocation:
// Identifier locations are used for built-in contracts,
// and are not stored in storage

constructorGenerator := constructorGenerator(common.ZeroAddress)
var err error
contractValue, err = inter.InvokeFunctionValue(
constructorGenerator,
nil,
nil,
nil,
compositeType,
invocationRange,
)
if err != nil {
panic(err)
}

case common.AddressLocation:
if addressLocation, ok := location.(common.AddressLocation); ok {
storageMap := e.storage.GetStorageMap(
location.Address,
addressLocation.Address,
StorageDomainContract,
false,
)
if storageMap != nil {
contractValue = storageMap.ReadValue(inter, interpreter.StringStorageMapKey(location.Name))
contractValue = storageMap.ReadValue(
inter,
interpreter.StringStorageMapKey(addressLocation.Name),
)
}
}

Expand Down
13 changes: 10 additions & 3 deletions tests/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11561,14 +11561,19 @@ func TestResultRedeclared(t *testing.T) {
})

}
func TestRuntimeContractValueLocation(t *testing.T) {

func TestRuntimeIdentifierLocationToAddressLocationRewrite(t *testing.T) {

t.Parallel()

signerAddress := common.MustBytesToAddress([]byte{0x42})

const fooContractName = "Foo"
fooIdentifierLocation := common.IdentifierLocation(fooContractName)
fooAddressLocation := common.AddressLocation{
Address: signerAddress,
Name: fooContractName,
}

fooContract := []byte(`
access(all)
Expand All @@ -11593,6 +11598,9 @@ func TestRuntimeContractValueLocation(t *testing.T) {
assert.Empty(t, identifiers)
assert.Equal(t, fooIdentifierLocation, location)

// NOTE: rewrite identifier location to address location
location = fooAddressLocation

return []ResolvedLocation{
{
Location: location,
Expand All @@ -11612,8 +11620,7 @@ func TestRuntimeContractValueLocation(t *testing.T) {
return nil
},
OnGetCode: func(location Location) ([]byte, error) {
assert.Equal(t, fooIdentifierLocation, location)
return fooContract, nil
return nil, errors.New("GetCode should not be called")
},
}

Expand Down

0 comments on commit 2e72919

Please sign in to comment.