From 2f730ffd68bb378e903193c9b98b71ba44194336 Mon Sep 17 00:00:00 2001 From: sacckey Date: Sat, 11 Jan 2025 14:56:43 +0900 Subject: [PATCH 1/3] Use DefaultRubyVM --- docs/worker.js | 41 +++++++---------------------------------- lib/executor.rb | 4 ++-- 2 files changed, 9 insertions(+), 36 deletions(-) diff --git a/docs/worker.js b/docs/worker.js index 2fb9886..6aea75d 100644 --- a/docs/worker.js +++ b/docs/worker.js @@ -1,5 +1,5 @@ -import { RubyVM } from 'https://cdn.jsdelivr.net/npm/@ruby/wasm-wasi@2.6.2/+esm'; -import { Directory, File, OpenDirectory, OpenFile, PreopenDirectory, WASI } from 'https://cdn.jsdelivr.net/npm/@bjorn3/browser_wasi_shim@0.3.0/+esm'; +import { DefaultRubyVM } from 'https://cdn.jsdelivr.net/npm/@ruby/wasm-wasi@2.6.2/dist/browser/+esm'; +import { File } from 'https://cdn.jsdelivr.net/npm/@bjorn3/browser_wasi_shim@0.3.0/+esm'; const DIRECTION_KEY_MASKS = { 'KeyD': 0b0001, // Right @@ -19,20 +19,6 @@ class Rubyboy { constructor() { this.wasmUrl = 'https://proxy.sacckey.dev/rubyboy.wasm'; - const rootContents = new Map(); - rootContents.set('RUBYBOY_TMP', new Directory(new Map())); - this.rootFs = rootContents; - - const args = ['ruby.wasm', '-e_=0']; - this.wasi = new WASI(args, [], [ - new OpenFile(new File([])), // stdin - new OpenFile(new File([])), // stdout - new OpenFile(new File([])), // stderr - new PreopenDirectory('/', rootContents) - ], { - debug: false - }); - this.directionKey = 0b1111; this.actionKey = 0b1111; } @@ -43,18 +29,8 @@ class Rubyboy { response = await fetch(this.wasmUrl); } - const buffer = await response.arrayBuffer(); - const imports = { - wasi_snapshot_preview1: this.wasi.wasiImport, - }; - const vm = new RubyVM(); - vm.addToImports(imports); - - const { instance } = await WebAssembly.instantiate(buffer, imports); - await vm.setInstance(instance); - this.wasi.initialize(instance); - vm.initialize(); - + const module = await WebAssembly.compileStreaming(response) + const { vm, wasi } = await DefaultRubyVM(module); vm.eval(` require 'js' require_relative 'lib/executor' @@ -63,15 +39,13 @@ class Rubyboy { `); this.vm = vm; + this.rootDir = wasi.fds[3].dir } sendPixelData() { this.vm.eval(`$executor.exec(${this.directionKey}, ${this.actionKey})`); - const tmpDir = this.rootFs.get('RUBYBOY_TMP'); - const op = new OpenDirectory(tmpDir); - const result = op.path_lookup('video.data', 0); - const file = result.inode_obj; + const file = this.rootDir.contents.get('video.data'); const bytes = file.data; postMessage({ type: 'pixelData', data: bytes.buffer }, [bytes.buffer]); @@ -127,8 +101,7 @@ self.addEventListener('message', async (event) => { if (event.data.type === 'loadROM') { const romFile = new File(new Uint8Array(event.data.data)); - const tmpDir = rubyboy.rootFs.get('RUBYBOY_TMP'); - tmpDir.contents.set('rom.data', romFile); + rubyboy.rootDir.contents.set('rom.data', romFile); rubyboy.vm.eval(` $executor.read_rom_from_virtual_fs `); diff --git a/lib/executor.rb b/lib/executor.rb index 440db2f..09247c7 100644 --- a/lib/executor.rb +++ b/lib/executor.rb @@ -15,11 +15,11 @@ def initialize def exec(direction_key = 0b1111, action_key = 0b1111) bin = @emulator.step(direction_key, action_key).pack('V*') - File.binwrite(File.join('/RUBYBOY_TMP', 'video.data'), bin) + File.binwrite('/video.data', bin) end def read_rom_from_virtual_fs - rom_path = '/RUBYBOY_TMP/rom.data' + rom_path = '/rom.data' raise "ROM file not found in virtual filesystem at #{rom_path}" unless File.exist?(rom_path) rom_data = File.open(rom_path, 'rb') { |file| file.read.bytes } From 7012d18ae0b9f811c698dd79ff2d3db0f60d2f96 Mon Sep 17 00:00:00 2001 From: sacckey Date: Sat, 11 Jan 2025 15:04:10 +0900 Subject: [PATCH 2/3] Bump ruby.wasm version to 2.7.0 --- Gemfile | 4 ++-- Gemfile.lock | 10 +++++----- docs/worker.js | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Gemfile b/Gemfile index d2425b2..583a46e 100644 --- a/Gemfile +++ b/Gemfile @@ -15,6 +15,6 @@ group :development, :test do end group :wasm do - gem 'js', '2.6.2' - gem 'ruby_wasm', '2.6.2' + gem 'js', '2.7.0' + gem 'ruby_wasm', '2.7.0' end diff --git a/Gemfile.lock b/Gemfile.lock index 2ded902..431b047 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -12,7 +12,7 @@ GEM diff-lcs (1.5.0) ffi (1.16.3) heap-profiler (0.7.0) - js (2.6.2) + js (2.7.0) json (2.6.3) language_server-protocol (3.17.0.3) parallel (1.23.0) @@ -52,8 +52,8 @@ GEM rubocop-ast (1.29.0) parser (>= 3.2.1.0) ruby-progressbar (1.13.0) - ruby_wasm (2.6.2-x86_64-darwin) - ruby_wasm (2.6.2-x86_64-linux) + ruby_wasm (2.7.0-x86_64-darwin) + ruby_wasm (2.7.0-x86_64-linux) stackprof (0.2.25) unicode-display_width (2.5.0) @@ -63,11 +63,11 @@ PLATFORMS DEPENDENCIES heap-profiler (~> 0.7.0) - js (= 2.6.2) + js (= 2.7.0) rake (~> 13.0) rspec (~> 3.12) rubocop (~> 1.57) - ruby_wasm (= 2.6.2) + ruby_wasm (= 2.7.0) rubyboy! stackprof (~> 0.2.25) diff --git a/docs/worker.js b/docs/worker.js index 6aea75d..7e15f48 100644 --- a/docs/worker.js +++ b/docs/worker.js @@ -1,4 +1,4 @@ -import { DefaultRubyVM } from 'https://cdn.jsdelivr.net/npm/@ruby/wasm-wasi@2.6.2/dist/browser/+esm'; +import { DefaultRubyVM } from 'https://cdn.jsdelivr.net/npm/@ruby/wasm-wasi@2.7.0/dist/browser/+esm'; import { File } from 'https://cdn.jsdelivr.net/npm/@bjorn3/browser_wasi_shim@0.3.0/+esm'; const DIRECTION_KEY_MASKS = { From f7f9cfedfc98da04877a147e736c260f6293a418 Mon Sep 17 00:00:00 2001 From: sacckey Date: Sat, 11 Jan 2025 15:25:32 +0900 Subject: [PATCH 3/3] Bump version to 1.4.2 --- CHANGELOG.md | 5 +++++ Gemfile.lock | 2 +- lib/rubyboy/version.rb | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f3ff21..6079d8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ ## [Unreleased] +## [1.4.2] - 2025-01-11 + +- Use DefaultRubyVM +- Bump ruby.wasm version to 2.7.0 + ## [1.4.1] - 2024-12-25 - PPU performance optimization through data caching and pixel format changes diff --git a/Gemfile.lock b/Gemfile.lock index 431b047..7b700e9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - rubyboy (1.4.1) + rubyboy (1.4.2) ffi (~> 1.16, >= 1.16.3) GEM diff --git a/lib/rubyboy/version.rb b/lib/rubyboy/version.rb index 59170af..5a28a86 100644 --- a/lib/rubyboy/version.rb +++ b/lib/rubyboy/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Rubyboy - VERSION = '1.4.1' + VERSION = '1.4.2' end