diff --git a/src/backend/functionNode.js b/src/backend/functionNode.js index 0b2413ff..2ab2516b 100644 --- a/src/backend/functionNode.js +++ b/src/backend/functionNode.js @@ -4,24 +4,89 @@ /// Represents a single function, inside JS, webGL, or openGL. /// This handles the raw state, converted state, etc. /// +/// @property {JS Function} jsFunction The JS Function the node represents +/// @property {String} jsFunctionString jsFunction.toString() +/// @property {[String,...]} paramNames Parameter names of the function +/// @property {[String,...]} paramType Shader land parameter type assumption +/// var functionNode = (function() { + // + // Constructor + //---------------------------------------------------------------------------------------------------- + /// - /// @function: functionNode + /// @function functionNode /// /// [Constructor] Builds the function with the given JS function, and argument type array. /// If argument types are not provided, they are assumed to be "float*" /// + /// @param {String} Function name to assume, if its null, it attempts to extract from the function /// @param {JS Function} JS Function to do conversion - /// @param {[string,...]} Parameter type array, assumes "float*" if not given + /// @param {[String,...]} Parameter type array, assumes "float*" if not given /// - function functionNode( jsFunction, argumentTypeArray ) { + function functionNode( functionName, jsFunction, paramTypeArray ) { + + // + // Setup jsFunction and its string property + validate them + // this.jsFunction = jsFunction; + if( !isFunction(this.jsFunction) ) { + throw "jsFunction, is not a valid JS Function"; + } + this.jsFunctionString = jsFunction.toString(); + if( !validateStringIsFunction(this.jsFunctionString) ) { + throw "jsFunction, to string conversion falied"; + } + this.paramNames = getParamNames(this.jsFunctionString); + + // + // Setup the function name property + // + this.functionName = functionName || jsFunction.name; + + // + // Extract parameter name, and its argument types + // + if( paramTypeArray != null ) { + if( paramTypeArray.length != this.paramNames.length ) { + throw "Invalid argument type array length, against function length -> ("+ + paramTypeArray.length+","+ + this.paramNames.length+ + ")"; + } + this.paramType = paramTypeArray; + } else { + this.paramType = []; + for(var a=0; a