scriggo icon indicating copy to clipboard operation
scriggo copied to clipboard

compiler: method call on interface receiver

Open zapateo opened this issue 5 years ago • 4 comments

package main

import "crypto/sha1"

func main() {
	h := sha1.New()
	sb := []byte("abc")
	h.Write(sb)
}

reports the error value of type *sha1.digest is not assignable to type []uint8

zapateo avatar Aug 06 '19 10:08 zapateo

The emitted code is:

Package main

Import "sha1"

Func main()
	; regs(1,0,1,7)
	Call sha1.New _ _ _ g3	; func() (g3 hash.Hash)
	Move g3 g2
	Move g2 g1
	Move "abc" s1
	Convert s1 []uint8 g3
	Move g3 g2
	Move g1 g3
	MethodValue g3 "Write" g4
	Move g1 g6
	Move g2 g7
	Call (g4) ...0 i1 _ _ g5	; func(g6 []uint8) (i1 int, g5 error)
	Return

gazerro avatar Aug 06 '19 18:08 gazerro

Note that h has type hash.Hash, which is an interface that embeds io.Writer.

zapateo avatar Aug 19 '19 07:08 zapateo

The emitted code is:

Package main

Import "sha1"

Func main()
	; regs(1,0,0,9)
	CallPredefined sha1.New	; New() (g3 hash.Hash)
	Move g3 g2
	Move g2 g1
	Move nil g5
	Move g5 g4
	Move g4 g3
	
	Move g1 g8
	Move g3 g9
	CallIndirect g6 ...0	; Stack shift: 0, 0, 0, 6
	Return

That was a problem of the disassembler, which has been resolved in https://github.com/open2b/scriggo/commit/e291a02e5e01303f79908e5d0370c25c3901f7c8.

The disassembled code is now

   1 
   2 Package main
   3 
   4 Import "sha1"
   5 
   6 Func main()
   7     ; regs(1,0,1,9)
   8     CallPredefined sha1.New ; New() (g3 hash.Hash)
   9     Move g3 g2
  10     Move g2 g1
  11     Move "abc" s1
  12     Convert s1 []uint8 g4
  13     Move g4 g3
  14     Move g1 g5
  15     MethodValue g5 "Write" g6
  16     Move g1 g8
  17     Move g3 g9
  18     CallIndirect g6 ...0    ; Stack shift: 0, 0, 1, 6
  19     Return

but the problem still persists.

zapateo avatar Aug 19 '19 08:08 zapateo

This is the current disassembled code:

Package main

Import "sha1"

Func main()
	; regs(1,0,1,7)
	Call sha1.New _ _ _ g3	; func() (g3 hash.Hash)
	Move g3 g2
	Move g2 g1
	Move "abc" s1
	Convert s1 []uint8 g3
	Move g3 g2
	Move g1 g3
	MethodValue g3 "Write" g4
	Move g1 g6
	Move g2 g7
	Call (g4) ...0 i1 _ _ g5	; func(g6 []uint8) (i1 int, g5 error)
	Return

gazerro avatar Aug 28 '21 17:08 gazerro