Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add address field to capabilities #736

Merged
merged 8 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions runtime/interpreter/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -7070,6 +7070,9 @@ func (v CapabilityValue) GetMember(inter *Interpreter, _ func() LocationRange, n
}
return inter.capabilityCheckFunction(v.Address, v.Path, borrowType)


case "address":
turbolent marked this conversation as resolved.
Show resolved Hide resolved
turbolent marked this conversation as resolved.
Show resolved Hide resolved
return v.Address
}

return nil
Expand Down
15 changes: 15 additions & 0 deletions runtime/sema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -6527,6 +6527,10 @@ const capabilityTypeCheckFunctionDocString = `
Returns true if the capability currently targets an object that satisfies the given type, i.e. could be borrowed using the given type
`

const addressTypeCheckFunctionDocString = `
Returns the address for a capability
turbolent marked this conversation as resolved.
Show resolved Hide resolved
`

func (t *CapabilityType) GetMembers() map[string]MemberResolver {
return withBuiltinMembers(t, map[string]MemberResolver{
"borrow": {
Expand All @@ -6551,6 +6555,17 @@ func (t *CapabilityType) GetMembers() map[string]MemberResolver {
)
},
},
"address": {
Kind: common.DeclarationKindField,
Resolve: func(identifier string, _ ast.Range, _ func(error)) *Member {
return NewPublicConstantFieldMember(
t,
identifier,
&AddressType{},
addressTypeCheckFunctionDocString,
)
},
},
})
}

Expand Down
17 changes: 17 additions & 0 deletions runtime/tests/checker/capability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,20 @@ func TestCheckCapability_check(t *testing.T) {
})
})
}

func TestCheckCapability_address(t *testing.T) {

t.Parallel()
t.Run("check address", func(t *testing.T) {
checker, err := ParseAndCheckWithPanic(t,
`
let capability: Capability = panic("")
let addr = capability.address
`,
)
require.NoError(t, err)

addrType := RequireGlobalValue(t, checker.Elaboration, "addr")
require.Equal(t, &sema.AddressType{}, addrType)
})
}
49 changes: 49 additions & 0 deletions runtime/tests/interpreter/capability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,3 +657,52 @@ func TestInterpretCapability_check(t *testing.T) {
})
})
}

func TestInterpretCapability_address(t *testing.T) {

t.Parallel()

t.Run("check address", func(t *testing.T) {

t.Parallel()

inter, _ := testAccount(
t,
true,
`
fun single(): Address {
return account.getCapability(/public/single).address
}

fun double(): Address {
return account.getCapability(/public/double).address
}

fun nonExistent() : Address {
return account.getCapability(/public/nonExistent).address
}
`,
)

t.Run("single", func(t *testing.T) {
value, err := inter.Invoke("single")
require.NoError(t, err)

require.IsType(t, interpreter.AddressValue{}, value)
})

t.Run("double", func(t *testing.T) {
value, err := inter.Invoke("double")
require.NoError(t, err)

require.IsType(t, interpreter.AddressValue{}, value)
})

t.Run("nonExistent", func(t *testing.T) {
value, err := inter.Invoke("nonExistent")
require.NoError(t, err)

require.IsType(t, interpreter.AddressValue{}, value)
})
})
turbolent marked this conversation as resolved.
Show resolved Hide resolved
}