build/
build.rs

1// Simple example of how to use sophon-wasm builder api.
2// Builder api introduced as a method for fast generation of
3// different small wasm modules.
4
5extern crate sophon_wasm;
6
7use std::env;
8
9use sophon_wasm::builder;
10use sophon_wasm::elements;
11
12fn main() {
13
14    // Example binary accepts one parameter which is the output file
15    // where generated wasm module will be written at the end of execution
16    let args = env::args().collect::<Vec<_>>();
17    if args.len() != 2 {
18        println!("Usage: {} output_file.wasm", args[0]);
19        return;
20    }
21
22    // Main entry for the builder api is the module function
23    // It returns empty module builder structure which can be further
24    // appended with various wasm artefacts
25    let module = builder::module()
26        // Here we append function to the builder
27        // function() function returns a function builder attached
28        // to the module builder.
29        .function()
30            // We describe signature for the function via signature()
31            // function. In our simple example it's just one input
32            // argument of type 'i32' without return value
33            .signature().with_param(elements::ValueType::I32).build()
34            // body() without any further arguments means that the body
35            // of the function will be empty
36            .body().build()
37            // This is the end of the function builder. When `build()` is
38            // invoked, function builder returns original module builder
39            // from which it was invoked
40            .build()
41        // And finally we finish our module builder to produce actual
42        // wasm module.
43        .build();
44
45    // Module structure can be serialzed to produce a valid wasm file
46    sophon_wasm::serialize_to_file(&args[1], module).unwrap();
47}