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
8 changes: 8 additions & 0 deletions src/main/java/io/github/kawamuray/wasmtime/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
import lombok.Getter;
import lombok.experimental.Accessors;

/**
* An Engine which is a global context for compilation and management of wasm modules.
* <p>
* Engines store global configuration preferences such as compilation settings, enabled features, etc.
* You'll likely only need at most one of these for a program.
*
* @see <a href="https://docs.wasmtime.dev/api/wasmtime/struct.Engine.html">Rust Documentation</a>
*/
@Accessors(fluent = true)
@EqualsAndHashCode
@AllArgsConstructor(access = AccessLevel.PACKAGE)
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/io/github/kawamuray/wasmtime/ExportType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.github.kawamuray.wasmtime;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.experimental.Accessors;

@Accessors(fluent = true)
@AllArgsConstructor(access = AccessLevel.PACKAGE)
public class ExportType {
Comment thread
BjoernAkAManf marked this conversation as resolved.
@Getter
private final ExternType type;

@Getter(AccessLevel.PACKAGE)
private final Object typeObj;

@Getter
private final String name;

private void ensureType(ExternType expected) {
if (type != expected) {
throw new RuntimeException(
String.format("ImportType expected to have type %s but is actually %s", expected, type));
}
}

public FuncType func() {
ensureType(ExternType.FUNC);
return (FuncType) typeObj;
}

public GlobalType global() {
ensureType(ExternType.GLOBAL);
return (GlobalType) typeObj;
}

public MemoryType memory() {
ensureType(ExternType.MEMORY);
return (MemoryType) typeObj;
}

public TableType table() {
ensureType(ExternType.TABLE);
return (TableType) typeObj;
}
}
11 changes: 11 additions & 0 deletions src/main/java/io/github/kawamuray/wasmtime/ExternType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.kawamuray.wasmtime;

public enum ExternType {
FUNC,
GLOBAL,
TABLE,
MEMORY,
// TODO: Currently Unsupported
INSTANCE,
MODULE
}
20 changes: 15 additions & 5 deletions src/main/java/io/github/kawamuray/wasmtime/Func.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
package io.github.kawamuray.wasmtime;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
* A WebAssembly function which can be called.
*
* This type is either provided by a WebAssembly Module or implemented in Java as a Host-function.
*
* A Func "belongs" to the store that it was originally created within. Operations on a Func only work with the store
* it belongs to. Otherwise an exception will be thrown.
*
* @see <a href="https://docs.wasmtime.dev/api/wasmtime/struct.Func.html">Rust Documentation</a>
*/
@Slf4j
@Accessors(fluent = true)
@AllArgsConstructor(access = AccessLevel.PACKAGE)
Expand Down
22 changes: 6 additions & 16 deletions src/main/java/io/github/kawamuray/wasmtime/ImportType.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,8 @@
@Accessors(fluent = true)
@AllArgsConstructor(access = AccessLevel.PACKAGE)
public class ImportType {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I think we don't even need ImportType and ExportType separately. They share exactly the same definition, so we just need to rename this class to ExternType and use it for exports too?

public enum Type {
FUNC,
GLOBAL,
TABLE,
MEMORY,
// TODO: Currently Unsupported
INSTANCE,
MODULE
}

@Getter
private final Type type;
private final ExternType type;

@Getter(AccessLevel.PACKAGE)
private final Object typeObj;
Expand All @@ -30,30 +20,30 @@ public enum Type {
@Getter
private final String name;

private void ensureType(ImportType.Type expected) {
private void ensureType(ExternType expected) {
if (type != expected) {
throw new RuntimeException(
String.format("ImportType expected to have type %s but is actually %s", expected, type));
}
}

public FuncType func() {
ensureType(ImportType.Type.FUNC);
ensureType(ExternType.FUNC);
return (FuncType) typeObj;
}

public GlobalType global() {
ensureType(ImportType.Type.GLOBAL);
ensureType(ExternType.GLOBAL);
return (GlobalType) typeObj;
}

public MemoryType memory() {
ensureType(ImportType.Type.MEMORY);
ensureType(ExternType.MEMORY);
return (MemoryType) typeObj;
}

public TableType table() {
ensureType(ImportType.Type.TABLE);
ensureType(ExternType.TABLE);
return (TableType) typeObj;
}
}
2 changes: 2 additions & 0 deletions src/main/java/io/github/kawamuray/wasmtime/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public static Module fromBinary(Engine engine, byte[] bytes) {

public native ImportType[] imports();

public native ExportType[] exports();

@Override
public native void dispose();

Expand Down
41 changes: 41 additions & 0 deletions src/main/java/io/github/kawamuray/wasmtime/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,47 @@
import lombok.Getter;
import lombok.experimental.Accessors;

/**
* A Store is a collection of WebAssembly instances and host-defined state.
* <p>
* All WebAssembly instances and items will be attached to and refer to a Store. For example instances, functions,
* globals, and tables are all attached to a Store. Instances are created by instantiating a Module within a Store.
* <p>
* A Store is intended to be a short-lived object in a program. No form of GC is implemented at this time so once an
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This document has a rust-java gap problems too. The notion of "GC", "drop", and so on.

* instance is created within a Store it will not be deallocated until the Store itself is dropped. This makes Store
* unsuitable for creating an unbounded number of instances in it because Store will never release this memory. It’s
* recommended to have a Store correspond roughly to the lifetime of a "main instance" that an embedding is interested
* in executing.
*
* <h1>Type parameter T</h1>
* <p>
* Each Store has a type parameter T associated with it. This T represents state defined by the host.
* This state will be accessible through the Caller type that host-defined functions get access to.
* This T is suitable for storing Store-specific information which imported functions may want access to.
* <p>
* The data T can be accessed through methods like Store::data and Store::data_mut.
* Stores, contexts, oh my
* <p>
* Most methods in Wasmtime take something of the form AsContext or AsContextMut as the first argument.
* These two traits allow ergonomically passing in the context you currently have to any method.
* The primary two sources of contexts are:
* <p>
* Store&lt;T&gt;
* Caller&lt;'_, T&gt;
* <p>
* corresponding to what you create and what you have access to in a host function. You can also explicitly acquire
* a StoreContext or StoreContextMut and pass that around as well.
* <p>
* Note that all methods on Store are mirrored onto StoreContext, StoreContextMut, and Caller. This way no matter what
* form of context you have you can call various methods, create objects, etc.
*
* <h1>Stores and Default</h1>
* <p>
* You can create a store with default configuration settings using Store::default(). This will create a
* brand new Engine with default configuration (see Config for more information).
*
* @param <T> State defined by the host
*/
@Accessors(fluent = true)
@AllArgsConstructor(access = AccessLevel.PACKAGE)
public class Store<T> implements Disposable {
Expand Down
Loading