|
8 | 8 | /// Properties: |
9 | 9 | /// nodeMap - {Object} Object map, where nodeMap[function] = functionNode; |
10 | 10 | /// |
11 | | -function functionBuilder() { |
12 | | - this.nodeMap = {}; |
13 | | -} |
| 11 | +var functionBuilder = (function() { |
| 12 | + |
| 13 | + /// |
| 14 | + /// Function: functionBuilder |
| 15 | + /// |
| 16 | + /// [Constructor] Blank constructor, which initializes the properties |
| 17 | + /// |
| 18 | + function functionBuilder() { |
| 19 | + this.nodeMap = {}; |
| 20 | + } |
| 21 | + |
| 22 | + /// |
| 23 | + /// Function: addFunction |
| 24 | + /// |
| 25 | + /// Creates the functionNode, and add it to the nodeMap |
| 26 | + /// |
| 27 | + /// Parameters: |
| 28 | + /// functionName - {String} Function name to assume, if its null, it attempts to extract from the function |
| 29 | + /// jsFunction - {JS Function} JS Function to do conversion |
| 30 | + /// paramTypeArray - {[String,...]} Parameter type array, assumes all parameters are "float" if null |
| 31 | + /// returnType - {String} The return type, assumes "float" if null |
| 32 | + /// |
| 33 | + function addFunction( functionName, jsFunction, paramTypeArray, returnType ) { |
| 34 | + this.addFunctionNode( new functionNode( functionName, jsFunction, paramTypeArray, returnType ) ); |
| 35 | + } |
| 36 | + functionBuilder.prototype.addFunction = addFunction; |
| 37 | + |
| 38 | + /// |
| 39 | + /// Function: addFunctionNode |
| 40 | + /// |
| 41 | + /// Add the funciton node directly |
| 42 | + /// |
| 43 | + /// Parameters: |
| 44 | + /// inNode - {functionNode} functionNode to add |
| 45 | + /// |
| 46 | + function addFunctionNode( inNode ) { |
| 47 | + this.nodeMap[ inNode.functionName ] = inNode; |
| 48 | + } |
| 49 | + functionBuilder.prototype.addFunctionNode = addFunctionNode; |
| 50 | + |
| 51 | + /// |
| 52 | + /// Function: traceFunctionCalls |
| 53 | + /// |
| 54 | + /// Trace all the depending functions being called, from a single function |
| 55 | + /// |
| 56 | + /// This allow for "uneeded" functions to be automatically optimized out. |
| 57 | + /// |
| 58 | + /// Parameters: |
| 59 | + /// functionName - {String} Function name to trace from, default to "kernel" |
| 60 | + /// retList - {[String,...]} Returning list of function names that is traced. Including itself. |
| 61 | + /// |
| 62 | + /// Returns: |
| 63 | + /// {[String,...]} Returning list of function names that is traced. Including itself. |
| 64 | + function traceFunctionCalls( functionName, retList ) { |
| 65 | + functionName = functionName || "kernel"; |
| 66 | + retList = retList || []; |
| 67 | + |
| 68 | + var fNode = this.nodeMap[functionName]; |
| 69 | + if( fNode ) { |
| 70 | + // Check if function already exists |
| 71 | + if( retList.indexOf(functionName) >= 0 ) { |
| 72 | + // Does nothing if already traced |
| 73 | + } else { |
| 74 | + retList.push(functionName); |
| 75 | + |
| 76 | + fNode.getWebglFunctionString(); //ensure JS trace is done |
| 77 | + for(var i=0; i<fNode.calledFunctions.length; ++i) { |
| 78 | + this.traceFunctionCalls( fNode.calledFunctions[i], retList ); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return retList; |
| 84 | + } |
| 85 | + functionBuilder.prototype.traceFunctionCalls = traceFunctionCalls; |
| 86 | + |
| 87 | + return functionBuilder; |
| 88 | +})(); |
0 commit comments