|
1 | 1 | package org.prism; |
2 | 2 |
|
3 | | -import com.dylibso.chicory.annotations.WasmModuleInterface; |
4 | 3 | import com.dylibso.chicory.runtime.ByteArrayMemory; |
5 | | -import com.dylibso.chicory.runtime.ExportFunction; |
6 | 4 | import com.dylibso.chicory.runtime.ImportValues; |
7 | 5 | import com.dylibso.chicory.runtime.Instance; |
8 | | -import com.dylibso.chicory.runtime.WasmRuntimeException; |
9 | | -import com.dylibso.chicory.wasi.WasiOptions; |
10 | | -import com.dylibso.chicory.wasi.WasiPreview1; |
| 6 | +import com.dylibso.chicory.runtime.InterpreterMachine; |
| 7 | +import com.dylibso.chicory.wasm.Parser; |
11 | 8 | import com.dylibso.chicory.wasm.WasmModule; |
12 | | -import com.dylibso.chicory.wasm.types.MemoryLimits; |
13 | 9 |
|
14 | | -import java.nio.charset.StandardCharsets; |
15 | | - |
16 | | -@WasmModuleInterface(WasmResource.absoluteFile) |
17 | 10 | public class PrismWASM extends Prism { |
18 | | - private final ExportFunction calloc; |
19 | | - private final ExportFunction pmSerializeParse; |
20 | | - private final ExportFunction pmBufferInit; |
21 | | - private final ExportFunction pmBufferSizeof; |
22 | | - private final ExportFunction pmBufferValue; |
23 | | - private final ExportFunction pmBufferLength; |
24 | | - |
| 11 | + private final Prism_ModuleExports exports; |
25 | 12 | private final Instance instance; |
26 | 13 |
|
27 | 14 | public PrismWASM() { |
28 | 15 | super(); |
29 | | - instance = Instance.builder(PrismParser.load()) |
| 16 | + WasmModule module = Parser.parse( |
| 17 | + PrismWASM.class.getResourceAsStream("/prism.wasm")); |
| 18 | + instance = Instance.builder(module) |
30 | 19 | .withMemoryFactory(ByteArrayMemory::new) |
31 | | - .withMachineFactory(PrismParser::create) |
| 20 | + .withMachineFactory(InterpreterMachine::new) |
32 | 21 | .withImportValues(ImportValues.builder().addFunction(wasi.toHostFunctions()).build()) |
33 | 22 | .build(); |
34 | | - |
35 | | - calloc = instance.exports().function("calloc"); |
36 | | - pmSerializeParse = instance.exports().function("pm_serialize_parse"); |
37 | | - pmBufferInit = instance.exports().function("pm_buffer_init"); |
38 | | - pmBufferSizeof = instance.exports().function("pm_buffer_sizeof"); |
39 | | - pmBufferValue = instance.exports().function("pm_buffer_value"); |
40 | | - pmBufferLength = instance.exports().function("pm_buffer_length"); |
| 23 | + exports = new Prism_ModuleExports(instance); |
41 | 24 | } |
42 | 25 |
|
43 | 26 | @Override |
44 | 27 | public byte[] serialize(byte[] packedOptions, byte[] sourceBytes, int sourceLength) { |
45 | | - var sourcePointer = calloc.apply(1, sourceLength); |
46 | | - instance.memory().write((int) sourcePointer[0], sourceBytes, 0, sourceLength); |
| 28 | + int sourcePointer = 0; |
| 29 | + int optionsPointer = 0; |
| 30 | + int bufferPointer = 0; |
| 31 | + byte[] result; |
| 32 | + try { |
| 33 | + sourcePointer = exports.calloc(1, sourceLength + 1); |
| 34 | + instance.memory().write(sourcePointer, sourceBytes, 0, sourceLength); |
| 35 | + instance.memory().writeByte(sourcePointer + sourceLength, (byte) 0); |
47 | 36 |
|
48 | | - var optionsPointer = calloc.apply(1, packedOptions.length); |
49 | | - instance.memory().write((int) optionsPointer[0], packedOptions); |
| 37 | + optionsPointer = exports.calloc(1, packedOptions.length); |
| 38 | + instance.memory().write(optionsPointer, packedOptions); |
50 | 39 |
|
51 | | - var bufferPointer = calloc.apply(pmBufferSizeof.apply()[0], 1); |
52 | | - pmBufferInit.apply(bufferPointer); |
| 40 | + bufferPointer = exports.calloc(exports.pmBufferSizeof(), 1); |
| 41 | + exports.pmBufferInit(bufferPointer); |
53 | 42 |
|
54 | | - pmSerializeParse.apply( |
55 | | - bufferPointer[0], sourcePointer[0], sourceLength, optionsPointer[0]); |
| 43 | + exports.pmSerializeParse( |
| 44 | + bufferPointer, sourcePointer, sourceLength, optionsPointer); |
56 | 45 |
|
57 | | - var result = instance.memory().readBytes( |
58 | | - (int) pmBufferValue.apply(bufferPointer[0])[0], |
59 | | - (int) pmBufferLength.apply(bufferPointer[0])[0]); |
| 46 | + result = instance.memory().readBytes( |
| 47 | + exports.pmBufferValue(bufferPointer), |
| 48 | + exports.pmBufferLength(bufferPointer)); |
| 49 | + } finally { |
| 50 | + if (sourcePointer != 0) { |
| 51 | + exports.free(sourcePointer); |
| 52 | + } |
| 53 | + if (optionsPointer != 0) { |
| 54 | + exports.free(optionsPointer); |
| 55 | + } |
| 56 | + if (bufferPointer != 0) { |
| 57 | + exports.pmBufferFree(bufferPointer); |
| 58 | + exports.free(bufferPointer); |
| 59 | + } |
| 60 | + } |
60 | 61 |
|
61 | 62 | return result; |
62 | 63 | } |
63 | 64 |
|
64 | | - @Override |
65 | | - public void close() { |
66 | | - if (wasi != null) { |
67 | | - wasi.close(); |
68 | | - } |
| 65 | + // DEBUG |
| 66 | + public int memorySize() { |
| 67 | + return instance.memory().pages(); |
69 | 68 | } |
70 | 69 | } |
0 commit comments