Skip to content

Commit

Permalink
fix(playcanvas): hijackArray working fine
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTelanie committed Sep 9, 2021
1 parent 047145e commit ba17b26
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 18 deletions.
14 changes: 9 additions & 5 deletions src/adapter/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export const hijackArray = (ArrayClass) => {
},
set(x) {
this[0] = x;
}
},
configurable: true
});

Object.defineProperty(prototype, 'y', {
Expand All @@ -18,7 +19,8 @@ export const hijackArray = (ArrayClass) => {
},
set(y) {
this[1] = y;
}
},
configurable: true
});

Object.defineProperty(prototype, 'z', {
Expand All @@ -27,17 +29,19 @@ export const hijackArray = (ArrayClass) => {
},
set(z) {
this[2] = z;
}
},
configurable: true
});

cachedValueOf(ArrayClass);

Object.defineProperty(prototype, 'len', {
get() {
return Math.sqrt((this[0] ** 2) + (this[1] ** 2) + (this[2] ** 2)) ** (1 / 2);
return ((this[0] ** 2) + (this[1] ** 2) + (this[2] ** 2)) ** (1 / 2);
},
set() {
throw new Error('set len not allowed');
}
},
configurable: true
});
};
3 changes: 2 additions & 1 deletion src/adapter/playcanvas.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// @ts-nocheck
import {
operatorCalc, cachedValueOf, defineVectorLength, cachedFactory, cachedFunction, defineMatrixLength
} from '../operator';
import {
multiplyMat3Vec, multiplyMat3Mat3, multiplyVecMat3, isNumber, multiplyVecMat4
} from '../utils/math';

export { hijackArray } from './array';

export function hijackPlayCanvas(pc) {
const {
Vec2, Vec3, Vec4, Quat, Mat3: AMat3, Mat4: AMat4, math
Expand Down
1 change: 0 additions & 1 deletion src/array.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-nocheck
import { cachedValueOf } from './operator';
import { isArray } from './utils/math';

Expand Down
1 change: 0 additions & 1 deletion src/color.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-nocheck
import { isArray, isNumber } from './utils/math';
import {
cachedFunction, cachedMethod, cachedValueOf, defineVectorLength, operatorCalc
Expand Down
1 change: 0 additions & 1 deletion src/degree.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-nocheck
import { normRad, isNumber } from './utils/math';
import { convertToCSSVars } from './utils/css';

Expand Down
2 changes: 1 addition & 1 deletion src/formatter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// https://code.open-xchange.com/#contentPanel;a7550a02-aa41-45d9-9cad-7e1c920815f3;null;ui/apps/io.ox/office/tk/utils.js;content
// @ts-nocheck

const DECIMAL = (0.5).toLocaleString().substring(1, 2) || '.';
const ZERO = (0).toLocaleString() || '0';

Expand Down
1 change: 0 additions & 1 deletion src/operator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-nocheck
import { isNumber } from './utils/math';

const X = 0;
Expand Down
1 change: 0 additions & 1 deletion src/point.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-nocheck
import { isArray, normRad, isNumber } from './utils/math';
import {
cachedFunction, cachedGetter, cachedMethod, cachedValueOf, defineVectorLength, operatorCalc
Expand Down
5 changes: 2 additions & 3 deletions src/quaternion.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// @ts-nocheck
import {
FORWARD, LEFT, RIGHT, UP, Vector, Victor
FORWARD, LEFT, RIGHT, UP
} from './vector';
import { isArray, multQuatVec, isNumber } from './utils/math';
import {
cachedFunction, defineMatrixLength, cachedValueOf, operatorCalc as calc
cachedFunction, defineMatrixLength, cachedValueOf
} from './operator';
import { degree, isAngle } from './degree';
import { convertToCSSVars } from './utils/css';
Expand Down
1 change: 0 additions & 1 deletion src/vector.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-nocheck
import {
acos, isArray, multQuatVec, normRad, multiplyVecMat3, isNumber
} from './utils/math';
Expand Down
17 changes: 15 additions & 2 deletions test/adapter/playcanvas.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assert } from 'chai';
import { hijackPlayCanvas } from '../../src/adapter/playcanvas';
import { hijackArray, hijackPlayCanvas } from '../../src/adapter/playcanvas';

class Vec2 {
constructor(x, y) {
Expand Down Expand Up @@ -43,7 +43,7 @@ const pc = {
math
};

describe('override valueOf of playcanvas Vec3', () => {
describe('override valueOf of playcanvas vector classes', () => {
it('calculations with the new class', () => {
hijackPlayCanvas(pc);

Expand All @@ -56,4 +56,17 @@ describe('override valueOf of playcanvas Vec3', () => {
assert.equal(pos.y, 18);
assert.equal(pos.z, 21);
});

it('calculations with Array class', () => {
hijackArray(Array);

const t1 = [3, 4, 5];
const t2 = [6, 7, 8];
const pos = pc.calc(() => t1 + t2 * 2);

assert.instanceOf(pos, Array);
assert.equal(pos.x, 15);
assert.equal(pos.y, 18);
assert.equal(pos.z, 21);
});
});

0 comments on commit ba17b26

Please sign in to comment.