From 8d8351deaf51a98363010536d193709fefde8351 Mon Sep 17 00:00:00 2001 From: sal Date: Sun, 28 Nov 2021 19:53:14 +0800 Subject: [PATCH] Update README.md --- README.md | 46 +++++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 94665d8..ef59977 100644 --- a/README.md +++ b/README.md @@ -22,30 +22,23 @@ package com.github.salpadding.wasmer.example; import com.github.salpadding.wasmer.*; -import kotlin.Pair; -import org.jetbrains.annotations.NotNull; import java.util.Arrays; import java.util.Collections; import java.util.List; -// a host function example + class MemoryPeek implements HostFunction { - @NotNull @Override - // name of host function public String getName() { return "__peek"; } - @NotNull @Override - // args are aligned to long - public long[] execute(@NotNull Instance inst, @NotNull long[] args) { + public long[] execute(Instance inst, long[] args) { int off = (int) args[0]; int len = (int) args[1]; - // read data from memory byte[] data = inst.getMemory("memory").read(off, len); for (byte b : data) { @@ -53,49 +46,51 @@ class MemoryPeek implements HostFunction { } System.out.println(); - return HostFunction.getEmptyLongs(); + return Instance.EMPTY_LONGS; + } + + @Override + public List getParams() { + return Arrays.asList(ValType.I32, ValType.I32); } - @NotNull @Override - public Pair, List> getSignature() { - return new Pair<>( - Arrays.asList(ValType.I32, ValType.I32), - Collections.emptyList() - ); + public List getRet() { + return Collections.emptyList(); } } class EmptyHost implements HostFunction { - private String name; + private final String name; public EmptyHost(String name) { this.name = name; } - @NotNull @Override public String getName() { return name; } - @NotNull @Override - public long[] execute(@NotNull Instance inst, @NotNull long[] args) { + public long[] execute(Instance inst, long[] args) { System.out.println("empty host function executed"); - return HostFunction.getEmptyLongs(); + return EMPTY_LONGS; } - @NotNull @Override - public Pair, List> getSignature() { - return new Pair<>(Collections.singletonList(ValType.I64), Collections.emptyList()); + public List getParams() { + return Collections.singletonList(ValType.I64); + } + + @Override + public List getRet() { + return Collections.emptyList(); } } public class Example { public static void main(String[] args) { - // initialize Native class Natives.initialize(1024); byte[] bin = TestUtil.readClassPathFile("testdata/wasm.wasm"); Instance ins = Instance.create(bin, Options.empty(), Arrays.asList(new EmptyHost("alert"), new MemoryPeek())); @@ -113,4 +108,5 @@ public class Example { } } } + ``` \ No newline at end of file