Skip to content

Commit

Permalink
Implemented final Wasm GC binary and textual format.
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkosertic committed Nov 30, 2023
1 parent 20dbc76 commit 87d0f4d
Show file tree
Hide file tree
Showing 17 changed files with 37 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,22 @@ public class Cast implements WasmValue {
@Override
public void writeTo(final TextWriter writer, final ExportContext context) throws IOException {
writer.opening();
writer.write("ref.cast_static $");
writer.write("ref.cast");
writer.space();
writer.opening();
writer.write("ref null $");
writer.write(structType.getName());
writer.closing();
writer.space();
source.writeTo(writer, context);
writer.closing();

}

@Override
public void writeTo(final BinaryWriter.Writer binaryWriter, final ExportContext context) throws IOException {
source.writeTo(binaryWriter, context);
binaryWriter.writeByte((byte) 0xfb);
binaryWriter.writeByte((byte) 0x45);
binaryWriter.writeByte((byte) 0x17);
binaryWriter.writeSignedLeb128(structType.index());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ public static GetStruct get(final StructType structType, final WasmValue source,
public static class array {

public static WasmValue newInstance(final WasmType type, final List<WasmValue> arguments) {
return new NewWasmArrayStaticInit(type, arguments);
return new NewWasmArrayFixed(type, arguments);
}

public static WasmValue newInstanceDefault(final WasmType type, final WasmValue length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void writeTo(final TextWriter writer, final ExportContext context) throws
public void writeTo(final BinaryWriter.Writer binaryWriter, final ExportContext context) throws IOException {
source.writeTo(binaryWriter, context);
binaryWriter.writeByte((byte) 0xfb);
binaryWriter.writeByte((byte) 0x03);
binaryWriter.writeByte((byte) 0x02);
binaryWriter.writeUnsignedLeb128(structType.index());
binaryWriter.writeUnsignedLeb128(structType.indexOfField(fieldName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void writeTo(final BinaryWriter.Writer binaryWriter, final ExportContext
array.writeTo(binaryWriter, context);
index.writeTo(binaryWriter, context);
binaryWriter.writeByte((byte) 0xfb);
binaryWriter.writeByte((byte) 0x13);
binaryWriter.writeByte((byte) 0x0b);
binaryWriter.writeUnsignedLeb128(type.index());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public void writeTo(final TextWriter writer, final ExportContext context) throws
public void writeTo(final BinaryWriter.Writer binaryWriter, final ExportContext context) throws IOException {
array.writeTo(binaryWriter, context);
binaryWriter.writeByte((byte) 0xfb);
binaryWriter.writeByte((byte) 0x19);
binaryWriter.writeByte((byte) 0x0f);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void writeTo(final BinaryWriter.Writer binaryWriter, final ExportContext
arg.writeTo(binaryWriter, context);
}
binaryWriter.writeByte((byte) 0xfb);
binaryWriter.writeByte((byte) 0x07);
binaryWriter.writeByte((byte) 0x00);
binaryWriter.writeUnsignedLeb128(type.index());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void writeTo(final TextWriter writer, final ExportContext context) throws
public void writeTo(final BinaryWriter.Writer binaryWriter, final ExportContext context) throws IOException {
length.writeTo(binaryWriter, context);
binaryWriter.writeByte((byte) 0xfb);
binaryWriter.writeByte((byte) 0x1c);
binaryWriter.writeByte((byte) 0x07);
binaryWriter.writeUnsignedLeb128(type.index());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
import java.io.IOException;
import java.util.List;

public class NewWasmArrayStaticInit implements WasmValue {
public class NewWasmArrayFixed implements WasmValue {

private final WasmType type;

private final List<WasmValue> arguments;

NewWasmArrayStaticInit(final WasmType type, final List<WasmValue> arguments) {
NewWasmArrayFixed(final WasmType type, final List<WasmValue> arguments) {
this.type = type;
this.arguments = arguments;
}

@Override
public void writeTo(final TextWriter writer, final ExportContext context) throws IOException {
writer.opening();
writer.write("array.init_static ");
writer.write("array.new_fixed ");
type.writeRefTo(writer);
for (final WasmValue arg : arguments) {
writer.space();
Expand All @@ -32,7 +32,7 @@ public void writeTo(final BinaryWriter.Writer binaryWriter, final ExportContext
arg.writeTo(binaryWriter, context);
}
binaryWriter.writeByte((byte) 0xfb);
binaryWriter.writeByte((byte) 0x1a);
binaryWriter.writeByte((byte) 0x08);
binaryWriter.writeUnsignedLeb128(type.index());
binaryWriter.writeUnsignedLeb128(arguments.size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
public class RefEq extends BinaryExpression {

RefEq(final WasmValue left, final WasmValue right) {
super(left, right, "ref.eq", (byte) 0xd5);
super(left, right, "ref.eq", (byte) 0xd3);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ public void writeRefTo(final TextWriter writer) {
@Override
public void writeTo(final BinaryWriter.Writer writer) throws IOException {
if (nullable) {
writer.writeByte((byte) 0x6c);
writer.writeByte((byte) 0x63);
} else {
writer.writeByte((byte) 0x6b);
writer.writeByte((byte) 0x64);
}
writer.writeSignedLeb128(type.index());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void writeTo(final BinaryWriter.Writer binaryWriter, final ExportContext
target.writeTo(binaryWriter, context);
value.writeTo(binaryWriter, context);
binaryWriter.writeByte((byte) 0xfb);
binaryWriter.writeByte((byte) 0x06);
binaryWriter.writeByte((byte) 0x05);
binaryWriter.writeUnsignedLeb128(structType.index());
binaryWriter.writeUnsignedLeb128(structType.indexOfField(fieldName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void writeTo(final BinaryWriter.Writer binaryWriter, final ExportContext
index.writeTo(binaryWriter, context);
value.writeTo(binaryWriter, context);
binaryWriter.writeByte((byte) 0xfb);
binaryWriter.writeByte((byte) 0x16);
binaryWriter.writeByte((byte) 0x0e);
binaryWriter.writeUnsignedLeb128(type.index());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,18 @@ public void writeTo(final TextWriter writer) {
writer.space();

writer.opening();
writer.write("struct_subtype");
writer.write("sub");
writer.space();
writer.write("$");
writer.write(supertype.name);
writer.space();
writer.opening();
writer.write("struct");
for (final Field field : fields) {
writer.space();
field.writeTo(writer);
}
writer.space();
writer.write("$");
writer.write(supertype.name);
writer.closing();
writer.closing();

writer.closing();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,17 @@ public void writeTo(final TextWriter writer) {
writer.write(name);
writer.space();
writer.opening();
writer.write("sub");
writer.space();
writer.opening();
writer.write("struct");
for (final Field field : fields) {
writer.space();
field.writeTo(writer);
}
writer.closing();
writer.closing();
writer.closing();
}

@Override
Expand All @@ -104,6 +108,8 @@ public void writeRefTo(final TextWriter writer) {

@Override
public void writeTo(final BinaryWriter.Writer writer) throws IOException {
writer.writeByte((byte) 0x50);
writer.writeByte((byte) 0);
writer.writeByte(PrimitiveType.struct.getBinaryType());
writer.writeByte((byte) fields.size());
for (final Field f : fields) {
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public void writeTo(final TextWriter writer, final ExportContext context) {
@Override
public void writeTo(final BinaryWriter.Writer binaryWriter, final ExportContext context) {
binaryWriter.writeByte((byte) 0xd0);
binaryWriter.writeByte((byte) 0x69);
binaryWriter.writeByte((byte) 0x72);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public void writeTo(final TextWriter writer, final ExportContext context) {
@Override
public void writeTo(final BinaryWriter.Writer binaryWriter, final ExportContext context) {
binaryWriter.writeByte((byte) 0xd0);
binaryWriter.writeByte((byte) 0x65);
binaryWriter.writeByte((byte) 0x71);
}
}

0 comments on commit 87d0f4d

Please sign in to comment.