11package dev .openfeature .contrib .providers .gofeatureflag .wasm ;
22
3+ import com .dylibso .chicory .runtime .ByteArrayMemory ;
34import com .dylibso .chicory .runtime .ExportFunction ;
45import com .dylibso .chicory .runtime .HostFunction ;
6+ import com .dylibso .chicory .runtime .ImportFunction ;
7+ import com .dylibso .chicory .runtime .ImportValues ;
58import com .dylibso .chicory .runtime .Instance ;
69import com .dylibso .chicory .runtime .Memory ;
7- import com .dylibso .chicory .runtime .Store ;
810import com .dylibso .chicory .wasi .WasiExitException ;
911import com .dylibso .chicory .wasi .WasiOptions ;
1012import com .dylibso .chicory .wasi .WasiPreview1 ;
11- import com .dylibso .chicory .wasm .Parser ;
1213import com .dylibso .chicory .wasm .types .ValueType ;
1314import dev .openfeature .contrib .providers .gofeatureflag .bean .GoFeatureFlagResponse ;
1415import dev .openfeature .contrib .providers .gofeatureflag .exception .WasmFileNotFound ;
1516import dev .openfeature .contrib .providers .gofeatureflag .util .Const ;
1617import dev .openfeature .contrib .providers .gofeatureflag .wasm .bean .WasmInput ;
1718import dev .openfeature .sdk .ErrorCode ;
1819import dev .openfeature .sdk .Reason ;
19- import java .io .InputStream ;
2020import java .nio .charset .StandardCharsets ;
21+ import java .util .Arrays ;
2122import java .util .Collections ;
23+ import java .util .List ;
24+ import java .util .stream .Collectors ;
2225import lombok .val ;
2326
2427/**
@@ -42,36 +45,21 @@ public EvaluationWasm() throws WasmFileNotFound {
4245 val wasi = WasiPreview1 .builder ()
4346 .withOptions (WasiOptions .builder ().inheritSystem ().build ())
4447 .build ();
45- val hostFunctions = wasi .toHostFunctions ();
46- val store = new Store ().addFunction (hostFunctions );
47- store .addFunction (getProcExitFunc ());
48- this .instance = store .instantiate ("evaluation" , Parser .parse (getWasmFile ()));
48+ List <ImportFunction > hostFunctions =
49+ Arrays .stream (wasi .toHostFunctions ()).map (this ::replaceProcExit ).collect (Collectors .toList ());
50+ this .instance = Instance .builder (Module .load ())
51+ .withMemoryFactory (ByteArrayMemory ::new )
52+ .withMachineFactory (Module ::create )
53+ .withImportValues (
54+ ImportValues .builder ().withFunctions (hostFunctions ).build ())
55+ .build ();
4956 this .evaluate = this .instance .export ("evaluate" );
5057 this .malloc = this .instance .export ("malloc" );
5158 this .free = this .instance .export ("free" );
5259 }
5360
54- /**
55- * getWasmFile is a function that returns the path to the WASM file.
56- * It looks for the file in the classpath under the directory "wasm".
57- * This method handles both file system resources and JAR-packaged resources.
58- *
59- * @return the path to the WASM file
60- * @throws WasmFileNotFound - if the file is not found
61- */
62- private InputStream getWasmFile () throws WasmFileNotFound {
63- try {
64- final String wasmResourcePath = "wasm/gofeatureflag-evaluation.wasi" ;
65- InputStream inputStream = EvaluationWasm .class .getClassLoader ().getResourceAsStream (wasmResourcePath );
66- if (inputStream == null ) {
67- throw new WasmFileNotFound ("WASM resource not found in classpath: " + wasmResourcePath );
68- }
69- return inputStream ;
70- } catch (WasmFileNotFound e ) {
71- throw e ;
72- } catch (Exception e ) {
73- throw new WasmFileNotFound (e );
74- }
61+ private ImportFunction replaceProcExit (HostFunction hf ) {
62+ return hf .name ().equals ("proc_exit" ) ? getProcExitFunc () : hf ;
7563 }
7664
7765 /**
@@ -81,7 +69,7 @@ private InputStream getWasmFile() throws WasmFileNotFound {
8169 *
8270 * @return a HostFunction that is called when the WASM module calls proc_exit
8371 */
84- private HostFunction getProcExitFunc () {
72+ private ImportFunction getProcExitFunc () {
8573 return new HostFunction (
8674 "wasi_snapshot_preview1" ,
8775 "proc_exit" ,
0 commit comments