11package org .prism ;
22
33import com .dylibso .chicory .runtime .ByteArrayMemory ;
4- import com .dylibso .chicory .runtime .ExportFunction ;
4+ import com .dylibso .chicory .experimental .hostmodule .annotations .WasmModuleInterface ;
5+ import com .dylibso .chicory .runtime .ByteArrayMemory ;
56import com .dylibso .chicory .runtime .ImportValues ;
67import com .dylibso .chicory .runtime .Instance ;
78import com .dylibso .chicory .wasi .WasiOptions ;
89import com .dylibso .chicory .wasi .WasiPreview1 ;
910
1011import java .nio .charset .StandardCharsets ;
1112
13+ @ WasmModuleInterface (WasmResource .absoluteFile )
1214public class Prism implements AutoCloseable {
13- private final ExportFunction calloc ;
14- private final ExportFunction pmSerializeParse ;
15- private final ExportFunction pmBufferInit ;
16- private final ExportFunction pmBufferSizeof ;
17- private final ExportFunction pmBufferValue ;
18- private final ExportFunction pmBufferLength ;
19-
2015 private final WasiPreview1 wasi ;
2116 private final Instance instance ;
17+ private final Prism_ModuleExports exports ;
2218
2319 public Prism () {
2420 this (WasiOptions .builder ().build ());
@@ -30,33 +26,52 @@ public Prism(WasiOptions wasiOpts) {
3026 .withMachineFactory (PrismModule ::create )
3127 .withImportValues (ImportValues .builder ().addFunction (wasi .toHostFunctions ()).build ())
3228 .build ();
29+ exports = new Prism_ModuleExports (instance );
30+ }
3331
34- calloc = instance .exports ().function ("calloc" );
35- pmSerializeParse = instance .exports ().function ("pm_serialize_parse" );
36- pmBufferInit = instance .exports ().function ("pm_buffer_init" );
37- pmBufferSizeof = instance .exports ().function ("pm_buffer_sizeof" );
38- pmBufferValue = instance .exports ().function ("pm_buffer_value" );
39- pmBufferLength = instance .exports ().function ("pm_buffer_length" );
32+ public Prism_ModuleExports exports () {
33+ return exports ;
4034 }
4135
4236 public ParseResult serializeParse (byte [] packedOptions , String source ) {
4337 var sourceBytes = source .getBytes (StandardCharsets .US_ASCII );
4438
45- var sourcePointer = calloc .apply (1 , source .length ());
46- instance .memory ().writeString ((int ) sourcePointer [0 ], source );
39+ int sourcePointer = 0 ;
40+ int optionsPointer = 0 ;
41+ int bufferPointer = 0 ;
42+ int resultPointer = 0 ;
43+ byte [] result ;
44+ try {
45+ sourcePointer = exports .calloc (1 , source .length ());
46+ exports .memory ().writeString (sourcePointer , source );
4747
48- var optionsPointer = calloc . apply (1 , packedOptions .length );
49- instance .memory ().write (( int ) optionsPointer [ 0 ] , packedOptions );
48+ optionsPointer = exports . calloc (1 , packedOptions .length );
49+ exports .memory ().write (optionsPointer , packedOptions );
5050
51- var bufferPointer = calloc . apply ( pmBufferSizeof . apply ()[ 0 ] , 1 );
52- pmBufferInit . apply (bufferPointer );
51+ bufferPointer = exports . calloc ( exports . pmBufferSizeof () , 1 );
52+ exports . pmBufferInit (bufferPointer );
5353
54- pmSerializeParse .apply (
55- bufferPointer [0 ], sourcePointer [0 ], source .length (), optionsPointer [0 ]);
54+ exports .pmSerializeParse (bufferPointer , sourcePointer , source .length (), optionsPointer );
5655
57- var result = instance .memory ().readBytes (
58- (int ) pmBufferValue .apply (bufferPointer [0 ])[0 ],
59- (int ) pmBufferLength .apply (bufferPointer [0 ])[0 ]);
56+ resultPointer = exports .pmBufferValue (bufferPointer );
57+
58+ result = instance .memory ().readBytes (
59+ resultPointer ,
60+ exports .pmBufferLength (bufferPointer ));
61+ } finally {
62+ if (sourcePointer != 0 ) {
63+ exports .free (sourcePointer );
64+ }
65+ if (optionsPointer != 0 ) {
66+ exports .free (optionsPointer );
67+ }
68+ if (bufferPointer != 0 ) {
69+ exports .free (bufferPointer );
70+ }
71+ if (resultPointer != 0 ) {
72+ exports .free (resultPointer );
73+ }
74+ }
6075
6176 return Loader .load (result , sourceBytes );
6277 }
0 commit comments