forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel-run-shortcut.js
More file actions
85 lines (82 loc) · 2.4 KB
/
kernel-run-shortcut.js
File metadata and controls
85 lines (82 loc) · 2.4 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
const { utils } = require('./utils');
/**
* Makes kernels easier for mortals (including me)
* @param kernel
* @returns {function()}
*/
function kernelRunShortcut(kernel) {
let run = function() {
kernel.build.apply(kernel, arguments);
run = function() {
let result = kernel.run.apply(kernel, arguments);
if (kernel.switchingKernels) {
const reasons = kernel.resetSwitchingKernels();
const newKernel = kernel.onRequestSwitchKernel(reasons, arguments, kernel);
shortcut.kernel = kernel = newKernel;
result = newKernel.run.apply(newKernel, arguments);
}
if (kernel.renderKernels) {
return kernel.renderKernels();
} else if (kernel.renderOutput) {
return kernel.renderOutput();
} else {
return result;
}
};
return run.apply(kernel, arguments);
};
const shortcut = function() {
return run.apply(kernel, arguments);
};
/**
* Run kernel in async mode
* @returns {Promise<KernelOutput>}
*/
shortcut.exec = function() {
return new Promise((accept, reject) => {
try {
accept(run.apply(this, arguments));
} catch (e) {
reject(e);
}
});
};
shortcut.replaceKernel = function(replacementKernel) {
kernel = replacementKernel;
bindKernelToShortcut(kernel, shortcut);
};
bindKernelToShortcut(kernel, shortcut);
return shortcut;
}
function bindKernelToShortcut(kernel, shortcut) {
if (shortcut.kernel) {
shortcut.kernel = kernel;
return;
}
const properties = utils.allPropertiesOf(kernel);
for (let i = 0; i < properties.length; i++) {
const property = properties[i];
if (property[0] === '_' && property[1] === '_') continue;
if (typeof kernel[property] === 'function') {
if (property.substring(0, 3) === 'add' || property.substring(0, 3) === 'set') {
shortcut[property] = function() {
shortcut.kernel[property].apply(shortcut.kernel, arguments);
return shortcut;
};
} else {
shortcut[property] = function() {
return shortcut.kernel[property].apply(shortcut.kernel, arguments);
};
}
} else {
shortcut.__defineGetter__(property, () => shortcut.kernel[property]);
shortcut.__defineSetter__(property, (value) => {
shortcut.kernel[property] = value;
});
}
}
shortcut.kernel = kernel;
}
module.exports = {
kernelRunShortcut
};