Extra corner case for sort()

Paul Ruizendaal pnr at planet.nl
Mon Apr 15 02:04:09 PDT 2013


Still more corner cases to consider. I guess when faced with 'defined' properties, all of push/pop/shift/unshift and sort must fall back to a slow path that really performs the Get/Put/Delete operations as prescribed in the spec, without any optimizing shortcuts.

All browsers seem to fail on 'test_1' (making me suspicious that this test might be wrong), Chrome fails on 'test_2'.

The author of this code dedicates any and all copyright interest in this code to the public domain. I make this dedication for the benefit of the public at large and to the detriment of my heirs and successors. I intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this code under copyright law.

Paul

function test_1() {
  var a = [4,3,2];
  var b = 1;
  Object.defineProperty(Array.prototype,'3',{get:function(){return b},set:function(x){b=x}});
  a.length = 4;

  a.sort();
  if( a[0]!==1 || a[1]!==2 || a[2]!==3 ) return false;
  if( Object.getOwnPropertyDescriptor(a,'3')!==undefined ) return false;
  if( a[3]!==4 || b!==4 ) return false;
  return true;
}

function test_2() {
  function mka() {return this};
  mka.prototype.sort = Array.prototype.sort;
  var b = 1;
  var a = new mka();
  a[0]=4; a[1]=3; a[2]=2;
  a.length = 4;
  Object.defineProperty(mka.prototype,'3',{get:function(){return b},set:function(x){b=x}});

  a.sort();
  if( a[0]!==1 || a[1]!==2 || a[2]!==3 ) return false;
  if( Object.getOwnPropertyDescriptor(a,'3')!==undefined ) return false;
  if( a[3]!==4 || b!==4 ) return false;
  return true;
}
  


More information about the test262-discuss mailing list