Repository navigation
Expand file tree
/
Copy pathFunctionCallNode.js
More file actions
187 lines (121 loc) · 3.89 KB
/
Copy pathFunctionCallNode.js
File metadata and controls
187 lines (121 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import Node from '../core/Node.js';
import { addMethodChaining, nodeArray, nodeObject, nodeObjects, float } from '../tsl/TSLCore.js';
import { error } from '../../utils.js';
/**
* This module represents the call of a {@link FunctionNode}. Developers are usually not confronted
* with this module since they use the predefined TSL syntax `wgslFn` and `glslFn` which encapsulate
* this logic.
*
* @augments Node
*/
class FunctionCallNode extends Node {
static get type() {
return 'FunctionCallNode';
}
/**
* Constructs a new function call node.
*
* @param {?FunctionNode} functionNode - The function node.
* @param {Object<string, Node>} [parameters={}] - The parameters for the function call.
*/
constructor( functionNode = null, parameters = {} ) {
super();
/**
* The function node.
*
* @type {?FunctionNode}
* @default null
*/
this.functionNode = functionNode;
/**
* The parameters of the function call.
*
* @type {Object<string, Node>}
* @default {}
*/
this.parameters = parameters;
}
/**
* Sets the parameters of the function call node.
*
* @param {Object<string, Node>} parameters - The parameters to set.
* @return {FunctionCallNode} A reference to this node.
*/
setParameters( parameters ) {
this.parameters = parameters;
return this;
}
/**
* Returns the parameters of the function call node.
*
* @return {Object<string, Node>} The parameters of this node.
*/
getParameters() {
return this.parameters;
}
/**
* Returns the type of this function call node.
*
* @param {NodeBuilder} builder - The current node builder.
* @returns {string} The type of this node.
*/
generateNodeType( builder ) {
return this.functionNode.getNodeType( builder );
}
/**
* Returns the function node of this function call node.
*
* @param {NodeBuilder} builder - The current node builder.
* @param {string} [name] - The name of the member.
* @returns {string} The type of the member.
*/
getMemberType( builder, name ) {
return this.functionNode.getMemberType( builder, name );
}
generate( builder ) {
const params = [];
const functionNode = this.functionNode;
const inputs = functionNode.getInputs( builder );
const parameters = this.parameters;
const generateInput = ( node, inputNode ) => {
const type = inputNode.type;
const pointer = type === 'pointer';
let output;
if ( pointer ) output = '&' + node.build( builder );
else output = node.build( builder, type );
return output;
};
if ( Array.isArray( parameters ) ) {
if ( parameters.length > inputs.length ) {
error( 'TSL: The number of provided parameters exceeds the expected number of inputs in \'Fn()\'.' );
parameters.length = inputs.length;
} else if ( parameters.length < inputs.length ) {
error( 'TSL: The number of provided parameters is less than the expected number of inputs in \'Fn()\'.' );
while ( parameters.length < inputs.length ) {
parameters.push( float( 0 ) );
}
}
for ( let i = 0; i < parameters.length; i ++ ) {
params.push( generateInput( parameters[ i ], inputs[ i ] ) );
}
} else {
for ( const inputNode of inputs ) {
const node = parameters[ inputNode.name ];
if ( node !== undefined ) {
params.push( generateInput( node, inputNode ) );
} else {
error( `TSL: Input '${ inputNode.name }' not found in \'Fn()\'.` );
params.push( generateInput( float( 0 ), inputNode ) );
}
}
}
const functionName = functionNode.build( builder, 'property' );
return `${ functionName }( ${ params.join( ', ' ) } )`;
}
}
export default FunctionCallNode;
export const call = ( func, ...params ) => {
params = params.length > 1 || ( params[ 0 ] && params[ 0 ].isNode === true ) ? nodeArray( params ) : nodeObjects( params[ 0 ] );
return new FunctionCallNode( nodeObject( func ), params );
};
addMethodChaining( 'call', call );