UNPKG

typegpu

Version:

A thin layer between JS and WebGPU/WGSL that improves development experience and allows for faster iteration.

56 lines (55 loc) 1.98 kB
import { getGPUValue } from "../getGPUValue.js"; import { $internal, $ownSnippet, $resolve } from "../shared/symbols.js"; import { accessIndex } from "../tgsl/accessIndex.js"; import { accessProp } from "../tgsl/accessProp.js"; import { getOwnSnippet } from "../types.js"; export const valueProxyHandler = { get(target, prop) { if (prop in target) { return Reflect.get(target, prop); } if (prop === 'toString' || prop === Symbol.toStringTag || prop === Symbol.toPrimitive) { return () => target.toString(); } if (typeof prop === 'symbol') { return undefined; } const targetSnippet = getOwnSnippet(target); const index = Number(prop); if (!Number.isNaN(index)) { const accessed = accessIndex(targetSnippet, index); if (!accessed) { // Prop was not found, must be missing from this object return undefined; } return new Proxy({ [$internal]: true, [$resolve]: (ctx) => ctx.resolveSnippet(accessed), [$ownSnippet]: accessed, toString: () => `${String(target)}[${prop}]`, }, valueProxyHandler); } const accessed = accessProp(targetSnippet, String(prop)); if (!accessed) { // Prop was not found, must be missing from this object return undefined; } return new Proxy({ [$internal]: true, [$resolve]: (ctx) => ctx.resolveSnippet(accessed), [$ownSnippet]: accessed, toString: () => `${String(target)}.${prop}`, }, valueProxyHandler); }, }; export function getGpuValueRecursively(value) { let unwrapped = value; while (true) { const gpuValue = getGPUValue(unwrapped); if (!gpuValue) { break; } unwrapped = gpuValue; } return unwrapped; }