diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index 5e905aa..98ce9b5 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -243,6 +243,24 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod codeStream.writeVarint( value ); } + /** + * {@inheritDoc} + */ + @Override + protected void writeConstFloat( float value ) throws IOException { + codeStream.write( F32_CONST ); + codeStream.writeFloat( value ); + } + + /** + * {@inheritDoc} + */ + @Override + protected void writeConstDouble( double value ) throws IOException { + codeStream.write( F64_CONST ); + codeStream.writeDouble( value ); + } + /** * {@inheritDoc} */ diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java index af2248d..0f4490e 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java @@ -249,8 +249,11 @@ public abstract class ModuleWriter implements Closeable { case 16: //bipush writeConstInt( byteCode.readByte() ); break; + case 18: //ldc + writeConst( constantPool.get( byteCode.readUnsignedByte() ) ); + break; case 20: //ldc2_w - writeConstLong( (Long)constantPool.get( byteCode.readUnsignedShort() ) ); + writeConst( constantPool.get( byteCode.readUnsignedShort() ) ); break; case 26: // iload_0 case 27: // iload_1 @@ -269,6 +272,8 @@ public abstract class ModuleWriter implements Closeable { break; case 172: // ireturn case 173: // lreturn + case 174: // freturn + case 175: // dreturn case 177: // return void writeReturn(); break; @@ -281,6 +286,21 @@ public abstract class ModuleWriter implements Closeable { } } + private void writeConst( Object value ) throws IOException, WasmException { + Class clazz = value.getClass(); + if( clazz == Integer.class ) { + writeConstInt( ((Integer)value).intValue() ); + } else if( clazz == Long.class ) { + writeConstLong( ((Long)value).longValue() ); + } else if( clazz == Float.class ) { + writeConstFloat( ((Float)value).floatValue() ); + } else if( clazz == Double.class ) { + writeConstDouble( ((Double)value).doubleValue() ); + } else { + throw new WasmException( "Not supported constant type: " + clazz, -1 ); + } + } + /** * Write a constant integer value * @@ -301,6 +321,26 @@ public abstract class ModuleWriter implements Closeable { */ protected abstract void writeConstLong( long value ) throws IOException; + /** + * Write a constant float value + * + * @param value + * the value + * @throws IOException + * if any I/O error occur + */ + protected abstract void writeConstFloat( float value ) throws IOException; + + /** + * Write a constant double value + * + * @param value + * the value + * @throws IOException + * if any I/O error occur + */ + protected abstract void writeConstDouble( double value ) throws IOException; + /** * Write or Load a local variable. * diff --git a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java index a273468..58a12bc 100644 --- a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java @@ -120,6 +120,24 @@ public class TextModuleWriter extends ModuleWriter { methodOutput.append( "i64.const " ).append( Long.toString( value ) ); } + /** + * {@inheritDoc} + */ + @Override + protected void writeConstFloat( float value ) throws IOException { + newline( methodOutput ); + methodOutput.append( "f32.const " ).append( Float.toString( value ) ); + } + + /** + * {@inheritDoc} + */ + @Override + protected void writeConstDouble( double value ) throws IOException { + newline( methodOutput ); + methodOutput.append( "f64.const " ).append( Double.toString( value ) ); + } + /** * {@inheritDoc} */