forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction-node-base.js
More file actions
615 lines (571 loc) · 16 KB
/
function-node-base.js
File metadata and controls
615 lines (571 loc) · 16 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
'use strict';
const utils = require('../core/utils');
const acorn = require('acorn');
module.exports = class BaseFunctionNode {
/**
* @constructor FunctionNodeBase
*
* @desc Represents a single function, inside JS, webGL, or openGL.
*
* <p>This handles all the raw state, converted state, etc. Of a single function.</p>
*
* @prop {String} functionName - Name of the function
* @prop {Function} jsFunction - The JS Function the node represents
* @prop {String} jsFunctionString - jsFunction.toString()
* @prop {String[]} paramNames - Parameter names of the function
* @prop {String[]} paramTypes - Shader land parameters type assumption
* @prop {Boolean} isRootKernel - Special indicator, for kernel function
* @prop {String} webglFunctionString - webgl converted function string
* @prop {String} openglFunctionString - opengl converted function string
* @prop {String[]} calledFunctions - List of all the functions called
* @param {String} functionName - Function name to assume, if its null, it attempts to extract from the function
* @param {Function|String} jsFunction - JS Function to do conversion
* @param {Object} options
*
*/
constructor(functionName, jsFunction, options) {
this.calledFunctions = [];
this.calledFunctionsArguments = {};
this.addFunction = null;
this.isRootKernel = false;
this.isSubKernel = false;
this.parent = null;
this.debug = null;
this.prototypeOnly = null;
this.constants = null;
this.output = null;
this.declarations = {};
this.states = [];
this.fixIntegerDivisionAccuracy = null;
let paramTypes;
let returnType;
if (options) {
if (options.hasOwnProperty('debug')) {
this.debug = options.debug;
}
if (options.hasOwnProperty('prototypeOnly')) {
this.prototypeOnly = options.prototypeOnly;
}
if (options.hasOwnProperty('constants')) {
this.constants = options.constants;
}
if (options.hasOwnProperty('output')) {
this.output = options.output;
}
if (options.hasOwnProperty('loopMaxIterations')) {
this.loopMaxIterations = options.loopMaxIterations;
}
if (options.hasOwnProperty('paramTypes')) {
this.paramTypes = paramTypes = options.paramTypes;
}
if (options.hasOwnProperty('constantTypes')) {
this.constantTypes = options.constantTypes;
} else {
this.constantTypes = {};
}
if (options.hasOwnProperty('returnType')) {
returnType = options.returnType;
}
if (options.hasOwnProperty('fixIntegerDivisionAccuracy')) {
this.fixIntegerDivisionAccuracy = options.fixIntegerDivisionAccuracy;
}
}
//
// Missing jsFunction object exception
//
if (!jsFunction) {
throw 'jsFunction, parameter is missing';
}
//
// Setup jsFunction and its string property + validate them
//
this.jsFunctionString = jsFunction.toString();
if (!utils.isFunctionString(this.jsFunctionString)) {
console.error('jsFunction, to string conversion check failed: not a function?', this.jsFunctionString);
throw 'jsFunction, to string conversion check failed: not a function?';
}
if (!utils.isFunction(jsFunction)) {
//throw 'jsFunction, is not a valid JS Function';
this.jsFunction = null;
} else {
this.jsFunction = jsFunction;
}
//
// Setup the function name property
//
this.functionName = functionName ||
(jsFunction && jsFunction.name) ||
utils.getFunctionNameFromString(this.jsFunctionString);
if (!(this.functionName)) {
throw 'jsFunction, missing name argument or value';
}
//
// Extract parameter name, and its argument types
//
this.paramNames = utils.getParamNamesFromString(this.jsFunctionString);
if (paramTypes) {
if (Array.isArray(paramTypes)) {
if (paramTypes.length !== this.paramNames.length) {
throw 'Invalid argument type array length, against function length -> (' +
paramTypes.length + ',' +
this.paramNames.length +
')';
}
this.paramTypes = paramTypes;
} else if (typeof paramTypes === 'object') {
const paramVariableNames = Object.keys(paramTypes);
if (paramTypes.hasOwnProperty('returns')) {
this.returnType = paramTypes.returns;
paramVariableNames.splice(paramVariableNames.indexOf('returns'), 1);
}
if (paramVariableNames.length > 0 && paramVariableNames.length !== this.paramNames.length) {
throw 'Invalid argument type array length, against function length -> (' +
paramVariableNames.length + ',' +
this.paramNames.length +
')';
} else {
this.paramTypes = this.paramNames.map((key) => {
if (paramTypes.hasOwnProperty(key)) {
return paramTypes[key];
} else {
return 'float';
}
});
}
}
} else {
this.paramTypes = [];
}
//
// Return type handling
//
if (!this.returnType) {
this.returnType = returnType || 'float';
}
}
isIdentifierConstant(paramName) {
if (!this.constants) return false;
return this.constants.hasOwnProperty(paramName);
}
isInput(paramName) {
return this.paramTypes[this.paramNames.indexOf(paramName)] === 'Input';
}
setAddFunction(fn) {
this.addFunction = fn;
return this;
}
pushState(state) {
this.states.push(state);
}
popState(state) {
if (this.state !== state) {
throw new Error(`Cannot popState ${ state } when in ${ this.state }`);
}
this.states.pop();
}
isState(state) {
return this.state === state;
}
get state() {
return this.states[this.states.length - 1];
}
/**
*
* Core Functions
*
*/
/**
* @memberOf FunctionNodeBase#
* @function
* @name getJSFunction
*
* @desc Gets and return the stored JS Function.
* Note: that this internally eval the function, if only the string was provided on construction
*
* @returns {Function} The function object
*
*/
getJsFunction() {
if (this.jsFunction) {
return this.jsFunction;
}
if (this.jsFunctionString) {
this.jsFunction = eval(this.jsFunctionString);
return this.jsFunction;
}
throw 'Missing jsFunction, and jsFunctionString parameter';
}
/**
* @memberOf FunctionNodeBase#
* @function
* @name astMemberExpressionUnroll
* @desc Parses the abstract syntax tree for binary expression.
*
* <p>Utility function for astCallExpression.</p>
*
* @param {Object} ast - the AST object to parse
*
* @returns {String} the function namespace call, unrolled
*/
astMemberExpressionUnroll(ast) {
if (ast.type === 'Identifier') {
return ast.name;
} else if (ast.type === 'ThisExpression') {
return 'this';
}
if (ast.type === 'MemberExpression') {
if (ast.object && ast.property) {
//babel sniffing
if (ast.object.hasOwnProperty('name') && ast.object.name[0] === '_') {
return this.astMemberExpressionUnroll(ast.property);
}
return (
this.astMemberExpressionUnroll(ast.object) +
'.' +
this.astMemberExpressionUnroll(ast.property)
);
}
}
//babel sniffing
if (ast.hasOwnProperty('expressions')) {
const firstExpression = ast.expressions[0];
if (firstExpression.type === 'Literal' && firstExpression.value === 0 && ast.expressions.length === 2) {
return this.astMemberExpressionUnroll(ast.expressions[1]);
}
}
// Failure, unknown expression
throw this.astErrorOutput(
'Unknown CallExpression_unroll',
ast
);
}
/**
* @memberOf FunctionNodeBase#
* @function
* @name getJsAST
*
* @desc Parses the class function JS, and returns its Abstract Syntax Tree object.
*
* This is used internally to convert to shader code
*
* @param {Object} [inParser] - Parser to use, assumes in scope 'parser' if null or undefined
*
* @returns {Object} The function AST Object, note that result is cached under this.jsFunctionAST;
*
*/
getJsAST(inParser) {
if (this.jsFunctionAST) {
return this.jsFunctionAST;
}
inParser = inParser || acorn;
if (inParser === null) {
throw 'Missing JS to AST parser';
}
const ast = inParser.parse('var ' + this.functionName + ' = ' + this.jsFunctionString + ';', {
locations: true
});
if (ast === null) {
throw 'Failed to parse JS code';
}
// take out the function object, outside the var declarations
const funcAST = ast.body[0].declarations[0].init;
this.jsFunctionAST = funcAST;
return funcAST;
}
/**
* @memberOf FunctionNodeBase#
* @function
* @name getFunctionString
*
* @desc Returns the converted webgl shader function equivalent of the JS function
*
* @returns {String} webgl function string, result is cached under this.webGlFunctionString
*
*/
getFunctionString() {
this.generate();
return this.functionString;
}
/**
* @memberOf FunctionNodeBase#
* @function
* @name setFunctionString
*
* @desc Set the functionString value, overwriting it
*
* @param {String} functionString - Shader code string, representing the function
*
*/
setFunctionString(functionString) {
this.functionString = functionString;
}
/**
* @memberOf FunctionNodeBase#
* @function
* @name getParamType
*
* @desc Return the type of parameter sent to subKernel/Kernel.
*
* @param {String} paramName - Name of the parameter
*
* @returns {String} Type of the parameter
*
*/
getParamType(paramName) {
const paramIndex = this.paramNames.indexOf(paramName);
if (paramIndex === -1) {
if (this.declarations.hasOwnProperty(paramName)) {
return this.declarations[paramName];
} else {
return null;
}
} else {
if (!this.parent) {
if (this.paramTypes[paramIndex]) return this.paramTypes[paramIndex];
} else {
if (this.paramTypes[paramIndex]) return this.paramTypes[paramIndex];
const calledFunctionArguments = this.parent.calledFunctionsArguments[this.functionName];
for (let i = 0; i < calledFunctionArguments.length; i++) {
const calledFunctionArgument = calledFunctionArguments[i];
if (calledFunctionArgument[paramIndex] !== null) {
return this.paramTypes[paramIndex] = calledFunctionArgument[paramIndex].type;
}
}
}
}
return null;
}
getConstantType(constantName) {
if (this.constantTypes[constantName]) {
return this.constantTypes[constantName];
}
return null;
}
/**
* @memberOf FunctionNodeBase#
* @function
* @name getUserParamName
*
* @desc Return the name of the *user parameter*(subKernel parameter) corresponding
* to the parameter supplied to the kernel
*
* @param {String} paramName - Name of the parameter
*
* @returns {String} Name of the parameter
*
*/
getUserParamName(paramName) {
const paramIndex = this.paramNames.indexOf(paramName);
if (paramIndex === -1) return null;
if (!this.parent || !this.isSubKernel) return null;
const calledFunctionArguments = this.parent.calledFunctionsArguments[this.functionName];
for (let i = 0; i < calledFunctionArguments.length; i++) {
const calledFunctionArgument = calledFunctionArguments[i];
const param = calledFunctionArgument[paramIndex];
if (param !== null && param.type !== 'Integer') {
return param.name;
}
}
return null;
}
generate(options) {
throw new Error('generate not defined on BaseFunctionNode');
}
/**
* @memberOf FunctionNodeBase#
* @function
* @name astGeneric
*
* @desc Parses the abstract syntax tree for generically to its respective function
*
* @param {Object} ast - the AST object to parse
* @param {Array} retArr - return array string
*
* @returns {Array} the parsed string array
*/
astGeneric(ast, retArr) {
if (ast === null) {
throw this.astErrorOutput('NULL ast', ast);
} else {
if (Array.isArray(ast)) {
for (let i = 0; i < ast.length; i++) {
this.astGeneric(ast[i], retArr);
}
return retArr;
}
switch (ast.type) {
case 'FunctionDeclaration':
return this.astFunctionDeclaration(ast, retArr);
case 'FunctionExpression':
return this.astFunctionExpression(ast, retArr);
case 'ReturnStatement':
return this.astReturnStatement(ast, retArr);
case 'Literal':
return this.astLiteral(ast, retArr);
case 'BinaryExpression':
return this.astBinaryExpression(ast, retArr);
case 'Identifier':
return this.astIdentifierExpression(ast, retArr);
case 'AssignmentExpression':
return this.astAssignmentExpression(ast, retArr);
case 'ExpressionStatement':
return this.astExpressionStatement(ast, retArr);
case 'EmptyStatement':
return this.astEmptyStatement(ast, retArr);
case 'BlockStatement':
return this.astBlockStatement(ast, retArr);
case 'IfStatement':
return this.astIfStatement(ast, retArr);
case 'BreakStatement':
return this.astBreakStatement(ast, retArr);
case 'ContinueStatement':
return this.astContinueStatement(ast, retArr);
case 'ForStatement':
return this.astForStatement(ast, retArr);
case 'WhileStatement':
return this.astWhileStatement(ast, retArr);
case 'DoWhileStatement':
return this.astDoWhileStatement(ast, retArr);
case 'VariableDeclaration':
return this.astVariableDeclaration(ast, retArr);
case 'VariableDeclarator':
return this.astVariableDeclarator(ast, retArr);
case 'ThisExpression':
return this.astThisExpression(ast, retArr);
case 'SequenceExpression':
return this.astSequenceExpression(ast, retArr);
case 'UnaryExpression':
return this.astUnaryExpression(ast, retArr);
case 'UpdateExpression':
return this.astUpdateExpression(ast, retArr);
case 'LogicalExpression':
return this.astLogicalExpression(ast, retArr);
case 'MemberExpression':
return this.astMemberExpression(ast, retArr);
case 'CallExpression':
return this.astCallExpression(ast, retArr);
case 'ArrayExpression':
return this.astArrayExpression(ast, retArr);
case 'DebuggerStatement':
return this.astDebuggerStatement(ast, retArr);
}
throw this.astErrorOutput('Unknown ast type : ' + ast.type, ast);
}
}
/**
* @function
* @name astErrorOutput
* @ignore
* @desc To throw the AST error, with its location.
*
* @todo add location support fpr the AST error
*
* @param {Object} error - the error message output
* @param {Object} ast - the AST object where the error is
*/
astErrorOutput(error, ast) {
console.error(utils.getAstString(this.jsFunctionString, ast));
console.error(error, ast, this);
return error;
}
astDebuggerStatement(arrNode, retArr) {
return retArr;
}
astFunctionDeclaration(ast, retArr) {
return retArr;
}
astFunctionExpression(ast, retArr) {
return retArr;
}
astReturnStatement(ast, retArr) {
return retArr;
}
astLiteral(ast, retArr) {
return retArr;
}
astBinaryExpression(ast, retArr) {
return retArr;
}
astIdentifierExpression(ast, retArr) {
return retArr;
}
astAssignmentExpression(ast, retArr) {
return retArr;
}
astExpressionStatement(ast, retArr) {
return retArr;
}
astEmptyStatement(ast, retArr) {
return retArr;
}
astBlockStatement(ast, retArr) {
return retArr;
}
astIfStatement(ast, retArr) {
return retArr;
}
astBreakStatement(ast, retArr) {
return retArr;
}
astContinueStatement(ast, retArr) {
return retArr;
}
astForStatement(ast, retArr) {
return retArr;
}
astWhileStatement(ast, retArr) {
return retArr;
}
astDoWhileStatement(ast, retArr) {
return retArr;
}
astVariableDeclaration(ast, retArr) {
return retArr;
}
astVariableDeclarator(ast, retArr) {
return retArr;
}
astThisExpression(ast, retArr) {
return retArr;
}
astSequenceExpression(ast, retArr) {
return retArr;
}
astUnaryExpression(ast, retArr) {
return retArr;
}
astUpdateExpression(ast, retArr) {
return retArr;
}
astLogicalExpression(ast, retArr) {
return retArr;
}
astMemberExpression(ast, retArr) {
return retArr;
}
astCallExpression(ast, retArr) {
return retArr;
}
astArrayExpression(ast, retArr) {
return retArr;
}
/**
* @ignore
* @function
* @name pushParameter
*
* @desc [INTERNAL] pushes a fn parameter onto retArr and 'casts' to int if necessary
* i.e. deal with force-int-parameter state
*
* @param {Array} retArr - return array string
* @param {String} parameter - the parameter name
*
*/
pushParameter(retArr, parameter) {
if (this.isState('in-get-call-parameters')) {
retArr.push(`int(${parameter})`);
} else {
retArr.push(parameter);
}
}
};