Fix compiler program de-duping

This commit is contained in:
kpdecker
2014-08-14 12:27:39 -05:00
parent b94656a31f
commit 9e3f824bf5
+19 -9
View File
@@ -1,4 +1,5 @@
import Exception from "../exception"; import Exception from "../exception";
import {isArray} from "../utils";
export function Compiler() {} export function Compiler() {}
@@ -19,20 +20,14 @@ Compiler.prototype = {
for (var i = 0; i < len; i++) { for (var i = 0; i < len; i++) {
var opcode = this.opcodes[i], var opcode = this.opcodes[i],
otherOpcode = other.opcodes[i]; otherOpcode = other.opcodes[i];
if (opcode.opcode !== otherOpcode.opcode || opcode.args.length !== otherOpcode.args.length) { if (opcode.opcode !== otherOpcode.opcode || !argEquals(opcode.args, otherOpcode.args)) {
return false; return false;
} }
for (var j = 0; j < opcode.args.length; j++) {
if (opcode.args[j] !== otherOpcode.args[j]) {
return false;
}
}
} }
// We know that length is the same between the two arrays because they are directly tied
// to the opcode behavior above.
len = this.children.length; len = this.children.length;
if (other.children.length !== len) {
return false;
}
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
if (!this.children[i].equals(other.children[i])) { if (!this.children[i].equals(other.children[i])) {
return false; return false;
@@ -449,3 +444,18 @@ export function compile(input, options, env) {
}; };
return ret; return ret;
} }
function argEquals(a, b) {
if (a === b) {
return true;
}
if (isArray(a) && isArray(b) && a.length === b.length) {
for (var i = 0; i < a.length; i++) {
if (!argEquals(a[i], b[i])) {
return false;
}
}
return true;
}
}