FunctionCallNode.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { TempNode } from './TempNode.js';
  5. function FunctionCallNode( func, inputs ) {
  6. TempNode.call( this );
  7. this.setFunction( func, inputs );
  8. };
  9. FunctionCallNode.prototype = Object.create( TempNode.prototype );
  10. FunctionCallNode.prototype.constructor = FunctionCallNode;
  11. FunctionCallNode.prototype.nodeType = "FunctionCall";
  12. FunctionCallNode.prototype.setFunction = function ( func, inputs ) {
  13. this.value = func;
  14. this.inputs = inputs || [];
  15. };
  16. FunctionCallNode.prototype.getFunction = function () {
  17. return this.value;
  18. };
  19. FunctionCallNode.prototype.getType = function ( builder ) {
  20. return this.value.getType( builder );
  21. };
  22. FunctionCallNode.prototype.generate = function ( builder, output ) {
  23. var type = this.getType( builder ),
  24. func = this.value;
  25. var code = func.build( builder, output ) + '( ',
  26. params = [];
  27. for ( var i = 0; i < func.inputs.length; i ++ ) {
  28. var inpt = func.inputs[ i ],
  29. param = this.inputs[ i ] || this.inputs[ inpt.name ];
  30. params.push( param.build( builder, builder.getTypeByFormat( inpt.type ) ) );
  31. }
  32. code += params.join( ', ' ) + ' )';
  33. return builder.format( code, type, output );
  34. };
  35. FunctionCallNode.prototype.copy = function ( source ) {
  36. TempNode.prototype.copy.call( this, source );
  37. for ( var prop in source.inputs ) {
  38. this.inputs[ prop ] = source.inputs[ prop ];
  39. }
  40. this.value = source.value;
  41. };
  42. FunctionCallNode.prototype.toJSON = function ( meta ) {
  43. var data = this.getJSONNode( meta );
  44. if ( ! data ) {
  45. var func = this.value;
  46. data = this.createJSONNode( meta );
  47. data.value = this.value.toJSON( meta ).uuid;
  48. if ( func.inputs.length ) {
  49. data.inputs = {};
  50. for ( var i = 0; i < func.inputs.length; i ++ ) {
  51. var inpt = func.inputs[ i ],
  52. node = this.inputs[ i ] || this.inputs[ inpt.name ];
  53. data.inputs[ inpt.name ] = node.toJSON( meta ).uuid;
  54. }
  55. }
  56. }
  57. return data;
  58. };
  59. export { FunctionCallNode };