### Running the gRPC Server Source: https://wasix.org/docs/language-guide/rust/tutorials/wasix-grpc This command demonstrates how to start the gRPC server for the helloworld example. It compiles and runs the server binary, which then listens for incoming connections on a specified address. ```bash $ cargo run --bin helloworld-server Finished dev [unoptimized + debuginfo] target(s) in 1.77s Running `target/debug/helloworld-server` Server listening on 127.0.0.1:50051 ``` -------------------------------- ### Run WASIX Binary with Flags Source: https://wasix.org/docs/language-guide/c/usage Example of running a WASIX binary with additional flags, such as enabling threads and network access. This demonstrates how to pass specific runtime options. ```Shell wasmer run --enable-threads --net ./target/wasix/debug/hello.wasm ``` -------------------------------- ### Basic Axum Application Setup Source: https://wasix.org/docs/language-guide/rust/tutorials/wasix-axum This Rust code defines a simple Axum web server. It sets up a single GET route '/' that is handled by the 'handler' function, which returns a static string. The server listens on `http://127.0.0.1:3000`. ```rust use axum::{routing::get, Router}; use std::net::SocketAddr; #[tokio::main] async fn main() { // Building our application with a single Route let app = Router::new().route("/", get(handler)); // Run the server with hyper on http://127.0.0.1:3000 let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); axum::Server::bind(&addr) .serve(app.into_make_service()) .await .unwrap(); } async fn handler() -> &'static str { "Hello, Axum ❤️ WASIX!" } ``` -------------------------------- ### Install WASIX for Rust Source: https://wasix.org/docs/language-guide/rust/installation Installs the WASIX toolchain and related components for Rust development using the cargo-wasix command-line tool. This requires the rustup toolchain manager to be installed. ```bash $ cargo install cargo-wasix ``` -------------------------------- ### WASIX Spawn Example Source: https://wasix.org/docs/language-guide/c/examples Illustrates the Spawn system call in WASIX for creating new processes. This is an alternative to Fork/Exec for process creation. ```c /* Spawn example code */ // Requires WASIX environment // Note: WASIX spawn might have specific implementations #include int main() { // Placeholder for spawn usage // The actual WASIX spawn API might differ from POSIX spawn printf("WASIX Spawn example placeholder.\n"); return 0; } ``` -------------------------------- ### WASIX Execve Example Source: https://wasix.org/docs/language-guide/c/examples Provides an example of the Execve system call in WASIX, used for executing a new program. This is fundamental for process management. ```c /* Execve example code */ // Requires WASIX environment #include int main() { char *args[] = {"/bin/program", NULL}; char *env[] = {NULL}; execve("/bin/program", args, env); // execve only returns on error return 1; } ``` -------------------------------- ### Create WASIX Readdir Project Directory Source: https://wasix.org/docs/language-guide/c/tutorials/readdir This snippet shows the shell commands to create a new directory for the WASIX readdir project and navigate into it. ```Shell mkdir wasix-readdir cd wasix-readdir ``` -------------------------------- ### WASIX Signal Example Source: https://wasix.org/docs/language-guide/c/examples Demonstrates handling signals using the Signal system call in WASIX. This example shows how to register a signal handler. ```c /* Signal example code */ // Requires WASIX environment #include #include #include void sigint_handler(int signum) { printf("\nReceived SIGINT (%d)\n", signum); } int main() { signal(SIGINT, sigint_handler); printf("Waiting for SIGINT...\n"); pause(); // Wait for a signal return 0; } ``` -------------------------------- ### Verify WASIX Toolchain Installation Source: https://wasix.org/docs/language-guide/rust/installation Checks if the WASIX toolchain has been correctly installed and is available in the rustup toolchain list. ```bash $ rustup toolchain list | grep wasix wasix ``` -------------------------------- ### WASIX Fork Longjmp Example Source: https://wasix.org/docs/language-guide/c/examples Demonstrates the interaction between the Fork and Longjmp system calls in WASIX. This example shows process creation and non-local goto. ```c /* Fork Longjmp example code */ // Requires WASIX environment #include #include jmp_buf env; void child_process() { // ... perform some operations ... longjmp(env, 1); } int main() { pid_t pid = fork(); if (pid == 0) { child_process(); } else { if (setjmp(env) == 0) { // Parent process waits or continues } } return 0; } ``` -------------------------------- ### WASIX Readdir Tree Example Source: https://wasix.org/docs/language-guide/c/examples Provides an example of traversing a directory tree using WASIX system calls like opendir, readdir, and closedir. This is useful for file system operations. ```c /* Readdir Tree example code */ // Requires WASIX environment #include #include void traverse_dir(const char *path) { DIR *dir = opendir(path); struct dirent *entry; if (dir == NULL) { perror("opendir"); return; } while ((entry = readdir(dir)) != NULL) { printf("%s\n", entry->d_name); // Potentially recurse for subdirectories } closedir(dir); } int main() { traverse_dir("."); // Traverse current directory return 0; } ``` -------------------------------- ### WASIX Vfork Example Source: https://wasix.org/docs/language-guide/c/examples Demonstrates the Vfork system call in WASIX, which creates a new process that shares memory with the parent until a call to execve or exit. Use with caution. ```c /* Vfork example code */ // Requires WASIX environment #include #include int main() { int x = 10; pid_t pid = vfork(); if (pid == 0) { // Child process // Child shares memory with parent x = 20; printf("Child: x = %d\n", x); _exit(0); // Must use _exit in vfork child } else if (pid > 0) { // Parent process printf("Parent: x = %d\n", x); // x might be 20 if child hasn't exited yet } else { // Error handling perror("vfork failed"); return 1; } return 0; } ``` -------------------------------- ### WASIX File Copy Example Source: https://wasix.org/docs/language-guide/c/examples Illustrates how to perform file copying operations using WASIX system calls. This example covers reading from one file and writing to another. ```c /* File Copy example code */ // Requires WASIX environment #include #include int main() { int fd_src = open("source.txt", O_RDONLY); int fd_dst = open("destination.txt", O_WRONLY | O_CREAT, 0644); char buffer[1024]; ssize_t bytes_read; while ((bytes_read = read(fd_src, buffer, sizeof(buffer))) > 0) { write(fd_dst, buffer, bytes_read); } close(fd_src); close(fd_dst); return 0; } ``` -------------------------------- ### Verify cargo-wasix Installation Source: https://wasix.org/docs/language-guide/rust/installation Verifies that the cargo-wasix command-line tool has been installed correctly by checking its version. ```bash $ cargo wasix --version cargo-wasix 0.1.17 ``` -------------------------------- ### Compile C Application with WASIX Source: https://wasix.org/docs/language-guide/c/tutorials/readdir This snippet shows the command to compile a C program using the WASIX SDK, targeting WebAssembly 32-bit WASI. ```Shell /path/to/wasix-sdk/clang readdir.c --target="wasm32-wasi" -o readdir.wasm ``` -------------------------------- ### Run WASIX Application Source: https://wasix.org/docs/language-guide/c/tutorials/readdir This snippet demonstrates how to run the compiled WASIX WebAssembly module using the wasmer runtime, providing the root directory as an argument. ```Shell wasmer run readdir.wasm / ``` -------------------------------- ### Testing gRPC Server and Client Source: https://wasix.org/docs/language-guide/rust/tutorials/wasix-grpc Provides shell commands to run the compiled Rust gRPC server and client. It shows the expected output for both starting the server and receiving a response from the client. ```Shell $ cargo run --bin helloworld-server Finished dev [unoptimized + debuginfo] target(s) in 0.14s Running `target/debug/helloworld-server` Server listening on 127.0.0.1:50051 ``` ```Shell $ cargo run --bin helloworld-client Finished dev [unoptimized + debuginfo] target(s) in 0.15s Running `target/debug/helloworld-client` RESPONSE=Response { metadata: MetadataMap { headers: {"content-type": "application/grpc", "date": "Mon, 28 Aug 2023 12:54:16 GMT", "grpc-status": "0"} }, message: HelloReply { message: "Hello Tonic!" }, extensions: Extensions } ``` -------------------------------- ### WASIX Epoll Example Source: https://wasix.org/docs/language-guide/c/examples Demonstrates the usage of the Epoll system call within the WASIX environment. This example showcases asynchronous event notification. ```c /* Epoll example code */ // Requires WASIX environment #include int main() { int epfd = epoll_create1(0); // ... epoll setup and usage ... return 0; } ``` -------------------------------- ### Create readdir.c file Source: https://wasix.org/docs/language-guide/c/tutorials/readdir This snippet shows the shell command to create an empty C source file named readdir.c. ```Shell touch readdir.c ``` -------------------------------- ### Compile C to WASIX Source: https://wasix.org/docs/language-guide/c/usage Instructions for compiling a C program to WASIX format. This requires setting up environment variables for the compiler, sysroot, and linker. ```Shell export CC=wasi-sdk/clang export SYSROOT=wasix-libc/sysroot export LLD_PATH=wasi-sdk/lld ``` -------------------------------- ### Run WASIX Binary with wasmer CLI and Flags Source: https://wasix.org/docs/language-guide/rust/usage Demonstrates running a WASIX binary using the `wasmer` CLI with additional runtime flags like `--enable-threads` and `--net`. The Wasmer runtime is required. ```bash wasmer run --enable-threads --net ./target/wasix/debug/hello.wasm ``` -------------------------------- ### Rust Project Setup for WASIX gRPC Source: https://wasix.org/docs/language-guide/rust/tutorials/wasix-grpc Demonstrates the initial steps for setting up a Rust project for WASIX gRPC communication. This includes creating a new binary project, deleting the default main file, creating server and client entry points, and setting up a proto directory. ```bash $ cargo new --bin wasix-grpc Created binary (application) `wasix-grpc` package $ rm src/main.rs $ touch src/server.rs src/client.rs $ mkdir proto $ touch proto/helloworld.proto ``` -------------------------------- ### Benchmark WASIX Binary with cargo wasix Source: https://wasix.org/docs/language-guide/rust/usage This command enables benchmarking of WASIX binaries through `cargo wasix`. The Wasmer runtime needs to be installed. ```bash $ cargo wasix bench ``` -------------------------------- ### Run WASIX Binary with cargo wasix Source: https://wasix.org/docs/language-guide/rust/usage This command is used to execute a compiled WASIX binary file using the `cargo wasix` tool. Ensure the Wasmer runtime is installed. ```bash $ cargo wasix run ``` -------------------------------- ### Create New Rust Project Source: https://wasix.org/docs/language-guide/rust/tutorials/wasix-axum This command initializes a new Rust binary project named 'wasix-axum' using Cargo, the Rust package manager. ```bash cargo new --bin wasix-axum Created binary (application) `wasix-axum` package ``` -------------------------------- ### Test WASIX Binary with cargo wasix Source: https://wasix.org/docs/language-guide/rust/usage This command allows you to run tests for a compiled WASIX binary using `cargo wasix`. The Wasmer runtime must be installed prior to execution. ```bash $ cargo wasix test ``` -------------------------------- ### WASIX Fork Example Source: https://wasix.org/docs/language-guide/c/examples An example showcasing the Fork system call in WASIX for creating new processes. This is a fundamental operation for multitasking. ```c /* Fork example code */ // Requires WASIX environment #include #include int main() { pid_t pid = fork(); if (pid < 0) { // Error handling return 1; } else if (pid == 0) { // Child process printf("This is the child process.\n"); } else { // Parent process printf("This is the parent process, child PID: %d\n", pid); } return 0; } ``` -------------------------------- ### Create New Rust Project for WASIX Source: https://wasix.org/docs/language-guide/rust/tutorials/wasix-reqwest Initializes a new Rust binary project named 'wasix-reqwest-proxy' using Cargo. This sets up the basic file structure for the WASIX application. ```bash $ cargo new --bin wasix-reqwest-proxy\n Created binary (application) `wasix-reqwest-proxy` package ``` -------------------------------- ### WASIX Pipe Example Source: https://wasix.org/docs/language-guide/c/examples Demonstrates the creation and usage of pipes for inter-process communication (IPC) in WASIX. This example shows data transfer between processes. ```c /* Pipe example code */ // Requires WASIX environment #include #include #include int main() { int pipefd[2]; char buffer[32]; pipe(pipefd); pid_t pid = fork(); if (pid == 0) { // Child process close(pipefd[0]); // Close read end write(pipefd[1], "Hello from child!\n", strlen("Hello from child!\n")); close(pipefd[1]); // Close write end } else { // Parent process close(pipefd[1]); // Close write end read(pipefd[0], buffer, sizeof(buffer) - 1); buffer[sizeof(buffer) - 1] = '\0'; printf("Parent received: %s", buffer); close(pipefd[0]); // Close read end } return 0; } ``` -------------------------------- ### Create New Rust Project Source: https://wasix.org/docs/language-guide/rust/tutorials/threading This snippet shows the command to create a new binary Rust project using Cargo. It initializes the project structure, including source files and configuration. ```Shell cargo new --bin wasix-threading Created binary (application) `wasix-threading` package ``` -------------------------------- ### WASIX Getrandom Example Source: https://wasix.org/docs/language-guide/c/examples Demonstrates the Getrandom system call in WASIX for obtaining cryptographically secure pseudo-random numbers. This is crucial for security-sensitive applications. ```c /* Getrandom example code */ // Requires WASIX environment #include #include int main() { unsigned char buf[16]; ssize_t bytes_read = getrandom(buf, sizeof(buf), 0); if (bytes_read == sizeof(buf)) { printf("Generated random bytes:\n"); for (int i = 0; i < sizeof(buf); ++i) { printf("%02x", buf[i]); } printf("\n"); } else { perror("getrandom failed"); return 1; } return 0; } ``` -------------------------------- ### Running the gRPC Client Source: https://wasix.org/docs/language-guide/rust/tutorials/wasix-grpc This command shows how to compile and execute the gRPC client. Upon successful execution, it sends a request to the server and prints the received response, confirming the client-server communication. ```bash $ cargo run --bin helloworld-client Finished dev [unoptimized + debuginfo] target(s) in 2.55s Running `target/debug/helloworld-client` RESPONSE=Response { metadata: MetadataMap { headers: {"content-type": "application/grpc", "date": "Mon, 28 Aug 2023 13:29:27 GMT", "grpc-status": "0"} }, message: HelloReply { message: "hello Alice" }, extensions: Extensions } ``` -------------------------------- ### Setup Hyper Server in Rust for WASIX Source: https://wasix.org/docs/language-guide/rust/tutorials/wasix-reqwest Sets up a basic Hyper server in the main.rs file to listen for incoming requests on port 3000. It configures the server to use a `handle` function for processing connections, which is the entry point for forwarding requests. ```rust let addr = SocketAddr::from(([127, 0, 0, 1], 3000));\n\n println!("Listening on {}", addr);\n\n // And a MakeService to handle each connection...\n let make_service = make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(handle)) });\n // ^^^^^^ \n // \ud83d\udc41\ufe0f Focus here - This handle function is what connects to the part 2 of our application.\n \n // Then bind and serve...\n let server = Server::bind(&addr).serve(make_service);\n\n // And run forever...\n Ok(server.await?) ``` -------------------------------- ### WASIX Longjmp Example Source: https://wasix.org/docs/language-guide/c/examples Illustrates the use of the Longjmp system call in WASIX for performing a non-local goto. This is often used in conjunction with setjmp for error handling. ```c /* Longjmp example code */ // Requires WASIX environment #include #include jmp_buf buf; void function_that_jumps() { printf("About to jump\n"); longjmp(buf, 1); } int main() { if (setjmp(buf) == 0) { function_that_jumps(); } else { printf("Returned from jump\n"); } return 0; } ``` -------------------------------- ### Run with WASIX Source: https://wasix.org/docs/language-guide/rust/tutorials/threading This command executes a Rust application compiled for WASIX. It shows the compilation and execution steps, including running the WebAssembly binary, and the output is similar to the native execution. ```Shell cargo wasix run Finished dev [unoptimized + debuginfo] target(s) in 0.09s Running `cargo-wasix target/wasm32-wasmer-wasi/debug/wasix-threading.wasm` info: Post-processing WebAssembly files Running `target/wasm32-wasmer-wasi/debug/wasix-threading.wasm` hi number 1 from the main thread! hi number 1 from the spawned thread! hi number 2 from the main thread! hi number 2 from the spawned thread! hi number 3 from the main thread! hi number 3 from the spawned thread! hi number 4 from the main thread! hi number 4 from the spawned thread! hi number 5 from the spawned thread! hi number 6 from the spawned thread! hi number 7 from the spawned thread! hi number 8 from the spawned thread! hi number 9 from the spawned thread! ``` -------------------------------- ### Testing WASIX Server with Curl Source: https://wasix.org/docs/language-guide/rust/tutorials/wasix-axum This example demonstrates how to send an HTTP request to a WASIX-based web server using `curl`. It targets `http://localhost:3000`, the default address for the axum server example, to verify its functionality. ```bash $ curl http://localhost:3000 ``` -------------------------------- ### Create and Run Condvar Example in Rust Source: https://wasix.org/docs/language-guide/rust/tutorials/condvar This Rust code demonstrates the use of a condition variable (Condvar) and a mutex to synchronize threads. It spawns a secondary thread that sleeps for a second, signals the condvar, and the main thread waits for this signal before proceeding. This example requires Rust and the WASIX runtime. ```bash $ cargo new --bin wasix-condvar Created binary (application) `wasix-condvar` package ``` ```toml [package] name = "wasix-condvar" version = "0.1.0" edition = "2021" [dependencies] ``` ```rust use std::{ sync::{Arc, Condvar, Mutex}, thread, time::Duration, }; /// Inside of our lock, spawn a new thread, and then wait for it to start. fn main() { // create a condvar, which is a shared object that threads can use to wait for another thread to wake them up. let pair = Arc::new((Mutex::new(false), Condvar::new())); // We enter a lock let (lock, cvar) = &*pair; // acquire the lock on the boolean let mut started = lock.lock().unwrap(); println!("condvar-secondary thread spawn"); { let pair = Arc::clone(&pair); thread::spawn(move || { { println!("condvar-secondary thread started"); let (lock, cvar) = &*pair; //sleep the secondary thread for 1 second so that the main thread can wait for it println!("condvar-secondary thread sleep(1sec) start"); thread::sleep(Duration::from_secs(1)); println!("condvar-secondary thread sleep(1sec) end"); // acquire the lock on the boolean and let mut started = lock.lock().unwrap(); *started = true; println!("condvar-secondary thread set condition"); // We notify the condvar that the value has changed. cvar.notify_one(); println!("condvar-secondary thread notify"); } thread::sleep(Duration::from_millis(50)); println!("condvar-secondary thread exit"); }); } thread::sleep(Duration::from_millis(100)); // Wait for the thread to start up. println!("condvar-main loop"); // check the boolean value and if it is false, wait for the condvar to be notified while !*started { println!("condvar-main wait"); // We wait for the condvar to be notified. started = cvar.wait(started).unwrap(); println!("condvar-main woken"); } println!("condvar-main parent done"); // sleep for 1 second so that the secondary thread can exit thread::sleep(Duration::from_millis(100)); println!("all done"); } ``` ```bash $ cargo run Compiling wasix-condvar v0.1.0 (/wasix-condvar) Finished dev [unoptimized + debuginfo] target(s) in 0.48s Running `target/debug/wasix-condvar` condvar-secondary thread spawn condvar-secondary thread started condvar-secondary thread sleep(1sec) start condvar-main loop condvar-main wait condvar-secondary thread sleep(1sec) end condvar-secondary thread set condition condvar-secondary thread notify condvar-main woken condvar-main parent done condvar-secondary thread exit all done ``` -------------------------------- ### Prepare Input and Output Files Source: https://wasix.org/docs/language-guide/c/tutorials/file-copy Shell commands to create an input text file with content and an empty output file for testing the file copy program. ```Shell $ touch input.txt $ echo "Hello, World!" > input.txt $ touch output.txt ``` -------------------------------- ### Create WASIX Signal Project Directory Source: https://wasix.org/docs/language-guide/c/tutorials/signal This snippet shows the shell commands to create a new directory for the WASIX signal project and navigate into it. ```Shell mkdir wasix-signal cd wasix-signal ``` -------------------------------- ### WASIX: TTY Property Management Source: https://wasix.org/docs/explanation/extensions-to-wasi Enables getting and setting TTY properties, allowing control over terminal behavior. ```WASI tty_get ``` ```WASI tty_set ``` -------------------------------- ### Create a new Rust project for WASIX Source: https://wasix.org/docs/language-guide/rust/tutorials/wasix-spawn This command initializes a new binary Rust project named 'wasix-spawn'. It sets up the basic directory structure and necessary files for a Rust application. ```Shell cargo new --bin wasix-spawn Created binary (application) `wasix-spawn` package ``` -------------------------------- ### Run WASIX Binary Source: https://wasix.org/docs/language-guide/c/usage Command to execute a WASIX binary using the wasmer-cli. This involves specifying the path to the compiled WebAssembly file. ```Shell $ wasmer run ``` -------------------------------- ### Protobuf Definition for gRPC Service Source: https://wasix.org/docs/language-guide/rust/tutorials/wasix-grpc Defines a simple gRPC service named 'Greeter' with a 'Send' RPC method that takes a 'HelloRequest' and returns a 'HelloReply'. This file specifies the message structures for requests and responses. ```protobuf // proto/helloworld.proto syntax = "proto3"; package helloworld; service Greeter { rpc Send(HelloRequest) returns (HelloReply) {} } message HelloRequest { string name = 1; } message HelloReply { string message = 1; } ```