Skip to content

Commit 4260a34

Browse files
fix: #493, and add setStrictIntegers to typescript def
fix: Remove repeated literal `${ast.start},${ast.end}`, in favor of var
1 parent 21b9e3b commit 4260a34

File tree

10 files changed

+258
-52
lines changed

10 files changed

+258
-52
lines changed

dist/gpu-browser-core.js

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* GPU Accelerated JavaScript
66
*
7-
* @version 2.0.0-rc.23
8-
* @date Fri Jul 26 2019 14:52:24 GMT-0400 (Eastern Daylight Time)
7+
* @version 2.0.0-rc.24
8+
* @date Mon Jul 29 2019 16:04:14 GMT-0400 (Eastern Daylight Time)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -2776,6 +2776,18 @@ class FunctionNode {
27762776
return 'Integer';
27772777
}
27782778
const type = this.getType(ast.left);
2779+
if (this.isState('skip-literal-correction')) return type;
2780+
if (type === 'LiteralInteger') {
2781+
const rightType = this.getType(ast.right);
2782+
if (rightType === 'LiteralInteger') {
2783+
if (ast.left.value % 1 === 0) {
2784+
return 'Integer';
2785+
} else {
2786+
return 'Float';
2787+
}
2788+
}
2789+
return rightType;
2790+
}
27792791
return typeLookupMap[type] || type;
27802792
case 'UpdateExpression':
27812793
return this.getType(ast.argument);
@@ -6529,7 +6541,9 @@ class WebGLFunctionNode extends FunctionNode {
65296541

65306542
astReturnStatement(ast, retArr) {
65316543
if (!ast.argument) throw this.astErrorOutput('Unexpected return statement', ast);
6544+
this.pushState('skip-literal-correction');
65326545
const type = this.getType(ast.argument);
6546+
this.popState('skip-literal-correction');
65336547

65346548
const result = [];
65356549

@@ -6606,22 +6620,23 @@ class WebGLFunctionNode extends FunctionNode {
66066620
);
66076621
}
66086622

6623+
const key = `${ast.start},${ast.end}`;
66096624
if (Number.isInteger(ast.value)) {
6610-
if (this.isState('in-for-loop-init') || this.isState('casting-to-integer')) {
6611-
this.literalTypes[`${ast.start},${ast.end}`] = 'Integer';
6625+
if (this.isState('in-for-loop-init') || this.isState('casting-to-integer') || this.isState('building-integer')) {
6626+
this.literalTypes[key] = 'Integer';
66126627
retArr.push(`${ast.value}`);
6613-
} else if (this.isState('casting-to-float')) {
6614-
this.literalTypes[`${ast.start},${ast.end}`] = 'Number';
6628+
} else if (this.isState('casting-to-float') || this.isState('building-float')) {
6629+
this.literalTypes[key] = 'Number';
66156630
retArr.push(`${ast.value}.0`);
66166631
} else {
6617-
this.literalTypes[`${ast.start},${ast.end}`] = 'Number';
6632+
this.literalTypes[key] = 'Number';
66186633
retArr.push(`${ast.value}.0`);
66196634
}
6620-
} else if (this.isState('casting-to-integer')) {
6621-
this.literalTypes[`${ast.start},${ast.end}`] = 'Integer';
6622-
retArr.push(parseInt(ast.raw));
6635+
} else if (this.isState('casting-to-integer') || this.isState('building-integer')) {
6636+
this.literalTypes[key] = 'Integer';
6637+
retArr.push(Math.round(ast.value));
66236638
} else {
6624-
this.literalTypes[`${ast.start},${ast.end}`] = 'Number';
6639+
this.literalTypes[key] = 'Number';
66256640
retArr.push(`${ast.value}`);
66266641
}
66276642
return retArr;
@@ -6634,6 +6649,7 @@ class WebGLFunctionNode extends FunctionNode {
66346649

66356650
if (this.fixIntegerDivisionAccuracy && ast.operator === '/') {
66366651
retArr.push('div_with_int_check(');
6652+
this.pushState('building-float');
66376653
switch (this.getType(ast.left)) {
66386654
case 'Integer':
66396655
this.castValueToFloat(ast.left, retArr);
@@ -6655,6 +6671,7 @@ class WebGLFunctionNode extends FunctionNode {
66556671
default:
66566672
this.astGeneric(ast.right, retArr);
66576673
}
6674+
this.popState('building-float');
66586675
retArr.push(')');
66596676
return retArr;
66606677
}
@@ -6668,40 +6685,51 @@ class WebGLFunctionNode extends FunctionNode {
66686685
const key = leftType + ' & ' + rightType;
66696686
switch (key) {
66706687
case 'Integer & Integer':
6688+
this.pushState('building-integer');
66716689
this.astGeneric(ast.left, retArr);
66726690
retArr.push(operatorMap[ast.operator] || ast.operator);
66736691
this.astGeneric(ast.right, retArr);
6692+
this.popState('building-integer');
66746693
break;
66756694
case 'Number & Float':
66766695
case 'Float & Number':
66776696
case 'Float & Float':
66786697
case 'Number & Number':
6698+
this.pushState('building-float');
66796699
this.astGeneric(ast.left, retArr);
66806700
retArr.push(operatorMap[ast.operator] || ast.operator);
66816701
this.astGeneric(ast.right, retArr);
6702+
this.popState('building-float');
66826703
break;
66836704
case 'LiteralInteger & LiteralInteger':
6684-
if (this.isState('casting-to-integer')) {
6705+
if (this.isState('casting-to-integer') || this.isState('building-integer')) {
6706+
this.pushState('building-integer');
66856707
this.astGeneric(ast.left, retArr);
66866708
retArr.push(operatorMap[ast.operator] || ast.operator);
66876709
this.astGeneric(ast.right, retArr);
6710+
this.popState('building-integer');
66886711
} else {
6712+
this.pushState('building-float');
66896713
this.castLiteralToFloat(ast.left, retArr);
66906714
retArr.push(operatorMap[ast.operator] || ast.operator);
66916715
this.castLiteralToFloat(ast.right, retArr);
6716+
this.popState('building-float');
66926717
}
66936718
break;
66946719

66956720
case 'Integer & Float':
66966721
case 'Integer & Number':
66976722
if (ast.operator === '>' || ast.operator === '<' && ast.right.type === 'Literal') {
66986723
if (!Number.isInteger(ast.right.value)) {
6724+
this.pushState('building-float');
66996725
this.castValueToFloat(ast.left, retArr);
67006726
retArr.push(operatorMap[ast.operator] || ast.operator);
67016727
this.astGeneric(ast.right, retArr);
6728+
this.popState('building-float');
67026729
break;
67036730
}
67046731
}
6732+
this.pushState('building-integer');
67056733
this.astGeneric(ast.left, retArr);
67066734
retArr.push(operatorMap[ast.operator] || ast.operator);
67076735
this.pushState('casting-to-integer');
@@ -6720,62 +6748,81 @@ class WebGLFunctionNode extends FunctionNode {
67206748
retArr.push(')');
67216749
}
67226750
this.popState('casting-to-integer');
6751+
this.popState('building-integer');
67236752
break;
67246753
case 'Integer & LiteralInteger':
6754+
this.pushState('building-integer');
67256755
this.astGeneric(ast.left, retArr);
67266756
retArr.push(operatorMap[ast.operator] || ast.operator);
67276757
this.castLiteralToInteger(ast.right, retArr);
6758+
this.popState('building-integer');
67286759
break;
67296760

67306761
case 'Number & Integer':
6762+
this.pushState('building-float');
67316763
this.astGeneric(ast.left, retArr);
67326764
retArr.push(operatorMap[ast.operator] || ast.operator);
67336765
this.castValueToFloat(ast.right, retArr);
6766+
this.popState('building-float');
67346767
break;
67356768
case 'Float & LiteralInteger':
67366769
case 'Number & LiteralInteger':
67376770
if (this.isState('in-for-loop-test')) {
6771+
this.pushState('building-integer');
67386772
retArr.push('int(');
67396773
this.astGeneric(ast.left, retArr);
67406774
retArr.push(')');
67416775
retArr.push(operatorMap[ast.operator] || ast.operator);
67426776
this.castLiteralToInteger(ast.right, retArr);
6777+
this.popState('building-integer');
67436778
} else {
6779+
this.pushState('building-float');
67446780
this.astGeneric(ast.left, retArr);
67456781
retArr.push(operatorMap[ast.operator] || ast.operator);
67466782
this.castLiteralToFloat(ast.right, retArr);
6783+
this.popState('building-float');
67476784
}
67486785
break;
67496786
case 'LiteralInteger & Float':
67506787
case 'LiteralInteger & Number':
67516788
if (this.isState('in-for-loop-test') || this.isState('in-for-loop-init') || this.isState('casting-to-integer')) {
6789+
this.pushState('building-integer');
67526790
this.castLiteralToInteger(ast.left, retArr);
67536791
retArr.push(operatorMap[ast.operator] || ast.operator);
67546792
this.castValueToInteger(ast.right, retArr);
6793+
this.popState('building-integer');
67556794
} else {
6795+
this.pushState('building-float');
67566796
this.astGeneric(ast.left, retArr);
67576797
retArr.push(operatorMap[ast.operator] || ast.operator);
67586798
this.pushState('casting-to-float');
67596799
this.astGeneric(ast.right, retArr);
67606800
this.popState('casting-to-float');
6801+
this.popState('building-float');
67616802
}
67626803
break;
67636804
case 'LiteralInteger & Integer':
6805+
this.pushState('building-integer');
67646806
this.castLiteralToInteger(ast.left, retArr);
67656807
retArr.push(operatorMap[ast.operator] || ast.operator);
67666808
this.astGeneric(ast.right, retArr);
6809+
this.popState('building-integer');
67676810
break;
67686811

67696812
case 'Boolean & Boolean':
6813+
this.pushState('building-boolean');
67706814
this.astGeneric(ast.left, retArr);
67716815
retArr.push(operatorMap[ast.operator] || ast.operator);
67726816
this.astGeneric(ast.right, retArr);
6817+
this.popState('building-boolean');
67736818
break;
67746819

67756820
case 'Float & Integer':
6821+
this.pushState('building-float');
67766822
this.astGeneric(ast.left, retArr);
67776823
retArr.push(operatorMap[ast.operator] || ast.operator);
67786824
this.castValueToFloat(ast.right, retArr);
6825+
this.popState('building-float');
67796826
break;
67806827

67816828
default:
@@ -7165,9 +7212,13 @@ class WebGLFunctionNode extends FunctionNode {
71657212
lastType = type;
71667213
declarationResult.push(`user_${declaration.id.name}=`);
71677214
if (actualType === 'Number' && type === 'Integer') {
7168-
declarationResult.push('int(');
7169-
this.astGeneric(init, declarationResult);
7170-
declarationResult.push(')');
7215+
if (init.left && init.left.type === 'Literal') {
7216+
this.astGeneric(init, declarationResult);
7217+
} else {
7218+
declarationResult.push('int(');
7219+
this.astGeneric(init, declarationResult);
7220+
declarationResult.push(')');
7221+
}
71717222
} else {
71727223
this.astGeneric(init, declarationResult);
71737224
}
@@ -13743,6 +13794,5 @@ const _systemEndianness = utils.getSystemEndianness();
1374313794
module.exports = {
1374413795
utils
1374513796
};
13746-
1374713797
},{"./gpu.js":101,"./input":103,"./texture":106,"acorn":1}]},{},[100])(100)
1374813798
});

dist/gpu-browser-core.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)