@dimvexa/wasm 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE-APACHE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ https://www.apache.org/licenses/LICENSE-2.0
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ https://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/LICENSE-MIT ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 The Dimvexa contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,43 @@
1
- # Temporary Holding Version
1
+ # dimvexa-wasm
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ WebAssembly binding for Dimvexa (ADR-011: `docs/adr/011-wasm-binding.md`). Thin: every call goes
4
+ to the Rust API. Own workspace, needs network to build, not part of `cargo xtask ci`.
5
+
6
+ ```console
7
+ cargo install wasm-bindgen-cli --version 0.2.130 --locked # once; must equal the pin in Cargo.toml
8
+ bindings/wasm/build.sh # writes bindings/wasm/pkg (Node.js target)
9
+ ```
10
+
11
+ ```js
12
+ const d = require('@dimvexa/wasm'); // after `npm install @dimvexa/wasm`; locally: require('./pkg/dimvexa.js')
13
+ d.convert(212, 'degF', 'degC'); // 100 (fast path, f64)
14
+ d.convertExact('98.6', 'degF', 'degC'); // 37 (exact rational, rounded once)
15
+ d.convertQuantity('5 ft', 'm'); // "1.524 m"
16
+ d.addQuantities('20 degC', '5 delta_K'); // "25 degC"
17
+
18
+ const c = new d.DimvexaConversion('m/s', 'km/h'); // parse and compose once
19
+ c.apply(10); // 36
20
+ c.applyBatch(new Float64Array([1, 2, 3])); // one JS->WASM call for the whole array
21
+ c.applyInPlace(arr); // overwrite arr, no allocation
22
+
23
+ try { d.convert(1, 'gal', 'L'); } catch (e) {
24
+ e.name; // "DimvexaError"
25
+ e.code; // "ambiguous_symbol" (stable, see Error::ALL_CODES / errorCodes())
26
+ e.candidates; // ["gal_us", "gal_imp"]
27
+ }
28
+ ```
29
+
30
+ Also: `unitCount()`, `dataVersion()`, `errorCodes()`, `DimvexaConversion#applyExact`, `#inverse`.
31
+
32
+ ## Node comparison
33
+
34
+ `node benchmarks/node/run_dimvexa_wasm.js` runs the same corpus and timers as the mathjs,
35
+ js-quantities and convert-units runners (modes `plain`, `conv`, `batch`); `benchmarks/run_all.sh`
36
+ builds the package and runs it with the others. Numbers from `--quick` runs are smoke tests and
37
+ must not be quoted (`docs/benchmarking.md`).
38
+
39
+ ## npm
40
+
41
+ `build.sh` also writes `pkg/package.json` (name `@dimvexa/wasm`, version taken from `Cargo.toml`). Publishing is
42
+ manual and owner-only: `cd bindings/wasm/pkg && npm publish --access public` as a member of the npm org `dimvexa` (scoped packages need `--access public`).
43
+ Check first with `npm pack --dry-run`.
package/dimvexa.d.ts ADDED
@@ -0,0 +1,102 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /**
5
+ * A conversion between two unit expressions, composed once and applied many times.
6
+ */
7
+ export class DimvexaConversion {
8
+ free(): void;
9
+ [Symbol.dispose](): void;
10
+ /**
11
+ * Fast path over a whole array in one call; returns a new `Float64Array`. The caller's
12
+ * array is not changed. To avoid allocating a result array on every call, reuse one array
13
+ * with `applyInPlace`.
14
+ */
15
+ applyBatch(input: Float64Array | number[]): Float64Array;
16
+ /**
17
+ * Exact path for one value: rational arithmetic, rounded once.
18
+ *
19
+ * # Errors
20
+ *
21
+ * Throws for NaN, infinity, or exact-arithmetic overflow.
22
+ */
23
+ applyExact(x: number): number;
24
+ /**
25
+ * Fast path in place: overwrites the array's contents, allocates nothing on the JS side.
26
+ */
27
+ applyInPlace(data: Float64Array): void;
28
+ /**
29
+ * Fast path (`f64`): at most 1 ulp (linear) or 2 ulp (affine), `docs/precision.md`.
30
+ */
31
+ apply(x: number): number;
32
+ /**
33
+ * The inverse conversion.
34
+ *
35
+ * # Errors
36
+ *
37
+ * Throws on exact-arithmetic overflow.
38
+ */
39
+ inverse(): DimvexaConversion;
40
+ /**
41
+ * Parses both expressions and composes the exact factor once.
42
+ *
43
+ * # Errors
44
+ *
45
+ * Throws a `DimvexaError` (unknown or ambiguous unit, dimension or kind mismatch, ...).
46
+ */
47
+ constructor(from: string, to: string);
48
+ }
49
+
50
+ /**
51
+ * Adds two quantity texts exactly (`"20 degC"` + `"5 delta_K"`) and returns the result as text.
52
+ * A temperature point plus a temperature point is refused with code `kind_mismatch`.
53
+ *
54
+ * # Errors
55
+ *
56
+ * Throws a `DimvexaError` with a stable `code`.
57
+ */
58
+ export function addQuantities(a: string, b: string): string;
59
+
60
+ /**
61
+ * One-shot fast-path conversion: `convert(1, "km", "m")`.
62
+ *
63
+ * # Errors
64
+ *
65
+ * Throws a `DimvexaError` with a stable `code`.
66
+ */
67
+ export function convert(value: number, from: string, to: string): number;
68
+
69
+ /**
70
+ * One-shot exact-path conversion of a decimal string, e.g. `convertExact("98.6", "degF", "degC")`.
71
+ * The string is parsed as an exact rational, so no input rounding happens; the result is
72
+ * rounded once to `f64`.
73
+ *
74
+ * # Errors
75
+ *
76
+ * Throws a `DimvexaError` with a stable `code`.
77
+ */
78
+ export function convertExact(value: string, from: string, to: string): number;
79
+
80
+ /**
81
+ * Converts a quantity text such as `"5 ft"` to `to`, returning the exact quantity as text.
82
+ *
83
+ * # Errors
84
+ *
85
+ * Throws a `DimvexaError` with a stable `code`.
86
+ */
87
+ export function convertQuantity(quantity: string, to: string): string;
88
+
89
+ /**
90
+ * Identifier of the unit table: `v<version>+<hash>`.
91
+ */
92
+ export function dataVersion(): string;
93
+
94
+ /**
95
+ * Every stable error code (`Error::ALL_CODES`).
96
+ */
97
+ export function errorCodes(): Array<any>;
98
+
99
+ /**
100
+ * Number of units in the table.
101
+ */
102
+ export function unitCount(): number;
package/dimvexa.js ADDED
@@ -0,0 +1,487 @@
1
+ /* @ts-self-types="./dimvexa.d.ts" */
2
+
3
+ /**
4
+ * A conversion between two unit expressions, composed once and applied many times.
5
+ */
6
+ class DimvexaConversion {
7
+ static __wrap(ptr) {
8
+ const obj = Object.create(DimvexaConversion.prototype);
9
+ obj.__wbg_ptr = ptr;
10
+ DimvexaConversionFinalization.register(obj, obj.__wbg_ptr, obj);
11
+ return obj;
12
+ }
13
+ __destroy_into_raw() {
14
+ const ptr = this.__wbg_ptr;
15
+ this.__wbg_ptr = 0;
16
+ DimvexaConversionFinalization.unregister(this);
17
+ return ptr;
18
+ }
19
+ free() {
20
+ const ptr = this.__destroy_into_raw();
21
+ wasm.__wbg_dimvexaconversion_free(ptr, 0);
22
+ }
23
+ /**
24
+ * Fast path over a whole array in one call; returns a new `Float64Array`. The caller's
25
+ * array is not changed. To avoid allocating a result array on every call, reuse one array
26
+ * with `applyInPlace`.
27
+ * @param {Float64Array | number[]} input
28
+ * @returns {Float64Array}
29
+ */
30
+ applyBatch(input) {
31
+ const ptr0 = passArrayF64ToWasm0(input, wasm.__wbindgen_malloc);
32
+ const len0 = WASM_VECTOR_LEN;
33
+ const ret = wasm.dimvexaconversion_applyBatch(this.__wbg_ptr, ptr0, len0);
34
+ return ret;
35
+ }
36
+ /**
37
+ * Exact path for one value: rational arithmetic, rounded once.
38
+ *
39
+ * # Errors
40
+ *
41
+ * Throws for NaN, infinity, or exact-arithmetic overflow.
42
+ * @param {number} x
43
+ * @returns {number}
44
+ */
45
+ applyExact(x) {
46
+ const ret = wasm.dimvexaconversion_applyExact(this.__wbg_ptr, x);
47
+ if (ret[2]) {
48
+ throw takeFromExternrefTable0(ret[1]);
49
+ }
50
+ return ret[0];
51
+ }
52
+ /**
53
+ * Fast path in place: overwrites the array's contents, allocates nothing on the JS side.
54
+ * @param {Float64Array} data
55
+ */
56
+ applyInPlace(data) {
57
+ var ptr0 = passArrayF64ToWasm0(data, wasm.__wbindgen_malloc);
58
+ var len0 = WASM_VECTOR_LEN;
59
+ wasm.dimvexaconversion_applyInPlace(this.__wbg_ptr, ptr0, len0, data);
60
+ }
61
+ /**
62
+ * Fast path (`f64`): at most 1 ulp (linear) or 2 ulp (affine), `docs/precision.md`.
63
+ * @param {number} x
64
+ * @returns {number}
65
+ */
66
+ apply(x) {
67
+ const ret = wasm.dimvexaconversion_apply(this.__wbg_ptr, x);
68
+ return ret;
69
+ }
70
+ /**
71
+ * The inverse conversion.
72
+ *
73
+ * # Errors
74
+ *
75
+ * Throws on exact-arithmetic overflow.
76
+ * @returns {DimvexaConversion}
77
+ */
78
+ inverse() {
79
+ const ret = wasm.dimvexaconversion_inverse(this.__wbg_ptr);
80
+ if (ret[2]) {
81
+ throw takeFromExternrefTable0(ret[1]);
82
+ }
83
+ return DimvexaConversion.__wrap(ret[0]);
84
+ }
85
+ /**
86
+ * Parses both expressions and composes the exact factor once.
87
+ *
88
+ * # Errors
89
+ *
90
+ * Throws a `DimvexaError` (unknown or ambiguous unit, dimension or kind mismatch, ...).
91
+ * @param {string} from
92
+ * @param {string} to
93
+ */
94
+ constructor(from, to) {
95
+ let ptr0, len0, ptr1, len1;
96
+ try {
97
+ ptr0 = passStringToWasm0(from, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
98
+ len0 = WASM_VECTOR_LEN;
99
+ ptr1 = passStringToWasm0(to, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
100
+ len1 = WASM_VECTOR_LEN;
101
+ } catch (e) {
102
+ if (ptr1) { wasm.__wbindgen_free(ptr1, len1 * 1, 1); }
103
+ if (ptr0) { wasm.__wbindgen_free(ptr0, len0 * 1, 1); }
104
+ throw e;
105
+ }
106
+ const ret = wasm.dimvexaconversion_new(ptr0, len0, ptr1, len1);
107
+ if (ret[2]) {
108
+ throw takeFromExternrefTable0(ret[1]);
109
+ }
110
+ this.__wbg_ptr = ret[0];
111
+ DimvexaConversionFinalization.register(this, this.__wbg_ptr, this);
112
+ return this;
113
+ }
114
+ }
115
+ if (Symbol.dispose) DimvexaConversion.prototype[Symbol.dispose] = DimvexaConversion.prototype.free;
116
+ exports.DimvexaConversion = DimvexaConversion;
117
+
118
+ /**
119
+ * Adds two quantity texts exactly (`"20 degC"` + `"5 delta_K"`) and returns the result as text.
120
+ * A temperature point plus a temperature point is refused with code `kind_mismatch`.
121
+ *
122
+ * # Errors
123
+ *
124
+ * Throws a `DimvexaError` with a stable `code`.
125
+ * @param {string} a
126
+ * @param {string} b
127
+ * @returns {string}
128
+ */
129
+ function addQuantities(a, b) {
130
+ let deferred4_0;
131
+ let deferred4_1;
132
+ try {
133
+ let ptr0, len0, ptr1, len1;
134
+ try {
135
+ ptr0 = passStringToWasm0(a, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
136
+ len0 = WASM_VECTOR_LEN;
137
+ ptr1 = passStringToWasm0(b, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
138
+ len1 = WASM_VECTOR_LEN;
139
+ } catch (e) {
140
+ if (ptr1) { wasm.__wbindgen_free(ptr1, len1 * 1, 1); }
141
+ if (ptr0) { wasm.__wbindgen_free(ptr0, len0 * 1, 1); }
142
+ throw e;
143
+ }
144
+ const ret = wasm.addQuantities(ptr0, len0, ptr1, len1);
145
+ var ptr3 = ret[0];
146
+ var len3 = ret[1];
147
+ if (ret[3]) {
148
+ ptr3 = 0; len3 = 0;
149
+ throw takeFromExternrefTable0(ret[2]);
150
+ }
151
+ deferred4_0 = ptr3;
152
+ deferred4_1 = len3;
153
+ return getStringFromWasm0(ptr3, len3);
154
+ } finally {
155
+ wasm.__wbindgen_free(deferred4_0, deferred4_1, 1);
156
+ }
157
+ }
158
+ exports.addQuantities = addQuantities;
159
+
160
+ /**
161
+ * One-shot fast-path conversion: `convert(1, "km", "m")`.
162
+ *
163
+ * # Errors
164
+ *
165
+ * Throws a `DimvexaError` with a stable `code`.
166
+ * @param {number} value
167
+ * @param {string} from
168
+ * @param {string} to
169
+ * @returns {number}
170
+ */
171
+ function convert(value, from, to) {
172
+ const val3 = +value;
173
+ let ptr0, len0, ptr1, len1;
174
+ try {
175
+ ptr0 = passStringToWasm0(from, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
176
+ len0 = WASM_VECTOR_LEN;
177
+ ptr1 = passStringToWasm0(to, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
178
+ len1 = WASM_VECTOR_LEN;
179
+ } catch (e) {
180
+ if (ptr1) { wasm.__wbindgen_free(ptr1, len1 * 1, 1); }
181
+ if (ptr0) { wasm.__wbindgen_free(ptr0, len0 * 1, 1); }
182
+ throw e;
183
+ }
184
+ const ret = wasm.convert(val3, ptr0, len0, ptr1, len1);
185
+ if (ret[2]) {
186
+ throw takeFromExternrefTable0(ret[1]);
187
+ }
188
+ return ret[0];
189
+ }
190
+ exports.convert = convert;
191
+
192
+ /**
193
+ * One-shot exact-path conversion of a decimal string, e.g. `convertExact("98.6", "degF", "degC")`.
194
+ * The string is parsed as an exact rational, so no input rounding happens; the result is
195
+ * rounded once to `f64`.
196
+ *
197
+ * # Errors
198
+ *
199
+ * Throws a `DimvexaError` with a stable `code`.
200
+ * @param {string} value
201
+ * @param {string} from
202
+ * @param {string} to
203
+ * @returns {number}
204
+ */
205
+ function convertExact(value, from, to) {
206
+ let ptr0, len0, ptr1, len1, ptr2, len2;
207
+ try {
208
+ ptr0 = passStringToWasm0(value, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
209
+ len0 = WASM_VECTOR_LEN;
210
+ ptr1 = passStringToWasm0(from, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
211
+ len1 = WASM_VECTOR_LEN;
212
+ ptr2 = passStringToWasm0(to, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
213
+ len2 = WASM_VECTOR_LEN;
214
+ } catch (e) {
215
+ if (ptr2) { wasm.__wbindgen_free(ptr2, len2 * 1, 1); }
216
+ if (ptr1) { wasm.__wbindgen_free(ptr1, len1 * 1, 1); }
217
+ if (ptr0) { wasm.__wbindgen_free(ptr0, len0 * 1, 1); }
218
+ throw e;
219
+ }
220
+ const ret = wasm.convertExact(ptr0, len0, ptr1, len1, ptr2, len2);
221
+ if (ret[2]) {
222
+ throw takeFromExternrefTable0(ret[1]);
223
+ }
224
+ return ret[0];
225
+ }
226
+ exports.convertExact = convertExact;
227
+
228
+ /**
229
+ * Converts a quantity text such as `"5 ft"` to `to`, returning the exact quantity as text.
230
+ *
231
+ * # Errors
232
+ *
233
+ * Throws a `DimvexaError` with a stable `code`.
234
+ * @param {string} quantity
235
+ * @param {string} to
236
+ * @returns {string}
237
+ */
238
+ function convertQuantity(quantity, to) {
239
+ let deferred4_0;
240
+ let deferred4_1;
241
+ try {
242
+ let ptr0, len0, ptr1, len1;
243
+ try {
244
+ ptr0 = passStringToWasm0(quantity, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
245
+ len0 = WASM_VECTOR_LEN;
246
+ ptr1 = passStringToWasm0(to, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
247
+ len1 = WASM_VECTOR_LEN;
248
+ } catch (e) {
249
+ if (ptr1) { wasm.__wbindgen_free(ptr1, len1 * 1, 1); }
250
+ if (ptr0) { wasm.__wbindgen_free(ptr0, len0 * 1, 1); }
251
+ throw e;
252
+ }
253
+ const ret = wasm.convertQuantity(ptr0, len0, ptr1, len1);
254
+ var ptr3 = ret[0];
255
+ var len3 = ret[1];
256
+ if (ret[3]) {
257
+ ptr3 = 0; len3 = 0;
258
+ throw takeFromExternrefTable0(ret[2]);
259
+ }
260
+ deferred4_0 = ptr3;
261
+ deferred4_1 = len3;
262
+ return getStringFromWasm0(ptr3, len3);
263
+ } finally {
264
+ wasm.__wbindgen_free(deferred4_0, deferred4_1, 1);
265
+ }
266
+ }
267
+ exports.convertQuantity = convertQuantity;
268
+
269
+ /**
270
+ * Identifier of the unit table: `v<version>+<hash>`.
271
+ * @returns {string}
272
+ */
273
+ function dataVersion() {
274
+ let deferred1_0;
275
+ let deferred1_1;
276
+ try {
277
+ const ret = wasm.dataVersion();
278
+ deferred1_0 = ret[0];
279
+ deferred1_1 = ret[1];
280
+ return getStringFromWasm0(ret[0], ret[1]);
281
+ } finally {
282
+ wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
283
+ }
284
+ }
285
+ exports.dataVersion = dataVersion;
286
+
287
+ /**
288
+ * Every stable error code (`Error::ALL_CODES`).
289
+ * @returns {Array<any>}
290
+ */
291
+ function errorCodes() {
292
+ const ret = wasm.errorCodes();
293
+ return ret;
294
+ }
295
+ exports.errorCodes = errorCodes;
296
+
297
+ /**
298
+ * Number of units in the table.
299
+ * @returns {number}
300
+ */
301
+ function unitCount() {
302
+ const ret = wasm.unitCount();
303
+ if (ret[2]) {
304
+ throw takeFromExternrefTable0(ret[1]);
305
+ }
306
+ return ret[0] >>> 0;
307
+ }
308
+ exports.unitCount = unitCount;
309
+ function __wbg_get_imports() {
310
+ const import0 = {
311
+ __proto__: null,
312
+ __wbg___wbindgen_copy_to_typed_array_680b191e71920dc1: function(arg0, arg1, arg2) {
313
+ new Uint8Array(arg2.buffer, arg2.byteOffset, arg2.byteLength).set(getArrayU8FromWasm0(arg0, arg1));
314
+ },
315
+ __wbg___wbindgen_throw_4c88d8bf264fd984: function(arg0, arg1) {
316
+ throw new Error(getStringFromWasm0(arg0, arg1));
317
+ },
318
+ __wbg_new_37eb45aaf5eeb820: function() {
319
+ const ret = new Array();
320
+ return ret;
321
+ },
322
+ __wbg_new_787b9fb330d55d78: function(arg0, arg1) {
323
+ const ret = new Error(getStringFromWasm0(arg0, arg1));
324
+ return ret;
325
+ },
326
+ __wbg_new_from_slice_aaf85a9746711968: function(arg0, arg1) {
327
+ const ret = new Float64Array(getArrayF64FromWasm0(arg0, arg1));
328
+ return ret;
329
+ },
330
+ __wbg_push_c9f4893fc209f768: function(arg0, arg1) {
331
+ const ret = arg0.push(arg1);
332
+ return ret;
333
+ },
334
+ __wbg_set_2793479888329cb6: function() { return handleError(function (arg0, arg1, arg2) {
335
+ const ret = Reflect.set(arg0, arg1, arg2);
336
+ return ret;
337
+ }, arguments); },
338
+ __wbg_set_name_822e3903f3184979: function(arg0, arg1, arg2) {
339
+ arg0.name = getStringFromWasm0(arg1, arg2);
340
+ },
341
+ __wbindgen_generic_0000000000000001: function(arg0, arg1) {
342
+ // Cast intrinsic for `Ref(String) -> Externref`.
343
+ const ret = getStringFromWasm0(arg0, arg1);
344
+ return ret;
345
+ },
346
+ __wbindgen_init_externref_table: function() {
347
+ const table = wasm.__wbindgen_externrefs;
348
+ table.set(0, undefined);
349
+ table.set(1024, undefined);
350
+ table.set(1025, null);
351
+ table.set(1026, true);
352
+ table.set(1027, false);
353
+ },
354
+ };
355
+ return {
356
+ __proto__: null,
357
+ "./dimvexa_bg.js": import0,
358
+ };
359
+ }
360
+
361
+ const DimvexaConversionFinalization = (typeof FinalizationRegistry === 'undefined')
362
+ ? { register: () => {}, unregister: () => {} }
363
+ : new FinalizationRegistry(ptr => wasm.__wbg_dimvexaconversion_free(ptr, 1));
364
+
365
+ function addToExternrefTable0(obj) {
366
+ const idx = wasm.__externref_table_alloc();
367
+ wasm.__wbindgen_externrefs.set(idx, obj);
368
+ return idx;
369
+ }
370
+
371
+ function getArrayF64FromWasm0(ptr, len) {
372
+ ptr = ptr >>> 0;
373
+ return getFloat64ArrayMemory0().subarray(ptr / 8, ptr / 8 + len);
374
+ }
375
+
376
+ function getArrayU8FromWasm0(ptr, len) {
377
+ ptr = ptr >>> 0;
378
+ return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
379
+ }
380
+
381
+ let cachedFloat64ArrayMemory0 = null;
382
+ function getFloat64ArrayMemory0() {
383
+ if (cachedFloat64ArrayMemory0 === null || cachedFloat64ArrayMemory0.byteLength === 0) {
384
+ cachedFloat64ArrayMemory0 = new Float64Array(wasm.memory.buffer);
385
+ }
386
+ return cachedFloat64ArrayMemory0;
387
+ }
388
+
389
+ function getStringFromWasm0(ptr, len) {
390
+ ptr = ptr >>> 0;
391
+ return decodeText(getUint8ArrayMemory0().subarray(ptr, ptr + len));
392
+ }
393
+
394
+ let cachedUint8ArrayMemory0 = null;
395
+ function getUint8ArrayMemory0() {
396
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
397
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
398
+ }
399
+ return cachedUint8ArrayMemory0;
400
+ }
401
+
402
+ function handleError(f, args) {
403
+ try {
404
+ return f.apply(this, args);
405
+ } catch (e) {
406
+ const idx = addToExternrefTable0(e);
407
+ wasm.__wbindgen_exn_store(idx);
408
+ }
409
+ }
410
+
411
+ function passArrayF64ToWasm0(arg, malloc) {
412
+ const ptr = malloc(arg.length * 8, 8) >>> 0;
413
+ getFloat64ArrayMemory0().set(arg, ptr / 8);
414
+ WASM_VECTOR_LEN = arg.length;
415
+ return ptr;
416
+ }
417
+
418
+ function passStringToWasm0(arg, malloc, realloc) {
419
+ if (realloc === undefined) {
420
+ const buf = cachedTextEncoder.encode(arg);
421
+ const ptr = malloc(buf.length, 1) >>> 0;
422
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
423
+ WASM_VECTOR_LEN = buf.length;
424
+ return ptr;
425
+ }
426
+
427
+ let len = arg.length;
428
+ let ptr = malloc(len, 1) >>> 0;
429
+
430
+ const mem = getUint8ArrayMemory0();
431
+
432
+ let offset = 0;
433
+
434
+ for (; offset < len; offset++) {
435
+ const code = arg.charCodeAt(offset);
436
+ if (code > 0x7F) break;
437
+ mem[ptr + offset] = code;
438
+ }
439
+ if (offset !== len) {
440
+ if (offset !== 0) {
441
+ arg = arg.slice(offset);
442
+ }
443
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
444
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
445
+ const ret = cachedTextEncoder.encodeInto(arg, view);
446
+
447
+ offset += ret.written;
448
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
449
+ }
450
+
451
+ WASM_VECTOR_LEN = offset;
452
+ return ptr;
453
+ }
454
+
455
+ function takeFromExternrefTable0(idx) {
456
+ const value = wasm.__wbindgen_externrefs.get(idx);
457
+ wasm.__externref_table_dealloc(idx);
458
+ return value;
459
+ }
460
+
461
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true });
462
+ cachedTextDecoder.decode();
463
+ function decodeText(bytes) {
464
+ return cachedTextDecoder.decode(bytes);
465
+ }
466
+
467
+ const cachedTextEncoder = new TextEncoder();
468
+
469
+ if (!('encodeInto' in cachedTextEncoder)) {
470
+ cachedTextEncoder.encodeInto = function (arg, view) {
471
+ const buf = cachedTextEncoder.encode(arg);
472
+ view.set(buf);
473
+ return {
474
+ read: arg.length,
475
+ written: buf.length
476
+ };
477
+ };
478
+ }
479
+
480
+ let WASM_VECTOR_LEN = 0;
481
+
482
+ const wasmPath = `${__dirname}/dimvexa_bg.wasm`;
483
+ const wasmBytes = require('fs').readFileSync(wasmPath);
484
+ const wasmModule = new WebAssembly.Module(wasmBytes);
485
+ let wasmInstance = new WebAssembly.Instance(wasmModule, __wbg_get_imports());
486
+ let wasm = wasmInstance.exports;
487
+ wasm.__wbindgen_start();
Binary file
@@ -0,0 +1,25 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ export const memory: WebAssembly.Memory;
4
+ export const __wbg_dimvexaconversion_free: (a: number, b: number) => void;
5
+ export const addQuantities: (a: number, b: number, c: number, d: number) => [number, number, number, number];
6
+ export const convert: (a: number, b: number, c: number, d: number, e: number) => [number, number, number];
7
+ export const convertExact: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number];
8
+ export const convertQuantity: (a: number, b: number, c: number, d: number) => [number, number, number, number];
9
+ export const dataVersion: () => [number, number];
10
+ export const dimvexaconversion_apply: (a: number, b: number) => number;
11
+ export const dimvexaconversion_applyBatch: (a: number, b: number, c: number) => any;
12
+ export const dimvexaconversion_applyExact: (a: number, b: number) => [number, number, number];
13
+ export const dimvexaconversion_applyInPlace: (a: number, b: number, c: number, d: any) => void;
14
+ export const dimvexaconversion_inverse: (a: number) => [number, number, number];
15
+ export const dimvexaconversion_new: (a: number, b: number, c: number, d: number) => [number, number, number];
16
+ export const errorCodes: () => any;
17
+ export const unitCount: () => [number, number, number];
18
+ export const __wbindgen_exn_store: (a: number) => void;
19
+ export const __externref_table_alloc: () => number;
20
+ export const __wbindgen_externrefs: WebAssembly.Table;
21
+ export const __wbindgen_malloc: (a: number, b: number) => number;
22
+ export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
23
+ export const __wbindgen_free: (a: number, b: number, c: number) => void;
24
+ export const __externref_table_dealloc: (a: number) => void;
25
+ export const __wbindgen_start: () => void;
package/package.json CHANGED
@@ -1,6 +1,13 @@
1
1
  {
2
2
  "name": "@dimvexa/wasm",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
4
+ "description": "Unit conversion with exact factors and loud failures (WebAssembly build of the Rust library)",
5
+ "license": "MIT OR Apache-2.0",
6
+ "repository": { "type": "git", "url": "git+https://github.com/welworx/dimvexa.git", "directory": "bindings/wasm" },
7
+ "homepage": "https://github.com/welworx/dimvexa",
8
+ "keywords": ["units", "unit-conversion", "dimensional-analysis", "wasm"],
9
+ "main": "dimvexa.js",
10
+ "types": "dimvexa.d.ts",
11
+ "files": ["dimvexa.js", "dimvexa.d.ts", "dimvexa_bg.wasm", "dimvexa_bg.wasm.d.ts", "README.md", "LICENSE-MIT", "LICENSE-APACHE"],
12
+ "engines": { "node": ">=18" }
13
+ }