mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-15 10:44:47 +01:00
implements struct.get/set
This commit is contained in:
parent
98f6265abd
commit
caa59cab83
@ -838,7 +838,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected void writeStructOperator( StructOperator op, StorageType type ) throws IOException {
|
||||
protected void writeStructOperator( StructOperator op, StorageType type, String fieldName ) throws IOException {
|
||||
int opCode;
|
||||
switch(op) {
|
||||
case NEW:
|
||||
@ -862,5 +862,9 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
||||
if( type != null ) {
|
||||
codeStream.writeValueType( type );
|
||||
}
|
||||
if( fieldName != null ) {
|
||||
StructTypeEntry entry = (StructTypeEntry)functionTypes.get( type.getCode() );
|
||||
codeStream.writeVaruint32( entry.getFieldIdx( fieldName ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,6 +18,9 @@ package de.inetsoftware.jwebassembly.binary;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import de.inetsoftware.jwebassembly.WasmException;
|
||||
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
|
||||
import de.inetsoftware.jwebassembly.wasm.ValueType;
|
||||
|
||||
@ -60,6 +63,23 @@ class StructTypeEntry extends TypeEntry {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the index of the field in this structure
|
||||
*
|
||||
* @param fieldName
|
||||
* the name of the field
|
||||
* @return the index
|
||||
*/
|
||||
int getFieldIdx( @Nonnull String fieldName ) {
|
||||
for( int i = 0; i < fields.size(); i++ ) {
|
||||
NamedStorageType field = fields.get( i );
|
||||
if( field.name.equals( fieldName ) ) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
throw new WasmException( fieldName + " not found", -1 );
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
@ -90,7 +90,7 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder {
|
||||
case 0: // nop
|
||||
break;
|
||||
case 1: // aconst_null
|
||||
addStructInstruction( StructOperator.NULL, null, codePos );
|
||||
addStructInstruction( StructOperator.NULL, null, null, codePos );
|
||||
break;
|
||||
case 2: // iconst_m1
|
||||
case 3: // iconst_0
|
||||
@ -532,8 +532,14 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder {
|
||||
ref = (ConstantRef)constantPool.get( byteCode.readUnsignedShort() );
|
||||
addGlobalInstruction( false, ref, codePos );
|
||||
break;
|
||||
//TODO case 180: // getfield
|
||||
//TODO case 181: // putfield
|
||||
case 180: // getfield
|
||||
ref = (ConstantRef)constantPool.get( byteCode.readUnsignedShort() );
|
||||
addStructInstruction( StructOperator.GET, ref.getClassName(), ref.getName(), codePos );
|
||||
break;
|
||||
case 181: // putfield
|
||||
ref = (ConstantRef)constantPool.get( byteCode.readUnsignedShort() );
|
||||
addStructInstruction( StructOperator.SET, ref.getClassName(), ref.getName(), codePos );
|
||||
break;
|
||||
//TODO case 182: // invokevirtual
|
||||
case 183: // invokespecial, invoke a constructor
|
||||
case 184: // invokestatic
|
||||
@ -545,7 +551,7 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder {
|
||||
//TODO case 186: // invokedynamic
|
||||
case 187: // new
|
||||
String name = ((ConstantClass)constantPool.get( byteCode.readUnsignedShort() )).getName();
|
||||
addStructInstruction( StructOperator.NEW_DEFAULT, name, codePos );
|
||||
addStructInstruction( StructOperator.NEW_DEFAULT, name, null, codePos );
|
||||
break;
|
||||
case 188: // newarray
|
||||
int typeValue = byteCode.readByte();
|
||||
|
@ -173,7 +173,7 @@ public class ModuleGenerator {
|
||||
throw new WasmException( "Missing class: " + className, -1 );
|
||||
}
|
||||
ClassFile classFile = new ClassFile( stream );
|
||||
ArrayList<NamedStorageType> list = new ArrayList<>();
|
||||
List<NamedStorageType> list = new ArrayList<>();
|
||||
FieldInfo[] fields = classFile.getFields();
|
||||
for( FieldInfo field : fields ) {
|
||||
if( field.isStatic() ) {
|
||||
|
@ -236,8 +236,9 @@ public abstract class ModuleWriter implements Closeable {
|
||||
* the operation
|
||||
* @param type
|
||||
* the type of the struct
|
||||
* @param fieldName TODO
|
||||
* @throws IOException
|
||||
* if any I/O error occur
|
||||
*/
|
||||
protected abstract void writeStructOperator( StructOperator op, StorageType type ) throws IOException;
|
||||
protected abstract void writeStructOperator( StructOperator op, StorageType type, String fieldName ) throws IOException;
|
||||
}
|
||||
|
@ -233,10 +233,12 @@ public abstract class WasmCodeBuilder {
|
||||
* the operation
|
||||
* @param typeName
|
||||
* the type name
|
||||
* @param fieldName
|
||||
* the name of field if needed for the operation
|
||||
* @param javaCodePos
|
||||
* the code position/offset in the Java method
|
||||
*/
|
||||
protected void addStructInstruction( StructOperator op, String typeName, int javaCodePos ) {
|
||||
instructions.add( new WasmStructInstruction( op, typeName, javaCodePos ) );
|
||||
protected void addStructInstruction( StructOperator op, @Nullable String typeName, @Nullable String fieldName, int javaCodePos ) {
|
||||
instructions.add( new WasmStructInstruction( op, typeName, fieldName, javaCodePos ) );
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
Copyright 2018 Volker Berlin (i-net software)
|
||||
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.
|
||||
@ -41,6 +41,8 @@ class WasmStructInstruction extends WasmInstruction {
|
||||
|
||||
private StorageType type;
|
||||
|
||||
private final String fieldName;
|
||||
|
||||
/**
|
||||
* Create an instance of numeric operation.
|
||||
*
|
||||
@ -48,13 +50,16 @@ class WasmStructInstruction extends WasmInstruction {
|
||||
* 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
|
||||
*/
|
||||
WasmStructInstruction( @Nullable StructOperator op, @Nullable String typeName, int javaCodePos ) {
|
||||
WasmStructInstruction( @Nullable StructOperator op, @Nullable String typeName, @Nullable String fieldName, int javaCodePos ) {
|
||||
super( javaCodePos );
|
||||
this.op = op;
|
||||
this.typeName = typeName;
|
||||
this.fieldName = fieldName;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -88,7 +93,7 @@ class WasmStructInstruction extends WasmInstruction {
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
|
||||
writer.writeStructOperator( op, type );
|
||||
writer.writeStructOperator( op, type, fieldName );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -257,8 +257,10 @@ public class TextModuleWriter extends ModuleWriter {
|
||||
case ref_eq:
|
||||
methodOutput.append( "ref.eq" );
|
||||
return;
|
||||
default:
|
||||
}
|
||||
break;
|
||||
default:
|
||||
}
|
||||
methodOutput.append( valueType ).append( '.' ).append( op );
|
||||
}
|
||||
@ -320,8 +322,10 @@ public class TextModuleWriter extends ModuleWriter {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a newline the insets.
|
||||
* Add a newline with the insets.
|
||||
*
|
||||
* @param output
|
||||
* the target
|
||||
* @throws IOException
|
||||
* if any I/O error occur
|
||||
*/
|
||||
@ -445,7 +449,7 @@ public class TextModuleWriter extends ModuleWriter {
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected void writeStructOperator( StructOperator op, StorageType type ) throws IOException {
|
||||
protected void writeStructOperator( StructOperator op, StorageType type, String fieldName ) throws IOException {
|
||||
String operation;
|
||||
switch( op ) {
|
||||
case NEW:
|
||||
@ -470,13 +474,10 @@ public class TextModuleWriter extends ModuleWriter {
|
||||
newline( methodOutput );
|
||||
methodOutput.append( operation );
|
||||
if( type != null ) {
|
||||
int typeCode = type.getCode();
|
||||
methodOutput.append( ' ' );
|
||||
if( typeCode < 0 ) {
|
||||
methodOutput.append( type );
|
||||
} else {
|
||||
methodOutput.append( typeCode );
|
||||
}
|
||||
methodOutput.append( ' ' ).append( type );
|
||||
}
|
||||
if( fieldName != null ) {
|
||||
methodOutput.append( " $" ).append( fieldName );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user