├── .gitignore ├── LICENSE ├── README.md ├── example ├── README.md ├── example_assemblyscript_host │ ├── Cargo.lock │ ├── Cargo.toml │ ├── README.md │ ├── assemblyscript_plugin │ │ ├── .gitignore │ │ ├── asconfig.json │ │ ├── assembly │ │ │ ├── env.ts │ │ │ ├── index.ts │ │ │ └── tsconfig.json │ │ ├── build │ │ │ └── .gitignore │ │ └── package.json │ └── src │ │ └── main.rs ├── example_guest │ ├── Cargo.lock │ ├── Cargo.toml │ ├── README.md │ └── src │ │ └── lib.rs └── example_host │ ├── Cargo.lock │ ├── Cargo.toml │ ├── README.md │ └── src │ └── main.rs ├── guest ├── Cargo.lock ├── Cargo.toml ├── README.md ├── guest_derive │ ├── Cargo.lock │ ├── Cargo.toml │ └── src │ │ └── lib.rs └── src │ ├── lib.rs │ └── serialization.rs └── host ├── Cargo.lock ├── Cargo.toml ├── README.md └── src ├── errors.rs ├── lib.rs └── serialization.rs /.gitignore: -------------------------------------------------------------------------------- 1 | **/target 2 | unused_soundbanks 3 | ship_dump.ron 4 | .cargo/config.toml 5 | bundle 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright © 2021 Alec Deason 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #OBSOLETE# 2 | These crates are no longer maintained and the use case is better served by the [WebAssembly Component specification](https://component-model.bytecodealliance.org/language-support/rust.html). 3 | 4 | 5 | A low-ish level tool for easily writing and hosting WASM based plugins. 6 | 7 | The goal of wasm_plugin is to make communicating across the host-plugin 8 | boundary as simple and idiomatic as possible while being unopinionated 9 | about how you actually use the plugin. 10 | 11 | 12 | Loading a plugin is as simple as reading the .wasm file off disk. 13 | 14 | ```rust 15 | let mut plugin = WasmPluginBuilder::from_file("path/to/plugin.wasm")?.finish()?; 16 | ``` 17 | 18 | Calling functions exported by the plugin takes one of two forms. Either 19 | the function takes no arguments and returns a single serde deserializable 20 | value: 21 | 22 | ```rust 23 | let response: ResultType = plugin.call_function("function_name")?; 24 | ``` 25 | 26 | Or it takes a single serializable argument and returns a single result: 27 | 28 | ```rust 29 | let message = Message::default(); 30 | let response: ResultType = plugin.call_function_with_argument("function_name", &message)?; 31 | ``` 32 | 33 | Exporting a function from a plugin is just a matter of wrapping it in a macro: 34 | 35 | ```rust 36 | fn local_hello() -> String { 37 | "Hello, host!".to_string() 38 | } 39 | wasm_plugin_guest::export_plugin_function_with_no_input(hello, local_hello); 40 | ``` 41 | 42 | ## API Stability 43 | 44 | I am not currently guaranteeing any stability, expect all releases to include breaking changes. 45 | -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | An example of a host/plugin pair. First build `example_guest` and then run `example_host`. 2 | 3 | example_assemblyscript_host demonstrates how to host non-rust plugins. 4 | -------------------------------------------------------------------------------- /example/example_assemblyscript_host/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "addr2line" 5 | version = "0.14.1" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | checksum = "a55f82cfe485775d02112886f4169bde0c5894d75e79ead7eafe7e40a25e45f7" 8 | dependencies = [ 9 | "gimli 0.23.0", 10 | ] 11 | 12 | [[package]] 13 | name = "adler" 14 | version = "1.0.2" 15 | source = "registry+https://github.com/rust-lang/crates.io-index" 16 | checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" 17 | 18 | [[package]] 19 | name = "autocfg" 20 | version = "1.0.1" 21 | source = "registry+https://github.com/rust-lang/crates.io-index" 22 | checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" 23 | 24 | [[package]] 25 | name = "backtrace" 26 | version = "0.3.56" 27 | source = "registry+https://github.com/rust-lang/crates.io-index" 28 | checksum = "9d117600f438b1707d4e4ae15d3595657288f8235a0eb593e80ecc98ab34e1bc" 29 | dependencies = [ 30 | "addr2line", 31 | "cfg-if 1.0.0", 32 | "libc", 33 | "miniz_oxide", 34 | "object 0.23.0", 35 | "rustc-demangle", 36 | ] 37 | 38 | [[package]] 39 | name = "bincode" 40 | version = "1.3.2" 41 | source = "registry+https://github.com/rust-lang/crates.io-index" 42 | checksum = "d175dfa69e619905c4c3cdb7c3c203fa3bdd5d51184e3afdb2742c0280493772" 43 | dependencies = [ 44 | "byteorder", 45 | "serde", 46 | ] 47 | 48 | [[package]] 49 | name = "bitfield" 50 | version = "0.13.2" 51 | source = "registry+https://github.com/rust-lang/crates.io-index" 52 | checksum = "46afbd2983a5d5a7bd740ccb198caf5b82f45c40c09c0eed36052d91cb92e719" 53 | 54 | [[package]] 55 | name = "bitflags" 56 | version = "1.2.1" 57 | source = "registry+https://github.com/rust-lang/crates.io-index" 58 | checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" 59 | 60 | [[package]] 61 | name = "byteorder" 62 | version = "1.3.4" 63 | source = "registry+https://github.com/rust-lang/crates.io-index" 64 | checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" 65 | 66 | [[package]] 67 | name = "cc" 68 | version = "1.0.67" 69 | source = "registry+https://github.com/rust-lang/crates.io-index" 70 | checksum = "e3c69b077ad434294d3ce9f1f6143a2a4b89a8a2d54ef813d85003a4fd1137fd" 71 | 72 | [[package]] 73 | name = "cfg-if" 74 | version = "0.1.10" 75 | source = "registry+https://github.com/rust-lang/crates.io-index" 76 | checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" 77 | 78 | [[package]] 79 | name = "cfg-if" 80 | version = "1.0.0" 81 | source = "registry+https://github.com/rust-lang/crates.io-index" 82 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 83 | 84 | [[package]] 85 | name = "cranelift-bforest" 86 | version = "0.68.0" 87 | source = "registry+https://github.com/rust-lang/crates.io-index" 88 | checksum = "9221545c0507dc08a62b2d8b5ffe8e17ac580b0a74d1813b496b8d70b070fbd0" 89 | dependencies = [ 90 | "cranelift-entity", 91 | ] 92 | 93 | [[package]] 94 | name = "cranelift-codegen" 95 | version = "0.68.0" 96 | source = "registry+https://github.com/rust-lang/crates.io-index" 97 | checksum = "7e9936ea608b6cd176f107037f6adbb4deac933466fc7231154f96598b2d3ab1" 98 | dependencies = [ 99 | "byteorder", 100 | "cranelift-bforest", 101 | "cranelift-codegen-meta", 102 | "cranelift-codegen-shared", 103 | "cranelift-entity", 104 | "gimli 0.22.0", 105 | "log", 106 | "regalloc", 107 | "smallvec", 108 | "target-lexicon", 109 | "thiserror", 110 | ] 111 | 112 | [[package]] 113 | name = "cranelift-codegen-meta" 114 | version = "0.68.0" 115 | source = "registry+https://github.com/rust-lang/crates.io-index" 116 | checksum = "4ef2b2768568306540f4c8db3acce9105534d34c4a1e440529c1e702d7f8c8d7" 117 | dependencies = [ 118 | "cranelift-codegen-shared", 119 | "cranelift-entity", 120 | ] 121 | 122 | [[package]] 123 | name = "cranelift-codegen-shared" 124 | version = "0.68.0" 125 | source = "registry+https://github.com/rust-lang/crates.io-index" 126 | checksum = "6759012d6d19c4caec95793f052613e9d4113e925e7f14154defbac0f1d4c938" 127 | 128 | [[package]] 129 | name = "cranelift-entity" 130 | version = "0.68.0" 131 | source = "registry+https://github.com/rust-lang/crates.io-index" 132 | checksum = "86badbce14e15f52a45b666b38abe47b204969dd7f8fb7488cb55dd46b361fa6" 133 | dependencies = [ 134 | "serde", 135 | ] 136 | 137 | [[package]] 138 | name = "cranelift-frontend" 139 | version = "0.68.0" 140 | source = "registry+https://github.com/rust-lang/crates.io-index" 141 | checksum = "b608bb7656c554d0a4cf8f50c7a10b857e80306f6ff829ad6d468a7e2323c8d8" 142 | dependencies = [ 143 | "cranelift-codegen", 144 | "log", 145 | "smallvec", 146 | "target-lexicon", 147 | ] 148 | 149 | [[package]] 150 | name = "crc32fast" 151 | version = "1.2.1" 152 | source = "registry+https://github.com/rust-lang/crates.io-index" 153 | checksum = "81156fece84ab6a9f2afdb109ce3ae577e42b1228441eded99bd77f627953b1a" 154 | dependencies = [ 155 | "cfg-if 1.0.0", 156 | ] 157 | 158 | [[package]] 159 | name = "crossbeam-channel" 160 | version = "0.5.0" 161 | source = "registry+https://github.com/rust-lang/crates.io-index" 162 | checksum = "dca26ee1f8d361640700bde38b2c37d8c22b3ce2d360e1fc1c74ea4b0aa7d775" 163 | dependencies = [ 164 | "cfg-if 1.0.0", 165 | "crossbeam-utils", 166 | ] 167 | 168 | [[package]] 169 | name = "crossbeam-deque" 170 | version = "0.8.0" 171 | source = "registry+https://github.com/rust-lang/crates.io-index" 172 | checksum = "94af6efb46fef72616855b036a624cf27ba656ffc9be1b9a3c931cfc7749a9a9" 173 | dependencies = [ 174 | "cfg-if 1.0.0", 175 | "crossbeam-epoch", 176 | "crossbeam-utils", 177 | ] 178 | 179 | [[package]] 180 | name = "crossbeam-epoch" 181 | version = "0.9.3" 182 | source = "registry+https://github.com/rust-lang/crates.io-index" 183 | checksum = "2584f639eb95fea8c798496315b297cf81b9b58b6d30ab066a75455333cf4b12" 184 | dependencies = [ 185 | "cfg-if 1.0.0", 186 | "crossbeam-utils", 187 | "lazy_static", 188 | "memoffset", 189 | "scopeguard", 190 | ] 191 | 192 | [[package]] 193 | name = "crossbeam-utils" 194 | version = "0.8.3" 195 | source = "registry+https://github.com/rust-lang/crates.io-index" 196 | checksum = "e7e9d99fa91428effe99c5c6d4634cdeba32b8cf784fc428a2a687f61a952c49" 197 | dependencies = [ 198 | "autocfg", 199 | "cfg-if 1.0.0", 200 | "lazy_static", 201 | ] 202 | 203 | [[package]] 204 | name = "darling" 205 | version = "0.12.2" 206 | source = "registry+https://github.com/rust-lang/crates.io-index" 207 | checksum = "a06d4a9551359071d1890820e3571252b91229e0712e7c36b08940e603c5a8fc" 208 | dependencies = [ 209 | "darling_core", 210 | "darling_macro", 211 | ] 212 | 213 | [[package]] 214 | name = "darling_core" 215 | version = "0.12.2" 216 | source = "registry+https://github.com/rust-lang/crates.io-index" 217 | checksum = "b443e5fb0ddd56e0c9bfa47dc060c5306ee500cb731f2b91432dd65589a77684" 218 | dependencies = [ 219 | "fnv", 220 | "ident_case", 221 | "proc-macro2", 222 | "quote", 223 | "strsim", 224 | "syn", 225 | ] 226 | 227 | [[package]] 228 | name = "darling_macro" 229 | version = "0.12.2" 230 | source = "registry+https://github.com/rust-lang/crates.io-index" 231 | checksum = "c0220073ce504f12a70efc4e7cdaea9e9b1b324872e7ad96a208056d7a638b81" 232 | dependencies = [ 233 | "darling_core", 234 | "quote", 235 | "syn", 236 | ] 237 | 238 | [[package]] 239 | name = "either" 240 | version = "1.6.1" 241 | source = "registry+https://github.com/rust-lang/crates.io-index" 242 | checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" 243 | 244 | [[package]] 245 | name = "enumset" 246 | version = "1.0.6" 247 | source = "registry+https://github.com/rust-lang/crates.io-index" 248 | checksum = "fbd795df6708a599abf1ee10eacc72efd052b7a5f70fdf0715e4d5151a6db9c3" 249 | dependencies = [ 250 | "enumset_derive", 251 | ] 252 | 253 | [[package]] 254 | name = "enumset_derive" 255 | version = "0.5.4" 256 | source = "registry+https://github.com/rust-lang/crates.io-index" 257 | checksum = "e19c52f9ec503c8a68dc04daf71a04b07e690c32ab1a8b68e33897f255269d47" 258 | dependencies = [ 259 | "darling", 260 | "proc-macro2", 261 | "quote", 262 | "syn", 263 | ] 264 | 265 | [[package]] 266 | name = "example_host" 267 | version = "0.1.0" 268 | dependencies = [ 269 | "wasm_plugin_host", 270 | ] 271 | 272 | [[package]] 273 | name = "fallible-iterator" 274 | version = "0.2.0" 275 | source = "registry+https://github.com/rust-lang/crates.io-index" 276 | checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" 277 | 278 | [[package]] 279 | name = "fnv" 280 | version = "1.0.7" 281 | source = "registry+https://github.com/rust-lang/crates.io-index" 282 | checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" 283 | 284 | [[package]] 285 | name = "getrandom" 286 | version = "0.2.2" 287 | source = "registry+https://github.com/rust-lang/crates.io-index" 288 | checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" 289 | dependencies = [ 290 | "cfg-if 1.0.0", 291 | "libc", 292 | "wasi", 293 | ] 294 | 295 | [[package]] 296 | name = "gimli" 297 | version = "0.22.0" 298 | source = "registry+https://github.com/rust-lang/crates.io-index" 299 | checksum = "aaf91faf136cb47367fa430cd46e37a788775e7fa104f8b4bcb3861dc389b724" 300 | dependencies = [ 301 | "fallible-iterator", 302 | "indexmap", 303 | "stable_deref_trait", 304 | ] 305 | 306 | [[package]] 307 | name = "gimli" 308 | version = "0.23.0" 309 | source = "registry+https://github.com/rust-lang/crates.io-index" 310 | checksum = "f6503fe142514ca4799d4c26297c4248239fe8838d827db6bd6065c6ed29a6ce" 311 | 312 | [[package]] 313 | name = "hashbrown" 314 | version = "0.9.1" 315 | source = "registry+https://github.com/rust-lang/crates.io-index" 316 | checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04" 317 | 318 | [[package]] 319 | name = "hermit-abi" 320 | version = "0.1.18" 321 | source = "registry+https://github.com/rust-lang/crates.io-index" 322 | checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" 323 | dependencies = [ 324 | "libc", 325 | ] 326 | 327 | [[package]] 328 | name = "ident_case" 329 | version = "1.0.1" 330 | source = "registry+https://github.com/rust-lang/crates.io-index" 331 | checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" 332 | 333 | [[package]] 334 | name = "indexmap" 335 | version = "1.6.1" 336 | source = "registry+https://github.com/rust-lang/crates.io-index" 337 | checksum = "4fb1fa934250de4de8aef298d81c729a7d33d8c239daa3a7575e6b92bfc7313b" 338 | dependencies = [ 339 | "autocfg", 340 | "hashbrown", 341 | "serde", 342 | ] 343 | 344 | [[package]] 345 | name = "lazy_static" 346 | version = "1.4.0" 347 | source = "registry+https://github.com/rust-lang/crates.io-index" 348 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" 349 | 350 | [[package]] 351 | name = "leb128" 352 | version = "0.2.4" 353 | source = "registry+https://github.com/rust-lang/crates.io-index" 354 | checksum = "3576a87f2ba00f6f106fdfcd16db1d698d648a26ad8e0573cad8537c3c362d2a" 355 | 356 | [[package]] 357 | name = "libc" 358 | version = "0.2.87" 359 | source = "registry+https://github.com/rust-lang/crates.io-index" 360 | checksum = "265d751d31d6780a3f956bb5b8022feba2d94eeee5a84ba64f4212eedca42213" 361 | 362 | [[package]] 363 | name = "libloading" 364 | version = "0.6.7" 365 | source = "registry+https://github.com/rust-lang/crates.io-index" 366 | checksum = "351a32417a12d5f7e82c368a66781e307834dae04c6ce0cd4456d52989229883" 367 | dependencies = [ 368 | "cfg-if 1.0.0", 369 | "winapi", 370 | ] 371 | 372 | [[package]] 373 | name = "log" 374 | version = "0.4.14" 375 | source = "registry+https://github.com/rust-lang/crates.io-index" 376 | checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" 377 | dependencies = [ 378 | "cfg-if 1.0.0", 379 | ] 380 | 381 | [[package]] 382 | name = "mach" 383 | version = "0.3.2" 384 | source = "registry+https://github.com/rust-lang/crates.io-index" 385 | checksum = "b823e83b2affd8f40a9ee8c29dbc56404c1e34cd2710921f2801e2cf29527afa" 386 | dependencies = [ 387 | "libc", 388 | ] 389 | 390 | [[package]] 391 | name = "memmap2" 392 | version = "0.2.1" 393 | source = "registry+https://github.com/rust-lang/crates.io-index" 394 | checksum = "04e3e85b970d650e2ae6d70592474087051c11c54da7f7b4949725c5735fbcc6" 395 | dependencies = [ 396 | "libc", 397 | ] 398 | 399 | [[package]] 400 | name = "memoffset" 401 | version = "0.6.1" 402 | source = "registry+https://github.com/rust-lang/crates.io-index" 403 | checksum = "157b4208e3059a8f9e78d559edc658e13df41410cb3ae03979c83130067fdd87" 404 | dependencies = [ 405 | "autocfg", 406 | ] 407 | 408 | [[package]] 409 | name = "miniz_oxide" 410 | version = "0.4.4" 411 | source = "registry+https://github.com/rust-lang/crates.io-index" 412 | checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" 413 | dependencies = [ 414 | "adler", 415 | "autocfg", 416 | ] 417 | 418 | [[package]] 419 | name = "more-asserts" 420 | version = "0.2.1" 421 | source = "registry+https://github.com/rust-lang/crates.io-index" 422 | checksum = "0debeb9fcf88823ea64d64e4a815ab1643f33127d995978e099942ce38f25238" 423 | 424 | [[package]] 425 | name = "nanoserde" 426 | version = "0.1.25" 427 | source = "registry+https://github.com/rust-lang/crates.io-index" 428 | checksum = "1ae5894adc81d64b16f75ce8a67c994a6aa4b6e1c99f2920d22e0e575755d7e9" 429 | dependencies = [ 430 | "nanoserde-derive", 431 | ] 432 | 433 | [[package]] 434 | name = "nanoserde-derive" 435 | version = "0.1.16" 436 | source = "registry+https://github.com/rust-lang/crates.io-index" 437 | checksum = "71c29c140bb2a43bdc554ca5a34ecfe678bb6e6c6a3dc8d6856852ba2c74f20f" 438 | 439 | [[package]] 440 | name = "num_cpus" 441 | version = "1.13.0" 442 | source = "registry+https://github.com/rust-lang/crates.io-index" 443 | checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" 444 | dependencies = [ 445 | "hermit-abi", 446 | "libc", 447 | ] 448 | 449 | [[package]] 450 | name = "object" 451 | version = "0.22.0" 452 | source = "registry+https://github.com/rust-lang/crates.io-index" 453 | checksum = "8d3b63360ec3cb337817c2dbd47ab4a0f170d285d8e5a2064600f3def1402397" 454 | dependencies = [ 455 | "crc32fast", 456 | "indexmap", 457 | ] 458 | 459 | [[package]] 460 | name = "object" 461 | version = "0.23.0" 462 | source = "registry+https://github.com/rust-lang/crates.io-index" 463 | checksum = "a9a7ab5d64814df0fe4a4b5ead45ed6c5f181ee3ff04ba344313a6c80446c5d4" 464 | 465 | [[package]] 466 | name = "pin-project-lite" 467 | version = "0.2.5" 468 | source = "registry+https://github.com/rust-lang/crates.io-index" 469 | checksum = "0cf491442e4b033ed1c722cb9f0df5fcfcf4de682466c46469c36bc47dc5548a" 470 | 471 | [[package]] 472 | name = "ppv-lite86" 473 | version = "0.2.10" 474 | source = "registry+https://github.com/rust-lang/crates.io-index" 475 | checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" 476 | 477 | [[package]] 478 | name = "proc-macro-error" 479 | version = "1.0.4" 480 | source = "registry+https://github.com/rust-lang/crates.io-index" 481 | checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" 482 | dependencies = [ 483 | "proc-macro-error-attr", 484 | "proc-macro2", 485 | "quote", 486 | "syn", 487 | "version_check", 488 | ] 489 | 490 | [[package]] 491 | name = "proc-macro-error-attr" 492 | version = "1.0.4" 493 | source = "registry+https://github.com/rust-lang/crates.io-index" 494 | checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" 495 | dependencies = [ 496 | "proc-macro2", 497 | "quote", 498 | "version_check", 499 | ] 500 | 501 | [[package]] 502 | name = "proc-macro2" 503 | version = "1.0.24" 504 | source = "registry+https://github.com/rust-lang/crates.io-index" 505 | checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" 506 | dependencies = [ 507 | "unicode-xid", 508 | ] 509 | 510 | [[package]] 511 | name = "quote" 512 | version = "1.0.9" 513 | source = "registry+https://github.com/rust-lang/crates.io-index" 514 | checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" 515 | dependencies = [ 516 | "proc-macro2", 517 | ] 518 | 519 | [[package]] 520 | name = "rand" 521 | version = "0.8.3" 522 | source = "registry+https://github.com/rust-lang/crates.io-index" 523 | checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" 524 | dependencies = [ 525 | "libc", 526 | "rand_chacha", 527 | "rand_core", 528 | "rand_hc", 529 | ] 530 | 531 | [[package]] 532 | name = "rand_chacha" 533 | version = "0.3.0" 534 | source = "registry+https://github.com/rust-lang/crates.io-index" 535 | checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" 536 | dependencies = [ 537 | "ppv-lite86", 538 | "rand_core", 539 | ] 540 | 541 | [[package]] 542 | name = "rand_core" 543 | version = "0.6.2" 544 | source = "registry+https://github.com/rust-lang/crates.io-index" 545 | checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" 546 | dependencies = [ 547 | "getrandom", 548 | ] 549 | 550 | [[package]] 551 | name = "rand_hc" 552 | version = "0.3.0" 553 | source = "registry+https://github.com/rust-lang/crates.io-index" 554 | checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" 555 | dependencies = [ 556 | "rand_core", 557 | ] 558 | 559 | [[package]] 560 | name = "rayon" 561 | version = "1.5.0" 562 | source = "registry+https://github.com/rust-lang/crates.io-index" 563 | checksum = "8b0d8e0819fadc20c74ea8373106ead0600e3a67ef1fe8da56e39b9ae7275674" 564 | dependencies = [ 565 | "autocfg", 566 | "crossbeam-deque", 567 | "either", 568 | "rayon-core", 569 | ] 570 | 571 | [[package]] 572 | name = "rayon-core" 573 | version = "1.9.0" 574 | source = "registry+https://github.com/rust-lang/crates.io-index" 575 | checksum = "9ab346ac5921dc62ffa9f89b7a773907511cdfa5490c572ae9be1be33e8afa4a" 576 | dependencies = [ 577 | "crossbeam-channel", 578 | "crossbeam-deque", 579 | "crossbeam-utils", 580 | "lazy_static", 581 | "num_cpus", 582 | ] 583 | 584 | [[package]] 585 | name = "redox_syscall" 586 | version = "0.2.5" 587 | source = "registry+https://github.com/rust-lang/crates.io-index" 588 | checksum = "94341e4e44e24f6b591b59e47a8a027df12e008d73fd5672dbea9cc22f4507d9" 589 | dependencies = [ 590 | "bitflags", 591 | ] 592 | 593 | [[package]] 594 | name = "regalloc" 595 | version = "0.0.31" 596 | source = "registry+https://github.com/rust-lang/crates.io-index" 597 | checksum = "571f7f397d61c4755285cd37853fe8e03271c243424a907415909379659381c5" 598 | dependencies = [ 599 | "log", 600 | "rustc-hash", 601 | "smallvec", 602 | ] 603 | 604 | [[package]] 605 | name = "region" 606 | version = "2.2.0" 607 | source = "registry+https://github.com/rust-lang/crates.io-index" 608 | checksum = "877e54ea2adcd70d80e9179344c97f93ef0dffd6b03e1f4529e6e83ab2fa9ae0" 609 | dependencies = [ 610 | "bitflags", 611 | "libc", 612 | "mach", 613 | "winapi", 614 | ] 615 | 616 | [[package]] 617 | name = "remove_dir_all" 618 | version = "0.5.3" 619 | source = "registry+https://github.com/rust-lang/crates.io-index" 620 | checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" 621 | dependencies = [ 622 | "winapi", 623 | ] 624 | 625 | [[package]] 626 | name = "rustc-demangle" 627 | version = "0.1.18" 628 | source = "registry+https://github.com/rust-lang/crates.io-index" 629 | checksum = "6e3bad0ee36814ca07d7968269dd4b7ec89ec2da10c4bb613928d3077083c232" 630 | 631 | [[package]] 632 | name = "rustc-hash" 633 | version = "1.1.0" 634 | source = "registry+https://github.com/rust-lang/crates.io-index" 635 | checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" 636 | 637 | [[package]] 638 | name = "scopeguard" 639 | version = "1.1.0" 640 | source = "registry+https://github.com/rust-lang/crates.io-index" 641 | checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" 642 | 643 | [[package]] 644 | name = "serde" 645 | version = "1.0.123" 646 | source = "registry+https://github.com/rust-lang/crates.io-index" 647 | checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae" 648 | dependencies = [ 649 | "serde_derive", 650 | ] 651 | 652 | [[package]] 653 | name = "serde_bytes" 654 | version = "0.11.5" 655 | source = "registry+https://github.com/rust-lang/crates.io-index" 656 | checksum = "16ae07dd2f88a366f15bd0632ba725227018c69a1c8550a927324f8eb8368bb9" 657 | dependencies = [ 658 | "serde", 659 | ] 660 | 661 | [[package]] 662 | name = "serde_derive" 663 | version = "1.0.123" 664 | source = "registry+https://github.com/rust-lang/crates.io-index" 665 | checksum = "9391c295d64fc0abb2c556bad848f33cb8296276b1ad2677d1ae1ace4f258f31" 666 | dependencies = [ 667 | "proc-macro2", 668 | "quote", 669 | "syn", 670 | ] 671 | 672 | [[package]] 673 | name = "smallvec" 674 | version = "1.6.1" 675 | source = "registry+https://github.com/rust-lang/crates.io-index" 676 | checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" 677 | 678 | [[package]] 679 | name = "stable_deref_trait" 680 | version = "1.2.0" 681 | source = "registry+https://github.com/rust-lang/crates.io-index" 682 | checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" 683 | 684 | [[package]] 685 | name = "strsim" 686 | version = "0.10.0" 687 | source = "registry+https://github.com/rust-lang/crates.io-index" 688 | checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" 689 | 690 | [[package]] 691 | name = "syn" 692 | version = "1.0.60" 693 | source = "registry+https://github.com/rust-lang/crates.io-index" 694 | checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081" 695 | dependencies = [ 696 | "proc-macro2", 697 | "quote", 698 | "unicode-xid", 699 | ] 700 | 701 | [[package]] 702 | name = "target-lexicon" 703 | version = "0.11.2" 704 | source = "registry+https://github.com/rust-lang/crates.io-index" 705 | checksum = "422045212ea98508ae3d28025bc5aaa2bd4a9cdaecd442a08da2ee620ee9ea95" 706 | 707 | [[package]] 708 | name = "tempfile" 709 | version = "3.2.0" 710 | source = "registry+https://github.com/rust-lang/crates.io-index" 711 | checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22" 712 | dependencies = [ 713 | "cfg-if 1.0.0", 714 | "libc", 715 | "rand", 716 | "redox_syscall", 717 | "remove_dir_all", 718 | "winapi", 719 | ] 720 | 721 | [[package]] 722 | name = "thiserror" 723 | version = "1.0.24" 724 | source = "registry+https://github.com/rust-lang/crates.io-index" 725 | checksum = "e0f4a65597094d4483ddaed134f409b2cb7c1beccf25201a9f73c719254fa98e" 726 | dependencies = [ 727 | "thiserror-impl", 728 | ] 729 | 730 | [[package]] 731 | name = "thiserror-impl" 732 | version = "1.0.24" 733 | source = "registry+https://github.com/rust-lang/crates.io-index" 734 | checksum = "7765189610d8241a44529806d6fd1f2e0a08734313a35d5b3a556f92b381f3c0" 735 | dependencies = [ 736 | "proc-macro2", 737 | "quote", 738 | "syn", 739 | ] 740 | 741 | [[package]] 742 | name = "tracing" 743 | version = "0.1.25" 744 | source = "registry+https://github.com/rust-lang/crates.io-index" 745 | checksum = "01ebdc2bb4498ab1ab5f5b73c5803825e60199229ccba0698170e3be0e7f959f" 746 | dependencies = [ 747 | "cfg-if 1.0.0", 748 | "pin-project-lite", 749 | "tracing-attributes", 750 | "tracing-core", 751 | ] 752 | 753 | [[package]] 754 | name = "tracing-attributes" 755 | version = "0.1.13" 756 | source = "registry+https://github.com/rust-lang/crates.io-index" 757 | checksum = "a8a9bd1db7706f2373a190b0d067146caa39350c486f3d455b0e33b431f94c07" 758 | dependencies = [ 759 | "proc-macro2", 760 | "quote", 761 | "syn", 762 | ] 763 | 764 | [[package]] 765 | name = "tracing-core" 766 | version = "0.1.17" 767 | source = "registry+https://github.com/rust-lang/crates.io-index" 768 | checksum = "f50de3927f93d202783f4513cda820ab47ef17f624b03c096e86ef00c67e6b5f" 769 | dependencies = [ 770 | "lazy_static", 771 | ] 772 | 773 | [[package]] 774 | name = "unicode-xid" 775 | version = "0.2.1" 776 | source = "registry+https://github.com/rust-lang/crates.io-index" 777 | checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" 778 | 779 | [[package]] 780 | name = "version_check" 781 | version = "0.9.2" 782 | source = "registry+https://github.com/rust-lang/crates.io-index" 783 | checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" 784 | 785 | [[package]] 786 | name = "wasi" 787 | version = "0.10.2+wasi-snapshot-preview1" 788 | source = "registry+https://github.com/rust-lang/crates.io-index" 789 | checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" 790 | 791 | [[package]] 792 | name = "wasm_plugin_host" 793 | version = "0.1.5" 794 | dependencies = [ 795 | "bitfield", 796 | "nanoserde", 797 | "wasmer", 798 | ] 799 | 800 | [[package]] 801 | name = "wasmer" 802 | version = "1.0.2" 803 | source = "registry+https://github.com/rust-lang/crates.io-index" 804 | checksum = "a70cfae554988d904d64ca17ab0e7cd652ee5c8a0807094819c1ea93eb9d6866" 805 | dependencies = [ 806 | "cfg-if 0.1.10", 807 | "indexmap", 808 | "more-asserts", 809 | "target-lexicon", 810 | "thiserror", 811 | "wasmer-compiler", 812 | "wasmer-compiler-cranelift", 813 | "wasmer-derive", 814 | "wasmer-engine", 815 | "wasmer-engine-jit", 816 | "wasmer-engine-native", 817 | "wasmer-types", 818 | "wasmer-vm", 819 | "wat", 820 | "winapi", 821 | ] 822 | 823 | [[package]] 824 | name = "wasmer-compiler" 825 | version = "1.0.2" 826 | source = "registry+https://github.com/rust-lang/crates.io-index" 827 | checksum = "6b7732a9cab472bd921d5a0c422f45b3d03f62fa2c40a89e0770cef6d47e383e" 828 | dependencies = [ 829 | "enumset", 830 | "serde", 831 | "serde_bytes", 832 | "smallvec", 833 | "target-lexicon", 834 | "thiserror", 835 | "wasmer-types", 836 | "wasmer-vm", 837 | "wasmparser", 838 | ] 839 | 840 | [[package]] 841 | name = "wasmer-compiler-cranelift" 842 | version = "1.0.2" 843 | source = "registry+https://github.com/rust-lang/crates.io-index" 844 | checksum = "48cb9395f094e1d81534f4c5e330ed4cdb424e8df870d29ad585620284f5fddb" 845 | dependencies = [ 846 | "cranelift-codegen", 847 | "cranelift-frontend", 848 | "gimli 0.22.0", 849 | "more-asserts", 850 | "rayon", 851 | "serde", 852 | "smallvec", 853 | "tracing", 854 | "wasmer-compiler", 855 | "wasmer-types", 856 | "wasmer-vm", 857 | ] 858 | 859 | [[package]] 860 | name = "wasmer-derive" 861 | version = "1.0.2" 862 | source = "registry+https://github.com/rust-lang/crates.io-index" 863 | checksum = "d8b86dcd2c3efdb8390728a2b56f762db07789aaa5aa872a9dc776ba3a7912ed" 864 | dependencies = [ 865 | "proc-macro-error", 866 | "proc-macro2", 867 | "quote", 868 | "syn", 869 | ] 870 | 871 | [[package]] 872 | name = "wasmer-engine" 873 | version = "1.0.2" 874 | source = "registry+https://github.com/rust-lang/crates.io-index" 875 | checksum = "efe4667d6bd888f26ae8062a63a9379fa697415b4b4e380f33832e8418fd71b5" 876 | dependencies = [ 877 | "backtrace", 878 | "bincode", 879 | "lazy_static", 880 | "memmap2", 881 | "more-asserts", 882 | "rustc-demangle", 883 | "serde", 884 | "serde_bytes", 885 | "target-lexicon", 886 | "thiserror", 887 | "wasmer-compiler", 888 | "wasmer-types", 889 | "wasmer-vm", 890 | ] 891 | 892 | [[package]] 893 | name = "wasmer-engine-jit" 894 | version = "1.0.2" 895 | source = "registry+https://github.com/rust-lang/crates.io-index" 896 | checksum = "26770be802888011b4a3072f2a282fc2faa68aa48c71b3db6252a3937a85f3da" 897 | dependencies = [ 898 | "bincode", 899 | "cfg-if 0.1.10", 900 | "region", 901 | "serde", 902 | "serde_bytes", 903 | "wasmer-compiler", 904 | "wasmer-engine", 905 | "wasmer-types", 906 | "wasmer-vm", 907 | "winapi", 908 | ] 909 | 910 | [[package]] 911 | name = "wasmer-engine-native" 912 | version = "1.0.2" 913 | source = "registry+https://github.com/rust-lang/crates.io-index" 914 | checksum = "2bb4083a6c69f2cd4b000b82a80717f37c6cc2e536aee3a8ffe9af3edc276a8b" 915 | dependencies = [ 916 | "bincode", 917 | "cfg-if 0.1.10", 918 | "leb128", 919 | "libloading", 920 | "serde", 921 | "tempfile", 922 | "tracing", 923 | "wasmer-compiler", 924 | "wasmer-engine", 925 | "wasmer-object", 926 | "wasmer-types", 927 | "wasmer-vm", 928 | "which", 929 | ] 930 | 931 | [[package]] 932 | name = "wasmer-object" 933 | version = "1.0.2" 934 | source = "registry+https://github.com/rust-lang/crates.io-index" 935 | checksum = "abf8e0c12b82ff81ebecd30d7e118be5fec871d6de885a90eeb105df0a769a7b" 936 | dependencies = [ 937 | "object 0.22.0", 938 | "thiserror", 939 | "wasmer-compiler", 940 | "wasmer-types", 941 | ] 942 | 943 | [[package]] 944 | name = "wasmer-types" 945 | version = "1.0.2" 946 | source = "registry+https://github.com/rust-lang/crates.io-index" 947 | checksum = "c7f4ac28c2951cd792c18332f03da523ed06b170f5cf6bb5b1bdd7e36c2a8218" 948 | dependencies = [ 949 | "cranelift-entity", 950 | "serde", 951 | "thiserror", 952 | ] 953 | 954 | [[package]] 955 | name = "wasmer-vm" 956 | version = "1.0.2" 957 | source = "registry+https://github.com/rust-lang/crates.io-index" 958 | checksum = "a7635ba0b6d2fd325f588d69a950ad9fa04dddbf6ad08b6b2a183146319bf6ae" 959 | dependencies = [ 960 | "backtrace", 961 | "cc", 962 | "cfg-if 0.1.10", 963 | "indexmap", 964 | "libc", 965 | "memoffset", 966 | "more-asserts", 967 | "region", 968 | "serde", 969 | "thiserror", 970 | "wasmer-types", 971 | "winapi", 972 | ] 973 | 974 | [[package]] 975 | name = "wasmparser" 976 | version = "0.65.0" 977 | source = "registry+https://github.com/rust-lang/crates.io-index" 978 | checksum = "87cc2fe6350834b4e528ba0901e7aa405d78b89dc1fa3145359eb4de0e323fcf" 979 | 980 | [[package]] 981 | name = "wast" 982 | version = "35.0.0" 983 | source = "registry+https://github.com/rust-lang/crates.io-index" 984 | checksum = "db5ae96da18bb5926341516fd409b5a8ce4e4714da7f0a1063d3b20ac9f9a1e1" 985 | dependencies = [ 986 | "leb128", 987 | ] 988 | 989 | [[package]] 990 | name = "wat" 991 | version = "1.0.36" 992 | source = "registry+https://github.com/rust-lang/crates.io-index" 993 | checksum = "0b0fa059022c5dabe129f02b429d67086400deb8277f89c975555dacc1dadbcc" 994 | dependencies = [ 995 | "wast", 996 | ] 997 | 998 | [[package]] 999 | name = "which" 1000 | version = "4.0.2" 1001 | source = "registry+https://github.com/rust-lang/crates.io-index" 1002 | checksum = "87c14ef7e1b8b8ecfc75d5eca37949410046e66f15d185c01d70824f1f8111ef" 1003 | dependencies = [ 1004 | "libc", 1005 | "thiserror", 1006 | ] 1007 | 1008 | [[package]] 1009 | name = "winapi" 1010 | version = "0.3.9" 1011 | source = "registry+https://github.com/rust-lang/crates.io-index" 1012 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 1013 | dependencies = [ 1014 | "winapi-i686-pc-windows-gnu", 1015 | "winapi-x86_64-pc-windows-gnu", 1016 | ] 1017 | 1018 | [[package]] 1019 | name = "winapi-i686-pc-windows-gnu" 1020 | version = "0.4.0" 1021 | source = "registry+https://github.com/rust-lang/crates.io-index" 1022 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 1023 | 1024 | [[package]] 1025 | name = "winapi-x86_64-pc-windows-gnu" 1026 | version = "0.4.0" 1027 | source = "registry+https://github.com/rust-lang/crates.io-index" 1028 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 1029 | -------------------------------------------------------------------------------- /example/example_assemblyscript_host/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "example_host" 3 | version = "0.1.0" 4 | authors = ["Alec Deason "] 5 | edition = "2018" 6 | publish = false 7 | 8 | [dependencies] 9 | wasm_plugin_host = { path = "../../host", default-features = false, features = ["serialize_nanoserde_json"] } 10 | -------------------------------------------------------------------------------- /example/example_assemblyscript_host/README.md: -------------------------------------------------------------------------------- 1 | This example demonstrates hosting a non-rust plugin using json serialization rather than bincode. 2 | 3 | To build the assemblyscript plugin: 4 | ``` 5 | cd assemblyscript_plugin 6 | npm run asbuild 7 | ``` 8 | 9 | Then run the host with: 10 | ``` 11 | cargo run 12 | ``` 13 | 14 | There is currently no non-rust equivalent to the wasm_plugin_guest crate to make writing plugins in assemblyscript, or any other language, ergonomic. Those may be added at some point. This example demonstrates how to manually implement the wasm_plugin calling convention and serialization in assemblyscript and the it should be reasonably straightforward to do the same thing in any other WASM targeting language. 15 | -------------------------------------------------------------------------------- /example/example_assemblyscript_host/assemblyscript_plugin/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | package-lock.json 3 | *.wat 4 | -------------------------------------------------------------------------------- /example/example_assemblyscript_host/assemblyscript_plugin/asconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "targets": { 3 | "debug": { 4 | "binaryFile": "build/untouched.wasm", 5 | "textFile": "build/untouched.wat", 6 | "sourceMap": true, 7 | "debug": true 8 | }, 9 | "release": { 10 | "binaryFile": "build/optimized.wasm", 11 | "textFile": "build/optimized.wat", 12 | "sourceMap": true, 13 | "optimizeLevel": 3, 14 | "shrinkLevel": 1, 15 | "converge": false, 16 | "noAssert": false 17 | } 18 | }, 19 | "options": {} 20 | } -------------------------------------------------------------------------------- /example/example_assemblyscript_host/assemblyscript_plugin/assembly/env.ts: -------------------------------------------------------------------------------- 1 | export declare function wasm_plugin_imported__please_capitalize_this(ptr: u32, len: u32): u64 2 | -------------------------------------------------------------------------------- /example/example_assemblyscript_host/assemblyscript_plugin/assembly/index.ts: -------------------------------------------------------------------------------- 1 | import { JSON, JSONEncoder } from "assemblyscript-json"; 2 | 3 | import {wasm_plugin_imported__please_capitalize_this} from "./env" 4 | 5 | export function wasm_plugin_exported__hello(): u64 { 6 | let data = String.UTF8.encode("\"Hello, Host!\""); 7 | let ptr = changetype(data); 8 | __pin(ptr); 9 | let len = data.byteLength as u64; 10 | 11 | return len << 32 | ptr as u64; 12 | } 13 | 14 | function please_capitalize_this(input: String): String { 15 | let data = String.UTF8.encode(input); 16 | let ptr = changetype(data); 17 | let len = data.byteLength; 18 | 19 | let fat_ptr = wasm_plugin_imported__please_capitalize_this(ptr as u32, len as u32); 20 | let len2 = (fat_ptr >> 32) & 0xFFFFFF; 21 | let ptr2 = fat_ptr & 0xFFFFFF; 22 | let result = String.UTF8.decodeUnsafe(ptr2 as usize, len2 as usize, false); 23 | return result 24 | } 25 | 26 | export function wasm_plugin_exported__echo(ptr: u32, len: u32): u64 { 27 | let input = String.UTF8.decodeUnsafe(ptr as usize, len as usize, false); 28 | let output = please_capitalize_this(input); 29 | 30 | let data = String.UTF8.encode(output); 31 | let ptr = changetype(data); 32 | __pin(ptr); 33 | let len = data.byteLength as u64; 34 | 35 | return len << 32 | ptr as u64; 36 | } 37 | 38 | export function wasm_plugin_exported__favorite_numbers(): u64 { 39 | let encoder = new JSONEncoder(); 40 | encoder.pushArray(null); 41 | encoder.setInteger(null, 1); 42 | encoder.setInteger(null, 2); 43 | encoder.setInteger(null, 43); 44 | encoder.popArray(); 45 | let response: string = encoder.toString(); 46 | let encoded_response = String.UTF8.encode(response); 47 | let ptr = changetype(encoded_response); 48 | __pin(ptr); 49 | let len = encoded_response.byteLength as u64; 50 | 51 | return len << 32 | ptr as u64; 52 | } 53 | 54 | export function allocate_message_buffer(len: u32): u32 { 55 | let buffer = new ArrayBuffer(len); 56 | let ptr = changetype(buffer); 57 | __pin(ptr); 58 | return ptr as u32; 59 | } 60 | 61 | export function free_message_buffer(ptr: u32, _len: u32): void { 62 | __unpin(ptr as usize); 63 | } 64 | -------------------------------------------------------------------------------- /example/example_assemblyscript_host/assemblyscript_plugin/assembly/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "assemblyscript/std/assembly.json", 3 | "include": [ 4 | "./**/*.ts" 5 | ] 6 | } -------------------------------------------------------------------------------- /example/example_assemblyscript_host/assemblyscript_plugin/build/.gitignore: -------------------------------------------------------------------------------- 1 | *.wasm 2 | *.wasm.map 3 | *.asm.js 4 | -------------------------------------------------------------------------------- /example/example_assemblyscript_host/assemblyscript_plugin/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "assemblyscript_plugin", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node tests", 8 | "asbuild:untouched": "asc assembly/index.ts --target debug", 9 | "asbuild:optimized": "asc assembly/index.ts --target release", 10 | "asbuild": "npm run asbuild:untouched && npm run asbuild:optimized" 11 | }, 12 | "author": "", 13 | "license": "ISC", 14 | "dependencies": { 15 | "@assemblyscript/loader": "^0.18.23", 16 | "assemblyscript-json": "^1.0.0", 17 | "source-map-support": "^0.5.19" 18 | }, 19 | "devDependencies": { 20 | "assemblyscript": "^0.18.30" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /example/example_assemblyscript_host/src/main.rs: -------------------------------------------------------------------------------- 1 | use wasm_plugin_host::WasmPluginBuilder; 2 | 3 | fn main() -> Result<(), Box> { 4 | let mut plugin = WasmPluginBuilder::from_file( 5 | "./assemblyscript_plugin/build/optimized.wasm", 6 | )? 7 | .import_function("please_capitalize_this", |mut s: String| { 8 | s.make_ascii_uppercase(); 9 | s 10 | }) 11 | .finish()?; 12 | let response: String = plugin.call_function("hello")?; 13 | println!("The guest says: '{}'", response); 14 | 15 | let message = "Hello, Guest!".to_string(); 16 | let response: String = plugin.call_function_with_argument("echo", &message)?; 17 | println!( 18 | "I said: '{}'. The guest said, '{}' back. Weird", 19 | message, response 20 | ); 21 | 22 | // Any type that can be serialized works 23 | let response: Vec = plugin.call_function("favorite_numbers")?; 24 | println!( 25 | "The guest's favorite integers (less that 2**32) are: '{:?}'", 26 | response 27 | ); 28 | 29 | Ok(()) 30 | } 31 | -------------------------------------------------------------------------------- /example/example_guest/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "bincode" 5 | version = "1.3.2" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | checksum = "d175dfa69e619905c4c3cdb7c3c203fa3bdd5d51184e3afdb2742c0280493772" 8 | dependencies = [ 9 | "byteorder", 10 | "serde", 11 | ] 12 | 13 | [[package]] 14 | name = "bitfield" 15 | version = "0.13.2" 16 | source = "registry+https://github.com/rust-lang/crates.io-index" 17 | checksum = "46afbd2983a5d5a7bd740ccb198caf5b82f45c40c09c0eed36052d91cb92e719" 18 | 19 | [[package]] 20 | name = "byteorder" 21 | version = "1.3.4" 22 | source = "registry+https://github.com/rust-lang/crates.io-index" 23 | checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" 24 | 25 | [[package]] 26 | name = "cfg-if" 27 | version = "1.0.0" 28 | source = "registry+https://github.com/rust-lang/crates.io-index" 29 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 30 | 31 | [[package]] 32 | name = "example_guest" 33 | version = "0.1.0" 34 | dependencies = [ 35 | "wasm_plugin_guest", 36 | ] 37 | 38 | [[package]] 39 | name = "getrandom" 40 | version = "0.2.2" 41 | source = "registry+https://github.com/rust-lang/crates.io-index" 42 | checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" 43 | dependencies = [ 44 | "cfg-if", 45 | "libc", 46 | "wasi", 47 | ] 48 | 49 | [[package]] 50 | name = "libc" 51 | version = "0.2.87" 52 | source = "registry+https://github.com/rust-lang/crates.io-index" 53 | checksum = "265d751d31d6780a3f956bb5b8022feba2d94eeee5a84ba64f4212eedca42213" 54 | 55 | [[package]] 56 | name = "proc-macro2" 57 | version = "1.0.24" 58 | source = "registry+https://github.com/rust-lang/crates.io-index" 59 | checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" 60 | dependencies = [ 61 | "unicode-xid", 62 | ] 63 | 64 | [[package]] 65 | name = "quote" 66 | version = "1.0.9" 67 | source = "registry+https://github.com/rust-lang/crates.io-index" 68 | checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" 69 | dependencies = [ 70 | "proc-macro2", 71 | ] 72 | 73 | [[package]] 74 | name = "serde" 75 | version = "1.0.123" 76 | source = "registry+https://github.com/rust-lang/crates.io-index" 77 | checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae" 78 | 79 | [[package]] 80 | name = "syn" 81 | version = "1.0.60" 82 | source = "registry+https://github.com/rust-lang/crates.io-index" 83 | checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081" 84 | dependencies = [ 85 | "proc-macro2", 86 | "quote", 87 | "unicode-xid", 88 | ] 89 | 90 | [[package]] 91 | name = "unicode-xid" 92 | version = "0.2.1" 93 | source = "registry+https://github.com/rust-lang/crates.io-index" 94 | checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" 95 | 96 | [[package]] 97 | name = "wasi" 98 | version = "0.10.2+wasi-snapshot-preview1" 99 | source = "registry+https://github.com/rust-lang/crates.io-index" 100 | checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" 101 | 102 | [[package]] 103 | name = "wasm_plugin_guest" 104 | version = "0.1.4" 105 | dependencies = [ 106 | "bincode", 107 | "bitfield", 108 | "getrandom", 109 | "serde", 110 | "wasm_plugin_guest_derive", 111 | ] 112 | 113 | [[package]] 114 | name = "wasm_plugin_guest_derive" 115 | version = "0.1.4" 116 | source = "registry+https://github.com/rust-lang/crates.io-index" 117 | checksum = "798077c1e20331f66c2d64d4ca503cc18c485d776bbd370ad43ab4641a45ffd0" 118 | dependencies = [ 119 | "proc-macro2", 120 | "quote", 121 | "syn", 122 | ] 123 | -------------------------------------------------------------------------------- /example/example_guest/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "example_guest" 3 | version = "0.1.0" 4 | authors = ["Alec Deason "] 5 | edition = "2018" 6 | publish = false 7 | 8 | [lib] 9 | crate-type = ["cdylib"] 10 | 11 | [dependencies] 12 | wasm_plugin_guest = { path = "../../guest" } 13 | -------------------------------------------------------------------------------- /example/example_guest/README.md: -------------------------------------------------------------------------------- 1 | This example shows a simple plugin that exports two functions to be used by ../example_host. First build this with `cargo build --target wasm32-unknown-unknown` and then go run that example. 2 | -------------------------------------------------------------------------------- /example/example_guest/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[wasm_plugin_guest::export_function] 2 | fn hello() -> String { 3 | "Hello, Host!".to_string() 4 | } 5 | 6 | #[wasm_plugin_guest::export_function] 7 | fn echo(message: String) -> String { 8 | let message = please_capitalize_this(message); 9 | please_capitalize_this(message.clone()); 10 | format!("{}", message) 11 | } 12 | 13 | wasm_plugin_guest::import_functions!{ 14 | fn the_hosts_favorite_numbers() -> Vec; 15 | fn please_capitalize_this(s: String) -> String; 16 | } 17 | 18 | #[wasm_plugin_guest::export_function] 19 | fn favorite_numbers() -> Vec { 20 | let numbers = the_hosts_favorite_numbers(); 21 | numbers.into_iter().map(|n| n+1).collect() 22 | } 23 | -------------------------------------------------------------------------------- /example/example_host/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "addr2line" 5 | version = "0.14.1" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | checksum = "a55f82cfe485775d02112886f4169bde0c5894d75e79ead7eafe7e40a25e45f7" 8 | dependencies = [ 9 | "gimli 0.23.0", 10 | ] 11 | 12 | [[package]] 13 | name = "adler" 14 | version = "1.0.2" 15 | source = "registry+https://github.com/rust-lang/crates.io-index" 16 | checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" 17 | 18 | [[package]] 19 | name = "autocfg" 20 | version = "1.0.1" 21 | source = "registry+https://github.com/rust-lang/crates.io-index" 22 | checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" 23 | 24 | [[package]] 25 | name = "backtrace" 26 | version = "0.3.56" 27 | source = "registry+https://github.com/rust-lang/crates.io-index" 28 | checksum = "9d117600f438b1707d4e4ae15d3595657288f8235a0eb593e80ecc98ab34e1bc" 29 | dependencies = [ 30 | "addr2line", 31 | "cfg-if 1.0.0", 32 | "libc", 33 | "miniz_oxide", 34 | "object 0.23.0", 35 | "rustc-demangle", 36 | ] 37 | 38 | [[package]] 39 | name = "bincode" 40 | version = "1.3.2" 41 | source = "registry+https://github.com/rust-lang/crates.io-index" 42 | checksum = "d175dfa69e619905c4c3cdb7c3c203fa3bdd5d51184e3afdb2742c0280493772" 43 | dependencies = [ 44 | "byteorder", 45 | "serde", 46 | ] 47 | 48 | [[package]] 49 | name = "bitfield" 50 | version = "0.13.2" 51 | source = "registry+https://github.com/rust-lang/crates.io-index" 52 | checksum = "46afbd2983a5d5a7bd740ccb198caf5b82f45c40c09c0eed36052d91cb92e719" 53 | 54 | [[package]] 55 | name = "bitflags" 56 | version = "1.2.1" 57 | source = "registry+https://github.com/rust-lang/crates.io-index" 58 | checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" 59 | 60 | [[package]] 61 | name = "byteorder" 62 | version = "1.3.4" 63 | source = "registry+https://github.com/rust-lang/crates.io-index" 64 | checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" 65 | 66 | [[package]] 67 | name = "cc" 68 | version = "1.0.67" 69 | source = "registry+https://github.com/rust-lang/crates.io-index" 70 | checksum = "e3c69b077ad434294d3ce9f1f6143a2a4b89a8a2d54ef813d85003a4fd1137fd" 71 | 72 | [[package]] 73 | name = "cfg-if" 74 | version = "0.1.10" 75 | source = "registry+https://github.com/rust-lang/crates.io-index" 76 | checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" 77 | 78 | [[package]] 79 | name = "cfg-if" 80 | version = "1.0.0" 81 | source = "registry+https://github.com/rust-lang/crates.io-index" 82 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 83 | 84 | [[package]] 85 | name = "cranelift-bforest" 86 | version = "0.68.0" 87 | source = "registry+https://github.com/rust-lang/crates.io-index" 88 | checksum = "9221545c0507dc08a62b2d8b5ffe8e17ac580b0a74d1813b496b8d70b070fbd0" 89 | dependencies = [ 90 | "cranelift-entity", 91 | ] 92 | 93 | [[package]] 94 | name = "cranelift-codegen" 95 | version = "0.68.0" 96 | source = "registry+https://github.com/rust-lang/crates.io-index" 97 | checksum = "7e9936ea608b6cd176f107037f6adbb4deac933466fc7231154f96598b2d3ab1" 98 | dependencies = [ 99 | "byteorder", 100 | "cranelift-bforest", 101 | "cranelift-codegen-meta", 102 | "cranelift-codegen-shared", 103 | "cranelift-entity", 104 | "gimli 0.22.0", 105 | "log", 106 | "regalloc", 107 | "smallvec", 108 | "target-lexicon", 109 | "thiserror", 110 | ] 111 | 112 | [[package]] 113 | name = "cranelift-codegen-meta" 114 | version = "0.68.0" 115 | source = "registry+https://github.com/rust-lang/crates.io-index" 116 | checksum = "4ef2b2768568306540f4c8db3acce9105534d34c4a1e440529c1e702d7f8c8d7" 117 | dependencies = [ 118 | "cranelift-codegen-shared", 119 | "cranelift-entity", 120 | ] 121 | 122 | [[package]] 123 | name = "cranelift-codegen-shared" 124 | version = "0.68.0" 125 | source = "registry+https://github.com/rust-lang/crates.io-index" 126 | checksum = "6759012d6d19c4caec95793f052613e9d4113e925e7f14154defbac0f1d4c938" 127 | 128 | [[package]] 129 | name = "cranelift-entity" 130 | version = "0.68.0" 131 | source = "registry+https://github.com/rust-lang/crates.io-index" 132 | checksum = "86badbce14e15f52a45b666b38abe47b204969dd7f8fb7488cb55dd46b361fa6" 133 | dependencies = [ 134 | "serde", 135 | ] 136 | 137 | [[package]] 138 | name = "cranelift-frontend" 139 | version = "0.68.0" 140 | source = "registry+https://github.com/rust-lang/crates.io-index" 141 | checksum = "b608bb7656c554d0a4cf8f50c7a10b857e80306f6ff829ad6d468a7e2323c8d8" 142 | dependencies = [ 143 | "cranelift-codegen", 144 | "log", 145 | "smallvec", 146 | "target-lexicon", 147 | ] 148 | 149 | [[package]] 150 | name = "crc32fast" 151 | version = "1.2.1" 152 | source = "registry+https://github.com/rust-lang/crates.io-index" 153 | checksum = "81156fece84ab6a9f2afdb109ce3ae577e42b1228441eded99bd77f627953b1a" 154 | dependencies = [ 155 | "cfg-if 1.0.0", 156 | ] 157 | 158 | [[package]] 159 | name = "crossbeam-channel" 160 | version = "0.5.0" 161 | source = "registry+https://github.com/rust-lang/crates.io-index" 162 | checksum = "dca26ee1f8d361640700bde38b2c37d8c22b3ce2d360e1fc1c74ea4b0aa7d775" 163 | dependencies = [ 164 | "cfg-if 1.0.0", 165 | "crossbeam-utils", 166 | ] 167 | 168 | [[package]] 169 | name = "crossbeam-deque" 170 | version = "0.8.0" 171 | source = "registry+https://github.com/rust-lang/crates.io-index" 172 | checksum = "94af6efb46fef72616855b036a624cf27ba656ffc9be1b9a3c931cfc7749a9a9" 173 | dependencies = [ 174 | "cfg-if 1.0.0", 175 | "crossbeam-epoch", 176 | "crossbeam-utils", 177 | ] 178 | 179 | [[package]] 180 | name = "crossbeam-epoch" 181 | version = "0.9.3" 182 | source = "registry+https://github.com/rust-lang/crates.io-index" 183 | checksum = "2584f639eb95fea8c798496315b297cf81b9b58b6d30ab066a75455333cf4b12" 184 | dependencies = [ 185 | "cfg-if 1.0.0", 186 | "crossbeam-utils", 187 | "lazy_static", 188 | "memoffset", 189 | "scopeguard", 190 | ] 191 | 192 | [[package]] 193 | name = "crossbeam-utils" 194 | version = "0.8.3" 195 | source = "registry+https://github.com/rust-lang/crates.io-index" 196 | checksum = "e7e9d99fa91428effe99c5c6d4634cdeba32b8cf784fc428a2a687f61a952c49" 197 | dependencies = [ 198 | "autocfg", 199 | "cfg-if 1.0.0", 200 | "lazy_static", 201 | ] 202 | 203 | [[package]] 204 | name = "darling" 205 | version = "0.12.2" 206 | source = "registry+https://github.com/rust-lang/crates.io-index" 207 | checksum = "a06d4a9551359071d1890820e3571252b91229e0712e7c36b08940e603c5a8fc" 208 | dependencies = [ 209 | "darling_core", 210 | "darling_macro", 211 | ] 212 | 213 | [[package]] 214 | name = "darling_core" 215 | version = "0.12.2" 216 | source = "registry+https://github.com/rust-lang/crates.io-index" 217 | checksum = "b443e5fb0ddd56e0c9bfa47dc060c5306ee500cb731f2b91432dd65589a77684" 218 | dependencies = [ 219 | "fnv", 220 | "ident_case", 221 | "proc-macro2", 222 | "quote", 223 | "strsim", 224 | "syn", 225 | ] 226 | 227 | [[package]] 228 | name = "darling_macro" 229 | version = "0.12.2" 230 | source = "registry+https://github.com/rust-lang/crates.io-index" 231 | checksum = "c0220073ce504f12a70efc4e7cdaea9e9b1b324872e7ad96a208056d7a638b81" 232 | dependencies = [ 233 | "darling_core", 234 | "quote", 235 | "syn", 236 | ] 237 | 238 | [[package]] 239 | name = "either" 240 | version = "1.6.1" 241 | source = "registry+https://github.com/rust-lang/crates.io-index" 242 | checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" 243 | 244 | [[package]] 245 | name = "enumset" 246 | version = "1.0.6" 247 | source = "registry+https://github.com/rust-lang/crates.io-index" 248 | checksum = "fbd795df6708a599abf1ee10eacc72efd052b7a5f70fdf0715e4d5151a6db9c3" 249 | dependencies = [ 250 | "enumset_derive", 251 | ] 252 | 253 | [[package]] 254 | name = "enumset_derive" 255 | version = "0.5.4" 256 | source = "registry+https://github.com/rust-lang/crates.io-index" 257 | checksum = "e19c52f9ec503c8a68dc04daf71a04b07e690c32ab1a8b68e33897f255269d47" 258 | dependencies = [ 259 | "darling", 260 | "proc-macro2", 261 | "quote", 262 | "syn", 263 | ] 264 | 265 | [[package]] 266 | name = "example_host" 267 | version = "0.1.0" 268 | dependencies = [ 269 | "wasm_plugin_host", 270 | ] 271 | 272 | [[package]] 273 | name = "fallible-iterator" 274 | version = "0.2.0" 275 | source = "registry+https://github.com/rust-lang/crates.io-index" 276 | checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" 277 | 278 | [[package]] 279 | name = "fnv" 280 | version = "1.0.7" 281 | source = "registry+https://github.com/rust-lang/crates.io-index" 282 | checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" 283 | 284 | [[package]] 285 | name = "getrandom" 286 | version = "0.2.2" 287 | source = "registry+https://github.com/rust-lang/crates.io-index" 288 | checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" 289 | dependencies = [ 290 | "cfg-if 1.0.0", 291 | "libc", 292 | "wasi", 293 | ] 294 | 295 | [[package]] 296 | name = "gimli" 297 | version = "0.22.0" 298 | source = "registry+https://github.com/rust-lang/crates.io-index" 299 | checksum = "aaf91faf136cb47367fa430cd46e37a788775e7fa104f8b4bcb3861dc389b724" 300 | dependencies = [ 301 | "fallible-iterator", 302 | "indexmap", 303 | "stable_deref_trait", 304 | ] 305 | 306 | [[package]] 307 | name = "gimli" 308 | version = "0.23.0" 309 | source = "registry+https://github.com/rust-lang/crates.io-index" 310 | checksum = "f6503fe142514ca4799d4c26297c4248239fe8838d827db6bd6065c6ed29a6ce" 311 | 312 | [[package]] 313 | name = "hashbrown" 314 | version = "0.9.1" 315 | source = "registry+https://github.com/rust-lang/crates.io-index" 316 | checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04" 317 | 318 | [[package]] 319 | name = "hermit-abi" 320 | version = "0.1.18" 321 | source = "registry+https://github.com/rust-lang/crates.io-index" 322 | checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" 323 | dependencies = [ 324 | "libc", 325 | ] 326 | 327 | [[package]] 328 | name = "ident_case" 329 | version = "1.0.1" 330 | source = "registry+https://github.com/rust-lang/crates.io-index" 331 | checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" 332 | 333 | [[package]] 334 | name = "indexmap" 335 | version = "1.6.1" 336 | source = "registry+https://github.com/rust-lang/crates.io-index" 337 | checksum = "4fb1fa934250de4de8aef298d81c729a7d33d8c239daa3a7575e6b92bfc7313b" 338 | dependencies = [ 339 | "autocfg", 340 | "hashbrown", 341 | "serde", 342 | ] 343 | 344 | [[package]] 345 | name = "lazy_static" 346 | version = "1.4.0" 347 | source = "registry+https://github.com/rust-lang/crates.io-index" 348 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" 349 | 350 | [[package]] 351 | name = "leb128" 352 | version = "0.2.4" 353 | source = "registry+https://github.com/rust-lang/crates.io-index" 354 | checksum = "3576a87f2ba00f6f106fdfcd16db1d698d648a26ad8e0573cad8537c3c362d2a" 355 | 356 | [[package]] 357 | name = "libc" 358 | version = "0.2.87" 359 | source = "registry+https://github.com/rust-lang/crates.io-index" 360 | checksum = "265d751d31d6780a3f956bb5b8022feba2d94eeee5a84ba64f4212eedca42213" 361 | 362 | [[package]] 363 | name = "libloading" 364 | version = "0.6.7" 365 | source = "registry+https://github.com/rust-lang/crates.io-index" 366 | checksum = "351a32417a12d5f7e82c368a66781e307834dae04c6ce0cd4456d52989229883" 367 | dependencies = [ 368 | "cfg-if 1.0.0", 369 | "winapi", 370 | ] 371 | 372 | [[package]] 373 | name = "log" 374 | version = "0.4.14" 375 | source = "registry+https://github.com/rust-lang/crates.io-index" 376 | checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" 377 | dependencies = [ 378 | "cfg-if 1.0.0", 379 | ] 380 | 381 | [[package]] 382 | name = "mach" 383 | version = "0.3.2" 384 | source = "registry+https://github.com/rust-lang/crates.io-index" 385 | checksum = "b823e83b2affd8f40a9ee8c29dbc56404c1e34cd2710921f2801e2cf29527afa" 386 | dependencies = [ 387 | "libc", 388 | ] 389 | 390 | [[package]] 391 | name = "memmap2" 392 | version = "0.2.1" 393 | source = "registry+https://github.com/rust-lang/crates.io-index" 394 | checksum = "04e3e85b970d650e2ae6d70592474087051c11c54da7f7b4949725c5735fbcc6" 395 | dependencies = [ 396 | "libc", 397 | ] 398 | 399 | [[package]] 400 | name = "memoffset" 401 | version = "0.6.1" 402 | source = "registry+https://github.com/rust-lang/crates.io-index" 403 | checksum = "157b4208e3059a8f9e78d559edc658e13df41410cb3ae03979c83130067fdd87" 404 | dependencies = [ 405 | "autocfg", 406 | ] 407 | 408 | [[package]] 409 | name = "miniz_oxide" 410 | version = "0.4.4" 411 | source = "registry+https://github.com/rust-lang/crates.io-index" 412 | checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" 413 | dependencies = [ 414 | "adler", 415 | "autocfg", 416 | ] 417 | 418 | [[package]] 419 | name = "more-asserts" 420 | version = "0.2.1" 421 | source = "registry+https://github.com/rust-lang/crates.io-index" 422 | checksum = "0debeb9fcf88823ea64d64e4a815ab1643f33127d995978e099942ce38f25238" 423 | 424 | [[package]] 425 | name = "num_cpus" 426 | version = "1.13.0" 427 | source = "registry+https://github.com/rust-lang/crates.io-index" 428 | checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" 429 | dependencies = [ 430 | "hermit-abi", 431 | "libc", 432 | ] 433 | 434 | [[package]] 435 | name = "object" 436 | version = "0.22.0" 437 | source = "registry+https://github.com/rust-lang/crates.io-index" 438 | checksum = "8d3b63360ec3cb337817c2dbd47ab4a0f170d285d8e5a2064600f3def1402397" 439 | dependencies = [ 440 | "crc32fast", 441 | "indexmap", 442 | ] 443 | 444 | [[package]] 445 | name = "object" 446 | version = "0.23.0" 447 | source = "registry+https://github.com/rust-lang/crates.io-index" 448 | checksum = "a9a7ab5d64814df0fe4a4b5ead45ed6c5f181ee3ff04ba344313a6c80446c5d4" 449 | 450 | [[package]] 451 | name = "pin-project-lite" 452 | version = "0.2.5" 453 | source = "registry+https://github.com/rust-lang/crates.io-index" 454 | checksum = "0cf491442e4b033ed1c722cb9f0df5fcfcf4de682466c46469c36bc47dc5548a" 455 | 456 | [[package]] 457 | name = "ppv-lite86" 458 | version = "0.2.10" 459 | source = "registry+https://github.com/rust-lang/crates.io-index" 460 | checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" 461 | 462 | [[package]] 463 | name = "proc-macro-error" 464 | version = "1.0.4" 465 | source = "registry+https://github.com/rust-lang/crates.io-index" 466 | checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" 467 | dependencies = [ 468 | "proc-macro-error-attr", 469 | "proc-macro2", 470 | "quote", 471 | "syn", 472 | "version_check", 473 | ] 474 | 475 | [[package]] 476 | name = "proc-macro-error-attr" 477 | version = "1.0.4" 478 | source = "registry+https://github.com/rust-lang/crates.io-index" 479 | checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" 480 | dependencies = [ 481 | "proc-macro2", 482 | "quote", 483 | "version_check", 484 | ] 485 | 486 | [[package]] 487 | name = "proc-macro2" 488 | version = "1.0.24" 489 | source = "registry+https://github.com/rust-lang/crates.io-index" 490 | checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" 491 | dependencies = [ 492 | "unicode-xid", 493 | ] 494 | 495 | [[package]] 496 | name = "quote" 497 | version = "1.0.9" 498 | source = "registry+https://github.com/rust-lang/crates.io-index" 499 | checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" 500 | dependencies = [ 501 | "proc-macro2", 502 | ] 503 | 504 | [[package]] 505 | name = "rand" 506 | version = "0.8.3" 507 | source = "registry+https://github.com/rust-lang/crates.io-index" 508 | checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" 509 | dependencies = [ 510 | "libc", 511 | "rand_chacha", 512 | "rand_core", 513 | "rand_hc", 514 | ] 515 | 516 | [[package]] 517 | name = "rand_chacha" 518 | version = "0.3.0" 519 | source = "registry+https://github.com/rust-lang/crates.io-index" 520 | checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" 521 | dependencies = [ 522 | "ppv-lite86", 523 | "rand_core", 524 | ] 525 | 526 | [[package]] 527 | name = "rand_core" 528 | version = "0.6.2" 529 | source = "registry+https://github.com/rust-lang/crates.io-index" 530 | checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" 531 | dependencies = [ 532 | "getrandom", 533 | ] 534 | 535 | [[package]] 536 | name = "rand_hc" 537 | version = "0.3.0" 538 | source = "registry+https://github.com/rust-lang/crates.io-index" 539 | checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" 540 | dependencies = [ 541 | "rand_core", 542 | ] 543 | 544 | [[package]] 545 | name = "rayon" 546 | version = "1.5.0" 547 | source = "registry+https://github.com/rust-lang/crates.io-index" 548 | checksum = "8b0d8e0819fadc20c74ea8373106ead0600e3a67ef1fe8da56e39b9ae7275674" 549 | dependencies = [ 550 | "autocfg", 551 | "crossbeam-deque", 552 | "either", 553 | "rayon-core", 554 | ] 555 | 556 | [[package]] 557 | name = "rayon-core" 558 | version = "1.9.0" 559 | source = "registry+https://github.com/rust-lang/crates.io-index" 560 | checksum = "9ab346ac5921dc62ffa9f89b7a773907511cdfa5490c572ae9be1be33e8afa4a" 561 | dependencies = [ 562 | "crossbeam-channel", 563 | "crossbeam-deque", 564 | "crossbeam-utils", 565 | "lazy_static", 566 | "num_cpus", 567 | ] 568 | 569 | [[package]] 570 | name = "redox_syscall" 571 | version = "0.2.5" 572 | source = "registry+https://github.com/rust-lang/crates.io-index" 573 | checksum = "94341e4e44e24f6b591b59e47a8a027df12e008d73fd5672dbea9cc22f4507d9" 574 | dependencies = [ 575 | "bitflags", 576 | ] 577 | 578 | [[package]] 579 | name = "regalloc" 580 | version = "0.0.31" 581 | source = "registry+https://github.com/rust-lang/crates.io-index" 582 | checksum = "571f7f397d61c4755285cd37853fe8e03271c243424a907415909379659381c5" 583 | dependencies = [ 584 | "log", 585 | "rustc-hash", 586 | "smallvec", 587 | ] 588 | 589 | [[package]] 590 | name = "region" 591 | version = "2.2.0" 592 | source = "registry+https://github.com/rust-lang/crates.io-index" 593 | checksum = "877e54ea2adcd70d80e9179344c97f93ef0dffd6b03e1f4529e6e83ab2fa9ae0" 594 | dependencies = [ 595 | "bitflags", 596 | "libc", 597 | "mach", 598 | "winapi", 599 | ] 600 | 601 | [[package]] 602 | name = "remove_dir_all" 603 | version = "0.5.3" 604 | source = "registry+https://github.com/rust-lang/crates.io-index" 605 | checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" 606 | dependencies = [ 607 | "winapi", 608 | ] 609 | 610 | [[package]] 611 | name = "rustc-demangle" 612 | version = "0.1.18" 613 | source = "registry+https://github.com/rust-lang/crates.io-index" 614 | checksum = "6e3bad0ee36814ca07d7968269dd4b7ec89ec2da10c4bb613928d3077083c232" 615 | 616 | [[package]] 617 | name = "rustc-hash" 618 | version = "1.1.0" 619 | source = "registry+https://github.com/rust-lang/crates.io-index" 620 | checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" 621 | 622 | [[package]] 623 | name = "scopeguard" 624 | version = "1.1.0" 625 | source = "registry+https://github.com/rust-lang/crates.io-index" 626 | checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" 627 | 628 | [[package]] 629 | name = "serde" 630 | version = "1.0.123" 631 | source = "registry+https://github.com/rust-lang/crates.io-index" 632 | checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae" 633 | dependencies = [ 634 | "serde_derive", 635 | ] 636 | 637 | [[package]] 638 | name = "serde_bytes" 639 | version = "0.11.5" 640 | source = "registry+https://github.com/rust-lang/crates.io-index" 641 | checksum = "16ae07dd2f88a366f15bd0632ba725227018c69a1c8550a927324f8eb8368bb9" 642 | dependencies = [ 643 | "serde", 644 | ] 645 | 646 | [[package]] 647 | name = "serde_derive" 648 | version = "1.0.123" 649 | source = "registry+https://github.com/rust-lang/crates.io-index" 650 | checksum = "9391c295d64fc0abb2c556bad848f33cb8296276b1ad2677d1ae1ace4f258f31" 651 | dependencies = [ 652 | "proc-macro2", 653 | "quote", 654 | "syn", 655 | ] 656 | 657 | [[package]] 658 | name = "smallvec" 659 | version = "1.6.1" 660 | source = "registry+https://github.com/rust-lang/crates.io-index" 661 | checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" 662 | 663 | [[package]] 664 | name = "stable_deref_trait" 665 | version = "1.2.0" 666 | source = "registry+https://github.com/rust-lang/crates.io-index" 667 | checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" 668 | 669 | [[package]] 670 | name = "strsim" 671 | version = "0.10.0" 672 | source = "registry+https://github.com/rust-lang/crates.io-index" 673 | checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" 674 | 675 | [[package]] 676 | name = "syn" 677 | version = "1.0.60" 678 | source = "registry+https://github.com/rust-lang/crates.io-index" 679 | checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081" 680 | dependencies = [ 681 | "proc-macro2", 682 | "quote", 683 | "unicode-xid", 684 | ] 685 | 686 | [[package]] 687 | name = "target-lexicon" 688 | version = "0.11.2" 689 | source = "registry+https://github.com/rust-lang/crates.io-index" 690 | checksum = "422045212ea98508ae3d28025bc5aaa2bd4a9cdaecd442a08da2ee620ee9ea95" 691 | 692 | [[package]] 693 | name = "tempfile" 694 | version = "3.2.0" 695 | source = "registry+https://github.com/rust-lang/crates.io-index" 696 | checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22" 697 | dependencies = [ 698 | "cfg-if 1.0.0", 699 | "libc", 700 | "rand", 701 | "redox_syscall", 702 | "remove_dir_all", 703 | "winapi", 704 | ] 705 | 706 | [[package]] 707 | name = "thiserror" 708 | version = "1.0.24" 709 | source = "registry+https://github.com/rust-lang/crates.io-index" 710 | checksum = "e0f4a65597094d4483ddaed134f409b2cb7c1beccf25201a9f73c719254fa98e" 711 | dependencies = [ 712 | "thiserror-impl", 713 | ] 714 | 715 | [[package]] 716 | name = "thiserror-impl" 717 | version = "1.0.24" 718 | source = "registry+https://github.com/rust-lang/crates.io-index" 719 | checksum = "7765189610d8241a44529806d6fd1f2e0a08734313a35d5b3a556f92b381f3c0" 720 | dependencies = [ 721 | "proc-macro2", 722 | "quote", 723 | "syn", 724 | ] 725 | 726 | [[package]] 727 | name = "tracing" 728 | version = "0.1.25" 729 | source = "registry+https://github.com/rust-lang/crates.io-index" 730 | checksum = "01ebdc2bb4498ab1ab5f5b73c5803825e60199229ccba0698170e3be0e7f959f" 731 | dependencies = [ 732 | "cfg-if 1.0.0", 733 | "pin-project-lite", 734 | "tracing-attributes", 735 | "tracing-core", 736 | ] 737 | 738 | [[package]] 739 | name = "tracing-attributes" 740 | version = "0.1.13" 741 | source = "registry+https://github.com/rust-lang/crates.io-index" 742 | checksum = "a8a9bd1db7706f2373a190b0d067146caa39350c486f3d455b0e33b431f94c07" 743 | dependencies = [ 744 | "proc-macro2", 745 | "quote", 746 | "syn", 747 | ] 748 | 749 | [[package]] 750 | name = "tracing-core" 751 | version = "0.1.17" 752 | source = "registry+https://github.com/rust-lang/crates.io-index" 753 | checksum = "f50de3927f93d202783f4513cda820ab47ef17f624b03c096e86ef00c67e6b5f" 754 | dependencies = [ 755 | "lazy_static", 756 | ] 757 | 758 | [[package]] 759 | name = "unicode-xid" 760 | version = "0.2.1" 761 | source = "registry+https://github.com/rust-lang/crates.io-index" 762 | checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" 763 | 764 | [[package]] 765 | name = "version_check" 766 | version = "0.9.2" 767 | source = "registry+https://github.com/rust-lang/crates.io-index" 768 | checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" 769 | 770 | [[package]] 771 | name = "wasi" 772 | version = "0.10.2+wasi-snapshot-preview1" 773 | source = "registry+https://github.com/rust-lang/crates.io-index" 774 | checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" 775 | 776 | [[package]] 777 | name = "wasm_plugin_host" 778 | version = "0.1.6" 779 | dependencies = [ 780 | "bincode", 781 | "bitfield", 782 | "getrandom", 783 | "serde", 784 | "wasmer", 785 | ] 786 | 787 | [[package]] 788 | name = "wasmer" 789 | version = "1.0.2" 790 | source = "registry+https://github.com/rust-lang/crates.io-index" 791 | checksum = "a70cfae554988d904d64ca17ab0e7cd652ee5c8a0807094819c1ea93eb9d6866" 792 | dependencies = [ 793 | "cfg-if 0.1.10", 794 | "indexmap", 795 | "more-asserts", 796 | "target-lexicon", 797 | "thiserror", 798 | "wasmer-compiler", 799 | "wasmer-compiler-cranelift", 800 | "wasmer-derive", 801 | "wasmer-engine", 802 | "wasmer-engine-jit", 803 | "wasmer-engine-native", 804 | "wasmer-types", 805 | "wasmer-vm", 806 | "wat", 807 | "winapi", 808 | ] 809 | 810 | [[package]] 811 | name = "wasmer-compiler" 812 | version = "1.0.2" 813 | source = "registry+https://github.com/rust-lang/crates.io-index" 814 | checksum = "6b7732a9cab472bd921d5a0c422f45b3d03f62fa2c40a89e0770cef6d47e383e" 815 | dependencies = [ 816 | "enumset", 817 | "serde", 818 | "serde_bytes", 819 | "smallvec", 820 | "target-lexicon", 821 | "thiserror", 822 | "wasmer-types", 823 | "wasmer-vm", 824 | "wasmparser", 825 | ] 826 | 827 | [[package]] 828 | name = "wasmer-compiler-cranelift" 829 | version = "1.0.2" 830 | source = "registry+https://github.com/rust-lang/crates.io-index" 831 | checksum = "48cb9395f094e1d81534f4c5e330ed4cdb424e8df870d29ad585620284f5fddb" 832 | dependencies = [ 833 | "cranelift-codegen", 834 | "cranelift-frontend", 835 | "gimli 0.22.0", 836 | "more-asserts", 837 | "rayon", 838 | "serde", 839 | "smallvec", 840 | "tracing", 841 | "wasmer-compiler", 842 | "wasmer-types", 843 | "wasmer-vm", 844 | ] 845 | 846 | [[package]] 847 | name = "wasmer-derive" 848 | version = "1.0.2" 849 | source = "registry+https://github.com/rust-lang/crates.io-index" 850 | checksum = "d8b86dcd2c3efdb8390728a2b56f762db07789aaa5aa872a9dc776ba3a7912ed" 851 | dependencies = [ 852 | "proc-macro-error", 853 | "proc-macro2", 854 | "quote", 855 | "syn", 856 | ] 857 | 858 | [[package]] 859 | name = "wasmer-engine" 860 | version = "1.0.2" 861 | source = "registry+https://github.com/rust-lang/crates.io-index" 862 | checksum = "efe4667d6bd888f26ae8062a63a9379fa697415b4b4e380f33832e8418fd71b5" 863 | dependencies = [ 864 | "backtrace", 865 | "bincode", 866 | "lazy_static", 867 | "memmap2", 868 | "more-asserts", 869 | "rustc-demangle", 870 | "serde", 871 | "serde_bytes", 872 | "target-lexicon", 873 | "thiserror", 874 | "wasmer-compiler", 875 | "wasmer-types", 876 | "wasmer-vm", 877 | ] 878 | 879 | [[package]] 880 | name = "wasmer-engine-jit" 881 | version = "1.0.2" 882 | source = "registry+https://github.com/rust-lang/crates.io-index" 883 | checksum = "26770be802888011b4a3072f2a282fc2faa68aa48c71b3db6252a3937a85f3da" 884 | dependencies = [ 885 | "bincode", 886 | "cfg-if 0.1.10", 887 | "region", 888 | "serde", 889 | "serde_bytes", 890 | "wasmer-compiler", 891 | "wasmer-engine", 892 | "wasmer-types", 893 | "wasmer-vm", 894 | "winapi", 895 | ] 896 | 897 | [[package]] 898 | name = "wasmer-engine-native" 899 | version = "1.0.2" 900 | source = "registry+https://github.com/rust-lang/crates.io-index" 901 | checksum = "2bb4083a6c69f2cd4b000b82a80717f37c6cc2e536aee3a8ffe9af3edc276a8b" 902 | dependencies = [ 903 | "bincode", 904 | "cfg-if 0.1.10", 905 | "leb128", 906 | "libloading", 907 | "serde", 908 | "tempfile", 909 | "tracing", 910 | "wasmer-compiler", 911 | "wasmer-engine", 912 | "wasmer-object", 913 | "wasmer-types", 914 | "wasmer-vm", 915 | "which", 916 | ] 917 | 918 | [[package]] 919 | name = "wasmer-object" 920 | version = "1.0.2" 921 | source = "registry+https://github.com/rust-lang/crates.io-index" 922 | checksum = "abf8e0c12b82ff81ebecd30d7e118be5fec871d6de885a90eeb105df0a769a7b" 923 | dependencies = [ 924 | "object 0.22.0", 925 | "thiserror", 926 | "wasmer-compiler", 927 | "wasmer-types", 928 | ] 929 | 930 | [[package]] 931 | name = "wasmer-types" 932 | version = "1.0.2" 933 | source = "registry+https://github.com/rust-lang/crates.io-index" 934 | checksum = "c7f4ac28c2951cd792c18332f03da523ed06b170f5cf6bb5b1bdd7e36c2a8218" 935 | dependencies = [ 936 | "cranelift-entity", 937 | "serde", 938 | "thiserror", 939 | ] 940 | 941 | [[package]] 942 | name = "wasmer-vm" 943 | version = "1.0.2" 944 | source = "registry+https://github.com/rust-lang/crates.io-index" 945 | checksum = "a7635ba0b6d2fd325f588d69a950ad9fa04dddbf6ad08b6b2a183146319bf6ae" 946 | dependencies = [ 947 | "backtrace", 948 | "cc", 949 | "cfg-if 0.1.10", 950 | "indexmap", 951 | "libc", 952 | "memoffset", 953 | "more-asserts", 954 | "region", 955 | "serde", 956 | "thiserror", 957 | "wasmer-types", 958 | "winapi", 959 | ] 960 | 961 | [[package]] 962 | name = "wasmparser" 963 | version = "0.65.0" 964 | source = "registry+https://github.com/rust-lang/crates.io-index" 965 | checksum = "87cc2fe6350834b4e528ba0901e7aa405d78b89dc1fa3145359eb4de0e323fcf" 966 | 967 | [[package]] 968 | name = "wast" 969 | version = "35.0.0" 970 | source = "registry+https://github.com/rust-lang/crates.io-index" 971 | checksum = "db5ae96da18bb5926341516fd409b5a8ce4e4714da7f0a1063d3b20ac9f9a1e1" 972 | dependencies = [ 973 | "leb128", 974 | ] 975 | 976 | [[package]] 977 | name = "wat" 978 | version = "1.0.36" 979 | source = "registry+https://github.com/rust-lang/crates.io-index" 980 | checksum = "0b0fa059022c5dabe129f02b429d67086400deb8277f89c975555dacc1dadbcc" 981 | dependencies = [ 982 | "wast", 983 | ] 984 | 985 | [[package]] 986 | name = "which" 987 | version = "4.0.2" 988 | source = "registry+https://github.com/rust-lang/crates.io-index" 989 | checksum = "87c14ef7e1b8b8ecfc75d5eca37949410046e66f15d185c01d70824f1f8111ef" 990 | dependencies = [ 991 | "libc", 992 | "thiserror", 993 | ] 994 | 995 | [[package]] 996 | name = "winapi" 997 | version = "0.3.9" 998 | source = "registry+https://github.com/rust-lang/crates.io-index" 999 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 1000 | dependencies = [ 1001 | "winapi-i686-pc-windows-gnu", 1002 | "winapi-x86_64-pc-windows-gnu", 1003 | ] 1004 | 1005 | [[package]] 1006 | name = "winapi-i686-pc-windows-gnu" 1007 | version = "0.4.0" 1008 | source = "registry+https://github.com/rust-lang/crates.io-index" 1009 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 1010 | 1011 | [[package]] 1012 | name = "winapi-x86_64-pc-windows-gnu" 1013 | version = "0.4.0" 1014 | source = "registry+https://github.com/rust-lang/crates.io-index" 1015 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 1016 | -------------------------------------------------------------------------------- /example/example_host/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "example_host" 3 | version = "0.1.0" 4 | authors = ["Alec Deason "] 5 | edition = "2018" 6 | publish = false 7 | 8 | [dependencies] 9 | wasm_plugin_host = { path = "../../host" } 10 | -------------------------------------------------------------------------------- /example/example_host/README.md: -------------------------------------------------------------------------------- 1 | This example loads the plugin in ../example_guest and calls a couple of functions on it. Compile the guest in release first, then just `cargo run` in this directory. 2 | -------------------------------------------------------------------------------- /example/example_host/src/main.rs: -------------------------------------------------------------------------------- 1 | use wasm_plugin_host::WasmPluginBuilder; 2 | 3 | fn main() -> Result<(), Box> { 4 | let mut plugin = WasmPluginBuilder::from_file( 5 | "../example_guest/target/wasm32-unknown-unknown/debug/example_guest.wasm", 6 | )? 7 | .import_function("the_hosts_favorite_numbers", || { 8 | vec![0, 1, 42] 9 | }) 10 | .import_function("please_capitalize_this", |mut s: String| { 11 | println!("From guest: {}", s); 12 | s.make_ascii_uppercase(); 13 | s 14 | }) 15 | .finish()?; 16 | let response: String = plugin.call_function("hello")?; 17 | println!("The guest says: '{}'", response); 18 | 19 | let message = "Hello, Guest!".to_string(); 20 | let response: String = plugin.call_function_with_argument("echo", &message)?; 21 | println!( 22 | "I said: '{}'. The guest said, '{}' back. Weird", 23 | message, response 24 | ); 25 | 26 | // Any type that can be serialized works 27 | let response: Vec = plugin.call_function("favorite_numbers")?; 28 | println!( 29 | "The guest's favorite integers (less that 2**32) are: '{:?}'", 30 | response 31 | ); 32 | 33 | Ok(()) 34 | } 35 | -------------------------------------------------------------------------------- /guest/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "bincode" 5 | version = "1.3.3" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" 8 | dependencies = [ 9 | "serde", 10 | ] 11 | 12 | [[package]] 13 | name = "bitfield" 14 | version = "0.13.2" 15 | source = "registry+https://github.com/rust-lang/crates.io-index" 16 | checksum = "46afbd2983a5d5a7bd740ccb198caf5b82f45c40c09c0eed36052d91cb92e719" 17 | 18 | [[package]] 19 | name = "cfg-if" 20 | version = "1.0.0" 21 | source = "registry+https://github.com/rust-lang/crates.io-index" 22 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 23 | 24 | [[package]] 25 | name = "getrandom" 26 | version = "0.2.2" 27 | source = "registry+https://github.com/rust-lang/crates.io-index" 28 | checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" 29 | dependencies = [ 30 | "cfg-if", 31 | "libc", 32 | "wasi", 33 | ] 34 | 35 | [[package]] 36 | name = "itoa" 37 | version = "0.4.7" 38 | source = "registry+https://github.com/rust-lang/crates.io-index" 39 | checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" 40 | 41 | [[package]] 42 | name = "libc" 43 | version = "0.2.94" 44 | source = "registry+https://github.com/rust-lang/crates.io-index" 45 | checksum = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e" 46 | 47 | [[package]] 48 | name = "nanoserde" 49 | version = "0.1.25" 50 | source = "registry+https://github.com/rust-lang/crates.io-index" 51 | checksum = "1ae5894adc81d64b16f75ce8a67c994a6aa4b6e1c99f2920d22e0e575755d7e9" 52 | dependencies = [ 53 | "nanoserde-derive", 54 | ] 55 | 56 | [[package]] 57 | name = "nanoserde-derive" 58 | version = "0.1.16" 59 | source = "registry+https://github.com/rust-lang/crates.io-index" 60 | checksum = "71c29c140bb2a43bdc554ca5a34ecfe678bb6e6c6a3dc8d6856852ba2c74f20f" 61 | 62 | [[package]] 63 | name = "proc-macro2" 64 | version = "1.0.26" 65 | source = "registry+https://github.com/rust-lang/crates.io-index" 66 | checksum = "a152013215dca273577e18d2bf00fa862b89b24169fb78c4c95aeb07992c9cec" 67 | dependencies = [ 68 | "unicode-xid", 69 | ] 70 | 71 | [[package]] 72 | name = "quote" 73 | version = "1.0.9" 74 | source = "registry+https://github.com/rust-lang/crates.io-index" 75 | checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" 76 | dependencies = [ 77 | "proc-macro2", 78 | ] 79 | 80 | [[package]] 81 | name = "ryu" 82 | version = "1.0.5" 83 | source = "registry+https://github.com/rust-lang/crates.io-index" 84 | checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" 85 | 86 | [[package]] 87 | name = "serde" 88 | version = "1.0.125" 89 | source = "registry+https://github.com/rust-lang/crates.io-index" 90 | checksum = "558dc50e1a5a5fa7112ca2ce4effcb321b0300c0d4ccf0776a9f60cd89031171" 91 | 92 | [[package]] 93 | name = "serde_json" 94 | version = "1.0.64" 95 | source = "registry+https://github.com/rust-lang/crates.io-index" 96 | checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79" 97 | dependencies = [ 98 | "itoa", 99 | "ryu", 100 | "serde", 101 | ] 102 | 103 | [[package]] 104 | name = "syn" 105 | version = "1.0.71" 106 | source = "registry+https://github.com/rust-lang/crates.io-index" 107 | checksum = "ad184cc9470f9117b2ac6817bfe297307418819ba40552f9b3846f05c33d5373" 108 | dependencies = [ 109 | "proc-macro2", 110 | "quote", 111 | "unicode-xid", 112 | ] 113 | 114 | [[package]] 115 | name = "unicode-xid" 116 | version = "0.2.2" 117 | source = "registry+https://github.com/rust-lang/crates.io-index" 118 | checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" 119 | 120 | [[package]] 121 | name = "wasi" 122 | version = "0.10.2+wasi-snapshot-preview1" 123 | source = "registry+https://github.com/rust-lang/crates.io-index" 124 | checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" 125 | 126 | [[package]] 127 | name = "wasm_plugin_guest" 128 | version = "0.1.5" 129 | dependencies = [ 130 | "bincode", 131 | "bitfield", 132 | "getrandom", 133 | "nanoserde", 134 | "serde", 135 | "serde_json", 136 | "wasm_plugin_guest_derive", 137 | ] 138 | 139 | [[package]] 140 | name = "wasm_plugin_guest_derive" 141 | version = "0.1.5" 142 | source = "registry+https://github.com/rust-lang/crates.io-index" 143 | checksum = "8029abbe46726b92fcfb290819097e8ae205030d8b09cdada5be037476ad879a" 144 | dependencies = [ 145 | "proc-macro2", 146 | "quote", 147 | "syn", 148 | ] 149 | -------------------------------------------------------------------------------- /guest/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "wasm_plugin_guest" 3 | version = "0.1.5" 4 | authors = ["Alec Deason "] 5 | edition = "2018" 6 | license = "MIT" 7 | readme = "README.md" 8 | repository = "https://github.com/alec-deason/wasm_plugin/tree/main/guest" 9 | description = "A low-ish level tool for easily writing WASM based plugins" 10 | keywords = ["WASM", "plugin"] 11 | categories = ["game-development", "wasm"] 12 | 13 | [features] 14 | default = ["inject_getrandom", "serialize_bincode"] 15 | inject_getrandom = ["getrandom"] 16 | serialize_bincode = ["bincode", "serde"] 17 | serialize_json = ["serde_json", "serde"] 18 | serialize_nanoserde_json = ["nanoserde"] 19 | 20 | 21 | [dependencies] 22 | wasm_plugin_guest_derive = "0.1.5" 23 | getrandom = { version = "0.2", features = ["custom"], optional = true } 24 | serde = { version = "1", optional = true } 25 | bincode = { version = "1", optional = true } 26 | serde_json = { version = "1", optional = true } 27 | nanoserde = { version = "0.1", optional = true } 28 | bitfield = "0.13.2" 29 | -------------------------------------------------------------------------------- /guest/README.md: -------------------------------------------------------------------------------- 1 | #OBSOLETE# 2 | These crates are no longer maintained and the use case is better served by the [WebAssembly Component specification](https://component-model.bytecodealliance.org/language-support/rust.html). 3 | 4 | 5 | [![Crates.io](https://img.shields.io/crates/v/wasm_plugin_guest.svg)](https://crates.io/crates/wasm_plugin_guest) 6 | [![Docs.rs](https://docs.rs/wasm_plugin_guest/badge.svg)](https://docs.rs/wasm_plugin_guest) 7 | [![license](https://img.shields.io/badge/license-MIT-blue.svg)](../LICENSE) 8 | 9 | A low-ish level tool for easily writing WASM based plugins to be hosted by 10 | wasm_plugin_host. 11 | 12 | The goal of wasm_plugin is to make communicating across the host-plugin 13 | boundary as simple and idiomatic as possible while being unopinionated 14 | about how you actually use the plugin. 15 | 16 | Plugins are meant to be run using [wasm_plugin_host](https://crates.io/crates/wasm_plugin_host) 17 | 18 | Exporting a function is just a matter of adding an attribute. 19 | 20 | ```rust 21 | #[wasm_plugin_guest::export_function] 22 | fn hello() -> String { 23 | "Hello, host!".to_string() 24 | } 25 | ``` 26 | 27 | ## API Stability 28 | 29 | I am not currently guaranteeing any stability, expect all releases to include breaking changes. 30 | -------------------------------------------------------------------------------- /guest/guest_derive/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "proc-macro2" 5 | version = "1.0.24" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" 8 | dependencies = [ 9 | "unicode-xid", 10 | ] 11 | 12 | [[package]] 13 | name = "quote" 14 | version = "1.0.9" 15 | source = "registry+https://github.com/rust-lang/crates.io-index" 16 | checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" 17 | dependencies = [ 18 | "proc-macro2", 19 | ] 20 | 21 | [[package]] 22 | name = "syn" 23 | version = "1.0.60" 24 | source = "registry+https://github.com/rust-lang/crates.io-index" 25 | checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081" 26 | dependencies = [ 27 | "proc-macro2", 28 | "quote", 29 | "unicode-xid", 30 | ] 31 | 32 | [[package]] 33 | name = "unicode-xid" 34 | version = "0.2.1" 35 | source = "registry+https://github.com/rust-lang/crates.io-index" 36 | checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" 37 | 38 | [[package]] 39 | name = "wasm_plugin_guest_derive" 40 | version = "0.1.5" 41 | dependencies = [ 42 | "proc-macro2", 43 | "quote", 44 | "syn", 45 | ] 46 | -------------------------------------------------------------------------------- /guest/guest_derive/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "wasm_plugin_guest_derive" 3 | version = "0.1.5" 4 | authors = ["Alec Deason "] 5 | edition = "2018" 6 | license = "MIT" 7 | repository = "https://github.com/alec-deason/wasm_plugin/tree/main/guest/guest_derive" 8 | description = "A low-ish level tool for easily writing WASM based plugins" 9 | keywords = ["WASM", "plugin"] 10 | categories = ["game-development", "wasm"] 11 | 12 | [lib] 13 | proc-macro = true 14 | 15 | [dependencies] 16 | syn = { version = "1", features = ["full"] } 17 | proc-macro2 = "1.0.24" 18 | quote = "1" 19 | -------------------------------------------------------------------------------- /guest/guest_derive/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![doc(html_root_url = "https://docs.rs/wasm_plugin_guest_derive/0.1.5")] 2 | #![deny(missing_docs)] 3 | 4 | //! This crate provides attribute macros used by [wasm_plugin_guest](https://crates.io/crates/wasm_plugin_guest) 5 | 6 | use proc_macro::TokenStream; 7 | extern crate proc_macro; 8 | use quote::{format_ident, quote}; 9 | 10 | /// Builds an extern function which will handle serializing and 11 | /// deserializing of arguments and return values of the function it is applied 12 | /// to. The function must take only deserializable arguments and return 13 | /// a serializable result. 14 | /// 15 | /// The name of the exported function will be mangled to 16 | /// `wasm_plugin_exported__ORIGINAL_NAME` The exported function is only 17 | /// intended to be used by [wasm_plugin_host](https://crates.io/crates/wasm_plugin_host) 18 | #[proc_macro_attribute] 19 | pub fn export_function(_args: TokenStream, input: TokenStream) -> TokenStream { 20 | let ast = syn::parse_macro_input!(input as syn::ItemFn); 21 | 22 | impl_function_export(&ast) 23 | } 24 | 25 | fn impl_function_export(ast: &syn::ItemFn) -> TokenStream { 26 | let name = &ast.sig.ident; 27 | let remote_name = format_ident!("wasm_plugin_exported__{}", name); 28 | let gen = if ast.sig.inputs.is_empty() { 29 | quote! { 30 | #[no_mangle] 31 | pub extern "C" fn #remote_name() -> u64 { 32 | let (ptr, len) = wasm_plugin_guest::write_message(&#name()); 33 | let mut fat = wasm_plugin_guest::FatPointer(0); 34 | fat.set_ptr(ptr as u32); 35 | fat.set_len(len as u32); 36 | fat.0 37 | } 38 | } 39 | } else { 40 | let mut argument_types = quote!(); 41 | let mut call = quote!(); 42 | if ast.sig.inputs.len() == 1 { 43 | if let syn::FnArg::Typed(t) = &ast.sig.inputs[0] { 44 | let ty = &t.ty; 45 | argument_types = quote!(#ty); 46 | } else { 47 | panic!(); 48 | } 49 | call = quote!(message); 50 | } else { 51 | for (i, arg) in ast.sig.inputs.iter().enumerate() { 52 | let i = syn::Index::from(i); 53 | call = quote!(#call message.#i,); 54 | if let syn::FnArg::Typed(t) = arg { 55 | let ty = &t.ty; 56 | argument_types = quote!(#argument_types #ty,); 57 | } else { 58 | panic!(); 59 | } 60 | } 61 | argument_types = quote! { (#argument_types) }; 62 | } 63 | quote! { 64 | #[no_mangle] 65 | pub extern "C" fn #remote_name(ptr: u32, len: u32) -> u64 { 66 | let message:#argument_types = wasm_plugin_guest::read_message(ptr as usize, len as usize); 67 | 68 | let (ptr, len) = wasm_plugin_guest::write_message(&#name(#call)); 69 | let mut fat = wasm_plugin_guest::FatPointer(0); 70 | fat.set_ptr(ptr as u32); 71 | fat.set_len(len as u32); 72 | fat.0 73 | } 74 | } 75 | }; 76 | quote!(#gen #ast).into() 77 | } 78 | 79 | struct FnImports { 80 | functions: Vec, 81 | } 82 | 83 | impl syn::parse::Parse for FnImports { 84 | fn parse(input: syn::parse::ParseStream) -> syn::parse::Result { 85 | let mut functions = vec![]; 86 | while let Ok(f) = input.parse::() { 87 | functions.push(f); 88 | input.parse::()?; 89 | } 90 | Ok(FnImports { functions }) 91 | } 92 | } 93 | 94 | /// Import functions from the host program. The function's arguments an return 95 | /// type must all be serializable. Several functions can be imported at once 96 | /// by listing their signatures seperated by `;` 97 | /// 98 | /// ```rust 99 | /// import_functions! { 100 | /// fn my_function(); 101 | /// fn my_other_function(s: String) -> Vec; 102 | /// } 103 | /// ``` 104 | /// The macro creates a safe wrapper function using the given name which can 105 | /// be called in the plugin code. The actual imported function, which normal 106 | /// code will never need to access, will have a mangled name: 107 | /// `wasm_plugin_imported__ORIGINAL_NAME` and is only intended to be called by 108 | /// by host code using [wasm_plugin_host](https://crates.io/crates/wasm_plugin_host) 109 | #[proc_macro] 110 | pub fn import_functions(input: TokenStream) -> TokenStream { 111 | let ast = syn::parse_macro_input!(input as FnImports); 112 | impl_import_functions(&ast) 113 | } 114 | 115 | fn impl_import_functions(ast: &FnImports) -> TokenStream { 116 | let mut remote_fns = quote!(); 117 | let mut local_fns = quote!(); 118 | for f in ast.functions.iter().cloned() { 119 | let remote_name = format_ident!("wasm_plugin_imported__{}", f.ident); 120 | let gen = if f.inputs.is_empty() { 121 | match &f.output { 122 | syn::ReturnType::Default => { 123 | quote! { 124 | #f { 125 | unsafe { 126 | #remote_name(); 127 | } 128 | } 129 | } 130 | } 131 | syn::ReturnType::Type(_, ty) => { 132 | quote! { 133 | #f { 134 | let fat_ptr = unsafe { 135 | #remote_name() 136 | }; 137 | let fat_ptr = wasm_plugin_guest::FatPointer(fat_ptr); 138 | let message:(#ty) = wasm_plugin_guest::read_message(fat_ptr.ptr() as usize, fat_ptr.len() as usize); 139 | message 140 | } 141 | } 142 | } 143 | } 144 | } else { 145 | let mut message = quote!(); 146 | if f.inputs.len() == 1 { 147 | if let syn::FnArg::Typed(syn::PatType { pat: p, .. }) = &f.inputs[0] { 148 | if let syn::Pat::Ident(i) = p.as_ref() { 149 | message = quote!(#i); 150 | } else { 151 | unimplemented!("unsupported argument type"); 152 | } 153 | } else { 154 | unimplemented!("unsupported argument type"); 155 | } 156 | } else { 157 | for item in &f.inputs { 158 | if let syn::FnArg::Typed(syn::PatType { pat: p, .. }) = item { 159 | if let syn::Pat::Ident(i) = p.as_ref() { 160 | message = quote!(#i,); 161 | } else { 162 | unimplemented!("unsupported argument type"); 163 | } 164 | } else { 165 | unimplemented!("unsupported argument type"); 166 | } 167 | } 168 | message = quote!((#message)); 169 | } 170 | match &f.output { 171 | syn::ReturnType::Default => { 172 | quote! { 173 | #f { 174 | let (ptr, len) = wasm_plugin_guest::write_message(&#message); 175 | unsafe { 176 | #remote_name(ptr as u32, len as u32); 177 | } 178 | } 179 | } 180 | } 181 | syn::ReturnType::Type(_, ty) => { 182 | quote! { 183 | #f { 184 | let (ptr, len) = wasm_plugin_guest::write_message(&(#message)); 185 | let fat_ptr = unsafe { 186 | #remote_name(ptr as u32, len as u32) 187 | }; 188 | let fat_ptr = wasm_plugin_guest::FatPointer(fat_ptr); 189 | let message:(#ty) = wasm_plugin_guest::read_message(fat_ptr.ptr() as usize, fat_ptr.len() as usize); 190 | message 191 | } 192 | } 193 | } 194 | } 195 | }; 196 | local_fns = quote! { 197 | #local_fns 198 | #gen 199 | }; 200 | let gen = if f.inputs.is_empty() { 201 | match &f.output { 202 | syn::ReturnType::Default => { 203 | quote! { 204 | fn #remote_name(); 205 | } 206 | } 207 | syn::ReturnType::Type(_, _) => { 208 | quote! { 209 | fn #remote_name() -> u64; 210 | } 211 | } 212 | } 213 | } else { 214 | match &f.output { 215 | syn::ReturnType::Default => { 216 | quote! { 217 | fn #remote_name(ptr: u32, len: u32); 218 | } 219 | } 220 | syn::ReturnType::Type(_, _) => { 221 | quote! { 222 | fn #remote_name(ptr: u32, len: u32) -> u64; 223 | } 224 | } 225 | } 226 | }; 227 | remote_fns = quote!(#remote_fns #gen); 228 | } 229 | let exports = quote! { 230 | #local_fns 231 | extern "C" { 232 | #remote_fns 233 | } 234 | }; 235 | exports.into() 236 | } 237 | -------------------------------------------------------------------------------- /guest/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![doc(html_root_url = "https://docs.rs/wasm_plugin_guest/0.1.5")] 2 | #![deny(missing_docs)] 3 | 4 | //! A low-ish level tool for easily writing WASM based plugins to be hosted by 5 | //! wasm_plugin_host. 6 | //! 7 | //! The goal of wasm_plugin is to make communicating across the host-plugin 8 | //! boundary as simple and idiomatic as possible while being unopinionated 9 | //! about how you actually use the plugin. 10 | //! 11 | //! This crate currently supports serialization either using bincode or json 12 | //! selected by feature: 13 | //! `serialize_bincode`: Uses serde and bincode. It is selected by default. 14 | //! `serialize_json`: Uses serde and serde_json. 15 | //! `serialize_nanoserde_json': Uses nanoserde. 16 | //! 17 | //! Bincode is likely the best choice if all plugins the system uses will be 18 | //! written in Rust. Json is useful if a mix or languages will be used. 19 | //! 20 | //! Plugins are meant to be run using [wasm_plugin_host](https://crates.io/crates/wasm_plugin_host) 21 | 22 | use std::mem::ManuallyDrop; 23 | 24 | mod serialization; 25 | pub use wasm_plugin_guest_derive::{export_function, import_functions}; 26 | 27 | bitfield::bitfield! { 28 | #[doc(hidden)] 29 | pub struct FatPointer(u64); 30 | u32; 31 | #[doc(hidden)] 32 | pub ptr, set_ptr: 31, 0; 33 | #[doc(hidden)] 34 | pub len, set_len: 63, 32; 35 | } 36 | 37 | /// Read a message from a buffer created with `allocate_message_buffer`. You should 38 | /// never need to call this directly. 39 | pub fn read_message(ptr: usize, len: usize) -> T { 40 | let buf = unsafe { std::slice::from_raw_parts(ptr as *const u8, len as usize) }; 41 | T::deserialize(buf) 42 | } 43 | 44 | /// Write a message to the buffer used to communicate with the host. You should 45 | /// never need to call this directly. 46 | pub fn write_message(message: &U) -> (usize, usize) 47 | where 48 | U: serialization::Serializable, 49 | { 50 | let message: Vec = message.serialize(); 51 | let local_len = message.len(); 52 | ( 53 | ManuallyDrop::new(message).as_mut_ptr() as *const usize as usize, 54 | local_len, 55 | ) 56 | } 57 | 58 | #[cfg(feature = "inject_getrandom")] 59 | mod getrandom_shim { 60 | use getrandom::register_custom_getrandom; 61 | 62 | use getrandom::Error; 63 | 64 | extern "C" { 65 | fn __getrandom(ptr: u32, len: u32); 66 | } 67 | 68 | #[allow(clippy::unnecessary_wraps)] 69 | fn external_getrandom(buf: &mut [u8]) -> Result<(), Error> { 70 | let len = buf.len(); 71 | let ptr = buf.as_ptr(); 72 | unsafe { 73 | __getrandom(ptr as u32, len as u32); 74 | } 75 | Ok(()) 76 | } 77 | register_custom_getrandom!(external_getrandom); 78 | } 79 | 80 | /// Allocate a buffer suitable for writing messages to and return it's address. 81 | #[no_mangle] 82 | pub extern "C" fn allocate_message_buffer(len: u32) -> u32 { 83 | let mut buffer: ManuallyDrop> = ManuallyDrop::new(Vec::with_capacity(len as usize)); 84 | buffer.as_mut_ptr() as *const u32 as u32 85 | } 86 | 87 | /// Frees a previously allocated buffer. 88 | #[no_mangle] 89 | pub extern "C" fn free_message_buffer(ptr: u32, len: u32) { 90 | unsafe { drop(Vec::from_raw_parts(ptr as *mut u8, 0, len as usize)) } 91 | } 92 | -------------------------------------------------------------------------------- /guest/src/serialization.rs: -------------------------------------------------------------------------------- 1 | pub trait Serializable { 2 | fn serialize(&self) -> Vec; 3 | } 4 | #[cfg(feature = "serialize_bincode")] 5 | impl Serializable for T { 6 | fn serialize(&self) -> Vec { 7 | bincode::serialize(self).unwrap() 8 | } 9 | } 10 | #[cfg(feature = "serialize_json")] 11 | impl Serializable for T { 12 | fn serialize(&self) -> Vec { 13 | serde_json::to_vec(self).unwrap() 14 | } 15 | } 16 | #[cfg(feature = "serialize_nanoserde_json")] 17 | impl Serializable for T { 18 | fn serialize(&self) -> Vec { 19 | nanoserde::SerJson::serialize_json(self).as_bytes().to_vec() 20 | } 21 | } 22 | 23 | pub trait Deserializable { 24 | fn deserialize(data: &[u8]) -> Self; 25 | } 26 | #[cfg(feature = "serialize_bincode")] 27 | impl Deserializable for T { 28 | fn deserialize(data: &[u8]) -> Self { 29 | bincode::deserialize(data).unwrap() 30 | } 31 | } 32 | #[cfg(feature = "serialize_json")] 33 | impl Deserializable for T { 34 | fn deserialize(data: &[u8]) -> Self { 35 | serde_json::from_slice(data).unwrap() 36 | } 37 | } 38 | #[cfg(feature = "serialize_nanoserde_json")] 39 | impl Deserializable for T { 40 | fn deserialize(data: &[u8]) -> Self { 41 | nanoserde::DeJson::deserialize_json(std::str::from_utf8(data).unwrap()).unwrap() 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /host/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "addr2line" 5 | version = "0.14.1" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | checksum = "a55f82cfe485775d02112886f4169bde0c5894d75e79ead7eafe7e40a25e45f7" 8 | dependencies = [ 9 | "gimli 0.23.0", 10 | ] 11 | 12 | [[package]] 13 | name = "adler" 14 | version = "1.0.2" 15 | source = "registry+https://github.com/rust-lang/crates.io-index" 16 | checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" 17 | 18 | [[package]] 19 | name = "autocfg" 20 | version = "1.0.1" 21 | source = "registry+https://github.com/rust-lang/crates.io-index" 22 | checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" 23 | 24 | [[package]] 25 | name = "backtrace" 26 | version = "0.3.56" 27 | source = "registry+https://github.com/rust-lang/crates.io-index" 28 | checksum = "9d117600f438b1707d4e4ae15d3595657288f8235a0eb593e80ecc98ab34e1bc" 29 | dependencies = [ 30 | "addr2line", 31 | "cfg-if 1.0.0", 32 | "libc", 33 | "miniz_oxide", 34 | "object 0.23.0", 35 | "rustc-demangle", 36 | ] 37 | 38 | [[package]] 39 | name = "bincode" 40 | version = "1.3.2" 41 | source = "registry+https://github.com/rust-lang/crates.io-index" 42 | checksum = "d175dfa69e619905c4c3cdb7c3c203fa3bdd5d51184e3afdb2742c0280493772" 43 | dependencies = [ 44 | "byteorder", 45 | "serde", 46 | ] 47 | 48 | [[package]] 49 | name = "bitfield" 50 | version = "0.13.2" 51 | source = "registry+https://github.com/rust-lang/crates.io-index" 52 | checksum = "46afbd2983a5d5a7bd740ccb198caf5b82f45c40c09c0eed36052d91cb92e719" 53 | 54 | [[package]] 55 | name = "bitflags" 56 | version = "1.2.1" 57 | source = "registry+https://github.com/rust-lang/crates.io-index" 58 | checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" 59 | 60 | [[package]] 61 | name = "byteorder" 62 | version = "1.3.4" 63 | source = "registry+https://github.com/rust-lang/crates.io-index" 64 | checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" 65 | 66 | [[package]] 67 | name = "cc" 68 | version = "1.0.67" 69 | source = "registry+https://github.com/rust-lang/crates.io-index" 70 | checksum = "e3c69b077ad434294d3ce9f1f6143a2a4b89a8a2d54ef813d85003a4fd1137fd" 71 | 72 | [[package]] 73 | name = "cfg-if" 74 | version = "0.1.10" 75 | source = "registry+https://github.com/rust-lang/crates.io-index" 76 | checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" 77 | 78 | [[package]] 79 | name = "cfg-if" 80 | version = "1.0.0" 81 | source = "registry+https://github.com/rust-lang/crates.io-index" 82 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 83 | 84 | [[package]] 85 | name = "cranelift-bforest" 86 | version = "0.68.0" 87 | source = "registry+https://github.com/rust-lang/crates.io-index" 88 | checksum = "9221545c0507dc08a62b2d8b5ffe8e17ac580b0a74d1813b496b8d70b070fbd0" 89 | dependencies = [ 90 | "cranelift-entity", 91 | ] 92 | 93 | [[package]] 94 | name = "cranelift-codegen" 95 | version = "0.68.0" 96 | source = "registry+https://github.com/rust-lang/crates.io-index" 97 | checksum = "7e9936ea608b6cd176f107037f6adbb4deac933466fc7231154f96598b2d3ab1" 98 | dependencies = [ 99 | "byteorder", 100 | "cranelift-bforest", 101 | "cranelift-codegen-meta", 102 | "cranelift-codegen-shared", 103 | "cranelift-entity", 104 | "gimli 0.22.0", 105 | "log", 106 | "regalloc", 107 | "smallvec", 108 | "target-lexicon", 109 | "thiserror", 110 | ] 111 | 112 | [[package]] 113 | name = "cranelift-codegen-meta" 114 | version = "0.68.0" 115 | source = "registry+https://github.com/rust-lang/crates.io-index" 116 | checksum = "4ef2b2768568306540f4c8db3acce9105534d34c4a1e440529c1e702d7f8c8d7" 117 | dependencies = [ 118 | "cranelift-codegen-shared", 119 | "cranelift-entity", 120 | ] 121 | 122 | [[package]] 123 | name = "cranelift-codegen-shared" 124 | version = "0.68.0" 125 | source = "registry+https://github.com/rust-lang/crates.io-index" 126 | checksum = "6759012d6d19c4caec95793f052613e9d4113e925e7f14154defbac0f1d4c938" 127 | 128 | [[package]] 129 | name = "cranelift-entity" 130 | version = "0.68.0" 131 | source = "registry+https://github.com/rust-lang/crates.io-index" 132 | checksum = "86badbce14e15f52a45b666b38abe47b204969dd7f8fb7488cb55dd46b361fa6" 133 | dependencies = [ 134 | "serde", 135 | ] 136 | 137 | [[package]] 138 | name = "cranelift-frontend" 139 | version = "0.68.0" 140 | source = "registry+https://github.com/rust-lang/crates.io-index" 141 | checksum = "b608bb7656c554d0a4cf8f50c7a10b857e80306f6ff829ad6d468a7e2323c8d8" 142 | dependencies = [ 143 | "cranelift-codegen", 144 | "log", 145 | "smallvec", 146 | "target-lexicon", 147 | ] 148 | 149 | [[package]] 150 | name = "crc32fast" 151 | version = "1.2.1" 152 | source = "registry+https://github.com/rust-lang/crates.io-index" 153 | checksum = "81156fece84ab6a9f2afdb109ce3ae577e42b1228441eded99bd77f627953b1a" 154 | dependencies = [ 155 | "cfg-if 1.0.0", 156 | ] 157 | 158 | [[package]] 159 | name = "crossbeam-channel" 160 | version = "0.5.0" 161 | source = "registry+https://github.com/rust-lang/crates.io-index" 162 | checksum = "dca26ee1f8d361640700bde38b2c37d8c22b3ce2d360e1fc1c74ea4b0aa7d775" 163 | dependencies = [ 164 | "cfg-if 1.0.0", 165 | "crossbeam-utils", 166 | ] 167 | 168 | [[package]] 169 | name = "crossbeam-deque" 170 | version = "0.8.0" 171 | source = "registry+https://github.com/rust-lang/crates.io-index" 172 | checksum = "94af6efb46fef72616855b036a624cf27ba656ffc9be1b9a3c931cfc7749a9a9" 173 | dependencies = [ 174 | "cfg-if 1.0.0", 175 | "crossbeam-epoch", 176 | "crossbeam-utils", 177 | ] 178 | 179 | [[package]] 180 | name = "crossbeam-epoch" 181 | version = "0.9.3" 182 | source = "registry+https://github.com/rust-lang/crates.io-index" 183 | checksum = "2584f639eb95fea8c798496315b297cf81b9b58b6d30ab066a75455333cf4b12" 184 | dependencies = [ 185 | "cfg-if 1.0.0", 186 | "crossbeam-utils", 187 | "lazy_static", 188 | "memoffset", 189 | "scopeguard", 190 | ] 191 | 192 | [[package]] 193 | name = "crossbeam-utils" 194 | version = "0.8.3" 195 | source = "registry+https://github.com/rust-lang/crates.io-index" 196 | checksum = "e7e9d99fa91428effe99c5c6d4634cdeba32b8cf784fc428a2a687f61a952c49" 197 | dependencies = [ 198 | "autocfg", 199 | "cfg-if 1.0.0", 200 | "lazy_static", 201 | ] 202 | 203 | [[package]] 204 | name = "darling" 205 | version = "0.12.2" 206 | source = "registry+https://github.com/rust-lang/crates.io-index" 207 | checksum = "a06d4a9551359071d1890820e3571252b91229e0712e7c36b08940e603c5a8fc" 208 | dependencies = [ 209 | "darling_core", 210 | "darling_macro", 211 | ] 212 | 213 | [[package]] 214 | name = "darling_core" 215 | version = "0.12.2" 216 | source = "registry+https://github.com/rust-lang/crates.io-index" 217 | checksum = "b443e5fb0ddd56e0c9bfa47dc060c5306ee500cb731f2b91432dd65589a77684" 218 | dependencies = [ 219 | "fnv", 220 | "ident_case", 221 | "proc-macro2", 222 | "quote", 223 | "strsim", 224 | "syn", 225 | ] 226 | 227 | [[package]] 228 | name = "darling_macro" 229 | version = "0.12.2" 230 | source = "registry+https://github.com/rust-lang/crates.io-index" 231 | checksum = "c0220073ce504f12a70efc4e7cdaea9e9b1b324872e7ad96a208056d7a638b81" 232 | dependencies = [ 233 | "darling_core", 234 | "quote", 235 | "syn", 236 | ] 237 | 238 | [[package]] 239 | name = "either" 240 | version = "1.6.1" 241 | source = "registry+https://github.com/rust-lang/crates.io-index" 242 | checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" 243 | 244 | [[package]] 245 | name = "enumset" 246 | version = "1.0.6" 247 | source = "registry+https://github.com/rust-lang/crates.io-index" 248 | checksum = "fbd795df6708a599abf1ee10eacc72efd052b7a5f70fdf0715e4d5151a6db9c3" 249 | dependencies = [ 250 | "enumset_derive", 251 | ] 252 | 253 | [[package]] 254 | name = "enumset_derive" 255 | version = "0.5.4" 256 | source = "registry+https://github.com/rust-lang/crates.io-index" 257 | checksum = "e19c52f9ec503c8a68dc04daf71a04b07e690c32ab1a8b68e33897f255269d47" 258 | dependencies = [ 259 | "darling", 260 | "proc-macro2", 261 | "quote", 262 | "syn", 263 | ] 264 | 265 | [[package]] 266 | name = "fallible-iterator" 267 | version = "0.2.0" 268 | source = "registry+https://github.com/rust-lang/crates.io-index" 269 | checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" 270 | 271 | [[package]] 272 | name = "fnv" 273 | version = "1.0.7" 274 | source = "registry+https://github.com/rust-lang/crates.io-index" 275 | checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" 276 | 277 | [[package]] 278 | name = "getrandom" 279 | version = "0.2.2" 280 | source = "registry+https://github.com/rust-lang/crates.io-index" 281 | checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" 282 | dependencies = [ 283 | "cfg-if 1.0.0", 284 | "libc", 285 | "wasi", 286 | ] 287 | 288 | [[package]] 289 | name = "gimli" 290 | version = "0.22.0" 291 | source = "registry+https://github.com/rust-lang/crates.io-index" 292 | checksum = "aaf91faf136cb47367fa430cd46e37a788775e7fa104f8b4bcb3861dc389b724" 293 | dependencies = [ 294 | "fallible-iterator", 295 | "indexmap", 296 | "stable_deref_trait", 297 | ] 298 | 299 | [[package]] 300 | name = "gimli" 301 | version = "0.23.0" 302 | source = "registry+https://github.com/rust-lang/crates.io-index" 303 | checksum = "f6503fe142514ca4799d4c26297c4248239fe8838d827db6bd6065c6ed29a6ce" 304 | 305 | [[package]] 306 | name = "hashbrown" 307 | version = "0.9.1" 308 | source = "registry+https://github.com/rust-lang/crates.io-index" 309 | checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04" 310 | 311 | [[package]] 312 | name = "hermit-abi" 313 | version = "0.1.18" 314 | source = "registry+https://github.com/rust-lang/crates.io-index" 315 | checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" 316 | dependencies = [ 317 | "libc", 318 | ] 319 | 320 | [[package]] 321 | name = "ident_case" 322 | version = "1.0.1" 323 | source = "registry+https://github.com/rust-lang/crates.io-index" 324 | checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" 325 | 326 | [[package]] 327 | name = "indexmap" 328 | version = "1.6.1" 329 | source = "registry+https://github.com/rust-lang/crates.io-index" 330 | checksum = "4fb1fa934250de4de8aef298d81c729a7d33d8c239daa3a7575e6b92bfc7313b" 331 | dependencies = [ 332 | "autocfg", 333 | "hashbrown", 334 | "serde", 335 | ] 336 | 337 | [[package]] 338 | name = "itoa" 339 | version = "0.4.7" 340 | source = "registry+https://github.com/rust-lang/crates.io-index" 341 | checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" 342 | 343 | [[package]] 344 | name = "lazy_static" 345 | version = "1.4.0" 346 | source = "registry+https://github.com/rust-lang/crates.io-index" 347 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" 348 | 349 | [[package]] 350 | name = "leb128" 351 | version = "0.2.4" 352 | source = "registry+https://github.com/rust-lang/crates.io-index" 353 | checksum = "3576a87f2ba00f6f106fdfcd16db1d698d648a26ad8e0573cad8537c3c362d2a" 354 | 355 | [[package]] 356 | name = "libc" 357 | version = "0.2.86" 358 | source = "registry+https://github.com/rust-lang/crates.io-index" 359 | checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" 360 | 361 | [[package]] 362 | name = "libloading" 363 | version = "0.6.7" 364 | source = "registry+https://github.com/rust-lang/crates.io-index" 365 | checksum = "351a32417a12d5f7e82c368a66781e307834dae04c6ce0cd4456d52989229883" 366 | dependencies = [ 367 | "cfg-if 1.0.0", 368 | "winapi", 369 | ] 370 | 371 | [[package]] 372 | name = "log" 373 | version = "0.4.14" 374 | source = "registry+https://github.com/rust-lang/crates.io-index" 375 | checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" 376 | dependencies = [ 377 | "cfg-if 1.0.0", 378 | ] 379 | 380 | [[package]] 381 | name = "mach" 382 | version = "0.3.2" 383 | source = "registry+https://github.com/rust-lang/crates.io-index" 384 | checksum = "b823e83b2affd8f40a9ee8c29dbc56404c1e34cd2710921f2801e2cf29527afa" 385 | dependencies = [ 386 | "libc", 387 | ] 388 | 389 | [[package]] 390 | name = "memmap2" 391 | version = "0.2.1" 392 | source = "registry+https://github.com/rust-lang/crates.io-index" 393 | checksum = "04e3e85b970d650e2ae6d70592474087051c11c54da7f7b4949725c5735fbcc6" 394 | dependencies = [ 395 | "libc", 396 | ] 397 | 398 | [[package]] 399 | name = "memoffset" 400 | version = "0.6.1" 401 | source = "registry+https://github.com/rust-lang/crates.io-index" 402 | checksum = "157b4208e3059a8f9e78d559edc658e13df41410cb3ae03979c83130067fdd87" 403 | dependencies = [ 404 | "autocfg", 405 | ] 406 | 407 | [[package]] 408 | name = "miniz_oxide" 409 | version = "0.4.4" 410 | source = "registry+https://github.com/rust-lang/crates.io-index" 411 | checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" 412 | dependencies = [ 413 | "adler", 414 | "autocfg", 415 | ] 416 | 417 | [[package]] 418 | name = "more-asserts" 419 | version = "0.2.1" 420 | source = "registry+https://github.com/rust-lang/crates.io-index" 421 | checksum = "0debeb9fcf88823ea64d64e4a815ab1643f33127d995978e099942ce38f25238" 422 | 423 | [[package]] 424 | name = "nanoserde" 425 | version = "0.1.25" 426 | source = "registry+https://github.com/rust-lang/crates.io-index" 427 | checksum = "1ae5894adc81d64b16f75ce8a67c994a6aa4b6e1c99f2920d22e0e575755d7e9" 428 | dependencies = [ 429 | "nanoserde-derive", 430 | ] 431 | 432 | [[package]] 433 | name = "nanoserde-derive" 434 | version = "0.1.16" 435 | source = "registry+https://github.com/rust-lang/crates.io-index" 436 | checksum = "71c29c140bb2a43bdc554ca5a34ecfe678bb6e6c6a3dc8d6856852ba2c74f20f" 437 | 438 | [[package]] 439 | name = "num_cpus" 440 | version = "1.13.0" 441 | source = "registry+https://github.com/rust-lang/crates.io-index" 442 | checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" 443 | dependencies = [ 444 | "hermit-abi", 445 | "libc", 446 | ] 447 | 448 | [[package]] 449 | name = "object" 450 | version = "0.22.0" 451 | source = "registry+https://github.com/rust-lang/crates.io-index" 452 | checksum = "8d3b63360ec3cb337817c2dbd47ab4a0f170d285d8e5a2064600f3def1402397" 453 | dependencies = [ 454 | "crc32fast", 455 | "indexmap", 456 | ] 457 | 458 | [[package]] 459 | name = "object" 460 | version = "0.23.0" 461 | source = "registry+https://github.com/rust-lang/crates.io-index" 462 | checksum = "a9a7ab5d64814df0fe4a4b5ead45ed6c5f181ee3ff04ba344313a6c80446c5d4" 463 | 464 | [[package]] 465 | name = "pin-project-lite" 466 | version = "0.2.4" 467 | source = "registry+https://github.com/rust-lang/crates.io-index" 468 | checksum = "439697af366c49a6d0a010c56a0d97685bc140ce0d377b13a2ea2aa42d64a827" 469 | 470 | [[package]] 471 | name = "ppv-lite86" 472 | version = "0.2.10" 473 | source = "registry+https://github.com/rust-lang/crates.io-index" 474 | checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" 475 | 476 | [[package]] 477 | name = "proc-macro-error" 478 | version = "1.0.4" 479 | source = "registry+https://github.com/rust-lang/crates.io-index" 480 | checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" 481 | dependencies = [ 482 | "proc-macro-error-attr", 483 | "proc-macro2", 484 | "quote", 485 | "syn", 486 | "version_check", 487 | ] 488 | 489 | [[package]] 490 | name = "proc-macro-error-attr" 491 | version = "1.0.4" 492 | source = "registry+https://github.com/rust-lang/crates.io-index" 493 | checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" 494 | dependencies = [ 495 | "proc-macro2", 496 | "quote", 497 | "version_check", 498 | ] 499 | 500 | [[package]] 501 | name = "proc-macro2" 502 | version = "1.0.24" 503 | source = "registry+https://github.com/rust-lang/crates.io-index" 504 | checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" 505 | dependencies = [ 506 | "unicode-xid", 507 | ] 508 | 509 | [[package]] 510 | name = "quote" 511 | version = "1.0.9" 512 | source = "registry+https://github.com/rust-lang/crates.io-index" 513 | checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" 514 | dependencies = [ 515 | "proc-macro2", 516 | ] 517 | 518 | [[package]] 519 | name = "rand" 520 | version = "0.8.3" 521 | source = "registry+https://github.com/rust-lang/crates.io-index" 522 | checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" 523 | dependencies = [ 524 | "libc", 525 | "rand_chacha", 526 | "rand_core", 527 | "rand_hc", 528 | ] 529 | 530 | [[package]] 531 | name = "rand_chacha" 532 | version = "0.3.0" 533 | source = "registry+https://github.com/rust-lang/crates.io-index" 534 | checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" 535 | dependencies = [ 536 | "ppv-lite86", 537 | "rand_core", 538 | ] 539 | 540 | [[package]] 541 | name = "rand_core" 542 | version = "0.6.2" 543 | source = "registry+https://github.com/rust-lang/crates.io-index" 544 | checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" 545 | dependencies = [ 546 | "getrandom", 547 | ] 548 | 549 | [[package]] 550 | name = "rand_hc" 551 | version = "0.3.0" 552 | source = "registry+https://github.com/rust-lang/crates.io-index" 553 | checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" 554 | dependencies = [ 555 | "rand_core", 556 | ] 557 | 558 | [[package]] 559 | name = "rayon" 560 | version = "1.5.0" 561 | source = "registry+https://github.com/rust-lang/crates.io-index" 562 | checksum = "8b0d8e0819fadc20c74ea8373106ead0600e3a67ef1fe8da56e39b9ae7275674" 563 | dependencies = [ 564 | "autocfg", 565 | "crossbeam-deque", 566 | "either", 567 | "rayon-core", 568 | ] 569 | 570 | [[package]] 571 | name = "rayon-core" 572 | version = "1.9.0" 573 | source = "registry+https://github.com/rust-lang/crates.io-index" 574 | checksum = "9ab346ac5921dc62ffa9f89b7a773907511cdfa5490c572ae9be1be33e8afa4a" 575 | dependencies = [ 576 | "crossbeam-channel", 577 | "crossbeam-deque", 578 | "crossbeam-utils", 579 | "lazy_static", 580 | "num_cpus", 581 | ] 582 | 583 | [[package]] 584 | name = "redox_syscall" 585 | version = "0.2.5" 586 | source = "registry+https://github.com/rust-lang/crates.io-index" 587 | checksum = "94341e4e44e24f6b591b59e47a8a027df12e008d73fd5672dbea9cc22f4507d9" 588 | dependencies = [ 589 | "bitflags", 590 | ] 591 | 592 | [[package]] 593 | name = "regalloc" 594 | version = "0.0.31" 595 | source = "registry+https://github.com/rust-lang/crates.io-index" 596 | checksum = "571f7f397d61c4755285cd37853fe8e03271c243424a907415909379659381c5" 597 | dependencies = [ 598 | "log", 599 | "rustc-hash", 600 | "smallvec", 601 | ] 602 | 603 | [[package]] 604 | name = "region" 605 | version = "2.2.0" 606 | source = "registry+https://github.com/rust-lang/crates.io-index" 607 | checksum = "877e54ea2adcd70d80e9179344c97f93ef0dffd6b03e1f4529e6e83ab2fa9ae0" 608 | dependencies = [ 609 | "bitflags", 610 | "libc", 611 | "mach", 612 | "winapi", 613 | ] 614 | 615 | [[package]] 616 | name = "remove_dir_all" 617 | version = "0.5.3" 618 | source = "registry+https://github.com/rust-lang/crates.io-index" 619 | checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" 620 | dependencies = [ 621 | "winapi", 622 | ] 623 | 624 | [[package]] 625 | name = "rustc-demangle" 626 | version = "0.1.18" 627 | source = "registry+https://github.com/rust-lang/crates.io-index" 628 | checksum = "6e3bad0ee36814ca07d7968269dd4b7ec89ec2da10c4bb613928d3077083c232" 629 | 630 | [[package]] 631 | name = "rustc-hash" 632 | version = "1.1.0" 633 | source = "registry+https://github.com/rust-lang/crates.io-index" 634 | checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" 635 | 636 | [[package]] 637 | name = "ryu" 638 | version = "1.0.5" 639 | source = "registry+https://github.com/rust-lang/crates.io-index" 640 | checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" 641 | 642 | [[package]] 643 | name = "scopeguard" 644 | version = "1.1.0" 645 | source = "registry+https://github.com/rust-lang/crates.io-index" 646 | checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" 647 | 648 | [[package]] 649 | name = "serde" 650 | version = "1.0.123" 651 | source = "registry+https://github.com/rust-lang/crates.io-index" 652 | checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae" 653 | dependencies = [ 654 | "serde_derive", 655 | ] 656 | 657 | [[package]] 658 | name = "serde_bytes" 659 | version = "0.11.5" 660 | source = "registry+https://github.com/rust-lang/crates.io-index" 661 | checksum = "16ae07dd2f88a366f15bd0632ba725227018c69a1c8550a927324f8eb8368bb9" 662 | dependencies = [ 663 | "serde", 664 | ] 665 | 666 | [[package]] 667 | name = "serde_derive" 668 | version = "1.0.123" 669 | source = "registry+https://github.com/rust-lang/crates.io-index" 670 | checksum = "9391c295d64fc0abb2c556bad848f33cb8296276b1ad2677d1ae1ace4f258f31" 671 | dependencies = [ 672 | "proc-macro2", 673 | "quote", 674 | "syn", 675 | ] 676 | 677 | [[package]] 678 | name = "serde_json" 679 | version = "1.0.64" 680 | source = "registry+https://github.com/rust-lang/crates.io-index" 681 | checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79" 682 | dependencies = [ 683 | "itoa", 684 | "ryu", 685 | "serde", 686 | ] 687 | 688 | [[package]] 689 | name = "smallvec" 690 | version = "1.6.1" 691 | source = "registry+https://github.com/rust-lang/crates.io-index" 692 | checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" 693 | 694 | [[package]] 695 | name = "stable_deref_trait" 696 | version = "1.2.0" 697 | source = "registry+https://github.com/rust-lang/crates.io-index" 698 | checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" 699 | 700 | [[package]] 701 | name = "strsim" 702 | version = "0.10.0" 703 | source = "registry+https://github.com/rust-lang/crates.io-index" 704 | checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" 705 | 706 | [[package]] 707 | name = "syn" 708 | version = "1.0.60" 709 | source = "registry+https://github.com/rust-lang/crates.io-index" 710 | checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081" 711 | dependencies = [ 712 | "proc-macro2", 713 | "quote", 714 | "unicode-xid", 715 | ] 716 | 717 | [[package]] 718 | name = "target-lexicon" 719 | version = "0.11.2" 720 | source = "registry+https://github.com/rust-lang/crates.io-index" 721 | checksum = "422045212ea98508ae3d28025bc5aaa2bd4a9cdaecd442a08da2ee620ee9ea95" 722 | 723 | [[package]] 724 | name = "tempfile" 725 | version = "3.2.0" 726 | source = "registry+https://github.com/rust-lang/crates.io-index" 727 | checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22" 728 | dependencies = [ 729 | "cfg-if 1.0.0", 730 | "libc", 731 | "rand", 732 | "redox_syscall", 733 | "remove_dir_all", 734 | "winapi", 735 | ] 736 | 737 | [[package]] 738 | name = "thiserror" 739 | version = "1.0.24" 740 | source = "registry+https://github.com/rust-lang/crates.io-index" 741 | checksum = "e0f4a65597094d4483ddaed134f409b2cb7c1beccf25201a9f73c719254fa98e" 742 | dependencies = [ 743 | "thiserror-impl", 744 | ] 745 | 746 | [[package]] 747 | name = "thiserror-impl" 748 | version = "1.0.24" 749 | source = "registry+https://github.com/rust-lang/crates.io-index" 750 | checksum = "7765189610d8241a44529806d6fd1f2e0a08734313a35d5b3a556f92b381f3c0" 751 | dependencies = [ 752 | "proc-macro2", 753 | "quote", 754 | "syn", 755 | ] 756 | 757 | [[package]] 758 | name = "tracing" 759 | version = "0.1.25" 760 | source = "registry+https://github.com/rust-lang/crates.io-index" 761 | checksum = "01ebdc2bb4498ab1ab5f5b73c5803825e60199229ccba0698170e3be0e7f959f" 762 | dependencies = [ 763 | "cfg-if 1.0.0", 764 | "pin-project-lite", 765 | "tracing-attributes", 766 | "tracing-core", 767 | ] 768 | 769 | [[package]] 770 | name = "tracing-attributes" 771 | version = "0.1.13" 772 | source = "registry+https://github.com/rust-lang/crates.io-index" 773 | checksum = "a8a9bd1db7706f2373a190b0d067146caa39350c486f3d455b0e33b431f94c07" 774 | dependencies = [ 775 | "proc-macro2", 776 | "quote", 777 | "syn", 778 | ] 779 | 780 | [[package]] 781 | name = "tracing-core" 782 | version = "0.1.17" 783 | source = "registry+https://github.com/rust-lang/crates.io-index" 784 | checksum = "f50de3927f93d202783f4513cda820ab47ef17f624b03c096e86ef00c67e6b5f" 785 | dependencies = [ 786 | "lazy_static", 787 | ] 788 | 789 | [[package]] 790 | name = "unicode-xid" 791 | version = "0.2.1" 792 | source = "registry+https://github.com/rust-lang/crates.io-index" 793 | checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" 794 | 795 | [[package]] 796 | name = "version_check" 797 | version = "0.9.2" 798 | source = "registry+https://github.com/rust-lang/crates.io-index" 799 | checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" 800 | 801 | [[package]] 802 | name = "wasi" 803 | version = "0.10.2+wasi-snapshot-preview1" 804 | source = "registry+https://github.com/rust-lang/crates.io-index" 805 | checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" 806 | 807 | [[package]] 808 | name = "wasm_plugin_host" 809 | version = "0.1.6" 810 | dependencies = [ 811 | "bincode", 812 | "bitfield", 813 | "getrandom", 814 | "nanoserde", 815 | "serde", 816 | "serde_json", 817 | "wasmer", 818 | ] 819 | 820 | [[package]] 821 | name = "wasmer" 822 | version = "1.0.2" 823 | source = "registry+https://github.com/rust-lang/crates.io-index" 824 | checksum = "a70cfae554988d904d64ca17ab0e7cd652ee5c8a0807094819c1ea93eb9d6866" 825 | dependencies = [ 826 | "cfg-if 0.1.10", 827 | "indexmap", 828 | "more-asserts", 829 | "target-lexicon", 830 | "thiserror", 831 | "wasmer-compiler", 832 | "wasmer-compiler-cranelift", 833 | "wasmer-derive", 834 | "wasmer-engine", 835 | "wasmer-engine-jit", 836 | "wasmer-engine-native", 837 | "wasmer-types", 838 | "wasmer-vm", 839 | "wat", 840 | "winapi", 841 | ] 842 | 843 | [[package]] 844 | name = "wasmer-compiler" 845 | version = "1.0.2" 846 | source = "registry+https://github.com/rust-lang/crates.io-index" 847 | checksum = "6b7732a9cab472bd921d5a0c422f45b3d03f62fa2c40a89e0770cef6d47e383e" 848 | dependencies = [ 849 | "enumset", 850 | "serde", 851 | "serde_bytes", 852 | "smallvec", 853 | "target-lexicon", 854 | "thiserror", 855 | "wasmer-types", 856 | "wasmer-vm", 857 | "wasmparser", 858 | ] 859 | 860 | [[package]] 861 | name = "wasmer-compiler-cranelift" 862 | version = "1.0.2" 863 | source = "registry+https://github.com/rust-lang/crates.io-index" 864 | checksum = "48cb9395f094e1d81534f4c5e330ed4cdb424e8df870d29ad585620284f5fddb" 865 | dependencies = [ 866 | "cranelift-codegen", 867 | "cranelift-frontend", 868 | "gimli 0.22.0", 869 | "more-asserts", 870 | "rayon", 871 | "serde", 872 | "smallvec", 873 | "tracing", 874 | "wasmer-compiler", 875 | "wasmer-types", 876 | "wasmer-vm", 877 | ] 878 | 879 | [[package]] 880 | name = "wasmer-derive" 881 | version = "1.0.2" 882 | source = "registry+https://github.com/rust-lang/crates.io-index" 883 | checksum = "d8b86dcd2c3efdb8390728a2b56f762db07789aaa5aa872a9dc776ba3a7912ed" 884 | dependencies = [ 885 | "proc-macro-error", 886 | "proc-macro2", 887 | "quote", 888 | "syn", 889 | ] 890 | 891 | [[package]] 892 | name = "wasmer-engine" 893 | version = "1.0.2" 894 | source = "registry+https://github.com/rust-lang/crates.io-index" 895 | checksum = "efe4667d6bd888f26ae8062a63a9379fa697415b4b4e380f33832e8418fd71b5" 896 | dependencies = [ 897 | "backtrace", 898 | "bincode", 899 | "lazy_static", 900 | "memmap2", 901 | "more-asserts", 902 | "rustc-demangle", 903 | "serde", 904 | "serde_bytes", 905 | "target-lexicon", 906 | "thiserror", 907 | "wasmer-compiler", 908 | "wasmer-types", 909 | "wasmer-vm", 910 | ] 911 | 912 | [[package]] 913 | name = "wasmer-engine-jit" 914 | version = "1.0.2" 915 | source = "registry+https://github.com/rust-lang/crates.io-index" 916 | checksum = "26770be802888011b4a3072f2a282fc2faa68aa48c71b3db6252a3937a85f3da" 917 | dependencies = [ 918 | "bincode", 919 | "cfg-if 0.1.10", 920 | "region", 921 | "serde", 922 | "serde_bytes", 923 | "wasmer-compiler", 924 | "wasmer-engine", 925 | "wasmer-types", 926 | "wasmer-vm", 927 | "winapi", 928 | ] 929 | 930 | [[package]] 931 | name = "wasmer-engine-native" 932 | version = "1.0.2" 933 | source = "registry+https://github.com/rust-lang/crates.io-index" 934 | checksum = "2bb4083a6c69f2cd4b000b82a80717f37c6cc2e536aee3a8ffe9af3edc276a8b" 935 | dependencies = [ 936 | "bincode", 937 | "cfg-if 0.1.10", 938 | "leb128", 939 | "libloading", 940 | "serde", 941 | "tempfile", 942 | "tracing", 943 | "wasmer-compiler", 944 | "wasmer-engine", 945 | "wasmer-object", 946 | "wasmer-types", 947 | "wasmer-vm", 948 | "which", 949 | ] 950 | 951 | [[package]] 952 | name = "wasmer-object" 953 | version = "1.0.2" 954 | source = "registry+https://github.com/rust-lang/crates.io-index" 955 | checksum = "abf8e0c12b82ff81ebecd30d7e118be5fec871d6de885a90eeb105df0a769a7b" 956 | dependencies = [ 957 | "object 0.22.0", 958 | "thiserror", 959 | "wasmer-compiler", 960 | "wasmer-types", 961 | ] 962 | 963 | [[package]] 964 | name = "wasmer-types" 965 | version = "1.0.2" 966 | source = "registry+https://github.com/rust-lang/crates.io-index" 967 | checksum = "c7f4ac28c2951cd792c18332f03da523ed06b170f5cf6bb5b1bdd7e36c2a8218" 968 | dependencies = [ 969 | "cranelift-entity", 970 | "serde", 971 | "thiserror", 972 | ] 973 | 974 | [[package]] 975 | name = "wasmer-vm" 976 | version = "1.0.2" 977 | source = "registry+https://github.com/rust-lang/crates.io-index" 978 | checksum = "a7635ba0b6d2fd325f588d69a950ad9fa04dddbf6ad08b6b2a183146319bf6ae" 979 | dependencies = [ 980 | "backtrace", 981 | "cc", 982 | "cfg-if 0.1.10", 983 | "indexmap", 984 | "libc", 985 | "memoffset", 986 | "more-asserts", 987 | "region", 988 | "serde", 989 | "thiserror", 990 | "wasmer-types", 991 | "winapi", 992 | ] 993 | 994 | [[package]] 995 | name = "wasmparser" 996 | version = "0.65.0" 997 | source = "registry+https://github.com/rust-lang/crates.io-index" 998 | checksum = "87cc2fe6350834b4e528ba0901e7aa405d78b89dc1fa3145359eb4de0e323fcf" 999 | 1000 | [[package]] 1001 | name = "wast" 1002 | version = "35.0.0" 1003 | source = "registry+https://github.com/rust-lang/crates.io-index" 1004 | checksum = "db5ae96da18bb5926341516fd409b5a8ce4e4714da7f0a1063d3b20ac9f9a1e1" 1005 | dependencies = [ 1006 | "leb128", 1007 | ] 1008 | 1009 | [[package]] 1010 | name = "wat" 1011 | version = "1.0.36" 1012 | source = "registry+https://github.com/rust-lang/crates.io-index" 1013 | checksum = "0b0fa059022c5dabe129f02b429d67086400deb8277f89c975555dacc1dadbcc" 1014 | dependencies = [ 1015 | "wast", 1016 | ] 1017 | 1018 | [[package]] 1019 | name = "which" 1020 | version = "4.0.2" 1021 | source = "registry+https://github.com/rust-lang/crates.io-index" 1022 | checksum = "87c14ef7e1b8b8ecfc75d5eca37949410046e66f15d185c01d70824f1f8111ef" 1023 | dependencies = [ 1024 | "libc", 1025 | "thiserror", 1026 | ] 1027 | 1028 | [[package]] 1029 | name = "winapi" 1030 | version = "0.3.9" 1031 | source = "registry+https://github.com/rust-lang/crates.io-index" 1032 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 1033 | dependencies = [ 1034 | "winapi-i686-pc-windows-gnu", 1035 | "winapi-x86_64-pc-windows-gnu", 1036 | ] 1037 | 1038 | [[package]] 1039 | name = "winapi-i686-pc-windows-gnu" 1040 | version = "0.4.0" 1041 | source = "registry+https://github.com/rust-lang/crates.io-index" 1042 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 1043 | 1044 | [[package]] 1045 | name = "winapi-x86_64-pc-windows-gnu" 1046 | version = "0.4.0" 1047 | source = "registry+https://github.com/rust-lang/crates.io-index" 1048 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 1049 | -------------------------------------------------------------------------------- /host/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "wasm_plugin_host" 3 | version = "0.1.7" 4 | authors = ["Alec Deason "] 5 | edition = "2018" 6 | license = "MIT" 7 | readme = "README.md" 8 | repository = "https://github.com/alec-deason/wasm_plugin/tree/main/host" 9 | description = "A low-ish level tool for easily hosting WASM based plugins" 10 | keywords = ["WASM", "plugin"] 11 | categories = ["game-development", "wasm"] 12 | 13 | [features] 14 | default = ["inject_getrandom", "serialize_bincode"] 15 | inject_getrandom = ["getrandom"] 16 | serialize_bincode = ["bincode", "serde"] 17 | serialize_json = ["serde_json", "serde"] 18 | serialize_nanoserde_json = ["nanoserde"] 19 | 20 | [dependencies] 21 | wasmer = "1" 22 | getrandom = { version = "0.2", optional = true } 23 | serde = { version = "1", optional = true } 24 | bincode = { version = "1", optional = true } 25 | serde_json = { version = "1", optional = true } 26 | nanoserde = { version = "0.1", optional = true } 27 | bitfield = "0.13.2" 28 | -------------------------------------------------------------------------------- /host/README.md: -------------------------------------------------------------------------------- 1 | #OBSOLETE# 2 | These crates are no longer maintained and the use case is better served by the [WebAssembly Component specification](https://component-model.bytecodealliance.org/language-support/rust.html). 3 | 4 | [![Crates.io](https://img.shields.io/crates/v/wasm_plugin_host.svg)](https://crates.io/crates/wasm_plugin_host) 5 | [![Docs.rs](https://docs.rs/wasm_plugin_host/badge.svg)](https://docs.rs/wasm_plugin_host) 6 | [![license](https://img.shields.io/badge/license-MIT-blue.svg)](../LICENSE) 7 | 8 | 9 | A low-ish level tool for easily hosting WASM based plugins. 10 | 11 | 12 | The goal of wasm_plugin is to make communicating across the host-plugin 13 | boundary as simple and idiomatic as possible while being unopinionated 14 | about how you actually use the plugin. 15 | 16 | Plugins should be written using [wasm_plugin_guest](https://crates.io/crates/wasm_plugin_guest) 17 | 18 | Loading a plugin is as simple as reading the .wasm file off disk. 19 | 20 | ```rust 21 | let mut plugin = WasmPluginBuilder::from_file("path/to/plugin.wasm")?.finish()?; 22 | ``` 23 | 24 | Calling functions exported by the plugin takes one of two forms. Either 25 | the function takes no arguments and returns a single serde deserializable 26 | value: 27 | 28 | ```rust 29 | let response: ResultType = plugin.call_function("function_name")?; 30 | ``` 31 | 32 | Or it takes a single serializable argument and returns a single result: 33 | 34 | ```rust 35 | let message = Message::default(); 36 | let response: ResultType = plugin.call_function_with_argument("function_name", &message)?; 37 | ``` 38 | 39 | ## API Stability 40 | 41 | I am not currently guaranteeing any stability, expect all releases to include breaking changes. 42 | -------------------------------------------------------------------------------- /host/src/errors.rs: -------------------------------------------------------------------------------- 1 | /// Error returned by WasmPlugin when loading plugins or calling functions. 2 | pub enum WasmPluginError { 3 | /// A problem compiling the plugin's WASM source 4 | WasmerCompileError(wasmer::CompileError), 5 | /// A problem instantiating the Wasmer runtime 6 | WasmerInstantiationError(wasmer::InstantiationError), 7 | /// A problem interacting with the plugin 8 | WasmerRuntimeError(wasmer::RuntimeError), 9 | /// A problem getting an export from the plugin 10 | WasmerExportError(wasmer::ExportError), 11 | /// A problem loading the plugin's source from disk 12 | IoError(std::io::Error), 13 | /// A problems serializing an argument to send to one of the plugin's 14 | /// functions. 15 | SerializationError, 16 | /// A problem deserializing the return value of a call to one of the 17 | /// plugin's functions. This almost always represents a type mismatch 18 | /// between the callsite in the host and the function signature in the 19 | /// plugin. 20 | DeserializationError, 21 | /// A problem decoding the utf8 sent by the plugin 22 | #[cfg(feature = "serialize_nanoserde_json")] 23 | FromUtf8Error(std::string::FromUtf8Error), 24 | } 25 | 26 | impl std::error::Error for WasmPluginError {} 27 | 28 | impl core::fmt::Debug for WasmPluginError { 29 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 30 | core::fmt::Display::fmt(self, f) 31 | } 32 | } 33 | 34 | impl core::fmt::Display for WasmPluginError { 35 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 36 | match self { 37 | WasmPluginError::WasmerCompileError(e) => e.fmt(f), 38 | WasmPluginError::WasmerInstantiationError(e) => e.fmt(f), 39 | WasmPluginError::WasmerRuntimeError(e) => e.fmt(f), 40 | WasmPluginError::WasmerExportError(e) => e.fmt(f), 41 | WasmPluginError::IoError(e) => e.fmt(f), 42 | 43 | WasmPluginError::SerializationError => write!(f, "There was a problem serializing the argument to the function call"), 44 | WasmPluginError::DeserializationError=> write!(f, "There was a problem deserializing the value returned by the plugin function. This almost certainly means that the type at the call site does not match the type in the plugin's function signature."), 45 | #[cfg(feature = "serialize_nanoserde_json")] 46 | WasmPluginError::FromUtf8Error(e) => e.fmt(f), 47 | } 48 | } 49 | } 50 | 51 | impl From for WasmPluginError { 52 | fn from(e: std::io::Error) -> WasmPluginError { 53 | WasmPluginError::IoError(e) 54 | } 55 | } 56 | 57 | impl From for WasmPluginError { 58 | fn from(e: wasmer::CompileError) -> WasmPluginError { 59 | WasmPluginError::WasmerCompileError(e) 60 | } 61 | } 62 | 63 | impl From for WasmPluginError { 64 | fn from(e: wasmer::InstantiationError) -> WasmPluginError { 65 | WasmPluginError::WasmerInstantiationError(e) 66 | } 67 | } 68 | 69 | impl From for WasmPluginError { 70 | fn from(e: wasmer::RuntimeError) -> WasmPluginError { 71 | WasmPluginError::WasmerRuntimeError(e) 72 | } 73 | } 74 | 75 | impl From for WasmPluginError { 76 | fn from(e: wasmer::ExportError) -> WasmPluginError { 77 | WasmPluginError::WasmerExportError(e) 78 | } 79 | } 80 | 81 | #[cfg(feature = "serialize_nanoserde_json")] 82 | impl From for WasmPluginError { 83 | fn from(e: std::string::FromUtf8Error) -> WasmPluginError { 84 | WasmPluginError::FromUtf8Error(e) 85 | } 86 | } 87 | 88 | pub type Result = std::result::Result; 89 | -------------------------------------------------------------------------------- /host/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![doc(html_root_url = "https://docs.rs/wasm_plugin_host/0.1.7")] 2 | #![deny(missing_docs)] 3 | 4 | //! A low-ish level tool for easily hosting WASM based plugins. 5 | //! 6 | //! The goal of wasm_plugin is to make communicating across the host-plugin 7 | //! boundary as simple and idiomatic as possible while being unopinionated 8 | //! about how you actually use the plugin. 9 | //! 10 | //! Plugins should be written using [wasm_plugin_guest](https://crates.io/crates/wasm_plugin_guest) 11 | //! 12 | //! Loading a plugin is as simple as reading the .wasm file off disk. 13 | //! 14 | //! ```rust 15 | //! # use std::error::Error; 16 | //! # 17 | //! # fn main() -> Result<(), Box> { 18 | //! let mut plugin = WasmPluginBuilder::from_file("path/to/plugin.wasm")?.finish()?; 19 | //! # 20 | //! # Ok(()) 21 | //! # } 22 | //! ``` 23 | //! 24 | //! Calling functions exported by the plugin takes one of two forms. Either 25 | //! the function takes no arguments and returns a single serde deserializable 26 | //! value: 27 | //! 28 | //! ```rust 29 | //! # #[derive(Deserialize)] 30 | //! # struct ResultType; 31 | //! # use std::error::Error; 32 | //! # 33 | //! # fn main() -> Result<(), Box> { 34 | //! # let mut plugin = WasmPluginBuilder::from_file("path/to/plugin.wasm")?.finish()?; 35 | //! let response: ResultType = plugin.call_function("function_name")?; 36 | //! # 37 | //! # Ok(()) 38 | //! # } 39 | //! ``` 40 | //! Or it takes a single serializable argument and returns a single result: 41 | //! ```rust 42 | //! # #[derive(Deserialize)] 43 | //! # struct ResultType; 44 | //! # #[derive(Serialize, Default)] 45 | //! # struct Message; 46 | //! # use std::error::Error; 47 | //! # 48 | //! # fn main() -> Result<(), Box> { 49 | //! # let mut plugin = WasmPluginBuilder::from_file("path/to/plugin.wasm")?.finish()?; 50 | //! let message = Message::default(); 51 | //! let response: ResultType = plugin.call_function_with_argument("function_name", &message)?; 52 | //! # 53 | //! # Ok(()) 54 | //! # } 55 | //! ``` 56 | //! If the `inject_getrandom` feature is selected then the host's getrandom 57 | //! will be injected into the plugin which allows `rand` to be used in the 58 | //! plugin. `inject_getrandom` is selected by default. 59 | //! 60 | //! Currently serialization uses either bincode or json, selected by feature: 61 | //! `serialize_bincode`: Uses serde and bincode. It is selected by default. 62 | //! `serialize_json`: Uses serde and serde_json. 63 | //! `serialize_nanoserde_json': Uses nanoserde. 64 | //! 65 | //! Bincode is likely the best choice if all plugins the system uses will be 66 | //! written in Rust. Json is useful if a mix of languages will be used. 67 | //! 68 | //! ## Limitations 69 | //! 70 | //! There is no reflection so you must know up front which functions 71 | //! a plugin exports and their signatures. 72 | 73 | use std::{ 74 | path::Path, 75 | sync::{Arc, Mutex}, 76 | }; 77 | 78 | use wasmer::{Exports, Function, Instance, LazyInit, Memory, MemoryView, Module, Store, WasmerEnv}; 79 | pub use wasmer::{Extern, HostFunction}; 80 | 81 | #[allow(missing_docs)] 82 | pub mod errors; 83 | #[allow(missing_docs)] 84 | pub mod serialization; 85 | use bitfield::bitfield; 86 | use serialization::{Deserializable, Serializable}; 87 | 88 | bitfield! { 89 | #[doc(hidden)] 90 | pub struct FatPointer(u64); 91 | impl Debug; 92 | u32; 93 | ptr, set_ptr: 31, 0; 94 | len, set_len: 63, 32; 95 | } 96 | 97 | #[derive(WasmerEnv, Clone)] 98 | struct Env 99 | where 100 | C: Send + Sync + Clone + 'static, 101 | { 102 | #[wasmer(export(name = "allocate_message_buffer"))] 103 | allocator: LazyInit, 104 | #[wasmer(export)] 105 | memory: LazyInit, 106 | garbage: Arc>>, 107 | ctx: C, 108 | } 109 | 110 | impl Env { 111 | fn new(garbage: Arc>>, ctx: C) -> Self { 112 | Self { 113 | allocator: Default::default(), 114 | memory: Default::default(), 115 | garbage, 116 | ctx, 117 | } 118 | } 119 | 120 | fn message_buffer(&self) -> MessageBuffer { 121 | unsafe { 122 | MessageBuffer { 123 | allocator: self.allocator.get_unchecked(), 124 | memory: self.memory.get_unchecked(), 125 | garbage: vec![], 126 | } 127 | } 128 | } 129 | } 130 | 131 | /// Constructs a WasmPlugin 132 | pub struct WasmPluginBuilder { 133 | module: Module, 134 | store: Store, 135 | env: Exports, 136 | // TODO: Can we do this without the lock? 137 | garbage: Arc>>, 138 | } 139 | impl WasmPluginBuilder { 140 | /// Load a plugin off disk and prepare it for use. 141 | pub fn from_file(path: impl AsRef) -> errors::Result { 142 | let source = std::fs::read(path)?; 143 | Self::from_source(&source) 144 | } 145 | 146 | /// Load a plugin from WASM source and prepare it for use. 147 | pub fn from_source(source: &[u8]) -> errors::Result { 148 | let store = Store::default(); 149 | let module = Module::new(&store, source)?; 150 | let mut env = wasmer::Exports::new(); 151 | let garbage: Arc>> = Default::default(); 152 | env.insert( 153 | "abort", 154 | Function::new_native(&store, |_: u32, _: u32, _: i32, _: i32| {}), 155 | ); 156 | #[cfg(feature = "inject_getrandom")] 157 | { 158 | env.insert( 159 | "__getrandom", 160 | Function::new_native_with_env( 161 | &store, 162 | Env::new(garbage.clone(), ()), 163 | getrandom_shim, 164 | ), 165 | ); 166 | } 167 | 168 | Ok(Self { 169 | module, 170 | store, 171 | env, 172 | garbage, 173 | }) 174 | } 175 | 176 | fn import(mut self, name: impl ToString, value: impl Into) -> Self { 177 | let name = format!("wasm_plugin_imported__{}", name.to_string()); 178 | self.env.insert(name, value); 179 | self 180 | } 181 | 182 | // FIXME: There is a lot of problematic duplication in this code. I need 183 | // to sit down and come up with a better abstraction. 184 | 185 | /// Import a function defined in the host into the guest. The function's 186 | /// arguments and return type must all be serializable. 187 | /// An immutable reference to `ctx` will be passed to the function as it's 188 | /// first argument each time it's called. 189 | /// 190 | /// NOTE: This method exists due to a limitation in the underlying Waswer 191 | /// engine which currently doesn't support imported closures with 192 | /// captured context. The Wasamer developers have said they are interested 193 | /// in removing that limitation and when they do this method will be 194 | /// removed in favor of `import_function' since context can be more 195 | /// idiomatically handled with captured values. 196 | pub fn import_function_with_context< 197 | Args, 198 | F: ImportableFnWithContext + Send + 'static, 199 | C: Send + Sync + Clone + 'static, 200 | >( 201 | self, 202 | name: impl ToString, 203 | ctx: C, 204 | value: F, 205 | ) -> Self { 206 | let env = Env::new(self.garbage.clone(), ctx); 207 | 208 | if F::has_arg() { 209 | let f = if F::has_return() { 210 | let wrapped = move |env: &Env, ptr: u32, len: u32| -> u64 { 211 | let mut buffer = env.message_buffer(); 212 | let r = value 213 | .call_with_input(&mut buffer, ptr as usize, len as usize, &env.ctx) 214 | .unwrap() 215 | .map(|p| p.0) 216 | .unwrap_or(0); 217 | env.garbage.lock().unwrap().extend(buffer.garbage.drain(..)); 218 | r 219 | }; 220 | Function::new_native_with_env(&self.store, env, wrapped) 221 | } else { 222 | let wrapped = move |env: &Env, ptr: u32, len: u32| { 223 | let mut buffer = env.message_buffer(); 224 | value 225 | .call_with_input(&mut buffer, ptr as usize, len as usize, &env.ctx) 226 | .unwrap(); 227 | env.garbage.lock().unwrap().extend(buffer.garbage.drain(..)); 228 | }; 229 | Function::new_native_with_env(&self.store, env, wrapped) 230 | }; 231 | self.import(name, f) 232 | } else { 233 | let f = if F::has_return() { 234 | let wrapped = move |env: &Env| -> u64 { 235 | let mut buffer = env.message_buffer(); 236 | let r = value 237 | .call_without_input(&mut buffer, &env.ctx) 238 | .unwrap() 239 | .map(|p| p.0) 240 | .unwrap_or(0); 241 | env.garbage.lock().unwrap().extend(buffer.garbage.drain(..)); 242 | r 243 | }; 244 | Function::new_native_with_env(&self.store, env, wrapped) 245 | } else { 246 | let wrapped = move |env: &Env| { 247 | let mut buffer = env.message_buffer(); 248 | value.call_without_input(&mut buffer, &env.ctx).unwrap(); 249 | env.garbage.lock().unwrap().extend(buffer.garbage.drain(..)); 250 | }; 251 | Function::new_native_with_env(&self.store, env, wrapped) 252 | }; 253 | self.import(name, f) 254 | } 255 | } 256 | 257 | /// Import a function defined in the host into the guest. The function's 258 | /// arguments and return type must all be serializable. 259 | pub fn import_function + Send + 'static>( 260 | self, 261 | name: impl ToString, 262 | value: F, 263 | ) -> Self { 264 | let env = Env::new(self.garbage.clone(), ()); 265 | 266 | if F::has_arg() { 267 | let f = if F::has_return() { 268 | let wrapped = move |env: &Env<()>, ptr: u32, len: u32| -> u64 { 269 | let mut buffer = env.message_buffer(); 270 | let r = value 271 | .call_with_input(&mut buffer, ptr as usize, len as usize) 272 | .unwrap() 273 | .map(|p| p.0) 274 | .unwrap_or(0); 275 | env.garbage.lock().unwrap().extend(buffer.garbage.drain(..)); 276 | r 277 | }; 278 | Function::new_native_with_env(&self.store, env, wrapped) 279 | } else { 280 | let wrapped = move |env: &Env<()>, ptr: u32, len: u32| { 281 | let mut buffer = env.message_buffer(); 282 | value 283 | .call_with_input(&mut buffer, ptr as usize, len as usize) 284 | .unwrap(); 285 | env.garbage.lock().unwrap().extend(buffer.garbage.drain(..)); 286 | }; 287 | Function::new_native_with_env(&self.store, env, wrapped) 288 | }; 289 | self.import(name, f) 290 | } else { 291 | let f = if F::has_return() { 292 | let wrapped = move |env: &Env<()>| -> u64 { 293 | let mut buffer = env.message_buffer(); 294 | let r = value 295 | .call_without_input(&mut buffer) 296 | .unwrap() 297 | .map(|p| p.0) 298 | .unwrap_or(0); 299 | env.garbage.lock().unwrap().extend(buffer.garbage.drain(..)); 300 | r 301 | }; 302 | Function::new_native_with_env(&self.store, env, wrapped) 303 | } else { 304 | let wrapped = move |env: &Env<()>| { 305 | let mut buffer = env.message_buffer(); 306 | value.call_without_input(&mut buffer).unwrap(); 307 | env.garbage.lock().unwrap().extend(buffer.garbage.drain(..)); 308 | }; 309 | Function::new_native_with_env(&self.store, env, wrapped) 310 | }; 311 | self.import(name, f) 312 | } 313 | } 314 | 315 | /// Finalize the builder and create the WasmPlugin ready for use. 316 | pub fn finish(self) -> errors::Result { 317 | let mut import_object = wasmer::ImportObject::new(); 318 | import_object.register("env", self.env); 319 | Ok(WasmPlugin { 320 | instance: Instance::new(&self.module, &import_object)?, 321 | garbage: self.garbage, 322 | }) 323 | } 324 | } 325 | 326 | /// A marker trait for Fn types who's arguments and return type can be 327 | /// serialized and are thus safe to import into a plugin; 328 | pub trait ImportableFnWithContext { 329 | #[doc(hidden)] 330 | fn has_arg() -> bool; 331 | #[doc(hidden)] 332 | fn has_return() -> bool; 333 | #[doc(hidden)] 334 | fn call_with_input( 335 | &self, 336 | message_buffer: &mut MessageBuffer, 337 | ptr: usize, 338 | len: usize, 339 | ctx: &C, 340 | ) -> errors::Result>; 341 | #[doc(hidden)] 342 | fn call_without_input( 343 | &self, 344 | message_buffer: &mut MessageBuffer, 345 | ctx: &C, 346 | ) -> errors::Result>; 347 | } 348 | 349 | impl ImportableFnWithContext for F 350 | where 351 | F: Fn(&C, Args) -> ReturnType, 352 | Args: Deserializable, 353 | ReturnType: Serializable, 354 | { 355 | fn has_arg() -> bool { 356 | true 357 | } 358 | fn has_return() -> bool { 359 | std::mem::size_of::() > 0 360 | } 361 | fn call_with_input( 362 | &self, 363 | message_buffer: &mut MessageBuffer, 364 | ptr: usize, 365 | len: usize, 366 | ctx: &C, 367 | ) -> errors::Result> { 368 | let message = message_buffer.read_message(ptr, len); 369 | let result = self(ctx, Args::deserialize(&message)?); 370 | if std::mem::size_of::() > 0 { 371 | // No need to write anything for ZSTs 372 | let message = result.serialize()?; 373 | Ok(Some(message_buffer.write_message(&message))) 374 | } else { 375 | Ok(None) 376 | } 377 | } 378 | 379 | fn call_without_input( 380 | &self, 381 | _message_buffer: &mut MessageBuffer, 382 | _ctx: &C, 383 | ) -> errors::Result> { 384 | unimplemented!("Requires argument") 385 | } 386 | } 387 | 388 | impl ImportableFnWithContext for F 389 | where 390 | F: Fn(&C) -> ReturnType, 391 | ReturnType: Serializable, 392 | { 393 | fn has_arg() -> bool { 394 | false 395 | } 396 | fn has_return() -> bool { 397 | std::mem::size_of::() > 0 398 | } 399 | fn call_with_input( 400 | &self, 401 | _message_buffer: &mut MessageBuffer, 402 | _ptr: usize, 403 | _len: usize, 404 | _ctx: &C, 405 | ) -> errors::Result> { 406 | unimplemented!("Must not supply argument") 407 | } 408 | 409 | fn call_without_input( 410 | &self, 411 | message_buffer: &mut MessageBuffer, 412 | ctx: &C, 413 | ) -> errors::Result> { 414 | let result = self(ctx); 415 | if std::mem::size_of::() > 0 { 416 | // No need to write anything for ZSTs 417 | let message = result.serialize()?; 418 | Ok(Some(message_buffer.write_message(&message))) 419 | } else { 420 | Ok(None) 421 | } 422 | } 423 | } 424 | 425 | /// A marker trait for Fn types who's arguments and return type can be 426 | /// serialized and are thus safe to import into a plugin; 427 | pub trait ImportableFn { 428 | #[doc(hidden)] 429 | fn has_arg() -> bool; 430 | #[doc(hidden)] 431 | fn has_return() -> bool; 432 | #[doc(hidden)] 433 | fn call_with_input( 434 | &self, 435 | message_buffer: &mut MessageBuffer, 436 | ptr: usize, 437 | len: usize, 438 | ) -> errors::Result>; 439 | #[doc(hidden)] 440 | fn call_without_input( 441 | &self, 442 | message_buffer: &mut MessageBuffer, 443 | ) -> errors::Result>; 444 | } 445 | 446 | impl ImportableFn for F 447 | where 448 | F: Fn(Args) -> ReturnType, 449 | Args: Deserializable, 450 | ReturnType: Serializable, 451 | { 452 | fn has_arg() -> bool { 453 | true 454 | } 455 | fn has_return() -> bool { 456 | std::mem::size_of::() > 0 457 | } 458 | fn call_with_input( 459 | &self, 460 | message_buffer: &mut MessageBuffer, 461 | ptr: usize, 462 | len: usize, 463 | ) -> errors::Result> { 464 | let message = message_buffer.read_message(ptr, len); 465 | let result = self(Args::deserialize(&message)?); 466 | if std::mem::size_of::() > 0 { 467 | let message = result.serialize()?; 468 | Ok(Some(message_buffer.write_message(&message))) 469 | } else { 470 | // No need to write anything for ZSTs 471 | Ok(None) 472 | } 473 | } 474 | 475 | fn call_without_input( 476 | &self, 477 | _message_buffer: &mut MessageBuffer, 478 | ) -> errors::Result> { 479 | unimplemented!("Requires argument") 480 | } 481 | } 482 | 483 | #[doc(hidden)] 484 | pub enum NoArgs {} 485 | 486 | impl ImportableFn for F 487 | where 488 | F: Fn() -> ReturnType, 489 | ReturnType: Serializable, 490 | { 491 | fn has_arg() -> bool { 492 | false 493 | } 494 | fn has_return() -> bool { 495 | std::mem::size_of::() > 0 496 | } 497 | fn call_with_input( 498 | &self, 499 | _message_buffer: &mut MessageBuffer, 500 | _ptr: usize, 501 | _len: usize, 502 | ) -> errors::Result> { 503 | unimplemented!("Must not supply argument") 504 | } 505 | 506 | fn call_without_input( 507 | &self, 508 | message_buffer: &mut MessageBuffer, 509 | ) -> errors::Result> { 510 | let result = self(); 511 | if std::mem::size_of::() > 0 { 512 | // No need to write anything for ZSTs 513 | let message = result.serialize()?; 514 | Ok(Some(message_buffer.write_message(&message))) 515 | } else { 516 | Ok(None) 517 | } 518 | } 519 | } 520 | 521 | /// A loaded plugin 522 | #[derive(Clone, Debug)] 523 | pub struct WasmPlugin { 524 | instance: Instance, 525 | garbage: Arc>>, 526 | } 527 | 528 | #[doc(hidden)] 529 | pub struct MessageBuffer<'a> { 530 | memory: &'a Memory, 531 | allocator: &'a Function, 532 | garbage: Vec, 533 | } 534 | 535 | impl<'a> MessageBuffer<'a> { 536 | fn write_message(&mut self, message: &[u8]) -> FatPointer { 537 | let len = message.len() as u32; 538 | 539 | let ptr = self 540 | .allocator 541 | .native::() 542 | .unwrap() 543 | .call(len as u32) 544 | .unwrap(); 545 | 546 | unsafe { 547 | let data = self.memory.data_unchecked_mut(); 548 | data[ptr as usize..ptr as usize + len as usize].copy_from_slice(&message); 549 | } 550 | 551 | let mut fat_ptr = FatPointer(0); 552 | fat_ptr.set_ptr(ptr); 553 | fat_ptr.set_len(len); 554 | self.garbage.push(FatPointer(fat_ptr.0)); 555 | fat_ptr 556 | } 557 | 558 | fn read_message(&self, ptr: usize, len: usize) -> Vec { 559 | let mut buff: Vec = vec![0; len]; 560 | unsafe { 561 | let data = self.memory.data_unchecked(); 562 | buff.copy_from_slice(&data[ptr..ptr + len]); 563 | } 564 | buff 565 | } 566 | 567 | fn read_message_from_fat_pointer(&self, fat_ptr: u64) -> Vec { 568 | unsafe { 569 | let data = self.memory.data_unchecked(); 570 | let fat_ptr = FatPointer(fat_ptr); 571 | let mut buff: Vec = vec![0; fat_ptr.len() as usize]; 572 | buff.copy_from_slice( 573 | &data[fat_ptr.ptr() as usize..fat_ptr.ptr() as usize + fat_ptr.len() as usize], 574 | ); 575 | buff 576 | } 577 | } 578 | } 579 | 580 | impl WasmPlugin { 581 | fn message_buffer(&self) -> errors::Result { 582 | Ok(MessageBuffer { 583 | memory: self.instance.exports.get_memory("memory")?, 584 | allocator: self 585 | .instance 586 | .exports 587 | .get::("allocate_message_buffer")?, 588 | garbage: vec![], 589 | }) 590 | } 591 | 592 | /// Call a function exported by the plugin with a single argument 593 | /// which will be serialized and sent to the plugin. 594 | /// 595 | /// Deserialization of the return value depends on the type being known 596 | /// at the call site. 597 | pub fn call_function_with_argument( 598 | &self, 599 | fn_name: &str, 600 | args: &Args, 601 | ) -> errors::Result 602 | where 603 | Args: Serializable, 604 | ReturnType: Deserializable, 605 | { 606 | let message = args.serialize()?; 607 | let mut buffer = self.message_buffer()?; 608 | let ptr = buffer.write_message(&message); 609 | 610 | let buff = self.call_function_raw(fn_name, Some(ptr))?; 611 | drop(buffer); 612 | ReturnType::deserialize(&buff) 613 | } 614 | 615 | fn call_function_raw( 616 | &self, 617 | fn_name: &str, 618 | input_buffer: Option, 619 | ) -> errors::Result> { 620 | let f = self 621 | .instance 622 | .exports 623 | .get_function(&format!("wasm_plugin_exported__{}", fn_name)) 624 | .unwrap_or_else(|_| panic!("Unable to find function {}", fn_name)); 625 | 626 | let ptr = if let Some(fat_ptr) = input_buffer { 627 | f.native::<(u32, u32), u64>()? 628 | .call(fat_ptr.ptr() as u32, fat_ptr.len() as u32)? 629 | } else { 630 | f.native::<(), u64>()?.call()? 631 | }; 632 | let result = self.message_buffer()?.read_message_from_fat_pointer(ptr); 633 | 634 | let mut garbage: Vec<_> = self.garbage.lock().unwrap().drain(..).collect(); 635 | 636 | if FatPointer(ptr).len() > 0 { 637 | garbage.push(FatPointer(ptr)); 638 | } 639 | if !garbage.is_empty() { 640 | let f = self 641 | .instance 642 | .exports 643 | .get_function("free_message_buffer") 644 | .unwrap_or_else(|_| panic!("Unable to find function 'free_message_buffer'")) 645 | .native::<(u32, u32), ()>()?; 646 | for fat_ptr in garbage { 647 | f.call(fat_ptr.ptr() as u32, fat_ptr.len() as u32)? 648 | } 649 | } 650 | 651 | Ok(result) 652 | } 653 | 654 | /// Call a function exported by the plugin. 655 | /// 656 | /// Deserialization of the return value depends on the type being known 657 | /// at the call site. 658 | pub fn call_function(&mut self, fn_name: &str) -> errors::Result 659 | where 660 | ReturnType: Deserializable, 661 | { 662 | let buff = self.call_function_raw(fn_name, None)?; 663 | ReturnType::deserialize(&buff) 664 | } 665 | } 666 | 667 | #[cfg(feature = "inject_getrandom")] 668 | fn getrandom_shim(env: &Env<()>, ptr: u32, len: u32) { 669 | if let Some(memory) = env.memory_ref() { 670 | let view: MemoryView = memory.view(); 671 | let mut buff: Vec = vec![0; len as usize]; 672 | getrandom::getrandom(&mut buff).unwrap(); 673 | for (dst, src) in view[ptr as usize..ptr as usize + len as usize] 674 | .iter() 675 | .zip(buff) 676 | { 677 | dst.set(src); 678 | } 679 | } 680 | } 681 | -------------------------------------------------------------------------------- /host/src/serialization.rs: -------------------------------------------------------------------------------- 1 | use crate::errors; 2 | 3 | pub trait Serializable { 4 | fn serialize(&self) -> errors::Result>; 5 | } 6 | #[cfg(feature = "serialize_bincode")] 7 | impl Serializable for T { 8 | fn serialize(&self) -> errors::Result> { 9 | bincode::serialize(self).map_err(|_| errors::WasmPluginError::SerializationError) 10 | } 11 | } 12 | #[cfg(feature = "serialize_json")] 13 | impl Serializable for T { 14 | fn serialize(&self) -> errors::Result> { 15 | serde_json::to_vec(self).map_err(|_| errors::WasmPluginError::SerializationError) 16 | } 17 | } 18 | #[cfg(feature = "serialize_nanoserde_json")] 19 | impl Serializable for T { 20 | fn serialize(&self) -> errors::Result> { 21 | Ok(nanoserde::SerJson::serialize_json(self).as_bytes().to_vec()) 22 | } 23 | } 24 | 25 | pub trait Deserializable { 26 | fn deserialize(data: &[u8]) -> errors::Result 27 | where 28 | Self: Sized; 29 | } 30 | #[cfg(feature = "serialize_bincode")] 31 | impl Deserializable for T { 32 | fn deserialize(data: &[u8]) -> errors::Result { 33 | bincode::deserialize(data).map_err(|_| errors::WasmPluginError::DeserializationError) 34 | } 35 | } 36 | #[cfg(feature = "serialize_json")] 37 | impl Deserializable for T { 38 | fn deserialize(data: &[u8]) -> errors::Result { 39 | serde_json::from_slice(data).map_err(|_| errors::WasmPluginError::DeserializationError) 40 | } 41 | } 42 | #[cfg(feature = "serialize_nanoserde_json")] 43 | impl Deserializable for T { 44 | fn deserialize(data: &[u8]) -> errors::Result { 45 | nanoserde::DeJson::deserialize_json( 46 | std::str::from_utf8(data).map_err(|_| errors::WasmPluginError::DeserializationError)?, 47 | ) 48 | .map_err(|_| errors::WasmPluginError::DeserializationError) 49 | } 50 | } 51 | --------------------------------------------------------------------------------