mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-22 22:12:28 -05:00
Revert changes to toNumber, fix comparison of infinity in cast.compare
This commit is contained in:
parent
dd81a261df
commit
70f00e30c4
2 changed files with 21 additions and 50 deletions
|
@ -38,14 +38,7 @@ class Cast {
|
|||
}
|
||||
return value;
|
||||
}
|
||||
let n;
|
||||
if (Cast.isInfinity(value)) {
|
||||
n = Infinity;
|
||||
} else if (Cast.isNegativeInfinity(value)) {
|
||||
n = -Infinity;
|
||||
} else {
|
||||
n = Number(value);
|
||||
}
|
||||
const n = Number(value);
|
||||
if (_NumberIsNaN(n)) {
|
||||
// Scratch treats NaN as 0, when needed as a number.
|
||||
// E.g., 0 + NaN -> 0.
|
||||
|
@ -123,24 +116,6 @@ class Cast {
|
|||
return val === null || (typeof val === 'string' && val.trim().length === 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a Scratch argument is infinity.
|
||||
* @param {*} val value to check.
|
||||
* @return {boolean} True if the argument is any capitalization of infinity.
|
||||
*/
|
||||
static isInfinity (val) {
|
||||
return val === Infinity || (typeof val === 'string' && val.toLowerCase() === 'infinity');
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a Scratch argument is negative infinity.
|
||||
* @param {*} val value to check.
|
||||
* @return {boolean} True if the argument is a '-' followed by any capitalization of infinity.
|
||||
*/
|
||||
static isNegativeInfinity (val) {
|
||||
return val === -Infinity || (typeof val === 'string' && val.toLowerCase() === '-infinity');
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare two values, using Scratch cast, case-insensitive string compare, etc.
|
||||
* In Scratch 2.0, this is captured by `interp.compare.`
|
||||
|
@ -149,10 +124,8 @@ class Cast {
|
|||
* @returns {number} Negative number if v1 < v2; 0 if equal; positive otherwise.
|
||||
*/
|
||||
static compare (v1, v2) {
|
||||
// If Cast.toNumber returns '0', then the value might be NaN. If the value is NaN,
|
||||
// this comparison algorithm needs to know.
|
||||
let n1 = Cast.toNumber(v1) || Number(v1);
|
||||
let n2 = Cast.toNumber(v2) || Number(v2);
|
||||
let n1 = Number(v1);
|
||||
let n2 = Number(v2);
|
||||
if (n1 === 0 && Cast.isWhiteSpace(v1)) {
|
||||
n1 = NaN;
|
||||
} else if (n2 === 0 && Cast.isWhiteSpace(v2)) {
|
||||
|
@ -170,17 +143,15 @@ class Cast {
|
|||
}
|
||||
return 0;
|
||||
}
|
||||
// Handle the special case of Infinity
|
||||
if (
|
||||
(n1 === Infinity && n2 === Infinity)
|
||||
|| (n1 === -Infinity && n2 === -Infinity)
|
||||
) {
|
||||
return 0;
|
||||
}
|
||||
// Compare as numbers.
|
||||
const r = n1 - n2;
|
||||
if (isNaN(r)) {
|
||||
if (n1 === Infinity && n2 === Infinity) {
|
||||
return 0;
|
||||
}
|
||||
if (n1 === -Infinity && n2 === -Infinity) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
return n1 - n2;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -16,7 +16,7 @@ test('divide: division with Infinity', t => {
|
|||
blocks.divide({NUM1: 'Infinity', NUM2: 111}), Infinity, '"Infinity" / 111 = Infinity'
|
||||
);
|
||||
t.strictEqual(
|
||||
blocks.divide({NUM1: 'INFINITY', NUM2: 222}), Infinity, '"INFINITY" / 222 = Infinity'
|
||||
blocks.divide({NUM1: 'INFINITY', NUM2: 222}), 0, '"INFINITY" / 222 = 0'
|
||||
);
|
||||
t.strictEqual(
|
||||
blocks.divide({NUM1: Infinity, NUM2: 333}), Infinity, 'Infinity / 333 = Infinity'
|
||||
|
@ -26,7 +26,7 @@ test('divide: division with Infinity', t => {
|
|||
blocks.divide({NUM1: 111, NUM2: 'Infinity'}), 0, '111 / "Infinity" = 0'
|
||||
);
|
||||
t.strictEqual(
|
||||
blocks.divide({NUM1: 222, NUM2: 'INFINITY'}), 0, '222 / "INFINITY" = 0'
|
||||
blocks.divide({NUM1: 222, NUM2: 'INFINITY'}), Infinity, '222 / "INFINITY" = Infinity'
|
||||
);
|
||||
t.strictEqual(
|
||||
blocks.divide({NUM1: 333, NUM2: Infinity}), 0, '333 / Infinity = 0'
|
||||
|
@ -36,7 +36,7 @@ test('divide: division with Infinity', t => {
|
|||
blocks.divide({NUM1: '-Infinity', NUM2: 111}), -Infinity, '"-Infinity" / 111 = -Infinity'
|
||||
);
|
||||
t.strictEqual(
|
||||
blocks.divide({NUM1: '-INFINITY', NUM2: 222}), -Infinity, '"-INFINITY" / 222 = -Infinity'
|
||||
blocks.divide({NUM1: '-INFINITY', NUM2: 222}), 0, '"-INFINITY" / 222 = 0'
|
||||
);
|
||||
t.strictEqual(
|
||||
blocks.divide({NUM1: -Infinity, NUM2: 333}), -Infinity, '-Infinity / 333 = -Infinity'
|
||||
|
@ -46,7 +46,7 @@ test('divide: division with Infinity', t => {
|
|||
blocks.divide({NUM1: 111, NUM2: '-Infinity'}), 0, '111 / "-Infinity" = 0'
|
||||
);
|
||||
t.strictEqual(
|
||||
blocks.divide({NUM1: 222, NUM2: '-INFINITY'}), 0, '222 / "-INFINITY" = 0'
|
||||
blocks.divide({NUM1: 222, NUM2: '-INFINITY'}), Infinity, '222 / "-INFINITY" = Infinity'
|
||||
);
|
||||
t.strictEqual(
|
||||
blocks.divide({NUM1: 333, NUM2: -Infinity}), 0, '333 / -Infinity = 0'
|
||||
|
@ -60,7 +60,7 @@ test('multiply: multiply Infinity with numbers', t => {
|
|||
blocks.multiply({NUM1: 'Infinity', NUM2: 111}), Infinity, '"Infinity" * 111 = Infinity'
|
||||
);
|
||||
t.strictEqual(
|
||||
blocks.multiply({NUM1: 'INFINITY', NUM2: 222}), Infinity, '"INFINITY" * 222 = Infinity'
|
||||
blocks.multiply({NUM1: 'INFINITY', NUM2: 222}), 0, '"INFINITY" * 222 = 0'
|
||||
);
|
||||
t.strictEqual(
|
||||
blocks.multiply({NUM1: Infinity, NUM2: 333}), Infinity, 'Infinity * 333 = Infinity'
|
||||
|
@ -69,7 +69,7 @@ test('multiply: multiply Infinity with numbers', t => {
|
|||
blocks.multiply({NUM1: '-Infinity', NUM2: 111}), -Infinity, '"-Infinity" * 111 = -Infinity'
|
||||
);
|
||||
t.strictEqual(
|
||||
blocks.multiply({NUM1: '-INFINITY', NUM2: 222}), -Infinity, '"-INFINITY" * 222 = -Infinity'
|
||||
blocks.multiply({NUM1: '-INFINITY', NUM2: 222}), 0, '"-INFINITY" * 222 = 0'
|
||||
);
|
||||
t.strictEqual(
|
||||
blocks.multiply({NUM1: -Infinity, NUM2: 333}), -Infinity, '-Infinity * 333 = -Infinity'
|
||||
|
@ -90,7 +90,7 @@ test('add: add Infinity to a number', t => {
|
|||
blocks.add({NUM1: 'Infinity', NUM2: 111}), Infinity, '"Infinity" + 111 = Infinity'
|
||||
);
|
||||
t.strictEqual(
|
||||
blocks.add({NUM1: 'INFINITY', NUM2: 222}), Infinity, '"INFINITY" + 222 = Infinity'
|
||||
blocks.add({NUM1: 'INFINITY', NUM2: 222}), 222, '"INFINITY" + 222 = 222'
|
||||
);
|
||||
t.strictEqual(
|
||||
blocks.add({NUM1: Infinity, NUM2: 333}), Infinity, 'Infinity + 333 = Infinity'
|
||||
|
@ -99,7 +99,7 @@ test('add: add Infinity to a number', t => {
|
|||
blocks.add({NUM1: '-Infinity', NUM2: 111}), -Infinity, '"-Infinity" + 111 = -Infinity'
|
||||
);
|
||||
t.strictEqual(
|
||||
blocks.add({NUM1: '-INFINITY', NUM2: 222}), -Infinity, '"-INFINITY" + 222 = -Infinity'
|
||||
blocks.add({NUM1: '-INFINITY', NUM2: 222}), 222, '"-INFINITY" + 222 = 222'
|
||||
);
|
||||
t.strictEqual(
|
||||
blocks.add({NUM1: -Infinity, NUM2: 333}), -Infinity, '-Infinity + 333 = -Infinity'
|
||||
|
@ -117,7 +117,7 @@ test('subtract: subtract Infinity with a number', t => {
|
|||
blocks.subtract({NUM1: 'Infinity', NUM2: 111}), Infinity, '"Infinity" - 111 = Infinity'
|
||||
);
|
||||
t.strictEqual(
|
||||
blocks.subtract({NUM1: 'INFINITY', NUM2: 222}), Infinity, '"INFINITY" - 222 = Infinity'
|
||||
blocks.subtract({NUM1: 'INFINITY', NUM2: 222}), -222, '"INFINITY" - 222 = -222'
|
||||
);
|
||||
t.strictEqual(
|
||||
blocks.subtract({NUM1: Infinity, NUM2: 333}), Infinity, 'Infinity - 333 = Infinity'
|
||||
|
@ -126,7 +126,7 @@ test('subtract: subtract Infinity with a number', t => {
|
|||
blocks.subtract({NUM1: 111, NUM2: 'Infinity'}), -Infinity, '111 - "Infinity" = -Infinity'
|
||||
);
|
||||
t.strictEqual(
|
||||
blocks.subtract({NUM1: 222, NUM2: 'INFINITY'}), -Infinity, '222 - "INFINITY" = -Infinity'
|
||||
blocks.subtract({NUM1: 222, NUM2: 'INFINITY'}), 222, '222 - "INFINITY" = 222'
|
||||
);
|
||||
t.strictEqual(
|
||||
blocks.subtract({NUM1: 333, NUM2: Infinity}), -Infinity, '333 - Infinity = -Infinity'
|
||||
|
|
Loading…
Reference in a new issue