Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Salpadding committed Nov 28, 2021
1 parent 0094d1f commit 8d8351d
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,80 +22,75 @@ 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) {
System.out.print(Integer.toString(b & 0xff, 16));
}

System.out.println();
return HostFunction.getEmptyLongs();
return Instance.EMPTY_LONGS;
}

@Override
public List<ValType> getParams() {
return Arrays.asList(ValType.I32, ValType.I32);
}

@NotNull
@Override
public Pair<List<ValType>, List<ValType>> getSignature() {
return new Pair<>(
Arrays.asList(ValType.I32, ValType.I32),
Collections.emptyList()
);
public List<ValType> 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<ValType>, List<ValType>> getSignature() {
return new Pair<>(Collections.singletonList(ValType.I64), Collections.emptyList());
public List<ValType> getParams() {
return Collections.singletonList(ValType.I64);
}

@Override
public List<ValType> 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()));
Expand All @@ -113,4 +108,5 @@ public class Example {
}
}
}

```

0 comments on commit 8d8351d

Please sign in to comment.