diff --git a/src/js-regex.js b/src/js-regex.js new file mode 100644 index 00000000..a66801df --- /dev/null +++ b/src/js-regex.js @@ -0,0 +1,139 @@ +var GPU_regex = (function() { + String.prototype.replaceAll = function (find, replace) { + var str = this; + return str.replace(new RegExp(find, 'g'), replace); + }; + + webCLGL = new WebCLGL(); + + function clone(obj) { + if(obj === null || typeof(obj) !== 'object' || 'isActiveClone' in obj) + return obj; + + var temp = obj.constructor(); // changed + + for(var key in obj) { + if(Object.prototype.hasOwnProperty.call(obj, key)) { + obj['isActiveClone'] = null; + temp[key] = clone(obj[key]); + delete obj['isActiveClone']; + } + } + + return temp; + } + + var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; + var ARGUMENT_NAMES = /([^\s,]+)/g; + function getParamNames(func) { + var fnStr = func.toString().replace(STRIP_COMMENTS, ''); + var result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES); + if(result === null) + result = []; + return result; + } + + /// Does simple validation of the provided function string if it is a function + /// this is a basic sanity testing before Jison conversion + /// + /// @param funcStr the input function string + /// + /// @returns boolean + function validateStringIsFunction( funcStr ) { + if( funcStr != null ) { + return (funcStr.slice(0, "function".length).toLowerCase() == "function") + } + return false;; + } + + /// Does the core conversion of a basic Javascript function into a webclgl + /// and returns a callable function if valid + /// + /// @param inputFunction The calling to perform the conversion + /// @param _threadDim The thread dim array configuration + /// @param _blockDim The block dim array configuration + /// @param paramObj The parameter object + /// + /// @returns callable function if converted, else returns null + function regex( inputFunction, _threadDim, _blockDim ) { + var threadDim = clone(_threadDim); + var blockDim = clone(_blockDim); + + while (threadDim.length < 3) { + threadDim.push(1); + } + + while (blockDim.length < 3) { + blockDim.push(1); + } + + var globalDim = [ + threadDim[0] * blockDim[0], + threadDim[1] * blockDim[1], + threadDim[2] * blockDim[2] + ]; + + var totalSize = globalDim[0] * globalDim[1] * globalDim[2]; + + var funcStr = inputFunction.toString(); + var funcBody = funcStr.toString().match(/function[^{]+\{([\s\S]*)\}$/)[1]; + + funcBody = funcBody.replaceAll('var ', 'float '); + funcBody = funcBody.replaceAll('this.thread.x', '_x_'); + funcBody = funcBody.replaceAll('return ', 'out_float = '); + + var argNames = getParamNames(inputFunction); + + var shaderCode = 'void main('; + for (var i=0; i + + + + GPU.JS : Basic Sum GPU AB + + + + + + + + + + + + + + + + + + + + +
+
+ + + + diff --git a/test/src/basic_return.js b/test/src/basic_return.js index 25525f41..686b8b39 100644 --- a/test/src/basic_return.js +++ b/test/src/basic_return.js @@ -12,7 +12,11 @@ function basic_return( assert, mode ) { } QUnit.test( "basic_return (string)", function( assert ) { - assert.equal( GPU._jsStrToWebclglStr( "function() { return 42.0; }" ), "", "Basic return string conversion" ); + assert.equal( + GPU._jsStrToWebclglStr( "function() { return 42.0; }" ), + "void main() { out_float = 42.0; return; }", + "Basic return string conversion" + ); }); QUnit.test( "basic_return (auto)", function( assert ) { diff --git a/test/src/basic_sum_AB.js b/test/src/basic_sum_AB.js index f63ae322..9cae539e 100644 --- a/test/src/basic_sum_AB.js +++ b/test/src/basic_sum_AB.js @@ -23,3 +23,12 @@ QUnit.test( "basic_sum_AB (GPU)", function( assert ) { QUnit.test( "basic_sum_AB (CPU)", function( assert ) { basic_sum_AB_test(assert, "cpu"); }); + +/* +// EXAMPLE GLSL +void main(float* a, float* b) { + vec2 _x_ = get_global_id(); + float ret = a[_x_] + b[_x_]; + out_float = ret; +} + */