Back to Boa

@boa-dev/boa_wasm

ffi/wasm/README.md

0.222.9 KB
Original Source

@boa-dev/boa_wasm

WebAssembly bindings for Boa, a Javascript engine written in Rust.

Overview

This package provides WebAssembly bindings to run JavaScript code using the Boa engine directly in the browser or Node.js environments. Boa supports more than 90% of the latest ECMAScript specification.

Installation

bash
npm install @boa-dev/boa_wasm

Or with yarn:

bash
yarn add @boa-dev/boa_wasm

Usage

Browser

javascript
import init, { evaluate } from "@boa-dev/boa_wasm";

// Initialize the Wasm module
await init();

// Evaluate JavaScript code
try {
  const result = evaluate("1 + 1");
  console.log(result); // "2"
} catch (error) {
  console.error("Evaluation error:", error);
}

Node.js

javascript
const { evaluate } = require("@boa-dev/boa_wasm");

try {
  const result = evaluate("1 + 1");
  console.log(result); // "2"
} catch (error) {
  console.error("Evaluation error:", error);
}

Advanced Example

javascript
import init, { evaluate } from "@boa-dev/boa_wasm";

await init();

const code = `
  function fibonacci(n) {
    if (n <= 1) return n;
    return fibonacci(n - 1) + fibonacci(n - 2);
  }
  
  fibonacci(10)
`;

const result = evaluate(code);
console.log(result); // "55"

API

evaluate(src: string): string

Evaluates the given ECMAScript code and returns the result as a string.

Parameters:

  • src - A string containing the JavaScript code to evaluate

Returns:

  • A string representation of the evaluation result

Throws:

  • A JsValue error if the execution throws an exception

Features

Boa's Wasm build includes:

  • Annex B: Legacy ECMAScript features
  • Internationalization: Full Intl API support
  • Experimental features: Latest ECMAScript proposals

Live Demo

Try Boa in your browser at our live playground!

Conformance

Check out Boa's ECMAScript Test262 conformance results to see our progress on implementing the ECMAScript specification.

Documentation

Contributing

Contributions are welcome! Please check out the contributing guide to get started.

Communication

License

This project is licensed under the Unlicense or MIT licenses, at your option.