Skip to content

Commit 984dd10

Browse files
committed
speed up compile by map
1 parent 6622ecb commit 984dd10

File tree

1 file changed

+72
-69
lines changed

1 file changed

+72
-69
lines changed

src/backend/function-node.js

+72-69
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,75 @@ const acorn = require('acorn');
22
const { utils } = require('../utils');
33
const { FunctionTracer } = require('./function-tracer');
44

5+
const mathProperties = {
6+
'E': true,
7+
'PI': true,
8+
'SQRT2': true,
9+
'SQRT1_2': true,
10+
'LN2': true,
11+
'LN10': true,
12+
'LOG2E': true,
13+
'LOG10E': true,
14+
};
15+
16+
const mathFunctions = {
17+
'abs': true,
18+
'acos': true,
19+
'acosh': true,
20+
'asin': true,
21+
'asinh': true,
22+
'atan': true,
23+
'atan2': true,
24+
'atanh': true,
25+
'cbrt': true,
26+
'ceil': true,
27+
'clz32': true,
28+
'cos': true,
29+
'cosh': true,
30+
'expm1': true,
31+
'exp': true,
32+
'floor': true,
33+
'fround': true,
34+
'imul': true,
35+
'log': true,
36+
'log2': true,
37+
'log10': true,
38+
'log1p': true,
39+
'max': true,
40+
'min': true,
41+
'pow': true,
42+
'random': true,
43+
'round': true,
44+
'sign': true,
45+
'sin': true,
46+
'sinh': true,
47+
'sqrt': true,
48+
'tan': true,
49+
'tanh': true,
50+
'trunc': true,
51+
};
52+
53+
const allowedExpressions = {
54+
'value': true,
55+
'value[]': true,
56+
'value[][]': true,
57+
'value[][][]': true,
58+
'value[][][][]': true,
59+
'value.value': true,
60+
'value.thread.value': true,
61+
'this.thread.value': true,
62+
'this.output.value': true,
63+
'this.constants.value': true,
64+
'this.constants.value[]': true,
65+
'this.constants.value[][]': true,
66+
'this.constants.value[][][]': true,
67+
'this.constants.value[][][][]': true,
68+
'fn()[]': true,
69+
'fn()[][]': true,
70+
'fn()[][][]': true,
71+
'[][]': true,
72+
};
73+
574
/**
675
*
776
* @desc Represents a single function, inside JS, webGL, or openGL.
@@ -592,61 +661,15 @@ class FunctionNode {
592661
}
593662

594663
isAstMathVariable(ast) {
595-
const mathProperties = [
596-
'E',
597-
'PI',
598-
'SQRT2',
599-
'SQRT1_2',
600-
'LN2',
601-
'LN10',
602-
'LOG2E',
603-
'LOG10E',
604-
];
605664
return ast.type === 'MemberExpression' &&
606665
ast.object && ast.object.type === 'Identifier' &&
607666
ast.object.name === 'Math' &&
608667
ast.property &&
609668
ast.property.type === 'Identifier' &&
610-
mathProperties.indexOf(ast.property.name) > -1;
669+
!!mathProperties[ast.property.name];
611670
}
612671

613672
isAstMathFunction(ast) {
614-
const mathFunctions = [
615-
'abs',
616-
'acos',
617-
'acosh',
618-
'asin',
619-
'asinh',
620-
'atan',
621-
'atan2',
622-
'atanh',
623-
'cbrt',
624-
'ceil',
625-
'clz32',
626-
'cos',
627-
'cosh',
628-
'expm1',
629-
'exp',
630-
'floor',
631-
'fround',
632-
'imul',
633-
'log',
634-
'log2',
635-
'log10',
636-
'log1p',
637-
'max',
638-
'min',
639-
'pow',
640-
'random',
641-
'round',
642-
'sign',
643-
'sin',
644-
'sinh',
645-
'sqrt',
646-
'tan',
647-
'tanh',
648-
'trunc',
649-
];
650673
return ast.type === 'CallExpression' &&
651674
ast.callee &&
652675
ast.callee.type === 'MemberExpression' &&
@@ -655,7 +678,7 @@ class FunctionNode {
655678
ast.callee.object.name === 'Math' &&
656679
ast.callee.property &&
657680
ast.callee.property.type === 'Identifier' &&
658-
mathFunctions.indexOf(ast.callee.property.name) > -1;
681+
!!mathFunctions[ast.callee.property.name];
659682
}
660683

661684
isAstVariable(ast) {
@@ -844,27 +867,7 @@ class FunctionNode {
844867
return signatureString;
845868
}
846869

847-
const allowedExpressions = [
848-
'value',
849-
'value[]',
850-
'value[][]',
851-
'value[][][]',
852-
'value[][][][]',
853-
'value.value',
854-
'value.thread.value',
855-
'this.thread.value',
856-
'this.output.value',
857-
'this.constants.value',
858-
'this.constants.value[]',
859-
'this.constants.value[][]',
860-
'this.constants.value[][][]',
861-
'this.constants.value[][][][]',
862-
'fn()[]',
863-
'fn()[][]',
864-
'fn()[][][]',
865-
'[][]',
866-
];
867-
if (allowedExpressions.indexOf(signatureString) > -1) {
870+
if (!!allowedExpressions[signatureString]) {
868871
return signatureString;
869872
}
870873
return null;

0 commit comments

Comments
 (0)