@@ -31,11 +31,11 @@ var functionNode = (function() {
3131 /// [Constructor] Builds the function with the given JS function, and argument type array.
3232 ///
3333 /// Parameters:
34- /// gpu - {GPU} The GPU instance
35- /// functionName - {String} Function name to assume, if its null, it attempts to extract from the function
36- /// jsFunction - {JS Function} JS Function to do conversion
37- /// paramTypeArray - {[String,...]} Parameter type array, assumes all parameters are "float" if null
38- /// returnType - {String} The return type, assumes "float" if null
34+ /// gpu - {GPU} The GPU instance
35+ /// functionName - {String} Function name to assume, if its null, it attempts to extract from the function
36+ /// jsFunction - {JS Function / String } JS Function to do conversion
37+ /// paramTypeArray - {[String,...]} Parameter type array, assumes all parameters are "float" if null
38+ /// returnType - {String} The return type, assumes "float" if null
3939 ///
4040 function functionNode ( gpu , functionName , jsFunction , paramTypeArray , returnType ) {
4141
@@ -50,22 +50,32 @@ var functionNode = (function() {
5050 this . writeVariables = [ ] ;
5151
5252 //
53- // Setup jsFunction and its string property + validate them
53+ // Missing jsFunction object exception
5454 //
55- this . jsFunction = jsFunction ;
56- if ( ! isFunction ( this . jsFunction ) ) {
57- throw "jsFunction, is not a valid JS Function" ;
55+ if ( jsFunction == null ) {
56+ throw "jsFunction, parameter is null" ;
5857 }
5958
59+ //
60+ // Setup jsFunction and its string property + validate them
61+ //
6062 this . jsFunctionString = jsFunction . toString ( ) ;
6163 if ( ! validateStringIsFunction ( this . jsFunctionString ) ) {
62- throw "jsFunction, to string conversion falied" ;
64+ console . error ( "jsFunction, to string conversion check falied: not a function?" , this . jsFunctionString ) ;
65+ throw "jsFunction, to string conversion check falied: not a function?" ;
66+ }
67+
68+ if ( ! isFunction ( jsFunction ) ) {
69+ //throw "jsFunction, is not a valid JS Function";
70+ this . jsFunction = null ;
71+ } else {
72+ this . jsFunction = jsFunction ;
6373 }
6474
6575 //
6676 // Setup the function name property
6777 //
68- this . functionName = functionName || jsFunction . name ;
78+ this . functionName = functionName || ( jsFunction && jsFunction . name ) || FUNCTION_NAME . exec ( this . jsFunctionString ) [ 1 ] ;
6979 if ( ! ( this . functionName ) ) {
7080 throw "jsFunction, missing name argument or value" ;
7181 }
@@ -136,6 +146,7 @@ var functionNode = (function() {
136146 return false ;
137147 }
138148
149+ var FUNCTION_NAME = / f u n c t i o n ( [ ^ ( ] * ) / ;
139150 var STRIP_COMMENTS = / ( ( \/ \/ .* $ ) | ( \/ \* [ \s \S ] * ?\* \/ ) ) / mg;
140151 var ARGUMENT_NAMES = / ( [ ^ \s , ] + ) / g;
141152
@@ -170,6 +181,29 @@ var functionNode = (function() {
170181 // Core function
171182 //----------------------------------------------------------------------------------------------------
172183
184+ ///
185+ /// Function: getJSFunction
186+ ///
187+ /// Gets and return the stored JS Function.
188+ /// Note: that this internally eval the function, if only the string was provided on construction
189+ ///
190+ /// Returns:
191+ /// {JS Function} The function object
192+ ///
193+ function getJSFunction ( ) {
194+ if ( this . jsFunction ) {
195+ return this . jsFunction ;
196+ }
197+
198+ if ( this . jsFunctionString ) {
199+ this . jsFunction = eval ( this . jsFunctionString ) ;
200+ return this . jsFunction ;
201+ }
202+
203+ throw "Missin jsFunction, and jsFunctionString parameter" ;
204+ }
205+ functionNode . prototype . getJSFunction = getJSFunction ;
206+
173207 ///
174208 /// Function: getJS_AST
175209 ///
0 commit comments