Skip to content

Commit

Permalink
Merge branch 'master' into fxamacker/track-resource-with-valueid
Browse files Browse the repository at this point in the history
  • Loading branch information
fxamacker committed Jul 7, 2023
2 parents f58cd3e + 9f9c5fd commit 1442291
Show file tree
Hide file tree
Showing 15 changed files with 373 additions and 344 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/compatibility-check-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,5 @@ jobs:
# Check Diff

- name: Check diff
working-directory: ./tools/compatibility-check
run: |
go run ./cmd/check_diff/main.go ../../tmp/output-old.txt ../../tmp/output-new.txt
diff -u --color ./tmp/output-old.txt ./tmp/output-new.txt
10 changes: 7 additions & 3 deletions runtime/ast/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package ast
import (
"encoding/json"

"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/errors"
)

Expand Down Expand Up @@ -58,9 +59,12 @@ var BasicAccesses = []Access{
AccessPublicSettable,
}

var AllAccesses = append(BasicAccesses[:],
AccessContract,
AccessAccount,
var AllAccesses = common.Concat(
BasicAccesses,
[]Access{
AccessContract,
AccessAccount,
},
)

func (a Access) Keyword() string {
Expand Down
36 changes: 36 additions & 0 deletions runtime/common/concat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package common

func Concat[T any](slices ...[]T) []T {
var length int

for _, slice := range slices {
length += len(slice)
}

result := make([]T, length)

var offset int
for _, slice := range slices {
offset += copy(result[offset:], slice)
}

return result
}
92 changes: 49 additions & 43 deletions runtime/interpreter/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -2540,8 +2540,12 @@ func (v *ArrayValue) Equal(interpreter *Interpreter, locationRange LocationRange
return true
}

func (v *ArrayValue) Storable(_ atree.SlabStorage, _ atree.Address, _ uint64) (atree.Storable, error) {
return atree.StorageIDStorable(v.StorageID()), nil
func (v *ArrayValue) Storable(
storage atree.SlabStorage,
address atree.Address,
maxInlineSize uint64,
) (atree.Storable, error) {
return v.array.Storable(storage, address, maxInlineSize)
}

func (v *ArrayValue) IsReferenceTrackedResourceKindedValue() {}
Expand Down Expand Up @@ -2583,11 +2587,10 @@ func (v *ArrayValue) Transfer(
}

currentStorageID := v.StorageID()
currentAddress := currentStorageID.Address

array := v.array

needsStoreTo := address != currentAddress
needsStoreTo := v.NeedsStoreTo(address)
isResourceKinded := v.IsResourceKinded(interpreter)

if needsStoreTo || !isResourceKinded {
Expand Down Expand Up @@ -2694,7 +2697,7 @@ func (v *ArrayValue) Clone(interpreter *Interpreter) Value {

array, err := atree.NewArrayFromBatchData(
config.Storage,
v.StorageID().Address,
v.StorageAddress(),
v.array.Type(),
func() (atree.Value, error) {
value, err := iterator.Next()
Expand Down Expand Up @@ -2760,8 +2763,12 @@ func (v *ArrayValue) StorageID() atree.StorageID {
return v.array.StorageID()
}

func (v *ArrayValue) StorageAddress() atree.Address {
return v.array.Address()
}

func (v *ArrayValue) GetOwner() common.Address {
return common.Address(v.StorageID().Address)
return common.Address(v.StorageAddress())
}

func (v *ArrayValue) SemaType(interpreter *Interpreter) sema.ArrayType {
Expand All @@ -2773,7 +2780,7 @@ func (v *ArrayValue) SemaType(interpreter *Interpreter) sema.ArrayType {
}

func (v *ArrayValue) NeedsStoreTo(address atree.Address) bool {
return address != v.StorageID().Address
return address != v.StorageAddress()
}

func (v *ArrayValue) IsResourceKinded(interpreter *Interpreter) bool {
Expand Down Expand Up @@ -2921,11 +2928,7 @@ func getNumberValueMember(interpreter *Interpreter, v NumberValue, name string,
case sema.NumericTypeSaturatingAddFunctionName:
return NewHostFunctionValue(
interpreter,
&sema.FunctionType{
ReturnTypeAnnotation: sema.NewTypeAnnotation(
typ,
),
},
sema.SaturatingArithmeticTypeFunctionTypes[typ],
func(invocation Invocation) Value {
other, ok := invocation.Arguments[0].(NumberValue)
if !ok {
Expand All @@ -2942,11 +2945,7 @@ func getNumberValueMember(interpreter *Interpreter, v NumberValue, name string,
case sema.NumericTypeSaturatingSubtractFunctionName:
return NewHostFunctionValue(
interpreter,
&sema.FunctionType{
ReturnTypeAnnotation: sema.NewTypeAnnotation(
typ,
),
},
sema.SaturatingArithmeticTypeFunctionTypes[typ],
func(invocation Invocation) Value {
other, ok := invocation.Arguments[0].(NumberValue)
if !ok {
Expand All @@ -2963,11 +2962,7 @@ func getNumberValueMember(interpreter *Interpreter, v NumberValue, name string,
case sema.NumericTypeSaturatingMultiplyFunctionName:
return NewHostFunctionValue(
interpreter,
&sema.FunctionType{
ReturnTypeAnnotation: sema.NewTypeAnnotation(
typ,
),
},
sema.SaturatingArithmeticTypeFunctionTypes[typ],
func(invocation Invocation) Value {
other, ok := invocation.Arguments[0].(NumberValue)
if !ok {
Expand All @@ -2984,11 +2979,7 @@ func getNumberValueMember(interpreter *Interpreter, v NumberValue, name string,
case sema.NumericTypeSaturatingDivideFunctionName:
return NewHostFunctionValue(
interpreter,
&sema.FunctionType{
ReturnTypeAnnotation: sema.NewTypeAnnotation(
typ,
),
},
sema.SaturatingArithmeticTypeFunctionTypes[typ],
func(invocation Invocation) Value {
other, ok := invocation.Arguments[0].(NumberValue)
if !ok {
Expand Down Expand Up @@ -15583,7 +15574,7 @@ func (v *CompositeValue) InitializeFunctions(interpreter *Interpreter) {
}

func (v *CompositeValue) OwnerValue(interpreter *Interpreter, locationRange LocationRange) OptionalValue {
address := v.StorageID().Address
address := v.StorageAddress()

if address == (atree.Address{}) {
return NilOptionalValue
Expand Down Expand Up @@ -15695,7 +15686,7 @@ func (v *CompositeValue) SetMember(
}()
}

address := v.StorageID().Address
address := v.StorageAddress()

value = value.Transfer(
interpreter,
Expand Down Expand Up @@ -16022,16 +16013,20 @@ func (v *CompositeValue) IsStorable() bool {
return v.Location != nil
}

func (v *CompositeValue) Storable(_ atree.SlabStorage, _ atree.Address, _ uint64) (atree.Storable, error) {
func (v *CompositeValue) Storable(
storage atree.SlabStorage,
address atree.Address,
maxInlineSize uint64,
) (atree.Storable, error) {
if !v.IsStorable() {
return NonStorable{Value: v}, nil
}

return atree.StorageIDStorable(v.StorageID()), nil
return v.dictionary.Storable(storage, address, maxInlineSize)
}

func (v *CompositeValue) NeedsStoreTo(address atree.Address) bool {
return address != v.StorageID().Address
return address != v.StorageAddress()
}

func (v *CompositeValue) IsResourceKinded(interpreter *Interpreter) bool {
Expand Down Expand Up @@ -16083,11 +16078,11 @@ func (v *CompositeValue) Transfer(
}

currentStorageID := v.StorageID()
currentAddress := currentStorageID.Address
currentAddress := v.StorageAddress()

dictionary := v.dictionary

needsStoreTo := address != currentAddress
needsStoreTo := v.NeedsStoreTo(address)
isResourceKinded := v.IsResourceKinded(interpreter)

if needsStoreTo && v.Kind == common.CompositeKindContract {
Expand Down Expand Up @@ -16250,7 +16245,7 @@ func (v *CompositeValue) Clone(interpreter *Interpreter) Value {

dictionary, err := atree.NewMapFromBatchData(
config.Storage,
v.StorageID().Address,
v.StorageAddress(),
atree.NewDefaultDigesterBuilder(),
v.dictionary.Type(),
StringAtreeValueComparator,
Expand Down Expand Up @@ -16337,7 +16332,7 @@ func (v *CompositeValue) DeepRemove(interpreter *Interpreter) {
}

func (v *CompositeValue) GetOwner() common.Address {
return common.Address(v.StorageID().Address)
return common.Address(v.StorageAddress())
}

// ForEachField iterates over all field-name field-value pairs of the composite value.
Expand All @@ -16360,6 +16355,10 @@ func (v *CompositeValue) StorageID() atree.StorageID {
return v.dictionary.StorageID()
}

func (v *CompositeValue) StorageAddress() atree.Address {
return v.dictionary.Address()
}

func (v *CompositeValue) RemoveField(
interpreter *Interpreter,
_ LocationRange,
Expand Down Expand Up @@ -17531,8 +17530,12 @@ func (v *DictionaryValue) Equal(interpreter *Interpreter, locationRange Location
}
}

func (v *DictionaryValue) Storable(_ atree.SlabStorage, _ atree.Address, _ uint64) (atree.Storable, error) {
return atree.StorageIDStorable(v.StorageID()), nil
func (v *DictionaryValue) Storable(
storage atree.SlabStorage,
address atree.Address,
maxInlineSize uint64,
) (atree.Storable, error) {
return v.dictionary.Storable(storage, address, maxInlineSize)
}

func (v *DictionaryValue) IsReferenceTrackedResourceKindedValue() {}
Expand Down Expand Up @@ -17577,11 +17580,10 @@ func (v *DictionaryValue) Transfer(
}

currentStorageID := v.StorageID()
currentAddress := currentStorageID.Address

dictionary := v.dictionary

needsStoreTo := address != currentAddress
needsStoreTo := v.NeedsStoreTo(address)
isResourceKinded := v.IsResourceKinded(interpreter)

if needsStoreTo || !isResourceKinded {
Expand Down Expand Up @@ -17703,7 +17705,7 @@ func (v *DictionaryValue) Clone(interpreter *Interpreter) Value {

dictionary, err := atree.NewMapFromBatchData(
config.Storage,
v.StorageID().Address,
v.StorageAddress(),
atree.NewDefaultDigesterBuilder(),
v.dictionary.Type(),
valueComparator,
Expand Down Expand Up @@ -17781,13 +17783,17 @@ func (v *DictionaryValue) DeepRemove(interpreter *Interpreter) {
}

func (v *DictionaryValue) GetOwner() common.Address {
return common.Address(v.StorageID().Address)
return common.Address(v.StorageAddress())
}

func (v *DictionaryValue) StorageID() atree.StorageID {
return v.dictionary.StorageID()
}

func (v *DictionaryValue) StorageAddress() atree.Address {
return v.dictionary.Address()
}

func (v *DictionaryValue) SemaType(interpreter *Interpreter) *sema.DictionaryType {
if v.semaType == nil {
// this function will panic already if this conversion fails
Expand All @@ -17797,7 +17803,7 @@ func (v *DictionaryValue) SemaType(interpreter *Interpreter) *sema.DictionaryTyp
}

func (v *DictionaryValue) NeedsStoreTo(address atree.Address) bool {
return address != v.StorageID().Address
return address != v.StorageAddress()
}

func (v *DictionaryValue) IsResourceKinded(interpreter *Interpreter) bool {
Expand Down
10 changes: 6 additions & 4 deletions runtime/literal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,10 +643,12 @@ func TestParseLiteral(t *testing.T) {
)
}

for _, signedIntegerType := range append(
sema.AllSignedIntegerTypes[:],
sema.IntegerType,
sema.SignedIntegerType,
for _, signedIntegerType := range common.Concat(
sema.AllSignedIntegerTypes,
[]sema.Type{
sema.IntegerType,
sema.SignedIntegerType,
},
) {

t.Run(
Expand Down
Loading

0 comments on commit 1442291

Please sign in to comment.