MCPcopy Index your code
hub / github.com/gpujs/gpu.js / astFunction

Method astFunction

src/backend/web-gl/function-node.js:50–123  ·  view source on GitHub ↗

* @desc Parses the abstract syntax tree for to its *named function* * @param {Object} ast - the AST object to parse * @param {Array} retArr - return array string * @returns {Array} the append retArr

(ast, retArr)

Source from the content-addressed store, hash-verified

48 * @returns {Array} the append retArr
49 */
50 astFunction(ast, retArr) {
51 // Setup function return type and name
52 if (this.isRootKernel) {
53 retArr.push('void');
54 } else {
55 // looking up return type, this is a little expensive, and can be avoided if returnType is set
56 if (!this.returnType) {
57 const lastReturn = this.findLastReturn();
58 if (lastReturn) {
59 this.returnType = this.getType(ast.body);
60 if (this.returnType === 'LiteralInteger') {
61 this.returnType = 'Number';
62 }
63 }
64 }
65
66 const { returnType } = this;
67 if (!returnType) {
68 retArr.push('void');
69 } else {
70 const type = typeMap[returnType];
71 if (!type) {
72 throw new Error(`unknown type ${returnType}`);
73 }
74 retArr.push(type);
75 }
76 }
77 retArr.push(' ');
78 retArr.push(this.name);
79 retArr.push('(');
80
81 if (!this.isRootKernel) {
82 // Arguments handling
83 for (let i = 0; i < this.argumentNames.length; ++i) {
84 const argumentName = this.argumentNames[i];
85
86 if (i > 0) {
87 retArr.push(', ');
88 }
89 let argumentType = this.argumentTypes[this.argumentNames.indexOf(argumentName)];
90 // The type is too loose ended, here we decide to solidify a type, lets go with float
91 if (!argumentType) {
92 throw this.astErrorOutput(`Unknown argument ${argumentName} type`, ast);
93 }
94 if (argumentType === 'LiteralInteger') {
95 this.argumentTypes[i] = argumentType = 'Number';
96 }
97 const type = typeMap[argumentType];
98 if (!type) {
99 throw this.astErrorOutput('Unexpected expression', ast);
100 }
101 const name = utils.sanitizeName(argumentName);
102 if (type === 'sampler2D' || type === 'sampler2DArray') {
103 // mash needed arguments together, since now we have end to end inference
104 retArr.push(`${type} user_${name},ivec2 user_${name}Size,ivec3 user_${name}Dim`);
105 } else {
106 retArr.push(`${type} user_${name}`);
107 }

Callers

nothing calls this directly

Calls 4

findLastReturnMethod · 0.80
getTypeMethod · 0.80
astErrorOutputMethod · 0.80
astGenericMethod · 0.80

Tested by

no test coverage detected