@danielsimonjr/mathts-wasm 0.5.0 → 0.5.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/README.md CHANGED
@@ -109,6 +109,11 @@ runs as a step of `gen:manifest`. The build fails when an export or a parameter
109
109
  that list. The debug glue (`@danielsimonjr/mathts-wasm/debug.js`) throws `mathts glue: <export> wrote its input
110
110
  parameter <param>` when a kernel writes a parameter that the list marks `in`.
111
111
 
112
+ The ESM glue reuses temporary WASM memory after a synchronous adapted call finishes copying
113
+ its results back to JavaScript. A live `Complex` handle keeps the heap intact. Calling the raw
114
+ `__new` or `__pin` exports disables automatic reuse until an explicit `heap_reset()`.
115
+ Only reset when all caller-owned pointers and `Complex` handles from that heap are finished.
116
+
112
117
  ### Dense decompositions (AS exports)
113
118
 
114
119
  From `algebra/decomposition.ts` (used by matrix `WASMBackend` tests, not public
@@ -1829,7 +1829,7 @@ async function instantiate(module, imports = {}) {
1829
1829
  ).slice();
1830
1830
  }
1831
1831
  /* mathts: typed-array write-back */
1832
- const __out = [];
1832
+ let __out = [];
1833
1833
  function __lowerTypedArray(constructor, id, align, values, track, name) {
1834
1834
  if (track & 4) __out.length = 0;
1835
1835
  if (values == null) return 0;
@@ -1931,15 +1931,30 @@ async function instantiate(module, imports = {}) {
1931
1931
  }
1932
1932
  function __abortAware(adapted) {
1933
1933
  const guarded = {};
1934
+ let depth = 0;
1935
+ let rawAllocations = false;
1934
1936
  for (const key of new Set([...Object.keys(exports), ...Object.keys(adapted)])) {
1935
1937
  const value = adapted[key];
1936
1938
  guarded[key] =
1937
1939
  typeof value === "function" && !key.startsWith("abort_")
1938
1940
  ? (...args) => {
1941
+ const previous = __out;
1942
+ __out = [];
1943
+ depth++;
1944
+ if (key === "__new" || key === "__pin") rawAllocations = true;
1939
1945
  try {
1940
- return value(...args);
1946
+ const result = value(...args);
1947
+ if (key === "heap_reset") rawAllocations = false;
1948
+ return result;
1941
1949
  } catch (error) {
1942
1950
  throw __abortError(error);
1951
+ } finally {
1952
+ __out = previous;
1953
+ if (--depth === 0 && Object.hasOwn(adapted, key)
1954
+ && !rawAllocations && refcounts.size === 0
1955
+ && typeof exports.heap_reset === "function") {
1956
+ exports.heap_reset();
1957
+ }
1943
1958
  }
1944
1959
  }
1945
1960
  : value;