equality testing for scripts, experimental

snap7
jmoenig 2021-12-01 19:51:01 +01:00
rodzic d095d9d961
commit 608027c957
3 zmienionych plików z 25 dodań i 4 usunięć

Wyświetl plik

@ -70,6 +70,7 @@
### 2021-12-01
* blocks, threads: block-assembly support refactorings, experimental
* blocks, threads: equality testing for scripts, experimental
### 2021-11-30
* blocks, threads: block-assembly support, experimental

Wyświetl plik

@ -3710,7 +3710,15 @@ BlockMorph.prototype.components = function () {
throw new Error('subclass responsility');
};
BlockMorph.prototype.equalTo = function (other) {
// private - only to be called from a Context
return this.constructor.name === other.constructor.name &&
this.selector === other.selector &&
this.blockSpec === other.blockSpec;
};
BlockMorph.prototype.copyWithInputs = function (inputs) {
// private - only to be called from a Context
var cpy = this.fullCopy(),
slots = cpy.inputs(),
dta = inputs.itemsArray().map(inp =>
@ -5489,7 +5497,8 @@ CommandBlockMorph.prototype.components = function () {
}
expr.fixBlockColor(null, true);
inputs = expr.inputs();
if (!inputs.length) {
if (!inputs.length ||
inputs.every(slot => slot.isEmptySlot && slot.isEmptySlot())) {
return expr.reify();
}
parts = new List([expr.reify()]);

Wyświetl plik

@ -87,8 +87,9 @@ const NONNUMBERS = [true, false, ''];
})();
function snapEquals(a, b) {
if (a instanceof List || (b instanceof List)) {
if (a instanceof List && (b instanceof List)) {
// lists, functions and blocks
if (a.equalTo || b.equalTo) {
if (a.constructor.name === b.constructor.name) {
return a.equalTo(b);
}
return false;
@ -7012,7 +7013,8 @@ Context.prototype.isInCustomBlock = function () {
// Context components - EXPERIMENTAL
Context.prototype.components = function () {
var expr = this.expression.components(),
var expr = this.expression.components ?
this.expression.components() : new Context(),
parts;
if (!this.inputs.length) {
return expr;
@ -7023,6 +7025,15 @@ Context.prototype.components = function () {
return parts;
};
Context.prototype.equalTo = function (other) {
var c1 = this.components(),
c2 = other.components();
if (c1 instanceof Context && (c2 instanceof Context)) {
return snapEquals(c1.expression, c2.expression);
}
return snapEquals(c1, c2);
};
Context.prototype.copyWithInputs = function (inputs) {
return this.expression.copyWithInputs(inputs);
};