WebAssembly and JavaScript

loadMAT exposes element, substance, and material evaluation in the browser.

Basic examples

After building the web module, import its loader and evaluate a formula:

import { loadMAT } from "./bindings/web/index.mjs";

const mat = await loadMAT();
const water = mat.substance("H2O");
console.log(water.composite.sum.mass.text);

Supply matter inputs and read a derived mass:

import { loadMAT } from "./bindings/web/index.mjs";

const mat = await loadMAT();
const brine = mat.material("0.965 <H2O> 0.035 <NaCl>", {
  normalization: "mass_fraction",
  massDensity: "1.02*g/cm3",
  volume: "1*L",
});
console.log(brine.matter.mass.text);

Common workflows

Task

Call chain

Result

Initialize MAT

await loadMAT()

Runtime object

Evaluate an element or formula

mat.element(...); mat.substance(...)

MATElementResult; MATSubstanceResult

Build a normalized mixture

mat.material(expression, {normalization, ...})

MATMaterialResult

Supply physical inputs

numberDensity, massDensity, volume, or mass

Derived fields under result.matter

Select isotope mode

{natural: true | false}

Natural or deterministic default isotope

Read a displayed value

result.element.mass.text or result.composite.sum.mass.text

Formatted string

Handle invalid input

try { ... } catch (error) { ... }

Error with diagnostic fields

import { loadMAT } from "./bindings/web/index.mjs";

const mat = await loadMAT();                         // runtime object
const water = mat.substance("H2O");                  // MATSubstanceResult
const brine = mat.material("0.965 <H2O> 0.035 <NaCl>", {
  normalization: "mass_fraction",
  massDensity: "1.02*g/cm3",
  volume: "1*L",
});                                                  // MATMaterialResult

console.log(water.composite.sum.mass.text);          // "18.0153*u"
console.log(brine.matter.mass?.text ?? "not derived");

try {
  mat.substance("H2O)");
} catch (error) {
  console.error(error.message, error.details, error.suggestion);
}

Results are plain JavaScript records, so there is no handle to dispose. Each matter quantity is independently null when it cannot be derived.

See the WebAssembly guide for optional matter inputs and the build instructions.

Current limitations and differences

Compared with the canonical C++ implementation:

  • Results are plain JavaScript records, not mutable C++ Element, Substance, or Material instances. There is no incremental add() API or exposed command-object lifecycle.

  • Matter inputs are PUQ strings. Output quantities contain scalar value, units, and text fields; unavailable optional quantities are null.

  • The runtime loads asynchronously and reports errors as JavaScript Error objects. Calculations use the same isotope data, normalization, and physical assumptions as C++.

Binding reference

This is a lookup index, not a sequence to read from top to bottom. Use Common workflows above to choose a task, then find the exact JavaScript name below. Methods are shown with parentheses; properties and option fields are shown without them. The exported TypeScript declarations are the complete source for result shapes.

  • mat.element(expression, options?) returns MATElementResult.

  • mat.substance(expression, options?) returns MATSubstanceResult.

  • mat.material(expression, options?) returns MATMaterialResult.

  • options.natural selects natural isotopes; it defaults to true.

  • options.normalization accepts number, number_fraction, or mass_fraction for materials.

  • options.numberDensity, options.massDensity, options.volume, and options.mass accept PUQ expression strings.

  • Errors expose message, details, and suggestion fields.

Each result contains kind, the typed record, and matter. The exported TypeScript declarations describe the complete MATElement, MATComposite, MATMatter, and result shapes. The web binding does not expose the mutable native Element, Substance, or Material classes.