FunctionCallNode.js 1.6 KB

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