Skip to content

Commit cb4d4f1

Browse files
fix: cpu canvas check
1 parent e4dd9d5 commit cb4d4f1

File tree

9 files changed

+79
-28
lines changed

9 files changed

+79
-28
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.3
8-
* @date Sun Nov 18 2018 14:38:47 GMT-0500 (EST)
7+
* @version 1.10.4
8+
* @date Sun Nov 18 2018 15:47:22 GMT-0500 (EST)
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: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* GPU Accelerated JavaScript
66
*
7-
* @version 1.10.3
8-
* @date Sun Nov 18 2018 14:38:47 GMT-0500 (EST)
7+
* @version 1.10.4
8+
* @date Sun Nov 18 2018 15:47:22 GMT-0500 (EST)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -890,16 +890,22 @@ module.exports = function (_KernelBase) {
890890
this.setupParams(arguments);
891891
this.validateOptions();
892892
var canvas = this._canvas;
893-
this._canvasCtx = canvas.getContext('2d');
893+
if (canvas) {
894+
this._canvasCtx = canvas.getContext('2d');
895+
}
894896
var threadDim = this.threadDim = utils.clone(this.output);
895897

896898
while (threadDim.length < 3) {
897899
threadDim.push(1);
898900
}
899901

900902
if (this.graphical) {
901-
canvas.width = threadDim[0];
902-
canvas.height = threadDim[1];
903+
var _canvas = this._canvas;
904+
if (!_canvas) {
905+
throw new Error('no canvas available for using graphical output');
906+
}
907+
_canvas.width = threadDim[0];
908+
_canvas.height = threadDim[1];
903909
this._imageData = this._canvasCtx.createImageData(threadDim[0], threadDim[1]);
904910
this._colorData = new Uint8ClampedArray(threadDim[0] * threadDim[1] * 4);
905911
}

bin/gpu.min.js

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

dist/backend/cpu/kernel.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = function (_KernelBase) {
1919
* @constructor CPUKernel
2020
*
2121
* @desc Kernel Implementation for CPU.
22-
*
22+
*
2323
* <p>Instantiates properties to the CPU Kernel.</p>
2424
*
2525
* @extends KernelBase
@@ -60,7 +60,7 @@ module.exports = function (_KernelBase) {
6060
* @function
6161
* @name validateOptions
6262
*
63-
* @desc Validate options related to CPU Kernel, such as
63+
* @desc Validate options related to CPU Kernel, such as
6464
* dimensions size, and auto dimension support.
6565
*
6666
*/
@@ -92,8 +92,8 @@ module.exports = function (_KernelBase) {
9292
* @function
9393
* @name build
9494
*
95-
* @desc Builds the Kernel, by generating the kernel
96-
* string using thread dimensions, and arguments
95+
* @desc Builds the Kernel, by generating the kernel
96+
* string using thread dimensions, and arguments
9797
* supplied to the kernel.
9898
*
9999
* <p>If the graphical flag is enabled, canvas is used.</p>
@@ -107,16 +107,23 @@ module.exports = function (_KernelBase) {
107107
this.setupParams(arguments);
108108
this.validateOptions();
109109
var canvas = this._canvas;
110-
this._canvasCtx = canvas.getContext('2d');
110+
if (canvas) {
111+
// if node or canvas is not found, don't die
112+
this._canvasCtx = canvas.getContext('2d');
113+
}
111114
var threadDim = this.threadDim = utils.clone(this.output);
112115

113116
while (threadDim.length < 3) {
114117
threadDim.push(1);
115118
}
116119

117120
if (this.graphical) {
118-
canvas.width = threadDim[0];
119-
canvas.height = threadDim[1];
121+
var _canvas = this._canvas;
122+
if (!_canvas) {
123+
throw new Error('no canvas available for using graphical output');
124+
}
125+
_canvas.width = threadDim[0];
126+
_canvas.height = threadDim[1];
120127
this._imageData = this._canvasCtx.createImageData(threadDim[0], threadDim[1]);
121128
this._colorData = new Uint8ClampedArray(threadDim[0] * threadDim[1] * 4);
122129
}
@@ -165,7 +172,7 @@ module.exports = function (_KernelBase) {
165172
* @name getKernelString
166173
*
167174
* @desc Generates kernel string for this kernel program.
168-
*
175+
*
169176
* <p>If sub-kernels are supplied, they are also factored in.
170177
* This string can be saved by calling the `toString` method
171178
* and then can be reused later.</p>

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.3",
3+
"version": "1.10.4",
44
"description": "GPU Accelerated JavaScript",
55
"main": "./dist/index.js",
66
"files": [

src/backend/cpu/kernel.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = class CPUKernel extends KernelBase {
1010
* @constructor CPUKernel
1111
*
1212
* @desc Kernel Implementation for CPU.
13-
*
13+
*
1414
* <p>Instantiates properties to the CPU Kernel.</p>
1515
*
1616
* @extends KernelBase
@@ -47,7 +47,7 @@ module.exports = class CPUKernel extends KernelBase {
4747
* @function
4848
* @name validateOptions
4949
*
50-
* @desc Validate options related to CPU Kernel, such as
50+
* @desc Validate options related to CPU Kernel, such as
5151
* dimensions size, and auto dimension support.
5252
*
5353
*/
@@ -75,8 +75,8 @@ module.exports = class CPUKernel extends KernelBase {
7575
* @function
7676
* @name build
7777
*
78-
* @desc Builds the Kernel, by generating the kernel
79-
* string using thread dimensions, and arguments
78+
* @desc Builds the Kernel, by generating the kernel
79+
* string using thread dimensions, and arguments
8080
* supplied to the kernel.
8181
*
8282
* <p>If the graphical flag is enabled, canvas is used.</p>
@@ -87,14 +87,21 @@ module.exports = class CPUKernel extends KernelBase {
8787
this.setupParams(arguments);
8888
this.validateOptions();
8989
const canvas = this._canvas;
90-
this._canvasCtx = canvas.getContext('2d');
90+
if (canvas) {
91+
// if node or canvas is not found, don't die
92+
this._canvasCtx = canvas.getContext('2d');
93+
}
9194
const threadDim = this.threadDim = utils.clone(this.output);
9295

9396
while (threadDim.length < 3) {
9497
threadDim.push(1);
9598
}
9699

97100
if (this.graphical) {
101+
const canvas = this._canvas;
102+
if (!canvas) {
103+
throw new Error('no canvas available for using graphical output');
104+
}
98105
canvas.width = threadDim[0];
99106
canvas.height = threadDim[1];
100107
this._imageData = this._canvasCtx.createImageData(threadDim[0], threadDim[1]);
@@ -144,7 +151,7 @@ module.exports = class CPUKernel extends KernelBase {
144151
* @name getKernelString
145152
*
146153
* @desc Generates kernel string for this kernel program.
147-
*
154+
*
148155
* <p>If sub-kernels are supplied, they are also factored in.
149156
* This string can be saved by calling the `toString` method
150157
* and then can be reused later.</p>

test/all.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,6 @@
8989
<script src="issues/382-bad-constant.js"></script>
9090
<script src="issues/390-thread-assignment.js"></script>
9191
<script src="issues/399-double-definition.js"></script>
92+
<script src="issues/401-cpu-canvas-check.js"></script>
9293
</body>
9394
</html>

test/issues/401-cpu-canvas-check.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
(function() {
2+
QUnit.test('Issue #401 - cpu no canvas graphical', function(assert) {
3+
assert.throws(function() {
4+
GPU.CPUKernel.prototype.build.apply({
5+
setupConstants: function() {},
6+
setupParams: function() {},
7+
validateOptions: function() {},
8+
getKernelString: function() {},
9+
graphical: true,
10+
output: [1],
11+
_canvas: null
12+
}, []);
13+
},
14+
new Error('no canvas available for using graphical output'),
15+
'throws when canvas is not available and using graphical output');
16+
});
17+
18+
QUnit.test('Issue #401 - cpu no canvas', function(assert) {
19+
GPU.CPUKernel.prototype.build.apply({
20+
setupConstants: function() {},
21+
setupParams: function() {},
22+
validateOptions: function() {},
23+
getKernelString: function() {},
24+
graphical: false,
25+
output: [1],
26+
_canvas: null
27+
}, []);
28+
assert.equal(true, true, 'ok when canvas is not available and not using graphical output');
29+
});
30+
})();

0 commit comments

Comments
 (0)