compiler: exclude unexported methods from manifest

We should be able to invoke exported methods only.
This commit is contained in:
Anna Shaleva 2020-07-03 12:58:41 +03:00
parent 1755ce10ac
commit 7d8a3a7065
2 changed files with 50 additions and 25 deletions

View file

@ -28,6 +28,8 @@ type MethodDebugInfo struct {
ID string `json:"id"` ID string `json:"id"`
// Name is the name of the method together with the namespace it belongs to. // Name is the name of the method together with the namespace it belongs to.
Name DebugMethodName `json:"name"` Name DebugMethodName `json:"name"`
// IsExported defines whether method is exported.
IsExported bool `json:"-"`
// Range is the range of smart-contract's opcodes corresponding to the method. // Range is the range of smart-contract's opcodes corresponding to the method.
Range DebugRange `json:"range"` Range DebugRange `json:"range"`
// Parameters is a list of method's parameters. // Parameters is a list of method's parameters.
@ -136,6 +138,7 @@ func (c *codegen) methodInfoFromScope(name string, scope *funcScope) *MethodDebu
return &MethodDebugInfo{ return &MethodDebugInfo{
ID: name, ID: name,
Name: DebugMethodName{Name: name}, Name: DebugMethodName{Name: name},
IsExported: scope.decl.Name.IsExported(),
Range: scope.rng, Range: scope.rng,
Parameters: params, Parameters: params,
ReturnType: c.scReturnTypeFromScope(scope), ReturnType: c.scReturnTypeFromScope(scope),
@ -350,9 +353,9 @@ func (di *DebugInfo) convertToManifest(fs smartcontract.PropertyState) (*manifes
if entryPoint.Name == "" { if entryPoint.Name == "" {
return nil, errors.New("no Main method was found") return nil, errors.New("no Main method was found")
} }
methods := make([]manifest.Method, 0, len(di.Methods)-1) methods := make([]manifest.Method, 0)
for _, method := range di.Methods { for _, method := range di.Methods {
if method.Name.Name != mainIdent { if method.Name.Name != mainIdent && method.IsExported {
mMethod, err := method.ToManifestMethod() mMethod, err := method.ToManifestMethod()
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -18,27 +18,30 @@ func TestCodeGen_DebugInfo(t *testing.T) {
func Main(op string) bool { func Main(op string) bool {
var s string var s string
_ = s _ = s
res := methodInt(op) res := MethodInt(op)
_ = methodString() _ = MethodString()
_ = methodByteArray() _ = MethodByteArray()
_ = methodArray() _ = MethodArray()
_ = methodStruct() _ = MethodStruct()
_ = MethodConcat("a", "b", "c")
_ = unexportedMethod()
return res == 42 return res == 42
} }
func methodInt(a string) int { func MethodInt(a string) int {
if a == "get42" { if a == "get42" {
return 42 return 42
} }
return 3 return 3
} }
func methodConcat(a, b string, c string) string{ func MethodConcat(a, b string, c string) string{
return a + b + c return a + b + c
} }
func methodString() string { return "" } func MethodString() string { return "" }
func methodByteArray() []byte { return nil } func MethodByteArray() []byte { return nil }
func methodArray() []bool { return nil } func MethodArray() []bool { return nil }
func methodStruct() struct{} { return struct{}{} } func MethodStruct() struct{} { return struct{}{} }
func unexportedMethod() int { return 1 }
` `
info, err := getBuildInfo(src) info, err := getBuildInfo(src)
@ -58,11 +61,12 @@ func methodStruct() struct{} { return struct{}{} }
t.Run("return types", func(t *testing.T) { t.Run("return types", func(t *testing.T) {
returnTypes := map[string]string{ returnTypes := map[string]string{
"methodInt": "Integer", "MethodInt": "Integer",
"methodConcat": "String", "MethodConcat": "String",
"methodString": "String", "methodByteArray": "ByteArray", "MethodString": "String", "MethodByteArray": "ByteArray",
"methodArray": "Array", "methodStruct": "Struct", "MethodArray": "Array", "MethodStruct": "Struct",
"Main": "Boolean", "Main": "Boolean",
"unexportedMethod": "Integer",
} }
for i := range d.Methods { for i := range d.Methods {
name := d.Methods[i].Name.Name name := d.Methods[i].Name.Name
@ -84,11 +88,11 @@ func methodStruct() struct{} { return struct{}{} }
t.Run("param types", func(t *testing.T) { t.Run("param types", func(t *testing.T) {
paramTypes := map[string][]DebugParam{ paramTypes := map[string][]DebugParam{
"methodInt": {{ "MethodInt": {{
Name: "a", Name: "a",
Type: "String", Type: "String",
}}, }},
"methodConcat": { "MethodConcat": {
{ {
Name: "a", Name: "a",
Type: "String", Type: "String",
@ -140,7 +144,7 @@ func methodStruct() struct{} { return struct{}{} }
}, },
Methods: []manifest.Method{ Methods: []manifest.Method{
{ {
Name: "methodInt", Name: "MethodInt",
Parameters: []manifest.Parameter{ Parameters: []manifest.Parameter{
{ {
Name: "a", Name: "a",
@ -150,25 +154,43 @@ func methodStruct() struct{} { return struct{}{} }
ReturnType: smartcontract.IntegerType, ReturnType: smartcontract.IntegerType,
}, },
{ {
Name: "methodString", Name: "MethodString",
Parameters: []manifest.Parameter{}, Parameters: []manifest.Parameter{},
ReturnType: smartcontract.StringType, ReturnType: smartcontract.StringType,
}, },
{ {
Name: "methodByteArray", Name: "MethodByteArray",
Parameters: []manifest.Parameter{}, Parameters: []manifest.Parameter{},
ReturnType: smartcontract.ByteArrayType, ReturnType: smartcontract.ByteArrayType,
}, },
{ {
Name: "methodArray", Name: "MethodArray",
Parameters: []manifest.Parameter{}, Parameters: []manifest.Parameter{},
ReturnType: smartcontract.ArrayType, ReturnType: smartcontract.ArrayType,
}, },
{ {
Name: "methodStruct", Name: "MethodStruct",
Parameters: []manifest.Parameter{}, Parameters: []manifest.Parameter{},
ReturnType: smartcontract.ArrayType, ReturnType: smartcontract.ArrayType,
}, },
{
Name: "MethodConcat",
Parameters: []manifest.Parameter{
{
Name: "a",
Type: smartcontract.StringType,
},
{
Name: "b",
Type: smartcontract.StringType,
},
{
Name: "c",
Type: smartcontract.StringType,
},
},
ReturnType: smartcontract.StringType,
},
}, },
Events: []manifest.Event{}, Events: []manifest.Event{},
}, },