Skip to content
ImagineWorld

Part II — JavaScript Interop

node_idnode-book_rust-wasm-fusion_section-02-javascript-interop
node_typebook-part
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_titlePart II — JavaScript Interop
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

Attribute macros are a way to expose JavaScript functions to JavaScript code. They allow you to define functions that can be called from JavaScript, and they provide a way to manage the lifecycle of these functions.

Exposed APIs are functions that are exposed to JavaScript through attribute macros. These functions can be called from JavaScript and can perform various tasks such as reading and writing data, performing calculations, and more.

#[js_namespace]
mod my_module {
#[js_function]
pub fn add(a: f64, b: f64) -> f64 {
a + b
}
}

In this example, the add function is exposed to JavaScript through the js_function attribute macro. The function takes two floating-point numbers as arguments and returns their sum.

  • Ease of Use: Attribute macros make it easy to expose Rust functions to JavaScript.
  • Type Safety: The functions are type-safe, ensuring that the correct arguments are passed.
  • Performance: Attribute macros provide high-performance access to Rust functions from JavaScript.

Attribute macros and exposed APIs are powerful tools for integrating Rust with JavaScript. They allow you to create efficient and type-safe interfaces between the two languages.