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 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
//! Defines WasmEdge Config struct.
use crate::{ffi, WasmEdgeResult};
use wasmedge_types::error::WasmEdgeError;
#[cfg(feature = "aot")]
use wasmedge_types::{CompilerOptimizationLevel, CompilerOutputFormat};
/// Defines Config struct used to check/set the configuration options.
///
/// [Config](crate::Config) manages the configuration options, which are used to initiate [Loader](crate::Loader), [Validator](crate::Validator), [Executor](crate::Executor), and [Compiler](crate::Compiler).
///
/// The configuration options are categorized into the following four groups:
///
/// - **WebAssembly Proposals**
///
/// This group of options are used to turn on/off the WebAssembly proposals. They are effective to any WasmEdge
/// context created with [Config](crate::Config).
///
/// - `MultiMemories` enables to use multiple memories within a single Wasm module.
///
/// Also see [Multiple Memories for Wasm](https://github.com/WebAssembly/multi-memory/blob/main/proposals/multi-memory/Overview.md)
///
/// - `ImportExportMutGlobals` supports mutable imported and exported globals.
///
/// Also see [Import/Export Mutable Globals Proposal](https://github.com/WebAssembly/mutable-global/blob/master/proposals/mutable-global/Overview.md#importexport-mutable-globals).
///
/// - `NonTrapFloatToIntConversions` supports the non-trapping float-to-int conversion.
///
/// Also see [Non-trapping Float-to-int Conversions Proposal](https://github.com/WebAssembly/spec/blob/main/proposals/nontrapping-float-to-int-conversion/Overview.md).
///
/// - `SignExtensionOperators` supports new integer instructions for sign-extending 8-bit, 16-bit, and 32-bit values.
///
/// Also see [Sign-extension Operators Proposal](https://github.com/WebAssembly/spec/blob/main/proposals/sign-extension-ops/Overview.md).
///
/// - `MultiValue` supports functions and instructions with multiple return values, and blocks with inputs.
///
/// Also see [Multi-value Extension](https://github.com/WebAssembly/spec/blob/main/proposals/multi-value/Overview.md).
///
/// - `BulkMemoryOperations` supports bulk memory operations.
///
/// Also see [Bulk Memory Operations Proposal](https://github.com/WebAssembly/spec/blob/main/proposals/bulk-memory-operations/Overview.md#motivation-for-bulk-memory-operations).
///
/// - `ReferenceTypes` supports reference types.
///
/// Also see [Reference Types Proposal](https://github.com/WebAssembly/spec/blob/main/proposals/reference-types/Overview.md).
///
/// - `SIMD` supports 128-bit packed SIMD extension to WebAssembly.
///
/// Also see [SIMD Proposal](https://github.com/WebAssembly/spec/blob/main/proposals/simd/SIMD.md).
///
/// - `TailCall` supports tail call optimization.
///
/// Also see [Tail Call Proposal](https://github.com/WebAssembly/tail-call/blob/master/proposals/tail-call/Overview.md).
///
/// - `Annotations` supports annotations in WASM text format.
///
/// Also see [Annotations Proposal](https://github.com/WebAssembly/annotations/blob/master/proposals/annotations/Overview.md).
///
/// - `Memory64` supports 64-bit memory indexes.
///
/// Also see [Memory64 Proposal](https://github.com/WebAssembly/memory64/blob/main/proposals/memory64/Overview.md).
///
/// - `Threads` supports threading feature.
///
/// Also see [Threading Proposal](https://github.com/WebAssembly/threads/blob/main/proposals/threads/Overview.md).
///
/// - `ExceptionHandling` supports exception handling.
///
/// Also see [Exception Handling Proposal](https://github.com/WebAssembly/exception-handling/blob/main/proposals/exception-handling/Exceptions.md).
///
/// - `FunctionReferences` supports typed function references for WebAssembly.
///
/// Also see [Function References Proposal](https://github.com/WebAssembly/function-references/blob/master/proposals/function-references/Overview.md).
///
/// - **Host Registrations**
/// - `Wasi` turns on the `WASI` support.
///
/// - `WasmEdgeProcess` turns on the `wasmedge_process` support.
///
/// - **Memory Management**
/// - `maximum_memory_page` limits the page size of [Memory](crate::Memory).
///
/// - **AOT Compilation**
///
/// The AOT compiler options configure the behavior about optimization level, output format, dump IR,
/// and generic binary.
///
/// - Compiler Optimization Levels
/// - `O0` performs as many optimizations as possible.
///
/// - `O1` optimizes quickly without destroying debuggability.
///
/// - `02` optimizes for fast execution as much as possible without triggering significant incremental
/// compile time or code size growth.
///
/// - `O3` optimizes for fast execution as much as possible.
///
/// - `Os` optimizes for small code size as much as possible without triggering significant incremental
/// compile time or execution time slowdowns.
///
/// - `Oz` optimizes for small code size as much as possible.
///
/// - Compiler Output Formats
/// - `Native` specifies the output format is native dynamic library (`*.wasm.so`).
///
/// - `Wasm` specifies the output format is WebAssembly with AOT compiled codes in custom section (`*.wasm`).
///
/// - `dump_ir` determines if AOT compiler generates IR or not.
///
/// - `generic_binary` determines if AOT compiler generates the generic binary or not.
///
/// - `interruptible` determines if AOT compiler generates interruptible binary or not.
///
/// The configuration options above are only effective to [Compiler](crate::Compiler).
///
/// - **Runtime Statistics**
/// - `instr_counting` determines if measuring the count of instructions when running a compiled or pure WASM.
///
/// - `cost_measuring` determines if measuring the instruction costs when running a compiled or pure WASM.
///
/// - `time_measuring` determines if measuring the running time when running a compiled or pure WASM.
///
/// API users can first set the options of interest, such as those related to the WebAssembly proposals,
/// host registrations, AOT compiler options, and etc., then apply the configuration
/// to create other WasmEdge runtime structs.
#[derive(Debug, Clone)]
pub struct Config {
pub(crate) inner: std::sync::Arc<InnerConfig>,
#[cfg(all(feature = "async", target_os = "linux"))]
async_wasi_enabled: bool,
}
impl Drop for Config {
fn drop(&mut self) {
if std::sync::Arc::strong_count(&self.inner) == 1 && !self.inner.0.is_null() {
unsafe { ffi::WasmEdge_ConfigureDelete(self.inner.0) };
}
}
}
impl Config {
/// Creates a new [Config](crate::Config).
///
/// # Error
///
/// If fail to create, then an error is returned.
pub fn create() -> WasmEdgeResult<Self> {
let ctx = unsafe { ffi::WasmEdge_ConfigureCreate() };
match ctx.is_null() {
true => Err(Box::new(WasmEdgeError::ConfigCreate)),
false => Ok(Self {
inner: std::sync::Arc::new(InnerConfig(ctx)),
#[cfg(all(feature = "async", target_os = "linux"))]
async_wasi_enabled: false,
}),
}
}
/// Enables or disables host registration wasi. By default, the option is disabled.
///
/// # Argument
///
/// * `enable` - Whether the option turns on or not.
pub fn wasi(&mut self, enable: bool) {
#[cfg(not(feature = "async"))]
unsafe {
if enable {
ffi::WasmEdge_ConfigureAddHostRegistration(
self.inner.0,
ffi::WasmEdge_HostRegistration_Wasi,
)
} else {
ffi::WasmEdge_ConfigureRemoveHostRegistration(
self.inner.0,
ffi::WasmEdge_HostRegistration_Wasi,
)
}
}
#[cfg(all(feature = "async", target_os = "linux"))]
{
self.async_wasi_enabled = enable;
}
}
/// Checks if host registration wasi turns on or not.
pub fn wasi_enabled(&self) -> bool {
#[cfg(not(feature = "async"))]
unsafe {
ffi::WasmEdge_ConfigureHasHostRegistration(
self.inner.0,
ffi::WasmEdge_HostRegistration_Wasi,
)
}
#[cfg(all(feature = "async", target_os = "linux"))]
{
self.async_wasi_enabled
}
}
/// Sets the maximum number of the memory pages available.
///
/// # Argument
///
/// * `count` - The page count (64KB per page).
pub fn set_max_memory_pages(&mut self, count: u32) {
unsafe { ffi::WasmEdge_ConfigureSetMaxMemoryPage(self.inner.0, count) }
}
/// Returns the number of the memory pages available.
pub fn get_max_memory_pages(&self) -> u32 {
unsafe { ffi::WasmEdge_ConfigureGetMaxMemoryPage(self.inner.0) }
}
/// Enables or disables the ImportExportMutGlobals option. By default, the option is enabled.
///
/// # Argument
///
/// * `enable` - Whether the option turns on or not.
pub fn mutable_globals(&mut self, enable: bool) {
unsafe {
if enable {
ffi::WasmEdge_ConfigureAddProposal(
self.inner.0,
ffi::WasmEdge_Proposal_ImportExportMutGlobals,
)
} else {
ffi::WasmEdge_ConfigureRemoveProposal(
self.inner.0,
ffi::WasmEdge_Proposal_ImportExportMutGlobals,
)
}
}
}
/// Checks if the ImportExportMutGlobals option turns on or not.
pub fn mutable_globals_enabled(&self) -> bool {
unsafe {
ffi::WasmEdge_ConfigureHasProposal(
self.inner.0,
ffi::WasmEdge_Proposal_ImportExportMutGlobals,
)
}
}
/// Enables or disables the NonTrapFloatToIntConversions option. By default, the option is enabled.
///
/// # Argument
///
/// * `enable` - Whether the option turns on or not.
pub fn non_trap_conversions(&mut self, enable: bool) {
unsafe {
if enable {
ffi::WasmEdge_ConfigureAddProposal(
self.inner.0,
ffi::WasmEdge_Proposal_NonTrapFloatToIntConversions,
)
} else {
ffi::WasmEdge_ConfigureRemoveProposal(
self.inner.0,
ffi::WasmEdge_Proposal_NonTrapFloatToIntConversions,
)
}
}
}
/// Checks if the NonTrapFloatToIntConversions option turns on or not.
pub fn non_trap_conversions_enabled(&self) -> bool {
unsafe {
ffi::WasmEdge_ConfigureHasProposal(
self.inner.0,
ffi::WasmEdge_Proposal_NonTrapFloatToIntConversions,
)
}
}
/// Enables or disables the SignExtensionOperators option. By default, the option is enabled.
///
/// # Argument
///
/// * `enable` - Whether the option turns on or not.
pub fn sign_extension_operators(&mut self, enable: bool) {
unsafe {
if enable {
ffi::WasmEdge_ConfigureAddProposal(
self.inner.0,
ffi::WasmEdge_Proposal_SignExtensionOperators,
)
} else {
ffi::WasmEdge_ConfigureRemoveProposal(
self.inner.0,
ffi::WasmEdge_Proposal_SignExtensionOperators,
)
}
}
}
/// Checks if the SignExtensionOperators option turns on or not.
pub fn sign_extension_operators_enabled(&self) -> bool {
unsafe {
ffi::WasmEdge_ConfigureHasProposal(
self.inner.0,
ffi::WasmEdge_Proposal_SignExtensionOperators,
)
}
}
/// Enables or disables the MultiValue option. By default, the option is enabled.
///
/// # Argument
///
/// * `enable` - Whether the option turns on or not.
pub fn multi_value(&mut self, enable: bool) {
unsafe {
if enable {
ffi::WasmEdge_ConfigureAddProposal(self.inner.0, ffi::WasmEdge_Proposal_MultiValue)
} else {
ffi::WasmEdge_ConfigureRemoveProposal(
self.inner.0,
ffi::WasmEdge_Proposal_MultiValue,
)
}
}
}
/// Checks if the MultiValue option turns on or not.
pub fn multi_value_enabled(&self) -> bool {
unsafe {
ffi::WasmEdge_ConfigureHasProposal(self.inner.0, ffi::WasmEdge_Proposal_MultiValue)
}
}
/// Enables or disables the BulkMemoryOperations option. By default, the option is enabled.
///
/// # Argument
///
/// * `enable` - Whether the option turns on or not.
pub fn bulk_memory_operations(&mut self, enable: bool) {
unsafe {
if enable {
ffi::WasmEdge_ConfigureAddProposal(
self.inner.0,
ffi::WasmEdge_Proposal_BulkMemoryOperations,
)
} else {
ffi::WasmEdge_ConfigureRemoveProposal(
self.inner.0,
ffi::WasmEdge_Proposal_BulkMemoryOperations,
)
}
}
}
/// Checks if the BulkMemoryOperations option turns on or not.
pub fn bulk_memory_operations_enabled(&self) -> bool {
unsafe {
ffi::WasmEdge_ConfigureHasProposal(
self.inner.0,
ffi::WasmEdge_Proposal_BulkMemoryOperations,
)
}
}
/// Enables or disables the ReferenceTypes option. By default, the option is enabled.
///
/// # Argument
///
/// * `enable` - Whether the option turns on or not.
pub fn reference_types(&mut self, enable: bool) {
unsafe {
if enable {
ffi::WasmEdge_ConfigureAddProposal(
self.inner.0,
ffi::WasmEdge_Proposal_ReferenceTypes,
)
} else {
ffi::WasmEdge_ConfigureRemoveProposal(
self.inner.0,
ffi::WasmEdge_Proposal_ReferenceTypes,
)
}
}
}
/// Checks if the ReferenceTypes option turns on or not.
pub fn reference_types_enabled(&self) -> bool {
unsafe {
ffi::WasmEdge_ConfigureHasProposal(self.inner.0, ffi::WasmEdge_Proposal_ReferenceTypes)
}
}
/// Enables or disables the SIMD option. By default, the option is enabled.
///
/// # Argument
///
/// * `enable` - Whether the option turns on or not.
pub fn simd(&mut self, enable: bool) {
unsafe {
if enable {
ffi::WasmEdge_ConfigureAddProposal(self.inner.0, ffi::WasmEdge_Proposal_SIMD)
} else {
ffi::WasmEdge_ConfigureRemoveProposal(self.inner.0, ffi::WasmEdge_Proposal_SIMD)
}
}
}
/// Checks if the SIMD option turns on or not.
pub fn simd_enabled(&self) -> bool {
unsafe { ffi::WasmEdge_ConfigureHasProposal(self.inner.0, ffi::WasmEdge_Proposal_SIMD) }
}
/// Enables or disables the TailCall option. By default, the option is disabled.
///
/// # Argument
///
/// * `enable` - Whether the option turns on or not.
pub fn tail_call(&mut self, enable: bool) {
unsafe {
if enable {
ffi::WasmEdge_ConfigureAddProposal(self.inner.0, ffi::WasmEdge_Proposal_TailCall)
} else {
ffi::WasmEdge_ConfigureRemoveProposal(self.inner.0, ffi::WasmEdge_Proposal_TailCall)
}
}
}
/// Checks if the TailCall option turns on or not.
pub fn tail_call_enabled(&self) -> bool {
unsafe { ffi::WasmEdge_ConfigureHasProposal(self.inner.0, ffi::WasmEdge_Proposal_TailCall) }
}
/// Enables or disables the Annotations option. By default, the option is disabled.
///
/// # Argument
///
/// * `enable` - Whether the option turns on or not.
pub fn annotations(&mut self, enable: bool) {
unsafe {
if enable {
ffi::WasmEdge_ConfigureAddProposal(self.inner.0, ffi::WasmEdge_Proposal_Annotations)
} else {
ffi::WasmEdge_ConfigureRemoveProposal(
self.inner.0,
ffi::WasmEdge_Proposal_Annotations,
)
}
}
}
/// Checks if the Annotations option turns on or not.
pub fn annotations_enabled(&self) -> bool {
unsafe {
ffi::WasmEdge_ConfigureHasProposal(self.inner.0, ffi::WasmEdge_Proposal_Annotations)
}
}
/// Enables or disables the Memory64 option. By default, the option is disabled.
///
/// # Argument
///
/// * `enable` - Whether the option turns on or not.
pub fn memory64(&mut self, enable: bool) {
unsafe {
if enable {
ffi::WasmEdge_ConfigureAddProposal(self.inner.0, ffi::WasmEdge_Proposal_Memory64)
} else {
ffi::WasmEdge_ConfigureRemoveProposal(self.inner.0, ffi::WasmEdge_Proposal_Memory64)
}
}
}
/// Checks if the Memory64 option turns on or not.
pub fn memory64_enabled(&self) -> bool {
unsafe { ffi::WasmEdge_ConfigureHasProposal(self.inner.0, ffi::WasmEdge_Proposal_Memory64) }
}
/// Enables or disables the Threads option. By default, the option is disabled.
///
/// # Argument
///
/// * `enable` - Whether the option turns on or not.
pub fn threads(&mut self, enable: bool) {
unsafe {
if enable {
ffi::WasmEdge_ConfigureAddProposal(self.inner.0, ffi::WasmEdge_Proposal_Threads)
} else {
ffi::WasmEdge_ConfigureRemoveProposal(self.inner.0, ffi::WasmEdge_Proposal_Threads)
}
}
}
/// Checks if the Threads option turns on or not.
pub fn threads_enabled(&self) -> bool {
unsafe { ffi::WasmEdge_ConfigureHasProposal(self.inner.0, ffi::WasmEdge_Proposal_Threads) }
}
/// Enables or disables the ExceptionHandling option. By default, the option is disabled.
///
/// # Argument
///
/// * `enable` - Whether the option turns on or not.
pub fn exception_handling(&mut self, enable: bool) {
unsafe {
if enable {
ffi::WasmEdge_ConfigureAddProposal(
self.inner.0,
ffi::WasmEdge_Proposal_ExceptionHandling,
)
} else {
ffi::WasmEdge_ConfigureRemoveProposal(
self.inner.0,
ffi::WasmEdge_Proposal_ExceptionHandling,
)
}
}
}
/// Checks if the ExceptionHandling option turns on or not.
pub fn exception_handling_enabled(&self) -> bool {
unsafe {
ffi::WasmEdge_ConfigureHasProposal(
self.inner.0,
ffi::WasmEdge_Proposal_ExceptionHandling,
)
}
}
/// Enables or disables the FunctionReferences option. By default, the option is disabled.
///
/// # Argument
///
/// * `enable` - Whether the option turns on or not.
pub fn function_references(&mut self, enable: bool) {
unsafe {
if enable {
ffi::WasmEdge_ConfigureAddProposal(
self.inner.0,
ffi::WasmEdge_Proposal_FunctionReferences,
)
} else {
ffi::WasmEdge_ConfigureRemoveProposal(
self.inner.0,
ffi::WasmEdge_Proposal_FunctionReferences,
)
}
}
}
/// Checks if the FunctionReferences option turns on or not.
pub fn function_references_enabled(&self) -> bool {
unsafe {
ffi::WasmEdge_ConfigureHasProposal(
self.inner.0,
ffi::WasmEdge_Proposal_FunctionReferences,
)
}
}
/// Enables or disables the MultiMemories option. By default, the option is disabled.
///
/// # Argument
///
/// * `enable` - Whether the option turns on or not.
pub fn multi_memories(&mut self, enable: bool) {
unsafe {
if enable {
ffi::WasmEdge_ConfigureAddProposal(
self.inner.0,
ffi::WasmEdge_Proposal_MultiMemories,
)
} else {
ffi::WasmEdge_ConfigureRemoveProposal(
self.inner.0,
ffi::WasmEdge_Proposal_MultiMemories,
)
}
}
}
/// Checks if the MultiMemories option turns on or not.
pub fn multi_memories_enabled(&self) -> bool {
unsafe {
ffi::WasmEdge_ConfigureHasProposal(self.inner.0, ffi::WasmEdge_Proposal_MultiMemories)
}
}
/// Enables or disables the `ForceInterpreter` option. By default, the option is disabled.
///
/// # Argument
///
/// * `enable` - Whether the option turns on or not.
pub fn interpreter_mode(&mut self, enable: bool) {
unsafe { ffi::WasmEdge_ConfigureSetForceInterpreter(self.inner.0, enable) }
}
/// Checks if the `ForceInterpreter` option turns on or not.
pub fn interpreter_mode_enabled(&self) -> bool {
unsafe { ffi::WasmEdge_ConfigureIsForceInterpreter(self.inner.0) }
}
// For AOT compiler
/// Sets the optimization level of AOT compiler. By default, the optimization level is `O3`.
///
/// Notice that this function is only available when the `aot` feature is enabled.
///
/// # Argument
///
/// * `opt_level` - The optimization level of AOT compiler.
#[cfg(feature = "aot")]
#[cfg_attr(docsrs, doc(cfg(feature = "aot")))]
pub fn set_aot_optimization_level(&mut self, opt_level: CompilerOptimizationLevel) {
unsafe {
ffi::WasmEdge_ConfigureCompilerSetOptimizationLevel(
self.inner.0,
opt_level as ffi::WasmEdge_CompilerOptimizationLevel,
)
}
}
/// Returns the optimization level of AOT compiler.
///
/// Notice that this function is only available when the `aot` feature is enabled.
#[cfg(feature = "aot")]
#[cfg_attr(docsrs, doc(cfg(feature = "aot")))]
pub fn get_aot_optimization_level(&self) -> CompilerOptimizationLevel {
let level = unsafe { ffi::WasmEdge_ConfigureCompilerGetOptimizationLevel(self.inner.0) };
level.into()
}
/// Sets the output binary format of AOT compiler. By default, the compiler output format is `Wasm`.
///
/// Notice that this function is only available when the `aot` feature is enabled.
///
/// # Argument
///
/// * `format` - The format of the output binary.
#[cfg(feature = "aot")]
#[cfg_attr(docsrs, doc(cfg(feature = "aot")))]
pub fn set_aot_compiler_output_format(&mut self, format: CompilerOutputFormat) {
unsafe {
ffi::WasmEdge_ConfigureCompilerSetOutputFormat(
self.inner.0,
format as ffi::WasmEdge_CompilerOutputFormat,
)
}
}
/// Returns the output binary format of AOT compiler.
///
/// Notice that this function is only available when the `aot` feature is enabled.
#[cfg(feature = "aot")]
#[cfg_attr(docsrs, doc(cfg(feature = "aot")))]
pub fn get_aot_compiler_output_format(&self) -> CompilerOutputFormat {
let value = unsafe { ffi::WasmEdge_ConfigureCompilerGetOutputFormat(self.inner.0) };
value.into()
}
/// Sets the dump IR option of AOT compiler. By default, the option is disabled.
///
/// Notice that this function is only available when the `aot` feature is enabled.
///
/// # Argument
///
/// * `flag` - Whether dump ir or not.
#[cfg(feature = "aot")]
#[cfg_attr(docsrs, doc(cfg(feature = "aot")))]
pub fn dump_ir(&mut self, flag: bool) {
unsafe { ffi::WasmEdge_ConfigureCompilerSetDumpIR(self.inner.0, flag) }
}
/// Checks if the dump IR option turns on or not.
///
/// Notice that this function is only available when the `aot` feature is enabled.
#[cfg(feature = "aot")]
#[cfg_attr(docsrs, doc(cfg(feature = "aot")))]
pub fn dump_ir_enabled(&self) -> bool {
unsafe { ffi::WasmEdge_ConfigureCompilerIsDumpIR(self.inner.0) }
}
/// Sets the generic binary option of AOT compiler. By default, the option is disabled.
///
/// Notice that this function is only available when the `aot` feature is enabled.
///
/// # Argument
///
/// * `flag` - Whether generate the generic binary or not when perform AOT compilation.
#[cfg(feature = "aot")]
#[cfg_attr(docsrs, doc(cfg(feature = "aot")))]
pub fn generic_binary(&mut self, flag: bool) {
unsafe { ffi::WasmEdge_ConfigureCompilerSetGenericBinary(self.inner.0, flag) }
}
/// Checks if the generic binary option of AOT compiler turns on or not.
///
/// Notice that this function is only available when the `aot` feature is enabled.
#[cfg(feature = "aot")]
#[cfg_attr(docsrs, doc(cfg(feature = "aot")))]
pub fn generic_binary_enabled(&self) -> bool {
unsafe { ffi::WasmEdge_ConfigureCompilerIsGenericBinary(self.inner.0) }
}
/// Enables or Disables the `Interruptible` option of AOT compiler. This option determines to generate interruptible binary or not when compilation in AOT compiler. By default, the option is disabled.
///
/// Notice that this function is only available when the `aot` feature is enabled.
///
/// # Argument
///
/// * `enable` - Whether turn on the `Interruptible` option.
#[cfg(feature = "aot")]
#[cfg_attr(docsrs, doc(cfg(feature = "aot")))]
pub fn interruptible(&mut self, enable: bool) {
unsafe { ffi::WasmEdge_ConfigureCompilerSetInterruptible(self.inner.0, enable) }
}
/// Checks if the `Interruptible` option of AOT Compiler turns on or not.
///
/// Notice that this function is only available when the `aot` feature is enabled.
#[cfg(feature = "aot")]
#[cfg_attr(docsrs, doc(cfg(feature = "aot")))]
pub fn interruptible_enabled(&self) -> bool {
unsafe { ffi::WasmEdge_ConfigureCompilerIsInterruptible(self.inner.0) }
}
// For Statistics
/// Sets the instruction counting option. By default, the option is disabled.
///
/// # Argument
///
/// * `flag` - Whether support instruction counting or not when execution after AOT compilation.
pub fn count_instructions(&mut self, flag: bool) {
unsafe { ffi::WasmEdge_ConfigureStatisticsSetInstructionCounting(self.inner.0, flag) }
}
/// Checks if the instruction counting option turns on or not.
pub fn is_instruction_counting(&self) -> bool {
unsafe { ffi::WasmEdge_ConfigureStatisticsIsInstructionCounting(self.inner.0) }
}
/// Sets the cost measuring option. By default, the option is disabled.
///
/// # Argument
///
/// * `flag` - Whether support cost measuring or not when execution after AOT compilation.
pub fn measure_cost(&mut self, flag: bool) {
unsafe { ffi::WasmEdge_ConfigureStatisticsSetCostMeasuring(self.inner.0, flag) }
}
/// Checks if the cost measuring option turns on or not.
pub fn is_cost_measuring(&self) -> bool {
unsafe { ffi::WasmEdge_ConfigureStatisticsIsCostMeasuring(self.inner.0) }
}
/// Sets the time measuring option. By default, the option is disabled.
///
/// # Argument
///
/// * `flag` - Whether support time measuring or not when execution after AOT compilation.
pub fn measure_time(&mut self, flag: bool) {
unsafe { ffi::WasmEdge_ConfigureStatisticsSetTimeMeasuring(self.inner.0, flag) }
}
/// Checks if the time measuring option turns on or not.
pub fn is_time_measuring(&self) -> bool {
unsafe { ffi::WasmEdge_ConfigureStatisticsIsTimeMeasuring(self.inner.0) }
}
/// Provides a raw pointer to the inner Configure context.
#[cfg(feature = "ffi")]
#[cfg_attr(docsrs, doc(cfg(feature = "ffi")))]
pub fn as_ptr(&self) -> *const ffi::WasmEdge_ConfigureContext {
self.inner.0 as *const _
}
}
#[derive(Debug)]
pub(crate) struct InnerConfig(pub(crate) *mut ffi::WasmEdge_ConfigureContext);
unsafe impl Send for InnerConfig {}
unsafe impl Sync for InnerConfig {}
#[cfg(test)]
mod tests {
use super::*;
use std::{
borrow::BorrowMut,
sync::{Arc, Mutex},
thread,
};
#[test]
fn test_config_options() {
// create a Config instance
let result = Config::create();
assert!(result.is_ok());
let mut config = result.unwrap();
// check default settings
assert!(!config.multi_memories_enabled());
assert!(!config.annotations_enabled());
assert!(config.bulk_memory_operations_enabled());
assert!(!config.exception_handling_enabled());
assert!(!config.function_references_enabled());
assert!(!config.memory64_enabled());
assert!(config.multi_value_enabled());
assert!(config.mutable_globals_enabled());
assert!(config.non_trap_conversions_enabled());
assert!(config.sign_extension_operators_enabled());
assert!(config.reference_types_enabled());
assert!(config.simd_enabled());
assert!(!config.tail_call_enabled());
assert!(!config.threads_enabled());
#[cfg(not(feature = "async"))]
assert!(!config.wasi_enabled());
assert!(!config.is_cost_measuring());
#[cfg(feature = "aot")]
assert!(!config.dump_ir_enabled());
#[cfg(feature = "aot")]
#[cfg(feature = "aot")]
assert!(!config.generic_binary_enabled());
assert!(!config.is_instruction_counting());
assert!(!config.is_time_measuring());
assert_eq!(config.get_max_memory_pages(), 65536);
#[cfg(feature = "aot")]
assert_eq!(
config.get_aot_optimization_level(),
CompilerOptimizationLevel::O3,
);
#[cfg(feature = "aot")]
assert_eq!(
config.get_aot_compiler_output_format(),
CompilerOutputFormat::Wasm,
);
assert!(!config.interpreter_mode_enabled());
#[cfg(feature = "aot")]
assert!(!config.interruptible_enabled());
// set options
config.multi_memories(true);
config.annotations(true);
config.bulk_memory_operations(false);
config.exception_handling(true);
config.function_references(true);
config.memory64(true);
config.multi_value(false);
config.mutable_globals(false);
config.non_trap_conversions(false);
config.sign_extension_operators(false);
config.reference_types(false);
config.simd(false);
config.tail_call(true);
config.threads(true);
config.measure_cost(true);
config.measure_time(true);
#[cfg(feature = "aot")]
config.dump_ir(true);
#[cfg(feature = "aot")]
config.generic_binary(true);
config.count_instructions(true);
config.interpreter_mode(true);
// check new settings
assert!(config.multi_memories_enabled());
assert!(config.annotations_enabled());
assert!(!config.bulk_memory_operations_enabled());
assert!(config.exception_handling_enabled());
assert!(config.function_references_enabled());
assert!(config.memory64_enabled());
assert!(!config.multi_value_enabled());
assert!(!config.mutable_globals_enabled());
assert!(!config.non_trap_conversions_enabled());
assert!(!config.sign_extension_operators_enabled());
assert!(!config.reference_types_enabled());
assert!(!config.simd_enabled());
assert!(config.tail_call_enabled());
assert!(config.threads_enabled());
assert!(config.is_cost_measuring());
#[cfg(feature = "aot")]
assert!(config.dump_ir_enabled());
#[cfg(feature = "aot")]
assert!(config.generic_binary_enabled());
assert!(config.is_instruction_counting());
assert!(config.is_time_measuring());
assert!(config.interpreter_mode_enabled());
// set maximum memory pages
config.set_max_memory_pages(10);
assert_eq!(config.get_max_memory_pages(), 10);
#[cfg(feature = "aot")]
config.set_aot_optimization_level(CompilerOptimizationLevel::Oz);
#[cfg(feature = "aot")]
assert_eq!(
config.get_aot_optimization_level(),
CompilerOptimizationLevel::Oz
);
#[cfg(feature = "aot")]
config.set_aot_compiler_output_format(CompilerOutputFormat::Native);
#[cfg(feature = "aot")]
assert_eq!(
config.get_aot_compiler_output_format(),
CompilerOutputFormat::Native,
);
}
#[test]
fn test_config_send() {
// create a Config instance
let result = Config::create();
assert!(result.is_ok());
let mut config = result.unwrap();
let handle = thread::spawn(move || {
// check default settings
assert!(!config.multi_memories_enabled());
assert!(!config.annotations_enabled());
assert!(config.bulk_memory_operations_enabled());
assert!(!config.exception_handling_enabled());
assert!(!config.function_references_enabled());
assert!(!config.memory64_enabled());
assert!(config.reference_types_enabled());
assert!(config.simd_enabled());
assert!(!config.tail_call_enabled());
assert!(!config.threads_enabled());
assert!(!config.is_cost_measuring());
#[cfg(feature = "aot")]
assert!(!config.dump_ir_enabled());
#[cfg(feature = "aot")]
assert!(!config.generic_binary_enabled());
assert!(!config.is_instruction_counting());
assert!(!config.is_time_measuring());
assert_eq!(config.get_max_memory_pages(), 65536);
#[cfg(feature = "aot")]
assert_eq!(
config.get_aot_optimization_level(),
CompilerOptimizationLevel::O3,
);
#[cfg(feature = "aot")]
assert_eq!(
config.get_aot_compiler_output_format(),
CompilerOutputFormat::Wasm,
);
// set options
config.multi_memories(true);
config.annotations(true);
config.bulk_memory_operations(false);
config.exception_handling(true);
config.function_references(true);
config.memory64(true);
config.reference_types(false);
config.simd(false);
config.tail_call(true);
config.threads(true);
config.measure_cost(true);
config.measure_time(true);
#[cfg(feature = "aot")]
config.dump_ir(true);
#[cfg(feature = "aot")]
config.generic_binary(true);
config.count_instructions(true);
// check new settings
assert!(config.multi_memories_enabled());
assert!(config.annotations_enabled());
assert!(!config.bulk_memory_operations_enabled());
assert!(config.exception_handling_enabled());
assert!(config.function_references_enabled());
assert!(config.memory64_enabled());
assert!(!config.reference_types_enabled());
assert!(!config.simd_enabled());
assert!(config.tail_call_enabled());
assert!(config.threads_enabled());
assert!(config.is_cost_measuring());
#[cfg(feature = "aot")]
assert!(config.dump_ir_enabled());
#[cfg(feature = "aot")]
assert!(config.generic_binary_enabled());
assert!(config.is_instruction_counting());
assert!(config.is_time_measuring());
});
handle.join().unwrap();
}
#[test]
fn test_config_sync() {
// create a Config instance
let result = Config::create();
assert!(result.is_ok());
let config = Arc::new(Mutex::new(result.unwrap()));
let config_cloned = Arc::clone(&config);
let handle = thread::spawn(move || {
let result = config_cloned.lock();
assert!(result.is_ok());
let mut config = result.unwrap();
// check default settings
assert!(!config.multi_memories_enabled());
assert!(!config.annotations_enabled());
assert!(config.bulk_memory_operations_enabled());
assert!(!config.exception_handling_enabled());
assert!(!config.function_references_enabled());
assert!(!config.memory64_enabled());
assert!(config.reference_types_enabled());
assert!(config.simd_enabled());
assert!(!config.tail_call_enabled());
assert!(!config.threads_enabled());
assert!(!config.is_cost_measuring());
#[cfg(feature = "aot")]
assert!(!config.dump_ir_enabled());
#[cfg(feature = "aot")]
assert!(!config.generic_binary_enabled());
assert!(!config.is_instruction_counting());
assert!(!config.is_time_measuring());
assert_eq!(config.get_max_memory_pages(), 65536);
#[cfg(feature = "aot")]
assert_eq!(
config.get_aot_optimization_level(),
CompilerOptimizationLevel::O3,
);
#[cfg(feature = "aot")]
assert_eq!(
config.get_aot_compiler_output_format(),
CompilerOutputFormat::Wasm,
);
// set options
let config_mut = config.borrow_mut();
config_mut.multi_memories(true);
config_mut.annotations(true);
config_mut.bulk_memory_operations(false);
config_mut.exception_handling(true);
config_mut.function_references(true);
config_mut.memory64(true);
config_mut.reference_types(false);
config_mut.simd(false);
config_mut.tail_call(true);
config_mut.threads(true);
config_mut.measure_cost(true);
config_mut.measure_time(true);
#[cfg(feature = "aot")]
config_mut.dump_ir(true);
#[cfg(feature = "aot")]
config_mut.generic_binary(true);
config_mut.count_instructions(true);
// check new settings
assert!(config.multi_memories_enabled());
assert!(config.annotations_enabled());
assert!(!config.bulk_memory_operations_enabled());
assert!(config.exception_handling_enabled());
assert!(config.function_references_enabled());
assert!(config.memory64_enabled());
assert!(!config.reference_types_enabled());
assert!(!config.simd_enabled());
assert!(config.tail_call_enabled());
assert!(config.threads_enabled());
assert!(config.is_cost_measuring());
#[cfg(feature = "aot")]
assert!(config.dump_ir_enabled());
#[cfg(feature = "aot")]
assert!(config.generic_binary_enabled());
assert!(config.is_instruction_counting());
assert!(config.is_time_measuring());
});
handle.join().unwrap();
}
#[test]
fn test_config_clone() {
// create a Config instance
let result = Config::create();
assert!(result.is_ok());
let mut config = result.unwrap();
assert_eq!(std::sync::Arc::strong_count(&config.inner), 1);
// set options
config.multi_memories(true);
config.annotations(true);
config.bulk_memory_operations(false);
config.exception_handling(true);
config.function_references(true);
config.memory64(true);
config.multi_value(false);
config.mutable_globals(false);
config.non_trap_conversions(false);
config.sign_extension_operators(false);
config.reference_types(false);
config.simd(false);
config.tail_call(true);
config.threads(true);
config.measure_cost(true);
config.measure_time(true);
#[cfg(feature = "aot")]
config.dump_ir(true);
#[cfg(feature = "aot")]
config.generic_binary(true);
config.count_instructions(true);
let config_clone = config.clone();
assert_eq!(std::sync::Arc::strong_count(&config.inner), 2);
// check new settings
assert!(config_clone.multi_memories_enabled());
assert!(config_clone.annotations_enabled());
assert!(!config_clone.bulk_memory_operations_enabled());
assert!(config_clone.exception_handling_enabled());
assert!(config_clone.function_references_enabled());
assert!(config_clone.memory64_enabled());
assert!(!config_clone.multi_value_enabled());
assert!(!config_clone.mutable_globals_enabled());
assert!(!config_clone.non_trap_conversions_enabled());
assert!(!config_clone.sign_extension_operators_enabled());
assert!(!config_clone.reference_types_enabled());
assert!(!config_clone.simd_enabled());
assert!(config_clone.tail_call_enabled());
assert!(config_clone.threads_enabled());
assert!(config_clone.is_cost_measuring());
#[cfg(feature = "aot")]
assert!(config_clone.dump_ir_enabled());
#[cfg(feature = "aot")]
assert!(config_clone.generic_binary_enabled());
assert!(config_clone.is_instruction_counting());
assert!(config_clone.is_time_measuring());
drop(config);
assert_eq!(std::sync::Arc::strong_count(&config_clone.inner), 1);
drop(config_clone);
}
}