Skip to content

Commit

Permalink
Merge pull request #2617 from darkdrag00nv2/charutf8
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent authored Jun 29, 2023
2 parents 5ff23cf + 7555576 commit 5f84e78
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
4 changes: 4 additions & 0 deletions runtime/interpreter/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,10 @@ func (v CharacterValue) GetMember(interpreter *Interpreter, _ LocationRange, nam
)
},
)

case sema.CharacterTypeUtf8FieldName:
common.UseMemory(interpreter, common.NewBytesMemoryUsage(len(v)))
return ByteSliceToByteArrayValue(interpreter, []byte(v))
}
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions runtime/sema/character.cdc
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

pub struct Character: Storable, Equatable, Comparable, Exportable, Importable {

/// The byte array of the UTF-8 encoding
pub let utf8: [UInt8]

/// Returns this character as a String
pub fun toString(): String
}
18 changes: 18 additions & 0 deletions runtime/sema/character.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

assert.IsType(t, &sema.InvalidCharacterLiteralError{}, errs[0])
}

func TestCheckCharacterUtf8Field(t *testing.T) {

t.Parallel()

checker, err := ParseAndCheck(t, `
let a: Character = "a"
let x = a.utf8
`)

require.NoError(t, err)

assert.Equal(t,
sema.ByteArrayType,
RequireGlobalValue(t, checker.Elaboration, "x"),
)
}
77 changes: 77 additions & 0 deletions runtime/tests/interpreter/character_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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 interpreter_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/interpreter"
. "github.com/onflow/cadence/runtime/tests/utils"
)

func TestInterpretCharacterUtf8Field(t *testing.T) {

t.Parallel()

runTest := func(t *testing.T, code string, expectedValues ...interpreter.Value) {
inter := parseCheckAndInterpret(t, fmt.Sprintf(`
fun test(): [UInt8] {
let c: Character = "%s"
return c.utf8
}
`, code))

result, err := inter.Invoke("test")
require.NoError(t, err)

RequireValuesEqual(
t,
inter,
interpreter.NewArrayValue(
inter,
interpreter.EmptyLocationRange,
interpreter.VariableSizedStaticType{
Type: interpreter.PrimitiveStaticTypeUInt8,
},
common.ZeroAddress,
expectedValues...,
),
result,
)
}

runTest(t, `a`, interpreter.NewUnmeteredUInt8Value(97))
runTest(t, `F`, interpreter.NewUnmeteredUInt8Value(70))
runTest(t, `\u{1F490}`,
interpreter.NewUnmeteredUInt8Value(240),
interpreter.NewUnmeteredUInt8Value(159),
interpreter.NewUnmeteredUInt8Value(146),
interpreter.NewUnmeteredUInt8Value(144),
)
runTest(t, "👪",
interpreter.NewUnmeteredUInt8Value(240),
interpreter.NewUnmeteredUInt8Value(159),
interpreter.NewUnmeteredUInt8Value(145),
interpreter.NewUnmeteredUInt8Value(170),
)
}

0 comments on commit 5f84e78

Please sign in to comment.