Repository: tetratelabs/wazero
Pull request #101 (2021-12-31 01:01:57)

Removes suffix Type from fields in FunctionType
Since there is no ambiguity in FunctionType about it being types, this
removes the suffix Type from its fields. I wouldn't do this, if for
example, there were mixed fields (ex type names). However, as it is, I
think this is less stuttering so overall better.



Patch:
diff --git a/wasm/jit/engine.go b/wasm/jit/engine.go
index 56922946..e32ebfbe 100644
--- a/wasm/jit/engine.go
+++ b/wasm/jit/engine.go
@@ -111,7 +111,7 @@ func (e *engine) Call(f *wasm.FunctionInstance, params ...uint64) (results []uin
 	}
 	// Note the top value is the tail of the results,
 	// so we assign them in reverse order.
-	results = make([]uint64, len(f.Signature.ResultTypes))
+	results = make([]uint64, len(f.Signature.Results))
 	for i := range results {
 		results[len(results)-1-i] = e.pop()
 	}
@@ -612,8 +612,8 @@ func (e *engine) compileWasmFunction(f *wasm.FunctionInstance) (*compiledWasmFun
 	cf := &compiledWasmFunction{
 		source:          f,
 		codeSegment:     code,
-		paramCount:      uint64(len(f.Signature.ParamTypes)),
-		resultCount:     uint64(len(f.Signature.ResultTypes)),
+		paramCount:      uint64(len(f.Signature.Params)),
+		resultCount:     uint64(len(f.Signature.Results)),
 		memory:          f.ModuleInstance.Memory,
 		maxStackPointer: maxStackPointer,
 	}
diff --git a/wasm/jit/jit_amd64.go b/wasm/jit/jit_amd64.go
index ba77ac8e..19c26a30 100644
--- a/wasm/jit/jit_amd64.go
+++ b/wasm/jit/jit_amd64.go
@@ -92,7 +92,7 @@ func (c *amd64Compiler) generate() ([]byte, uint64, error) {
 }
 
 func (c *amd64Compiler) pushFunctionParams() {
-	for _, t := range c.f.Signature.ParamTypes {
+	for _, t := range c.f.Signature.Params {
 		loc := c.locationStack.pushValueOnStack()
 		switch t {
 		case wasm.ValueTypeI32, wasm.ValueTypeI64:
diff --git a/wasm/store.go b/wasm/store.go
index 8ca39634..f78ad5c1 100644
--- a/wasm/store.go
+++ b/wasm/store.go
@@ -153,7 +153,7 @@ func (s *Store) Instantiate(module *Module, name string) error {
 			return fmt.Errorf("invalid start function index: %d", index)
 		}
 		signature := instance.Functions[index].Signature
-		if len(signature.ParamTypes) != 0 || len(signature.ResultTypes) != 0 {
+		if len(signature.Params) != 0 || len(signature.Results) != 0 {
 			return fmt.Errorf("start function must have the empty signature")
 		}
 	}
@@ -187,12 +187,12 @@ func (s *Store) CallFunction(moduleName, funcName string, params ...uint64) (res
 	}
 
 	f := exp.Function
-	if len(f.Signature.ParamTypes) != len(params) {
+	if len(f.Signature.Params) != len(params) {
 		return nil, nil, fmt.Errorf("invalid number of parameters")
 	}
 
 	ret, err := s.engine.Call(f, params...)
-	return ret, f.Signature.ResultTypes, err
+	return ret, f.Signature.Results, err
 }
 
 func (s *Store) resolveImports(module *Module, target *ModuleInstance) error {
@@ -252,10 +252,10 @@ func (s *Store) applyFunctionImport(target *ModuleInstance, typeIndexPtr *uint32
 		return fmt.Errorf("unknown type for function import")
 	}
 	iSig := target.Types[typeIndex]
-	if !HasSameSignature(iSig.ResultTypes, f.Signature.ResultTypes) {
-		return fmt.Errorf("return signature mimatch: %#x != %#x", iSig.ResultTypes, f.Signature.ResultTypes)
-	} else if !HasSameSignature(iSig.ParamTypes, f.Signature.ParamTypes) {
-		return fmt.Errorf("input signature mimatch: %#x != %#x", iSig.ParamTypes, f.Signature.ParamTypes)
+	if !HasSameSignature(iSig.Results, f.Signature.Results) {
+		return fmt.Errorf("return signature mimatch: %#x != %#x", iSig.Results, f.Signature.Results)
+	} else if !HasSameSignature(iSig.Params, f.Signature.Params) {
+		return fmt.Errorf("input signature mimatch: %#x != %#x", iSig.Params, f.Signature.Params)
 	}
 	target.Functions = append(target.Functions, f)
 	return nil
@@ -1034,23 +1034,23 @@ func analyzeFunction(
 			pc += num - 1
 			switch Opcode(op) {
 			case OpcodeLocalGet:
-				inputLen := uint32(len(f.Signature.ParamTypes))
+				inputLen := uint32(len(f.Signature.Params))
 				if l := f.NumLocals + inputLen; index >= l {
 					return fmt.Errorf("invalid local index for local.get %d >= %d(=len(locals)+len(parameters))", index, l)
 				}
 				if index < inputLen {
-					valueTypeStack.push(f.Signature.ParamTypes[index])
+					valueTypeStack.push(f.Signature.Params[index])
 				} else {
 					valueTypeStack.push(f.LocalTypes[index-inputLen])
 				}
 			case OpcodeLocalSet:
-				inputLen := uint32(len(f.Signature.ParamTypes))
+				inputLen := uint32(len(f.Signature.Params))
 				if l := f.NumLocals + inputLen; index >= l {
 					return fmt.Errorf("invalid local index for local.set %d >= %d(=len(locals)+len(parameters))", index, l)
 				}
 				var expType ValueType
 				if index < inputLen {
-					expType = f.Signature.ParamTypes[index]
+					expType = f.Signature.Params[index]
 				} else {
 					expType = f.LocalTypes[index-inputLen]
 				}
@@ -1058,13 +1058,13 @@ func analyzeFunction(
 					return err
 				}
 			case OpcodeLocalTee:
-				inputLen := uint32(len(f.Signature.ParamTypes))
+				inputLen := uint32(len(f.Signature.Params))
 				if l := f.NumLocals + inputLen; index >= l {
 					return fmt.Errorf("invalid local index for local.tee %d >= %d(=len(locals)+len(parameters))", index, l)
 				}
 				var expType ValueType
 				if index < inputLen {
-					expType = f.Signature.ParamTypes[index]
+					expType = f.Signature.Params[index]
 				} else {
 					expType = f.LocalTypes[index-inputLen]
 				}
@@ -1098,7 +1098,7 @@ func analyzeFunction(
 			pc += num - 1
 			// Check type soundness.
 			target := labelStack[len(labelStack)-int(index)-1]
-			targetResultType := target.BlockType.ResultTypes
+			targetResultType := target.BlockType.Results
 			if target.IsLoop {
 				// Loop operation doesn't require results since the continuation is
 				// the beginning of the loop.
@@ -1125,7 +1125,7 @@ func analyzeFunction(
 			}
 			// Check type soundness.
 			target := labelStack[len(labelStack)-int(index)-1]
-			targetResultType := target.BlockType.ResultTypes
+			targetResultType := target.BlockType.Results
 			if target.IsLoop {
 				// Loop operation doesn't require results since the continuation is
 				// the beginning of the loop.
@@ -1169,7 +1169,7 @@ func analyzeFunction(
 				return fmt.Errorf("cannot pop the required operand for br_table")
 			}
 			lnLabel := labelStack[len(labelStack)-1-int(ln)]
-			expType := lnLabel.BlockType.ResultTypes
+			expType := lnLabel.BlockType.Results
 			if lnLabel.IsLoop {
 				// Loop operation doesn't require results since the continuation is
 				// the beginning of the loop.
@@ -1180,7 +1180,7 @@ func analyzeFunction(
 					return fmt.Errorf("invalid l param given for br_table")
 				}
 				label := labelStack[len(labelStack)-1-int(l)]
-				expType2 := label.BlockType.ResultTypes
+				expType2 := label.BlockType.Results
 				if label.IsLoop {
 					// Loop operation doesn't require results since the continuation is
 					// the beginning of the loop.
@@ -1211,12 +1211,12 @@ func analyzeFunction(
 				return fmt.Errorf("invalid function index")
 			}
 			funcType := module.TypeSection[functionDeclarations[index]]
-			for i := 0; i < len(funcType.ParamTypes); i++ {
-				if err := valueTypeStack.popAndVerifyType(funcType.ParamTypes[len(funcType.ParamTypes)-1-i]); err != nil {
+			for i := 0; i < len(funcType.Params); i++ {
+				if err := valueTypeStack.popAndVerifyType(funcType.Params[len(funcType.Params)-1-i]); err != nil {
 					return fmt.Errorf("type mismatch on call operation param type")
 				}
 			}
-			for _, exp := range funcType.ResultTypes {
+			for _, exp := range funcType.Results {
 				valueTypeStack.push(exp)
 			}
 		} else if op == OpcodeCallIndirect {
@@ -1240,12 +1240,12 @@ func analyzeFunction(
 				return fmt.Errorf("invalid type index at call_indirect: %d", typeIndex)
 			}
 			funcType := module.TypeSection[typeIndex]
-			for i := 0; i < len(funcType.ParamTypes); i++ {
-				if err := valueTypeStack.popAndVerifyType(funcType.ParamTypes[len(funcType.ParamTypes)-1-i]); err != nil {
+			for i := 0; i < len(funcType.Params); i++ {
+				if err := valueTypeStack.popAndVerifyType(funcType.Params[len(funcType.Params)-1-i]); err != nil {
 					return fmt.Errorf("type mismatch on call_indirect operation input type")
 				}
 			}
-			for _, exp := range funcType.ResultTypes {
+			for _, exp := range funcType.Results {
 				valueTypeStack.push(exp)
 			}
 		} else if OpcodeI32Eqz <= op && op <= OpcodeF64ReinterpretI64 {
@@ -1490,7 +1490,7 @@ func analyzeFunction(
 			bl := labelStack[len(labelStack)-1]
 			bl.ElseAt = pc
 			// Check the type soundness of the instructions *before*　 entering this Eles Op.
-			if err := valueTypeStack.popResults(bl.BlockType.ResultTypes, true); err != nil {
+			if err := valueTypeStack.popResults(bl.BlockType.Results, true); err != nil {
 				return fmt.Errorf("invalid instruction results in then instructions")
 			}
 			// Before entring instructions inside else, we pop all the values pushed by
@@ -1501,7 +1501,7 @@ func analyzeFunction(
 			bl.EndAt = pc
 			labelStack = labelStack[:len(labelStack)-1]
 			if bl.IsIf && bl.ElseAt <= bl.StartAt {
-				if len(bl.BlockType.ResultTypes) > 0 {
+				if len(bl.BlockType.Results) > 0 {
 					return fmt.Errorf("type mismatch between then and else blocks")
 				}
 				// To handle if block without else properly,
@@ -1509,20 +1509,20 @@ func analyzeFunction(
 				bl.ElseAt = bl.EndAt - 1
 			}
 			// Check type soundness.
-			if err := valueTypeStack.popResults(bl.BlockType.ResultTypes, true); err != nil {
-				return fmt.Errorf("invalid instruction results at end instruction; expected %v: %v", bl.BlockType.ResultTypes, err)
+			if err := valueTypeStack.popResults(bl.BlockType.Results, true); err != nil {
+				return fmt.Errorf("invalid instruction results at end instruction; expected %v: %v", bl.BlockType.Results, err)
 			}
 			// Put the result types at the end after resetting at the stack limit
 			// since we might have Any type between the limit and the current top.
 			valueTypeStack.resetAtStackLimit()
-			for _, exp := range bl.BlockType.ResultTypes {
+			for _, exp := range bl.BlockType.Results {
 				valueTypeStack.push(exp)
 			}
 			// We exit if/loop/block, so reset the constraints on the stack manipulation
 			// on values previously pushed by outer blocks.
 			valueTypeStack.popStackLimit()
 		} else if op == OpcodeReturn {
-			expTypes := f.Signature.ResultTypes
+			expTypes := f.Signature.Results
 			for i := 0; i < len(expTypes); i++ {
 				if err := valueTypeStack.popAndVerifyType(expTypes[len(expTypes)-1-i]); err != nil {
 					return fmt.Errorf("return type mismatch on return: %v; want %v", err, expTypes)
@@ -1582,13 +1582,13 @@ func ReadBlockType(types []*FunctionType, r io.Reader) (*BlockType, uint64, erro
 	case -64: // 0x40 in original byte = nil
 		ret = &BlockType{}
 	case -1: // 0x7f in original byte = i32
-		ret = &BlockType{ResultTypes: []ValueType{ValueTypeI32}}
+		ret = &BlockType{Results: []ValueType{ValueTypeI32}}
 	case -2: // 0x7e in original byte = i64
-		ret = &BlockType{ResultTypes: []ValueType{ValueTypeI64}}
+		ret = &BlockType{Results: []ValueType{ValueTypeI64}}
 	case -3: // 0x7d in original byte = f32
-		ret = &BlockType{ResultTypes: []ValueType{ValueTypeF32}}
+		ret = &BlockType{Results: []ValueType{ValueTypeF32}}
 	case -4: // 0x7c in original byte = f64
-		ret = &BlockType{ResultTypes: []ValueType{ValueTypeF64}}
+		ret = &BlockType{Results: []ValueType{ValueTypeF64}}
 	default:
 		if raw < 0 || (raw >= int64(len(types))) {
 			return nil, 0, fmt.Errorf("invalid block type: %d", raw)
@@ -1633,7 +1633,7 @@ func (s *Store) AddHostFunction(moduleName, funcName string, fn reflect.Value) e
 				return nil, err
 			}
 		}
-		return &FunctionType{ParamTypes: paramTypes, ResultTypes: resultTypes}, nil
+		return &FunctionType{Params: paramTypes, Results: resultTypes}, nil
 	}
 
 	m, ok := s.ModuleInstances[moduleName]
diff --git a/wasm/type.go b/wasm/type.go
index 93f746b4..657da13a 100644
--- a/wasm/type.go
+++ b/wasm/type.go
@@ -8,7 +8,7 @@ import (
 )
 
 type FunctionType struct {
-	ParamTypes, ResultTypes []ValueType
+	Params, Results []ValueType
 }
 
 func readFunctionType(r io.Reader) (*FunctionType, error) {
@@ -44,8 +44,8 @@ func readFunctionType(r io.Reader) (*FunctionType, error) {
 	}
 
 	return &FunctionType{
-		ParamTypes:  paramTypes,
-		ResultTypes: resultTypes,
+		Params:  paramTypes,
+		Results: resultTypes,
 	}, nil
 }
 
diff --git a/wasm/wazeroir/compiler.go b/wasm/wazeroir/compiler.go
index 9e993aa7..366e5688 100644
--- a/wasm/wazeroir/compiler.go
+++ b/wasm/wazeroir/compiler.go
@@ -148,7 +148,7 @@ func Compile(f *wasm.FunctionInstance) (*CompilationResult, error) {
 	c := compiler{controlFrames: &controlFrames{}, f: f, result: CompilationResult{LabelCallers: map[string]int{}}}
 
 	// Push function arguments.
-	for _, t := range f.Signature.ParamTypes {
+	for _, t := range f.Signature.Params {
 		c.stackPush(wasmValueTypeToUnsignedType(t))
 	}
 	// Emit const expressions for locals.
@@ -160,13 +160,13 @@ func Compile(f *wasm.FunctionInstance) (*CompilationResult, error) {
 	}
 
 	// Insert the function control frame.
-	returns := make([]UnsignedType, 0, len(f.Signature.ResultTypes))
-	for _, t := range f.Signature.ResultTypes {
+	returns := make([]UnsignedType, 0, len(f.Signature.Results))
+	for _, t := range f.Signature.Results {
 		returns = append(returns, wasmValueTypeToUnsignedType(t))
 	}
 	c.controlFrames.push(&controlFrame{
 		frameID:          c.nextID(),
-		originalStackLen: len(f.Signature.ParamTypes),
+		originalStackLen: len(f.Signature.Params),
 		returns:          returns,
 		kind:             controlFrameKindFunction,
 	})
@@ -231,7 +231,7 @@ operatorSwitch:
 			originalStackLen: len(c.stack),
 			kind:             controlFrameKindBlockWithoutContinuationLabel,
 		}
-		for _, t := range bt.ResultTypes {
+		for _, t := range bt.Results {
 			frame.returns = append(frame.returns, wasmValueTypeToUnsignedType(t))
 		}
 		c.controlFrames.push(frame)
@@ -257,7 +257,7 @@ operatorSwitch:
 			originalStackLen: len(c.stack),
 			kind:             controlFrameKindLoop,
 		}
-		for _, t := range bt.ResultTypes {
+		for _, t := range bt.Results {
 			frame.returns = append(frame.returns, wasmValueTypeToUnsignedType(t))
 		}
 		c.controlFrames.push(frame)
@@ -297,7 +297,7 @@ operatorSwitch:
 			// when else opcode found later.
 			kind: controlFrameKindIfWithoutElse,
 		}
-		for _, t := range bt.ResultTypes {
+		for _, t := range bt.Results {
 			frame.returns = append(frame.returns, wasmValueTypeToUnsignedType(t))
 		}
 		c.controlFrames.push(frame)
diff --git a/wasm/wazeroir/interpreter.go b/wasm/wazeroir/interpreter.go
index 63ca2896..561689f5 100644
--- a/wasm/wazeroir/interpreter.go
+++ b/wasm/wazeroir/interpreter.go
@@ -495,7 +495,7 @@ func (it *interpreter) Call(f *wasm.FunctionInstance, params ...uint64) (results
 		}
 		it.callNativeFunc(g)
 	}
-	results = make([]uint64, len(f.Signature.ResultTypes))
+	results = make([]uint64, len(f.Signature.Results))
 	for i := range results {
 		results[len(results)-1-i] = it.pop()
 	}
@@ -588,7 +588,7 @@ func (it *interpreter) callNativeFunc(f *interpreterFunction) {
 		case OperationKindCall:
 			{
 				if op.f.hostFn != nil {
-					it.callHostFunc(op.f, it.stack[len(it.stack)-len(op.f.signature.ParamTypes):]...)
+					it.callHostFunc(op.f, it.stack[len(it.stack)-len(op.f.signature.Params):]...)
 				} else {
 					it.callNativeFunc(op.f)
 				}
@@ -604,7 +604,7 @@ func (it *interpreter) callNativeFunc(f *interpreterFunction) {
 				}
 				// Call in.
 				if target.hostFn != nil {
-					it.callHostFunc(target, it.stack[len(it.stack)-len(target.signature.ParamTypes):]...)
+					it.callHostFunc(target, it.stack[len(it.stack)-len(target.signature.Params):]...)
 				} else {
 					it.callNativeFunc(target)
 				}
@@ -1473,8 +1473,8 @@ func funcTypeString(t *wasm.FunctionType) string {
 		// Fast stringification of byte slice.
 		// This is safe anyway as the results are copied
 		// into the return value string.
-		*(*string)(unsafe.Pointer(&t.ParamTypes)),
-		*(*string)(unsafe.Pointer(&t.ResultTypes)),
+		*(*string)(unsafe.Pointer(&t.Params)),
+		*(*string)(unsafe.Pointer(&t.Results)),
 	)
 }
 
diff --git a/wasm/wazeroir/signature.go b/wasm/wazeroir/signature.go
index 63f8ad60..61eff100 100644
--- a/wasm/wazeroir/signature.go
+++ b/wasm/wazeroir/signature.go
@@ -170,37 +170,37 @@ func wasmOpcodeSignature(f *wasm.FunctionInstance, op wasm.Opcode, index uint32)
 	case wasm.OpcodeSelect:
 		return signature_UnknownUnkownI32_Unknown, nil
 	case wasm.OpcodeLocalGet:
-		inputLen := uint32(len(f.Signature.ParamTypes))
+		inputLen := uint32(len(f.Signature.Params))
 		if l := f.NumLocals + inputLen; index >= l {
 			return nil, fmt.Errorf("invalid local index for local.get %d >= %d", index, l)
 		}
 		var t UnsignedType
 		if index < inputLen {
-			t = wasmValueTypeToUnsignedType(f.Signature.ParamTypes[index])
+			t = wasmValueTypeToUnsignedType(f.Signature.Params[index])
 		} else {
 			t = wasmValueTypeToUnsignedType(f.LocalTypes[index-inputLen])
 		}
 		return &signature{out: []UnsignedType{t}}, nil
 	case wasm.OpcodeLocalSet:
-		inputLen := uint32(len(f.Signature.ParamTypes))
+		inputLen := uint32(len(f.Signature.Params))
 		if l := f.NumLocals + inputLen; index >= l {
 			return nil, fmt.Errorf("invalid local index for local.get %d >= %d", index, l)
 		}
 		var t UnsignedType
 		if index < inputLen {
-			t = wasmValueTypeToUnsignedType(f.Signature.ParamTypes[index])
+			t = wasmValueTypeToUnsignedType(f.Signature.Params[index])
 		} else {
 			t = wasmValueTypeToUnsignedType(f.LocalTypes[index-inputLen])
 		}
 		return &signature{in: []UnsignedType{t}}, nil
 	case wasm.OpcodeLocalTee:
-		inputLen := uint32(len(f.Signature.ParamTypes))
+		inputLen := uint32(len(f.Signature.Params))
 		if l := f.NumLocals + inputLen; index >= l {
 			return nil, fmt.Errorf("invalid local index for local.get %d >= %d", index, l)
 		}
 		var t UnsignedType
 		if index < inputLen {
-			t = wasmValueTypeToUnsignedType(f.Signature.ParamTypes[index])
+			t = wasmValueTypeToUnsignedType(f.Signature.Params[index])
 		} else {
 			t = wasmValueTypeToUnsignedType(f.LocalTypes[index-inputLen])
 		}
@@ -353,10 +353,10 @@ func wasmOpcodeSignature(f *wasm.FunctionInstance, op wasm.Opcode, index uint32)
 
 func funcTypeToSignature(tps *wasm.FunctionType) *signature {
 	ret := &signature{}
-	for _, vt := range tps.ParamTypes {
+	for _, vt := range tps.Params {
 		ret.in = append(ret.in, wasmValueTypeToUnsignedType(vt))
 	}
-	for _, vt := range tps.ResultTypes {
+	for _, vt := range tps.Results {
 		ret.out = append(ret.out, wasmValueTypeToUnsignedType(vt))
 	}
 	return ret


Test patch:
diff --git a/wasm/jit/jit_amd64_test.go b/wasm/jit/jit_amd64_test.go
index 39c4d17d..227a0606 100644
--- a/wasm/jit/jit_amd64_test.go
+++ b/wasm/jit/jit_amd64_test.go
@@ -51,11 +51,11 @@ func (c *amd64Compiler) movIntConstToRegister(val int64, targetRegister int16) *
 
 func TestAmd64Compiler_pushFunctionInputs(t *testing.T) {
 	f := &wasm.FunctionInstance{Signature: &wasm.FunctionType{
-		ParamTypes: []wasm.ValueType{wasm.ValueTypeF64, wasm.ValueTypeI32},
+		Params: []wasm.ValueType{wasm.ValueTypeF64, wasm.ValueTypeI32},
 	}}
 	compiler := &amd64Compiler{locationStack: newValueLocationStack(), f: f}
 	compiler.pushFunctionParams()
-	require.Equal(t, uint64(len(f.Signature.ParamTypes)), compiler.locationStack.sp)
+	require.Equal(t, uint64(len(f.Signature.Params)), compiler.locationStack.sp)
 	loc := compiler.locationStack.pop()
 	require.Equal(t, uint64(1), loc.stackPointer)
 	loc = compiler.locationStack.pop()
@@ -482,8 +482,8 @@ func TestEngine_exec_callHostFunction(t *testing.T) {
 		hostFunctionInstance := &wasm.FunctionInstance{
 			HostFunction: &hostFunc,
 			Signature: &wasm.FunctionType{
-				ParamTypes:  []wasm.ValueType{wasm.ValueTypeI64, wasm.ValueTypeI64},
-				ResultTypes: []wasm.ValueType{wasm.ValueTypeI64},
+				Params:  []wasm.ValueType{wasm.ValueTypeI64, wasm.ValueTypeI64},
+				Results: []wasm.ValueType{wasm.ValueTypeI64},
 			},
 		}
 		eng.compiledHostFunctionIndex[hostFunctionInstance] = 1