Skip to content

Commit 5f84e78

Browse files
authored
Merge pull request #2617 from darkdrag00nv2/charutf8
2 parents 5ff23cf + 7555576 commit 5f84e78

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

runtime/interpreter/value.go

+4
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,10 @@ func (v CharacterValue) GetMember(interpreter *Interpreter, _ LocationRange, nam
984984
)
985985
},
986986
)
987+
988+
case sema.CharacterTypeUtf8FieldName:
989+
common.UseMemory(interpreter, common.NewBytesMemoryUsage(len(v)))
990+
return ByteSliceToByteArrayValue(interpreter, []byte(v))
987991
}
988992
return nil
989993
}

runtime/sema/character.cdc

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11

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

4+
/// The byte array of the UTF-8 encoding
5+
pub let utf8: [UInt8]
6+
47
/// Returns this character as a String
58
pub fun toString(): String
69
}

runtime/sema/character.gen.go

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

runtime/tests/checker/character_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,20 @@ func TestCheckInvalidCharacterLiteral(t *testing.T) {
5757

5858
assert.IsType(t, &sema.InvalidCharacterLiteralError{}, errs[0])
5959
}
60+
61+
func TestCheckCharacterUtf8Field(t *testing.T) {
62+
63+
t.Parallel()
64+
65+
checker, err := ParseAndCheck(t, `
66+
let a: Character = "a"
67+
let x = a.utf8
68+
`)
69+
70+
require.NoError(t, err)
71+
72+
assert.Equal(t,
73+
sema.ByteArrayType,
74+
RequireGlobalValue(t, checker.Elaboration, "x"),
75+
)
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Cadence - The resource-oriented smart contract programming language
3+
*
4+
* Copyright Dapper Labs, Inc.
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 interpreter_test
20+
21+
import (
22+
"fmt"
23+
"testing"
24+
25+
"github.com/stretchr/testify/require"
26+
27+
"github.com/onflow/cadence/runtime/common"
28+
"github.com/onflow/cadence/runtime/interpreter"
29+
. "github.com/onflow/cadence/runtime/tests/utils"
30+
)
31+
32+
func TestInterpretCharacterUtf8Field(t *testing.T) {
33+
34+
t.Parallel()
35+
36+
runTest := func(t *testing.T, code string, expectedValues ...interpreter.Value) {
37+
inter := parseCheckAndInterpret(t, fmt.Sprintf(`
38+
fun test(): [UInt8] {
39+
let c: Character = "%s"
40+
return c.utf8
41+
}
42+
`, code))
43+
44+
result, err := inter.Invoke("test")
45+
require.NoError(t, err)
46+
47+
RequireValuesEqual(
48+
t,
49+
inter,
50+
interpreter.NewArrayValue(
51+
inter,
52+
interpreter.EmptyLocationRange,
53+
interpreter.VariableSizedStaticType{
54+
Type: interpreter.PrimitiveStaticTypeUInt8,
55+
},
56+
common.ZeroAddress,
57+
expectedValues...,
58+
),
59+
result,
60+
)
61+
}
62+
63+
runTest(t, `a`, interpreter.NewUnmeteredUInt8Value(97))
64+
runTest(t, `F`, interpreter.NewUnmeteredUInt8Value(70))
65+
runTest(t, `\u{1F490}`,
66+
interpreter.NewUnmeteredUInt8Value(240),
67+
interpreter.NewUnmeteredUInt8Value(159),
68+
interpreter.NewUnmeteredUInt8Value(146),
69+
interpreter.NewUnmeteredUInt8Value(144),
70+
)
71+
runTest(t, "👪",
72+
interpreter.NewUnmeteredUInt8Value(240),
73+
interpreter.NewUnmeteredUInt8Value(159),
74+
interpreter.NewUnmeteredUInt8Value(145),
75+
interpreter.NewUnmeteredUInt8Value(170),
76+
)
77+
}

0 commit comments

Comments
 (0)