JoinNode.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { addNodeClass } from '../core/Node.js';
  2. import TempNode from '../core/TempNode.js';
  3. class JoinNode extends TempNode {
  4. constructor( nodes = [], nodeType = null ) {
  5. super( nodeType );
  6. this.nodes = nodes;
  7. }
  8. getNodeType( builder ) {
  9. if ( this.nodeType !== null ) {
  10. return builder.getVectorType( this.nodeType );
  11. }
  12. return builder.getTypeFromLength( this.nodes.reduce( ( count, cur ) => count + builder.getTypeLength( cur.getNodeType( builder ) ), 0 ) );
  13. }
  14. generate( builder, output ) {
  15. const type = this.getNodeType( builder );
  16. const nodes = this.nodes;
  17. const primitiveType = builder.getComponentType( type );
  18. const snippetValues = [];
  19. for ( const input of nodes ) {
  20. let inputSnippet = input.build( builder );
  21. const inputPrimitiveType = builder.getComponentType( input.getNodeType( builder ) );
  22. if ( inputPrimitiveType !== primitiveType ) {
  23. inputSnippet = builder.format( inputSnippet, inputPrimitiveType, primitiveType );
  24. }
  25. snippetValues.push( inputSnippet );
  26. }
  27. const snippet = `${ builder.getType( type ) }( ${ snippetValues.join( ', ' ) } )`;
  28. return builder.format( snippet, type, output );
  29. }
  30. }
  31. export default JoinNode;
  32. addNodeClass( 'JoinNode', JoinNode );