Skip to content

Commit 0a98fef

Browse files
authored
Merge pull request #2 from gpujs/chore/modernize-and-security
chore: Fix 49 vulnerabilities, repair two crash bugs, restore CI
2 parents 280f5d3 + 4964aae commit 0a98fef

9 files changed

Lines changed: 4562 additions & 28 deletions

File tree

‎.github/workflows/test.yml‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: test
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
workflow_dispatch:
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
test:
14+
runs-on: ubuntu-latest
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
# 18.x is the floor declared in package.json engines.node
19+
node-version: ['18.x', '20.x', '22.x', '24.x']
20+
name: node ${{ matrix.node-version }}
21+
steps:
22+
- uses: actions/checkout@v4
23+
24+
- uses: actions/setup-node@v4
25+
with:
26+
node-version: ${{ matrix.node-version }}
27+
cache: npm
28+
29+
- run: npm ci
30+
31+
- run: npm test
32+
33+
# NOTE: example.js is deliberately not run here. Its final step needs gpu.js,
34+
# whose `gl` dependency is a native module requiring system GL headers on the
35+
# runner, which makes the job fail for reasons unrelated to this package.

‎.gitignore‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules/
2+
coverage/
3+
*.log
4+
.DS_Store

‎.travis.yml‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎README.md‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Matrix Log
22

3+
[![test](https://github.com/gpujs/matrix-log.js/actions/workflows/test.yml/badge.svg)](https://github.com/gpujs/matrix-log.js/actions/workflows/test.yml)
4+
35
A matrix log dependency utility. Useful for converting and testing algorithm behavior of CPU code to GPU code.
46

57
## Why?
@@ -17,9 +19,16 @@ Don't understand?
1719
2. Think of the Leonardo 2's cannons talking with each other to build up their values and how this violates the laws of physics.
1820
3. Have eureka moment.
1921

22+
## Installation
23+
```bash
24+
npm install matrix-log.js
25+
```
26+
2027
## API
2128
```js
22-
import * as MatrixLog from 'matrix-log';
29+
const MatrixLog = require('matrix-log.js');
30+
// or, from an ES module:
31+
// import MatrixLog from 'matrix-log.js';
2332

2433

2534
// create a matrix log that will have dependencies called
@@ -152,7 +161,7 @@ If you prefer to learn by code rather than have step by step instructions take a
152161
for (let y = 0; y < 4; y++) {
153162
let filterY = y < 2 ? 0 : 1;
154163
for (let x = 0; x < 4; x++) {
155-
let filterX = x < filter.length ? 0 : 1;
164+
let filterX = x < 2 ? 0 : 1;
156165
filters[filterY][filterX] += weights[y][x];
157166
}
158167
}
@@ -181,14 +190,17 @@ If you prefer to learn by code rather than have step by step instructions take a
181190
for (let y = 0; y < 4; y++) {
182191
let filterY = y < 2 ? 0 : 1;
183192
for (let x = 0; x < 4; x++) {
184-
let filterX = x < filter.length ? 0 : 1;
193+
let filterX = x < 2 ? 0 : 1;
185194
filters[filterY][filterX] += weights[y][x];
186195
matrixLog
187-
.at({ x, y })
196+
.at({
197+
x: filterX,
198+
y: filterY
199+
})
188200
.add({
189201
name: 'weights',
190-
x: filterX,
191-
y: filterY,
202+
x,
203+
y,
192204
width: weights[0].length,
193205
height: weights.length
194206
});
@@ -293,7 +305,7 @@ If you prefer to learn by code rather than have step by step instructions take a
293305
5. If we use a GPU environment, such as GPU.js, we could then then convert the kernel so that our algorithm actually works for setting the value of `filters` like this:
294306

295307
```js
296-
import GPU from 'gpu.js';
308+
import { GPU } from 'gpu.js';
297309
const gpu = new GPU();
298310
299311
const filterKernel = gpu.createKernel(function(filters, weights) {

‎__tests__/test.js‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,44 @@ describe('MatrixLog', () => {
7272
expect(location.highY).toBe(childY);
7373
expect(location.points[childZ][childY].indexOf(childX)).toBe(0);
7474
});
75+
it('can add below a child that is wider than it is tall', () => {
76+
// regression: highY was previously compared against highX, so a child
77+
// whose width exceeded its height never grew its lower bound and threw
78+
const matrixLog = new MatrixLog('test-matrix', 1, 1);
79+
matrixLog.at({ x: 0, y: 0 })
80+
.add({
81+
name:'child-matrix',
82+
x: 0,
83+
y: 3,
84+
width: 6,
85+
height: 2
86+
});
87+
const location = matrixLog.locations[0][0][0]['child-matrix'];
88+
expect(location.lowY).toBe(0);
89+
expect(location.highY).toBe(3);
90+
expect(location.lowX).toBe(0);
91+
expect(location.highX).toBe(5);
92+
expect(location.points[0][3].indexOf(0)).toBe(0);
93+
});
94+
it('can add repeatedly above a child without losing points', () => {
95+
// regression: only the call that extended a bound created its row, so a
96+
// second point on an already extended row threw
97+
const matrixLog = new MatrixLog('test-matrix', 1, 1);
98+
const child = {
99+
name:'child-matrix',
100+
width: 2,
101+
height: 6
102+
};
103+
matrixLog.at({ x: 0, y: 0 })
104+
.add(Object.assign({}, child, { x: 0, y: -3 }))
105+
.add(Object.assign({}, child, { x: 1, y: -2 }))
106+
.add(Object.assign({}, child, { x: 0, y: -2 }));
107+
const location = matrixLog.locations[0][0][0]['child-matrix'];
108+
expect(location.lowY).toBe(-3);
109+
expect(location.highY).toBe(5);
110+
expect(location.points[0][-3]).toEqual([0]);
111+
expect(location.points[0][-2]).toEqual([1, 0]);
112+
});
75113
});
76114
describe('.toString()', () => {
77115
it('repeats for every point logged', () => {

‎example.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const targetValue = step1CPU();
1212
assert.deepStrictEqual(step2CPUWithMatrixLog(), targetValue);
1313
assert.deepStrictEqual(step3GPUStyleLoops(), targetValue);
1414
assert.deepStrictEqual(step4GPUStyleKernel(), targetValue);
15-
assert.deepStrictEqual(step5GPUKernel(), targetValue);
15+
// a gpu.js kernel returns Float32Array rows, so normalise them before comparing
16+
assert.deepStrictEqual(step5GPUKernel().map(row => Array.from(row)), targetValue);
1617

1718
function step1CPU() {
1819
const filters = [
@@ -134,7 +135,7 @@ function step5GPUKernel() {
134135
[0,0],
135136
[0,0]
136137
];
137-
const GPU = require('gpu.js');
138+
const { GPU } = require('gpu.js');
138139
const gpu = new GPU();
139140

140141
const filterKernel = gpu.createKernel(function (filters, weights) {

‎index.js‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,8 @@ class MatrixLog {
9595
// handle negative and positive
9696
if (y < location.lowY) {
9797
location.lowY = y;
98-
points[z][y] = [];
99-
} else if (y > location.highX) {
98+
} else if (y > location.highY) {
10099
location.highY = y;
101-
points[z][y] = [];
102100
}
103101

104102
if (x < location.lowX) {
@@ -107,6 +105,11 @@ class MatrixLog {
107105
location.highX = x;
108106
}
109107

108+
// rows outside the child's declared height are created on demand
109+
if (!points[z][y]) {
110+
points[z][y] = [];
111+
}
112+
110113
points[z][y].push(x);
111114
return this;
112115
}

0 commit comments

Comments
 (0)