Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions packages/unplugin-typegpu/test/use-gpu-directive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,70 @@ describe('[BABEL] "use gpu" directive', () => {
`);
});

it('makes plugin transpile marked object method', () => {
const code = `\
const obj = {
mod: (a: number, b: number): number => {
'use gpu';
return a % b;
}
}

const isPrime = (n: number): boolean => {
'use gpu';
if (n <= 1) {
return false;
}

for (let i = 2; i < n; i++) {
if (obj.mod(n, i) === 0) {
return false;
}
}
return true;
}
`;

expect(babelTransform(code)).toMatchInlineSnapshot(`
"const obj = {
mod: ($ => (globalThis.__TYPEGPU_META__ ??= new WeakMap()).set($.f = (a: number, b: number): number => {
'use gpu';

return a % b;
}, {
v: 1,
name: void 0,
ast: {"params":[{"type":"i","name":"a"},{"type":"i","name":"b"}],"body":[0,[[10,[1,"a","%","b"]]]],"externalNames":[]},
get externals() {
return {};
}
}) && $.f)({})
};
const isPrime = ($ => (globalThis.__TYPEGPU_META__ ??= new WeakMap()).set($.f = (n: number): boolean => {
'use gpu';

if (n <= 1) {
return false;
}
for (let i = 2; i < n; i++) {
if (obj.mod(n, i) === 0) {
return false;
}
}
return true;
}, {
v: 1,
name: "isPrime",
ast: {"params":[{"type":"i","name":"n"}],"body":[0,[[11,[1,"n","<=",[5,"1"]],[0,[[10,false]]]],[14,[12,"i",[5,"2"]],[1,"i","<","n"],[102,"++","i"],[0,[[11,[1,[6,[7,"obj","mod"],["n","i"]],"==",[5,"0"]],[0,[[10,false]]]]]]],[10,true]]],"externalNames":["obj"]},
get externals() {
return {
obj
};
}
}) && $.f)({});"
`);
});

it('parses when no typegpu import', () => {
const code = `\
function add(a, b) {
Expand Down Expand Up @@ -430,6 +494,65 @@ describe('[ROLLUP] "use gpu" directive', () => {
`);
});

it('makes plugin transpile marked object method', async () => {
const code = `\
const obj = {
mod: (a, b) => {
'use gpu';
return a % b;
}
}

const isPrime = (n) => {
'use gpu';
if (n <= 1) {
return false;
}

for (let i = 2; i < n; i++) {
if (obj.mod(n, i) === 0) {
return false;
}
}
return true;
}
`;

expect(await rollupTransform(code)).toMatchInlineSnapshot(`
"const obj = {
mod: (($ => (globalThis.__TYPEGPU_META__ ??= new WeakMap()).set($.f = ((a, b) => {
'use gpu';
return a % b;
}), {
v: 1,
name: undefined,
ast: {"params":[{"type":"i","name":"a"},{"type":"i","name":"b"}],"body":[0,[[10,[1,"a","%","b"]]]],"externalNames":[]},
get externals() { return {}; },
}) && $.f)({}))
};

(($ => (globalThis.__TYPEGPU_META__ ??= new WeakMap()).set($.f = ((n) => {
'use gpu';
if (n <= 1) {
return false;
}

for (let i = 2; i < n; i++) {
if (obj.mod(n, i) === 0) {
return false;
}
}
return true;
}), {
v: 1,
name: "isPrime",
ast: {"params":[{"type":"i","name":"n"}],"body":[0,[[11,[1,"n","<=",[5,"1"]],[0,[[10,false]]]],[14,[12,"i",[5,"2"]],[1,"i","<","n"],[102,"++","i"],[0,[[11,[1,[6,[7,"obj","mod"],["n","i"]],"==",[5,"0"]],[0,[[10,false]]]]]]],[10,true]]],"externalNames":["obj"]},
get externals() { return {obj}; },
}) && $.f)({}));
"
`);
});

it('throws when hoisting was meant to be used', async () => {
const code = `\
const sum = add(1, 2);
Expand Down