Skip to content

Commit

Permalink
GlobalInstance should expose MutabilityType instead of HostGlobal
Browse files Browse the repository at this point in the history
Move the MutabilityType field from HostGlobal to GlobalInstance.
Notice that GlobalInstance has both getters and setters, we
might want to revisit that.

Adjust tests accordingly.

Signed-off-by: Edoardo Vacchi <[email protected]>
  • Loading branch information
evacchi committed Aug 27, 2024
1 parent 781ee2b commit 1a1a381
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ public static HostImports fallback() {
new HostGlobal(
"test",
"global-mut-i32",
new GlobalInstance(Value.i32(0)),
MutabilityType.Var),
new GlobalInstance(Value.i32(0), MutabilityType.Var)),
new HostGlobal(
"test", "g", new GlobalInstance(Value.i32(0)), MutabilityType.Var)
"test", "g", new GlobalInstance(Value.i32(0), MutabilityType.Var))
},
new HostMemory[] {
new HostMemory("spectest", "memory", new Memory(new MemoryLimits(1, 1)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ public static HostImports fallback() {
new HostGlobal(
"test",
"global-mut-i32",
new GlobalInstance(Value.i32(0)),
MutabilityType.Var),
new GlobalInstance(Value.i32(0), MutabilityType.Var)),
new HostGlobal(
"test", "g", new GlobalInstance(Value.i32(0)), MutabilityType.Var))
"test", "g", new GlobalInstance(Value.i32(0), MutabilityType.Var)))
.addTable(
new HostTable(
"spectest",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public static HostImports fallback() {
new HostGlobal(
"test",
"global-mut-i32",
new GlobalInstance(Value.i32(0)),
MutabilityType.Var),
new GlobalInstance(Value.i32(0), MutabilityType.Var)),
new HostGlobal("", "", new GlobalInstance(Value.externRef(0))),
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,7 @@ public static HostImports fallback() {
new HostGlobal(
"test",
"global-mut-i64",
new GlobalInstance(Value.i64(0)),
MutabilityType.Var))
new GlobalInstance(Value.i64(0), MutabilityType.Var)))
.addMemory(
new HostMemory("spectest", "memory", new Memory(new MemoryLimits(1))),
new HostMemory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static HostImports Ng() {
List.of()))
.addGlobal(
new HostGlobal("Mg", "glob", MgInstance.global(0)),
new HostGlobal("Mg", "mut_glob", MgInstance.global(1), MutabilityType.Var))
new HostGlobal("Mg", "mut_glob", MgInstance.global(1)))
.build();
}

Expand Down Expand Up @@ -186,14 +186,12 @@ public static HostImports Mref_im() {
new HostGlobal(
"Mref_ex",
"g-var-func",
new GlobalInstance(Value.funcRef(0)),
MutabilityType.Var))
new GlobalInstance(Value.funcRef(0), MutabilityType.Var)))
.addGlobal(
new HostGlobal(
"Mref_ex",
"g-var-extern",
new GlobalInstance(Value.externRef(0)),
MutabilityType.Var))
new GlobalInstance(Value.externRef(0), MutabilityType.Var)))
.build();
}

Expand Down Expand Up @@ -231,14 +229,12 @@ public static HostImports fallback() {
new HostGlobal(
"Mref_ex",
"g-var-func",
new GlobalInstance(Value.funcRef(0)),
MutabilityType.Var))
new GlobalInstance(Value.funcRef(0), MutabilityType.Var)))
.addGlobal(
new HostGlobal(
"Mref_ex",
"g-var-extern",
new GlobalInstance(Value.externRef(0)),
MutabilityType.Var))
new GlobalInstance(Value.externRef(0), MutabilityType.Var)))
.addTable(
new HostTable(
"Mtable_ex",
Expand All @@ -255,7 +251,7 @@ public static HostImports fallback() {
if (MgInstance != null) {
builder.addGlobal(
new HostGlobal("Mg", "glob", MgInstance.global(0)),
new HostGlobal("Mg", "mut_glob", MgInstance.global(1), MutabilityType.Var));
new HostGlobal("Mg", "mut_glob", MgInstance.global(1)));
}

if (MsInstance != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ public static HostImports fallback() {
new HostGlobal(
"test",
"global-mut-i32",
new GlobalInstance(Value.i32(0)),
MutabilityType.Var),
new GlobalInstance(Value.i32(0), MutabilityType.Var)),
new HostGlobal(
"test", "g", new GlobalInstance(Value.i32(0)), MutabilityType.Var)
"test", "g", new GlobalInstance(Value.i32(0), MutabilityType.Var))
},
new HostMemory[] {
new HostMemory("spectest", "memory", new Memory(new MemoryLimits(1, 1)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ public static HostImports fallback() {
new HostGlobal(
"test",
"global-mut-i32",
new GlobalInstance(Value.i32(0)),
MutabilityType.Var),
new GlobalInstance(Value.i32(0), MutabilityType.Var)),
new HostGlobal(
"test", "g", new GlobalInstance(Value.i32(0)), MutabilityType.Var))
"test", "g", new GlobalInstance(Value.i32(0), MutabilityType.Var)))
.addTable(
new HostTable(
"spectest",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public static HostImports fallback() {
new HostGlobal(
"test",
"global-mut-i32",
new GlobalInstance(Value.i32(0)),
MutabilityType.Var),
new GlobalInstance(Value.i32(0), MutabilityType.Var)),
new HostGlobal("", "", new GlobalInstance(Value.externRef(0))),
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,7 @@ public static HostImports fallback() {
new HostGlobal(
"test",
"global-mut-i64",
new GlobalInstance(Value.i64(0)),
MutabilityType.Var))
new GlobalInstance(Value.i64(0), MutabilityType.Var)))
.addMemory(
new HostMemory("spectest", "memory", new Memory(new MemoryLimits(1))),
new HostMemory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static HostImports Ng() {
List.of()))
.addGlobal(
new HostGlobal("Mg", "glob", MgInstance.global(0)),
new HostGlobal("Mg", "mut_glob", MgInstance.global(1), MutabilityType.Var))
new HostGlobal("Mg", "mut_glob", MgInstance.global(1)))
.build();
}

Expand Down Expand Up @@ -186,14 +186,12 @@ public static HostImports Mref_im() {
new HostGlobal(
"Mref_ex",
"g-var-func",
new GlobalInstance(Value.funcRef(0)),
MutabilityType.Var))
new GlobalInstance(Value.funcRef(0), MutabilityType.Var)))
.addGlobal(
new HostGlobal(
"Mref_ex",
"g-var-extern",
new GlobalInstance(Value.externRef(0)),
MutabilityType.Var))
new GlobalInstance(Value.externRef(0), MutabilityType.Var)))
.build();
}

Expand Down Expand Up @@ -231,14 +229,12 @@ public static HostImports fallback() {
new HostGlobal(
"Mref_ex",
"g-var-func",
new GlobalInstance(Value.funcRef(0)),
MutabilityType.Var))
new GlobalInstance(Value.funcRef(0), MutabilityType.Var)))
.addGlobal(
new HostGlobal(
"Mref_ex",
"g-var-extern",
new GlobalInstance(Value.externRef(0)),
MutabilityType.Var))
new GlobalInstance(Value.externRef(0), MutabilityType.Var)))
.addTable(
new HostTable(
"Mtable_ex",
Expand All @@ -255,7 +251,7 @@ public static HostImports fallback() {
if (MgInstance != null) {
builder.addGlobal(
new HostGlobal("Mg", "glob", MgInstance.global(0)),
new HostGlobal("Mg", "mut_glob", MgInstance.global(1), MutabilityType.Var));
new HostGlobal("Mg", "mut_glob", MgInstance.global(1)));
}

if (MsInstance != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static Value computeConstantValue(Instance instance, List<Instruction> ex
{
var idx = (int) instruction.operands()[0];
if (idx < instance.imports().globalCount()) {
if (instance.imports().global(idx).mutabilityType()
if (instance.imports().global(idx).instance().getMutabilityType()
!= MutabilityType.Const) {
throw new InvalidException(
"constant expression required, initializer expression"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package com.dylibso.chicory.runtime;

import com.dylibso.chicory.wasm.types.MutabilityType;
import com.dylibso.chicory.wasm.types.Value;

public class GlobalInstance {
private Value value;
private Instance instance;
private MutabilityType mutabilityType;

public GlobalInstance(Value value, MutabilityType mutabilityType) {
this.value = value;
this.mutabilityType = mutabilityType;
}

public GlobalInstance(Value value) {
this.value = value;
this.mutabilityType = MutabilityType.Const;
}

public Value getValue() {
Expand All @@ -25,4 +33,12 @@ public Instance getInstance() {
public void setInstance(Instance instance) {
this.instance = instance;
}

public MutabilityType getMutabilityType() {
return mutabilityType;
}

public void setMutabilityType(MutabilityType mutabilityType) {
this.mutabilityType = mutabilityType;
}
}
13 changes: 0 additions & 13 deletions runtime/src/main/java/com/dylibso/chicory/runtime/HostGlobal.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
package com.dylibso.chicory.runtime;

import com.dylibso.chicory.wasm.types.MutabilityType;

public class HostGlobal implements FromHost {
private final GlobalInstance instance;
private final MutabilityType type;
private final String moduleName;
private final String fieldName;

public HostGlobal(String moduleName, String fieldName, GlobalInstance instance) {
this(moduleName, fieldName, instance, MutabilityType.Const);
}

public HostGlobal(
String moduleName, String fieldName, GlobalInstance instance, MutabilityType type) {
this.instance = instance;
this.type = type;
this.moduleName = moduleName;
this.fieldName = fieldName;
}
Expand All @@ -24,10 +15,6 @@ public GlobalInstance instance() {
return instance;
}

public MutabilityType mutabilityType() {
return type;
}

@Override
public String moduleName() {
return moduleName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public Instance initialize(boolean start) {
throw new InvalidException(
"type mismatch, expected: " + g.valueType() + ", got: " + value.type());
}
globals[i] = new GlobalInstance(value);
globals[i] = new GlobalInstance(value, g.mutabilityType());
globals[i].setInstance(this);
}

Expand Down Expand Up @@ -431,7 +431,7 @@ private void validateHostFunctionSignature(FunctionImport imprt, HostFunction f)

private void validateHostGlobalType(GlobalImport i, HostGlobal g) {
if (i.type() != g.instance().getValue().type()
|| i.mutabilityType() != g.mutabilityType()) {
|| i.mutabilityType() != g.instance().getMutabilityType()) {
throw new UnlinkableException("incompatible import type");
}
}
Expand Down

0 comments on commit 1a1a381

Please sign in to comment.