JWebAssembly/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java

123 lines
3.1 KiB
Java
Raw Normal View History

2018-12-05 23:30:46 +01:00
/*
2019-01-13 11:36:07 +01:00
Copyright 2018 - 2019 Volker Berlin (i-net software)
2018-12-05 23:30:46 +01:00
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 javax.annotation.Nonnull;
import javax.annotation.Nullable;
import de.inetsoftware.jwebassembly.WasmException;
import de.inetsoftware.jwebassembly.module.TypeManager.StructType;
2019-01-14 20:09:00 +01:00
import de.inetsoftware.jwebassembly.wasm.AnyType;
2018-12-05 23:30:46 +01:00
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;
2018-12-05 23:30:46 +01:00
2019-01-13 11:36:07 +01:00
private final String fieldName;
2018-12-05 23:30:46 +01:00
/**
* Create an instance of numeric operation.
*
* @param op
* the struct operation
* @param type
* the type of the parameters
2019-01-13 11:36:07 +01:00
* @param fieldName
* the name of field if needed for the operation
2018-12-05 23:30:46 +01:00
* @param javaCodePos
* the code position/offset in the Java method
*/
WasmStructInstruction( @Nullable StructOperator op, @Nullable StructType type, @Nullable String fieldName, int javaCodePos ) {
2018-12-05 23:30:46 +01:00
super( javaCodePos );
this.op = op;
this.type = type;
2019-01-13 11:36:07 +01:00
this.fieldName = fieldName;
2018-12-05 23:30:46 +01:00
}
2018-12-19 20:10:26 +01:00
/**
* Get the struct type of this instruction.
2018-12-19 20:10:26 +01:00
*
* @return the type
*/
StructType getStructType() {
return type;
2018-12-19 20:10:26 +01:00
}
2018-12-16 18:22:44 +01:00
/**
* {@inheritDoc}
*/
@Override
Type getType() {
return Type.Struct;
}
2018-12-05 23:30:46 +01:00
/**
* {@inheritDoc}
*/
public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
2019-01-13 11:36:07 +01:00
writer.writeStructOperator( op, type, fieldName );
2018-12-05 23:30:46 +01:00
}
/**
* {@inheritDoc}
*/
AnyType getPushValueType() {
2018-12-05 23:30:46 +01:00
switch( op ) {
2019-01-20 11:31:33 +01:00
case NULL:
2018-12-05 23:30:46 +01:00
return ValueType.anyref;
case NEW:
case NEW_DEFAULT:
2018-12-05 23:30:46 +01:00
case GET:
return type;
2018-12-05 23:30:46 +01:00
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:
case NULL:
2018-12-05 23:30:46 +01:00
return 0;
default:
throw new WasmException( "Unknown array operation: " + op, -1 );
}
}
}