/*
   Copyright 2018 - 2019 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.module;

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

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import de.inetsoftware.jwebassembly.WasmException;
import de.inetsoftware.jwebassembly.javascript.JavaScriptSyntheticFunctionName;
import de.inetsoftware.jwebassembly.module.TypeManager.StructType;
import de.inetsoftware.jwebassembly.wasm.AnyType;
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
import de.inetsoftware.jwebassembly.wasm.StructOperator;
import de.inetsoftware.jwebassembly.wasm.ValueType;

/**
 * WasmInstruction for struct operation. A struct is like a Java class without methods.
 * 
 * @author Volker Berlin
 *
 */
class WasmStructInstruction extends WasmInstruction {

    private final StructOperator   op;

    private final StructType       type;

    private final NamedStorageType fieldName;

    private SyntheticFunctionName functionName;

    /**
     * Create an instance of numeric operation.
     * 
     * @param op
     *            the struct operation
     * @param typeName
     *            the type name of the parameters
     * @param fieldName
     *            the name of field if needed for the operation
     * @param javaCodePos
     *            the code position/offset in the Java method
     * @param lineNumber
     *            the line number in the Java source code
     * @param types
     *            the type manager
     */
    WasmStructInstruction( @Nullable StructOperator op, @Nullable String typeName, @Nullable NamedStorageType fieldName, int javaCodePos, int lineNumber, TypeManager types ) {
        super( javaCodePos, lineNumber );
        this.op = op;
        this.type = typeName == null ? null : types.valueOf( typeName );
        this.fieldName = fieldName;
        if( type != null && fieldName != null ) {
            type.useFieldName( fieldName );
        }
    }

    /**
     * Create the synthetic polyfill function of this instruction for nonGC mode.
     * 
     * @return the function or null if not needed
     */
    SyntheticFunctionName createNonGcFunction() {
        switch( op ) {
            case NEW:
            case NEW_DEFAULT:
                functionName = new JavaScriptSyntheticFunctionName( "NonGC", "new_" + getSciptTypeName(), () -> "{}", null, type );
                break;
            case SET:
                AnyType fieldType = fieldName.getType();
                functionName = new JavaScriptSyntheticFunctionName( "NonGC", "set_" + fieldType, () -> "(a,i,v) => a[i]=v", ValueType.anyref, ValueType.i32, fieldType, null, null );
                break;
            case GET:
                fieldType = fieldName.getType();
                functionName = new JavaScriptSyntheticFunctionName( "NonGC", "get_" + fieldType, () -> "(a,i) => a[i]", ValueType.anyref, ValueType.i32, null, fieldType );
                break;
            default:
        }
        return functionName;
    }

    private String getSciptTypeName() {
        return type.getName().replace( '/', '_' );
    }

    /**
     * Get the StructOperator
     * 
     * @return the operator
     */
    StructOperator getOperator() {
        return op;
    }

    /**
     * Get the struct type of this instruction.
     * 
     * @return the type
     */
    StructType getStructType() {
        return type;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    Type getType() {
        return Type.Struct;
    }

    /**
     * {@inheritDoc}
     */
    public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
        int idx = -1;
        if( type != null && fieldName != null ) {
            // The fieldName of the struct operation does not contain the class name in which the field was declared. It contains the class name of the variable. This can be the class or a subclass.
            List<NamedStorageType> fields = type.getFields();
            boolean classNameMatched = type.getName().equals( fieldName.geClassName() );
            for( int i = fields.size()-1; i >= 0; i-- ) {
                NamedStorageType field = fields.get( i );
                if( !classNameMatched && field.geClassName().equals( fieldName.geClassName() ) ) {
                    classNameMatched = true;
                }
                if( classNameMatched && field.getName().equals( fieldName.getName() ) ) {
                    idx = i;
                    break;
                }
            }
            if( !classNameMatched ) {
                // special case, the type self does not add a needed field, that we search in all fields
                for( int i = fields.size()-1; i >= 0; i-- ) {
                    NamedStorageType field = fields.get( i );
                    if( field.getName().equals( fieldName.getName() ) ) {
                        idx = i;
                        break;
                    }
                }
            }
        }
        if( functionName != null ) { // nonGC
            if( fieldName != null ) {
                writer.writeConst( idx, ValueType.i32 );
            }
            writer.writeFunctionCall( functionName );
        } else {
            writer.writeStructOperator( op, type, fieldName, idx );
        }
    }

    /**
     * {@inheritDoc}
     */
    AnyType getPushValueType() {
        switch( op ) {
            case NULL:
                return ValueType.anyref;
            case NEW:
            case NEW_DEFAULT:
            case GET:
                return type;
            case SET:
                return null;
            default:
                throw new WasmException( "Unknown array operation: " + op, -1 );
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    int getPopCount() {
        switch( op ) {
            case NEW:
            case NEW_DEFAULT:
            case GET:
                return 1;
            case SET:
                return 2;
            case NULL:
                return 0;
            default:
                throw new WasmException( "Unknown array operation: " + op, -1 );
        }
    }
}

@@ -2898,38 +2898,179 @@
 %22 + 
-getSciptTypeName(), () -%3E %22%7B%7D%22
+type.getName().replace( '/', '_' ), () -%3E %7B%0A                    return %22() =%3E %7Breturn %7B0:0%7D%7D%22; //TODO default values of fields and then use Object.seal()%0A                %7D
 , nu
@@ -3294,11 +3294,11 @@
 %22(a,
-i,v
+v,i
 ) =%3E
@@ -3725,106 +3725,8 @@
  %7D%0A%0A
-    private String getSciptTypeName() %7B%0A        return type.getName().replace( '/', '_' );%0A    %7D%0A%0A
     

fix parameter order of nonGC polyfill call