diff --git a/core/src/main/java/org/teavm/wasm/binary/BinaryWriter.java b/core/src/main/java/org/teavm/wasm/binary/BinaryWriter.java index 9aae24ebb..7a4aa2b12 100644 --- a/core/src/main/java/org/teavm/wasm/binary/BinaryWriter.java +++ b/core/src/main/java/org/teavm/wasm/binary/BinaryWriter.java @@ -123,41 +123,17 @@ public class BinaryWriter { result[offset++] = (byte) (v >> 8); result[offset++] = (byte) v; } else if (type == DataPrimitives.INT) { - offset = align(offset, 4); - int v = value.getInt(0); - result[offset++] = (byte) (v >> 24); - result[offset++] = (byte) (v >> 16); - result[offset++] = (byte) (v >> 8); - result[offset++] = (byte) v; + offset = writeInt(offset, result, value.getInt(0)); } else if (type == DataPrimitives.LONG) { - offset = align(offset, 8); - long v = value.getInt(0); - result[offset++] = (byte) (v >> 56); - result[offset++] = (byte) (v >> 48); - result[offset++] = (byte) (v >> 40); - result[offset++] = (byte) (v >> 32); - result[offset++] = (byte) (v >> 24); - result[offset++] = (byte) (v >> 16); - result[offset++] = (byte) (v >> 8); - result[offset++] = (byte) v; + offset = writeLong(offset, result, value.getLong(0)); } else if (type == DataPrimitives.FLOAT) { - offset = align(offset, 4); - int v = Float.floatToRawIntBits(value.getInt(0)); - result[offset++] = (byte) (v >> 24); - result[offset++] = (byte) (v >> 16); - result[offset++] = (byte) (v >> 8); - result[offset++] = (byte) v; + int v = Float.floatToRawIntBits(value.getFloat(0)); + offset = writeInt(offset, result, v); } else if (type == DataPrimitives.DOUBLE) { - offset = align(offset, 8); long v = Double.doubleToRawLongBits(value.getDouble(0)); - result[offset++] = (byte) (v >> 56); - result[offset++] = (byte) (v >> 48); - result[offset++] = (byte) (v >> 40); - result[offset++] = (byte) (v >> 32); - result[offset++] = (byte) (v >> 24); - result[offset++] = (byte) (v >> 16); - result[offset++] = (byte) (v >> 8); - result[offset++] = (byte) v; + offset = writeLong(offset, result, v); + } else if (type == DataPrimitives.ADDRESS) { + offset = writeInt(offset, result, (int) value.getAddress(0)); } else if (type instanceof DataArray) { DataArray array = (DataArray) type; for (int i = 0; i < array.getSize(); ++i) { @@ -176,6 +152,28 @@ public class BinaryWriter { return offset; } + private int writeInt(int offset, byte[] result, int v) { + offset = align(offset, 4); + result[offset++] = (byte) (v >> 24); + result[offset++] = (byte) (v >> 16); + result[offset++] = (byte) (v >> 8); + result[offset++] = (byte) v; + return offset; + } + + private int writeLong(int offset, byte[] result, long v) { + offset = align(offset, 8); + result[offset++] = (byte) (v >> 56); + result[offset++] = (byte) (v >> 48); + result[offset++] = (byte) (v >> 40); + result[offset++] = (byte) (v >> 32); + result[offset++] = (byte) (v >> 24); + result[offset++] = (byte) (v >> 16); + result[offset++] = (byte) (v >> 8); + result[offset++] = (byte) v; + return offset; + } + public byte[] getMetadata() { MetadataWriter writer = new MetadataWriter(); writer.write(values.stream().map(value -> value.getType()).toArray(DataType[]::new)); diff --git a/core/src/main/java/org/teavm/wasm/generate/WasmClassGenerator.java b/core/src/main/java/org/teavm/wasm/generate/WasmClassGenerator.java index 645dae616..46877a1de 100644 --- a/core/src/main/java/org/teavm/wasm/generate/WasmClassGenerator.java +++ b/core/src/main/java/org/teavm/wasm/generate/WasmClassGenerator.java @@ -177,9 +177,8 @@ public class WasmClassGenerator { for (FieldReader field : cls.getFields()) { int desiredAlignment = getDesiredAlignment(field.getType()); if (field.hasModifier(ElementModifier.STATIC)) { - /*int offset = align(address, desiredAlignment); - data.fieldLayout.put(field.getName(), offset); - address = offset + desiredAlignment;*/ + DataType type = asDataType(field.getType()); + data.fieldLayout.put(field.getName(), binaryWriter.append(type.createValue())); } else { int offset = align(data.size, desiredAlignment); data.fieldLayout.put(field.getName(), offset); @@ -188,6 +187,28 @@ public class WasmClassGenerator { } } + private static DataType asDataType(ValueType type) { + if (type instanceof ValueType.Primitive) { + switch (((ValueType.Primitive) type).getKind()) { + case BOOLEAN: + case BYTE: + return DataPrimitives.BYTE; + case SHORT: + case CHARACTER: + return DataPrimitives.SHORT; + case INTEGER: + return DataPrimitives.INT; + case LONG: + return DataPrimitives.LONG; + case FLOAT: + return DataPrimitives.FLOAT; + case DOUBLE: + return DataPrimitives.DOUBLE; + } + } + return DataPrimitives.ADDRESS; + } + private static int align(int base, int alignment) { if (base == 0) { return 0;