FunctionCallNode.js 1.9 KB

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