* @desc Trace all the depending functions being called, from a single function * * This allow for 'unneeded' functions to be automatically optimized out. * Note that the 0-index, is the starting function trace. * * @param {String} functionName - Function name to trace from, default to
(functionName, retList)
| 266 | * @returns {String[]} Returning list of function names that is traced. Including itself. |
| 267 | */ |
| 268 | traceFunctionCalls(functionName, retList) { |
| 269 | functionName = functionName || 'kernel'; |
| 270 | retList = retList || []; |
| 271 | |
| 272 | if (this.nativeFunctionNames.indexOf(functionName) > -1) { |
| 273 | const nativeFunctionIndex = retList.indexOf(functionName); |
| 274 | if (nativeFunctionIndex === -1) { |
| 275 | retList.push(functionName); |
| 276 | } else { |
| 277 | /** |
| 278 | * https://github.com/gpujs/gpu.js/issues/207 |
| 279 | * if dependent function is already in the list, because a function depends on it, and because it has |
| 280 | * already been traced, we know that we must move the dependent function to the end of the the retList. |
| 281 | * */ |
| 282 | const dependantNativeFunctionName = retList.splice(nativeFunctionIndex, 1)[0]; |
| 283 | retList.push(dependantNativeFunctionName); |
| 284 | } |
| 285 | return retList; |
| 286 | } |
| 287 | |
| 288 | const functionNode = this.functionMap[functionName]; |
| 289 | if (functionNode) { |
| 290 | // Check if function already exists |
| 291 | const functionIndex = retList.indexOf(functionName); |
| 292 | if (functionIndex === -1) { |
| 293 | retList.push(functionName); |
| 294 | functionNode.toString(); //ensure JS trace is done |
| 295 | for (let i = 0; i < functionNode.calledFunctions.length; ++i) { |
| 296 | this.traceFunctionCalls(functionNode.calledFunctions[i], retList); |
| 297 | } |
| 298 | } else { |
| 299 | /** |
| 300 | * https://github.com/gpujs/gpu.js/issues/207 |
| 301 | * if dependent function is already in the list, because a function depends on it, and because it has |
| 302 | * already been traced, we know that we must move the dependent function to the end of the the retList. |
| 303 | * */ |
| 304 | const dependantFunctionName = retList.splice(functionIndex, 1)[0]; |
| 305 | retList.push(dependantFunctionName); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | return retList; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * @desc Return the string for a function |
no test coverage detected