1
1
package org .prism ;
2
2
3
3
import 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 ;
5
6
import com .dylibso .chicory .runtime .ImportValues ;
6
7
import com .dylibso .chicory .runtime .Instance ;
7
8
import com .dylibso .chicory .wasi .WasiOptions ;
8
9
import com .dylibso .chicory .wasi .WasiPreview1 ;
9
10
10
11
import java .nio .charset .StandardCharsets ;
11
12
13
+ @ WasmModuleInterface (WasmResource .absoluteFile )
12
14
public 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
-
20
15
private final WasiPreview1 wasi ;
21
16
private final Instance instance ;
17
+ private final Prism_ModuleExports exports ;
22
18
23
19
public Prism () {
24
20
this (WasiOptions .builder ().build ());
@@ -30,33 +26,52 @@ public Prism(WasiOptions wasiOpts) {
30
26
.withMachineFactory (PrismModule ::create )
31
27
.withImportValues (ImportValues .builder ().addFunction (wasi .toHostFunctions ()).build ())
32
28
.build ();
29
+ exports = new Prism_ModuleExports (instance );
30
+ }
33
31
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 ;
40
34
}
41
35
42
36
public ParseResult serializeParse (byte [] packedOptions , String source ) {
43
37
var sourceBytes = source .getBytes (StandardCharsets .US_ASCII );
44
38
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 );
47
47
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 );
50
50
51
- var bufferPointer = calloc . apply ( pmBufferSizeof . apply ()[ 0 ] , 1 );
52
- pmBufferInit . apply (bufferPointer );
51
+ bufferPointer = exports . calloc ( exports . pmBufferSizeof () , 1 );
52
+ exports . pmBufferInit (bufferPointer );
53
53
54
- pmSerializeParse .apply (
55
- bufferPointer [0 ], sourcePointer [0 ], source .length (), optionsPointer [0 ]);
54
+ exports .pmSerializeParse (bufferPointer , sourcePointer , source .length (), optionsPointer );
56
55
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
+ }
60
75
61
76
return Loader .load (result , sourceBytes );
62
77
}
0 commit comments