Skip to content

Commit 87ccce5

Browse files
fix: thread & output variabel assignment
Fixes #390
1 parent 1d7174f commit 87ccce5

File tree

9 files changed

+184
-40
lines changed

9 files changed

+184
-40
lines changed

bin/gpu-core.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* GPU Accelerated JavaScript
66
*
7-
* @version 1.10.0
8-
* @date Tue Oct 30 2018 07:33:44 GMT-0600 (MDT)
7+
* @version 1.10.1
8+
* @date Thu Nov 01 2018 10:01:30 GMT-0400 (EDT)
99
*
1010
* @license MIT
1111
* The MIT License

bin/gpu-core.min.js

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

bin/gpu.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* GPU Accelerated JavaScript
66
*
7-
* @version 1.10.0
8-
* @date Tue Oct 30 2018 07:33:45 GMT-0600 (MDT)
7+
* @version 1.10.1
8+
* @date Thu Nov 01 2018 10:01:30 GMT-0400 (EDT)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -2908,22 +2908,25 @@ module.exports = function (_FunctionNodeBase) {
29082908
if (init) {
29092909
if (init.object) {
29102910
if (init.object.type === 'MemberExpression' && init.object.object) {
2911-
if (init.object.object.type === 'Identifier') {
2912-
var _type2 = this.getParamType(init.object.object.name);
2913-
declarationType = typeLookupMap[_type2];
2911+
if (init.object.object.type === 'ThisExpression' && init.object.property && (init.object.property.name === 'thread' || init.object.property.name === 'output')) {
2912+
declarationType = 'Integer';
29142913
}
2915-
else if (init.object.object.object && init.object.object.object.type === 'Identifier') {
2916-
var _type3 = this.getParamType(init.object.object.object.name);
2917-
declarationType = typeLookupMap[_type3];
2914+
else if (init.object.object.type === 'Identifier') {
2915+
var _type2 = this.getParamType(init.object.object.name);
2916+
declarationType = typeLookupMap[_type2];
29182917
}
2919-
else if (init.object.object.object.object && init.object.object.object.object.type === 'ThisExpression' && init.object.object.object.property.name === 'constants') {
2920-
var _type4 = this.getConstantType(init.object.object.property.name);
2921-
declarationType = typeLookupMap[_type4];
2918+
else if (init.object.object.object && init.object.object.object.type === 'Identifier') {
2919+
var _type3 = this.getParamType(init.object.object.object.name);
2920+
declarationType = typeLookupMap[_type3];
29222921
}
2923-
else if (init.object.object.object.object.object && init.object.object.object.object.object.type === 'ThisExpression' && init.object.object.object.object.property.name === 'constants') {
2924-
var _type5 = this.getConstantType(init.object.object.object.property.name);
2925-
declarationType = typeLookupMap[_type5];
2922+
else if (init.object.object.object && init.object.object.object.object && init.object.object.object.object.type === 'ThisExpression' && init.object.object.object.property.name === 'constants') {
2923+
var _type4 = this.getConstantType(init.object.object.property.name);
2924+
declarationType = typeLookupMap[_type4];
29262925
}
2926+
else if (init.object.object.object && init.object.object.object.object && init.object.object.object.object.object && init.object.object.object.object.object.type === 'ThisExpression' && init.object.object.object.object.property.name === 'constants') {
2927+
var _type5 = this.getConstantType(init.object.object.object.property.name);
2928+
declarationType = typeLookupMap[_type5];
2929+
}
29272930
}
29282931
if (!declarationType) {
29292932
throw new Error('unknown lookup type ' + typeLookupMap);

bin/gpu.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.

dist/backend/web-gl/function-node.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -689,26 +689,30 @@ module.exports = function (_FunctionNodeBase) {
689689
if (init) {
690690
if (init.object) {
691691
if (init.object.type === 'MemberExpression' && init.object.object) {
692-
// param[]
693-
if (init.object.object.type === 'Identifier') {
694-
var _type2 = this.getParamType(init.object.object.name);
695-
declarationType = typeLookupMap[_type2];
692+
// this.thread.x, this.thread.y, this.thread.z
693+
if (init.object.object.type === 'ThisExpression' && init.object.property && (init.object.property.name === 'thread' || init.object.property.name === 'output')) {
694+
declarationType = 'Integer';
696695
}
697-
// param[][]
698-
else if (init.object.object.object && init.object.object.object.type === 'Identifier') {
699-
var _type3 = this.getParamType(init.object.object.object.name);
700-
declarationType = typeLookupMap[_type3];
696+
// param[]
697+
else if (init.object.object.type === 'Identifier') {
698+
var _type2 = this.getParamType(init.object.object.name);
699+
declarationType = typeLookupMap[_type2];
701700
}
702-
// this.constants.param[]
703-
else if (init.object.object.object.object && init.object.object.object.object.type === 'ThisExpression' && init.object.object.object.property.name === 'constants') {
704-
var _type4 = this.getConstantType(init.object.object.property.name);
705-
declarationType = typeLookupMap[_type4];
701+
// param[][]
702+
else if (init.object.object.object && init.object.object.object.type === 'Identifier') {
703+
var _type3 = this.getParamType(init.object.object.object.name);
704+
declarationType = typeLookupMap[_type3];
706705
}
707-
// this.constants.param[][]
708-
else if (init.object.object.object.object.object && init.object.object.object.object.object.type === 'ThisExpression' && init.object.object.object.object.property.name === 'constants') {
709-
var _type5 = this.getConstantType(init.object.object.object.property.name);
710-
declarationType = typeLookupMap[_type5];
706+
// this.constants.param[]
707+
else if (init.object.object.object && init.object.object.object.object && init.object.object.object.object.type === 'ThisExpression' && init.object.object.object.property.name === 'constants') {
708+
var _type4 = this.getConstantType(init.object.object.property.name);
709+
declarationType = typeLookupMap[_type4];
711710
}
711+
// this.constants.param[][]
712+
else if (init.object.object.object && init.object.object.object.object && init.object.object.object.object.object && init.object.object.object.object.object.type === 'ThisExpression' && init.object.object.object.object.property.name === 'constants') {
713+
var _type5 = this.getConstantType(init.object.object.object.property.name);
714+
declarationType = typeLookupMap[_type5];
715+
}
712716
}
713717
if (!declarationType) {
714718
throw new Error('unknown lookup type ' + typeLookupMap);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gpu.js",
3-
"version": "1.10.0",
3+
"version": "1.10.1",
44
"description": "GPU Accelerated JavaScript",
55
"main": "./dist/index.js",
66
"files": ["dist", "bin"],

src/backend/web-gl/function-node.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,19 @@ module.exports = class WebGLFunctionNode extends FunctionNodeBase {
653653
init.object.type === 'MemberExpression' &&
654654
init.object.object
655655
) {
656+
// this.thread.x, this.thread.y, this.thread.z
657+
if (
658+
init.object.object.type === 'ThisExpression' &&
659+
init.object.property &&
660+
(
661+
init.object.property.name === 'thread' ||
662+
init.object.property.name === 'output'
663+
)
664+
) {
665+
declarationType = 'Integer';
666+
}
656667
// param[]
657-
if (init.object.object.type === 'Identifier') {
668+
else if (init.object.object.type === 'Identifier') {
658669
const type = this.getParamType(init.object.object.name);
659670
declarationType = typeLookupMap[type];
660671
}
@@ -668,6 +679,7 @@ module.exports = class WebGLFunctionNode extends FunctionNodeBase {
668679
}
669680
// this.constants.param[]
670681
else if (
682+
init.object.object.object &&
671683
init.object.object.object.object &&
672684
init.object.object.object.object.type === 'ThisExpression' &&
673685
init.object.object.object.property.name === 'constants'
@@ -677,6 +689,8 @@ module.exports = class WebGLFunctionNode extends FunctionNodeBase {
677689
}
678690
// this.constants.param[][]
679691
else if (
692+
init.object.object.object &&
693+
init.object.object.object.object &&
680694
init.object.object.object.object.object &&
681695
init.object.object.object.object.object.type === 'ThisExpression' &&
682696
init.object.object.object.object.property.name === 'constants'

test/all.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
<!-- gpu.js scripts -->
99
<script src="../bin/gpu.js"></script>
10-
<script src="../bin/gpu-core.js"></script>
1110
</head>
1211
<body>
1312
<div id="qunit"></div>
@@ -88,5 +87,6 @@
8887
<script src="issues/357-modulus-issue.js"></script>
8988
<script src="issues/359-addfunction-params-wrong.js"></script>
9089
<script src="issues/382-bad-constant.js"></script>
90+
<script src="issues/390-thread-assignment.js"></script>
9191
</body>
9292
</html>

test/issues/390-thread-assignment.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
(function() {
2+
QUnit.test('Issue #390 - thread assignment (webgl)', function(assert) {
3+
var node = new GPU.WebGLFunctionNode(null, function assignThreadToVar() {
4+
let x = this.thread.x;
5+
let y = this.thread.y;
6+
let sum = x + y;
7+
return sum;
8+
});
9+
assert.equal(node.generate(), 'float assignThreadToVar() {\n\
10+
float user_x=float(threadId.x);\n\
11+
float user_y=float(threadId.y);\n\
12+
float user_sum=(user_x+user_y);\n\
13+
return user_sum;\n\
14+
}');
15+
assert.equal(node.declarations.x, 'Integer');
16+
assert.equal(node.declarations.y, 'Integer');
17+
assert.equal(node.declarations.sum, 'Number');
18+
});
19+
20+
QUnit.test('Issue #390 - thread assignment (webgl2)', function(assert) {
21+
var node = new GPU.WebGL2FunctionNode(null, function assignThreadToVar() {
22+
let x = this.thread.x;
23+
let y = this.thread.y;
24+
let sum = x + y;
25+
return sum;
26+
});
27+
assert.equal(node.generate(), 'float assignThreadToVar() {\n\
28+
float user_x=float(threadId.x);\n\
29+
float user_y=float(threadId.y);\n\
30+
float user_sum=(user_x+user_y);\n\
31+
return user_sum;\n\
32+
}');
33+
assert.equal(node.declarations.x, 'Integer');
34+
assert.equal(node.declarations.y, 'Integer');
35+
assert.equal(node.declarations.sum, 'Number');
36+
});
37+
38+
QUnit.test('Issue #390 - thread assignment (cpu)', function(assert) {
39+
var node = new GPU.CPUFunctionNode(null, function assignThreadToVar() {
40+
let x = this.thread.x;
41+
let y = this.thread.y;
42+
let sum = x + y;
43+
return sum;
44+
});
45+
assert.equal(node.generate(), 'function assignThreadToVar() {\n\
46+
var user_x=_this.thread.x;\n\
47+
var user_y=_this.thread.y;\n\
48+
var user_sum=(user_x+user_y);\n\
49+
return user_sum;\n\
50+
}');
51+
assert.equal(node.declarations.x, 'var');
52+
assert.equal(node.declarations.y, 'var');
53+
assert.equal(node.declarations.sum, 'var');
54+
});
55+
})();
56+
57+
(function() {
58+
QUnit.test('Issue #390 (related) - output assignment (webgl)', function(assert) {
59+
var node = new GPU.WebGLFunctionNode(null, function assignThreadToVar() {
60+
let x = this.output.x;
61+
let y = this.output.y;
62+
let z = this.output.z;
63+
let sum = x + y + z;
64+
return sum;
65+
});
66+
node.output = [1,2,3];
67+
assert.equal(node.generate(), 'float assignThreadToVar() {\n\
68+
float user_x=1.0;\n\
69+
float user_y=2.0;\n\
70+
float user_z=3.0;\n\
71+
float user_sum=((user_x+user_y)+user_z);\n\
72+
return user_sum;\n\
73+
}');
74+
assert.equal(node.declarations.x, 'Integer');
75+
assert.equal(node.declarations.y, 'Integer');
76+
assert.equal(node.declarations.z, 'Integer');
77+
assert.equal(node.declarations.sum, 'Number');
78+
});
79+
80+
QUnit.test('Issue #390 (related) - output assignment (webgl2)', function(assert) {
81+
var node = new GPU.WebGL2FunctionNode(null, function assignThreadToVar() {
82+
let x = this.output.x;
83+
let y = this.output.y;
84+
let z = this.output.z;
85+
let sum = x + y + z;
86+
return sum;
87+
});
88+
node.output = [1,2,3];
89+
assert.equal(node.generate(), 'float assignThreadToVar() {\n\
90+
float user_x=1.0;\n\
91+
float user_y=2.0;\n\
92+
float user_z=3.0;\n\
93+
float user_sum=((user_x+user_y)+user_z);\n\
94+
return user_sum;\n\
95+
}');
96+
assert.equal(node.declarations.x, 'Integer');
97+
assert.equal(node.declarations.y, 'Integer');
98+
assert.equal(node.declarations.z, 'Integer');
99+
assert.equal(node.declarations.sum, 'Number');
100+
});
101+
102+
QUnit.test('Issue #390 (related) - output assignment (cpu)', function(assert) {
103+
var node = new GPU.CPUFunctionNode(null, function assignThreadToVar() {
104+
let x = this.output.x;
105+
let y = this.output.y;
106+
let z = this.output.z;
107+
let sum = x + y + z;
108+
return sum;
109+
});
110+
node.output = [1,2,3];
111+
assert.equal(node.generate(), 'function assignThreadToVar() {\n\
112+
var user_x=1;\n\
113+
var user_y=2;\n\
114+
var user_z=3;\n\
115+
var user_sum=((user_x+user_y)+user_z);\n\
116+
return user_sum;\n\
117+
}');
118+
assert.equal(node.declarations.x, 'var');
119+
assert.equal(node.declarations.y, 'var');
120+
assert.equal(node.declarations.z, 'var');
121+
assert.equal(node.declarations.sum, 'var');
122+
});
123+
})();

0 commit comments

Comments
 (0)