Rhino 1.5 R5在查找数组差异时未正确评估JavaScript

Rhino 1.5 R5 not evaluating JavaScript correctly when finding array differences

本文关键字:JavaScript 评估 R5 数组 查找 Rhino      更新时间:2024-02-18

我遇到了一些JavaScript问题,该问题正在使用Rhino 1.5 R5的应用程序中进行评估(Rhino 1.5 release 5 2004 03 25)。我无法独立更新Rhino。

我有两个数组:原始数据(pastedArr)和一个只有有效值的数组(validatedArr),我正在努力找出差异("坏"值)。我在网上找到了一个函数来帮助(sym),我不得不为Each制作原型,但它在jsfiddle中运行得很好(https://jsfiddle.net/zr3abc0c/)。

在应用程序中,使用Rhino,它不会返回整数元素(即使它们存储为字符串),就像下面的脚本和fiddle链接中一样,该链接在jsfiddle上返回["33"],但在我的应用程序中为null。如果参数0是["33"、"1A"、"ABC"],则在应用程序中只返回["1A"answers"ABC"。

我不知道这些"integer"字符串元素在哪里失败了。

// Production steps of ECMA-262, Edition 5, 15.4.4.18
// Reference: http://es5.github.io/#x15.4.4.18
if (!Array.prototype.forEach) {
  Array.prototype.forEach = function(callback, thisArg) {
    var T, k;
    if (this == null) {
      throw new TypeError(' this is null or not defined');
    }
    // 1. Let O be the result of calling toObject() passing the
    // |this| value as the argument.
    var O = Object(this);
    // 2. Let lenValue be the result of calling the Get() internal
    // method of O with the argument "length".
    // 3. Let len be toUint32(lenValue).
    var len = O.length >>> 0;
    // 4. If isCallable(callback) is false, throw a TypeError exception.
    // See: http://es5.github.com/#x9.11
    if (typeof callback !== "function") {
      throw new TypeError(callback + ' is not a function');
    }
    // 5. If thisArg was supplied, let T be thisArg; else let
    // T be undefined.
    if (arguments.length > 1) {
      T = thisArg;
    }
    // 6. Let k be 0
    k = 0;
    // 7. Repeat, while k < len
    while (k < len) {
      var kValue;
      // a. Let Pk be ToString(k).
      //    This is implicit for LHS operands of the in operator
      // b. Let kPresent be the result of calling the HasProperty
      //    internal method of O with argument Pk.
      //    This step can be combined with c
      // c. If kPresent is true, then
      if (k in O) {
        // i. Let kValue be the result of calling the Get internal
        // method of O with argument Pk.
        kValue = O[k];
        // ii. Call the Call internal method of callback with T as
        // the this value and argument list containing kValue, k, and O.
        callback.call(T, kValue, k, O);
      }
      // d. Increase k by 1.
      k++;
    }
    // 8. return undefined
  };
}
function sym( /* pass one or more arrays here */ ) {
  var ans = [],
    cnts = {},
    currentMap;
  //count all items in the array
  for (var i = 0; i < arguments.length; i++) {
    currentMap = {};
    arguments[i].forEach(function(item) {
      // if we haven't already counted this item in this array
      if (!currentMap.hasOwnProperty(item)) {
        if (cnts.hasOwnProperty(item)) {
          // increase cnt
          ++cnts[item].cnt;
        } else {
          // initalize cnt and value
          cnts[item] = {
            cnt: 1,
            val: item
          };
        }
      }
      // keep track of whethere we've already counted this item in this array
      currentMap[item] = true;
    });
  }
  // output all items that have a cnt of 1
  for (var item in cnts) {
    if (cnts.hasOwnProperty(item) && cnts[item].cnt === 1) {
      ans.push(cnts[item].val);
    }
  }
  return ans;
}
function sort_uniq_fast(a) {
  var seen = {};
  var out = [];
  var len = a.length;
  var j = 0;
  for (var i = 0; i < len; i++) {
    var item = a[i];
    if (seen[item] !== 1) {
      seen[item] = 1;
      out[j++] = item;
    }
  }
  return out.sort();
}
function getBadCodes(pastedArr, validatedArr) {
  var result = sym(pastedArr, validatedArr);
  return sort_uniq_fast(result);
}
   var parameter0 = ["33"];
   var parameter1 = new Array();
alert(getBadCodes(parameter0,parameter1));

我没有Rhino 1.5R5,但Rhino将整数属性视为特殊属性,因为它们在Array中使用。我推测Rhino 1.5R5中的这种处理方式存在错误。

我建议对项目进行预处理以进行比较,并对结果进行后处理,就像我在这个答案中所做的那样(请参阅前缀和取消前缀)。

让我们知道它是否有效!(我会相应地更新答案。)

// Production steps of ECMA-262, Edition 5, 15.4.4.18
// Reference: http://es5.github.io/#x15.4.4.18
if (!Array.prototype.forEach) {
  Array.prototype.forEach = function(callback, thisArg) {
    var T, k;
    if (this == null) {
      throw new TypeError(' this is null or not defined');
    }
    // 1. Let O be the result of calling toObject() passing the
    // |this| value as the argument.
    var O = Object(this);
    // 2. Let lenValue be the result of calling the Get() internal
    // method of O with the argument "length".
    // 3. Let len be toUint32(lenValue).
    var len = O.length >>> 0;
    // 4. If isCallable(callback) is false, throw a TypeError exception.
    // See: http://es5.github.com/#x9.11
    if (typeof callback !== "function") {
      throw new TypeError(callback + ' is not a function');
    }
    // 5. If thisArg was supplied, let T be thisArg; else let
    // T be undefined.
    if (arguments.length > 1) {
      T = thisArg;
    }
    // 6. Let k be 0
    k = 0;
    // 7. Repeat, while k < len
    while (k < len) {
      var kValue;
      // a. Let Pk be ToString(k).
      //    This is implicit for LHS operands of the in operator
      // b. Let kPresent be the result of calling the HasProperty
      //    internal method of O with argument Pk.
      //    This step can be combined with c
      // c. If kPresent is true, then
      if (k in O) {
        // i. Let kValue be the result of calling the Get internal
        // method of O with argument Pk.
        kValue = O[k];
        // ii. Call the Call internal method of callback with T as
        // the this value and argument list containing kValue, k, and O.
        callback.call(T, kValue, k, O);
      }
      // d. Increase k by 1.
      k++;
    }
    // 8. return undefined
  };
}
function sym( /* pass one or more arrays here */ ) {
  var ans = [],
    cnts = {},
    currentMap;
  //count all items in the array
  for (var i = 0; i < arguments.length; i++) {
    currentMap = {};
    arguments[i].forEach(function(item) {
      // if we haven't already counted this item in this array
      if (!currentMap.hasOwnProperty(item)) {
        if (cnts.hasOwnProperty(item)) {
          // increase cnt
          ++cnts[item].cnt;
        } else {
          // initalize cnt and value
          cnts[item] = {
            cnt: 1,
            val: item
          };
        }
      }
      // keep track of whethere we've already counted this item in this array
      currentMap[item] = true;
    });
  }
  // output all items that have a cnt of 1
  for (var item in cnts) {
    if (cnts.hasOwnProperty(item) && cnts[item].cnt === 1) {
      ans.push(cnts[item].val);
    }
  }
  return ans;
}
function sort_uniq_fast(a) {
  var seen = {};
  var out = [];
  var len = a.length;
  var j = 0;
  for (var i = 0; i < len; i++) {
    var item = a[i];
    if (seen[item] !== 1) {
      seen[item] = 1;
      out[j++] = item;
    }
  }
  return out.sort();
}
function prefix(array) {
    var rv = [];
    for (var i=0; i<array.length; i++) {
      rv[i] = "A" + array[i];
    }
    return rv;
}
function unprefix(array) {
    var rv = [];
    for (var i=0; i<array.length; i++) {
      rv[i] = array[i].substring(1);
    }
    return rv;
}
function getBadCodes(pastedArr, validatedArr) {
  var result = unprefix(sym(prefix(pastedArr), prefix(validatedArr)));
  return sort_uniq_fast(result);
}
   var parameter0 = ["33"];
   var parameter1 = new Array();
alert(getBadCodes(parameter0,parameter1));