Skip to content

Commit 4803d60

Browse files
committed
feat: compile go-feature-flag to Java bytecode
Signed-off-by: andreatp <[email protected]>
1 parent 0b720f3 commit 4803d60

File tree

4 files changed

+39
-45
lines changed

4 files changed

+39
-45
lines changed

providers/go-feature-flag/pom.xml

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
<properties>
2020
<!-- override module name defined in parent ("-" is not allowed) -->
2121
<module-name>${groupId}.gofeatureflag</module-name>
22+
23+
<wasm.version>v1.45.4</wasm.version>
2224
</properties>
2325

2426
<developers>
@@ -74,16 +76,10 @@
7476
<scope>test</scope>
7577
</dependency>
7678

77-
<dependency>
78-
<groupId>com.dylibso.chicory</groupId>
79-
<artifactId>runtime</artifactId>
80-
<version>1.5.0</version>
81-
</dependency>
82-
8379
<dependency>
8480
<groupId>com.dylibso.chicory</groupId>
8581
<artifactId>wasi</artifactId>
86-
<version>1.5.0</version>
82+
<version>1.6.0</version>
8783
</dependency>
8884

8985
<dependency>
@@ -117,20 +113,18 @@
117113
<build>
118114
<plugins>
119115
<plugin>
120-
<groupId>org.codehaus.mojo</groupId>
121-
<artifactId>exec-maven-plugin</artifactId>
116+
<groupId>com.dylibso.chicory</groupId>
117+
<artifactId>chicory-compiler-maven-plugin</artifactId>
118+
<version>1.6.0</version>
122119
<executions>
123120
<execution>
124-
<id>download-latest-github-asset</id>
125-
<phase>generate-resources</phase>
121+
<id>gofeatureflag</id>
126122
<goals>
127-
<goal>exec</goal>
123+
<goal>compile</goal>
128124
</goals>
129125
<configuration>
130-
<executable>bash</executable>
131-
<arguments>
132-
<argument>${project.basedir}/download-wasm.sh</argument>
133-
</arguments>
126+
<name>dev.openfeature.contrib.providers.gofeatureflag.wasm.Module</name>
127+
<wasmFile>wasm-releases/evaluation/gofeatureflag-evaluation_${wasm.version}.wasi</wasmFile>
134128
</configuration>
135129
</execution>
136130
</executions>

providers/go-feature-flag/src/main/java/dev/openfeature/contrib/providers/gofeatureflag/wasm/EvaluationWasm.java

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
package dev.openfeature.contrib.providers.gofeatureflag.wasm;
22

3+
import com.dylibso.chicory.runtime.ByteArrayMemory;
34
import com.dylibso.chicory.runtime.ExportFunction;
45
import com.dylibso.chicory.runtime.HostFunction;
6+
import com.dylibso.chicory.runtime.ImportFunction;
7+
import com.dylibso.chicory.runtime.ImportValues;
58
import com.dylibso.chicory.runtime.Instance;
69
import com.dylibso.chicory.runtime.Memory;
7-
import com.dylibso.chicory.runtime.Store;
810
import com.dylibso.chicory.wasi.WasiExitException;
911
import com.dylibso.chicory.wasi.WasiOptions;
1012
import com.dylibso.chicory.wasi.WasiPreview1;
11-
import com.dylibso.chicory.wasm.Parser;
1213
import com.dylibso.chicory.wasm.types.ValueType;
1314
import dev.openfeature.contrib.providers.gofeatureflag.bean.GoFeatureFlagResponse;
1415
import dev.openfeature.contrib.providers.gofeatureflag.exception.WasmFileNotFound;
1516
import dev.openfeature.contrib.providers.gofeatureflag.util.Const;
1617
import dev.openfeature.contrib.providers.gofeatureflag.wasm.bean.WasmInput;
1718
import dev.openfeature.sdk.ErrorCode;
1819
import dev.openfeature.sdk.Reason;
19-
import java.io.InputStream;
2020
import java.nio.charset.StandardCharsets;
21+
import java.util.Arrays;
2122
import java.util.Collections;
23+
import java.util.List;
24+
import java.util.stream.Collectors;
2225
import 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",
Binary file not shown.

spotbugs-exclusions.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
<Bug pattern="PREDICTABLE_RANDOM" />
1818
</Match>
1919

20+
<!-- Suppress: EI_EXPOSE_REP2 in ModuleMachine -->
21+
<Match>
22+
<Class name="dev.openfeature.contrib.providers.gofeatureflag.wasm.ModuleMachine"/>
23+
<Bug pattern="EI_EXPOSE_REP2"/>
24+
</Match>
25+
26+
<!-- Suppress: SKIPPED_CLASS_TOO_BIG in ModuleMachineFuncGroup_0 -->
27+
<Match>
28+
<Class name="dev.openfeature.contrib.providers.gofeatureflag.wasm.ModuleMachineFuncGroup_0"/>
29+
<Bug pattern="SKIPPED_CLASS_TOO_BIG"/>
30+
</Match>
31+
2032
<!-- All bugs in test classes, except for JUnit-specific bugs -->
2133
<Match>
2234
<Class name="~.*\.*Test" />

0 commit comments

Comments
 (0)