@threenative/three-native 0.3.15 → 0.3.17

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@threenative/three-native",
3
- "version": "0.3.15",
3
+ "version": "0.3.17",
4
4
  "license": "MIT",
5
5
  "description": "The native engine's machine-readable compatibility catalog and the generators that adapt it to TypeScript declarations and the C binding ABI.",
6
6
  "keywords": [
@@ -73,6 +73,8 @@ export interface IBrowserRuntime {
73
73
  release(self: IEngineRef): void;
74
74
  /** For each ref, how many references other engine objects hold to its object; one call a batch. */
75
75
  engineReferences?(refs: readonly IEngineRef[]): ArrayLike<number>;
76
+ /** PRD-898 J5: moves whenever an `engineReferences` count may have changed (`tn_ownership_epoch`). */
77
+ ownershipEpoch?(): number;
76
78
  /**
77
79
  * PRD-828: advances a batch of animation rigs in one crossing — the weight ramp, the stride rate and
78
80
  * one mixer step each. A backend that cannot batch leaves it out and the JS loop keeps running.
@@ -156,7 +158,10 @@ type AbiCall =
156
158
  | "_tn_anim_tick";
157
159
  export type TnAbiModule = Record<AbiCall, (...args: number[]) => number> &
158
160
  Partial<
159
- Record<"_tnw_attribute_view" | "_tnw_attribute_view_release", (...args: number[]) => number>
161
+ Record<
162
+ "_tnw_attribute_view" | "_tnw_attribute_view_release" | "_tn_ownership_epoch",
163
+ (...args: number[]) => number
164
+ >
160
165
  > &
161
166
  Record<"HEAPU8", Uint8Array> &
162
167
  Record<"HEAPF64", Float64Array> &
@@ -87,6 +87,8 @@ export interface IBrowserEngine {
87
87
 
88
88
  /** Safe points a pass over the settled wrappers spans. */
89
89
  const SETTLED_PASS = 64;
90
+ /** Safe points between settled passes the ownership epoch did not ask for (adapter.cpp kFullEvery). */
91
+ const FULL_PASS_EVERY = 512;
90
92
 
91
93
  /** An engine object's address, kept on it after the first ask: every lane read and write needs it. */
92
94
  const ADDRESS = Symbol("tn.engineAddress");
@@ -328,6 +330,7 @@ export function defineBrowserClasses(
328
330
  debug?.crossing("call", self, method, typeNames.get(self.type) ?? String(self.type));
329
331
  return source.invoke(self, method, args);
330
332
  },
333
+ ...(source.ownershipEpoch === undefined ? {} : { ownershipEpoch: source.ownershipEpoch }),
331
334
  ...(source.engineReferences === undefined
332
335
  ? {}
333
336
  : {
@@ -625,6 +628,12 @@ export function defineBrowserClasses(
625
628
  };
626
629
  let cursor = settled.values(); // collect()'s place in `settled`
627
630
  let peak = 0; // the most wrappers settled since this pass of `cursor` began
631
+ // PRD-898 J5: a settled wrapper can only lose its engine references when the ownership epoch moves.
632
+ // While it stands still and no pass is due, a safe point asks nothing about the settled set.
633
+ let epochSeen: number | undefined;
634
+ let epochAtPass: number | undefined;
635
+ let passDue = true;
636
+ let sinceFullPass = 0;
628
637
  // userData is the game's: it lives with the wrapper, which `held` keeps while the engine references it.
629
638
  const userData = new WeakMap<object, unknown>();
630
639
  const shaderNodes = new Map<string, Map<string, unknown>>(); // material key -> slot -> TSL node
@@ -981,6 +990,14 @@ export function defineBrowserClasses(
981
990
  } finally {
982
991
  if (loans.length > mark) result = repay(mark, result);
983
992
  if (drives) animated = new WeakMap();
993
+ // A native record batch writes other attributes through the receiver's method.
994
+ // Keep their JS arrays current so a later read cannot upload pre-refilter data.
995
+ if (method === "__tnRecords" && Array.isArray(args[0])) {
996
+ for (const attribute of args[0] as object[]) {
997
+ const entry = arrays.get(attribute);
998
+ if (entry !== undefined) syncFull(attribute, entry);
999
+ }
1000
+ }
984
1001
  }
985
1002
  // The engine's clone and copy carry the material's graphs; the TSL nodes a slot reads back
986
1003
  // follow them, or `clone().maskNode` answers null and a composed mask drops the source's.
@@ -1887,8 +1904,17 @@ export function defineBrowserClasses(
1887
1904
  // The sixty-fourth is of the pass's peak, so releases during a pass do not shrink it: a wrapper the
1888
1905
  // engine lets go is asked within two passes (tests/browser-backend-gc.ts). Holding a wrapper a
1889
1906
  // little longer only delays its release.
1907
+ const epoch = runtime.ownershipEpoch?.();
1908
+ // The native adapter's rule (adapter.cpp `collect`): a change the epoch cannot see is caught by a
1909
+ // pass every FULL_PASS_EVERY safe points.
1910
+ if (epoch === undefined || epoch !== epochSeen || ++sinceFullPass >= FULL_PASS_EVERY) {
1911
+ epochSeen = epoch;
1912
+ sinceFullPass = 0;
1913
+ passDue = true;
1914
+ }
1915
+ if (held.size === 0 && !passDue) return;
1890
1916
  peak = Math.max(peak, settled.size);
1891
- const budget = Math.ceil(peak / SETTLED_PASS);
1917
+ const budget = passDue ? Math.ceil(peak / SETTLED_PASS) : 0;
1892
1918
  const candidates: object[] = [...held];
1893
1919
  const fresh = candidates.length;
1894
1920
  while (candidates.length < fresh + budget) {
@@ -1899,6 +1925,9 @@ export function defineBrowserClasses(
1899
1925
  }
1900
1926
  cursor = settled.values();
1901
1927
  peak = settled.size;
1928
+ // A whole pass ran with the epoch unmoved: nothing it already asked can have changed since.
1929
+ if (epoch !== undefined && epochAtPass === epoch) passDue = false;
1930
+ epochAtPass = epoch;
1902
1931
  // An emptied set would restart forever: a safe point that let the last wrapper go hung here.
1903
1932
  if (candidates.length > fresh || settled.size === 0) break;
1904
1933
  }
@@ -70,7 +70,9 @@ export function setValues(target: object, className: string, values: object): vo
70
70
  if (value === undefined) continue;
71
71
  const current = self[key];
72
72
  if (current === undefined)
73
- throw new TypeError(`TN_BROWSER_PARAMETER_UNSUPPORTED: ${className} has no '${key}'`);
73
+ throw new TypeError(
74
+ `TN_BROWSER_PARAMETER_UNSUPPORTED: ${className} has no '${key}'. The engine does not implement this parameter on ${className}: remove the key.`,
75
+ );
74
76
  if (isWrapperOf(current, "Color")) setColor(current, value);
75
77
  else if (isWrapperOf(current, "Vector3") && (value as { isVector3?: boolean }).isVector3)
76
78
  current.copy?.(value);
@@ -392,6 +392,9 @@ export function createWasmRuntime(abi: TnAbiModule): IBrowserRuntime {
392
392
  );
393
393
  return readValue(out);
394
394
  }),
395
+ ...(abi._tn_ownership_epoch === undefined
396
+ ? {}
397
+ : { ownershipEpoch: () => (abi._tn_ownership_epoch as () => number)() }),
395
398
  engineReferences: (refs) =>
396
399
  scoped(() => {
397
400
  const handles = alloc(Math.max(1, refs.length) * HANDLE);