Guest User

Untitled

a guest
Dec 15th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. import { NativeArray } from '../../lib/mixins/array';
  2. import { AbstractTestCase, moduleFor } from 'internal-test-helpers';
  3.  
  4. class ArrayPrototypeExtensionSelfReferenceTests extends AbstractTestCase {
  5. '@test should not create non-Symbol, enumerable properties that refer to itself'() {
  6. // Don't want to pollute Array.prototype so we make our own to extend
  7. class ThrowAwayArray extends Array {}
  8.  
  9. // Extend our throw-away prototype (like EXTEND_PROTOTYPES.Array would)
  10. NativeArray.apply(ThrowAwayArray.prototype);
  11.  
  12. // Create an instance to test
  13. let obj = new ThrowAwayArray();
  14.  
  15. // Home-made depth-first-search for cycle detection
  16. // Alternatively a module from npm (like 'is-circular' could be used instead)
  17. function isCyclic(node, path) {
  18. path = path || [];
  19. path.push(node);
  20. const root = path[0];
  21.  
  22. for (let p in node) {
  23. const value = node[p];
  24. if (value === root) {
  25. return path.concat(p);
  26. }
  27. else if (typeof value === 'object') {
  28. try {
  29. const result = isCyclic(value, path);
  30. if (result) {
  31. return result;
  32. }
  33. } catch(e) {
  34. return true;
  35. }
  36. }
  37. }
  38. return false;
  39. }
  40. // Make sure that no enumerable properties refer back to the object (creating a cyclic structure)
  41. const result = isCyclic(obj);
  42. this.assert.ok(
  43. result === false,
  44. `Detected a cyclic reference (${result}) on an enumerable part of the prototype.
  45. This is bad because it may break a lot of 3rd party code.
  46. For example, code that explores all properties,
  47. such as jQuery.extend and other "deep cloning" functions,
  48. will get stuck in an infinite loop.
  49. `.replace(/\s+/g, ' ')
  50. );
  51. }
  52. }
  53.  
  54. moduleFor(`NativeArray: apply`, ArrayPrototypeExtensionSelfReferenceTests);
Add Comment
Please, Sign In to add comment