1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
use super::store::wasm_store_t;
use super::types::{wasm_byte_vec_t, wasm_exporttype_vec_t, wasm_importtype_vec_t};
use std::ptr::NonNull;
use wasmer_api::Module;
/// Opaque type representing a WebAssembly module.
#[derive(Clone)]
#[allow(non_camel_case_types)]
pub struct wasm_module_t {
pub(crate) inner: Module,
}
/// A WebAssembly module contains stateless WebAssembly code that has
/// already been compiled and can be instantiated multiple times.
///
/// Creates a new WebAssembly Module given the configuration
/// in the store.
///
/// ## Security
///
/// Before the code is compiled, it will be validated using the store
/// features.
///
/// # Example
///
/// See the module's documentation.
#[no_mangle]
pub unsafe extern "C" fn wasm_module_new(
store: Option<&mut wasm_store_t>,
bytes: Option<&wasm_byte_vec_t>,
) -> Option<Box<wasm_module_t>> {
let store = store?.inner.store_mut();
let bytes = bytes?;
let module = c_try!(Module::from_binary(&store, bytes.as_slice()));
Some(Box::new(wasm_module_t { inner: module }))
}
/// Deletes a WebAssembly module.
///
/// # Example
///
/// See [`wasm_module_new`].
#[no_mangle]
pub unsafe extern "C" fn wasm_module_delete(_module: Option<Box<wasm_module_t>>) {}
/// Validates a new WebAssembly module given the configuration
/// in the [store][super::store].
///
/// This validation is normally pretty fast and checks the enabled
/// WebAssembly features in the [store engine][super::engine] to
/// assure deterministic validation of the module.
///
/// # Example
///
/// ```rust
/// # use wasmer_inline_c::assert_c;
/// # fn main() {
/// # (assert_c! {
/// # #include "tests/wasmer.h"
/// #
/// int main() {
/// // Create the engine and the store.
/// wasm_engine_t* engine = wasm_engine_new();
/// wasm_store_t* store = wasm_store_new(engine);
///
/// // Create a WebAssembly module from a WAT definition.
/// wasm_byte_vec_t wat;
/// wasmer_byte_vec_new_from_string(&wat, "(module)");
/// wasm_byte_vec_t wasm;
/// wat2wasm(&wat, &wasm);
///
/// // Validate that the WebAssembly module is correct.
/// assert(wasm_module_validate(store, &wasm));
///
/// // Free everything.
/// wasm_byte_vec_delete(&wasm);
/// wasm_byte_vec_delete(&wat);
/// wasm_store_delete(store);
/// wasm_engine_delete(engine);
///
/// return 0;
/// }
/// # })
/// # .success();
/// # }
/// ```
#[no_mangle]
pub unsafe extern "C" fn wasm_module_validate(
store: Option<&mut wasm_store_t>,
bytes: Option<&wasm_byte_vec_t>,
) -> bool {
let store = match store {
Some(store) => store.inner.store_mut(),
None => return false,
};
let bytes = match bytes {
Some(bytes) => bytes,
None => return false,
};
Module::validate(&store, bytes.as_slice())
.map(|_| true)
.unwrap_or(false)
}
/// Returns an array of the exported types in the module.
///
/// The order of the exports is guaranteed to be the same as in the
/// WebAssembly bytecode.
///
/// # Example
///
/// ```rust
/// # use wasmer_inline_c::assert_c;
/// # fn main() {
/// # (assert_c! {
/// # #include "tests/wasmer.h"
/// #
/// int main() {
/// // Create the engine and the store.
/// wasm_engine_t* engine = wasm_engine_new();
/// wasm_store_t* store = wasm_store_new(engine);
///
/// // Create a WebAssembly module from a WAT definition.
/// wasm_byte_vec_t wat;
/// wasmer_byte_vec_new_from_string(
/// &wat,
/// "(module\n"
/// " (func (export \"function\") (param i32 i64))\n"
/// " (global (export \"global\") i32 (i32.const 7))\n"
/// " (table (export \"table\") 0 funcref)\n"
/// " (memory (export \"memory\") 1))"
/// );
/// wasm_byte_vec_t wasm;
/// wat2wasm(&wat, &wasm);
///
/// // Create the module.
/// wasm_module_t* module = wasm_module_new(store, &wasm);
/// assert(module);
///
/// // Extract the types exported by this module.
/// wasm_exporttype_vec_t export_types;
/// wasm_module_exports(module, &export_types);
///
/// // We have 4 of them.
/// assert(export_types.size == 4);
///
/// // The first one is a function. Use
/// // `wasm_externtype_as_functype_const` to continue to inspect the
/// // type.
/// {
/// wasm_exporttype_t* export_type = export_types.data[0];
///
/// const wasm_name_t* export_name = wasm_exporttype_name(export_type);
/// wasmer_assert_name(export_name, "function");
///
/// const wasm_externtype_t* extern_type = wasm_exporttype_type(export_type);
/// assert(wasm_externtype_kind(extern_type) == WASM_EXTERN_FUNC);
/// }
///
/// // The second one is a global. Use
/// // `wasm_externtype_as_globaltype_const` to continue to inspect the
/// // type.
/// {
/// wasm_exporttype_t* export_type = export_types.data[1];
///
/// const wasm_name_t* export_name = wasm_exporttype_name(export_type);
/// wasmer_assert_name(export_name, "global");
///
/// const wasm_externtype_t* extern_type = wasm_exporttype_type(export_type);
/// assert(wasm_externtype_kind(extern_type) == WASM_EXTERN_GLOBAL);
/// }
///
/// // The third one is a table. Use
/// // `wasm_externtype_as_tabletype_const` to continue to inspect the
/// // type.
/// {
/// wasm_exporttype_t* export_type = export_types.data[2];
///
/// const wasm_name_t* export_name = wasm_exporttype_name(export_type);
/// wasmer_assert_name(export_name, "table");
///
/// const wasm_externtype_t* extern_type = wasm_exporttype_type(export_type);
/// assert(wasm_externtype_kind(extern_type) == WASM_EXTERN_TABLE);
/// }
///
/// // The fourth one is a memory. Use
/// // `wasm_externtype_as_memorytype_const` to continue to inspect the
/// // type.
/// {
/// wasm_exporttype_t* export_type = export_types.data[3];
///
/// const wasm_name_t* export_name = wasm_exporttype_name(export_type);
/// wasmer_assert_name(export_name, "memory");
///
/// const wasm_externtype_t* extern_type = wasm_exporttype_type(export_type);
/// assert(wasm_externtype_kind(extern_type) == WASM_EXTERN_MEMORY);
/// }
///
/// // Free everything.
/// wasm_exporttype_vec_delete(&export_types);
/// wasm_byte_vec_delete(&wasm);
/// wasm_byte_vec_delete(&wat);
/// wasm_module_delete(module);
/// wasm_store_delete(store);
/// wasm_engine_delete(engine);
///
/// return 0;
/// }
/// # })
/// # .success();
/// # }
/// ```
#[no_mangle]
pub unsafe extern "C" fn wasm_module_exports(
module: &wasm_module_t,
// own
out: &mut wasm_exporttype_vec_t,
) {
let exports = module
.inner
.exports()
.map(|export| Some(Box::new(export.into())))
.collect();
out.set_buffer(exports);
}
/// Returns an array of the imported types in the module.
///
/// The order of the imports is guaranteed to be the same as in the
/// WebAssembly bytecode.
///
/// # Example
///
/// ```rust
/// # use wasmer_inline_c::assert_c;
/// # fn main() {
/// # (assert_c! {
/// # #include "tests/wasmer.h"
/// #
/// int main() {
/// // Create the engine and the store.
/// wasm_engine_t* engine = wasm_engine_new();
/// wasm_store_t* store = wasm_store_new(engine);
///
/// // Create a WebAssembly module from a WAT definition.
/// wasm_byte_vec_t wat;
/// wasmer_byte_vec_new_from_string(
/// &wat,
/// "(module\n"
/// " (import \"ns\" \"function\" (func))\n"
/// " (import \"ns\" \"global\" (global f32))\n"
/// " (import \"ns\" \"table\" (table 1 2 funcref))\n"
/// " (import \"ns\" \"memory\" (memory 3 4)))"
/// );
/// wasm_byte_vec_t wasm;
/// wat2wasm(&wat, &wasm);
///
/// // Create the module.
/// wasm_module_t* module = wasm_module_new(store, &wasm);
/// assert(module);
///
/// // Extract the types imported by the module.
/// wasm_importtype_vec_t import_types;
/// wasm_module_imports(module, &import_types);
///
/// // We have 4 of them.
/// assert(import_types.size == 4);
///
/// // The first one is a function. Use
/// // `wasm_externtype_as_functype_const` to continue to inspect the
/// // type.
/// {
/// const wasm_importtype_t* import_type = import_types.data[0];
///
/// const wasm_name_t* import_module = wasm_importtype_module(import_type);
/// wasmer_assert_name(import_module, "ns");
///
/// const wasm_name_t* import_name = wasm_importtype_name(import_type);
/// wasmer_assert_name(import_name, "function");
///
/// const wasm_externtype_t* extern_type = wasm_importtype_type(import_type);
/// assert(wasm_externtype_kind(extern_type) == WASM_EXTERN_FUNC);
/// }
///
/// // The second one is a global. Use
/// // `wasm_externtype_as_globaltype_const` to continue to inspect
/// // the type.
/// {
/// const wasm_importtype_t* import_type = import_types.data[1];
///
/// const wasm_name_t* import_module = wasm_importtype_module(import_type);
/// wasmer_assert_name(import_module, "ns");
///
/// const wasm_name_t* import_name = wasm_importtype_name(import_type);
/// wasmer_assert_name(import_name, "global");
///
/// const wasm_externtype_t* extern_type = wasm_importtype_type(import_type);
/// assert(wasm_externtype_kind(extern_type) == WASM_EXTERN_GLOBAL);
/// }
///
/// // The third one is a table. Use
/// // `wasm_externtype_as_tabletype_const` to continue to inspect
/// // the type.
/// {
/// const wasm_importtype_t* import_type = import_types.data[2];
///
/// const wasm_name_t* import_module = wasm_importtype_module(import_type);
/// wasmer_assert_name(import_module, "ns");
///
/// const wasm_name_t* import_name = wasm_importtype_name(import_type);
/// wasmer_assert_name(import_name, "table");
///
/// const wasm_externtype_t* extern_type = wasm_importtype_type(import_type);
/// assert(wasm_externtype_kind(extern_type) == WASM_EXTERN_TABLE);
/// }
///
/// // The fourth one is a memory. Use
/// // `wasm_externtype_as_memorytype_const` to continue to inspect
/// // the type.
/// {
/// const wasm_importtype_t* import_type = import_types.data[3];
///
/// const wasm_name_t* import_module = wasm_importtype_module(import_type);
/// wasmer_assert_name(import_module, "ns");
///
/// const wasm_name_t* import_name = wasm_importtype_name(import_type);
/// wasmer_assert_name(import_name, "memory");
///
/// const wasm_externtype_t* extern_type = wasm_importtype_type(import_type);
/// assert(wasm_externtype_kind(extern_type) == WASM_EXTERN_MEMORY);
///
/// const wasm_memorytype_t* memory_type = wasm_externtype_as_memorytype_const(extern_type);
/// const wasm_limits_t* memory_limits = wasm_memorytype_limits(memory_type);
/// assert(memory_limits->min == 3);
/// assert(memory_limits->max == 4);
/// }
///
/// // Free everything.
/// wasm_importtype_vec_delete(&import_types);
/// wasm_module_delete(module);
/// wasm_byte_vec_delete(&wasm);
/// wasm_byte_vec_delete(&wat);
/// wasm_store_delete(store);
/// wasm_engine_delete(engine);
///
/// return 0;
/// }
/// # })
/// # .success();
/// # }
/// ```
#[no_mangle]
pub unsafe extern "C" fn wasm_module_imports(
module: &wasm_module_t,
// own
out: &mut wasm_importtype_vec_t,
) {
let imports = module
.inner
.imports()
.map(|import| Some(Box::new(import.into())))
.collect();
out.set_buffer(imports);
}
/// Deserializes a serialized module binary into a `wasm_module_t`.
///
/// Note: the module has to be serialized before with the
/// `wasm_module_serialize` function.
///
/// # Safety
///
/// This function is inherently **unsafe** as the provided bytes:
///
/// 1. Are going to be deserialized directly into Rust and C objects,
/// 2. Contains the function assembly bodies and, if intercepted,
/// a malicious actor could inject code into executable
/// memory.
///
/// And as such, the `wasm_module_deserialize` method is unsafe.
///
/// # Example
///
/// ```rust
/// # use wasmer_inline_c::assert_c;
/// # fn main() {
/// # (assert_c! {
/// # #include "tests/wasmer.h"
/// #
/// int main() {
/// // Create the engine and the store.
/// wasm_engine_t* engine = wasm_engine_new();
/// wasm_store_t* store = wasm_store_new(engine);
///
/// // Create a WebAssembly module from a WAT definition.
/// wasm_byte_vec_t wat;
/// wasmer_byte_vec_new_from_string(
/// &wat,
/// "(module\n"
/// " (func (export \"function\") (param i32 i64))\n"
/// " (global (export \"global\") i32 (i32.const 7))\n"
/// " (table (export \"table\") 0 funcref)\n"
/// " (memory (export \"memory\") 1))"
/// );
/// wasm_byte_vec_t wasm;
/// wat2wasm(&wat, &wasm);
///
/// // Create the module.
/// wasm_module_t* module = wasm_module_new(store, &wasm);
/// assert(module);
///
/// // Serialize the module into bytes.
/// wasm_byte_vec_t serialized_module;
/// wasm_module_serialize(module, &serialized_module);
/// assert(serialized_module.size > 0);
///
/// // Free the module.
/// wasm_module_delete(module);
///
/// // Deserialize the serialized module. Note that the store must
/// // be the same as the one used to serialize.
/// wasm_module_t* deserialized_module = wasm_module_deserialize(
/// store,
/// &serialized_module
/// );
/// wasm_byte_vec_delete(&serialized_module);
/// assert(deserialized_module);
///
/// // Check we have our 4 export types.
/// wasm_exporttype_vec_t export_types;
/// wasm_module_exports(deserialized_module, &export_types);
///
/// assert(export_types.size == 4);
///
/// // Free everything.
/// wasm_exporttype_vec_delete(&export_types);
/// wasm_module_delete(deserialized_module);
/// wasm_byte_vec_delete(&wasm);
/// wasm_byte_vec_delete(&wat);
/// wasm_store_delete(store);
/// wasm_engine_delete(engine);
///
/// return 0;
/// }
/// # })
/// # .success();
/// # }
/// ```
#[no_mangle]
pub unsafe extern "C" fn wasm_module_deserialize(
store: &wasm_store_t,
bytes: Option<&wasm_byte_vec_t>,
) -> Option<NonNull<wasm_module_t>> {
let bytes = bytes?;
let module = c_try!(Module::deserialize(&store.inner.store(), bytes.as_slice()));
Some(NonNull::new_unchecked(Box::into_raw(Box::new(
wasm_module_t { inner: module },
))))
}
/// Serializes a module into a binary representation that the
/// [engine][super::engine] can later process via
/// [`wasm_module_deserialize`].
///
/// # Example
///
/// See [`wasm_module_deserialize`].
#[no_mangle]
pub unsafe extern "C" fn wasm_module_serialize(module: &wasm_module_t, out: &mut wasm_byte_vec_t) {
let byte_vec = c_try!(module.inner.serialize(); otherwise ());
out.set_buffer(byte_vec.to_vec());
}
#[cfg(test)]
mod tests {
#[cfg(not(target_os = "windows"))]
use inline_c::assert_c;
#[cfg(target_os = "windows")]
use wasmer_inline_c::assert_c;
#[cfg_attr(coverage, ignore)]
#[test]
fn test_module_validate() {
(assert_c! {
#include "tests/wasmer.h"
int main() {
wasm_engine_t* engine = wasm_engine_new();
wasm_store_t* store = wasm_store_new(engine);
wasm_byte_vec_t wat;
wasmer_byte_vec_new_from_string(&wat, "(module)");
wasm_byte_vec_t wasm;
wat2wasm(&wat, &wasm);
assert(wasm_module_validate(store, &wasm));
wasm_byte_vec_delete(&wasm);
wasm_byte_vec_delete(&wat);
wasm_store_delete(store);
wasm_engine_delete(engine);
return 0;
}
})
.success();
}
#[cfg_attr(coverage, ignore)]
#[test]
fn test_module_new() {
(assert_c! {
#include "tests/wasmer.h"
int main() {
wasm_engine_t* engine = wasm_engine_new();
wasm_store_t* store = wasm_store_new(engine);
wasm_byte_vec_t wat;
wasmer_byte_vec_new_from_string(&wat, "(module)");
wasm_byte_vec_t wasm;
wat2wasm(&wat, &wasm);
wasm_module_t* module = wasm_module_new(store, &wasm);
assert(module);
wasm_byte_vec_delete(&wasm);
wasm_byte_vec_delete(&wat);
wasm_module_delete(module);
wasm_store_delete(store);
wasm_engine_delete(engine);
return 0;
}
})
.success();
}
#[cfg_attr(coverage, ignore)]
#[test]
fn test_module_exports() {
(assert_c! {
#include "tests/wasmer.h"
int main() {
wasm_engine_t* engine = wasm_engine_new();
wasm_store_t* store = wasm_store_new(engine);
wasm_byte_vec_t wat;
wasmer_byte_vec_new_from_string(
&wat,
"(module\n"
" (func (export \"function\") (param i32 i64))\n"
" (global (export \"global\") i32 (i32.const 7))\n"
" (table (export \"table\") 0 funcref)\n"
" (memory (export \"memory\") 1))"
);
wasm_byte_vec_t wasm;
wat2wasm(&wat, &wasm);
wasm_module_t* module = wasm_module_new(store, &wasm);
assert(module);
wasm_exporttype_vec_t export_types;
wasm_module_exports(module, &export_types);
assert(export_types.size == 4);
{
wasm_exporttype_t* export_type = export_types.data[0];
const wasm_name_t* export_name = wasm_exporttype_name(export_type);
wasmer_assert_name(export_name, "function");
const wasm_externtype_t* extern_type = wasm_exporttype_type(export_type);
assert(wasm_externtype_kind(extern_type) == WASM_EXTERN_FUNC);
const wasm_functype_t* func_type = wasm_externtype_as_functype_const(extern_type);
const wasm_valtype_vec_t* func_params = wasm_functype_params(func_type);
assert(func_params && func_params->size == 2);
assert(wasm_valtype_kind(func_params->data[0]) == WASM_I32);
assert(wasm_valtype_kind(func_params->data[1]) == WASM_I64);
const wasm_valtype_vec_t* func_results = wasm_functype_results(func_type);
assert(func_results && func_results->size == 0);
}
{
wasm_exporttype_t* export_type = export_types.data[1];
const wasm_name_t* export_name = wasm_exporttype_name(export_type);
wasmer_assert_name(export_name, "global");
const wasm_externtype_t* extern_type = wasm_exporttype_type(export_type);
assert(wasm_externtype_kind(extern_type) == WASM_EXTERN_GLOBAL);
const wasm_globaltype_t* global_type = wasm_externtype_as_globaltype_const(extern_type);
assert(wasm_valtype_kind(wasm_globaltype_content(global_type)) == WASM_I32);
assert(wasm_globaltype_mutability(global_type) == WASM_CONST);
}
{
wasm_exporttype_t* export_type = export_types.data[2];
const wasm_name_t* export_name = wasm_exporttype_name(export_type);
wasmer_assert_name(export_name, "table");
const wasm_externtype_t* extern_type = wasm_exporttype_type(export_type);
assert(wasm_externtype_kind(extern_type) == WASM_EXTERN_TABLE);
const wasm_tabletype_t* table_type = wasm_externtype_as_tabletype_const(extern_type);
assert(wasm_valtype_kind(wasm_tabletype_element(table_type)) == WASM_FUNCREF);
const wasm_limits_t* table_limits = wasm_tabletype_limits(table_type);
assert(table_limits->min == 0);
assert(table_limits->max == wasm_limits_max_default);
}
{
wasm_exporttype_t* export_type = export_types.data[3];
const wasm_name_t* export_name = wasm_exporttype_name(export_type);
wasmer_assert_name(export_name, "memory");
const wasm_externtype_t* extern_type = wasm_exporttype_type(export_type);
assert(wasm_externtype_kind(extern_type) == WASM_EXTERN_MEMORY);
const wasm_memorytype_t* memory_type = wasm_externtype_as_memorytype_const(extern_type);
const wasm_limits_t* memory_limits = wasm_memorytype_limits(memory_type);
assert(memory_limits->min == 1);
assert(memory_limits->max == wasm_limits_max_default);
}
wasm_exporttype_vec_delete(&export_types);
wasm_byte_vec_delete(&wasm);
wasm_byte_vec_delete(&wat);
wasm_module_delete(module);
wasm_store_delete(store);
wasm_engine_delete(engine);
return 0;
}
})
.success();
}
#[cfg_attr(coverage, ignore)]
#[test]
fn test_module_imports() {
(assert_c! {
#include "tests/wasmer.h"
int main() {
wasm_engine_t* engine = wasm_engine_new();
wasm_store_t* store = wasm_store_new(engine);
wasm_byte_vec_t wat;
wasmer_byte_vec_new_from_string(
&wat,
"(module\n"
" (import \"ns\" \"function\" (func))\n"
" (import \"ns\" \"global\" (global f32))\n"
" (import \"ns\" \"table\" (table 1 2 funcref))\n"
" (import \"ns\" \"memory\" (memory 3 4)))"
);
wasm_byte_vec_t wasm;
wat2wasm(&wat, &wasm);
wasm_module_t* module = wasm_module_new(store, &wasm);
assert(module);
wasm_importtype_vec_t import_types;
wasm_module_imports(module, &import_types);
assert(import_types.size == 4);
{
const wasm_importtype_t* import_type = import_types.data[0];
const wasm_name_t* import_module = wasm_importtype_module(import_type);
wasmer_assert_name(import_module, "ns");
const wasm_name_t* import_name = wasm_importtype_name(import_type);
wasmer_assert_name(import_name, "function");
const wasm_externtype_t* extern_type = wasm_importtype_type(import_type);
assert(wasm_externtype_kind(extern_type) == WASM_EXTERN_FUNC);
const wasm_functype_t* func_type = wasm_externtype_as_functype_const(extern_type);
const wasm_valtype_vec_t* func_params = wasm_functype_params(func_type);
assert(func_params && func_params->size == 0);
const wasm_valtype_vec_t* func_results = wasm_functype_results(func_type);
assert(func_results && func_results->size == 0);
}
{
const wasm_importtype_t* import_type = import_types.data[1];
const wasm_name_t* import_module = wasm_importtype_module(import_type);
wasmer_assert_name(import_module, "ns");
const wasm_name_t* import_name = wasm_importtype_name(import_type);
wasmer_assert_name(import_name, "global");
const wasm_externtype_t* extern_type = wasm_importtype_type(import_type);
assert(wasm_externtype_kind(extern_type) == WASM_EXTERN_GLOBAL);
const wasm_globaltype_t* global_type = wasm_externtype_as_globaltype_const(extern_type);
assert(wasm_valtype_kind(wasm_globaltype_content(global_type)) == WASM_F32);
assert(wasm_globaltype_mutability(global_type) == WASM_CONST);
}
{
const wasm_importtype_t* import_type = import_types.data[2];
const wasm_name_t* import_module = wasm_importtype_module(import_type);
wasmer_assert_name(import_module, "ns");
const wasm_name_t* import_name = wasm_importtype_name(import_type);
wasmer_assert_name(import_name, "table");
const wasm_externtype_t* extern_type = wasm_importtype_type(import_type);
assert(wasm_externtype_kind(extern_type) == WASM_EXTERN_TABLE);
const wasm_tabletype_t* table_type = wasm_externtype_as_tabletype_const(extern_type);
assert(wasm_valtype_kind(wasm_tabletype_element(table_type)) == WASM_FUNCREF);
const wasm_limits_t* table_limits = wasm_tabletype_limits(table_type);
assert(table_limits->min == 1);
assert(table_limits->max == 2);
}
{
const wasm_importtype_t* import_type = import_types.data[3];
const wasm_name_t* import_module = wasm_importtype_module(import_type);
wasmer_assert_name(import_module, "ns");
const wasm_name_t* import_name = wasm_importtype_name(import_type);
wasmer_assert_name(import_name, "memory");
const wasm_externtype_t* extern_type = wasm_importtype_type(import_type);
assert(wasm_externtype_kind(extern_type) == WASM_EXTERN_MEMORY);
const wasm_memorytype_t* memory_type = wasm_externtype_as_memorytype_const(extern_type);
const wasm_limits_t* memory_limits = wasm_memorytype_limits(memory_type);
assert(memory_limits->min == 3);
assert(memory_limits->max == 4);
}
wasm_importtype_vec_delete(&import_types);
wasm_module_delete(module);
wasm_byte_vec_delete(&wasm);
wasm_byte_vec_delete(&wat);
wasm_store_delete(store);
wasm_engine_delete(engine);
return 0;
}
})
.success();
}
#[cfg_attr(coverage, ignore)]
#[test]
fn test_module_serialize() {
(assert_c! {
#include "tests/wasmer.h"
int main() {
wasm_engine_t* engine = wasm_engine_new();
wasm_store_t* store = wasm_store_new(engine);
wasm_byte_vec_t wat;
wasmer_byte_vec_new_from_string(&wat, "(module)");
wasm_byte_vec_t wasm;
wat2wasm(&wat, &wasm);
wasm_module_t* module = wasm_module_new(store, &wasm);
assert(module);
wasm_byte_vec_t serialized_module;
wasm_module_serialize(module, &serialized_module);
assert(serialized_module.size > 0);
wasm_module_delete(module);
wasm_byte_vec_delete(&serialized_module);
wasm_byte_vec_delete(&wasm);
wasm_byte_vec_delete(&wat);
wasm_store_delete(store);
wasm_engine_delete(engine);
return 0;
}
})
.success();
}
#[cfg_attr(coverage, ignore)]
#[test]
fn test_module_serialize_and_deserialize() {
(assert_c! {
#include "tests/wasmer.h"
int main() {
wasm_engine_t* engine = wasm_engine_new();
wasm_store_t* store = wasm_store_new(engine);
wasm_byte_vec_t wat;
wasmer_byte_vec_new_from_string(
&wat,
"(module\n"
" (func (export \"function\") (param i32 i64))\n"
" (global (export \"global\") i32 (i32.const 7))\n"
" (table (export \"table\") 0 funcref)\n"
" (memory (export \"memory\") 1))"
);
wasm_byte_vec_t wasm;
wat2wasm(&wat, &wasm);
wasm_module_t* module = wasm_module_new(store, &wasm);
assert(module);
wasm_byte_vec_t serialized_module;
wasm_module_serialize(module, &serialized_module);
assert(serialized_module.size > 0);
wasm_module_delete(module);
wasm_module_t* deserialized_module = wasm_module_deserialize(
store,
&serialized_module
);
wasm_byte_vec_delete(&serialized_module);
assert(deserialized_module);
wasm_exporttype_vec_t export_types;
wasm_module_exports(deserialized_module, &export_types);
assert(export_types.size == 4);
wasm_exporttype_vec_delete(&export_types);
wasm_module_delete(deserialized_module);
wasm_byte_vec_delete(&wasm);
wasm_byte_vec_delete(&wat);
wasm_store_delete(store);
wasm_engine_delete(engine);
return 0;
}
})
.success();
}
}