0PricingLogin
WebAssembly (WASM) for High Performance Apps · Lesson

Basic Data Exchange: Primitives

Understand how to pass simple data types like integers and floats between your JavaScript and WASM code.

Exchanging Basic Data Types

Welcome! In this lesson, we'll learn how to pass simple data types, known as primitives, between your JavaScript code and your WebAssembly (WASM) module compiled from C/C++.

Primitives include numbers like integers (int) and floating-point numbers (float, double). This is fundamental for enabling communication and interaction between your web application and high-performance WASM logic.

JS to WASM: Integer Input

Passing an integer from JavaScript to a C/C++ WASM function is quite direct. You define your C/C++ function with integer parameters, and Emscripten handles the necessary type mapping.

For example, a C function to add two integers would look like this:

int add_integers(int a, int b) {
  // Function body
  return a + b;
}

All lessons in this course

  1. Compiling C/C++ to WASM with Emscripten
  2. Loading & Running WASM in JavaScript
  3. Basic Data Exchange: Primitives
  4. Calling JavaScript Functions from C/C++
← Back to WebAssembly (WASM) for High Performance Apps