Skip to content

Commit

Permalink
Merge pull request #2750 from onflow/bastian/2748-statictype-typeid
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent authored Aug 29, 2023
2 parents bd3056f + c29c99e commit c5ea847
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 73 deletions.
2 changes: 1 addition & 1 deletion runtime/interpreter/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ func (e UUIDUnavailableError) Error() string {

// TypeLoadingError
type TypeLoadingError struct {
TypeID common.TypeID
TypeID TypeID
}

var _ errors.UserError = TypeLoadingError{}
Expand Down
30 changes: 15 additions & 15 deletions runtime/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ type PublicAccountHandlerFunc func(
type UUIDHandlerFunc func() (uint64, error)

// CompositeTypeHandlerFunc is a function that loads composite types.
type CompositeTypeHandlerFunc func(location common.Location, typeID common.TypeID) *sema.CompositeType
type CompositeTypeHandlerFunc func(location common.Location, typeID TypeID) *sema.CompositeType

// CompositeTypeCode contains the "prepared" / "callable" "code"
// for the functions and the destructor of a composite
Expand Down Expand Up @@ -3061,7 +3061,7 @@ func lookupComposite(interpreter *Interpreter, typeID string) (*sema.CompositeTy
return nil, err
}

typ, err := interpreter.GetCompositeType(location, qualifiedIdentifier, common.TypeID(typeID))
typ, err := interpreter.GetCompositeType(location, qualifiedIdentifier, TypeID(typeID))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -3108,7 +3108,7 @@ func init() {
// We assign this here because it depends on the interpreter, so this breaks the initialization cycle
defineBaseValue(
BaseActivation,
"DictionaryType",
sema.DictionaryTypeFunctionName,
NewUnmeteredHostFunctionValue(
sema.DictionaryTypeFunctionType,
func(invocation Invocation) Value {
Expand Down Expand Up @@ -3147,7 +3147,7 @@ func init() {

defineBaseValue(
BaseActivation,
"CompositeType",
sema.CompositeTypeFunctionName,
NewUnmeteredHostFunctionValue(
sema.CompositeTypeFunctionType,
func(invocation Invocation) Value {
Expand Down Expand Up @@ -3175,7 +3175,7 @@ func init() {

defineBaseValue(
BaseActivation,
"InterfaceType",
sema.InterfaceTypeFunctionName,
NewUnmeteredHostFunctionValue(
sema.InterfaceTypeFunctionType,
func(invocation Invocation) Value {
Expand Down Expand Up @@ -3203,7 +3203,7 @@ func init() {

defineBaseValue(
BaseActivation,
"FunctionType",
sema.FunctionTypeFunctionName,
NewUnmeteredHostFunctionValue(
sema.FunctionTypeFunctionType,
func(invocation Invocation) Value {
Expand Down Expand Up @@ -3250,7 +3250,7 @@ func init() {

defineBaseValue(
BaseActivation,
"RestrictedType",
sema.RestrictedTypeFunctionName,
NewUnmeteredHostFunctionValue(
sema.RestrictedTypeFunctionType,
RestrictedTypeFunction,
Expand Down Expand Up @@ -3428,7 +3428,7 @@ type runtimeTypeConstructor struct {
// Constructor functions are stateless functions. Hence they can be re-used across interpreters.
var runtimeTypeConstructors = []runtimeTypeConstructor{
{
name: "OptionalType",
name: sema.OptionalTypeFunctionName,
converter: NewUnmeteredHostFunctionValue(
sema.OptionalTypeFunctionType,
func(invocation Invocation) Value {
Expand All @@ -3448,7 +3448,7 @@ var runtimeTypeConstructors = []runtimeTypeConstructor{
),
},
{
name: "VariableSizedArrayType",
name: sema.VariableSizedArrayTypeFunctionName,
converter: NewUnmeteredHostFunctionValue(
sema.VariableSizedArrayTypeFunctionType,
func(invocation Invocation) Value {
Expand All @@ -3469,7 +3469,7 @@ var runtimeTypeConstructors = []runtimeTypeConstructor{
),
},
{
name: "ConstantSizedArrayType",
name: sema.ConstantSizedArrayTypeFunctionName,
converter: NewUnmeteredHostFunctionValue(
sema.ConstantSizedArrayTypeFunctionType,
func(invocation Invocation) Value {
Expand All @@ -3495,7 +3495,7 @@ var runtimeTypeConstructors = []runtimeTypeConstructor{
),
},
{
name: "ReferenceType",
name: sema.ReferenceTypeFunctionName,
converter: NewUnmeteredHostFunctionValue(
sema.ReferenceTypeFunctionType,
func(invocation Invocation) Value {
Expand All @@ -3522,7 +3522,7 @@ var runtimeTypeConstructors = []runtimeTypeConstructor{
),
},
{
name: "CapabilityType",
name: sema.CapabilityTypeFunctionName,
converter: NewUnmeteredHostFunctionValue(
sema.CapabilityTypeFunctionType,
func(invocation Invocation) Value {
Expand Down Expand Up @@ -4730,7 +4730,7 @@ func (interpreter *Interpreter) ConvertStaticToSemaType(staticType StaticType) (
func(location common.Location, qualifiedIdentifier string) (*sema.InterfaceType, error) {
return interpreter.getInterfaceType(location, qualifiedIdentifier)
},
func(location common.Location, qualifiedIdentifier string, typeID common.TypeID) (*sema.CompositeType, error) {
func(location common.Location, qualifiedIdentifier string, typeID TypeID) (*sema.CompositeType, error) {
return interpreter.GetCompositeType(location, qualifiedIdentifier, typeID)
},
)
Expand Down Expand Up @@ -4788,7 +4788,7 @@ func (interpreter *Interpreter) GetContractComposite(contractLocation common.Add
func (interpreter *Interpreter) GetCompositeType(
location common.Location,
qualifiedIdentifier string,
typeID common.TypeID,
typeID TypeID,
) (*sema.CompositeType, error) {
var compositeType *sema.CompositeType
if location == nil {
Expand Down Expand Up @@ -4817,7 +4817,7 @@ func (interpreter *Interpreter) GetCompositeType(
}
}

func (interpreter *Interpreter) getUserCompositeType(location common.Location, typeID common.TypeID) *sema.CompositeType {
func (interpreter *Interpreter) getUserCompositeType(location common.Location, typeID TypeID) *sema.CompositeType {
elaboration := interpreter.getElaboration(location)
if elaboration == nil {
return nil
Expand Down
54 changes: 30 additions & 24 deletions runtime/interpreter/primitivestatictype.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,12 @@ var PrimitiveStaticTypes = _PrimitiveStaticType_map

type PrimitiveStaticType uint

func (t PrimitiveStaticType) Equal(other StaticType) bool {
otherPrimitiveType, ok := other.(PrimitiveStaticType)
if !ok {
return false
}

return t == otherPrimitiveType
}
var _ StaticType = PrimitiveStaticType(0)

const primitiveStaticTypePrefix = "PrimitiveStaticType"

var primitiveStaticTypeConstantLength = len(primitiveStaticTypePrefix) + 2 // + 2 for parentheses

func (t PrimitiveStaticType) MeteredString(memoryGauge common.MemoryGauge) string {
if str, ok := PrimitiveStaticTypes[t]; ok {
common.UseMemory(memoryGauge, common.NewRawStringMemoryUsage(len(str)))
return str
}

memoryAmount := primitiveStaticTypeConstantLength + OverEstimateIntStringLength(int(t))
common.UseMemory(memoryGauge, common.NewRawStringMemoryUsage(memoryAmount))

rawValueStr := strconv.FormatInt(int64(t), 10)
return fmt.Sprintf("%s(%s)", primitiveStaticTypePrefix, rawValueStr)
}

func NewPrimitiveStaticType(
memoryGauge common.MemoryGauge,
staticType PrimitiveStaticType,
Expand Down Expand Up @@ -300,8 +280,34 @@ func (t PrimitiveStaticType) elementSize() uint {
return UnknownElementSize
}

func (i PrimitiveStaticType) SemaType() sema.Type {
switch i {
func (t PrimitiveStaticType) Equal(other StaticType) bool {
otherPrimitiveType, ok := other.(PrimitiveStaticType)
if !ok {
return false
}

return t == otherPrimitiveType
}

func (t PrimitiveStaticType) MeteredString(memoryGauge common.MemoryGauge) string {
if str, ok := PrimitiveStaticTypes[t]; ok {
common.UseMemory(memoryGauge, common.NewRawStringMemoryUsage(len(str)))
return str
}

memoryAmount := primitiveStaticTypeConstantLength + OverEstimateIntStringLength(int(t))
common.UseMemory(memoryGauge, common.NewRawStringMemoryUsage(memoryAmount))

rawValueStr := strconv.FormatInt(int64(t), 10)
return fmt.Sprintf("%s(%s)", primitiveStaticTypePrefix, rawValueStr)
}

func (t PrimitiveStaticType) ID() TypeID {
return t.SemaType().ID()
}

func (t PrimitiveStaticType) SemaType() sema.Type {
switch t {
case PrimitiveStaticTypeVoid:
return sema.VoidType

Expand Down Expand Up @@ -454,7 +460,7 @@ func (i PrimitiveStaticType) SemaType() sema.Type {
case PrimitiveStaticTypePublicAccountCapabilities:
return sema.PublicAccountCapabilitiesType
default:
panic(errors.NewUnexpectedError("missing case for %s", i))
panic(errors.NewUnexpectedError("missing case for %s", t))
}
}

Expand Down
Loading

0 comments on commit c5ea847

Please sign in to comment.