Skip to content

Commit

Permalink
Switch to Chicory Wasm runtime with (aot experiment)
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Dec 23, 2024
1 parent eaef539 commit 38d6e0f
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 119 deletions.
7 changes: 5 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ dependencies {
// Plasmid
modImplementation("xyz.nucleoid:plasmid:${project.plasmid_version}")

// Wasmtime
include implementation("io.github.kawamuray.wasmtime:wasmtime-java:${project.wasmtime_version}")
// Chicory
include implementation("com.dylibso.chicory:runtime:${project.chicory_version}")
include implementation("com.dylibso.chicory:aot-experimental:${project.chicory_version}")
compileOnly("com.dylibso.chicory:host-module-annotations-experimental:${project.chicory_version}")
annotationProcessor("com.dylibso.chicory:host-module-processor-experimental:${project.chicory_version}")
}

processResources {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ loader_version = 0.16.9
fabric_version = 0.112.0+1.21.4

plasmid_version = 0.6.2+1.21.4
wasmtime_version = 0.18.0
chicory_version = 1.0.0-M2
174 changes: 93 additions & 81 deletions src/main/java/io/github/haykam821/consolebox/game/GameCanvas.java

Large diffs are not rendered by default.

33 changes: 14 additions & 19 deletions src/main/java/io/github/haykam821/consolebox/game/GameMemory.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import io.github.haykam821.consolebox.mixin.MemoryTypeAccessor;
import io.github.kawamuray.wasmtime.Extern;
import io.github.kawamuray.wasmtime.Memory;
import io.github.kawamuray.wasmtime.MemoryType;
import io.github.kawamuray.wasmtime.Store;
import com.dylibso.chicory.runtime.Memory;
import com.dylibso.chicory.wasm.types.MemoryLimits;
import io.github.haykam821.consolebox.mixin.MemoryAccessor;

public final class GameMemory {
private static final int PALETTE_ADDRESS = 0x0004;
Expand All @@ -25,24 +23,24 @@ public final class GameMemory {
private static final int FRAMEBUFFER_SIZE = 6400;

private final Memory memory;

private final ByteBuffer buffer;
private final ByteBuffer framebuffer;
protected GameMemory(Store<Void> store) {
this.memory = GameMemory.createMemory(store, HardwareConstants.MEMORY_PAGES);

this.buffer = memory.buffer(store);
protected GameMemory() {
this.memory = GameMemory.createMemory(HardwareConstants.MEMORY_PAGES);
// Todo: fix it later
this.buffer = ((MemoryAccessor) (Object) this.memory).getBuffer();
this.framebuffer = this.buffer.slice(FRAMEBUFFER_ADDRESS, FRAMEBUFFER_SIZE);

this.initializeMemory();

}

public ByteBuffer getBuffer() {
return this.buffer;
public Memory memory() {
return this.memory;
}

public Extern createExtern() {
return Extern.fromMemory(this.memory);
public ByteBuffer getBuffer() {
return this.buffer;
}

public ByteBuffer getFramebuffer() {
Expand Down Expand Up @@ -176,10 +174,7 @@ public String toString() {
return "GameMemory{" + this.memory + "}";
}

private static Memory createMemory(Store<Void> store, int pages) {
MemoryType memoryType = new MemoryType(pages, false);
((MemoryTypeAccessor) (Object) memoryType).setMaximum(pages);

return new Memory(store, memoryType);
private static Memory createMemory(int pages) {
return new Memory(new MemoryLimits(pages, pages));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.haykam821.consolebox.mixin;

import com.dylibso.chicory.runtime.Memory;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

import java.nio.ByteBuffer;

@Mixin(Memory.class)
public interface MemoryAccessor {
@Accessor
ByteBuffer getBuffer();
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.haykam821.consolebox.mixin;

import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.wasm.WasmModule;
import com.dylibso.chicory.wasm.types.Section;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(Parser.class)
public interface ParserAccessor {
@Invoker
static void callOnSection(WasmModule.Builder module, Section s) {
throw new UnsupportedOperationException();
}
}
5 changes: 3 additions & 2 deletions src/main/resources/consolebox.mixin.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"required": true,
"package": "io.github.haykam821.consolebox.mixin",
"compatibilityLevel": "JAVA_8",
"compatibilityLevel": "JAVA_21",
"mixins": [
"MemoryTypeAccessor"
"MemoryAccessor",
"ParserAccessor"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit 38d6e0f

Please sign in to comment.