Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 10 additions & 16 deletions providers/go-feature-flag/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
<properties>
<!-- override module name defined in parent ("-" is not allowed) -->
<module-name>${groupId}.gofeatureflag</module-name>

<wasm.version>v1.45.4</wasm.version>
</properties>

<developers>
Expand Down Expand Up @@ -74,16 +76,10 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>runtime</artifactId>
<version>1.5.0</version>
</dependency>

<dependency>
<groupId>com.dylibso.chicory</groupId>
<artifactId>wasi</artifactId>
<version>1.5.0</version>
<version>1.6.0</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -117,20 +113,18 @@
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<groupId>com.dylibso.chicory</groupId>
<artifactId>chicory-compiler-maven-plugin</artifactId>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For build reproducibility, it's a best practice to explicitly specify the version for Maven plugins. Since the wasi dependency is at version 1.6.0, it's likely this plugin should be pinned to the same version.

                <artifactId>chicory-compiler-maven-plugin</artifactId>
                <version>1.6.0</version>

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

<version>1.6.0</version>
<executions>
<execution>
<id>download-latest-github-asset</id>
<phase>generate-resources</phase>
<id>gofeatureflag</id>
<goals>
<goal>exec</goal>
<goal>compile</goal>
</goals>
<configuration>
<executable>bash</executable>
<arguments>
<argument>${project.basedir}/download-wasm.sh</argument>
</arguments>
<name>dev.openfeature.contrib.providers.gofeatureflag.wasm.Module</name>
<wasmFile>wasm-releases/evaluation/gofeatureflag-evaluation_${wasm.version}.wasi</wasmFile>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
package dev.openfeature.contrib.providers.gofeatureflag.wasm;

import com.dylibso.chicory.runtime.ByteArrayMemory;
import com.dylibso.chicory.runtime.ExportFunction;
import com.dylibso.chicory.runtime.HostFunction;
import com.dylibso.chicory.runtime.ImportFunction;
import com.dylibso.chicory.runtime.ImportValues;
import com.dylibso.chicory.runtime.Instance;
import com.dylibso.chicory.runtime.Memory;
import com.dylibso.chicory.runtime.Store;
import com.dylibso.chicory.wasi.WasiExitException;
import com.dylibso.chicory.wasi.WasiOptions;
import com.dylibso.chicory.wasi.WasiPreview1;
import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.wasm.types.ValueType;
import dev.openfeature.contrib.providers.gofeatureflag.bean.GoFeatureFlagResponse;
import dev.openfeature.contrib.providers.gofeatureflag.exception.WasmFileNotFound;
import dev.openfeature.contrib.providers.gofeatureflag.util.Const;
import dev.openfeature.contrib.providers.gofeatureflag.wasm.bean.WasmInput;
import dev.openfeature.sdk.ErrorCode;
import dev.openfeature.sdk.Reason;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import lombok.val;

/**
Expand All @@ -42,36 +45,21 @@ public EvaluationWasm() throws WasmFileNotFound {
val wasi = WasiPreview1.builder()
.withOptions(WasiOptions.builder().inheritSystem().build())
.build();
val hostFunctions = wasi.toHostFunctions();
val store = new Store().addFunction(hostFunctions);
store.addFunction(getProcExitFunc());
this.instance = store.instantiate("evaluation", Parser.parse(getWasmFile()));
List<ImportFunction> hostFunctions =
Arrays.stream(wasi.toHostFunctions()).map(this::replaceProcExit).collect(Collectors.toList());
this.instance = Instance.builder(Module.load())
.withMemoryFactory(ByteArrayMemory::new)
.withMachineFactory(Module::create)
.withImportValues(
ImportValues.builder().withFunctions(hostFunctions).build())
.build();
this.evaluate = this.instance.export("evaluate");
this.malloc = this.instance.export("malloc");
this.free = this.instance.export("free");
}

/**
* getWasmFile is a function that returns the path to the WASM file.
* It looks for the file in the classpath under the directory "wasm".
* This method handles both file system resources and JAR-packaged resources.
*
* @return the path to the WASM file
* @throws WasmFileNotFound - if the file is not found
*/
private InputStream getWasmFile() throws WasmFileNotFound {
try {
final String wasmResourcePath = "wasm/gofeatureflag-evaluation.wasi";
InputStream inputStream = EvaluationWasm.class.getClassLoader().getResourceAsStream(wasmResourcePath);
if (inputStream == null) {
throw new WasmFileNotFound("WASM resource not found in classpath: " + wasmResourcePath);
}
return inputStream;
} catch (WasmFileNotFound e) {
throw e;
} catch (Exception e) {
throw new WasmFileNotFound(e);
}
private ImportFunction replaceProcExit(HostFunction hf) {
return hf.name().equals("proc_exit") ? getProcExitFunc() : hf;
}

/**
Expand All @@ -81,7 +69,7 @@ private InputStream getWasmFile() throws WasmFileNotFound {
*
* @return a HostFunction that is called when the WASM module calls proc_exit
*/
private HostFunction getProcExitFunc() {
private ImportFunction getProcExitFunc() {
return new HostFunction(
"wasi_snapshot_preview1",
"proc_exit",
Expand Down
Binary file not shown.
12 changes: 12 additions & 0 deletions spotbugs-exclusions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
<Bug pattern="PREDICTABLE_RANDOM" />
</Match>

<!-- Suppress: EI_EXPOSE_REP2 in ModuleMachine -->
<Match>
<Class name="dev.openfeature.contrib.providers.gofeatureflag.wasm.ModuleMachine"/>
<Bug pattern="EI_EXPOSE_REP2"/>
</Match>

<!-- Suppress: SKIPPED_CLASS_TOO_BIG in ModuleMachineFuncGroup_0 -->
<Match>
<Class name="dev.openfeature.contrib.providers.gofeatureflag.wasm.ModuleMachineFuncGroup_0"/>
<Bug pattern="SKIPPED_CLASS_TOO_BIG"/>
</Match>

<!-- All bugs in test classes, except for JUnit-specific bugs -->
<Match>
<Class name="~.*\.*Test" />
Expand Down