Skip to content

Commit a3326c0

Browse files
authored
Merge pull request #3537 from enebo/topic/javawasm_changes
[java-wasm] Allow serialize to be called separately from serializeParse.
2 parents 1dc3675 + 915a3d2 commit a3326c0

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

java-wasm/src/main/java/org/prism/Prism.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import com.dylibso.chicory.runtime.ByteArrayMemory;
44
import com.dylibso.chicory.experimental.hostmodule.annotations.WasmModuleInterface;
5-
import com.dylibso.chicory.runtime.ByteArrayMemory;
65
import com.dylibso.chicory.runtime.ImportValues;
76
import com.dylibso.chicory.runtime.Instance;
87
import com.dylibso.chicory.wasi.WasiOptions;
98
import com.dylibso.chicory.wasi.WasiPreview1;
9+
import com.dylibso.chicory.wasm.types.MemoryLimits;
1010

1111
import java.nio.charset.StandardCharsets;
1212

@@ -22,7 +22,7 @@ public Prism() {
2222
public Prism(WasiOptions wasiOpts) {
2323
wasi = WasiPreview1.builder().withOptions(wasiOpts).build();
2424
instance = Instance.builder(PrismModule.load())
25-
.withMemoryFactory(ByteArrayMemory::new)
25+
.withMemoryFactory(limits -> new ByteArrayMemory(new MemoryLimits(10, MemoryLimits.MAX_PAGES)))
2626
.withMachineFactory(PrismModule::create)
2727
.withImportValues(ImportValues.builder().addFunction(wasi.toHostFunctions()).build())
2828
.build();
@@ -33,25 +33,23 @@ public Prism_ModuleExports exports() {
3333
return exports;
3434
}
3535

36-
public ParseResult serializeParse(byte[] packedOptions, String source) {
37-
var sourceBytes = source.getBytes(StandardCharsets.US_ASCII);
38-
36+
public byte[] serialize(byte[] packedOptions, byte[] source, int sourceLength) {
3937
int sourcePointer = 0;
4038
int optionsPointer = 0;
4139
int bufferPointer = 0;
4240
int resultPointer = 0;
4341
byte[] result;
4442
try {
45-
sourcePointer = exports.calloc(1, source.length());
46-
exports.memory().writeString(sourcePointer, source);
43+
sourcePointer = exports.calloc(1, sourceLength);
44+
exports.memory().write(sourcePointer, source);
4745

4846
optionsPointer = exports.calloc(1, packedOptions.length);
4947
exports.memory().write(optionsPointer, packedOptions);
5048

5149
bufferPointer = exports.calloc(exports.pmBufferSizeof(), 1);
5250
exports.pmBufferInit(bufferPointer);
5351

54-
exports.pmSerializeParse(bufferPointer, sourcePointer, source.length(), optionsPointer);
52+
exports.pmSerializeParse(bufferPointer, sourcePointer, sourceLength, optionsPointer);
5553

5654
resultPointer = exports.pmBufferValue(bufferPointer);
5755

@@ -73,6 +71,14 @@ public ParseResult serializeParse(byte[] packedOptions, String source) {
7371
}
7472
}
7573

74+
return result;
75+
}
76+
77+
public ParseResult serializeParse(byte[] packedOptions, String source) {
78+
var sourceBytes = source.getBytes(StandardCharsets.US_ASCII);
79+
80+
byte[] result = serialize(packedOptions, sourceBytes, sourceBytes.length);
81+
7682
return Loader.load(result, sourceBytes);
7783
}
7884

0 commit comments

Comments
 (0)