Skip to content

Commit

Permalink
Merge pull request #18 from gpujs/eugene-#4-async-api
Browse files Browse the repository at this point in the history
While loop test case, fixed no parameter test case
  • Loading branch information
Eugene Cheah authored and Eugene Cheah committed Feb 28, 2016
2 parents 5cfe3ef + 0265c60 commit 8e6badb
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 124 deletions.
64 changes: 32 additions & 32 deletions src/backend/functionNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
/// writeVariables - {[String,...]} List of variables write operations occur
///
var functionNode = (function() {

//
// Constructor
//----------------------------------------------------------------------------------------------------

///
/// Function: functionNode
///
Expand All @@ -38,24 +38,24 @@ var functionNode = (function() {
/// returnType - {String} The return type, assumes "float" if null
///
function functionNode( gpu, functionName, jsFunction, paramTypeArray, returnType ) {

this.gpu = gpu;

//
// Internal vars setup
//
this.calledFunctions = [];
this.initVariables = [];
this.readVariables = [];
this.writeVariables = [];

//
// Missing jsFunction object exception
//
if( jsFunction == null ) {
throw "jsFunction, parameter is null";
}

//
// Setup jsFunction and its string property + validate them
//
Expand All @@ -64,22 +64,22 @@ var functionNode = (function() {
console.error("jsFunction, to string conversion check falied: not a function?", this.jsFunctionString);
throw "jsFunction, to string conversion check falied: not a function?";
}

if( !isFunction(jsFunction) ) {
//throw "jsFunction, is not a valid JS Function";
this.jsFunction = null;
} else {
this.jsFunction = jsFunction;
}

//
// Setup the function name property
//
this.functionName = functionName || (jsFunction && jsFunction.name) || FUNCTION_NAME.exec(this.jsFunctionString)[1];
if( !(this.functionName) ) {
throw "jsFunction, missing name argument or value";
}

//
// Extract parameter name, and its argument types
//
Expand All @@ -98,23 +98,23 @@ var functionNode = (function() {
this.paramType.push("float");
}
}

//
// Return type handling
//
this.returnType = returnType || "float";
}

//
// Utility functions
//----------------------------------------------------------------------------------------------------

///
/// Function: isFunction
///
/// [static] Return TRUE, on a JS function
///
/// This is 'static' function, not a class function (functionNode.prototype)
/// This is 'static' function, not a class function functionNode.isFunction(...)
///
/// Parameters:
/// funcObj - {JS Function} Object to validate if its a function
Expand All @@ -125,13 +125,13 @@ var functionNode = (function() {
function isFunction( funcObj ) {
return typeof(funcObj) === 'function';
}

///
/// Function: validateStringIsFunction
///
/// [static] Return TRUE, on a valid JS function string
///
/// This is 'static' function, not a class function (functionNode.prototype)
/// This is 'static' function, not a class function functionNode.validateStringIsFunction(...)
///
/// Parameters:
/// funcStr - {String} String of JS function to validate
Expand All @@ -145,17 +145,17 @@ var functionNode = (function() {
}
return false;
}

var FUNCTION_NAME = /function ([^(]*)/;
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
var ARGUMENT_NAMES = /([^\s,]+)/g;

///
/// Function: getParamNames
///
/// [static] Return list of parameter names extracted from the JS function string
///
/// This is 'static' function, not a class function (functionNode.prototype)
/// This is 'static' function, not a class function: functionNode.getParamNames(...)
///
/// Parameters:
/// funcStr - {String} String of JS function to validate
Expand All @@ -176,34 +176,34 @@ var functionNode = (function() {
functionNode.isFunction = isFunction;
functionNode.validateStringIsFunction = validateStringIsFunction;
functionNode.getParamNames = getParamNames;

//
// Core function
//----------------------------------------------------------------------------------------------------

///
/// Function: getJSFunction
///
/// Gets and return the stored JS Function.
/// Note: that this internally eval the function, if only the string was provided on construction
///
/// Returns:
/// {JS Function} The function object
/// {JS Function} The function object
///
function getJSFunction() {
if( this.jsFunction ) {
return this.jsFunction;
}

if( this.jsFunctionString ) {
this.jsFunction = eval( this.jsFunctionString );
return this.jsFunction;
}

throw "Missin jsFunction, and jsFunctionString parameter";
}
functionNode.prototype.getJSFunction = getJSFunction;

///
/// Function: getJS_AST
///
Expand All @@ -221,25 +221,25 @@ var functionNode = (function() {
if( this.jsFunctionAST ) {
return this.jsFunctionAST;
}

inParser = inParser || parser;
if( inParser == null ) {
throw "Missing JS to AST parser";
}

var prasedObj = parser.parse( "var "+this.functionName+" = "+this.jsFunctionString+";" );
if( prasedObj === null ) {
throw "Failed to parse JS code via JISON";
}

// take out the function object, outside the var declarations
var funcAST = prasedObj.body[0].declarations[0].init;
this.jsFunctionAST = funcAST;

return funcAST;
}
functionNode.prototype.getJS_AST = getJS_AST;

///
/// Function: getWebglString
///
Expand All @@ -252,11 +252,11 @@ var functionNode = (function() {
if( this.webglFunctionString ) {
return this.webglFunctionString;
}

return this.webglFunctionString = functionNode_webgl(this);
}
functionNode.prototype.getWebglFunctionString = getWebglFunctionString;

///
/// Function: setWebglString
///
Expand All @@ -269,6 +269,6 @@ var functionNode = (function() {
this.webglFunctionString = shaderCode;
}
functionNode.prototype.setWebglFunctionString = setWebglFunctionString;

return functionNode;
})();
Loading

0 comments on commit 8e6badb

Please sign in to comment.