minor optimizations/fixes
This commit is contained in:
3 files changed
+24
-26
No files matched your search
@@ -103,7 +103,7 @@ const multiply = /** @param {InputUtil} util */ (util) => {
|
||||
const divide = /** @param {InputUtil} util */ (util) => {
|
||||
const NUM1 = util.input('NUM1');
|
||||
const NUM2 = util.input('NUM2');
|
||||
return util.number(`(${NUM1.asNumber()} / ${NUM2.asNumber()})`).nanable();
|
||||
return util.number(`(${NUM1.asNumber()} / ${NUM2.asNumber()})`).setFlag(util.FLAG_NANABLE);
|
||||
};
|
||||
|
||||
const mathop = /** @param {InputUtil} util */ (util) => {
|
||||
@@ -113,7 +113,7 @@ const mathop = /** @param {InputUtil} util */ (util) => {
|
||||
case 'abs': return util.number(`Math.abs(${NUM})`);
|
||||
case 'floor': return util.number(`Math.floor(${NUM})`);
|
||||
case 'ceiling': return util.number(`Math.ceil(${NUM})`);
|
||||
case 'sqrt': return util.number(`Math.sqrt(${NUM})`).nanable();
|
||||
case 'sqrt': return util.number(`Math.sqrt(${NUM})`).setFlag(util.FLAG_NANABLE);
|
||||
// TODO: consider rounding as Scratch 3 does
|
||||
case 'sin': return util.number(`Math.sin((Math.PI * ${NUM}) / 180)`);
|
||||
case 'cos': return util.number(`Math.cos((Math.PI * ${NUM}) / 180)`);
|
||||
|
||||
+14
-16
@@ -28,8 +28,7 @@ const TYPE_NUMBER = 1;
|
||||
const TYPE_BOOLEAN = 2;
|
||||
const TYPE_STRING = 3;
|
||||
|
||||
const FLAG_NULLABLE = 1;
|
||||
const FLAG_NANABLE = 2;
|
||||
const FLAG_NANABLE = 1;
|
||||
|
||||
defaultExtensions.forEach((ext) => {
|
||||
const extensionInputs = ext.getInputs();
|
||||
@@ -49,8 +48,8 @@ defaultExtensions.forEach((ext) => {
|
||||
|
||||
/**
|
||||
* Prevents the use of toString() on an object by throwing an error.
|
||||
* Useful to make sure that a method is always called, never stringified.
|
||||
* @param {Object} obj
|
||||
* Useful to make sure that a method is always called, never accidentally stringified.
|
||||
* @param {Object} obj
|
||||
*/
|
||||
const disableToString = (obj) => {
|
||||
obj.toString = () => {
|
||||
@@ -67,6 +66,13 @@ class BlockUtil {
|
||||
this.block = block;
|
||||
}
|
||||
|
||||
// Expose some constants that are likely to be of use to blocks.
|
||||
get TYPE_UNKNOWN() { return TYPE_UNKNOWN; }
|
||||
get TYPE_NUMBER() { return TYPE_NUMBER; }
|
||||
get TYPE_BOOLEAN() { return TYPE_BOOLEAN; }
|
||||
get TYPE_STRING() { return TYPE_STRING; }
|
||||
get FLAG_NANABLE() { return FLAG_NANABLE; }
|
||||
|
||||
get target() {
|
||||
return this.compiler.target;
|
||||
}
|
||||
@@ -281,7 +287,6 @@ class CompiledInput {
|
||||
/**
|
||||
* The input's type.
|
||||
* @readonly
|
||||
* @private
|
||||
*/
|
||||
this.type = type;
|
||||
/**
|
||||
@@ -292,18 +297,11 @@ class CompiledInput {
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable the NULLABLE flag.
|
||||
* Enable a flag.
|
||||
* @param {number} flag The value of the flag to enable.
|
||||
*/
|
||||
nullable() {
|
||||
this.flags |= FLAG_NULLABLE;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable the NANABLE flag.
|
||||
*/
|
||||
nanable() {
|
||||
this.flags |= FLAG_NANABLE;
|
||||
setFlag(flag) {
|
||||
this.flags |= flag;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -123,8 +123,7 @@ const retire = () => {
|
||||
* @returns {number}
|
||||
*/
|
||||
const toNumber = (value) => {
|
||||
// The only falsey values that Number can return is 0 and NaN, both of which are treated as 0.
|
||||
return Number(value) || 0;
|
||||
return +value || 0;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -152,7 +151,7 @@ const toBoolean = (value) => {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return Boolean(value);
|
||||
return !!value;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -183,16 +182,16 @@ const isWhiteSpace = (val) => {
|
||||
* @returns {number} Negative if v1 < v2, 0 if equal, positive if v1 > v2
|
||||
*/
|
||||
const compare = (v1, v2) => {
|
||||
let n1 = Number(v1);
|
||||
let n2 = Number(v2);
|
||||
let n1 = +v1;
|
||||
let n2 = +v2;
|
||||
if (n1 === 0 && isWhiteSpace(v1)) {
|
||||
n1 = NaN;
|
||||
} else if (n2 === 0 && isWhiteSpace(v2)) {
|
||||
n2 = NaN;
|
||||
}
|
||||
if (isNaN(n1) || isNaN(n2)) {
|
||||
const s1 = String(v1).toLowerCase();
|
||||
const s2 = String(v2).toLowerCase();
|
||||
const s1 = ('' + v1).toLowerCase();
|
||||
const s2 = ('' + v2).toLowerCase();
|
||||
if (s1 < s2) {
|
||||
return -1;
|
||||
} else if (s1 > s2) {
|
||||
@@ -253,8 +252,9 @@ var listIndex = (index, length) => {
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
index = toNumber(index);
|
||||
}
|
||||
index = Math.floor(toNumber(index));
|
||||
index = Math.floor(index);
|
||||
if (index < 1 || index > length) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user