Skip to content

Commit

Permalink
Merge pull request #63 from mhawryluk/function-arguments
Browse files Browse the repository at this point in the history
Add argument and return type info to non-entry FunctionInfo
  • Loading branch information
brendan-duncan authored Oct 22, 2024
2 parents e77eca2 + 9415cdf commit 8846370
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 3 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ class FunctionInfo {
stage: string | null;
inputs: Array<InputInfo>;
outputs: Array<OutputInfo>;
arguments: Array<ArgumentInfo>; // only for non-entry functions
returnType: TypeInfo | null;
resources: Array<VariableInfo>;
startLine: number;
endLine: number;
Expand Down Expand Up @@ -166,6 +168,11 @@ class OverrideInfo {
type: TypeInfo | null;
id: number;
}

class ArgumentInfo {
name: string;
type: TypeInfo;
}
```

## Examples
Expand Down
26 changes: 26 additions & 0 deletions src/wgsl_reflect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,26 @@ export class OverrideInfo {
}
}

export class ArgumentInfo {
name: string;
type: TypeInfo;

constructor(
name: string,
type: TypeInfo,
) {
this.name = name;
this.type = type;
}
}

export class FunctionInfo {
name: string;
stage: string | null = null;
inputs: Array<InputInfo> = [];
outputs: Array<OutputInfo> = [];
arguments: Array<ArgumentInfo> = [];
returnType: TypeInfo | null = null;
resources: Array<VariableInfo> = [];
overrides: Array<OverrideInfo> = [];
startLine: number = -1;
Expand Down Expand Up @@ -494,6 +509,17 @@ export class WgslReflect {
fn.inputs = this._getInputs(node.args);
fn.outputs = this._getOutputs(node.returnType);
this.entry[stage.name].push(fn);
} else {
fn.arguments = node.args.map(
(arg) =>
new ArgumentInfo(
arg.name,
this._getTypeInfo(arg.type, arg.attributes)
)
);
fn.returnType = node.returnType
? this._getTypeInfo(node.returnType, node.attributes)
: null;
}
continue;
}
Expand Down
24 changes: 24 additions & 0 deletions test/tests/test_reflect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1225,4 +1225,28 @@ fn shuffler() { }
test.equals(reflect.entry.vertex[5].resources[0].name, "u1");
test.equals(reflect.entry.vertex[5].resources[1].name, "u2");
});

test("function arguments and return types", function (test) {
const reflect = new WgslReflect(`
fn rotate(v: vec2<f32>, angle: f32) -> vec2f {
let pos = vec2(
(v.x * cos(angle)) - (v.y * sin(angle)),
(v.x * sin(angle)) + (v.y * cos(angle))
);
return pos;
}`);

const args = reflect.functions[0].arguments;
test.equals(args[0].name, "v");
test.equals(args[0].type.name, "vec2");
test.equals(args[0].type.format.name, "f32");
test.equals(args[1].name, "angle");
test.equals(args[1].type.name, "f32");

test.equals(reflect.functions[0].returnType.name, "vec2f");

const logFunc = new WgslReflect("fn log() {}").functions[0];
test.equals(logFunc.arguments.length, 0);
test.equals(logFunc.returnType, null);
});
});
7 changes: 7 additions & 0 deletions types/wgsl_reflect.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,18 @@ export declare class OverrideInfo {
id: number;
constructor(name: string, type: TypeInfo | null, attributes: Array<AST.Attribute> | null, id: number);
}
export declare class ArgumentInfo {
name: string;
type: TypeInfo;
constructor(name: string, type: TypeInfo);
}
export declare class FunctionInfo {
name: string;
stage: string | null;
inputs: Array<InputInfo>;
outputs: Array<OutputInfo>;
arguments: Array<ArgumentInfo>;
returnType: TypeInfo | null;
resources: Array<VariableInfo>;
overrides: Array<OverrideInfo>;
startLine: number;
Expand Down
16 changes: 15 additions & 1 deletion wgsl_reflect.module.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wgsl_reflect.module.js.map

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions wgsl_reflect.node.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wgsl_reflect.node.js.map

Large diffs are not rendered by default.

0 comments on commit 8846370

Please sign in to comment.