/*
 * Copyright 2017 - 2018 Volker Berlin (i-net software)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.inetsoftware.jwebassembly.binary;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import de.inetsoftware.jwebassembly.module.ValueType;

/**
 * An entry in the type section of the WebAssembly.
 * 
 * @author Volker Berlin
 */
class FunctionType extends SectionEntry {

    final List<ValueType> params = new ArrayList<>();

    ValueType             result;                    // Java has only one return parameter

    /**
     * {@inheritDoc}
     */
    @Override
    void writeSectionEntry( WasmOutputStream stream ) throws IOException {
        stream.write( ValueType.func.getCode() );
        stream.writeVaruint32( this.params.size() );
        for( ValueType valueType : this.params ) {
            stream.write( valueType.getCode() );
        }
        if( this.result == null ) {
            stream.writeVaruint32( 0 );
        } else {
            stream.writeVaruint32( 1 );
            stream.write( this.result.getCode() );
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return params.hashCode() + 31 * Objects.hashCode( result );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals( Object obj ) {
        if( obj == this ) {
            return true;
        }
        if( obj == null || obj.getClass() != getClass() ) {
            return false;
        }
        FunctionType type = (FunctionType)obj;
        return Objects.equals( params, type.params ) && Objects.equals( result, type.result );
    }
}

@@ -1017,94 +1017,58 @@
     
-ValueType             result;                    // Java has only one return parameter
+final List%3CValueType%3E results = new ArrayList%3C%3E();
 %0A%0A  
@@ -1416,132 +1416,105 @@
     
-if( this.result == null ) %7B%0A            stream.writeVaruint32( 0 );%0A        %7D else %7B%0A            stream.writeVaruint32( 1 );
+stream.writeVaruint32( this.results.size() );%0A        for( ValueType valueType : this.results ) %7B
 %0A   
@@ -1536,27 +1536,25 @@
 .write( 
-this.result
+valueType
 .getCode
@@ -1695,21 +1695,21 @@
  + 31 * 
-Objec
+resul
 ts.hashC
@@ -1716,16 +1716,8 @@
 ode(
- result 
 );%0A 
@@ -2036,22 +2036,21 @@
  return 
-Object
+param
 s.equals
@@ -2054,16 +2054,8 @@
 als(
- params,
  typ
@@ -2068,21 +2068,21 @@
 ms ) && 
-Objec
+resul
 ts.equal
@@ -2087,16 +2087,8 @@
 als(
- result,
  typ
@@ -2095,16 +2095,17 @@
 e.result
+s
  );%0A    

Fix multiple return values