Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Practical WebAssembly

You're reading from   Practical WebAssembly Explore the fundamentals of WebAssembly programming using Rust

Arrow left icon
Product type Paperback
Published in May 2022
Publisher Packt
ISBN-13 9781838828004
Length 232 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Sendil Kumar Nellaiyapen Sendil Kumar Nellaiyapen
Author Profile Icon Sendil Kumar Nellaiyapen
Sendil Kumar Nellaiyapen
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Section 1: Introduction to WebAssembly
2. Chapter 1: Understanding LLVM FREE CHAPTER 3. Chapter 2: Understanding Emscripten 4. Chapter 3: Exploring WebAssembly Modules 5. Section 2: WebAssembly Tools
6. Chapter 4: Understanding WebAssembly Binary Toolkit 7. Chapter 5: Understanding Sections in WebAssembly Modules 8. Chapter 6: Installing and Using Binaryen 9. Section 3: Rust and WebAssembly
10. Chapter 7: Integrating Rust with WebAssembly 11. Chapter 8: Bundling WebAssembly Using wasm-pack 12. Chapter 9: Crossing the Boundary between Rust and WebAssembly 13. Chapter 10: Optimizing Rust and WebAssembly 14. Other Books You May Enjoy

Converting WAST into WASM

wat2wasm helps to convert the WAST format into WASM. Let's take it for a spin:

  1. Create a new folder called wabt-playground and go into the folder:
    $ mkdir wabt-playground
    $ cd wabt-playground
  2. Create a .wat file called add.wat:
    $ touch add.wat
  3. Add the following contents to add.wat:
    (module
    (func $add (param $lhs i32) (param $rhs i32) (result i32)
     get_local $lhs 
      get_local $rhs
        i32.add
    )
    )
  4. Convert the WAST format into WASM using the wat2wasm binary:
    $ /path/to/build/directory/of/wabt/wat2wasm add.wat

This generates a valid WebAssembly binary in add.wasm file:

00 61 73 6d 01 00 00 00 01 07 01 60 02 7f 7f 017f 03
 02 01 00 0a 09 01 07 00 20 00 20 01 6a 0b

Note that the size of the generated binary is 32 bytes.

  1. WABT reads the WAST format file (.wat) and converts it into a WebAssembly module (.wasm). wat2wasm first validates the given file (.wat) and then converts it into a .wasm file...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime