Skip to content

Commit c57e885

Browse files
Bundle WASi component adapter for preview1 modules
1 parent 1025883 commit c57e885

File tree

7 files changed

+41
-11
lines changed

7 files changed

+41
-11
lines changed

lib/ruby_wasm.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require_relative "ruby_wasm/util"
55
require_relative "ruby_wasm/build"
66
require_relative "ruby_wasm/packager"
7+
require_relative "ruby_wasm/packager/component_adapter"
78
require_relative "ruby_wasm/packager/file_system"
89
require_relative "ruby_wasm/packager/core"
910

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module RubyWasm::Packager::ComponentAdapter
2+
module_function
3+
4+
# The path to the component adapter for the given WASI execution model.
5+
#
6+
# @param exec_model [String] "command" or "reactor"
7+
def wasi_snapshot_preview1(exec_model)
8+
File.join(
9+
File.dirname(__FILE__),
10+
"component_adapter",
11+
"wasi_snapshot_preview1.#{exec_model}.wasm"
12+
)
13+
end
14+
end
Binary file not shown.
Binary file not shown.

lib/ruby_wasm/packager/core.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,12 @@ def link_exts(executor, build)
113113
]
114114

115115
wasi_libc_shared_libs.each do |lib|
116-
wasi_sdk_path = build.toolchain.wasi_sdk_path
116+
# @type var toolchain: RubyWasm::WASISDK
117+
toolchain = build.toolchain
118+
wasi_sdk_path = toolchain.wasi_sdk_path
117119
libraries << File.join(wasi_sdk_path, "share/wasi-sysroot/lib/wasm32-wasi", lib)
118120
end
119-
wasi_adapter = ENV["WASI_COMPONENT_ADAPTER"] or raise "WASI_COMPONENT_ADAPTER is not set"
121+
wasi_adapter = RubyWasm::Packager::ComponentAdapter.wasi_snapshot_preview1("command")
120122
adapters = [wasi_adapter]
121123
dl_openable_libs = Dir.glob(File.join(ruby_root, "usr", "local", "lib", "ruby", "**", "*.so"))
122124
linker = RubyWasmExt::ComponentLink.new
@@ -127,22 +129,16 @@ def link_exts(executor, build)
127129
libraries.each do |lib|
128130
# Non-DL openable libraries should be referenced as base name
129131
lib_name = File.basename(lib)
132+
# @type var module_bytes: Array[Integer]
130133
module_bytes = File.binread(lib).unpack("C*")
131134
RubyWasm.logger.info "Linking #{lib_name} (#{module_bytes.size} bytes)"
132135
linker.library(lib_name, module_bytes, false)
133136
end
134137

135-
filter_libs = [
136-
"enc/encdb.so",
137-
"stringio.so",
138-
"monitor.so",
139-
"pathname.so",
140-
"strscan.so",
141-
]
142138
dl_openable_libs.each do |lib|
143139
# DL openable lib_name should be a relative path from ruby_root
144140
lib_name = "/" + Pathname.new(lib).relative_path_from(Pathname.new(ruby_root)).to_s
145-
# next unless filter_libs.any? { |filter| lib_name.include?(filter) }
141+
# @type var module_bytes: Array[Integer]
146142
module_bytes = File.binread(lib).unpack("C*")
147143
RubyWasm.logger.info "Linking #{lib_name} (#{module_bytes.size} bytes)"
148144
linker.library(lib_name, module_bytes, true)
@@ -152,7 +148,9 @@ def link_exts(executor, build)
152148
adapter_name = File.basename(adapter)
153149
# e.g. wasi_snapshot_preview1.command.wasm -> wasi_snapshot_preview1
154150
adapter_name = adapter_name.split(".")[0]
155-
linker.adapter(adapter_name, File.binread(adapter).unpack("C*"))
151+
# @type var module_bytes: Array[Integer]
152+
module_bytes = File.binread(adapter).unpack("C*")
153+
linker.adapter(adapter_name, module_bytes)
156154
end
157155
return linker.encode()
158156
end

rakelib/packaging.rake

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,12 @@ namespace :standalone do
154154
end
155155
end
156156
end
157+
158+
namespace :gem do
159+
task :update_component_adapters do
160+
["command", "reactor"].each do |exec_model|
161+
sh "curl", "-L", "-o", "lib/ruby_wasm/packager/component_adapter/wasi_snapshot_preview1.#{exec_model}.wasm",
162+
"https://github.com/bytecodealliance/wasmtime/releases/download/v19.0.1/wasi_snapshot_preview1.#{exec_model}.wasm"
163+
end
164+
end
165+
end

sig/ruby_wasm/packager.rbs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class RubyWasm::Packager
4848
@packager: RubyWasm::Packager
4949
def initialize: (RubyWasm::Packager) -> void
5050
def build: (RubyWasm::BuildExecutor, untyped options) -> String
51+
def build_and_link_exts: (RubyWasm::BuildExecutor) -> Array[Integer]
5152

5253
extend Forwardable
5354

@@ -63,13 +64,16 @@ class RubyWasm::Packager
6364
def initialize: (RubyWasm::Packager) -> void
6465
def build: (RubyWasm::BuildExecutor, untyped options) -> String
6566
def specs_with_extensions: () -> Array[[untyped, Array[string]]]
67+
def build_and_link_exts: (RubyWasm::BuildExecutor) -> void
6668
end
6769

6870
class DynamicLinking < RubyWasm::Packager::Core::BuildStrategy
6971
@build: RubyWasm::Build
7072
def derive_build: () -> RubyWasm::Build
7173
def build_exts: (RubyWasm::BuildExecutor, RubyWasm::Build) -> void
7274
def name: () -> string
75+
76+
private def link_exts: (RubyWasm::BuildExecutor, RubyWasm::Build) -> Array[Integer]
7377
end
7478

7579
class StaticLinking < RubyWasm::Packager::Core::BuildStrategy
@@ -109,4 +113,8 @@ class RubyWasm::Packager
109113
def ruby_version: () -> String
110114
def rubyarchdir: () -> String
111115
end
116+
117+
module ComponentAdapter
118+
def self.wasi_snapshot_preview1: (String exec_model) -> String
119+
end
112120
end

0 commit comments

Comments
 (0)