kobako 0.29.0 → 0.31.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.
Files changed (291) hide show
  1. checksums.yaml +4 -4
  2. data/.release-please-manifest.json +1 -1
  3. data/CHANGELOG.md +552 -10
  4. data/Cargo.lock +595 -139
  5. data/README.md +126 -564
  6. data/ROADMAP.md +3 -2
  7. data/crates/kobako-runtime/CHANGELOG.md +488 -1
  8. data/crates/kobako-runtime/Cargo.toml +73 -15
  9. data/crates/kobako-runtime/README.md +11 -4
  10. data/crates/kobako-runtime/src/cred.rs +124 -0
  11. data/crates/kobako-runtime/src/dispatch.rs +5 -6
  12. data/crates/kobako-runtime/src/endpoint/buffer.rs +301 -0
  13. data/crates/kobako-runtime/src/endpoint/dispatch.rs +292 -0
  14. data/crates/kobako-runtime/src/endpoint/invoke.rs +181 -0
  15. data/crates/kobako-runtime/src/endpoint.rs +267 -0
  16. data/crates/kobako-runtime/src/errno.rs +391 -0
  17. data/crates/kobako-runtime/src/error.rs +31 -10
  18. data/crates/kobako-runtime/src/filesystem/anon_inodes.rs +52 -0
  19. data/crates/kobako-runtime/src/filesystem/bind/fid.rs +82 -0
  20. data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +274 -0
  21. data/crates/kobako-runtime/src/filesystem/bind/walk.rs +248 -0
  22. data/crates/kobako-runtime/src/filesystem/bind.rs +2857 -0
  23. data/crates/kobako-runtime/src/filesystem/overlay/file.rs +89 -0
  24. data/crates/kobako-runtime/src/filesystem/overlay/inode.rs +106 -0
  25. data/crates/kobako-runtime/src/filesystem/overlay/path.rs +263 -0
  26. data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +128 -0
  27. data/crates/kobako-runtime/src/filesystem/overlay/stack/dir.rs +238 -0
  28. data/crates/kobako-runtime/src/filesystem/overlay/stack/namei.rs +135 -0
  29. data/crates/kobako-runtime/src/filesystem/overlay/stack/readdir.rs +54 -0
  30. data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +316 -739
  31. data/crates/kobako-runtime/src/filesystem/overlay.rs +2627 -1154
  32. data/crates/kobako-runtime/src/filesystem/pipe.rs +224 -0
  33. data/crates/kobako-runtime/src/filesystem/rdir.rs +84 -0
  34. data/crates/kobako-runtime/src/filesystem/tmpfs/seed.rs +32 -0
  35. data/crates/kobako-runtime/src/filesystem/tmpfs/tmpfile.rs +32 -0
  36. data/crates/kobako-runtime/src/filesystem/tmpfs/whiteout.rs +107 -0
  37. data/crates/kobako-runtime/src/filesystem/tmpfs/xattr.rs +22 -0
  38. data/crates/kobako-runtime/src/filesystem/tmpfs.rs +1997 -0
  39. data/crates/kobako-runtime/src/filesystem/wrap.rs +208 -0
  40. data/crates/kobako-runtime/src/filesystem.rs +25 -388
  41. data/crates/kobako-runtime/src/fork.rs +96 -0
  42. data/crates/kobako-runtime/src/grant.rs +3 -0
  43. data/crates/kobako-runtime/src/hashtable.rs +46 -0
  44. data/crates/kobako-runtime/src/lib.rs +27 -11
  45. data/crates/kobako-runtime/src/limits.rs +195 -0
  46. data/crates/kobako-runtime/src/locking/seqlock.rs +66 -0
  47. data/crates/kobako-runtime/src/locking.rs +228 -0
  48. data/crates/kobako-runtime/src/memcg.rs +383 -0
  49. data/crates/kobako-runtime/src/mount.rs +86 -101
  50. data/crates/kobako-runtime/src/net/backends.rs +43 -0
  51. data/crates/kobako-runtime/src/net/connections.rs +179 -0
  52. data/crates/kobako-runtime/src/net/credential.rs +125 -0
  53. data/crates/kobako-runtime/src/net/egress.rs +160 -0
  54. data/crates/kobako-runtime/src/net/family.rs +181 -0
  55. data/crates/kobako-runtime/src/net/http.rs +2733 -0
  56. data/crates/kobako-runtime/src/net/lookup.rs +285 -0
  57. data/crates/kobako-runtime/src/net/mock.rs +251 -0
  58. data/crates/kobako-runtime/src/net/namespace.rs +1366 -0
  59. data/crates/kobako-runtime/src/net/reactor.rs +531 -0
  60. data/crates/kobako-runtime/src/net/resolv/darwin.rs +275 -0
  61. data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +469 -0
  62. data/crates/kobako-runtime/src/net/resolv/stub.rs +536 -0
  63. data/crates/kobako-runtime/src/net/resolv/windows.rs +248 -0
  64. data/crates/kobako-runtime/src/net/resolv.rs +227 -0
  65. data/crates/kobako-runtime/src/net/socket.rs +870 -0
  66. data/crates/kobako-runtime/src/net/tcp.rs +1588 -0
  67. data/crates/kobako-runtime/src/net/tls.rs +58 -0
  68. data/crates/kobako-runtime/src/net/transport.rs +279 -0
  69. data/crates/kobako-runtime/src/net/udp.rs +1003 -0
  70. data/crates/kobako-runtime/src/net.rs +42 -0
  71. data/crates/kobako-runtime/src/network/builder.rs +177 -0
  72. data/crates/kobako-runtime/src/network/ruleset.rs +777 -0
  73. data/crates/kobako-runtime/src/network.rs +551 -0
  74. data/crates/kobako-runtime/src/policy.rs +16 -0
  75. data/crates/kobako-runtime/src/profile.rs +37 -16
  76. data/crates/kobako-runtime/src/runtime.rs +3 -5
  77. data/crates/kobako-runtime/src/sched.rs +311 -0
  78. data/crates/kobako-runtime/src/security/entry_policy.rs +22 -0
  79. data/crates/kobako-runtime/src/security/hooks.rs +242 -0
  80. data/crates/kobako-runtime/src/security/mount_policy.rs +158 -0
  81. data/crates/kobako-runtime/src/security.rs +601 -0
  82. data/crates/kobako-runtime/src/snapshot.rs +121 -2
  83. data/crates/kobako-runtime/src/vfs/attr.rs +162 -0
  84. data/crates/kobako-runtime/src/vfs/dentry.rs +1466 -0
  85. data/crates/kobako-runtime/src/vfs/file.rs +1911 -0
  86. data/crates/kobako-runtime/src/vfs/inode.rs +425 -0
  87. data/crates/kobako-runtime/src/vfs/kern_path.rs +213 -0
  88. data/crates/kobako-runtime/src/vfs/mount.rs +60 -0
  89. data/crates/kobako-runtime/src/vfs/namei.rs +5837 -0
  90. data/crates/kobako-runtime/src/vfs/namespace.rs +162 -0
  91. data/crates/kobako-runtime/src/vfs/open_files.rs +69 -0
  92. data/crates/kobako-runtime/src/vfs/openat2.rs +116 -0
  93. data/crates/kobako-runtime/src/vfs/path.rs +64 -0
  94. data/crates/kobako-runtime/src/vfs/readdir.rs +163 -0
  95. data/crates/kobako-runtime/src/vfs/stat.rs +293 -0
  96. data/crates/kobako-runtime/src/vfs/super_block.rs +126 -0
  97. data/crates/kobako-runtime/src/vfs/utimes.rs +299 -0
  98. data/crates/kobako-runtime/src/vfs.rs +63 -0
  99. data/crates/kobako-runtime/src/wait.rs +356 -0
  100. data/crates/kobako-transport/CHANGELOG.md +39 -2
  101. data/crates/kobako-transport/Cargo.toml +3 -3
  102. data/crates/kobako-transport/README.md +8 -8
  103. data/crates/kobako-transport/src/abi.rs +78 -72
  104. data/crates/kobako-transport/src/envelope/bytes.rs +49 -30
  105. data/crates/kobako-transport/src/envelope/call.rs +37 -9
  106. data/crates/kobako-transport/src/envelope/compile_reply.rs +2 -2
  107. data/crates/kobako-transport/src/envelope/error_record.rs +24 -3
  108. data/crates/kobako-transport/src/envelope/fault.rs +2 -2
  109. data/crates/kobako-transport/src/envelope/name.rs +99 -0
  110. data/crates/kobako-transport/src/envelope/outcome.rs +11 -11
  111. data/crates/kobako-transport/src/envelope/preamble.rs +34 -31
  112. data/crates/kobako-transport/src/envelope/reply.rs +29 -29
  113. data/crates/kobako-transport/src/envelope/request.rs +2 -2
  114. data/crates/kobako-transport/src/envelope/run.rs +1 -1
  115. data/crates/kobako-transport/src/envelope/unsupported.rs +12 -12
  116. data/crates/kobako-transport/src/envelope.rs +15 -10
  117. data/crates/kobako-wasmtime/CHANGELOG.md +315 -8
  118. data/crates/kobako-wasmtime/Cargo.toml +35 -15
  119. data/crates/kobako-wasmtime/README.md +11 -6
  120. data/crates/kobako-wasmtime/build/preview2.rs +1339 -0
  121. data/crates/kobako-wasmtime/build.rs +16 -0
  122. data/crates/kobako-wasmtime/src/abi.rs +19 -47
  123. data/crates/kobako-wasmtime/src/ambient.rs +33 -19
  124. data/crates/kobako-wasmtime/src/cache.rs +28 -20
  125. data/crates/kobako-wasmtime/src/config.rs +59 -60
  126. data/crates/kobako-wasmtime/src/driver.rs +63 -145
  127. data/crates/kobako-wasmtime/src/exports.rs +147 -32
  128. data/crates/kobako-wasmtime/src/guest.rs +44 -0
  129. data/crates/kobako-wasmtime/src/guest_binary.rs +14 -0
  130. data/crates/kobako-wasmtime/src/imports.rs +48 -18
  131. data/crates/kobako-wasmtime/src/instance_pre.rs +7 -7
  132. data/crates/kobako-wasmtime/src/invocation.rs +93 -51
  133. data/crates/kobako-wasmtime/src/lib.rs +6 -7
  134. data/crates/kobako-wasmtime/src/limiter.rs +52 -211
  135. data/crates/kobako-wasmtime/src/preview1/descriptors.rs +309 -0
  136. data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +70 -57
  137. data/crates/kobako-wasmtime/src/preview1/differential.rs +137 -27
  138. data/crates/kobako-wasmtime/src/preview1/errno.rs +90 -233
  139. data/crates/kobako-wasmtime/src/preview1/fd.rs +1301 -522
  140. data/crates/kobako-wasmtime/src/preview1/memory.rs +126 -150
  141. data/crates/kobako-wasmtime/src/preview1/mount_view/wrap.rs +192 -0
  142. data/crates/kobako-wasmtime/src/preview1/mount_view.rs +1989 -708
  143. data/crates/kobako-wasmtime/src/preview1/path.rs +181 -365
  144. data/crates/kobako-wasmtime/src/preview1/poll.rs +345 -173
  145. data/crates/kobako-wasmtime/src/preview1/sock.rs +10 -25
  146. data/crates/kobako-wasmtime/src/preview1/testsuite-expected.txt +6 -0
  147. data/crates/kobako-wasmtime/src/preview1/testsuite.rs +147 -0
  148. data/crates/kobako-wasmtime/src/preview1.rs +471 -416
  149. data/crates/kobako-wasmtime/src/preview2/abi.rs +227 -0
  150. data/crates/kobako-wasmtime/src/preview2/error.rs +118 -0
  151. data/crates/kobako-wasmtime/src/preview2/filesystem.rs +122 -0
  152. data/crates/kobako-wasmtime/src/preview2/host.rs +3959 -0
  153. data/crates/kobako-wasmtime/src/preview2/http.rs +1503 -0
  154. data/crates/kobako-wasmtime/src/preview2/io.rs +315 -0
  155. data/crates/kobako-wasmtime/src/preview2/sockets.rs +379 -0
  156. data/crates/kobako-wasmtime/src/preview2/table.rs +305 -0
  157. data/crates/kobako-wasmtime/src/preview2.rs +57 -0
  158. data/crates/kobako-wasmtime/src/trap.rs +31 -16
  159. data/crates/kobako-wasmtime/src/wasi.rs +238 -93
  160. data/crates/kobako-wasmtime/wit/deps/cli.wit +233 -0
  161. data/crates/kobako-wasmtime/wit/deps/clocks.wit +162 -0
  162. data/crates/kobako-wasmtime/wit/deps/filesystem.wit +589 -0
  163. data/crates/kobako-wasmtime/wit/deps/http.wit +733 -0
  164. data/crates/kobako-wasmtime/wit/deps/io.wit +299 -0
  165. data/crates/kobako-wasmtime/wit/deps/random.wit +92 -0
  166. data/crates/kobako-wasmtime/wit/deps/sockets.wit +949 -0
  167. data/crates/kobako-wasmtime/witx/typenames.witx +750 -0
  168. data/crates/kobako-wasmtime/witx/wasi_snapshot_preview1.witx +441 -0
  169. data/data/kobako.wasm +0 -0
  170. data/ext/kobako/Cargo.toml +1 -1
  171. data/ext/kobako/src/{runtime/errors.rs → errors.rs} +35 -27
  172. data/ext/kobako/src/escape.rs +98 -0
  173. data/ext/kobako/src/{runtime/filesystem → filesystem}/grants.rs +36 -16
  174. data/ext/kobako/src/filesystem/ruby.rs +689 -0
  175. data/ext/kobako/src/filesystem.rs +217 -0
  176. data/ext/kobako/src/gvl.rs +306 -0
  177. data/ext/kobako/src/lib.rs +64 -3
  178. data/ext/kobako/src/network/mock.rs +187 -0
  179. data/ext/kobako/src/network.rs +591 -0
  180. data/ext/kobako/src/runtime/bridge.rs +31 -30
  181. data/ext/kobako/src/runtime/snapshot.rs +5 -5
  182. data/ext/kobako/src/runtime.rs +221 -138
  183. data/ext/kobako/src/wire.rs +42 -0
  184. data/lib/kobako/catalog/extensions.rb +19 -7
  185. data/lib/kobako/catalog/flags.rb +4 -6
  186. data/lib/kobako/catalog/grants.rb +5 -4
  187. data/lib/kobako/catalog/handles.rb +9 -31
  188. data/lib/kobako/catalog/mount.rb +12 -12
  189. data/lib/kobako/catalog/network.rb +35 -0
  190. data/lib/kobako/catalog/replacements.rb +28 -0
  191. data/lib/kobako/catalog/services.rb +7 -21
  192. data/lib/kobako/catalog/snippets.rb +10 -25
  193. data/lib/kobako/catalog.rb +4 -1
  194. data/lib/kobako/codec/{utils.rb → boundary.rb} +5 -3
  195. data/lib/kobako/codec/decoder.rb +12 -12
  196. data/lib/kobako/codec/ext_types.rb +5 -7
  197. data/lib/kobako/codec/handle_walk.rb +5 -47
  198. data/lib/kobako/codec/nesting.rb +7 -7
  199. data/lib/kobako/codec/value_budget.rb +134 -42
  200. data/lib/kobako/codec.rb +1 -1
  201. data/lib/kobako/context.rb +28 -15
  202. data/lib/kobako/errors.rb +15 -12
  203. data/lib/kobako/extension.rb +3 -2
  204. data/lib/kobako/filesystem.rb +53 -15
  205. data/lib/kobako/handle.rb +2 -2
  206. data/lib/kobako/names.rb +28 -0
  207. data/lib/kobako/network.rb +210 -0
  208. data/lib/kobako/outcome.rb +7 -7
  209. data/lib/kobako/payload/arguments.rb +2 -2
  210. data/lib/kobako/pool.rb +47 -25
  211. data/lib/kobako/runtime.rb +3 -2
  212. data/lib/kobako/sandbox.rb +113 -56
  213. data/lib/kobako/sandbox_options.rb +41 -107
  214. data/lib/kobako/snippet/bytecode.rb +1 -1
  215. data/lib/kobako/snippet/source.rb +1 -1
  216. data/lib/kobako/transport/argument_wrap.rb +64 -0
  217. data/lib/kobako/transport/block_failure.rb +1 -1
  218. data/lib/kobako/transport/dispatcher.rb +10 -10
  219. data/lib/kobako/transport/exposure.rb +28 -17
  220. data/lib/kobako/transport/reflection.rb +28 -11
  221. data/lib/kobako/transport/run.rb +13 -28
  222. data/lib/kobako/transport/yielder.rb +3 -10
  223. data/lib/kobako/transport.rb +1 -0
  224. data/lib/kobako/version.rb +1 -1
  225. data/lib/kobako.rb +38 -0
  226. data/release-please-config.json +16 -0
  227. data/sig/core/thread.rbs +6 -0
  228. data/sig/kobako/catalog/extensions.rbs +5 -3
  229. data/sig/kobako/catalog/grants.rbs +7 -4
  230. data/sig/kobako/catalog/handles.rbs +2 -4
  231. data/sig/kobako/catalog/mount.rbs +3 -5
  232. data/sig/kobako/catalog/network.rbs +20 -0
  233. data/sig/kobako/catalog/replacements.rbs +14 -0
  234. data/sig/kobako/catalog/services.rbs +1 -5
  235. data/sig/kobako/catalog/snippets.rbs +3 -5
  236. data/sig/kobako/catalog.rbs +1 -1
  237. data/sig/kobako/codec/boundary.rbs +9 -0
  238. data/sig/kobako/codec/decoder.rbs +1 -1
  239. data/sig/kobako/codec/handle_walk.rbs +0 -6
  240. data/sig/kobako/codec/nesting.rbs +2 -2
  241. data/sig/kobako/codec/value_budget.rbs +48 -5
  242. data/sig/kobako/codec.rbs +1 -2
  243. data/sig/kobako/context.rbs +4 -2
  244. data/sig/kobako/errors.rbs +4 -4
  245. data/sig/kobako/filesystem.rbs +38 -15
  246. data/sig/kobako/guest_yielder.rbs +1 -1
  247. data/sig/kobako/network.rbs +115 -0
  248. data/sig/kobako/outcome.rbs +1 -1
  249. data/sig/kobako/payload/arguments.rbs +1 -1
  250. data/sig/kobako/pool.rbs +8 -4
  251. data/sig/kobako/runtime.rbs +32 -3
  252. data/sig/kobako/sandbox.rbs +22 -7
  253. data/sig/kobako/sandbox_options.rbs +29 -38
  254. data/sig/kobako/transport/argument_wrap.rbs +15 -0
  255. data/sig/kobako/transport/dispatcher.rbs +2 -2
  256. data/sig/kobako/transport/error.rbs +1 -1
  257. data/sig/kobako/transport/exposure.rbs +3 -5
  258. data/sig/kobako/transport/reflection.rbs +12 -2
  259. data/sig/kobako/transport/run.rbs +3 -5
  260. data/sig/kobako/transport/yielder.rbs +0 -3
  261. data/sig/kobako/transport.rbs +2 -2
  262. data/sig/kobako.rbs +13 -0
  263. metadata +131 -34
  264. data/crates/kobako-runtime/src/filesystem/directory/spelling.rs +0 -194
  265. data/crates/kobako-runtime/src/filesystem/directory.rs +0 -1082
  266. data/crates/kobako-runtime/src/filesystem/memory.rs +0 -1100
  267. data/crates/kobako-runtime/src/filesystem/overlay/exclusive.rs +0 -88
  268. data/crates/kobako-runtime/src/filesystem/path.rs +0 -60
  269. data/crates/kobako-runtime/src/filesystem/temporary.rs +0 -167
  270. data/crates/kobako-wasmtime/src/capture.rs +0 -96
  271. data/crates/kobako-wasmtime/src/descriptors.rs +0 -167
  272. data/crates/kobako-wasmtime/src/dispatch.rs +0 -125
  273. data/crates/kobako-wasmtime/src/guest_mem.rs +0 -271
  274. data/crates/kobako-wasmtime/src/io.rs +0 -89
  275. data/crates/kobako-wasmtime/src/preview1/args.rs +0 -60
  276. data/crates/kobako-wasmtime/src/preview1/beneath.rs +0 -188
  277. data/crates/kobako-wasmtime/tests/compile_posture.rs +0 -43
  278. data/crates/kobako-wasmtime/tests/guest_file.rs +0 -1054
  279. data/crates/kobako-wasmtime/tests/preamble_flags.rs +0 -70
  280. data/crates/kobako-wasmtime/tests/snippet_compile.rs +0 -95
  281. data/crates/kobako-wasmtime/tests/support/mod.rs +0 -30
  282. data/crates/kobako-wasmtime/tests/unanswered_call.rs +0 -86
  283. data/crates/kobako-wasmtime/tests/unsupported_call.rs +0 -51
  284. data/ext/kobako/src/runtime/escape.rs +0 -53
  285. data/ext/kobako/src/runtime/filesystem/ruby.rs +0 -400
  286. data/ext/kobako/src/runtime/filesystem.rs +0 -236
  287. data/ext/kobako/src/runtime/gvl.rs +0 -112
  288. data/lib/kobako/constant_name.rb +0 -8
  289. data/sig/kobako/codec/utils.rbs +0 -9
  290. data/sig/kobako/constant_name.rbs +0 -3
  291. /data/ext/kobako/src/{runtime/pinned_value.rs → pinned_value.rs} +0 -0
@@ -1,8 +1,9 @@
1
- //! Host-side magnus shell over the extracted wasmtime driver.
1
+ //! The Ruby handle of the contract `Runtime` a Sandbox runs each
2
+ //! invocation on.
2
3
  //!
3
4
  //! The Ruby-visible classes are
4
5
  //!
5
- //! Kobako::Runtime — wraps a `kobako_wasmtime::Driver`
6
+ //! Kobako::Runtime — holds one contract `Runtime`
6
7
  //! Kobako::Snapshot — one invocation's completion + captures + usage
7
8
  //!
8
9
  //! `Kobako::Runtime` is constructed via `Kobako::Runtime.from_path(path,
@@ -11,24 +12,18 @@
11
12
  //! argument, instantiates a fresh instance, and returns a `Snapshot` — the
12
13
  //! whole per-invocation result — so the Runtime holds no per-invocation
13
14
  //! state and one Runtime is safe to drive concurrently. The run mechanics —
14
- //! engine/module caches, caps, trap classification — live in the
15
- //! `kobako-wasmtime` crate behind the `kobako_runtime` contract; no wasm
15
+ //! engine/module caches, caps, trap classification — live in the Wasm
16
+ //! Driver `from_path` builds, behind the `kobako_runtime` contract; no wasm
16
17
  //! engine type reaches this crate or the Host App.
17
18
  //!
18
19
  //! Each module holds one responsibility and opens with its own doc.
19
20
 
20
21
  mod bridge;
21
- mod errors;
22
- mod escape;
23
- mod filesystem;
24
- mod gvl;
25
- mod pinned_value;
26
22
  mod snapshot;
27
23
 
28
24
  use magnus::{
29
- function, method, prelude::*, scan_args::get_kwargs, typed_data::DataTypeFunctions,
30
- value::Opaque, Error as MagnusError, RArray, RHash, RModule, RString, Ruby, Symbol, TypedData,
31
- Value,
25
+ function, method, prelude::*, typed_data::DataTypeFunctions, value::Opaque,
26
+ Error as MagnusError, RArray, RHash, RModule, RString, Ruby, Symbol, TypedData, Value,
32
27
  };
33
28
 
34
29
  use std::path::Path;
@@ -36,23 +31,18 @@ use std::sync::Arc;
36
31
  use std::time::Duration;
37
32
 
38
33
  use kobako_runtime::dispatch::DispatchHandler;
34
+ use kobako_runtime::limits::{Cap, Limits};
35
+ use kobako_runtime::net::Backends;
39
36
  use kobako_runtime::profile::Profile;
40
37
  use kobako_runtime::runtime::{Entry, Runtime as ContractRuntime};
41
38
  use kobako_transport::envelope::{Bindings, Flag, Preamble, Snippet, Snippets};
42
39
  use kobako_wasmtime::{Config, Driver};
43
40
 
41
+ use crate::{errors, filesystem, gvl, name_of, network, symbol_name_of, to_bytes, value_by_name};
44
42
  use snapshot::Snapshot;
45
43
 
46
- /// The one place the boundary borrows an RString's bytes: the borrow does
47
- /// not outlive this call, so no Ruby allocation can move the RString
48
- /// between the borrow and the copy.
49
- fn rstring_to_vec(s: RString) -> Vec<u8> {
50
- // SAFETY: see item doc.
51
- unsafe { s.as_slice() }.to_vec()
52
- }
53
-
54
44
  /// `kind` arrives as a Symbol so the registry never names the wire's
55
- /// discriminant; an off-ladder kind raises rather than defaulting.
45
+ /// discriminant; an unknown kind raises rather than defaulting.
56
46
  fn to_snippets(ruby: &Ruby, entries: RArray) -> Result<Snippets, MagnusError> {
57
47
  let mut snippets = Snippets {
58
48
  entries: Vec::with_capacity(entries.len()),
@@ -66,7 +56,7 @@ fn to_snippets(ruby: &Ruby, entries: RArray) -> Result<Snippets, MagnusError> {
66
56
  body: entry.entry(2)?,
67
57
  },
68
58
  "bytecode" => Snippet::Bytecode {
69
- body: rstring_to_vec(entry.entry(2)?),
59
+ body: to_bytes(entry.entry(2)?),
70
60
  },
71
61
  other => {
72
62
  return Err(MagnusError::new(
@@ -106,23 +96,20 @@ fn to_preamble(ruby: &Ruby, parts: RArray) -> Result<Preamble, MagnusError> {
106
96
  // Ruby init
107
97
  // ---------------------------------------------------------------------------
108
98
 
109
- pub fn init(ruby: &Ruby, kobako: RModule) -> Result<(), MagnusError> {
110
- // Error hierarchy lives in `lib/kobako/errors.rb`; the ext raises
111
- // directly into those classes through the constructors and mappers
112
- // in `runtime/errors.rs` — no intermediate hierarchy is registered.
113
-
99
+ pub(crate) fn register(ruby: &Ruby, kobako: RModule) -> Result<(), MagnusError> {
114
100
  let runtime = kobako.define_class("Runtime", ruby.class_object())?;
115
101
  runtime.define_singleton_method("from_path", function!(Runtime::from_path, 2))?;
102
+ runtime.define_singleton_method("default_options", function!(Runtime::default_options, 0))?;
103
+ runtime.define_singleton_method("normalize_caps", function!(normalize_caps, 1))?;
104
+ runtime.define_singleton_method("value_budget_of", function!(value_budget_of, 1))?;
116
105
  runtime.define_method("eval", method!(Runtime::eval, 3))?;
117
106
  runtime.define_method("run", method!(Runtime::run, 4))?;
118
107
  runtime.define_method("profile", method!(Runtime::profile, 0))?;
119
108
  runtime.define_method("compile", method!(Runtime::compile, 1))?;
120
- runtime.define_singleton_method("validate_mount!", function!(filesystem::validate_mount, 3))?;
121
109
  // The guest re-enters for a block yield through a frame-scoped
122
110
  // `Kobako::GuestYielder` the dispatcher hands the Proc, not a method on
123
111
  // Runtime.
124
112
  bridge::register(kobako)?;
125
- filesystem::register(ruby, kobako)?;
126
113
 
127
114
  // Snapshot — the per-invocation result object each entry point returns.
128
115
  snapshot::register(ruby, kobako)?;
@@ -137,16 +124,133 @@ pub fn init(ruby: &Ruby, kobako: RModule) -> Result<(), MagnusError> {
137
124
  Ok(())
138
125
  }
139
126
 
127
+ /// Each cap in `caps`, read as `to_limits` reads it and keyed as
128
+ /// `Kobako::SandboxOptions` names it.
129
+ fn normalize_caps(caps: RHash) -> Result<RHash, MagnusError> {
130
+ let ruby = Ruby::get().expect("Ruby thread");
131
+ caps_of(&ruby, &to_limits(&ruby, caps)?)
132
+ }
133
+
134
+ /// The most values a guest payload may make the host build under
135
+ /// `memory_limit`, as the runtime's caps count them.
136
+ fn value_budget_of(memory_limit: Option<usize>) -> Option<usize> {
137
+ let mut limits = Limits::default();
138
+ limits.memory_limit = memory_limit;
139
+ limits.value_budget()
140
+ }
141
+
142
+ /// The caps in `caps`, keyed as `Kobako::SandboxOptions` names them, each read
143
+ /// as its kind: a deadline as seconds, a positive finite Numeric, and every
144
+ /// other cap as a positive Integer, `nil` asking for none. One outside its
145
+ /// kind is refused as an argument error, and a zero by the runtime's own
146
+ /// check, which the Rust SDK asks too.
147
+ fn to_limits(ruby: &Ruby, caps: RHash) -> Result<Limits, MagnusError> {
148
+ let mut limits = Limits::default();
149
+ for (name, cap) in limits.caps_mut() {
150
+ let value = caps.fetch(ruby.to_symbol(name))?;
151
+ match cap {
152
+ Cap::Deadline(deadline) => *deadline = to_deadline(ruby, name, value)?,
153
+ Cap::Count(count) => *count = to_count(ruby, name, value)?,
154
+ }
155
+ }
156
+ limits
157
+ .check()
158
+ .map_err(|zero| MagnusError::new(ruby.exception_arg_error(), zero.to_string()))?;
159
+ Ok(limits)
160
+ }
161
+
162
+ /// Each cap `limits` holds, keyed as `Kobako::SandboxOptions` names it, a
163
+ /// deadline in seconds.
164
+ fn caps_of(ruby: &Ruby, limits: &Limits) -> Result<RHash, MagnusError> {
165
+ let caps = ruby.hash_new();
166
+ let mut limits = *limits;
167
+ for (name, cap) in limits.caps_mut() {
168
+ let key = ruby.to_symbol(name);
169
+ match cap {
170
+ Cap::Deadline(deadline) => caps.aset(key, deadline.map(|d| d.as_secs_f64()))?,
171
+ Cap::Count(count) => caps.aset(key, *count)?,
172
+ }
173
+ }
174
+ Ok(caps)
175
+ }
176
+
177
+ /// The deadline `value` names in seconds for `name`, none for `nil`. A
178
+ /// deadline that never arrives, is past what a `Duration` counts, or is not a
179
+ /// number, is refused: the way to ask for none is `nil`.
180
+ fn to_deadline(ruby: &Ruby, name: &str, value: Value) -> Result<Option<Duration>, MagnusError> {
181
+ if value.is_nil() {
182
+ return Ok(None);
183
+ }
184
+ if !value.is_kind_of(ruby.class_numeric()) {
185
+ return Err(MagnusError::new(
186
+ ruby.exception_type_error(),
187
+ format!(
188
+ "{name} must be Numeric or nil, got {}",
189
+ value.class().inspect()
190
+ ),
191
+ ));
192
+ }
193
+ let seconds: f64 = value.funcall("to_f", ())?;
194
+ let refusal = if seconds > 0.0 && seconds.is_finite() {
195
+ ruby.exception_range_error()
196
+ } else {
197
+ ruby.exception_arg_error()
198
+ };
199
+ match Duration::try_from_secs_f64(seconds) {
200
+ Ok(deadline) if seconds.is_finite() => Ok(Some(deadline)),
201
+ _ => Err(MagnusError::new(
202
+ refusal,
203
+ format!(
204
+ "{name} must be > 0 and at most {} seconds (got {})",
205
+ Duration::MAX.as_secs(),
206
+ value.inspect()
207
+ ),
208
+ )),
209
+ }
210
+ }
211
+
212
+ /// The count `value` names for the cap `name`, none for `nil`; anything but
213
+ /// an Integer a `usize` holds is refused.
214
+ fn to_count(ruby: &Ruby, name: &str, value: Value) -> Result<Option<usize>, MagnusError> {
215
+ if value.is_nil() {
216
+ return Ok(None);
217
+ }
218
+ let Some(integer) = magnus::Integer::from_value(value) else {
219
+ return Err(MagnusError::new(
220
+ ruby.exception_type_error(),
221
+ format!(
222
+ "{name} must be an Integer or nil, got {}",
223
+ value.class().inspect()
224
+ ),
225
+ ));
226
+ };
227
+ let refusal = if value.funcall::<_, _, bool>("negative?", ())? {
228
+ ruby.exception_arg_error()
229
+ } else {
230
+ ruby.exception_range_error()
231
+ };
232
+ match integer.to_usize().ok() {
233
+ Some(count) => Ok(Some(count)),
234
+ None => Err(MagnusError::new(
235
+ refusal,
236
+ format!(
237
+ "{name} must be a positive Integer or nil, got {}",
238
+ value.inspect()
239
+ ),
240
+ )),
241
+ }
242
+ }
243
+
140
244
  #[derive(TypedData)]
141
245
  #[magnus(class = "Kobako::Runtime", free_immediately, size)]
142
246
  struct Runtime {
143
- // The magnus-free wasmtime driver that runs every invocation; the
144
- // shell only shuttles Ruby values across its boundary. The Runtime
145
- // holds no per-invocation state — each `#eval` / `#run` takes its
146
- // dispatch handler as an argument and returns its whole result as a
147
- // `Snapshot` — so `Driver`'s own `Send + Sync` carries the type with no
148
- // interior mutability to guard.
149
- driver: Driver,
247
+ // The runtime every invocation runs on, held behind the contract so the
248
+ // driver is named only where it is built. The Runtime holds no
249
+ // per-invocation state — each `#eval` / `#run` takes its dispatch
250
+ // handler as an argument and returns its whole result as a `Snapshot`
251
+ // — so the contract's `Send + Sync` carries the type with no interior
252
+ // mutability to guard.
253
+ runtime: Box<dyn ContractRuntime + Send + Sync>,
150
254
  // Whether each invocation releases Ruby's GVL for its guest span
151
255
  // (`gvl: :release`) or holds it throughout (`gvl: :hold`). Fixed at
152
256
  // construction; a `bool` carries no interior mutability, so the type
@@ -157,99 +261,69 @@ struct Runtime {
157
261
  impl DataTypeFunctions for Runtime {}
158
262
 
159
263
  impl Runtime {
264
+ /// Each option `from_path` takes, keyed as `Kobako::SandboxOptions` names
265
+ /// it, at the runtime's default, so every frontend applies one set. A
266
+ /// run holds the GVL unless the Host App asks to release it.
267
+ fn default_options() -> Result<RHash, MagnusError> {
268
+ let ruby = Ruby::get().expect("Ruby thread");
269
+ let options = caps_of(&ruby, &Limits::default())?;
270
+ options.aset(
271
+ ruby.to_symbol("profile"),
272
+ to_profile_name(&ruby, Profile::default()),
273
+ )?;
274
+ options.aset(ruby.to_symbol("gvl"), ruby.to_symbol("hold"))?;
275
+ options.aset(ruby.to_symbol("network"), ruby.qnil())?;
276
+ Ok(options)
277
+ }
278
+
160
279
  /// The only Ruby-facing constructor, so Engine and Module are never
161
- /// visible to Ruby. Every option is a required keyword: the defaults
162
- /// belong to `Kobako::SandboxOptions`, which names the keys.
280
+ /// visible to Ruby. Every option is a required keyword, as
281
+ /// `default_options` answers each.
163
282
  fn from_path(path: String, options: RHash) -> Result<Self, MagnusError> {
164
283
  let ruby = Ruby::get().expect("Ruby thread");
165
- type Options = (
166
- Option<f64>,
167
- Option<f64>,
168
- Option<usize>,
169
- Option<usize>,
170
- Option<usize>,
171
- Option<usize>,
172
- Symbol,
173
- Symbol,
174
- );
175
- let options = get_kwargs::<_, Options, (), ()>(
176
- options,
177
- &[
178
- "timeout",
179
- "setup_timeout",
180
- "memory_limit",
181
- "stdout_limit",
182
- "stderr_limit",
183
- "open_file_limit",
184
- "profile",
185
- "gvl",
186
- ],
187
- &[],
188
- )?;
189
- let (
190
- timeout_seconds,
191
- setup_timeout_seconds,
192
- memory_limit,
193
- stdout_limit,
194
- stderr_limit,
195
- open_file_limit,
196
- profile,
197
- gvl,
198
- ) = options.required;
199
- // An invalid cap argument is a Host App programming error and
200
- // raises `ArgumentError`, outside the construction-failure
201
- // `SetupError` branch. `SandboxOptions` is the primary guard (it
202
- // never lets a bad timeout reach here); this is defence-in-depth
203
- // for direct `from_path` calls.
204
- let to_duration = |seconds: Option<f64>, name: &str| match seconds {
205
- None => Ok(None),
206
- Some(secs) if secs.is_finite() && secs > 0.0 => Ok(Some(Duration::from_secs_f64(secs))),
207
- Some(secs) => Err(MagnusError::new(
284
+ let limits = to_limits(&ruby, options)?;
285
+ let profile: Value = options.fetch(ruby.to_symbol("profile"))?;
286
+ let gvl: Value = options.fetch(ruby.to_symbol("gvl"))?;
287
+ // A value kobako does not name a profile has no profile to stand
288
+ // for, so it is refused rather than read as a weaker one.
289
+ let Some(profile) =
290
+ symbol_name_of(profile)?.and_then(|name| value_by_name(&PROFILE_NAMES, &name))
291
+ else {
292
+ return Err(MagnusError::new(
208
293
  ruby.exception_arg_error(),
209
- format!("{name} must be > 0 and finite, got {secs} seconds"),
210
- )),
294
+ format!(
295
+ "profile must be :permissive or :hermetic, got {}",
296
+ profile.inspect()
297
+ ),
298
+ ));
211
299
  };
212
- let timeout = to_duration(timeout_seconds, "timeout")?;
213
- let setup_timeout = to_duration(setup_timeout_seconds, "setup_timeout")?;
214
- // Fail closed on an off-ladder rung: an unrecognized posture must
215
- // never fall back to a weaker posture. Same defence in depth as the
216
- // timeout guard above — `SandboxOptions` is the primary validator.
217
- let profile = match profile.name()?.as_ref() {
218
- "hermetic" => Profile::Hermetic,
219
- "permissive" => Profile::Permissive,
220
- other => {
300
+ // A mode kobako does not name is refused as a profile is.
301
+ let release_gvl = match symbol_name_of(gvl)?.as_deref() {
302
+ Some("hold") => false,
303
+ Some("release") => true,
304
+ _ => {
221
305
  return Err(MagnusError::new(
222
306
  ruby.exception_arg_error(),
223
- format!("profile must be :permissive or :hermetic, got :{other}"),
307
+ format!("gvl must be :hold or :release, got {}", gvl.inspect()),
224
308
  ));
225
309
  }
226
310
  };
227
- // Same fail-closed posture as `profile`: an unrecognized mode raises
228
- // rather than defaulting. `SandboxOptions` is the primary validator;
229
- // this guards direct `from_path` calls.
230
- let release_gvl = match gvl.name()?.as_ref() {
231
- "hold" => false,
232
- "release" => true,
233
- other => {
234
- return Err(MagnusError::new(
235
- ruby.exception_arg_error(),
236
- format!("gvl must be :hold or :release, got :{other}"),
237
- ));
238
- }
311
+
312
+ // `nil` is the host's own network; anything else must be a Mock.
313
+ let mock: Value = options.fetch(ruby.to_symbol("network"))?;
314
+ let backends = match mock.is_nil() {
315
+ true => Backends::default(),
316
+ false => <&network::MockNet>::try_convert(mock)?.backends(),
239
317
  };
240
318
 
241
319
  let mut config = Config::default();
242
- config.timeout = timeout;
243
- config.setup_timeout = setup_timeout;
244
- config.memory_limit = memory_limit;
245
- config.stdout_limit = stdout_limit;
246
- config.stderr_limit = stderr_limit;
247
- config.open_file_limit = open_file_limit;
320
+ config.limits = limits;
248
321
  config.profile = profile;
249
- let driver =
250
- Driver::new(Path::new(&path), config).map_err(|e| errors::setup_to_magnus(&ruby, e))?;
322
+ config.network = backends;
323
+ let driver = Driver::from_file(Path::new(&path), config)
324
+ .map_err(|e| errors::from_setup(&ruby, e))?;
251
325
  Ok(Self {
252
- driver,
326
+ runtime: Box::new(driver),
253
327
  release_gvl,
254
328
  })
255
329
  }
@@ -259,11 +333,12 @@ impl Runtime {
259
333
  // argument and returns a `Snapshot` for any completed invocation —
260
334
  // success or trap alike. Only a could-not-start fault (a missing export
261
335
  // or a fault before the export call) raises directly, as the class
262
- // `errors::to_magnus` gives its cause, since it yields no `Snapshot`.
336
+ // `errors::from_invoke` gives its cause, since it yields no `Snapshot`.
263
337
  //
264
- // `setup` is the invocation's `[paths, snippets, flags, mounts]`: the
265
- // Preamble's three parts, then the Mounts as
266
- // `filesystem::to_grants` reads them.
338
+ // `setup` is the invocation's `[paths, snippets, flags, mounts,
339
+ // networks]`: the Preamble's three parts, then the Mounts as
340
+ // `filesystem::to_grants` reads them and the network entries as
341
+ // `network::to_grants` reads them.
267
342
  // -----------------------------------------------------------------
268
343
 
269
344
  fn eval(
@@ -272,7 +347,7 @@ impl Runtime {
272
347
  setup: RArray,
273
348
  source: RString,
274
349
  ) -> Result<Snapshot, MagnusError> {
275
- let source = rstring_to_vec(source);
350
+ let source = to_bytes(source);
276
351
  self.invoke(dispatch, setup, Entry::Eval { source: &source })
277
352
  }
278
353
 
@@ -288,7 +363,7 @@ impl Runtime {
288
363
  setup,
289
364
  Entry::Run {
290
365
  entrypoint: &entrypoint,
291
- payload: rstring_to_vec(payload),
366
+ payload: to_bytes(payload),
292
367
  },
293
368
  )
294
369
  }
@@ -308,29 +383,30 @@ impl Runtime {
308
383
  };
309
384
  let handler = build_handler(dispatch);
310
385
  let preamble = to_preamble(&ruby, setup)?;
311
- let (grants, _pins) = filesystem::to_grants(&ruby, setup.entry(3)?)?;
386
+ let (mut grants, mut pins) = filesystem::to_grants(&ruby, setup.entry(3)?)?;
387
+ let (networks, network_pins) = network::to_grants(&ruby, setup.entry(4)?)?;
388
+ grants.extend(networks);
389
+ pins.extend(network_pins);
312
390
  // Release the GVL around the guest span iff this Sandbox asks for it;
313
- // the closure touches no Ruby VALUE (the driver is magnus-free, and a
391
+ // the closure touches no Ruby VALUE (the runtime is magnus-free, and a
314
392
  // guest→host dispatch, or a mount's block, re-acquires the GVL).
315
- let result = gvl::region(self.release_gvl, || {
316
- self.driver.invoke(entry, &preamble, handler, &grants)
317
- });
318
- escape::resume()?;
319
- result
320
- .map(|snapshot| Snapshot::new(snapshot, verb))
321
- .map_err(|e| errors::to_magnus(&ruby, e))
393
+ gvl::run(&ruby, self.release_gvl, || {
394
+ self.runtime.invoke(entry, &preamble, handler, &grants)
395
+ })?
396
+ .map(|snapshot| Snapshot::new(snapshot, verb))
397
+ .map_err(|e| errors::from_invoke(&ruby, e))
322
398
  }
323
399
 
324
400
  /// The bytecode of the source snippets, one binary String each in
325
401
  /// order, or `nil` when the guest compiled nothing; no failure raises,
326
402
  /// since the Sandbox keeps the source either way. Takes the Preamble an
327
- /// invocation sends, `paths` included, so the driver can tell whether
403
+ /// invocation sends, `paths` included, so the runtime can tell whether
328
404
  /// the bytecode still fits it; no dispatch handler is bound, since
329
405
  /// compiling runs no script.
330
406
  fn compile(&self, preamble: RArray) -> Result<Option<RArray>, MagnusError> {
331
407
  let ruby = Ruby::get().expect("Ruby thread");
332
408
  let preamble = to_preamble(&ruby, preamble)?;
333
- let bytecodes = gvl::region(self.release_gvl, || self.driver.compile(&preamble));
409
+ let bytecodes = gvl::run(&ruby, self.release_gvl, || self.runtime.compile(&preamble))?;
334
410
  Ok(bytecodes.map(|bytecodes| {
335
411
  ruby.ary_from_iter(
336
412
  bytecodes
@@ -340,19 +416,26 @@ impl Runtime {
340
416
  }))
341
417
  }
342
418
 
343
- /// The driver's declaration, which the Sandbox checks against the
344
- /// posture its `profile:` option requested.
419
+ /// The isolation profile the runtime declares it built.
345
420
  fn profile(&self) -> Symbol {
346
421
  let ruby = Ruby::get().expect("Ruby thread");
347
- match self.driver.profile() {
348
- Profile::Hermetic => ruby.to_symbol("hermetic"),
349
- Profile::Permissive => ruby.to_symbol("permissive"),
350
- }
422
+ to_profile_name(&ruby, self.runtime.profile())
351
423
  }
352
424
  }
353
425
 
426
+ /// Each profile beside the name the `profile:` option takes it by.
427
+ const PROFILE_NAMES: [(Profile, &str); 2] = [
428
+ (Profile::Hermetic, "hermetic"),
429
+ (Profile::Permissive, "permissive"),
430
+ ];
431
+
432
+ /// `profile` as the Symbol the `profile:` option names it by.
433
+ fn to_profile_name(ruby: &Ruby, profile: Profile) -> Symbol {
434
+ ruby.to_symbol(name_of(&PROFILE_NAMES, &profile).expect("every profile has a name"))
435
+ }
436
+
354
437
  /// The Proc stays GC-rooted for the synchronous `#eval` / `#run` call as a
355
- /// live method argument on the Ruby stack, so the driver only borrows it
438
+ /// live method argument on the Ruby stack, so the runtime only borrows it
356
439
  /// (the safety contract on `kobako_runtime::runtime::Runtime`).
357
440
  fn build_handler(dispatch: Value) -> Option<Arc<dyn DispatchHandler>> {
358
441
  if dispatch.is_nil() {
@@ -0,0 +1,42 @@
1
+ //! The Wire's rules a Ruby frontend asks before it writes a name the guest
2
+ //! interns or issues a Capability Handle id, so both frontends hold to the
3
+ //! one rule the Envelope tier keeps.
4
+
5
+ use magnus::{function, prelude::*, Error as MagnusError, RModule, RString};
6
+
7
+ use kobako_transport::envelope::name::{collides, is_constant_name, is_constant_path, is_reserved};
8
+ use kobako_transport::envelope::MAX_HANDLE_ID;
9
+
10
+ pub(crate) fn register(kobako: RModule) -> Result<(), MagnusError> {
11
+ kobako.define_singleton_method("max_handle_id", function!(|| MAX_HANDLE_ID, 0))?;
12
+ kobako.define_singleton_method(
13
+ "constant_name?",
14
+ function!(|name: RString| holds(name, is_constant_name), 1),
15
+ )?;
16
+ kobako.define_singleton_method(
17
+ "constant_path?",
18
+ function!(|path: RString| holds(path, is_constant_path), 1),
19
+ )?;
20
+ kobako.define_singleton_method(
21
+ "reserved_path?",
22
+ function!(|path: RString| holds(path, is_reserved), 1),
23
+ )?;
24
+ kobako.define_singleton_method("paths_collide?", function!(paths_collide, 2))?;
25
+ Ok(())
26
+ }
27
+
28
+ /// Whether the paths `path` and `bound` spell cannot both be bound; bytes
29
+ /// that spell no text are no name at all, so they collide with nothing.
30
+ fn paths_collide(path: RString, bound: RString) -> bool {
31
+ let (path, bound) = (crate::to_bytes(path), crate::to_bytes(bound));
32
+ match (std::str::from_utf8(&path), std::str::from_utf8(&bound)) {
33
+ (Ok(path), Ok(bound)) => collides(path, bound),
34
+ _ => false,
35
+ }
36
+ }
37
+
38
+ /// Whether `rule` holds for the text `s` spells; bytes that spell no text
39
+ /// are no name at all.
40
+ fn holds(s: RString, rule: fn(&str) -> bool) -> bool {
41
+ std::str::from_utf8(&crate::to_bytes(s)).is_ok_and(rule)
42
+ }
@@ -16,10 +16,10 @@ module Kobako
16
16
  # The Extension readers are duck-typed, so a malformed +name+ or
17
17
  # +backend.path+ surfaces through the registry it routes to.
18
18
  def install(extension, snippets:, services:)
19
- validate!(extension)
19
+ validate(extension)
20
20
  snippets.register(code: extension.source, name: extension.name)
21
21
  backend = extension.backend
22
- services.bind(backend.path, install_object(backend)) if backend
22
+ services.bind(backend.path, object_of(backend)) if backend
23
23
  @entries << extension
24
24
  self
25
25
  end
@@ -27,7 +27,7 @@ module Kobako
27
27
  # The asserted flag flips only on success, so a check that failed
28
28
  # re-runs on the next invocation rather than silently passing a broken
29
29
  # Sandbox.
30
- def assert_dependencies!
30
+ def assert_dependencies
31
31
  return self if @asserted
32
32
 
33
33
  extension, dependency = unmet_dependency
@@ -52,7 +52,7 @@ module Kobako
52
52
 
53
53
  private
54
54
 
55
- def install_object(backend) = backend.object.nil? ? Kobako::Unresolved : backend.object
55
+ def object_of(backend) = backend.object.nil? ? Kobako::Unresolved : backend.object
56
56
 
57
57
  def resolve_backend(extension, resolved, by_provider)
58
58
  backend = extension.backend
@@ -64,15 +64,27 @@ module Kobako
64
64
  resolved[backend.path.to_s] = by_provider.fetch(provider) { by_provider[provider] = provider.call }
65
65
  end
66
66
 
67
- def validate!(extension)
67
+ def validate(extension)
68
68
  source = extension.source
69
- raise ArgumentError, "Extension #source must be a String, got #{source.class}" unless source.is_a?(String)
69
+ raise TypeError, "Extension #source must be a String, got #{source.class}" unless source.is_a?(String)
70
70
 
71
+ validate_dependencies(extension.depends_on)
71
72
  backend = extension.backend
72
73
  return if backend.nil?
73
74
  return if backend.respond_to?(:path) && backend.respond_to?(:object) && backend.respond_to?(:provider)
74
75
 
75
- raise ArgumentError, "Extension #backend must expose #path, #object, and #provider"
76
+ raise TypeError, "Extension #backend must expose #path, #object, and #provider"
77
+ end
78
+
79
+ # Each dependency names an Extension as its +name+ does.
80
+ def validate_dependencies(dependencies)
81
+ return if dependencies.nil?
82
+ unless dependencies.is_a?(Array)
83
+ raise TypeError,
84
+ "Extension #depends_on must be an Array, got #{dependencies.class}"
85
+ end
86
+
87
+ dependencies.each { |dependency| Kobako.to_name(dependency, "Extension #depends_on entry") }
76
88
  end
77
89
 
78
90
  def unmet_dependency
@@ -13,15 +13,13 @@ module Kobako
13
13
  end
14
14
 
15
15
  def register(name, value, advisory:)
16
- unless name.is_a?(Symbol) || name.is_a?(String)
17
- raise ArgumentError, "flag name must be a Symbol or String, got #{name.class}"
18
- end
19
- raise ArgumentError, "flag value must be a String, got #{value.class}" unless value.is_a?(String)
16
+ name = Kobako.to_name(name, "flag name")
17
+ raise TypeError, "flag value must be a String, got #{value.class}" unless value.is_a?(String)
20
18
 
21
- key = Catalog.to_utf8!(name.to_s, "flag name")
19
+ key = Catalog.to_utf8(name, "flag name")
22
20
  raise ArgumentError, "flag #{key.inspect} already set" if @entries.key?(key)
23
21
 
24
- @entries[key] = [Catalog.to_utf8!(value, "flag value"), !advisory]
22
+ @entries[key] = [Catalog.to_utf8(value, "flag value"), !advisory]
25
23
  nil
26
24
  end
27
25
  end
@@ -4,7 +4,7 @@ module Kobako
4
4
  module Catalog
5
5
  class Grants # :nodoc:
6
6
  def initialize
7
- @entries = {} # : Hash[String, Mount]
7
+ @entries = {} # : Hash[key, Mount | Network]
8
8
  end
9
9
 
10
10
  # Each Grant decides what it cannot stand beside.
@@ -20,10 +20,11 @@ module Kobako
20
20
  @entries.key?(grant.key)
21
21
  end
22
22
 
23
- # Each Grant as the native side reads it, an override standing in for
24
- # the Grant it shares a key with.
23
+ # The Mounts, then the network entries, each as the native side reads
24
+ # it, an override standing in for the Grant it shares a key with.
25
25
  def entries(overrides = {})
26
- @entries.merge(overrides).values.map(&:to_native)
26
+ grants = @entries.merge(overrides).values
27
+ [grants.grep(Mount).map(&:to_native), grants.grep(Network).map(&:to_native)]
27
28
  end
28
29
  end
29
30
  end