ifc_lite_wasm/lib.rs
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! # IFC-Lite WebAssembly Bindings
6//!
7//! JavaScript/TypeScript API for IFC-Lite built with [wasm-bindgen](https://docs.rs/wasm-bindgen).
8//!
9//! ## Overview
10//!
11//! This crate provides WebAssembly bindings for IFC-Lite, enabling high-performance
12//! IFC parsing and geometry processing in web browsers.
13//!
14//! ## Features
15//!
16//! - **Zero-Copy Buffers**: Direct GPU buffer access without data copying
17//! - **Streaming Parse**: Event-based parsing with progress callbacks
18//! - **Small Bundle**: ~60 KB WASM binary, ~20 KB gzipped
19//!
20//! ## JavaScript Usage
21//!
22//! ```javascript
23//! import init, { IfcAPI, version } from 'ifc-lite-wasm';
24//!
25//! // Initialize WASM
26//! await init();
27//!
28//! // Create API instance
29//! const api = new IfcAPI();
30//!
31//! // Parse IFC file
32//! const buffer = await fetch('model.ifc').then(r => r.arrayBuffer());
33//! const result = api.parse(new Uint8Array(buffer));
34//!
35//! console.log(`Parsed ${result.entityCount} entities`);
36//! console.log(`Version: ${version()}`);
37//! ```
38//!
39//! ## Streaming Parse
40//!
41//! ```javascript
42//! const result = await api.parseStreaming(data, (event) => {
43//! if (event.type === 'progress') {
44//! console.log(`Progress: ${event.percent}%`);
45//! }
46//! });
47//! ```
48
49use wasm_bindgen::prelude::*;
50
51// The crate's panic hook (console report + analytics location stash — see
52// utils.rs). Kept as the ONLY way a hook is installed here: installing
53// `console_error_panic_hook::set_once` directly would silently drop the
54// location stash the viewer's error tracking relies on (#1196, #2527).
55pub use utils::set_panic_hook;
56// Observability seam for the wasm32 test leg only (tests/panic_stash.rs).
57#[cfg(all(feature = "console_error_panic_hook", target_arch = "wasm32"))]
58#[doc(hidden)]
59pub use utils::stash_location_parts;
60
61// Threaded build (off by default): exposes `initThreadPool(n)` to JS and makes
62// the geometry crate's `par_iter` element loops parallel in WASM. Built as a
63// SEPARATE bundle (atomics/shared-memory flags) via `build-wasm.sh BUILD_THREADED=1`;
64// requires cross-origin isolation at runtime. See
65// docs/architecture/csg-threading-design.md.
66#[cfg(feature = "threads")]
67pub use wasm_bindgen_rayon::init_thread_pool;
68
69mod api;
70mod progress_hook;
71#[cfg(feature = "opening-perf-trace")]
72mod opening_perf_trace;
73#[cfg(feature = "console-tracing")]
74mod tracing_console;
75mod utils;
76mod zero_copy;
77
78pub use api::IfcAPI;
79pub use progress_hook::set_geometry_progress_callback;
80mod alignment_axis;
81pub use alignment_axis::AlignmentAxisJs;
82pub use utils::set_panic_hook as init_panic_hook;
83pub use zero_copy::{
84 get_memory, MeshCollection, MeshDataJs, SymbolicCircle, SymbolicFillArea, SymbolicPolyline,
85 SymbolicRepresentationCollection, SymbolicText,
86};
87
88/// Initialize the WASM module.
89///
90/// This function is called automatically when the WASM module is loaded.
91/// It sets up panic hooks for better error messages in the browser console,
92/// and points core's scan diagnostics at that console too.
93#[wasm_bindgen(start)]
94pub fn init() {
95 utils::set_panic_hook();
96 // `ifc_lite_core::report_oversized_ids` (#3395) writes to stderr by
97 // default, and wasm32 has none — leaving every core and processing scan
98 // this module drives silently unable to say it refused a record, which is
99 // the absence-reads-as-success shape the issue is about. Set-once, and
100 // this runs before any `IfcAPI` is constructed.
101 ifc_lite_core::set_report_sink(|message| {
102 web_sys::console::warn_1(&JsValue::from_str(message));
103 });
104}
105
106/// Get the version of IFC-Lite.
107///
108/// # Returns
109///
110/// Version string (e.g., "0.1.0")
111///
112/// # Example
113///
114/// ```javascript
115/// console.log(`IFC-Lite version: ${version()}`);
116/// ```
117#[wasm_bindgen]
118pub fn version() -> String {
119 env!("CARGO_PKG_VERSION").to_string()
120}