Skip to content

Commit 83735fc

Browse files
committed
Merge DefaultWasmGlobal into WasmGlobal.
1 parent b8b324a commit 83735fc

File tree

5 files changed

+83
-153
lines changed

5 files changed

+83
-153
lines changed

wasm/src/org.graalvm.wasm.test/src/org/graalvm/wasm/test/WasmJsApiSuite.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
import org.graalvm.wasm.constants.Sizes;
8080
import org.graalvm.wasm.exception.WasmException;
8181
import org.graalvm.wasm.exception.WasmJsApiException;
82-
import org.graalvm.wasm.globals.DefaultWasmGlobal;
8382
import org.graalvm.wasm.globals.WasmGlobal;
8483
import org.graalvm.wasm.memory.WasmMemory;
8584
import org.graalvm.wasm.memory.WasmMemoryLibrary;
@@ -504,7 +503,7 @@ public void testGlobalWriteNull() throws IOException {
504503
public void testGlobalWriteAnyfuncRefTypesDisabled() throws IOException {
505504
runTest(WasmJsApiSuite::disableRefTypes, context -> {
506505
final WebAssembly wasm = new WebAssembly(context);
507-
final WasmGlobal global = new DefaultWasmGlobal(ValueType.anyfunc, true, WasmConstant.NULL);
506+
final WasmGlobal global = new WasmGlobal(ValueType.anyfunc, true, WasmConstant.NULL);
508507
try {
509508
wasm.globalWrite(global, WasmConstant.NULL);
510509
Assert.fail("Should have failed - ref types not enabled");
@@ -518,7 +517,7 @@ public void testGlobalWriteAnyfuncRefTypesDisabled() throws IOException {
518517
public void testGlobalWriteExternrefRefTypesDisabled() throws IOException {
519518
runTest(WasmJsApiSuite::disableRefTypes, context -> {
520519
final WebAssembly wasm = new WebAssembly(context);
521-
final WasmGlobal global = new DefaultWasmGlobal(ValueType.externref, true, WasmConstant.NULL);
520+
final WasmGlobal global = new WasmGlobal(ValueType.externref, true, WasmConstant.NULL);
522521
try {
523522
wasm.globalWrite(global, WasmConstant.NULL);
524523
Assert.fail("Should have failed - ref types not enabled");

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/Linker.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@
9595
import org.graalvm.wasm.constants.GlobalModifier;
9696
import org.graalvm.wasm.exception.Failure;
9797
import org.graalvm.wasm.exception.WasmException;
98-
import org.graalvm.wasm.globals.DefaultWasmGlobal;
9998
import org.graalvm.wasm.globals.WasmGlobal;
10099
import org.graalvm.wasm.memory.WasmMemory;
101100
import org.graalvm.wasm.memory.WasmMemoryLibrary;
@@ -391,7 +390,7 @@ private static void initializeGlobal(WasmInstance instance, int globalIndex, Obj
391390
assert !instance.globals().isInitialized(globalIndex) : globalIndex;
392391
SymbolTable symbolTable = instance.symbolTable();
393392
if (symbolTable.globalExternal(globalIndex)) {
394-
var global = new DefaultWasmGlobal(ValueType.fromByteValue(symbolTable.globalValueType(globalIndex)), symbolTable.isGlobalMutable(globalIndex), initValue);
393+
var global = new WasmGlobal(ValueType.fromByteValue(symbolTable.globalValueType(globalIndex)), symbolTable.isGlobalMutable(globalIndex), initValue);
395394
instance.setExternalGlobal(globalIndex, global);
396395
} else {
397396
instance.globals().store(symbolTable.globalValueType(globalIndex), symbolTable.globalAddress(globalIndex), initValue);

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/api/WebAssembly.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
import org.graalvm.wasm.exception.Failure;
6969
import org.graalvm.wasm.exception.WasmException;
7070
import org.graalvm.wasm.exception.WasmJsApiException;
71-
import org.graalvm.wasm.globals.DefaultWasmGlobal;
7271
import org.graalvm.wasm.globals.WasmGlobal;
7372
import org.graalvm.wasm.memory.WasmMemory;
7473
import org.graalvm.wasm.memory.WasmMemoryFactory;
@@ -792,23 +791,23 @@ public WasmGlobal globalAlloc(ValueType valueType, boolean mutable, Object value
792791
try {
793792
switch (valueType) {
794793
case i32:
795-
return new DefaultWasmGlobal(valueType, mutable, valueInterop.asInt(value));
794+
return new WasmGlobal(valueType, mutable, valueInterop.asInt(value));
796795
case i64:
797-
return new DefaultWasmGlobal(valueType, mutable, valueInterop.asLong(value));
796+
return new WasmGlobal(valueType, mutable, valueInterop.asLong(value));
798797
case f32:
799-
return new DefaultWasmGlobal(valueType, mutable, Float.floatToRawIntBits(valueInterop.asFloat(value)));
798+
return new WasmGlobal(valueType, mutable, Float.floatToRawIntBits(valueInterop.asFloat(value)));
800799
case f64:
801-
return new DefaultWasmGlobal(valueType, mutable, Double.doubleToRawLongBits(valueInterop.asDouble(value)));
800+
return new WasmGlobal(valueType, mutable, Double.doubleToRawLongBits(valueInterop.asDouble(value)));
802801
case anyfunc:
803802
if (!refTypes || !(value == WasmConstant.NULL || value instanceof WasmFunctionInstance)) {
804803
throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "Invalid value type");
805804
}
806-
return new DefaultWasmGlobal(valueType, mutable, value);
805+
return new WasmGlobal(valueType, mutable, value);
807806
case externref:
808807
if (!refTypes) {
809808
throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "Invalid value type");
810809
}
811-
return new DefaultWasmGlobal(valueType, mutable, value);
810+
return new WasmGlobal(valueType, mutable, value);
812811
default:
813812
throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "Invalid value type");
814813
}

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/globals/DefaultWasmGlobal.java

Lines changed: 0 additions & 121 deletions
This file was deleted.

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/globals/WasmGlobal.java

Lines changed: 74 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,60 +60,114 @@
6060

6161
@SuppressWarnings("static-method")
6262
@ExportLibrary(InteropLibrary.class)
63-
public abstract class WasmGlobal extends EmbedderDataHolder implements TruffleObject {
63+
public final class WasmGlobal extends EmbedderDataHolder implements TruffleObject {
6464

6565
private final ValueType valueType;
6666
private final boolean mutable;
6767

68-
protected WasmGlobal(ValueType valueType, boolean mutable) {
68+
private long globalValue;
69+
private Object globalObjectValue;
70+
71+
private WasmGlobal(ValueType valueType, boolean mutable) {
6972
this.valueType = valueType;
7073
this.mutable = mutable;
7174
}
7275

73-
public final ValueType getValueType() {
76+
public WasmGlobal(ValueType valueType, boolean mutable, int value) {
77+
this(valueType, mutable, (long) value);
78+
}
79+
80+
public WasmGlobal(ValueType valueType, boolean mutable, long value) {
81+
this(valueType, mutable);
82+
assert ValueType.isNumberType(getValueType());
83+
this.globalValue = value;
84+
}
85+
86+
public WasmGlobal(ValueType valueType, boolean mutable, Object value) {
87+
this(valueType, mutable);
88+
this.globalValue = switch (valueType) {
89+
case i32 -> (int) value;
90+
case i64 -> (long) value;
91+
case f32 -> Float.floatToRawIntBits((float) value);
92+
case f64 -> Double.doubleToRawLongBits((double) value);
93+
default -> 0;
94+
};
95+
this.globalObjectValue = switch (valueType) {
96+
case v128, anyfunc, externref -> value;
97+
default -> null;
98+
};
99+
}
100+
101+
public ValueType getValueType() {
74102
return valueType;
75103
}
76104

77-
public final boolean isMutable() {
105+
public boolean isMutable() {
78106
return mutable;
79107
}
80108

81-
public final byte getMutability() {
109+
public byte getMutability() {
82110
return mutable ? GlobalModifier.MUTABLE : GlobalModifier.CONSTANT;
83111
}
84112

85-
public abstract int loadAsInt();
113+
public int loadAsInt() {
114+
assert ValueType.isNumberType(getValueType());
115+
return (int) globalValue;
116+
}
86117

87-
public abstract long loadAsLong();
118+
public long loadAsLong() {
119+
assert ValueType.isNumberType(getValueType());
120+
return globalValue;
121+
}
88122

89-
public abstract Vector128 loadAsVector128();
123+
public Vector128 loadAsVector128() {
124+
assert ValueType.isVectorType(getValueType());
125+
assert globalObjectValue != null;
126+
return (Vector128) globalObjectValue;
127+
}
90128

91-
public abstract Object loadAsReference();
129+
public Object loadAsReference() {
130+
assert ValueType.isReferenceType(getValueType());
131+
assert globalObjectValue != null;
132+
return globalObjectValue;
133+
}
92134

93-
public abstract void storeInt(int value);
135+
public void storeInt(int value) {
136+
assert ValueType.isNumberType(getValueType());
137+
this.globalValue = value;
138+
}
94139

95-
public abstract void storeLong(long value);
140+
public void storeLong(long value) {
141+
assert ValueType.isNumberType(getValueType());
142+
this.globalValue = value;
143+
}
96144

97-
public abstract void storeVector128(Vector128 value);
145+
public void storeVector128(Vector128 value) {
146+
assert ValueType.isVectorType(getValueType());
147+
this.globalObjectValue = value;
148+
}
98149

99-
public abstract void storeReference(Object value);
150+
public void storeReference(Object value) {
151+
assert ValueType.isReferenceType(getValueType());
152+
this.globalObjectValue = value;
153+
}
100154

101155
public static final String VALUE_MEMBER = "value";
102156

103157
@ExportMessage
104-
final boolean hasMembers() {
158+
boolean hasMembers() {
105159
return true;
106160
}
107161

108162
@ExportMessage
109163
@TruffleBoundary
110-
final boolean isMemberReadable(String member) {
164+
boolean isMemberReadable(String member) {
111165
return VALUE_MEMBER.equals(member);
112166
}
113167

114168
@ExportMessage
115169
@TruffleBoundary
116-
final Object readMember(String member) throws UnknownIdentifierException {
170+
Object readMember(String member) throws UnknownIdentifierException {
117171
if (!isMemberReadable(member)) {
118172
throw UnknownIdentifierException.create(member);
119173
}
@@ -130,19 +184,19 @@ final Object readMember(String member) throws UnknownIdentifierException {
130184

131185
@ExportMessage
132186
@TruffleBoundary
133-
final boolean isMemberModifiable(String member) {
187+
boolean isMemberModifiable(String member) {
134188
return VALUE_MEMBER.equals(member) && isMutable();
135189
}
136190

137191
@ExportMessage
138192
@TruffleBoundary
139-
final boolean isMemberInsertable(@SuppressWarnings("unused") String member) {
193+
boolean isMemberInsertable(@SuppressWarnings("unused") String member) {
140194
return false;
141195
}
142196

143197
@ExportMessage
144198
@TruffleBoundary
145-
final void writeMember(String member, Object value,
199+
void writeMember(String member, Object value,
146200
@CachedLibrary(limit = "5") InteropLibrary valueLibrary) throws UnknownIdentifierException, UnsupportedMessageException {
147201
if (!isMemberReadable(member)) {
148202
throw UnknownIdentifierException.create(member);
@@ -179,7 +233,7 @@ final void writeMember(String member, Object value,
179233

180234
@ExportMessage
181235
@TruffleBoundary
182-
final Object getMembers(@SuppressWarnings("unused") boolean includeInternal) {
236+
Object getMembers(@SuppressWarnings("unused") boolean includeInternal) {
183237
return new WasmNamesObject(new String[]{VALUE_MEMBER});
184238
}
185239
}

0 commit comments

Comments
 (0)