* @desc Parses the class function JS, and returns its Abstract Syntax Tree object. * This is used internally to convert to shader code * * @param {Object} [inParser] - Parser to use, assumes in scope 'parser' if null or undefined * * @returns {Object} The function AST Object, note tha
(inParser)
| 184 | * @returns {Object} The function AST Object, note that result is cached under this.ast; |
| 185 | */ |
| 186 | getJsAST(inParser) { |
| 187 | if (this.ast) { |
| 188 | return this.ast; |
| 189 | } |
| 190 | if (typeof this.source === 'object') { |
| 191 | this.traceFunctionAST(this.source); |
| 192 | return this.ast = this.source; |
| 193 | } |
| 194 | |
| 195 | inParser = inParser || acorn; |
| 196 | if (inParser === null) { |
| 197 | throw new Error('Missing JS to AST parser'); |
| 198 | } |
| 199 | |
| 200 | const ast = Object.freeze(inParser.parse(`const parser_${ this.name } = ${ this.source };`, { |
| 201 | locations: true |
| 202 | })); |
| 203 | // take out the function object, outside the var declarations |
| 204 | const functionAST = ast.body[0].declarations[0].init; |
| 205 | this.traceFunctionAST(functionAST); |
| 206 | |
| 207 | if (!ast) { |
| 208 | throw new Error('Failed to parse JS code'); |
| 209 | } |
| 210 | |
| 211 | return this.ast = functionAST; |
| 212 | } |
| 213 | |
| 214 | traceFunctionAST(ast) { |
| 215 | const { contexts, declarations, functions, identifiers, functionCalls } = new FunctionTracer(ast); |
no test coverage detected