Skip to content

Commit 3d768c7

Browse files
committed
more accurate behavior comparing to JS
1 parent 8308ba7 commit 3d768c7

4 files changed

Lines changed: 42 additions & 53 deletions

File tree

packages/typegpu/src/std/boolean.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,7 @@ export const not = dualImpl({
240240
return stitch`!(${vecConstructorStr}(${arg}))`;
241241
}
242242

243-
throw new Error(
244-
`\`std.not\` cannot determine truthiness for runtime value of type: ${ctx.resolve(dataType).value}.`,
245-
);
243+
return 'false';
246244
},
247245
});
248246

packages/typegpu/src/tgsl/wgslGenerator.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,7 @@ const unaryOpCodeToCodegen = {
176176
return snip(`!bool(${argStr})`, bool, 'runtime');
177177
}
178178

179-
if (wgsl.isVec(dataType)) {
180-
console.warn('Use `std.not` for the WGSL unary operator `!` on vector types.');
181-
}
182-
183-
throw new Error(
184-
`The unary operator \`!\` cannot determine truthiness for runtime value of type: ${ctx.resolve(dataType).value}.`,
185-
);
179+
return snip(false, bool, 'constant');
186180
},
187181
} satisfies Partial<Record<tinyest.UnaryOperator, (...args: never[]) => unknown>>;
188182

packages/typegpu/tests/std/boolean/not.test.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,28 @@ describe('not', () => {
114114
`);
115115
});
116116

117-
it('throws when cannot determine truthiness of argument at compile time', ({ root }) => {
117+
it('mimics JS on non-primitive values', ({ root }) => {
118118
const buffer = root.createUniform(d.mat4x4f);
119-
const testFn = tgpu.fn(
120-
[],
121-
d.bool,
122-
)(() => {
123-
return not(buffer.$);
119+
const testFn = tgpu.fn([d.vec3f, d.atomic(d.u32), d.ptrPrivate(d.u32)])((v, a, p) => {
120+
const _b0 = !buffer;
121+
const _b1 = !buffer.$;
122+
const _b2 = !v;
123+
const _b3 = !a;
124+
const _b4 = !p;
125+
const _b5 = !p.$;
124126
});
125127

126-
expect(() => tgpu.resolve([testFn])).toThrowErrorMatchingInlineSnapshot(`
127-
[Error: Resolution of the following tree failed:
128-
- <root>
129-
- fn:testFn
130-
- fn:not: \`std.not\` cannot determine truthiness for runtime value of type: mat4x4f.]
128+
expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(`
129+
"@group(0) @binding(0) var<uniform> buffer: mat4x4f;
130+
131+
fn testFn(v: vec3f, a: atomic<u32>, p: ptr<private, u32>) {
132+
const _b0 = false;
133+
const _b1 = false;
134+
const _b2 = false;
135+
const _b3 = false;
136+
const _b4 = false;
137+
let _b5 = !bool((*p));
138+
}"
131139
`);
132140
});
133141
});

packages/typegpu/tests/tgsl/wgslGenerator.test.ts

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { CodegenState } from '../../src/types.ts';
1616
import { it } from 'typegpu-testing-utility';
1717
import { ArrayExpression } from '../../src/tgsl/generationHelpers.ts';
1818
import { extractSnippetFromFn } from '../utils/parseResolved.ts';
19+
import { UnknownData } from '../../src/tgsl/shaderGenerator_members.ts';
1920

2021
const { NodeTypeCatalog: NODE } = tinyest;
2122

@@ -2099,41 +2100,29 @@ describe('wgslGenerator', () => {
20992100
`);
21002101
});
21012102

2102-
it('warns and throws when cannot determine truthiness of a unary operator `!` operand', ({
2103-
root,
2104-
}) => {
2105-
using warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
2106-
2103+
it('handles unary operator `!` on non-primitive values', ({ root }) => {
21072104
const buffer = root.createUniform(d.mat4x4f);
2108-
const testFn1 = tgpu.fn(
2109-
[],
2110-
d.bool,
2111-
)(() => {
2112-
return !buffer.$;
2113-
});
2114-
2115-
expect(() => tgpu.resolve([testFn1])).toThrowErrorMatchingInlineSnapshot(`
2116-
[Error: Resolution of the following tree failed:
2117-
- <root>
2118-
- fn:testFn1: The unary operator \`!\` cannot determine truthiness for runtime value of type: mat4x4f.]
2119-
`);
2120-
expect(warnSpy).not.toHaveBeenCalled();
2121-
2122-
const testFn2 = tgpu.fn(
2123-
[d.vec3f],
2124-
d.bool,
2125-
)((v) => {
2126-
return !v;
2105+
const testFn = tgpu.fn([d.vec3f, d.atomic(d.u32), d.ptrPrivate(d.u32)])((v, a, p) => {
2106+
const _b0 = !buffer;
2107+
const _b1 = !buffer.$;
2108+
const _b2 = !v;
2109+
const _b3 = !a;
2110+
const _b4 = !p;
2111+
const _b5 = !p.$;
21272112
});
21282113

2129-
expect(() => tgpu.resolve([testFn2])).toThrowErrorMatchingInlineSnapshot(`
2130-
[Error: Resolution of the following tree failed:
2131-
- <root>
2132-
- fn:testFn2: The unary operator \`!\` cannot determine truthiness for runtime value of type: vec3f.]
2133-
`);
2134-
expect(warnSpy.mock.calls[0]![0]).toMatchInlineSnapshot(
2135-
`"Use \`std.not\` for the WGSL unary operator \`!\` on vector types."`,
2136-
);
2114+
expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(`
2115+
"@group(0) @binding(0) var<uniform> buffer: mat4x4f;
2116+
2117+
fn testFn(v: vec3f, a: atomic<u32>, p: ptr<private, u32>) {
2118+
const _b0 = false;
2119+
const _b1 = false;
2120+
const _b2 = false;
2121+
const _b3 = false;
2122+
const _b4 = false;
2123+
let _b5 = !bool((*p));
2124+
}"
2125+
`);
21372126
});
21382127

21392128
it('handles unary operator `!` on numeric and boolean comptime-known operands', () => {

0 commit comments

Comments
 (0)