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 |
|
Runtime object |
Evaluate an element or formula |
|
|
Build a normalized mixture |
|
|
Supply physical inputs |
|
Derived fields under |
Select isotope mode |
|
Natural or deterministic default isotope |
Read a displayed value |
|
Formatted |
Handle invalid input |
|
|
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, andtextfields; unavailable optional quantities arenull.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?)returnsMATElementResult.mat.substance(expression, options?)returnsMATSubstanceResult.mat.material(expression, options?)returnsMATMaterialResult.options.naturalselects natural isotopes; it defaults totrue.options.normalizationacceptsnumber,number_fraction, ormass_fractionfor materials.options.numberDensity,options.massDensity,options.volume, andoptions.massaccept PUQ expression strings.Errors expose
message,details, andsuggestionfields.
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.