kobako 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Cargo.toml ADDED
@@ -0,0 +1,11 @@
1
+ # This Cargo.toml is here to let externals tools (IDEs, etc.) know that this is
2
+ # a Rust project. Your extensions dependencies should be added to the Cargo.toml
3
+ # in the ext/ directory.
4
+
5
+ [workspace]
6
+ members = ["./ext/kobako"]
7
+ # `wasm/` is a sibling workspace (kobako-wasm crate) compiled for
8
+ # wasm32-wasip1; excluding it keeps the host (wasmtime) and guest
9
+ # dependency graphs separate.
10
+ exclude = ["wasm", "vendor"]
11
+ resolver = "2"
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 ZhengXian Qiu
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,228 @@
1
+ # Kobako
2
+
3
+ Kobako is a Ruby gem that embeds a Wasm-isolated mruby interpreter inside your application, so you can execute untrusted Ruby scripts (LLM-generated code, user formulas, student submissions, third-party plugins) in-process without giving them access to host memory, files, network, or credentials.
4
+
5
+ The host (`wasmtime`) runs a precompiled `kobako.wasm` guest containing mruby and an RPC client. The only way a guest script can reach the outside world is through Host App-declared **Services** — named Ruby objects you explicitly inject into the sandbox.
6
+
7
+ ## Features
8
+
9
+ - **In-process Wasm sandbox** — no subprocess, no container. Each `Sandbox#run` is a synchronous Ruby call.
10
+ - **Capability injection via Services** — guest scripts can only call Ruby objects you explicitly `bind` under a two-level `Group::Member` namespace.
11
+ - **Structured outcome** — `#run` returns the deserialized last expression of the guest script as a normal Ruby value.
12
+ - **Three-class error taxonomy** — every failure is exactly one of `TrapError` (Wasm engine), `SandboxError` (script / wire fault), or `ServiceError` (Service capability fault), so you can route errors without inspecting messages.
13
+ - **Per-run state reset** — Handles issued during one `#run` are invalidated before the next; Service bindings remain.
14
+ - **Separated stdout / stderr capture** — guest `puts`/`warn` output is buffered (1 MiB default cap, configurable, with a `[truncated]` marker on overflow) and is independent of the RPC channel.
15
+ - **Capability Handles** — Services may return stateful host objects; the guest receives an opaque token it can use as the target of follow-up RPC calls, with no way to dereference it.
16
+
17
+ ## Requirements
18
+
19
+ - **Ruby ≥ 3.3.0**
20
+ - **Rust / Cargo** at install time — the native extension compiles from source via `rb_sys`
21
+ - **Linux** or **macOS** — Windows is not supported
22
+
23
+ The precompiled `kobako.wasm` Guest Binary ships inside the gem, so end users do **not** need a WASI toolchain. (The toolchain is only required if you build the gem from a source checkout — see [Development](#development).)
24
+
25
+ ## Installation
26
+
27
+ Add Kobako to your Gemfile:
28
+
29
+ ```bash
30
+ bundle add kobako
31
+ ```
32
+
33
+ Or install it directly:
34
+
35
+ ```bash
36
+ gem install kobako
37
+ ```
38
+
39
+ ## Quick Start
40
+
41
+ ```ruby
42
+ require "kobako"
43
+
44
+ sandbox = Kobako::Sandbox.new
45
+
46
+ result = sandbox.run(<<~RUBY)
47
+ 1 + 2
48
+ RUBY
49
+
50
+ result # => 3
51
+ sandbox.stdout # => ""
52
+ ```
53
+
54
+ The script executes inside the Wasm guest. It cannot read your filesystem, open sockets, or touch your `ENV`.
55
+
56
+ ## Injecting Services
57
+
58
+ Guest scripts reach host resources only through Services. Declare a **Group**, then `bind` named **Members** on it — each member can be any Ruby object that responds to the methods the guest will call.
59
+
60
+ ```ruby
61
+ sandbox = Kobako::Sandbox.new
62
+
63
+ sandbox.define(:KV).bind(:Lookup, ->(key) { redis.get(key) })
64
+ sandbox.define(:Log).bind(:Sink, ->(msg) { logger.info(msg) })
65
+
66
+ sandbox.run(<<~RUBY)
67
+ Log::Sink.call("starting")
68
+ KV::Lookup.call("user_42")
69
+ RUBY
70
+ # => "..." (the redis value)
71
+ ```
72
+
73
+ Names must match the Ruby constant pattern `/\A[A-Z]\w*\z/`. Services declared before the first `#run` remain active across subsequent runs.
74
+
75
+ ### Keyword arguments
76
+
77
+ Keyword keys travel as Symbols and reach the host method as keyword arguments:
78
+
79
+ ```ruby
80
+ sandbox.define(:Geo).bind(:Lookup, ->(name:, region:) { "#{region}/#{name}" })
81
+
82
+ sandbox.run('Geo::Lookup.call(name: "alice", region: "us")')
83
+ # => "us/alice"
84
+ ```
85
+
86
+ ## Capturing stdout and stderr
87
+
88
+ Guest output is captured into per-run buffers and exposed independently from the return value:
89
+
90
+ ```ruby
91
+ sandbox = Kobako::Sandbox.new
92
+
93
+ result = sandbox.run(<<~RUBY)
94
+ puts "hello"
95
+ warn "be careful"
96
+ 42
97
+ RUBY
98
+
99
+ result # => 42
100
+ sandbox.stdout # => "hello\n"
101
+ sandbox.stderr # => "be careful\n"
102
+ ```
103
+
104
+ Each `#run` clears the buffers at start. Output past the per-channel cap is truncated; the buffer ends with `[truncated]` and `#run` still returns normally.
105
+
106
+ ```ruby
107
+ Kobako::Sandbox.new(stdout_limit: 64 * 1024, stderr_limit: 64 * 1024)
108
+ ```
109
+
110
+ ## Error handling
111
+
112
+ Every `#run` either returns a value or raises exactly one of three classes:
113
+
114
+ ```ruby
115
+ begin
116
+ sandbox.run(script)
117
+ rescue Kobako::TrapError => e
118
+ # Wasm engine crashed: OOM, stack overflow, corrupted guest runtime.
119
+ # The Sandbox is unrecoverable — discard and recreate it.
120
+ rescue Kobako::ServiceError => e
121
+ # A Service call failed and the script did not rescue it.
122
+ # Treat like any other downstream-service failure in your app.
123
+ rescue Kobako::SandboxError => e
124
+ # The script itself raised, failed to compile, or produced an
125
+ # unrepresentable value. A script-level fault, not infrastructure.
126
+ end
127
+ ```
128
+
129
+ `SandboxError` and `ServiceError` carry structured fields (`origin`, `klass`, `backtrace_lines`, `details`) when the guest produced a panic envelope.
130
+
131
+ `Kobako::ServiceError::Disconnected` is a named subclass raised when an RPC target Handle has been invalidated. `Kobako::HandleTableExhausted` is a named `SandboxError` subclass raised when the per-run Handle counter reaches its cap (2³¹ − 1).
132
+
133
+ ## Capability Handles
134
+
135
+ When a Service returns a stateful host object (anything beyond `nil` / Boolean / Integer / Float / String / Symbol / Array / Hash), the wire layer transparently allocates an opaque Handle. The guest receives a `Kobako::Handle` proxy it can use as the target of further RPC calls — but cannot dereference, forge from an integer, or smuggle across runs.
136
+
137
+ ```ruby
138
+ class Greeter
139
+ def initialize(name) = @name = name
140
+ def greet = "hi, #{@name}"
141
+ end
142
+
143
+ sandbox.define(:Factory).bind(:Make, ->(name) { Greeter.new(name) })
144
+
145
+ sandbox.run(<<~RUBY)
146
+ g = Factory::Make.call("Bob") # g is a Kobako::Handle proxy
147
+ g.greet # second RPC, routed to the Greeter
148
+ RUBY
149
+ # => "hi, Bob"
150
+ ```
151
+
152
+ Handles are scoped to a single `#run` — a Handle obtained in run N is invalid in run N+1, even on the same Sandbox.
153
+
154
+ ## Setup-once, run-many
155
+
156
+ A single Sandbox can serve many script executions. Service bindings persist; capability state (Handles, stdout, stderr) resets between runs.
157
+
158
+ ```ruby
159
+ sandbox = Kobako::Sandbox.new
160
+ sandbox.define(:Data).bind(:Fetch, ->(id) { records[id] })
161
+
162
+ sandbox.run('Data::Fetch.call("a")') # => "..."
163
+ sandbox.run('Data::Fetch.call("b")') # => "..." (same bindings, fresh state)
164
+ ```
165
+
166
+ For workloads that must be isolated from each other (e.g., one Sandbox per tenant, per student submission), construct a fresh `Kobako::Sandbox` per scope. wasmtime's Engine and the compiled Module are cached at process scope, so additional Sandboxes amortize cold-start cost automatically.
167
+
168
+ ## Development
169
+
170
+ After checking out the repo:
171
+
172
+ ```bash
173
+ bin/setup # install dependencies
174
+ bundle exec rake # default: compile + test + rubocop
175
+ ```
176
+
177
+ Building from source requires a WASI-capable Rust toolchain in addition to the standard host toolchain. The first compile walks the full vendor / mruby / wasm chain:
178
+
179
+ ```bash
180
+ bundle exec rake compile # build the native extension
181
+ bundle exec rake wasm:build # rebuild data/kobako.wasm (requires vendor:setup + mruby:build)
182
+ bundle exec rake test # run the Ruby test suite
183
+ ```
184
+
185
+ `bin/console` opens an IRB session with the gem preloaded for experimentation.
186
+
187
+ To install the local checkout as a gem:
188
+
189
+ ```bash
190
+ bundle exec rake install
191
+ ```
192
+
193
+ ## Performance
194
+
195
+ Headline numbers from the current baseline (macOS arm64, Ruby 3.4.7 — full results in [`benchmark/results/`](benchmark/results)):
196
+
197
+ | What | Cost |
198
+ |---|---|
199
+ | First `Sandbox.new` in a fresh process (Engine init + Module compile) | ~410 ms one-time |
200
+ | Subsequent `Sandbox.new` (cache warm) | ~90 µs |
201
+ | Reusing a Sandbox for one `#run("nil")` | ~67 µs |
202
+ | Fresh Sandbox per request — the tenant-isolation pattern | ~175 µs (+110 µs versus reuse) |
203
+ | Per-RPC cost amortized across many calls in one `#run` | ~5.4 µs |
204
+ | 100 000-iteration integer XOR loop in mruby | ~44 ms |
205
+ | One-time process memory for wasmtime Engine + Module | ~110 MB |
206
+ | Memory per additional Sandbox after the first | ~200 KB |
207
+ | 1 000 isolated tenants in one process (1 Sandbox each) | ~340 MB total |
208
+ | Aggregate throughput across N Threads | GVL-bound — wasm execution is serialized, modest scaling from Ruby-side overlap |
209
+
210
+ Practical implications:
211
+
212
+ - **Pre-warm at boot.** The 410 ms first-Sandbox cost is paid once per process; every subsequent Sandbox amortizes to micro-, not milliseconds. Construct one Sandbox at boot before serving requests.
213
+ - **Tenant isolation is affordable.** Per-request Sandbox construction adds ~110 µs of overhead; per-tenant RSS budget is ~200 KB plus one-time ~110 MB for the engine. 1 000 isolated tenants in a single Sidekiq / Puma worker is well within typical RSS limits.
214
+ - **Batch RPCs inside one `#run`.** A single Service call costs ~76 µs because each `#run` carries ~67 µs of setup; 1 000 calls inside one `#run` reduce the per-call cost to ~5.4 µs.
215
+
216
+ A +10% regression on any of the five SPEC-mandated benchmarks blocks release. See [`benchmark/README.md`](benchmark/README.md) for the full per-suite breakdown, rake task reference, and known measurement caveats (guest String size cap, GVL bounds, allocator retention).
217
+
218
+ ```bash
219
+ bundle exec rake bench # five gated regression benchmarks (≤ 1 MiB payloads, ~5-7 min)
220
+ ```
221
+
222
+ ## Contributing
223
+
224
+ Bug reports and pull requests are welcome at <https://github.com/elct9620/kobako>. Please open an issue before starting on non-trivial changes so we can align on scope.
225
+
226
+ ## License
227
+
228
+ Kobako is released under the [Apache License 2.0](https://opensource.org/licenses/Apache-2.0).
data/data/kobako.wasm ADDED
Binary file
@@ -0,0 +1,36 @@
1
+ [package]
2
+ name = "kobako"
3
+ version = "0.1.0"
4
+ edition = "2021"
5
+ authors = ["Aotokitsuruya <contact@aotoki.me>"]
6
+ license = "Apache-2.0"
7
+ publish = false
8
+
9
+ [lib]
10
+ crate-type = ["cdylib"]
11
+
12
+ [dependencies]
13
+ magnus = { version = "0.8.2" }
14
+ # wasmtime — host-side embedder for kobako.wasm. We disable default-features
15
+ # and opt back in only what kobako needs: a Cranelift-backed runtime that can
16
+ # compile a pre-built wasm32-wasip1 module on the host triple, plus the `wat`
17
+ # feature
18
+ # so test fixtures can be expressed as text. WASI integration is layered on
19
+ # later via `wasmtime-wasi` once stdin/stdout wiring is needed (item #16).
20
+ # `cache` / `parallel-compilation` / `pooling` / `component-model` / `async`
21
+ # are intentionally off — kobako runs short-lived synchronous sandboxes.
22
+ wasmtime = { version = "44.0.1", default-features = false, features = [
23
+ "cranelift",
24
+ "runtime",
25
+ "gc",
26
+ "gc-drc",
27
+ "addr2line",
28
+ "demangle",
29
+ "wat",
30
+ ] }
31
+ # wasmtime-wasi provides WASI preview1 support for routing guest stdout/stderr
32
+ # into in-memory buffers (SPEC.md §B-04). The `p1` feature enables the
33
+ # WasiCtxBuilder + preview1 adapter which wires fd 1/2 to pipes. We omit
34
+ # `p2` (component-model) and `p0`/`p3` (async) because kobako runs
35
+ # synchronous sandboxes only.
36
+ wasmtime-wasi = { version = "44.0.1", default-features = false, features = ["p1"] }
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mkmf"
4
+ require "rb_sys/mkmf"
5
+
6
+ create_rust_makefile("kobako/kobako")
@@ -0,0 +1,10 @@
1
+ use magnus::{Error, Ruby};
2
+
3
+ mod wasm;
4
+
5
+ #[magnus::init]
6
+ fn init(ruby: &Ruby) -> Result<(), Error> {
7
+ let module = ruby.define_module("Kobako")?;
8
+ wasm::init(ruby, module)?;
9
+ Ok(())
10
+ }
@@ -0,0 +1,92 @@
1
+ //! Process-wide caches for the wasmtime [`Engine`] and compiled
2
+ //! [`Module`].
3
+ //!
4
+ //! SPEC.md "Code Organization" pins `ext/` as private and forbids
5
+ //! exposing wasm engine types to the Host App or downstream gems. To
6
+ //! amortise Engine creation and Module JIT compilation across multiple
7
+ //! `Kobako::Sandbox` constructions, the ext keeps a process-scope
8
+ //! shared Engine and a per-path Module cache. Both are transparent to
9
+ //! Ruby callers, who construct an `Instance` via
10
+ //! `Kobako::Wasm::Instance.from_path(...)` and never see Engine or
11
+ //! Module.
12
+ //!
13
+ //! Concurrency: under Ruby's GVL only one thread can execute Rust code
14
+ //! at a time, so the Mutex is held briefly during HashMap insert/lookup
15
+ //! and serves to satisfy `Sync` bounds rather than to arbitrate real
16
+ //! contention.
17
+ //!
18
+ //! [`Engine`]: wasmtime::Engine
19
+ //! [`Module`]: wasmtime::Module
20
+
21
+ use std::collections::HashMap;
22
+ use std::fs;
23
+ use std::path::{Path, PathBuf};
24
+ use std::sync::{Mutex, OnceLock};
25
+
26
+ use magnus::{Error as MagnusError, Ruby};
27
+ use wasmtime::{Config as WtConfig, Engine as WtEngine, Module as WtModule};
28
+
29
+ use super::{wasm_err, MODULE_NOT_BUILT_ERROR};
30
+
31
+ static SHARED_ENGINE: OnceLock<WtEngine> = OnceLock::new();
32
+ static MODULE_CACHE: OnceLock<Mutex<HashMap<PathBuf, WtModule>>> = OnceLock::new();
33
+
34
+ /// Return the process-wide wasmtime Engine, building it on first call.
35
+ ///
36
+ /// Enables the wasm exceptions proposal so `kobako.wasm` (which uses
37
+ /// `try_table` / `exnref` / `tag` for mruby's setjmp-via-new-EH path)
38
+ /// can be loaded. The mruby wasi build config uses
39
+ /// `-mllvm -wasm-use-legacy-eh=false`, which generates new-style
40
+ /// exception handling instructions in the wasm32 object files;
41
+ /// wasmtime must have the proposal enabled to parse and JIT those
42
+ /// instructions.
43
+ pub(crate) fn shared_engine() -> Result<&'static WtEngine, MagnusError> {
44
+ if let Some(engine) = SHARED_ENGINE.get() {
45
+ return Ok(engine);
46
+ }
47
+ let mut config = WtConfig::new();
48
+ config.wasm_exceptions(true);
49
+ let engine = WtEngine::new(&config).map_err(|e| {
50
+ let ruby = Ruby::get().expect("Ruby thread");
51
+ wasm_err(&ruby, format!("engine init: {}", e))
52
+ })?;
53
+ Ok(SHARED_ENGINE.get_or_init(|| engine))
54
+ }
55
+
56
+ /// Look up `path` in the per-path Module cache, compiling and inserting
57
+ /// the artifact on a miss. Raises `Kobako::Wasm::ModuleNotBuiltError`
58
+ /// when the file is missing — the headline error for the common
59
+ /// pre-build state on a fresh clone before `rake compile`.
60
+ pub(crate) fn cached_module(path: &Path) -> Result<WtModule, MagnusError> {
61
+ let ruby = Ruby::get().expect("Ruby thread");
62
+ let cache = MODULE_CACHE.get_or_init(|| Mutex::new(HashMap::new()));
63
+
64
+ if let Some(module) = cache
65
+ .lock()
66
+ .expect("module cache mutex poisoned")
67
+ .get(path)
68
+ .cloned()
69
+ {
70
+ return Ok(module);
71
+ }
72
+
73
+ if !path.exists() {
74
+ return Err(MagnusError::new(
75
+ ruby.get_inner(&MODULE_NOT_BUILT_ERROR),
76
+ format!(
77
+ "wasm module not found at {}; run `bundle exec rake wasm:build` to build it",
78
+ path.display()
79
+ ),
80
+ ));
81
+ }
82
+
83
+ let bytes =
84
+ fs::read(path).map_err(|e| wasm_err(&ruby, format!("read {}: {}", path.display(), e)))?;
85
+ let module = WtModule::new(shared_engine()?, &bytes)
86
+ .map_err(|e| wasm_err(&ruby, format!("compile module: {}", e)))?;
87
+ cache
88
+ .lock()
89
+ .expect("module cache mutex poisoned")
90
+ .insert(path.to_path_buf(), module.clone());
91
+ Ok(module)
92
+ }