Skip to content

Commit

Permalink
support slice[struct] type mangling
Browse files Browse the repository at this point in the history
  • Loading branch information
douyixuan committed Oct 14, 2024
1 parent 038c837 commit c5b6370
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
26 changes: 21 additions & 5 deletions compiler/compiler/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,30 @@ func (c *Compiler) compileDefineFuncNode(v *parser.DefineFuncNode) value.Value {

argTypes := make([]parser.TypeNode, len(v.Arguments))
argTypesName := ""
// Struct and Interface are mangled in another manner to support receiver-method.
getTypeManglingName := func(p string, name string) string {
if ty, hit := c.currentPackage.GetPkgType(name, true); hit {
return ty.Name()
} else if pkg, ok := c.packages[p]; ok {
if ty, hit := pkg.GetPkgType(name, true); hit {
return ty.Name()
}
}
return ""
}
for k, v := range v.Arguments {
argTypes[k] = v.Type
if ty, hit := c.currentPackage.GetPkgType(v.Type.Type(), true); hit {
argTypesName += ty.Name()
} else if pkg, ok := c.packages[v.Type.GetPackage()]; ok {
if ty, hit := pkg.GetPkgType(v.Type.Type(), true); hit {
argTypesName += ty.Name()
typeMangling := getTypeManglingName(v.Type.GetPackage(), v.Type.Type())
if sliceTy, ok := v.Type.(*parser.SliceTypeNode); ok && typeMangling == "" {
itemTy := sliceTy.ItemType
itemTyMangling := getTypeManglingName(itemTy.GetPackage(), itemTy.Type())
if itemTyMangling != "" {
typeMangling = "slice" + itemTyMangling
}
}
// todo: support more compound types
if typeMangling != "" {
argTypesName += typeMangling
} else {
argTypesName += v.Type.Mangling()
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/tx/tx.cell
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ func getWitness() string {
func getBlake2b(data string) string {
return get_blake2b(data)
}
func executeFunc(hash string, hashType uint8, funcName string, isOwnerMode bool, extensionIndex int64, args string) int64 {
func executeFunc(hash string, hashType uint8, funcName string, isOwnerMode bool, extensionIndex uint64, args string) int64 {
return execute_func(hash, hashType, funcName, isOwnerMode, extensionIndex, args)
}
6 changes: 3 additions & 3 deletions tests/examples/xudt.cell
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func main() {
}
blake160Hash := args[tx.BLAKE2B_BLOCK_SIZE+FLAGS_SIZE:]
witness := tx.getWitness()
witnessBlake2bHash := tx.getBlake2b()
witnessBlake2bHash := tx.getBlake2b(witness)
if witnessBlake2bHash[0:tx.BLAKE160_SIZE] == blake160Hash {
return ERROR_HASH_MISMATCHED
}
Expand All @@ -222,7 +222,7 @@ func main() {

// decode ownerScript raw bytes to Script
var ownerScript CellScript
err := tx.executeFunc(ownerScriptHash, ownerScript.hashType, "validate", owner_mode, 0, ownerScript.args)
err := tx.executeFunc(ownerScriptHash, ownerScript.hashType, "validate", owner_mode, 0u64, ownerScript.args)
if err == SUCCESS {
owner_mode = true
}
Expand Down Expand Up @@ -254,7 +254,7 @@ func main() {
extensionScripts := loadExtensionScripts()
// execute all scripts
for i, script := range(extensionScripts) {
tx.executeFunc(script.codeHash, script.hashType, "validate", owner_mode, i, script.args)
tx.executeFunc(script.codeHash, script.hashType, "validate", owner_mode, uint64(i), script.args)
}
return SUCCESS
}

0 comments on commit c5b6370

Please sign in to comment.