<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: DJ Hemath</title>
    <description>The latest articles on DEV Community by DJ Hemath (@djhemath).</description>
    <link>https://dev.to/djhemath</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F296409%2F1264cfcf-a4b7-44d2-9082-b9017516a59b.png</url>
      <title>DEV Community: DJ Hemath</title>
      <link>https://dev.to/djhemath</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/djhemath"/>
    <language>en</language>
    <item>
      <title>Components of WebAssembly</title>
      <dc:creator>DJ Hemath</dc:creator>
      <pubDate>Thu, 23 Jan 2025 03:30:00 +0000</pubDate>
      <link>https://dev.to/djhemath/components-of-webassembly-1507</link>
      <guid>https://dev.to/djhemath/components-of-webassembly-1507</guid>
      <description>&lt;p&gt;Hey makkals,&lt;/p&gt;

&lt;p&gt;This post is a part of a multi-part series on WebAssembly. Check out other parts of the series &lt;a href="https://dev.to/djhemath/webassembly-a-beginners-guide-27e9"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we have a good idea about what WebAssembly is, it's time to understand the architecture that powers such a technology. WebAssembly comprises the following key components,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Module&lt;/li&gt;
&lt;li&gt;Execution environment&lt;/li&gt;
&lt;li&gt;Stack machine&lt;/li&gt;
&lt;li&gt;Memory&lt;/li&gt;
&lt;li&gt;Tables&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  WebAssembly Module
&lt;/h2&gt;

&lt;p&gt;A WebAssembly module is a compiled unit of code that contains functions, memory, tables and other resources required for running a piece of code. Each module is self-contained and can be instantiated independently.&lt;/p&gt;

&lt;p&gt;A module is essentially a file with the &lt;code&gt;.wasm&lt;/code&gt; extension.&lt;/p&gt;

&lt;p&gt;Modules can import and export functions and memory. This enables modular programming and seamless integration with other web technologies.&lt;/p&gt;

&lt;h2&gt;
  
  
  WebAssembly Execution Environment
&lt;/h2&gt;

&lt;p&gt;WebAssembly relies on a sandboxed execution environment. It ensures secure and isolated code execution. This sandboxed environment is embedded within a host. The great thing about this is that a host can be a browser or NodeJS environment. &lt;/p&gt;

&lt;p&gt;This sandboxed environment ensures security by restricting access to the host system. WebAssembly environments are closed by default. If a resource needs to be accessed, it should be explicitly requested.&lt;/p&gt;

&lt;p&gt;The execution environment includes a WebAssembly runtime, which is responsible for,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Loading modules&lt;/li&gt;
&lt;li&gt;Verifying modules&lt;/li&gt;
&lt;li&gt;Managing memory&lt;/li&gt;
&lt;li&gt;Executing instructions&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Stack machine
&lt;/h2&gt;

&lt;p&gt;WebAssembly operates as a stack-based virtual machine. All the instructions are executed by this virtual machine. It uses stack data structure to manage data and the control flow during the execution.&lt;/p&gt;

&lt;p&gt;A stack machine uses a stack data structure where instructions are pushed and popped during execution. Let's take a look at a simple example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(
    func $add(param $a i32) (param $b i32) (result i32)
        local.get $a
        local.get $b

        i32.add
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above example does the following,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Defines a function named &lt;code&gt;add&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The function takes 2 integers of 32-bits as parameter - &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Adds &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Returns the value of addition as a 32-bit integer&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The stack machine executes the instructions in the following order,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Initially the stack is empty&lt;/li&gt;
&lt;li&gt;Put the value of &lt;code&gt;$a&lt;/code&gt; in the stack&lt;/li&gt;
&lt;li&gt;Put the value of &lt;code&gt;$b&lt;/code&gt; in the stack&lt;/li&gt;
&lt;li&gt;Pop both &lt;code&gt;$a&lt;/code&gt; and &lt;code&gt;$b&lt;/code&gt;, perform 32-bit integer addition operation&lt;/li&gt;
&lt;li&gt;Put the result in the stack&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And whatever is left in the stack is the return value of the function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz6nt9h235fmbzbre83fz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz6nt9h235fmbzbre83fz.png" alt="WebAssembly stack machine" width="800" height="287"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Memory
&lt;/h2&gt;

&lt;p&gt;One of the essential components that makes WebAssembly interoperable is its memory model. WebAssembly follows a &lt;a href="https://en.wikipedia.org/wiki/Flat_memory_model" rel="noopener noreferrer"&gt;linear memory model&lt;/a&gt;. It is a contiguous, growable array of bytes. It is a one-dimensional array used for storing data. This is the HEAP memory for WebAssembly programs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu3ck1xqga7oyvfy79vap.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu3ck1xqga7oyvfy79vap.png" alt="WebAssembly linear memory" width="800" height="287"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is indexed using byte offsets. It allows direct access to specific locations.&lt;/p&gt;

&lt;p&gt;For example: Let's say we store two 8-bit numbers (say 10 and 16) in the memory. Since 8 bits equal 1 byte, the addresses of the numbers will be as follows,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;10 - address 0&lt;/li&gt;
&lt;li&gt;16 - address 1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3zh2230k7ew7tnl26dm3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3zh2230k7ew7tnl26dm3.png" alt="WebAssembly linear memory example" width="800" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Both of these integers necessarily need not to be exactly right next to each other. They can be in any location within the memory. But they can be accessed with the offset.&lt;/p&gt;

&lt;p&gt;There can be any bits of data defined in the memory. It all comes down to the type of view we read the data.&lt;/p&gt;

&lt;p&gt;It is not good to read a memory in an 8-bit view when the data defined are in 32-bit. It will produce unnecessary results. So it is better to stick with one view of memory to avoid unnecessary conflicts.&lt;/p&gt;

&lt;p&gt;In JavaScript, we can access the memory with &lt;a href="https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/Memory" rel="noopener noreferrer"&gt;WebAssembly.Memory&lt;/a&gt; interface. To define a piece of memory from JavaScript, we can instantiate the Memory class like following,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;memory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;WebAssembly&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Memory&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;initial&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;maximum&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code provisions 10 pages of memory. Generally in WebAssembly, &lt;strong&gt;1 page&lt;/strong&gt; of memory equals &lt;strong&gt;64kB&lt;/strong&gt;. In the above example, we provision &lt;code&gt;64 x 10 = 640kB&lt;/code&gt; of memory as minimum. Since the memory can be grown as needed, the maximum set is &lt;code&gt;64 x 100 = 6.4mB&lt;/code&gt; of memory.&lt;/p&gt;

&lt;p&gt;We can also create &lt;a href="https://developer.mozilla.org/en-US/docs/WebAssembly/Understanding_the_text_format#shared_memories" rel="noopener noreferrer"&gt;shared memory&lt;/a&gt; with JavaScript. By creating a shared memory, we can allow multiple threads to access and manipulate the same memory.&lt;/p&gt;

&lt;p&gt;The easiest way to set or get a value from WASM memory is to use two runtime methods you can export during compilation,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;setValue(ptr, value, type)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;getValue(ptr, type)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's see a simple example to understand the usage of memory from JavaScript to C. I borrowed this example from one of my favourite &lt;a href="https://marcoselvatici.github.io/WASM_tutorial/" rel="noopener noreferrer"&gt;resource&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;addOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;input_ptr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;output_ptr&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;output_ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;input_ptr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, the function &lt;code&gt;addOne&lt;/code&gt; receives two pointers. It then gets whatever the value in the &lt;code&gt;input_ptr&lt;/code&gt; and adds 1 to it. And it sets the resultant value to another pointer named &lt;code&gt;output_ptr&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In JavaScript,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;addOne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cwrap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;addOne&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="c1"&gt;// alloc 4 bytes of memory for the input and 4 for the output (32-bit integers)&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;input_ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;output_ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// set the value in WASM memory&lt;/span&gt;
    &lt;span class="nx"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input_ptr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;i32&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// call the WASM function&lt;/span&gt;
    &lt;span class="nf"&gt;addOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input_ptr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;output_ptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// extract the result from WASM memory&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;output_ptr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;i32&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;The result read is&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;at position&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;output_ptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// deallocate memory to avoid memory leaks&lt;/span&gt;
    &lt;span class="nx"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input_ptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;output_ptr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code does,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Allocate two new memory locations in the heap and get pointers to them (&lt;code&gt;malloc&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Set the value of the input writing directly to that memory position (&lt;code&gt;setValue&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Call the C function to perform our calculation.&lt;/li&gt;
&lt;li&gt;Access the output value stored in the memory (&lt;code&gt;getValue&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Free the memory in the heap (&lt;code&gt;free&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Note that we are explicitly de-allocating the memory at the end using the &lt;code&gt;free&lt;/code&gt; method. Even though JavaScript has a garbage collector, C doesn't have one. So anytime we allocate something, we have to remember to de-allocate it.&lt;/p&gt;

&lt;p&gt;Also, memory always needs not to be imported from JavaScript. A WebAssembly module can define its own memory. The syntax is different for each language.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tables
&lt;/h2&gt;

&lt;p&gt;WebAssembly tables are a separate memory segment (not included in the memory we discussed above) that stores the references to functions in WASM. It enables dynamic function calls.&lt;/p&gt;

&lt;p&gt;Dynamic function calls means invoking a function indirectly through a reference or index stored in a table, instead of calling it directly by name or address. In WebAssembly, this is achieved using tables and the &lt;code&gt;call_indirect&lt;/code&gt; instruction.&lt;/p&gt;

&lt;p&gt;This enables calling functions in runtime without knowing the function beforehand. We don't need to know the exact function name. All we need is the memory address or the index of the function in the WASM table.&lt;/p&gt;

&lt;p&gt;For example, let's say we are building a game. The game lets users modify skins of characters and objects.&lt;/p&gt;

&lt;p&gt;In this case, users can download any skin and add to the game. We will never know what all the functions are inside the new skin plugin. But our game engine should be able to call the necessary functions. This is where tables help us.&lt;/p&gt;

&lt;p&gt;Let's say our game expects three skin methods,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;head() - to get the details of head like shape, color, eye color, etc.,&lt;/li&gt;
&lt;li&gt;hands() - to get the information related to hands&lt;/li&gt;
&lt;li&gt;shoes() - to get the information related to shoes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Our game instead of calling these functions with names, it instead calls with index. And our game expects that the shader or skin plugin stores the respective functions in the specified order in WASM tables.&lt;/p&gt;

&lt;p&gt;Example, our game expects code related to,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;head in the index 0&lt;/li&gt;
&lt;li&gt;hands in the index 1&lt;/li&gt;
&lt;li&gt;shoes in the index 2&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now the plugin can define any number of functions. It doesn't matter what the names of these functions are. But there should be a proper WASM table implemented in the same order.&lt;/p&gt;

&lt;p&gt;Then our original game can blindly call these functions via reference via index and things should work seamlessly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkj8u8cm9gjhm014v7o4i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkj8u8cm9gjhm014v7o4i.png" alt="WebAssembly tables example" width="800" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;That is the high-level information about the major components of WebAssembly. Note that this is a beginner guide, so the examples and the definition are intentionally kept simple. We can do much more with these components which are all not mentioned here.&lt;/p&gt;

&lt;p&gt;There are a lot of resources out there on the internet about these individual components in detail.&lt;/p&gt;

&lt;p&gt;Some of my favourite ones are,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://marcoselvatici.github.io/WASM_tutorial/" rel="noopener noreferrer"&gt;WASM tutorial&lt;/a&gt; by Marco Selvatici&lt;/li&gt;
&lt;li&gt;MDN's WebAssembly &lt;a href="https://developer.mozilla.org/en-US/docs/WebAssembly" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://wasmbyexample.dev/home.en-us.html" rel="noopener noreferrer"&gt;WASM by example&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So far, we've covered the foundational theory of WebAssembly. Now it's time to build some projects with real values.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>webassembly</category>
    </item>
    <item>
      <title>Why WebAssembly is faster than JavaScript</title>
      <dc:creator>DJ Hemath</dc:creator>
      <pubDate>Wed, 22 Jan 2025 03:30:00 +0000</pubDate>
      <link>https://dev.to/djhemath/why-webassembly-is-faster-than-javascript-2gja</link>
      <guid>https://dev.to/djhemath/why-webassembly-is-faster-than-javascript-2gja</guid>
      <description>&lt;p&gt;Hey makkals,&lt;/p&gt;

&lt;p&gt;This post is a part of a multi-part series on WebAssembly. Check out other parts of the series &lt;a href="https://dev.to/djhemath/webassembly-a-beginners-guide-27e9"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Originally published - &lt;a href="https://hemath.dev/blog/webassembly/why-webassembly-is-faster-than-javascript" rel="noopener noreferrer"&gt;https://hemath.dev/blog/webassembly/why-webassembly-is-faster-than-javascript&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Alright, you might now be wondering why exactly WebAssembly is faster than JavaScript. And that's the question we are gonna answer in this post.&lt;/p&gt;

&lt;h2&gt;
  
  
  How is JavaScript executed?
&lt;/h2&gt;

&lt;p&gt;Before we can understand how WebAssembly is faster, we need to understand how JavaScript is executed under the hood. Take a look at the following diagram.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs36oq7kdfexf1uha61wm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs36oq7kdfexf1uha61wm.png" alt="Steps involved in the execution of JavaScript" width="800" height="287"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above chart explains the steps involved in executing JavaScript code. Each bar represents the relative time taken between each process.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;NOTE: This is not an exact representation. It differs from browser to browser and the amount of JavaScript loaded in the page&lt;br&gt;
But it gives a high-level idea about all the steps involved.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Each bar represents time taken for each step:&lt;/p&gt;

&lt;h3&gt;
  
  
  Parsing
&lt;/h3&gt;

&lt;p&gt;Parsing is the process of converting text JavaScript code into an &lt;a href="https://en.wikipedia.org/wiki/Abstract_syntax_tree" rel="noopener noreferrer"&gt;AST&lt;/a&gt;. This step takes place after the JavaScript is downloaded by the browser. Representing the JavaScript code as an AST tree helps the interpreter to execute the JavaScript code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Compiling + optimizing
&lt;/h3&gt;

&lt;p&gt;Modern browsers these days have a just-in-time compiler that compiles JavaScript. It's not technically a static compilation like it's done on C and other compiled languages. Instead the compiler assumes the types of parameters and returns types based on the usage. It then compiles it and keeps it in the memory for reuse. It results in a better optimized execution&lt;/p&gt;

&lt;h3&gt;
  
  
  Re-optimizing
&lt;/h3&gt;

&lt;p&gt;However these assumptions are not always right. Sometimes a function can get/return different value types. In such cases, the JIT compiler throws away the existing optimized version and re-compiles it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Execution
&lt;/h3&gt;

&lt;p&gt;Execution is step where the JavaScript code is converted to executable instructions followed by executing the code line by line&lt;/p&gt;

&lt;h3&gt;
  
  
  Garbage collection
&lt;/h3&gt;

&lt;p&gt;In this step, unused memory will be cleaned up by the built-in garbage collector. This adds an extra bit of overhead to the JavaScript's execution since the garbage collector keeps checking what memory objects need to be cleaned up.&lt;/p&gt;

&lt;h2&gt;
  
  
  How is WebAssembly executed?
&lt;/h2&gt;

&lt;p&gt;After downloading the WASM file, it goes through only 3 steps instead of 5 steps.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwbmdf0rd3gmkwybzw2rx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwbmdf0rd3gmkwybzw2rx.png" alt="Steps involved in the execution of WebAssembly" width="800" height="287"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Decode
&lt;/h3&gt;

&lt;p&gt;Similar to parsing JavaScript. But WASM doesn't need to be converted into any structures. So after decoding, the integrity of the module is validated. After the validation, the WASM is ready to be executed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Compiling + optimizing
&lt;/h3&gt;

&lt;p&gt;WebAssembly code starts off much closer to the machine code. So compiling and optimizing WebAssembly is quicker than JavaScript. Since it is statically typed, the compiler doesn't need to spend time running the code to observe the usage of types.&lt;/p&gt;

&lt;p&gt;Moreover, many optimizations are already done in the build step. So less work is needed to compile and optimize it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Execution
&lt;/h3&gt;

&lt;p&gt;Since compilers already produced the set of instructions, execution is a lot faster than JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;referred from &lt;a href="https://hacks.mozilla.org/2017/02/what-makes-webassembly-fast/" rel="noopener noreferrer"&gt;Lin Clark's post&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;WebAssembly is faster than JavaScript in many cases because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;fetching&lt;/strong&gt; WebAssembly takes less time because it is more compact than JavaScript, even when compressed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;decoding&lt;/strong&gt; WebAssembly takes less time than parsing JavaScript.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;compiling&lt;/strong&gt; and optimizing takes less time because WebAssembly is closer to machine code than JavaScript and already has gone through optimization on the server side.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;re-optimizing&lt;/strong&gt; doesn't need to happen because WebAssembly has types and other information built in, so the JS engine doesn't need to speculate when it optimizes the way it does with JavaScript.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;executing&lt;/strong&gt; often takes less time because there are fewer compiler tricks and gotchas that the developer needs to know to write consistently performant code, plus WebAssembly's set of instructions are more ideal for machines.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;garbage collection&lt;/strong&gt; is not required since the memory is managed manually.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And, This is why, in many cases, WebAssembly will outperform JavaScript when doing the same task.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>webassembly</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Build your first webassembly project</title>
      <dc:creator>DJ Hemath</dc:creator>
      <pubDate>Tue, 21 Jan 2025 03:30:00 +0000</pubDate>
      <link>https://dev.to/djhemath/build-your-first-webassembly-project-3o3o</link>
      <guid>https://dev.to/djhemath/build-your-first-webassembly-project-3o3o</guid>
      <description>&lt;p&gt;Hey makkals,&lt;/p&gt;

&lt;p&gt;This post is a part of a multi-part series on WebAssembly. Check out other parts of the series &lt;a href="https://dev.to/djhemath/webassembly-a-beginners-guide-27e9"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have you ever heard about WebAssembly and thought, "That sounds cool, but it's probably too complex for me"?&lt;/p&gt;

&lt;p&gt;Well, today you're going to build a simple WebAssembly project from scratch. Let me set the right expectations. We are going to build a very simple project which generates random hexadecimal color code.&lt;/p&gt;

&lt;p&gt;Think of it as the “Hello, World!” of WebAssembly with a splash of color. By the end, you'll learn how to write simple WebAssembly code, compile it, and use it with JavaScript to generate random colors right in your browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup WebAssembly
&lt;/h2&gt;

&lt;p&gt;In this blog post series, we will be using C and C++ for examples. So we will be using a tool called &lt;code&gt;Emscripten&lt;/code&gt; to compile our C / C++ code to WASM. However, you can use any supported language with the respective compiling tools.&lt;/p&gt;

&lt;p&gt;Setting up Emscripten is straightforward. Just follow this page - &lt;a href="https://emscripten.org/docs/getting_started/downloads.html" rel="noopener noreferrer"&gt;https://emscripten.org/docs/getting_started/downloads.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you're using macOS, you can simply use homebrew to install it,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew &lt;span class="nb"&gt;install &lt;/span&gt;emscripten
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're using linux, follow this post - &lt;a href="https://marcoselvatici.github.io/WASM_tutorial/#install_emscripten" rel="noopener noreferrer"&gt;https://marcoselvatici.github.io/WASM_tutorial/#install_emscripten&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After setting up Emscripten, you should be able to run the following command in your terminal,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;emcc &lt;span class="nt"&gt;-v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Simple addition
&lt;/h2&gt;

&lt;p&gt;Before implementing a random hex code generator, let's build something simple first to understand the WASM workflow. Let's build an application that just adds 2 numbers.&lt;/p&gt;

&lt;p&gt;We will first build the application in JavaScript and then it into WASM later. Create a file named &lt;code&gt;index.html&lt;/code&gt; and paste the following code,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"number"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"num1"&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"Enter first number"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"number"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"num2"&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"Enter second number"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;onclick=&lt;/span&gt;&lt;span class="s"&gt;"addNumbers()"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Add&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"result"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addNumbers&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;num1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;num2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;addTwoNumbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;result&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`Result: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addTwoNumbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code is self-explanatory. On clicking the button, we get 2 numbers from the input fields and pass it to &lt;code&gt;addTwoNumbers&lt;/code&gt; function. And the function returns a number which we display inside a paragraph.&lt;/p&gt;

&lt;p&gt;Now let's move the &lt;code&gt;addTwoNumbers&lt;/code&gt; function to &lt;code&gt;C&lt;/code&gt; language. Create a file named &lt;code&gt;add.c&lt;/code&gt; and write the following piece,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// add.c&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;addTwoNumbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's compile the C code to WASM,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;emcc add.c &lt;span class="nt"&gt;-o&lt;/span&gt; add.js &lt;span class="nt"&gt;-s&lt;/span&gt; &lt;span class="nv"&gt;EXPORTED_FUNCTIONS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"['_addTwoNumbers']"&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt; &lt;span class="s2"&gt;"EXTRA_EXPORTED_RUNTIME_METHODS=['ccall', 'cwrap']"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's breakdown what this command does,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;emcc add.c -o add.js&lt;/code&gt; - Tells the Emscripten compiler to compile the C code from &lt;code&gt;add.c&lt;/code&gt; to JavaScript in a file named &lt;code&gt;add.js&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-s EXPORTED_FUNCTIONS="['_addTwoNumbers']"&lt;/code&gt; - Tells the compiler to export the &lt;code&gt;addTwoNumbers&lt;/code&gt; function so it can be used in JavaScript. The underscore (&lt;code&gt;_&lt;/code&gt;) is part of the command, not part of the function name.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall', 'cwrap']"&lt;/code&gt; - Tells the compiler to export additional runtime functions - cwrap and ccall are the functions that we use to &lt;code&gt;wrap&lt;/code&gt; a C function and &lt;code&gt;call&lt;/code&gt; it from JavaScript respectively.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After successful compilation, 2 new files would be generated - &lt;code&gt;add.wasm&lt;/code&gt; and &lt;code&gt;add.js&lt;/code&gt;. Here,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;add.wasm&lt;/code&gt; is the actual WASM binary file that the browser will run.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;add.js&lt;/code&gt; is just a glue-code that lets us use the functions defined in the C language seamlessly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And change the HTML file as follows,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"number"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"num1"&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"Enter first number"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"number"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"num2"&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"Enter second number"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;onclick=&lt;/span&gt;&lt;span class="s"&gt;"addNumbers()"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Add&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"result"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

    &lt;span class="c"&gt;&amp;lt;!-- Including the JavaScript glue-code file generated by Emscripten --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./add.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
      &lt;span class="c1"&gt;// Wrap the function provided by C&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addTwoNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cwrap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;addTwoNumbers&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

      &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addNumbers&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;num1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;num2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Calling the wrapped function&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;addTwoNumbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;result&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`Result: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;Module.cwrap&lt;/code&gt; is used to wrap the C function, so that we can use it as a normal JavaScript function.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cwrap&lt;/code&gt; takes three parameters,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;function name - &lt;code&gt;addTwoNumbers&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;return type - &lt;code&gt;number&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;types of parameters - [&lt;code&gt;number&lt;/code&gt;, &lt;code&gt;number&lt;/code&gt;]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Run the application from a local server. If you don't have a local server installed in your machine, try any one of the following.&lt;/p&gt;

&lt;p&gt;If you have Python 3 installed, try these,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python &lt;span class="nt"&gt;-m&lt;/span&gt; http.server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 &lt;span class="nt"&gt;-m&lt;/span&gt; http.server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or, if you have NodeJS installed, try the following,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i &lt;span class="nt"&gt;-g&lt;/span&gt; live-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and run the following command in the directory which contains the all the files that we have created now,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;live-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If everything went well, you will be able to see 2 input fields. Try entering 2 numbers and click the button to see the result.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5edi4zsvov5z7o5bren5.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5edi4zsvov5z7o5bren5.gif" alt="Demo of adding 2 numbers using WebAssembly" width="800" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, all the DOM manipulations such as event listeners, getting value from inputs, displaying result are done by JavaScript. Whereas, the addition is done by WASM which was written in C language.&lt;/p&gt;

&lt;p&gt;You can find the full code for this project - &lt;a href="https://github.com/djhemath/Webassembly-demos/tree/main/add-two-numbers" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's start working on our actual project - random hexadecimal color generator&lt;/p&gt;

&lt;h2&gt;
  
  
  What is hexadecimal?
&lt;/h2&gt;

&lt;p&gt;Before building our project, let's understand what is hexadecimal and how it can be represented as colors.&lt;/p&gt;

&lt;p&gt;Hexadecimal is a number system with base 16. It means, the number system contains 16 symbols to represent values. The possible hexadecimal symbols are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, followed by A, B, C, D, E, F.&lt;/p&gt;

&lt;p&gt;Here, A = 10, B = 11 and so on..&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftbddk9qkuvin0wymp6ac.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftbddk9qkuvin0wymp6ac.png" alt="Hexadecimal representation" width="800" height="277"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can use hex code to represent colors with six digits. For example, #FF5733 or #42A5F5&lt;/p&gt;

&lt;p&gt;To understand more about colors and their representation, read - &lt;a href="https://www.pluralsight.com/resources/blog/upskilling/understanding-hexadecimal-colors-simple" rel="noopener noreferrer"&gt;Understanding Hexadecimal Colors.&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Random color generator
&lt;/h2&gt;

&lt;p&gt;Let's write our C code first. Create a file named &lt;code&gt;random_color.c&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We need to use two libraries: &lt;code&gt;stdlib.h&lt;/code&gt; and &lt;code&gt;time.h&lt;/code&gt;,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// random_color.c&lt;/span&gt;

&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;time.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We define the &lt;code&gt;generateRandomHexColor&lt;/code&gt; function, which returns a character pointer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;generateRandomHexColor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's define necessary variable we need, and initiate random seed&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;generateRandomHexColor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// Store the color as a string (e.g., "#A3B2C7")&lt;/span&gt;

    &lt;span class="n"&gt;srand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

    &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'#'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use the &lt;code&gt;rand&lt;/code&gt; function to generate a random number between 0 and 255. Then we need to convert it into a hexadecimal digit (i.e., 0 - 9 and A - F).&lt;/p&gt;

&lt;p&gt;Let's first write an util function that converts an integer into a valid hex value,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;intToHex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;hexStr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// Store the hex string (2 characters + null terminator)&lt;/span&gt;
    &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;hexDigits&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0123456789ABCDEF"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;hexStr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hexDigits&lt;/span&gt;&lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xF&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// High nibble&lt;/span&gt;
    &lt;span class="n"&gt;hexStr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hexDigits&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xF&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;         &lt;span class="c1"&gt;// Low nibble&lt;/span&gt;
    &lt;span class="n"&gt;hexStr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'\0'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                         &lt;span class="c1"&gt;// Null terminator&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;hexStr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;generateRandomHexColor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can use this &lt;code&gt;intToHex&lt;/code&gt; function to convert our random numbers into hex code. The final code is,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// random_color.c&lt;/span&gt;

&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;time.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;intToHex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;hexStr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// Store the hex string (2 characters + null terminator)&lt;/span&gt;
    &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;hexDigits&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0123456789ABCDEF"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;hexStr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hexDigits&lt;/span&gt;&lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xF&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// High nibble&lt;/span&gt;
    &lt;span class="n"&gt;hexStr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hexDigits&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xF&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;         &lt;span class="c1"&gt;// Low nibble&lt;/span&gt;
    &lt;span class="n"&gt;hexStr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'\0'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                         &lt;span class="c1"&gt;// Null terminator&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;hexStr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;generateRandomHexColor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// Store the color as a string (e.g., "#A3B2C7")&lt;/span&gt;

    &lt;span class="n"&gt;srand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

    &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'#'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;intToHex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;256&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;intToHex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;256&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;intToHex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;256&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'\0'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Null terminator to make it a valid string&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's compile it to WASM,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;emcc random_color.c &lt;span class="nt"&gt;-o&lt;/span&gt; random_color.js &lt;span class="nt"&gt;-s&lt;/span&gt; &lt;span class="nv"&gt;EXPORTED_FUNCTIONS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"['_generateRandomHexColor']"&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt; &lt;span class="s2"&gt;"EXTRA_EXPORTED_RUNTIME_METHODS=['ccall', 'cwrap', 'UTF8ToString']"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And let's prepare a HTML file to use this function,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Random Background Color&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;onclick=&lt;/span&gt;&lt;span class="s"&gt;"changeBackgroundColor()"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Generate Random Color&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"random_color.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt; &lt;span class="c"&gt;&amp;lt;!-- Include the generated glue code --&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;changeBackgroundColor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Wrapping the function written in C&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;generateRandomHexColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cwrap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;generateRandomHexColor&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;

      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;colorPtr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;generateRandomHexColor&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UTF8ToString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;colorPtr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If everything goes well, you should be able to click a button that changes the background color of the page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frjflsrbz5oqo9fsalslo.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frjflsrbz5oqo9fsalslo.gif" alt="Demo of random hex color generator using WebAssembly" width="800" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's totally okay if don't understand how we get the color from pointer and how C's pointers are accessible via JavaScript. We will see more about WebAssembly architecture and memory in the upcoming posts.&lt;/p&gt;

&lt;p&gt;You can find the full code for this project - &lt;a href="https://github.com/djhemath/Webassembly-demos/tree/main/random-color-generator" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this post, we've taken our first steps in  WebAssembly by writing simple C code, compiling it into WASM using Emscripten, and integrating it with JavaScript to build a functional random color generator. While this project is just the beginning, it shows the power and flexibility of WebAssembly in bringing high-performance code to the web.&lt;/p&gt;

&lt;p&gt;With WebAssembly, you can unlock new possibilities for building faster, more efficient web applications. In future posts, we'll explore more advanced use cases, like image processing, to further understand how WebAssembly can enhance performance and extend JavaScript's capabilities.&lt;/p&gt;

&lt;p&gt;Stay tuned for more hands-on examples and deeper dives into WebAssembly&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>webassembly</category>
    </item>
    <item>
      <title>WebAssembly - a beginners guide</title>
      <dc:creator>DJ Hemath</dc:creator>
      <pubDate>Mon, 20 Jan 2025 03:30:00 +0000</pubDate>
      <link>https://dev.to/djhemath/webassembly-a-beginners-guide-27e9</link>
      <guid>https://dev.to/djhemath/webassembly-a-beginners-guide-27e9</guid>
      <description>&lt;p&gt;Hello makkals,&lt;/p&gt;

&lt;p&gt;It's been a while since I jumped in WebAssembly. I fell in love with the idea of WebAssembly. Built and worked in some cool projects. Now I decided share my knowledge with you all. This is going to be a series of blog posts that dives into many topics involving WebAssembly. At this point, I really don't know how many posts are going to be published. My rought guestimate tells at least 5 posts, but it may grow.&lt;/p&gt;

&lt;p&gt;Nevertheless, worry not, I'll keep updating the this post with links of all the upcoming ones. This post is going to serve as an index for all other posts.&lt;/p&gt;

&lt;p&gt;Almost every post is going to be practical-first so that everyone can understand the underlying theory by seeing clear examples.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/djhemath/introduction-to-webassembly-7kc"&gt;Introduction to WebAssembly&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/djhemath/build-your-first-webassembly-project-3o3o"&gt;Build your first ever WebAssembly project&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/djhemath/why-webassembly-is-faster-than-javascript-2gja"&gt;Why WebAssembly is faster than JavaScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Components of WebAssembly&lt;/li&gt;
&lt;li&gt;Building a simple image processor using WebAssembly&lt;/li&gt;
&lt;li&gt;Building a video to gif convertor with ffmpeg using WebAssembly&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can find code for all the projects we build in this &lt;a href="https://github.com/djhemath/Webassembly-demos" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;NOTE: This is a beginners guide to get started with WebAssembly. So it doesn't include granular details of every concepts in WebAssembly. It's a collection of all the knowledge that I attained so far in the WebAssembly.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;/blockquote&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>webassembly</category>
    </item>
    <item>
      <title>Introduction to WebAssembly</title>
      <dc:creator>DJ Hemath</dc:creator>
      <pubDate>Mon, 20 Jan 2025 03:30:00 +0000</pubDate>
      <link>https://dev.to/djhemath/introduction-to-webassembly-7kc</link>
      <guid>https://dev.to/djhemath/introduction-to-webassembly-7kc</guid>
      <description>&lt;p&gt;Hey makkals,&lt;/p&gt;

&lt;p&gt;This post is a part of a multi-part series on WebAssembly. Check out other parts of the series &lt;a href="https://dev.to/djhemath/webassembly-a-beginners-guide-27e9"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;WebAssembly, or WASM for short, is a low-level assembly-like language. It lets us run applications built with different programming languages in the browser. It's a truly cross-platform way to build applications. It is low-level so that it runs at near-native speed enabling us to do a lot more on the web that weren't possible with just JavaScript.&lt;/p&gt;

&lt;p&gt;If you’ve ever wished your web apps could run faster or handle tasks that seem too heavy for JavaScript, WebAssembly is here to help. In this post, we’ll break down what WebAssembly is, why it was created, and how you can start using it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is WebAssembly actually?
&lt;/h2&gt;

&lt;p&gt;WebAssembly is a low-level compilation target. It is designed to run in web browsers. It's not something that lets you directly run other languages. Instead, it specifies an assembly language-like structure which other languages can &lt;strong&gt;compile-to&lt;/strong&gt;, thus the term "compilation target".&lt;/p&gt;

&lt;p&gt;Let's understand this by a simple example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above C code produces the following Assembly instructions (or machine code),&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mov eax, 1    ; Load 1 into register EAX
mov ebx, 2    ; Load 2 into register EBX
add eax, ebx  ; Add EAX and EBX, result in EAX
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This in-turn produces following binary,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;00000000000000000000000000000011
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;NOTE: The above output varies on the CPU architecture. It's just a simple example that helps you understand the idea.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi6ri8ltji4vw6ppaisco.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi6ri8ltji4vw6ppaisco.png" alt="WebAssembly C compilation" width="800" height="277"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see that the C code is transformed into Assembly code and further into binary. The binary is something that can be executed directly by the computer.&lt;/p&gt;

&lt;p&gt;Now, let's alter this process a little bit. Instead of the C compiler producing standard Assembly code, let's say it produces something like this,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$a int
$b int
$c int

set $a 1
set $b 2
set $c = add $a $b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;NOTE: This is a made up syntax to let you understand the idea easily. It's not a standard syntax for anything in the world of WASM. We will see the actual syntax of WASM in this post later.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fie0yhnrvug0zsxhw1d6i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fie0yhnrvug0zsxhw1d6i.png" alt="Webassembly wasm compilation" width="800" height="277"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Assume that all browsers (Chrome, Firefox, Safari, etc.) understand and execute the above syntax. Then this opens up a wide-variety of opportunities for us on the web.&lt;/p&gt;

&lt;p&gt;Because like the C language, we can write compilers for other potential languages like C++, Rust, Go, etc., that produce the above output for the respective code. It means that we can basically run programs written in any language in any browser no matter what type of OS is, or what browser it is, we should be able to run anything in the browser. And this is so cool!&lt;/p&gt;

&lt;p&gt;Since all the browsers are built by separate teams in separate tech stack, we need a specification that defines how WASM should be treated. That specification is called WebAssembly and it is defined by W3C. And already all major browsers support WebAssembly natively. It means, today, we can run applications written in many programming languages inside a web browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  WebAssembly representation
&lt;/h2&gt;

&lt;p&gt;WebAssembly can be represented in 2 formats,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;WAT&lt;/strong&gt; - WebAssembly Text, it's a textual format designed to help us debug and understand the code&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;WASM&lt;/strong&gt; - WebAssembly module is a binary format that browser uses to execute the instructions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both are one-to-one representations of each other. It means that we can convert WAT to WASM and WASM to WAT. So compiling from a language to either WASM or WAT is possible, and we can obtain one from another.&lt;/p&gt;

&lt;p&gt;Now, let's understand the very basic syntax with our above example,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The following piece of code declares three &lt;strong&gt;32-bit integer&lt;/strong&gt; variables - &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(local $a i32)
(local $b i32)
(local $c i32)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Let's assign some values to these variables,
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;i32.const 1
set_local $a 

i32.const 2
set_local $b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Let's add &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;, and store it in &lt;code&gt;c&lt;/code&gt;,
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;get_local $a
get_local $b

i32.add

set_local $c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you don't understand the syntax intuitively, it's okay. WebAssembly is designed to run in a stack-machine. We will see more about stack machines in future posts. But for now, understand that a stack machine puts and pops instructions based on the instruction type.&lt;/p&gt;

&lt;p&gt;So when we do, &lt;code&gt;get_local $a&lt;/code&gt; and &lt;code&gt;get_local $b&lt;/code&gt;, we are pushing both of them into the stack. And the instruction &lt;code&gt;i32.add&lt;/code&gt; tells the browser to,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;add the top 2 numbers in the stack &lt;/li&gt;
&lt;li&gt;pop them out&lt;/li&gt;
&lt;li&gt;keep the result of addition in the stack (in our example, it's the number 3)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And the &lt;code&gt;set_local $c&lt;/code&gt; tells it to store whatever the value is in top of the stack to the variable &lt;code&gt;$c&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is the textual representation (WAT) representation of the code. When transpiled into WASM, it looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0x20 0x00    ;; get_local $a
0x20 0x01    ;; get_local $b
0x6A             ;; i32.add
0x21 0x02    ;; set_local $c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don’t worry if these representations seem complex. WebAssembly is designed as a compilation target, not a language you need to write directly.&lt;/p&gt;

&lt;p&gt;This is a compilation-target, not a programming language per se. Almost all the time we will write code in programming languages like C, C++, Rust, etc., and compile it into this representation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Will WebAssembly replace JavaScript?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Short answer: No&lt;/strong&gt;, at least not anytime soon. WASM is designed to work along with JavaScript, not to replace it. JavaScript and WebAssembly complement each other.&lt;/p&gt;

&lt;p&gt;JavaScript was originally created for basic web operations like DOM manipulation and form validation. In the past decades, we wanted more from JavaScript and made it do certain things that it isn't designed for. The result is poor performance on complex applications. Since JavaScript is interpreted it is relatively slow compared to other compiled languages. However, modern web browsers have a JIT compiler which improves performance of JavaScript applications.&lt;/p&gt;

&lt;p&gt;I might probably write a blog post about the JIT compiler soon. But before that you can follow one of my favorite blog posts on the topic - &lt;a href="https://hacks.mozilla.org/2017/02/a-crash-course-in-just-in-time-jit-compilers" rel="noopener noreferrer"&gt;https://hacks.mozilla.org/2017/02/a-crash-course-in-just-in-time-jit-compilers&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even with JIT compilers, JavaScript can only do so much. That's where WebAssembly comes into the picture.&lt;/p&gt;

&lt;p&gt;Let's say we want to process video files. Video files are generally large, and doing some operations with it like optimizing, editing, different codecs, etc., are very data intensive operations. Since JavaScript is interpreted and JIT compiled at its best, it cannot deliver native performance.&lt;/p&gt;

&lt;p&gt;But with WebAssembly, we can achieve &lt;strong&gt;near-native&lt;/strong&gt; performance since the code is pre-compiled and close to machine code, the execution will be pretty fast. So WebAssembly is just a new gym-rat friend for JavaScript. WebAssembly can do the heavy-lifting CPU intensive tasks very well while JavaScript can do the tasks (DOM manipulations, handling events) it was supposed to do.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use cases of WebAssembly
&lt;/h2&gt;

&lt;p&gt;WebAssembly’s near-native performance makes it ideal for computationally intensive applications.&lt;/p&gt;

&lt;p&gt;Some of the use cases are,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Image processing&lt;/li&gt;
&lt;li&gt;Video processing&lt;/li&gt;
&lt;li&gt;Games - even 3d games&lt;/li&gt;
&lt;li&gt;Music applications&lt;/li&gt;
&lt;li&gt;Encryption and other cryptographic solutions&lt;/li&gt;
&lt;li&gt;Visualization tools&lt;/li&gt;
&lt;li&gt;Simulators and emulators&lt;/li&gt;
&lt;li&gt;And existing applications like AutoCAD, etc.,&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There are some applications that are already using WebAssembly are,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Figma - &lt;a href="https://www.figma.com/blog/webassembly-cut-figmas-load-time-by-3x/" rel="noopener noreferrer"&gt;https://www.figma.com/blog/webassembly-cut-figmas-load-time-by-3x/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Amazon Prime - &lt;a href="https://www.amazon.science/blog/how-prime-video-updates-its-app-for-more-than-8-000-device-types" rel="noopener noreferrer"&gt;https://www.amazon.science/blog/how-prime-video-updates-its-app-for-more-than-8-000-device-types&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Google Earth - &lt;a href="https://madewithwebassembly.com/showcase/google-earth/" rel="noopener noreferrer"&gt;https://madewithwebassembly.com/showcase/google-earth/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Autodesk - &lt;a href="https://forums.autodesk.com/t5/engineering-hub-blog/autodesk-open-sources-web-based-usd-viewing-implementation/ba-p/11071751" rel="noopener noreferrer"&gt;https://forums.autodesk.com/t5/engineering-hub-blog/autodesk-open-sources-web-based-usd-viewing-implementation/ba-p/11071751&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;and many more..&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You could and most probably will build WebAssembly applications in the near future.&lt;/p&gt;

&lt;h2&gt;
  
  
  WebAssembly - beyond the browser
&lt;/h2&gt;

&lt;p&gt;WebAssembly was initially designed as a binary instruction format for executing native code efficiently within web browsers. However, its potential extends far beyond the browser.&lt;/p&gt;

&lt;p&gt;Yes, you read it right. There is another project that was built with WASM as the basis known as WASI (WebAssembly System Interface). It is a group of standards API specifications for softwares that compile to the WASM standard.&lt;/p&gt;

&lt;p&gt;We can build cross-platform applications and produce a single binary. Previously if we wanted to build a cross-platform application we had to build binaries for all the platforms and architectures (x86, arm, etc.,) that we needed to support. Or, we have a choice to use Java's virtual machine. By using Java, we are constrained within the Java ecosystem.&lt;/p&gt;

&lt;p&gt;But WASI enables us to write code in any language, build a single binary file and just ship it! WASI runtimes are built for other platforms and architectures, so that we don't have to produce binaries for every platform and architecture combination.&lt;/p&gt;

&lt;p&gt;It's so great that even Docker's founder posted a tweet saying that if WASI was invented in 2008, there is no need for Docker to be created in the first place.&lt;/p&gt;

&lt;p&gt;Take a look at the tweet - &lt;a href="https://x.com/solomonstre/status/1111004913222324225" rel="noopener noreferrer"&gt;https://x.com/solomonstre/status/1111004913222324225&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will look more about WASI in the upcoming posts.&lt;/p&gt;

&lt;h2&gt;
  
  
  Before WebAssembly
&lt;/h2&gt;

&lt;p&gt;Before WebAssembly was invented there were multiple projects that focused on running other programming languages on the web. Some of them were successful to a point, while others failed. WebAssembly was designed to address the limitations of these projects while providing a standardized solution.&lt;/p&gt;

&lt;p&gt;Some of those old projects are,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Microsoft's ActiveX&lt;/strong&gt; - allowed embedding native code in Internet explorer, but there were some major security vulnerabilities&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Java Applet's&lt;/strong&gt; - Let the Java bytecode run in the browser, but it is required to install Java plugin in browsers. It also possessed some degree of security issues.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Adobe's Flash&lt;/strong&gt; - It was a big hit and many people used it. It also came in the form of a plugin. It was a proprietary technology controlled by Adobe which had lack of standards and led to its decline&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Google's Native client (NaCl)&lt;/strong&gt; - Above 3 faced performance issues, while NaCl allowed near-native performances and supported multiple architectures. But it was limited to Chrome browsers and failed to gain wide adoption.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Firefox's asm.js&lt;/strong&gt; - Firefox's effort to bring near-native performance to the web. It can be said that it's a direct successor of today's WASM. asm.js is also specified as a compilation target and compiled to a low-level, statically typed JavaScript. It enabled JIT compilers to execute fast as it was statically typed. But it was still slower than native code and produced large code sizes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Google's Dart&lt;/strong&gt; - Dart could be compiled to JavaScript or run natively in the Dart VM. But it never gained support in other browsers.&lt;/li&gt;
&lt;li&gt;And some more like Silverlight, etc.,&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;All of these failed at some point mainly because of the lack of standards and poor performances in some cases. WebAssembly was designed to solve these problems by being &lt;strong&gt;standard&lt;/strong&gt;, &lt;strong&gt;portable&lt;/strong&gt;, &lt;strong&gt;secure&lt;/strong&gt;, &lt;strong&gt;performant&lt;/strong&gt; and &lt;strong&gt;extensible&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The fact that it was adopted as a W3C standard made all major browsers to support it natively. Previously independent companies came up with solutions and tried to integrate with their own browsers and hoped other browsers would implement. But almost all of them failed. By adopting WebAssembly as a W3C standard made it clear that all major browsers will support it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion and next steps
&lt;/h2&gt;

&lt;p&gt;That's it for this post. This post aimed to provide a high-level understanding of what WASM is and its capabilities. In the upcoming posts, we will dive deep into more topics and build some projects in WebAssembly.&lt;/p&gt;

&lt;p&gt;Read the next post in this series - &lt;a href="https://dev.to/djhemath/build-your-first-webassembly-project-3o3o"&gt;Build your first Webassembly project&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>webassembly</category>
    </item>
  </channel>
</rss>
