.. _puq-web: WebAssembly and JavaScript ========================== ``loadPUQ`` exposes the native PUQ conversion command to JavaScript. The WebAssembly build also powers the live converter on the :doc:`Conversion and unit systems <../conversion>` page. Basic examples -------------- After building the web module, import its loader and convert an energy: .. code-block:: javascript import { loadPUQ } from "./bindings/web/index.mjs"; const puq = await loadPUQ(); console.log(puq.convert("35*eV", "J")); // 5.60762e-18*J Select unit systems for a contextual conversion: .. code-block:: javascript import { loadPUQ } from "./bindings/web/index.mjs"; const puq = await loadPUQ(); console.log(puq.convert("12*statA", "A", { inputSystem: "ESU", outputSystem: "SI", outputQuantity: "I", })); Common workflows ---------------- .. list-table:: :header-rows: 1 * - Task - Call chain - Result * - Initialize PUQ - ``await loadPUQ()`` - Runtime object * - Discover systems - ``puq.systems()`` - ``string[]`` * - Convert with SI defaults - ``puq.convert(expression, units)`` - Formatted ``string`` * - Convert with a system context - ``puq.convert(..., {inputSystem, outputSystem, outputQuantity})`` - Formatted ``string`` * - Handle a rejected conversion - ``try { ... } catch (error) { ... }`` - ``Error`` with diagnostic fields .. code-block:: javascript const puq = await loadPUQ(); // runtime object const systems = puq.systems(); // string[] const result = puq.convert("12*statA", "A", { inputSystem: "ESU", outputSystem: "SI", outputQuantity: "I", }); // string try { puq.convert("1*m", "s"); } catch (error) { console.error(error.message, error.details, error.suggestion); } See the :doc:`WebAssembly guide <../../../integrations/web>` for building the module and serving its JavaScript and WASM files. Current limitations and differences ----------------------------------- Compared with the canonical C++ implementation: - The binding exposes the C++ conversion command, not the mutable ``Quantity`` object model. Results are formatted strings rather than numerical values with separate unit and uncertainty fields. - Conversion retains uncertainties present in the input. General quantity arithmetic, correlation, Monte Carlo sampling, custom-unit registration, and full unit-definition listings are not exposed. - Loaders initialize WebAssembly asynchronously. Unit-system choices are per-call options rather than native C++ context objects. 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 TypeScript declaration file is the complete source for option and result shapes. - ``loadPUQ(options?)`` loads the WebAssembly runtime. - ``puq.systems()`` returns the supported unit-system abbreviations. - ``puq.convert(expression, outputUnits, options?)`` returns formatted text. - ``options.inputSystem``, ``options.outputSystem``, and ``options.outputQuantity`` are optional strings. - Rejected conversions throw an ``Error`` with ``message``, ``details``, and ``suggestion`` fields.