|
| 1 | +/* |
| 2 | + * Cadence - The resource-oriented smart contract programming language |
| 3 | + * |
| 4 | + * Copyright Flow Foundation |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package stdlib |
| 20 | + |
| 21 | +//go:generate go run ../sema/gen -p stdlib ccf.cdc ccf.gen.go |
| 22 | + |
| 23 | +import ( |
| 24 | + "github.com/onflow/cadence" |
| 25 | + "github.com/onflow/cadence/common" |
| 26 | + "github.com/onflow/cadence/encoding/ccf" |
| 27 | + "github.com/onflow/cadence/errors" |
| 28 | + "github.com/onflow/cadence/interpreter" |
| 29 | +) |
| 30 | + |
| 31 | +type Exporter interface { |
| 32 | + ExportValue( |
| 33 | + value interpreter.Value, |
| 34 | + interpreter *interpreter.Interpreter, |
| 35 | + locationRange interpreter.LocationRange, |
| 36 | + ) ( |
| 37 | + cadence.Value, |
| 38 | + error, |
| 39 | + ) |
| 40 | +} |
| 41 | + |
| 42 | +type CCFContractHandler interface { |
| 43 | + Exporter |
| 44 | +} |
| 45 | + |
| 46 | +// newCCFEncodeFunction creates a new host function that encodes a value using the CCF encoding format. |
| 47 | +func newCCFEncodeFunction( |
| 48 | + gauge common.MemoryGauge, |
| 49 | + handler CCFContractHandler, |
| 50 | +) *interpreter.HostFunctionValue { |
| 51 | + return interpreter.NewStaticHostFunctionValue( |
| 52 | + gauge, |
| 53 | + CCFTypeEncodeFunctionType, |
| 54 | + func(invocation interpreter.Invocation) interpreter.Value { |
| 55 | + inter := invocation.Interpreter |
| 56 | + locationRange := invocation.LocationRange |
| 57 | + |
| 58 | + referenceValue, ok := invocation.Arguments[0].(interpreter.ReferenceValue) |
| 59 | + if !ok { |
| 60 | + panic(errors.NewUnreachableError()) |
| 61 | + } |
| 62 | + |
| 63 | + referencedValue := referenceValue.ReferencedValue(inter, locationRange, true) |
| 64 | + if referencedValue == nil { |
| 65 | + return interpreter.Nil |
| 66 | + } |
| 67 | + |
| 68 | + exportedValue, err := handler.ExportValue(*referencedValue, inter, locationRange) |
| 69 | + if err != nil { |
| 70 | + return interpreter.Nil |
| 71 | + } |
| 72 | + |
| 73 | + encoded, err := ccf.Encode(exportedValue) |
| 74 | + if err != nil { |
| 75 | + return interpreter.Nil |
| 76 | + } |
| 77 | + |
| 78 | + res := interpreter.ByteSliceToByteArrayValue(inter, encoded) |
| 79 | + |
| 80 | + return interpreter.NewSomeValueNonCopying(inter, res) |
| 81 | + }, |
| 82 | + ) |
| 83 | +} |
| 84 | + |
| 85 | +var CCFTypeStaticType = interpreter.ConvertSemaToStaticType(nil, CCFType) |
| 86 | + |
| 87 | +func NewCCFContract( |
| 88 | + gauge common.MemoryGauge, |
| 89 | + handler CCFContractHandler, |
| 90 | +) StandardLibraryValue { |
| 91 | + |
| 92 | + ccfContractFields := map[string]interpreter.Value{ |
| 93 | + CCFTypeEncodeFunctionName: newCCFEncodeFunction(gauge, handler), |
| 94 | + } |
| 95 | + |
| 96 | + var ccfContractValue = interpreter.NewSimpleCompositeValue( |
| 97 | + gauge, |
| 98 | + CCFType.ID(), |
| 99 | + CCFTypeStaticType, |
| 100 | + nil, |
| 101 | + ccfContractFields, |
| 102 | + nil, |
| 103 | + nil, |
| 104 | + nil, |
| 105 | + ) |
| 106 | + |
| 107 | + return StandardLibraryValue{ |
| 108 | + Name: CCFTypeName, |
| 109 | + Type: CCFType, |
| 110 | + Value: ccfContractValue, |
| 111 | + Kind: common.DeclarationKindContract, |
| 112 | + } |
| 113 | +} |
0 commit comments