4

从java中调用webassembly函数

 1 year ago
source link: https://www.jdon.com/63932
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

从java中调用webassembly函数


existism插件框架套件刚刚发布了他们最初的 java 支持。
使用@extism 插件库中的java host-sdk,您现在可以在基于jvm 的应用程序中运行webassembly
可以点击标题找到文档和示例。


对于 Maven,添加到您的pom.xml:

<dependency>
  <groupId>org.extism.sdk</groupId>
  <artifactId>extism</artifactId>
  <version>0.1.0</version>
</dependency>

package example;

import org.extism.sdk.Context;
import org.extism.sdk.manifest.Manifest;
import org.extism.sdk.wasm.WasmSourceResolver;

import java.nio.file.Path;

public class App 
{
    public static void main(String[] args)
    {
        var resolver = new WasmSourceResolver();
        var manifest = new Manifest(resolver.resolve(Path.of("./code.wasm")));

        try (var ctx = new Context(); var plugin = ctx.newPlugin(manifest, false)) 
        {
            var output = plugin.call("count_vowels", "Hello World");
            System.out.println(output);
        }
    }
}

在这个例子中code.wasm是我们计算元音的示例插件。如果你想运行这个,先下载它并设置路径:

curl https://raw.githubusercontent.com/extism/extism/main/wasm/code.wasm > code.wasm

运行Java应用输出:
{"count": 3}

有关更多的示例,请参阅https://github.com/thomasdarimont/extism-java-example


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK