Skip to main content

wasmer_c_api/wasm_c_api/unstable/
features.rs

1//! Unstable non-standard Wasmer-specific API that contains a Features
2//! API for the engine and the compiler.
3//!
4//!
5//! # Example
6//!
7//! ```rust
8//! # use inline_c::assert_c;
9//! # fn main() {
10//! #    (assert_c! {
11//! # #include "tests/wasmer.h"
12//! #
13//! int main() {
14//!     // Declare features.
15//!     wasmer_features_t* features = wasmer_features_new();
16//!
17//!     // Now, let's enable the SIMD feature.
18//!     wasmer_features_simd(features, true);
19//!
20//!     // And also the memory64 feature.     
21//!     wasmer_features_memory64(features, true);
22//!
23//!     wasmer_features_delete(features);
24//!
25//!     return 0;
26//! }
27//! #    })
28//! #    .success();
29//! # }
30//! ```
31//!
32//! To go further, see
33//! [`wasm_config_set_features`](super::engine::wasm_config_set_features).
34
35use wasmer_types::Features;
36
37/// Controls which experimental features will be enabled.
38/// Features usually have a corresponding [WebAssembly proposal].
39///
40/// [WebAssembly proposal]: https://github.com/WebAssembly/proposals
41///
42/// # Example
43///
44/// See the module's documentation.
45#[derive(Debug)]
46#[allow(non_camel_case_types)]
47pub struct wasmer_features_t {
48    pub(crate) inner: Features,
49}
50
51/// Creates a new [`wasmer_features_t`].
52///
53/// # Example
54///
55/// See the module's documentation.
56#[unsafe(no_mangle)]
57pub extern "C" fn wasmer_features_new() -> Box<wasmer_features_t> {
58    Box::new(wasmer_features_t {
59        inner: Features::new(),
60    })
61}
62
63/// Delete a [`wasmer_features_t`].
64///
65/// # Example
66///
67/// See the module's documentation.
68#[unsafe(no_mangle)]
69pub extern "C" fn wasmer_features_delete(_features: Option<Box<wasmer_features_t>>) {}
70
71/// Configures whether the WebAssembly threads proposal will be enabled.
72///
73/// The [WebAssembly threads proposal][threads] is not currently fully
74/// standardized and is undergoing development. Support for this feature can
75/// be enabled through this method for appropriate WebAssembly modules.
76///
77/// This feature gates items such as shared memories and atomic
78/// instructions.
79///
80/// This is `true` by default.
81///
82/// [threads]: https://github.com/webassembly/threads
83///
84/// # Example
85///
86/// See the module's documentation.
87#[unsafe(no_mangle)]
88pub extern "C" fn wasmer_features_threads(
89    features: Option<&mut wasmer_features_t>,
90    enable: bool,
91) -> bool {
92    let features = match features {
93        Some(features) => features,
94        _ => return false,
95    };
96
97    features.inner.threads(enable);
98
99    true
100}
101
102/// Configures whether the WebAssembly reference types proposal will be
103/// enabled.
104///
105/// The [WebAssembly reference types proposal][proposal] is now
106/// fully standardized and enabled by default.
107///
108/// This feature gates items such as the `externref` type and multiple tables
109/// being in a module. Note that enabling the reference types feature will
110/// also enable the bulk memory feature.
111///
112/// This is `true` by default.
113///
114/// [proposal]: https://github.com/webassembly/reference-types
115///
116/// # Example
117///
118/// See the module's documentation.
119#[unsafe(no_mangle)]
120pub extern "C" fn wasmer_features_reference_types(
121    features: Option<&mut wasmer_features_t>,
122    enable: bool,
123) -> bool {
124    let features = match features {
125        Some(features) => features,
126        _ => return false,
127    };
128
129    features.inner.reference_types(enable);
130
131    true
132}
133
134/// Configures whether the WebAssembly SIMD proposal will be
135/// enabled.
136///
137/// The [WebAssembly SIMD proposal][proposal] is now
138/// fully standardized.
139/// Support for this feature can be enabled through this method
140/// for appropriate WebAssembly modules.
141///
142/// This feature gates items such as the `v128` type and all of its
143/// operators being in a module.
144///
145/// This is `true` by default.
146///
147/// [proposal]: https://github.com/webassembly/simd
148///
149/// # Example
150///
151/// See the module's documentation.
152#[unsafe(no_mangle)]
153pub extern "C" fn wasmer_features_simd(
154    features: Option<&mut wasmer_features_t>,
155    enable: bool,
156) -> bool {
157    let features = match features {
158        Some(features) => features,
159        _ => return false,
160    };
161
162    features.inner.simd(enable);
163
164    true
165}
166
167/// Configures whether the WebAssembly bulk memory operations proposal will
168/// be enabled.
169///
170/// The [WebAssembly bulk memory operations proposal][proposal] is now
171/// fully standardized and enabled by default.
172///
173/// This feature gates items such as the `memory.copy` instruction, passive
174/// data/table segments, etc, being in a module.
175///
176/// This is `true` by default.
177///
178/// [proposal]: https://github.com/webassembly/bulk-memory-operations
179///
180/// # Example
181///
182/// See the module's documentation.
183#[unsafe(no_mangle)]
184pub extern "C" fn wasmer_features_bulk_memory(
185    features: Option<&mut wasmer_features_t>,
186    enable: bool,
187) -> bool {
188    let features = match features {
189        Some(features) => features,
190        _ => return false,
191    };
192
193    features.inner.bulk_memory(enable);
194
195    true
196}
197
198/// Configures whether the WebAssembly multi-value proposal will
199/// be enabled.
200///
201/// The [WebAssembly multi-value proposal][proposal] is now fully
202/// standardized and enabled by default.
203///
204/// This feature gates functions and blocks returning multiple values in a
205/// module, for example.
206///
207/// Singlepass support for multi-value is experimental and does not include
208/// integration with host functions returning multiple values.
209///
210/// This is `true` by default.
211///
212/// [proposal]: https://github.com/webassembly/multi-value
213///
214/// # Example
215///
216/// See the module's documentation.
217#[unsafe(no_mangle)]
218pub extern "C" fn wasmer_features_multi_value(
219    features: Option<&mut wasmer_features_t>,
220    enable: bool,
221) -> bool {
222    let features = match features {
223        Some(features) => features,
224        _ => return false,
225    };
226
227    features.inner.multi_value(enable);
228
229    true
230}
231
232/// Configures whether the WebAssembly tail-call proposal will
233/// be enabled.
234///
235/// The [WebAssembly tail-call proposal][proposal] is not
236/// currently fully standardized and is undergoing development.
237/// Support for this feature can be enabled through this method for
238/// appropriate WebAssembly modules.
239///
240/// This feature gates tail-call functions in WebAssembly.
241///
242/// This is `false` by default.
243///
244/// [proposal]: https://github.com/webassembly/tail-call
245///
246/// # Example
247///
248/// See the module's documentation.
249#[unsafe(no_mangle)]
250pub extern "C" fn wasmer_features_tail_call(
251    features: Option<&mut wasmer_features_t>,
252    enable: bool,
253) -> bool {
254    let features = match features {
255        Some(features) => features,
256        _ => return false,
257    };
258
259    features.inner.tail_call(enable);
260
261    true
262}
263
264/// Configures whether the WebAssembly module linking proposal will
265/// be enabled.
266///
267/// The [WebAssembly module linking proposal][proposal] is not
268/// currently fully standardized and is undergoing development.
269/// Support for this feature can be enabled through this method for
270/// appropriate WebAssembly modules.
271///
272/// This feature allows WebAssembly modules to define, import and
273/// export modules and instances.
274///
275/// This is `false` by default.
276///
277/// [proposal]: https://github.com/webassembly/module-linking
278///
279/// # Example
280///
281/// See the module's documentation.
282#[unsafe(no_mangle)]
283pub extern "C" fn wasmer_features_module_linking(
284    features: Option<&mut wasmer_features_t>,
285    enable: bool,
286) -> bool {
287    let features = match features {
288        Some(features) => features,
289        _ => return false,
290    };
291
292    features.inner.module_linking(enable);
293
294    true
295}
296
297/// Configures whether the WebAssembly multi-memory proposal will
298/// be enabled.
299///
300/// The [WebAssembly multi-memory proposal][proposal] is now
301/// fully standardized.
302/// Support for this feature can be enabled through this method
303/// for appropriate WebAssembly modules.
304///
305/// This feature adds the ability to use multiple memories within a
306/// single Wasm module.
307///
308/// This is `true` by default.
309///
310/// [proposal]: https://github.com/WebAssembly/multi-memory
311///
312/// # Example
313///
314/// See the module's documentation.
315#[unsafe(no_mangle)]
316pub extern "C" fn wasmer_features_multi_memory(
317    features: Option<&mut wasmer_features_t>,
318    enable: bool,
319) -> bool {
320    let features = match features {
321        Some(features) => features,
322        _ => return false,
323    };
324
325    features.inner.multi_memory(enable);
326
327    true
328}
329
330/// Configures whether the WebAssembly 64-bit memory proposal will
331/// be enabled.
332///
333/// The [WebAssembly 64-bit memory proposal][proposal] is now
334/// fully standardized.
335/// Support for this feature can be enabled through this method
336/// for appropriate WebAssembly modules.
337///
338/// This feature gates support for linear memory of sizes larger than
339/// 2^32 bits.
340///
341/// This is `false` by default.
342///
343/// [proposal]: https://github.com/WebAssembly/memory64
344///
345/// # Example
346///
347/// See the module's documentation.
348#[unsafe(no_mangle)]
349pub extern "C" fn wasmer_features_memory64(
350    features: Option<&mut wasmer_features_t>,
351    enable: bool,
352) -> bool {
353    let features = match features {
354        Some(features) => features,
355        _ => return false,
356    };
357
358    features.inner.memory64(enable);
359
360    true
361}
362
363/// Configures whether the WebAssembly exception-handling proposal will be enabled.
364///
365/// The [WebAssembly exception-handling proposal][eh] is now
366/// fully standardized.
367/// Support for this feature can be enabled through this method
368/// for appropriate WebAssembly modules.
369///
370/// This is `false` by default.
371///
372/// [eh]: https://github.com/webassembly/exception-handling
373///
374/// # Example
375///
376/// See the module's documentation.
377#[unsafe(no_mangle)]
378pub extern "C" fn wasmer_features_exceptions(
379    features: Option<&mut wasmer_features_t>,
380    enable: bool,
381) -> bool {
382    let features = match features {
383        Some(features) => features,
384        _ => return false,
385    };
386
387    features.inner.exceptions(enable);
388
389    true
390}
391
392/// Configures whether the WebAssembly Relaxed SIMD proposal will be
393/// enabled.
394///
395/// The [WebAssembly Relaxed SIMD proposal][proposal] is now
396/// fully standardized.
397/// Support for this feature can be enabled through this method
398/// for appropriate WebAssembly modules.
399///
400/// This is `false` by default.
401///
402/// [proposal]: https://github.com/WebAssembly/relaxed-simd
403///
404/// # Example
405///
406/// See the module's documentation.
407#[unsafe(no_mangle)]
408pub extern "C" fn wasmer_features_relaxed_simd(
409    features: Option<&mut wasmer_features_t>,
410    enable: bool,
411) -> bool {
412    let features = match features {
413        Some(features) => features,
414        _ => return false,
415    };
416
417    features.inner.relaxed_simd(enable);
418
419    true
420}
421
422/// Configures whether the WebAssembly Extended Constant Expressions proposal will be enabled.
423///
424/// The [WebAssembly Extended Constant Expressions][extended-const] proposal is
425/// now fully standardized.
426/// Support for this feature can be enabled through this method
427/// for appropriate WebAssembly modules.
428///
429/// This is `true` by default.
430///
431/// [extended-const]: https://github.com/WebAssembly/extended-const
432///
433/// # Example
434///
435/// See the module's documentation.
436#[unsafe(no_mangle)]
437pub extern "C" fn wasmer_features_extended_const(
438    features: Option<&mut wasmer_features_t>,
439    enable: bool,
440) -> bool {
441    let features = match features {
442        Some(features) => features,
443        _ => return false,
444    };
445
446    features.inner.extended_const(enable);
447
448    true
449}
450
451/// Configures whether the WebAssembly wide arithmetic proposal will be enabled.
452///
453/// The [Wide Arithmetic][wa] is not currently fully
454/// standardized and is undergoing development. Support for this feature can
455/// be enabled through this method for appropriate WebAssembly modules.
456///
457/// This is `false` by default.
458///
459/// [wa]: https://github.com/WebAssembly/wide-arithmetic
460///
461/// # Example
462///
463/// See the module's documentation.
464#[unsafe(no_mangle)]
465pub extern "C" fn wasmer_features_wide_arithmetic(
466    features: Option<&mut wasmer_features_t>,
467    enable: bool,
468) -> bool {
469    let features = match features {
470        Some(features) => features,
471        _ => return false,
472    };
473
474    features.inner.wide_arithmetic(enable);
475
476    true
477}