MCPcopy
hub / github.com/BrainJS/brain.js / BaseFunctionNode

Function BaseFunctionNode

browser.min.js:287–287  ·  view source on GitHub ↗
(t,e,n)

Source from the content-addressed store, hash-verified

285
286},{}],59:[function(require,module,exports){
287"use strict";function _classCallCheck(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},_createClass=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),utils=require("../core/utils"),acorn=require("acorn");module.exports=function(){function BaseFunctionNode(t,e,n){_classCallCheck(this,BaseFunctionNode),this.calledFunctions=[],this.calledFunctionsArguments={},this.builder=null,this.isRootKernel=!1,this.isSubKernel=!1,this.parent=null,this.debug=null,this.prototypeOnly=null,this.constants=null,this.output=null,this.declarations={},this.states=[],this.fixIntegerDivisionAccuracy=null;var r=void 0,s=void 0;if(n&&(n.hasOwnProperty("debug")&&(this.debug=n.debug),n.hasOwnProperty("prototypeOnly")&&(this.prototypeOnly=n.prototypeOnly),n.hasOwnProperty("constants")&&(this.constants=n.constants),n.hasOwnProperty("output")&&(this.output=n.output),n.hasOwnProperty("loopMaxIterations")&&(this.loopMaxIterations=n.loopMaxIterations),n.hasOwnProperty("paramTypes")&&(this.paramTypes=r=n.paramTypes),n.hasOwnProperty("constantTypes")?this.constantTypes=n.constantTypes:this.constantTypes={},n.hasOwnProperty("returnType")&&(s=n.returnType),n.hasOwnProperty("fixIntegerDivisionAccuracy")&&(this.fixIntegerDivisionAccuracy=n.fixIntegerDivisionAccuracy)),!e)throw"jsFunction, parameter is missing";if(this.jsFunctionString=e.toString(),!utils.isFunctionString(this.jsFunctionString))throw console.error("jsFunction, to string conversion check failed: not a function?",this.jsFunctionString),"jsFunction, to string conversion check failed: not a function?";if(utils.isFunction(e)?this.jsFunction=e:this.jsFunction=null,this.functionName=t||e&&e.name||utils.getFunctionNameFromString(this.jsFunctionString),!this.functionName)throw"jsFunction, missing name argument or value";if(this.paramNames=utils.getParamNamesFromString(this.jsFunctionString),r){if(Array.isArray(r)){if(r.length!==this.paramNames.length)throw"Invalid argument type array length, against function length -> ("+r.length+","+this.paramNames.length+")";this.paramTypes=r}else if("object"===(void 0===r?"undefined":_typeof(r))){var i=Object.keys(r);if(r.hasOwnProperty("returns")&&(this.returnType=r.returns,i.splice(i.indexOf("returns"),1)),i.length>0&&i.length!==this.paramNames.length)throw"Invalid argument type array length, against function length -> ("+i.length+","+this.paramNames.length+")";this.paramTypes=this.paramNames.map(function(t){return r.hasOwnProperty(t)?r[t]:"Number"})}}else this.paramTypes=[];this.returnType||(this.returnType=s||"Number")}return _createClass(BaseFunctionNode,[{key:"isIdentifierConstant",value:function(t){return!!this.constants&&this.constants.hasOwnProperty(t)}},{key:"isInput",value:function(t){return"Input"===this.paramTypes[this.paramNames.indexOf(t)]}},{key:"setBuilder",value:function(t){return this.builder=t,this}},{key:"pushState",value:function(t){this.states.push(t)}},{key:"popState",value:function(t){if(this.state!==t)throw new Error("Cannot popState "+t+" when in "+this.state);this.states.pop()}},{key:"isState",value:function(t){return this.state===t}},{key:"getJsFunction",value:function getJsFunction(){if(this.jsFunction)return this.jsFunction;if(this.jsFunctionString)return this.jsFunction=eval(this.jsFunctionString),this.jsFunction;throw"Missing jsFunction, and jsFunctionString parameter"}},{key:"astMemberExpressionUnroll",value:function(t){if("Identifier"===t.type)return t.name;if("ThisExpression"===t.type)return"this";if("MemberExpression"===t.type&&t.object&&t.property)return t.object.hasOwnProperty("name")&&"_"===t.object.name[0]?this.astMemberExpressionUnroll(t.property):this.astMemberExpressionUnroll(t.object)+"."+this.astMemberExpressionUnroll(t.property);if(t.hasOwnProperty("expressions")){var e=t.expressions[0];if("Literal"===e.type&&0===e.value&&2===t.expressions.length)return this.astMemberExpressionUnroll(t.expressions[1])}throw this.astErrorOutput("Unknown CallExpression_unroll",t)}},{key:"getJsAST",value:function(t){if(this.jsFunctionAST)return this.jsFunctionAST;if(null===(t=t||acorn))throw"Missing JS to AST parser";var e=t.parse("var "+this.functionName+" = "+this.jsFunctionString+";",{locations:!0});if(null===e)throw"Failed to parse JS code";var n=e.body[0].declarations[0].init;return this.jsFunctionAST=n,n}},{key:"getFunctionString",value:function(){return this.generate(),this.functionString}},{key:"setFunctionString",value:function(t){this.functionString=t}},{key:"getParamType",value:function(t){var e=this.paramNames.indexOf(t);if(-1===e)return this.declarations.hasOwnProperty(t)?this.declarations[t]:"Number";if(this.parent){if(this.paramTypes[e])return this.paramTypes[e];for(var n=this.parent.calledFunctionsArguments[this.functionName],r=0;r<n.length;r++){var s=n[r];if(null!==s[e])return this.paramTypes[e]=s[e].type}}else if(this.paramTypes[e])return this.paramTypes[e];return"Number"}},{key:"getConstantType",value:function(t){return this.constantTypes[t]?this.constantTypes[t]:null}},{key:"getUserParamName",value:function(t){var e=this.paramNames.indexOf(t);if(-1===e)return null;if(!this.parent||!this.isSubKernel)return null;for(var n=this.parent.calledFunctionsArguments[this.functionName],r=0;r<n.length;r++){var s=n[r],i=s[e];if(null!==i&&"Integer"!==i.type)return i.name}return null}},{key:"generate",value:function(t){throw new Error("generate not defined on BaseFunctionNode")}},{key:"astGeneric",value:function(t,e){if(null===t)throw this.astErrorOutput("NULL ast",t);if(Array.isArray(t)){for(var n=0;n<t.length;n++)this.astGeneric(t[n],e);return e}switch(t.type){case"FunctionDeclaration":return this.astFunctionDeclaration(t,e);case"FunctionExpression":return this.astFunctionExpression(t,e);case"ReturnStatement":return this.astReturnStatement(t,e);case"Literal":return this.astLiteral(t,e);case"BinaryExpression":return this.astBinaryExpression(t,e);case"Identifier":return this.astIdentifierExpression(t,e);case"AssignmentExpression":return this.astAssignmentExpression(t,e);case"ExpressionStatement":return this.astExpressionStatement(t,e);case"EmptyStatement":return this.astEmptyStatement(t,e);case"BlockStatement":return this.astBlockStatement(t,e);case"IfStatement":return this.astIfStatement(t,e);case"BreakStatement":return this.astBreakStatement(t,e);case"ContinueStatement":return this.astContinueStatement(t,e);case"ForStatement":return this.astForStatement(t,e);case"WhileStatement":return this.astWhileStatement(t,e);case"DoWhileStatement":return this.astDoWhileStatement(t,e);case"VariableDeclaration":return this.astVariableDeclaration(t,e);case"VariableDeclarator":return this.astVariableDeclarator(t,e);case"ThisExpression":return this.astThisExpression(t,e);case"SequenceExpression":return this.astSequenceExpression(t,e);case"UnaryExpression":return this.astUnaryExpression(t,e);case"UpdateExpression":return this.astUpdateExpression(t,e);case"LogicalExpression":return this.astLogicalExpression(t,e);case"MemberExpression":return this.astMemberExpression(t,e);case"CallExpression":return this.astCallExpression(t,e);case"ArrayExpression":return this.astArrayExpression(t,e);case"DebuggerStatement":return this.astDebuggerStatement(t,e)}throw this.astErrorOutput("Unknown ast type : "+t.type,t)}},{key:"astErrorOutput",value:function(t,e){return console.error(utils.getAstString(this.jsFunctionString,e)),console.error(t,e,this),t}},{key:"astDebuggerStatement",value:function(t,e){return e}},{key:"astFunctionDeclaration",value:function(t,e){return e}},{key:"astFunctionExpression",value:function(t,e){return e}},{key:"astReturnStatement",value:function(t,e){return e}},{key:"astLiteral",value:function(t,e){return e}},{key:"astBinaryExpression",value:function(t,e){return e}},{key:"astIdentifierExpression",value:function(t,e){return e}},{key:"astAssignmentExpression",value:function(t,e){return e}},{key:"astExpressionStatement",value:function(t,e){return e}},{key:"astEmptyStatement",value:function(t,e){return e}},{key:"astBlockStatement",value:function(t,e){return e}},{key:"astIfStatement",value:function(t,e){return e}},{key:"astBreakStatement",value:function(t,e){return e}},{key:"astContinueStatement",value:function(t,e){return e}},{key:"astForStatement",value:function(t,e){return e}},{key:"astWhileStatement",value:function(t,e){return e}},{key:"astDoWhileStatement",value:function(t,e){return e}},{key:"astVariableDeclaration",value:function(t,e){return e}},{key:"astVariableDeclarator",value:function(t,e){return e}},{key:"astThisExpression",value:function(t,e){return e}},{key:"astSequenceExpression",value:function(t,e){return e}},{key:"astUnaryExpression",value:function(t,e){return e}},{key:"astUpdateExpression",value:function(t,e){return e}},{key:"astLogicalExpression",value:function(t,e){return e}},{key:"astMemberExpression",value:function(t,e){return e}},{key:"astCallExpression",value:function(t,e){return e}},{key:"astArrayExpression",value:function(t,e){return e}},{key:"pushParameter",value:function(t,e){this.isState("in-get-call-parameters")?t.push("int("+e+")"):t.push(e)}},{key:"state",get:function(){return this.states[this.states.length-1]}}]),BaseFunctionNode}();
288
289},{"../core/utils":84,"acorn":86}],60:[function(require,module,exports){
290"use strict";function _classCallCheck(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function t(t,e){for(var n=0;n<e.length;n++){var s=e[n];s.enumerable=s.enumerable||!1,s.configurable=!0,"value"in s&&(s.writable=!0),Object.defineProperty(t,s.key,s)}}return function(e,n,s){return n&&t(e.prototype,n),s&&t(e,s),e}}(),utils=require("../core/utils"),Input=require("../core/input");module.exports=function(){function t(e,n){_classCallCheck(this,t),this.paramNames=utils.getParamNamesFromString(e),this.fnString=e,this.output=null,this.debug=!1,this.graphical=!1,this.loopMaxIterations=0,this.constants=null,this.wraparound=null,this.hardcodeConstants=null,this.outputToTexture=null,this.outputImmutable=null,this.texSize=null,this._canvas=null,this._webGl=null,this.threadDim=null,this.floatTextures=null,this.floatOutput=null,this.floatOutputForce=null,this.addFunction=null,this.functions=null,this.nativeFunctions=null,this.subKernels=null,this.subKernelProperties=null,this.subKernelNames=null,this.subKernelOutputVariableNames=null,this.functionBuilder=null,this.paramTypes=null,this.paramSizes=null,this.constantTypes=null,this.fixIntegerDivisionAccuracy=null;for(var s in n)n.hasOwnProperty(s)&&this.hasOwnProperty(s)&&(this[s]=n[s]);n.hasOwnProperty("canvas")&&(this._canvas=n.canvas),n.hasOwnProperty("webGl")&&(this._webGl=n.webGl),n.hasOwnProperty("output")&&this.setOutput(n.output),this._canvas||(this._canvas=utils.initCanvas())}return _createClass(t,[{key:"build",value:function(){throw new Error('"build" not defined on Base')}},{key:"setupParams",value:function(t){this.paramTypes=[],this.paramSizes=[];for(var e=0;e<t.length;e++){var n=t[e];this.paramTypes.push(utils.getArgumentType(n)),this.paramSizes.push(n.constructor===Input?n.size:null)}}},{key:"setupConstants",value:function(){if(this.constantTypes={},this.constants)for(var t in this.constants)this.constantTypes[t]=utils.getArgumentType(this.constants[t])}},{key:"setFunctions",value:function(t){return this.functions=t,this}},{key:"setOutput",value:function(t){return t.hasOwnProperty("x")?t.hasOwnProperty("y")?t.hasOwnProperty("z")?this.output=[t.x,t.y,t.z]:this.output=[t.x,t.y]:this.output=[t.x]:this.output=t,this}},{key:"setDebug",value:function(t){return this.debug=t,this}},{key:"setGraphical",value:function(t){return this.graphical=t,this}},{key:"setLoopMaxIterations",value:function(t){return this.loopMaxIterations=t,this}},{key:"setFixIntegerDivisionAccuracy",value:function(t){return this.fixIntegerDivisionAccuracy=t,this}},{key:"setConstants",value:function(t){return this.constants=t,this}},{key:"setWraparound",value:function(t){return console.warn("Wraparound mode is not supported and undocumented."),this.wraparound=t,this}},{key:"setHardcodeConstants",value:function(t){return this.hardcodeConstants=t,this}},{key:"setOutputToTexture",value:function(t){return this.outputToTexture=t,this}},{key:"setOutputImmutable",value:function(t){return this.outputImmutable=t,this}},{key:"setFloatTextures",value:function(t){return this.floatTextures=t,this}},{key:"setFloatOutput",value:function(t){return this.floatOutput=t,this}},{key:"setFloatOutputForce",value:function(t){return this.floatOutputForce=t,this}},{key:"setCanvas",value:function(t){return this._canvas=t,this}},{key:"setWebGl",value:function(t){return this._webGl=t,this}},{key:"getCanvas",value:function(){return this._canvas}},{key:"getWebGl",value:function(){return this._webGl}},{key:"validateOptions",value:function(){throw new Error("validateOptions not defined")}},{key:"exec",value:function(){return this.execute.apply(this,arguments)}},{key:"execute",value:function(){var t=this,e=1===arguments.length?[arguments[0]]:Array.apply(null,arguments);return utils.newPromise(function(n,s){try{n(t.run.apply(t,e))}catch(t){s(t)}})}},{key:"addSubKernel",value:function(t){return null===this.subKernels&&(this.subKernels=[],this.subKernelNames=[]),this.subKernels.push(t),this.subKernelNames.push(utils.getFunctionNameFromString(t)),this}},{key:"addSubKernelProperty",value:function(t,e){if(null===this.subKernelProperties&&(this.subKernelProperties={},this.subKernelNames=[]),this.subKernelProperties.hasOwnProperty(t))throw new Error("cannot add sub kernel "+t+", already defined");return this.subKernelProperties[t]=e,this.subKernelNames.push(utils.getFunctionNameFromString(e)),this}},{key:"addNativeFunction",value:function(t,e){this.functionBuilder.addNativeFunction(t,e)}},{key:"destroy",value:function(){}}]),t}();

Callers

nothing calls this directly

Calls 2

toStringMethod · 0.80
_classCallCheckFunction · 0.70

Tested by

no test coverage detected